Generating Self-signed Certificates for use in Java
There are different ways of creating a self-signed certificate, such as using Java keytool. I prefer openSSL because the keys and certificates generated this way are more standardized and can be used for other purposes. Follow the steps below to generate a self-signed SSL certificate for use when testing SSL applications and servers.
Update If you need to create self signed certificates for use in Java and are running Linux, I have developed a Shell Script you can use. You can find the source and instructions on GitHub.
Update If you need self-signed certificates for Mutual Authentication, please see my follow-up article.
1) Create a host private key using openSSL
This private key is 2048 bits long, generated using the RSA algorithm, and we choose not to protect it with an additional passphrase because the key will be used with a server certificate. The name of the private key is HOSTNAME-private.pem where HOSTNAME should be replaced by the name of the machine you intend to use the certificate.1.1) Derive the public key using openSSL
This step is not necessary, unless you want to distribute the public key to others.2) Create a self-signed X509 certificate
Then you will be prompted to enter a few pieces of information, use “.” if you wish to leave the field blank.NOTE: The field Common Name is quite important here. It is the hostname of the machine you are trying to certify with the certificate, which is the name in the DNS entry corresponding to your machine IP.
3) Create a PKCS12 keystore and import the host certificate
Provide a password when prompted.4) Convert the PKCS12 keystore to Java keystore using Java keytool
Keytool will first ask you for the new password for the JKS keystore twice, and it will also ask you for the password you set for the PKCS12 keystore created earlier.It will output the number of entries successfully imported, failed, and cancelled. If nothing went wrong, you should have another keystore file: keystore.jks.
keystore.jks is the file that you will want to use in your java applications and servers.
References
- http://www.openssl.org/docs/HOWTO/
- http://docs.oracle.com/javase/1.3/docs/tooldocs/win32/keytool.html
Published
03 July 2012