use of com.helger.phase4.crypto.IAS4CryptoFactory in project phase4 by phax.
the class AS4ClientUserMessage method buildMessage.
@Override
@Nonnull
public AS4ClientBuiltMessage buildMessage(@Nonnull @Nonempty final String sMessageID, @Nullable final IAS4ClientBuildMessageCallback aCallback) throws WSSecurityException, MessagingException {
final String sAgreementRefPMode = m_aPModeIDFactory.apply(this);
// check mandatory attributes
_checkMandatoryAttributes();
final boolean bSign = signingParams().isSigningEnabled();
final boolean bEncrypt = cryptParams().isCryptEnabled(LOGGER::warn);
final boolean bAttachmentsPresent = m_aAttachments.isNotEmpty();
final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo(sMessageID, getRefToMessageID(), getSendingDateTimeOrNow());
final Ebms3PayloadInfo aEbms3PayloadInfo = MessageHelperMethods.createEbms3PayloadInfo(m_aPayload != null, m_aAttachments);
final Ebms3CollaborationInfo aEbms3CollaborationInfo = MessageHelperMethods.createEbms3CollaborationInfo(sAgreementRefPMode, m_sAgreementRefValue, m_sServiceType, m_sServiceValue, m_sAction, m_sConversationID);
final Ebms3PartyInfo aEbms3PartyInfo = MessageHelperMethods.createEbms3PartyInfo(m_sFromRole, m_sFromPartyIDType, m_sFromPartyIDValue, m_sToRole, m_sToPartyIDType, m_sToPartyIDValue);
final Ebms3MessageProperties aEbms3MessageProperties = MessageHelperMethods.createEbms3MessageProperties(m_aEbms3Properties);
final AS4UserMessage aUserMsg = AS4UserMessage.create(aEbms3MessageInfo, aEbms3PayloadInfo, aEbms3CollaborationInfo, aEbms3PartyInfo, aEbms3MessageProperties, getSoapVersion()).setMustUnderstand(true);
if (aCallback != null)
aCallback.onAS4Message(aUserMsg);
final Document aPureDoc = aUserMsg.getAsSoapDocument(m_aPayload);
if (aCallback != null)
aCallback.onSoapDocument(aPureDoc);
// 1. compress
// Is done when the attachments are added
// 2. sign and/or encrpyt
Document aDoc = aPureDoc;
AS4MimeMessage aMimeMsg = null;
if (bSign || bEncrypt) {
AS4HttpDebug.debug(() -> "Unsigned/unencrypted UserMessage:\n" + XMLWriter.getNodeAsString(aPureDoc, AS4HttpDebug.getDebugXMLWriterSettings()));
final IAS4CryptoFactory aCryptoFactory = internalCreateCryptoFactory();
// 2a. sign
if (bSign) {
final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage(aCryptoFactory, aDoc, getSoapVersion(), aUserMsg.getMessagingID(), m_aAttachments, getAS4ResourceHelper(), bMustUnderstand, signingParams().getClone());
aDoc = aSignedDoc;
if (aCallback != null)
aCallback.onSignedSoapDocument(aSignedDoc);
AS4HttpDebug.debug(() -> "Signed UserMessage:\n" + XMLWriter.getNodeAsString(aSignedDoc, AS4HttpDebug.getDebugXMLWriterSettings()));
}
// 2b. encrypt
if (bEncrypt) {
// MustUnderstand always set to true
final boolean bMustUnderstand = true;
if (bAttachmentsPresent) {
aMimeMsg = AS4Encryptor.encryptMimeMessage(getSoapVersion(), aDoc, m_aAttachments, aCryptoFactory, bMustUnderstand, getAS4ResourceHelper(), cryptParams().getClone());
if (aCallback != null)
aCallback.onEncryptedMimeMessage(aMimeMsg);
} else {
final Document aEncryptedDoc = AS4Encryptor.encryptSoapBodyPayload(aCryptoFactory, getSoapVersion(), aDoc, bMustUnderstand, cryptParams().getClone());
if (aCallback != null)
aCallback.onEncryptedSoapDocument(aDoc);
aDoc = aEncryptedDoc;
}
}
}
if (bAttachmentsPresent && aMimeMsg == null) {
// * not encrypted, not signed
// * not encrypted, signed
aMimeMsg = MimeMessageCreator.generateMimeMessage(getSoapVersion(), aDoc, m_aAttachments);
}
if (aMimeMsg != null) {
// Wrap MIME message
return new AS4ClientBuiltMessage(sMessageID, new HttpMimeMessageEntity(aMimeMsg));
}
// Wrap SOAP XML
return new AS4ClientBuiltMessage(sMessageID, new HttpXMLEntity(aDoc, getSoapVersion().getMimeType()));
}
use of com.helger.phase4.crypto.IAS4CryptoFactory in project phase4 by phax.
the class AS4ClientPullRequestMessage method buildMessage.
@Override
public AS4ClientBuiltMessage buildMessage(@Nonnull @Nonempty final String sMessageID, @Nullable final IAS4ClientBuildMessageCallback aCallback) throws WSSecurityException {
_checkMandatoryAttributes();
final Ebms3MessageInfo aEbms3MessageInfo = MessageHelperMethods.createEbms3MessageInfo(sMessageID, getRefToMessageID(), getSendingDateTimeOrNow());
final AS4PullRequestMessage aPullRequest = AS4PullRequestMessage.create(getSoapVersion(), aEbms3MessageInfo, m_sMPC, any().getClone());
if (aCallback != null)
aCallback.onAS4Message(aPullRequest);
final Document aPureDoc = aPullRequest.getAsSoapDocument();
if (aCallback != null)
aCallback.onSoapDocument(aPureDoc);
Document aDoc = aPureDoc;
if (signingParams().isSigningEnabled()) {
final IAS4CryptoFactory aCryptoFactory = internalCreateCryptoFactory();
final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage(aCryptoFactory, aDoc, getSoapVersion(), aPullRequest.getMessagingID(), null, getAS4ResourceHelper(), bMustUnderstand, signingParams().getClone());
if (aCallback != null)
aCallback.onSignedSoapDocument(aSignedDoc);
aDoc = aSignedDoc;
}
// Wrap SOAP XML
return new AS4ClientBuiltMessage(sMessageID, new HttpXMLEntity(aDoc, getSoapVersion().getMimeType()));
}
use of com.helger.phase4.crypto.IAS4CryptoFactory in project phase4 by phax.
the class AS4ClientReceiptMessage method buildMessage.
@Override
public AS4ClientBuiltMessage buildMessage(@Nonnull @Nonempty final String sMessageID, @Nullable final IAS4ClientBuildMessageCallback aCallback) throws WSSecurityException {
_checkMandatoryAttributes();
final AS4ReceiptMessage aReceiptMsg = AS4ReceiptMessage.create(getSoapVersion(), sMessageID, m_aEbms3UserMessage, m_aSoapDocument, m_bNonRepudiation);
if (aCallback != null)
aCallback.onAS4Message(aReceiptMsg);
final Document aPureDoc = aReceiptMsg.getAsSoapDocument();
if (aCallback != null)
aCallback.onSoapDocument(aPureDoc);
Document aDoc = aPureDoc;
if (m_bReceiptShouldBeSigned && signingParams().isSigningEnabled()) {
final IAS4CryptoFactory aCryptoFactory = internalCreateCryptoFactory();
final boolean bMustUnderstand = true;
final Document aSignedDoc = AS4Signer.createSignedMessage(aCryptoFactory, aDoc, getSoapVersion(), aReceiptMsg.getMessagingID(), null, getAS4ResourceHelper(), bMustUnderstand, signingParams().getClone());
if (aCallback != null)
aCallback.onSignedSoapDocument(aSignedDoc);
aDoc = aSignedDoc;
}
// Wrap SOAP XML
return new AS4ClientBuiltMessage(sMessageID, new HttpXMLEntity(aDoc, getSoapVersion().getMimeType()));
}
use of com.helger.phase4.crypto.IAS4CryptoFactory in project phase4 by phax.
the class AS4XServletHandler method handleRequest.
// Don't make this final, so that subclasses can call the other handleRequest
public void handleRequest(@Nonnull final IRequestWebScopeWithoutResponse aRequestScope, @Nonnull final UnifiedResponse aUnifiedResponse) throws Exception {
// Resolved once per request
final IAS4CryptoFactory aCF = m_aCryptoFactorySupplier.get();
if (aCF == null)
throw new IllegalStateException("Failed to get an AS4 CryptoFactory");
// Created above in #createUnifiedResponse
handleRequest(aRequestScope, (AS4UnifiedResponse) aUnifiedResponse, aCF, m_aPModeResolver, m_aIAF, m_aHandlerCustomizer);
}
use of com.helger.phase4.crypto.IAS4CryptoFactory in project phase4 by phax.
the class EncryptionTest method testAES128GCM.
@Test
public void testAES128GCM() throws Exception {
final IAS4CryptoFactory aCryptoFactory = AS4CryptoFactoryProperties.getDefaultInstance();
final Document doc = _getSoapEnvelope11();
final WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
final WSSecEncrypt builder = new WSSecEncrypt(secHeader);
// builder.setUserInfo ("wss40");
builder.setUserInfo(aCryptoFactory.getKeyAlias(), aCryptoFactory.getKeyPassword());
builder.setKeyIdentifierType(ECryptoKeyIdentifierType.BST_DIRECT_REFERENCE.getTypeID());
builder.setSymmetricEncAlgorithm(ECryptoAlgorithmCrypt.AES_128_GCM.getAlgorithmURI());
// Generate a session key
final KeyGenerator aKeyGen = KeyUtils.getKeyGenerator(WSS4JConstants.AES_128);
final SecretKey aSymmetricKey = aKeyGen.generateKey();
final Document encryptedDoc = builder.build(aCryptoFactory.getCrypto(), aSymmetricKey);
final String outputString = XMLUtils.prettyDocumentToString(encryptedDoc);
// System.out.println (outputString);
assertFalse(outputString.contains("counter_port_type"));
}
Aggregations