Python (pip) throwing [SSL: CERTIFICATE_VERIFY_FAILED] even if certificate chain updated

江枫思渺然 提交于 2020-01-01 06:36:14

问题


This is a followup to a previous SO post.

I am using Windows/cygwin and I have the need for python to understand a custom CA certificate, as the network infrastructure resigns all SSL requests with its own certificate.

If I try to run pip search SimpleHTTPServer, I get the following error message:

...
  File "c:\users\erbe\appdata\local\programs\python\python35-32\lib\ssl.py", line 633, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

I have tried to add the certificates to my list of trusted certificates by doing the following:

  1. Copy my .pem file to /etc/pki/ca-trust/source/anchors
  2. update-ca-trust extract

I have verified that this works as I can now point to the generated PEM file and run pip successfully: pip --cert /usr/local/ssl/cert.pem search SimpleHTTPServer:

$ pip --cert tls-ca-bundle.pem search SimpleHTTPServer
ComplexHTTPServer (0.1)      - A Multithreaded Python SimpleHTTPServer
SimpleTornadoServer (1.0)    - better SimpleHTTPServer using tornado
rangehttpserver (1.2.0)      - SimpleHTTPServer with support for Range requests

However, I want this to work without having to specify the certificate manually every time. I am hoping to update the certificate chain that python uses:

$ python -c "import ssl; print(ssl.get_default_verify_paths())"
DefaultVerifyPaths(cafile=None, capath=None, openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/usr/local/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/usr/local/ssl/certs')

I have verified that through a series of symlinks, that /usr/local/ssl/cert.pem points to the same file. However, if I execute pip, I still get the [SSL: CERTIFICATE_VERIFY_FAILED] error message.

I uninstalled the Windows version of python, and reinstalled the Cygwin version of python. With it, I ran easy_install-2.7 pip. Now at least I am able to execute pip with the full certificate path without an error message:

$ pip --cert /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem search simpleHttpServer
LittleHTTPServer (0.5.0)     - Little bit extended SimpleHTTPServer
SimpleHTTP404Server (0.2.0)  - A Python SimpleHTTPServer, but serves 404.html if a page is not found.
django-localsrv (0.1.2)      - Django app for serving static content from different sources (files, strings, urls, etc.) at custom paths,

Just to be safe, I also tried updating the SSL_CERT_DIR varaible to point to /etc/pki/ca-trust-extracted/pem and set the SSL_CERT_FILE to /etc/pki/ca-trust-extracted/pem/tls-ca-bundle.pem but these do not work:

$ set | grep SSL
SSL_CERT_DIR=/etc/pki/ca-trust/extracted/pem
SSL_CERT_FILE=/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem

$ python -c "import ssl; print(ssl.get_default_verify_paths())"
DefaultVerifyPaths(cafile='/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem', capath='/etc/pki/ca-trust/extracted/pem', openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/usr/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/usr/ssl/certs')


$ pip search simpleHttpServer
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  ...
  ...
  File "/usr/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg/pip/_vendor/requests/adapters.py", line 477, in send
    raise SSLError(e, request=request)
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

What am I doing wrong? Is this a cygwin vs Windows problem? Which PEM files do I need to update?


回答1:


You can add pip command line option defaults to its configuration file. In windows, it should be located under %APPDATA%\pip\pip.ini.

To add a certificate, put the following lines in the file:

[global]
cert = windows path to your certificate


来源:https://stackoverflow.com/questions/39626142/python-pip-throwing-ssl-certificate-verify-failed-even-if-certificate-chain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!