public final class KeyStoreSupporter extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
KeyStoreSupporter.KeyStoreType
represents the possible keystore types that are supported
|
| Modifier and Type | Method and Description |
|---|---|
static KeyStore |
addCertificateEntryToKeyStore(KeyStore keyStore,
Certificate certificate,
String alias)
this method simply adds a certificate entry to the given keystore.
|
static KeyStore |
addKeyEntryToKeystore(KeyStore keyStore,
String alias,
Key key,
Certificate[] certificateChain,
String password)
will try to add the given key-entry under the given alias to the given keystore
|
static KeyStore |
convertKeyStore(KeyStore keyStore,
String keyStorePassword,
KeyStoreSupporter.KeyStoreType keyStoreType)
This method will convert a given keystore with all its entries into another type of keystore.
|
static KeyStore |
createEmptyKeyStore(KeyStoreSupporter.KeyStoreType keyStoreType,
String keystorePassword)
creates an empty keystore
|
static Enumeration<String> |
getAliases(KeyStore keyStore)
convenience method to access the aliases of the keystore without having to handle the exception
|
static byte[] |
getBytes(KeyStore keyStore,
String password)
will convert the given keystore into a byte array
|
static Optional<Certificate> |
getCertificate(KeyStore keyStore,
String alias)
convenience method to read a certificate entry from a keystore
|
static Optional<Certificate[]> |
getCertificateChain(KeyStore keyStore,
String alias)
will get the certificateChain from the given alias
|
static void |
keyStoreToFile(File file,
KeyStore keyStore,
String keystorePassword)
Will store the given keystore into the given file.
|
static void |
keyStoreToFile(File directory,
String filename,
KeyStore keyStore,
String keystorePassword)
Will store the given keystore into the given file.
|
static KeyStore |
mergeKeyStores(KeyStore keyStore1,
String password1,
KeyStore keyStore2,
String password2,
KeyStoreSupporter.KeyStoreType keyStoreType,
String mergedKeyStoreKeyPassword)
this method will merge all accessible entries from the given keystores into a single keystore
WARNING: It might be that keystore1 and 2 may contain different entries under the same alias. |
static KeyPair |
readFirstKeyPairEntryFromKeyStore(KeyStore keyStore,
String privateKeyPassword)
reads the first found keystore entry and expects it to be a private-key entry.
|
static KeyStore |
readKeyStore(byte[] keyStoreBytes,
KeyStoreSupporter.KeyStoreType keyStoreType,
String keyStorePassword)
will read a byte array to a keystore.
|
static KeyStore |
readKeyStore(File file,
KeyStoreSupporter.KeyStoreType keyStoreType,
String keyStorePassword)
will read a file to a keystore.
|
static KeyStore |
readKeyStore(File file,
String keyStorePassword)
will read a file to a keystore.
|
static KeyStore |
readKeyStore(InputStream keyStoreStream,
KeyStoreSupporter.KeyStoreType keyStoreType,
String keyStorePassword)
will read an input stream to a keystore.
|
static KeyStore |
readTruststore(byte[] truststoreBytes,
KeyStoreSupporter.KeyStoreType keyStoreType)
will read a keystore from the given byte array that can only be used as truststore
|
static KeyStore |
readTruststore(byte[] truststoreBytes,
KeyStoreSupporter.KeyStoreType keyStoreType,
String password)
will read a keystore from the given byte array that can only be used as truststore
|
static KeyStore |
readTruststore(InputStream truststoreStream,
KeyStoreSupporter.KeyStoreType keyStoreType)
will read a keystore from the given inputstream that can only be used as truststore
|
static KeyStore |
readTruststore(InputStream truststoreStream,
KeyStoreSupporter.KeyStoreType keyStoreType,
String password)
will read a keystore from the given inputstream that can only be used as truststore
|
static Provider |
selectProvider(KeyStoreSupporter.KeyStoreType keyStoreType)
this method will make sure that the correct security provider is chosen for the different keystore types.
|
static KeyStore |
toKeyStore(byte[] privateKeyBytes,
byte[] certificateBytes,
String alias,
String keystorePassword,
KeyStoreSupporter.KeyStoreType keyStoreType)
creates a keystore from the given
privateKey and the certificate |
static KeyStore |
toKeyStore(Certificate certificate,
String alias,
String keystorePassword,
KeyStoreSupporter.KeyStoreType keyStoreType)
creates a keystore from the given
certificate |
static KeyStore |
toKeyStore(PrivateKey privateKey,
Certificate certificate,
String alias,
String keystorePassword,
KeyStoreSupporter.KeyStoreType keyStoreType)
creates a keystore from the given
privateKey and the certificate |
static void |
tryCopyKeyEntry(KeyStore keyStore,
String keyStorePassword,
String keyPassword,
KeyStoreSupporter.KeyStoreType keyStoreType,
KeyStore newKeyStore,
String alias)
this method tries to access an entry of the given
keyStore and will add it to the
newKeyStore object no matter if the given alias is a key-entry or a certificate entry |
public static Provider selectProvider(KeyStoreSupporter.KeyStoreType keyStoreType)
BouncyCastleProvider is often tried to be used for JKS and JCEKS
keystores. But bouncy castle cannot handle these types why we are chosing the providers manually herekeyStoreType - the keystore type for which a provider is needed.public static KeyStore toKeyStore(PrivateKey privateKey, Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType)
privateKey and the certificateprivateKey - the private key that should be packed into a keystorecertificate - the certificate that should be packed into the keystore alongside the private keyalias - the alias that should be used for the private key and the certificatekeystorePassword - the password to safe the keystore and the private keykeyStoreType - the type of the keystoreKeyStoreCreationFailedException - if the algorithm of the keyStoreType could not be resolvedKeyStoreEntryException - if the certificate or private key could not be added to the keystorepublic static KeyStore toKeyStore(Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType)
certificatecertificate - the certificate that should be packed into the keystore alongside the private keyalias - the alias that should be used for the private key and the certificatekeystorePassword - the password to safe the keystore and the private keykeyStoreType - the type of the keystoreKeyStoreCreationFailedException - if the algorithm of the keyStoreType could not be resolvedKeyStoreEntryException - if the certificate or private key could not be added to the keystorepublic static KeyStore toKeyStore(byte[] privateKeyBytes, byte[] certificateBytes, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType)
privateKey and the certificateprivateKeyBytes - the private key that should be packed into a keystorecertificateBytes - the certificate that should be packed into the keystore alongside the private keyalias - the alias that should be used for the private key and the certificatekeystorePassword - the password to safe the keystore and the private keykeyStoreType - the type of the keystoreKeyStoreCreationFailedException - if the algorithm of the keyStoreType could not be resolvedKeyStoreEntryException - if the certificate or private key could not be added to the keystoreKeyGenerationException - if the private key could not be created from the given byte-arrayCertificateCreationException - if the certificate could not be created from the given data.public static byte[] getBytes(KeyStore keyStore, String password)
keyStore - the keystore that should be convertedpassword - the keystore password that will be used as encryption password for the keystoreKeyStoreConvertException - if the keystore conversion failed.public static KeyStore createEmptyKeyStore(KeyStoreSupporter.KeyStoreType keyStoreType, String keystorePassword)
keyStoreType - the type of keystore to createkeystorePassword - the password to secure the keystorepublic static KeyStore addCertificateEntryToKeyStore(KeyStore keyStore, Certificate certificate, String alias)
keyStore - the keystore to which the certificate should be addedcertificate - the certificate to add to the given keystorealias - the alias that will be used for the certificate entry.public static KeyStore addKeyEntryToKeystore(KeyStore keyStore, String alias, Key key, Certificate[] certificateChain, String password)
keyStore - the keystore to which the key entry should be addedalias - the alias to use for the key-entrykey - the key to set under the given aliaspassword - the password to secure the key within the keystorepublic static KeyStore convertKeyStore(KeyStore keyStore, String keyStorePassword, KeyStoreSupporter.KeyStoreType keyStoreType)
keyStore - the kystore that shall be convertedkeyStorePassword - the password to open the keystorekeyStoreType - the type to which the keystore should be convertedpublic static void tryCopyKeyEntry(KeyStore keyStore, String keyStorePassword, String keyPassword, KeyStoreSupporter.KeyStoreType keyStoreType, KeyStore newKeyStore, String alias)
keyStore and will add it to the
newKeyStore object no matter if the given alias is a key-entry or a certificate entrykeyStore - the keystore that holds the original entrykeyStorePassword - the password to access the original keystorekeyPassword - the password to access the original key entry under the given aliaskeyStoreType - the type of the original keystorenewKeyStore - the new keystore to which the entry should be copiedalias - the alias of the entry that should be copiedpublic static void keyStoreToFile(File file, KeyStore keyStore, String keystorePassword)
file - the file where the keystore should be saved.keyStore - the keystore to save.keystorePassword - the password to access and save the given keystorepublic static void keyStoreToFile(File directory, String filename, KeyStore keyStore, String keystorePassword)
directory - the target directory where the keystore should be saved.filename - the file where the keystore should be saved.keyStore - the keystore to save.keystorePassword - the password to access and save the given keystorepublic static KeyStore readKeyStore(File file, String keyStorePassword)
file - the file that should be read to a keystorekeyStorePassword - the password to access the keystorepublic static KeyStore readKeyStore(File file, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword)
file - the file that should be read to a keystorekeyStoreType - the type of the keystore.keyStorePassword - the password to access the keystorepublic static KeyStore readKeyStore(byte[] keyStoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword)
keyStoreBytes - the bytes of the keyStore that should be readkeyStoreType - the type of the keystore.keyStorePassword - the password to access the keystorepublic static KeyStore readKeyStore(InputStream keyStoreStream, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword)
keyStoreStream - the bytes of the keyStore that should be readkeyStoreType - the type of the keystore.keyStorePassword - the password to access the keystorepublic static KeyStore readTruststore(byte[] truststoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType)
truststoreBytes - the bytes of the truststorekeyStoreType - the keystore type that the truststore representspublic static KeyStore readTruststore(byte[] truststoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType, String password)
truststoreBytes - the bytes of the truststorekeyStoreType - the keystore type that the truststore representspassword - an optional password that can be entered for JKS keystores and must be entered for PKCS12
keystorespublic static KeyStore readTruststore(InputStream truststoreStream, KeyStoreSupporter.KeyStoreType keyStoreType)
truststoreStream - a stream containing the truststore datakeyStoreType - the keystore type that the truststore representspublic static KeyStore readTruststore(InputStream truststoreStream, KeyStoreSupporter.KeyStoreType keyStoreType, String password)
truststoreStream - a stream containing the truststore datakeyStoreType - the keystore type that the truststore representspassword - an optional password that can be entered for JKS keystores and must be entered for PKCS12
keystorespublic static KeyStore mergeKeyStores(KeyStore keyStore1, String password1, KeyStore keyStore2, String password2, KeyStoreSupporter.KeyStoreType keyStoreType, String mergedKeyStoreKeyPassword)
keyStore1 - the first keystorepassword1 - the password to access the first keystorekeyStore2 - the second keystorepassword2 - the password to access the second keystorekeyStoreType - this will be the type of the keystore that contains the new entries.mergedKeyStoreKeyPassword - this will be the password of all added private keys within the merged
keystore.public static KeyPair readFirstKeyPairEntryFromKeyStore(KeyStore keyStore, String privateKeyPassword)
keyStore - the keystore with hopefully only a single private key entryprivateKeyPassword - the password of the private keyKeyStoreReadingException - if the keystore entry could not be read or if the first keystore entry is
only a certificate entrypublic static Enumeration<String> getAliases(KeyStore keyStore)
keyStore - the keystore to get the aliases fromKeyStoreReadingException - in case of a KeyStoreExceptionpublic static Optional<Certificate[]> getCertificateChain(KeyStore keyStore, String alias)
keyStore - the keystore from which the certificate chain should be extractedalias - the alias where the chain should be foundpublic static Optional<Certificate> getCertificate(KeyStore keyStore, String alias)
Copyright © 2020. All rights reserved.