From c973040bad000b8fa79a2b6a77e3a2c82bbb8656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Thu, 9 Jan 2014 22:10:21 +0100 Subject: [PATCH] Improve libcrypto detection Closes #23. --- CHANGES | 1 + attic/crypto.py | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 0065c6134..bf8d4c5e7 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ Version 0.9 - Improved error handling / reporting. (#12) - Use fcntl() instead of flock() when locking repository/cache. (#15) - Let ssh figure out port/user if not specified so we don't override .ssh/config (#9) +- Improved libcrypto path detection (#23). Version 0.8.1 ------------- diff --git a/attic/crypto.py b/attic/crypto.py index daac6cec8..d093778c3 100644 --- a/attic/crypto.py +++ b/attic/crypto.py @@ -1,17 +1,29 @@ """A thin ctypes based wrapper for OpenSSL 1.0 """ -import sys +import os from ctypes import cdll, c_char_p, c_int, c_uint, c_void_p, POINTER, create_string_buffer from ctypes.util import find_library import struct -libcrypto = cdll.LoadLibrary(find_library('crypto')) -# Default libcrypto on OS X is too old, try the brew version -if not hasattr(libcrypto, 'PKCS5_PBKDF2_HMAC') and sys.platform == 'darwin': - libcrypto = cdll.LoadLibrary('/usr/local/opt/openssl/lib/libcrypto.dylib') -# Default libcrypto on FreeBSD is too old, try the ports version -if not hasattr(libcrypto, 'PKCS5_PBKDF2_HMAC') and sys.platform.startswith('freebsd'): - libcrypto = cdll.LoadLibrary('/usr/local/lib/libcrypto.so') + +def _find_libcrypto(): + _possible_paths = [ + find_library('crypto'), + os.environ.get('ATTIC_LIBCRYPTO_PATH'), + '/usr/local/opt/openssl/lib/libcrypto.dylib', # OS X Brew + '/usr/local/lib/libcrypto.so', # FreeBSD Ports + '/usr/local/ssl/lib/libcrypto.so' + ] + for path in _possible_paths: + try: + lib = cdll.LoadLibrary(path) + if hasattr(lib, 'PKCS5_PBKDF2_HMAC'): + return lib + except OSError: + pass + raise Exception('Failed to find libcrypto version >= 1.0') + +libcrypto = _find_libcrypto() libcrypto.PKCS5_PBKDF2_HMAC.argtypes = (c_char_p, c_int, c_char_p, c_int, c_int, c_void_p, c_int, c_char_p) libcrypto.EVP_sha256.restype = c_void_p