【问题标题】:Add an arbitrary/deprecated extension to a certificate using python cryptography certificate builder使用 python 加密证书生成器向证书添加任意/不推荐的扩展
【发布时间】:2020-09-09 10:40:48
【问题描述】:

我正在尝试在 python 中创建一个以前使用openssl ca 命令构建的证书。除了一件事,一切都完美无缺:我需要添加“nsCertType”扩展,这似乎已被弃用。但是,我找不到添加任意证书扩展的方法。 This guy is asking the same question for go,甚至指定了使用 OpenSSL 的 python 解决方案,但是我不知道如何在没有 OpenSSL 的情况下做到这一点。这是我的代码:

    inter_server_cert = x509.CertificateBuilder().subject_name(
    subject
).issuer_name(
    inter_ca_cert.issuer
).public_key(
    inter_server_key.public_key()
).serial_number(
    x509.random_serial_number
).not_valid_before(
    datetime.datetime.utcnow()
).not_valid_after(
    datetime.datetime.utcnow() + datetime.timedelta(days=duration_rootca)
).add_extension(
    x509.BasicConstraints(
        ca=False, path_length=0
    ),
    critical=True
).add_extension(
    x509.SubjectKeyIdentifier.from_public_key(inter_server_key.public_key()),
    critical=False
).add_extension(
    x509.AuthorityKeyIdentifier.from_issuer_subject_key_identifier(authority_key_identifier.value),
    critical=False
).add_extension(
    x509.KeyUsage(
        key_cert_sign=False,
        crl_sign=False,
        digital_signature=True,
        content_commitment=False,
        key_encipherment=True,
        data_encipherment=False,
        key_agreement=True,
        encipher_only=False,
        decipher_only=False
    ),
    critical=False
).add_extension(
    x509.ExtendedKeyUsage([x509.oid.ExtendedKeyUsageOID.SERVER_AUTH]),
    critical=False
).sign(inter_ca_key, hashes.SHA256(), default_backend())

【问题讨论】:

    标签: python-3.x openssl cryptography


    【解决方案1】:

    您可以使用UnrecognizedExtensioncryptography 中的任意扩展名进行编码。这没有直接记录,但您可以在测试中看到它的使用示例 (https://github.com/pyca/cryptography/blob/3367c18bf2e71639843e38498f5ad2159835122d/tests/x509/test_x509.py#L3327)。

    请注意,您必须以字节形式提供 OID 和已经 DER 编码的有效负载。如果您有一个带有您需要的值的示例证书,您可以对其进行解析以获得正确的序列。

    【讨论】:

    • 谢谢,这完全符合预期。如果将来有人偶然发现这一点,nsCertType 的 OID 和值“server”的有效负载是:x509.UnrecognizedExtension(oid=x509.ObjectIdentifier('2.16.840.1.113730.1.1'), value=b'\x03\x02\x06@')
    • 谢谢保罗,正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    相关资源
    最近更新 更多