首页 文章

如何验证双绞线SSL客户端中的SSL服务器证书

提问于
浏览
3

如何在我的双绞线SSL客户端中验证SSL服务器证书?

我是SSL的初学者,我已经完成了twisted SSL tutorials,但我仍然不清楚某些事情 .

我的疑问是:

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

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

  • 如何告知双向SSL客户端公钥文件的位置?

1 回答

  • 4

    自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之前,该过程涉及更多:

    Build 连接并且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 及其值 .

相关问题