Skip to content

Add TlsConnectionOptions to connection abstractions #42831

Closed
@JamesNK

Description

@JamesNK

Background and Motivation

Kestrel needs to pass TLS configuration to the QUIC transport. Neither project has a dependency on the other.

Communication is done through values added to IFeatureCollection. Rather than add a loose collection of types and delegates to the feature collection, I'd rather have a strongly typed options type that has the necessary configuration.

Also, part of TLS configuration is a callback that's called for each connection to resolve SslServerAuthenticationOptions. If the callback takes a context object, then new values can be added in the future.

Proposed API

Note: Microsoft.AspNetCore.Connections.Abstractions targets .NET Framework and .NET Standard. These types would only be present in .NET 7 it's the only target that supports SslServerAuthenticationOptions

namespace Microsoft.AspNetCore.Connections;

/// <summary>
/// Options used to configure a per connection callback for TLS configuration.
/// </summary>
public class TlsConnectionCallbackOptions
{
    /// <summary>
    /// The callback to invoke per connection. This property is required.
    /// </summary>
    public Func<TlsConnectionCallbackContext, ValueTask<SslServerAuthenticationOptions>> OnConnection { get; set; } = default!;

    /// <summary>
    /// Optional application state to flow to the <see cref="OnConnection"/> callback.
    /// </summary>
    public object? OnConnectionState { get; set; }

    /// <summary>
    /// Gets or sets a list of ALPN protocols.
    /// </summary>
    public List<SslApplicationProtocol> ApplicationProtocols { get; set; } = default!;
}

/// <summary>
/// Per connection state used to determine the TLS options.
/// </summary>
public class TlsConnectionCallbackContext
{
    /// <summary>
    /// Information from the Client Hello message.
    /// </summary>
    public SslClientHelloInfo ClientHelloInfo { get; set; }

    /// <summary>
    /// The information that was passed when registering the callback.
    /// </summary>
    public object? State { get; set; }

    /// <summary>
    /// The token to monitor for cancellation requests.
    /// </summary>
    public CancellationToken CancellationToken { get; set; }

    /// <summary>
    /// Information about an individual connection.
    /// </summary>
    public ConnectionContext Connection { get; set; } = default!;
}

Usage Examples

var features = new FeaturesCollection();

features.Set(new TlsConnectionCallbackOptions
{
    ApplicationProtocols = new List<SslApplicationProtocol> { SslApplicationProtocol.Http3 },
    OnConnection = context =>
    {
        return listenOptions.HttpsCallbackOptions.OnConnection(new TlsHandshakeCallbackContext
        {
            ClientHelloInfo = context.ClientHelloInfo,
            CancellationToken = context.CancellationToken,
            State = context.State,
            Connection = context.Connection,
        });
    },
    OnConnectionState = listenOptions.HttpsCallbackOptions.OnConnectionState,
});

Alternative Designs

These types are very similar to what is in Kestrel:

Risks

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions