Skip to content

Parse subject alternative directory address of an X509 certificate

BouncyCastle

Here is an example of extracting the common name of the directory address of the subject alternative name of a certificate using the bouncycastle library


X509Certificate cert = ...

foreach (ArrayList sName in cert.GetSubjectAlternativeNames() as ArrayList)
{
    if ((int)sName[0] == 4) //tag 4 is for DirectoryAddress
    {
        string dirName = sName[1].ToString();

        X509Name x500Name = new (dirName);
        string altCn = x500Name.GetValues(X509Name.CN)[0].ToString();

        Console.WriteLine(altCn);
    }
}

Other possible general names:


public const int OtherName                  = 0;
public const int Rfc822Name                 = 1;
public const int DnsName                    = 2;
public const int X400Address                = 3;
public const int DirectoryName              = 4;
public const int EdiPartyName               = 5;
public const int UniformResourceIdentifier  = 6;
public const int IPAddress                  = 7;
public const int RegisteredID               = 8;

Post a Comment

Previous Post Next Post