Add abstraction layer for server certificate.

This commit is contained in:
Christian Kratky
2020-05-26 23:46:43 +02:00
parent e60a732b61
commit c779b394f1
11 changed files with 118 additions and 44 deletions
@@ -2,6 +2,8 @@
using System.Net;
using System.Net.Security;
using System.Security.Authentication;
using MQTTnet.Certificates;
#if !WINDOWS_UWP
using System.Security.Cryptography.X509Certificates;
#endif
@@ -10,7 +12,7 @@ namespace MQTTnet.Server
{
public class MqttServerOptionsBuilder
{
private readonly MqttServerOptions _options = new MqttServerOptions();
readonly MqttServerOptions _options = new MqttServerOptions();
public MqttServerOptionsBuilder WithConnectionBacklog(int value)
{
@@ -85,17 +87,24 @@ namespace MQTTnet.Server
return this;
}
#if !WINDOWS_UWP
public MqttServerOptionsBuilder WithEncryptionCertificate(byte[] value, IMqttServerCertificateCredentials credentials = null)
{
_options.TlsEndpointOptions.Certificate = value;
_options.TlsEndpointOptions.CertificateCredentials = credentials;
if (value == null) throw new ArgumentNullException(nameof(value));
_options.TlsEndpointOptions.CertificateProvider = new BlobCertificateProvider(value)
{
Password = credentials?.Password
};
return this;
}
#if !WINDOWS_UWP
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;
}
#endif