Mqtt C client authentication

183 views
Skip to first unread message

Daniele Carvalho

unread,
Apr 7, 2017, 8:35:11 AM4/7/17
to MQTT
Hello!

I have installed mosquitto broker on my vps and I installed Certbot for Let's Encrypt Certificates form https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7.

When I connect via websockets I can connect and send and recieve messages evething is ok.

But i have this development kit (samd21 + winc1500)  as a c client and i can't authenticate my connection...

Any advice?


My base code is the mqtt_chat_example project from Atmel Studio 7.
This function doesn't allow me to send a certificate. 

int mqtt_connect_broker (struct mqtt_module *const module, uint8_t clean_session, const char *id, const char *password, const char *client_id, const char *will_topic, const char *will_msg, uint32_t will_msg_len, uint8_t will_qos, uint8_t will_retain);


It was working before I installed Certbot for Let's Encrypt Certificates (without the authentication).

Thanks in advance.
Daniele

Daniele Carvalho

unread,
Apr 7, 2017, 9:20:05 AM4/7/17
to MQTT

Mosquitto broker via Mqtt: (error)

1491560759: New connection from 89.114.125.123 on port 8883.
1491560759: OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure
1491560759: Socket error on client <unknown>, disconnecting.


Mosquitto broker via websockets: (success)

1491568948: New client connected from 89.114.125.123 as websocket_client_86f8624b (c1, k60, u'admin').
1491568948: Sending CONNACK to websocket_client_86f8624b (0, 0)
1491568948: Received SUBSCRIBE from websocket_client_86f8624b
1491568948:     tedayc/# (QoS 0)
1491568948: websocket_client_86f8624b 0 tedayc/#
1491568948: Sending SUBACK to websocket_client_86f8624b
1491568949: Received PUBLISH from websocket_client_86f8624b (d0, q0, r0, m0, 'tedayc/websocket_client_86f8624b', ... (9 bytes))
1491568949: Sending PUBLISH to websocket_client_86f8624b (d0, q0, r0, m0, 'tedayc/websocket_client_86f8624b', ... (9 bytes))



I think i need to send the ca certificate via mqtt but i dont have extra options or parameters to send it. My function only asks for the username and the password.

/**
 * \brief Send MQTT connect message to broker server with MQTT parameter.
 * If operation of this function is complete, MQTT_CALLBACK_CONNECTED event will be sent through MQTT callback.
 *
 * \param[in]  module_inst     Instance of MQTT module.
 * \param[in]  clean_session   If this value set to 1, Broker server store the previous subscribed informations after disconnected.
 * \param[in]  id              ID of user.
 * \param[in]  password        Password of user.
 * \param[in]  client_id       Client ID of this connection.
 * \param[in]  will_topic      A MQTT topic that will be sent to subscribers when this connection is disconnected.
 * \param[in]  will_msg        A MQTT payload that will be sent to subscribers when this connection is disconnected.
 * \param[in]  will_qos        QOS level of will message.
 * \param[in]  will_retain     Whether broker server will be store MQTT will message or not.
 *
 * \return     0               Function succeeded
 * \return     -ENOENT         No such address.
 * \return     -EINVAL         Invalid argument.
 * \return     -ENOSPC         No space left on device.
 * \return     -EIO            Device was occurred error due to unknown exception.
 * \return     -EDESTADDRREQ   Destination address required.
 * \return     -ECONNRESET     Connection reset by peer.
 * \return     -EAGAIN         Try again.
 * \return     -EBUSY          Device or resource busy.
 * \return     -EADDRINUSE     Address already in use.
 * \return     -EALREADY       Socket already connected.
 * \return     -ENOTCONN       Service in bad state.
 * \return     -ECONNREFUSED   Connection refused.
 * \return     -EOVERFLOW      Value too large for defined data type.
 * \return     -EBADMSG        Not a data message.
 */

int mqtt_connect_broker (struct mqtt_module *const module, uint8_t clean_session, const char *id, const char *password, const char *client_id, const char *will_topic, const char *will_msg, uint32_t will_msg_len, uint8_t will_qos, uint8_t will_retain);

mqtt_connect_broker(&module_inst, 1, MAIN_MQTT_USERNAME, MAIN_MQTT_PASSWORD, mqtt_user, NULL, NULL, 0, 0, 0);

Filipe Nicoli

unread,
Apr 24, 2017, 8:09:57 AM4/24/17
to MQTT
You may need to implement authentication by yourself modifying your library.
You can use this as an example or try to port it to your architecture.
Reply all
Reply to author
Forward
0 new messages