如果SSL实验室显示“此服务器的证书链不完整”,则证书握手是否可以完成。等级上限为B?

2024-04-28 12:16:34 发布

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

我在试着用urllib.request.urlopen在以“https”开头的网站上。错误输出为: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

有很多很好的线程可以覆盖这个错误。包括提到SSL实验室等级的this one。我可以使用urllib.request.urlopen在我测试过的每一个“https”网站上。在

SSL实验室显示以下输出:

Key                        RSA 2048 bits (e 65537)
Issuer                     Let's Encrypt Authority X3
AIA:                       http://cert.int-x3.letsencrypt.org/
Signature algorithm        SHA256withRSA
Extended Validation        No
Certificate Transparency   No
OCSP Must Staple           No
Revocation information     OCSP 
Revocation status          Good (not revoked)
DNS CAA                    No (more info)
Trusted                    Yes

为了澄清,我的问题是:有没有解决方案来完成握手,其中不包括绕过证书验证?如果有一个解决方案,它能完全在linux、macOS和Windows上的python脚本中解决吗?在


Tags: nohttpsssl网站request错误certificateurllib
2条回答

我能够解决这个问题(对于基于debian的系统,我运行的是debian 9)。我仍然需要在macOS和Windows上测试解决方案。在

SSL Labs报告的“Certification Paths”标题下,它显示:

1   Sent by server  www.exampleSITE.com

BLAH BLAH BLAH

2   Extra download  Let's Encrypt Authority X3

BLAH BLAH BLAH

3   In trust store  DST Root CA X3   Self-signed

BLAH BLAH BLAH

我导航到/etc/ssl/certs/,注意到没有Let's Encrypt证书。然后我下载了.pem并重新编译。在

^{pr2}$

我刚才测试了python的一行

page = urllib.request.urlopen('https://www.exampleSITE.com').read()

它成功地检索到了页面。在

您可以通过将丢失的中间证书添加到活动的X509Store来解决此问题:

cert_text = '''
  -BEGIN CERTIFICATE  -
...put your actual certificate text here...
  -END CERTIFICATE  -
'''

# fill this out depending on which specific intermediate cert you're missing
missing_cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert_text)

context = ssl.create_default_context() # load default trusted certificates
store = context.get_cert_store()       # get the X509Store for that context
store.add_cert(missing_cert)           # add your missing cert to it

urllib.request.urlopen(site, context=context)

请注意,如果您只需要与正在为其执行此操作的服务器通信,则可以将适当的cafile或{}参数传递给create_default_context()。在

相关问题 更多 >