javassljks

How to import an existing X.509 certificate and private key in Java keystore to use in SSL?


I have a pair of X.509 cert and a key file.

How do I import those two in a single keystore? All examples I could Google always generate the key themselves, but I already have a key.

I have tried:

keytool -import  -keystore ./broker.ks -file mycert.crt

However, this only imports the certificate and not the key file. I have tried concatenating the cert and the key but got the same result.

How do I import the key?


Solution

  • Believe or not, keytool does not provide such basic functionality like importing private key to keystore. You can try this workaround with merging PKSC12 file with private key to a keystore:

    keytool -importkeystore \
      -deststorepass storepassword \
      -destkeypass keypassword \
      -destkeystore my-keystore.jks \
      -srckeystore cert-and-key.p12 \
      -srcstoretype PKCS12 \
      -srcstorepass p12password \
      -alias 1
    

    Or just use more user-friendly KeyMan from IBM for keystore handling instead of keytool.