Skip to content

Commit

Permalink
Correct OAEP snafu for pubkey encryption handler
Browse files Browse the repository at this point in the history
The prefer_oaep conditional was flipped and the tests did not detect
this. That situation has been rectified.
  • Loading branch information
MatthiasValvekens committed Mar 21, 2024
1 parent 9d1ef1a commit 4fce74d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyhanko/pdf_utils/crypt/pubkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ def _recipient_info(
{'issuer_and_serial_number': iss_serial_rid}
)
if policy.prefer_oaep:
return _rsaes_pkcs1v15_recipient(pub_key_info, rid, envelope_key)
else:
return _rsaes_oaep_recipient(pub_key_info, rid, envelope_key)
else:
return _rsaes_pkcs1v15_recipient(pub_key_info, rid, envelope_key)
elif algorithm_name in ('ec', 'x25519', 'x448'):
ka_rid = cms.KeyAgreementRecipientIdentifier(
{'issuer_and_serial_number': iss_serial_rid}
Expand Down
12 changes: 12 additions & 0 deletions pyhanko_tests/test_crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ def _validate_pubkey_decryption(r, result):
assert b'0 1 0 rg /a0 gs' in page['/Contents'].data


def _extract_ktri_kea(r: PdfFileReader):
recp_bytes = r.encrypt_dict['/CF']['/DefaultCryptFilter']['/Recipients'][0]
content_info = cms.ContentInfo.load(recp_bytes)
ktri = content_info['content']['recipient_infos'][0].chosen
return ktri['key_encryption_algorithm']['algorithm'].native


@pytest.mark.parametrize(
"version, keylen, use_aes, use_crypt_filters",
[
Expand All @@ -253,6 +260,9 @@ def test_pubkey_encryption(version, keylen, use_aes, use_crypt_filters):
assert r.input_version == (2, 0)
result = r.decrypt_pubkey(PUBKEY_TEST_DECRYPTER)
_validate_pubkey_decryption(r, result)
if version == SecurityHandlerVersion.AES256:
kea = _extract_ktri_kea(r)
assert kea == 'rsaes_pkcs1v15'


@pytest.mark.parametrize(
Expand Down Expand Up @@ -297,6 +307,8 @@ def test_oaep_encryption():
r = PdfFileReader(out)
result = r.decrypt_pubkey(PUBKEY_TEST_DECRYPTER)
_validate_pubkey_decryption(r, result)
kea = _extract_ktri_kea(r)
assert kea == 'rsaes_oaep'


def test_ecdh_decryption_smoke():
Expand Down

0 comments on commit 4fce74d

Please sign in to comment.