see sharp RSS 2.0
# Wednesday, 16 March 2011

If you have to parse the Subject Alternative Name (aka SAN) of a Certificate CertGetNameString is your friend:

[DllImport("crypt32.dll", EntryPoint = "CertGetNameString", CharSet = CharSet.Auto, SetLastError = true)]
static extern UInt32 CertGetNameString(
    IntPtr CertContext, 
    UInt32 lType, 
    UInt32 lFlags, 
    IntPtr pTypeParameter, 
    StringBuilder str, 
    UInt32 cch);

private const int CERT_NAME_EMAIL_TYPE = 1;
private const int CERT_NAME_UPN_TYPE = 8;
private const int CERT_NAME_NO_FLAG = 0;
private const int SIZE = 255;

private static void ParseSan(X509Certificate2 cc)
{
    Oid oid = new Oid("2.5.29.17");
    X509Extension ext = cc.Extensions[oid.Value]; // get the SAN extension

    if (null != ext)
    {
        StringBuilder Buffer = new StringBuilder(SIZE);               

        UInt32 nChars = CertGetNameString(cc.Handle,
                CERT_NAME_EMAIL_TYPE,
                CERT_NAME_NO_FLAG,
                IntPtr.Zero,
                Buffer,
                SIZE);
        if (nChars == 1)
        {
            nChars = CertGetNameString(cc.Handle,
                CERT_NAME_UPN_TYPE,
                CERT_NAME_NO_FLAG,
                IntPtr.Zero,
                Buffer,
                SIZE);
        }
                
        Console.WriteLine("{1}:'{0}'", Buffer.ToString(), cc.Thumbprint);

               
    }
}
Wednesday, 16 March 2011 15:34:13 (Mitteleuropäische Zeit, UTC+01:00)  #    -
C# | CAPI | Certificates | P/INVOKE
Archive
<2011 March>
SunMonTueWedThuFriSat
272812345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Any link on this site may lead to an external website that is not under my control and that external website might show an opinion that is not mine.

© Copyright 2017
Hannes Köhler
Sign In
Statistics
Total Posts: 39
This Year: 0
This Month: 0
This Week: 0
Comments: 1
All Content © 2017, Hannes Köhler
DasBlog theme 'Business' created by Christoph De Baene (delarou)