Package com.helger.security.keystore
Class KeyStoreHelper
- java.lang.Object
-
- com.helger.security.keystore.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static KeyStorecreateKeyStoreWithOnlyOneItem(KeyStore aBaseKeyStore, String sAliasToCopy, char[] aAliasPassword)Create a new key store based on an existing key storestatic com.helger.commons.io.resourceprovider.IReadableResourceProvidergetResourceProvider()static KeyStoregetSimiliarKeyStore(KeyStore aOther)static LoadedKeyStoreloadKeyStore(IKeyStoreType aKeyStoreType, String sKeyStorePath, String sKeyStorePassword)Load the provided key store in a safe manner.static KeyStoreloadKeyStoreDirect(IKeyStoreType aKeyStoreType, String sKeyStorePath, char[] aKeyStorePassword)Load a key store from a resource.static KeyStoreloadKeyStoreDirect(IKeyStoreType aKeyStoreType, String sKeyStorePath, String sKeyStorePassword)Load a key store from a resource.static LoadedKey<KeyStore.PrivateKeyEntry>loadPrivateKey(KeyStore aKeyStore, String sKeyStorePath, String sKeyStoreKeyAlias, char[] aKeyStoreKeyPassword)Load the specified private key entry from the provided key store.static LoadedKey<KeyStore.SecretKeyEntry>loadSecretKey(KeyStore aKeyStore, String sKeyStorePath, String sKeyStoreKeyAlias, char[] aKeyStoreKeyPassword)Load the specified secret key entry from the provided key store.static LoadedKey<KeyStore.TrustedCertificateEntry>loadTrustedCertificateKey(KeyStore aKeyStore, String sKeyStorePath, String sKeyStoreKeyAlias, char[] aKeyStoreKeyPassword)Load the specified private key entry from the provided key store.static voidsetResourceProvider(com.helger.commons.io.resourceprovider.IReadableResourceProvider aResourceProvider)
-
-
-
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)
-
getSimiliarKeyStore
@Nonnull public static KeyStore getSimiliarKeyStore(@Nonnull KeyStore aOther) throws KeyStoreException
- Throws:
KeyStoreException
-
loadKeyStoreDirect
@Nonnull public static KeyStore loadKeyStoreDirect(@Nonnull IKeyStoreType aKeyStoreType, @Nullable String sKeyStorePath, @Nullable String sKeyStorePassword) throws GeneralSecurityException, IOException
Load a key store from a resource.- Parameters:
aKeyStoreType- Type of key store. May not benull.sKeyStorePath- The path pointing to the key store. May only benullforEKeyStoreType.PKCS11.sKeyStorePassword- The key store password. May benullto indicate that no password is required.- Returns:
- The Java key-store object.
- Throws:
GeneralSecurityException- In case of a key store errorIOException- In case key store loading failsIllegalArgumentException- If the key store path is invalid
-
loadKeyStoreDirect
@Nonnull public static KeyStore loadKeyStoreDirect(@Nonnull IKeyStoreType aKeyStoreType, @Nullable String sKeyStorePath, @Nullable char[] aKeyStorePassword) throws GeneralSecurityException, IOException
Load a key store from a resource.- Parameters:
aKeyStoreType- Type of key store. May not benull.sKeyStorePath- The path pointing to the key store. May only benullforEKeyStoreType.PKCS11or other key store types that don't require a path.aKeyStorePassword- The key store password. May benullto indicate that no password is required.- Returns:
- The Java key-store object.
- Throws:
GeneralSecurityException- In case of a key store errorIOException- In case key store loading failsIllegalArgumentException- If the key store path is invalid- See Also:
KeyStore.load(InputStream, char[])
-
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 benullsAliasToCopy- The name of the alias in the source key store that should be put in the new key storeaAliasPassword- The optional password to access the alias in the source key store. If it is notnullthe 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 errorIOException- 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 benull.sKeyStorePath- Path to the key store. May not benullfor all key store types that require a path.sKeyStorePassword- Password for the key store. May not benullto 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 benull.sKeyStorePath- Key store path. For nice error messages only. May not benull.sKeyStoreKeyAlias- The alias to be resolved in the key store. Must be non-nullto succeed.aKeyStoreKeyPassword- The key password for the key store. Must be non-nullto 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 benull.sKeyStorePath- Key store path. For nice error messages only. May not benull.sKeyStoreKeyAlias- The alias to be resolved in the key store. Must be non-nullto succeed.aKeyStoreKeyPassword- The key password for the key store. Must be non-nullto 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 benull.sKeyStorePath- Key store path. For nice error messages only. May not benull.sKeyStoreKeyAlias- The alias to be resolved in the key store. Must be non-nullto succeed.aKeyStoreKeyPassword- The key password for the key store. Must be non-nullto succeed.- Returns:
- The key loading result. Never
null.
-
-