From f293d625daec58d98bede7882a0e4863056a7a6f Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Fri, 5 Mar 2021 00:22:49 +0800 Subject: [PATCH] [dtls] make the max dtls buffer length configurable (#6233) On systems where OpenThread share mbedtls configuration with HTTPS and other TLS based protocols. The TLS max content length can be way larger than OpenThread requires. Add an OpenThread config macro to reduce stack consumption under such circumstance. --- src/core/BUILD.gn | 1 + src/core/Makefile.am | 1 + src/core/config/dtls.h | 60 +++++++++++++++++++++++++++++++ src/core/meshcop/dtls.cpp | 2 +- src/core/openthread-core-config.h | 8 +---- 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 src/core/config/dtls.h diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index 89a989229..616bb9417 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -669,6 +669,7 @@ source_set("libopenthread_core_config") { "config/diag.h", "config/dns_client.h", "config/dnssd_server.h", + "config/dtls.h", "config/ip6.h", "config/joiner.h", "config/link_quality.h", diff --git a/src/core/Makefile.am b/src/core/Makefile.am index fb5dfe70d..96387d50f 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -407,6 +407,7 @@ HEADERS_COMMON = \ config/diag.h \ config/dns_client.h \ config/dnssd_server.h \ + config/dtls.h \ config/ip6.h \ config/joiner.h \ config/link_quality.h \ diff --git a/src/core/config/dtls.h b/src/core/config/dtls.h new file mode 100644 index 000000000..e27918c72 --- /dev/null +++ b/src/core/config/dtls.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file includes compile-time configurations for DTLS. + * + */ + +#ifndef CONFIG_DTLS_H_ +#define CONFIG_DTLS_H_ + +#include "config/border_router.h" +#include "config/coap.h" +#include "config/commissioner.h" +#include "config/joiner.h" + +/** + * @def OPENTHREAD_CONFIG_DTLS_MAX_CONTENT_LEN + * + * The max length of the OpenThread dtls content buffer. + * + */ +#ifndef OPENTHREAD_CONFIG_DTLS_MAX_CONTENT_LEN +#define OPENTHREAD_CONFIG_DTLS_MAX_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN +#endif + +#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE || OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE || \ + OPENTHREAD_CONFIG_COMMISSIONER_ENABLE || OPENTHREAD_CONFIG_JOINER_ENABLE +#define OPENTHREAD_CONFIG_DTLS_ENABLE 1 +#else +#define OPENTHREAD_CONFIG_DTLS_ENABLE 0 +#endif + +#endif // CONFIG_DTLS_H_ diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index e10096a51..1bdbc5c9b 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -781,7 +781,7 @@ void Dtls::HandleTimer(void) void Dtls::Process(void) { - uint8_t buf[MBEDTLS_SSL_MAX_CONTENT_LEN]; + uint8_t buf[OPENTHREAD_CONFIG_DTLS_MAX_CONTENT_LEN]; bool shouldDisconnect = false; int rval; diff --git a/src/core/openthread-core-config.h b/src/core/openthread-core-config.h index d17a01496..acac4a622 100644 --- a/src/core/openthread-core-config.h +++ b/src/core/openthread-core-config.h @@ -66,6 +66,7 @@ #include "config/diag.h" #include "config/dns_client.h" #include "config/dnssd_server.h" +#include "config/dtls.h" #include "config/ip6.h" #include "config/joiner.h" #include "config/link_quality.h" @@ -82,13 +83,6 @@ #include "config/time_sync.h" #include "config/tmf.h" -#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE || OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE || \ - OPENTHREAD_CONFIG_COMMISSIONER_ENABLE || OPENTHREAD_CONFIG_JOINER_ENABLE -#define OPENTHREAD_CONFIG_DTLS_ENABLE 1 -#else -#define OPENTHREAD_CONFIG_DTLS_ENABLE 0 -#endif - #undef OPENTHREAD_CORE_CONFIG_H_IN #include "config/openthread-core-config-check.h"