Package java.security
Class KeyStore
java.lang.Object
java.security.KeyStore
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.
- See Also:
Certificate,PrivateKey
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKeyStore.BuilderBuilderis used to construct new instances ofKeyStore.static classKeyStore.CallbackHandlerProtectionstatic interfaceKeyStore.EntryEntryis the common marker interface for aKeyStoreentry.static interfaceKeyStore.LoadStoreParameterLoadStoreParameterrepresents a parameter that specifies how aKeyStorecan be loaded and stored.static classKeyStore.PasswordProtectionPasswordProtectionis aProtectionParameterthat protects aKeyStoreusing a password.static classKeyStore.PrivateKeyEntryPrivateKeyEntryrepresents aKeyStoreentry that holds a private key.static interfaceKeyStore.ProtectionParameterProtectionParameteris a marker interface for protection parameters.static classKeyStore.SecretKeyEntrySecretKeyEntryrepresents aKeyStoreentry that holds a secret key.static classKeyStore.TrustedCertificateEntryTrustedCertificateEntryrepresents aKeyStoreentry that holds a trusted certificate. -
Constructor Summary
Constructors Modifier Constructor Description protectedKeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)Constructs a new instance ofKeyStorewith the given arguments. -
Method Summary
Modifier and Type Method Description Enumeration<String>aliases()Returns anEnumerationover all alias names stored in thisKeyStore.booleancontainsAlias(String alias)Indicates whether the given alias is present in thisKeyStore.voiddeleteEntry(String alias)Deletes the entry identified with the given alias from thisKeyStore.booleanentryInstanceOf(String alias, Class<? extends KeyStore.Entry> entryClass)Indicates whether the entry for the given alias is assignable to the providedClass.CertificategetCertificate(String alias)Returns the trusted certificate for the entry with the given alias.StringgetCertificateAlias(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.DategetCreationDate(String alias)Returns the creation date of the entry with the given alias.static StringgetDefaultType()Returns the default type forKeyStoreinstances.KeyStore.EntrygetEntry(String alias, KeyStore.ProtectionParameter param)Returns theEntrywith the given alias, using the specifiedProtectionParameter.static KeyStoregetInstance(String type)Returns a new instance ofKeyStorewith the specified type.static KeyStoregetInstance(String type, String provider)Returns a new instance ofKeyStorefrom the specified provider with the given type.static KeyStoregetInstance(String type, Provider provider)Returns a new instance ofKeyStorefrom the specified provider with the given type.KeygetKey(String alias, char[] password)Returns the key with the given alias, using the password to recover the key from the store.ProvidergetProvider()Returns the provider associated with thisKeyStore.StringgetType()Returns the type of thisKeyStore.booleanisCertificateEntry(String alias)Indicates whether the specified alias is associated with aKeyStore.TrustedCertificateEntry.booleanisKeyEntry(String alias)Indicates whether the specified alias is associated with either aKeyStore.PrivateKeyEntryor aKeyStore.SecretKeyEntry.voidload(InputStream stream, char[] password)Initializes thisKeyStorefrom the providedInputStream.voidload(KeyStore.LoadStoreParameter param)Loads thisKeyStoreusing the specifiedLoadStoreParameter.voidsetCertificateEntry(String alias, Certificate cert)Associates the given alias with a certificate.voidsetEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter param)Stores the givenEntryin thisKeyStoreand associates the entry with the givenalias.voidsetKeyEntry(String alias, byte[] key, Certificate[] chain)Associates the given alias with a key and a certificate chain.voidsetKeyEntry(String alias, Key key, char[] password, Certificate[] chain)Associates the given alias with the key, password and certificate chain.intsize()Returns the number of entries stored in thisKeyStore.voidstore(OutputStream stream, char[] password)Writes thisKeyStoreto the specifiedOutputStream.voidstore(KeyStore.LoadStoreParameter param)Stores thisKeyStoreusing the specifiedLoadStoreParameter.
-
Constructor Details
-
KeyStore
Constructs a new instance ofKeyStorewith the given arguments.- Parameters:
keyStoreSpi- the concrete key store.provider- the provider.type- the type of theKeyStoreto be constructed.
-
-
Method Details
-
getInstance
Returns a new instance ofKeyStorewith the specified type.- Parameters:
type- the type of the returnedKeyStore.- Returns:
- a new instance of
KeyStorewith the specified type. - Throws:
KeyStoreException- if an error occurred during the creation of the newKeyStore.NullPointerException- iftype == null- See Also:
getDefaultType()
-
getInstance
public static KeyStore getInstance(String type, String provider) throws KeyStoreException, NoSuchProviderExceptionReturns a new instance ofKeyStorefrom the specified provider with the given type.- Parameters:
type- the type of the returnedKeyStore.provider- name of the provider of theKeyStore.- Returns:
- a new instance of
KeyStorefrom the specified provider with the given type. - Throws:
KeyStoreException- if an error occurred during the creation of the newKeyStore.NoSuchProviderException- if the specified provider is not available.IllegalArgumentException- ifprovider == null || provider.isEmpty()NullPointerException- iftypeisnull(instead of NoSuchAlgorithmException) as in 1.4 release- See Also:
getDefaultType()
-
getInstance
Returns a new instance ofKeyStorefrom the specified provider with the given type.- Parameters:
type- the type of the returnedKeyStore.provider- the provider of theKeyStore.- Returns:
- a new instance of
KeyStorefrom the specified provider with the given type. - Throws:
KeyStoreException- if an error occurred during the creation of the newKeyStore.IllegalArgumentException- ifproviderisnullor the empty string.NullPointerException- iftype == null(instead of NoSuchAlgorithmException) as in 1.4 release- See Also:
getDefaultType()
-
getDefaultType
Returns the default type forKeyStoreinstances.The default is specified in the
'keystore.type'property in the file namedjava.securityproperties file. If this property is not set,"jks"will be used.- Returns:
- the default type for
KeyStoreinstances
-
getProvider
Returns the provider associated with thisKeyStore.- Returns:
- the provider associated with this
KeyStore.
-
getType
Returns the type of thisKeyStore.- Returns:
- the type of this
KeyStore.
-
getKey
public final Key getKey(String alias, char[] password) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyExceptionReturns the key with the given alias, using the password to recover the key from the store.- Parameters:
alias- the alias for the entry.password- the password used to recover the key.- Returns:
- the key with the specified alias, or
nullif the specified alias is not bound to an entry. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.NoSuchAlgorithmException- if the algorithm for recovering the key is not available.UnrecoverableKeyException- if the key can not be recovered.
-
getCertificateChain
Returns the certificate chain for the entry with the given alias.- Parameters:
alias- the alias for the entry.- Returns:
- the certificate chain for the entry with the given alias, or
nullif the specified alias is not bound to an entry. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
getCertificate
Returns the trusted certificate for the entry with the given alias.- Parameters:
alias- the alias for the entry.- Returns:
- the trusted certificate for the entry with the given alias, or
nullif the specified alias is not bound to an entry. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
getCreationDate
Returns the creation date of the entry with the given alias.- Parameters:
alias- the alias for the entry.- Returns:
- the creation date, or
nullif the specified alias is not bound to an entry. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
setKeyEntry
public final void setKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreExceptionAssociates the given alias with the key, password and certificate chain.If the specified alias already exists, it will be reassigned.
- Parameters:
alias- the alias for the key.key- the key.password- the password.chain- the certificate chain.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.IllegalArgumentException- ifkeyis aPrivateKeyandchaindoes not contain any certificates.NullPointerException- ifaliasisnull.
-
setKeyEntry
public final void setKeyEntry(String alias, byte[] key, Certificate[] chain) throws KeyStoreExceptionAssociates the given alias with a key and a certificate chain.If the specified alias already exists, it will be reassigned.
If this
KeyStoreis of type"jks",keymust be encoded conform to the PKS#8 standard as anEncryptedPrivateKeyInfo.- Parameters:
alias- the alias for the key.key- the key in an encoded format.chain- the certificate chain.- Throws:
KeyStoreException- if thisKeyStoreis not initialized or ifkeyis null.IllegalArgumentException- ifkeyis aPrivateKeyandchaindoes.NullPointerException- ifaliasisnull.
-
setCertificateEntry
Associates the given alias with a certificate.If the specified alias already exists, it will be reassigned.
- Parameters:
alias- the alias for the certificate.cert- the certificate.- Throws:
KeyStoreException- if thisKeyStoreis 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- ifaliasisnull.
-
deleteEntry
Deletes the entry identified with the given alias from thisKeyStore.- Parameters:
alias- the alias for the entry.- Throws:
KeyStoreException- if thisKeyStoreis not initialized, or if the entry can not be deleted.
-
aliases
Returns anEnumerationover all alias names stored in thisKeyStore.- Returns:
- an
Enumerationover all alias names stored in thisKeyStore. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
containsAlias
Indicates whether the given alias is present in thisKeyStore.- Parameters:
alias- the alias of an entry.- Returns:
trueif the alias exists,falseotherwise.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
size
Returns the number of entries stored in thisKeyStore.- Returns:
- the number of entries stored in this
KeyStore. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
isKeyEntry
Indicates whether the specified alias is associated with either aKeyStore.PrivateKeyEntryor aKeyStore.SecretKeyEntry.- Parameters:
alias- the alias of an entry.- Returns:
trueif the given alias is associated with a key entry.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
isCertificateEntry
Indicates whether the specified alias is associated with aKeyStore.TrustedCertificateEntry.- Parameters:
alias- the alias of an entry.- Returns:
trueif the given alias is associated with a certificate entry.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
getCertificateAlias
Returns the alias associated with the first entry whose certificate matches the specified certificate.- Parameters:
cert- the certificate to find the associated entry's alias for.- Returns:
- the alias or
nullif no entry with the specified certificate can be found. - Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-
store
public final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateExceptionWrites thisKeyStoreto the specifiedOutputStream. The data written to theOutputStreamis protected by the specified password.- Parameters:
stream- theOutputStreamto write the store's data to.password- the password to protect the data.- Throws:
KeyStoreException- if thisKeyStoreis 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 thisKeyStore.
-
store
public final void store(KeyStore.LoadStoreParameter param) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateExceptionStores thisKeyStoreusing the specifiedLoadStoreParameter.- Parameters:
param- theLoadStoreParameterthat specifies how to store thisKeyStore, maybenull.- Throws:
KeyStoreException- if thisKeyStoreis 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 thisKeyStore.IllegalArgumentException- if the givenKeyStore.LoadStoreParameteris not recognized.
-
load
public final void load(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateExceptionInitializes thisKeyStorefrom the providedInputStream. Passnullas thestreamargument to initialize an emptyKeyStoreor to initialize aKeyStorewhich does not rely on anInputStream. ThisKeyStoreutilizes the given password to verify the stored data.- Parameters:
stream- theInputStreamto load thisKeyStore's data from ornull.password- the password to verify the stored data, maybenull.- Throws:
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 thisKeyStore.
-
load
public final void load(KeyStore.LoadStoreParameter param) throws IOException, NoSuchAlgorithmException, CertificateExceptionLoads thisKeyStoreusing the specifiedLoadStoreParameter.- Parameters:
param- theLoadStoreParameterthat specifies how to load thisKeyStore, maybenull.- Throws:
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 thisKeyStore.IllegalArgumentException- if the givenKeyStore.LoadStoreParameteris not recognized.
-
getEntry
public final KeyStore.Entry getEntry(String alias, KeyStore.ProtectionParameter param) throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreExceptionReturns theEntrywith the given alias, using the specifiedProtectionParameter.- Parameters:
alias- the alias of the requested entry.param- theProtectionParameterused to protect the requested entry, maybenull.- Returns:
- he
Entrywith the given alias, using the specifiedProtectionParameter. - Throws:
NoSuchAlgorithmException- if the required algorithm is not available.UnrecoverableEntryException- if the entry can not be recovered.KeyStoreException- if thisKeyStoreis not initialized.NullPointerException- ifaliasisnull.
-
setEntry
public final void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter param) throws KeyStoreExceptionStores the givenEntryin thisKeyStoreand associates the entry with the givenalias. The entry is protected by the specifiedProtectionParameter.If the specified alias already exists, it will be reassigned.
- Parameters:
alias- the alias for the entry.entry- the entry to store.param- theProtectionParameterto protect the entry.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.NullPointerException- ifaliasisnullorentryisnull.
-
entryInstanceOf
public final boolean entryInstanceOf(String alias, Class<? extends KeyStore.Entry> entryClass) throws KeyStoreExceptionIndicates whether the entry for the given alias is assignable to the providedClass.- Parameters:
alias- the alias for the entry.entryClass- the type of the entry.- Returns:
trueif theEntryfor the alias is assignable to the specifiedentryClass.- Throws:
KeyStoreException- if thisKeyStoreis not initialized.
-