如何从BaseHTTPRequestHandler python获取x509.Certificate

时间:2019-05-31 16:54:02

标签: python ssl basehttprequesthandler

我正在使用this example通过python 2.7.5设置HTTPS / TLS服务器。但是,我尝试使用自定义类'CustomHandler',该类扩展了BaseHTTPRequestHandler以在一个python应用程序中支持HTML和Rest API。我一直牢记在BaseHTTPRequestHandler中调用什么方法来获取用户/客户端证书作为x509.Certificate对象。最终,我希望从python的SSLSocket对象获得完整的用户/客户端DN。

我发现self.connection.getpeercert()可以将字典对象返回为documented here

obj.Dump(obj.GetType())

这是我用来尝试运行此代码的代码。

{'issuer': ((('countryName', 'IL'),),
            (('organizationName', 'StartCom Ltd.'),),
            (('organizationalUnitName',
              'Secure Digital Certificate Signing'),),
            (('commonName',
              'StartCom Class 2 Primary Intermediate Server CA'),)),
 'notAfter': 'Nov 22 08:15:19 2013 GMT',
 'notBefore': 'Nov 21 03:09:52 2011 GMT',
 'serialNumber': '95F0',
 'subject': ((('description', '571208-SLe257oHY9fVQ07Z'),),
             (('countryName', 'US'),),
             (('stateOrProvinceName', 'California'),),
             (('localityName', 'San Francisco'),),
             (('organizationName', 'Electronic Frontier Foundation, Inc.'),),
             (('commonName', '*.eff.org'),),
             (('emailAddress', 'hostmaster@eff.org'),)),
 'subjectAltName': (('DNS', '*.eff.org'), ('DNS', 'eff.org')),
 'version': 3}

我希望self.connection.getpeercert()返回一个x509.Certificate对象与一个字典对象。我宁愿使用已经存在的类,也不愿编写自定义代码来解析字典。

已更新:研究发现链接对我有帮助...

1 个答案:

答案 0 :(得分:0)

您可以尝试PyOpenSSL。

x509_cert = OpenSSL.crypto.load_certificate(
    OpenSSL.crypto.FILETYPE_ASN1, self.connection.getpeercert(True)
)