Search code examples
c#certificatex509certificate2

How to get issuer name in RFC-1779 from X509Certificate2?


The Issuer property from X509Certificate2 returns a string such as:

"CN=eBusiness Development CA, OU=ITSB eBusiness Systems Team, O=AVANO, L=Sydney, S=NSW, C=AU"

How do I get the same in RFC-1779? For example:

"/C=AU/ST=NSW/L=Sydney/O=AVANO/OU=ITSB eBusiness Systems Team/CN=eBusiness Development CA"

var cert = new X509Certificate2(certPath, password);
Console.WriteLine(cert.Issuer);

Solution

  • The string you're wanting is just the reverse of the string you're actually getting from Issuer, and is delimited by slashes instead of commas with a following space.

    My first attempt to solve this problem was to create a new X500DistinguishedName object, passing the certificate's IssuerName and X500DistinguishedNameFlags.Reversed to the constructor:

    var distinguishedName = new X500DistinguishedName(cert.Issuer, X500DistinguishedNameFlags.Reversed);
    

    But that didn't provide me with the result I was hoping for. I'm not sure of any baked in way to do this. But you could try reformatting the string manually to meet your needs:

    var split = cert.Issuer.Split(new []{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
    var reversed = split.Reverse();
    var finalIssuerName = string.Join("/", reversed);