runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Establishing TLS secure channel fails when NewSessionTicket message received

Open aarondcoleman opened this issue 11 years ago • 50 comments

Overview HttpWebRequest throws error a connection error when a TLS key on the server is changed. Specifically this is the implementation done by CloudFlare. Every hour, on the hour xx:00 a web service fronted by CloudFlare will throw the following exception on the first execution, but all subsequent ones succeed:

System.Net Error: 0 : [7328] Exception in HttpWebRequest#58000300::EndGetResponse - The request was aborted: Could not create SSL/TLS secure channel.

Sample Reproducible example here, but it needs to be running right before the hour (xx:59:59) through the following hour (xx:00:00) in order to observe the exception. We are using Fitbit.com as the example as this is fronted by the CloudFlaire service:

https://github.com/aarondcoleman/FitbitProxyTest

Other Links

  • More info on how CloudFlare works: https://blog.cloudflare.com/tls-session-resumption-full-speed-and-secure/
  • Forum thread on this issue: https://social.msdn.microsoft.com/Forums/vstudio/en-US/eb999cd4-b43b-4779-b01b-4a04017714e3/httpwebrequest-ssl-caching-bug-cloudflare-the-request-was-aborted-could-not-create-ssltls?forum=netfxbcl
  • System.Net.Trace showing the occurence at the changeover of hour: https://github.com/aarondcoleman/FitbitProxyTest/blob/master/System.Net.Trace.Log

This is of high importance as our integration with the Fitbit API is affected. Every first call of the hour to the API fails per server node of our infrastructure.

aarondcoleman avatar Oct 15 '15 20:10 aarondcoleman

Verified on Windows 8.1 with all patches using the example scenario provided.

This looks like it may be a bug in the underlying SChannel InitializeSecurityContext function. The server returns a NewSessionTicket message (0x04) during the establishment of the new HTTPS connection (which was part of an existing HTTPS session with a cached SessionTicket). See https://github.com/aarondcoleman/FitbitProxyTest/blob/master/System.Net.Trace.Log#L6683

This NewSessionTicket message arriving at this time appears to cause SChannel to return IllegalMessage and fail the connection. .NET sees this result and performs a RST on the underlying TCP/IP connection.

newsessionticket

image

It's remotely possible that there's a bug in what CloudFlare is sending (e.g. in the ChangeCipherSpec message) but I think it's far more likely that there's a problem in the handling of the NewSessionTicket message at this point in the handshake. This message is relatively uncommon and .NET is historically known to be frail when it comes to unexpected alerts and messages during handshaking.

A Microsoft Message Analyzer Network capture of the failure can be found here: https://onedrive.live.com/redir?resid=D2FAD6A7909B0897!37926&authkey=!ANf4JrBtKlSP8EI&ithint=file%2cmatp

ericlaw1979 avatar Oct 15 '15 21:10 ericlaw1979

cc: @davidsh, @cipop, @SidharthNabar, @bartonjs

stephentoub avatar Oct 15 '15 21:10 stephentoub

Is this repro on CoreFx code or .NET Framework (desktop)?

davidsh avatar Oct 15 '15 21:10 davidsh

I ask this question because the log entry cited above looks like something from a System.Net trace using .NET Framework (Desktop).

System.Net Error: 0 : [7328] Exception in HttpWebRequest#58000300::EndGetResponse - The request was aborted: Could not create SSL/TLS secure channel.

davidsh avatar Oct 15 '15 21:10 davidsh

The log is from the .NET Framework on Windows. Is SslStream done differently on CoreFx?

ericlaw1979 avatar Oct 15 '15 21:10 ericlaw1979

The trace was done on my local dev, but we run into this problem in production (Azure Websites) ASP.NET as well.

aarondcoleman avatar Oct 15 '15 21:10 aarondcoleman

In terms of the HTTP stack, the implementation in CoreFx of the System.Net.Http and System.Net.HttpWebRequest API are TOTALLY DIFFERENT. On CoreFx, the Http APIs on Windows are built directly on top of WinHTTP. It does not use "SslStream" nor .NET managed Sockets.

The SslStream class in CoreFx, though, is built on managed .NET Sockets and would be essentially the same architecture using SCHANNEL api's.

But your repro here regarding use of Http APIs will probably NOT repro in CoreFx.

davidsh avatar Oct 15 '15 21:10 davidsh

Fair enough. My interest, of course, is for Fiddler, which is based on the SslStream class. And if the root cause of this is a problem in SChannel (as I suspect it may be) it would also repro in anything based on it (like WinHTTP/WinINET).

ericlaw1979 avatar Oct 15 '15 21:10 ericlaw1979

@davidsh are you suggesting we file this elsewhere, or change the label?

aarondcoleman avatar Oct 15 '15 21:10 aarondcoleman

Paying attention at CloudFlare here. We'd love to see this addressed. Workarounds we've contemplated would reduce TLS performance notably.

pencoyd avatar Oct 15 '15 22:10 pencoyd

@aarondcoleman, @ericlaw1979 It's not clear: can you repro this on CoreFX with either SslStream or the HTTP stack?

For Desktop, please report using https://connect.microsoft.com/.

CIPop avatar Oct 15 '15 22:10 CIPop

Yes - it would be very helpful if you can file this as a Connect issue since this repo does not cover components of the Windows OS such as Schannel, WinInet/WinHTTP, etc. Thanks!

SidharthNabar avatar Oct 15 '15 22:10 SidharthNabar

@aarondcoleman @ericlaw1979 It would be useful to also collect the following Schannel traces for debugging purposes (note that the resulting log may contain private information):

     logman start schannel_trace -p "{37D2C3CD-C5D4-4587-8531-4696C44244C8}" 0x4000ffff -o schannel.etl -ets -ln schannel
     Run the repro.
     logman stop schannel_trace -ets

CIPop avatar Oct 15 '15 22:10 CIPop

@CIPop so, Using System.Net.HttpClient on a sample ASP.NET website? I can certainly repro there, that's the main use case where we ran in to this.

@SidharthNabar as in, here? https://connect.microsoft.com/VisualStudio

aarondcoleman avatar Oct 15 '15 22:10 aarondcoleman

so, Using System.Net.HttpClient on a sample ASP.NET website? I can certainly repro there, that's the main use case where we ran in to this.

That should work.

CIPop avatar Oct 15 '15 22:10 CIPop

@aarondcoleman - Yes, that's the right Connect path.

SidharthNabar avatar Oct 15 '15 22:10 SidharthNabar

@CIPop and what kind of tracing / logging do you need?

aarondcoleman avatar Oct 15 '15 22:10 aarondcoleman

and what kind of tracing / logging do you need?

You'll need to follow the instructions in my previous message regarding Schannel tracing:

@aarondcoleman @ericlaw1979 It would be useful to also collect the following Schannel traces for debugging purposes (note that the resulting log may contain private information):

     logman start schannel_trace -p "{37D2C3CD-C5D4-4587-8531-4696C44244C8}" 0x4000ffff -o schannel.etl -ets -ln schannel
     Run the repro.
     logman stop schannel_trace -ets

CIPop avatar Oct 15 '15 22:10 CIPop

@CIPop: Your "-ets" argument was corrupted by Word or something else that converts hypens to en-dashes.

After running the command in question, what file should be attached to the bug report filed (somewhere?) on CONNECT-- schannel.etl?

ericlaw1979 avatar Oct 15 '15 23:10 ericlaw1979

I've started a CONNECT bug issue as well here: https://connect.microsoft.com/VisualStudio/feedback/details/1906395

I'm waiting for the hour changeover to capture the error.

aarondcoleman avatar Oct 15 '15 23:10 aarondcoleman