net: app: Allow TLS and DTLS to be enabled separately

TLS and DTLS are not related to each other so allow DTLS to be
enabled even if TLS is disabled.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2017-08-26 01:04:02 +03:00 committed by Anas Nashif
parent adb1df7f8f
commit 098483d6dd
6 changed files with 30 additions and 30 deletions

View File

@ -11,7 +11,7 @@
#ifndef __NET_APP_H
#define __NET_APP_H
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
#if defined(CONFIG_MBEDTLS)
#if !defined(CONFIG_MBEDTLS_CFG_FILE)
#include "mbedtls/config.h"
@ -38,7 +38,7 @@
#include <mbedtls/error.h>
#include <mbedtls/debug.h>
#endif /* CONFIG_MBEDTLS */
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#include <net/net_ip.h>
#include <net/net_pkt.h>
@ -169,7 +169,7 @@ typedef int (*net_app_send_data_t)(struct net_pkt *pkt,
void *token,
void *user_data);
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
/* Internal information for managing TLS data */
struct tls_context {
struct net_pkt *rx_pkt;
@ -241,7 +241,7 @@ typedef int (*net_app_ca_cert_cb_t)(struct net_app_ctx *ctx,
*/
typedef int (*net_app_entropy_src_cb_t)(void *data, unsigned char *output,
size_t len, size_t *olen);
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#if defined(CONFIG_NET_APP_DTLS)
struct dtls_timing_context {
@ -333,7 +333,7 @@ struct net_app_ctx {
} client;
#endif /* CONFIG_NET_APP_CLIENT */
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
struct {
/** TLS stack for mbedtls library. */
k_thread_stack_t stack;
@ -389,7 +389,7 @@ struct net_app_ctx {
/** Have we called connect cb yet? */
bool connect_cb_called;
} tls;
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
/** Network packet (net_pkt) memory pool for network contexts attached
@ -877,7 +877,7 @@ int net_app_close(struct net_app_ctx *ctx);
*/
int net_app_release(struct net_app_ctx *ctx);
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
#if defined(CONFIG_NET_APP_CLIENT)
/**
* @brief Initialize TLS support for this net_app client context.
@ -955,7 +955,7 @@ int net_app_server_tls(struct net_app_ctx *ctx,
#endif /* CONFIG_NET_APP_SERVER */
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
/**
* @}

View File

@ -87,6 +87,7 @@ config NET_APP_CLIENT
config NET_APP_TLS
bool "Enable TLS support for TCP applications"
default n
depends on NET_TCP
select MBEDTLS
help
Enables net app library to use TLS for encrypted communication.
@ -94,10 +95,10 @@ config NET_APP_TLS
config NET_APP_DTLS
bool "Enable DTLS support for UDP applications"
depends on NET_UDP
depends on NET_APP_TLS
select MBEDTLS
default n
help
Enables net app library to use TLS for encrypted UDP communication.
Enables net app library to use DTLS for encrypted UDP communication.
config NET_APP_DTLS_TIMEOUT
int "DTLS session timeout"
@ -109,7 +110,7 @@ config NET_APP_DTLS_TIMEOUT
config NET_DEBUG_APP_TLS_LEVEL
int "Debug level for mbedtls in net app library"
depends on NET_APP_TLS && NET_DEBUG_APP
depends on (NET_APP_TLS || NET_APP_DTLS) && NET_DEBUG_APP
default 0
range 0 4
help
@ -124,7 +125,7 @@ config NET_DEBUG_APP_TLS_LEVEL
config NET_APP_TLS_STACK_SIZE
int "TLS handler thread stack size"
default 8192
depends on NET_APP_TLS
depends on NET_APP_TLS || NET_APP_DTLS
help
TLS handler thread stack size. The mbedtls routines will use this stack
thus it is by default very large.

View File

@ -28,10 +28,10 @@
#include "net_app_private.h"
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
#define TLS_STARTUP_TIMEOUT K_SECONDS(5)
static int start_tls_client(struct net_app_ctx *ctx);
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#if defined(CONFIG_DNS_RESOLVER)
static void dns_cb(enum dns_resolve_status status,
@ -422,7 +422,7 @@ static void _app_connected(struct net_context *net_ctx,
{
struct net_app_ctx *ctx = user_data;
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (ctx->is_tls) {
k_sem_give(&ctx->client.connect_wait);
}
@ -430,7 +430,7 @@ static void _app_connected(struct net_context *net_ctx,
net_context_recv(net_ctx, ctx->recv_cb, K_NO_WAIT, ctx);
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (ctx->is_tls) {
/* If we have TLS connection, the connect cb is called
* after TLS handshakes are done.
@ -553,7 +553,7 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
return -EAFNOSUPPORT;
}
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (ctx->is_tls && !ctx->tls.tid &&
(ctx->proto == IPPROTO_TCP ||
(IS_ENABLED(CONFIG_NET_APP_DTLS) && ctx->proto == IPPROTO_UDP))) {
@ -571,7 +571,7 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
}
#else
ARG_UNUSED(started);
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#if defined(CONFIG_NET_APP_DTLS)
if (ctx->proto == IPPROTO_UDP) {
@ -608,7 +608,7 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
if (ret < 0) {
NET_DBG("Cannot connect to peer (%d)", ret);
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (started) {
_net_app_tls_handler_stop(ctx);
}
@ -618,7 +618,7 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
return ret;
}
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
static void tls_client_handler(struct net_app_ctx *ctx,
struct k_sem *startup_sync)
{
@ -746,4 +746,4 @@ int net_app_client_tls(struct net_app_ctx *ctx,
*/
return 0;
}
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */

View File

@ -864,7 +864,7 @@ int net_app_close(struct net_app_ctx *ctx)
return 0;
}
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
#if defined(MBEDTLS_DEBUG_C) && defined(CONFIG_NET_DEBUG_APP)
static void my_debug(void *ctx, int level,
const char *file, int line, const char *str)
@ -1301,7 +1301,6 @@ void _net_app_tls_received(struct net_context *context,
*/
}
}
dtls_disconnect:
#endif /* CONFIG_NET_APP_DTLS */
ret = k_mem_pool_alloc(ctx->tls.pool, &block,
@ -1960,5 +1959,5 @@ void _net_app_tls_handler_stop(struct net_app_ctx *ctx)
k_thread_abort(ctx->tls.tid);
ctx->tls.tid = 0;
}
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */

View File

@ -103,7 +103,7 @@ void _net_app_accept_cb(struct net_context *net_ctx,
#if defined(CONFIG_NET_APP_CLIENT)
#endif /* CONFIG_NET_APP_CLIENT */
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
bool _net_app_server_tls_enable(struct net_app_ctx *ctx);
bool _net_app_server_tls_disable(struct net_app_ctx *ctx);
void _net_app_tls_handler_stop(struct net_app_ctx *ctx);
@ -111,7 +111,7 @@ int _net_app_tls_init(struct net_app_ctx *ctx, int client_or_server);
int _net_app_entropy_source(void *data, unsigned char *output, size_t len,
size_t *olen);
int _net_app_ssl_tx(void *context, const unsigned char *buf, size_t size);
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
#if defined(CONFIG_NET_APP_DTLS)
#include "../../ip/connection.h"

View File

@ -260,7 +260,7 @@ fail:
return ret;
}
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
static inline void new_server(struct net_app_ctx *ctx,
const char *server_banner)
{
@ -426,7 +426,7 @@ int net_app_server_tls(struct net_app_ctx *ctx,
/* Then mbedtls specific initialization */
return 0;
}
#endif /* CONFIG_NET_APP_TLS */
#endif /* CONFIG_NET_APP_TLS || CONFIG_NET_APP_DTLS */
bool net_app_server_enable(struct net_app_ctx *ctx)
{
@ -438,7 +438,7 @@ bool net_app_server_enable(struct net_app_ctx *ctx)
ctx->is_enabled = true;
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (ctx->is_tls) {
_net_app_server_tls_enable(ctx);
}
@ -456,7 +456,7 @@ bool net_app_server_disable(struct net_app_ctx *ctx)
ctx->is_enabled = false;
#if defined(CONFIG_NET_APP_TLS)
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)
if (ctx->is_tls) {
_net_app_server_tls_disable(ctx);
}