public class KeyStore extends Object
Constructor and Description |
---|
KeyStore() |
Modifier and Type | Method and Description |
---|---|
Enumeration<String> |
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.
|
Certificate |
getCertificate(String alias)
Returns the certificate associated with the given alias.
|
Certificate[] |
getCertificateChain(String alias)
Returns the certificate chain associated with the given alias.
|
static String |
getDefaultType()
Returns the default keystore type as specified by the
keystore.type security property, or the string
"jks" (acronym for "Java keystore")
if no such property exists. |
static KeyStore |
getInstance(String type)
Returns a keystore object of the specified type.
|
Key |
getKey(String alias,
char[] password)
Returns the key associated with the given alias, using the given
password to recover it.
|
String |
getType()
Returns the type of this keystore.
|
boolean |
isCertificateEntry(String alias)
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(String alias)
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(InputStream stream,
char[] password)
Loads this KeyStore from the given input stream.
|
void |
setCertificateEntry(String alias,
Certificate cert)
Assigns the given trusted certificate to the given alias.
|
void |
setKeyEntry(String alias,
byte[] key,
Certificate[] chain)
Assigns the given key (that has already been protected) to the given
alias.
|
int |
size()
Retrieves the number of entries in this keystore.
|
public static KeyStore getInstance(String type) throws KeyStoreException
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.
type
- the type of keystore.
See the KeyStore section in the
Java Cryptography Architecture Standard Algorithm Name Documentation
for information about standard keystore types.KeyStoreException
- if no Provider supports a
KeyStoreSpi implementation for the
specified type.public static final String getDefaultType()
keystore.type
security property, or the string
"jks" (acronym for "Java keystore")
if no such property exists.
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 to the desired keystore type.
keystore.type
security property, or the string "jks"
if no such property exists.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 keyKeyStoreException
- if the keystore has not been initialized
(loaded).NoSuchAlgorithmException
- if the algorithm for recovering the
key cannot be foundUnrecoverableKeyException
- 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 nameKeyStoreException
- if the keystore has not been initialized
(loaded).public final Certificate getCertificate(String alias) throws KeyStoreException
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 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.
alias
- the alias nameKeyStoreException
- if the keystore has not been initialized
(loaded).public final void setKeyEntry(String alias, byte[] key, 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
).KeyStoreException
- if the keystore has not been initialized
(loaded), or if this operation fails for some other reason.public final void setCertificateEntry(String alias, Certificate cert) throws KeyStoreException
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.
alias
- the alias namecert
- the certificateKeyStoreException
- if the keystore has not been initialized,
or the given alias already exists and does not identify an
entry containing a trusted certificate,
or this operation fails for some other reason.public final void deleteEntry(String alias) throws KeyStoreException
alias
- the alias nameKeyStoreException
- if the keystore has not been initialized,
or if the entry cannot be removed.public final Enumeration<String> aliases() throws KeyStoreException
KeyStoreException
- if the keystore has not been initialized
(loaded).public final boolean containsAlias(String alias) throws KeyStoreException
alias
- the alias nameKeyStoreException
- if the keystore has not been initialized
(loaded).public final int size() throws KeyStoreException
KeyStoreException
- if the keystore has not been initialized
(loaded).public final boolean isKeyEntry(String alias) throws KeyStoreException
setKeyEntry
,
or created by a call to setEntry
with a
PrivateKeyEntry
or a SecretKeyEntry
.alias
- the alias for the keystore entry to be checkedKeyStoreException
- if the keystore has not been initialized
(loaded).public final boolean isCertificateEntry(String alias) throws KeyStoreException
setCertificateEntry
,
or created by a call to setEntry
with a
TrustedCertificateEntry
.alias
- the alias for the keystore entry to be checkedKeyStoreException
- if the keystore has not been initialized
(loaded).public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
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 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
password
- the password used to check the integrity of
the keystore, the password used to unlock the keystore,
or null
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. If the error is due to a
wrong password, the cause
of the
IOException
should be an
UnrecoverableKeyException
NoSuchAlgorithmException
- if the algorithm used to check
the integrity of the keystore cannot be foundCertificateException
- if any of the certificates in the
keystore could not be loaded