使用BouncyCastle签名X509证书-无效的数字签名 [英] Signing X509 Certs w/BouncyCastle - invalid digital signature

查看:682
本文介绍了使用BouncyCastle签名X509证书-无效的数字签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

生成的已签名X.509客户端证书无效(没有指向其CA的证书链)

我按照以下示例操作:

http://www.bouncycastle.org/ wiki / display / JA1 / X.509 + Public + Key + Certificate + and + Certification + Request + Generation

但是生成的已签名客户端证书具有在Windows中打开时出现以下错误:

But the resulting signed client certificate has the following error when opened in windows:

此文件无法用作以下文件:安全证书

"This file is invalid for use as the following: Security Certificate"

如果我仍然安装它并使用certmgr查看,则证书路径看起来不错-我看到我的自签名证书颁发机构(whic h很好,没有问题),但是客户端证书具有以下状态:

If I install it anyway and view it with certmgr, the certification path looks OK - I see my self-signed Certificate Authority (which is fine, no problems there) but the client cert has the following status:

此证书的数字签名无效。

"This certificate has an invalid digital signature."

如果我调用X509Certificate.Verify(),则会引发以下异常:

If I call X509Certificate.Verify() it throws the following exception:

公开密钥不用于证书签名

"Public key presented not for certificate signature"

但是我使用的是从Pkcs10CertificationRequest中提取的完全相同的公钥,当我调用Verify()就可以了。

Yet I'm using the same exact public key extracted from the Pkcs10CertificationRequest and when I called Verify() on that it's fine.

有什么想法吗?经过几天的努力,除了最后一个,我已经完成了所有工作-真正令人困惑的是,我自己签名的CA证书还不错。客户端证书只是发生了一些事情。这是整个代码块:

Any ideas? After days of struggling through this, I've got all the pieces working except this last one - and what's really confusing is that my self-signed CA cert is fine. There's just something going on with the client cert. Here's the entire block of code:

        TextReader textReader = new StreamReader("certificaterequest.pkcs10");
        PemReader pemReader = new PemReader(textReader);

        Pkcs10CertificationRequest certificationRequest = (Pkcs10CertificationRequest)pemReader.ReadObject();
        CertificationRequestInfo certificationRequestInfo = certificationRequest.GetCertificationRequestInfo();
        SubjectPublicKeyInfo publicKeyInfo = certificationRequestInfo.SubjectPublicKeyInfo;

        RsaPublicKeyStructure publicKeyStructure = RsaPublicKeyStructure.GetInstance(publicKeyInfo.GetPublicKey());

        RsaKeyParameters publicKey = new RsaKeyParameters(false, publicKeyStructure.Modulus, publicKeyStructure.PublicExponent);

        bool certIsOK = certificationRequest.Verify(publicKey);
        // public key is OK here...

        // get the server certificate
        Org.BouncyCastle.X509.X509Certificate serverCertificate = DotNetUtilities.FromX509Certificate(System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile("servermastercertificate.cer"));

        // get the server private key
        byte[] privateKeyBytes = File.ReadAllBytes("serverprivate.key");
        AsymmetricKeyParameter serverPrivateKey = PrivateKeyFactory.CreateKey(privateKeyBytes);

        // generate the client certificate
        X509V3CertificateGenerator generator = new X509V3CertificateGenerator();

        generator.SetSerialNumber(BigInteger.ProbablePrime(120, new Random()));
        generator.SetIssuerDN(serverCertificate.SubjectDN);
        generator.SetNotBefore(DateTime.Now);
        generator.SetNotAfter(DateTime.Now.AddYears(5));
        generator.SetSubjectDN(certificationRequestInfo.Subject);
        generator.SetPublicKey(publicKey);
        generator.SetSignatureAlgorithm("SHA512withRSA");
        generator.AddExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(serverCertificate));
        generator.AddExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(publicKey));

        var newClientCert = generator.Generate(serverPrivateKey);

        newClientCert.Verify(publicKey); // <-- this blows up

        return DotNetUtilities.ToX509Certificate(newClientCert).Export(X509ContentType.Pkcs12, "user password");


推荐答案

我知道了。如果调用 X509Certificate.Verify(publicKey),则必须从 Pkcs10CertificationRequest

I figured this out. If you call X509Certificate.Verify(publicKey) you have to pass the CA's public key, not the client's public key from the Pkcs10CertificationRequest.

这篇关于使用BouncyCastle签名X509证书-无效的数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆