mqtt_client icon indicating copy to clipboard operation
mqtt_client copied to clipboard

cannot add the self signed certificate to the flutter App Local Trust Certificate Store

Open kira2015 opened this issue 2 years ago • 1 comments

    String filePath = '/Users/xxx/mqtt.pem';
    SecurityContext security = SecurityContext.defaultContext;
    security.allowLegacyUnsafeRenegotiation = true;

    ByteData fileByte = await rootBundle.load(filePath);
    if (File(filePath).existsSync()) {
      security.setClientAuthorities(filePath);
      security.setTrustedCertificates(filePath);
      security.useCertificateChain(filePath);
    }

    _client.securityContext = security;
    _client.setProtocolV311();

log


flutter: 1-2022-08-31 11:34:31.961919 -- MqttClient::connect - keep alive is enabled with a value of 15 seconds
flutter: 1-2022-08-31 11:34:31.965749 -- MqttConnectionKeepAlive:: Initialised with a keep alive value of 15 seconds
flutter: 1-2022-08-31 11:34:31.966406 -- MqttConnectionKeepAlive:: Disconnect on no ping response is disabled
flutter: 1-2022-08-31 11:34:31.968253 -- MqttConnectionHandlerBase::connect - server test-xx.im.cn, port 19814
flutter: 1-2022-08-31 11:34:31.970540 -- SynchronousMqttServerConnectionHandler::internalConnect entered
flutter: 1-2022-08-31 11:34:31.971435 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
flutter: 1-2022-08-31 11:34:31.972216 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
flutter: 1-2022-08-31 11:34:31.973169 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect
flutter: 1-2022-08-31 11:34:31.974252 -- MqttNormalConnection::connect - entered
flutter: 1-2022-08-31 11:34:32.012251 -- MqttServerConnection::_startListening
flutter: 1-2022-08-31 11:34:32.015683 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
flutter: 1-2022-08-31 11:34:32.016329 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
flutter: 1-2022-08-31 11:34:32.017215 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=15
MqttConnectPayload - client identifier is : rootx
flutter: 1-2022-08-31 11:34:32.033058 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:32.066745 -- MqttConnectionBase::_onDone - calling disconnected callback
flutter: 1-2022-08-31 11:34:37.036674 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:37.037373 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 1, auto reconnect in progress false
flutter: 1-2022-08-31 11:34:37.037822 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
flutter: 1-2022-08-31 11:34:37.038216 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect
flutter: 1-2022-08-31 11:34:37.038587 -- MqttNormalConnection::connect - entered
flutter: 1-2022-08-31 11:34:37.042051 -- MqttServerConnection::_startListening
flutter: 1-2022-08-31 11:34:37.042718 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
flutter: 1-2022-08-31 11:34:37.043123 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
flutter: 1-2022-08-31 11:34:37.043538 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 76
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=15
MqttConnectPayload - client identifier is : rootx
flutter: 1-2022-08-31 11:34:37.044366 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:37.108374 -- MqttConnectionBase::_onDone - calling disconnected callback
flutter: 1-2022-08-31 11:34:42.046116 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:42.046690 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 2, auto reconnect in progress false
flutter: 1-2022-08-31 11:34:42.047108 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
flutter: 1-2022-08-31 11:34:42.047486 -- SynchronousMqttServerConnectionHandler::internalConnect - calling connect
flutter: 1-2022-08-31 11:34:42.047864 -- MqttNormalConnection::connect - entered
flutter: 1-2022-08-31 11:34:42.055768 -- MqttServerConnection::_startListening
flutter: 1-2022-08-31 11:34:42.056356 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
flutter: 1-2022-08-31 11:34:42.056759 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
flutter: 1-2022-08-31 11:34:42.057141 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 76
Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=true, WillFlag=true, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=15
MqttConnectPayload - client identifier is : rootx
flutter: 1-2022-08-31 11:34:42.057953 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:42.116857 -- MqttConnectionBase::_onDone - calling disconnected callback
flutter: 1-2022-08-31 11:34:46.969717 -- MqttConnectionKeepAlive::pingRequired - NOT sending ping - not connected
flutter: 1-2022-08-31 11:34:46.970246 -- MqttConnectionKeepAlive::pingRequired - restarting ping timer
flutter: 1-2022-08-31 11:34:47.061118 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code of noneSpecified and a disconnection origin of none
flutter: 1-2022-08-31 11:34:47.061650 -- SynchronousMqttServerConnectionHandler::internalConnect failed
flutter: MQTTX 连接报错:mqtt-client::NoConnectionException: The maximum allowed connection attempts ({3}) were exceeded. The broker is not responding to the connection request message (Missing Connection Acknowledgement?
flutter: MQTTX 连接失败

My demo can be connected to broker.emqx.io:1883 without using a certificate

kira2015 avatar Aug 31 '22 03:08 kira2015

You are connecting and sending a connect message which is being rejected by your broker, you need to check your broker logs to find out why.

The SecurityContext class is provided by the Dart/flutter runtime here , not the client. If you are having problems with this or your platform you need to contact the flutter guys at Google, the client can't help you with this.

shamblett avatar Aug 31 '22 06:08 shamblett