Python使用系统SSL证书?

2024-05-13 12:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

上周我遇到了最近的Authorize.net SSL证书失效debacle

我终于让curl接受了他们的证书:

$ curl -Iv https://secure.authorize.net
...
*  SSL certificate verify ok.
...

但python仍然拒绝了它的请求:

>>> requests.get('https://secure.authorize.net', verify=True)
...
  InsecurePlatformWarning

在我的代码中:

File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake
    self._sslobj.do_handshake()
SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

有人能告诉我为什么python似乎没有使用系统证书进行验证吗?有什么解决办法吗?

编辑

我正在使用Ubuntu并以这种方式安装证书:

sudo curl -o /usr/local/share/ca-certificates/entrust_ssl_ca.crt https://www.entrust.net/downloads/binary/entrust_ssl_ca.cer
sudo update-ca-certificates

运行之后,curl工作正常,但是python仍然无法识别证书。


Tags: httpssslnetusrcertificatecurldoca
1条回答
网友
1楼 · 发布于 2024-05-13 12:21:27

你没有提到你在使用什么操作系统,或者你在哪里安装了证书,以使它们可以用于Curl。

我在系统上使用strace查看Python在哪里寻找证书。在我的Fedora系统上,Python正在使用/etc/pki/tls/certs/ca-bundle.crt,这是Fedora、Red Hat和类似系统上的标准位置。

在Ubuntu上,Python正在寻找/etc/ssl/certs/ca-certificates.crt

根据the documentation

You can pass verify the path to a CA_BUNDLE file with certificates of trusted CAs. This list of trusted CAs can also be specified through the REQUESTS_CA_BUNDLE environment variable.

…因此您可以向应用程序提供一个CA证书列表,该列表独立于系统上安装的内容。

更新

运行openssl s_client -showcerts -connect secure.authorize.net:443表明*.authorize.net证书由“委托证书颁发机构-L1K”证书签名,该证书由“委托根证书颁发机构-G2”证书签名,该证书由“委托根证书颁发机构”证书签名。您作为entrust_ssl_ca.crt安装的证书是“委托.net安全服务器证书颁发机构”,它是“以上都不是”。

我只想访问http://www.entrust.com/get-support/ssl-certificate-support/root-certificate-downloads/并下载所有内容,但上面链中的顶级证书是this one。这是下载页面上列出的第二个证书。

相关问题 更多 >