mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-22 04:43:04 +08:00
Add abstraction layer for server certificate.
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user