Skip to content

Commit 3d93c9a

Browse files
committed
extmod/modtls_mbedtls: Add server-side support for TLS tickets.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
1 parent 8161d87 commit 3d93c9a

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

extmod/modtls_mbedtls.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@
6363
#include "mbedtls/asn1.h"
6464
#endif
6565

66+
#if defined(MBEDTLS_CONFIG_FILE)
67+
#include MBEDTLS_CONFIG_FILE
68+
#endif
69+
70+
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
71+
#include "mbedtls/ssl_ticket.h"
72+
#endif
73+
6674
#ifndef MICROPY_MBEDTLS_CONFIG_BARE_METAL
6775
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (0)
6876
#endif
@@ -86,6 +94,9 @@ typedef struct _mp_obj_ssl_context_t {
8694
mbedtls_x509_crt cacert;
8795
mbedtls_x509_crt cert;
8896
mbedtls_pk_context pkey;
97+
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
98+
mbedtls_ssl_ticket_context ticket;
99+
#endif
89100
int authmode;
90101
int *ciphersuites;
91102
mp_obj_t handler;
@@ -340,6 +351,9 @@ static mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args
340351
mbedtls_x509_crt_init(&self->cacert);
341352
mbedtls_x509_crt_init(&self->cert);
342353
mbedtls_pk_init(&self->pkey);
354+
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
355+
mbedtls_ssl_ticket_init(&self->ticket);
356+
#endif
343357
self->ciphersuites = NULL;
344358
self->handler = mp_const_none;
345359
#if MICROPY_PY_SSL_ECDSA_SIGN_ALT
@@ -381,6 +395,14 @@ static mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args
381395
mbedtls_ssl_conf_dbg(&self->conf, mbedtls_debug, NULL);
382396
#endif
383397

398+
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
399+
ret = mbedtls_ssl_ticket_setup(&self->ticket, mbedtls_ctr_drbg_random, &self->ctr_drbg, MBEDTLS_CIPHER_AES_256_GCM, 86400);
400+
if (ret != 0) {
401+
mbedtls_raise_error(ret);
402+
}
403+
mbedtls_ssl_conf_session_tickets_cb(&self->conf, mbedtls_ssl_ticket_write, mbedtls_ssl_ticket_parse, &self->ticket);
404+
#endif
405+
384406
return MP_OBJ_FROM_PTR(self);
385407
}
386408

@@ -421,6 +443,9 @@ static void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
421443
#if MICROPY_PY_SSL_FINALISER
422444
static mp_obj_t ssl_context___del__(mp_obj_t self_in) {
423445
mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
446+
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_TICKET_C)
447+
mbedtls_ssl_ticket_free(&self->ticket);
448+
#endif
424449
mbedtls_pk_free(&self->pkey);
425450
mbedtls_x509_crt_free(&self->cert);
426451
mbedtls_x509_crt_free(&self->cacert);

ports/unix/mbedtls/mbedtls_config_port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828

2929
// Set mbedtls configuration
3030
#define MBEDTLS_CIPHER_MODE_CTR // needed for MICROPY_PY_CRYPTOLIB_CTR
31+
#define MBEDTLS_SSL_SESSION_TICKETS
3132

3233
// Enable mbedtls modules
34+
#define MBEDTLS_GCM_C
35+
#define MBEDTLS_SSL_TICKET_C
3336
#define MBEDTLS_TIMING_C
3437

3538
#if defined(MICROPY_UNIX_COVERAGE)

0 commit comments

Comments
 (0)