搜尋
首頁後端開發Golang在 golang 中產生期間向 CSR 提供​​ ExtendedKeyUsage 資訊時出現問題

在 golang 中生成期间向 CSR 提供​​ ExtendedKeyUsage 信息时出现问题

在golang中產生CSR期間,出現了向ExtendedKeyUsage提供資訊的問題。這個問題可能會影響到產生的CSR的正確性和有效性。 ExtendedKeyUsage是憑證擴充的一部分,用於指定憑證的使用目的。透過向CSR提供正確的ExtendedKeyUsage信息,可以確保證書在特定場景下的可用性和安全性。然而,在golang中,有時會遇到產生CSR時無法正確提供ExtendedKeyUsage資訊的問題。 php小編西瓜將在本文中詳細介紹這個問題的原因和解決方案,幫助讀者解決類似的問題並順利產生符合要求的CSR。

問題內容

我偶然發現了一個奇怪的問題。 我正在編寫一個小型 golang 工具,它根據一些用戶提供的輸入產生 csr 。我基本上成功地實現了目標,但在 extendedkeyusage 方面遇到了問題。簡單地說,它不起作用。

一些 x509 欄位的 asn1 編組程式碼:

var oidemailaddress = asn1.objectidentifier{1, 2, 840, 113549, 1, 9, 1}
    var oidextensionkeyusage = asn1.objectidentifier{2, 5, 29, 15}
    var oidextensionextendedkeyusage = asn1.objectidentifier{2, 5, 29, 37}

    asn1keyusagedigsig, err := asn1.marshal(asn1.bitstring{
        bytes:     []byte{byte(x509.keyusagedigitalsignature)},
        bitlength: 8,
    })
    asn1keyusagedatenc, err := asn1.marshal(asn1.bitstring{
        bytes:     []byte{byte(x509.keyusagedataencipherment)},
        bitlength: 8,
    })
    asn1keyusagecauth, err := asn1.marshal(asn1.bitstring{
        bytes:     []byte{byte(x509.extkeyusageclientauth)},
        bitlength: 8,
    })

    if err != nil {
        error.fatalf("can't serialize extended key usage %s", err)
    }

然後我創建了一個模板並成功生成並保存了csr,差不多了:

template := x509.certificaterequest{
        rawsubject:         asn1subj,
        emailaddresses:     []string{emailaddress},
        signaturealgorithm: _sigalg,
        extraextensions: []pkix.extension{
            {
                id: oidextensionextendedkeyusage,
                value: asn1keyusagecauth,
            },
            {
                id:       oidextensionkeyusage,
                critical: true,
                value:    asn1keyusagedatenc,
            },
            {
                id:       oidextensionkeyusage,
                critical: true,
                value:    asn1keyusagedigsig,
            },
        },
    }

    csrbytes, _ := x509.createcertificaterequest(rand.reader, &template, privatekey)
and here is an openssl req -in my_output.csr -text -noout
******
         asn1 oid: prime256v1
                nist curve: p-256
        attributes:
        requested extensions:
            x509v3 subject alternative name: 
                email:[email protected]
            x509v3 extended key usage: 
                ....
            x509v3 key usage: critical
                key agreement
            x509v3 key usage: critical
                encipher only
    signature algorithm: ecdsa-with-sha256

******

我的extendedkeyusage是空的,而它應該是clientauthentication。我做錯了什麼?

我期待看到:

X509v3 Extended Key Usage: ClientAuthentication

我看到的是空白欄位。我嘗試使用來自另一個 oid 的不同位元組集,但仍然沒有任何結果。就好像 extendedkeyusage 欄位不允許寫入任何內容(雖然它應該)

如果是導入的:

版本:go1.19.3 darwin/amd64

解決方法

我認為問題出在列印資料時。鍵/值實際上存在於資料中。

從程式碼:

var oidextensionextendedkeyusage = asn1.objectidentifier{2, 5, 29, 37}

asn1keyusagecauth, err := asn1.marshal(asn1.bitstring{
    bytes:     []byte{byte(x509.extkeyusageclientauth)},
    bitlength: 8,
})

extraextensions: []pkix.extension{
    {
        id: oidextensionextendedkeyusage,
        //critical: true,
        value: asn1keyusagecauth,
        //value: {2, 5, 29, 15},
    },

oidextensionextendedkeyusage 為 asn.1 oid 2.5.29.37,使用 der 編碼器編碼時將為「55 1d 25」

您可以在線對其進行編碼,以查看它將生成什麼二進位(例如 https://www.php.cn/link/8e08227323cd829e449559bb381484b7)

asn1keyusagecauth 值為2(在x509.go 中定義的常數),當使用der 編碼器編碼為asn.1 bit string 時將為“00 02”(第一個00 是填充位數(無), 02 是填充位數)值2)

現在取得憑證要求的 base64 值並使用 asn.1 der 解碼器對其進行解碼(例如:https: //asn1.io/asn1playground)

miibtzccav0caqawgzwxczajbgnvbaytakfvmq8wdqydvqqiewztewruzxkxdzan bgnvbactbln5zg5letetmbega1uechmkc210aensawvuddelmakga1uecxmcsvqx jtajbgnvbamthhntdggtq2xpzw50lvk4cdg1bk1psvnzmgliz0exijagbgkqhkig 9w0bcqeme3ntdghjbgllbnrac210ac5jb20wwtatbgcqhkjopqibbggqhkjopqmb bwncaar4riguoxsyxdaml9f9e2grjumuk8q0jilotb2kadmbz1rocedszuuxkqcr 0vud2aw3vidph1ar4hkqwkm43hxqof4wxayjkozihvcnaqkomu8wttaebgnvhree fzavgrnzbxroy2xpzw50qhntdgguy29tmasga1udjqqeawiiaajaobgnvhq8baf8e bamcaagwdgydvr0paqh/baqdagabmaogccqgsm49bamca0gameuciqdtbj 0atjy f1gy8am2mv7/x3tsebmmvdszkw8l6rvseqigmih8co9nkp0axdmgp9x4kvjjzk9x rw3roydt89d73oa=

#
try the full power of oss' asn-1step by downloading a free trial

oss nokalva tlv print utility  version 8.6.1
copyright (c) 1997-2022 oss nokalva, inc.  all rights reserved.


30 8201b7(439)
  30 82015d(349)
    02 01 00
    30 819c(156)
      31 0b
        30 09
          06 03 550406
          13 02 4155
      31 0f
        30 0d
          06 03 550408
          13 06 5379646e6579
      31 0f
        30 0d
          06 03 550407
          13 06 5379646e6579
      31 13
        30 11
          06 03 55040a
          13 0a 736d7468436c69656e74
      31 0b
        30 09
          06 03 55040b
          13 02 4954
      31 25
        30 23
          06 03 550403
          13 1c 736d74682d436c69656e742d59387038356e4d694953733069486741
      31 22
        30 20
          06 09 2a864886f70d010901
          0c 13 736d7468636c69656e7440736d74682e636f6d
    30 59
      30 13
        06 07 2a8648ce3d0201
        06 08 2a8648ce3d030107
      03 42 000478ac88143b14b25dd68c2fd17d7b68118ee3142bc4348e29684dbda401d9...
    a0 5e
      30 5c
        06 09 2a864886f70d01090e
        31 4f
          30 4d
            30 1e
              06 03 551d11
              04 17 30158113736d7468636c69656e7440736d74682e636f6d
            30 0b -- here it is!
              06 03 551d25
              04 04 03020002
            30 0e
              06 03 551d0f
              01 01 ff
              04 04 03020008
            30 0e
              06 03 551d0f
              01 01 ff
              04 04 03020001
  30 0a
    06 08 2a8648ce3d040302
  03 48 003045022100d3063fb402d8f2175198f0033632feff5f7b6c11b98c55db332b0f25...


results
to get more details, please provide/compile a schema for your data.

向下捲動上面的輸出,直到找到 here it is!

您的鍵/值是:

30 0B    -- a SEQUENCE of 11 bytes
    06 03 551D25   -- an item of 3 bytes (551D25 ... OidExtensionExtendedKeyUsage)
    04 04 03020002 --  an item of 4 bytes (03 02 0002 ... an item of 2 bytes 0002 ... asn1KeyUsageCAuth)

我很想根據 asn.1 規範解碼 csr ...但我找不到它:(

以上是在 golang 中產生期間向 CSR 提供​​ ExtendedKeyUsage 資訊時出現問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除
使用GO編程語言構建可擴展系統使用GO編程語言構建可擴展系統Apr 25, 2025 am 12:19 AM

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建築物內currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

有效地使用Init功能的最佳實踐有效地使用Init功能的最佳實踐Apr 25, 2025 am 12:18 AM

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用輔助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

INIT函數在GO軟件包中的執行順序INIT函數在GO軟件包中的執行順序Apr 25, 2025 am 12:14 AM

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization

在GO中定義和使用自定義接口在GO中定義和使用自定義接口Apr 25, 2025 am 12:09 AM

CustomInterfacesingoarecrucialforwritingFlexible,可維護,andTestableCode.TheyEnableDevelostOverostOcusonBehaviorBeiroveration,增強ModularityAndRobustness.byDefiningMethodSigntulSignatulSigntulSignTypaterSignTyperesthattypesmustemmustemmustemmustemplement,InterfaceSallowForCodeRepodEreusaperia

在GO中使用接口進行模擬和測試在GO中使用接口進行模擬和測試Apr 25, 2025 am 12:07 AM

使用接口進行模擬和測試的原因是:接口允許定義合同而不指定實現方式,使得測試更加隔離和易於維護。 1)接口的隱式實現使創建模擬對像變得簡單,這些對像在測試中可以替代真實實現。 2)使用接口可以輕鬆地在單元測試中替換服務的真實實現,降低測試複雜性和時間。 3)接口提供的靈活性使得可以為不同測試用例更改模擬行為。 4)接口有助於從一開始就設計可測試的代碼,提高代碼的模塊化和可維護性。

在GO中使用init進行包裝初始化在GO中使用init進行包裝初始化Apr 24, 2025 pm 06:25 PM

在Go中,init函數用於包初始化。 1)init函數在包初始化時自動調用,適用於初始化全局變量、設置連接和加載配置文件。 2)可以有多個init函數,按文件順序執行。 3)使用時需考慮執行順序、測試難度和性能影響。 4)建議減少副作用、使用依賴注入和延遲初始化以優化init函數的使用。

GO的選擇語句:多路復用並發操作GO的選擇語句:多路復用並發操作Apr 24, 2025 pm 05:21 PM

go'SselectStatementTreamLinesConcurrentProgrambyMultiplexingOperations.1)itallowSwaitingOnMultipleChannEloperations,執行thefirstreadyone.2)theDefirstreadyone.2)thedefefcasepreventlocksbysbysbysbysbysbythoplocktrograpraproxrograpraprocrecrecectefnoopeready.3)

GO中的高級並發技術:上下文和候補組GO中的高級並發技術:上下文和候補組Apr 24, 2025 pm 05:09 PM

contextancandwaitgroupsarecrucialingoformanaginggoroutineseflect.1)context contextsallowsAllowsAllowsAllowsAllowsAllingCancellationAndDeadLinesAcrossapibiboundaries,確保GoroutinesCanbestoppedGrace.2)WaitGroupsSynChronizeGoroutines,確保Allimizegoroutines,確保AllizeNizeGoROutines,確保AllimizeGoroutines

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境