openssl pylint issue fixes

This commit is contained in:
lakshyacis 2019-12-21 15:14:31 +05:30
parent 814aae5166
commit 8659c5313d
No known key found for this signature in database
GPG Key ID: D2C539C8EC63E9EB
1 changed files with 78 additions and 49 deletions

View File

@ -2,22 +2,20 @@
# See LICENSE for details. # See LICENSE for details.
# #
# Software slightly changed by Jonathan Warren <bitmessage at-symbol jonwarren.org> # Software slightly changed by Jonathan Warren <bitmessage at-symbol jonwarren.org>
# pylint: disable=protected-access
""" """
This module loads openssl libs with ctypes and incapsulates This module loads openssl libs with ctypes and incapsulates
needed openssl functionality in class _OpenSSL. needed openssl functionality in class _OpenSSL.
""" """
# pylint: disable=protected-access
import sys import sys
import ctypes import ctypes
OpenSSL = None OpenSSL = None
class CipherName: class CipherName(object):
"""Class returns cipher name, pointer and blocksize""" """Class returns cipher name, pointer and blocksize"""
# pylint: disable=old-style-class
def __init__(self, name, pointer, blocksize): def __init__(self, name, pointer, blocksize):
self._name = name self._name = name
self._pointer = pointer self._pointer = pointer
@ -73,11 +71,11 @@ def get_version(library):
return (version, hexversion, cflags) return (version, hexversion, cflags)
class _OpenSSL: class _OpenSSL(object):
""" """
Wrapper for OpenSSL using ctypes Wrapper for OpenSSL using ctypes
""" """
# pylint: disable=too-many-statements, too-many-instance-attributes, old-style-class # pylint: disable=too-many-statements, too-many-instance-attributes
def __init__(self, library): def __init__(self, library):
""" """
Build the wrapper Build the wrapper
@ -140,7 +138,8 @@ class _OpenSSL:
self.EC_KEY_get0_group.restype = ctypes.c_void_p self.EC_KEY_get0_group.restype = ctypes.c_void_p
self.EC_KEY_get0_group.argtypes = [ctypes.c_void_p] self.EC_KEY_get0_group.argtypes = [ctypes.c_void_p]
self.EC_POINT_get_affine_coordinates_GFp = self._lib.EC_POINT_get_affine_coordinates_GFp self.EC_POINT_get_affine_coordinates_GFp = \
self._lib.EC_POINT_get_affine_coordinates_GFp
self.EC_POINT_get_affine_coordinates_GFp.restype = ctypes.c_int self.EC_POINT_get_affine_coordinates_GFp.restype = ctypes.c_int
self.EC_POINT_get_affine_coordinates_GFp.argtypes = [ctypes.c_void_p, self.EC_POINT_get_affine_coordinates_GFp.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p,
@ -163,7 +162,8 @@ class _OpenSSL:
self.EC_KEY_set_group.argtypes = [ctypes.c_void_p, self.EC_KEY_set_group.argtypes = [ctypes.c_void_p,
ctypes.c_void_p] ctypes.c_void_p]
self.EC_POINT_set_affine_coordinates_GFp = self._lib.EC_POINT_set_affine_coordinates_GFp self.EC_POINT_set_affine_coordinates_GFp = \
self._lib.EC_POINT_set_affine_coordinates_GFp
self.EC_POINT_set_affine_coordinates_GFp.restype = ctypes.c_int self.EC_POINT_set_affine_coordinates_GFp.restype = ctypes.c_int
self.EC_POINT_set_affine_coordinates_GFp.argtypes = [ctypes.c_void_p, self.EC_POINT_set_affine_coordinates_GFp.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p,
@ -297,7 +297,8 @@ class _OpenSSL:
self.EVP_CipherUpdate = self._lib.EVP_CipherUpdate self.EVP_CipherUpdate = self._lib.EVP_CipherUpdate
self.EVP_CipherUpdate.restype = ctypes.c_int self.EVP_CipherUpdate.restype = ctypes.c_int
self.EVP_CipherUpdate.argtypes = [ctypes.c_void_p, self.EVP_CipherUpdate.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_int]
self.EVP_CipherFinal_ex = self._lib.EVP_CipherFinal_ex self.EVP_CipherFinal_ex = self._lib.EVP_CipherFinal_ex
self.EVP_CipherFinal_ex.restype = ctypes.c_int self.EVP_CipherFinal_ex.restype = ctypes.c_int
@ -330,12 +331,14 @@ class _OpenSSL:
self.ECDSA_sign = self._lib.ECDSA_sign self.ECDSA_sign = self._lib.ECDSA_sign
self.ECDSA_sign.restype = ctypes.c_int self.ECDSA_sign.restype = ctypes.c_int
self.ECDSA_sign.argtypes = [ctypes.c_int, ctypes.c_void_p, self.ECDSA_sign.argtypes = [ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] ctypes.c_int, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p]
self.ECDSA_verify = self._lib.ECDSA_verify self.ECDSA_verify = self._lib.ECDSA_verify
self.ECDSA_verify.restype = ctypes.c_int self.ECDSA_verify.restype = ctypes.c_int
self.ECDSA_verify.argtypes = [ctypes.c_int, ctypes.c_void_p, self.ECDSA_verify.argtypes = [ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p] ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p]
if self._hexversion >= 0x10100000 and not self._libreSSL: if self._hexversion >= 0x10100000 and not self._libreSSL:
self.EVP_MD_CTX_new = self._lib.EVP_MD_CTX_new self.EVP_MD_CTX_new = self._lib.EVP_MD_CTX_new
@ -393,7 +396,8 @@ class _OpenSSL:
self.HMAC = self._lib.HMAC self.HMAC = self._lib.HMAC
self.HMAC.restype = ctypes.c_void_p self.HMAC.restype = ctypes.c_void_p
self.HMAC.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, self.HMAC.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p] ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p, ctypes.c_void_p]
try: try:
self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC
@ -530,17 +534,29 @@ class _OpenSSL:
def _set_ciphers(self): def _set_ciphers(self):
self.cipher_algo = { self.cipher_algo = {
'aes-128-cbc': CipherName('aes-128-cbc', self.EVP_aes_128_cbc, 16), 'aes-128-cbc': CipherName(
'aes-256-cbc': CipherName('aes-256-cbc', self.EVP_aes_256_cbc, 16), 'aes-128-cbc', self.EVP_aes_128_cbc, 16),
'aes-128-cfb': CipherName('aes-128-cfb', self.EVP_aes_128_cfb128, 16), 'aes-256-cbc': CipherName(
'aes-256-cfb': CipherName('aes-256-cfb', self.EVP_aes_256_cfb128, 16), 'aes-256-cbc', self.EVP_aes_256_cbc, 16),
'aes-128-ofb': CipherName('aes-128-ofb', self._lib.EVP_aes_128_ofb, 16), 'aes-128-cfb': CipherName(
'aes-256-ofb': CipherName('aes-256-ofb', self._lib.EVP_aes_256_ofb, 16), 'aes-128-cfb', self.EVP_aes_128_cfb128, 16),
# 'aes-128-ctr': CipherName('aes-128-ctr', self._lib.EVP_aes_128_ctr, 16), 'aes-256-cfb': CipherName(
# 'aes-256-ctr': CipherName('aes-256-ctr', self._lib.EVP_aes_256_ctr, 16), 'aes-256-cfb', self.EVP_aes_256_cfb128, 16),
'bf-cfb': CipherName('bf-cfb', self.EVP_bf_cfb64, 8), 'aes-128-ofb': CipherName(
'bf-cbc': CipherName('bf-cbc', self.EVP_bf_cbc, 8), 'aes-128-ofb', self._lib.EVP_aes_128_ofb, 16),
'rc4': CipherName('rc4', self.EVP_rc4, 128), # 128 is the initialisation size not block size 'aes-256-ofb': CipherName(
'aes-256-ofb', self._lib.EVP_aes_256_ofb, 16),
# 'aes-128-ctr': CipherName(
# 'aes-128-ctr', self._lib.EVP_aes_128_ctr, 16),
# 'aes-256-ctr': CipherName(
# 'aes-256-ctr', self._lib.EVP_aes_256_ctr, 16),
'bf-cfb': CipherName(
'bf-cfb', self.EVP_bf_cfb64, 8),
'bf-cbc': CipherName(
'bf-cbc', self.EVP_bf_cbc, 8),
# 128 is the initialisation size not block size
'rc4': CipherName(
'rc4', self.EVP_rc4, 128),
} }
def _set_curves(self): def _set_curves(self):
@ -600,14 +616,13 @@ class _OpenSSL:
raise Exception("Unknown curve") raise Exception("Unknown curve")
return self.curves[name] return self.curves[name]
def get_curve_by_id(self, id): def get_curve_by_id(self, id_):
""" """
returns the name of a elliptic curve with his id returns the name of a elliptic curve with his id
""" """
# pylint: disable=redefined-builtin
res = None res = None
for i in self.curves: for i in self.curves:
if self.curves[i] == id: if self.curves[i] == id_:
res = i res = i
break break
if res is None: if res is None:
@ -618,32 +633,31 @@ class _OpenSSL:
""" """
OpenSSL random function OpenSSL random function
""" """
# pylint: disable=redefined-builtin buffer_ = self.malloc(0, size)
buffer = self.malloc(0, size) # This pyelliptic library, by default, didn't check the return value
# This pyelliptic library, by default, didn't check the return value of RAND_bytes. It is # of RAND_bytes. It is evidently possible that it returned an error
# evidently possible that it returned an error and not-actually-random data. However, in # and not-actually-random data. However, in tests on various
# tests on various operating systems, while generating hundreds of gigabytes of random # operating systems, while generating hundreds of gigabytes of random
# strings of various sizes I could not get an error to occur. Also Bitcoin doesn't check # strings of various sizes I could not get an error to occur.
# the return value of RAND_bytes either. # Also Bitcoin doesn't check the return value of RAND_bytes either.
# Fixed in Bitmessage version 0.4.2 (in source code on 2013-10-13) # Fixed in Bitmessage version 0.4.2 (in source code on 2013-10-13)
while self.RAND_bytes(buffer, size) != 1: while self.RAND_bytes(buffer_, size) != 1:
import time import time
time.sleep(1) time.sleep(1)
return buffer.raw return buffer_.raw
def malloc(self, data, size): def malloc(self, data, size):
""" """
returns a create_string_buffer (ctypes) returns a create_string_buffer (ctypes)
""" """
# pylint: disable=redefined-builtin buffer_ = None
buffer = None
if data != 0: if data != 0:
if sys.version_info.major == 3 and isinstance(data, type('')): if sys.version_info.major == 3 and isinstance(data, type('')):
data = data.encode() data = data.encode()
buffer = self.create_string_buffer(data, size) buffer_ = self.create_string_buffer(data, size)
else: else:
buffer = self.create_string_buffer(size) buffer_ = self.create_string_buffer(size)
return buffer return buffer_
def loadOpenSSL(): def loadOpenSSL():
@ -657,12 +671,24 @@ def loadOpenSSL():
if getattr(sys, 'frozen', None): if getattr(sys, 'frozen', None):
if 'darwin' in sys.platform: if 'darwin' in sys.platform:
libdir.extend([ libdir.extend([
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.dylib'), path.join(
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.1.1.0.dylib'), environ['RESOURCEPATH'], '..',
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.1.0.2.dylib'), 'Frameworks', 'libcrypto.dylib'),
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.1.0.1.dylib'), path.join(
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.1.0.0.dylib'), environ['RESOURCEPATH'], '..',
path.join(environ['RESOURCEPATH'], '..', 'Frameworks', 'libcrypto.0.9.8.dylib'), 'Frameworks', 'libcrypto.1.1.0.dylib'),
path.join(
environ['RESOURCEPATH'], '..',
'Frameworks', 'libcrypto.1.0.2.dylib'),
path.join(
environ['RESOURCEPATH'], '..',
'Frameworks', 'libcrypto.1.0.1.dylib'),
path.join(
environ['RESOURCEPATH'], '..',
'Frameworks', 'libcrypto.1.0.0.dylib'),
path.join(
environ['RESOURCEPATH'], '..',
'Frameworks', 'libcrypto.0.9.8.dylib'),
]) ])
elif 'win32' in sys.platform or 'win64' in sys.platform: elif 'win32' in sys.platform or 'win64' in sys.platform:
libdir.append(path.join(sys._MEIPASS, 'libeay32.dll')) libdir.append(path.join(sys._MEIPASS, 'libeay32.dll'))
@ -682,7 +708,8 @@ def loadOpenSSL():
path.join(sys._MEIPASS, 'libssl.so.0.9.8'), path.join(sys._MEIPASS, 'libssl.so.0.9.8'),
]) ])
if 'darwin' in sys.platform: if 'darwin' in sys.platform:
libdir.extend(['libcrypto.dylib', '/usr/local/opt/openssl/lib/libcrypto.dylib']) libdir.extend([
'libcrypto.dylib', '/usr/local/opt/openssl/lib/libcrypto.dylib'])
elif 'win32' in sys.platform or 'win64' in sys.platform: elif 'win32' in sys.platform or 'win64' in sys.platform:
libdir.append('libeay32.dll') libdir.append('libeay32.dll')
else: else:
@ -690,7 +717,8 @@ def loadOpenSSL():
libdir.append('libssl.so') libdir.append('libssl.so')
libdir.append('libcrypto.so.1.0.0') libdir.append('libcrypto.so.1.0.0')
libdir.append('libssl.so.1.0.0') libdir.append('libssl.so.1.0.0')
if 'linux' in sys.platform or 'darwin' in sys.platform or 'bsd' in sys.platform: if 'linux' in sys.platform or 'darwin' in sys.platform \
or 'bsd' in sys.platform:
libdir.append(find_library('ssl')) libdir.append(find_library('ssl'))
elif 'win32' in sys.platform or 'win64' in sys.platform: elif 'win32' in sys.platform or 'win64' in sys.platform:
libdir.append(find_library('libeay32')) libdir.append(find_library('libeay32'))
@ -700,7 +728,8 @@ def loadOpenSSL():
return return
except: except:
pass pass
raise Exception("Couldn't find and load the OpenSSL library. You must install it.") raise Exception(
"Couldn't find and load the OpenSSL library. You must install it.")
loadOpenSSL() loadOpenSSL()