Configure keystores and trust stores for AS4 message signing and encryption using the two built-in crypto factory approaches.
phase4 uses Apache WSS4J (with the Merlin provider) for WS-Security operations: signing, signature verification, encryption, and decryption. A crypto factory ties the key material to the AS4 stack.There are two built-in crypto factory classes:
AS4CryptoFactoryConfiguration
Reads keystore and truststore paths and passwords from the global application.properties. The simplest approach for a single, application-wide key pair.
AS4CryptoFactoryInMemoryKeyStore
Receives an already-loaded java.security.KeyStore object at construction time. Use this when you load keys programmatically, from a secrets manager, or need per-PMode key material.
import com.helger.phase4.crypto.AS4CryptoFactoryConfiguration;// Reads from the active AS4Configuration (application.properties etc.)AS4CryptoFactoryConfiguration cryptoFactory = AS4CryptoFactoryConfiguration.getDefaultInstance();// Returns null instead of throwing if configuration is incomplete:AS4CryptoFactoryConfiguration cryptoFactoryOrNull = AS4CryptoFactoryConfiguration.getDefaultInstanceOrNull();
AS4KeyStoreDescriptor is a static helper that reads key store configuration from a IConfigWithFallback object and produces a KeyStoreAndKeyDescriptor. It reads the following properties relative to the given prefix:
Suffix
Description
keystore.type
Key store type (JKS, PKCS12, …). Default: JKS.
keystore.file
Path to the key store file. Mandatory.
keystore.password
Key store password. Mandatory.
keystore.alias
Alias of the private key entry. Mandatory.
keystore.private.password
Private key password. Mandatory.
Returns null if any mandatory property is missing.
To use a different key pair for each P-Mode, implement the IAS4PModeAwareCryptoFactory interface. phase4 will call it with the active PMode and select the appropriate factory:
import com.helger.phase4.crypto.IAS4CryptoFactory;import com.helger.phase4.crypto.IAS4PModeAwareCryptoFactory;import com.helger.phase4.model.pmode.IPMode;public class MyPerPModeCryptoFactory implements IAS4PModeAwareCryptoFactory { @Override public IAS4CryptoFactory getCryptoFactory(IPMode pmode) { if ("peppol-prod".equals(pmode.getID())) { return prodFactory; } return defaultFactory; }}
Use IAS4PModeAwareCryptoFactory when your deployment handles messages for multiple Peppol participants or profiles that require separate key material.