Skip to content

Commit ddb888a

Browse files
MirkoCovizzijukkar
authored andcommitted
[nrf noup] net: mqtt: add native TLS support
This commit adds an extra parameter in the configuration structure to configure native TLS support at runtime. Signed-off-by: Mirko Covizzi <[email protected]> (cherry picked from commit 3fbd1c5)
1 parent 1183e14 commit ddb888a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/connectivity/networking/api/mqtt.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ additional configuration information:
150150
tls_config->sec_tag_list = m_sec_tags;
151151
tls_config->sec_tag_count = ARRAY_SIZE(m_sec_tags);
152152
tls_config->hostname = MQTT_BROKER_HOSTNAME;
153+
tls_config->set_native_tls = true;
153154
154155
In this sample code, the ``m_sec_tags`` array holds a list of tags, referencing TLS
155156
credentials that the MQTT library should use for authentication. We do not specify
@@ -162,6 +163,8 @@ Note, that TLS credentials referenced by the ``m_sec_tags`` array must be
162163
registered in the system first. For more information on how to do that, refer
163164
to :ref:`secure sockets documentation <secure_sockets_interface>`.
164165

166+
Finally, ``set_native_tls`` can be optionally set to enable native TLS support instead of offloading TLS operations to the modem.
167+
165168
An example of how to use TLS with MQTT is also present in
166169
:zephyr:code-sample:`mqtt-publisher` sample application.
167170

include/zephyr/net/mqtt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,9 @@ struct mqtt_sec_config {
773773

774774
/** Indicates the preference for copying certificates to the heap. */
775775
int cert_nocopy;
776+
777+
/** Set socket to native TLS */
778+
bool set_native_tls;
776779
};
777780

778781
/** @brief MQTT transport type. */

subsys/net/lib/mqtt/mqtt_transport_socket_tls.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ int mqtt_client_tls_connect(struct mqtt_client *client)
2222
{
2323
const struct sockaddr *broker = client->broker;
2424
struct mqtt_sec_config *tls_config = &client->transport.tls.config;
25+
int type = SOCK_STREAM;
2526
int ret;
2627

28+
if (tls_config->set_native_tls) {
29+
type |= SOCK_NATIVE_TLS;
30+
}
31+
2732
client->transport.tls.sock = zsock_socket(broker->sa_family,
28-
SOCK_STREAM, IPPROTO_TLS_1_2);
33+
type, IPPROTO_TLS_1_2);
2934
if (client->transport.tls.sock < 0) {
3035
return -errno;
3136
}

0 commit comments

Comments
 (0)