亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 go 中比較客戶端證書

在 go 中比較客戶端證書

Go
慕田峪7331174 2023-07-17 16:28:46
我的用例看起來像我知道客戶的公共證書并且只想允許它們。我有一個基于 gin 的 go 服務器和一個 TLS 配置,其中我為屬性“VerifyPeerCertificate”分配了一個方法。該函數看起來像func customVerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {if len(verifiedChains) < 1 {    return errors.New("Verified certificate chains is empty.")}if len(verifiedChains[0]) < 1 {    return errors.New("No certificates in certificate chains.")}if len(verifiedChains[0][0].Subject.CommonName) < 1 {    return errors.New("Common name can not be empty.")}fmt.Println(verifiedChains[0][0].Raw)publicKeyDer, _ := x509.MarshalPKIXPublicKey(verifiedChains[0][0].PublicKey)publicKeyBlock := pem.Block{    Type:  "CERTIFICATE",    Bytes: publicKeyDer,}publicKeyPem := string(pem.EncodeToMemory(&publicKeyBlock))}然而,問題是變量“publicKeyPem”中的字符串看起來不像我用來向服務器發送請求的客戶端公共證書,它的長度也較短。
查看完整描述

2 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

證書不僅僅是它的公鑰。整個x509.Certificate對象代表了客戶端提交的證書,公鑰字段只是公鑰的實際值。

如果您想比較證書的嚴格相等性,您應該使用rawCerts [][]byte傳遞給回調的參數。這在tls.Config注釋中提到VerifyPeerCertificate

VerifyPeerCertificate,?if?not?nil,?is?called?after?normal
????certificate?verification?by?either?a?TLS?client?or?server.?It
????receives?the?raw?ASN.1?certificates?provided?by?the?peer?and?also
????any?verified?chains?that?normal?processing?found.?If?it?returns?a
????non-nil?error,?the?handshake?is?aborted?and?that?error?results.


查看完整回答
反對 回復 2023-07-17
?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

我知道我使用了錯誤的變量。要將證書轉換為客戶端使用的字符串,請使用以下代碼


publicKeyBlock := pem.Block{

  Type:  "CERTIFICATE",

  Bytes: rawCerts[0],

}

publicKeyPem := string(pem.EncodeToMemory(&publicKeyBlock))


查看完整回答
反對 回復 2023-07-17
  • 2 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號