Harshil Kapasi
Harshil Kapasi

Reputation: 11

Unable to send message specific signed MDN response in Apache Camel AS2 library

I am using the Apache Camel AS2 library in my Java project where I have implemented an AS2 server. Currently, I'm generating a signed MDN response by configuring the certificate and algorithm in the server endpoint configuration, and it is working fine. However, I want to sign the MDN based on the AS2 request, where in headers, I will get the Mic algorithm that the user has requested. So, I want to sign using one of those algorithm at runtime, instead of hard-coding the algorithm at the endpoint creation.

I have also gone through the library code, and checked online, but I was not able to find any solution.

I have seen online server https://mendelson-e-c.com/as2_testserver which does the same, and signs according to the AS2 message, please help me in implementing this.

My server code is something like below, with signing defined in the endpoint configuration.

@Singleton
public class As2Server extends RouteBuilder {
    private final CamelContext camelContext;

    @Inject
    public As2Server(CamelContext camelContext) {
        this.camelContext = camelContext;
    }

    @Override
    public void configure() {
        try {
            from(configureServerEndpoint()).process(exchange -> {

            // processing logic

            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Endpoint configureServerEndpoint() throws Exception {
        AS2Configuration endpointConfiguration = new AS2Configuration();
        endpointConfiguration.setApiName(AS2ApiName.SERVER);
        endpointConfiguration.setMethodName(AS2ServerManagerApiMethod.LISTEN.name());
        endpointConfiguration.setDecryptingPrivateKey(getPrivateKey());
        endpointConfiguration.setSigningPrivateKey(getPrivateKey());
        endpointConfiguration.setSigningCertificateChain(getCertificateChain());
        endpointConfiguration.setSigningAlgorithm(AS2SignatureAlgorithm.MD5WITHRSA);
        endpointConfiguration.setMdnMessageTemplate("Your AS2 message has been received");

        try (AS2Component as2Component = new AS2Component()) {
            as2Component.setCamelContext(camelContext);
            as2Component.setConfiguration(endpointConfiguration);
            return as2Component.createEndpoint(STR."as2://server/listen?serverPortNumber=11080&requestUriPattern=*");
        }
    }

}

Upvotes: 1

Views: 134

Answers (2)

anniyan vr
anniyan vr

Reputation: 1

Try to set the MIC with:

endpointConfiguration.setSignedReceiptMicAlgorithms(new String[] {"sha1"});

Upvotes: 0

ameleito
ameleito

Reputation: 11

In this moment we have a issue with the mic cause we are using the AS2SignatureAlgorithm.SHA256WITHRSA and its possible that when you try to send the algorithm directly from the message you start to get issues. if you know that MD5 works why is required to change it?

By the way like i told you we are testing with SHA256WITHRSA and apparently doesnt works. i just make some changes to use SHA2WITHRSA cause in some article i read that the support algorithm for signature are SHA-1 and MD5, did you test any other the error that we are receiving is the next one.

org.apache.http.HttpException: Failed to encode MIC
at org.apache.camel.component.as2.api.util.MicUtils.createReceivedContentMic(MicUtils.java:111)
at org.apache.camel.component.as2.api.entity.AS2MessageDispositionNotificationEntity.<init>(AS2MessageDispositionNotificationEntity.java:95)
at org.apache.camel.component.as2.api.entity.DispositionNotificationMultipartReportEntity.<init>(DispositionNotificationMultipartReportEntity.java:66)
at org.apache.camel.component.as2.api.protocol.ResponseMDN.process(ResponseMDN.java:157)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:142)
at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:360)
at org.apache.camel.component.as2.api.AS2ServerConnection$RequestHandlerThread.run(AS2ServerConnection.java:163)
Caused by: java.lang.IllegalArgumentException: Data must be specified
at org.apache.camel.util.ObjectHelper.notNull(ObjectHelper.java:153)
at org.apache.camel.component.as2.api.util.EntityUtils.encode(EntityUtils.java:89)
at org.apache.camel.component.as2.api.util.MicUtils$ReceivedContentMic.<init>(MicUtils.java:48)
at org.apache.camel.component.as2.api.util.MicUtils.createReceivedContentMic(MicUtils.java:109)
... 6 common frames omitted

Let me know if you test with other algorithms before to try to do the thing that you want to do.

Upvotes: 1

Related Questions