java.security
Class KeyStore

java.lang.Object
  |
  +--java.security.KeyStore

public class KeyStore
extends Object

Untamed: This class represents an in-memory collection of keys and certificates. It manages two types of entries:

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.

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.

Since:
1.2
Version:
1.31, 12/03/01
Author:
Jan Luehe
See Also:
java.security.PrivateKey, java.security.cert.Certificate

Field Summary
private  boolean initialized
           
private static String KEYSTORE_TYPE
           
private  KeyStoreSpi keyStoreSpi
           
private  Provider provider
           
private  String type
           
 
Constructor Summary
protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
          Creates a KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it.
 
Method Summary
 Enumeration aliases()
          Enabled: Lists all the alias names of this keystore.
 boolean containsAlias(String alias)
          Enabled: Checks if the given alias exists in this keystore.
 void deleteEntry(String alias)
          Enabled: Deletes the entry identified by the given alias from this keystore.
 java.security.cert.Certificate getCertificate(String alias)
          Enabled: Returns the certificate associated with the given alias.
 String getCertificateAlias(java.security.cert.Certificate cert)
          Enabled: Returns the (alias) name of the first keystore entry whose certificate matches the given certificate.
 java.security.cert.Certificate[] getCertificateChain(String alias)
          Enabled: Returns the certificate chain associated with the given alias.
 Date getCreationDate(String alias)
          Enabled: Returns the creation date of the entry identified by the given alias.
static String getDefaultType()
          Enabled: Returns the default keystore type as specified in the Java security properties file, or the string "jks" (acronym for "Java keystore") if no such property exists.
static KeyStore getInstance(String type)
          Enabled: Generates a keystore object of the given type.
static KeyStore getInstance(String type, Provider provider)
          Enabled: Generates a keystore object for the specified keystore type from the specified provider.
static KeyStore getInstance(String type, String provider)
          Enabled: Generates a keystore object for the specified keystore type from the specified provider.
 Key getKey(String alias, char[] password)
          Enabled: Returns the key associated with the given alias, using the given password to recover it.
 Provider getProvider()
          Enabled: Returns the provider of this keystore.
 String getType()
          Enabled: Returns the type of this keystore.
 boolean isCertificateEntry(String alias)
          Enabled: Returns true if the entry identified by the given alias is a trusted certificate entry, and false otherwise.
 boolean isKeyEntry(String alias)
          Enabled: Returns true if the entry identified by the given alias is a key entry, and false otherwise.
 void load(InputStream stream, char[] password)
          Enabled: Loads this KeyStore from the given input stream.
 void setCertificateEntry(String alias, java.security.cert.Certificate cert)
          Enabled: Assigns the given certificate to the given alias.
 void setKeyEntry(String alias, byte[] key, java.security.cert.Certificate[] chain)
          Enabled: Assigns the given key (that has already been protected) to the given alias.
 void setKeyEntry(String alias, Key key, char[] password, java.security.cert.Certificate[] chain)
          Enabled: Assigns the given key to the given alias, protecting it with the given password.
 int size()
          Enabled: Retrieves the number of entries in this keystore.
 void store(OutputStream stream, char[] password)
          Enabled: Stores this keystore to the given output stream, and protects its integrity with the given password.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEYSTORE_TYPE

private static final String KEYSTORE_TYPE

type

private String type

provider

private Provider provider

keyStoreSpi

private KeyStoreSpi keyStoreSpi

initialized

private boolean initialized
Constructor Detail

KeyStore

protected KeyStore(KeyStoreSpi keyStoreSpi,
                   Provider provider,
                   String type)
Creates a KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it.

Parameters:
keyStoreSpi - the provider implementation.
provider - the provider.
type - the keystore type.
Method Detail

getInstance

public static KeyStore getInstance(String type)
                            throws KeyStoreException
Enabled: Generates a keystore object of the given type.

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.

Parameters:
type - the type of keystore. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard keystore types.
Returns:
a keystore object of the specified type.
KeyStoreException

getInstance

public static KeyStore getInstance(String type,
                                   String provider)
                            throws KeyStoreException,
                                   NoSuchProviderException
Enabled: Generates a keystore object for the specified keystore type from the specified provider.

Parameters:
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.
Returns:
a keystore object of the specified type, as supplied by the specified provider.
KeyStoreException
NoSuchProviderException
See Also:
Provider

getInstance

public static KeyStore getInstance(String type,
                                   Provider provider)
                            throws KeyStoreException
Enabled: Generates a keystore object for the specified keystore type from the specified provider. Note: the provider doesn't have to be registered.

Parameters:
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.
Returns:
a keystore object of the specified type, as supplied by the specified provider.
KeyStoreException
Since:
1.4
See Also:
Provider

getProvider

public final Provider getProvider()
Enabled: Returns the provider of this keystore.

Returns:
the provider of this keystore.

getType

public final String getType()
Enabled: Returns the type of this keystore.

Returns:
the type of this keystore.

getKey

public final Key getKey(String alias,
                        char[] password)
                 throws KeyStoreException,
                        NoSuchAlgorithmException,
                        UnrecoverableKeyException
Enabled: Returns the key associated with the given alias, using the given password to recover it.

Parameters:
alias - the alias name
password - the password for recovering the key
Returns:
the requested key, or null if the given alias does not exist or does not identify a key entry.
KeyStoreException
NoSuchAlgorithmException
UnrecoverableKeyException

getCertificateChain

public final java.security.cert.Certificate[] getCertificateChain(String alias)
                                                           throws KeyStoreException
Enabled: Returns the certificate chain associated with the given alias.

Parameters:
alias - the alias name
Returns:
the certificate chain (ordered with the user's certificate first and the root certificate authority last), or null if the given alias does not exist or does not contain a certificate chain (i.e., the given alias identifies either a trusted certificate entry or a key entry without a certificate chain).
KeyStoreException

getCertificate

public final java.security.cert.Certificate getCertificate(String alias)
                                                    throws KeyStoreException
Enabled: Returns the certificate associated with the given alias.

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.

Parameters:
alias - the alias name
Returns:
the certificate, or null if the given alias does not exist or does not contain a certificate.
KeyStoreException

getCreationDate

public final Date getCreationDate(String alias)
                           throws KeyStoreException
Enabled: Returns the creation date of the entry identified by the given alias.

Parameters:
alias - the alias name
Returns:
the creation date of this entry, or null if the given alias does not exist
KeyStoreException

setKeyEntry

public final void setKeyEntry(String alias,
                              Key key,
                              char[] password,
                              java.security.cert.Certificate[] chain)
                       throws KeyStoreException
Enabled: Assigns the given key to the given alias, protecting it with the given password.

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).

Parameters:
alias - the alias name
key - the key to be associated with the alias
password - the password to protect the key
chain - the certificate chain for the corresponding public key (only required if the given key is of type java.security.PrivateKey).
KeyStoreException

setKeyEntry

public final void setKeyEntry(String alias,
                              byte[] key,
                              java.security.cert.Certificate[] chain)
                       throws KeyStoreException
Enabled: Assigns the given key (that has already been protected) to the given alias.

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).

Parameters:
alias - the alias name
key - the key (in protected format) to be associated with the alias
chain - the certificate chain for the corresponding public key (only useful if the protected key is of type java.security.PrivateKey).
KeyStoreException

setCertificateEntry

public final void setCertificateEntry(String alias,
                                      java.security.cert.Certificate cert)
                               throws KeyStoreException
Enabled: Assigns the given certificate to the given alias.

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.

Parameters:
alias - the alias name
cert - the certificate
KeyStoreException

deleteEntry

public final void deleteEntry(String alias)
                       throws KeyStoreException
Enabled: Deletes the entry identified by the given alias from this keystore.

Parameters:
alias - the alias name
KeyStoreException

aliases

public final Enumeration aliases()
                          throws KeyStoreException
Enabled: Lists all the alias names of this keystore.

Returns:
enumeration of the alias names
KeyStoreException

containsAlias

public final boolean containsAlias(String alias)
                            throws KeyStoreException
Enabled: Checks if the given alias exists in this keystore.

Parameters:
alias - the alias name
Returns:
true if the alias exists, false otherwise
KeyStoreException

size

public final int size()
               throws KeyStoreException
Enabled: Retrieves the number of entries in this keystore.

Returns:
the number of entries in this keystore
KeyStoreException

isKeyEntry

public final boolean isKeyEntry(String alias)
                         throws KeyStoreException
Enabled: Returns true if the entry identified by the given alias is a key entry, and false otherwise.

Parameters:
alias - the alias for the keystore entry to be checked
Returns:
true if the entry identified by the given alias is a key entry, false otherwise.
KeyStoreException

isCertificateEntry

public final boolean isCertificateEntry(String alias)
                                 throws KeyStoreException
Enabled: Returns true if the entry identified by the given alias is a trusted certificate entry, and false otherwise.

Parameters:
alias - the alias for the keystore entry to be checked
Returns:
true if the entry identified by the given alias is a trusted certificate entry, false otherwise.
KeyStoreException

getCertificateAlias

public final String getCertificateAlias(java.security.cert.Certificate cert)
                                 throws KeyStoreException
Enabled: Returns the (alias) name of the first keystore entry whose certificate matches the given certificate.

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).

Parameters:
cert - the certificate to match with.
Returns:
the (alias) name of the first entry with matching certificate, or null if no such entry exists in this keystore.
KeyStoreException

store

public final void store(OutputStream stream,
                        char[] password)
                 throws KeyStoreException,
                        IOException,
                        NoSuchAlgorithmException,
                        java.security.cert.CertificateException
Enabled: Stores this keystore to the given output stream, and protects its integrity with the given password.

Parameters:
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.CertificateException

load

public final void load(InputStream stream,
                       char[] password)
                throws IOException,
                       NoSuchAlgorithmException,
                       java.security.cert.CertificateException
Enabled: Loads this KeyStore from the given input stream.

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.

Parameters:
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.CertificateException

getDefaultType

public static final String getDefaultType()
Enabled: Returns the default keystore type as specified in the Java security properties file, or the string "jks" (acronym for "Java keystore") if no such property exists. The Java security properties file is located in the file named <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME> refers to the directory where the SDK was installed.

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.

Returns:
the default keystore type as specified in the Java security properties file, or the string "jks" if no such property exists.


comments?