Skip to content

Commit 003377c

Browse files
committed
extmod/modussl_mbedtls: Wire in support for DTLS.
A new boolean argument `dtls` is added to `ussl.wrap_socket()`, if true the library assumes that the underlying socket is a datagram socket (i.e. UDP or similar). Implement our own timer callbacks as the out of the box implementation relies on `gettimeofday()`. To fully support asyncio for DTLS socket, we will need to return a readable or writable event in `poll(MP_STREAM_POLL, ...)` if `_mbedtls_timing_get_delay(self) >= 1`. This is left for future work so as not to interfere with micropython#9871.
1 parent 8b61b1b commit 003377c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

extmod/modussl_mbedtls.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "py/runtime.h"
3636
#include "py/stream.h"
3737
#include "py/objstr.h"
38+
#include "py/smallint.h"
39+
#include "py/mphal.h"
3840

3941
// mbedtls_time_t
4042
#include "mbedtls/platform.h"
@@ -43,6 +45,7 @@
4345
#include "mbedtls/pk.h"
4446
#include "mbedtls/entropy.h"
4547
#include "mbedtls/ctr_drbg.h"
48+
#include "mbedtls/timing.h"
4649
#include "mbedtls/debug.h"
4750
#include "mbedtls/error.h"
4851

@@ -61,6 +64,12 @@ typedef struct _mp_obj_ssl_socket_t {
6164

6265
uintptr_t poll_mask; // Indicates which read or write operations the protocol needs next
6366
int last_error; // The last error code, if any
67+
68+
#ifdef MBEDTLS_SSL_PROTO_DTLS
69+
mp_uint_t timer_start_ms;
70+
mp_int_t timer_fin_ms;
71+
mp_int_t timer_int_ms;
72+
#endif
6473
} mp_obj_ssl_socket_t;
6574

6675
struct ssl_args {
@@ -71,6 +80,7 @@ struct ssl_args {
7180
mp_arg_val_t cert_reqs;
7281
mp_arg_val_t cadata;
7382
mp_arg_val_t do_handshake;
83+
mp_arg_val_t dtls;
7484
};
7585

7686
STATIC const mp_obj_type_t ussl_socket_type;
@@ -158,6 +168,40 @@ STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
158168
}
159169
}
160170

171+
#ifdef MBEDTLS_SSL_PROTO_DTLS
172+
STATIC void _mbedtls_timing_set_delay(void *ctx, uint32_t int_ms, uint32_t fin_ms) {
173+
mp_obj_ssl_socket_t *o = (mp_obj_ssl_socket_t *)ctx;
174+
175+
o->timer_int_ms = int_ms;
176+
o->timer_fin_ms = fin_ms;
177+
178+
if (fin_ms != 0) {
179+
o->timer_start_ms = mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1);
180+
}
181+
}
182+
183+
STATIC int _mbedtls_timing_get_delay(void *ctx) {
184+
mp_obj_ssl_socket_t *o = (mp_obj_ssl_socket_t *)ctx;
185+
186+
if (o->timer_fin_ms == 0) {
187+
return -1;
188+
}
189+
190+
mp_uint_t now = mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1);
191+
mp_int_t elapsed_ms = ((now - o->timer_start_ms + MICROPY_PY_UTIME_TICKS_PERIOD / 2) & (MICROPY_PY_UTIME_TICKS_PERIOD - 1))
192+
- MICROPY_PY_UTIME_TICKS_PERIOD / 2;
193+
194+
if (elapsed_ms >= o->timer_fin_ms) {
195+
return 2;
196+
}
197+
198+
if (elapsed_ms >= o->timer_int_ms) {
199+
return 1;
200+
}
201+
202+
return 0;
203+
}
204+
#endif
161205

162206
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
163207
// Verify the socket object has the full stream protocol
@@ -194,7 +238,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
194238

195239
ret = mbedtls_ssl_config_defaults(&o->conf,
196240
args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
197-
MBEDTLS_SSL_TRANSPORT_STREAM,
241+
args->dtls.u_bool ? MBEDTLS_SSL_TRANSPORT_DATAGRAM : MBEDTLS_SSL_TRANSPORT_STREAM,
198242
MBEDTLS_SSL_PRESET_DEFAULT);
199243
if (ret != 0) {
200244
goto cleanup;
@@ -219,6 +263,12 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
219263
}
220264
}
221265

266+
#ifdef MBEDTLS_SSL_PROTO_DTLS
267+
mbedtls_ssl_set_timer_cb(&o->ssl, o,
268+
_mbedtls_timing_set_delay,
269+
_mbedtls_timing_get_delay);
270+
#endif
271+
222272
mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
223273

224274
if (args->key.u_obj != mp_const_none) {
@@ -483,6 +533,7 @@ STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_
483533
{ MP_QSTR_cert_reqs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MBEDTLS_SSL_VERIFY_NONE}},
484534
{ MP_QSTR_cadata, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
485535
{ MP_QSTR_do_handshake, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
536+
{ MP_QSTR_dtls, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
486537
};
487538

488539
// TODO: Check that sock implements stream protocol

ports/esp32/boards/sdkconfig.base

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ CONFIG_LWIP_PPP_CHAP_SUPPORT=y
4646
# SSL
4747
# Use 4kiB output buffer instead of default 16kiB (because IDF heap is fragmented in 4.0)
4848
CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y
49+
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
4950

5051
# ULP coprocessor support
5152
CONFIG_ESP32_ULP_COPROC_ENABLED=y

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