python - 如何在扭曲的 SSL 客户端中验证 SSL 服务器证书

标签 python ssl ssl-certificate twisted

如何在我的扭曲 SSL 客户端中验证 SSL 服务器证书?

我是 SSL 的新手,我已经完成了 twisted SSL tutorials但我仍然不清楚某些事情。

我的查询是:

  • 我应该如何使用 twisted.internet.ssl 模块验证 SSL 服务器证书,

  • ssl.ClientContextFactory.getContext 方法在处理 SSL 时如何发挥作用,

  • 如何告知扭曲的 SSL 客户端有关公钥文件的位置?

最佳答案

自 Twisted 14.0 以来,optionsForClientTLS 是执行此操作的最佳方式:

from twisted.internet.ssl import optionsForClientTLS
from twisted.internet.endpoints import SSL4ClientEndpoint

ctx = optionsForClientTLS(hostname=u"example.com")
endpoint = SSL4ClientEndpoint(reactor, host, port, ctx)
factory = ...
d = endpoint.connect(factory)
...

optionsForClientTLS 也接受其他(可选)参数,这些参数也可能有用。

在 Twisted 14.0 之前,这个过程有点复杂:

建立连接并成功完成 SSL 握手后(这意味着证书当前根据其 notBeforenotAfter 值有效并且由您指定的证书颁发机构证书是可信的)您可以从传输中获取证书:

certificate = self.transport.getPeerCertificate()

证书表示为 pyOpenSSL X509 实例。它有一个方法可以用来检索证书的主题名称:

subject_name = certificate.get_subject()

主题名称是一个专有名称,表示为 pyOpenSSL X509Name 实例。您可以检查其字段:

common_name = subject_name.commonName

这是一个字符串,例如 "example.com"

如果您需要检查 subjectAltName(您很可能会这样做),那么您可以在证书的扩展中找到此信息:

extensions = certificate.get_extensions()

这是 pyOpenSSL X509Extension 实例的列表。您必须解析每一个以找到 subjectAltName 及其值。

关于python - 如何在扭曲的 SSL 客户端中验证 SSL 服务器证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15026559/

相关文章:

ssl - HAProxy 负载均衡器 : Peer's Certificate issuer is not recognized

ajax - 如果数据实际上安全地传递到服务器,网站是否需要 SSL?

tomcat - 即使密码错误,信任库也能正常工作,为什么?

apache - 如何在 Amazon Web Services/Apache 上更新 SSL 证书

python pandas 分组依据和聚合列

python - 无法将 Python 代码发布到网站 rosalind.info

python - 与 plotly 的行顺序

python - 将 pyspark 数据框中的两列转换为一个 python 字典

asp.net-mvc-3 - 你如何在 Rackspace 云站点中使用 [RequireHttps]

ssl - 如何将https设置为默认?