TopBlend: Here is the first difference. There are 56 differences. is old. is new.

java.security
Class KeyStore


java.lang.Object
  extended by java.security.KeyStore

public class KeyStore
extends Object

This class represents a storage facility for cryptographic keys and certificates.

A KeyStore manages different types of entries. Each type of entry implements the KeyStore.Entry interface. Three basic KeyStore.Entry implementations are provided: 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).

Typical ways to request a KeyStore object include relying on the default type and providing a specific keystore type. 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 .


 
 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

 // get user password and file input stream
 char[] password = getPassword();
 java.io.FileInputStream fis =
 new java.io.FileInputStream("keyStoreName");
 ks.load(fis, password);
 fis.close();
 
To create an empty keystore using the above load method, pass null as the InputStream argument. In order to create an empty keystore, you pass null as the InputStream argument to the load method.

Once the keystore has been loaded, it is possible to read existing entries from the keystore, or to write new entries into the keystore:


 
 // get my private key
 KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
 ks.getEntry("privateKeyAlias", password);
 PrivateKey myPrivateKey = pkEntry.getPrivateKey();

 // save my secret key
 javax.crypto.SecretKey mySecretKey;
 KeyStore.SecretKeyEntry skEntry =
 new KeyStore.SecretKeyEntry(mySecretKey);
 ks.setEntry("secretKeyAlias", skEntry, password);

 // store away the keystore
 java.io.FileOutputStream fos =
 new java.io.FileOutputStream("newKeyStoreName");
 ks.store(fos, password);
 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.

Since:
1.2
See Also:
PrivateKey , SecretKey , Certificate

Nested Class Summary
static class KeyStore.Builder
          A description of a to-be-instantiated KeyStore object.
static interface KeyStore.Entry
          A marker interface for KeyStore entry types.
static interface KeyStore.LoadStoreParameter
          A marker interface for KeyStore load and store
static class KeyStore.PasswordProtection
          A password-based implementation of ProtectionParameter.
static class KeyStore.PrivateKeyEntry
          A KeyStore entry that holds a PrivateKey and corresponding certificate chain.
static interface KeyStore.ProtectionParameter
          A marker interface for keystore protection parameters.
static class KeyStore.SecretKeyEntry
          A KeyStore entry that holds a SecretKey.
static class KeyStore.TrustedCertificateEntry
          A KeyStore entry that holds a trusted Certificate.
 
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 ()
          Lists all the alias names of this keystore.
 boolean containsAlias ( String  alias)
          Checks if the given alias exists in this keystore.
 void deleteEntry ( String  alias)
          Deletes the entry identified by the given alias from this keystore.
 boolean entryInstanceOf ( String  alias, Class
          Determines if the keystore Entry for the specified alias is an instance or subclass of the specified entryClass
  Certificate getCertificate ( String  alias)
          Returns the certificate associated with the given alias.
  String getCertificateAlias ( Certificate  cert)
          Returns the (alias) name of the first keystore entry whose certificate matches the given certificate.
  Certificate [] getCertificateChain ( String  alias)
          Returns the certificate chain associated with the given alias.
  Date getCreationDate ( String  alias)
          Returns the creation date of the entry identified by the given alias.
static  String getDefaultType ()
          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.
  KeyStore.Entry getEntry ( String  alias, KeyStore.ProtectionParameter
          Gets a keystore Entry for the specified alias with the specified protection parameter.
static  KeyStore getInstance ( String  type)
          Generates a keystore object of the given type.
static  KeyStore getInstance ( String  type, Provider  provider)
          Generates a keystore object for the specified keystore type from the specified provider.
static  KeyStore getInstance ( String  type, String  provider)
          Generates a keystore object for the specified keystore type from the specified provider.
  Key getKey ( String  alias, char[] password)
          Returns the key associated with the given alias, using the given password to recover it.
  Provider getProvider ()
          Returns the provider of this keystore.
  String getType ()
          Returns the type of this keystore.
 boolean isCertificateEntry ( String
          Returns true if the entry identified by the given alias was created by is a call to setCertificateEntry, or created by a call to setEntry with a TrustedCertificateEntry. trusted certificate entry , and false otherwise.
 boolean isKeyEntry ( String
          Returns true if the entry identified by the given alias was created by is a call to setKeyEntry, or created by a call to setEntry with a PrivateKeyEntry or a SecretKeyEntry. key entry , and false otherwise.
 void load ( InputStream  stream, char[] password)
          Loads this KeyStore from the given input stream.
 void load setCertificateEntry ( KeyStore.LoadStoreParameter String  alias, Certificate
          Loads this keystore using           Assigns the given LoadStoreParameter. certificate to the given alias.
 void setCertificateEntry ( String  alias, Certificate
          Assigns the given trusted certificate to the given alias.
 void setEntry ( String  alias, KeyStore.Entry  entry, KeyStore.ProtectionParameter
          Saves a keystore Entry under the specified alias.
 void setKeyEntry ( String  alias, byte[] key, Certificate [] chain)
          Assigns the given key (that has already been protected) to the given alias.
 void setKeyEntry ( String  alias, Key  key, char[] password, Certificate [] chain)
          Assigns the given key to the given alias, protecting it with the given password.
 int size ()
          Retrieves the number of entries in this keystore.
 void store ( KeyStore.LoadStoreParameter
          Stores this keystore using the given LoadStoreParameter.
 void store ( OutputStream  stream, char[] password)
          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
 

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
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.
Throws:
KeyStoreException - if the requested keystore type is not available in the default provider package or any of the other provider packages that were searched.

getInstance


public static KeyStore getInstance(String type,
                                   String provider)
                            throws KeyStoreException,
                                   NoSuchProviderException
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.
Throws:
KeyStoreException - if the requested keystore type is not available from the provider.
NoSuchProviderException - if the provider has not been configured.
IllegalArgumentException - if the provider name is null or empty.
See Also:
Provider

getInstance


public static KeyStore getInstance(String type,
                                   Provider provider)
                            throws KeyStoreException
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.
Throws:
KeyStoreException - if the requested keystore type is not available from the provider.
IllegalArgumentException - if the provider is null.
Since:
1.4
See Also:
Provider

getDefaultType


 
public static final StringgetDefaultType () 
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.

getProvider


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

Returns:
the provider of this keystore.

getType


public final String getType()
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
Returns the key associated with the given alias, using the given password to recover it. The key must have been associated with the alias by a call to setKeyEntry, or by a call to setEntry with a PrivateKeyEntry or SecretKeyEntry.

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-related entry. key entry .
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).
NoSuchAlgorithmException - if the algorithm for recovering the key cannot be found
UnrecoverableKeyException - if the key cannot be recovered (e.g., the given password is wrong).

getCertificateChain


public final Certificate[] getCertificateChain(String alias)
                                        throws KeyStoreException
Returns the certificate chain associated with the given alias. The certificate chain must have been associated with the alias by a call to setKeyEntry, or by a call to setEntry with a PrivateKeyEntry.

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).
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

getCertificate


public final Certificate getCertificate(String alias)
                                 throws KeyStoreException
Returns the certificate associated with the given alias.

If the given alias name identifies an entry created by a call to setCertificateEntry, or created by a call to setEntry with a TrustedCertificateEntry, then the trusted certificate contained in that entry is returned. 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.

If the given alias name identifies an entry created by a call to setKeyEntry, or created by a call to setEntry with a PrivateKeyEntry, then the first element of the certificate chain in that entry is returned.

Parameters:
alias - the alias name
Returns:
the certificate, or null if the given alias does not exist or does not contain a certificate.
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

getCreationDate


public final Date getCreationDate(String alias)
                           throws KeyStoreException
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
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

setKeyEntry


public final void setKeyEntry(String alias,
                              Key key,
                              char[] password,
                              Certificate[] chain)
                       throws KeyStoreException
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).
Throws:
KeyStoreException - if the keystore has not been initialized (loaded), the given key cannot be protected, or this operation fails for some other reason

setKeyEntry


public final void setKeyEntry(String alias,
                              byte[] key,
                              Certificate[] chain)
                       throws KeyStoreException
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 public key (only useful if the protected key is of type java.security.PrivateKey). type java.security.PrivateKey).
Throws:
KeyStoreException - if the keystore has not been initialized (loaded), or if this operation fails for some other reason.

setCertificateEntry


public final void setCertificateEntry(String alias,
                                      Certificate cert)
                               throws KeyStoreException
Assigns the given trusted certificate to the given alias.

If the given alias identifies an existing entry created by a call to setCertificateEntry, or created by a call to setEntry with a TrustedCertificateEntry, the trusted certificate in the existing entry is overridden by the given certificate. 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
Throws:
KeyStoreException - if the keystore has not been initialized, or the given alias already exists and does not identify an entry containing a trusted certificate, certificate entry , or this operation fails for some other reason.

deleteEntry


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

Parameters:
alias - the alias name
Throws:
KeyStoreException - if the keystore has not been initialized, or if the entry cannot be removed.

aliases


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

Returns:
enumeration of the alias names
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

containsAlias


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

Parameters:
alias - the alias name
Returns:
true if the alias exists, false otherwise
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

size


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

Returns:
the number of entries in this keystore
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

isKeyEntry


public final boolean isKeyEntry(String alias)
                         throws KeyStoreException
Returns true if the entry identified by the given alias was created by a call to setKeyEntry, or created by a call to setEntry with a PrivateKeyEntry or a SecretKeyEntry. 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-related entry, key entry , false otherwise.
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

isCertificateEntry


public final boolean isCertificateEntry(String alias)
                                 throws KeyStoreException
Returns true if the entry identified by the given alias was created by a call to setCertificateEntry, or created by a call to setEntry with a TrustedCertificateEntry. 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 contains is a trusted certificate, certificate entry , false otherwise.
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).

getCertificateAlias


public final String getCertificateAlias(Certificate cert)
                                 throws KeyStoreException
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 was created by a call to setCertificateEntry, or created by a call to setEntry with a TrustedCertificateEntry, then the given certificate is compared to that entry's certificate. 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).

If the entry being considered was created by a call to setKeyEntry, or created by a call to setEntry with a PrivateKeyEntry, then the given certificate is compared to the first element of that entry's certificate chain.

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

store


public final void store(OutputStream stream,
                        char[] password)
                 throws KeyStoreException,
                        IOException,
                        NoSuchAlgorithmException,
                        CertificateException
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
Throws:
KeyStoreException - if the keystore has not been initialized (loaded).
IOException - if there was an I/O problem with data
NoSuchAlgorithmException - if the appropriate data integrity algorithm could not be found
CertificateException - if any of the certificates included in the keystore data could not be stored

store


 
public final void store ( KeyStore.LoadStoreParameter param)
 throws KeyStoreException,
 IOException,
 NoSuchAlgorithmException,
 CertificateException
Stores this keystore using the given LoadStoreParameter.

Parameters:
param - the LoadStoreParameter that specifies how to store the keystore, which may be null
Throws:
IllegalArgumentException - if the given LoadStoreParameter input is not recognized
KeyStoreException - if the keystore has not been initialized (loaded)
IOException - if there was an I/O problem with data
NoSuchAlgorithmException - if the appropriate data integrity algorithm could not be found
CertificateException - if any of the certificates included in the keystore data could not be stored
Since:
1.5

load


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

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. 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, stream (e.g., because it is stored on a hardware token device), you pass null as the stream argument.

Note that if this keystore 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, the password used to unlock the keystore, or null keystore.
Throws:
IOException - if there is an I/O or format problem with the keystore data, if a password is required but not given, or if the given password was incorrect data
NoSuchAlgorithmException - if the algorithm used to check the integrity of the keystore cannot be found
CertificateException - if any of the certificates in the keystore could not be loaded

load getDefaultType


public static final void Stringload getDefaultType ( KeyStore.LoadStoreParameter param)
 throws IOException,
 NoSuchAlgorithmException,
 CertificateException() 
Loads this keystore using the given LoadStoreParameter. 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.

Note that if this KeyStore has already been loaded, it is reinitialized and loaded again from the given parameter. 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.

Parameters: Returns:
param - the LoadStoreParameter that specifies how to load the keystore, which may be null
Throws:
IllegalArgumentException - if the given LoadStoreParameter input is not recognized
IOException - if there is an I/O or format problem with the keystore data
NoSuchAlgorithmException - if the algorithm used to check the integrity of the keystore cannot be found
CertificateException - if any of the certificates in the keystore could not be loaded
Since:
1.5
the default keystore type as specified in the Java security properties file, or the string "jks" if no such property exists.

getEntry


 
public final KeyStore.EntrygetEntry ( String alias,
 KeyStore.ProtectionParameter protParam)
 throws NoSuchAlgorithmException,
 UnrecoverableEntryException,
 KeyStoreException
Gets a keystore Entry for the specified alias with the specified protection parameter.

Parameters:
alias - get the keystore Entry for this alias
protParam - the ProtectionParameter used to protect the Entry, which may be null
Returns:
the keystore Entry for the specified alias, or null if there is no such entry
Throws:
NullPointerException - if alias is null
NoSuchAlgorithmException - if the algorithm for recovering the entry cannot be found
UnrecoverableEntryException - if the specified protParam were insufficient or invalid
KeyStoreException - if the keystore has not been initialized (loaded).
Since:
1.5

setEntry


 
public final void setEntry ( String alias,
 KeyStore.Entry entry,
 KeyStore.ProtectionParameter protParam)
 throws KeyStoreException
Saves a keystore 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.

Parameters:
alias - save the keystore Entry under this alias
entry - the Entry to save
protParam - the ProtectionParameter used to protect the Entry, which may be null
Throws:
NullPointerException - if alias or entry is null
KeyStoreException - if the keystore has not been initialized (loaded), or if this operation fails for some other reason
Since:
1.5

entryInstanceOf


 
public final boolean entryInstanceOf ( String alias,
 Class entryClass)
 throws KeyStoreException
Determines if the keystore Entry for the specified alias is an instance or subclass of the specified entryClass

Parameters:
alias - the alias name
entryClass - the entry class
Returns:
true if the keystore Entry for the specified alias is an instance or subclass of the specified entryClass, false otherwise
Throws:
NullPointerException - if alias or entryClass is null
KeyStoreException - if the keystore has not been initialized (loaded)