This page outlines how to create a keystore by importing private keys and public certificates that you have received from somebody else into a local keystore so that you can use them for encrypted communication.
Public keys will commonly be in a format called PEM. PEM files begin and end with a special delimiter line and consist of a body of Base-64 encoded binary.
Public keys will often have one of the following file extensions:
.PEM, .CRT
The following is an example of what a PEM file should look like:
-----BEGIN CERTIFICATE-----
MIIDdzCCAl+gAwIBAgIETWBDIDANBgkqhkiG9w0BAQsFADBsMRAwDgYDVQQGEwdVbmtub3duMRAw
DgYDVQQIEwdVbmtub3duMRAwDgYDVQQHEwdVbmtub3duMRAwDgYDVQQKEwdVbmtub3duMRAwDgYD
... some lines removed ...
4UJdLAICuodKr1YNzWOi+qu/C/toHkGIH3qqFlBwxYv+03VFy1ZeqqSFOS4yVZReXrNP9dpvIkcs
G9w7mJXpRj444RtF+sKQ15WYs7MmABnIrv9r/+mVbgXO1yawhEzc4rt4m/T5tGpiAE485mGUwZzo
7/5nr32KlhSkexwwuZCDpuzm3FC36g+puzVLUic32S1GZ50Z
-----END CERTIFICATE-----
The key may also be in a format known as DER, which is a binary format. To import a key file in either PEM or DER format into a keystore, issue the following command. The following arguments should be substituted for something appropriate:
-alias trust_app_alias
refers to the name
by which the key will be known to the keystore. It generaly doesn't matter what
you put here as long as you choose something meaningful to the
certificate you are putting in.
-file public.cert
refers to the filename of
the certificate. This file should be in one of the formats listed above.
-keystore keystore.jks
refers to the
name of the keystore you are putting the key in. If no file exists with
this name, one will be created and given the keystore password that you
specify at the prompt below. If this keystore already exists, the new
certificate will be added to it, and the password you enter must match
the password that this keystore was previously given.
Private keys will often be provided using a format called PKCS#12. This is
a format produced by the OpenSSL toolset. PKCS#12 files are generally provided
using the extension .PFX
or .P12
.
The "openssl" tool can first be used to verify the contents of a PKCS#12 file. Note that the file itself will have a password which should be supplied by whoever supplies the file. In the example below, the password is "helloworld123".
In particular, the output above contains the "friendlyName" attribute value of "le-d7ffb209-fb59-4e0c-bd42-75157dccc563". This is a kind of alias which will be used to refer to the key when importing it into the keystore.
To import a PKCS#12 file into a keystore, use the following command. Note the following arguments:
-destkeystore otherside.jks
tells
keytool the filename for the destination keystore. This is the keystore that the
key will be copied into
-deststorepass changeit
tells
keytool the password to use for the keystore. If this is a new keystore being created,
this same password will need to be used for any subsequent modifications. If this is
an existing keystore being added to, the password must match its pre-existing password
-srckeystore private_key.pfx
refers to the
name of the PKCS#12 file containing the private key
-srcalias le-d7ffb209-fb59-4e0c-bd42-75157dccc563
refers to the friendly name of the key within the PKCS#12 file. This can be obtained using
openssl, as seen above.
-destalias lab_staging_system
gives the key a friendly name (or alias) within the destination keystore. This
should be a small ID which refers to the specific purpose for the key
-destkeypass changeit
gives the key itself a password within the keystore.