Python打印所有public密钥证书信息(X.509格式)

Python print all public key certificate information (X.509 Format)

我正在尝试编写一个 Python3 程序,它将向我显示 public 密钥证书中包含的所有信息,类似于以下 linux 命令:

openssl x509 -in website.com.pem -text

这将 return 类似于

的结果
Certificate:
Data:
    Version: 3 (0x2)
    Serial Number:
        04:7a:f7:95:47:c0:7d:0f:ef:80:a5:b2:1f:51:e3:63
Signature Algorithm: sha256WithRSAEncryption
    Issuer: C = GB, ST = Greater Manchester, L = Salford, O = COMODO CA Limited, CN = COMODO RSA Domain Validation Secure Server CA
    Validity
        Not Before: Mar 12 00:00:00 2018 GMT
        Not After : Mar 11 23:59:59 2020 GMT
    Subject: OU = Domain Control Validated, OU = PositiveSSL, CN = acs.cdroutertest.com
    Subject Public Key Info:
        Public Key Algorithm: rsaEncryption
            Public-Key: (2048 bit)

如下网站所示:https://support.qacafe.com/knowledge-base/how-do-i-display-the-contents-of-a-ssl-certificate/

我已经在 python3 中尝试使用 Cryptography 或 pyopenssl 模块,并且能够导入证书并显示,例如public 键。但是我没有找到一种方法来浏览所有可用信息并只显示它们,而不必为证书中可能可用或不可用的每个字段编写 print() 语句。

有没有人知道如何将我推向正确的方向?

非常感谢,谢谢!

实际上我没有注意到的一个解决方案是使用模块 asn1tools

import asn1tools
foo = asn1tools.compile_files("x509.asn")
output = foo.decode("Certificate", cert)

只有 X509 所需的所有 ASN.1 定义必须在文件 x509.asn 中可用(可通过相应的 RFC 获得),而“cert”包含字节串。

结果将是一个包含所有信息的 python 字典,无需循环任何内容或遗漏一些不寻常的参数