Skip to content

Commit 91a3373

Browse files
author
John Judge
committed
Add certificate chain handling for certificate specified in the default
section
1 parent cd5ca69 commit 91a3373

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

src/Servers/Kestrel/Core/src/IHttpsConfigurationService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ void ApplyHttpsConfiguration(
9090
internal readonly struct CertificateAndConfig
9191
{
9292
public readonly X509Certificate2 Certificate;
93+
public readonly X509Certificate2Collection CertificateChain;
9394
public readonly CertificateConfig CertificateConfig;
9495

9596
public CertificateAndConfig(X509Certificate2 certificate, CertificateConfig certificateConfig)
9697
{
9798
Certificate = certificate;
9899
CertificateConfig = certificateConfig;
100+
CertificateChain = [];
101+
}
102+
103+
public CertificateAndConfig(X509Certificate2 certificate, CertificateConfig certificateConfig, X509Certificate2Collection certificateChain){
104+
Certificate = certificate;
105+
CertificateConfig = certificateConfig;
106+
CertificateChain = certificateChain;
99107
}
100108
}

src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ internal KestrelConfigurationLoader(
7777
private CertificateConfig? DefaultCertificateConfig { get; set; }
7878
internal X509Certificate2? DefaultCertificate { get; set; }
7979

80+
internal X509Certificate2Collection? DefaultCertificateChain {get; set;}
8081
/// <summary>
8182
/// Specifies a configuration Action to run when an endpoint with the given name is loaded from configuration.
8283
/// </summary>
@@ -345,12 +346,14 @@ internal void ProcessEndpointsToAdd()
345346

346347
DefaultCertificateConfig = null;
347348
DefaultCertificate = null;
349+
DefaultCertificateChain = null;
348350

349351
ConfigurationReader = new ConfigurationReader(Configuration);
350352

351353
if (_httpsConfigurationService.IsInitialized && _httpsConfigurationService.LoadDefaultCertificate(ConfigurationReader) is CertificateAndConfig certPair)
352354
{
353355
DefaultCertificate = certPair.Certificate;
356+
DefaultCertificateChain = certPair.CertificateChain;
354357
DefaultCertificateConfig = certPair.CertificateConfig;
355358
}
356359

src/Servers/Kestrel/Core/src/KestrelServerOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ internal void ApplyDefaultCertificate(HttpsConnectionAdapterOptions httpsOptions
303303
if (ConfigurationLoader?.DefaultCertificate is X509Certificate2 certificateFromLoader)
304304
{
305305
httpsOptions.ServerCertificate = certificateFromLoader;
306+
if (ConfigurationLoader?.DefaultCertificateChain is X509Certificate2Collection certificateChainFromLoader){
307+
httpsOptions.ServerCertificateChain = certificateChainFromLoader;
308+
}
306309
return;
307310
}
308311

src/Servers/Kestrel/Core/src/TlsConfigurationLoader.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,12 @@ public ListenOptions UseHttpsWithSni(
128128
{
129129
if (configurationReader.Certificates.TryGetValue("Default", out var defaultCertConfig))
130130
{
131-
var (defaultCert, _ /* cert chain */) = _certificateConfigLoader.LoadCertificate(defaultCertConfig, "Default");
131+
var (defaultCert, defaultCertChain) = _certificateConfigLoader.LoadCertificate(defaultCertConfig, "Default");
132132
if (defaultCert != null)
133133
{
134+
if(defaultCertChain != null){
135+
return new CertificateAndConfig(defaultCert,defaultCertConfig,defaultCertChain);
136+
}
134137
return new CertificateAndConfig(defaultCert, defaultCertConfig);
135138
}
136139
}

0 commit comments

Comments
 (0)