From 0497e145eaa78b8b66e6c0ff724c55a8ce3be53b Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Thu, 25 May 2017 13:47:17 +1000 Subject: [PATCH] Bug fix: Allow for unexpected cert subject names --- .../Interop/CertificateInterop.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Disco.ClientBootstrapper/Interop/CertificateInterop.cs b/Disco.ClientBootstrapper/Interop/CertificateInterop.cs index 89a777be..31807ad2 100644 --- a/Disco.ClientBootstrapper/Interop/CertificateInterop.cs +++ b/Disco.ClientBootstrapper/Interop/CertificateInterop.cs @@ -106,7 +106,26 @@ namespace Disco.ClientBootstrapper.Interop public static string ShortSubjectName(this X509Certificate2 Certificate) { string s = Certificate.Subject; - return s.Substring(s.IndexOf("=") + 1, s.IndexOf(",") - s.IndexOf("=") - 1); + if (string.IsNullOrWhiteSpace(s)) + { + return $"Unknown Certificate: {Certificate.Thumbprint}"; + } + else + { + if (s.Length > 3 && s.StartsWith("CN=", StringComparison.OrdinalIgnoreCase)) + { + var nameLength = s.IndexOf(',') - 3; + if (nameLength > 0) + { + return s.Substring(3, nameLength); + } + else + { + return s.Substring(3); + } + } + return s; + } } public static bool Add(StoreName StoreName, StoreLocation StoreLocation, X509Certificate2 Certificate)