java.lang.ObjectThis class represents a storage facility for cryptographic keys and certificates.java.security.KeyStore
A KeyStore
manages different types of entries.
Each type of entry implements the KeyStore.Entry
interface.
Three basic KeyStore.Entry
implementations are provided:
This type of entry holds a cryptographic PrivateKey
,
which is optionally stored in a protected format to prevent
unauthorized access. It is also accompanied by a certificate chain
for the corresponding public key.
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.
This type of entry holds a cryptographic SecretKey
,
which is optionally stored in a protected format to prevent
unauthorized access.
This type of entry contains a single public key Certificate
belonging to another party. It is called a trusted certificate
because the keystore owner trusts that the public key in the certificate
indeed belongs to the identity identified by the subject (owner)
of the certificate.
This type of entry can be used to authenticate other parties.
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 public key algorithms.
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.
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).
Typical ways to request a KeyStore object include relying on the default type and providing a specific keystore type.
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());The system will return a keystore implementation for the default type.
KeyStore ks = KeyStore.getInstance("JKS");The system will return the most preferred implementation of the specified keystore type available in the environment.
Before a keystore can be accessed, it must be loaded .
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); // get user password and file input stream char[] password = getPassword(); java.io.FileInputStream fis = null; try { fis = new java.io.FileInputStream("keyStoreName"); ks.load(fis, password); } finally { if (fis != null) { fis.close(); } }To create an empty keystore using the above
load
method,
pass null
as the InputStream
argument.
Once the keystore has been loaded, it is possible to read existing entries from the keystore, or to write new entries into the keystore:
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 java.io.FileOutputStream fos = null; try { fos = new java.io.FileOutputStream("newKeyStoreName"); ks.store(fos, password); } finally { if (fos != null) { fos.close(); } }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.
Every implementation of the Java platform is required to support
the following standard KeyStore
type:
Jan
- Luehe1.2
- Nested Class Summary: | ||
---|---|---|
public static interface | KeyStore.LoadStoreParameter | A marker interface for KeyStore
{@link #load(KeyStore.LoadStoreParameter) load}
and
{@link #store(KeyStore.LoadStoreParameter) store}
parameters. |
public static interface | KeyStore.ProtectionParameter | A marker interface for keystore protection parameters.
The information stored in a |
public static class | KeyStore.PasswordProtection | A password-based implementation of ProtectionParameter . |
public static class | KeyStore.CallbackHandlerProtection | A ProtectionParameter encapsulating a CallbackHandler. |
public static interface | KeyStore.Entry | A marker interface for KeyStore entry types. |
public static final class | KeyStore.PrivateKeyEntry | A KeyStore entry that holds a PrivateKey
and corresponding certificate chain. |
public static final class | KeyStore.SecretKeyEntry | A KeyStore entry that holds a SecretKey . |
public static final class | KeyStore.TrustedCertificateEntry | A KeyStore entry that holds a trusted
Certificate . |
abstract public static class | KeyStore.Builder | A description of a to-be-instantiated KeyStore object.
An instance of this class encapsulates the information needed to instantiate and initialize a KeyStore object. That process is triggered when the {@linkplain #getKeyStore} method is called. This makes it possible to decouple configuration from KeyStore object creation and e.g. delay a password prompt until it is needed. |
static class | KeyStore.SimpleLoadStoreParameter |
Constructor: |
---|
|
Method from java.security.KeyStore Summary: |
---|
aliases, containsAlias, deleteEntry, entryInstanceOf, getCertificate, getCertificateAlias, getCertificateChain, getCreationDate, getDefaultType, getEntry, getInstance, getInstance, getInstance, getKey, getProvider, getType, isCertificateEntry, isKeyEntry, load, load, setCertificateEntry, setEntry, setKeyEntry, setKeyEntry, size, store, store |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from java.security.KeyStore Detail: |
---|
|
|
|
Entry for the specified
alias is an instance or subclass of the specified
entryClass . |
If the given alias name identifies an entry
created by a call to If the given alias name identifies an entry
created by a call to |
This method attempts to match the given certificate with each
keystore entry. If the entry being considered was
created by a call to If the entry being considered was
created by a call to |
setKeyEntry ,
or by a call to setEntry with a
PrivateKeyEntry . |
|
The default keystore type can be used by applications that do not
want to use a hard-coded keystore type when calling one of the
The default keystore type can be changed by setting the value of the "keystore.type" security property (in the Java security properties file) to the desired keystore type. |
Entry for the specified alias
with the specified protection parameter. |
This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyStore object encapsulating the KeyStoreSpi implementation from the first Provider that supports the specified type is returned. Note that the list of registered providers may be retrieved via the Security.getProviders() method. |
A new KeyStore object encapsulating the KeyStoreSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list. Note that the list of registered providers may be retrieved via the Security.getProviders() method. |
A new KeyStore object encapsulating the KeyStoreSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list. |
setKeyEntry ,
or by a call to setEntry with a
PrivateKeyEntry or SecretKeyEntry . |
|
|
setCertificateEntry ,
or created by a call to setEntry with a
TrustedCertificateEntry . |
setKeyEntry ,
or created by a call to setEntry with a
PrivateKeyEntry or a SecretKeyEntry . |
LoadStoreParameter .
Note that if this KeyStore has already been loaded, it is reinitialized and loaded again from the given parameter. |
A password may be given to unlock the keystore (e.g. the keystore resides on a hardware token device), or to check the integrity of the keystore data. If a password is not given for integrity checking, then integrity checking is not performed. In order to create an empty keystore, or if the keystore cannot
be initialized from a stream, pass Note that if this keystore has already been loaded, it is reinitialized and loaded again from the given input stream. |
If the given alias identifies an existing entry
created by a call to |
Entry under the specified alias.
The protection parameter is used to protect the
Entry .
If an entry already exists for the specified alias, it is overridden. |
If the protected key is of type
If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain). |
If the given key is of type If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain). |
|
LoadStoreParameter . |
|