Commit 6ddbd568 authored by Michael Scott's avatar Michael Scott Committed by Anas Nashif
Browse files

net: lwm2m: add support for DTLS



- Add needed settings for DTLS support to the lwm2m_ctx structure.
- Add initialization of MBEDTLS to the LwM2M lib based on the
  user application settings in lwm2m_ctx.

Signed-off-by: default avatarMichael Scott <michael@opensourcefoundries.com>
parent 7111491b
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2017 Linaro Limited
 * Copyright (c) 2017 Open Source Foundries Limited.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
@@ -56,6 +57,22 @@ struct lwm2m_ctx {
	struct coap_pending pendings[CONFIG_LWM2M_ENGINE_MAX_PENDING];
	struct coap_reply replies[CONFIG_LWM2M_ENGINE_MAX_REPLIES];
	struct k_delayed_work retransmit_work;

#if defined(CONFIG_NET_APP_DTLS)
	/** Pre-Shared Key  Information*/
	unsigned char *client_psk;
	size_t client_psk_len;
	char *client_psk_id;
	size_t client_psk_id_len;

	/** DTLS support structures */
	char *cert_host;
	u8_t *dtls_result_buf;
	size_t dtls_result_buf_len;
	struct k_mem_pool *dtls_pool;
	k_thread_stack_t *dtls_stack;
	size_t dtls_stack_len;
#endif
};

typedef void *(*lwm2m_engine_get_data_cb_t)(u16_t obj_inst_id,
+3 −0
Original line number Diff line number Diff line
zephyr_link_interface_ifdef(CONFIG_MBEDTLS mbedTLS)
zephyr_library()

zephyr_include_directories(.)
@@ -36,3 +37,5 @@ zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_TEMP_SENSOR
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_LIGHT_CONTROL
    ipso_light_control.c
    )

zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2017 Linaro Limited
 * Copyright (c) 2017 Open Source Foundries Limited.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
@@ -75,6 +76,10 @@
#define REG_PREFACE		""
#endif

#if defined(CONFIG_NET_APP_DTLS)
#define INSTANCE_INFO "Zephyr DTLS LwM2M-client"
#endif

#define MAX_TOKEN_LEN		8

struct observe_node {
@@ -3215,6 +3220,25 @@ void lwm2m_engine_context_init(struct lwm2m_ctx *client_ctx)
#endif
}

#if defined(CONFIG_NET_APP_DTLS)
static int setup_cert(struct net_app_ctx *app_ctx, void *cert)
{
#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
	struct lwm2m_ctx *client_ctx = CONTAINER_OF(app_ctx,
						    struct lwm2m_ctx,
						    net_app_ctx);
	return mbedtls_ssl_conf_psk(
			&app_ctx->tls.mbedtls.conf,
			(const unsigned char *)client_ctx->client_psk,
			client_ctx->client_psk_len,
			(const unsigned char *)client_ctx->client_psk_id,
			client_ctx->client_psk_id_len);
#else
	return 0;
#endif
}
#endif /* CONFIG_NET_APP_DTLS */

int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,
		       char *peer_str, u16_t peer_port)
{
@@ -3242,6 +3266,24 @@ int lwm2m_engine_start(struct lwm2m_ctx *client_ctx,
		goto error_start;
	}

#if defined(CONFIG_NET_APP_DTLS)
	ret = net_app_client_tls(&client_ctx->net_app_ctx,
				 client_ctx->dtls_result_buf,
				 client_ctx->dtls_result_buf_len,
				 INSTANCE_INFO,
				 strlen(INSTANCE_INFO),
				 setup_cert,
				 client_ctx->cert_host,
				 NULL,
				 client_ctx->dtls_pool,
				 client_ctx->dtls_stack,
				 client_ctx->dtls_stack_len);
	if (ret < 0) {
		SYS_LOG_ERR("Cannot init DTLS (%d)", ret);
		goto error_start;
	}
#endif

	ret = net_app_connect(&client_ctx->net_app_ctx,
			      client_ctx->net_timeout);
	if (ret < 0) {