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
@@ -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