shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Error when trying to reconnect (secure connection) - Flutter #51

Closed madtocc closed 5 years ago

madtocc commented 5 years ago

Whenever I loose my connection weather by disconnecting the broker, turning my wifi/data off or through calling disconnect() and I try to reconnect using client.connect I get an error due an existing certficate:

E/flutter ( 5332): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception: E/flutter ( 5332): TlsException: Failure trusting builtin roots (OS Error: E/flutter ( 5332): CERT_ALREADY_IN_HASH_TABLE(x509_lu.c:356), errno = 0) E/flutter ( 5332): #0 _SecurityContext.setTrustedCertificatesBytes (dart:io/runtime/binsecure_socket_patch.dart:171:59) E/flutter ( 5332): #1 MqttSecureConnection.connect (file:///Users/mt/Projects/flutter/mqtt_client/lib/src/connectionhandling/mqtt_client_mqtt_secure_connection.dart:56:19) E/flutter ( 5332): #2 SynchronousMqttConnectionHandler.internalConnect (file:///Users/mt/Projects/flutter/mqtt_client/lib/src/connectionhandling/mqtt_client_synchronous_mqtt_connection_handler.dart:53:24) E/flutter ( 5332): E/flutter ( 5332): #3 MqttConnectionHandler.connect (file:///Users/mt/Projects/flutter/mqtt_client/lib/src/connectionhandling/mqtt_client_mqtt_connection_handler.dart:56:13) E/flutter ( 5332): E/flutter ( 5332): #4 MqttClient.connect (file:///Users/mt/Projects/flutter/mqtt_client/lib/src/mqtt_client.dart:165:37) E/flutter ( 5332): E/flutter ( 5332): #5 Mqtt.connect (package:mqtt/mqtt.dart:39:18) E/flutter ( 5332):

I don't know if you are able to reproduce it without flutter...

shamblett commented 5 years ago

Yes, using the iot-core example

lsException: Failure trusting builtin roots (OS Error: CERT_ALREADY_IN_HASH_TABLE(x509_lu.c:356), errno = 0)

0 _SecurityContext.setTrustedCertificatesBytes (dart:io/runtime/binsecure_socket_patch.dart:171:59)

1 _SecurityContext.setTrustedCertificates (dart:io/runtime/binsecure_socket_patch.dart:167:5)

2 MqttSecureConnection.connect (file:///home/steve/Development/google/dart/projects/mqtt_client/lib/src/connectionhandling/mqtt_client_mqtt_secure_connection.dart:52:17)

3 SynchronousMqttConnectionHandler.internalConnect (file:///home/steve/Development/google/dart/projects/mqtt_client/lib/src/connectionhandling/mqtt_client_synchronous_mqtt_connection_handler.dart:53:24)

#4 MqttConnectionHandler.connect (file:///home/steve/Development/google/dart/projects/mqtt_client/lib/src/connectionhandling/mqtt_client_mqtt_connection_handler.dart:56:13) #5 MqttClient.connect (file:///home/steve/Development/google/dart/projects/mqtt_client/lib/src/mqtt_client.dart:163:37) #6 main (file:///home/steve/Development/google/dart/projects/mqtt_client/example/iot_core.dart:58:16) #7 _startIsolate. (dart:isolate/runtime/libisolate_patch.dart:289:19) #8 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12) I believe this comes from the SDK's SSL implementation(Boring SSL I think). why it does this I don't know but I've found that reconnecting without specifying the cert path works Ok, it seem that once the machine you are running on has the cert cached it throws this error. When you have disconnected and reconnect set this parameter to null, it should be OK then.
madtocc commented 5 years ago

Hi Steve, thanks for your prompt answer! 👍 Perfect, thanks!

madtocc commented 5 years ago

@shamblett just a suggestion maybe you can set trustedCertPath to null on the function disconnect under mqtt_client.dart... it fixes this issue (: Cheers

ettaibi commented 4 years ago

I'm still having this issue with iot core, so when setting up the connection

    ByteData  data = await rootBundle.load("assets/pem/roots.pem");
    var context = SecurityContext.defaultContext;
    context.setTrustedCertificatesBytes(data.buffer.asUint8List());

but the next time it will try to setup the connection again, it will raise the issue. I tried "" and null

    context.setTrustedCertificates("");

but I get that the path is not found, or that null is not supported. Any idea please how to solve it ?

thanks in advance

shamblett commented 4 years ago

If the cert is already bset up Ok don't set it again, just comment the context.set.. out, this has worked for me in the past.

ettaibi commented 4 years ago

thanks a lot for your fast reply, do you how to do the check if it is already set or not ? in my case I have a method _setUpMqttClient and I set the certificate at this level, so when I m back to the screen the _setUpMqttClient is called if connection is lost.

shamblett commented 4 years ago

I don't think I did, iot-home isn't a live package as such its more a demo project so I probably just commented the lines out when the error occurred on my test platform, I can't remember the details of it but you should be able to write a little Dart(or any other language for that matter) to set the certs once and then not call it in iot-home, not sure how you would make this portable from platform to platform though.