SSLSocket.getpeercert returns none if cert isn't valid
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
It's possible I'm misunderstanding, but it seems the documentation around SSLSocket.getpeercert is incorrect and/or the function is bugged. This was run on Fedora 40 with python 3.12.5. I'm trying to bypass DNS and pull the SSL cert straight from an IP for a specific set of Hosts.
the docs I'm referencing: https://docs.python.org/3/library/ssl.html#ssl.SSLSocket.getpeercert
Site and HostIP are variables representing a site URL and a Apache server's direct IP address. This is inside a function call.
the code:
#empty cert info dict
SiteCertInfo = {}
#create SSL context and socket
#this overrides "DNS" so we can pull the SSL cert as it is on the server
sslContext = ssl.create_default_context()
sslSock = socket.socket()
sslConn = sslContext.wrap_socket(sslSock, server_hostname=Site)
#try the connection
try:
sslConn.connect((HostIp, 443))
#ssl cert can't be verified / doesn't match
except ssl.SSLCertVerificationError as ex:
#show error reasons
print(Site + " site SSL did not verify. reason: " + ex.verify_message, file=sys.stderr)
#continue
pass
#pull the cert from the connection
sslCert = sslConn.getpeercert()
The error:
sslCert = sslConn.getpeercert()
^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.12/ssl.py", line 1129, in getpeercert
return self._sslobj.getpeercert(binary_form)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'getpeercert'
The issue:
The documentation states "for a client SSL socket, the server will always provide a certificate, regardless of whether validation was required;" I'm reading that as my python "client" connecting to the remote server should always get a cert even if it fails validation.
However, if the certificate is expired I'm catching the error and continuing on via the pass call. The sslConn.getpeercert() call fails as sslConn is None. If I don't catch the error then it just fails as normal.
If I try and by-pass validation by doing:
sslContext = ssl.SSLContext(protocol = ssl.PROTOCOL_TLS_CLIENT)
sslContext.check_hostname = False
sslContext.verify_mode = ssl.CERT_NONE
sslContext.set_default_verify_paths()
This just fails and pulls a "empty" dict for sslCert. If I set verify_mode back to CERT_REQUIRED I'm back to square one.
So, there doesn't seem to be a way to "catch" the validation error, but still continue to load the cert if the cert is expired or to disable validation via CERT_NONE and still pull the cert data and manually "validate" it.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先从 ssl.SSLSocket.getpeercert 的文档行为和 SSLContext 的验证设置入手,然后使用过期证书和 CERT_NONE 重现报告中的连接流程。将 SSLCertVerificationError 之后的 socket 状态与 getpeercert(binary_form=True) 的结果进行比较。完成的标准是确定该行为属于实现错误还是文档问题,并添加相应的回归测试或文档说明。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- networking, security
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100