gpt4 book ai didi

c - 如何验证证书是否具有 "CA=true"基本约束?

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:12 25 4
gpt4 key购买 nike

我是 openssl API 的新手

当我使用带有此命令的 openssl APP 时:

openssl x509 -in mycert.crt -text -noout

形成它的输出我感兴趣的是:

    ...
X509v3 extensions:
X509v3 Basic Constraints: critical
CA:TRUE <-- this section I want to validate in my code
...

我想使用 OpenSSL c API 验证 CA 部分,但不知道如何验证。我已经深入研究了 APP src 代码,但是当我到达时迷路了

openssl/crypto/ct/ct_prn:102

BIO_printf(out, "\n%*sExtensions: ", indent + 4, "");
if (sct->ext_len == 0)
BIO_printf(out, "none");
else
BIO_hex_string(out, indent + 16, 16, sct->ext, sct->ext_len);

我的问题:迭代扩展的正确方法是什么,找到基本约束部分并验证我的证书是 CA:TRUE

奖励问题:您在 openssl API 中搜索方法的方法是什么

最佳答案

bonus question: what is your method searching the openssl API for methods

遗憾的是,有时没有简单的方法。我将向您介绍我用来回答此类独特问题的步骤。

如果您知道要寻找什么,有时您可以使用符号浏览器,如 ctags。但是我不使用它;相反,我通常 grep 源代码。

如果您听说过 OpenSSL “ self 记录”,这就是它的缩影。


my question: what is the proper way to iterate over the extensions , find tthe basic constrains section and validate that my certificate is CA:TRUE

一段时间后,您就会对这些有趣的东西有所了解。从下面的 grep 中,我知道它是 NID_basic_constraints

openssl-1.1.0b$ grep -iR basic * | grep constraint
crypto/objects/obj_dat.h: 0x55,0x1D,0x13, /* [ 505] OBJ_basic_constraints */
crypto/objects/obj_dat.h: {"basicConstraints", "X509v3 Basic Constraints", NID_basic_constraints, 3, &so[505]},
crypto/objects/obj_dat.h: 87, /* OBJ_basic_constraints 2 5 29 19 */
crypto/objects/obj_mac.num:basic_constraints 87
crypto/objects/objects.txt:!Cname basic-constraints
crypto/x509v3/v3_bcons.c: NID_basic_constraints, 0,
crypto/x509v3/v3_purp.c: NID_basic_constraints, /* 87 */
crypto/x509v3/v3_purp.c: /* Handle basic constraints */
crypto/x509v3/v3_purp.c: if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL)))
...

从上面,我知道了一些事情。首先,v3_purp.c 是感兴趣的文件。其次,X509_get_ext_d2i(x, ...) 表示 x 是一个 X509*。第三,NID_basic_constraints 是感兴趣的项目。

这里是函数的头部和感兴趣的位。 BASIC_CONSTRAINTSbs->ca 看起来都很有趣。

static void x509v3_cache_extensions(X509 *x)
{
BASIC_CONSTRAINTS *bs;
PROXY_CERT_INFO_EXTENSION *pci;
ASN1_BIT_STRING *usage;
ASN1_BIT_STRING *ns;
EXTENDED_KEY_USAGE *extusage;
X509_EXTENSION *ex;

int i;
if (x->ex_flags & EXFLAG_SET)
return;
X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
/* V1 should mean no extensions ... */
if (!X509_get_version(x))
x->ex_flags |= EXFLAG_V1;
/* Handle basic constraints */
if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
if (bs->ca)
x->ex_flags |= EXFLAG_CA;
if (bs->pathlen) {
if ((bs->pathlen->type == V_ASN1_NEG_INTEGER)
|| !bs->ca) {
x->ex_flags |= EXFLAG_INVALID;
x->ex_pathlen = 0;
} else
x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
} else
x->ex_pathlen = -1;
BASIC_CONSTRAINTS_free(bs);
x->ex_flags |= EXFLAG_BCONS;
}
...
}

接下来,检查 BASIC_CONSTRAINTS。您需要 _st,因为这就是 OpenSSL 命名其结构的方式。

$ grep -IR BASIC_CONSTRAINTS * | grep _st
include/openssl/x509v3.h:typedef struct BASIC_CONSTRAINTS_st {

然后快速浏览一下 BASIC_CONSTRAINTS_st:

typedef struct BASIC_CONSTRAINTS_st {
int ca;
ASN1_INTEGER *pathlen;
} BASIC_CONSTRAINTS;

最后一个问题可能是,您如何获得 X509 *x?好吧,你可以从:

  • SSL_get_peer_certificate(来自 SSL 连接)
  • PEM_read_X509(从 PEM 编码转换)
  • d2i_X509(从ASN.1/DER编码转换)
  • 等...

如果您已经有一个X509* x,看起来您只需要测试x->ex_flags & EXFLAG_V1.

起泡、冲洗、重复 EXFLAG_V1X509_st:

$ grep -IR EXFLAG_V1 * | grep define
crypto/x509v3/v3_purp.c:#define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
include/openssl/x509v3.h:# define EXFLAG_V1 0x40

$ grep -IR X509 * | grep _st | grep typedef
crypto/x509v3/pcy_int.h:typedef struct X509_POLICY_DATA_st X509_POLICY_DATA;
include/openssl/ossl_typ.h:typedef struct x509_st X509;
include/openssl/ossl_typ.h:typedef struct X509_algor_st X509_ALGOR;
include/openssl/ossl_typ.h:typedef struct X509_crl_st X509_CRL;
...

关于c - 如何验证证书是否具有 "CA=true"基本约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40609792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com