using Microsoft.Extensions.Configuration; namespace ZB.MOM.WW.MxGateway.Server.Security.Tls; /// /// Inspects the Kestrel configuration to decide whether the gateway must supply /// a generated default certificate (an HTTPS endpoint exists with no certificate /// of its own). /// public static class KestrelTlsInspector { /// /// Returns when at least one HTTPS endpoint in /// Kestrel:Endpoints has no certificate of its own (no /// Certificate:Path, Certificate:Subject, or /// Certificate:Thumbprint), meaning the gateway must supply a /// generated fallback certificate. /// public static bool RequiresGeneratedCertificate(IConfiguration configuration) { IConfigurationSection endpoints = configuration.GetSection("Kestrel:Endpoints"); foreach (IConfigurationSection endpoint in endpoints.GetChildren()) { string? url = endpoint["Url"]; if (string.IsNullOrWhiteSpace(url) || !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { continue; } IConfigurationSection certificate = endpoint.GetSection("Certificate"); bool hasOwnCertificate = !string.IsNullOrWhiteSpace(certificate["Path"]) || !string.IsNullOrWhiteSpace(certificate["Subject"]) || !string.IsNullOrWhiteSpace(certificate["Thumbprint"]); if (!hasOwnCertificate) { return true; } } return false; } }