java.lang.Object | +--java.security.KeyStore
Untamed: This class represents an in-memory collection of keys and certificates. It manages two types of entries:
This type of keystore entry holds very sensitive cryptographic key information, which is stored in a protected format to prevent unauthorized access.
Typically, a key stored in this type of entry is a secret key, or a private key accompanied by the 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 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 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).
There are two ways to request a KeyStore object: by specifying either just a keystore type, or both a keystore type and a package provider.
KeyStore ks = KeyStore.getInstance("JKS");
the system will determine if there is an implementation of the keystore type
requested available in the environment, and if there is more than one, if
there is a preferred one.
KeyStore ks = KeyStore.getInstance("JKS", "SUN");
the system will determine if there is an implementation of the
keystore type in the package requested, and throw an exception if there
is not.
Before a keystore can be accessed, it must be
loaded. In order to create
an empty keystore, you pass null
as the InputStream argument to the load method.
java.security.PrivateKey,
java.security.cert.Certificateprotected
KeyStore(KeyStoreSpi keyStoreSpi,
Provider provider,
String type)
void
deleteEntry(String alias)
java.security.cert.Certificate
getCertificate(String alias)
String
getCertificateAlias(java.security.cert.Certificate cert)
java.security.cert.Certificate[]
getCertificateChain(String alias)
Date
getCreationDate(String alias)
static String
getDefaultType()
static KeyStore
getInstance(String type,
Provider provider)
static KeyStore
getInstance(String type,
String provider)
Key
getKey(String alias,
char[] password)
boolean
isCertificateEntry(String alias)
boolean
isKeyEntry(String alias)
void
load(InputStream stream,
char[] password)
void
setCertificateEntry(String alias,
java.security.cert.Certificate cert)
void
setKeyEntry(String alias,
byte[] key,
java.security.cert.Certificate[] chain)
void
setKeyEntry(String alias,
Key key,
char[] password,
java.security.cert.Certificate[] chain)
void
store(OutputStream stream,
char[] password)
private static final String KEYSTORE_TYPE
private String type
private Provider provider
private KeyStoreSpi keyStoreSpi
private boolean initialized
protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
keyStoreSpi - the provider implementation.provider - the provider.type - the keystore type.public static KeyStore getInstance(String type) throws KeyStoreException
If the default provider package provides a keystore implementation
of the given type, an instance of KeyStore containing that
implementation is returned. If the requested keystore type is not
available in the default package, other packages are searched.
type - the type of keystore.
See Appendix A in the
Java Cryptography Architecture API Specification & Reference
for information about standard keystore types.
KeyStoreExceptionpublic static KeyStore getInstance(String type, String provider) throws KeyStoreException, NoSuchProviderException
type - the type of keystore.
See Appendix A in the
Java Cryptography Architecture API Specification & Reference
for information about standard keystore types.provider - the name of the provider.
KeyStoreException
NoSuchProviderExceptionProviderpublic static KeyStore getInstance(String type, Provider provider) throws KeyStoreException
provider
doesn't have to be registered.
type - the type of keystore.
See Appendix A in the
Java Cryptography Architecture API Specification & Reference
for information about standard keystore types.provider - the provider.
KeyStoreExceptionProviderpublic final Provider getProvider()
public final String getType()
public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException
alias - the alias namepassword - the password for recovering the key
KeyStoreException
NoSuchAlgorithmException
UnrecoverableKeyExceptionpublic final java.security.cert.Certificate[] getCertificateChain(String alias) throws KeyStoreException
alias - the alias name
KeyStoreExceptionpublic final java.security.cert.Certificate getCertificate(String alias) throws KeyStoreException
If the given alias name identifies a trusted certificate entry, the certificate associated with that entry is returned. If the given alias name identifies a key entry, the first element of the certificate chain of that entry is returned, or null if that entry does not have a certificate chain.
alias - the alias name
KeyStoreExceptionpublic final Date getCreationDate(String alias) throws KeyStoreException
alias - the alias name
KeyStoreExceptionpublic final void setKeyEntry(String alias, Key key, char[] password, java.security.cert.Certificate[] chain) throws KeyStoreException
If the given key is of type java.security.PrivateKey,
it must be accompanied by a certificate chain certifying the
corresponding public key.
If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain).
alias - the alias namekey - the key to be associated with the aliaspassword - the password to protect the keychain - the certificate chain for the corresponding public
key (only required if the given key is of type
java.security.PrivateKey).
KeyStoreExceptionpublic final void setKeyEntry(String alias, byte[] key, java.security.cert.Certificate[] chain) throws KeyStoreException
If the protected key is of type
java.security.PrivateKey, it must be accompanied by a
certificate chain certifying the corresponding public key. If the
underlying keystore implementation is of type jks,
key must be encoded as an
EncryptedPrivateKeyInfo as defined in the PKCS #8 standard.
If the given alias already exists, the keystore information associated with it is overridden by the given key (and possibly certificate chain).
alias - the alias namekey - the key (in protected format) to be associated with the aliaschain - the certificate chain for the corresponding public
key (only useful if the protected key is of type
java.security.PrivateKey).
KeyStoreExceptionpublic final void setCertificateEntry(String alias, java.security.cert.Certificate cert) throws KeyStoreException
If the given alias already exists in this keystore and identifies a trusted certificate entry, the certificate associated with it is overridden by the given certificate.
alias - the alias namecert - the certificate
KeyStoreExceptionpublic final void deleteEntry(String alias) throws KeyStoreException
alias - the alias name
KeyStoreExceptionpublic final Enumeration aliases() throws KeyStoreException
KeyStoreExceptionpublic final boolean containsAlias(String alias) throws KeyStoreException
alias - the alias name
KeyStoreExceptionpublic final int size() throws KeyStoreException
KeyStoreExceptionpublic final boolean isKeyEntry(String alias) throws KeyStoreException
alias - the alias for the keystore entry to be checked
KeyStoreExceptionpublic final boolean isCertificateEntry(String alias) throws KeyStoreException
alias - the alias for the keystore entry to be checked
KeyStoreExceptionpublic final String getCertificateAlias(java.security.cert.Certificate cert) throws KeyStoreException
This method attempts to match the given certificate with each keystore entry. If the entry being considered is a trusted certificate entry, the given certificate is compared to that entry's certificate. If the entry being considered is a key entry, the given certificate is compared to the first element of that entry's certificate chain (if a chain exists).
cert - the certificate to match with.
KeyStoreExceptionpublic final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, java.security.cert.CertificateException
stream - the output stream to which this keystore is written.password - the password to generate the keystore integrity check
KeyStoreException
IOException
NoSuchAlgorithmException
java.security.cert.CertificateExceptionpublic final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, java.security.cert.CertificateException
If a password is given, it is used to check the integrity of the keystore data. Otherwise, the integrity of the keystore is not checked.
In order to create an empty keystore, or if the keystore cannot
be initialized from a stream (e.g., because it is stored on a hardware
token device), you pass null
as the stream argument.
Note that if this KeyStore has already been loaded, it is reinitialized and loaded again from the given input stream.
stream - the input stream from which the keystore is loaded, or
null if an empty keystore is to be created.password - the (optional) password used to check the integrity of
the keystore.
IOException
NoSuchAlgorithmException
java.security.cert.CertificateExceptionpublic static final String getDefaultType()
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
getInstance methods, and want to provide a default keystore
type in case a user does not specify its own.
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.