MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

SSL_ERROR ssl3_read_bytes:sslv3 alert handshake failure on linux

Open adriannowaq opened this issue 4 years ago • 5 comments

Describe the bug

Can't connect to server with SSL from Linux, (tried on rasbian - buster and Ubuntu 18.04.1 LTS) same code works fine on Windows, tried also to connect from linux with mosquitto_pub and it works fine.

Which project is your bug related to?

  • Client

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of MQTTnet '3.1.1'.
  2. Using .NET 5.0
  3. Run this code
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var caCertPath = @"ca.crt";
            var clientPfxPath = @"certificate.pfx";


            var caCert = X509Certificate.CreateFromCertFile(caCertPath);
            var clientCert = new X509Certificate2(clientPfxPath, "pfxpass");

            var factory = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();

            var tlsParameters = new MqttClientOptionsBuilderTlsParameters
            {
                UseTls = true,
                Certificates = new List<X509Certificate> { caCert, clientCert },
                IgnoreCertificateRevocationErrors = true,
                IgnoreCertificateChainErrors = true,
                AllowUntrustedCertificates = true,
                SslProtocol = System.Security.Authentication.SslProtocols.Tls12
            };


            var connectOptions = new MqttClientOptionsBuilder()
                .WithKeepAlivePeriod(new TimeSpan(0, 0, 0, 10))
                .WithTcpServer("host", 8883)
                .WithCredentials("user", "password")
                .WithTls(tlsParameters)
                .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
                .WithCleanSession()
                .Build();

            mqttClient.ConnectAsync(connectOptions).Wait();


            while (true)
            {
                mqttClient.PublishAsync("test", DateTime.Now.Second.ToString());
                Thread.Sleep(1000);
            }
        }
    }
}

Expected behavior

I should be able to connect to broker from linux.

Additional context / logging

Unhandled exception. System.AggregateException: One or more errors occurred. (Authentication failed, see inner exception.)
 ---> MQTTnet.Exceptions.MqttCommunicationException: Authentication failed, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
 ---> Interop+OpenSsl+SslException: SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.
 ---> Interop+Crypto+OpenSslCryptographicException: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
   --- End of inner exception stack trace ---
   at Interop.OpenSsl.DoSslHandshake(SafeSslHandle context, ReadOnlySpan`1 input, Byte[]& sendBuf, Int32& sendCount)
   at System.Net.Security.SslStreamPal.HandshakeInternal(SafeFreeCredentials credential, SafeDeleteSslContext& context, ReadOnlySpan`1 inputBuffer, Byte[]& outputBuffer, SslAuthenticationOptions sslAuthenticationOptions)
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
   at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
   at MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2 action, TimeSpan timeout, CancellationToken cancellationToken)
   at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MQTTnet.Adapter.MqttChannelAdapter.WrapAndThrowException(Exception exception)
   at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
   at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
   at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at ConsoleApp3.Program.Main(String[] args) in C:\Users\User\source\repos\ConsoleApp15\ConsoleApp15\Program.cs:line 17

Tried this, but doesnt work for me. #45244

adriannowaq avatar Dec 07 '21 13:12 adriannowaq

Does it make any difference when not specifying the TLS version? Referring to #1271.

logicaloud avatar Dec 08 '21 21:12 logicaloud

Changed SslProtocol to System.Security.Authentication.SslProtocols.None

Now I'm getting error:


Unhandled exception. System.AggregateException: One or more errors occurred. (Transport endpoint is not connected)
 ---> MQTTnet.Exceptions.MqttCommunicationException: Transport endpoint is not connected
 ---> System.Net.Sockets.SocketException (107): Transport endpoint is not connected
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.get_RemoteEndPoint()
   at MQTTnet.Implementations.CrossPlatformSocket.get_RemoteEndPoint()
   at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
   at MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2 action, TimeSpan timeout, CancellationToken cancellationToken)
   at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MQTTnet.Adapter.MqttChannelAdapter.WrapAndThrowException(Exception exception)
   at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
   at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
   at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at ConsoleApp3.Program.Main(String[] args) in C:\Users\Adrian\source\repos\ConsoleApp2\ConsoleApp2\Program.cs:line 15
Aborted

On windows still working.

adriannowaq avatar Dec 09 '21 12:12 adriannowaq

Soulution is to change order of certificates: from

Certificates = new List<X509Certificate> { caCert, clientCert },

to

Certificates = new List<X509Certificate> {  clientCert, caCert },

adriannowaq avatar Dec 30 '21 07:12 adriannowaq

Soulution is to change order of certificates:

Really? Oh lord 😅

SeppPenner avatar Jan 10 '22 17:01 SeppPenner

I assume this is closed and no issue of this library.

chkr1011 avatar Jun 29 '22 15:06 chkr1011