Skip to content

Commit b765af9

Browse files
author
Zhang Jun Hao
committed
feat(mbedtls): Add mbedtls debug function
1 parent 3693d42 commit b765af9

File tree

6 files changed

+124
-10
lines changed

6 files changed

+124
-10
lines changed

components/ssl/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ target_link_libraries(ssl "-L ${CMAKE_CURRENT_SOURCE_DIR}/wolfssl/lib")
3636
target_link_libraries(ssl wolfssl)
3737
else()
3838
if(CONFIG_SSL_USING_MBEDTLS)
39-
target_compile_options(${COMPONENT_NAME} PUBLIC -DMBEDTLS_CONFIG_FILE="esp_config.h")
39+
target_compile_options(${COMPONENT_NAME} PUBLIC -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h")
4040
endif()
4141
endif()

components/ssl/Kconfig

+4-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ menu "mbedTLS"
2121

2222
config MBEDTLS_SSL_MAX_CONTENT_LEN
2323
int "TLS maximum message content length"
24-
default 16384
24+
default 4096
2525
range 512 16384
2626
help
2727
Maximum TLS message length (in bytes) supported by mbedTLS.
@@ -45,14 +45,12 @@ config MBEDTLS_DEBUG
4545
help
4646
Enable mbedTLS debugging functions at compile time.
4747

48-
If this option is enabled, you can include
49-
"mbedtls/esp_debug.h" and call mbedtls_esp_enable_debug_log()
50-
at runtime in order to enable mbedTLS debug output via the ESP
51-
log mechanism.
48+
If this option is enabled, you must call mbedtls_esp_enable_debug_log
49+
at runtime in order to enable mbedTLS debug output.
5250

5351
config MBEDTLS_HAVE_TIME
5452
bool "Enable mbedtls time"
55-
default n
53+
default y
5654
help
5755
System has time.h and time().
5856
The time does not need to be correct, only time differences are used,

components/ssl/Makefile.projbuild

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# alternative config file
33

44
#ifdef CONFIG_SSL_USING_MBEDTLS
5-
CPPFLAGS += -DMBEDTLS_CONFIG_FILE='"esp_config.h"'
5+
CPPFLAGS += -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"'
66
#endif
77

88
#ifdef CONFIG_SSL_USING_WOLFSSL

components/ssl/mbedtls/port/include/esp_config.h renamed to components/ssl/mbedtls/port/include/mbedtls/esp_config.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@
19721972
* This module provides debugging functions.
19731973
*/
19741974
#if CONFIG_MBEDTLS_DEBUG
1975-
//#define MBEDTLS_DEBUG_C
1975+
#define MBEDTLS_DEBUG_C
19761976
#endif
19771977

19781978
/**
@@ -2887,7 +2887,6 @@
28872887
//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
28882888

28892889
/* SSL options */
2890-
extern unsigned int max_content_len;
28912890
#define MBEDTLS_SSL_MAX_CONTENT_LEN CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
28922891
//#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
28932892
//#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef _ESP_DEBUG_H_
15+
#define _ESP_DEBUG_H_
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
#include "sdkconfig.h"
22+
23+
#ifdef CONFIG_MBEDTLS_DEBUG
24+
25+
/** @brief Enable mbedTLS debug logging via the esp_log mechanism.
26+
*
27+
* mbedTLS internal debugging is filtered from a specified mbedTLS
28+
* threshold level to esp_log level at runtime:
29+
*
30+
* - 1 - Warning
31+
* - 2 - Info
32+
* - 3 - Debug
33+
* - 4 - Verbose
34+
*
35+
* (Note that mbedTLS debug thresholds are not always consistently used.)
36+
*
37+
* This function will set the esp log level for "mbedtls" to the specified mbedTLS
38+
* threshold level that matches. However, the overall max ESP log level must be set high
39+
* enough in menuconfig, or some messages may be filtered at compile time.
40+
*
41+
* @param conf mbedtls_ssl_config structure
42+
* @param mbedTLS debug threshold, 0-4. Messages are filtered at runtime.
43+
*/
44+
void mbedtls_esp_enable_debug_log(mbedtls_ssl_config *conf, int threshold);
45+
46+
/** @brief Disable mbedTLS debug logging via the esp_log mechanism.
47+
*
48+
*/
49+
void mbedtls_esp_disable_debug_log(mbedtls_ssl_config *conf);
50+
51+
#endif
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#endif /* __ESP_DEBUG_H__ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <strings.h>
16+
17+
#include "mbedtls/platform.h"
18+
#include "mbedtls/debug.h"
19+
#include "mbedtls/ssl.h"
20+
#include "mbedtls/esp_debug.h"
21+
22+
#ifdef MBEDTLS_DEBUG_C
23+
24+
static void mbedtls_esp_debug(void *ctx, int level,
25+
const char *file, int line,
26+
const char *str);
27+
28+
void mbedtls_esp_enable_debug_log(mbedtls_ssl_config *conf, int threshold)
29+
{
30+
mbedtls_debug_set_threshold(threshold);
31+
mbedtls_ssl_conf_dbg(conf, mbedtls_esp_debug, NULL);
32+
}
33+
34+
void mbedtls_esp_disable_debug_log(mbedtls_ssl_config *conf)
35+
{
36+
mbedtls_ssl_conf_dbg(conf, NULL, NULL);
37+
}
38+
39+
/* Default mbedtls debug function that translates mbedTLS debug output
40+
to ESP_LOGx debug output.
41+
*/
42+
static void mbedtls_esp_debug(void *ctx, int level,
43+
const char *file, int line,
44+
const char *str)
45+
{
46+
char *file_sep;
47+
48+
/* Shorten 'file' from the whole file path to just the filename
49+
50+
This is a bit wasteful because the macros are compiled in with
51+
the full _FILE_ path in each case.
52+
*/
53+
file_sep = rindex(file, '/');
54+
if(file_sep)
55+
file = file_sep+1;
56+
57+
printf("mbedtls: %s:%d %s", file, line, str);
58+
}
59+
60+
#endif

0 commit comments

Comments
 (0)