From d6d2b30cddb6ea628f4d4dbe9b521af52947dc0c Mon Sep 17 00:00:00 2001 From: mattsb42-aws Date: Mon, 24 Dec 2018 18:43:50 -0800 Subject: [PATCH 1/2] import rsa backends with backend-explicit naming to avoid confusion --- tests/algorithms/test_RSA.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/algorithms/test_RSA.py b/tests/algorithms/test_RSA.py index 894e3aac..4af2e61e 100644 --- a/tests/algorithms/test_RSA.py +++ b/tests/algorithms/test_RSA.py @@ -1,15 +1,15 @@ import sys try: - from Crypto.PublicKey import RSA + from Crypto.PublicKey import RSA as PyCryptoRSA except ImportError: - RSA = None + PyCryptoRSA = None try: from cryptography.hazmat.backends import default_backend - from cryptography.hazmat.primitives.asymmetric import rsa + from cryptography.hazmat.primitives.asymmetric import rsa as pyca_rsa except ImportError: - default_backend = rsa = None + default_backend = pyca_rsa = None from jose.backends import RSAKey from jose.constants import ALGORITHMS @@ -77,9 +77,9 @@ @pytest.mark.pycrypto @pytest.mark.pycryptodome -@pytest.mark.skipif(RSA is None, reason="Pycrypto/dome backend not available") +@pytest.mark.skipif(PyCryptoRSA is None, reason="Pycrypto/dome backend not available") def test_pycrypto_RSA_key_instance(): - key = RSA.construct((long( + key = PyCryptoRSA.construct((long( 26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819), long(65537))) RSAKey(key, ALGORITHMS.RS256) @@ -99,12 +99,12 @@ def test_pycrypto_unencoded_cleartext(): @pytest.mark.cryptography @pytest.mark.skipif( - None in (default_backend, rsa), + None in (default_backend, pyca_rsa), reason="Cryptography backend not available" ) def test_cryptography_RSA_key_instance(): - key = rsa.RSAPublicNumbers( + key = pyca_rsa.RSAPublicNumbers( long(65537), long(26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819), ).public_key(default_backend()) From 5ee69d8776aeb062f38df4f50310d61d107d2efb Mon Sep 17 00:00:00 2001 From: mattsb42-aws Date: Mon, 24 Dec 2018 18:57:06 -0800 Subject: [PATCH 2/2] make backend-explicit tests use backend-explicit keys test_pycrypto_RSA_key_instance and test_cryptography_RSA_key_instance both test compatibility of the backend-explicit RSAKey classes with the backend-native RSA key structures. As such, these tests must use the backend-explicit RSAKey classes rather than the default loaded from jose.backends to avoid breaking when multiple backends are present. --- tests/algorithms/test_RSA.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/algorithms/test_RSA.py b/tests/algorithms/test_RSA.py index 4af2e61e..ad1cef68 100644 --- a/tests/algorithms/test_RSA.py +++ b/tests/algorithms/test_RSA.py @@ -2,14 +2,16 @@ try: from Crypto.PublicKey import RSA as PyCryptoRSA + from jose.backends.pycrypto_backend import RSAKey as PyCryptoRSAKey except ImportError: - PyCryptoRSA = None + PyCryptoRSA = PyCryptoRSAKey = None try: from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa as pyca_rsa + from jose.backends.cryptography_backend import CryptographyRSAKey except ImportError: - default_backend = pyca_rsa = None + default_backend = pyca_rsa = CryptographyRSAKey = None from jose.backends import RSAKey from jose.constants import ALGORITHMS @@ -77,12 +79,12 @@ @pytest.mark.pycrypto @pytest.mark.pycryptodome -@pytest.mark.skipif(PyCryptoRSA is None, reason="Pycrypto/dome backend not available") +@pytest.mark.skipif(None in (PyCryptoRSA, PyCryptoRSAKey), reason="Pycrypto/dome backend not available") def test_pycrypto_RSA_key_instance(): key = PyCryptoRSA.construct((long( 26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819), long(65537))) - RSAKey(key, ALGORITHMS.RS256) + PyCryptoRSAKey(key, ALGORITHMS.RS256) # TODO: Unclear why this test was marked as only for pycrypto @pytest.mark.pycrypto @@ -99,7 +101,7 @@ def test_pycrypto_unencoded_cleartext(): @pytest.mark.cryptography @pytest.mark.skipif( - None in (default_backend, pyca_rsa), + None in (default_backend, pyca_rsa, CryptographyRSAKey), reason="Cryptography backend not available" ) def test_cryptography_RSA_key_instance(): @@ -109,7 +111,7 @@ def test_cryptography_RSA_key_instance(): long(26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819), ).public_key(default_backend()) - pubkey = RSAKey(key, ALGORITHMS.RS256) + pubkey = CryptographyRSAKey(key, ALGORITHMS.RS256) assert pubkey.is_public() pem = pubkey.to_pem()