正在提取证书中主题属性的所有值

Extracting all values of a subject attribute in a certificate

本文关键字:属性 提取 证书      更新时间:2023-10-16

我目前正在使用CertGetNameString来提取每个主题属性的值,比如:

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

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

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(和其他)属性的所有值?

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

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

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

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"

如果需要单独的属性值,则可以对其进行解析。