-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
do not call RemoteCertificateValidationCallback on the same certificate again #42836
Conversation
Tagging subscribers to this area: @dotnet/ncl |
This is specific to HttpClientHandler (you're not hitting this when using SocketsHttpHandler directly, right?). The callback signature from HttpClientHandler expects the HttpRequestMessage to be passed in, so we need to capture the instance into the closure used for the underlying RemoteCertificateValidationCallback so that it can be in turn passed to the handler's ServerCertificateCustomValidationCallback. But as a result of capturing it in that way, the HttpRequestMessage would end up being referenced for the lifetime of the callback, thus keeping it alive and from being garbage collected. The fix for this issue (#36979) was to null out the field after the callback was invoked (#37021), with the assumption that it wouldn't be invoked again. If it is invoked again, then it triggers the assert put in place to validate it wasn't being invoked again. So, we have a choice to make, either:
None of these options seem great, but (2) is probably the best. @geoffkizer? (@jeffhandley, FYI on nullable annotations) |
src/libraries/System.Net.Security/src/System/Net/Security/SecureChannel.cs
Outdated
Show resolved
Hide resolved
yes, this is good description. I was thinking about it more as I was reading your writeup @stephentoub and all the cases when renegotiation happens in practice are when server asks for client certificate later in the session. Aside from the TLS13 I'm trying to fix, I'm not sure there is practical case when server's certificate would change. Option 2 seems best to me. Keeping Debug.Assert is perhaps good choice as it would not be in production code and we would effectively pass in NULL - something applications can deal with. Cutting memory use is more important IMHO. |
Yeah, this is my understanding as well. Can the server actually send a new certificate during renegotiation? Even if it can, this doesn't seem like it's common at all in practice. In other words, I'm not sure there's a real issue here. That said, if there are cases where this can happen, I agree that option 2 is best (i.e. pass null).
In the TLS13 case, the cert isn't actually changing though, right? It's just that it's not available until slightly later than usual. So it should still get the HttpRequestMessage, right? |
no, the certificate is not changing @geoffkizer . But the renegotiation path assumed it could and always called the validation callback again. |
src/libraries/System.Net.Security/src/System/Net/Security/SecureChannel.cs
Outdated
Show resolved
Hide resolved
_remoteCertificate = CertificateValidationPal.GetRemoteCertificate(_securityContext, out remoteCertificateStore); | ||
X509Certificate2? remoteCertificate = CertificateValidationPal.GetRemoteCertificate(_securityContext, out remoteCertificateStore); | ||
|
||
if (remoteCertificate != null && _remoteCertificate != null && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if both are null? Is this possible? Should we call the callback again in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we will fall-through and hit the "normal" case. SslPolicyErrors.RemoteCertificateNotAvailable will be set.
If peer certificate was required (and that is true on client side) that would lead to negotiation failure unless that was override by the callback for some reason. To prevent this form happening twice, we would probably need to add another property to track if this was already called. I simply use presence of certificate to detect that now but it can still probably happen on server side. It feels like unlikely corner case and it should not impact the SocketHttpClient nor be TLS 13 specific. The callback is only one way for app to know "something" e.g. renegotiation happened. So I think it is ok to be called multiple times in general case.
The interesting scenario IMHO would be TLS resume where session can be established without full exchange. I don't know if Schannel remembers the certificates from the previous session and if that generally works same way with OpenSSL. (resume currently not working on Linux but I'm hoping to fix that)
This is fix fix for #42823 to make the verification more consistent. On Windows, the callback can be triggered by sending data as the handshake is not quite finished when AuthenticateAs*() returns.
I'm not sure if the X509Certificate.Equals is sufficient but for now I assume so since the peer is already verified and trusted.
While testing this I bump to
It seems like the HTTP callback is not ready to called multiple times. In this case it is lame, but if renegotiation is enabled this may happen. Since there is no field issue, I left it as it was.