Class KeyStoreSupporter
java.lang.Object
de.captaingoldfish.scim.sdk.client.keys.KeyStoreSupporter
author: Pascal Knueppel
created at: 09.12.2019
This class is meant to provide additional operations to work with keystores. This implies adding new entries into a keystore, reading entries, convert a keystore from jks to pkcs12 or vice versa etc.
created at: 09.12.2019
This class is meant to provide additional operations to work with keystores. This implies adding new entries into a keystore, reading entries, convert a keystore from jks to pkcs12 or vice versa etc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumrepresents the possible keystore types that are supported -
Method Summary
Modifier and TypeMethodDescriptionstatic KeyStoreaddCertificateEntry(KeyStore keyStore, String alias, Certificate certificate) convenience method for adding a certificate entry to the given keystore under the given alias, without having to handle the checked exceptionstatic KeyStoreaddCertificateEntryToKeyStore(KeyStore keyStore, Certificate certificate, String alias) this method simply adds a certificate entry to the given keystore.static KeyStoreaddEntryToKeystore(KeyStore keyStore, String alias, Key key, Certificate[] certificateChain, String password) will try to add the given entry under the given alias to the given keystorestatic KeyStoreconvertKeyStore(KeyStore keyStore, String keyStorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) This method will convert a given keystore with all its entries into another type of keystore.static KeyStorecreateEmptyKeyStore(KeyStoreSupporter.KeyStoreType keyStoreType, String keystorePassword) creates an empty keystorestatic Enumeration<String>getAliases(KeyStore keyStore) convenience method to access the aliases of the keystore without having to handle the exceptionstatic byte[]will convert the given keystore into a byte arraystatic Optional<Certificate>getCertificate(KeyStore keyStore, String alias) convenience method to read a certificate entry from a keystorestatic Optional<Certificate[]>getCertificateChain(KeyStore keyStore, String alias) will get the certificateChain from the given aliasgetKeyEntry(KeyStore keyStore, String alias, String password) convenience method to access the private key from the keystore without having to handle the checked exceptionsstatic voidkeyStoreToFile(File directory, String filename, KeyStore keyStore, String keystorePassword) Will store the given keystore into the given file.static voidkeyStoreToFile(File file, KeyStore keyStore, String keystorePassword) Will store the given keystore into the given file.static KeyStoremergeKeyStores(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 KeyPairreadFirstKeyPairEntryFromKeyStore(KeyStore keyStore, String privateKeyPassword) reads the first found keystore entry and expects it to be a private-key entry.static KeyStorereadKeyStore(byte[] keyStoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword) will read a byte array to a keystore.static KeyStorereadKeyStore(File file, String keyStorePassword) will read a file to a keystore.static KeyStorereadKeyStore(InputStream keyStoreStream, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword) will read an input stream to a keystore.static KeyStorereadTruststore(byte[] truststoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType) will read a keystore from the given byte array that can only be used as truststorestatic KeyStorereadTruststore(byte[] truststoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType, String password) will read a keystore from the given byte array that can only be used as truststorestatic KeyStorereadTruststore(InputStream truststoreStream, KeyStoreSupporter.KeyStoreType keyStoreType) will read a keystore from the given inputstream that can only be used as truststorestatic KeyStorereadTruststore(InputStream truststoreStream, KeyStoreSupporter.KeyStoreType keyStoreType, String password) will read a keystore from the given inputstream that can only be used as truststorestatic ProviderselectProvider(KeyStoreSupporter.KeyStoreType keyStoreType) this method will make sure that the correct security provider is chosen for the different keystore types.static KeyStoretoKeyStore(byte[] privateKeyBytes, byte[] certificateBytes, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givenprivateKeyand thecertificatestatic KeyStoretoKeyStore(Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givencertificatestatic KeyStoretoKeyStore(PrivateKey privateKey, Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givenprivateKeyand thecertificatestatic voidtryCopyEntry(KeyStore keyStore, String keyStorePassword, String keyPassword, KeyStoreSupporter.KeyStoreType keyStoreType, KeyStore newKeyStore, String alias) this method tries to access an entry of the givenkeyStoreand will add it to thenewKeyStoreobject no matter if the given alias is a key-entry or a certificate entry
-
Method Details
-
selectProvider
this method will make sure that the correct security provider is chosen for the different keystore types. The experience shows us thatBouncyCastleProvideris often tried to be used for JKS and JCEKS keystores. But bouncy castle cannot handle these types why we are chosing the providers manually here- Parameters:
keyStoreType- the keystore type for which a provider is needed.- Returns:
- the provider that can handle the given keystore
-
toKeyStore
public static KeyStore toKeyStore(PrivateKey privateKey, Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givenprivateKeyand thecertificate- Parameters:
privateKey- 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 keystore- Returns:
- the keystore with the private key and the certificate
- Throws:
KeyStoreCreationFailedException- if the algorithm of thekeyStoreTypecould not be resolvedKeyStoreEntryException- if the certificate or private key could not be added to the keystore
-
toKeyStore
public static KeyStore toKeyStore(Certificate certificate, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givencertificate- Parameters:
certificate- 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 keystore- Returns:
- the keystore with the private key and the certificate
- Throws:
KeyStoreCreationFailedException- if the algorithm of thekeyStoreTypecould not be resolvedKeyStoreEntryException- if the certificate or private key could not be added to the keystore
-
toKeyStore
public static KeyStore toKeyStore(byte[] privateKeyBytes, byte[] certificateBytes, String alias, String keystorePassword, KeyStoreSupporter.KeyStoreType keyStoreType) creates a keystore from the givenprivateKeyand thecertificate- Parameters:
privateKeyBytes- 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 keystore- Returns:
- the keystore with the private key and the certificate
- Throws:
KeyStoreCreationFailedException- if the algorithm of thekeyStoreTypecould 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.
-
getBytes
will convert the given keystore into a byte array- Parameters:
keyStore- the keystore that should be convertedpassword- the keystore password that will be used as encryption password for the keystore- Returns:
- the byte array that contains the data of the given keystore
-
createEmptyKeyStore
public static KeyStore createEmptyKeyStore(KeyStoreSupporter.KeyStoreType keyStoreType, String keystorePassword) creates an empty keystore- Parameters:
keyStoreType- the type of keystore to createkeystorePassword- the password to secure the keystore- Returns:
- the newly created empty keystore-instance
-
addCertificateEntryToKeyStore
public static KeyStore addCertificateEntryToKeyStore(KeyStore keyStore, Certificate certificate, String alias) this method simply adds a certificate entry to the given keystore. This method is only to extend the adding of certificate method by logging and it prevents overriding existing entries.- Parameters:
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.- Returns:
- the keystore that was also given as parameter with the added certificate.
-
addCertificateEntry
public static KeyStore addCertificateEntry(KeyStore keyStore, String alias, Certificate certificate) convenience method for adding a certificate entry to the given keystore under the given alias, without having to handle the checked exception- Parameters:
keyStore- the keystore to extend with the certificatealias- the alias under which the certificate should be storedcertificate- the certificate for the new entry- Returns:
- the keystore that was given as parameter
-
addEntryToKeystore
public static KeyStore addEntryToKeystore(KeyStore keyStore, String alias, Key key, Certificate[] certificateChain, String password) will try to add the given entry under the given alias to the given keystore- Parameters:
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 keystore- Returns:
- the same keystore that was given as parameter
-
convertKeyStore
public 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.
this will of course only work if the private key passwords are matching the keystore password.- Parameters:
keyStore- the kystore that shall be convertedkeyStorePassword- the password to open the keystorekeyStoreType- the type to which the keystore should be converted- Returns:
- the converted keystore.
-
tryCopyEntry
public static void tryCopyEntry(KeyStore keyStore, String keyStorePassword, String keyPassword, KeyStoreSupporter.KeyStoreType keyStoreType, KeyStore newKeyStore, String alias) this method tries to access an entry of the givenkeyStoreand will add it to thenewKeyStoreobject no matter if the given alias is a key-entry or a certificate entry- Parameters:
keyStore- 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 copied
-
keyStoreToFile
Will store the given keystore into the given file.- Parameters:
file- the file where the keystore should be saved.keyStore- the keystore to save.keystorePassword- the password to access and save the given keystore
-
keyStoreToFile
public static void keyStoreToFile(File directory, String filename, KeyStore keyStore, String keystorePassword) Will store the given keystore into the given file.- Parameters:
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 keystore
-
readKeyStore
will read a file to a keystore.- Parameters:
file- the file that should be read to a keystorekeyStorePassword- the password to access the keystore- Returns:
- the read keystore
-
readKeyStore
public static KeyStore readKeyStore(byte[] keyStoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword) will read a byte array to a keystore.- Parameters:
keyStoreBytes- the bytes of the keyStore that should be readkeyStoreType- the type of the keystore.keyStorePassword- the password to access the keystore- Returns:
- the read keystore
-
readKeyStore
public static KeyStore readKeyStore(InputStream keyStoreStream, KeyStoreSupporter.KeyStoreType keyStoreType, String keyStorePassword) will read an input stream to a keystore.- Parameters:
keyStoreStream- the bytes of the keyStore that should be readkeyStoreType- the type of the keystore.keyStorePassword- the password to access the keystore- Returns:
- the read keystore
-
readTruststore
public static KeyStore readTruststore(byte[] truststoreBytes, KeyStoreSupporter.KeyStoreType keyStoreType) will read a keystore from the given byte array that can only be used as truststore- Parameters:
truststoreBytes- the bytes of the truststorekeyStoreType- the keystore type that the truststore represents- Returns:
- a keystore that can only be used as truststore
-
readTruststore
public 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- Parameters:
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 keystores- Returns:
- a keystore that can only be used as truststore
-
readTruststore
public static KeyStore readTruststore(InputStream truststoreStream, KeyStoreSupporter.KeyStoreType keyStoreType) will read a keystore from the given inputstream that can only be used as truststore- Parameters:
truststoreStream- a stream containing the truststore datakeyStoreType- the keystore type that the truststore represents- Returns:
- a keystore that can only be used as truststore
-
readTruststore
public 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- Parameters:
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 keystores- Returns:
- a keystore that can only be used as truststore
-
mergeKeyStores
public 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. In order for these both not to collide with one another the alias from keystore2 will be extended by "_2"
If keystore 1 and 2 will share the same entry under different aliases the alias from keystore1 is preferred unless the entry of keystore1 is accessible. Otherwise the entry of keystore2 will be added if it is accessbile instead.
If a private key entry cannot be accessed since its password is not matching the keystore password the entry will be omitted and only be added as a certificate entry.- Parameters:
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.- Returns:
- a new keystore that contains all entries of the two keystores that were directly accessible.
-
readFirstKeyPairEntryFromKeyStore
public static KeyPair readFirstKeyPairEntryFromKeyStore(KeyStore keyStore, String privateKeyPassword) reads the first found keystore entry and expects it to be a private-key entry. This method can be used if the alias of the keystore is unknown and the given keystore contains only a single private-key-entry- Parameters:
keyStore- the keystore with hopefully only a single private key entryprivateKeyPassword- the password of the private key- Returns:
- the keypair of the keystore. (Since empty keystores are invalid this method should never return null)
- Throws:
KeyStoreReadingException- if the keystore entry could not be read or if the first keystore entry is only a certificate entry
-
getAliases
convenience method to access the aliases of the keystore without having to handle the exception- Parameters:
keyStore- the keystore to get the aliases from- Returns:
- the aliases of the given keystore
- Throws:
KeyStoreReadingException- in case of aKeyStoreException
-
getKeyEntry
convenience method to access the private key from the keystore without having to handle the checked exceptions- Parameters:
keyStore- the keystore from which the private key should be accessedalias- the alias of the private key entrypassword- the password of the private key entry for the given alias- Returns:
- the private key entry if it does exist
-
getCertificateChain
will get the certificateChain from the given alias- Parameters:
keyStore- the keystore from which the certificate chain should be extractedalias- the alias where the chain should be found- Returns:
- the certificate chain if present or an empty
-
getCertificate
convenience method to read a certificate entry from a keystore
-