feat(gateway): generate self-signed ECDSA cert with SANs
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
using ZB.MOM.WW.MxGateway.Server.Security.Tls;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Tests.Security.Tls;
|
||||
|
||||
public sealed class SelfSignedCertificateProviderTests
|
||||
{
|
||||
private static SelfSignedCertificateProvider CreateProvider(TlsOptions options, FakeTimeProvider time)
|
||||
=> new(options, NullLogger<SelfSignedCertificateProvider>.Instance, time);
|
||||
|
||||
[Fact]
|
||||
public void GenerateCertificate_HasExpectedSansEkuAndValidity()
|
||||
{
|
||||
FakeTimeProvider time = new(new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero));
|
||||
TlsOptions options = new() { ValidityYears = 7, AdditionalDnsNames = ["gw.internal"] };
|
||||
|
||||
using X509Certificate2 cert = CreateProvider(options, time).GenerateCertificate();
|
||||
|
||||
Assert.Equal(time.GetUtcNow().AddYears(7).UtcDateTime.Date, cert.NotAfter.ToUniversalTime().Date);
|
||||
Assert.True(cert.NotBefore.ToUniversalTime() < time.GetUtcNow().UtcDateTime);
|
||||
Assert.True(cert.HasPrivateKey);
|
||||
|
||||
string sans = ReadSubjectAltNames(cert);
|
||||
Assert.Contains("localhost", sans);
|
||||
Assert.Contains("gw.internal", sans);
|
||||
|
||||
X509EnhancedKeyUsageExtension eku = cert.Extensions.OfType<X509EnhancedKeyUsageExtension>().Single();
|
||||
Assert.Contains(eku.EnhancedKeyUsages.Cast<System.Security.Cryptography.Oid>(),
|
||||
o => o.Value == "1.3.6.1.5.5.7.3.1"); // serverAuth
|
||||
}
|
||||
|
||||
private static string ReadSubjectAltNames(X509Certificate2 cert)
|
||||
=> cert.Extensions
|
||||
.First(e => e.Oid?.Value == "2.5.29.17")
|
||||
.Format(false);
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
|
||||
Reference in New Issue
Block a user