想根據發行鏈驗證 PEM 證書,該發行鏈也是一個.pem包含多個由換行符分隔的證書的文件,如本要點所示,https://gist.github.com/kurtpeek/8bf3282e344c781a20c5deadac75059f。我已經嘗試過Certpool.AppendCertsFromPEM如下:package mainimport ( "crypto/x509" "encoding/pem" "io/ioutil" "github.com/sirupsen/logrus")func main() { caCertPEM, err := ioutil.ReadFile("issuing_chain.pem") if err != nil { logrus.WithError(err).Fatal("read CA PEM file") } certPEM, err := ioutil.ReadFile("3007e750-e769-440b-9075-41dc2b5b1787.pem") if err != nil { logrus.WithError(err).Fatal("read cert PEM file") } block, rest := pem.Decode(certPEM) if block == nil { logrus.WithField("rest", rest).Fatal("Decode CA PEM") } cert, err := x509.ParseCertificate(block.Bytes) if err != nil { logrus.WithError(err).Fatal("parse certificate") } roots := x509.NewCertPool() roots.AppendCertsFromPEM(caCertPEM) chain, err := cert.Verify(x509.VerifyOptions{Roots: roots}) if err != nil { logrus.WithError(err).Fatal("failed to verify cert") } logrus.Infof("issuing chain: %+v", chain)}但是,如果我運行它,我會收到以下錯誤:FATA[0000] failed to verify cert error="x509: certificate specifies an incompatible key usage"exit status 1我相信此錯誤是在https://golang.org/src/crypto/x509/verify.go的第 790 行返回的:if len(chains) == 0 { return nil, CertificateInvalidError{c, IncompatibleUsage, ""}}換句話說,該Verify()方法無法chains根據提供的選項構建任何內容。我嘗試將中間體(issuing_chain.pem要點中顯示的前兩個)拆分為一個單獨的 PEM 文件并將其添加到Intermediates中x509.VerifyOptions,但我仍然遇到相同的錯誤。在 Go 中根據發行鏈驗證證書的正確方法是什么?
1 回答

慕絲7291255
TA貢獻1859條經驗 獲得超6個贊
您的葉證書僅用于客戶端身份驗證。
$ openssl x509 -noout -text -in leaf.pem | grep -A1 'Key Usage'
X509v3 Key Usage: critical
Digital Signature, Key Encipherment
X509v3 Extended Key Usage:
TLS Web Client Authentication
如果這是故意的,則必須指定 KeyUsages 選項,因為“空列表意味著 ExtKeyUsageServerAuth”。您還必須返回到單獨提供中間證書的代碼版本:
chain, err := cert.Verify(x509.VerifyOptions{
Roots: roots,
Intermediates: inters,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
})
在操場上嘗試一下:https ://play.golang.org/p/1BNLthzu5Tz 。請注意,playground 需要 CurrentTime 選項才能正確驗證。復制到其他地方時刪除它!
- 1 回答
- 0 關注
- 149 瀏覽
添加回答
舉報
0/150
提交
取消