cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

No success in packaging an application using cryptography

Open mike805 opened this issue 11 years ago • 12 comments

I have an application written using python 2.7, twisted, OpenSSL, and (implicitly) cryptography. It works fine with a Python installation, and is all ready to put out for beta testing, but I cannot create a working Windows EXE.

I've tried cx_freeze, pyinstaller, and py2exe. I tried both the pip-installed cryptography 0.61 and building source from master (0.7-dev1), after reading that 0.7 might handle packaging better. No matter what I do, the binding fails at _ensure_ffi_initialized or build_ffi_for_binding and I get either "No module named aes" or "RuntimeError: Attempted implicit compile of a cffi module. All cffi modules should be pre-compiled at installation time." The three cryptography pyd files are present in the build directory.

There are multiple reports of this all over stackoverflow and various forums, with no clear solutions presented. I've tried all the suggestions and nothing has worked.

Is there any program out there that uses cryptography and has been successfully built to exe? I'd like to just take a working setup procedure and adapt it. Any ideas? Thank you.

mike805 avatar Dec 14 '14 00:12 mike805

After a few days fighting this problem on and off, I started adding print statements all along the call chain, including Verifier.py in the site-packages directory. I found that it is looking for the _cryptography*pyd files in the pycache under my python home directory.

Long story short, this hack made it work:

In src\cryptography\hazmat\bindings\utils.py around line 112 is def build_ffi(...):, and: tmpdir='', I changed that to: tmpdir=os.path.dirname(os.path.realpath(sys.argv[0])), Rebuild and reinstall cryptography package. Then after the cx_freeze I did: ren "cryptography._Cryptography_cffi_3a414503xe735153f.pyd" "_Cryptography_cffi_3a414503xe735153f.pyd" ren "cryptography._Cryptography_cffi_590da19fxffc7b1ce.pyd" "_Cryptography_cffi_590da19fxffc7b1ce.pyd" ren "cryptography._Cryptography_cffi_8f86901cxc1767c5a.pyd" "_Cryptography_cffi_8f86901cxc1767c5a.pyd" and now my program works without an interpreter - whew!

mike805 avatar Dec 15 '14 10:12 mike805

I may have run into this as well. The error I get is: cryptography.exceptions.UnsupportedAlgorithm: cipher AES in CBC mode is not supported by this backend. I've created a small separate test case at https://github.com/latusrepo/cryptographybugtestcase in case someone wants to look at that and/or include it in regression tests.

jamesabel avatar Jan 19 '15 04:01 jamesabel

Same problem here. Getting UnsupportedAlgorithm: sha256 is not a supported hash in this backend with both cx_freeze and pyinstaller. Tried @mike805's suggestion but no luck unfortunately. Any luck tracing this down @latusrepo?

samdobson avatar Feb 19 '15 00:02 samdobson

No - I ended up using pynsist ( https://github.com/takluyver/pynsist ) as my installer, which doesn't expose this problem. @takluyver might have an idea on what the actual issue is.

jamesabel avatar Feb 19 '15 00:02 jamesabel

Freeze tools put Python packages into zip files. In principle, you can import Python modules directly from a zip file, but in practice this seems to to be very error prone - there's lots of ways for modules to implicitly assume that they're on a filesystem. And even if they can handle being in a zip file, they may only handle being in a setuptools zipped egg monstrosity (don't get me started on eggs).

My guess is that the loading of compiled libraries for CFFI falls foul of this in some way.

Pynsist is my attempt at packaging Python apps without freezing. I am also trying to make the next version of cx_Freeze work differently in this respect, because the zipped packages cause so many problems.

takluyver avatar Feb 19 '15 01:02 takluyver

I debugged this for Confidant Mail by putting print statements in the cx_freeze libraries until I figured out what it was looking for.

On 2/18/2015 5:12 PM, Thomas Kluyver wrote:

Freeze tools put Python packages into zip files. In principle, you can import Python modules directly from a zip file, but in practice this seems to to be very error prone - there's lots of ways for modules to implicitly assume that they're on a filesystem. And even if they can handle being in a zip file, they may only handle being in a setuptools zipped egg monstrosity (don't get me started on eggs).

My guess is that the loading of compiled libraries for CFFI falls foul of this in some way.

Pynsist is my attempt at packaging Python apps without freezing. I am also trying https://bitbucket.org/anthony_tuininga/cx_freeze/pull-request/73/store-packages-as-files-folders-by-default to make the next version of cx_Freeze work differently in this respect, because the zipped packages cause so many problems.

— Reply to this email directly or view it on GitHub https://github.com/pyca/cryptography/issues/1524#issuecomment-74982807.

mike805 avatar Feb 19 '15 01:02 mike805

I'm also trying to freeze a Python application that has pyOpenSSL and therefore cryptography as a dependency, except in this case using PyInstaller.

The resulting binary was not able to find the _Cryptography_cffi_4ee21876x41fb9936.c file.

IOError: [Errno 2] No such file or directory: 'C:\\agent\\build\\agent\\out00PYZ.pyz\\__pycache__\\_Cryptography_cffi_4ee21876x41fb9936.c'

As mentioned above I made the following change:

In src\cryptography\hazmat\bindings\utils.py around line 112 is def build_ffi(...):, and:
tmpdir='',
I changed that to:
tmpdir=os.path.dirname(os.path.realpath(sys.argv[0])),

This allowed the resulting PyInstaller created exe to find the Crypto__cffi.pyd files at runtime.

Unfortunately, the frozen exe is now throwing this RunTimeError:

C:\Users\sacreman\agent\dist\agent>agent.exe
Traceback (most recent call last):
  File "<string>", line 19, in <module>
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\lib.plugin", line 23,
in import_plugin_deps
  File "c:\python27\lib\site-packages\PyInstaller-2.1.1dev_67610f2-py2.7.egg\PyI
nstaller\loader\pyi_importers.py", line 276, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\OpenSSL", line 8, in <
module>
  File "c:\python27\lib\site-packages\PyInstaller-2.1.1dev_67610f2-py2.7.egg\PyI
nstaller\loader\pyi_importers.py", line 276, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\OpenSSL.rand", line 11
, in <module>
  File "c:\python27\lib\site-packages\PyInstaller-2.1.1dev_67610f2-py2.7.egg\PyI
nstaller\loader\pyi_importers.py", line 276, in load_module
    exec(bytecode, module.__dict__)
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\OpenSSL._util", line 4
, in <module>
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\cryptography.hazmat.bi
ndings.openssl.binding", line 113, in __init__
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\cryptography.hazmat.bi
ndings.openssl.binding", line 123, in _ensure_ffi_initialized
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\cryptography.hazmat.bi
ndings.utils", line 32, in load_library_for_binding
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\cffi.verifier", line 7
4, in load_library
  File "C:\Users\sacreman\agent\build\agent\out00-PYZ.pyz\cryptography.hazmat.bi
ndings.utils", line 128, in _compile_module
RuntimeError: Attempted implicit compile of a cffi module. All cffi modules should be pre-compiled at installation time.

I'm not entirely sure how to troubleshoot this. I've had a look in the source but wasn't sure what verifier was supposed to be doing.

sacreman avatar Feb 20 '15 00:02 sacreman

I made the change:

cryptography-master\src\cryptography\hazmat\bindings\utils.py Verifier( line tmpdir=os.path.dirname(os.path.realpath(sys.argv[0])),

setup.py is:

-- coding: utf-8 --

from cx_Freeze import setup, Executable

buildOptions = dict( packages = [ "os", "twisted.internet.ssl", "twisted.internet", "cryptography", "OpenSSL", "dbhash", "pycparser" ], include_files = [ ], excludes = ['pdb', 'doctest', 'unittest', 'audio', 'pydoc', 'difflib', 'PIL', 'tcl', 'ttk', 'xmllib', 'xml', 'xmlrpc', 'Tkinter', 'encoding'], includes = ["OpenSSL.crypto"])

gui = Executable('config_chooser.py', targetName = "confidantmail.exe", icon = 'keyicon.ico', base = 'Win32GUI') #) #icon = 'static\logo.ico', base = 'Win32GUI')

setup( name = "Confidant Mail", version = "0.2", description = "Confidant Mail", author = 'Mike Ingle', executables = [gui], options = dict(build_exe = buildOptions))

Then I do:

python setup.py build move "build\exe.win32-2.7\cryptography._Cryptography_cffi_3a414503xe735153f.pyd" "build\exe.win32-2.7_Cryptography_cffi_3a414503xe735153f.pyd" move "build\exe.win32-2.7\cryptography._Cryptography_cffi_590da19fxffc7b1ce.pyd" "build\exe.win32-2.7_Cryptography_cffi_590da19fxffc7b1ce.pyd" move "build\exe.win32-2.7\cryptography._Cryptography_cffi_8f86901cxc1767c5a.pyd" "build\exe.win32-2.7_Cryptography_cffi_8f86901cxc1767c5a.pyd"

That's how I got a working release for Win7-64.

mike805 avatar Feb 20 '15 02:02 mike805

Hi, everyone. I also have this problem when I tried to create a Mac App using py2app. The error message I got is:

UnsupportedAlgorithm: sha256 is not a supported hash in this backend

I found the problem, which is a path finding issue in cffi. py2app packs everything non-binary in a site-packages.zip and the binary files (such as *.so) in the lib-dynload folder. The problem happens in function Verifier._locate_module() in cffi/verifier.py: the path is not correct. In my case, it points to .../.../site-packages.zip/cryptography/.../*.so which is obviously the wrong place.

So I made an easy (and ugly) fix for this as follows:

def _locate_module(self):
    if not os.path.isfile(self.modulefilename):
        if self.ext_package:
            ...
            path = pkg.__path__
        else:
            path = None
        <ADD THIS LINE:> path = [p.replace('site-packages.zip', 'lib-dynload') for p in path]
        filename = self._vengine.find_module(...)

This solves my Mac problem, but I haven't tried it on Windows yet. If you are not sure, what you can do is to print the path value before you feed it to self._vengine.find_module() to see what exactly it is looking for the binaries.

Good luck!

LipuFei avatar Mar 26 '15 17:03 LipuFei

I'm getting the same issue. Is there a version of cx_freeze that works with this package? I've tried all of the workarounds to no avail.

saluk avatar May 29 '15 04:05 saluk

Our cffi 1.0 work is likely to significantly change this situation (not necessarily for the better, but it will definitely be different). We will likely look into this more deeply after our tenth release (4-8 weeks).

reaperhulk avatar May 29 '15 04:05 reaperhulk

closing in favor of the central tracking issue #2039

reaperhulk avatar Jun 18 '15 03:06 reaperhulk