提取证书中主题属性的所有值 [英] Extracting all values of a subject attribute in a certificate

查看:80
本文介绍了提取证书中主题属性的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用 CertGetNameString 来提取每个主题属性的值,如下所示:

I'm currently using CertGetNameString to extract the values for each subject attribute like so:

CertGetNameString(pCertificate,
                  CERT_NAME_ATTR_TYPE,
                  0,
                  szOID_ORGANIZATIONAL_UNIT_NAME,
                  buf,
                  _countof(buf));

但是我发现某些证书具有组织单位名称(OU)的多个值,并且 CertGetNameString 只能读取第一个.例如,这是Adobe证书的主题:

However some certificates I've found have multiple values for the organizational unit name (OU) and CertGetNameString can only read the first. For instance this is the subject of an Adobe certificate:

CN = Adobe Systems, Incorporated
OU = Acrobat Engineering
OU = Digital ID Class 3 - Microsoft Software Validation v2
O = Adobe Systems, Incorporated
L = San Jose
S = California
C = US

如何使用CryptoAPI读取OU(和其他)属性的所有值?

How can I read all values for the OU (and other) attribute(s) using CryptoAPI?

推荐答案

好,找到了解决方案.正确使用的API是 CertNameToStr ,如下所示:

Ok, found the solution. The correct API to use is CertNameToStr, like so:

    CertNameToStr(X509_ASN_ENCODING,
                  &pCertificate->pCertInfo->Subject,
                  CERT_X500_NAME_STR,
                  buf,
                  _countof(buf));

它将返回一个字符串,例如:

It will return a string such as:

C=US, S=California, L=San Jose, O="Adobe Systems, Incorporated", OU=Digital ID Class 3 - Microsoft Software Validation v2, OU=Acrobat Engineering, CN="Adobe Systems, Incorporated"

如果需要单个属性值,则可以对其进行解析.

Which can then be parsed if individual attribute values are required.

这篇关于提取证书中主题属性的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆