-
-
Save iamjasonp/dbe3966aba99e8bc5d22 to your computer and use it in GitHub Desktop.
Parse X509Certificate SAN extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.ServiceModel; | |
using System.ServiceModel.Channels; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Security.Cryptography.X509Certificates; | |
using System.ServiceModel.Security; | |
using System.Security.Authentication; | |
using System.Net; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
public static void Main() | |
{ | |
const string x509ExtensionBase64String = "MD2CFW5vdC1yZWFsLXN1YmplY3QtbmFtZYIZbm90LXJlYWwtZnFkbi5leGFtcGxlLmNvbYIJbG9jYWxob3N0"; | |
const string subjectName1 = "not-real-subject-name"; | |
X509Extension x509Extension = new X509Extension("2.5.29.17", Convert.FromBase64String(x509ExtensionBase64String), true); | |
string x509ExtensionFormattedString = x509Extension.Format(false); | |
// On Windows, x509ExtensionFormattedString is: | |
// DNS Name=not-real-subject-name, DNS Name=not-real-fqdn.example.com, DNS Name=localhost | |
// <identifier><delimter><value><separator(s)> | |
int delimiterIndex = x509ExtensionFormattedString.IndexOf(subjectName1) - 1; | |
char delimiter = x509ExtensionFormattedString[delimiterIndex]; | |
string identifier = x509ExtensionFormattedString.Substring(0, delimiterIndex); | |
int separatorFirstChar = delimiterIndex + subjectName1.Length + 1; | |
int separatorLength = 1; | |
for (int i = separatorFirstChar + 1; i < x509ExtensionFormattedString.Length; i++) | |
{ | |
if (x509ExtensionFormattedString[i] == identifier[0]) | |
{ | |
break; | |
} | |
else | |
{ | |
separatorLength++; | |
} | |
} | |
Console.WriteLine(x509Extension); | |
Console.WriteLine("done."); | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment