From f83e25c81bb186ed8a96d4a569d5068546a24349 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 21 Feb 2015 18:34:00 -0600 Subject: Support for traditional OpenSSL and PKCS8 RSA private key serialization --- tests/hazmat/backends/test_openssl.py | 29 +++++++- tests/hazmat/primitives/test_rsa.py | 99 ++++++++++++++++++++++++++- tests/hazmat/primitives/test_serialization.py | 29 +++++++- 3 files changed, 151 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 0e4d75ed..35b7c5c3 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -15,11 +15,12 @@ import pytest from cryptography import utils from cryptography.exceptions import InternalError, _Reasons +from cryptography.hazmat.backends.interfaces import RSABackend from cryptography.hazmat.backends.openssl.backend import ( Backend, backend ) from cryptography.hazmat.backends.openssl.ec import _sn_to_elliptic_curve -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import dsa, padding from cryptography.hazmat.primitives.ciphers import ( BlockCipherAlgorithm, Cipher, CipherAlgorithm @@ -27,7 +28,7 @@ from cryptography.hazmat.primitives.ciphers import ( from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.modes import CBC, CTR, Mode -from ..primitives.fixtures_rsa import RSA_KEY_512 +from ..primitives.fixtures_rsa import RSA_KEY_2048, RSA_KEY_512 from ...utils import load_vectors_from_file, raises_unsupported_algorithm @@ -493,3 +494,27 @@ class TestOpenSSLEllipticCurve(object): def test_sn_to_elliptic_curve_not_supported(self): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_ELLIPTIC_CURVE): _sn_to_elliptic_curve(backend, b"fake") + + +@pytest.mark.requires_backend_interface(interface=RSABackend) +class TestRSAPEMSerialization(object): + def test_password_length_limit(self): + password = b"x" * 1024 + key = RSA_KEY_2048.private_key(backend) + with pytest.raises(ValueError): + key.dump( + serialization.PKCS8( + serialization.Encoding.PEM + ), + serialization.BestAvailable(password) + ) + + def test_unsupported_key_encoding(self): + key = RSA_KEY_2048.private_key(backend) + with pytest.raises(ValueError): + key.dump( + serialization.PKCS8( + serialization.Encoding.DER + ), + serialization.NoEncryption() + ) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 74183010..72bc08ad 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -15,8 +15,10 @@ from cryptography import utils from cryptography.exceptions import ( AlreadyFinalized, InvalidSignature, _Reasons ) -from cryptography.hazmat.backends.interfaces import RSABackend -from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.backends.interfaces import ( + PEMSerializationBackend, RSABackend +) +from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import padding, rsa from cryptography.hazmat.primitives.asymmetric.rsa import ( RSAPrivateNumbers, RSAPublicNumbers @@ -46,6 +48,11 @@ class DummyMGF(object): _salt_length = 0 +@utils.register_interface(serialization.KeySerializationEncryption) +class DummyKeyEncryption(object): + pass + + def _flatten_pkcs1_examples(vectors): flattened_vectors = [] for vector in vectors: @@ -78,6 +85,18 @@ def test_modular_inverse(): ) +def _skip_if_no_serialization(key, backend): + if not isinstance(key, rsa.RSAPrivateKeyWithSerialization): + pytest.skip( + "{0} does not support RSA key serialization".format(backend) + ) + + +def test_skip_if_no_serialization(): + with pytest.raises(pytest.skip.Exception): + _skip_if_no_serialization("notakeywithserialization", "backend") + + @pytest.mark.requires_backend_interface(interface=RSABackend) class TestRSA(object): @pytest.mark.parametrize( @@ -1725,3 +1744,79 @@ class TestRSAPrimeFactorRecovery(object): def test_invalid_recover_prime_factors(self): with pytest.raises(ValueError): rsa.rsa_recover_prime_factors(34, 3, 7) + + +@pytest.mark.requires_backend_interface(interface=RSABackend) +@pytest.mark.requires_backend_interface(interface=PEMSerializationBackend) +class TestRSAPEMWriter(object): + @pytest.mark.parametrize( + ("serializer", "password"), + itertools.product( + [serialization.TraditionalOpenSSL, serialization.PKCS8], + [ + b"s", + b"longerpassword", + b"!*$&(@#$*&($T@%_somesymbols", + b"\x01" * 1000, + ] + ) + ) + def test_dump_encrypted_pem(self, backend, serializer, password): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + serialized = key.dump( + serializer(serialization.Encoding.PEM), + serialization.BestAvailable(password) + ) + loaded_key = serialization.load_pem_private_key( + serialized, password, backend + ) + loaded_priv_num = loaded_key.private_numbers() + priv_num = key.private_numbers() + assert loaded_priv_num == priv_num + + @pytest.mark.parametrize( + "serializer", + (serialization.TraditionalOpenSSL, serialization.PKCS8), + ) + def test_dump_unencrypted_pem(self, backend, serializer): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + serialized = key.dump( + serializer(serialization.Encoding.PEM), + serialization.NoEncryption() + ) + loaded_key = serialization.load_pem_private_key( + serialized, None, backend + ) + loaded_priv_num = loaded_key.private_numbers() + priv_num = key.private_numbers() + assert loaded_priv_num == priv_num + + def test_dump_invalid_serializer(self, backend): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.dump("notaserializer", serialization.NoEncryption()) + + def test_dump_invalid_encryption_algorithm(self, backend): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.dump( + serialization.TraditionalOpenSSL( + serialization.Encoding.PEM + ), + "notanencalg" + ) + + def test_dump_unsupported_encryption_type(self, backend): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + with pytest.raises(ValueError): + key.dump( + serialization.TraditionalOpenSSL( + serialization.Encoding.PEM + ), + DummyKeyEncryption() + ) diff --git a/tests/hazmat/primitives/test_serialization.py b/tests/hazmat/primitives/test_serialization.py index a17aac4b..2a5fb21d 100644 --- a/tests/hazmat/primitives/test_serialization.py +++ b/tests/hazmat/primitives/test_serialization.py @@ -18,8 +18,9 @@ from cryptography.hazmat.backends.interfaces import ( ) from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa from cryptography.hazmat.primitives.serialization import ( - load_der_private_key, load_der_public_key, load_pem_private_key, - load_pem_public_key, load_ssh_public_key + BestAvailable, Encoding, PKCS8, TraditionalOpenSSL, load_der_private_key, + load_der_public_key, load_pem_private_key, load_pem_public_key, + load_ssh_public_key ) @@ -1159,3 +1160,27 @@ class TestECDSASSHSerialization(object): ) with pytest.raises(ValueError): load_ssh_public_key(ssh_key, backend) + + +@pytest.mark.parametrize( + "serializer", + [PKCS8, TraditionalOpenSSL] +) +class TestSerializers(object): + def test_invalid_encoding(self, serializer): + with pytest.raises(TypeError): + serializer("thing") + + def test_valid_params(self, serializer): + fmt = serializer(Encoding.PEM) + assert isinstance(fmt, (PKCS8, TraditionalOpenSSL)) + + +class TestKeySerializationEncryptionTypes(object): + def test_non_bytes_password(self): + with pytest.raises(ValueError): + BestAvailable(object()) + + def test_encryption_with_zero_length_password(self): + with pytest.raises(ValueError): + BestAvailable(b"") -- cgit v1.2.3 From 199dc276cd1b45a799b511090b37237df49d68a3 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 23 Feb 2015 20:45:21 -0600 Subject: address review comments --- tests/hazmat/backends/test_openssl.py | 12 +++---- tests/hazmat/primitives/test_rsa.py | 51 ++++++++++++++++++--------- tests/hazmat/primitives/test_serialization.py | 23 +++--------- 3 files changed, 43 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 35b7c5c3..b5de702d 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -503,18 +503,16 @@ class TestRSAPEMSerialization(object): key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): key.dump( - serialization.PKCS8( - serialization.Encoding.PEM - ), - serialization.BestAvailable(password) + serialization.Encoding.PEM, + serialization.Format.PKCS8, + serialization.BestAvailableEncryption(password) ) def test_unsupported_key_encoding(self): key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): key.dump( - serialization.PKCS8( - serialization.Encoding.DER - ), + serialization.Encoding.DER, + serialization.Format.PKCS8, serialization.NoEncryption() ) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 72bc08ad..a0df2f26 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1750,9 +1750,12 @@ class TestRSAPrimeFactorRecovery(object): @pytest.mark.requires_backend_interface(interface=PEMSerializationBackend) class TestRSAPEMWriter(object): @pytest.mark.parametrize( - ("serializer", "password"), + ("fmt", "password"), itertools.product( - [serialization.TraditionalOpenSSL, serialization.PKCS8], + [ + serialization.Format.TraditionalOpenSSL, + serialization.Format.PKCS8 + ], [ b"s", b"longerpassword", @@ -1761,12 +1764,13 @@ class TestRSAPEMWriter(object): ] ) ) - def test_dump_encrypted_pem(self, backend, serializer, password): + def test_dump_encrypted_pem(self, backend, fmt, password): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) serialized = key.dump( - serializer(serialization.Encoding.PEM), - serialization.BestAvailable(password) + serialization.Encoding.PEM, + fmt, + serialization.BestAvailableEncryption(password) ) loaded_key = serialization.load_pem_private_key( serialized, password, backend @@ -1776,14 +1780,15 @@ class TestRSAPEMWriter(object): assert loaded_priv_num == priv_num @pytest.mark.parametrize( - "serializer", - (serialization.TraditionalOpenSSL, serialization.PKCS8), + "fmt", + (serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8), ) - def test_dump_unencrypted_pem(self, backend, serializer): + def test_dump_unencrypted_pem(self, backend, fmt): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) serialized = key.dump( - serializer(serialization.Encoding.PEM), + serialization.Encoding.PEM, + fmt, serialization.NoEncryption() ) loaded_key = serialization.load_pem_private_key( @@ -1793,20 +1798,33 @@ class TestRSAPEMWriter(object): priv_num = key.private_numbers() assert loaded_priv_num == priv_num - def test_dump_invalid_serializer(self, backend): + def test_dump_invalid_encoding(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.dump("notaserializer", serialization.NoEncryption()) + key.dump( + "notencoding", + serialization.Format.PKCS8, + serialization.NoEncryption() + ) + + def test_dump_invalid_format(self, backend): + key = RSA_KEY_2048.private_key(backend) + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.dump( + serialization.Encoding.PEM, + "invalidformat", + serialization.NoEncryption() + ) def test_dump_invalid_encryption_algorithm(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): key.dump( - serialization.TraditionalOpenSSL( - serialization.Encoding.PEM - ), + serialization.Encoding.PEM, + serialization.Format.TraditionalOpenSSL, "notanencalg" ) @@ -1815,8 +1833,7 @@ class TestRSAPEMWriter(object): _skip_if_no_serialization(key, backend) with pytest.raises(ValueError): key.dump( - serialization.TraditionalOpenSSL( - serialization.Encoding.PEM - ), + serialization.Encoding.PEM, + serialization.Format.TraditionalOpenSSL, DummyKeyEncryption() ) diff --git a/tests/hazmat/primitives/test_serialization.py b/tests/hazmat/primitives/test_serialization.py index 2a5fb21d..fc8f8664 100644 --- a/tests/hazmat/primitives/test_serialization.py +++ b/tests/hazmat/primitives/test_serialization.py @@ -18,9 +18,8 @@ from cryptography.hazmat.backends.interfaces import ( ) from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa from cryptography.hazmat.primitives.serialization import ( - BestAvailable, Encoding, PKCS8, TraditionalOpenSSL, load_der_private_key, - load_der_public_key, load_pem_private_key, load_pem_public_key, - load_ssh_public_key + BestAvailableEncryption, load_der_private_key, load_der_public_key, + load_pem_private_key, load_pem_public_key, load_ssh_public_key ) @@ -1162,25 +1161,11 @@ class TestECDSASSHSerialization(object): load_ssh_public_key(ssh_key, backend) -@pytest.mark.parametrize( - "serializer", - [PKCS8, TraditionalOpenSSL] -) -class TestSerializers(object): - def test_invalid_encoding(self, serializer): - with pytest.raises(TypeError): - serializer("thing") - - def test_valid_params(self, serializer): - fmt = serializer(Encoding.PEM) - assert isinstance(fmt, (PKCS8, TraditionalOpenSSL)) - - class TestKeySerializationEncryptionTypes(object): def test_non_bytes_password(self): with pytest.raises(ValueError): - BestAvailable(object()) + BestAvailableEncryption(object()) def test_encryption_with_zero_length_password(self): with pytest.raises(ValueError): - BestAvailable(b"") + BestAvailableEncryption(b"") -- cgit v1.2.3 From 8aad028501ef434071d3969bce41c4e6375b4c61 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 23 Feb 2015 22:03:09 -0600 Subject: rename dump to as_bytes --- tests/hazmat/backends/test_openssl.py | 4 ++-- tests/hazmat/primitives/test_rsa.py | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index b5de702d..8cf14b98 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -502,7 +502,7 @@ class TestRSAPEMSerialization(object): password = b"x" * 1024 key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): - key.dump( + key.as_bytes( serialization.Encoding.PEM, serialization.Format.PKCS8, serialization.BestAvailableEncryption(password) @@ -511,7 +511,7 @@ class TestRSAPEMSerialization(object): def test_unsupported_key_encoding(self): key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): - key.dump( + key.as_bytes( serialization.Encoding.DER, serialization.Format.PKCS8, serialization.NoEncryption() diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index a0df2f26..17a2a414 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1764,10 +1764,10 @@ class TestRSAPEMWriter(object): ] ) ) - def test_dump_encrypted_pem(self, backend, fmt, password): + def test_as_bytes_encrypted_pem(self, backend, fmt, password): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) - serialized = key.dump( + serialized = key.as_bytes( serialization.Encoding.PEM, fmt, serialization.BestAvailableEncryption(password) @@ -1783,10 +1783,10 @@ class TestRSAPEMWriter(object): "fmt", (serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8), ) - def test_dump_unencrypted_pem(self, backend, fmt): + def test_as_bytes_unencrypted_pem(self, backend, fmt): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) - serialized = key.dump( + serialized = key.as_bytes( serialization.Encoding.PEM, fmt, serialization.NoEncryption() @@ -1798,41 +1798,41 @@ class TestRSAPEMWriter(object): priv_num = key.private_numbers() assert loaded_priv_num == priv_num - def test_dump_invalid_encoding(self, backend): + def test_as_bytes_invalid_encoding(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.dump( + key.as_bytes( "notencoding", serialization.Format.PKCS8, serialization.NoEncryption() ) - def test_dump_invalid_format(self, backend): + def test_as_bytes_invalid_format(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.dump( + key.as_bytes( serialization.Encoding.PEM, "invalidformat", serialization.NoEncryption() ) - def test_dump_invalid_encryption_algorithm(self, backend): + def test_as_bytes_invalid_encryption_algorithm(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.dump( + key.as_bytes( serialization.Encoding.PEM, serialization.Format.TraditionalOpenSSL, "notanencalg" ) - def test_dump_unsupported_encryption_type(self, backend): + def test_as_bytes_unsupported_encryption_type(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(ValueError): - key.dump( + key.as_bytes( serialization.Encoding.PEM, serialization.Format.TraditionalOpenSSL, DummyKeyEncryption() -- cgit v1.2.3 From 858dd3fcb2851b647c2581c8f4246d0d9c7c2be2 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 23 Feb 2015 23:47:06 -0600 Subject: listify a parametrized test --- tests/hazmat/primitives/test_rsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 17a2a414..8dcb6450 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1781,7 +1781,7 @@ class TestRSAPEMWriter(object): @pytest.mark.parametrize( "fmt", - (serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8), + [serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8], ) def test_as_bytes_unencrypted_pem(self, backend, fmt): key = RSA_KEY_2048.private_key(backend) -- cgit v1.2.3 From 20456e96ed510f96c5bdc24b799b89200e3dba4c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 27 Feb 2015 22:45:24 -0600 Subject: add exact byte test --- tests/hazmat/primitives/test_rsa.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 8dcb6450..dde73b06 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1798,6 +1798,23 @@ class TestRSAPEMWriter(object): priv_num = key.private_numbers() assert loaded_priv_num == priv_num + def test_as_bytes_traditional_openssl_unencrypted_pem(self, backend): + key_bytes = load_vectors_from_file( + os.path.join( + "asymmetric", + "Traditional_OpenSSL_Serialization", + "testrsa.pem" + ), + lambda pemfile: pemfile.read().encode() + ) + key = serialization.load_pem_private_key(key_bytes, None, backend) + serialized = key.as_bytes( + serialization.Encoding.PEM, + serialization.Format.TraditionalOpenSSL, + serialization.NoEncryption() + ) + assert serialized == key_bytes + def test_as_bytes_invalid_encoding(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) -- cgit v1.2.3 From 223a8f02a37a87b3c7366441647013cf9a18b061 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 28 Feb 2015 18:54:10 -0600 Subject: change as_bytes to private_bytes, link more things --- tests/hazmat/backends/test_openssl.py | 4 ++-- tests/hazmat/primitives/test_rsa.py | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 8cf14b98..4f44f686 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -502,7 +502,7 @@ class TestRSAPEMSerialization(object): password = b"x" * 1024 key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): - key.as_bytes( + key.private_bytes( serialization.Encoding.PEM, serialization.Format.PKCS8, serialization.BestAvailableEncryption(password) @@ -511,7 +511,7 @@ class TestRSAPEMSerialization(object): def test_unsupported_key_encoding(self): key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): - key.as_bytes( + key.private_bytes( serialization.Encoding.DER, serialization.Format.PKCS8, serialization.NoEncryption() diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index dde73b06..0cf94afe 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1764,10 +1764,10 @@ class TestRSAPEMWriter(object): ] ) ) - def test_as_bytes_encrypted_pem(self, backend, fmt, password): + def test_private_bytes_encrypted_pem(self, backend, fmt, password): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) - serialized = key.as_bytes( + serialized = key.private_bytes( serialization.Encoding.PEM, fmt, serialization.BestAvailableEncryption(password) @@ -1783,10 +1783,10 @@ class TestRSAPEMWriter(object): "fmt", [serialization.Format.TraditionalOpenSSL, serialization.Format.PKCS8], ) - def test_as_bytes_unencrypted_pem(self, backend, fmt): + def test_private_bytes_unencrypted_pem(self, backend, fmt): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) - serialized = key.as_bytes( + serialized = key.private_bytes( serialization.Encoding.PEM, fmt, serialization.NoEncryption() @@ -1798,7 +1798,7 @@ class TestRSAPEMWriter(object): priv_num = key.private_numbers() assert loaded_priv_num == priv_num - def test_as_bytes_traditional_openssl_unencrypted_pem(self, backend): + def test_private_bytes_traditional_openssl_unencrypted_pem(self, backend): key_bytes = load_vectors_from_file( os.path.join( "asymmetric", @@ -1808,48 +1808,48 @@ class TestRSAPEMWriter(object): lambda pemfile: pemfile.read().encode() ) key = serialization.load_pem_private_key(key_bytes, None, backend) - serialized = key.as_bytes( + serialized = key.private_bytes( serialization.Encoding.PEM, serialization.Format.TraditionalOpenSSL, serialization.NoEncryption() ) assert serialized == key_bytes - def test_as_bytes_invalid_encoding(self, backend): + def test_private_bytes_invalid_encoding(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.as_bytes( + key.private_bytes( "notencoding", serialization.Format.PKCS8, serialization.NoEncryption() ) - def test_as_bytes_invalid_format(self, backend): + def test_private_bytes_invalid_format(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.as_bytes( + key.private_bytes( serialization.Encoding.PEM, "invalidformat", serialization.NoEncryption() ) - def test_as_bytes_invalid_encryption_algorithm(self, backend): + def test_private_bytes_invalid_encryption_algorithm(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(TypeError): - key.as_bytes( + key.private_bytes( serialization.Encoding.PEM, serialization.Format.TraditionalOpenSSL, "notanencalg" ) - def test_as_bytes_unsupported_encryption_type(self, backend): + def test_private_bytes_unsupported_encryption_type(self, backend): key = RSA_KEY_2048.private_key(backend) _skip_if_no_serialization(key, backend) with pytest.raises(ValueError): - key.as_bytes( + key.private_bytes( serialization.Encoding.PEM, serialization.Format.TraditionalOpenSSL, DummyKeyEncryption() -- cgit v1.2.3