44using System . Diagnostics ;
55using System . Diagnostics . CodeAnalysis ;
66using System . Diagnostics . Tracing ;
7+ using System . Formats . Asn1 ;
78using System . Linq ;
89using System . Runtime . InteropServices ;
910using System . Security . Cryptography ;
@@ -19,16 +20,16 @@ namespace Microsoft.AspNetCore.Certificates.Generation;
1920
2021internal 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