Tuesday, March 22, 2011

Managing certificates with Java (the keystore is the key)

This is not a very difficult task, but I'm putting out here to mainly help me remember the syntax for the keytool utility (that comes with the JDK).

How to install a certificate:
  1. Procure the desired certificate for installation into the keystore and copy it to the keystore installation location (see additional instructions at the end for details on how to do this from different browsers (if you don't already have the *.cer file).

  2. Open a command window and navigate to the appropriate directory containing the keystore that you wish to modify (e.g. C:\Program Files\Java\jdk1.6.0_10\jre\lib\security\cacerts is the default keystore).

  3. Execute the following command to import the certificate (from the *.cer file) to the keystore:

    keytool -import -file newcertificate.cer -alias new-certificate-name -keystore keystore-name

    where:

    newcertificate.cer is the name of the certificate file
    new-certificate-name is the name used to store that certificate (doesn't really matter, just something descriptive)
    keystore-name is the name of the keystore being updated (e.g. cacerts).
How to view the certificates already installed in a keystore:
  1. Execute the following command to list all of the certificates in the keystore:

    keytool -list
How to retrieve a certificate using your browser:
  1. Navigate to the desired URL (using https://), then depending on your browser, there are several ways to retrieve the cert:

    IE
    Either click on the small padlock icon in the lower right corner, or select File | Properties | Certificates from the menu.



    Then, select Details | Copy To File, and it will launch a wizard to export the certificate to a file (I typically use Base-64 encoded X.509)



    Firefox
    Very similar process, but instead of clicking the padlock, you click the highlighted logo on the left side of the address bar.



    And then select More Information | Details | View Certificate | Export.