Question

Is it possible to regenerate a private key in a .JKS keystore using keytool or equivalent?

I've been supplied with a certificate and a JKS keystore, but on importing the cert it looks like the private key that was used to generate the CSR has been deleted.

I can see how to create a new keystore with a new private key, but this won't then match the CSR or certificate.

Was it helpful?

Solution

No, that's the whole point of asymmetric cryptography: making it impossible to produce the private key when knowing only the public key (which is contained in the CSR and in the certificate).

If you could re-generate the private key only from the CSR or the certificate, anyone could impersonate the entity to which the certificate has been issued.

If you've lost your private key, you'll simply have to create a new key-pair, submit a new CSR and get a new certificate. Some CAs allow this sort of re-keying for free as part of their contract within the duration of the initial certificate.


EDIT: Just to clarify what a CSR is.

To apply for an X.509 certificate, you must:

  • Generate a public/private key pair. By nature, the public key can be publicly distributed, because it's not sufficient to obtain the private key (at least not in a reasonable time).
  • Submit that public key with your identity information to the Certification Authority. This can be done using:
    • A Certificate Signing Request (CSR, PKCS#10), which contains your public key and the data you would like to be in the certificate (e.g. the Subject DN you want). Although very similar to the data in a certificate, this data should mainly be used by the CA for identifying the request itself, since CAs doing their job properly should check what they put in the certificate, not just turn the CSR into a certificate blindly. The CSR itself is signed using the private key matching the public key in the certificate. It's effectively very similar to a self-signed X.509 certificate (without Issuer information and validity dates), but isn't one.
    • SPKAC or CRMF for in-browser certificate applications.
  • The CA then takes this CSR (or equivalent) and makes the necessary verification outside this process to vet the pieces of information it wants to embed in your certificate, and issues your certificate (by signing it with its own private key). It's effectively vouching for the binding between your public key (extracted from the CSR) and the information it embeds in the certificate (e.g. your name and/or the domain name for which this cert is). It sends you back this certificate.

You then have to use this certificate in conjunction with the private key matching its public key. Some tools do this using separate files, it's also possible to import the cert back against the private key entry in a keystore.

Having the CSR or the cert without the private key is of no use. You can quite easily create a new CSR again, but you'll also need to create a new key pair. The most important part of the CSR is the public key, and to have the matching private key. You can discard the CSR otherwise.

OTHER TIPS

Is it possible to regenerate a private key in a .JKS keystore using keytool or equivalent?

Yes, but regenerate the private key and CSR. The CSR is submitted to the CA where you are provided a new public key.

You can reimport to the keystore anytime using the following command:

keytool.exe" -import -keystore "%JAVA_HOME%\jre\lib\security\cacerts" -file .\certificate.cer

Make sure you also import the certificate to both paths for the newer JDK releases:

C:\Program Files\Java\jdk1.6.0_31

The newer releases also deploy a separate JRE:

C:\Program Files\Java\jre6

Failure to do so may result in the following exceptions in log:

Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

To regenerate your private key and CSR, you can use the following command:

$ openssl req -new -newkey rsa:2048  -nodes -keyout private.key -out signing request.csr -config openssl.conf

I had the same trouble (my private key was accidentally deleted from keystore) and there was just one way to recover it: replacing the keystore file (*.jks) with a backup. So I recommend to always make backup with all files related to SSL, and if you delete anything by mistake on keystore just replace the file with an older one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top