330e665f6b
Drop KeyEncipherment from the self-signed cert's key-usage extension — it is semantically wrong for ECDSA (RSA key-transport only); DigitalSignature alone is correct for TLS 1.3 / ECDHE server certs. CertificateRequest is unchanged (not IDisposable in .NET 10). Test now also asserts MachineName, 127.0.0.1 and IPv6 loopback are present in the SAN extension.
189 lines
6.9 KiB
C#
189 lines
6.9 KiB
C#
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Microsoft.Extensions.Logging;
|
|
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Server.Security.Tls;
|
|
|
|
/// <summary>
|
|
/// Generates and persists a long-lived self-signed certificate used as the
|
|
/// Kestrel HTTPS default when no operator certificate is configured.
|
|
/// </summary>
|
|
public sealed class SelfSignedCertificateProvider
|
|
{
|
|
private const string ServerAuthOid = "1.3.6.1.5.5.7.3.1";
|
|
|
|
private readonly TlsOptions _options;
|
|
private readonly ILogger<SelfSignedCertificateProvider> _logger;
|
|
private readonly TimeProvider _timeProvider;
|
|
|
|
public SelfSignedCertificateProvider(
|
|
TlsOptions options,
|
|
ILogger<SelfSignedCertificateProvider> logger,
|
|
TimeProvider timeProvider)
|
|
{
|
|
_options = options;
|
|
_logger = logger;
|
|
_timeProvider = timeProvider;
|
|
}
|
|
|
|
/// <summary>Creates a fresh in-memory ECDSA P-256 self-signed certificate.</summary>
|
|
public X509Certificate2 GenerateCertificate()
|
|
{
|
|
using ECDsa key = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
|
CertificateRequest request = new(
|
|
new X500DistinguishedName("CN=MxAccessGateway Self-Signed"),
|
|
key,
|
|
HashAlgorithmName.SHA256);
|
|
|
|
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, true));
|
|
request.CertificateExtensions.Add(new X509KeyUsageExtension(
|
|
X509KeyUsageFlags.DigitalSignature,
|
|
critical: true));
|
|
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(
|
|
[new Oid(ServerAuthOid, "Server Authentication")],
|
|
critical: false));
|
|
|
|
SubjectAlternativeNameBuilder san = new();
|
|
san.AddDnsName("localhost");
|
|
string machine = Environment.MachineName;
|
|
if (!string.IsNullOrWhiteSpace(machine))
|
|
{
|
|
san.AddDnsName(machine);
|
|
}
|
|
|
|
foreach (string extra in _options.AdditionalDnsNames)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(extra))
|
|
{
|
|
san.AddDnsName(extra);
|
|
}
|
|
}
|
|
|
|
san.AddIpAddress(IPAddress.Loopback);
|
|
san.AddIpAddress(IPAddress.IPv6Loopback);
|
|
request.CertificateExtensions.Add(san.Build());
|
|
|
|
DateTimeOffset now = _timeProvider.GetUtcNow();
|
|
return request.CreateSelfSigned(now.AddDays(-1), now.AddYears(_options.ValidityYears));
|
|
}
|
|
|
|
/// <summary>Loads the persisted certificate, regenerating when missing,
|
|
/// expired (and allowed), or unreadable.</summary>
|
|
public X509Certificate2 LoadOrCreate()
|
|
{
|
|
string path = _options.SelfSignedCertPath;
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"MxGateway:Tls:SelfSignedCertPath must be set when an HTTPS endpoint has no certificate.");
|
|
}
|
|
|
|
if (File.Exists(path))
|
|
{
|
|
try
|
|
{
|
|
X509Certificate2 existing = X509CertificateLoader.LoadPkcs12FromFile(path, password: null, KeyStorageFlags());
|
|
if (existing.NotAfter.ToUniversalTime() > _timeProvider.GetUtcNow().UtcDateTime)
|
|
{
|
|
Log("Loaded", existing);
|
|
return existing;
|
|
}
|
|
|
|
if (!_options.RegenerateIfExpired)
|
|
{
|
|
string notAfter = existing.NotAfter.ToUniversalTime().ToString("u");
|
|
existing.Dispose();
|
|
throw new InvalidOperationException(
|
|
$"Persisted gateway certificate at '{path}' expired on {notAfter} " +
|
|
"and MxGateway:Tls:RegenerateIfExpired is false.");
|
|
}
|
|
|
|
_logger.LogWarning(
|
|
"Persisted gateway certificate at {Path} expired on {NotAfter:u}; regenerating.",
|
|
path, existing.NotAfter.ToUniversalTime());
|
|
existing.Dispose();
|
|
}
|
|
catch (CryptographicException ex)
|
|
{
|
|
_logger.LogWarning(ex,
|
|
"Persisted gateway certificate at {Path} is unreadable; regenerating.", path);
|
|
}
|
|
}
|
|
|
|
return GenerateAndPersist(path);
|
|
}
|
|
|
|
private X509Certificate2 GenerateAndPersist(string path)
|
|
{
|
|
using X509Certificate2 generated = GenerateCertificate();
|
|
byte[] pfx = generated.Export(X509ContentType.Pkcs12);
|
|
|
|
string? directory = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrEmpty(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
string temp = path + ".tmp";
|
|
File.WriteAllBytes(temp, pfx);
|
|
File.Move(temp, path, overwrite: true);
|
|
HardenPermissions(path);
|
|
|
|
X509Certificate2 loaded = X509CertificateLoader.LoadPkcs12FromFile(path, password: null, KeyStorageFlags());
|
|
Log("Generated", loaded);
|
|
return loaded;
|
|
}
|
|
|
|
private static X509KeyStorageFlags KeyStorageFlags()
|
|
=> OperatingSystem.IsWindows()
|
|
? X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable
|
|
: X509KeyStorageFlags.Exportable;
|
|
|
|
private static void HardenPermissions(string path)
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
HardenWindowsAcl(path);
|
|
}
|
|
else
|
|
{
|
|
File.SetUnixFileMode(path, UnixFileMode.UserRead | UnixFileMode.UserWrite);
|
|
}
|
|
}
|
|
|
|
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
|
|
private static void HardenWindowsAcl(string path)
|
|
{
|
|
FileInfo file = new(path);
|
|
System.Security.AccessControl.FileSecurity security = new();
|
|
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
|
|
|
|
foreach (System.Security.Principal.WellKnownSidType sid in new[]
|
|
{
|
|
System.Security.Principal.WellKnownSidType.LocalSystemSid,
|
|
System.Security.Principal.WellKnownSidType.BuiltinAdministratorsSid,
|
|
})
|
|
{
|
|
System.Security.Principal.SecurityIdentifier identifier = new(sid, null);
|
|
security.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(
|
|
identifier,
|
|
System.Security.AccessControl.FileSystemRights.FullControl,
|
|
System.Security.AccessControl.AccessControlType.Allow));
|
|
}
|
|
|
|
file.SetAccessControl(security);
|
|
}
|
|
|
|
private void Log(string action, X509Certificate2 cert)
|
|
{
|
|
string sans = cert.Extensions
|
|
.FirstOrDefault(e => e.Oid?.Value == "2.5.29.17")?
|
|
.Format(false) ?? "(none)";
|
|
_logger.LogInformation(
|
|
"{Action} gateway self-signed certificate: thumbprint={Thumbprint}, notAfter={NotAfter:u}, sans={Sans}",
|
|
action, cert.Thumbprint, cert.NotAfter.ToUniversalTime(), sans);
|
|
}
|
|
}
|