【问题标题】:How do I remove CT extensions from an x.509 certificate in Python?如何从 Python 中的 x.509 证书中删除 CT 扩展?
【发布时间】:2020-09-22 07:26:03
【问题描述】:

我正在编写一个 Python 脚本,我需要在该脚本中确定预证书和叶证书是否匹配。

为此,我需要在删除 SCT(1.3.6.1.4.1.11129.2.4.2) 和 Precert Poison(1.3.6.1.4.1.11129.2.4.3) 之后比较 precert 和叶子证书的 TBS 证书扩展。

使用python密码学模块,轻松获得TBS证书:

from cryptography import x509
from cryptography.hazmat.backends import default_backend

cert = x509.load_pem_x509_certificate(cert_data_pem, default_backend())

print(cert.tbs_certificate_bytes)

但是我无法弄清楚如何删除这些扩展。看起来 asn1crypto 可以做到,但可用的文档似乎很少。

删除这些扩展的最佳方法是什么?如果可行,我很高兴依赖 openssl,因为我已经在脚本中使用它。

【问题讨论】:

    标签: python x509 asn.1 pyasn1


    【解决方案1】:

    pyasn1 library 最终奏效了。这个 sn-p 解码 TBS 证书并删除两个扩展名,然后重新编码:

    from pyasn1.codec.der.decoder import decode as asn1_decode
    from pyasn1.codec.der.encoder import encode as asn1_encode
    from pyasn1_modules import rfc5280
    from cryptography import x509
    from cryptography.hazmat.backends import default_backend
    
    cert = asn1_decode(x509.load_pem_x509_certificate(cert_data_pem, default_backend()).tbs_certificate_bytes, asn1Spec=rfc5280.TBSCertificate())[0]
    
    newExts = [ext for ext in cert["extensions"] if str(ext["extnID"]) not in ("1.3.6.1.4.1.11129.2.4.2", "1.3.6.1.4.1.11129.2.4.3")]
    cert["extensions"].clear()
    cert["extensions"].extend(newExts)
    
    print(asn1_encode(cert))
    
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 2019-02-22
      • 1970-01-01
      • 2015-05-16
      • 2011-09-21
      相关资源
      最近更新 更多