public class KeyStoreUtils extends Object
Modifier and Type | Field and Description |
---|---|
private static String |
BCFKS_EXT |
private static String |
CERT_ALIAS |
private static String |
CERT_DN |
private static int |
CERT_DURATION_DAYS |
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 String |
SIGNING_ALGORITHM |
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)
Loads the Keystore and returns a X509 Certificate with the given values.
|
static TlsConfiguration |
createTlsConfigAndNewKeystoreTruststore()
Creates a temporary default Keystore and Truststore and returns it wrapped in a TLS configuration.
|
static TlsConfiguration |
createTlsConfigAndNewKeystoreTruststore(TlsConfiguration tlsConfiguration)
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
|
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 |
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 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. |
static String |
sslContextToString(SSLContext sslContext) |
static String |
sslParametersToString(SSLParameters sslParameters) |
static String |
sslServerSocketToString(SSLServerSocket sslServerSocket) |
private static final org.slf4j.Logger logger
public static final String SUN_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 String CERT_DN
private static final String KEY_ALGORITHM
private static final String SIGNING_ALGORITHM
private static final int CERT_DURATION_DAYS
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
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 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 TlsConfiguration createTlsConfigAndNewKeystoreTruststore() throws IOException, GeneralSecurityException
TlsConfiguration
IOException
GeneralSecurityException
public static TlsConfiguration createTlsConfigAndNewKeystoreTruststore(TlsConfiguration tlsConfiguration) throws IOException, GeneralSecurityException
tlsConfiguration
- a TlsConfiguration
TlsConfiguration
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 String sslContextToString(SSLContext sslContext)
public static String sslParametersToString(SSLParameters sslParameters)
public static String sslServerSocketToString(SSLServerSocket sslServerSocket)
private static X509Certificate createKeyStoreAndGetX509Certificate(String alias, String keyStorePassword, String keyPassword, String keyStorePath, KeystoreType keyStoreType) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException
alias
- the certificate aliaskeyStorePassword
- the keystore passwordkeyPassword
- the key passwordkeyStorePath
- the keystore pathkeyStoreType
- the keystore typeX509Certificate
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()
Copyright © 2021 Apache NiFi Project. All rights reserved.