Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

bouncycastle - How to create a .crt store file containing a certificate chain from a PKCS12 one?

I have a PKCS12 file containing a certificate chain and a private key. I would like to use BouncyCastle to create a CRT file with that cert chain, the same way we can do using OpenSSL command-line tool:

openssl pkcs12 -in [yourfilename.pfx] -clcerts -nokeys -out [certificatename.crt]

I was already able to load the pkcs12 Keystore and obtain the certificates:

Certificate[] certs = pKeyStore.getCertificateChain(pAlias);

But I wasn't able to find a store builder for a CRT file that could accept the array of certificates above...

question from:https://stackoverflow.com/questions/65903364/how-to-create-a-crt-store-file-containing-a-certificate-chain-from-a-pkcs12-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The class you are looking for is org.bouncycastle.openssl.jcajce.JcaPEMWriter in bcpkix. You can use its write method to encode all sorts of JCE interfaces (X509Certificate, X509CRL, PublicKey, PrivateKey, KeyPair) to a PEM file.

In your case:

final JcaPEMWriter pemWriter = new JcaPEMWriter(System.out);
for (final Certificate cert : certs) {
    pemWriter.write(cert);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...