Skip to content

Decrypt messages whose content key is wrapped with RSAES-OAEP - #635

Merged
uhurusurfa merged 1 commit into
OpenAS2:masterfrom
adamlewison:fix/decrypt-rsaes-oaep
Oct 25, 2026
Merged

uhurusurfa merged 1 commit into
OpenAS2:masterfrom
adamlewison:fix/decrypt-rsaes-oaep

Conversation

@adamlewison

@adamlewison adamlewison commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

An inbound message whose content key is wrapped with RSAES-OAEP (1.2.840.113549.1.1.7) can't be decrypted. It fails with:

org.bouncycastle.cms.CMSException: exception unwrapping key: unable to recover secret key: block incorrect
Caused by: org.bouncycastle.crypto.InvalidCipherTextException: block incorrect

The partner then gets a processed/error:decryption-failed MDN. We hit this with a trading partner running Microsoft BizTalk, which wraps with OAEP. A packet capture confirmed the envelope was addressed to the correct certificate. Every message from that partner failed, while messages wrapped with PKCS#1 v1.5 decrypted fine.

Cause

BCCryptoHelper.decrypt unwraps with BouncyCastle's lightweight BcRSAKeyTransEnvelopedRecipient. Its unwrapper, BcRSAAsymmetricKeyUnwrapper.createAsymmetricUnwrapper, always returns new PKCS1Encoding(new RSABlindedEngine()) and ignores the key-encryption algorithm named in the recipient info. So an OAEP-wrapped key is decoded as PKCS#1 v1.5 and fails the padding check.

Fix

Use JceKeyTransEnvelopedRecipient((PrivateKey) receiverKey).setProvider("BC"). It reads the algorithm and its parameters from the envelope, so PKCS#1 v1.5 and OAEP both work. This matches the existing KeyAgree branch next to it, which already uses the JCE recipient. Sending is unchanged.

Tests

The new KeyTransportDecryptionTest encrypts a part to a generated key in three ways (PKCS#1 v1.5, OAEP with default SHA-1 parameters, OAEP with SHA-256) and asserts that BCCryptoHelper.decrypt recovers the payload.

  • On current master, both OAEP cases fail with block incorrect.
  • With this change, all three pass.
  • ./mvnw -pl Server -am test: 249 tests run, 0 failures (1 skipped, as before).

We are running this change on 4.8.3 on our own AS2 host, where it now decrypts every message from that BizTalk partner.

🤖 Generated with Claude Code

BCCryptoHelper.decrypt unwrapped the content key with BouncyCastle's
lightweight BcRSAKeyTransEnvelopedRecipient, whose BcRSAAsymmetricKeyUnwrapper
always uses PKCS#1 v1.5 and ignores the key-encryption algorithm named in the
envelope. A key wrapped with RSAES-OAEP (1.2.840.113549.1.1.7) therefore failed
with "unable to recover secret key: block incorrect", and the partner got a
decryption-failed MDN. Microsoft BizTalk wraps with OAEP, so no message from a
BizTalk partner could be received.

JceKeyTransEnvelopedRecipient reads the algorithm from the recipient info and
handles PKCS#1 v1.5 and OAEP alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@uhurusurfa uhurusurfa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Thanks for the fix.

@uhurusurfa
uhurusurfa merged commit 41a8383 into OpenAS2:master Oct 25, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants