Importing Existing X.509 Certificate and Private Key in Java Keystore
Java keystores provide secure storage for cryptographic keys and certificates. To use an existing X.509 certificate and private key in an SSL context, it is necessary to import them into the keystore.
Exporting to PKCS12 Intermediate File
Since Java keystore import only supports PKCS12 files, the first step involves converting the certificate and key pair to a PKCS12 file using OpenSSL:
openssl pkcs12 -export -in server.crt -inkey server.key \ -out server.p12 -name [some-alias] \ -CAfile ca.crt -caname root
Importing PKCS12 File into Java Keystore
With the PKCS12 file created, it can be imported into the Java keystore using the keytool command:
keytool -importkeystore \ -deststorepass [changeit] -destkeypass [changeit] -destkeystore server.keystore \ -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass some-password \ -alias [some-alias]
Additional Considerations
Troubleshooting: Keystore Password Error
If using OpenSSL 3.0 with a recent Java version and encountering the error "keystore password was incorrect," refer to the linked Stack Overflow answer for a possible solution.
The above is the detailed content of How to Import an Existing X.509 Certificate and Private Key into a Java Keystore?. For more information, please follow other related articles on the PHP Chinese website!