Class KeyStoreUtil

java.lang.Object
com.predic8.membrane.core.security.KeyStoreUtil

public class KeyStoreUtil extends Object
  • 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 filter
      keyPass - the password for accessing the key
      keyAlias - 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 initialized
      IOException - if there is an I/O error during the operation
      NoSuchAlgorithmException - if the algorithm for recovering the key cannot be found
      CertificateException - if any of the certificates in the chain are invalid
      UnrecoverableKeyException - 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

      public static String firstAliasOrThrow(KeyStore ks) throws KeyStoreException
      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 store
      RuntimeException - if no certificate is available in the key store
    • aliasOrThrow

      public static String aliasOrThrow(KeyStore ks, String alias) throws KeyStoreException
      Returns alias if it was found within the specified key store, else throws.
      Parameters:
      ks - the KeyStore which will be queried
      alias - 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 initialized
      RuntimeException - if the certificate with the specified alias is not present in the key store
    • getFirstCertAlias

      public static Optional<String> getFirstCertAlias(KeyStore keystore) throws KeyStoreException
      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