33try :
44 from jose .backends .rsa_backend import RSAKey as PurePythonRSAKey
55 from jose .backends .cryptography_backend import CryptographyRSAKey
6- from jose .backends .pycrypto_backend import RSAKey
6+ from jose .backends .pycrypto_backend import RSAKey as PyCryptoRSAKey
77except ImportError :
8- PurePythonRSAKey = CryptographyRSAKey = RSAKey = None
8+ PurePythonRSAKey = CryptographyRSAKey = PyCryptoRSAKey = None
99from jose .constants import ALGORITHMS
1010
1111from .test_RSA import private_key
1212
13+ CRYPTO_BACKENDS = (
14+ pytest .param (PurePythonRSAKey , id = "python_rsa" ),
15+ pytest .param (CryptographyRSAKey , id = "pyca/cryptography" ),
16+ pytest .param (PyCryptoRSAKey , id = "pycrypto/dome" )
17+ )
18+ ENCODINGS = ("PKCS1" , "PKCS8" )
19+
1320
1421@pytest .mark .backend_compatibility
1522@pytest .mark .skipif (
16- None in (PurePythonRSAKey , CryptographyRSAKey , RSAKey ),
23+ None in (PurePythonRSAKey , CryptographyRSAKey , PyCryptoRSAKey ),
1724 reason = "Multiple crypto backends not available for backend compatibility tests"
1825)
1926class TestBackendRsaCompatibility (object ):
2027
21- @pytest .mark .parametrize ("BackendSign" , [ RSAKey , CryptographyRSAKey , PurePythonRSAKey ] )
22- @pytest .mark .parametrize ("BackendVerify" , [ RSAKey , CryptographyRSAKey , PurePythonRSAKey ] )
28+ @pytest .mark .parametrize ("BackendSign" , CRYPTO_BACKENDS )
29+ @pytest .mark .parametrize ("BackendVerify" , CRYPTO_BACKENDS )
2330 def test_signing_parity (self , BackendSign , BackendVerify ):
2431 key_sign = BackendSign (private_key , ALGORITHMS .RS256 )
2532 key_verify = BackendVerify (private_key , ALGORITHMS .RS256 ).public_key ()
@@ -33,18 +40,58 @@ def test_signing_parity(self, BackendSign, BackendVerify):
3340 # invalid signature
3441 assert not key_verify .verify (msg , b'n' * 64 )
3542
36- @pytest .mark .parametrize ("BackendFrom" , [RSAKey , CryptographyRSAKey , PurePythonRSAKey ])
37- @pytest .mark .parametrize ("BackendTo" , [RSAKey , CryptographyRSAKey , PurePythonRSAKey ])
38- def test_public_key_to_pem (self , BackendFrom , BackendTo ):
43+ @pytest .mark .parametrize ("encoding" , ENCODINGS )
44+ @pytest .mark .parametrize ("BackendFrom" , CRYPTO_BACKENDS )
45+ @pytest .mark .parametrize ("BackendTo" , CRYPTO_BACKENDS )
46+ def test_public_key_to_pem (self , BackendFrom , BackendTo , encoding ):
47+ key = BackendFrom (private_key , ALGORITHMS .RS256 )
48+ key2 = BackendTo (private_key , ALGORITHMS .RS256 )
49+
50+ key1_pem = key .public_key ().to_pem (pem_format = encoding ).strip ()
51+ key2_pem = key2 .public_key ().to_pem (pem_format = encoding ).strip ()
52+ assert key1_pem == key2_pem
53+
54+ @pytest .mark .parametrize ("encoding" , ENCODINGS )
55+ @pytest .mark .parametrize ("BackendFrom" , CRYPTO_BACKENDS )
56+ @pytest .mark .parametrize ("BackendTo" , CRYPTO_BACKENDS )
57+ def test_private_key_to_pem (self , BackendFrom , BackendTo , encoding ):
58+ key = BackendFrom (private_key , ALGORITHMS .RS256 )
59+ key2 = BackendTo (private_key , ALGORITHMS .RS256 )
60+
61+ key1_pem = key .to_pem (pem_format = encoding ).strip ()
62+ key2_pem = key2 .to_pem (pem_format = encoding ).strip ()
63+
64+ import base64
65+ a = base64 .b64decode (key1_pem [key1_pem .index (b"\n " ):key1_pem .rindex (b"\n " )])
66+ b = base64 .b64decode (key2_pem [key2_pem .index (b"\n " ):key2_pem .rindex (b"\n " )])
67+ assert a == b
68+
69+ assert key1_pem == key2_pem
70+
71+ @pytest .mark .parametrize ("encoding_save" , ENCODINGS )
72+ @pytest .mark .parametrize ("encoding_load" , ENCODINGS )
73+ @pytest .mark .parametrize ("BackendFrom" , CRYPTO_BACKENDS )
74+ @pytest .mark .parametrize ("BackendTo" , CRYPTO_BACKENDS )
75+ def test_public_key_load_cycle (self , BackendFrom , BackendTo , encoding_save , encoding_load ):
76+ key = BackendFrom (private_key , ALGORITHMS .RS256 )
77+
78+ pem_pub_reference = key .public_key ().to_pem (pem_format = encoding_save ).strip ()
79+ pem_pub_load = key .public_key ().to_pem (pem_format = encoding_load ).strip ()
80+
81+ pubkey_2 = BackendTo (pem_pub_load , ALGORITHMS .RS256 )
82+
83+ assert pem_pub_reference == pubkey_2 .to_pem (encoding_save ).strip ()
84+
85+ @pytest .mark .parametrize ("encoding_save" , ENCODINGS )
86+ @pytest .mark .parametrize ("encoding_load" , ENCODINGS )
87+ @pytest .mark .parametrize ("BackendFrom" , CRYPTO_BACKENDS )
88+ @pytest .mark .parametrize ("BackendTo" , CRYPTO_BACKENDS )
89+ def test_private_key_load_cycle (self , BackendFrom , BackendTo , encoding_save , encoding_load ):
3990 key = BackendFrom (private_key , ALGORITHMS .RS256 )
40- pubkey = key .public_key ()
4191
42- pkcs1_pub = pubkey .to_pem (pem_format = 'PKCS1' ).strip ()
43- pkcs8_pub = pubkey .to_pem (pem_format = 'PKCS8' ).strip ()
44- assert pkcs1_pub != pkcs8_pub , BackendFrom
92+ pem_reference = key .to_pem (pem_format = encoding_save ).strip ()
93+ pem_load = key .to_pem (pem_format = encoding_load ).strip ()
4594
46- pub1 = BackendTo (pkcs1_pub , ALGORITHMS .RS256 )
47- pub8 = BackendTo (pkcs8_pub , ALGORITHMS .RS256 )
95+ key_2 = BackendTo (pem_load , ALGORITHMS .RS256 )
4896
49- assert pkcs8_pub == pub1 .to_pem (pem_format = 'PKCS8' ).strip ()
50- assert pkcs1_pub == pub8 .to_pem (pem_format = 'PKCS1' ).strip ()
97+ assert pem_reference == key_2 .to_pem (encoding_save ).strip ()
0 commit comments