Search code examples
pythonrsam2cryptopython-venvfalconframework

Falcon python M2Crypto RSA Error


We have recently developed an API that uses RSA, the main problem is that in console it works correctly but once entering the "venv" of the application it show different errors.

Traceback (most recent call last):


File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/root/merci_api/venv/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
  File "/root/merci_api/main.py", line 12, in <module>
    import transaction_commit
  File "/root/merci_api/transaction_commit.py", line 9, in <module>
    import cfdi
  File "/root/merci_api/cfdi.py", line 15, in <module>
    from M2Crypto import RSA
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/__init__.py", line 26, in <module>
    from M2Crypto import (ASN1, AuthCookie, BIO, BN, DH, DSA, EVP, Engine, Err,
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/SSL/__init__.py", line 24, in <module>
    from M2Crypto.SSL.SSLServer import (ForkingSSLServer, SSLServer,
  File "/root/merci_api/venv/lib/python3.5/site-packages/M2Crypto-0.25.1-py3.5-linux-x86_64.egg/M2Crypto/SSL/SSLServer.py", line 12, in <module>
    from SocketServer import BaseServer, ForkingMixIn, TCPServer, ThreadingMixIn
ImportError: No module named 'SocketServer'

M2Crypto libraries were installed in both ubuntu and pip.

The code that use RSA is:

def sella_xml(cfdi, numero_certificado, archivo_cer, archivo_pem):
    keys = RSA.load_key(archivo_pem)
    cert_file = open(archivo_cer, 'r')
    cert = base64.b64encode(cert_file.read())
    xdoc = ET.fromstring(cfdi)
    xsl_root = ET.parse('cadenaoriginal_3_3.xslt')
    xsl = ET.XSLT(xsl_root)
    cadena_original = xsl(xdoc)
    digest = hashlib.new('sha256', str(cadena_original)).digest()
    sello = base64.b64encode(keys.sign(digest, "sha256"))
    comp = xdoc.get('Comprobante')
    xdoc.attrib['Sello'] = sello
    xdoc.attrib['Certificado'] = cert
    return ET.tostring(xdoc)

Command for Run Env:

 /root/merci_api/venv/bin/gunicorn --workers 3 -b localhost:5000 main:app

Any advice?

What am i doing wrong?


Solution

  • Well, the issue was that M2Crypto on the version that we have installed wasn't compatible with Python 3.*, so when we use another M2Crypto version we have another issue "SSL v2" to resolve that we need this.

    Unless you use python3 branch M2Crypto still doesn’t support py3k.