public class KeyStore extends Object
KeyStore
is responsible for maintaining cryptographic keys and their
owners.
The type of the system key store can be changed by setting the 'keystore.type'
property in the file named JAVA_HOME/lib/security/java.security
.
Certificate
,
PrivateKey
Modifier and Type | Class and Description |
---|---|
static class |
KeyStore.Builder
Builder is used to construct new instances of KeyStore . |
static class |
KeyStore.CallbackHandlerProtection
|
static interface |
KeyStore.Entry
Entry is the common marker interface for a KeyStore
entry. |
static interface |
KeyStore.LoadStoreParameter
LoadStoreParameter represents a parameter that specifies how a
KeyStore can be loaded and stored. |
static class |
KeyStore.PasswordProtection
PasswordProtection is a ProtectionParameter that protects
a KeyStore using a password. |
static class |
KeyStore.PrivateKeyEntry
PrivateKeyEntry represents a KeyStore entry that
holds a private key. |
static interface |
KeyStore.ProtectionParameter
ProtectionParameter is a marker interface for protection
parameters. |
static class |
KeyStore.SecretKeyEntry
SecretKeyEntry represents a KeyStore entry that
holds a secret key. |
static class |
KeyStore.TrustedCertificateEntry
TrustedCertificateEntry represents a KeyStore entry that
holds a trusted certificate. |
Modifier | Constructor and Description |
---|---|
protected |
KeyStore(KeyStoreSpi keyStoreSpi,
Provider provider,
String type)
Constructs a new instance of
KeyStore with the given arguments. |
Modifier and Type | Method and Description |
---|---|
Enumeration<String> |
aliases()
Returns an
Enumeration over all alias names stored in this
KeyStore . |
boolean |
containsAlias(String alias)
Indicates whether the given alias is present in this
KeyStore . |
void |
deleteEntry(String alias)
Deletes the entry identified with the given alias from this
KeyStore . |
boolean |
entryInstanceOf(String alias,
Class<? extends KeyStore.Entry> entryClass)
Indicates whether the entry for the given alias is assignable to the
provided
Class . |
Certificate |
getCertificate(String alias)
Returns the trusted certificate for the entry with the given alias.
|
String |
getCertificateAlias(Certificate cert)
Returns the alias associated with the first entry whose certificate
matches the specified certificate.
|
Certificate[] |
getCertificateChain(String alias)
Returns the certificate chain for the entry with the given alias.
|
Date |
getCreationDate(String alias)
Returns the creation date of the entry with the given alias.
|
static String |
getDefaultType()
Returns the default type for
KeyStore instances. |
KeyStore.Entry |
getEntry(String alias,
KeyStore.ProtectionParameter param)
Returns the
Entry with the given alias, using the specified
ProtectionParameter . |
static KeyStore |
getInstance(String type)
Returns a new instance of
KeyStore with the specified type. |
static KeyStore |
getInstance(String type,
Provider provider)
Returns a new instance of
KeyStore from the specified provider
with the given type. |
static KeyStore |
getInstance(String type,
String provider)
Returns a new instance of
KeyStore from the specified provider
with the given type. |
Key |
getKey(String alias,
char[] password)
Returns the key with the given alias, using the password to recover the
key from the store.
|
Provider |
getProvider()
Returns the provider associated with this
KeyStore . |
String |
getType()
Returns the type of this
KeyStore . |
boolean |
isCertificateEntry(String alias)
Indicates whether the specified alias is associated with a
KeyStore.TrustedCertificateEntry . |
boolean |
isKeyEntry(String alias)
Indicates whether the specified alias is associated with either a
KeyStore.PrivateKeyEntry or a KeyStore.SecretKeyEntry . |
void |
load(InputStream stream,
char[] password)
Initializes this
KeyStore from the provided InputStream . |
void |
load(KeyStore.LoadStoreParameter param)
Loads this
KeyStore using the specified LoadStoreParameter . |
void |
setCertificateEntry(String alias,
Certificate cert)
Associates the given alias with a certificate.
|
void |
setEntry(String alias,
KeyStore.Entry entry,
KeyStore.ProtectionParameter param)
Stores the given
Entry in this KeyStore and associates
the entry with the given alias . |
void |
setKeyEntry(String alias,
byte[] key,
Certificate[] chain)
Associates the given alias with a key and a certificate chain.
|
void |
setKeyEntry(String alias,
Key key,
char[] password,
Certificate[] chain)
Associates the given alias with the key, password and certificate chain.
|
int |
size()
Returns the number of entries stored in this
KeyStore . |
void |
store(KeyStore.LoadStoreParameter param)
Stores this
KeyStore using the specified LoadStoreParameter . |
void |
store(OutputStream stream,
char[] password)
Writes this
KeyStore to the specified OutputStream . |
protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
KeyStore
with the given arguments.keyStoreSpi
- the concrete key store.provider
- the provider.type
- the type of the KeyStore
to be constructed.public static KeyStore getInstance(String type) throws KeyStoreException
KeyStore
with the specified type.type
- the type of the returned KeyStore
.KeyStore
with the specified type.KeyStoreException
- if an error occurred during the creation of the new KeyStore
.NullPointerException
- if type == null
getDefaultType()
public static KeyStore getInstance(String type, String provider) throws KeyStoreException, NoSuchProviderException
KeyStore
from the specified provider
with the given type.type
- the type of the returned KeyStore
.provider
- name of the provider of the KeyStore
.KeyStore
from the specified provider
with the given type.KeyStoreException
- if an error occurred during the creation of the new KeyStore
.NoSuchProviderException
- if the specified provider is not available.IllegalArgumentException
- if provider == null || provider.isEmpty()
NullPointerException
- if type
is null
(instead of
NoSuchAlgorithmException) as in 1.4 releasegetDefaultType()
public static KeyStore getInstance(String type, Provider provider) throws KeyStoreException
KeyStore
from the specified provider
with the given type.type
- the type of the returned KeyStore
.provider
- the provider of the KeyStore
.KeyStore
from the specified provider
with the given type.KeyStoreException
- if an error occurred during the creation of the new KeyStore
.IllegalArgumentException
- if provider
is null
or the empty string.NullPointerException
- if type == null
(instead of
NoSuchAlgorithmException) as in 1.4 releasegetDefaultType()
public static final String getDefaultType()
KeyStore
instances.
The default is specified in the 'keystore.type'
property in the
file named java.security
properties file. If this property
is not set, "jks"
will be used.
KeyStore
instancespublic final Provider getProvider()
KeyStore
.KeyStore
.public final String getType()
KeyStore
.KeyStore
.public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException
alias
- the alias for the entry.password
- the password used to recover the key.null
if the
specified alias is not bound to an entry.KeyStoreException
- if this KeyStore
is not initialized.NoSuchAlgorithmException
- if the algorithm for recovering the key is not available.UnrecoverableKeyException
- if the key can not be recovered.public final Certificate[] getCertificateChain(String alias) throws KeyStoreException
alias
- the alias for the entry.null
if the specified alias is not bound to an entry.KeyStoreException
- if this KeyStore
is not initialized.public final Certificate getCertificate(String alias) throws KeyStoreException
alias
- the alias for the entry.null
if the specified alias is not bound to an entry.KeyStoreException
- if this KeyStore
is not initialized.public final Date getCreationDate(String alias) throws KeyStoreException
alias
- the alias for the entry.null
if the specified alias is not
bound to an entry.KeyStoreException
- if this KeyStore
is not initialized.public final void setKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException
If the specified alias already exists, it will be reassigned.
alias
- the alias for the key.key
- the key.password
- the password.chain
- the certificate chain.KeyStoreException
- if this KeyStore
is not initialized.IllegalArgumentException
- if key
is a PrivateKey
and chain
does
not contain any certificates.NullPointerException
- if alias
is null
.public final void setKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreException
If the specified alias already exists, it will be reassigned.
If this KeyStore
is of type "jks"
, key
must be
encoded conform to the PKS#8 standard as an
EncryptedPrivateKeyInfo
.
alias
- the alias for the key.key
- the key in an encoded format.chain
- the certificate chain.KeyStoreException
- if this KeyStore
is not initialized or if key
is null.IllegalArgumentException
- if key
is a PrivateKey
and chain
does.NullPointerException
- if alias
is null
.public final void setCertificateEntry(String alias, Certificate cert) throws KeyStoreException
If the specified alias already exists, it will be reassigned.
alias
- the alias for the certificate.cert
- the certificate.KeyStoreException
- if this KeyStore
is not initialized, or an existing
alias is not associated to an entry containing a trusted
certificate, or this method fails for any other reason.NullPointerException
- if alias
is null
.public final void deleteEntry(String alias) throws KeyStoreException
KeyStore
.alias
- the alias for the entry.KeyStoreException
- if this KeyStore
is not initialized, or if the entry
can not be deleted.public final Enumeration<String> aliases() throws KeyStoreException
Enumeration
over all alias names stored in this
KeyStore
.Enumeration
over all alias names stored in this
KeyStore
.KeyStoreException
- if this KeyStore
is not initialized.public final boolean containsAlias(String alias) throws KeyStoreException
KeyStore
.alias
- the alias of an entry.true
if the alias exists, false
otherwise.KeyStoreException
- if this KeyStore
is not initialized.public final int size() throws KeyStoreException
KeyStore
.KeyStore
.KeyStoreException
- if this KeyStore
is not initialized.public final boolean isKeyEntry(String alias) throws KeyStoreException
KeyStore.PrivateKeyEntry
or a KeyStore.SecretKeyEntry
.alias
- the alias of an entry.true
if the given alias is associated with a key entry.KeyStoreException
- if this KeyStore
is not initialized.public final boolean isCertificateEntry(String alias) throws KeyStoreException
KeyStore.TrustedCertificateEntry
.alias
- the alias of an entry.true
if the given alias is associated with a certificate
entry.KeyStoreException
- if this KeyStore
is not initialized.public final String getCertificateAlias(Certificate cert) throws KeyStoreException
cert
- the certificate to find the associated entry's alias for.null
if no entry with the specified
certificate can be found.KeyStoreException
- if this KeyStore
is not initialized.public final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
KeyStore
to the specified OutputStream
. The
data written to the OutputStream
is protected by the specified
password.stream
- the OutputStream
to write the store's data to.password
- the password to protect the data.KeyStoreException
- if this KeyStore
is not initialized.IOException
- if a problem occurred while writing to the stream.NoSuchAlgorithmException
- if the required algorithm is not available.CertificateException
- if an exception occurred while storing the certificates of
this KeyStore
.public final void store(KeyStore.LoadStoreParameter param) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
KeyStore
using the specified LoadStoreParameter
.param
- the LoadStoreParameter
that specifies how to store
this KeyStore
, maybe null
.KeyStoreException
- if this KeyStore
is not initialized.IOException
- if a problem occurred while writing to the stream.NoSuchAlgorithmException
- if the required algorithm is not available.CertificateException
- if an exception occurred while storing the certificates of
this KeyStore
.IllegalArgumentException
- if the given KeyStore.LoadStoreParameter
is not recognized.public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException
KeyStore
from the provided InputStream
.
Pass null
as the stream
argument to initialize an empty
KeyStore
or to initialize a KeyStore
which does not rely
on an InputStream
. This KeyStore
utilizes the given
password to verify the stored data.stream
- the InputStream
to load this KeyStore
's data
from or null
.password
- the password to verify the stored data, maybe null
.IOException
- if a problem occurred while reading from the stream.NoSuchAlgorithmException
- if the required algorithm is not available.CertificateException
- if an exception occurred while loading the certificates of
this KeyStore
.public final void load(KeyStore.LoadStoreParameter param) throws IOException, NoSuchAlgorithmException, CertificateException
KeyStore
using the specified LoadStoreParameter
.param
- the LoadStoreParameter
that specifies how to load this
KeyStore
, maybe null
.IOException
- if a problem occurred while reading from the stream.NoSuchAlgorithmException
- if the required algorithm is not available.CertificateException
- if an exception occurred while loading the certificates of
this KeyStore
.IllegalArgumentException
- if the given KeyStore.LoadStoreParameter
is not recognized.public final KeyStore.Entry getEntry(String alias, KeyStore.ProtectionParameter param) throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException
Entry
with the given alias, using the specified
ProtectionParameter
.alias
- the alias of the requested entry.param
- the ProtectionParameter
used to protect the requested
entry, maybe null
.Entry
with the given alias, using the specified
ProtectionParameter
.NoSuchAlgorithmException
- if the required algorithm is not available.UnrecoverableEntryException
- if the entry can not be recovered.KeyStoreException
- if this KeyStore
is not initialized.NullPointerException
- if alias
is null
.public final void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter param) throws KeyStoreException
Entry
in this KeyStore
and associates
the entry with the given alias
. The entry is protected by the
specified ProtectionParameter
.
If the specified alias already exists, it will be reassigned.
alias
- the alias for the entry.entry
- the entry to store.param
- the ProtectionParameter
to protect the entry.KeyStoreException
- if this KeyStore
is not initialized.NullPointerException
- if alias
is null
or entry
is null
.public final boolean entryInstanceOf(String alias, Class<? extends KeyStore.Entry> entryClass) throws KeyStoreException
Class
.alias
- the alias for the entry.entryClass
- the type of the entry.true
if the Entry
for the alias is assignable to
the specified entryClass
.KeyStoreException
- if this KeyStore
is not initialized.