From b84cf9aeb85f67b840c6b2cf84b1717e65edd5cd Mon Sep 17 00:00:00 2001 From: NoDRM Date: Sun, 2 Jan 2022 17:29:27 +0100 Subject: [PATCH] Fix libcrypto DLL path search (see #13 and #14) Co-authored-by: Adriano Caloiaro --- DeDRM_plugin/adobekey.py | 27 +++++++++++++++++++++++---- DeDRM_plugin/config.py | 4 ++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/DeDRM_plugin/adobekey.py b/DeDRM_plugin/adobekey.py index 9871eda..e696b8f 100644 --- a/DeDRM_plugin/adobekey.py +++ b/DeDRM_plugin/adobekey.py @@ -124,11 +124,27 @@ if iswindows: except ImportError: import _winreg as winreg + def get_fake_windows_libcrypto_path(): + # There seems to be a bug in Wine where a `find_library('libcrypto-1_1')` + # will not return the path to the libcrypto-1_1.dll file. + # So if we're on Windows, and we didn't find the libcrypto the normal way, + # lets try a hack-y workaround. It's already over anyways at this + # point, can't really make it worse. + import sys, os + for p in sys.path: + if os.path.isfile(os.path.join(p, "libcrypto-1_1.dll")): + return os.path.join(p, "libcrypto-1_1.dll") + if os.path.isfile(os.path.join(p, "libeay32.dll")): + return os.path.join(p, "libeay.dll") + return None + def _load_crypto_libcrypto(): from ctypes.util import find_library libcrypto = find_library('libcrypto-1_1') if libcrypto is None: libcrypto = find_library('libeay32') + if libcrypto is None: + libcrypto = get_fake_windows_libcrypto_path() if libcrypto is None: raise ADEPTError('libcrypto not found') libcrypto = CDLL(libcrypto) @@ -170,7 +186,10 @@ if iswindows: return AES def _load_crypto_pycrypto(): - from Crypto.Cipher import AES as _AES + try: + from Crypto.Cipher import AES as _AES + except (ImportError, ModuleNotFoundError): + from Cryptodome.Cipher import AES as _AES class AES(object): def __init__(self, key): self._aes = _AES.new(key, _AES.MODE_CBC, b'\x00'*16) @@ -184,7 +203,7 @@ if iswindows: try: AES = loader() break - except (ImportError, ADEPTError): + except (ImportError, ModuleNotFoundError, ADEPTError): pass return AES @@ -396,7 +415,7 @@ if iswindows: try: regkey = winreg.OpenKey(cuser, DEVICE_KEY_PATH) device = winreg.QueryValueEx(regkey, 'key')[0] - except WindowsError: + except WindowsError, FileNotFoundError: raise ADEPTError("Adobe Digital Editions not activated") keykey = CryptUnprotectData(device, entropy) userkey = None @@ -404,7 +423,7 @@ if iswindows: names = [] try: plkroot = winreg.OpenKey(cuser, PRIVATE_LICENCE_KEY_PATH) - except WindowsError: + except WindowsError, FileNotFoundError: raise ADEPTError("Could not locate ADE activation") i = -1 diff --git a/DeDRM_plugin/config.py b/DeDRM_plugin/config.py index fa92fa2..40f645b 100755 --- a/DeDRM_plugin/config.py +++ b/DeDRM_plugin/config.py @@ -1132,7 +1132,7 @@ class AddAdeptDialog(): defaultkeys, defaultnames = adeptkeys() else: # linux - from .wineutils import WineGetKeys + from wineutils import WineGetKeys scriptpath = os.path.join(parent.parent.alfdir,"adobekey.py") defaultkeys, defaultnames = WineGetKeys(scriptpath, ".der",parent.getwineprefix()) @@ -1228,7 +1228,7 @@ class AddKindleDialog(QDialog): defaultkeys = kindlekeys() else: # linux - from .wineutils import WineGetKeys + from wineutils import WineGetKeys scriptpath = os.path.join(parent.parent.alfdir,"kindlekey.py") defaultkeys = WineGetKeys(scriptpath, ".k4i",parent.getwineprefix())