You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add SslStream.ServerName and other properties from SslClientAuthenticationOptions #27619
I would like to use SocketsHttpHandler instead of HttpClientHandler, because of the additional timeout options. In my current implementation I use HttpClientHandler.ServerCertificateCustomValidationCallback to implement custom SSL certificate validation. Inside the callback I use HttpRequestMessage.RequestUri.Host to see if the user has configured a trusted certificate for the specified host. If the certificate is not trusted, then I add the certificate to HttpRequestMessage.Properties, so that the requester/caller can show the certificate to the user. When I try to re-implement this using SocketsHttpHandler.SslOptions.RemoteCertificateValidationCallback, then I get an SslStream instead of a HttpResponseMessage. As a result I do not know for which host I am verifying the certificate and I cannot add the certificate to the request properties.
As a workaround I use reflection to get HttpClientHandler._socketsHttpHandler, so that I can configure the timeout options of SocketsHttpHandler, while still using HttpClientHandler. Of course I would prefer not having to resort to using reflection and exploiting implementation details.
Another way to allow this is to expose System.Net.Http.ConnectHelper.CertificateCallbackMapper, which is what HttpClientHandler.ServerCertificateCustomValidationCallback uses internally.
For (2), this would only impact the HttpRequestMessage that actually caused the connection to be established; other requests that reused the same connection would not cause the callback to be invoked and thus would not have their Properties configured. How does that fit into your scenario?
For (1), I think a cleaner approach would be to ensure that SslStream exposes the information needed for such situations. @davidsh, is there any way to get that info from the SslStream instance? If not, have we considered adding it?
I'd like us to try hard to avoid adding a duplicated callback as you suggest. I understand why you want it, and it is a bit unfortunate that the request message isn't made available in the callback. It was done this way so that the same SslOptions bag could be used that's generally used with SslStream, so that all of those options can be used to configure it, now and in the future with any other properties/configuration added there. We could of course add the other variety directly to SocketsHttpHandler rather than having you set the callback on SocketsHttpHandler.SslOptions, but then that ends up being a bit confusing, that some SSL configuration is in the SslOptions and some not, and that there are two different variants of the same callback that do almost exactly the same thing.
For (1) I agree that having the host name available on the SslStream would be nicer. Having duplicate callbacks is indeed confusing. I also understand that the API was made to be more in line with SSL connections in general for code re-use. I was focusing a bit too much on the way it used to work with HttpClientHandler.
For (2) the idea was that the callback was called again for a second request, because the connection from the first request is not maintained, because of the certificate validation failure. Although, now I think more about it, that probably would not have worked if two requests would have been made at roughly the same time, and both would immediately try to share a single connection. As such it is probably a good idea for me to leave HttpRequestMessage alone. I could just as well store the last certificate that failed validation for each host elsewhere, and lookup the invalid certificate when I get the exception. Thanks for noticing a concurrency issue in my code, which I didn't even realize existed.
For (1), I think a cleaner approach would be to ensure that SslStream exposes the information needed for such situations. @davidsh, is there any way to get that info from the SslStream instance? If not, have we considered adding it?
What additional information are you asking to be "exposed"? Is it the "host name"? If so, wouldn't that already be exposed in the SNI information of SslStream.
I would like to use SocketsHttpHandler instead of HttpClientHandler, because of the additional timeout options.
HttpClientHandler is really the class that we encourage all developers to use. It is the only class in .NET Standard. Perhaps the additional properties such as "timeouts" should be added to that. Then you wouldn't have to directly create/use SocketsHttpHandler.
Where? I must be missing it. Given an SslStream, from where do I get the host name?
I might have been thinking of the server-side SslStream API where we have a callback for received SNI (hostname) information. So, yes, it's possible we don't have a way to determine what host (SNI) was sent during TLS protocol exchange.
I see can the value of having the host (SNI) name being sent by client-side APIs as useful information. I just don't see exactly where we would provide that without creating another callback.
I just don't see exactly where we would provide that without creating another callback.
Could it / should it not just be stored onto the SslStream instance as field, exposed via a property, that's set and available for read if/once the information is available? Similar to what we do with information like what hash algorithm, cipher algorithm, key exchange algorithm, etc. was established?
Could it / should it not just be stored onto the SslStream instance as field, exposed via a property, that's set and available for read if/once the information is available? Similar to what we do with information like what hash algorithm, cipher algorithm, key exchange algorithm, etc. was established?
Yes, that is a possibility.
I look forward to seeing an API proposal for this.
Triage: We agree to expose RequestUri.Host (SNI) on SslStream (assuming it is not there).
We need API proposal. The name should be likely something like ServerName.
This is similar / overlapping with dotnet/corefx#41622 - we should keep that in mind (it asks to allow changes to SslStream options in the callback).
changed the title [-]Add a ServerCertificateCustomValidationCallback option to SocketsHttpHandler that provides the HttpRequestMessage[/-][+]Add SslStream.ServerName and other properties from SslClientAuthenticationOptions[/+]on Oct 9, 2019
We store SslClientAuthenticationOptions.TargetHost internally but it is not exposed publicly.
(all the other overloaded are converted to SslClientAuthenticationOptions under the cover)
On server, that property is used to hold name requested by client (if any)
That may possibly be used past the callback for diagnostic or in cases when certificate is specified without delegate (like wildcard cert matching subdomains and all sites).
Activity
stephentoub commentedon Oct 14, 2018
Thanks, @ChristofferGersen.
There are two pieces here:
For (2), this would only impact the HttpRequestMessage that actually caused the connection to be established; other requests that reused the same connection would not cause the callback to be invoked and thus would not have their Properties configured. How does that fit into your scenario?
For (1), I think a cleaner approach would be to ensure that SslStream exposes the information needed for such situations. @davidsh, is there any way to get that info from the SslStream instance? If not, have we considered adding it?
I'd like us to try hard to avoid adding a duplicated callback as you suggest. I understand why you want it, and it is a bit unfortunate that the request message isn't made available in the callback. It was done this way so that the same SslOptions bag could be used that's generally used with SslStream, so that all of those options can be used to configure it, now and in the future with any other properties/configuration added there. We could of course add the other variety directly to SocketsHttpHandler rather than having you set the callback on SocketsHttpHandler.SslOptions, but then that ends up being a bit confusing, that some SSL configuration is in the SslOptions and some not, and that there are two different variants of the same callback that do almost exactly the same thing.
ChristofferGersen commentedon Oct 15, 2018
For (1) I agree that having the host name available on the SslStream would be nicer. Having duplicate callbacks is indeed confusing. I also understand that the API was made to be more in line with SSL connections in general for code re-use. I was focusing a bit too much on the way it used to work with HttpClientHandler.
For (2) the idea was that the callback was called again for a second request, because the connection from the first request is not maintained, because of the certificate validation failure. Although, now I think more about it, that probably would not have worked if two requests would have been made at roughly the same time, and both would immediately try to share a single connection. As such it is probably a good idea for me to leave HttpRequestMessage alone. I could just as well store the last certificate that failed validation for each host elsewhere, and lookup the invalid certificate when I get the exception. Thanks for noticing a concurrency issue in my code, which I didn't even realize existed.
davidsh commentedon Oct 15, 2018
What additional information are you asking to be "exposed"? Is it the "host name"? If so, wouldn't that already be exposed in the SNI information of SslStream.
HttpClientHandler is really the class that we encourage all developers to use. It is the only class in .NET Standard. Perhaps the additional properties such as "timeouts" should be added to that. Then you wouldn't have to directly create/use SocketsHttpHandler.
stephentoub commentedon Oct 15, 2018
Yes
Where? I must be missing it. Given an SslStream, from where do I get the host name?
davidsh commentedon Oct 16, 2018
I might have been thinking of the server-side
SslStreamAPI where we have a callback for received SNI (hostname) information. So, yes, it's possible we don't have a way to determine what host (SNI) was sent during TLS protocol exchange.stephentoub commentedon Oct 16, 2018
Thanks. Any reason not to consider adding that?
davidsh commentedon Oct 16, 2018
I see can the value of having the host (SNI) name being sent by client-side APIs as useful information. I just don't see exactly where we would provide that without creating another callback.
stephentoub commentedon Oct 16, 2018
Could it / should it not just be stored onto the SslStream instance as field, exposed via a property, that's set and available for read if/once the information is available? Similar to what we do with information like what hash algorithm, cipher algorithm, key exchange algorithm, etc. was established?
davidsh commentedon Oct 16, 2018
Yes, that is a possibility.
I look forward to seeing an API proposal for this.
karelz commentedon Oct 9, 2019
Triage: We agree to expose
RequestUri.Host(SNI) onSslStream(assuming it is not there).We need API proposal. The name should be likely something like
ServerName.This is similar / overlapping with dotnet/corefx#41622 - we should keep that in mind (it asks to allow changes to
SslStreamoptions in the callback).[-]Add a ServerCertificateCustomValidationCallback option to SocketsHttpHandler that provides the HttpRequestMessage[/-][+]Add SslStream.ServerName and other properties from SslClientAuthenticationOptions[/+]wfurt commentedon Jun 10, 2020
We store SslClientAuthenticationOptions.TargetHost internally but it is not exposed publicly.
(all the other overloaded are converted to SslClientAuthenticationOptions under the cover)
On server, that property is used to hold name requested by client (if any)
That may possibly be used past the callback for diagnostic or in cases when certificate is specified without delegate (like wildcard cert matching subdomains and all sites).
How about adding this @stephentoub @davidsh
stephentoub commentedon Jun 10, 2020
That's effectively what I had in mind. Could it be
HostName?wfurt commentedon Jun 10, 2020
We could use 'HostName' if preferable. I proposed TargetName for consistency:
It can be also IP address in string form.
stephentoub commentedon Jun 10, 2020
I don't have a strong opinion; it just seems strange when the SslStream is on the server-side / representing the host.
wfurt commentedon Jun 10, 2020
maybe leave it to API review folks to discuss beauty, purity and clarity.
stephentoub commentedon Jun 10, 2020
Sure. We should just be clear on what the semantics are we're proposing for the property, whether it's used on client or the server side.