【问题标题】:Creating a Key Pair Certificate and Signing It with External CA using BouncyCastle使用 BouncyCastle 创建密钥对证书并使用外部 CA 对其进行签名
【发布时间】:2014-02-17 23:14:11
【问题描述】:

到目前为止,我已经为用户生成了证书

    try {
        Security.addProvider(new BouncyCastleProvider()); // adding provider
                                                            // to
        String pathtoSave = "D://sureshtest.cer";

        KeyPair keyPair = generateKeypair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        X509Certificate trustCert = createCertificate(null, "CN=CompanyName",
                "CN=Owner", publicKey, privateKey);
        java.security.cert.Certificate[] outChain = { trustCert, };
        trustCert.checkValidity();
        KeyStore outStore = KeyStore.getInstance("PKCS12");
        outStore.load(null, null);
        outStore.setKeyEntry("my own certificate", privateKey,
                "admin123".toCharArray(), outChain);
        OutputStream outputStream = new FileOutputStream(pathtoSave);
        outStore.store(outputStream, "admin123".toCharArray());
        outputStream.flush();
        outputStream.close();


    } catch (Exception e) {
        e.printStackTrace();
    }

以上代码生成带有私钥和公钥的证书。

现在我想使用由证书颁发机构 (CA) 颁发的签名证书签署该证书。之后我会将该证书授予用户。

我收到了一些输入 from here,这似乎不是我的案例所需的答案。

不需要完整的实现,只需一个有效的过程或一些提示就会有很大帮助。

【问题讨论】:

  • 好的,它会生成证书——您需要帮助的具体是什么?
  • @chrylis 现在我想在颁发给用户之前签署该证书。
  • 太好了。您是在询问如何使用 BouncyCastle 编写 Java 代码来做到这一点,还是想了解如何将 CSR 提交给 CA?请注意,除非您正在实施密钥托管系统,否则 CA 生成密钥通常被认为是错误的形式,因为它不需要知道私钥。
  • 不向 CA 提交 CSR。我已经有一个有效的 CA 证书,使用该有效证书我想签署这个生成的证书。我在概念上是在错误的方向吗?还是做不到?
  • 链接的问题如何使它不适合您的情况?它正在执行您所询问的确切操作。

标签: java security encryption x509certificate bouncycastle


【解决方案1】:

您需要生成一个 CSR,以便可以从使用 BC API 的 Sign CSR using Bouncy Castle 调用代码。将此添加到上面的代码中:

        final PKCS10 request = new PKCS10(publicKey);
        final String sigAlgName = "SHA1WithRSA"; // change this to SHA1WithDSA if it's a DSA key
        final Signature signature = Signature.getInstance(sigAlgName);
        signature.initSign(privateKey);
        final X500Name subject = new X500Name(trustCert.getSubjectDN().toString());
        final X500Signer signer = new X500Signer(signature, subject);

        // Sign the request and base-64 encode it
        request.encodeAndSign(signer);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final PrintStream writer = new PrintStream(baos);
        request.print(writer);
        // Remove -----BEGIN NEW CERTIFICATE REQUEST----- and -----END NEW CERTIFICATE REQUEST-----
        final String requestBase64 = new String(baos.toByteArray());
        String withoutTags = requestBase64.substring(41);
        withoutTags = withoutTags.substring(0, withoutTags.length() - 39);

        // org.bouncycastle.pkcs.PKCS10CertificationRequestHolder
        final PKCS10CertificationRequest holder = new PKCS10CertificationRequest(Base64.decode(withoutTags));
        // Feed this into https://stackoverflow.com/questions/7230330/sign-csr-using-bouncy-castle

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 2020-11-25
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2019-04-30
    • 2020-01-02
    • 2019-11-30
    • 1970-01-01
    相关资源
    最近更新 更多