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: Michael Scott <michael@opensourcefoundries.com>
This commit is contained in:
Michael Scott 2017-12-04 21:54:16 -08:00 committed by Anas Nashif
commit 6ddbd56853
3 changed files with 62 additions and 0 deletions

View file

@ -1,5 +1,6 @@
/*
* 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) {