Skip to content

Commit 55212a4

Browse files
committed
Add Subject Key Identifier and Authority Key Identifier extensions to the generated dev cert
1 parent 48fa780 commit 55212a4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/Shared/CertificateGeneration/CertificateManager.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Diagnostics.Tracing;
7+
using System.Formats.Asn1;
78
using System.Linq;
89
using System.Runtime.InteropServices;
910
using System.Security.Cryptography;
@@ -19,16 +20,16 @@ namespace Microsoft.AspNetCore.Certificates.Generation;
1920

2021
internal abstract class CertificateManager
2122
{
22-
internal const int CurrentAspNetCoreCertificateVersion = 4;
23-
internal const int CurrentMinimumAspNetCoreCertificateVersion = 4;
23+
internal const int CurrentAspNetCoreCertificateVersion = 5;
24+
internal const int CurrentMinimumAspNetCoreCertificateVersion = 5;
2425

2526
// OID used for HTTPS certs
2627
internal const string AspNetHttpsOid = "1.3.6.1.4.1.311.84.1.1";
2728
internal const string AspNetHttpsOidFriendlyName = "ASP.NET Core HTTPS development certificate";
2829

2930
private const string ServerAuthenticationEnhancedKeyUsageOid = "1.3.6.1.5.5.7.3.1";
3031
private const string ServerAuthenticationEnhancedKeyUsageOidFriendlyName = "Server Authentication";
31-
32+
3233
// dns names of the host from a container
3334
private const string LocalhostDockerHttpsDnsName = "host.docker.internal";
3435
private const string ContainersDockerHttpsDnsName = "host.containers.internal";
@@ -828,6 +829,18 @@ internal static X509Certificate2 CreateSelfSignedCertificate(
828829
request.CertificateExtensions.Add(extension);
829830
}
830831

832+
// Only add the SKI and AKI extensions if neither is already present.
833+
if (!request.CertificateExtensions.OfType<X509SubjectKeyIdentifierExtension>().Any() && !request.CertificateExtensions.OfType<X509AuthorityKeyIdentifierExtension>().Any())
834+
{
835+
// RFC 5280 section 4.2.1.2
836+
var subjectKeyIdentifier = new X509SubjectKeyIdentifierExtension(new PublicKey(key), critical: false);
837+
// RFC 5280 section 4.2.1.1
838+
var authorityKeyIdentifier = X509AuthorityKeyIdentifierExtension.CreateFromSubjectKeyIdentifier(subjectKeyIdentifier);
839+
840+
request.CertificateExtensions.Add(subjectKeyIdentifier);
841+
request.CertificateExtensions.Add(authorityKeyIdentifier);
842+
}
843+
831844
var result = request.CreateSelfSigned(notBefore, notAfter);
832845
return result;
833846

0 commit comments

Comments
 (0)