diff --git a/phase4-euctp-client/src/test/java/com/helger/phase4/euctp/MainPhase4EuCtpSenderExample.java b/phase4-euctp-client/src/test/java/com/helger/phase4/euctp/MainPhase4EuCtpSenderExample.java index cc3af36ef..f75542ab1 100644 --- a/phase4-euctp-client/src/test/java/com/helger/phase4/euctp/MainPhase4EuCtpSenderExample.java +++ b/phase4-euctp-client/src/test/java/com/helger/phase4/euctp/MainPhase4EuCtpSenderExample.java @@ -44,8 +44,6 @@ import com.helger.phase4.client.AS4ClientReceiptMessage; import com.helger.phase4.client.AS4ClientSentMessage; import com.helger.phase4.crypto.AS4CryptoFactoryInMemoryKeyStore; -import com.helger.phase4.crypto.AS4KeyStoreDescriptor; -import com.helger.phase4.crypto.AS4TrustStoreDescriptor; import com.helger.phase4.crypto.ECryptoAlgorithmC14N; import com.helger.phase4.crypto.ECryptoKeyEncryptionAlgorithm; import com.helger.phase4.crypto.IAS4CryptoFactory; @@ -68,6 +66,8 @@ import com.helger.phase4.util.AS4ResourceHelper; import com.helger.phase4.util.Phase4Exception; import com.helger.security.keystore.EKeyStoreType; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; +import com.helger.security.keystore.TrustStoreDescriptor; import com.helger.servlet.mock.MockServletContext; import com.helger.web.scope.mgr.WebScopeManager; @@ -80,21 +80,21 @@ public class MainPhase4EuCtpSenderExample @Nonnull private static IAS4CryptoFactory _buildAs4CryptoFactory () { - return new AS4CryptoFactoryInMemoryKeyStore (AS4KeyStoreDescriptor.builder () - .type (EKeyStoreType.PKCS12) - .path (System.getenv ("AS4_SIGNING_KEYSTORE_PATH")) - .password (System.getenv ("AS4_SIGNING_KEYSTORE_PASSWORD")) - .keyAlias (System.getenv ("AS4_SIGNING_KEY_ALIAS")) - .keyPassword (System.getenv ("AS4_SIGNING_KEY_PASSWORD")) - .build (), + return new AS4CryptoFactoryInMemoryKeyStore (KeyStoreAndKeyDescriptor.builder () + .type (EKeyStoreType.PKCS12) + .path (System.getenv ("AS4_SIGNING_KEYSTORE_PATH")) + .password (System.getenv ("AS4_SIGNING_KEYSTORE_PASSWORD")) + .keyAlias (System.getenv ("AS4_SIGNING_KEY_ALIAS")) + .keyPassword (System.getenv ("AS4_SIGNING_KEY_PASSWORD")) + .build (), // must include the Taxud CA // and intermediate // certificates - AS4TrustStoreDescriptor.builder () - .type (EKeyStoreType.PKCS12) - .path (System.getenv ("AS4_SIGNING_TRUST_KEYSTORE_PATH")) - .password (System.getenv ("AS4_SIGNING_TRUST_KEYSTORE_PASSWORD")) - .build ()); + TrustStoreDescriptor.builder () + .type (EKeyStoreType.PKCS12) + .path (System.getenv ("AS4_SIGNING_TRUST_KEYSTORE_PATH")) + .password (System.getenv ("AS4_SIGNING_TRUST_KEYSTORE_PASSWORD")) + .build ()); } public static void main (final String [] args) diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryConfiguration.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryConfiguration.java index 6e37dec38..cf864a5cf 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryConfiguration.java +++ b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryConfiguration.java @@ -31,6 +31,8 @@ import com.helger.config.IConfig; import com.helger.config.fallback.IConfigWithFallback; import com.helger.phase4.config.AS4Configuration; +import com.helger.security.keystore.IKeyStoreAndKeyDescriptor; +import com.helger.security.keystore.ITrustStoreDescriptor; import com.helger.security.keystore.LoadedKey; import com.helger.security.keystore.LoadedKeyStore; @@ -93,8 +95,8 @@ public static AS4CryptoFactoryConfiguration getDefaultInstanceOrNull () } } - private final IAS4KeyStoreDescriptor m_aKeyStoreDesc; - private final IAS4TrustStoreDescriptor m_aTrustStorDesc; + private final IKeyStoreAndKeyDescriptor m_aKeyStoreDesc; + private final ITrustStoreDescriptor m_aTrustStorDesc; /** * This constructor takes the configuration object and uses the default prefix @@ -109,10 +111,10 @@ public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig } @Nonnull - private static IAS4KeyStoreDescriptor _loadKeyStore (@Nonnull final IConfigWithFallback aConfig, - @Nonnull @Nonempty final String sConfigPrefix) + private static IKeyStoreAndKeyDescriptor _loadKeyStore (@Nonnull final IConfigWithFallback aConfig, + @Nonnull @Nonempty final String sConfigPrefix) { - final IAS4KeyStoreDescriptor aDescriptor = AS4KeyStoreDescriptor.createFromConfig (aConfig, sConfigPrefix, null); + final IKeyStoreAndKeyDescriptor aDescriptor = AS4KeyStoreDescriptor.createFromConfig (aConfig, sConfigPrefix, null); final LoadedKeyStore aLKS = aDescriptor.loadKeyStore (); if (aLKS.getKeyStore () == null) { @@ -136,13 +138,11 @@ private static IAS4KeyStoreDescriptor _loadKeyStore (@Nonnull final IConfigWithF } @Nullable - private static IAS4TrustStoreDescriptor _loadTrustStore (@Nonnull final IConfigWithFallback aConfig, - @Nonnull @Nonempty final String sConfigPrefix) + private static ITrustStoreDescriptor _loadTrustStore (@Nonnull final IConfigWithFallback aConfig, + @Nonnull @Nonempty final String sConfigPrefix) { // Load the trust store - may be null - final IAS4TrustStoreDescriptor aDescriptor = AS4TrustStoreDescriptor.createFromConfig (aConfig, - sConfigPrefix, - null); + final ITrustStoreDescriptor aDescriptor = AS4TrustStoreDescriptor.createFromConfig (aConfig, sConfigPrefix, null); if (aDescriptor != null) { final LoadedKeyStore aLTS = aDescriptor.loadTrustStore (); @@ -182,8 +182,8 @@ public AS4CryptoFactoryConfiguration (@Nonnull final IConfigWithFallback aConfig * The trust store descriptor. May be null in which case * the global JRE CA certs list will be used. */ - private AS4CryptoFactoryConfiguration (@Nonnull final IAS4KeyStoreDescriptor aKeyStoreDesc, - @Nonnull final IAS4TrustStoreDescriptor aTrustStorDesc) + private AS4CryptoFactoryConfiguration (@Nonnull final IKeyStoreAndKeyDescriptor aKeyStoreDesc, + @Nonnull final ITrustStoreDescriptor aTrustStorDesc) { super (aKeyStoreDesc, aTrustStorDesc); m_aKeyStoreDesc = aKeyStoreDesc; @@ -194,7 +194,7 @@ private AS4CryptoFactoryConfiguration (@Nonnull final IAS4KeyStoreDescriptor aKe * @return The descriptor used to load the key store. Never null. */ @Nonnull - public IAS4KeyStoreDescriptor getKeyStoreDescriptor () + public IKeyStoreAndKeyDescriptor getKeyStoreDescriptor () { return m_aKeyStoreDesc; } @@ -204,7 +204,7 @@ public IAS4KeyStoreDescriptor getKeyStoreDescriptor () * null. */ @Nonnull - public IAS4TrustStoreDescriptor getTrustStoreDescriptor () + public ITrustStoreDescriptor getTrustStoreDescriptor () { return m_aTrustStorDesc; } diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryInMemoryKeyStore.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryInMemoryKeyStore.java index 7f935b803..fc9326541 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryInMemoryKeyStore.java +++ b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4CryptoFactoryInMemoryKeyStore.java @@ -28,6 +28,8 @@ import com.helger.commons.ValueEnforcer; import com.helger.commons.annotation.Nonempty; import com.helger.commons.string.ToStringGenerator; +import com.helger.security.keystore.IKeyStoreAndKeyDescriptor; +import com.helger.security.keystore.ITrustStoreDescriptor; /** * This class contains an implementation of {@link IAS4CryptoFactory} in which @@ -58,8 +60,8 @@ public class AS4CryptoFactoryInMemoryKeyStore extends AbstractAS4CryptoFactory * the global JRE CA certs list will be used. * @since 3.0.0 */ - public AS4CryptoFactoryInMemoryKeyStore (@Nonnull final IAS4KeyStoreDescriptor aKeyStoreDesc, - @Nullable final IAS4TrustStoreDescriptor aTrustStoreDesc) + public AS4CryptoFactoryInMemoryKeyStore (@Nonnull final IKeyStoreAndKeyDescriptor aKeyStoreDesc, + @Nullable final ITrustStoreDescriptor aTrustStoreDesc) { this (aKeyStoreDesc.loadKeyStore ().getKeyStore (), aKeyStoreDesc.getKeyAlias (), diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4KeyStoreDescriptor.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4KeyStoreDescriptor.java index 4f4467c2d..f768ce343 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4KeyStoreDescriptor.java +++ b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4KeyStoreDescriptor.java @@ -16,138 +16,31 @@ */ package com.helger.phase4.crypto; -import java.security.KeyStore.PrivateKeyEntry; import java.security.Provider; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; import com.helger.commons.ValueEnforcer; import com.helger.commons.annotation.Nonempty; -import com.helger.commons.annotation.ReturnsMutableObject; -import com.helger.commons.builder.IBuilder; import com.helger.commons.string.StringHelper; -import com.helger.commons.string.ToStringGenerator; import com.helger.config.fallback.IConfigWithFallback; import com.helger.phase4.config.AS4Configuration; import com.helger.security.keystore.EKeyStoreType; -import com.helger.security.keystore.IKeyStoreType; -import com.helger.security.keystore.KeyStoreHelper; -import com.helger.security.keystore.LoadedKey; -import com.helger.security.keystore.LoadedKeyStore; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; /** - * The default implementation of {@link IAS4KeyStoreDescriptor}. + * A specific helper for {@link KeyStoreAndKeyDescriptor} * * @author Philip Helger * @since 3.0.0 */ -public class AS4KeyStoreDescriptor implements IAS4KeyStoreDescriptor +@Immutable +public final class AS4KeyStoreDescriptor { - private final IKeyStoreType m_aType; - private final String m_sPath; - private final char [] m_aPassword; - private final Provider m_aProvider; - private final String m_sKeyAlias; - private final char [] m_aKeyPassword; - // Lazily initialized - private LoadedKeyStore m_aLKS; - private LoadedKey m_aLK; - - public AS4KeyStoreDescriptor (@Nonnull final IKeyStoreType aType, - @Nonnull @Nonempty final String sPath, - @Nonnull final char [] aPassword, - @Nullable final Provider aProvider, - @Nonnull @Nonempty final String sKeyAlias, - @Nonnull final char [] aKeyPassword) - { - ValueEnforcer.notNull (aType, "Type"); - ValueEnforcer.notEmpty (sPath, "Path"); - ValueEnforcer.notNull (aPassword, "Password"); - ValueEnforcer.notEmpty (sKeyAlias, "KeyAlias"); - ValueEnforcer.notNull (aKeyPassword, "KeyPassword"); - m_aType = aType; - m_sPath = sPath; - m_aPassword = aPassword; - m_aProvider = aProvider; - m_sKeyAlias = sKeyAlias; - m_aKeyPassword = aKeyPassword; - } - - @Nonnull - public IKeyStoreType getKeyStoreType () - { - return m_aType; - } - - @Nonnull - @Nonempty - public String getKeyStorePath () - { - return m_sPath; - } - - @Nonnull - @ReturnsMutableObject - public char [] getKeyStorePassword () - { - return m_aPassword; - } - - @Nullable - public Provider getProvider () - { - return m_aProvider; - } - - @Nonnull - public LoadedKeyStore loadKeyStore () - { - LoadedKeyStore ret = m_aLKS; - if (ret == null) - ret = m_aLKS = KeyStoreHelper.loadKeyStore (m_aType, m_sPath, m_aPassword, m_aProvider); - return ret; - } - - @Nonnull - @Nonempty - public String getKeyAlias () - { - return m_sKeyAlias; - } - - @Nonnull - @ReturnsMutableObject - public char [] getKeyPassword () - { - return m_aKeyPassword; - } - - @Nonnull - public LoadedKey loadKey () - { - LoadedKey ret = m_aLK; - if (ret == null) - { - ret = m_aLK = KeyStoreHelper.loadPrivateKey (loadKeyStore ().getKeyStore (), - m_sPath, - m_sKeyAlias, - m_aKeyPassword); - } - return ret; - } - - @Override - public String toString () - { - return new ToStringGenerator (null).append ("Type", m_aType) - .append ("Path", m_sPath) - .appendPassword ("Password") - .appendIfNotNull ("Provider", m_aProvider) - .append ("KeyAlias", m_sKeyAlias) - .appendPassword ("KeyPassword") - .getToString (); - } + private AS4KeyStoreDescriptor () + {} /** * Create the key store descriptor from the default configuration item. The @@ -162,11 +55,11 @@ public String toString () * password * * - * @return A new {@link AS4KeyStoreDescriptor} object and never + * @return A new {@link KeyStoreAndKeyDescriptor} object and never * null. */ @Nonnull - public static AS4KeyStoreDescriptor createFromConfig () + public static KeyStoreAndKeyDescriptor createFromConfig () { return createFromConfig (AS4Configuration.getConfig (), CAS4Crypto.DEFAULT_CONFIG_PREFIX, null); } @@ -192,13 +85,13 @@ public static AS4KeyStoreDescriptor createFromConfig () * @param aProvider * The Java security provider for loading the key store. May be * null to use the default. - * @return A new {@link AS4KeyStoreDescriptor} object and never + * @return A new {@link KeyStoreAndKeyDescriptor} object and never * null. */ @Nonnull - public static AS4KeyStoreDescriptor createFromConfig (@Nonnull final IConfigWithFallback aConfig, - @Nonnull @Nonempty final String sConfigPrefix, - @Nullable final Provider aProvider) + public static KeyStoreAndKeyDescriptor createFromConfig (@Nonnull final IConfigWithFallback aConfig, + @Nonnull @Nonempty final String sConfigPrefix, + @Nullable final Provider aProvider) { ValueEnforcer.notNull (aConfig, "Config"); ValueEnforcer.notEmpty (sConfigPrefix, "ConfigPrefix"); @@ -215,128 +108,6 @@ public static AS4KeyStoreDescriptor createFromConfig (@Nonnull final IConfigWith final String sKeyAlias = aConfig.getAsString (sConfigPrefix + "keystore.alias"); final char [] aKeyPassword = aConfig.getAsCharArray (sConfigPrefix + "keystore.private.password"); - return new AS4KeyStoreDescriptor (aType, sPath, aPassword, aProvider, sKeyAlias, aKeyPassword); - } - - /** - * @return A new builder for {@link AS4KeyStoreDescriptor} objects. Never - * null. - */ - @Nonnull - public static AS4KeyStoreDescriptorBuilder builder () - { - return new AS4KeyStoreDescriptorBuilder (); - } - - /** - * Create a new builder using the provided descriptor. - * - * @param a - * The existing descriptor. May not be null. - * @return A new builder for {@link AS4KeyStoreDescriptor} objects. Never - * null. - */ - @Nonnull - public static AS4KeyStoreDescriptorBuilder builder (@Nonnull final AS4KeyStoreDescriptor a) - { - return new AS4KeyStoreDescriptorBuilder (a); - } - - /** - * Builder class for class {@link AS4KeyStoreDescriptor}. - * - * @author Philip Helger - */ - public static class AS4KeyStoreDescriptorBuilder implements IBuilder - { - private IKeyStoreType m_aType; - private String m_sPath; - private char [] m_aPassword; - private Provider m_aProvider; - private String m_sKeyAlias; - private char [] m_aKeyPassword; - - public AS4KeyStoreDescriptorBuilder () - {} - - public AS4KeyStoreDescriptorBuilder (@Nonnull final AS4KeyStoreDescriptor a) - { - type (a.m_aType).path (a.m_sPath) - .password (a.m_aPassword) - .provider (m_aProvider) - .keyAlias (m_sKeyAlias) - .keyPassword (m_aKeyPassword); - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder type (@Nullable final IKeyStoreType a) - { - m_aType = a; - return this; - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder path (@Nullable final String s) - { - m_sPath = s; - return this; - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder password (@Nullable final String s) - { - return password (s == null ? null : s.toCharArray ()); - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder password (@Nullable final char [] a) - { - m_aPassword = a; - return this; - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder provider (@Nullable final Provider a) - { - m_aProvider = a; - return this; - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder keyAlias (@Nullable final String s) - { - m_sKeyAlias = s; - return this; - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder keyPassword (@Nullable final String s) - { - return keyPassword (s == null ? null : s.toCharArray ()); - } - - @Nonnull - public final AS4KeyStoreDescriptorBuilder keyPassword (@Nullable final char [] a) - { - m_aKeyPassword = a; - return this; - } - - @Nonnull - public AS4KeyStoreDescriptor build () - { - if (m_aType == null) - throw new IllegalStateException ("Type is missing"); - if (StringHelper.hasNoText (m_sPath)) - throw new IllegalStateException ("Path is empty"); - if (m_aPassword == null) - throw new IllegalStateException ("Password is missing"); - // Provider may be null - if (StringHelper.hasNoText (m_sKeyAlias)) - throw new IllegalStateException ("KeyAlias is empty"); - if (m_aKeyPassword == null) - throw new IllegalStateException ("KeyPassword is missing"); - return new AS4KeyStoreDescriptor (m_aType, m_sPath, m_aPassword, m_aProvider, m_sKeyAlias, m_aKeyPassword); - } + return new KeyStoreAndKeyDescriptor (aType, sPath, aPassword, aProvider, sKeyAlias, aKeyPassword); } } diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4TrustStoreDescriptor.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4TrustStoreDescriptor.java index cf9696dda..e39f42a1a 100644 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4TrustStoreDescriptor.java +++ b/phase4-lib/src/main/java/com/helger/phase4/crypto/AS4TrustStoreDescriptor.java @@ -20,93 +20,27 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; import com.helger.commons.ValueEnforcer; import com.helger.commons.annotation.Nonempty; -import com.helger.commons.annotation.ReturnsMutableObject; -import com.helger.commons.builder.IBuilder; import com.helger.commons.string.StringHelper; -import com.helger.commons.string.ToStringGenerator; import com.helger.config.fallback.IConfigWithFallback; import com.helger.phase4.config.AS4Configuration; import com.helger.security.keystore.EKeyStoreType; -import com.helger.security.keystore.IKeyStoreType; -import com.helger.security.keystore.KeyStoreHelper; -import com.helger.security.keystore.LoadedKeyStore; +import com.helger.security.keystore.TrustStoreDescriptor; /** - * The default implementation of {@link IAS4TrustStoreDescriptor}. + * A specific helper for {@link TrustStoreDescriptor} * * @author Philip Helger * @since 3.0.0 */ -public class AS4TrustStoreDescriptor implements IAS4TrustStoreDescriptor +@Immutable +public final class AS4TrustStoreDescriptor { - private final IKeyStoreType m_aType; - private final String m_sPath; - private final char [] m_aPassword; - private final Provider m_aProvider; - // Lazily initialized - private LoadedKeyStore m_aLTS; - - public AS4TrustStoreDescriptor (@Nonnull final IKeyStoreType aType, - @Nonnull @Nonempty final String sPath, - @Nonnull final char [] aPassword, - @Nullable final Provider aProvider) - { - ValueEnforcer.notNull (aType, "Type"); - ValueEnforcer.notEmpty (sPath, "Path"); - ValueEnforcer.notNull (aPassword, "Password"); - m_aType = aType; - m_sPath = sPath; - m_aPassword = aPassword; - m_aProvider = aProvider; - } - - @Nonnull - public IKeyStoreType getTrustStoreType () - { - return m_aType; - } - - @Nonnull - @Nonempty - public String getTrustStorePath () - { - return m_sPath; - } - - @Nonnull - @ReturnsMutableObject - public char [] getTrustStorePassword () - { - return m_aPassword; - } - - @Nullable - public Provider getProvider () - { - return m_aProvider; - } - - @Nonnull - public LoadedKeyStore loadTrustStore () - { - LoadedKeyStore ret = m_aLTS; - if (ret == null) - ret = m_aLTS = KeyStoreHelper.loadKeyStore (m_aType, m_sPath, m_aPassword, m_aProvider); - return ret; - } - - @Override - public String toString () - { - return new ToStringGenerator (null).append ("Type", m_aType) - .append ("Path", m_sPath) - .appendPassword ("Password") - .appendIfNotNull ("Provider", m_aProvider) - .getToString (); - } + private AS4TrustStoreDescriptor () + {} /** * Create the trust store descriptor from the default configuration item. The @@ -119,11 +53,11 @@ public String toString () * password * * - * @return A new {@link AS4TrustStoreDescriptor} object or null - * if path or password are not present. + * @return A new {@link TrustStoreDescriptor} object or null if + * path or password are not present. */ @Nullable - public static AS4TrustStoreDescriptor createFromConfig () + public static TrustStoreDescriptor createFromConfig () { return createFromConfig (AS4Configuration.getConfig (), CAS4Crypto.DEFAULT_CONFIG_PREFIX, null); } @@ -147,13 +81,13 @@ public static AS4TrustStoreDescriptor createFromConfig () * @param aProvider * The Java security provider for loading the trust store. May be * null to use the default. - * @return A new {@link AS4TrustStoreDescriptor} object or null - * if path or password are not present. + * @return A new {@link TrustStoreDescriptor} object or null if + * path or password are not present. */ @Nullable - public static AS4TrustStoreDescriptor createFromConfig (@Nonnull final IConfigWithFallback aConfig, - @Nonnull @Nonempty final String sConfigPrefix, - @Nullable final Provider aProvider) + public static TrustStoreDescriptor createFromConfig (@Nonnull final IConfigWithFallback aConfig, + @Nonnull @Nonempty final String sConfigPrefix, + @Nullable final Provider aProvider) { ValueEnforcer.notNull (aConfig, "Config"); ValueEnforcer.notEmpty (sConfigPrefix, "ConfigPrefix"); @@ -170,98 +104,6 @@ public static AS4TrustStoreDescriptor createFromConfig (@Nonnull final IConfigWi if (StringHelper.hasNoText (sPath) || aPassword == null) return null; - return new AS4TrustStoreDescriptor (aType, sPath, aPassword, aProvider); - } - - /** - * @return A new builder for {@link AS4TrustStoreDescriptor} objects. Never - * null. - */ - @Nonnull - public static AS4TrustStoreDescriptorBuilder builder () - { - return new AS4TrustStoreDescriptorBuilder (); - } - - /** - * Create a new builder using the provided descriptor. - * - * @param a - * The existing descriptor. May not be null. - * @return A new builder for {@link AS4TrustStoreDescriptor} objects. Never - * null. - */ - @Nonnull - public static AS4TrustStoreDescriptorBuilder builder (@Nonnull final AS4TrustStoreDescriptor a) - { - return new AS4TrustStoreDescriptorBuilder (a); - } - - /** - * Builder class for class {@link AS4TrustStoreDescriptor}. - * - * @author Philip Helger - */ - public static class AS4TrustStoreDescriptorBuilder implements IBuilder - { - private IKeyStoreType m_aType; - private String m_sPath; - private char [] m_aPassword; - private Provider m_aProvider; - - public AS4TrustStoreDescriptorBuilder () - {} - - public AS4TrustStoreDescriptorBuilder (@Nonnull final AS4TrustStoreDescriptor a) - { - type (a.m_aType).path (a.m_sPath).password (a.m_aPassword).provider (m_aProvider); - } - - @Nonnull - public final AS4TrustStoreDescriptorBuilder type (@Nullable final IKeyStoreType a) - { - m_aType = a; - return this; - } - - @Nonnull - public final AS4TrustStoreDescriptorBuilder path (@Nullable final String s) - { - m_sPath = s; - return this; - } - - @Nonnull - public final AS4TrustStoreDescriptorBuilder password (@Nullable final String s) - { - return password (s == null ? null : s.toCharArray ()); - } - - @Nonnull - public final AS4TrustStoreDescriptorBuilder password (@Nullable final char [] a) - { - m_aPassword = a; - return this; - } - - @Nonnull - public final AS4TrustStoreDescriptorBuilder provider (@Nullable final Provider a) - { - m_aProvider = a; - return this; - } - - @Nonnull - public AS4TrustStoreDescriptor build () - { - if (m_aType == null) - throw new IllegalStateException ("Type is missing"); - if (StringHelper.hasNoText (m_sPath)) - throw new IllegalStateException ("Path is empty"); - if (m_aPassword == null) - throw new IllegalStateException ("Password is missing"); - // Provider may be null - return new AS4TrustStoreDescriptor (m_aType, m_sPath, m_aPassword, m_aProvider); - } + return new TrustStoreDescriptor (aType, sPath, aPassword, aProvider); } } diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4KeyStoreDescriptor.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4KeyStoreDescriptor.java deleted file mode 100644 index 021b3302e..000000000 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4KeyStoreDescriptor.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2015-2024 Philip Helger (www.helger.com) - * philip[at]helger[dot]com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.helger.phase4.crypto; - -import java.security.KeyStore.PrivateKeyEntry; -import java.security.Provider; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.helger.commons.annotation.Nonempty; -import com.helger.security.keystore.IKeyStoreType; -import com.helger.security.keystore.LoadedKey; -import com.helger.security.keystore.LoadedKeyStore; - -/** - * Interface describing the parameters needed to reference a key store. - * - * @author Philip Helger - * @since 3.0.0 - */ -public interface IAS4KeyStoreDescriptor -{ - /** - * @return The type of the key store. May not be null. - */ - @Nonnull - IKeyStoreType getKeyStoreType (); - - /** - * @return The path to the key store. May neither be null nor - * empty. The interpretation of the path is implementation dependent. - */ - @Nonnull - @Nonempty - String getKeyStorePath (); - - /** - * @return The password required to open the key store. May not be - * null but may be empty. - */ - @Nonnull - char [] getKeyStorePassword (); - - /** - * @return The Java security provider for loading the key store. May be - * null. - */ - @Nullable - Provider getProvider (); - - /** - * @return The loaded key store based on the parameters in this descriptor. - * Never null. - */ - @Nonnull - LoadedKeyStore loadKeyStore (); - - /** - * Note: the case sensitivity of the key alias depends on the key store type. - * - * @return The alias of the key inside a key store. May neither be - * null nor empty. - */ - @Nonnull - @Nonempty - String getKeyAlias (); - - /** - * @return The password required to access the key inside the key store. May - * not be null but may be empty. - */ - @Nonnull - char [] getKeyPassword (); - - /** - * @return The loaded key based on the loaded key store and the parameters in - * this descriptor. - */ - @Nonnull - LoadedKey loadKey (); -} diff --git a/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4TrustStoreDescriptor.java b/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4TrustStoreDescriptor.java deleted file mode 100644 index db1937d6a..000000000 --- a/phase4-lib/src/main/java/com/helger/phase4/crypto/IAS4TrustStoreDescriptor.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (C) 2015-2024 Philip Helger (www.helger.com) - * philip[at]helger[dot]com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.helger.phase4.crypto; - -import java.security.Provider; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.helger.commons.annotation.Nonempty; -import com.helger.security.keystore.IKeyStoreType; -import com.helger.security.keystore.LoadedKeyStore; - -/** - * Interface describing the parameters needed to reference a trust store. - * - * @author Philip Helger - * @since 3.0.0 - */ -public interface IAS4TrustStoreDescriptor -{ - /** - * @return The type of the trust store. May not be null. - */ - @Nonnull - IKeyStoreType getTrustStoreType (); - - /** - * @return The path to the trust store. May neither be null nor - * empty. The interpretation of the path is implementation dependent. - */ - @Nonnull - @Nonempty - String getTrustStorePath (); - - /** - * @return The password required to open the trust store. May not be - * null but may be empty. - */ - @Nonnull - char [] getTrustStorePassword (); - - /** - * @return The Java security provider for loading the trust store. May be - * null. - */ - @Nullable - Provider getProvider (); - - /** - * @return The loaded trust store based on the parameters in this descriptor. - * Never null. - */ - @Nonnull - LoadedKeyStore loadTrustStore (); -} diff --git a/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java b/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java index 4f04c2ca0..6f1e005be 100644 --- a/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java +++ b/phase4-lib/src/test/java/com/helger/phase4/supplementary/tools/MainDecipherAS4In.java @@ -27,6 +27,8 @@ import com.helger.phase4.crypto.AS4TrustStoreDescriptor; import com.helger.phase4.crypto.IAS4CryptoFactory; import com.helger.phase4.dump.AS4DumpReader; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; +import com.helger.security.keystore.TrustStoreDescriptor; /** * This is a small tool that demonstrates how the "as4in" files can be decrypted @@ -48,14 +50,14 @@ public static void main (final String [] args) throws Exception throw new IllegalStateException (); // Change path of key store and trust store - AS4KeyStoreDescriptor aKSD = AS4KeyStoreDescriptor.createFromConfig (); - aKSD = AS4KeyStoreDescriptor.builder (aKSD) - .path (folder.getAbsolutePath () + "/" + aKSD.getKeyStorePath ()) - .build (); - AS4TrustStoreDescriptor aTSD = AS4TrustStoreDescriptor.createFromConfig (); - aTSD = AS4TrustStoreDescriptor.builder (aTSD) - .path (folder.getAbsolutePath () + "/" + aTSD.getTrustStorePath ()) - .build (); + KeyStoreAndKeyDescriptor aKSD = AS4KeyStoreDescriptor.createFromConfig (); + aKSD = KeyStoreAndKeyDescriptor.builder (aKSD) + .path (folder.getAbsolutePath () + "/" + aKSD.getKeyStorePath ()) + .build (); + TrustStoreDescriptor aTSD = AS4TrustStoreDescriptor.createFromConfig (); + aTSD = TrustStoreDescriptor.builder (aTSD) + .path (folder.getAbsolutePath () + "/" + aTSD.getTrustStorePath ()) + .build (); final IAS4CryptoFactory aCryptoFactory = new AS4CryptoFactoryInMemoryKeyStore (aKSD, aTSD); LOGGER.info ("Reading " + f.getName ()); diff --git a/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/receivers/MainPhase4PeppolSenderExplicitCryptoProperties.java b/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/receivers/MainPhase4PeppolSenderExplicitCryptoProperties.java index 5348edd85..daf2c1cad 100644 --- a/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/receivers/MainPhase4PeppolSenderExplicitCryptoProperties.java +++ b/phase4-peppol-client/src/test/java/com/helger/phase4/peppol/receivers/MainPhase4PeppolSenderExplicitCryptoProperties.java @@ -26,13 +26,13 @@ import com.helger.peppol.utils.PeppolKeyStoreHelper; import com.helger.peppolid.IParticipantIdentifier; import com.helger.phase4.crypto.AS4CryptoFactoryInMemoryKeyStore; -import com.helger.phase4.crypto.AS4KeyStoreDescriptor; -import com.helger.phase4.crypto.AS4TrustStoreDescriptor; import com.helger.phase4.peppol.Phase4PeppolSender; import com.helger.phase4.peppol.Phase4PeppolValidatonResultHandler; import com.helger.phase4.sender.EAS4UserMessageSendResult; import com.helger.phive.peppol.PeppolValidation2024_05; import com.helger.security.keystore.EKeyStoreType; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; +import com.helger.security.keystore.TrustStoreDescriptor; import com.helger.servlet.mock.MockServletContext; import com.helger.smpclient.peppol.SMPClientReadOnly; import com.helger.web.scope.mgr.WebScopeManager; @@ -57,18 +57,18 @@ public static void main (final String [] args) if (aPayloadBytes == null) throw new IllegalStateException ("Failed to read XML file to be send"); - final AS4KeyStoreDescriptor aKSD = AS4KeyStoreDescriptor.builder () - .type (EKeyStoreType.PKCS12) - .path ("test-ap.p12") - .password ("peppol") - .keyAlias ("openpeppol aisbl id von pop000306") - .keyPassword ("peppol") - .build (); - final AS4TrustStoreDescriptor aTSD = AS4TrustStoreDescriptor.builder () - .type (PeppolKeyStoreHelper.TRUSTSTORE_TYPE) - .path (PeppolKeyStoreHelper.Config2018.TRUSTSTORE_AP_PILOT_CLASSPATH) - .password (PeppolKeyStoreHelper.TRUSTSTORE_PASSWORD) - .build (); + final KeyStoreAndKeyDescriptor aKSD = KeyStoreAndKeyDescriptor.builder () + .type (EKeyStoreType.PKCS12) + .path ("test-ap.p12") + .password ("peppol") + .keyAlias ("openpeppol aisbl id von pop000306") + .keyPassword ("peppol") + .build (); + final TrustStoreDescriptor aTSD = TrustStoreDescriptor.builder () + .type (PeppolKeyStoreHelper.TRUSTSTORE_TYPE) + .path (PeppolKeyStoreHelper.Config2018.TRUSTSTORE_AP_PILOT_CLASSPATH) + .password (PeppolKeyStoreHelper.TRUSTSTORE_PASSWORD) + .build (); // Start configuring here final IParticipantIdentifier aReceiverID = Phase4PeppolSender.IF.createParticipantIdentifierWithDefaultScheme ("9958:peppol-development-governikus-01"); diff --git a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientReceiptMessageTest.java b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientReceiptMessageTest.java index cfc7beeff..7c941d99f 100644 --- a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientReceiptMessageTest.java +++ b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientReceiptMessageTest.java @@ -29,7 +29,6 @@ import com.helger.commons.io.resource.ClassPathResource; import com.helger.phase4.AS4TestConstants; import com.helger.phase4.crypto.AS4CryptoFactoryInMemoryKeyStore; -import com.helger.phase4.crypto.AS4KeyStoreDescriptor; import com.helger.phase4.crypto.ECryptoAlgorithmSign; import com.helger.phase4.crypto.ECryptoAlgorithmSignDigest; import com.helger.phase4.model.ESoapVersion; @@ -38,6 +37,7 @@ import com.helger.phase4.server.message.MockMessages; import com.helger.phase4.util.AS4ResourceHelper; import com.helger.security.keystore.EKeyStoreType; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; import com.helger.xml.serialize.read.DOMReader; /** @@ -121,13 +121,13 @@ public void testBuildMessageSignedChecks () throws Exception aClient.setNonRepudiation (true); aClient.setReceiptShouldBeSigned (true); - aClient.setCryptoFactory (new AS4CryptoFactoryInMemoryKeyStore (AS4KeyStoreDescriptor.builder () - .type (EKeyStoreType.JKS) - .path ("keys/dummy-pw-test.jks") - .password ("test") - .keyAlias ("ph-as4") - .keyPassword ("test") - .build (), + aClient.setCryptoFactory (new AS4CryptoFactoryInMemoryKeyStore (KeyStoreAndKeyDescriptor.builder () + .type (EKeyStoreType.JKS) + .path ("keys/dummy-pw-test.jks") + .password ("test") + .keyAlias ("ph-as4") + .keyPassword ("test") + .build (), null)); aClient.signingParams () diff --git a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java index 0e63cec11..3780c9481 100644 --- a/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java +++ b/phase4-test/src/test/java/com/helger/phase4/client/AS4ClientUserMessageTest.java @@ -42,7 +42,6 @@ import com.helger.phase4.CAS4; import com.helger.phase4.attachment.EAS4CompressionMode; import com.helger.phase4.crypto.AS4CryptoFactoryInMemoryKeyStore; -import com.helger.phase4.crypto.AS4KeyStoreDescriptor; import com.helger.phase4.crypto.ECryptoAlgorithmCrypt; import com.helger.phase4.crypto.ECryptoAlgorithmSign; import com.helger.phase4.crypto.ECryptoAlgorithmSignDigest; @@ -57,6 +56,8 @@ import com.helger.phase4.test.profile.AS4TestProfileRegistarSPI; import com.helger.phase4.util.AS4ResourceHelper; import com.helger.security.keystore.EKeyStoreType; +import com.helger.security.keystore.IKeyStoreAndKeyDescriptor; +import com.helger.security.keystore.KeyStoreAndKeyDescriptor; import com.helger.xml.microdom.IMicroDocument; import com.helger.xml.microdom.serialize.MicroWriter; import com.helger.xml.serialize.read.DOMReader; @@ -173,13 +174,13 @@ private static TestClientUserMessage _createMandatoryAttributesSuccessMessage () @Nonnull private static AS4ClientUserMessage _setKeyStoreTestData (@Nonnull final AS4ClientUserMessage aClient) { - final AS4KeyStoreDescriptor aKSD = AS4KeyStoreDescriptor.builder () - .type (EKeyStoreType.JKS) - .path ("keys/dummy-pw-test.jks") - .password ("test") - .keyAlias ("ph-as4") - .keyPassword ("test") - .build (); + final IKeyStoreAndKeyDescriptor aKSD = KeyStoreAndKeyDescriptor.builder () + .type (EKeyStoreType.JKS) + .path ("keys/dummy-pw-test.jks") + .password ("test") + .keyAlias ("ph-as4") + .keyPassword ("test") + .build (); aClient.setCryptoFactory (new AS4CryptoFactoryInMemoryKeyStore (aKSD, null)); aClient.cryptParams ().setAlias (aKSD.getKeyAlias ()); return aClient;