Class KeyStoreHelper


  • @ThreadSafe
    public final class KeyStoreHelper
    extends Object
    Helper methods to access Java key stores of type JKS (Java KeyStore).
    Author:
    PEPPOL.AT, BRZ, Philip Helger
    • Method Detail

      • getResourceProvider

        @Nonnull
        public static com.helger.commons.io.resourceprovider.IReadableResourceProvider getResourceProvider()
      • setResourceProvider

        public static void setResourceProvider​(@Nonnull
                                               com.helger.commons.io.resourceprovider.IReadableResourceProvider aResourceProvider)
      • createKeyStoreWithOnlyOneItem

        @Nonnull
        public static KeyStore createKeyStoreWithOnlyOneItem​(@Nonnull
                                                             KeyStore aBaseKeyStore,
                                                             @Nonnull
                                                             String sAliasToCopy,
                                                             @Nullable
                                                             char[] aAliasPassword)
                                                      throws GeneralSecurityException,
                                                             IOException
        Create a new key store based on an existing key store
        Parameters:
        aBaseKeyStore - The source key store. May not be null
        sAliasToCopy - The name of the alias in the source key store that should be put in the new key store
        aAliasPassword - The optional password to access the alias in the source key store. If it is not null the same password will be used in the created key store
        Returns:
        The created in-memory key store
        Throws:
        GeneralSecurityException - In case of a key store error
        IOException - In case key store loading fails
      • loadKeyStore

        @Nonnull
        public static LoadedKeyStore loadKeyStore​(@Nonnull
                                                  IKeyStoreType aKeyStoreType,
                                                  @Nullable
                                                  String sKeyStorePath,
                                                  @Nullable
                                                  String sKeyStorePassword)
        Load the provided key store in a safe manner.
        Parameters:
        aKeyStoreType - Type of key store. May not be null.
        sKeyStorePath - Path to the key store. May not be null for all key store types that require a path.
        sKeyStorePassword - Password for the key store. May not be null to succeed.
        Returns:
        The key store loading result. Never null.
      • loadPrivateKey

        @Nonnull
        public static LoadedKey<KeyStore.PrivateKeyEntry> loadPrivateKey​(@Nonnull
                                                                         KeyStore aKeyStore,
                                                                         @Nonnull
                                                                         String sKeyStorePath,
                                                                         @Nullable
                                                                         String sKeyStoreKeyAlias,
                                                                         @Nullable
                                                                         char[] aKeyStoreKeyPassword)
        Load the specified private key entry from the provided key store.
        Parameters:
        aKeyStore - The key store to load the key from. May not be null.
        sKeyStorePath - Key store path. For nice error messages only. May not be null.
        sKeyStoreKeyAlias - The alias to be resolved in the key store. Must be non- null to succeed.
        aKeyStoreKeyPassword - The key password for the key store. Must be non-null to succeed.
        Returns:
        The key loading result. Never null.
      • loadSecretKey

        @Nonnull
        public static LoadedKey<KeyStore.SecretKeyEntry> loadSecretKey​(@Nonnull
                                                                       KeyStore aKeyStore,
                                                                       @Nonnull
                                                                       String sKeyStorePath,
                                                                       @Nullable
                                                                       String sKeyStoreKeyAlias,
                                                                       @Nullable
                                                                       char[] aKeyStoreKeyPassword)
        Load the specified secret key entry from the provided key store.
        Parameters:
        aKeyStore - The key store to load the key from. May not be null.
        sKeyStorePath - Key store path. For nice error messages only. May not be null.
        sKeyStoreKeyAlias - The alias to be resolved in the key store. Must be non- null to succeed.
        aKeyStoreKeyPassword - The key password for the key store. Must be non-null to succeed.
        Returns:
        The key loading result. Never null.
      • loadTrustedCertificateKey

        @Nonnull
        public static LoadedKey<KeyStore.TrustedCertificateEntry> loadTrustedCertificateKey​(@Nonnull
                                                                                            KeyStore aKeyStore,
                                                                                            @Nonnull
                                                                                            String sKeyStorePath,
                                                                                            @Nullable
                                                                                            String sKeyStoreKeyAlias,
                                                                                            @Nullable
                                                                                            char[] aKeyStoreKeyPassword)
        Load the specified private key entry from the provided key store.
        Parameters:
        aKeyStore - The key store to load the key from. May not be null.
        sKeyStorePath - Key store path. For nice error messages only. May not be null.
        sKeyStoreKeyAlias - The alias to be resolved in the key store. Must be non- null to succeed.
        aKeyStoreKeyPassword - The key password for the key store. Must be non-null to succeed.
        Returns:
        The key loading result. Never null.