.NET 8 C#: SslStream "Cannot determine the frame size or a corrupted frame was received."

101 Views Asked by At

I have two .NET 8 C# applications communicating with one another from two separate servers. I am attempting to use SslStream secure TCP. I am getting an error re: the Authentication between the two applications, and I would please like guidance on how to fix this error.

Here is my code that reads a message.

string ReadMessage(SslStream sslStream)
{
    // Read the  message sent by the server.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    byte[] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)];
        decoder.GetChars(buffer, 0, bytes, chars, 0);
        messageData.Append(chars);
        // Check for EOF.
        if (messageData.ToString().Contains("<EOF>", StringComparison.CurrentCulture))
        {
            break;
        }
    } while (bytes != 0);

    return messageData.ToString();
}

On the line bytes = sslStream.Read(buffer, 0, buffer.Length) I am getting the following error:

System.Security.Authentication.AuthenticationException: Cannot determine the frame size or a corrupted frame was received.
at System.Net.Security.SslStream.GetFrameSize(ReadOnlySpan1 buffer)
at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](CancellationToken cancellationToken, Int32 estimatedSize)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)

I can confirm the following about my connection and certs:

  • Protocol: Tls12
  • Is authenticated as server: True
  • Is encrypted: True
  • Can read: True
  • Can write: True
0

There are 0 best solutions below