Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random certificates

Tags:

java

I am looking for a utility class that can generate random certificate strings for testing purposes. Any idea if there is one already implemented?

like image 318
Joly Avatar asked Mar 20 '12 13:03

Joly


1 Answers

To add to solution given by martijno,

Instead of writing your own content signer, JCAContentSigner can be used to avoid mappings to AlgorithmIdentifier (i.e. OID).

JcaContentSignerBuilder takes algorithm names as defined here.

X509v3CertificateBuilder certBuilder = new X509v3CertificateBuilder(issuer, serialNumber, startDate, expiryDate, subject, SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded()));
JcaContentSignerBuilder builder = new JcaContentSignerBuilder("SHA256withRSA");
ContentSigner signer = builder.build(keyPair.getPrivate());

byte[] certBytes = certBuilder.build(signer).getEncoded();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)certificateFactory.generateCertificate(new ByteArrayInputStream(certBytes));
like image 67
Camath Avatar answered Sep 20 '22 23:09

Camath