|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.IO.Pipelines; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Security; |
| 7 | +using Microsoft.AspNetCore.Connections; |
| 8 | +using Microsoft.AspNetCore.Hosting; |
| 9 | +using Microsoft.AspNetCore.Http.Features; |
| 10 | +using Microsoft.AspNetCore.Server.Kestrel.Core.Internal; |
| 11 | +using Microsoft.AspNetCore.Server.Kestrel.Https; |
| 12 | +using Microsoft.AspNetCore.Server.Kestrel.Https.Internal; |
| 13 | +using Microsoft.Extensions.Hosting; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | + |
| 16 | +namespace Microsoft.AspNetCore.Server.Kestrel.Core; |
| 17 | + |
| 18 | +/// <inheritdoc /> |
| 19 | +internal sealed class HttpsConfigurationService : IHttpsConfigurationService |
| 20 | +{ |
| 21 | + private readonly IInitializer? _initializer; |
| 22 | + private bool _isInitialized; |
| 23 | + |
| 24 | + private TlsConfigurationLoader? _tlsConfigurationLoader; |
| 25 | + private Action<FeatureCollection, ListenOptions>? _populateMultiplexedTransportFeatures; |
| 26 | + private Func<ListenOptions, ListenOptions>? _useHttpsWithDefaults; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Create an uninitialized <see cref="HttpsConfigurationService"/>. |
| 30 | + /// To initialize it later, call <see cref="Initialize"/>. |
| 31 | + /// </summary> |
| 32 | + public HttpsConfigurationService() |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Create an initialized <see cref="HttpsConfigurationService"/>. |
| 38 | + /// </summary> |
| 39 | + /// <remarks> |
| 40 | + /// In practice, <see cref="Initialize"/> won't be called until it's needed. |
| 41 | + /// </remarks> |
| 42 | + public HttpsConfigurationService(IInitializer initializer) |
| 43 | + { |
| 44 | + _initializer = initializer; |
| 45 | + } |
| 46 | + |
| 47 | + /// <inheritdoc /> |
| 48 | + // If there's an initializer, it *can* be initialized, even though it might not be yet. |
| 49 | + // Use explicit interface implentation so we don't accidentally call it within this class. |
| 50 | + bool IHttpsConfigurationService.IsInitialized => _isInitialized || _initializer is not null; |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + public void Initialize( |
| 54 | + IHostEnvironment hostEnvironment, |
| 55 | + ILogger<KestrelServer> serverLogger, |
| 56 | + ILogger<HttpsConnectionMiddleware> httpsLogger) |
| 57 | + { |
| 58 | + if (_isInitialized) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + _isInitialized = true; |
| 64 | + |
| 65 | + _tlsConfigurationLoader = new TlsConfigurationLoader(hostEnvironment, serverLogger, httpsLogger); |
| 66 | + _populateMultiplexedTransportFeatures = PopulateMultiplexedTransportFeaturesWorker; |
| 67 | + _useHttpsWithDefaults = UseHttpsWithDefaultsWorker; |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc/> |
| 71 | + public void ApplyHttpsConfiguration( |
| 72 | + HttpsConnectionAdapterOptions httpsOptions, |
| 73 | + EndpointConfig endpoint, |
| 74 | + KestrelServerOptions serverOptions, |
| 75 | + CertificateConfig? defaultCertificateConfig, |
| 76 | + ConfigurationReader configurationReader) |
| 77 | + { |
| 78 | + EnsureInitialized(); |
| 79 | + _tlsConfigurationLoader!.ApplyHttpsConfiguration(httpsOptions, endpoint, serverOptions, defaultCertificateConfig, configurationReader); |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + public ListenOptions UseHttpsWithSni(ListenOptions listenOptions, HttpsConnectionAdapterOptions httpsOptions, EndpointConfig endpoint) |
| 84 | + { |
| 85 | + EnsureInitialized(); |
| 86 | + return _tlsConfigurationLoader!.UseHttpsWithSni(listenOptions, httpsOptions, endpoint); |
| 87 | + } |
| 88 | + |
| 89 | + /// <inheritdoc/> |
| 90 | + public CertificateAndConfig? LoadDefaultCertificate(ConfigurationReader configurationReader) |
| 91 | + { |
| 92 | + EnsureInitialized(); |
| 93 | + return _tlsConfigurationLoader!.LoadDefaultCertificate(configurationReader); |
| 94 | + } |
| 95 | + |
| 96 | + /// <inheritdoc/> |
| 97 | + public void PopulateMultiplexedTransportFeatures(FeatureCollection features, ListenOptions listenOptions) |
| 98 | + { |
| 99 | + EnsureInitialized(); |
| 100 | + _populateMultiplexedTransportFeatures!.Invoke(features, listenOptions); |
| 101 | + } |
| 102 | + |
| 103 | + /// <inheritdoc/> |
| 104 | + public ListenOptions UseHttpsWithDefaults(ListenOptions listenOptions) |
| 105 | + { |
| 106 | + EnsureInitialized(); |
| 107 | + return _useHttpsWithDefaults!.Invoke(listenOptions); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// If this instance has not been initialized, initialize it if possible and throw otherwise. |
| 112 | + /// </summary> |
| 113 | + /// <exception cref="InvalidOperationException">If initialization is not possible.</exception> |
| 114 | + private void EnsureInitialized() |
| 115 | + { |
| 116 | + if (!_isInitialized) |
| 117 | + { |
| 118 | + if (_initializer is not null) |
| 119 | + { |
| 120 | + _initializer.Initialize(this); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + throw new InvalidOperationException(CoreStrings.NeedHttpsConfiguration); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// The initialized implementation of <see cref="PopulateMultiplexedTransportFeatures"/>. |
| 131 | + /// </summary> |
| 132 | + internal static void PopulateMultiplexedTransportFeaturesWorker(FeatureCollection features, ListenOptions listenOptions) |
| 133 | + { |
| 134 | + // HttpsOptions or HttpsCallbackOptions should always be set in production, but it's not set for InMemory tests. |
| 135 | + // The QUIC transport will check if TlsConnectionCallbackOptions is missing. |
| 136 | + if (listenOptions.HttpsOptions != null) |
| 137 | + { |
| 138 | + var sslServerAuthenticationOptions = HttpsConnectionMiddleware.CreateHttp3Options(listenOptions.HttpsOptions); |
| 139 | + features.Set(new TlsConnectionCallbackOptions |
| 140 | + { |
| 141 | + ApplicationProtocols = sslServerAuthenticationOptions.ApplicationProtocols ?? new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 }, |
| 142 | + OnConnection = (context, cancellationToken) => ValueTask.FromResult(sslServerAuthenticationOptions), |
| 143 | + OnConnectionState = null, |
| 144 | + }); |
| 145 | + } |
| 146 | + else if (listenOptions.HttpsCallbackOptions != null) |
| 147 | + { |
| 148 | + features.Set(new TlsConnectionCallbackOptions |
| 149 | + { |
| 150 | + ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 }, |
| 151 | + OnConnection = (context, cancellationToken) => |
| 152 | + { |
| 153 | + return listenOptions.HttpsCallbackOptions.OnConnection(new TlsHandshakeCallbackContext |
| 154 | + { |
| 155 | + ClientHelloInfo = context.ClientHelloInfo, |
| 156 | + CancellationToken = cancellationToken, |
| 157 | + State = context.State, |
| 158 | + Connection = new ConnectionContextAdapter(context.Connection), |
| 159 | + }); |
| 160 | + }, |
| 161 | + OnConnectionState = listenOptions.HttpsCallbackOptions.OnConnectionState, |
| 162 | + }); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// The initialized implementation of <see cref="UseHttpsWithDefaults"/>. |
| 168 | + /// </summary> |
| 169 | + internal static ListenOptions UseHttpsWithDefaultsWorker(ListenOptions listenOptions) |
| 170 | + { |
| 171 | + return listenOptions.UseHttps(); |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// TlsHandshakeCallbackContext.Connection is ConnectionContext but QUIC connection only implements BaseConnectionContext. |
| 176 | + /// </summary> |
| 177 | + private sealed class ConnectionContextAdapter : ConnectionContext |
| 178 | + { |
| 179 | + private readonly BaseConnectionContext _inner; |
| 180 | + |
| 181 | + public ConnectionContextAdapter(BaseConnectionContext inner) => _inner = inner; |
| 182 | + |
| 183 | + public override IDuplexPipe Transport |
| 184 | + { |
| 185 | + get => throw new NotSupportedException("Not supported by HTTP/3 connections."); |
| 186 | + set => throw new NotSupportedException("Not supported by HTTP/3 connections."); |
| 187 | + } |
| 188 | + public override string ConnectionId |
| 189 | + { |
| 190 | + get => _inner.ConnectionId; |
| 191 | + set => _inner.ConnectionId = value; |
| 192 | + } |
| 193 | + public override IFeatureCollection Features => _inner.Features; |
| 194 | + public override IDictionary<object, object?> Items |
| 195 | + { |
| 196 | + get => _inner.Items; |
| 197 | + set => _inner.Items = value; |
| 198 | + } |
| 199 | + public override EndPoint? LocalEndPoint |
| 200 | + { |
| 201 | + get => _inner.LocalEndPoint; |
| 202 | + set => _inner.LocalEndPoint = value; |
| 203 | + } |
| 204 | + public override EndPoint? RemoteEndPoint |
| 205 | + { |
| 206 | + get => _inner.RemoteEndPoint; |
| 207 | + set => _inner.RemoteEndPoint = value; |
| 208 | + } |
| 209 | + public override CancellationToken ConnectionClosed |
| 210 | + { |
| 211 | + get => _inner.ConnectionClosed; |
| 212 | + set => _inner.ConnectionClosed = value; |
| 213 | + } |
| 214 | + public override ValueTask DisposeAsync() => _inner.DisposeAsync(); |
| 215 | + } |
| 216 | + |
| 217 | + /// <summary> |
| 218 | + /// Register an instance of this type to initialize registered instances of <see cref="HttpsConfigurationService"/>. |
| 219 | + /// </summary> |
| 220 | + internal interface IInitializer |
| 221 | + { |
| 222 | + /// <summary> |
| 223 | + /// Invokes <see cref="IHttpsConfigurationService.Initialize"/>, passing appropriate arguments. |
| 224 | + /// </summary> |
| 225 | + void Initialize(IHttpsConfigurationService httpsConfigurationService); |
| 226 | + } |
| 227 | + |
| 228 | + /// <inheritdoc/> |
| 229 | + internal sealed class Initializer : IInitializer |
| 230 | + { |
| 231 | + private readonly IHostEnvironment _hostEnvironment; |
| 232 | + private readonly ILogger<KestrelServer> _serverLogger; |
| 233 | + private readonly ILogger<HttpsConnectionMiddleware> _httpsLogger; |
| 234 | + |
| 235 | + public Initializer( |
| 236 | + IHostEnvironment hostEnvironment, |
| 237 | + ILogger<KestrelServer> serverLogger, |
| 238 | + ILogger<HttpsConnectionMiddleware> httpsLogger) |
| 239 | + { |
| 240 | + _hostEnvironment = hostEnvironment; |
| 241 | + _serverLogger = serverLogger; |
| 242 | + _httpsLogger = httpsLogger; |
| 243 | + } |
| 244 | + |
| 245 | + /// <inheritdoc/> |
| 246 | + public void Initialize(IHttpsConfigurationService httpsConfigurationService) |
| 247 | + { |
| 248 | + httpsConfigurationService.Initialize(_hostEnvironment, _serverLogger, _httpsLogger); |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
0 commit comments