10

So I'm trying to add SSL to my web server running on my pi B+ (using cherrypy)

I made sure that OpenSSL was installed with:

sudo apt-get install openssl

Then I installed pyOpenSSL with:

sudo apt-get install python-openssl

In both cases it says:

openssl is already the newest version.
python-openssl is already the newest version.

Because I've run the command more than once. So this seems fine.

By then in python when I do:

import OpenSSL

I get errors. Specifically:

Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenSSL
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
    from OpenSSL import rand, crypto, SSL 
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/rand.py", line 11, in <module>
    from OpenSSL._util import (
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/_util.py", line 3, in <module>
    from cryptography.hazmat.bindings.openssl.binding import Binding
ImportError: No module named cryptography.hazmat.bindings.openssl.binding

Do I have a compatibility issue here? Are the two latest versions incompatible with each other? Anyone seen this before?

The version info I have:

% openssl version
OpenSSL 1.0.1e 11 Feb 2013

And for python-openssl I have:

% dpkg -s python-openssl
Package: python-openssl
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 532
Maintainer: Debian Python Modules Team <[email protected]>
Architecture: armhf
Source: pyopenssl
Version: 0.13-2+rpi1+deb7u1
Depends: python (<< 2.8), python (>= 2.6), python-support (>= 0.90.0), libc6 (>= 2.13-28), libssl1.0.0 (>= 1.0.0)
Suggests: python-openssl-doc, python-openssl-dbg
Description: Python 2 wrapper around the OpenSSL library
High-level wrapper around a subset of the OpenSSL library, includes
 .
   * SSL.Connection objects, wrapping the methods of Python's portable
     sockets
   * Callbacks written in Python
   * Extensive error-handling mechanism, mirroring OpenSSL's error
     codes
 .
A lot of the object methods do nothing more than calling a
corresponding function in the OpenSSL library.
Homepage: http://launchpad.net/pyopenssl

When using the -v flag on python I get:

>>> import OpenSSL
import OpenSSL # directory /usr/local/lib/python2.7/dist-packages/OpenSSL
# /usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.pyc matches /usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py
import OpenSSL # precompiled from /usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.pyc
# /usr/local/lib/python2.7/dist-packages/OpenSSL/rand.pyc matches /usr/local/lib/python2.7/dist-packages/OpenSSL/rand.py
import OpenSSL.rand # precompiled from /usr/local/lib/python2.7/dist-packages/OpenSSL/rand.pyc
# /usr/lib/python2.7/functools.pyc matches /usr/lib/python2.7/functools.py
import functools # precompiled from /usr/lib/python2.7/functools.pyc
import _functools # builtin
# /usr/local/lib/python2.7/dist-packages/six.pyc matches /usr/local/lib/python2.7/dist-packages/six.py
import six # precompiled from /usr/local/lib/python2.7/dist-packages/six.pyc
# /usr/lib/python2.7/__future__.pyc matches /usr/lib/python2.7/__future__.py
import __future__ # precompiled from /usr/lib/python2.7/__future__.pyc
import itertools # builtin
import operator # builtin
# /usr/lib/python2.7/StringIO.pyc matches /usr/lib/python2.7/StringIO.py
import StringIO # precompiled from /usr/lib/python2.7/StringIO.pyc
# /usr/local/lib/python2.7/dist-packages/OpenSSL/_util.pyc matches /usr/local/lib/python2.7/dist-packages/OpenSSL/_util.py
import OpenSSL._util # precompiled from /usr/local/lib/python2.7/dist-packages/OpenSSL/_util.pyc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/rand.py", line 11, in <module>
from OpenSSL._util import (
  File "/usr/local/lib/python2.7/dist-packages/OpenSSL/_util.py", line 3, in <module>
from cryptography.hazmat.bindings.openssl.binding import Binding
ImportError: No module named cryptography.hazmat.bindings.openssl.binding
3
  • if none of the other answers work give this a try pip install cryptography Mar 1, 2015 at 2:03
  • Such a simple approach, who would have thought it worked. Thanks @SteveRobillard you solved this issue. I had to also install the libffi libraries needed by cryptography (sudo pip install libffi-dev). But then I was able to install cryptograpy, and now I can import OpenSSL without error. It takes a long time to import it, but it does not error. I'm a bit new here, can we make your comment an answer?
    – jrel
    Mar 1, 2015 at 5:01
  • Done, I included your comment regarding libffi-dev for completeness. Mar 1, 2015 at 5:29

6 Answers 6

8

You can satisfy the missing dependency by doing the following:

install the libffi libraries needed by cryptography

sudo pip install libffi-dev 

or

sudo apt-get install libffi-dev

then install cryptography:

pip install cryptography
12
  • 1
    I reckon using pip was the cause of the error. It wouldn't surprise me if pip was used to install the Python openssl module. That may have screwed up the Debian packaging system.
    – joan
    Mar 1, 2015 at 9:50
  • @joan thanks I was curious why my suggestion worked and two of you reported it working normally. Mar 1, 2015 at 9:56
  • I've just read a similar tale about the Python spidev module. The SPI I/F has changed so everyone needs to upgrade. Some people have no problem, others just can't get it to work. A chap just posted that doing a pip uninstall (he originally installed with pip) magically made the module work.
    – joan
    Mar 1, 2015 at 10:06
  • @joan, I found something similar (regarding incompatible requirements after upgrading packages) doing a search on one of the error lines from the OP's question, that is where I got the idea about pip install. Mar 1, 2015 at 12:23
  • @monojohnny it does work. Mar 1, 2015 at 17:10
2

I needed to reinstall the packages:

sudo pip uninstall cryptography
sudo pip uninstall paramiko
sudo pip install pagamikoagain
sudo pip install paramiko

then it started to work for me.

1

I just tried this on a B+ ; seems to work for me:

user@beeplus ~ $ openssl version
OpenSSL 1.0.1e 11 Feb 2013

Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenSSL
>>>

I didn't run an 'apt-get install openssl' though - just one for 'python-openssl'. My Python version appears to be identical to yours : what version of openssl is reported back to you ?

The stack trace you report (at least the last few lines) appears else where on the web. For instance: https://mail.python.org/pipermail/python-bugs-list/2014-March/235055.html - no response on there though :-(

Maybe try the imports that apparently cause an error, one at a time like this - these work on my system:

Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from OpenSSL import rand
>>> from OpenSSL import crypto
>>>

However I tried the other import that is mentioned last in the stack, and this DOES error when I try and directly import it:

>>> from cryptography.hazmat.bindings.openssl.binding import Binding
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cryptography.hazmat.bindings.openssl.binding

Additionally: this StackOverflow link appears to be about the same thing (in a different context) - and has a solution which apparently worked: https://stackoverflow.com/questions/24338840/installing-package-dependencies-for-scrapy

Taking the approach from this StackOverFlow post : https://stackoverflow.com/questions/7332299/trace-python-imports

Try running python with a '-v' flag:

I get the following for comparision:

>>> import OpenSSL
import OpenSSL # directory /usr/lib/pymodules/python2.7/OpenSSL
# /usr/lib/pymodules/python2.7/OpenSSL/__init__.pyc matches /usr/lib/pymodules/python2.7/OpenSSL/__init__.py
import OpenSSL # precompiled from /usr/lib/pymodules/python2.7/OpenSSL/__init__.pyc
# /usr/lib/python2.7/plat-linux2/DLFCN.pyc matches /usr/lib/python2.7/plat-linux2/DLFCN.py
import DLFCN # precompiled from /usr/lib/python2.7/plat-linux2/DLFCN.pyc
dlopen("/usr/lib/pymodules/python2.7/OpenSSL/crypto.so", 102);
import OpenSSL.crypto # dynamically loaded from /usr/lib/pymodules/python2.7/OpenSSL/crypto.so
dlopen("/usr/lib/pymodules/python2.7/OpenSSL/rand.so", 2);
import OpenSSL.rand # dynamically loaded from /usr/lib/pymodules/python2.7/OpenSSL/rand.so
dlopen("/usr/lib/pymodules/python2.7/OpenSSL/SSL.so", 2);
import OpenSSL.SSL # dynamically loaded from /usr/lib/pymodules/python2.7/OpenSSL/SSL.so
# /usr/lib/pymodules/python2.7/OpenSSL/version.pyc matches /usr/lib/pymodules/python2.7/OpenSSL/version.py
import OpenSSL.version # precompiled from /usr/lib/pymodules/python2.7/OpenSSL/version.pyc

And here's the SHA1 of the files being refered to here:

user@beeplus /usr/lib/pymodules/python2.7/OpenSSL $ shasum *
46ef76e58b652dc3a604299a9a7af1e46f6b2d0b  crypto.so
775a319724acbfbb10bd708e35178fa72d9afcc1  __init__.py
b69a3f5e97fc540bc05eb1f25d115d2dff14327b  __init__.pyc
0c5742e81de0d00c3d135bf287aa057052a592f2  rand.so
f5c3532c6f5bef71f664b17be07e506152d7368c  SSL.so
shasum: test:
77e453b9076e9d17f0234097b737b87f08182a29  tsafe.py
aa605271b940aba6f538c0810ad616f5948eb868  tsafe.pyc
b1d283eb33e173e10703cac2140718efa88929e4  version.py
696e2c15eba578b0f305f1822a080379e4d26279  version.pyc

Maybe the *.so files are missing/wrong on your system ?

EDIT: it looks like my environment is loading from a different directory structure from yours.

Is this a 'virtual env' : https://virtualenv.pypa.io/en/latest/ ? If so, that might account for the difference ? I don't really have a good understanding of how Python manages it's libraries to be honest - you'll need a Python expert to ship in !

Additionally (to answer your comment about SHA1) - I can't remember if I had to install a particular package to get the 'shasum' tool - it's a Perl Script apparently. But anything that can generate a checksum (or even at a push just do an 'ls -l') will do I guess.

One final bit of information, in case its helps anyone narrow down the issue

$ uname -a
Linux beeplus 3.18.8+ #761 PREEMPT Fri Feb 27 15:43:30 GMT 2015 armv6l GNU/Linux
14
  • Added my version info to the description. I do have the same openssl as you.
    – jrel
    Feb 28, 2015 at 22:40
  • And yes, searching on the error does produce some results, but none of them with a resolution.
    – jrel
    Feb 28, 2015 at 22:41
  • 1
    The import for OpenSSL seems to trigger a sequence of other imports in the library itself - maybe try importing them one-at-time, to narrow down the error ? I'll edit my post to show what I mean.
    – monojohnny
    Feb 28, 2015 at 22:43
  • Yes, looks like you are getting to the crux of it. But now what?
    – jrel
    Feb 28, 2015 at 22:47
  • On the guess that this might be a Python packaging problem - maybe cross-post with a Python-tagged post on the main StackOverFlow site - and see if some Python experts can help out.
    – monojohnny
    Feb 28, 2015 at 22:50
1

I have just had a very similar issue on a Pi(B).

import OpenSSL was resulting in exactly the same erroneous response. Running pip list showed pyOpenSSL as v 0.14.

After exhausting all other ideas I removed pyOpenSSL using sudo pip uninstall pyOpenSSL

pip list then showed pyOpenSSL as v0.13. I did sudo pip uninstall pyOpenSSL 2 or 3 more times but pip list still shows pyOpenSSL (0.13)

Then I found that the import OpenSSL did not show an issue as before. Also initial issue I had been troubleshooting magically disappeared.

Hope this helps.

0

No problem on a Pi2B.

paul /ram $ sudo apt-get install openssl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
openssl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
paul /ram $ sudo apt-get install python-openssl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-openssl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
paul /ram $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import OpenSSL
>>> 
paul /ram $ pydoc OpenSSL

Help on package OpenSSL:

NAME
    OpenSSL - pyOpenSSL - A simple wrapper around the OpenSSL library

FILE
    /usr/lib/pymodules/python2.7/OpenSSL/__init__.py

PACKAGE CONTENTS
    SSL
    crypto
    rand
    test (package)
    tsafe
    version

DATA
    __all__ = ['rand', 'crypto', 'SSL', 'tsafe', '__version__']
    __version__ = '0.13'

VERSION
    0.13
:
0

This worked for me!

rm -rf /usr/local/lib/python2.7/dist-packages/fabric/fabric-home-assistant

sudo apt-get install libffi-dev libssl-dev

sudo pip install cryptography --force-reinstall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.