@ -31,6 +31,37 @@ class CipherName:
return self . _blocksize
def get_version ( library ) :
version = None
hexversion = None
cflags = None
try :
#OpenSSL 1.1
OPENSSL_VERSION = 0
OPENSSL_CFLAGS = 1
library . OpenSSL_version . argtypes = [ ctypes . c_int ]
library . OpenSSL_version . restype = ctypes . c_char_p
version = library . OpenSSL_version ( OPENSSL_VERSION )
cflags = library . OpenSSL_version ( OPENSSL_CFLAGS )
library . OpenSSL_version_num . restype = ctypes . c_long
hexversion = library . OpenSSL_version_num ( )
except AttributeError :
try :
#OpenSSL 1.0
SSLEAY_VERSION = 0
SSLEAY_CFLAGS = 2
library . SSLeay . restype = ctypes . c_long
library . SSLeay_version . restype = ctypes . c_char_p
library . SSLeay_version . argtypes = [ ctypes . c_int ]
version = library . SSLeay_version ( SSLEAY_VERSION )
cflags = library . SSLeay_version ( SSLEAY_CFLAGS )
hexversion = library . SSLeay ( )
except AttributeError :
#raise NotImplementedError('Cannot determine version of this OpenSSL library.')
pass
return ( version , hexversion , cflags )
class _OpenSSL :
"""
Wrapper for OpenSSL using ctypes
@ -40,6 +71,7 @@ class _OpenSSL:
Build the wrapper
"""
self . _lib = ctypes . CDLL ( library )
self . _version , self . _hexversion , self . _cflags = get_version ( self . _lib )
self . pointer = ctypes . pointer
self . c_int = ctypes . c_int
@ -138,18 +170,27 @@ class _OpenSSL:
self . EC_KEY_set_private_key . argtypes = [ ctypes . c_void_p ,
ctypes . c_void_p ]
self . ECDH_OpenSSL = self . _lib . ECDH_OpenSSL
self . _lib . ECDH_OpenSSL . restype = ctypes . c_void_p
self . _lib . ECDH_OpenSSL . argtypes = [ ]
if self . _hexversion > 0x10100000 :
self . EC_KEY_OpenSSL = self . _lib . EC_KEY_OpenSSL
self . _lib . EC_KEY_OpenSSL . restype = ctypes . c_void_p
self . _lib . EC_KEY_OpenSSL . argtypes = [ ]
self . EC_KEY_set_method = self . _lib . EC_KEY_set_method
self . _lib . EC_KEY_set_method . restype = ctypes . c_int
self . _lib . EC_KEY_set_method . argtypes = [ ctypes . c_void_p , ctypes . c_void_p ]
else :
self . ECDH_OpenSSL = self . _lib . ECDH_OpenSSL
self . _lib . ECDH_OpenSSL . restype = ctypes . c_void_p
self . _lib . ECDH_OpenSSL . argtypes = [ ]
self . ECDH_set_method = self . _lib . ECDH_set_method
self . _lib . ECDH_set_method . restype = ctypes . c_int
self . _lib . ECDH_set_method . argtypes = [ ctypes . c_void_p , ctypes . c_void_p ]
self . BN_CTX_new = self . _lib . BN_CTX_new
self . _lib . BN_CTX_new . restype = ctypes . c_void_p
self . _lib . BN_CTX_new . argtypes = [ ]
self . ECDH_set_method = self . _lib . ECDH_set_method
self . _lib . ECDH_set_method . restype = ctypes . c_int
self . _lib . ECDH_set_method . argtypes = [ ctypes . c_void_p , ctypes . c_void_p ]
self . ECDH_compute_key = self . _lib . ECDH_compute_key
self . ECDH_compute_key . restype = ctypes . c_int
self . ECDH_compute_key . argtypes = [ ctypes . c_void_p ,
@ -208,10 +249,15 @@ class _OpenSSL:
self . EVP_rc4 = self . _lib . EVP_rc4
self . EVP_rc4 . restype = ctypes . c_void_p
self . EVP_rc4 . argtypes = [ ]
self . EVP_CIPHER_CTX_cleanup = self . _lib . EVP_CIPHER_CTX_cleanup
self . EVP_CIPHER_CTX_cleanup . restype = ctypes . c_int
self . EVP_CIPHER_CTX_cleanup . argtypes = [ ctypes . c_void_p ]
if self . _hexversion > 0x10100000 :
self . EVP_CIPHER_CTX_reset = self . _lib . EVP_CIPHER_CTX_reset
self . EVP_CIPHER_CTX_reset . restype = ctypes . c_int
self . EVP_CIPHER_CTX_reset . argtypes = [ ctypes . c_void_p ]
else :
self . EVP_CIPHER_CTX_cleanup = self . _lib . EVP_CIPHER_CTX_cleanup
self . EVP_CIPHER_CTX_cleanup . restype = ctypes . c_int
self . EVP_CIPHER_CTX_cleanup . argtypes = [ ctypes . c_void_p ]
self . EVP_CIPHER_CTX_free = self . _lib . EVP_CIPHER_CTX_free
self . EVP_CIPHER_CTX_free . restype = None
@ -250,10 +296,6 @@ class _OpenSSL:
self . EVP_DigestFinal_ex . argtypes = [ ctypes . c_void_p ,
ctypes . c_void_p , ctypes . c_void_p ]
self . EVP_ecdsa = self . _lib . EVP_ecdsa
self . _lib . EVP_ecdsa . restype = ctypes . c_void_p
self . _lib . EVP_ecdsa . argtypes = [ ]
self . ECDSA_sign = self . _lib . ECDSA_sign
self . ECDSA_sign . restype = ctypes . c_int
self . ECDSA_sign . argtypes = [ ctypes . c_int , ctypes . c_void_p ,
@ -264,23 +306,47 @@ class _OpenSSL:
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 ]
self . EVP_MD_CTX_create = self . _lib . EVP_MD_CTX_create
self . EVP_MD_CTX_create . restype = ctypes . c_void_p
self . EVP_MD_CTX_create . argtypes = [ ]
if self . _hexversion > 0x10100000 :
self . EVP_MD_CTX_new = self . _lib . EVP_MD_CTX_new
self . EVP_MD_CTX_new . restype = ctypes . c_void_p
self . EVP_MD_CTX_new . argtypes = [ ]
self . EVP_MD_CTX_reset = self . _lib . EVP_MD_CTX_reset
self . EVP_MD_CTX_reset . restype = None
self . EVP_MD_CTX_reset . argtypes = [ ctypes . c_void_p ]
self . EVP_MD_CTX_init = self . _lib . EVP_MD_CTX_init
self . EVP_MD_CTX_init . restype = None
self . EVP_MD_CTX_init . argtypes = [ ctypes . c_void_p ]
self . EVP_MD_CTX_free = self . _lib . EVP_MD_CTX_free
self . EVP_MD_CTX_free . restype = None
self . EVP_MD_CTX_free . argtypes = [ ctypes . c_void_p ]
self . EVP_MD_CTX_destroy = self . _lib . EVP_MD_CTX_destroy
self . EVP_MD_CTX_destroy . restype = None
self . EVP_MD_CTX_destroy . argtypes = [ ctypes . c_void_p ]
self . EVP_sha1 = self . _lib . EVP_sha1
self . EVP_sha1 . restype = ctypes . c_void_p
self . EVP_sha1 . argtypes = [ ]
self . digest_ecdsa_sha1 = self . EVP_sha1
else :
self . EVP_MD_CTX_create = self . _lib . EVP_MD_CTX_create
self . EVP_MD_CTX_create . restype = ctypes . c_void_p
self . EVP_MD_CTX_create . argtypes = [ ]
self . EVP_MD_CTX_init = self . _lib . EVP_MD_CTX_init
self . EVP_MD_CTX_init . restype = None
self . EVP_MD_CTX_init . argtypes = [ ctypes . c_void_p ]
self . EVP_MD_CTX_destroy = self . _lib . EVP_MD_CTX_destroy
self . EVP_MD_CTX_destroy . restype = None
self . EVP_MD_CTX_destroy . argtypes = [ ctypes . c_void_p ]
self . EVP_ecdsa = self . _lib . EVP_ecdsa
self . _lib . EVP_ecdsa . restype = ctypes . c_void_p
self . _lib . EVP_ecdsa . argtypes = [ ]
self . digest_ecdsa_sha1 = self . EVP_ecdsa
self . RAND_bytes = self . _lib . RAND_bytes
self . RAND_bytes . restype = ctypes . c_int
self . RAND_bytes . argtypes = [ ctypes . c_void_p , ctypes . c_int ]
self . EVP_sha256 = self . _lib . EVP_sha256
self . EVP_sha256 . restype = ctypes . c_void_p
self . EVP_sha256 . argtypes = [ ]
@ -437,7 +503,11 @@ def loadOpenSSL():
if ' darwin ' in sys . platform :
libdir . extend ( [
path . join ( environ [ ' RESOURCEPATH ' ] , ' .. ' , ' Frameworks ' , ' libcrypto.dylib ' ) ,
path . join ( environ [ ' RESOURCEPATH ' ] , ' .. ' , ' Frameworks ' , ' libcrypto.1.0.0.dylib ' )
path . join ( environ [ ' RESOURCEPATH ' ] , ' .. ' , ' 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 :
libdir . append ( path . join ( sys . _MEIPASS , ' libeay32.dll ' ) )
@ -445,8 +515,16 @@ def loadOpenSSL():
libdir . extend ( [
path . join ( sys . _MEIPASS , ' libcrypto.so ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so ' ) ,
path . join ( sys . _MEIPASS , ' libcrypto.so.1.1.0 ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so.1.1.0 ' ) ,
path . join ( sys . _MEIPASS , ' libcrypto.so.1.0.2 ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so.1.0.2 ' ) ,
path . join ( sys . _MEIPASS , ' libcrypto.so.1.0.1 ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so.1.0.1 ' ) ,
path . join ( sys . _MEIPASS , ' libcrypto.so.1.0.0 ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so.1.0.0 ' ) ,
path . join ( sys . _MEIPASS , ' libcrypto.so.0.9.8 ' ) ,
path . join ( sys . _MEIPASS , ' libssl.so.0.9.8 ' ) ,
] )
if ' darwin ' in sys . platform :
libdir . extend ( [ ' libcrypto.dylib ' , ' /usr/local/opt/openssl/lib/libcrypto.dylib ' ] )
@ -455,7 +533,7 @@ def loadOpenSSL():
else :
libdir . append ( ' libcrypto.so ' )
libdir . append ( ' libssl.so ' )
if ' linux ' in sys . platform or ' darwin ' in sys . platform or ' free bsd' in sys . platform :
if ' linux ' in sys . platform or ' darwin ' in sys . platform or ' bsd' in sys . platform :
libdir . append ( find_library ( ' ssl ' ) )
elif ' win32 ' in sys . platform or ' win64 ' in sys . platform :
libdir . append ( find_library ( ' libeay32 ' ) )
@ -467,4 +545,4 @@ def loadOpenSSL():
pass
raise Exception ( " Couldn ' t find and load the OpenSSL library. You must install it. " )
loadOpenSSL ( )
loadOpenSSL ( )