diff options
author | David Reid <dreid@dreid.org> | 2013-11-04 19:37:46 -0800 |
---|---|---|
committer | David Reid <dreid@dreid.org> | 2013-11-04 19:37:46 -0800 |
commit | 4a84def6d508cafef5d32d72c68b0c56fc1f810c (patch) | |
tree | e428bf2a267577f93e37ea05714cd08ab2ae3f54 | |
parent | 79eaa7efa2aaafd62ffeee7385526f14647e5a7e (diff) | |
parent | 86f98a051c9298020ac1d6cdb06ceb513699c7fa (diff) | |
download | cryptography-4a84def6d508cafef5d32d72c68b0c56fc1f810c.tar.gz cryptography-4a84def6d508cafef5d32d72c68b0c56fc1f810c.tar.bz2 cryptography-4a84def6d508cafef5d32d72c68b0c56fc1f810c.zip |
Merge pull request #216 from alex/more-error-condition
Replaced an assertion in the OpenSSL backend with a proper exception
-rw-r--r-- | cryptography/hazmat/bindings/openssl/backend.py | 4 | ||||
-rw-r--r-- | tests/hazmat/bindings/test_openssl.py | 23 |
2 files changed, 26 insertions, 1 deletions
diff --git a/cryptography/hazmat/bindings/openssl/backend.py b/cryptography/hazmat/bindings/openssl/backend.py index 7367818a..ea1073b9 100644 --- a/cryptography/hazmat/bindings/openssl/backend.py +++ b/cryptography/hazmat/bindings/openssl/backend.py @@ -147,7 +147,9 @@ class _CipherContext(object): raise UnsupportedAlgorithm evp_cipher = adapter(self._backend, cipher, mode) - assert evp_cipher != self._backend.ffi.NULL + if evp_cipher == self._backend.ffi.NULL: + raise UnsupportedAlgorithm + if isinstance(mode, interfaces.ModeWithInitializationVector): iv_nonce = mode.initialization_vector elif isinstance(mode, interfaces.ModeWithNonce): diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index fb01c10a..f283a7dd 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -13,11 +13,21 @@ import pytest +from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.bindings.openssl.backend import backend, Backend +from cryptography.hazmat.primitives.block import BlockCipher from cryptography.hazmat.primitives.block.ciphers import AES from cryptography.hazmat.primitives.block.modes import CBC +class FakeMode(object): + pass + + +class FakeCipher(object): + pass + + class TestOpenSSL(object): def test_backend_exists(self): assert backend @@ -44,3 +54,16 @@ class TestOpenSSL(object): b = Backend() assert b.ffi is backend.ffi assert b.lib is backend.lib + + def test_nonexistent_cipher(self): + b = Backend() + b.ciphers.register_cipher_adapter( + FakeCipher, + FakeMode, + lambda backend, cipher, mode: backend.ffi.NULL + ) + cipher = BlockCipher( + FakeCipher(), FakeMode(), backend=b, + ) + with pytest.raises(UnsupportedAlgorithm): + cipher.encryptor() |