Package org.forgerock.opendj.security
Class KeyManagers
java.lang.Object
org.forgerock.opendj.security.KeyManagers
This class contains methods for creating common types of key manager.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAllows to return a security provider that can be used to load a keystore or a truststore. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic X509KeyManagerfindX509KeyManager(KeyStore keyStore, char[] password) Returns aX509KeyManagerfor the given key store and password.static booleanReturnstrueif the PKCS#11 keystore type can be associated to the provided keystore path.static KeyStoreReturns the JVM default keystore.static KeyStoreloadKeyStore(String keyStoreType, char[] password) Loads and then returns a new key store created with the provided type, and password.static KeyStoreloadKeyStore(String keyStorePath, char[] password, String keyStoreType, Provider provider) Loads and then returns a new key store created with the provided path, password, type and provider.static KeyStoreloadKeyStore(String keyStoreType, char[] password, Provider provider) Loads and then returns a new key store created with the provided type, password and provider.static KeyStoreloadKeyStore(Path keyStorePath, char[] password, String keyStoreType) Loads and then returns a new key store created with the provided path, password and type.static KeyStoreloadKeyStore(Path keyStorePath, char[] password, String keyStoreType, Provider provider) Loads and then returns a new key store created with the provided path, password, type and java security provider.static KeyStoreresolveKeyStore(String keyStoreType, Provider provider) Returns a new key store object created with the provided type and provider.static X509KeyManageruseAnyCertificate(X509KeyManager keyManager, String componentName, Collection<String> aliases, boolean sniMatching) Returns a newX509KeyManagerwhich selects one of the named certificates from the provided key manager.static X509KeyManagerCreates a newX509KeyManagerwhich will use the JVM's default keystore for retrieving certificates.static X509KeyManageruseKeyStoreFile(String file) Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates.static X509KeyManageruseKeyStoreFile(String file, char[] password, String type) Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates.static X509KeyManageruseKeyStoreFile(String keyStoreFile, char[] password, String type, String providerName) Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates.static X509KeyManagerusePkcs11Token(char[] password) Creates a newX509KeyManagerwhich will use a PKCS#11 token for retrieving certificates.static X509KeyManageruseSingleCertificate(X509KeyManager keyManager, String alias) Returns a newX509KeyManagerwhich selects the named certificate from the provided key manager.
-
Field Details
-
PKCS12
PKCS12 keystore type.- See Also:
-
JCEKS
JCEKS keystore type.- See Also:
-
JKS
JKS keystore type.- See Also:
-
PKCS11
PKCS11 keystore type.- See Also:
-
JVM
JVM keystore type.- See Also:
-
-
Method Details
-
useKeyStoreFile
public static X509KeyManager useKeyStoreFile(String file) throws GeneralSecurityException, IOException Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates. It will use the default key store type for the JVM (e.g.PKCS12) and will not use a password to open the key store.- Parameters:
file- The key store file name.- Returns:
- A new
X509KeyManagerwhich will use the named key store file for retrieving certificates. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to incorrect type, or missing algorithms.IOException- If the key store file could not be found or could not be read.NullPointerException- If provided file wasnull.
-
useKeyStoreFile
public static X509KeyManager useKeyStoreFile(String file, char[] password, String type) throws GeneralSecurityException, IOException Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates. It will use the provided key store type and password.- Parameters:
file- The key store file name.password- The key store password, which may benull.type- The key store type, which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.- Returns:
- A new
X509KeyManagerwhich will use the named key store file for retrieving certificates. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to incorrect type, or missing algorithms.IOException- If the key store file could not be found or could not be read.NullPointerException- Iffilewasnull.
-
useKeyStoreFile
public static X509KeyManager useKeyStoreFile(String keyStoreFile, char[] password, String type, String providerName) throws GeneralSecurityException, IOException Creates a newX509KeyManagerwhich will use the named key store file for retrieving certificates. It will use the provided key store type and password.- Parameters:
keyStoreFile- The key store file name.password- The key store password, which may benull.type- The key store type, which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.providerName- The key store provider, which may benullto indicate that the default key store provider for the JVM should be used.- Returns:
- A new
X509KeyManagerwhich will use the named key store file for retrieving certificates. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to incorrect type, or missing algorithms.IOException- If the key store file could not be found or could not be read.NullPointerException- If provided file wasnull.
-
usePkcs11Token
public static X509KeyManager usePkcs11Token(char[] password) throws GeneralSecurityException, IOException Creates a newX509KeyManagerwhich will use a PKCS#11 token for retrieving certificates.- Parameters:
password- The password to use for accessing the PKCS#11 token, which may benullif no password is required.- Returns:
- A new
X509KeyManagerwhich will use a PKCS#11 token for retrieving certificates. - Throws:
GeneralSecurityException- If the PKCS#11 token could not be accessed, perhaps due to incorrect password, or missing algorithms.IOException- If ever an I/O error occurs
-
findX509KeyManager
public static X509KeyManager findX509KeyManager(KeyStore keyStore, char[] password) throws GeneralSecurityException Returns aX509KeyManagerfor the given key store and password.- Parameters:
keyStore- The key store to use.password- The password to use for accessing the key store, which may benull.- Returns:
- An
X509KeyManagerfor the given key store and password. - Throws:
GeneralSecurityException- If the key manager could not be found.
-
jvmDefaultKeyStore
Returns the JVM default keystore.- Returns:
- The JVM default keystore or
nullif the necessary JVM settings are missing. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to incorrect type, or missing algorithms.IOException- If the key store file could not be found or could not be read.
-
useJvmDefaultKeyManager
Creates a newX509KeyManagerwhich will use the JVM's default keystore for retrieving certificates.- Returns:
- A new
X509KeyManagerwhich will use the JVM's default keystore for retrieving certificates ornullif the necessary JVM settings are missing. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to incorrect type, or missing algorithms.IOException- If the key store file could not be found or could not be read.
-
useSingleCertificate
Returns a newX509KeyManagerwhich selects the named certificate from the provided key manager. No SNI matching will be performed.- Parameters:
keyManager- The key manager to be filtered.alias- The alias of the certificate that should be selected for operations involving this key manager.- Returns:
- The filtered key manager.
- Throws:
NullPointerException- If provided keyManager or alias wasnull.
-
useAnyCertificate
public static X509KeyManager useAnyCertificate(X509KeyManager keyManager, String componentName, Collection<String> aliases, boolean sniMatching) Returns a newX509KeyManagerwhich selects one of the named certificates from the provided key manager.- Parameters:
keyManager- The key manager to be filtered.componentName- The optional user-friendly name of the component using the key manager. This will be used in error messages when the key manager fails to find a suitable certificate for the SSL handshake. May benullin which case the component name will be "unknown".aliases- The aliases of the certificates that can be selected for operations involving this key manager.sniMatching- Should the server name indication (SNI) be matched against the certificate SANs.- Returns:
- The filtered key manager.
- Throws:
NullPointerException- If provided keyManager or aliases wasnullor empty.
-
loadKeyStore
public static KeyStore loadKeyStore(String keyStoreType, char[] password) throws GeneralSecurityException, IOException Loads and then returns a new key store created with the provided type, and password.- Parameters:
keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.password- The password to use for loading the key store, might benullif the key store has no password.- Returns:
- A
KeyStorealready loaded which can be used for creating anX509KeyManager. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to an incorrect type, missing algorithms or a missing provider.IOException- If the key store should be loaded from a file which could not be found or could not be read.
-
loadKeyStore
public static KeyStore loadKeyStore(String keyStoreType, char[] password, Provider provider) throws GeneralSecurityException, IOException Loads and then returns a new key store created with the provided type, password and provider.- Parameters:
keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.password- The password to use for loading the key store, might benullif the key store has no password.provider- The provider to use for loading the key store. Can benull, in which case the most appropriate provider from the list of registered security providers will be used.- Returns:
- A
KeyStorealready loaded which can be used for creating anX509KeyManager. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to an incorrect type, missing algorithms or a missing provider.IOException- If the key store should be loaded from a file which could not be found or could not be read.- See Also:
-
loadKeyStore
public static KeyStore loadKeyStore(String keyStorePath, char[] password, String keyStoreType, Provider provider) throws GeneralSecurityException, IOException Loads and then returns a new key store created with the provided path, password, type and provider.- Parameters:
keyStorePath- The path of the key store to create. It can benull, empty or set to NONE if the keystore should be load from an hardware token (associated key store type isPKCS11).password- The password to use for loading the key store, might benullif the key store has no password.keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.provider- The provider to use for loading the key store. Can benull, in which case the most appropriate provider from the list of registered security providers will be used.- Returns:
- A
KeyStorealready loaded which can be used for creating anX509KeyManager. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to an incorrect type, missing algorithms or a missing provider.IOException- If the key store should be loaded from a file which could not be found or could not be read.- See Also:
-
loadKeyStore
public static KeyStore loadKeyStore(Path keyStorePath, char[] password, String keyStoreType) throws GeneralSecurityException, IOException Loads and then returns a new key store created with the provided path, password and type.The most preferred provider from the list of registered security provider will be used to create the
KeyStore object.- Parameters:
keyStorePath- The path of the key store to create. It can benullif the keystore should be load from an hardware token (associated key store type isPKCS11).password- The password to use for loading the key store, might benullif the key store has no password.keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.- Returns:
- A
KeyStorealready loaded which can be used for creating anX509KeyManager. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to an incorrect type or missing algorithms.IOException- If the key store should be loaded from a file which could not be found or could not be read.- See Also:
-
loadKeyStore
public static KeyStore loadKeyStore(Path keyStorePath, char[] password, String keyStoreType, Provider provider) throws GeneralSecurityException, IOException Loads and then returns a new key store created with the provided path, password, type and java security provider.The most preferred provider from the list of registered security provider will be used to create the
KeyStore object.- Parameters:
keyStorePath- The path of the key store to create. It can benullif the keystore should be load from an hardware token (associated key store type isPKCS11).password- The password to use for loading the key store, might benullif the key store has no password.keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.provider- The provider to use for loading the key store. Can benull, in which case the most appropriate provider from the list of registered security providers will be used.- Returns:
- A
KeyStorealready loaded which can be used for creating anX509KeyManager. - Throws:
GeneralSecurityException- If the key store could not be loaded, perhaps due to an incorrect type or missing algorithms.IOException- If the key store should be loaded from a file which could not be found or could not be read.- See Also:
-
resolveKeyStore
public static KeyStore resolveKeyStore(String keyStoreType, Provider provider) throws KeyStoreException Returns a new key store object created with the provided type and provider.- Parameters:
keyStoreType- The key store type (e.g., "JKS", "PKCS12", "PKCS11"), which may benullto indicate that the default key store type for the JVM (e.g.PKCS12) should be used.provider- The provider to use for loading the key store. Can benull, in which case the most appropriate provider from the list of registered security providers will be used.- Returns:
- A
KeyStorewhich can be used for creating anX509KeyManager. - Throws:
KeyStoreException
-
isPkcs11
Returnstrueif the PKCS#11 keystore type can be associated to the provided keystore path.This method considers that the PKCS#11 type can be inferred if either:
- path is
null - path is a blank string
- path is set (case insensitive) to NONE (see documentation)
- Parameters:
keyStorePath- The keystore path to test- Returns:
trueif the PKCS#11 keystore type can be associated to the provided keystore path
- path is
-