-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Custom Certificate settings (#537)
- Loading branch information
Showing
24 changed files
with
478 additions
and
103 deletions.
There are no files selected for viewing
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
36 changes: 36 additions & 0 deletions
36
examples/WireMock.Net.Console.NETCoreApp3WithCertificate/Program.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using WireMock.Logging; | ||
using WireMock.Server; | ||
using WireMock.Settings; | ||
|
||
namespace WireMock.Net.Console.NETCoreApp3WithCertificate | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
string url = "https://localhost:8433/"; | ||
|
||
var server = WireMockServer.Start(new WireMockServerSettings | ||
{ | ||
Urls = new[] { url }, | ||
StartAdminInterface = true, | ||
Logger = new WireMockConsoleLogger(), | ||
CertificateSettings = new WireMockCertificateSettings | ||
{ | ||
X509StoreName = "My", | ||
X509StoreLocation = "CurrentUser", | ||
X509StoreThumbprintOrSubjectName = "FE16586076A8B3F3E2F1466803A6C4C7CA35455B" | ||
|
||
// X509CertificateFilePath = "example.pfx", | ||
// X509CertificatePassword = "wiremock" | ||
} | ||
|
||
}); | ||
System.Console.WriteLine("WireMockServer listening at {0}", string.Join(",", server.Urls)); | ||
|
||
System.Console.WriteLine("Press any key to stop the server"); | ||
System.Console.ReadKey(); | ||
server.Stop(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...Console.NETCoreApp3WithCertificate/WireMock.Net.Console.NETCoreApp3WithCertificate.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\WireMock.Net.StandAlone\WireMock.Net.StandAlone.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="example.pfx"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
examples/WireMock.Net.Console.NETCoreApp3WithCertificate/base64_encoded.cer
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEsDCCApigAwIBAgIQJbH6hSGKdoFI0B7qCIOK7jANBgkqhkiG9w0BAQUFADAU | ||
MRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMjAxMDMwMjMwMDAwWhcNMzAxMTA2MjMw | ||
MDAwWjAUMRIwEAYDVQQDEwlsb2NhbGhvc3QwggIiMA0GCSqGSIb3DQEBAQUAA4IC | ||
DwAwggIKAoICAQCl5fQSrRgT3Q6WoULR98Y+rrDWtTTgVpbLU04G0hLZ4yUeP7Wa | ||
yuVbvx7zX8XT4lA8Hu5T/GG91U077JcSSEjnPBFsh4hE7FkRoSYIEW6BFG7D7eUG | ||
dGHnDV8UkSRQ97LJPyjXuHVDJzNDJ9xQGMzOZ4n8vQ7SEKBw9hRG2ugkP5b2jVIN | ||
e1E549tq2jnIVpKCZ4+prf64ZLsaokX7VHe+b/CW3GoAqUUaUjdTpAQ7LpypJuFz | ||
415enOrKQe+UEBd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FUKkPtM4 | ||
f242FIV/XnOeloDmhGGeTB7aODB+gGCvgmOH92njjUEIv+SnYQkflQaRhhyNIACi | ||
ZvWlP/96H+X4fUG5kVNBHY021ZWmurUDqVxWUaswg63+DfsZcYtt6wgxiAN4ssXG | ||
wLnLPw== | ||
-----END CERTIFICATE----- |
Binary file added
BIN
+3.7 KB
examples/WireMock.Net.Console.NETCoreApp3WithCertificate/base64_encoded.privatekey
Binary file not shown.
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace WireMock.HttpsCertificate | ||
{ | ||
internal static class CertificateLoader | ||
{ | ||
/// <summary> | ||
/// Used by the WireMock.Net server | ||
/// </summary> | ||
public static X509Certificate2 LoadCertificate( | ||
string storeName, | ||
string storeLocation, | ||
string thumbprintOrSubjectName, | ||
string filePath, | ||
string password, | ||
string host) | ||
{ | ||
if (!string.IsNullOrEmpty(storeName) && !string.IsNullOrEmpty(storeLocation)) | ||
{ | ||
var thumbprintOrSubjectNameOrHost = thumbprintOrSubjectName ?? host; | ||
|
||
var certStore = new X509Store((StoreName)Enum.Parse(typeof(StoreName), storeName), (StoreLocation)Enum.Parse(typeof(StoreLocation), storeLocation)); | ||
try | ||
{ | ||
certStore.Open(OpenFlags.ReadOnly); | ||
|
||
// Attempt to find by Thumbprint first | ||
var matchingCertificates = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprintOrSubjectNameOrHost, false); | ||
if (matchingCertificates.Count == 0) | ||
{ | ||
// Fallback to SubjectName | ||
matchingCertificates = certStore.Certificates.Find(X509FindType.FindBySubjectName, thumbprintOrSubjectNameOrHost, false); | ||
if (matchingCertificates.Count == 0) | ||
{ | ||
// No certificates matched the search criteria. | ||
throw new FileNotFoundException($"No Certificate found with in store '{storeName}', location '{storeLocation}' for Thumbprint or SubjectName '{thumbprintOrSubjectNameOrHost}'."); | ||
} | ||
} | ||
|
||
// Use the first matching certificate. | ||
return matchingCertificates[0]; | ||
} | ||
finally | ||
{ | ||
#if NETSTANDARD || NET46 | ||
certStore.Dispose(); | ||
#else | ||
certStore.Close(); | ||
#endif | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(password)) | ||
{ | ||
return new X509Certificate2(filePath, password); | ||
} | ||
|
||
throw new InvalidOperationException("X509StoreName and X509StoreLocation OR X509CertificateFilePath and X509CertificatePassword are mandatory."); | ||
} | ||
|
||
/// <summary> | ||
/// Used for Proxy | ||
/// </summary> | ||
public static X509Certificate2 LoadCertificate(string thumbprintOrSubjectName) | ||
{ | ||
var 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 SubjectName | ||
matchingCertificates = certStore.Certificates.Find(X509FindType.FindBySubjectName, thumbprintOrSubjectName, false); | ||
if (matchingCertificates.Count == 0) | ||
{ | ||
// No certificates matched the search criteria. | ||
throw new FileNotFoundException("No certificate found with specified Thumbprint or SubjectName.", thumbprintOrSubjectName); | ||
} | ||
} | ||
|
||
// Use the first matching certificate. | ||
return matchingCertificates[0]; | ||
} | ||
finally | ||
{ | ||
#if NETSTANDARD || NET46 | ||
certStore.Dispose(); | ||
#else | ||
certStore.Close(); | ||
#endif | ||
} | ||
} | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
src/WireMock.Net/HttpsCertificate/ClientCertificateHelper.cs
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.