Because of compliance reasons we have to switch off the support of some ciphers and SSL2 on our webservers. This is not really a problem, but we would also like to inform them, after their successful login into the website, that we suggest switching on TLS 1.2 in their browser in case they are not already connecting to the server with TLS 1.2. So the question I have is:
How can I detect the protocol and cipher used in an https request to an ASP.net (MVC 4) application running in IIS?
I know that there are ways to log the SCHANNEL request to the event log and then read them out again, but this sounds very ugly to me.
And I have seen that the System.Net.Security.SslStream has the properties that I would need, e.g.: CipherAlgorithm, HashAlgorithm, KeyExchangeAlgorithm & SslProtocol, but I'm not sure where I can get these properties in my Controller Action in a mvc4 application.
Best Solution
The bad news, as determined by ILSpy, is that there is no way to get to a
System.Net.SslStream
instance from anywhere inside ASP.NET. That class is used for direct programming against the network, for example by the WCF framework. The best you can do from ASP.NET (whether using System.Web or OWIN on top of IIS or HttpListener) is to get a server variable (see list of IIS server variables) for whether the connection is secured by whatever secure transport was negotiated with the client.As far as deterministically reading data from the event log during a web request... that seems scary. But if you can make it work, please share the code. :)
Alternatively, you could try to implement your own Owin host (aka web server!) that uses
SslStream
underneath. Maybe. :P See this article for a thorough introduction toSslStream
programming.But since you're already able to turn off certain protocols on your server (as in this article, I assume)... You could set up your site on two different subdomains, e.g.
www.example.com
andsecure.example.com
, where the former is a vanilla web server and the latter is configured to only accept TLS 1.2 connections. Then you'd write some bootstrapping logic that gets served fromwww.example.com
and attempts to make an AJAX request tosecure.example.com/securityUpgradeCheck
(possibly with a nicely styled spinner animation and "Please wait, attempting to secure this connection" text to impress your users :)). If that request succeeds, the user can be redirected tosecure.example.com
(probably permanently, since that user agent is then known to support TLS 1.2 unless for some reason the user changes their browser settings).For added impact, order an EV SSL certificate for the secure domain so your users will notice the upgrade in security. :)
UPDATE: I did some more digging, on the theoretical basis of writing a custom (native) ISAPI filter (or extension) to get at this information via the SChannel API. At first I was hopeful because I discovered a function
HSE_REQ_GET_SSPI_INFO
that would return an SSPICtxtHandle
structure, and which you could call from a custom ISAPI extension via theEXTENSION_CONTROL_BLOCK
ServerSupportFunction
function. ThatCtxtHandle
structure, it turns out, represents an SChannel context and can get you a reference to aSECPKG_ATTR_CONNECTION_INFO
attribute with which you can retrieve SSL connection-level information (the same information that's surfaced in theSslStream
class in .NET, as far as I could tell). However, sadly, Microsoft anticipated that possibility and decided that this information is only available if you are using client certificates. The behavior is "by design."There was one (native) SSPI function,
QueryContextAttributes (Schannel)
, that I discovered during a long hunt through MSDN which may work. I haven't tried it, and it could simply fail for the same "by design" reason as the ISAPI API limitation linked to above. However, it may be worth a try. If you want to explore this route, here is an example of an ISAPI extension. Actually, with this approach you might be able to write an IIS module instead, using the newer IIS 7.0+ SDK.But, assuming you don't have the luxury of requiring client certificates and that long shot doesn't work, that absolutely leaves only two options.
By the way - is your load balancer configured to do any SSL interception? Because then this whole thing is moot anyways... Just a thought to consider.
UPDATE: Enabling Schannel logging netted this gem:
This can be read out directly from managed code. I think the UserID only corresponds to the IIS worker process SID, unfortunately, but assuming you can come up with some kind of correlation heuristic, you could set up a background thread to continually poll the event log and give you a list of recently established client handshakes (use a
ConcurrentDictionary
perhaps).There. That's it. No more curious investigating for me. I'm done. :P