public class KeyStore extends Object
 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 aliases are case-sensitive is implementation dependent. In order to avoid problems, it is recommended not to use aliases in a KeyStore that only differ in case.
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 specifying an existing keystore file, relying on the default type and providing a specific keystore type. 
    // get keystore password
    char[] password = getPassword();
    // probe the keystore file and load the keystore entries
    KeyStore ks = KeyStore.getInstance(new File("keyStoreName"), password);
 The system will probe the specified file to determine its keystore type and return a keystore implementation with its entries already loaded. When this approach is used there is no need to call the keystore's load method. 
    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 (unless it was already loaded during instantiation). 
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    // get user password and file input stream
    char[] password = getPassword();
    try (FileInputStream fis = new FileInputStream("keyStoreName")) {
        ks.load(fis, password);
    }
  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:
    KeyStore.PasswordProtection protParam =
        new KeyStore.PasswordProtection(password);
    try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
        // get my private key
        KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
            ks.getEntry("privateKeyAlias", protParam);
        PrivateKey myPrivateKey = pkEntry.getPrivateKey();
        // save my secret key
        javax.crypto.SecretKey mySecretKey;
        KeyStore.SecretKeyEntry skEntry =
            new KeyStore.SecretKeyEntry(mySecretKey);
        ks.setEntry("secretKeyAlias", skEntry, protParam);
        // store away the keystore
        ks.store(fos, password);
    } finally {
        protParam.destroy();
    }
  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.  Every implementation of the Java platform is required to support the following standard KeyStore type: 
PKCS12| Modifier and Type | Class | Description | 
|---|---|---|
| static class  | KeyStore.Builder | A description of a to-be-instantiated  KeyStoreobject. | 
| static class  | KeyStore.CallbackHandlerProtection | A  ProtectionParameterencapsulating a CallbackHandler. | 
| static interface  | KeyStore.Entry | A marker interface for  KeyStoreentry types. | 
| static interface  | KeyStore.LoadStoreParameter | |
| static class  | KeyStore.PasswordProtection | A password-based implementation of  ProtectionParameter. | 
| static final class  | KeyStore.PrivateKeyEntry | A  KeyStoreentry that holds aPrivateKeyand corresponding certificate chain. | 
| static interface  | KeyStore.ProtectionParameter | A marker interface for keystore protection parameters. | 
| static final class  | KeyStore.SecretKeyEntry | A  KeyStoreentry that holds aSecretKey. | 
| static final class  | KeyStore.TrustedCertificateEntry | A  KeyStoreentry that holds a trustedCertificate. | 
| Modifier | Constructor | Description | 
|---|---|---|
| protected  | Creates a  KeyStoreobject of the given type, and encapsulates the given provider implementation (SPI object) in it. | 
| Modifier and Type | Method | Description | 
|---|---|---|
| final Enumeration | aliases() | Lists all the alias names of this keystore. | 
| final boolean | containsAlias | Checks if the given alias exists in this keystore. | 
| final void | deleteEntry | Deletes the entry identified by the given alias from this keystore. | 
| final boolean | entryInstanceOf | Determines if the keystore  Entryfor the specifiedaliasis an instance or subclass of the specifiedentryClass. | 
| final Set | getAttributes | Retrieves the attributes associated with the given alias. | 
| final Certificate | getCertificate | Returns the certificate associated with the given alias. | 
| final String | getCertificateAlias | Returns the (alias) name of the first keystore entry whose certificate matches the given certificate. | 
| final Certificate[] | getCertificateChain | Returns the certificate chain associated with the given alias. | 
| final Date | getCreationDate | Returns the creation date of the entry identified by the given alias. | 
| static final String | getDefaultType() | Returns the default keystore type as specified by the  keystore.typesecurity property, or the string "jks" (acronym for "Java keystore") if no such property exists. | 
| final KeyStore.Entry | getEntry | Gets a keystore  Entryfor the specified alias with the specified protection parameter. | 
| static final KeyStore | getInstance | Returns a loaded keystore object of the appropriate keystore type. | 
| static final KeyStore | getInstance | Returns a loaded keystore object of the appropriate keystore type. | 
| static KeyStore | getInstance | Returns a  KeyStoreobject of the specified type. | 
| static KeyStore | getInstance | Returns a  KeyStoreobject of the specified type. | 
| static KeyStore | getInstance | Returns a  KeyStoreobject of the specified type. | 
| final Key | getKey | Returns the key associated with the given alias, using the given password to recover it. | 
| final Provider | getProvider() | Returns the provider of this keystore. | 
| final String | getType() | Returns the type of this keystore. | 
| final boolean | isCertificateEntry | Returns  trueif the entry identified by the given alias was created by a call tosetCertificateEntry, or created by a call tosetEntrywith aTrustedCertificateEntry. | 
| final boolean | isKeyEntry | Returns  trueif the entry identified by the given alias was created by a call tosetKeyEntry, or created by a call tosetEntrywith aPrivateKeyEntryor aSecretKeyEntry. | 
| final void | load | Loads this keystore from the given input stream. | 
| final void | load | Loads this keystore using the given  LoadStoreParameter. | 
| final void | setCertificateEntry | Assigns the given trusted certificate to the given alias. | 
| final void | setEntry | Saves a keystore  Entryunder the specified alias. | 
| final void | setKeyEntry | Assigns the given key (that has already been protected) to the given alias. | 
| final void | setKeyEntry | Assigns the given key to the given alias, protecting it with the given password. | 
| final int | size() | Retrieves the number of entries in this keystore. | 
| final void | store | Stores this keystore to the given output stream, and protects its integrity with the given password. | 
| final void | store | Stores this keystore using the given  LoadStoreParameter. | 
protected KeyStore(KeyStoreSpi keyStoreSpi, Provider provider, String type)
KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it.keyStoreSpi - the provider implementation.provider - the provider.type - the keystore type.public static KeyStore getInstance(String type) throws KeyStoreException
KeyStore object of the specified type.  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. 
 Note that the list of registered providers may be retrieved via the Security.getProviders() method.
jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different from the order of providers returned by Security.getProviders().type - the type of keystore. See the KeyStore section in the  Java Security Standard Algorithm Names Specification for information about standard keystore types.KeyStoreException - if no provider supports a KeyStoreSpi implementation for the specified typeNullPointerException - if type is null
public static KeyStore getInstance(String type, String provider) throws KeyStoreException, NoSuchProviderException
KeyStore object of the specified type.  A new KeyStore object encapsulating the KeyStoreSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list. 
 Note that the list of registered providers may be retrieved via the Security.getProviders() method.
type - the type of keystore. See the KeyStore section in the  Java Security Standard Algorithm Names Specification for information about standard keystore types.provider - the name of the provider.IllegalArgumentException - if the provider name is null or emptyKeyStoreException - if a KeyStoreSpi implementation for the specified type is not available from the specified providerNoSuchProviderException - if the specified provider is not registered in the security provider listNullPointerException - if type is null
public static KeyStore getInstance(String type, Provider provider) throws KeyStoreException
KeyStore object of the specified type.  A new KeyStore object encapsulating the KeyStoreSpi implementation from the specified provider object is returned. Note that the specified provider object does not have to be registered in the provider list.
type - the type of keystore. See the KeyStore section in the  Java Security Standard Algorithm Names Specification for information about standard keystore types.provider - the provider.IllegalArgumentException - if the specified provider is null
KeyStoreException - if KeyStoreSpi implementation for the specified type is not available from the specified Provider objectNullPointerException - if type is null
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 Provider getProvider()
public final String getType()
public final Set<KeyStore.Entry.Attribute> getAttributes(String alias) throws KeyStoreException
alias - the alias nameSet of attributes. This set is empty if the KeyStoreSpi implementation has not overridden KeyStoreSpi.engineGetAttributes(String), or the given alias does not exist, or there are no attributes associated with the alias. This set may also be empty for PrivateKeyEntry or SecretKeyEntry entries that contain protected attributes and are only available through the KeyStore.Entry.getAttributes() method after the entry is extracted.KeyStoreException - if the keystore has not been initialized (loaded).NullPointerException - if alias is null
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 keynull if the given alias does not exist or does not identify a key-related entry.KeyStoreException - 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 namenull if the given alias does not exist or does not contain a certificate chainKeyStoreException - 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 namenull if the given alias does not exist or does not contain a certificate.KeyStoreException - if the keystore has not been initialized (loaded).public final Date getCreationDate(String alias) throws KeyStoreException
alias - the alias namenull if the given alias does not existKeyStoreException - if the keystore has not been initialized (loaded).public final void setKeyEntry(String alias, Key key, char[] password, Certificate[] chain) throws KeyStoreException
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).
alias - the alias namekey - the key to be associated with the aliaspassword - the password to protect the keychain - the certificate chain for the corresponding public key (only required if the given key is of type java.security.PrivateKey).KeyStoreException - if the keystore has not been initialized (loaded), the given key cannot be protected, or this operation fails for some other reasonpublic 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 nametrue if the alias exists, false otherwiseKeyStoreException - 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
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.alias - the alias for the keystore entry to be checkedtrue if the entry identified by the given alias is a key-related entry, false otherwise.KeyStoreException - if the keystore has not been initialized (loaded).public final boolean isCertificateEntry(String alias) throws KeyStoreException
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.alias - the alias for the keystore entry to be checkedtrue if the entry identified by the given alias contains a trusted certificate, false otherwise.KeyStoreException - if the keystore has not been initialized (loaded).public final String getCertificateAlias(Certificate cert) throws KeyStoreException
 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 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.
cert - the certificate to match with.null if no such entry exists in this keystore.KeyStoreException - if the keystore has not been initialized (loaded).public final void store(OutputStream stream, char[] password) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
stream - the output stream to which this keystore is written.password - the password to generate the keystore integrity check. May be null if the keystore does not support or require an integrity check.KeyStoreException - if the keystore has not been initialized (loaded).IOException - if there was an I/O problem with dataNoSuchAlgorithmException - if the appropriate data integrity algorithm could not be foundCertificateException - if any of the certificates included in the keystore data could not be storedpublic final void store(KeyStore.LoadStoreParameter param) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
LoadStoreParameter.param - the LoadStoreParameter that specifies how to store the keystore, which may be null
IllegalArgumentException - if the given LoadStoreParameter input is not recognizedKeyStoreException - if the keystore has not been initialized (loaded)IOException - if there was an I/O problem with dataNoSuchAlgorithmException - if the appropriate data integrity algorithm could not be foundCertificateException - if any of the certificates included in the keystore data could not be storedUnsupportedOperationException - if this operation is not supportedpublic 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 loadedpublic final void load(KeyStore.LoadStoreParameter param) throws IOException, NoSuchAlgorithmException, CertificateException
LoadStoreParameter.  Note that if this KeyStore has already been loaded, it is reinitialized and loaded again from the given parameter.
param - the LoadStoreParameter that specifies how to load the keystore, which may be null
IllegalArgumentException - if the given LoadStoreParameter input is not recognizedIOException - if there is an I/O or format problem with the keystore data. If the error is due to an incorrect ProtectionParameter (e.g. 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 loadedpublic final KeyStore.Entry getEntry(String alias, KeyStore.ProtectionParameter protParam) throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException
Entry for the specified alias with the specified protection parameter.alias - get the keystore Entry for this aliasprotParam - the ProtectionParameter used to protect the Entry, which may be null
Entry for the specified alias, or null if there is no such entryNullPointerException - if alias is null
NoSuchAlgorithmException - if the algorithm for recovering the entry cannot be foundUnrecoverableEntryException - if the specified protParam were insufficient or invalidUnrecoverableKeyException - if the entry is a PrivateKeyEntry or SecretKeyEntry and the specified protParam does not contain the information needed to recover the key (e.g. wrong password)KeyStoreException - if the keystore has not been initialized (loaded).public final void setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter protParam) throws KeyStoreException
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.
alias - save the keystore Entry under this aliasentry - the Entry to saveprotParam - the ProtectionParameter used to protect the Entry, which may be null
NullPointerException - if alias or entry is null
KeyStoreException - if the keystore has not been initialized (loaded), or if this operation fails for some other reasonpublic final boolean entryInstanceOf(String alias, Class<? extends KeyStore.Entry> entryClass) throws KeyStoreException
Entry for the specified alias is an instance or subclass of the specified entryClass.alias - the alias nameentryClass - the entry classtrue if the keystore Entry for the specified alias is an instance or subclass of the specified entryClass, false otherwiseNullPointerException - if alias or entryClass is null
KeyStoreException - if the keystore has not been initialized (loaded)public static final KeyStore getInstance(File file, char[] password) throws KeyStoreException, 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.
 This method traverses the list of registered security providers, starting with the most preferred provider. For each KeyStoreSpi implementation supported by a provider, it invokes the engineProbe method to determine if it supports the specified keystore. A new KeyStore object is returned that encapsulates the KeyStoreSpi implementation from the first provider that supports the specified file. 
 Note that the list of registered providers may be retrieved via the Security.getProviders() method.
file - the keystore filepassword - the keystore password, which may be null
KeyStoreException - if no provider supports a KeyStoreSpi implementation for the specified keystore file.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 found.CertificateException - if any of the certificates in the keystore could not be loaded.IllegalArgumentException - if file does not exist or does not refer to a normal file.NullPointerException - if file is null.SecurityException - if a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the specified file.public static final KeyStore getInstance(File file, KeyStore.LoadStoreParameter param) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException
LoadStoreParameter may be supplied which specifies how to unlock the keystore data or perform an integrity check.  This method traverses the list of registered security providers, starting with the most preferred provider. For each KeyStoreSpi implementation supported by a provider, it invokes the engineProbe method to determine if it supports the specified keystore. A new KeyStore object is returned that encapsulates the KeyStoreSpi implementation from the first provider that supports the specified file. 
 Note that the list of registered providers may be retrieved via the Security.getProviders() method.
file - the keystore fileparam - the LoadStoreParameter that specifies how to load the keystore, which may be null
KeyStoreException - if no provider supports a KeyStoreSpi implementation for the specified keystore file.IOException - if there is an I/O or format problem with the keystore data. If the error is due to an incorrect ProtectionParameter (e.g. 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 found.CertificateException - if any of the certificates in the keystore could not be loaded.IllegalArgumentException - if file does not exist or does not refer to a normal file, or if param is not recognized.NullPointerException - if file is null.SecurityException - if a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor) method denies read access to the specified file.
    © 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
    https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/KeyStore.html