gpt4 book ai didi

python - 在 Android 上执行 Python 脚本时出现 "SSLError: [SSL: BAD_SIGNATURE] bad signature (_ssl.c:726)"异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:26 31 4
gpt4 key购买 nike

我有一个简单的 Python 脚本,它在 Windows 10 上的 Python 2.7 上运行得很好。

however it gives me

"SSLError: [SSL: BAD_SIGNATURE] bad signature (_ssl.c:726)" 

error when I run it from Android.

代码很简单,如下:

import urllib
import urllib2
import ssl
import traceback

#ssl._https_verify_certificates(False)
#ssl._create_default_https_context = ssl._create_unverified_context

try:

url="https://stackoverflow.com/"
https_context = ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE,check_hostname=False)
https_handler = urllib2.HTTPSHandler(context=https_context)
opener = urllib2.build_opener(https_handler)
rep=opener.open(url)
print(rep.read())
except Exception, e:
urllib.urlopen("http://c.seechentech.com/exception?code=484&error=%s&u=%s" % (traceback.format_exc(), url))

经过一番调查,我发现这个异常是在套接字握手期间引发的,但我已经将证书验证设置为False(不需要为我验证证书),为什么它会检查签名?如何解决这个问题?

最佳答案

这个问题已经解决,我想分享我的发现。

发现 1:无论是否设置为验证证书,由 Python Socket 调用的 OpenSSL 套接字将始终在需要 PEM 证书的地方进行 SSL 握手。这一发现回答了为什么我的代码在套接字握手期间失败。

发现 2:Python 能够从 Windows 10 默认位置加载系统受信任的 CA,但是,它在 Android 上失败。这可能是由两个潜在原因引起的:

1) Python 在 Andorid 上找不到默认的 CA 位置/system/etc/security/cacerts/

2) Python 期望 CA 为 PEM 格式,但 Android 信任的 CA 在 ---END CERTIFICAT--- 之后有额外的行,这是 CA 内容结束的标志。

具体是什么原因导致CA证书加载失败我没有验证。我为 Python 提供正确的 CA 数据所做的工作是将从一台 Android 设备提取的所有 CA 文件合并到一个文件中,并使用 load_verify_locations 方法设置为 ssl.SSLContext。

发现 3:与 Windows 上的受信任 CA 相比,Android 上缺少一些受信任的 CA,因此我必须手动将它们添加到我的组合 CA 文件中。

发现 4:Python 文档说“如果你想要客户端和服务器之间的最大兼容性,建议使用 PROTOCOL_SSLv23 作为协议(protocol)版本”,这是不正确的。当我使用这个协议(protocol)版本时,它会导致无效的认证错误。 ssl.PROTOCOL_TLSv1 协议(protocol)版本适用于我的情况。

发现 5:Python 文档说将证书作为“ASCII PEM 字符串”提供,这是不正确的。我们必须提供一个 unicode 字符串。

引用代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
from os import path
import urllib
import urllib2
import ssl

import traceback
import shutil


def loadCA():

concatedCA = u'''
-----BEGIN CERTIFICATE-----
MIIEIDCCAwigAwIBAgIQNE7VVyDV7exJ9C/ON9srbTANBgkqhkiG9w0BAQUFADCBqTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMfQ2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIwMDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNVBAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMDYxMTE3MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCBqTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMfQ2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIwMDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNVBAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCsoPD7gFnUnMekz52hWXMJEEUMDSxuaPFsW0hoSVk3/AszGcJ3f8wQLZU0HObrTQmnHNK4yZc2AreJ1CRfBsDMRJSUjQJib+ta3RGNKJpchJAQeg29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1WZmAT7qSeaiNbz69t2Vjpk1mA42GHWx3d1Qcnyu3HeIzg/3kCDKo2cuH1Z/e+FE6kKVxF0NAVBGFfKBiVlsit2M8RKhjTpCipj4SzR7JzsItG8kO3KdY3RYPBpsP0/HEZrIqPW1N+8QRcZs2eBelSaz662jue5/DJpmNXMyYE7l3YphLG5SEXdoltMYdVEVABt0iN3hxzgEQyjpFv3ZBdRdRydg1vs4O2xyopT4Qhrf7W8GjEXCBgCq5Ojc2bXhc3js9iPc0d1sjhqPpepUfJa3w/5Vjo1JXvxku88+vZbrac2/4EjxYoIQ5QxGV/Iz2tDIY+3GH5QFlkoakdH368+PUq4NCNk+qKBR6cGHdNXJ93SrLlP7u3r7l+L4HyaPs9Kg4DdbKDsx5Q5XLVq4rXmsXiBmGqW5prU5wfWYQ//u+aen/e7KJD2AFsQXj4rBYKEMrltDR5FL1ZoXX/nUh8HCjLfn4g8wGTeGrODcQgPmlKidrv0PJFGUzpII0fxQ8ANAe4hZ7Q7drNJ3gjTcBpUC2JD5Leo31Rpg0Gcg19hCC0Wvgmje3WYkN5AplBlGGSW4gNfL1IYoakRwJiNiqZ+Gb7+6kHDSVneFeO/qJakXzlByjAA6quPbYzSf+AZxAeKCINT+b72x
-----END CERTIFICATE-----
'''

return concatedCA


cadata = loadCA()

try:

url="https://stackoverflow.com/"


https_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
https_context.verify_mode = ssl.CERT_NONE
https_context.check_hostname = False
https_context.load_verify_locations(cadata=cadata)

https_handler = urllib2.HTTPSHandler(debuglevel=1,context=https_context)

redirect_handler = SmartRedirectHandler()

opener = urllib2.build_opener(https_handler,redirect_handler)
rep=opener.open(url)

except Exception, e:
traceback.print_exc()

关于python - 在 Android 上执行 Python 脚本时出现 "SSLError: [SSL: BAD_SIGNATURE] bad signature (_ssl.c:726)"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539683/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com