python - PyOpenSSL:获取 CRL 的上次更新和下一次更新字段

标签 python openssl pyopenssl

我正在尝试使用 PyOpenSSL 获取 CRL 的日期。 CRL 类不包含它们作为可访问的成员。我正在浏览所有下划线成员,但我宁愿不使用其中一个,因为它们不应该是“公开的”。

关于确定日期有什么建议吗?

最佳答案

你不能用 pyOpenSSL 做到这一点,但实际上可以使用 PyCrypto 的 asn1 解析器从 CRL 中提取这些信息,没有太多问题。请参见下面的示例:

import types
from Crypto.Util import asn1
import datetime as dt
from pytz import UTC

def decode_time(obj, format):
    return dt.datetime.strptime(obj.payload, format).replace(tzinfo=UTC)

time_formats = {
    23: lambda(obj): decode_time(obj, "%y%m%d%H%M%SZ"),
    24: lambda(obj): decode_time(obj, "%Y%m%d%H%M%SZ"),
    }

def crl_dates(crl_der):
    crl_seq = asn1.DerSequence()
    crl_seq.decode(crl_der)
    if len(crl_seq) != 3: raise ValueError("unknown crl format")
    tbsCertList = asn1.DerSequence()
    tbsCertList.decode(crl_seq[0])
    thisUpdate = asn1.DerObject()
    nextUpdate = asn1.DerObject()
    if isinstance(tbsCertList[0], types.StringTypes): # CRL v1
        thisUpdate.decode(tbsCertList[2])
        nextUpdate.decode(tbsCertList[3])
    else:
        if tbsCertList[0] > 1: raise ValueError("unsupported CRL profile version: %d" % tbsCertList[0])
        thisUpdate.decode(tbsCertList[3])
        nextUpdate.decode(tbsCertList[4])
    if thisUpdate.typeTag not in time_formats or \
       nextUpdate.typeTag not in time_formats:
        raise ValueError("invalid CRL date/time fields")
    return time_formats[thisUpdate.typeTag](thisUpdate), \
           time_formats[nextUpdate.typeTag](nextUpdate)

if __name__ == '__main__':
    from urllib2 import urlopen
    print "CRL v1", crl_dates(urlopen("http://crl.verisign.com/pca1.1.1.crl").read())
    print "CRL v2", crl_dates(urlopen("http://www.gstatic.com/GoogleInternetAuthority/GoogleInternetAuthority.crl").read())

注意:此代码不检查任何签名或类似内容,仅提取 CRL 日期。

关于python - PyOpenSSL:获取 CRL 的上次更新和下一次更新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694984/

相关文章:

python - python中寻址属性的风格

python - 将 Cython 中的 numpy 数组传递给需要动态分配数组的 C 函数

python - 计算字符串出现次数的最快方法

windows - openssl s_server 和 gethostbyname 失败

encryption - openssl RSA_public_encrypt() 返回的大小总是 = RSA_size(rsa)?

python - 尝试在 Python 中写入文件时收到 TypeError

c++ - base64 解码 C++ 失败行尾字节

python - 如何从 x509 证书中提取签名

ssl - Scrapy 无法连接到仅支持旧版 TLSv1 的 HTTPS 站点。连接丢失

python - pyOpenSSL 密码列表失败