This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a2eb7dd7055 CAMEL-28095: camel-as2 - compute the MIC of a signed, 
then compressed, then encrypted message over the signed part as received 
(#31330)
8a2eb7dd7055 is described below

commit 8a2eb7dd7055d00b63d6a6ff0edcb86c6c95e04e
Author: Parker Mauney <[email protected]>
AuthorDate: Thu Dec 3 16:35:30 2026 -0500

    CAMEL-28095: camel-as2 - compute the MIC of a signed, then compressed, then 
encrypted message over the signed part as received (#31330)
    
    MicUtils looked for the multipart/signed inside an enveloped-data message
    only when the decrypted entity was multipart/signed itself. When the signed
    message was compressed before it was encrypted, the decrypted entity is a
    compressed-data entity, so the MIC fell back to a re-serialisation of the
    parsed part, which differs from what the sender signed when the part has
    folded headers or its own base64 or quoted-printable line length.
    
    The enveloped-data branch now also decompresses a decrypted compressed-data
    entity, with the same expansion bound as the top-level compressed-data 
branch,
    and takes the multipart/signed found inside. The 4.25 upgrade guide notes 
the
    change.
    
    _Claude Code on behalf of @ParkerM_
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/as2/api/util/MicUtils.java     | 10 ++++++
 .../MultipartSignedEntityAsReceivedTest.java       | 41 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |  9 +++++
 3 files changed, 60 insertions(+)

diff --git 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/MicUtils.java
 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/MicUtils.java
index 003f175bac6b..e67bb9cc4a10 100644
--- 
a/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/MicUtils.java
+++ 
b/components/camel-as2/camel-as2-api/src/main/java/org/apache/camel/component/as2/api/util/MicUtils.java
@@ -201,6 +201,16 @@ public final class MicUtils {
                             return signedEntity;
                         }
                     }
+                    if (decryptedEntity instanceof 
ApplicationPkcs7MimeCompressedDataEntity compressedEntity) {
+                        // Sign-before-compress, then encrypted: decompress 
the decrypted entity, then find the signed
+                        // entity inside
+                        MimeEntity inner = compressedEntity
+                                .getCompressedEntity(
+                                        new 
ZlibExpanderProvider(HttpMessageUtils.MAX_COMPRESSED_ENTITY_EXPANSION));
+                        if (inner instanceof MultipartSignedEntity 
signedEntity) {
+                            return signedEntity;
+                        }
+                    }
                 }
             }
         }
diff --git 
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/MultipartSignedEntityAsReceivedTest.java
 
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/MultipartSignedEntityAsReceivedTest.java
index b3aad0acfb02..cf87470d6462 100644
--- 
a/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/MultipartSignedEntityAsReceivedTest.java
+++ 
b/components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/entity/MultipartSignedEntityAsReceivedTest.java
@@ -40,10 +40,12 @@ import org.apache.hc.core5.http.ContentType;
 import org.apache.hc.core5.http.io.entity.BasicHttpEntity;
 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.bouncycastle.cms.CMSAlgorithm;
+import org.bouncycastle.cms.CMSCompressedDataGenerator;
 import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
 import org.bouncycastle.cms.CMSProcessableByteArray;
 import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder;
 import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator;
+import org.bouncycastle.cms.jcajce.ZlibCompressor;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
@@ -339,6 +341,45 @@ class MultipartSignedEntityAsReceivedTest {
                 "a signature over the part as transmitted must verify after 
decryption too");
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "base64", "quoted-printable", "binary" })
+    void 
micOfASignedThenCompressedThenEncryptedMessageIsComputedOverThePartAsReceived(String
 transferEncoding)
+            throws Exception {
+        byte[] signedPart = signedPart(transferEncoding);
+        byte[] signature = sign(signedPart);
+
+        // signed, then compressed, then encrypted: the compressed-data entity 
holding the multipart/signed is the
+        // enveloped content
+        byte[] compressed = new CMSCompressedDataGenerator()
+                .generate(new 
CMSProcessableByteArray(multipartSignedEntity(signedPart, signature)), new 
ZlibCompressor())
+                .getEncoded();
+        byte[] compressedEntity = ("Content-Type: application/pkcs7-mime; 
smime-type=compressed-data; name=smime.p7z\r\n"
+                                   + "Content-Transfer-Encoding: base64\r\n"
+                                   + "\r\n"
+                                   + 
wrap(Base64.getEncoder().encodeToString(compressed), 76))
+                .getBytes(StandardCharsets.US_ASCII);
+        CMSEnvelopedDataGenerator envelopedDataGenerator = new 
CMSEnvelopedDataGenerator();
+        envelopedDataGenerator.addRecipientInfoGenerator(new 
JceKeyTransRecipientInfoGenerator(signingCert));
+        byte[] encrypted = envelopedDataGenerator.generate(
+                new CMSProcessableByteArray(compressedEntity),
+                new 
JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC).build()).getEncoded();
+
+        String envelopedContentType = "application/pkcs7-mime; 
smime-type=enveloped-data; name=smime.p7m";
+        BasicClassicHttpRequest request = new BasicClassicHttpRequest("POST", 
"/");
+        request.addHeader(AS2Header.CONTENT_TYPE, envelopedContentType);
+        request.addHeader(AS2Header.DISPOSITION_NOTIFICATION_OPTIONS, 
DISPOSITION_NOTIFICATION_OPTIONS);
+        request.setEntity(new BasicHttpEntity(
+                new ByteArrayInputStream(encrypted), 
ContentType.parse(envelopedContentType)));
+
+        MicUtils.ReceivedContentMic mic = MicUtils.createReceivedContentMic(
+                request, new Certificate[] { signingCert }, 
signingKP.getPrivate());
+
+        assertNotNull(mic, "a MIC must be created");
+        byte[] digest = 
MessageDigest.getInstance("SHA-256").digest(signedPart);
+        assertEquals(Base64.getEncoder().encodeToString(digest), 
mic.getEncodedMessageDigest(),
+                "the MIC must cover the signed part as transmitted, also when 
the signed message was compressed and encrypted");
+    }
+
     @Test
     void entityBuiltLocallyIsVerifiedOverItsSerialisation() throws Exception {
         // no "as received" bytes exist for an entity camel-as2 built itself: 
its serialisation is what is signed
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index a3638dcc0c47..855a4459420e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -652,3 +652,12 @@ The polling consumer (`pollEnrich`, or `receive` of a 
`ConsumerTemplate`) now cl
 the polled exchange is done, instead of leaving it open. With `pollEnrich` 
this happens when the enriched exchange
 completes. With a `ConsumerTemplate`, call `doneUoW(exchange)` when you are 
done with the received exchange, as for any
 other polled endpoint.
+
+=== camel-as2 - server/listen - MIC of a message signed, then compressed, then 
encrypted
+
+The `Received-Content-MIC` of the MDN for a message that was signed, then 
compressed, then encrypted is now computed
+over the signed part exactly as it was received, as it already was for a 
message that is only signed, signed and
+encrypted, or compressed, then signed, then encrypted. Previously the 
`multipart/signed` inside the compressed entity
+of an encrypted message was not found, and the MIC was computed over a 
re-serialisation of the parsed part, which the
+sender could not match when the part has folded headers or a `base64` or 
`quoted-printable` body encoded with a
+different line length or escaping.

Reply via email to