Skip to content

Commit eaa6383

Browse files
committed
expand RSA and ECDSA compatibility tests to validate serialization compatibility between backends
1 parent b4d6ca2 commit eaa6383

File tree

2 files changed

+103
-17
lines changed

2 files changed

+103
-17
lines changed

tests/algorithms/test_EC_compat.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
None in (ECDSAECKey, CryptographyECKey),
1616
reason="Multiple crypto backends not available for backend compatibility tests"
1717
)
18-
class TestBackendRsaCompatibility(object):
18+
class TestBackendEcdsaCompatibility(object):
1919

2020
@pytest.mark.parametrize("BackendSign", [ECDSAECKey, CryptographyECKey])
2121
@pytest.mark.parametrize("BackendVerify", [ECDSAECKey, CryptographyECKey])
@@ -31,3 +31,42 @@ def test_signing_parity(self, BackendSign, BackendVerify):
3131

3232
# invalid signature
3333
assert not key_verify.verify(msg, b'n' * 64)
34+
35+
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
36+
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
37+
def test_public_key_to_pem(self, BackendFrom, BackendTo):
38+
key = BackendFrom(private_key, ALGORITHMS.ES256)
39+
key2 = BackendTo(private_key, ALGORITHMS.ES256)
40+
41+
assert key.public_key().to_pem().strip() == key2.public_key().to_pem().strip()
42+
43+
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
44+
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
45+
def test_private_key_to_pem(self, BackendFrom, BackendTo):
46+
key = BackendFrom(private_key, ALGORITHMS.ES256)
47+
key2 = BackendTo(private_key, ALGORITHMS.ES256)
48+
49+
assert key.to_pem().strip() == key2.to_pem().strip()
50+
51+
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
52+
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
53+
def test_public_key_load_cycle(self, BackendFrom, BackendTo):
54+
key = BackendFrom(private_key, ALGORITHMS.ES256)
55+
pubkey = key.public_key()
56+
57+
pub_pem_source = pubkey.to_pem().strip()
58+
59+
pub_target = BackendTo(pub_pem_source, ALGORITHMS.ES256)
60+
61+
assert pub_pem_source == pub_target.to_pem().strip()
62+
63+
@pytest.mark.parametrize("BackendFrom", [ECDSAECKey, CryptographyECKey])
64+
@pytest.mark.parametrize("BackendTo", [ECDSAECKey, CryptographyECKey])
65+
def test_private_key_load_cycle(self, BackendFrom, BackendTo):
66+
key = BackendFrom(private_key, ALGORITHMS.ES256)
67+
68+
pem_source = key.to_pem().strip()
69+
70+
target = BackendTo(pem_source, ALGORITHMS.ES256)
71+
72+
assert pem_source == target.to_pem().strip()

tests/algorithms/test_RSA_compat.py

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@
33
try:
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
77
except ImportError:
8-
PurePythonRSAKey = CryptographyRSAKey = RSAKey = None
8+
PurePythonRSAKey = CryptographyRSAKey = PyCryptoRSAKey = None
99
from jose.constants import ALGORITHMS
1010

1111
from .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
)
1926
class 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

Comments
 (0)