Repository navigation
Description
Description
On Linux, a server TlsSocketSession reads the ClientHello off the socket itself before OpenSSL takes the socket over: always when the options passed to TlsContext.CreateServer have no server certificate, certificate context or certificate selection callback, otherwise while the System.Net.Security.CaptureClientHello switch is on (the default). When that read fails, TlsSession throws an IOException built from net_ssl_read_bio_failed_error without SR.Format, so the message contains a literal {0}:
int rc = Interop.Ssl.BioPeekTlsFrame(_peekBio, out byte* framePtr, out int frameLen);
if (rc == 0)
{
// Need more bytes off the socket. Caller polls SelectRead and retries.
result = TlsOperationStatus.NeedMoreData;
return;
}
if (rc < 0)
{
throw new IOException(SR.net_ssl_read_bio_failed_error);
} <data name="net_ssl_read_bio_failed_error" xml:space="preserve">
<value>SSL Read BIO failed with OpenSSL error - {0}.</value>The read fails when the peer closes or resets the connection before a full TLS record has arrived, or when the record header announces more bytes than the read buffer holds. Formatting the resource would not give a true message either: none of these failures is an OpenSSL error. CryptoNative_BioPeekTlsFrame clears the OpenSSL error queue on entry, and its -1 returns follow argument and buffer checks, a failed malloc, the record-length check, EOF and a failed recv:
int32_t CryptoNative_BioPeekTlsFrame(BIO* bio, uint8_t** outPtr, int32_t* outLen)
{
ERR_clear_error();
if (bio == NULL || outPtr == NULL || outLen == NULL)
{
return -1;
}
SocketReplayBioCtx* ctx = GetSocketReplayBioCtx(bio);
if (ctx == NULL || ctx->fd < 0)
{
return -1;
} if (need > ctx->prefixCap)
{
return -1; // record too large for our buffer
} if (n == 0)
{
return -1; // peer closed before ClientHello
}
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
return 0; // caller polls and retries
}
return -1;Reproduction Steps
A client connects to a server TlsSocketSession and shuts down its sending side without sending anything. The program drives Handshake() and prints the exception.
repro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>$(NoWarn);SYSLIB5007</NoWarn>
</PropertyGroup>
</Project>Program.cs:
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using X509Certificate2 cert = CreateCertificate();
using TlsContext ctx = TlsContext.CreateServer(new SslServerAuthenticationOptions { ServerCertificate = cert });
using Socket listener = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen();
using Socket client = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(listener.LocalEndPoint!);
using Socket server = listener.Accept();
server.Blocking = false;
// The client closes its side without sending a ClientHello.
client.Shutdown(SocketShutdown.Send);
using TlsSocketSession session = new(server.SafeHandle);
session.SetContext(ctx);
try
{
TlsOperationStatus status;
while ((status = session.Handshake()) == TlsOperationStatus.NeedMoreData)
{
server.Poll(-1, SelectMode.SelectRead);
}
Console.WriteLine($"Handshake returned {status}");
}
catch (Exception ex)
{
Console.WriteLine($"{ex.GetType().Name}: {ex.Message}");
}
static X509Certificate2 CreateCertificate()
{
using RSA key = RSA.Create(2048);
CertificateRequest request = new("CN=localhost", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
using X509Certificate2 cert = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
return X509CertificateLoader.LoadPkcs12(cert.Export(X509ContentType.Pfx), null);
}Build and run:
dotnet build -c Release -o out
dotnet out/repro.dll
Expected behavior
An IOException whose message says the ClientHello could not be read, with no placeholder and no claim of an OpenSSL error.
Actual behavior
On .NET 11.0.0-rc.1.26420.103:
IOException: SSL Read BIO failed with OpenSSL error - {0}.
Regression?
No. The line came in with the commit that added TlsContext and TlsSession (#130366), and main at 90b22d9 still has it unchanged.
Known Workarounds
When the options passed to TlsContext.CreateServer include a server certificate, setting DOTNET_SYSTEM_NET_SECURITY_CAPTURECLIENTHELLO=0 (the System.Net.Security.CaptureClientHello switch) skips this read, at the cost of ClientHello capture. The same client behaviors then throw AuthenticationException ("Authentication failed, see inner exception.") with an inner message of SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.
Configuration
.NET 11.0.0-rc.1.26420.103, Ubuntu 22.04.5 LTS (WSL2), x64, OpenSSL 3.0.2. A local build of main at 90b22d9 prints the same line.
Only Linux was run. TlsSession.OpenSsl.cs is compiled for every platform target except Windows, Android and Apple:
System.Net.Security.csproj:86-87
<Compile Include="System\Net\Security\TlsSession.OpenSsl.cs"
Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows' and '$(UseAndroidCrypto)' != 'true' and '$(UseAppleCrypto)' != 'true'" />Other information
The fix is a new resource without a placeholder, net_ssl_read_client_hello_failed ("The TLS ClientHello could not be read from the socket."), thrown in the same IOException. With it, a client that shuts down its sending side, a client that resets the connection, and a record header announcing a 65535-byte fragment all give that message.
Options not taken:
Interop.OpenSsl.CreateSslException(SR.net_ssl_read_bio_failed_error), as the other uses of the resource do: the OpenSSL error queue is empty here, so the message becomesSSL Read BIO failed with OpenSSL error - ., and the exception type changes fromIOExceptionto the internalInterop+OpenSsl+SslException.SR.Formatwitherrno: theBioPeekTlsFrameimport doesn't setSetLastError, soMarshal.GetLastPInvokeError()doesn't return itserrno; built that way, all three cases above giveSSL Read BIO failed with OpenSSL error - errno 0 (Success).- Separate return codes from
CryptoNative_BioPeekTlsFramefor EOF,recvfailure and an oversized record, so EOF returnsTlsOperationStatus.Closedand arecverror throwsSocketException: changes whatHandshake()returns or throws, which is a separate change from the message. - Throwing
AuthenticationException, as the handshake does when ClientHello capture is off: changes the exception type callers see, which is a separate change from the message.
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.
Source read at dotnet/runtime 90b22d9. The repro ran on Ubuntu 22.04.5 LTS x64 (WSL2) with OpenSSL 3.0.2, on .NET 11.0.0-rc.1.26420.103 and on a ./build.sh clr+libs -rc Release build of 90b22d9, with dotnet build -c Release -o out and dotnet out/repro.dll (the local build through its testhost dotnet with DOTNET_ROLL_FORWARD=LatestMajor). The messages of the first two options not taken come from building each into System.Net.Security on that build and running the fix's test for the three cases, with the exception printed.
Activity
dotnet-policy-service commentedon Oct 13, 2026
Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.
Fix TlsSession's ClientHello peek failure message (#135700)