public class PKCS12KeyStore
extends java.security.KeyStoreSpi
KeyStore
Service Provider Interface
(SPI) to load and save PKCS#12
files, which contain private key entries (private keys and certificate chains)
and certificate entries (X.509 certificates).
Note that there has been a change in the API from version IAIK-JCE 5.2 to IAIK-JCE 5.3. The PKCS12KeyStore implementation of version 5.2 (or lower) only supported private key entries (private keys and certificate chains) as this is the most widely used format for saving private keys with certificates in files (supported by Microsoft WindowsTM, Microsoft Internet ExplorerTM, Microsoft OutlookTM, Mozilla Firefox, Mozilla Thunderbird,...).
If you want to instantiate a KeyStore
, specify keystore type and
provider.
KeyStore ks = KeyStore.getInstance("PKCS12", "IAIK");Before a
KeyStore
can be used, it must be loaded.
ks.load(inputStream, password);You must even load the
KeyStore
in case you instantiate a new
empty KeyStore
object. In this case, the parameters shall be
null
.
ks.load(null, null);
Implementation Details
This implementation of theKeyStoreSpi
loads and stores
files according to the PKCS#12 v1.0: Personal Information Exchange
Syntax. You can get this document from the RSA
Laboratories.
The keystore loads private keys and certificate chains from a
password-encrypted PKCS#12-file (e.g. .pfx, .p12). Inside the PKCS#12 file,
the localKeyID is used to match the private key with the corresponding
certificate. KeyEntries, set with method
KeyStore.setKeyEntry(String, byte[], Certificate[])
or
KeyStore.setKeyEntry(String, Key, char[], Certificate[])
use the current time (Milliseconds since 1970) as key ID. When loading a
file, the creation date is extracted from this ID if possible. With method
KeyStore.store(OutputStream, char[])
, all keys and
certificate chains are saved in a PKCS#12 file.
This implementation supports multiple keys and certificate chains within one PKCS#12 file, and also multiple certificate entries.
PKCS#12 carries certificates in CertBags, and keys in KeyBags. The relationship between a certificate contained in a CertBag and the corresponding key contained in a KeyBag may be established by giving Cert- and KeyBag the same lokaleKeyId and/or friendlyName attribute. This KeyStore implementation follows the following strategy for getting/setting the ceritifcates for Key- and Certificate Entries from/for a PKCS#12 KeyStore:
keyStore.getCertificateChain
this certificate is located
at index 0 of the returned certificate chain. The remaining certificates
of the certificate chain are got from all other CertBags (regardless
of their attributes) by trying to build a certificate path for the
"index-0" certificate based on IssuerDN/SubjecDN
and AuthorityKeyIdentifier/SubjectKeyIdentifier (if available) relationship.
Constructor and Description |
---|
PKCS12KeyStore()
Creates a new PKCS#12
KeyStore object. |
Modifier and Type | Method and Description |
---|---|
java.util.Enumeration |
engineAliases() |
boolean |
engineContainsAlias(java.lang.String alias)
Checks if the given alias exists in this keystore.
|
void |
engineDeleteEntry(java.lang.String alias)
Deletes the entry identified by the given alias from this keystore.
|
java.security.cert.Certificate |
engineGetCertificate(java.lang.String alias)
Returns the certificate associated with the given alias.
|
java.lang.String |
engineGetCertificateAlias(java.security.cert.Certificate cert)
Returns the (alias) name of the first keystore entry whose certificate
matches the given certificate.
|
java.security.cert.Certificate[] |
engineGetCertificateChain(java.lang.String alias)
Returns the certificate chain associated with the given alias.
|
java.util.Date |
engineGetCreationDate(java.lang.String alias)
Returns the creation date of the entry identified by the given alias or -
if not available - the date on which the certificate became valid.
|
java.security.Key |
engineGetKey(java.lang.String alias,
char[] password)
Returns the key associated with the given alias, using the given password
to recover it.
|
boolean |
engineIsCertificateEntry(java.lang.String alias)
Checks if the entry with the given alias represents a certificate entry.
|
boolean |
engineIsKeyEntry(java.lang.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 |
engineLoad(java.io.InputStream stream,
char[] password)
Loads the keystore from the given input stream.
|
void |
engineSetCertificateEntry(java.lang.String alias,
java.security.cert.Certificate cert)
Assigns the given certificate to the given alias.
|
void |
engineSetKeyEntry(java.lang.String alias,
byte[] key,
java.security.cert.Certificate[] chain)
Assigns the given key (that has already been protected) to the given alias.
|
void |
engineSetKeyEntry(java.lang.String alias,
java.security.Key key,
char[] password,
java.security.cert.Certificate[] chain)
Assigns the given key to the given alias, protecting it with the given
password.
|
int |
engineSize() |
void |
engineStore(java.io.OutputStream stream,
char[] password)
Stores this keystore to the given output stream, and protects its integrity
with the given password.
|
public void engineLoad(java.io.InputStream stream, char[] password) throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.cert.CertificateException
Set both parameters to null
to initialize an new empty
keystore.
engineLoad
in class java.security.KeyStoreSpi
stream
- the input stream from which the keystore is loaded, or nullpassword
- the password used to check the integrity of the keystore, the
password used to unlock the keystore, or nulljava.io.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
incorrectjava.security.NoSuchAlgorithmException
- if the algorithm used to check the integrity of the keystore
cannot be foundjava.security.cert.CertificateException
- if any of the certificates in the keystore could not be loadedpublic java.util.Enumeration engineAliases()
engineAliases
in class java.security.KeyStoreSpi
public boolean engineContainsAlias(java.lang.String alias)
engineContainsAlias
in class java.security.KeyStoreSpi
alias
- The alias name.true
if the alias exists, false
otherwise.public void engineDeleteEntry(java.lang.String alias) throws java.security.KeyStoreException
engineDeleteEntry
in class java.security.KeyStoreSpi
alias
- The alias name.java.security.KeyStoreException
- If the entry cannot be removed.public java.security.cert.Certificate engineGetCertificate(java.lang.String alias)
engineGetCertificate
in class java.security.KeyStoreSpi
alias
- the alias namepublic java.lang.String engineGetCertificateAlias(java.security.cert.Certificate cert)
engineGetCertificateAlias
in class java.security.KeyStoreSpi
cert
- the certificate to match with.public java.security.cert.Certificate[] engineGetCertificateChain(java.lang.String alias)
engineGetCertificateChain
in class java.security.KeyStoreSpi
alias
- the alias namepublic java.util.Date engineGetCreationDate(java.lang.String alias)
engineGetCreationDate
in class java.security.KeyStoreSpi
alias
- the alias namepublic java.security.Key engineGetKey(java.lang.String alias, char[] password) throws java.security.NoSuchAlgorithmException, java.security.UnrecoverableKeyException
engineGetKey
in class java.security.KeyStoreSpi
alias
- the alias namepassword
- the password for recovering the keyjava.security.NoSuchAlgorithmException
- if the algorithm for recovering the key cannot be foundjava.security.UnrecoverableKeyException
- if the key cannot be recovered (e.g. the given password is
wrong).public boolean engineIsCertificateEntry(java.lang.String alias)
engineIsCertificateEntry
in class java.security.KeyStoreSpi
alias
- the alias for the keystore entry to be checkedpublic boolean engineIsKeyEntry(java.lang.String alias)
engineIsKeyEntry
in class java.security.KeyStoreSpi
alias
- the alias for the keystore entry to be checkedpublic void engineSetCertificateEntry(java.lang.String alias, java.security.cert.Certificate cert) throws java.security.KeyStoreException
engineSetCertificateEntry
in class java.security.KeyStoreSpi
alias
- the alias namecert
- the certificate to be associated with the aliasjava.security.KeyStoreException
- if this operation fails, e.g. the given alias was already
used for a KeyEntry of this keystorepublic void engineSetKeyEntry(java.lang.String alias, byte[] key, java.security.cert.Certificate[] chain) throws java.security.KeyStoreException
engineSetKeyEntry
in class java.security.KeyStoreSpi
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).java.security.KeyStoreException
- if this operation fails, e.g. the given alias was already
used for a CertEntry of this keystorepublic void engineSetKeyEntry(java.lang.String alias, java.security.Key key, char[] password, java.security.cert.Certificate[] chain) throws java.security.KeyStoreException
PrivateKey
, it
must be accompanied by a certificate chain certifying the corresponding
public key. This certificate chain must be arranged having the end-user
certificate at index 0 and the top-CA-certificate at the end of the chain.
If the given alias already exists and identifies another key entry, the
keystore information associated with it is overridden by the given key
and certificate chain. However, if an already exiting alias identifies
a cert entry, a KeyStore exception is thrown to not allow using the
same alias for key- and cert-entries.
The provided password is ignored. Only the password provided to the
engineStore(OutputStream, char[])
is used.
engineSetKeyEntry
in class java.security.KeyStoreSpi
alias
- the alias namekey
- the key to be associated with the aliaspassword
- the password to encrypt the key withchain
- the certificate chain for the corresponding public key (only
required if the given key is of type java.security.PrivateKey).java.security.KeyStoreException
- if the given key cannot be protected, or this operation fails for
some other reason, e.g. the given alias was already
used for a CertEntry of this keystorepublic int engineSize()
engineSize
in class java.security.KeyStoreSpi
public void engineStore(java.io.OutputStream stream, char[] password) throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.cert.CertificateException
engineStore
in class java.security.KeyStoreSpi
stream
- the output stream to which this keystore is written.password
- the password to generate the keystore integrity checkjava.io.IOException
- if there was an I/O problem with datajava.security.NoSuchAlgorithmException
- if the appropriate data integrity algorithm could not be foundjava.security.cert.CertificateException
- if any of the certificates included in the keystore data could
not be stored