我正在使用OpenSSL库的X509证书类,我需要查询“密钥用法”扩展.
放弃OpenSSL的vapourware“文档”后,一些黑暗的网络搜索最终显示我需要打电话
X509_get_ext_d2i(X509 *x,int nid,int *crit,int *idx)
问题是,这个调用返回一个指向void的指针,这显然可以指向各种结构,具体取决于所要求的扩展名.
解决方法
读取关键用法的最简单的解决方案似乎是
X509* x509_cert = ... // without X509_check_ca x509_cert->ex_kusage always returns 0 (no idea why) int ca = X509_check_ca(x509_cert); unsigned long usage = x509_cert->ex_kusage;
结果值在opensc / pkcs15-init.h中定义
SC_PKCS15INIT_X509_DIGITAL_SIGNATURE 0x0080UL SC_PKCS15INIT_X509_NON_REPUDIATION 0x0040UL SC_PKCS15INIT_X509_KEY_ENCIPHERMENT 0x0020UL SC_PKCS15INIT_X509_DATA_ENCIPHERMENT 0x0010UL SC_PKCS15INIT_X509_KEY_AGREEMENT 0x0008UL SC_PKCS15INIT_X509_KEY_CERT_SIGN 0x0004UL SC_PKCS15INIT_X509_CRL_SIGN 0x0002UL
/* Handle key usage */ if((usage=X509_get_ext_d2i(x,NID_key_usage,NULL,NULL))) { if(usage->length > 0) { x->ex_kusage = usage->data[0]; if(usage->length > 1) x->ex_kusage |= usage->data[1] << 8; } else x->ex_kusage = 0; x->ex_flags |= EXFLAG_KUSAGE; ASN1_BIT_STRING_free(usage); }