diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2015-01-17 17:26:42 -0500 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2015-01-17 17:26:42 -0500 |
commit | 4f27959a58f7cc0a26a488fd2005fd9e69364092 (patch) | |
tree | ca8e765f88170b46f692a0c2f7514bbdd22b3d5f | |
parent | 2dcdf87195b9bbe3be8abd5439ebc14a4e8bfe36 (diff) | |
parent | 170f1da651bcd97a05f3876953e04a6e7f44c06e (diff) | |
download | cryptography-4f27959a58f7cc0a26a488fd2005fd9e69364092.tar.gz cryptography-4f27959a58f7cc0a26a488fd2005fd9e69364092.tar.bz2 cryptography-4f27959a58f7cc0a26a488fd2005fd9e69364092.zip |
Merge pull request #1609 from reaperhulk/der-multibackend
DER multibackend
-rw-r--r-- | src/cryptography/hazmat/backends/multibackend.py | 25 | ||||
-rw-r--r-- | tests/hazmat/backends/test_multibackend.py | 27 |
2 files changed, 46 insertions, 6 deletions
diff --git a/src/cryptography/hazmat/backends/multibackend.py b/src/cryptography/hazmat/backends/multibackend.py index 27ee8e7f..9bda76c8 100644 --- a/src/cryptography/hazmat/backends/multibackend.py +++ b/src/cryptography/hazmat/backends/multibackend.py @@ -7,14 +7,15 @@ from __future__ import absolute_import, division, print_function from cryptography import utils from cryptography.exceptions import UnsupportedAlgorithm, _Reasons from cryptography.hazmat.backends.interfaces import ( - CMACBackend, CipherBackend, DSABackend, EllipticCurveBackend, HMACBackend, - HashBackend, PBKDF2HMACBackend, PEMSerializationBackend, - RSABackend, X509Backend + CMACBackend, CipherBackend, DERSerializationBackend, DSABackend, + EllipticCurveBackend, HMACBackend, HashBackend, PBKDF2HMACBackend, + PEMSerializationBackend, RSABackend, X509Backend ) @utils.register_interface(CMACBackend) @utils.register_interface(CipherBackend) +@utils.register_interface(DERSerializationBackend) @utils.register_interface(HashBackend) @utils.register_interface(HMACBackend) @utils.register_interface(PBKDF2HMACBackend) @@ -288,6 +289,24 @@ class MultiBackend(object): _Reasons.UNSUPPORTED_SERIALIZATION ) + def load_der_private_key(self, data, password): + for b in self._filtered_backends(DERSerializationBackend): + return b.load_der_private_key(data, password) + + raise UnsupportedAlgorithm( + "This backend does not support this key serialization.", + _Reasons.UNSUPPORTED_SERIALIZATION + ) + + def load_der_public_key(self, data): + for b in self._filtered_backends(DERSerializationBackend): + return b.load_der_public_key(data) + + raise UnsupportedAlgorithm( + "This backend does not support this key serialization.", + _Reasons.UNSUPPORTED_SERIALIZATION + ) + def load_pem_x509_certificate(self, data): for b in self._filtered_backends( X509Backend diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index 43ecbf83..5a8891c4 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -9,9 +9,9 @@ from cryptography.exceptions import ( UnsupportedAlgorithm, _Reasons ) from cryptography.hazmat.backends.interfaces import ( - CMACBackend, CipherBackend, DSABackend, EllipticCurveBackend, HMACBackend, - HashBackend, PBKDF2HMACBackend, PEMSerializationBackend, - RSABackend, X509Backend + CMACBackend, CipherBackend, DERSerializationBackend, DSABackend, + EllipticCurveBackend, HMACBackend, HashBackend, PBKDF2HMACBackend, + PEMSerializationBackend, RSABackend, X509Backend ) from cryptography.hazmat.backends.multibackend import MultiBackend from cryptography.hazmat.primitives import cmac, hashes, hmac @@ -180,6 +180,15 @@ class DummyPEMSerializationBackend(object): pass +@utils.register_interface(DERSerializationBackend) +class DummyDERSerializationBackend(object): + def load_der_private_key(self, data, password): + pass + + def load_der_public_key(self, data): + pass + + @utils.register_interface(X509Backend) class DummyX509Backend(object): def load_pem_x509_certificate(self, data): @@ -446,6 +455,18 @@ class TestMultiBackend(object): with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION): backend.load_pem_public_key(b"keydata") + def test_der_serialization_backend(self): + backend = MultiBackend([DummyDERSerializationBackend()]) + + backend.load_der_private_key(b"keydata", None) + backend.load_der_public_key(b"keydata") + + backend = MultiBackend([]) + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION): + backend.load_der_private_key(b"keydata", None) + with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_SERIALIZATION): + backend.load_der_public_key(b"keydata") + def test_x509_backend(self): backend = MultiBackend([DummyX509Backend()]) |