3

我是 pyOpenSSL 的新用户,我想用以下代码制作证书

from OpenSSL import crypto as c

cert = c.X509()
cert.add_extensions([
    c.X509Extension('crlDistributionPoints', False, 'dirName:/C=US/O=TEST'),
])

此代码无法工作,谁能帮助我?pyOpenSSL 似乎不支持 dirName

cert.add_extensions([
    c.X509Extension('crlDistributionPoints', False, 'URI:http://somesite') can work
])
4

2 回答 2

0

这是您可以生成 DER 的一种方式……它不包含 dirName 的代码,但我希望它可以让您了解如何构建 DER

from pyasn1.codec.der import encoder as der_encoder
from pyasn1.type import tag
from pyasn1_modules import rfc2459

class GeneralNames(rfc2459.GeneralNames):
    """
    rfc2459 has wrong tagset.
    """
    tagSet = tag.TagSet(
        (),
        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0),
        )

class DistributionPointName(rfc2459.DistributionPointName):
    """
    rfc2459 has wrong tagset.
    """
    tagSet = tag.TagSet(
        (),
        tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0),
        )

cdps = [('uri', 'http://something'), ('dns', 'some.domain.com')]

cdp = rfc2459.CRLDistPointsSyntax()
values = []
position = 0
for cdp_type, cdp_value in cdps:
    cdp_entry = rfc2459.DistributionPoint()

    general_name = rfc2459.GeneralName()

    if cdp_type == 'uri':
        general_name.setComponentByName(
            'uniformResourceIdentifier',
            cdp_value,
            )
    elif cdp_type == 'dns':
        general_name.setComponentByName(
            'dNSName',
            cdp_value,
            )

    general_names = GeneralNames()
    general_names.setComponentByPosition(0, general_name)

    name = DistributionPointName()
    name.setComponentByName('fullName', general_names)
    cdp_entry.setComponentByName('distributionPoint', name)

    cdp.setComponentByPosition(position, cdp_entry)
    position += 1

cdp_der = der_encoder.encode(cdp)

extensions.append(
    crypto.X509Extension(
        b'crlDistributionPoints',
        False,
        'DER:' + cdp_der.encode('hex'),
        ),
    )
于 2016-05-30T21:32:11.117 回答
0

我遇到了完全相同的问题,但是我也找不到真正的解决方案,我设法找到了一种解决方法来通过 Python 完成它。在此页面中,格式解释 了http://openssl.org/docs/apps/x509v3_config.html#CRL-distribution-points以及使用原始 DER 字节的选项。(部分:任意扩展)

首先从已经具有正确 URI 和 dirName 的证书中“收集”DER 字节。另一种方法是使用带有正确 crlDistributionPoint 的 openssl 制作证书,此示例中的 tmpcert 就是此证书。还要弄清楚使用了哪个扩展索引。get_short_name 将给出扩展的“密钥”,因此搜索 crlDistributionPoint。使用以下方法收集它:

from binascii import hexlify
print tmpcert.get_extension(5).get_short_name()
print hexlify(tmpcert.get_extension(5).get_data())

然后格式化此输出并在 X509Extension() 的初始化程序中使用它

crypto.X509Extension('crlDistributionPoints', False,  
"DER:30:6a:xx:xx:xx:..........:xx:xx")

据了解,这是一种“硬编码”解决方案,没有直接的方法可以以这种方式更改该字段的内容。

于 2015-08-05T10:56:49.313 回答