shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
544 stars 176 forks source link

TLS no validation #553

Open spartakos87 opened 1 month ago

spartakos87 commented 1 month ago

Hello, I am trying to subscribe to a specific topic in MQTT broker which use TLS with no validation. But I failed because of certification failed to validation my flutter code

client = MqttServerClient.withPort(broker, clientId, port);
    final context = SecurityContext(withTrustedRoots: false);
    client!.secure = false;
    client!.securityContext = context;
    client!.logging(on: true);

On the other hand, I have already achieve to connect to this broker with this Python script


client = mqtt.Client(client_id=client_id)
client.username_pw_set(username, password)
def on_connect(client, userdata, flags, rc):
    print(f"Connected with result code {rc}")
    client.subscribe(topic)

def on_message(client, userdata, msg):
    print(f"Received message '{msg.payload.decode()}' on topic '{msg.topic}'")

client.on_connect = on_connect
client.on_message = on_message
client.tls_set(cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLS)
client.connect(broker, port)
client.loop_forever()

Any idea how to solve this problem? Thanks

spartakos87 commented 1 month ago

I also try to use the defaultContext

final context = SecurityContext.defaultContext;
    client!.secure = false;
    client!.securityContext = context;

And the result was,

I/flutter (16544): 1-2024-07-23 07:31:10.430226 -- SynchronousMqttServerConnectionHandler::internalConnect failed, attempt 3
I/flutter (16544): 1-2024-07-23 07:31:10.430684 -- SynchronousMqttServerConnectionHandler::internalConnect failed
I/flutter (16544): MQTT::NoConnectionException - mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?
I/flutter (16544): 1-2024-07-23 07:31:10.438501 -- MqttConnectionHandlerBase::disconnect - entered
I/flutter (16544): 1-2024-07-23 07:31:10.439620 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
I/flutter (16544): MQTT::Disconnected
shamblett commented 1 month ago

If you are using secure sockets the 'secure' flag needs to be set to true, as per the API docs, you are setting it to false -

client!.secure = false;

This won't work, set it to true.