From 8a2a5ca1f9fc0036d28807d2a9b8a130e560d85b Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Sat, 6 Feb 2016 17:51:44 +0300 Subject: [PATCH] Fixed incompatibility with OpenSSL 1.1-pre for digest, cipher and MAC modules. Changed added oid in tests for one which doesn't exists in OpenSSL 1.1 OID database --- ctypescrypto/cipher.py | 11 +++++++++-- ctypescrypto/digest.py | 17 ++++++++++++----- ctypescrypto/mac.py | 2 +- tests/testoids.py | 4 ++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ctypescrypto/cipher.py b/ctypescrypto/cipher.py index d4035c1..b4a00cf 100644 --- a/ctypescrypto/cipher.py +++ b/ctypescrypto/cipher.py @@ -229,7 +229,7 @@ class Cipher(object): """ try: if self.ctx is not None: - libcrypto.EVP_CIPHER_CTX_cleanup(self.ctx) + self.__ctxcleanup(self.ctx) libcrypto.EVP_CIPHER_CTX_free(self.ctx) del self.ctx except AttributeError: @@ -241,7 +241,14 @@ class Cipher(object): # Used C function block_size # libcrypto.EVP_CIPHER_block_size.argtypes = (c_void_p, ) -libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p, ) + +#Function EVP_CIPHER_CTX_cleanup renamed to EVP_CIPHER_CTX_reset +# in the OpenSSL 1.1.0 +if hasattr(libcrypto,"EVP_CIPHER_CTX_cleanup"): + Cipher.__ctxcleanup = libcrypto.EVP_CIPHER_CTX_cleanup.argtypes +else: + Cipher.__ctxcleanup = libcrypto.EVP_CIPHER_CTX_reset +Cipher.__ctxcleanup.argtypes = (c_void_p, ) libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p, ) libcrypto.EVP_CIPHER_CTX_new.restype = c_void_p libcrypto.EVP_CIPHER_CTX_set_padding.argtypes = (c_void_p, c_int) diff --git a/ctypescrypto/digest.py b/ctypescrypto/digest.py index 8a9a74e..3589fc3 100644 --- a/ctypescrypto/digest.py +++ b/ctypescrypto/digest.py @@ -97,7 +97,7 @@ class Digest(object): """ Initializes digest using given type. """ - self.ctx = libcrypto.EVP_MD_CTX_create() + self.ctx = self.newctx() if self.ctx is None: raise DigestError("Unable to create digest context") self.digest_out = None @@ -167,7 +167,7 @@ class Digest(object): """ try: if self.ctx is not None: - libcrypto.EVP_MD_CTX_destroy(self.ctx) + libcrypto.EVP_MD_CTX_free(self.ctx) del self.ctx except AttributeError: pass @@ -186,15 +186,22 @@ class Digest(object): # Declare function result and argument types libcrypto.EVP_get_digestbyname.restype = c_void_p libcrypto.EVP_get_digestbyname.argtypes = (c_char_p, ) -libcrypto.EVP_MD_CTX_create.restype = c_void_p +# These two functions are renamed in OpenSSL 1.1.0 +if hasattr(libcrypto,"EVP_MD_CTX_create"): + Digest.newctx = libcrypto.EVP_MD_CTX_create + Digest.freectx = libcrypto.EVP_MD_CTX_destroy +else: + Digest.newctx = libcrypto.EVP_MD_CTX_new + Digest.freectx = libcrypto.EVP_MD_CTX_free +Digest.newctx.restype = c_void_p +Digest.freectx.argtypes = (c_void_p, ) # libcrypto.EVP_MD_CTX_create has no arguments -libcrypto.EVP_DigestInit_ex.restupe = c_int +libcrypto.EVP_DigestInit_ex.restype = c_int libcrypto.EVP_DigestInit_ex.argtypes = (c_void_p, c_void_p, c_void_p) libcrypto.EVP_DigestUpdate.restype = c_int libcrypto.EVP_DigestUpdate.argtypes = (c_void_p, c_char_p, c_longlong) libcrypto.EVP_DigestFinal_ex.restype = c_int libcrypto.EVP_DigestFinal_ex.argtypes = (c_void_p, c_char_p, POINTER(c_long)) -libcrypto.EVP_MD_CTX_destroy.argtypes = (c_void_p, ) libcrypto.EVP_MD_CTX_copy.restype = c_int libcrypto.EVP_MD_CTX_copy.argtypes = (c_void_p, c_void_p) libcrypto.EVP_MD_type.argtypes = (c_void_p, ) diff --git a/ctypescrypto/mac.py b/ctypescrypto/mac.py index 0a96cb0..370eeb5 100644 --- a/ctypescrypto/mac.py +++ b/ctypescrypto/mac.py @@ -56,7 +56,7 @@ class MAC(Digest): if self.key is None: raise DigestError("EVP_PKEY_new_mac_key") pctx=c_void_p() - self.ctx = libcrypto.EVP_MD_CTX_create() + self.ctx = self.newctx() if self.ctx == 0: raise DigestError("Unable to create digest context") if libcrypto.EVP_DigestSignInit(self.ctx,pointer(pctx),d,None,self.key) <= 0: diff --git a/tests/testoids.py b/tests/testoids.py index 981b1bd..66a6c7f 100644 --- a/tests/testoids.py +++ b/tests/testoids.py @@ -57,8 +57,8 @@ class TestCustom(unittest.TestCase): x=Oid(sn) self.assertEqual(o,x) def testCleanup(self): - d='1.2.643.100.3' - sn="SNILS" + d='1.2.643.100.9' + sn="SNILX" long_name="Russian Pension security number" o=create(d,sn,long_name) cleanup() -- 2.39.5