gpt4 book ai didi

openssl - 如何从 X509 证书中获取 Keyusage 值?

转载 作者:行者123 更新时间:2023-12-02 06:05:59 33 4
gpt4 key购买 nike

我想从 X509 结构化证书中检索 key 使用值,我尝试了以下代码

 X509* lcert=NULL;
lCert=PEM_read(filename); // function will return the certificate in X509
unsigned long lKeyusage= lCert->ex_kusage;

当我打印 lKeyusage 值时.. 有时我得到 128 ... 有时我得到相同证书的 0 ..谁能告诉我错误是什么?如果我做错了,请给我一些示例代码或正确的 API ..

最佳答案

我认为最简单的方法是使用内存 BIO:

...
X509 *lcert = NULL;
BUF_MEM *bptr = NULL;
char *buf = NULL;
int loc;

FILE *f = fopen("your cert goes here", "rb");
if( (lcert = PEM_read_X509(f, &lcert, NULL, NULL)) == NULL){
// error handling...
}

loc = X509_get_ext_by_NID( lcert, NID_key_usage, -1);
X509_EXTENSION *ex = X509_get_ext(lcert, loc);

BIO *bio = BIO_new(BIO_s_mem());
if(!X509V3_EXT_print(bio, ex, 0, 0)){
// error handling...
}
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bptr);

// now bptr contains the strings of the key_usage, take
// care that bptr->data is NOT NULL terminated, so
// to print it well, let's do something..
buf = (char *)malloc( (bptr->length + 1)*sizeof(char) );

memcpy(buf, bptr->data, bptr->length);
buf[bptr->length] = '\0';

// Now you can printf it or parse it, the way you want...
printf ("%s\n", buf);

...

在我的例子中,对于测试证书,它打印了“数字签名、不可否认、 key 加密”

还有其他方法,比如使用 ASN1_BIT_STRING *。如果以上内容不符合您的需求,我可以告诉您。

问候。

关于openssl - 如何从 X509 证书中获取 Keyusage 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10011730/

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