Skip to content

Commit 5264478

Browse files
tvedpgeorge
authored andcommitted
extmod/modussl_mbedtls: Integrate shorter error strings.
The stm32 and esp32 ports now use shorter error strings for mbedtls errors. Also, MBEDTLS_ERROR_C is enabled on stm32 by default to get these strings.
1 parent 3e758ef commit 5264478

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

extmod/modussl_mbedtls.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons
7777
#endif
7878

7979
STATIC NORETURN void mbedtls_raise_error(int err) {
80-
#if defined(MBEDTLS_ERROR_C)
81-
// Including mbedtls_strerror takes about 16KB on the esp32 due to all the strings.
82-
// MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
83-
// It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
84-
// "small" negative integer error codes come from underlying stream/sockets, not mbedtls
80+
// _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the
81+
// underlying socket into negative codes to pass them through mbedtls. Here we turn them
82+
// positive again so they get interpreted as the OSError they really are. The
83+
// cut-off of -256 is a bit hacky, sigh.
8584
if (err < 0 && err > -256) {
8685
mp_raise_OSError(-err);
8786
}
8887

88+
#if defined(MBEDTLS_ERROR_C)
89+
// Including mbedtls_strerror takes about 1.5KB due to the error strings.
90+
// MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
91+
// It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
92+
8993
// Try to allocate memory for the message
90-
#define ERR_STR_MAX 100 // mbedtls_strerror truncates if it doesn't fit
94+
#define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit
9195
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
9296
byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX);
9397
if (o_str == NULL || o_str_buf == NULL) {
@@ -96,7 +100,7 @@ STATIC NORETURN void mbedtls_raise_error(int err) {
96100

97101
// print the error message into the allocated buffer
98102
mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX);
99-
size_t len = strnlen((char *)o_str_buf, ERR_STR_MAX);
103+
size_t len = strlen((char *)o_str_buf);
100104

101105
// Put the exception object together
102106
o_str->base.type = &mp_type_str;
@@ -108,7 +112,7 @@ STATIC NORETURN void mbedtls_raise_error(int err) {
108112
nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
109113
#else
110114
// mbedtls is compiled without error strings so we simply return the err number
111-
mp_raise_OSError(err); // typ. err is negative
115+
mp_raise_OSError(err); // err is typically a large negative number
112116
#endif
113117
}
114118

ports/esp32/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ EXTMOD_SRC_C += $(addprefix extmod/,\
362362
)
363363

364364
LIB_SRC_C = $(addprefix lib/,\
365+
mbedtls_errors/mp_mbedtls_errors.c \
365366
mp-readline/readline.c \
366367
netutils/netutils.c \
367368
timeutils/timeutils.c \
@@ -506,11 +507,12 @@ ESPIDF_LWIP_O = $(patsubst %.c,%.o,\
506507
$(wildcard $(ESPCOMP)/lwip/port/esp32/*/*.c) \
507508
)
508509

509-
ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o,\
510+
# Mbedtls source files, exclude error.c in favor of lib/mbedtls_errors/mp_mbedtls_errors.c
511+
ESPIDF_MBEDTLS_O = $(patsubst %.c,%.o, $(filter-out %/error.c,\
510512
$(wildcard $(ESPCOMP)/mbedtls/mbedtls/library/*.c) \
511513
$(wildcard $(ESPCOMP)/mbedtls/port/*.c) \
512514
$(wildcard $(ESPCOMP)/mbedtls/port/esp32/*.c) \
513-
)
515+
))
514516

515517
ESPIDF_MDNS_O = $(patsubst %.c,%.o,$(wildcard $(ESPCOMP)/mdns/*.c))
516518

ports/stm32/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ endif
472472
ifeq ($(MICROPY_SSL_MBEDTLS),1)
473473
CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"'
474474
SRC_MOD += mbedtls/mbedtls_port.c
475+
# replace mbedtls' error.c by ours
476+
SRC_MOD := $(filter-out %/mbedtls/library/error.c, $(SRC_MOD))
477+
LIB_SRC_C += lib/mbedtls_errors/mp_mbedtls_errors.c
475478
endif
476479

477480
ifeq ($(MICROPY_PY_BLUETOOTH),1)

ports/stm32/mbedtls/mbedtls_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#define MBEDTLS_CTR_DRBG_C
6868
//#define MBEDTLS_ECP_C
6969
#define MBEDTLS_ENTROPY_C
70+
#define MBEDTLS_ERROR_C
7071
#define MBEDTLS_MD_C
7172
#define MBEDTLS_MD5_C
7273
#define MBEDTLS_OID_C

tests/net_inet/test_tls_sites.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def main():
5454
test_one(site, opts)
5555
print(site, "ok")
5656
except Exception as e:
57-
print(site, repr(e))
57+
print(site, e)
5858

5959

6060
main()

tests/net_inet/tls_text_errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def test(addr):
1414
print("wrap: no exception")
1515
except OSError as e:
1616
# mbedtls produces "mbedtls -0x7200: SSL - An invalid SSL record was received"
17-
# axtls produces "RECORD_OVERFLOW"
17+
# axtls produces "RECORD_OVERFLOW" but also prints "TLS buffer overflow,..."
1818
# CPython produces "[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1108)"
1919
ok = (
20-
"invalid SSL record" in str(e)
20+
"SSL_INVALID_RECORD" in str(e)
2121
or "RECORD_OVERFLOW" in str(e)
2222
or "wrong version" in str(e)
2323
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy