mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-23 22:04:43 +08:00
Add abstraction layer for server certificate.
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
<description>This is a support library to integrate MQTTnet into AspNetCore.</description>
|
<description>This is a support library to integrate MQTTnet into AspNetCore.</description>
|
||||||
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<group targetFramework="netstandard2.0">
|
<group targetFramework="netstandard2.0">
|
||||||
<dependency id="MQTTnet" version="$nugetVersion" />
|
<dependency id="MQTTnet" version="$nugetVersion" />
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
<description>This is an extension library which provides a managed MQTT client with additional features using MQTTnet.</description>
|
<description>This is an extension library which provides a managed MQTT client with additional features using MQTTnet.</description>
|
||||||
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency id="MQTTnet" version="$nugetVersion" />
|
<dependency id="MQTTnet" version="$nugetVersion" />
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
<description>This is an extension library which allows executing synchronous device calls including a response using MQTTnet.</description>
|
<description>This is an extension library which allows executing synchronous device calls including a response using MQTTnet.</description>
|
||||||
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency id="MQTTnet" version="$nugetVersion" />
|
<dependency id="MQTTnet" version="$nugetVersion" />
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
<description>This is an extension library which allows using _WebSocket4Net_ as transport for MQTTnet clients.</description>
|
<description>This is an extension library which allows using _WebSocket4Net_ as transport for MQTTnet clients.</description>
|
||||||
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
<releaseNotes>For release notes please go to MQTTnet release notes (https://www.nuget.org/packages/MQTTnet/).</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<group targetFramework="netstandard1.3">
|
<group targetFramework="netstandard1.3">
|
||||||
<dependency id="MQTTnet" version="$nugetVersion" />
|
<dependency id="MQTTnet" version="$nugetVersion" />
|
||||||
|
|||||||
@@ -16,9 +16,10 @@
|
|||||||
* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!)
|
* [AspNetCore] Adjusted some namespaces (BREAKING CHANGE!)
|
||||||
* [Server] Adjusted some namespaces (BREAKING CHANGE!)
|
* [Server] Adjusted some namespaces (BREAKING CHANGE!)
|
||||||
* [Server] Added state checks (throw if not started etc.) for most server APIs.
|
* [Server] Added state checks (throw if not started etc.) for most server APIs.
|
||||||
|
* [Server] Exposed real X509Certificate2 (instead byte array) to TLS options (thanks to @borigas).
|
||||||
</releaseNotes>
|
</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin</tags>
|
<tags>MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin Blazor</tags>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<group targetFramework="netstandard1.3">
|
<group targetFramework="netstandard1.3">
|
||||||
<dependency id="NETStandard.Library" version="1.3.0" />
|
<dependency id="NETStandard.Library" version="1.3.0" />
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
|
namespace MQTTnet.Certificates
|
||||||
|
{
|
||||||
|
public class BlobCertificateProvider : ICertificateProvider
|
||||||
|
{
|
||||||
|
public BlobCertificateProvider(byte[] blob)
|
||||||
|
{
|
||||||
|
Blob = blob ?? throw new ArgumentNullException(nameof(blob));
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Blob { get; }
|
||||||
|
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
public X509Certificate2 GetCertificate()
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(Password))
|
||||||
|
{
|
||||||
|
// Use a different overload when no password is specified. Otherwise the constructor will fail.
|
||||||
|
return new X509Certificate2(Blob);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new X509Certificate2(Blob, Password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
|
namespace MQTTnet.Certificates
|
||||||
|
{
|
||||||
|
public interface ICertificateProvider
|
||||||
|
{
|
||||||
|
X509Certificate2 GetCertificate();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#if !WINDOWS_UWP
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
|
namespace MQTTnet.Certificates
|
||||||
|
{
|
||||||
|
public class X509CertificateProvider : ICertificateProvider
|
||||||
|
{
|
||||||
|
readonly X509Certificate2 _certificate;
|
||||||
|
|
||||||
|
public X509CertificateProvider(X509Certificate2 certificate)
|
||||||
|
{
|
||||||
|
_certificate = certificate ?? throw new ArgumentNullException(nameof(certificate));
|
||||||
|
}
|
||||||
|
|
||||||
|
public X509Certificate2 GetCertificate()
|
||||||
|
{
|
||||||
|
return _certificate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -44,30 +44,12 @@ namespace MQTTnet.Implementations
|
|||||||
|
|
||||||
if (options.TlsEndpointOptions?.IsEnabled == true)
|
if (options.TlsEndpointOptions?.IsEnabled == true)
|
||||||
{
|
{
|
||||||
if (options.TlsEndpointOptions.Certificate == null && options.TlsEndpointOptions.X509Certificate == null)
|
if (options.TlsEndpointOptions.CertificateProvider == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("TLS certificate is not set.");
|
throw new ArgumentException("TLS certificate is not set.");
|
||||||
}
|
}
|
||||||
else if (options.TlsEndpointOptions.Certificate != null && options.TlsEndpointOptions.X509Certificate != null)
|
|
||||||
{
|
var tlsCertificate = options.TlsEndpointOptions.CertificateProvider.GetCertificate();
|
||||||
throw new ArgumentException($"{nameof(MqttServerTlsTcpEndpointOptions.Certificate)} and {nameof(MqttServerTlsTcpEndpointOptions.X509Certificate)} cannot both be set");
|
|
||||||
}
|
|
||||||
|
|
||||||
X509Certificate2 tlsCertificate;
|
|
||||||
if (options.TlsEndpointOptions.X509Certificate != null)
|
|
||||||
{
|
|
||||||
tlsCertificate = options.TlsEndpointOptions.X509Certificate;
|
|
||||||
}
|
|
||||||
else if (string.IsNullOrEmpty(options.TlsEndpointOptions.CertificateCredentials?.Password))
|
|
||||||
{
|
|
||||||
// Use a different overload when no password is specified. Otherwise the constructor will fail.
|
|
||||||
tlsCertificate = new X509Certificate2(options.TlsEndpointOptions.Certificate);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tlsCertificate = new X509Certificate2(options.TlsEndpointOptions.Certificate, options.TlsEndpointOptions.CertificateCredentials.Password);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tlsCertificate.HasPrivateKey)
|
if (!tlsCertificate.HasPrivateKey)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("The certificate for TLS encryption must contain the private key.");
|
throw new InvalidOperationException("The certificate for TLS encryption must contain the private key.");
|
||||||
@@ -76,13 +58,13 @@ namespace MQTTnet.Implementations
|
|||||||
RegisterListeners(options.TlsEndpointOptions, tlsCertificate, _cancellationTokenSource.Token);
|
RegisterListeners(options.TlsEndpointOptions, tlsCertificate, _cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.FromResult(0);
|
return PlatformAbstractionLayer.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync()
|
public Task StopAsync()
|
||||||
{
|
{
|
||||||
Cleanup();
|
Cleanup();
|
||||||
return Task.FromResult(0);
|
return PlatformAbstractionLayer.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Security;
|
using System.Net.Security;
|
||||||
using System.Security.Authentication;
|
using System.Security.Authentication;
|
||||||
|
using MQTTnet.Certificates;
|
||||||
|
|
||||||
#if !WINDOWS_UWP
|
#if !WINDOWS_UWP
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
#endif
|
#endif
|
||||||
@@ -10,7 +12,7 @@ namespace MQTTnet.Server
|
|||||||
{
|
{
|
||||||
public class MqttServerOptionsBuilder
|
public class MqttServerOptionsBuilder
|
||||||
{
|
{
|
||||||
private readonly MqttServerOptions _options = new MqttServerOptions();
|
readonly MqttServerOptions _options = new MqttServerOptions();
|
||||||
|
|
||||||
public MqttServerOptionsBuilder WithConnectionBacklog(int value)
|
public MqttServerOptionsBuilder WithConnectionBacklog(int value)
|
||||||
{
|
{
|
||||||
@@ -85,17 +87,24 @@ namespace MQTTnet.Server
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !WINDOWS_UWP
|
||||||
public MqttServerOptionsBuilder WithEncryptionCertificate(byte[] value, IMqttServerCertificateCredentials credentials = null)
|
public MqttServerOptionsBuilder WithEncryptionCertificate(byte[] value, IMqttServerCertificateCredentials credentials = null)
|
||||||
{
|
{
|
||||||
_options.TlsEndpointOptions.Certificate = value;
|
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||||
_options.TlsEndpointOptions.CertificateCredentials = credentials;
|
|
||||||
|
_options.TlsEndpointOptions.CertificateProvider = new BlobCertificateProvider(value)
|
||||||
|
{
|
||||||
|
Password = credentials?.Password
|
||||||
|
};
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !WINDOWS_UWP
|
|
||||||
public MqttServerOptionsBuilder WithEncryptionCertificate(X509Certificate2 certificate)
|
public MqttServerOptionsBuilder WithEncryptionCertificate(X509Certificate2 certificate)
|
||||||
{
|
{
|
||||||
_options.TlsEndpointOptions.X509Certificate = certificate;
|
if (certificate == null) throw new ArgumentNullException(nameof(certificate));
|
||||||
|
|
||||||
|
_options.TlsEndpointOptions.CertificateProvider = new X509CertificateProvider(certificate);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,33 +1,56 @@
|
|||||||
using System.Net.Security;
|
using System;
|
||||||
using System.Security.Authentication;
|
using System.Security.Authentication;
|
||||||
#if !WINDOWS_UWP
|
using MQTTnet.Certificates;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace MQTTnet.Server
|
namespace MQTTnet.Server
|
||||||
{
|
{
|
||||||
public class MqttServerTlsTcpEndpointOptions : MqttServerTcpEndpointBaseOptions
|
public class MqttServerTlsTcpEndpointOptions : MqttServerTcpEndpointBaseOptions
|
||||||
{
|
{
|
||||||
|
ICertificateProvider _certificateProvider;
|
||||||
|
|
||||||
public MqttServerTlsTcpEndpointOptions()
|
public MqttServerTlsTcpEndpointOptions()
|
||||||
{
|
{
|
||||||
Port = 8883;
|
Port = 8883;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Obsolete("Please use CertificateProvider with 'BlobCertificateProvider' instead.")]
|
||||||
public byte[] Certificate { get; set; }
|
public byte[] Certificate { get; set; }
|
||||||
|
|
||||||
#if !WINDOWS_UWP
|
[Obsolete("Please use CertificateProvider with 'BlobCertificateProvider' including password property instead.")]
|
||||||
public X509Certificate2 X509Certificate { get; set; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public IMqttServerCertificateCredentials CertificateCredentials { get; set; }
|
public IMqttServerCertificateCredentials CertificateCredentials { get; set; }
|
||||||
|
|
||||||
|
#if !WINDOWS_UWP
|
||||||
|
public System.Net.Security.RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get; set; }
|
||||||
|
#endif
|
||||||
|
public ICertificateProvider CertificateProvider
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
// Backward compatibility only. Gets converted to auto property when
|
||||||
|
// obsolete properties are removed.
|
||||||
|
if (_certificateProvider != null)
|
||||||
|
{
|
||||||
|
return _certificateProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Certificate == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BlobCertificateProvider(Certificate)
|
||||||
|
{
|
||||||
|
Password = CertificateCredentials?.Password
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
set => _certificateProvider = value;
|
||||||
|
}
|
||||||
|
|
||||||
public bool ClientCertificateRequired { get; set; }
|
public bool ClientCertificateRequired { get; set; }
|
||||||
|
|
||||||
public bool CheckCertificateRevocation { get; set; }
|
public bool CheckCertificateRevocation { get; set; }
|
||||||
|
|
||||||
#if !WINDOWS_UWP
|
|
||||||
public RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get; set; }
|
|
||||||
#endif
|
|
||||||
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
|
public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user