From 267f5ce0a5a7dfaa9553b9fab6cce2a70a8284bf Mon Sep 17 00:00:00 2001 From: mattsb42-aws Date: Thu, 27 Dec 2018 16:10:45 -0800 Subject: [PATCH 1/2] make cryptography_backend the default for RSAKey --- jose/backends/__init__.py | 4 ++-- tests/test_backends.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/test_backends.py diff --git a/jose/backends/__init__.py b/jose/backends/__init__.py index 732bd5b2..d1b9fa1a 100644 --- a/jose/backends/__init__.py +++ b/jose/backends/__init__.py @@ -1,9 +1,9 @@ try: - from jose.backends.pycrypto_backend import RSAKey # noqa: F401 + from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey # noqa: F401 except ImportError: try: - from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey # noqa: F401 + from jose.backends.pycrypto_backend import RSAKey # noqa: F401 except ImportError: from jose.backends.rsa_backend import RSAKey # noqa: F401 diff --git a/tests/test_backends.py b/tests/test_backends.py new file mode 100644 index 00000000..6e633a19 --- /dev/null +++ b/tests/test_backends.py @@ -0,0 +1,35 @@ +"""Test the default import handling.""" +try: + from jose.backends.rsa_backend import RSAKey as PurePythonRSAKey +except ImportError: + PurePythonRSAKey = None +try: + from jose.backends.cryptography_backend import CryptographyRSAKey, CryptographyECKey +except ImportError: + CryptographyRSAKey = CryptographyECKey = None +try: + from jose.backends.pycrypto_backend import RSAKey as PyCryptoRSAKey +except ImportError: + PyCryptoRSAKey = None +try: + from jose.backends.ecdsa_backend import ECDSAECKey as PurePythonECDSAKey +except ImportError: + PurePythonRSAKey = None + +from jose.backends import ECKey, RSAKey + + +def test_default_ec_backend(): + if CryptographyECKey is not None: + assert ECKey is CryptographyECKey + else: + assert ECKey is PurePythonECDSAKey + + +def test_default_rsa_backend(): + if CryptographyRSAKey is not None: + assert RSAKey is CryptographyRSAKey + elif PyCryptoRSAKey is not None: + assert RSAKey is PyCryptoRSAKey + else: + assert RSAKey is PurePythonRSAKey From f96daecd92a1130d48a4a56d95fafbec7a880c61 Mon Sep 17 00:00:00 2001 From: mattsb42-aws Date: Thu, 27 Dec 2018 16:19:44 -0800 Subject: [PATCH 2/2] isolate pycrypto/dome-explict test and explicitly use pycrypto/dome backend --- tests/algorithms/test_RSA.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/algorithms/test_RSA.py b/tests/algorithms/test_RSA.py index 944b9945..20a59422 100644 --- a/tests/algorithms/test_RSA.py +++ b/tests/algorithms/test_RSA.py @@ -345,8 +345,9 @@ def test_pycrypto_RSA_key_instance(): @pytest.mark.pycrypto @pytest.mark.pycryptodome @pytest.mark.parametrize("private_key", PRIVATE_KEYS) +@pytest.mark.skipif(None in (PyCryptoRSA, PyCryptoRSAKey), reason="Pycrypto/dome backend not available") def test_pycrypto_unencoded_cleartext(private_key): - key = RSAKey(private_key, ALGORITHMS.RS256) + key = PyCryptoRSAKey(private_key, ALGORITHMS.RS256) msg = b'test' signature = key.sign(msg) public_key = key.public_key()