diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 22:39:49 -0400 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-03-08 22:39:49 -0400 |
commit | 0ee210bf4c03309512fdaad13d481548d9056237 (patch) | |
tree | 3bdb6730ce0a993f373193528e618388282e70cf /tests/hazmat/primitives/test_dsa.py | |
parent | 930fc6a24e890ac1ed044985eaf82118db542617 (diff) | |
parent | 26006c526782bba2628da1d7819550b455a3b784 (diff) | |
download | cryptography-0ee210bf4c03309512fdaad13d481548d9056237.tar.gz cryptography-0ee210bf4c03309512fdaad13d481548d9056237.tar.bz2 cryptography-0ee210bf4c03309512fdaad13d481548d9056237.zip |
Merge pull request #1734 from reaperhulk/serialize-dsa-public-key
DSA public key serialization
Diffstat (limited to 'tests/hazmat/primitives/test_dsa.py')
-rw-r--r-- | tests/hazmat/primitives/test_dsa.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/hazmat/primitives/test_dsa.py b/tests/hazmat/primitives/test_dsa.py index 19ca0794..112818f4 100644 --- a/tests/hazmat/primitives/test_dsa.py +++ b/tests/hazmat/primitives/test_dsa.py @@ -31,7 +31,10 @@ from ...utils import ( def _skip_if_no_serialization(key, backend): - if not isinstance(key, dsa.DSAPrivateKeyWithSerialization): + if not isinstance( + key, + (dsa.DSAPrivateKeyWithSerialization, dsa.DSAPublicKeyWithSerialization) + ): pytest.skip( "{0} does not support DSA key serialization".format(backend) ) @@ -936,3 +939,43 @@ class TestDSASerialization(object): serialization.PrivateFormat.TraditionalOpenSSL, DummyKeyEncryption() ) + + +@pytest.mark.requires_backend_interface(interface=DSABackend) +@pytest.mark.requires_backend_interface(interface=PEMSerializationBackend) +class TestDSAPEMPublicKeySerialization(object): + def test_public_bytes_unencrypted_pem(self, backend): + key_bytes = load_vectors_from_file( + os.path.join("asymmetric", "PKCS8", "unenc-dsa-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_invalid_encoding(self, backend): + key = DSA_KEY_2048.private_key(backend).public_key() + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.public_bytes( + "notencoding", + serialization.PublicFormat.SubjectPublicKeyInfo + ) + + def test_public_bytes_invalid_format(self, backend): + key = DSA_KEY_2048.private_key(backend).public_key() + _skip_if_no_serialization(key, backend) + with pytest.raises(TypeError): + key.public_bytes(serialization.Encoding.PEM, "invalidformat") + + def test_public_bytes_pkcs1_unsupported(self, backend): + key = DSA_KEY_2048.private_key(backend).public_key() + _skip_if_no_serialization(key, backend) + with pytest.raises(ValueError): + key.public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.PKCS1 + ) |