diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 16:34:32 -0400 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 16:34:32 -0400 |
commit | 88e7ed6415ccf7fb2432b90876deefa8ab88cc98 (patch) | |
tree | f0d9d75696b461b6628d13b2e61c94ceff81e180 /tests | |
parent | 7d5483b7cd0065b1f21b068ac2278ba74c21dc67 (diff) | |
parent | efc7f3d23836b7fd6633e95af0e2626eb1f594da (diff) | |
download | cryptography-88e7ed6415ccf7fb2432b90876deefa8ab88cc98.tar.gz cryptography-88e7ed6415ccf7fb2432b90876deefa8ab88cc98.tar.bz2 cryptography-88e7ed6415ccf7fb2432b90876deefa8ab88cc98.zip |
Merge pull request #1706 from reaperhulk/serialize-rsa-public-key
Serialize RSA public keys
Diffstat (limited to 'tests')
-rw-r--r-- | tests/hazmat/backends/test_openssl.py | 10 | ||||
-rw-r--r-- | tests/hazmat/primitives/test_rsa.py | 49 |
2 files changed, 56 insertions, 3 deletions
diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 8ee9d246..ba0a2ba3 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -508,7 +508,7 @@ class TestRSAPEMSerialization(object): serialization.BestAvailableEncryption(password) ) - def test_unsupported_key_encoding(self): + def test_unsupported_private_key_encoding(self): key = RSA_KEY_2048.private_key(backend) with pytest.raises(ValueError): key.private_bytes( @@ -516,3 +516,11 @@ class TestRSAPEMSerialization(object): serialization.PrivateFormat.PKCS8, serialization.NoEncryption() ) + + def test_unsupported_public_key_encoding(self): + key = RSA_KEY_2048.private_key(backend).public_key() + with pytest.raises(ValueError): + key.public_bytes( + serialization.Encoding.DER, + serialization.PublicFormat.SubjectPublicKeyInfo + ) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 890a1d4e..e6d0ac28 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -86,7 +86,10 @@ def test_modular_inverse(): def _skip_if_no_serialization(key, backend): - if not isinstance(key, rsa.RSAPrivateKeyWithSerialization): + if not isinstance( + key, + (rsa.RSAPrivateKeyWithSerialization, rsa.RSAPublicKeyWithSerialization) + ): pytest.skip( "{0} does not support RSA key serialization".format(backend) ) @@ -1748,7 +1751,7 @@ class TestRSAPrimeFactorRecovery(object): @pytest.mark.requires_backend_interface(interface=RSABackend) @pytest.mark.requires_backend_interface(interface=PEMSerializationBackend) -class TestRSAPEMWriter(object): +class TestRSAPEMPrivateKeySerialization(object): @pytest.mark.parametrize( ("fmt", "password"), itertools.product( @@ -1857,3 +1860,45 @@ class TestRSAPEMWriter(object): serialization.PrivateFormat.TraditionalOpenSSL, DummyKeyEncryption() ) + + +@pytest.mark.requires_backend_interface(interface=RSABackend) +@pytest.mark.requires_backend_interface(interface=PEMSerializationBackend) +class TestRSAPEMPublicKeySerialization(object): + def test_public_bytes_unencrypted_pem(self, backend): + key_bytes = load_vectors_from_file( + os.path.join("asymmetric", "PKCS8", "unenc-rsa-pkcs8.pub.pem"), + lambda pemfile: pemfile.read().encode() + ) + key = serialization.load_pem_public_key(key_bytes, backend) + _skip_if_no_serialization(key, backend) + serialized = key.public_bytes( + serialization.Encoding.PEM, + serialization.PublicFormat.SubjectPublicKeyInfo, + ) + assert serialized == key_bytes + + def test_public_bytes_pkcs1_unencrypted_pem(self, backend): + key_bytes = load_vectors_from_file( + os.path.join("asymmetric", "public", "PKCS1", "rsa.pub.pem"), + lambda pemfile: pemfile.read().encode() + ) + key = serialization.load_pem_public_key(key_bytes, backend) + _skip_if_no_serialization(key, backend) + serialized = key.public_bytes( + serialization.Encoding.PEM, + serialization.PublicFormat.PKCS1, + ) + assert serialized == key_bytes + + def test_public_bytes_invalid_encoding(self, backend): + key = RSA_KEY_2048.private_key(backend).public_key() + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.public_bytes("notencoding", serialization.PublicFormat.PKCS1) + + def test_public_bytes_invalid_format(self, backend): + key = RSA_KEY_2048.private_key(backend).public_key() + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.public_bytes(serialization.Encoding.PEM, "invalidformat") |