【问题标题】:PyOpenSSL: Get a CRL's last update and next update fieldsPyOpenSSL:获取 CRL 的上次更新和下次更新字段
【发布时间】:2012-12-04 01:59:51
【问题描述】:

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

有什么关于约会的建议吗?

【问题讨论】:

    标签: python openssl pyopenssl


    【解决方案1】:

    使用 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 日期。

    【讨论】:

    • 显然在 OS X 上安装 PyCrypto 需要太多的麻烦,所以我无法测试代码,但我认为它可以工作。但遗憾的是,目前仍然最容易坚持在服务器上调用“openssl crl ...”命令并使用该命令的输出。 OpenSSL 将始终存在于我们的环境中,因此不值得将其替换为非标准库(在我们的例子中)。
    • 或者:我在标准安装 Ubuntu 12.04 服务器时是否包含 PyCrypto?
    • @JoelL,PyCrypto 可以通过 MacPorts 轻松安装在 OS X 上(例如,如果您将 MacPorts 与 python27 一起使用,只需运行sudo port install py27-crypto),也可以在 Ubuntu 上使用 (apt-get install python-crypto)跨度>
    【解决方案2】:

    看来get_rev_date() 会将该日期返回为ASN1 GENERALIZEDTIME

    我在 pyOpenSSL 的 documentation 上找到了这个。

    【讨论】:

    • 这将返回列表中单个证书的吊销日期,而不是 CRL nextUpdate/lastUpdate 字段。
    【解决方案3】:

    该功能目前不存在。 我们最终不得不扩展 pyOpenSSL 来处理这个问题。

    【讨论】:

    • 您是否打算将您的此功能实现贡献给 pyOpenSSL 项目?如果是这样,也许 OP 想审查/测试您的代码。作为参考,这里是 get_rev_date 的实现
    • (我最终通过 python 使用了 openssl 命令(“openssl crl -in FILENAME.crl -noout -nextupdate”),并解析输出以获得 nextUpdate 日期时间值。不是最优雅的解决方案…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2020-01-06
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多