Search in sources :

Example 6 with IAS4CryptoFactory

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()));
}
Also used : Ebms3MessageProperties(com.helger.phase4.ebms3header.Ebms3MessageProperties) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) AS4UserMessage(com.helger.phase4.messaging.domain.AS4UserMessage) Ebms3PartyInfo(com.helger.phase4.ebms3header.Ebms3PartyInfo) Ebms3PayloadInfo(com.helger.phase4.ebms3header.Ebms3PayloadInfo) Ebms3CollaborationInfo(com.helger.phase4.ebms3header.Ebms3CollaborationInfo) AS4MimeMessage(com.helger.phase4.messaging.mime.AS4MimeMessage) HttpMimeMessageEntity(com.helger.phase4.http.HttpMimeMessageEntity) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo) Nonnull(javax.annotation.Nonnull)

Example 7 with IAS4CryptoFactory

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()));
}
Also used : AS4PullRequestMessage(com.helger.phase4.messaging.domain.AS4PullRequestMessage) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document) Ebms3MessageInfo(com.helger.phase4.ebms3header.Ebms3MessageInfo)

Example 8 with IAS4CryptoFactory

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()));
}
Also used : AS4ReceiptMessage(com.helger.phase4.messaging.domain.AS4ReceiptMessage) IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) HttpXMLEntity(com.helger.phase4.http.HttpXMLEntity) Document(org.w3c.dom.Document)

Example 9 with IAS4CryptoFactory

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);
}
Also used : IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory)

Example 10 with IAS4CryptoFactory

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"));
}
Also used : IAS4CryptoFactory(com.helger.phase4.crypto.IAS4CryptoFactory) SecretKey(javax.crypto.SecretKey) WSSecHeader(org.apache.wss4j.dom.message.WSSecHeader) WSSecEncrypt(org.apache.wss4j.dom.message.WSSecEncrypt) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Aggregations

IAS4CryptoFactory (com.helger.phase4.crypto.IAS4CryptoFactory)17 IAS4ClientBuildMessageCallback (com.helger.phase4.client.IAS4ClientBuildMessageCallback)8 File (java.io.File)8 IOException (java.io.IOException)8 Nonnull (javax.annotation.Nonnull)8 Document (org.w3c.dom.Document)7 WSS4JAttachment (com.helger.phase4.attachment.WSS4JAttachment)6 Element (org.w3c.dom.Element)6 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)5 Ebms3SignalMessage (com.helger.phase4.ebms3header.Ebms3SignalMessage)5 Ebms3UserMessage (com.helger.phase4.ebms3header.Ebms3UserMessage)5 ESimpleUserMessageSendResult (com.helger.phase4.sender.AbstractAS4UserMessageBuilder.ESimpleUserMessageSendResult)5 Nullable (javax.annotation.Nullable)5 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)5 HttpXMLEntity (com.helger.phase4.http.HttpXMLEntity)4 IAS4IncomingMessageMetadata (com.helger.phase4.messaging.IAS4IncomingMessageMetadata)4 AS4UserMessage (com.helger.phase4.messaging.domain.AS4UserMessage)4 Phase4Exception (com.helger.phase4.util.Phase4Exception)4 MessagingException (javax.mail.MessagingException)4 Wrapper (com.helger.commons.wrapper.Wrapper)3