KeyStore Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
This class represents a storage facility for cryptographic keys and certificates.
[Android.Runtime.Register("java/security/KeyStore", DoNotGenerateAcw=true)]
public class KeyStore : Java.Lang.Object
[<Android.Runtime.Register("java/security/KeyStore", DoNotGenerateAcw=true)>]
type KeyStore = class
inherit Object
- Inheritance
- Attributes
Remarks
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:
<ul> <li><b>KeyStore.PrivateKeyEntry</b>
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.
<li><b>KeyStore.SecretKeyEntry</b>
This type of entry holds a cryptographic SecretKey
, which is optionally stored in a protected format to prevent unauthorized access.
<li><b>KeyStore.TrustedCertificateEntry</b>
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. </ul>
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.
<ul> <li>To specify an existing keystore file:
// 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(java.io.InputStream, char[]) load
method.
<li>To rely on the default type:
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
The system will return a keystore implementation for the default type.
<li>To provide a specific keystore type:
KeyStore ks = KeyStore.getInstance("JKS");
The system will return the most preferred implementation of the specified keystore type available in the environment. </ul>
Before a keystore can be accessed, it must be #load(java.io.InputStream, char[]) loaded
.
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.ProtectionParameter protParam =
new KeyStore.PasswordProtection(password);
// 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
try (FileOutputStream fos = new FileOutputStream("newKeyStoreName")) {
ks.store(fos, password);
}
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.
Android provides the following KeyStore
types: <table> <thead> <tr> <th>Algorithm</th> <th>Supported API Levels</th> </tr> </thead> <tbody> <tr> <td>AndroidCAStore</td> <td>14+</td> </tr> <tr> <td>AndroidKeyStore</td> <td>18+</td> </tr> <tr class="deprecated"> <td>BCPKCS12</td> <td>1-8</td> </tr> <tr> <td>BKS</td> <td>1+</td> </tr> <tr> <td>BouncyCastle</td> <td>1+</td> </tr> <tr> <td>PKCS12</td> <td>1+</td> </tr> <tr class="deprecated"> <td>PKCS12-DEF</td> <td>1-8</td> </tr> </tbody> </table>
These types are described in the KeyStore section of the Java Cryptography Architecture Standard Algorithm Name Documentation.
Added in 1.2.
Java documentation for java.security.KeyStore
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
KeyStore(IntPtr, JniHandleOwnership) |
A constructor used when creating managed representations of JNI objects; called by the runtime. |
KeyStore(KeyStoreSpi, Provider, String) |
Creates a KeyStore object of the given type, and encapsulates the given provider implementation (SPI object) in it. |
Properties
Class |
Returns the runtime class of this |
DefaultType |
Returns the default keystore type as specified by the
|
Handle |
The handle to the underlying Android instance. (Inherited from Object) |
JniIdentityHashCode | (Inherited from Object) |
JniPeerMembers | |
PeerReference | (Inherited from Object) |
Provider |
Returns the provider of this keystore. |
ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. |
Type |
Returns the type of this keystore. |
Methods
Aliases() |
Lists all the alias names of this keystore. |
Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
ContainsAlias(String) |
Checks if the given alias exists in this keystore. |
DeleteEntry(String) |
Deletes the entry identified by the given alias from this keystore. |
Dispose() | (Inherited from Object) |
Dispose(Boolean) | (Inherited from Object) |
EntryInstanceOf(String, Class) |
Determines if the keystore |
Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
GetCertificate(String) |
Returns the certificate associated with the given alias. |
GetCertificateAlias(Certificate) |
Returns the (alias) name of the first keystore entry whose certificate matches the given certificate. |
GetCertificateChain(String) |
Returns the certificate chain associated with the given alias. |
GetCreationDate(String) |
Returns the creation date of the entry identified by the given alias. |
GetEntry(String, KeyStore+IProtectionParameter) |
Gets a keystore |
GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
GetInstance(File, Char[]) |
Returns a loaded keystore object of the appropriate keystore type. |
GetInstance(File, KeyStore+ILoadStoreParameter) | |
GetInstance(String, Provider) |
Returns a keystore object of the specified type. |
GetInstance(String, String) |
Returns a keystore object of the specified type. |
GetInstance(String) |
Returns a keystore object of the specified type. |
GetKey(String, Char[]) |
Returns the key associated with the given alias, using the given password to recover it. |
IsCertificateEntry(String) |
Returns true if the entry identified by the given alias
was created by a call to |
IsKeyEntry(String) |
Returns true if the entry identified by the given alias
was created by a call to |
JavaFinalize() |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
Load(KeyStore+ILoadStoreParameter) |
Loads this KeyStore from the given input stream. |
Load(Stream, Char[]) |
Loads this KeyStore from the given input stream. |
Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
SetCertificateEntry(String, Certificate) |
Assigns the given trusted certificate to the given alias. |
SetEntry(String, KeyStore+IEntry, KeyStore+IProtectionParameter) |
Saves a keystore |
SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
SetKeyEntry(String, Byte[], Certificate[]) |
Assigns the given key (that has already been protected) to the given alias. |
SetKeyEntry(String, IKey, Char[], Certificate[]) |
Assigns the given key to the given alias, protecting it with the given password. |
Size() |
Retrieves the number of entries in this keystore. |
Store(KeyStore+ILoadStoreParameter) |
Stores this keystore to the given output stream, and protects its integrity with the given password. |
Store(Stream, Char[]) |
Stores this keystore to the given output stream, and protects its integrity with the given password. |
ToArray<T>() | (Inherited from Object) |
ToString() |
Returns a string representation of the object. (Inherited from Object) |
UnregisterFromRuntime() | (Inherited from Object) |
Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
IJavaPeerable.Disposed() | (Inherited from Object) |
IJavaPeerable.DisposeUnlessReferenced() | (Inherited from Object) |
IJavaPeerable.Finalized() | (Inherited from Object) |
IJavaPeerable.JniManagedPeerState | (Inherited from Object) |
IJavaPeerable.SetJniIdentityHashCode(Int32) | (Inherited from Object) |
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) | (Inherited from Object) |
IJavaPeerable.SetPeerReference(JniObjectReference) | (Inherited from Object) |
Extension Methods
JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
JavaCast<TResult>(IJavaObject) | |
GetJniTypeName(IJavaPeerable) |