简体   繁体   English

我的代码可以验证自签名证书,但是使用VeriSign颁发的证书时失败

[英]My code can verify self signed certificate but is failing when VeriSign issued certificate is used

My application is using java security APIs to sign a file and verify it. 我的应用程序使用Java安全性API对文件进行签名并进行验证。 While signing , I am using PFX file and password as inputs and after signing I am generating a signature file using the bytes. 签名时,我使用PFX文件和密码作为输入,签名后,我使用字节生成签名文件。 While verification process I am using signature file ,certificate file and the signed file as inputs. 在验证过程中,我使用签名文件,证书文件和签名文件作为输入。 Please find the code I am using in verification below: 请在下面找到我在验证中使用的代码:

 // KeyFilePath= path of certificate file
 // fileToVerify = path of signed file
 // signatureFilePath = path of signature file



 InputStream inputStream = new FileInputStream(KeyFilePath);
 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
 X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream);

 // input the signature bytes
 String sigFile = signatureFilePath;

 FileInputStream sigFileInputStream = new FileInputStream(sigFile);
 byte[] sigToVerify = new byte[sigFileInputStream.available()];
 sigFileInputStream.read(sigToVerify);
 sigFileInputStream.close();

 PublicKey pubKey = x509Certificate.getPublicKey();
 Signature signature = Signature.getInstance(signAlgorithm);

 signature.initVerify(pubKey);

 // Update and verify the data
 try {
    FileInputStream dataFileInputStream = new FileInputStream(fileToVerify);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(dataFileInputStream);

    byte[] buffer = new byte[IVerifyDigitalSignature.BYTE_SIZE];
    int bufferedInputStreamLength;

    while (bufferedInputStream.available() != IVerifyDigitalSignature.ZERO_LENGTH) {
        bufferedInputStreamLength = bufferedInputStream.read(buffer);
        signature.update(buffer, IVerifyDigitalSignature.ZERO_LENGTH, bufferedInputStreamLength);
    }

    bufferedInputStream.close();

    // Verify the Signature
    x509Certificate.verify(pubKey);
    verifyDigitalSignature = signature.verify(sigToVerify);

Please help me in resolving the same as it is yet not closed. 请帮助我解决尚未解决的问题。

If you want to do this yourself, yes you must iterate over the certs in the chain from a trust anchor to your desired cert, however long that it is (it may vary for different CAs, classes, and at different times). 如果您想自己执行此操作,是的,您必须遍历链中的证书,从信任锚到所需的证书,无论证书有多长(可能因不同的CA,类和在不同的时间而异)。 Verifying the signature on each "child" (lower level) cert using the publickey from the "parent" (next higher) cert is only a fairly small part of this; 使用来自“父”(下一个)证书的公钥来验证每个“子”(下级)证书的签名只是其中的一小部分; there are many other steps needed. 还有许多其他步骤。 Often just finding the right certs can be an issue; 通常,仅仅找到合适的证书可能是一个问题。 if you already have a correct chain you have a head start. 如果您已经有了正确的链条,那么您将拥有一个良好的开端。 But are you sure you have "the" right chain? 但是,您确定您拥有“正确的”链条吗? Frequently there are several possible chains for a given cert, and sometimes some of them are valid but others have expired or become unverifiable. 通常,给定证书有几种可能的链,有时其中一些是有效的,但另一些已过期或变得不可验证。 Verisign in particular issued I believe all recent certs under their G5 root but provided an alternate path back to (effectively) G1 for reliers that aren't up to date, and sometimes can't be updated. 我相信Verisign特别发布了,我相信所有最新的证书都在其G5根目录下,但是为不是最新的(有时无法更新)的标准版提供了(有效)回到G1的替代途径。

The algorithm for most situations is defined in "PKIX" RFC5280 , except that OCSP RFC6960 instead of CRL for revocation is becoming more common. 在大多数情况下,该算法在“ PKIX” RFC5280中进行了定义,但用于撤销的OCSP RFC6960而非CRL变得更加普遍。 You might get away with omitting cross-hierarchy and NameConstraints, which AFAIK aren't actually used by public CAs like Verisign, and the Policy stuff which CAs do use but users/reliers don't care about. 您可能会忽略交叉层次结构和NameConstraints,而像Verisign这样的公共CA并没有真正使用AFAIK,而CA确实使用了但用户/用户并不关心的Policy东西。 https://security.stackexchange.com/questions/37409/certificate-chain-checking has a good but not complete introduction. https://security.stackexchange.com/questions/37409/certificate-chain-checking有一个不错的但不完整的介绍。

But you're probably better off using Java's (really JCE's) CertPathValidator for "PKIX" -- and if needed CertPathBuilder -- to which I already referred you. 但是您最好使用Java(实际上是JCE)的CertPathValidator作为“ PKIX” ,如果需要,还可以使用CertPathBuilder(我已经提到过)。 This has already been written and tested by experts. 这已经由专家编写和测试。 Just calling it is still a little bit complicated, but nowhere near as complicated as rewriting all the things it does. 仅仅调用它仍然有点复杂,但是没有什么比重写它所做的所有事情复杂的了。

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

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