简体   繁体   English

如何验证 x509 证书的签名?

[英]How to verify the signature of a x509 certificate?

I have two X509Certificate objects x1 and x2.我有两个 X509Certificate 对象 x1 和 x2。

I want to verify that x2 was signed by x1.我想验证 x2 是否由 x1 签名。

I think this is done with the public key of x1 and the signature of x2.我认为这是通过 x1 的公钥和 x2 的签名完成的。

How to exactly do this?如何准确地做到这一点?

I also want to know if it is common practice to compare the issuer of x2 with the subject of x1 byte-by-byte and show an error if they differ.我还想知道将 x2 的发行者与 x1 的主题逐字节进行比较是否是常见的做法,如果它们不同则显示错误。

I found this 12456079 post but I can't figure it out.我找到了这个12456079帖子,但我无法弄清楚。

You are looking for certificate chain which is a common thing in PKI (Public Key Infrastructure).您正在寻找 PKI(公钥基础设施)中常见的证书链。 One certificate can sign another certificate to show that this certificate can be trusted.一个证书可以签署另一个证书以表明该证书是可以信任的。

In simple example there would be a Root certificate which is self signed and is trusted - everyone trusts this certificate.在简单的示例中,将有一个自签名且受信任的根证书 - 每个人都信任此证书。 Next you can ask the owner of this certificate to sign your certificate with Root's certificate private key.接下来,您可以要求此证书的所有者使用 Root 的证书私钥签署您的证书。 So if someone wants to use your certificate, he can check that your certificate was signed by Root certificate and if he trusts Root certificate - he can also trust you.因此,如果有人想使用您的证书,他可以检查您的证书是否由 Root 证书签名,如果他信任 Root 证书 - 他也可以信任您。

In Java you can check if a certificate was signed by the private key of corresponding certificate using something like this:在 Java 中,您可以使用以下内容检查证书是否由相应证书的私钥签名:

X509Certificate yourCert = ...
X509Certificate root = ...

try {
    yourCert.verify(root.getPublicKey()); } 
catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException e) {
    //handle wrong algos
} catch (SignatureException ex) {
    //signature validation error
}

The Certificate::verify serves this purpose: Certificate::verify用于此目的:

Verifies that this certificate was signed using the private key that corresponds to the specified public key.验证此证书是否使用与指定公钥对应的私钥签名。

Since X509Certificate extends Certificate you can use this method on X509Certificate implementations (since X509Certificate is an abstract class).由于X509Certificate extends Certificate您可以在X509Certificate实现上使用此方法(因为X509Certificate是一个abstract类)。

Also you can have a look at X509Certificate::verify(PublicKey, Provider) which takes PublicKey and Provider implementation.您还可以查看采用PublicKeyProvider实现的X509Certificate::verify(PublicKey, Provider)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM