SSLError: 未知错误 (_ssl.c:2825).在python中验证.cer文件时



我在连接到安全网址时在python中遇到SSL错误。作为快速解决方法,我通过了verify=false,它奏效了。后来我得到了.cer文件,现在文件路径是要验证的。现在我得到

SSL 查询:未知错误 (_ssl.c:2825)

问题出在哪里?

是因为我给了.cer而不是 .pem 吗? 我可以将.cer转换为 .pem 文件吗?

如何解决这个问题?

本质上,是的 - 这个"未知错误"是具有.cer文件而不是.pem文件的结果。我刚刚遇到了这个问题,行号不同(_ssl.c:4025左右),但症状相同,并使用.pem证书文件代替修复它。

由HUB和Marcel Friedmann在服务器故障上提供,以下是在这些格式之间转换证书的方法:

  • 使用记事本++或类似文件打开证书文件。如果它以-----BEGIN CERTIFICATE-----开头,它已经采用正确的格式 - 只需将其重命名为.pem.

  • 否则,如果您安装了OpenSSL,这是一件相当容易的事情:

    openssl x509 -inform der -in certificate.cer -out certificate.pem
    
  • 如果你没有安装OpenSSL,但有Java的keytool,你可以使用它,但它有点复杂。

    首先,找到一个不介意使用密钥库。如果没有,请创建一个:

    # create a dummy certificate in the file test.keystore, forcing the keystore to be created
    keytool -genkey -alias test -keystore test.keystore
    # and now delete the cert
    keytool -delete -alias test -keystore test.keystore
    # the (empty) keystore will still exist
    

    然后,导入.cer格式证书:

    keytool -import -trustcacerts -alias test -file certificate.cer -keystore test.keystore 
    

    最后,将其导出为.pem(确保它的实际格式正确):

    keytool -exportcert -alias test -file certificate.pem -rfc -keystore test.keystore
    
  • 如果您没有OpenSSL或Javakeytool,则需要安装其中一个,或者找到其他方法。

最新更新