Add an x509 certificate parameter

This commit is contained in:
Ben Origas
2020-04-10 17:16:42 -05:00
parent 4b08bb41f5
commit 28641892f5
3 changed files with 16 additions and 2 deletions
@@ -44,13 +44,17 @@ namespace MQTTnet.Implementations
if (options.TlsEndpointOptions?.IsEnabled == true)
{
if (options.TlsEndpointOptions.Certificate == null)
if (options.TlsEndpointOptions.Certificate == null && options.TlsEndpointOptions.X509Certificate == null)
{
throw new ArgumentException("TLS certificate is not set.");
}
X509Certificate2 tlsCertificate;
if (string.IsNullOrEmpty(options.TlsEndpointOptions.CertificateCredentials?.Password))
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);
@@ -2,6 +2,7 @@
using System.Net;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace MQTTnet.Server
{
@@ -89,6 +90,12 @@ namespace MQTTnet.Server
return this;
}
public MqttServerOptionsBuilder WithEncryptionCertificate(X509Certificate2 certificate)
{
_options.TlsEndpointOptions.X509Certificate = certificate;
return this;
}
public MqttServerOptionsBuilder WithEncryptionSslProtocol(SslProtocols value)
{
_options.TlsEndpointOptions.SslProtocol = value;
@@ -1,5 +1,6 @@
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace MQTTnet.Server
{
@@ -12,6 +13,8 @@ namespace MQTTnet.Server
public byte[] Certificate { get; set; }
public X509Certificate2 X509Certificate { get; set; }
public IMqttServerCertificateCredentials CertificateCredentials { get; set; }
public bool ClientCertificateRequired { get; set; }