This class represents a storage facility for cryptographic
keys and certificates.
<p> A {@code KeyStore} manages different types of entries.
Each type of entry : the {@code KeyStore.Entry} interface.
Three basic {@code KeyStore.Entry} implementations are provided:
<ul>
<li><b>KeyStore.PrivateKeyEntry</b>
<p> This type of entry holds a cryptographic {@code PrivateKey},
which is optionally stored in a protected format to prevent
unauthorized access. It is also accompanied by a certificate chain
for the corresponding key.
<p> Private keys and certificate chains are used by a given entity for
self-authentication. Applications for this authentication include software
distribution organizations which sign JAR files as part of releasing
and/or licensing software.
<li><b>KeyStore.SecretKeyEntry</b>
<p> This type of entry holds a cryptographic {@code SecretKey},
which is optionally stored in a protected format to prevent
unauthorized access.
<li><b>KeyStore.TrustedCertificateEntry</b>
<p> This type of entry contains a single key {@code Certificate}
belonging to another party. It is called a <i>trusted certificate</i>
because the keystore owner trusts that the key in the certificate
indeed belongs to the identity identified by the <i>subject</i> (owner)
of the certificate.
<p>This type of entry can be used to authenticate other parties.
</ul>
<p> Each entry in a keystore is identified by an "alias" string. In the
case of private keys and their associated certificate chains, these strings
distinguish among the different ways in which the entity may authenticate
itself. For example, the entity may authenticate itself using different
certificate authorities, or using different key algorithms.
<p> Whether aliases are case sensitive is implementation dependent. In order
to avoid problems, it is recommended not to use aliases in a KeyStore that
only differ in case.
<p> Whether keystores are persistent, and the mechanisms used by the
keystore if it is persistent, are not specified here. This allows
use of a variety of techniques for protecting sensitive (e.g., private or
secret) keys. Smart cards or other integrated cryptographic engines
(SafeKeyper) are one option, and simpler mechanisms such as files may also
be used (in a variety of formats).
<p> Typical ways to request a KeyStore object include
relying on the default type and providing a specific keystore type.
<ul>
<li>To rely on the default type:
<pre>
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
</pre>
The system will return a keystore implementation for the default type.
<li>To provide a specific keystore type:
<pre>
KeyStore ks = KeyStore.getInstance("JKS");
</pre>
The system will return the most preferred implementation of the
specified keystore type available in the environment. <p>
</ul>
<p> Before a keystore can be accessed, it must be
{@link #load(java.io.InputStream, char[]) loaded}.
<pre>
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
// get user password and file input stream
char[] password = getPassword();
To create an empty keystore using the above {@code load} method,
pass {@code null} as the {@code InputStream} argument.
<p> Once the keystore has been loaded, it is possible
to read existing entries from the keystore, or to write new entries
into the keystore:
<pre>
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(password);
// get my private key
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
ks.getEntry("privateKeyAlias", protParam);
PrivateKey myPrivateKey = pkEntry.getPrivateKey();
// save my secret key
javax.crypto.SecretKey mySecretKey;
KeyStore.SecretKeyEntry skEntry =
new KeyStore.SecretKeyEntry(mySecretKey);
ks.setEntry("secretKeyAlias", skEntry, protParam);
// store away the keystore
try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
ks.store(fos, password);
}
</pre>
Note that although the same password may be used to
load the keystore, to protect the private key entry,
to protect the secret key entry, and to store the keystore
(as is shown in the sample code above),
different passwords or other protection parameters
may also be used.
<p> Every implementation of the Java platform is required to support
the following standard {@code KeyStore} type:
<ul>
<li>{@code PKCS12}</li>
</ul>
This type is described in the <a href=
"{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore">
KeyStore section</a> of the
Java Cryptography Architecture Standard Algorithm Name Documentation.
Consult the release documentation for your implementation to see if any
other types are supported.
This class represents a storage facility for cryptographic keys and certificates.
<p> A {@code KeyStore} manages different types of entries. Each type of entry : the {@code KeyStore.Entry} interface. Three basic {@code KeyStore.Entry} implementations are provided:
<ul> <li><b>KeyStore.PrivateKeyEntry</b> <p> This type of entry holds a cryptographic {@code PrivateKey}, which is optionally stored in a protected format to prevent unauthorized access. It is also accompanied by a certificate chain for the corresponding key.
<p> Private keys and certificate chains are used by a given entity for self-authentication. Applications for this authentication include software distribution organizations which sign JAR files as part of releasing and/or licensing software.
<li><b>KeyStore.SecretKeyEntry</b> <p> This type of entry holds a cryptographic {@code SecretKey}, which is optionally stored in a protected format to prevent unauthorized access.
<li><b>KeyStore.TrustedCertificateEntry</b> <p> This type of entry contains a single key {@code Certificate} belonging to another party. It is called a <i>trusted certificate</i> because the keystore owner trusts that the key in the certificate indeed belongs to the identity identified by the <i>subject</i> (owner) of the certificate.
<p>This type of entry can be used to authenticate other parties. </ul>
<p> Each entry in a keystore is identified by an "alias" string. In the case of private keys and their associated certificate chains, these strings distinguish among the different ways in which the entity may authenticate itself. For example, the entity may authenticate itself using different certificate authorities, or using different key algorithms.
<p> Whether aliases are case sensitive is implementation dependent. In order to avoid problems, it is recommended not to use aliases in a KeyStore that only differ in case.
<p> Whether keystores are persistent, and the mechanisms used by the keystore if it is persistent, are not specified here. This allows use of a variety of techniques for protecting sensitive (e.g., private or secret) keys. Smart cards or other integrated cryptographic engines (SafeKeyper) are one option, and simpler mechanisms such as files may also be used (in a variety of formats).
<p> Typical ways to request a KeyStore object include relying on the default type and providing a specific keystore type.
<ul> <li>To rely on the default type: <pre> KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); </pre> The system will return a keystore implementation for the default type.
<li>To provide a specific keystore type: <pre> KeyStore ks = KeyStore.getInstance("JKS"); </pre> The system will return the most preferred implementation of the specified keystore type available in the environment. <p> </ul>
<p> Before a keystore can be accessed, it must be {@link #load(java.io.InputStream, char[]) loaded}. <pre> KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
// get user password and file input stream char[] password = getPassword();
try (FileInputStream fis = new FileInputStream("keyStoreName")) { ks.load(fis, password); } </pre>
To create an empty keystore using the above {@code load} method, pass {@code null} as the {@code InputStream} argument.
<p> Once the keystore has been loaded, it is possible to read existing entries from the keystore, or to write new entries into the keystore: <pre> KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
// get my private key KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("privateKeyAlias", protParam); PrivateKey myPrivateKey = pkEntry.getPrivateKey();
// save my secret key javax.crypto.SecretKey mySecretKey; KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(mySecretKey); ks.setEntry("secretKeyAlias", skEntry, protParam);
// store away the keystore try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) { ks.store(fos, password); } </pre>
Note that although the same password may be used to load the keystore, to protect the private key entry, to protect the secret key entry, and to store the keystore (as is shown in the sample code above), different passwords or other protection parameters may also be used.
<p> Every implementation of the Java platform is required to support the following standard {@code KeyStore} type: <ul> <li>{@code PKCS12}</li> </ul> This type is described in the <a href= "{@docRoot}/../technotes/guides/security/StandardNames.html#KeyStore"> KeyStore section</a> of the Java Cryptography Architecture Standard Algorithm Name Documentation. Consult the release documentation for your implementation to see if any other types are supported.
@author Jan Luehe
@see java.security.PrivateKey @see javax.crypto.SecretKey @see java.security.cert.Certificate
@since 1.2