Don't leak how PKCS#7 encryptedKey decryption failed - #15369
Merged
Conversation
pkcs7_decrypt_der and its PEM and S/MIME variants reported invalid RSA PKCS#1 v1.5 padding, a recovered key of the wrong length, and a wrong key as three distinct errors, and the middle one disclosed the exact recovered length. That is a Bleichenbacher oracle for any caller that decrypts untrusted messages. The wrong-length case also returned before the AES-CBC pass, so the same distinction was observable by timing, proportional to a content size the attacker chooses. Substitute a random key of the expected length on failure, per RFC 3218, and continue down an identical path so that every outcome looks alike. EnvelopedData still authenticates nothing, so revealing whether decryption succeeded remains a CBC padding oracle regardless. That is a property of PKCS#7 rather than of this implementation, and is now documented instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Only a ValueError from unwrapping the key is attacker controlled and so deliberately silenced. A private key implemented outside of this library can fail in other ways, and those still reach the caller -- but nothing exercised that arm, leaving it uncovered. Add a DummyRSAPrivateKey double whose decrypt() raises a TypeError, and tests for each of its methods. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
reaperhulk
reviewed
Jul 31, 2026
| .encrypt(serialization.Encoding.DER, []) | ||
| ) | ||
|
|
||
| with pytest.raises(TypeError, match="the smartcard is unplugged"): |
Member
There was a problem hiding this comment.
Let's use a message that makes it clear this is a test rather than a plausible error message.
reaperhulk
approved these changes
Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pkcs7_decrypt_der and its PEM and S/MIME variants reported invalid RSA PKCS#1 v1.5 padding, a recovered key of the wrong length, and a wrong key as three distinct errors, and the middle one disclosed the exact recovered length. That is a Bleichenbacher oracle for any caller that decrypts untrusted messages. The wrong-length case also returned before the AES-CBC pass, so the same distinction was observable by timing, proportional to a content size the attacker chooses.
Substitute a random key of the expected length on failure, per RFC 3218, and continue down an identical path so that every outcome looks alike.
EnvelopedData still authenticates nothing, so revealing whether decryption succeeded remains a CBC padding oracle regardless. That is a property of PKCS#7 rather than of this implementation, and is now documented instead.