Skip to content

TlsBufferSession.RequestClientCertificate throws when the TLS 1.3 client didn't offer post-handshake auth #135047

Closed

Description

@caraioniurie47

Description

On a server TlsBufferSession, RequestClientCertificate throws AuthenticationException when the TLS 1.3 client didn't offer post-handshake authentication. In the same case SslStream.NegotiateClientCertificateAsync returns. The TlsBufferSession stays usable after the exception.

Both PALs report the case as SecurityStatusPalErrorCode.NoRenegotiation: OpenSSL fails SSL_verify_client_post_handshake with SSL_R_EXTENSION_NOT_RECEIVED, and SChannel returns SECURITY_STATUS.NoRenegotiation (0x00090360). RequestClientCertificateBufferedCore throws on any failed token, TlsSession.cs:1484-1487:

if (token.Failed)
{
    throw new AuthenticationException(SR.net_auth_SSPI, token.GetException());
}

ProtocolToken.Failed is true for every status except OK and ContinueNeeded, so for NoRenegotiation the branch further down is never reached, TlsSession.cs:1501-1507:

if (!staged)
{
    // The PAL produced no renegotiation request (e.g. the peer
    // declined with NoRenegotiation). Leave the session in its
    // completed state; there is nothing for the caller to drive.
    return TlsOperationStatus.Complete;
}

SslStream.RenegotiateAsync and the Linux socket-bound path (TryFastRequestClientCertificate) check for NoRenegotiation and return instead. A TlsSocketSession on Windows has no such check: it runs the buffered core over the socket and throws the same way.

Reproduction Steps

dotnet new console -n repro (.NET 11 SDK), with this Program.cs. It is a server on 127.0.0.1:5443; with the argument sslstream it does the same through SslStream.

using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

#pragma warning disable SYSLIB5007 // TlsSession is experimental

bool useSslStream = args is ["sslstream"];

using RSA key = RSA.Create(2048);
CertificateRequest request = new("CN=localhost", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
using X509Certificate2 ephemeral = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
using X509Certificate2 serverCert = X509CertificateLoader.LoadPkcs12(ephemeral.Export(X509ContentType.Pfx), null);
SslServerAuthenticationOptions options = new() { ServerCertificate = serverCert, EnabledSslProtocols = SslProtocols.Tls13 };

using TcpListener listener = new(IPAddress.Loopback, 5443);
listener.Start();
Console.WriteLine("Listening on 127.0.0.1:5443");
using NetworkStream transport = (await listener.AcceptTcpClientAsync()).GetStream();

if (useSslStream)
{
    using SslStream server = new(transport);
    await server.AuthenticateAsServerAsync(options);
    await server.ReadAsync(new byte[64]);
    try
    {
        await server.NegotiateClientCertificateAsync();
        Console.WriteLine("NegotiateClientCertificateAsync returned");
    }
    catch (Exception e)
    {
        Console.WriteLine($"NegotiateClientCertificateAsync threw {e.GetType()}: {e.Message}\n  inner {e.InnerException?.GetType()}: {e.InnerException?.Message}");
    }
    await server.WriteAsync("still usable\n"u8.ToArray());
    await server.ShutdownAsync();
    return;
}

using TlsContext context = TlsContext.CreateServer(options);
using TlsBufferSession session = new();
session.SetContext(context);
byte[] input = new byte[64 * 1024];
byte[] output = new byte[64 * 1024];
int inputLength = 0;

// Drive the handshake, then read the client's first line of application data.
while (!session.IsHandshakeComplete)
{
    TlsOperationStatus status = session.Handshake(input.AsSpan(0, inputLength), output, out int consumed, out int written);
    input.AsSpan(consumed, inputLength - consumed).CopyTo(input);
    inputLength -= consumed;
    await transport.WriteAsync(output.AsMemory(0, written));
    if (status == TlsOperationStatus.NeedMoreData)
    {
        inputLength += await transport.ReadAsync(input.AsMemory(inputLength));
    }
    else if (status == TlsOperationStatus.NeedsCertificateValidation)
    {
        session.AcceptWithDefaultValidation();
    }
}
while (true)
{
    session.Read(input.AsSpan(0, inputLength), output, out int consumed, out int written);
    input.AsSpan(consumed, inputLength - consumed).CopyTo(input);
    inputLength -= consumed;
    if (written > 0)
    {
        Console.Write($"Received: {Encoding.ASCII.GetString(output, 0, written)}");
        break;
    }
    inputLength += await transport.ReadAsync(input.AsMemory(inputLength));
}

try
{
    TlsOperationStatus status = session.RequestClientCertificate(output, out int written);
    Console.WriteLine($"RequestClientCertificate returned {status}, {written} bytes");
    await transport.WriteAsync(output.AsMemory(0, written));
}
catch (Exception e)
{
    Console.WriteLine($"RequestClientCertificate threw {e.GetType()}: {e.Message}\n  inner {e.InnerException?.GetType()}: {e.InnerException?.Message}");
}
Console.WriteLine($"IsHandshakeComplete: {session.IsHandshakeComplete}");

session.Write("still usable\n"u8, output, out _, out int encrypted);
await transport.WriteAsync(output.AsMemory(0, encrypted));
session.Shutdown(output, out int alert);
await transport.WriteAsync(output.AsMemory(0, alert));
dotnet build -c Release
dotnet run -c Release --no-build              # or: dotnet run -c Release --no-build sslstream

The client is openssl s_client, which offers post-handshake authentication only with -enable_pha. In a second terminal:

(sleep 1; echo hello; sleep 3) | openssl s_client -connect 127.0.0.1:5443 -tls1_3 -quiet

Expected behavior

RequestClientCertificate returns Complete with nothing written and the session stays in its completed state, as the comment above says. SslStream already behaves this way: with the sslstream argument it prints NegotiateClientCertificateAsync returned on Linux and Windows. With the fix, on Windows:

Listening on 127.0.0.1:5443
Received: hello
RequestClientCertificate returned Complete, 0 bytes
IsHandshakeComplete: True

Actual behavior

Linux, OpenSSL 3.0.2:

Listening on 127.0.0.1:5443
Received: hello
RequestClientCertificate threw System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
  inner Interop+Crypto+OpenSslCryptographicException: error:0A000117:SSL routines::extension not received
IsHandshakeComplete: True

Windows 11:

Listening on 127.0.0.1:5443
Received: hello
RequestClientCertificate threw System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
  inner System.ComponentModel.Win32Exception: The recipient rejected the renegotiation request.
IsHandshakeComplete: True

In every run the client then printed still usable.

Regression?

No. TlsSession is new in .NET 11 (#130366), and the token.Failed throw is in .NET 11.0.0-rc.1 too.

Known Workarounds

Catch AuthenticationException around RequestClientCertificate and treat it as "no certificate"; the session stays usable. Its inner exception is platform-specific.

Configuration

  • .NET 11.0.0-rc.1.26425.128 and the daily build 11.0.0-rtm.26480.113 (dotnet/runtime c43cf3c6448, release/11.0), x64.
  • Ubuntu 22.04 (WSL2) with OpenSSL 3.0.2.
  • Windows 11 (build 26200) with SChannel. The client there was Git for Windows' openssl s_client 3.5.7.

Other information

Only the not-offered case differs: when the client offers post-handshake authentication but has no certificate (s_client -enable_pha, or a Windows SslStream client, which offers it even without a certificate), the request is sent, and on the daily build TlsBufferSession, TlsSocketSession and SslStream all fail validation with RemoteCertificateNotAvailable. So a .NET client on Windows doesn't hit this.

Related: found while working on #134640. #134641 made the OpenSSL PAL recognize this decline as NoRenegotiation on OpenSSL 1.1.1 too, which fixed SslStream and the Linux socket-bound session there. The buffered core throws on any failed status, so that change doesn't reach it.

Searching issues and PRs for TlsBufferSession RequestClientCertificate, TlsSession NoRenegotiation and RequestClientCertificate AuthenticationException found no existing report.

I have a fix with a test ready and will open a PR for it shortly. Could this be assigned to me?

Note

AI-generated, written at my direction and reviewed by me before posting. The repro was compiled and run as written on .NET 11.0.0-rc.1.26425.128 and on the daily build 11.0.0-rtm.26480.113, on Ubuntu 22.04 x64 with OpenSSL 3.0.2 and on Windows 11 x64 with openssl s_client 3.5.7 as the client; the expected output is the same repro on a local Windows build of main with the fix. What a Windows SslStream client offers, the -enable_pha results and the TlsSocketSession results come from a larger scratch app run on the same builds. The excerpts are from main at ced3e57460d; the daily build's runtime commit has the same RequestClientCertificateBufferedCore.

Activity

  1. added
    untriaged
    on Oct 1, 2026
  2. dotnet-policy-service commented on Oct 1, 2026

    @dotnet-policy-service
    Contributor

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

  3. removed
    untriaged
    on Oct 1, 2026
  4. added this to the 12.0.0 milestone on Oct 1, 2026
  5. wfurt commented on Oct 1, 2026

    @wfurt
    Member

    thanks for the report @caraioniurie47. We should fix it.

  6. added a commit that references this issue on Oct 2, 2026
    c94f652
  7. locked and limited conversation to collaborators on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions