Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RSA_R_OAEP_DECODING_ERROR error flag. #2323

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/_cffi_src/openssl/err.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
static const int Cryptography_HAS_098C_CAMELLIA_CODES;
static const int Cryptography_HAS_EC_CODES;
static const int Cryptography_HAS_RSA_R_PKCS_DECODING_ERROR;
static const int Cryptography_HAS_RSA_R_OAEP_DECODING_ERROR;

struct ERR_string_data_st {
unsigned long error;
Expand Down Expand Up @@ -230,6 +231,7 @@
static const int RSA_R_BLOCK_TYPE_IS_NOT_01;
static const int RSA_R_BLOCK_TYPE_IS_NOT_02;
static const int RSA_R_PKCS_DECODING_ERROR;
static const int RSA_R_OAEP_DECODING_ERROR;
static const int RSA_F_RSA_SIGN;
"""

Expand Down Expand Up @@ -334,4 +336,11 @@
static const long Cryptography_HAS_RSA_R_PKCS_DECODING_ERROR = 0;
static const long RSA_R_PKCS_DECODING_ERROR = 0;
#endif

#ifdef RSA_R_OAEP_DECODING_ERROR
static const long Cryptography_HAS_RSA_R_OAEP_DECODING_ERROR = 1;
#else
static const long Cryptography_HAS_RSA_R_OAEP_DECODING_ERROR = 0;
static const long RSA_R_OAEP_DECODING_ERROR = 0;
#endif
"""
3 changes: 3 additions & 0 deletions src/cryptography/hazmat/backends/openssl/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ def _handle_rsa_enc_dec_error(backend, key):
if backend._lib.Cryptography_HAS_RSA_R_PKCS_DECODING_ERROR:
decoding_errors.append(backend._lib.RSA_R_PKCS_DECODING_ERROR)

if backend._lib.Cryptography_HAS_RSA_R_OAEP_DECODING_ERROR:
decoding_errors.append(backend._lib.RSA_R_OAEP_DECODING_ERROR)

assert errors[0].reason in decoding_errors
raise ValueError("Decryption failed.")

Expand Down
3 changes: 3 additions & 0 deletions src/cryptography/hazmat/bindings/openssl/_conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@
"Cryptography_HAS_RSA_R_PKCS_DECODING_ERROR": [
"RSA_R_PKCS_DECODING_ERROR"
],
"Cryptography_HAS_RSA_R_OAEP_DECODING_ERROR": [
"RSA_R_OAEP_DECODING_ERROR"
],
"Cryptography_HAS_GCM": [
"EVP_CTRL_GCM_GET_TAG",
"EVP_CTRL_GCM_SET_TAG",
Expand Down
26 changes: 25 additions & 1 deletion tests/hazmat/backends/test_openssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from cryptography.hazmat.primitives.ciphers.modes import CBC, CTR, Mode

from ..primitives.fixtures_dsa import DSA_KEY_2048
from ..primitives.fixtures_rsa import RSA_KEY_2048, RSA_KEY_512
from ..primitives.fixtures_rsa import RSA_KEY_2048, RSA_KEY_512, RSA_KEY_512_ALT
Copy link
Member

Choose a reason for hiding this comment

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

This line is too long, you'll need to format it:

from ..primitives.fixtures_rsa import (
    RSA_KEY_2048, RSA_KEY_512, RSA_KEY_512_ALT
)

from ..primitives.test_ec import _skip_curve_unsupported
from ...utils import load_vectors_from_file, raises_unsupported_algorithm

Expand Down Expand Up @@ -467,6 +467,30 @@ def test_unsupported_oaep_label_decrypt(self):
)
)

def test_supported_oaep_decrypt(self):
private_key = RSA_KEY_512.private_key(backend)

ciphertext = private_key.public_key().encrypt(
b'secure data',
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)

private_key_alt = RSA_KEY_512_ALT.private_key(backend)

with pytest.raises(ValueError):
private_key_alt.decrypt(
ciphertext,
Copy link
Member

Choose a reason for hiding this comment

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

Can you please move the RSA_KEY_512_ALT.private_key(backend) call outside of the with pytest.raises(ValueError) block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)


@pytest.mark.skipif(
backend._lib.OPENSSL_VERSION_NUMBER <= 0x10001000,
Expand Down