Java's keytool
Russell Bateman |
While this is about creating artifacts that are put into a Java keystore and truststore, most of the work is done using openssl (though much of that could probably be done using keytool).
Useful link: The keytool command.
You'll find these accumulating in the current working directory as you follow these steps.
Server keystore |
Server truststore |
User keystore |
---|---|---|
server.key | trust.key | user.key |
— | — | user.csr |
server.crt | trust.crt | user.crt |
server.pkcs12 | — | user.pkcs12 |
— | trust.jks | — |
$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus ...
$ ll server.key
-rw------- 1 russ russ 1675 Jul 27 11:21 server.key
$ openssl req -new -x509 -days 365 -key server.key -out server.crt -subj "/CN=localhost/O=Wind of Keltia/L=Provo/ST=UT/C=US/" $ ll server.crt -rw-rw-r-- 1 russ russ 1294 Jul 27 11:23 server.crt
$ openssl x509 -in server.crt -text
Certificate:
...
$ openssl pkcs12 -export -out server.pkcs12 -password pass:changeit -inkey server.key -in server.crt
$ openssl pkcs12 -info -noout -in server.pkcs12 Enter Import Password: ... $ ll -rw-rw-r-- 1 russ russ 1294 Jul 27 11:23 server.crt -rw------- 1 russ russ 1675 Jul 27 11:21 server.key -rw------- 1 russ russ 2517 Jul 27 11:25 server.pkcs12
$ openssl genrsa -out trust.key 2048
Generating RSA private key, 2048 bit long modulus ...
$ ll trust.key
-rw------- 1 russ russ 1679 Jul 27 11:26 trust.key
$ openssl req -new -x509 -days 365 -key trust.key -out trust.crt -subj "/CN=windofkeltia.com/O=Wind of Keltia/L=Provo/ST=UT/C=US/" $ ll trust.crt -rw-rw-r-- 1 russ russ 1314 Jul 27 11:28 trust.crt
$ openssl x509 -in trust.crt -text
Certificate:
...
$ keytool -importcert -alias tomcat -file trust.crt -keystore trust.jks -storepass changeit Owner: C=US, ST=UT, L=Provo, O=Wind of Keltia, CN=windofkeltia.com ... Trust this certificate? [no]: yes Certificate was added to keystore $ ll trust.jks -rw-rw-r-- 1 russ russ 1287 Jul 27 11:35 trust.jks
$ keytool -list -v -keystore trust.jks -storepass changeit
Keystore type: PKCS12
Keystore provider: SUN
Your keystore contains 1 entry
...
$ openssl genrsa -out user.key 2048 Generating RSA private key, 2048 bit long modulus ... $ ll user.key -rw------- 1 russ russ 1675 Jul 27 11:41 user.key
$ openssl req -out user.csr -key user.key -new -subj "/CN=windofkeltia.com/O=Wind of Keltia/L=Provo/ST=UT/C=US/" $ ll user.csr -rw-rw-r-- 1 russ russ 993 Jul 27 11:43 user.csr
$ cat user.csr
-----BEGIN CERTIFICATE REQUEST-----
MIICozCCAYsCAQAwXjEZMBcGA1UEAwwQd2luZG9ma2VsdGlhLmNvbTEXMBUGA1UE
...
$ ll -rw-rw-r-- 1 russ russ 1294 Jul 27 11:23 server.crt -rw------- 1 russ russ 1675 Jul 27 11:21 server.key -rw------- 1 russ russ 2517 Jul 27 11:25 server.pkcs12 -rw-rw-r-- 1 russ russ 1314 Jul 27 11:28 trust.crt -rw-rw-r-- 1 russ russ 1287 Jul 27 11:35 trust.jks -rw------- 1 russ russ 1679 Jul 27 11:26 trust.key -rw-rw-r-- 1 russ russ 993 Jul 27 11:43 user.csr -rw------- 1 russ russ 1675 Jul 27 11:41 user.key $ openssl x509 -req -days 365 -in user.csr -CA trust.crt -CAkey trust.key -CAcreateserial -out user.crt Signature ok subject=CN = windofkeltia.com, O = Wind of Keltia, L = Provo, ST = UT, C = US Getting CA Private Key $ ll user.crt -rw-rw-r-- 1 russ russ 1192 Jul 27 13:23 user.crt
$ openssl x509 -in user.crt -text
Certificate:
...
$ openssl pkcs12 -export -inkey user.key -in user.crt -out user.pkcs12 -password pass:changeit $ ll -rw-rw-r-- 1 russ russ 1294 Jul 27 11:23 server.crt -rw------- 1 russ russ 1675 Jul 27 11:21 server.key -rw------- 1 russ russ 2517 Jul 27 11:25 server.pkcs12 -rw-rw-r-- 1 russ russ 1314 Jul 27 11:28 trust.crt -rw-rw-r-- 1 russ russ 1287 Jul 27 11:35 trust.jks -rw------- 1 russ russ 1679 Jul 27 11:26 trust.key -rw-rw-r-- 1 russ russ 41 Jul 27 13:23 trust.srl* -rw-rw-r-- 1 russ russ 1192 Jul 27 13:23 user.crt -rw-rw-r-- 1 russ russ 993 Jul 27 11:43 user.csr -rw------- 1 russ russ 1675 Jul 27 11:41 user.key -rw------- 1 russ russ 2437 Jul 27 13:27 user.pkcs12
$ openssl pkcs12 -info -noout -in user.pkcs12 -password pass:changeit
MAC: sha1, Iteration 2048
MAC length: 20, salt length: 8
PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 2048
Certificate bag
PKCS7 Data
Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 2048
* An SRL file, security certificate serial number, contains a serial number generated while signing an OpenSSL certificate and is used to identify a signed certificate uniquely. Its generation is the result of option -CAcreateserial.
When a user creates his first OpenSSL certificate, he uses the -CAcreateserial option to create the SRL file, then, for a subsequent certificate, the -CAserial option plus path to the file for creating, beside the new certificate, a new SRL file containing an incremented serial number. This file's contents are hexadecimal.
See Step by Step guide to Enable HTTPS or SSL correct way on Apache Tomcat Server—Port 8443.
Also check out How to generate a self-signed certificate.