Skip to content

Commit 9e9cc27

Browse files
Fix: Apply PEM cert support to HTTP client path and dispose _caCertificate
Co-authored-by: thomasneuberger <23504477+thomasneuberger@users.noreply.github.com>
1 parent 053347a commit 9e9cc27

3 files changed

Lines changed: 36 additions & 29 deletions

File tree

TgHomeBot.SmartHome.HomeAssistant/Bootstrap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static IServiceCollection AddHomeAssistant(this IServiceCollection servic
2424
var handler = new HttpClientHandler();
2525
if (!string.IsNullOrEmpty(options.CertificateAuthorityPath))
2626
{
27-
var certificate = X509CertificateLoader.LoadCertificateFromFile(options.CertificateAuthorityPath);
27+
var certificate = CertificateHelper.LoadCertificate(options.CertificateAuthorityPath);
2828
handler.ServerCertificateCustomValidationCallback = (_, cert, chain, errors) =>
2929
{
3030
if (errors == SslPolicyErrors.None) return true;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Security.Cryptography.X509Certificates;
2+
3+
namespace TgHomeBot.SmartHome.HomeAssistant;
4+
5+
internal static class CertificateHelper
6+
{
7+
internal static X509Certificate2 LoadCertificate(string path)
8+
{
9+
const int maxScanLines = 50;
10+
var isPem = false;
11+
12+
using (var reader = new StreamReader(path))
13+
{
14+
string? line;
15+
var linesRead = 0;
16+
17+
while (linesRead < maxScanLines && (line = reader.ReadLine()) is not null)
18+
{
19+
linesRead++;
20+
21+
if (line.Contains("-----BEGIN CERTIFICATE-----", StringComparison.Ordinal))
22+
{
23+
isPem = true;
24+
break;
25+
}
26+
}
27+
}
28+
29+
return isPem
30+
? X509Certificate2.CreateFromPemFile(path)
31+
: X509CertificateLoader.LoadCertificateFromFile(path);
32+
}
33+
}

TgHomeBot.SmartHome.HomeAssistant/HomeAssistantMonitor.cs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class HomeAssistantMonitor(
2727
private readonly CancellationTokenSource _cancellationTokenSource = new();
2828
private readonly X509Certificate2? _caCertificate = string.IsNullOrEmpty(options.Value.CertificateAuthorityPath)
2929
? null
30-
: LoadCertificate(options.Value.CertificateAuthorityPath);
30+
: CertificateHelper.LoadCertificate(options.Value.CertificateAuthorityPath);
3131

3232
private bool _reconnect;
3333

@@ -276,33 +276,6 @@ private async Task ProcessEvent(string message)
276276
}
277277
}
278278

279-
private static X509Certificate2 LoadCertificate(string path)
280-
{
281-
const int maxScanLines = 50;
282-
var isPem = false;
283-
284-
using (var reader = new StreamReader(path))
285-
{
286-
string? line;
287-
var linesRead = 0;
288-
289-
while (linesRead < maxScanLines && (line = reader.ReadLine()) is not null)
290-
{
291-
linesRead++;
292-
293-
if (line.Contains("-----BEGIN CERTIFICATE-----", StringComparison.Ordinal))
294-
{
295-
isPem = true;
296-
break;
297-
}
298-
}
299-
}
300-
301-
return isPem
302-
? X509Certificate2.CreateFromPemFile(path)
303-
: X509CertificateLoader.LoadCertificateFromFile(path);
304-
}
305-
306279
private static DeviceState GetState(DeviceStateThresholds deviceThresholds, string state)
307280
{
308281
if (!float.TryParse(state, out var value))
@@ -353,5 +326,6 @@ public void Dispose()
353326
_cancellationTokenSource.Cancel();
354327
_webSocket?.Dispose();
355328
_cancellationTokenSource.Dispose();
329+
_caCertificate?.Dispose();
356330
}
357331
}

0 commit comments

Comments
 (0)