Tuesday, June 2, 2015

Useful keytool commands for certificate management

keytool command

keytool command location : $JAVA_HOME/jre/bin/keytool
cacerts location : $JAVA_HOME/jre/lib/security/cacerts

(Generally this is the location of keytool command or cacerts, It may vary based on your environment)

Parameters for below examples


Alias Name/Label: "This is a cert"
Certifcate Filename: testcert.cer
Keystore Name: cacerts

Importing Certificate


keytool -import -trustcacerts -alias "Alias_Name" -file "Filename" -keystore "keystore_Name"

Example:


keytool -import -trustcacerts -alias "This is a cert" -file testcert.cer -keystore cacerts

This above command will import testcert.cer into the keystore cacerts with the label "This is a cert"

Listing Certificate

keytool -list -keystore "keystore_name"

Examples:

keytool -list -keystore cacerts
Lists all the certificates in the keystore cacerts

keytool -list -v -keystore cacerts
Lists all the details of all certificates in the keystore cacerts

keytool -list -alias "This is a cert" -keystore cacerts
Lists the certificate withe the Alias "This is a cert" in the kestore cacerts

keytool -list -v -alias "This is a cert" -keystore cacerts
Lists the certificate with the label "This is a cert" in the keystre cacerts

keytool -list -v -keystore cacerts |grep Alias
Lists the Alias of all the certificates in the keystre cacerts.

Deleting the certificate


keytool -delete -alias "Alias_Name" -keystore "Keystore_Name"