self-signed-certificate + #83

This commit is contained in:
Stef Heyenrath
2018-02-03 12:06:12 +01:00
parent 361d40189b
commit 180526c8b4
17 changed files with 143 additions and 32 deletions

View File

@@ -1,41 +0,0 @@
using System;
using System.Security.Cryptography.X509Certificates;
namespace WireMock.Http
{
internal static class CertificateUtil
{
public static X509Certificate2 GetCertificate(string thumbprintOrSubjectName)
{
X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
try
{
//Certificate must be in the local machine store
certStore.Open(OpenFlags.ReadOnly);
//Attempt to find by thumbprint first
var matchingCertificates = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprintOrSubjectName, false);
if (matchingCertificates.Count == 0)
{
//Fallback to subject name
matchingCertificates = certStore.Certificates.Find(X509FindType.FindBySubjectName, thumbprintOrSubjectName, false);
if (matchingCertificates.Count == 0)
{
// No certificates matched the search criteria.
throw new Exception($"No certificate found with Thumbprint or SubjectName '{thumbprintOrSubjectName}'");
}
}
// Use the first matching certificate.
return matchingCertificates[0];
}
finally
{
#if NETSTANDARD || NET46
certStore.Dispose();
#else
certStore.Close();
#endif
}
}
}
}

View File

@@ -5,6 +5,7 @@ using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WireMock.HttpsCertificate;
using WireMock.Validation;
namespace WireMock.Http
@@ -33,7 +34,7 @@ namespace WireMock.Http
{
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
var x509Certificate2 = CertificateUtil.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName);
var x509Certificate2 = ClientCertificateHelper.GetCertificate(clientX509Certificate2ThumbprintOrSubjectName);
handler.ClientCertificates.Add(x509Certificate2);
}