|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.security.KeyStore
public class KeyStore

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 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 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 =
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.
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.
PrivateKey
,
SecretKey,
Certificate

| Nested Class Summary | |
|---|---|
static class |
KeyStore.Builder
A description of a to-be-instantiated KeyStore object. |
static class |
KeyStore.CallbackHandlerProtection
A ProtectionParameter encapsulating a CallbackHandler. |
static interface |
KeyStore.Entry
A marker interface for KeyStore entry types. |
static interface |
KeyStore.LoadStoreParameter
A marker interface for KeyStore
load
and
store
parameters. |
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
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
Checks if the given alias exists in this keystore. |
void |
deleteEntry
Deletes the entry identified by the given alias from this keystore. |
boolean |
entryInstanceOf
Determines if the keystore Entry for the specified
alias is an instance or subclass of the specified
entryClass. |
Certificate |
getCertificate
Returns the certificate associated with the given alias. |
String |
getCertificateAlias
Returns the (alias) name of the first keystore entry whose certificate matches the given certificate. |
Certificate |
getCertificateChain
Returns the certificate chain associated with the given alias. |
Date |
getCreationDate
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
Gets a keystore Entry for the specified alias
with the specified protection parameter. |
static KeyStore |
getInstance
Generates a keystore object of the given type. |
static KeyStore |
getInstance
Generates a keystore object for the specified keystore type from the specified provider. |
static KeyStore |
getInstance
Generates a keystore object for the specified keystore type from the specified provider. |
Key |
getKey
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
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. |
boolean |
isKeyEntry
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. |
void |
load
Loads this KeyStore from the given input stream. |
void |
load
Loads this keystore using the given LoadStoreParameter. |
void |
setCertificateEntry
Assigns the given trusted certificate to the given alias. |
void |
setEntry
Saves a keystore Entry under the specified alias. |
void |
setKeyEntry
Assigns the given key (that has already been protected) to the given alias. |
void |
setKeyEntry
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
Stores this keystore using the given LoadStoreParameter. |
void |
store
Stores this keystore to the given output stream, and protects its integrity with the given password. |
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Constructor Detail |
|---|

protected KeyStore(KeyStoreSpi![]()
![]()
keyStoreSpi, Provider
![]()
![]()
provider, String
![]()
![]()
type)
keyStoreSpi - the provider implementation.provider - the provider.type - the keystore type.| Method Detail |
|---|

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

- if the requested keystore type is
not available in the default provider package or any of the other
provider packages that were searched.

public 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

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


public 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.
KeyStoreException

- if the requested keystore type is not
available from the provider.
IllegalArgumentException

- if the provider is
null.Provider


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

public final Provider![]()
![]()
getProvider()

public final String![]()
![]()
getType()

public final Key![]()
![]()
getKey(String
![]()
![]()
alias, char[] password) throws KeyStoreException
![]()
![]()
, NoSuchAlgorithmException
![]()
![]()
, UnrecoverableKeyException
![]()
![]()
setKeyEntry,
or by a call to setEntry with a
PrivateKeyEntry or SecretKeyEntry.
alias - the alias namepassword - the password for recovering the key
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).

public final Certificate![]()
![]()
[] getCertificateChain(String
![]()
![]()
alias) throws KeyStoreException
![]()
![]()
setKeyEntry,
or by a call to setEntry with a
PrivateKeyEntry.
alias - the alias name
KeyStoreException

- if the keystore has not been initialized
(loaded).