Skip to content

API Proposal: SocketsHttpHandler better certificate APIs #41191

Description

@davidfowl

Background and Motivation

Today SocketsHttpHandler exposes certificate settings via SslOptions (which is of type SslClientAuthenticationOptions). We can use these APIs to handle server certificate validation and client certificate selection. The only issue is that these callbacks don't provide the context of the HttpRequestMessage which can make it hard to determine what logic should be run (especially in multitenant scenarios). We should add 2 callbacks to the SocketsHttpHandler for these scenarios, one to handler client cert selection and the other to handle server certificate validation. HttpClientHandler has a server certificate selection callback for this but nothing for client cert selection.

Proposed API

namespace System.Net.Http
{
    public class SocketsHttpHandler
    {
+      public Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> ServerCertificateValidationCallback { get; set; }
+      public Func<HttpRequestMessage, X509CertificateCollection, X509Certificate, string[], X509Certificate2> ClientCertificateSelectionCallback { get; set; }
    }
}

Usage Examples

var handler = new SocketsHttpHandler();
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
    var tenant = FindTenant(message);
    return tenant.CheckServerCert(cert, chain, errors);
};

handler.ClientCertificateSelectionCallback = (message, localCertificates, remoteCertificate, acceptableIssuers) =>
{
    var tenant = FindTenant(message);
    return tenant.ClientCertificate;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions