Skip to content

Commit

Permalink
Check the development certificate’s valid dates
Browse files Browse the repository at this point in the history
  • Loading branch information
NCarlsonMSFT committed Dec 6, 2022
1 parent 82daf67 commit 49d04ad
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ private void LoadDefaultCert()
{
var certificate = new X509Certificate2(certificatePath, certificateConfig.Password);

if (IsDevelopmentCertificate(certificate))
if (IsValidDevelopmentCertificate(certificate))
{
return (certificate, certificateConfig);
}
Expand All @@ -450,13 +450,20 @@ private void LoadDefaultCert()
return (null, null);
}

private static bool IsDevelopmentCertificate(X509Certificate2 certificate)
private static bool IsValidDevelopmentCertificate(X509Certificate2 certificate)
{
if (!string.Equals(certificate.Subject, "CN=localhost", StringComparison.Ordinal))
{
return false;
}

DateTimeOffset currentDate = DateTimeOffset.Now;
if (certificate.NotBefore > currentDate ||
currentDate > certificate.NotAfter)
{
return false;
}

foreach (var ext in certificate.Extensions)
{
if (string.Equals(ext.Oid?.Value, CertificateManager.AspNetHttpsOid, StringComparison.Ordinal))
Expand Down

0 comments on commit 49d04ad

Please sign in to comment.