Search code examples
delphicryptoapi

How to parse certificate Subject string?


Using CertNameToStr function you can get a certificate's Subject string:

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"

My question is, how to parse it to get an array of name-value pairs?

I tried to use the SplitString() function with , character as a delimiter:

  StrArr := SplitString(edtSubjectStr.Text, ',');
  for I := Low(StrArr) to High(StrArr) do
    Memo1.Lines.Append(Trim(StrArr[i]));

but the result is unsatisfactory:

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"

Is there any API function to parse this subject string?

Or maybe there is a way of parsing it with some other Delphi components|classes|functions?


Solution

  • Here is the answer:

    just use CERT_X500_NAME_STR or CERT_NAME_STR_CRLF_FLAG as the value of the dwStrType property of the CertNameToStr() function and you'll get a CRLF separated string!