Class KeyStoreUtil
java.lang.Object
com.predic8.membrane.core.security.KeyStoreUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaliasOrThrow(KeyStore ks, String alias) Returns alias if it was found within the specified key store, else throws.static KeyStorefilterKeyStoreByAlias(KeyStore ks, char[] keyPass, String keyAlias) Filters a KeyStore by a specific alias, creating a new KeyStore containing only the key and certificate chain associated with the provided alias.static StringRetrieves the first certificate alias from the provided key store.static @NotNull KeyStoregetAndLoadKeyStore(Store store, ResolverMap resourceResolver, String baseLocation, String type, char[] password) Retrieves and loads a KeyStore based on the provided parameters.static @NotNull StringGenerates an SHA-256 digest of the certificate associated with the given alias in the KeyStore.getFirstCertAlias(KeyStore keystore) Retrieves the first alias of a key entry from the specified KeyStore.
-
Constructor Details
-
KeyStoreUtil
public KeyStoreUtil()
-
-
Method Details
-
filterKeyStoreByAlias
public static KeyStore filterKeyStoreByAlias(KeyStore ks, char[] keyPass, String keyAlias) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException Filters a KeyStore by a specific alias, creating a new KeyStore containing only the key and certificate chain associated with the provided alias.- Parameters:
ks- the original KeyStore to filterkeyPass- the password for accessing the keykeyAlias- the alias of the key to filter- Returns:
- a new KeyStore containing only the key and certificate chain associated with the specified alias
- Throws:
KeyStoreException- if the KeyStore cannot be initializedIOException- if there is an I/O error during the operationNoSuchAlgorithmException- if the algorithm for recovering the key cannot be foundCertificateException- if any of the certificates in the chain are invalidUnrecoverableKeyException- if the key cannot be recovered using the given password
-
getDigest
@NotNull public static @NotNull String getDigest(KeyStore ks, String alias) throws CertificateEncodingException, KeyStoreException, NoSuchAlgorithmException Generates an SHA-256 digest of the certificate associated with the given alias in the KeyStore.- Parameters:
ks- The KeyStore containing the certificate.alias- The alias of the certificate in the KeyStore.- Returns:
- A String representation of the SHA-256 digest, with bytes separated by colons.
- Throws:
CertificateEncodingException- If there's an error encoding the certificate.KeyStoreException- If there's an error accessing the KeyStore.NoSuchAlgorithmException- If the SHA-256 algorithm is not available.
-
getAndLoadKeyStore
@NotNull public static @NotNull KeyStore getAndLoadKeyStore(Store store, ResolverMap resourceResolver, String baseLocation, String type, char[] password) throws KeyStoreException, NoSuchProviderException, IOException, NoSuchAlgorithmException, CertificateException Retrieves and loads a KeyStore based on the provided parameters.- Parameters:
store- The Store object containing information about the keystore.resourceResolver- The ResolverMap used to resolve the keystore location.baseLocation- The base location to be combined with the store's location.type- The type of the KeyStore (e.g., "JKS", "PKCS12").password- The password used to check the integrity of the keystore.- Returns:
- A loaded KeyStore instance.
- Throws:
KeyStoreException- If there's an error accessing the keystore.NoSuchProviderException- If the specified provider is not found.IOException- If there's an I/O error while loading the keystore.NoSuchAlgorithmException- If the algorithm used to check the integrity of the keystore cannot be found.CertificateException- If any of the certificates in the keystore could not be loaded.
-
firstAliasOrThrow
Retrieves the first certificate alias from the provided key store.- Parameters:
ks- the key store from which to retrieve the certificate alias- Returns:
- the first certificate alias as a String
- Throws:
KeyStoreException- if there is an error accessing the key storeRuntimeException- if no certificate is available in the key store
-
aliasOrThrow
Returns alias if it was found within the specified key store, else throws.- Parameters:
ks- the KeyStore which will be queriedalias- the alias of the certificate to be queried- Returns:
- the alias of the certificate if it exists
- Throws:
KeyStoreException- if the key store has not been initializedRuntimeException- if the certificate with the specified alias is not present in the key store
-
getFirstCertAlias
Retrieves the first alias of a key entry from the specified KeyStore.- Parameters:
keystore- the key store to search for key entries- Returns:
- Optional containing the first alias of a key entry, or an empty Optional if none is found
- Throws:
KeyStoreException- is thrown if key store has not been initialized
-