[英]Golang: verify x509 certificate was signed using private key that corresponds to the specified public key
我希望验证X509证书以确保它是由与公钥对应的私钥签名的:
var publicKey *rsa.PublicKey = getPublicKey()
var certificate *x509.Certificate = getCertificate()
certificate.CheckSignature(...)
在我看来, certificate.CheckSignature
方法是正确的方法,但我无法弄清楚它需要的参数,并希望得到社区的帮助。
顺便说一句,我能够在java中做同样的事情(处理两个相邻的项目)。 它看起来像这样:
RSAPublicKey publicKey = getPublicKey();
X509Certificate certificate = X509CertUtils.parse(...);
// Verifies that this certificate was signed using the
// private key that corresponds to the specified public key.
certificate.verify(publicKey);
我很欣赏这个领域的任何暗示! P.
如果我理解你想要做什么,那么回答很简单。
您的证书包含公钥。 因此,您只需要将公钥与证书中的公钥进行比较。 代码如下:
if certificate.PublicKey.(*rsa.PublicKey).N.Cmp(publicKey.(*rsa.PublicKey).N) == 0 && publicKey.(*rsa.PublicKey).E == certificate.PublicKey.(*rsa.PublicKey).E {
println("Same key")
} else {
println("Different keys")
}
更新
刚刚检查了OpenJDK 实现的.verify
方法 。 看起来可能存在证书不包含公钥并且您确实需要验证签名的情况。 这种情况的Go代码如下所示:
h := sha256.New()
h.Write(certificate.RawTBSCertificate)
hash_data := h.Sum(nil)
err = rsa.VerifyPKCS1v15(publicKey.(*rsa.PublicKey), crypto.SHA256, hash_data, certificate.Signature)
if err != nil {
println("Signature does not match")
}
谢谢Roman,我设法让它像这样工作:
hash := sha1.New()
hash.Write(certificate.RawTBSCertificate)
hashData := hash.Sum(nil)
rsa.VerifyPKCS1v15(dsPublicKey, crypto.SHA1, hashData, certificate.Signature)
所以,这基本上是你推荐的,但是使用了sha1哈希 - 这就是我在本地生成的证书所获得的。 我还设法通过临时交换证书的公钥以及我想要验证的密钥来使其工作:
certificate.PublicKey = certificateAuthorityPublicKey
certificate.CheckSignature(x509.SHA1WithRSA, certificate.RawTBSCertificate, certificate.Signature)
第二种方法看起来很糟糕,但它们都按预期工作......
想知道是否可以在运行时确定它是SHA1还是SHA256?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.