Skip to content

Enable SocketsHttpHandler.ConnectCallback to negotiate TLS #42455

Closed

Description

@brporter

AB#1219925

Description

When using the new ConnectCallback feature, returning an SslStream results in errors related to TLS negotiation. HttpConnectionPool expects to be in charge of negotiating SSL over the returned stream, but design eliminates lots of possible transport-level opportunities for people who want or need to control or manage transport processing.

Here is an example implementation of ConnectCallback that will fail as a result of this issue.

            var retVal = new HttpMessageInvoker(
                new SocketsHttpHandler()
                {
                    ConnectCallback = async (ctx, cancellationToken) =>
                    {
                        var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                        await socket.ConnectAsync(ctx.DnsEndPoint);

                        var networkStream = new NetworkStream(socket);
                        var sslStream = new SslStream(networkStream);

                        await sslStream.AuthenticateAsClientAsync(ctx.RequestMessage.RequestUri.Host);

                        return sslStream;
                    }
                }
            );

Ideally, HttpConnectionPool would manage negotiating TLS for me if I returned a Stream implementation, but if I returned an SslStream, it ought to defer negotiation to me.

Configuration

Latest bits from release/5.0-rc2, not arch or platform specific.

Regression?

Not a regression.

Other information

Being able to return an SslStream (or other stream types) is important because it enables the ConnectCallback to provide a facility for transport-level optimizations like stream multiplexing (imagine a stream implementation that's built atop a series of TCP sockets for compatibility reasons, but actually parallelizes writes over multiple underlying TCP connections), TLS pre-negotiation (TLS negotiation for small payloads is expensive, perhaps the implementor of the ConnectCallback would like to maintain a pool of pre-negotiated TLS sessions for specific destinations) and telemetry (streams returned by ConnectCallback can't effectively have their contents intercepted by other code running in the stack).

This seems like something as simple as a type check on the returned stream would be sufficient:

if (!(is SslStream)) {
    /// negotiate TLS in the usual, existing way
}

I'm happy to propose a PR with a fix for this issue along with corresponding tests.

Activity

  1. added
    untriaged
    on Sep 18, 2020
  2. changed the title [-]Unable to return SslStream from SocketsHttpHandler.ConnectCallback[/-] [+]Enable SocketsHttpHandler.ConnectCallback to negotiate TLS[/+] on Sep 18, 2020
  3. added
    enhancement
    and removed
    untriaged
    on Sep 18, 2020
  4. added this to the Future milestone on Sep 18, 2020
  5. brporter commented on Sep 18, 2020

    @brporter
    Author

    I've proposed a PR with a fix, along with a test. (#42473)

  6. geoffkizer commented on Sep 18, 2020

    @geoffkizer
    Contributor

    ConnectCallback isn't intended to support this. It just lets you control how the connection is established. SocketsHttpHandler still owns the TLS negotiation.

    I do think it's potentially interesting to allow you to control TLS negotiation as well, but I think we'd expose this as a separate hook.

    Note that you can return an SslStream from ConnectCallback; you just can't replace the TLS negotiation logic. So you could specify an HTTP url and then actually establish a TLS connection under the covers if you want to.

  7. scalablecory commented on Sep 18, 2020

    @scalablecory
    Contributor

    Initial prototyping for System.Net.Connections exposed a HTTPS middleware concept that allowed exactly this, but we weren't able to find a good real-world need for it so we dropped it.

  8. mjsabby commented on Sep 18, 2020

    @mjsabby
    Contributor

    @geoffkizer this is our use-case as well, so I'm missing what is not possible.

  9. brporter commented on Sep 18, 2020

    @brporter
    Author

    @geoffkizer understanding that the intent of ConnectCallback was just for socket creation, the basic interface lends itself to all sorts of potential optimizations. Given that the existing implementation of ConnectCallback expects you to return a stream and not a socket, enabling a caller to perform their own TLS negotiation is natural. I think it's actually more surprising to users that I can't return any old stream, and that the stream I provide has to be in a certain state.

    This fixes that by removing any preconditions on the returned stream (for TLS anyway, although I don't see others in the code).

  10. 19 remaining items

  11. added
    Cost:M
    on Jan 4, 2021
  12. removed
    Bottom Up Work
    Cost:M
    Priority:2
    User Story
    on Jan 12, 2021
  13. modified the milestones: 6.0.0, Future on May 4, 2021
  14. ghost added
    in-pr
    on Jan 18, 2022
  15. ghost removed
    in-pr
    on Feb 22, 2022
  16. wfurt commented on Feb 24, 2022

    @wfurt
    Member

    There still may be time in 7.0 for some improvements mentioned in #63851. It would be great if interested folks can get daily build (or new preview) and provide feedback.

  17. ghost locked as resolved and limited conversation to collaborators on Mar 26, 2022
  18. modified the milestones: Future, 7.0.0 on Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.Httpenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions