-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds WithCaFile TlsOption to validate the connection with X509ChainPolicy.CustomRootTrust #1848
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,24 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL | ||
BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG | ||
A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU | ||
BgNVBAMMDW1vc3F1aXR0by5vcmcxHzAdBgkqhkiG9w0BCQEWEHJvZ2VyQGF0Y2hv | ||
by5vcmcwHhcNMjAwNjA5MTEwNjM5WhcNMzAwNjA3MTEwNjM5WjCBkDELMAkGA1UE | ||
BhMCR0IxFzAVBgNVBAgMDlVuaXRlZCBLaW5nZG9tMQ4wDAYDVQQHDAVEZXJieTES | ||
MBAGA1UECgwJTW9zcXVpdHRvMQswCQYDVQQLDAJDQTEWMBQGA1UEAwwNbW9zcXVp | ||
dHRvLm9yZzEfMB0GCSqGSIb3DQEJARYQcm9nZXJAYXRjaG9vLm9yZzCCASIwDQYJ | ||
KoZIhvcNAQEBBQADggEPADCCAQoCggEBAME0HKmIzfTOwkKLT3THHe+ObdizamPg | ||
UZmD64Tf3zJdNeYGYn4CEXbyP6fy3tWc8S2boW6dzrH8SdFf9uo320GJA9B7U1FW | ||
Te3xda/Lm3JFfaHjkWw7jBwcauQZjpGINHapHRlpiCZsquAthOgxW9SgDgYlGzEA | ||
s06pkEFiMw+qDfLo/sxFKB6vQlFekMeCymjLCbNwPJyqyhFmPWwio/PDMruBTzPH | ||
3cioBnrJWKXc3OjXdLGFJOfj7pP0j/dr2LH72eSvv3PQQFl90CZPFhrCUcRHSSxo | ||
E6yjGOdnz7f6PveLIB574kQORwt8ePn0yidrTC1ictikED3nHYhMUOUCAwEAAaNT | ||
MFEwHQYDVR0OBBYEFPVV6xBUFPiGKDyo5V3+Hbh4N9YSMB8GA1UdIwQYMBaAFPVV | ||
6xBUFPiGKDyo5V3+Hbh4N9YSMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEL | ||
BQADggEBAGa9kS21N70ThM6/Hj9D7mbVxKLBjVWe2TPsGfbl3rEDfZ+OKRZ2j6AC | ||
6r7jb4TZO3dzF2p6dgbrlU71Y/4K0TdzIjRj3cQ3KSm41JvUQ0hZ/c04iGDg/xWf | ||
+pp58nfPAYwuerruPNWmlStWAXf0UTqRtg4hQDWBuUFDJTuWuuBvEXudz74eh/wK | ||
sMwfu1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839 | ||
LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE | ||
m/XriWr/Cq4h/JfB7NTsezVslgkBaoU= | ||
-----END CERTIFICATE----- |
51 changes: 51 additions & 0 deletions
51
Source/MQTTnet/Client/Options/MqttClientCaFileCertificateValidationHandler.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,51 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace MQTTnet.Client.Options | ||
{ | ||
internal class MqttClientCaFileCertificateValidationHandler | ||
{ | ||
internal static bool Handle(MqttClientCertificateValidationEventArgs cvArgs) | ||
{ | ||
if (cvArgs.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
// only configure the chain if RemoteChainErrors is the only error | ||
if (cvArgs.SslPolicyErrors == System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) | ||
{ | ||
string caFile = cvArgs.ClientOptions.TlsOptions.CaFile; | ||
if (!string.IsNullOrEmpty(caFile)) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
X509Certificate2Collection caCerts = new X509Certificate2Collection(); | ||
caCerts.ImportFromPemFile(caFile); | ||
cvArgs.Chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust; | ||
cvArgs.Chain.ChainPolicy.CustomTrustStore.AddRange(caCerts); | ||
cvArgs.Chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; | ||
X509Certificate2 cert = new X509Certificate2(cvArgs.Certificate); | ||
var res = cvArgs.Chain.Build(cert); | ||
if (!res) | ||
{ | ||
cvArgs.Chain.ChainStatus.ToList().ForEach(s => Trace.TraceWarning(s.StatusInformation)); | ||
} | ||
return res; | ||
#else | ||
throw new InvalidOperationException("WithCaFile requires .NET6 or greater"); | ||
#endif | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Collections.Generic; | ||
using System.Security.Authentication; | ||
using System.Security.Cryptography.X509Certificates; | ||
using MQTTnet.Client.Options; | ||
#if NETCOREAPP3_1_OR_GREATER | ||
using System.Net.Security; | ||
#endif | ||
|
@@ -45,6 +46,11 @@ public MqttClientTlsOptionsBuilder WithCertificateValidationHandler(Func<MqttCli | |
throw new ArgumentNullException(nameof(certificateValidationHandler)); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(_tlsOptions.CaFile)) | ||
{ | ||
throw new InvalidOperationException("WithCaFile cannot be used with a custom Certificate Validation Handler"); | ||
} | ||
|
||
_tlsOptions.CertificateValidationHandler = certificateValidationHandler; | ||
return this; | ||
} | ||
|
@@ -131,6 +137,17 @@ public MqttClientTlsOptionsBuilder WithCipherSuitesPolicy(EncryptionPolicy encry | |
_tlsOptions.EncryptionPolicy = encryptionPolicy; | ||
return this; | ||
} | ||
|
||
public MqttClientTlsOptionsBuilder WithCaFile(string caFile) | ||
{ | ||
if (_tlsOptions.CertificateValidationHandler != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WithCaFile needs to configure the CertificateValidationHandler, if it was manually assigned this throws an exception. |
||
{ | ||
throw new InvalidOperationException("WithCaFile cannot be used with a custom Certificate Validation Handler"); | ||
} | ||
WithCertificateValidationHandler(MqttClientCaFileCertificateValidationHandler.Handle); | ||
_tlsOptions.CaFile = caFile; | ||
return this; | ||
} | ||
#endif | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private CAs are very likely to do not offer revocation endpoints.