Skip to content

Commit 4e8846b

Browse files
feat: bypass server certificate config (#16)
* feat: add Bypass Certificate Check Flag * CA Cert was not actually being checked * Correctly Check CA
1 parent fa67116 commit 4e8846b

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

application/DotNetMeshClient/NHS.Mesh.Client/Clients/MeshConnectClient.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ private async Task<HttpResponseMessage> SendHttpRequest(HttpRequestMessage httpR
8282
handler.SslProtocols = SslProtocols.Tls12;
8383
handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, chain, sslPolicyErrors) =>
8484
{
85-
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
85+
86+
if(_meshConnectConfiguration.BypassServerCertificateValidation)
87+
{
88+
_logger.LogWarning("Bypassing Server Certificate Validation");
89+
return true;
90+
}
91+
else if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
8692
{
8793
return true; // Everything is fine
8894
}
@@ -94,12 +100,26 @@ private async Task<HttpResponseMessage> SendHttpRequest(HttpRequestMessage httpR
94100
{
95101
chain.ChainPolicy.CustomTrustStore.Add(caCert);
96102
}
97-
if (cert != null)
103+
if (cert == null)
104+
{
105+
return false;
106+
}
107+
// Rebuild the chain with added certs
108+
if (!chain.Build(cert))
98109
{
99-
// Rebuild the chain with added certs
100-
return chain.Build(cert);
110+
return false;
101111
}
102-
return false;
112+
113+
bool isValidCA = mailboxConfiguration.serverSideCertCollection
114+
.Any(caCert => caCert.Thumbprint == cert.Thumbprint);
115+
if (!isValidCA)
116+
{
117+
_logger.LogError("Server certificate is not issued by a trusted CA!");
118+
return false;
119+
}
120+
121+
return true;
122+
103123
};
104124
}
105125

application/DotNetMeshClient/NHS.Mesh.Client/Configuration/MeshConnectConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ public class MeshConnectConfiguration : IMeshConnectConfiguration
5454
public bool ProxyUseDefaultCredentials { get; set; }
5555
/// <summary>Gets the chunk size in bytes for sending chunked messages 19Mb limit outside of HSCN 100Mb limit within</summary>
5656
public int ChunkSize { get; set; }
57+
/// <summary>Flag if the Servers Certificate is Checked against the CA Chain</summary>
58+
public bool BypassServerCertificateValidation { get; set; }
5759
}

application/DotNetMeshClient/NHS.Mesh.Client/Contracts/Configurations/IMeshConnectConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ public interface IMeshConnectConfiguration
5353
bool ProxyUseDefaultCredentials { get; set; }
5454
/// <summary>Gets the chunk size in bytes for sending chunked messages 19Mb limit outside of HSCN 100Mb limit within</summary>
5555
int ChunkSize { get; set; }
56+
/// <summary>Flag if the Servers Certificate is Checked against the CA Chain</summary>
57+
public bool BypassServerCertificateValidation { get; set; }
5658
}

application/DotNetMeshClient/NHS.Mesh.Client/Extensions/MeshMailboxBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public MeshMailboxBuilder(IServiceCollection services,Action<IMeshConnectConfigu
2828
MeshApiInboxUriPath = "inbox",
2929
MeshApiOutboxUriPath = "outbox",
3030
MeshApiAcknowledgeUriPath = "status/acknowledged",
31-
ChunkSize = 19 * 1024 * 1024// below the 20mb limit for external
31+
ChunkSize = 19 * 1024 * 1024,// below the 20mb limit for external
32+
BypassServerCertificateValidation = false
3233
};
3334

3435
options(_meshConnectConfiguration);

0 commit comments

Comments
 (0)