Browse Source

Improve libcrypto detection

Closes #23.
Jonas Borgström 11 years ago
parent
commit
c973040bad
2 changed files with 21 additions and 8 deletions
  1. 1 0
      CHANGES
  2. 20 8
      attic/crypto.py

+ 1 - 0
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
 -------------

+ 20 - 8
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