Parker Mauney created CAMEL-28095:
-------------------------------------
Summary: camel-as2 - the MDN MIC of a signed, then compressed,
then encrypted message is computed over a re-serialization of the signed part
Key: CAMEL-28095
URL: https://issues.apache.org/jira/browse/CAMEL-28095
Project: Camel
Issue Type: Bug
Components: camel-as2
Affects Versions: 4.24.0
Reporter: Parker Mauney
h3. Problem
CAMEL-26425 made the receiver compute the MDN's {{Received-content-MIC}} over
the signed part as received ({{MultipartSignedEntity#getSignedDataContent}}),
as RFC 4130 7.3.1 requires. One message structure doesn't get that: sign, then
compress, then encrypt. It is the "sign-before-compress" layout CAMEL-23065
describes:
{code}
Enveloped (encrypted)
└─ CompressedDataEntity
└─ (decompress) → MultipartSigned
└─ part 0 ← MIC must be computed over THIS, as
received
{code}
A sender whose signed part has a folded header gets a MIC that doesn't match
its own. A long {{Content-Disposition}} folded by JavaMail is one common case.
Base64 wrapped at a width other than 76 is another, and so are bare-LF line
endings. The sender then reports the MDN as an integrity failure and typically
resends.
The other structures are fine on 4.24.0: signed only, signed then encrypted,
and compressed then signed then encrypted all get the MIC over the part as
received.
h3. Analysis so far
{{MicUtils#findMultipartSignedEntity}} (camel-4.24.0, {{MicUtils.java}} lines
166-209) looks for the signed entity in three places:
* the message itself ({{multipart/signed}});
* inside a top-level {{compressed-data}} entity;
* the decrypted entity of an {{enveloped-data}} message, only when that is
{{multipart/signed}} itself.
When the decrypted entity is {{compressed-data}}, it returns {{null}}.
{{findMicContent}} then falls back to
{{EntityUtils.getContent(HttpMessageUtils.extractEdiPayload(...))}} (line 158),
which writes the parsed part out again. That rewrite unfolds folded header
lines and re-encodes the body with the part's transfer encoding. So the MIC
matches the sender's only when the signed part happens to come out
byte-identical, for example a single unfolded {{Content-Type}} header and a
binary body.
h3. Reproducer
Against an AS2 server consumer (camel-4.24.0) configured with a decrypting key,
sending with Bouncy Castle so camel-as2's own client doesn't normalise anything:
# Signed part, with a folded header:
{code}
Content-Type: application/edifact\r\n
Content-Disposition: attachment;\r\n
filename=orders.edi\r\n
\r\n
UNB+UNOA:1+005435656:1+006415160:1+060515:1434+00000000000778'
{code}
# Sign it detached (SHA256withRSA, {{CMSSignedDataGenerator}}) and wrap it as
{{multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-256}}
with the part bytes unchanged.
# Compress that {{multipart/signed}} entity ({{CMSCompressedDataGenerator}},
{{ZlibCompressor}}) into an {{application/pkcs7-mime;
smime-type=compressed-data}} entity.
# Encrypt the compressed entity for the server's certificate
({{CMSEnvelopedDataGenerator}}, AES128_CBC) and POST it as
{{application/pkcs7-mime; smime-type=enveloped-data}} with
{{Disposition-Notification-Options:
signed-receipt-protocol=optional,pkcs7-signature;
signed-receipt-micalg=optional,sha-256}}.
# Compare the MDN's {{Received-content-MIC}} with {{base64(SHA-256(signed part
bytes))}}.
Result: they differ. With step 3 left out (signed, then encrypted), or with the
header unfolded, they match.
h3. Expected
The MIC of a signed, then compressed, then encrypted message is computed over
the signed part as received, as it already is for the other structures.
h3. Related
* CAMEL-26425: MIC over the signed part as received.
* CAMEL-23065: MIC for compressed messages, which describes this structure.
Happy to send a PR if that helps.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)