Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SslStream RemoteCertificateValidationCallback has regressed to being unable to access protocol/cipher info #114351

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

Closed
dlstucki opened this issue Apr 7, 2025 · 2 comments · Fixed by #115186
Assignees
Milestone

Comments

@dlstucki
Copy link
Contributor

dlstucki commented Apr 7, 2025

Description

This scenario was enabled, including a unit test in .NET 5. It is broken again since .NET 7. I've observed this issue on .NET 8 Windows x64, but looking over the source it appears the issue is still present in higher versions.

The desired capability is being able to call Stream.SslProtocol/CipherAlogrithm from the SslStream's RemoteCertificateValidationCallback as described here: #919.

Original fix was #37580. It included a unit test named ClientAsyncAuthenticate_ConnectionInfoInCallback_DoesNotThrow.

Later #68678 changed the implementation of SslStream.ThrowIfExceptionalOrNotHandshake. This broke the property access scenario, but at some point the test method ClientAsyncAuthenticateTest.ClientAsyncSslHelper was refactored a bit and the ClientAsyncAuthenticate_ConnectionInfoInCallback_DoesNotThrow unit test was no longer using its special remote certificate validation callback to verify property access.

Reproduction Steps

Either of:

  1. This could easily be reproduced by fixing the ClientAsyncAuthenticate_ConnectionInfoInCallback_DoesNotThrow unit test and running that. That update would be to change ClientAsyncSslHelper(EncryptionPolicy, SslProtocols, SslProtocols, RemoteCertificateValidationCallback) to honor the RemoteCertificateValidationCallback parameter. AllowAnyServerCertificateAndVerifyConnectionInfo would then attempt to access the properties and the test would fail.

  2. The desired capability is being able to call Stream.SslProtocol/CipherAlogrithm from the SslStream's RemoteCertificateValidationCallback as described here: SslStream RemoteCertificateValidationCallback cannot access protocol/cipher info #919:

static bool ValidateRemoteCertificate(object Sender, X509Certificate Certificate, X509Chain Chain, SslPolicyErrors PolicyErrors)
{
    var stream = (SslStream)Sender;

    // The linked docs imply that these properties should be accessible at this point but
    // they throw InvalidOperationException when accessed
    return stream.CipherAlgorithm != CipherAlgorithmType.Rc4 &&
        stream.HashAlgorithm != HashAlgorithmType.Md5 &&
        stream.SslProtocol != SslProtocols.Tls13;
}

Expected behavior

SslStream.SslProtocol should be accessible from the RemoteCertificateValidationCallback

Actual behavior

System.InvalidOperationException: This operation is only allowed using a successfully authenticated context.
   at System.Net.Security.SslStream.ThrowNotAuthenticated()
   at System.Net.Security.SslStream.get_CipherAlgorithm()
   at Microsoft.Azure.SomeProcduct.RemoteServerValidationCallback(Object, X509Certificate, X509Chain, SslPolicyErrors)

Regression?

Crypto properties were accessible at RemoteCertificateValidationCallback time in .NET 5 and 6. Since .NET 7 that scenario has been broken.

Known Workarounds

I couldn't quickly come up with anything.

Configuration

.NET 7+ is broken. I tested with .NET 8, Windows 11, x64.

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 7, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@wfurt
Copy link
Member

wfurt commented Apr 7, 2025

we should fix this @rikm if possible. I'm wondering how that plays together with plan to do validation during handshake....
But at the point we should have ServerHello with selected cipher.

@wfurt wfurt removed the untriaged New issue has not been triaged by the area owner label Apr 7, 2025
@wfurt wfurt added this to the 10.0.0 milestone Apr 7, 2025
@rzikm rzikm self-assigned this Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants