From acda0445b1d22da120bbd46283e374887758c8b2 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Fri, 12 Sep 2014 23:21:32 +0100 Subject: Added PKCS8 encoded private keys to tests Generated two files with the same private key as PEM_Serialization's ec_private_key.pem, one unencrypted and one encrypted with "123456". Also changed existing PEMSerialization unit tests to take parameters so that tests can be extended easily. --- tests/hazmat/primitives/test_serialization.py | 48 +++++++++++++++++---------- 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'tests/hazmat') diff --git a/tests/hazmat/primitives/test_serialization.py b/tests/hazmat/primitives/test_serialization.py index 8542408b..7c912a92 100644 --- a/tests/hazmat/primitives/test_serialization.py +++ b/tests/hazmat/primitives/test_serialization.py @@ -49,29 +49,21 @@ class TestPEMSerialization(object): if isinstance(key, interfaces.RSAPrivateKeyWithNumbers): _check_rsa_private_numbers(key.private_numbers()) + @pytest.mark.parametrize( + ("key_file", "password"), + [ + ("ec_private_key.pem", None), + ("ec_private_key_encrypted.pem", b"123456"), + ] + ) @pytest.mark.elliptic - def test_load_pem_ec_private_key_unencrypted(self, backend): - _skip_curve_unsupported(backend, ec.SECP256R1()) - key = load_vectors_from_file( - os.path.join( - "asymmetric", "PEM_Serialization", "ec_private_key.pem"), - lambda pemfile: load_pem_private_key( - pemfile.read().encode(), None, backend - ) - ) - - assert key - assert isinstance(key, interfaces.EllipticCurvePrivateKey) - - @pytest.mark.elliptic - def test_load_pem_ec_private_key_encrypted(self, backend): + def test_load_pem_ec_private_key(self, key_file, password, backend): _skip_curve_unsupported(backend, ec.SECP256R1()) key = load_vectors_from_file( os.path.join( - "asymmetric", "PEM_Serialization", - "ec_private_key_encrypted.pem"), + "asymmetric", "PEM_Serialization", key_file), lambda pemfile: load_pem_private_key( - pemfile.read().encode(), b"123456", backend + pemfile.read().encode(), password, backend ) ) @@ -335,6 +327,26 @@ class TestPKCS8Serialization(object): if isinstance(key, interfaces.RSAPrivateKeyWithNumbers): _check_rsa_private_numbers(key.private_numbers()) + @pytest.mark.parametrize( + ("key_file", "password"), + [ + ("ec_private_key.pem", None), + ("ec_private_key_encrypted.pem", b"123456"), + ] + ) + @pytest.mark.elliptic + def test_load_pem_ec_private_key(self, key_file, password, backend): + _skip_curve_unsupported(backend, ec.SECP256R1()) + key = load_vectors_from_file( + os.path.join( + "asymmetric", "PKCS8", key_file), + lambda pemfile: load_pem_pkcs8_private_key( + pemfile.read().encode(), password, backend + ) + ) + assert key + assert isinstance(key, interfaces.EllipticCurvePrivateKey) + def test_unused_password(self, backend): key_file = os.path.join( "asymmetric", "PKCS8", "unencpkcs8.pem") -- cgit v1.2.3