public class KeyStoreUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static String |
BCFKS_EXT |
private static String |
CERT_ALIAS |
private static X500Principal |
CERT_DN |
private static String |
JKS_EXT |
private static String |
KEY_ALGORITHM |
private static String |
KEY_ALIAS |
private static Map<KeystoreType,String> |
KEY_STORE_EXTENSIONS |
private static Map<String,String> |
KEY_STORE_TYPE_PROVIDERS |
private static String |
KEYSTORE_ERROR_MSG |
private static org.slf4j.Logger |
logger |
private static int |
PASSWORD_LENGTH |
private static String |
PKCS12_EXT |
private static Map<KeystoreType,String> |
SECRET_KEY_STORE_PROVIDERS |
static String |
SUN_JSSE_PROVIDER_NAME |
static String |
SUN_PROVIDER_NAME |
private static String |
TEST_KEYSTORE_PREFIX |
private static String |
TEST_TRUSTSTORE_PREFIX |
private static String |
TRUSTSTORE_ERROR_MSG |
Constructor and Description |
---|
KeyStoreUtils() |
Modifier and Type | Method and Description |
---|---|
private static X509Certificate |
createKeyStoreAndGetX509Certificate(String alias,
String keyStorePassword,
String keyPassword,
String keyStorePath,
KeystoreType keyStoreType,
int certDurationDays,
String[] dnsSubjectAlternativeNames)
Loads the Keystore and returns a X509 Certificate with the given values.
|
static TlsConfiguration |
createTlsConfigAndNewKeystoreTruststore(TlsConfiguration tlsConfiguration,
int certDurationDays,
String[] dnsSubjectAlternativeNames)
Creates a temporary Keystore and Truststore and returns it wrapped in a new TLS configuration with the given values.
|
private static void |
createTrustStore(X509Certificate cert,
String alias,
String password,
String path,
KeystoreType truststoreType)
Loads the Truststore with the given values.
|
private static String |
generatePassword()
Generates a random Hex-encoded password.
|
private static Path |
generateTempKeystorePath(KeystoreType keystoreType)
Generates a temporary keystore file and returns the path.
|
private static Path |
generateTempTruststorePath(KeystoreType truststoreType)
Generates a temporary truststore file and returns the path.
|
static KeyManagerFactory |
getKeyManagerFactoryFromKeyStore(KeyStore keyStore,
char[] keystorePassword,
char[] keyPassword)
Returns the
KeyManagerFactory from the provided KeyStore object, initialized with the key or keystore password. |
static KeyStore |
getKeyStore(String keyStoreType)
Returns an empty KeyStore backed by the appropriate provider
|
private static String |
getKeystoreExtension(KeystoreType keystoreType)
Returns the Keystore extension given the Keystore type.
|
static String |
getKeyStoreProvider(String keyStoreType)
Returns the provider that will be used for the given keyStoreType
|
private static KeystoreType |
getKeystoreType(String keystoreTypeName) |
static KeystoreType |
getKeystoreTypeFromExtension(String keystorePath)
Get Keystore Type based on file extension defaults to returning PKCS12
|
static KeyStore |
getSecretKeyStore(String keystoreTypeName)
Returns an empty KeyStore for Secret Keys backed by the appropriate provider
|
static TrustManagerFactory |
getTrustManagerFactoryFromTrustStore(KeyStore trustStore)
Returns the
TrustManagerFactory from the provided KeyStore object, initialized. |
static boolean |
isKeyPasswordCorrect(URL keystore,
KeystoreType keystoreType,
char[] password,
char[] keyPassword)
Returns true if the given keystore can be loaded using the given keystore type and password and the default
(first) alias can be retrieved with the key-specific password.
|
static boolean |
isSecretKeyEntrySupported(KeystoreType keystoreType)
Is Secret Key Entry supported for specified Keystore Type
|
static boolean |
isStoreValid(URL keystore,
KeystoreType keystoreType,
char[] password)
Returns true if the given keystore can be loaded using the given keystore type and password.
|
private static KeyStore |
loadEmptyKeyStore(KeystoreType keyStoreType)
Loads and returns an empty Keystore backed by the appropriate provider.
|
static KeyManagerFactory |
loadKeyManagerFactory(String keystorePath,
String keystorePassword,
String keyPassword,
String keystoreType)
Returns the initialized
KeyManagerFactory . |
static KeyManagerFactory |
loadKeyManagerFactory(TlsConfiguration tlsConfiguration)
Returns the initialized
KeyManagerFactory . |
static KeyStore |
loadKeyStore(String keystorePath,
char[] keystorePassword,
String keystoreType)
Returns a loaded
KeyStore given the provided configuration values. |
static KeyStore |
loadSecretKeyStore(String keystorePath,
char[] keystorePassword,
String keystoreTypeName)
Load
KeyStore containing Secret Key entries using configured Security Provider |
static TrustManagerFactory |
loadTrustManagerFactory(String truststorePath,
String truststorePassword,
String truststoreType)
Returns the initialized
TrustManagerFactory . |
static TrustManagerFactory |
loadTrustManagerFactory(TlsConfiguration tlsConfiguration)
Returns the initialized
TrustManagerFactory . |
static KeyStore |
loadTrustStore(String truststorePath,
char[] truststorePassword,
String truststoreType)
Returns a loaded
KeyStore (acting as a truststore) given the provided configuration values. |
private static final org.slf4j.Logger logger
public static final String SUN_PROVIDER_NAME
public static final String SUN_JSSE_PROVIDER_NAME
private static final String JKS_EXT
private static final String PKCS12_EXT
private static final String BCFKS_EXT
private static final String KEY_ALIAS
private static final String CERT_ALIAS
private static final X500Principal CERT_DN
private static final String KEY_ALGORITHM
private static final int PASSWORD_LENGTH
private static final String TEST_KEYSTORE_PREFIX
private static final String TEST_TRUSTSTORE_PREFIX
private static final String KEYSTORE_ERROR_MSG
private static final String TRUSTSTORE_ERROR_MSG
private static final Map<KeystoreType,String> KEY_STORE_EXTENSIONS
private static final Map<KeystoreType,String> SECRET_KEY_STORE_PROVIDERS
public static String getKeyStoreProvider(String keyStoreType)
keyStoreType
- the keyStoreTypepublic static KeyStore getKeyStore(String keyStoreType) throws KeyStoreException
keyStoreType
- the keyStoreTypeKeyStoreException
- if a KeyStore of the given type cannot be instantiatedpublic static KeyStore getSecretKeyStore(String keystoreTypeName) throws KeyStoreException
keystoreTypeName
- Keystore Type NameKeyStoreException
- if a KeyStore of the given type cannot be instantiatedpublic static KeyStore loadKeyStore(String keystorePath, char[] keystorePassword, String keystoreType) throws TlsException
KeyStore
given the provided configuration values.keystorePath
- the file path to the keystorekeystorePassword
- the keystore passwordkeystoreType
- the keystore typeTlsException
- if there is a problem loading the keystorepublic static KeyStore loadSecretKeyStore(String keystorePath, char[] keystorePassword, String keystoreTypeName) throws TlsException
KeyStore
containing Secret Key entries using configured Security ProviderkeystorePath
- File path to KeyStorekeystorePassword
- Password for loading KeyStorekeystoreTypeName
- Keystore Type NameTlsException
- Thrown when unable to load KeyStore or unsupported Keystore Typepublic static TlsConfiguration createTlsConfigAndNewKeystoreTruststore(TlsConfiguration tlsConfiguration, int certDurationDays, String[] dnsSubjectAlternativeNames) throws IOException, GeneralSecurityException
tlsConfiguration
- a TlsConfiguration
certDurationDays
- The number of days the cert should be validdnsSubjectAlternativeNames
- An optional array of dnsName SANsTlsConfiguration
IOException
GeneralSecurityException
public static KeyManagerFactory getKeyManagerFactoryFromKeyStore(KeyStore keyStore, char[] keystorePassword, char[] keyPassword) throws TlsException
KeyManagerFactory
from the provided KeyStore
object, initialized with the key or keystore password.keyStore
- the loaded keystorekeystorePassword
- the keystore passwordkeyPassword
- the key passwordTlsException
- if there is a problem initializing or reading from the keystorepublic static KeyManagerFactory loadKeyManagerFactory(TlsConfiguration tlsConfiguration) throws TlsException
KeyManagerFactory
.tlsConfiguration
- the TLS configurationTlsException
- if there is a problem initializing or reading from the keystorepublic static KeyManagerFactory loadKeyManagerFactory(String keystorePath, String keystorePassword, String keyPassword, String keystoreType) throws TlsException
KeyManagerFactory
.keystorePath
- the file path to the keystorekeystorePassword
- the keystore passwordkeyPassword
- the key passwordkeystoreType
- the keystore typeTlsException
- if there is a problem initializing or reading from the keystorepublic static KeyStore loadTrustStore(String truststorePath, char[] truststorePassword, String truststoreType) throws TlsException
KeyStore
(acting as a truststore) given the provided configuration values.truststorePath
- the file path to the truststoretruststorePassword
- the truststore passwordtruststoreType
- the truststore typeTlsException
- if there is a problem loading the truststorepublic static TrustManagerFactory getTrustManagerFactoryFromTrustStore(KeyStore trustStore) throws TlsException
TrustManagerFactory
from the provided KeyStore
object, initialized.trustStore
- the loaded truststoreTlsException
- if there is a problem initializing or reading from the truststorepublic static TrustManagerFactory loadTrustManagerFactory(TlsConfiguration tlsConfiguration) throws TlsException
TrustManagerFactory
.tlsConfiguration
- the TLS configurationTlsException
- if there is a problem initializing or reading from the truststorepublic static TrustManagerFactory loadTrustManagerFactory(String truststorePath, String truststorePassword, String truststoreType) throws TlsException
TrustManagerFactory
.truststorePath
- the file path to the truststoretruststorePassword
- the truststore passwordtruststoreType
- the truststore typeTlsException
- if there is a problem initializing or reading from the truststorepublic static boolean isStoreValid(URL keystore, KeystoreType keystoreType, char[] password)
keystore
- the keystore to validatekeystoreType
- the type of the keystorepassword
- the password to access the keystorepublic static boolean isKeyPasswordCorrect(URL keystore, KeystoreType keystoreType, char[] password, char[] keyPassword)
keystore
- the keystore to validatekeystoreType
- the type of the keystorepassword
- the password to access the keystorekeyPassword
- the password to access the specific keypublic static KeystoreType getKeystoreTypeFromExtension(String keystorePath)
keystorePath
- Path to KeyStorepublic static boolean isSecretKeyEntrySupported(KeystoreType keystoreType)
keystoreType
- Keystore Typeprivate static X509Certificate createKeyStoreAndGetX509Certificate(String alias, String keyStorePassword, String keyPassword, String keyStorePath, KeystoreType keyStoreType, int certDurationDays, String[] dnsSubjectAlternativeNames) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
alias
- the certificate aliaskeyStorePassword
- the keystore passwordkeyPassword
- the key passwordkeyStorePath
- the keystore pathkeyStoreType
- the keystore typednsSubjectAlternativeNames
- An optional array of dnsName SANscertDurationDays
- the duration of the validity of the certificate, in daysX509Certificate
IOException
KeyStoreException
NoSuchAlgorithmException
CertificateException
private static void createTrustStore(X509Certificate cert, String alias, String password, String path, KeystoreType truststoreType) throws KeyStoreException, NoSuchAlgorithmException, CertificateException
cert
- the certificatealias
- the certificate aliaspassword
- the truststore passwordpath
- the truststore pathtruststoreType
- the truststore typeKeyStoreException
NoSuchAlgorithmException
CertificateException
private static Path generateTempKeystorePath(KeystoreType keystoreType) throws IOException
keystoreType
- the Keystore typeIOException
private static Path generateTempTruststorePath(KeystoreType truststoreType) throws IOException
truststoreType
- the Truststore typeIOException
private static KeyStore loadEmptyKeyStore(KeystoreType keyStoreType) throws KeyStoreException, CertificateException, NoSuchAlgorithmException
keyStoreType
- the keystore typeKeyStoreException
- if a keystore of the given type cannot be instantiatedCertificateException
NoSuchAlgorithmException
private static String getKeystoreExtension(KeystoreType keystoreType)
keystoreType
- the keystore typeprivate static String generatePassword()
private static KeystoreType getKeystoreType(String keystoreTypeName)
Copyright © 2023 Apache NiFi Project. All rights reserved.