Search code examples
javax509certificatebouncycastlex509

Get PrivateKey from X509Certificate


Here is my code to generate a SSL certificate using Let's Encrypt (based on https://github.com/zero11it/acme-client):

AcmeChallengeListener challengeListener = new HttpChallengeListener(sessionToken, appObjectId, userId, domains[0], "");
Acme acme = new Acme(CA_STAGING_URL, new DefaultCertificateStorage(true), true, true);
X509Certificate cert = acme.getCertificate(domains, AGREEMENT_URL, contacts, challengeListener);
String fullchain = CertificateHelper.x509ToBase64PEMString(cert);

Is there a way to get the PrivateKey and also get it as String with the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY-----? So it can be uploaded by our app to our NGINX server.


Solution

  • The solution was to modify the library and expose the certificate storage like this:

    public CertificateStorage getCertificateStorage() { return certificateStorage; }

    From here the app can get the PrivateKey