Skip to content

Commit

Permalink
Fix certificate loading warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
barnstee committed Jan 15, 2025
1 parent 5131e55 commit 80bc07a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Controllers/CertManagerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ public async Task<IActionResult> Load(IFormFile file)
{
byte[] bytes = new byte[file.Length];
content.ReadExactly(bytes, 0, (int)file.Length);
certificate = new X509Certificate2(bytes);

if (file.FileName.ToLower().EndsWith(".pfx"))
{
certificate = X509CertificateLoader.LoadPkcs12(bytes, string.Empty);
}
else
{
certificate = X509CertificateLoader.LoadCertificate(bytes);
}
}

// store in our own trust list
Expand Down
11 changes: 10 additions & 1 deletion Controllers/ConfigController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ public async Task<ActionResult> LocalCertOpen(IFormFile file)
}

// update cert file hash and expiry
X509Certificate2 cert = new X509Certificate2(filePath);
X509Certificate2 cert;
if (filePath.ToLower().EndsWith(".pfx"))
{
cert = X509CertificateLoader.LoadPkcs12FromFile(filePath, string.Empty);
}
else
{
cert = X509CertificateLoader.LoadCertificateFromFile(filePath);
}

Settings.Instance.MQTTClientCertThumbprint = cert.Thumbprint;
Settings.Instance.MQTTClientCertExpiry = cert.NotAfter;
}
Expand Down
10 changes: 8 additions & 2 deletions MQTTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Opc.Ua.Cloud.Publisher.Configuration
{
using Microsoft.Extensions.Logging;
using MQTTnet;
using MQTTnet.Adapter;
using MQTTnet.Exceptions;
using MQTTnet.Packets;
using MQTTnet.Protocol;
Expand Down Expand Up @@ -57,7 +56,14 @@ X509CertificateCollection IMqttClientCertificatesProvider.GetCertificates()
if (Settings.Instance.UseCustomCertAuth)
{
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "customclientcert");
appCert = new X509Certificate2(filePath);
if (filePath.ToLower().EndsWith(".pfx"))
{
appCert = X509CertificateLoader.LoadPkcs12FromFile(filePath, string.Empty);
}
else
{
appCert = X509CertificateLoader.LoadCertificateFromFile(filePath);
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion UAApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private async Task CreateIssuerCert()
}
else
{
IssuerCert = new X509Certificate2(File.ReadAllBytes(issuerCerts[0]));
IssuerCert = X509CertificateLoader.LoadPkcs12FromFile(issuerCerts[0], string.Empty);
}

Settings.Instance.UAIssuerCertThumbprint = IssuerCert.Thumbprint;
Expand Down

0 comments on commit 80bc07a

Please sign in to comment.