Extracting a Private Key From the Java Keystore (JKS)
Check this out if you need help extracting a private key from a JKS instance.
Join the DZone community and get the full member experience.
Join For FreeI’ve been working with the AS2 Protocol and the AdroitLogic AS2Gateway for quite some time now, and hence, playing with JKS has been a must. One of the tricks that were required from time to time was extracting the private key and public key (certificate) from Java KeyStores. In this blog post, we’ll go through a couple of simple commands on how to do that.
What Is a Java KeyStore (JKS)?
A JKS is an encrypted security file used to store a set of cryptographic keys or certificates in the binary format, and it requires a password to be opened. JKS files are used for a variety of security purposes. They can be used to identify the author of an Android app during a build and when publishing to Android Market in Google Play or in SSL encryption.
Java: A Complete Tutorial from Zero to JDBC Course.*
*Affiliate link. See Terms of Use.
Are There Any Other KeyStore Types?
Yes. There are other KeyStore types. PKCS12 is one such type.
What Are the Tools Used to Manipulate KeyStores?
For JKS, we can use the Java keytool utility, which comes inbuilt with the JDK, and for PKCS12, we can use the openssl utility.
Let's Get to Work
Exporting the public key from a JSK is quite straightforward with the keytool utility, but exporting the private key is not allowed. Therefore, we need to get the support of the openssl utility for that. Additionally, you can write some custom Java code to get the private key extracted as well.
To begin with, let's create a simple KeyStore:
keytool -genkeypair -alias notebook -keyalg RSA -dname "CN=rajind,OU=dev,O=bft,L=mt,C=Srilanka" -keystore identity.jks -keypass keypassword -storepass storepassword
Extracting the Private Key With OpenSSL and Keytool
1. Convert JKS to the PKCS12 format:
keytool -importkeystore -srckeystore identity.jks -srcstorepass storepassword -srckeypass keypassword -srcalias notebook -destalias notebook -destkeystore identity.p12 -deststoretype PKCS12 -deststorepass password -destkeypass password
Note that we have given the destkeypass
and deststore
pass the same value. This is a requirement of PKCS12 as it does not support different passwords for key store and key. If you try to give different passwords, you’ll get a warning as follows as the destkeypass
will be ignored.
Warning: Different store and key passwords not supported for PKCS12 KeyStores. Ignoring user-specified -destkeypass value.
The final result of this step would be an identity.p12 file.
2. Exporting the private key from the PKCS12 format keystore:
openssl pkcs12 -in identity.p12 -nodes -nocerts -out private_key.pem
Once you enter this command, you will be prompted for the password, and once the password (in this case ‘password’) is given, the private key will be saved to a file by the named private_key.pem.
Note that in this command, nodes means ‘don’t encrypt private keys’ and nocerts means ‘don’t output certificates,’ which are the public keys.
Use the following help commands to get more details on them.
keytool -importkeystore –help
openssl pkcs12 –help
Exporting the Public Key:
openssl pkcs12 -in identity.p12 -nokeys -out cert.pem
Happy extracting!
Call To Action
Originally published at notebookbft.wordpress.com on January 1, 2019.
Published at DZone with permission of Rajind Ruparathna, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments