访问 M2Crypto 中的 CSR 扩展堆栈

发布于 2024-10-10 01:28:12 字数 256 浏览 7 评论 0原文

我有一个添加了扩展堆栈的证书签名请求。当根据此请求构建证书时,我希望能够访问该堆栈以用于创建最终证书。

然而,虽然 M2Crypto.X509.X509 有许多用于访问扩展的帮助程序(get_extget_ext_at 等),M2Crypto .X509.Request 似乎仅提供用于添加扩展的成员,但无法检查已与给定对象关联的扩展。

我在这里错过了什么吗?

I have a certificate signing request with an extension stack added. When building a certificate based on this request, I would like to be able to access that stack to use in creating the final certificate.

However, while M2Crypto.X509.X509 has a number of helpers for accessing extensions (get_ext, get_ext_at and the like), M2Crypto.X509.Request appears to provide only a member for adding extensions, but no way to inspect the extensions already associated with a given object.

Am I missing something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-10-17 01:28:12

你说得对。

当前版本的 M2Crypto 未公开必要的 OpenSSL 接口 - X509_REQ_get_extensions

只是为了了解 C 方面涉及的内容:

X509_REQ *req = /* ... */;
STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(req);
int count = sk_X509_EXTENSION_num(exts);
int i;
for (i = 0; i < count; ++i) {
    X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
    /* Do something with ext */
}
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);

由于 M2Crypto 使用 SWIG 来包装 C 代码,因此如果您有良好的 C 背景,那么公开新的 API 应该不难。

You're right.

The current version of M2Crypto doesn't expose the necessary OpenSSL interface - X509_REQ_get_extensions.

Just to give an idea of what's involved in terms of C:

X509_REQ *req = /* ... */;
STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(req);
int count = sk_X509_EXTENSION_num(exts);
int i;
for (i = 0; i < count; ++i) {
    X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
    /* Do something with ext */
}
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);

Since M2Crypto uses SWIG to wrap the C code, it shouldn't be difficult to expose a new API if you have a good C background.

会发光的星星闪亮亮i 2024-10-17 01:28:12

对于通过与我来到这里类似的谷歌搜索找到这个问题的其他人。

面对类似的问题,并且没有针对 M2Crypto 缺陷的补丁,我采取了调用 OpenSSL 实用程序并解析输出的方式,它看起来与 YAML 非常相似,我们可以通过一些清理来伪造它。

def req_extensions(csrFilename):
    cmd = ('openssl req -text -noout -in %s'
        % csrFilename)

    output = subprocess.check_output(cmd.split(),
        stderr=subprocess.STDOUT)

    output = re.sub(r': rsaEncryption', ':', output)
    output = re.sub(r'[0-9a-f]{2}:', '', output)

    return yaml.load(output)

然后...

csrExt = self.req_extensions('my.csr')
keyUsage = (
    csrExt['Certificate Request']['Data']['Requested Extensions']
          ['X509v3 Key Usage'])

SAN = (
    csrExt['Certificate Request']['Data']['Requested Extensions']
          ['X509v3 Subject Alternative Name'])

等等。

To others finding this question via a similar Google search to what brought me here.

Faced with a similar problem, and no patch to this shortcoming of M2Crypto in sight, I went down the path of calling the OpenSSL utility and parsing the output, which looks similar-enough to YAML that we can fake it with a little cleanup.

def req_extensions(csrFilename):
    cmd = ('openssl req -text -noout -in %s'
        % csrFilename)

    output = subprocess.check_output(cmd.split(),
        stderr=subprocess.STDOUT)

    output = re.sub(r': rsaEncryption', ':', output)
    output = re.sub(r'[0-9a-f]{2}:', '', output)

    return yaml.load(output)

Then...

csrExt = self.req_extensions('my.csr')
keyUsage = (
    csrExt['Certificate Request']['Data']['Requested Extensions']
          ['X509v3 Key Usage'])

SAN = (
    csrExt['Certificate Request']['Data']['Requested Extensions']
          ['X509v3 Subject Alternative Name'])

etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文