CompositeKeyStore

public class CompositeKeyStore

Composite keystore which manages keystores of different types. A typical Java keystore supports only one keystore type per file. That will be inconvenient for applications to manage several types of keystore. Also, this composite keystore supports managing multiple keystore files. This can be viewed as a keystore registry, that is, this object manages a pool of keystore files.

Author:kcyee

Fields

cache

protected Hashtable cache

Internal storage of the aliases inside the keystore file

keystores

protected Vector keystores

Internal storage of the keystore object

storage

protected Hashtable storage

Internal storage of the keystore file information

Constructors

CompositeKeyStore

public CompositeKeyStore()

Default constructor. The internal variables are being initialized.

Methods

addKeyStoreFile

public void addKeyStoreFile(String keyFile, String type, char[] password)

Adds a keystore file to the keystore management pool.

Parameters:
  • keyFile – the name of the keystore file
  • type – the type of the keystore
  • password – the password for accessing the keystore

addKeyStoreFile

protected void addKeyStoreFile(File keyFile, String type, char[] password)

Adds a keystore file to the keystore management pool.

Parameters:
  • keyFile – the keystore file
  • type – the type of the keystore
  • password – the password for accessing the keystore

aliases

public Enumeration aliases()

Gets all the aliases of the keystores pointed by this composite keystore.

Returns:an enumeration of string, holding the aliases of the keys

containsAlias

public boolean containsAlias(String alias)

Determines whether a given alias exists in one of the keystores pointed by this composite keystore or not.

Parameters:
  • alias – the alias of the key/certificate
Returns:

true if the alias exists in one of the keystores, false & otherwise

getCertificate

public Certificate getCertificate(String alias)

Gets the certificate named by the given alias, from the collection of keystores pointed by this composite keystore.

Parameters:
  • alias – the alias of the key/certificate
Throws:
  • KeyStoreException – the keystore is corrupted
Returns:

the certificate named by the given alias, null if not found

getCertificateAlias

public String getCertificateAlias(Certificate cert)

Gets the alias of the specified certificate.

Parameters:
  • cert – the certificate
Returns:

the alias of the certificate, if the certificate can be found in the collection of keystores pointed by this composite keystore. Otherwise, null will be returned

getCertificateChain

public Certificate[] getCertificateChain(String alias)

Gets the certificate chain by the specified alias.

Parameters:
  • alias – the alias of the key/certificate
Throws:
  • KeyStoreException – the keystore is corrupted
Returns:

the certificate chain by the specified alias, null if not found

getCreationDate

public Date getCreationDate(String alias)

Gets the creation date of the key/certificate by the specified alias.

Parameters:
  • alias – the alias of the key/certificate
Throws:
  • KeyStoreException – the keystore is corrupted
Returns:

the creation date of the key/certificate by the specified alias, null if not found

getKey

public Key getKey(String alias, char[] password)

Gets the key by the specified alias. A password should be given also to retrieve the key.

Parameters:
  • alias – the alias of the key/certificate
  • password – the password to retrieve the key
Throws:
  • KeyStoreException – the keystore is corrupted
  • NoSuchAlgorithmException – the keystore cannot be read
  • UnrecoverableKeyException – the keystore cannot be read
Returns:

the key specified by the alias, null if not found

getKeyStore

public KeyStore getKeyStore()

Gets the first KeyStore object from the keystore management pool.

Returns:the first KeyStore object from the keystore management pool

isCertificateEntry

public boolean isCertificateEntry(String alias)

Determines whether the specified alias is specifying a certificate or not.

Parameters:
  • alias – the alias of the key/certificate
Throws:
  • KeyStoreException – the keystore is corrupted

isKeyEntry

public boolean isKeyEntry(String alias)

Determines whether the specified alias is specifying a key or not.

Parameters:
  • alias – the alias of the key/certificate
Throws:
  • KeyStoreException – the keystore is corrupted

load

public void load(String storeFileName)

Loads the composite keystore from a persistent file in the file system.

Parameters:
  • storeFileName – the name of the composite keystore persistent file
Throws:

load

public void load(File storeFile)

Loads the composite keystore from a persistent file in the file system.

Parameters:
  • storeFile – the composite keystore persistent file
Throws:

loadCache

protected void loadCache()

Loads the keystores pointed by this composite keystore into memory and create a caching of aliases.

loadKeyStore

protected KeyStore loadKeyStore(String fileName, KeyStoreFileProp ksp)

Gets an instance of the keystore of correct type. This function will consider the Java version and determine whether to use JSSE or not. For Java version 1.4 or above, JSSE is built in. So, no need to call an external provider to create an instance of PKCS#12 formatted keystore. Otherwise, JSSE should be used, and we make use of dynamic binding to load the JSSE library.

Parameters:
  • fileName – the keystore file name to load
  • ksp – other keystore parameters for loading
Returns:

keystore instance of the correct type

removeKeyStoreFile

public void removeKeyStoreFile(String keyFile)

Removes a keystore file from the keystore management pool.

Parameters:
  • keyFile – the name of the keystore file

removeKeyStoreFile

protected void removeKeyStoreFile(File keyFile)

Removes a keystore file from the keystore management pool.

Parameters:
  • keyFile – the keystore file

size

public int size()

Gets the total number of keys/certificates in all the keystores pointed by this composite keystore.

Returns:the total number of keys/certificates

store

public void store(String storeFileName)

Stores the composite keystore to a persistent file in the file system.

Parameters:
  • storeFileName – the name of the composite keystore persistent file
Throws:
  • StoreException – the composite keystore is not successfully stored

store

public void store(File storeFile)

Stores the composite keystore to a persistent file in the file system.

Parameters:
  • storeFile – the composite keystore persistent file
Throws:
  • StoreException – the composite keystore is not successfully stored