Skip to content

Commit

Permalink
extmod/modussl_mbedtls: Wire in support for PSK modes.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Tournoud <[email protected]>
  • Loading branch information
damz committed Jan 7, 2024
1 parent ddb064f commit 12f3c0f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions extmod/modssl_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,28 @@ STATIC mp_obj_t ssl_context_set_ciphers(mp_obj_t self_in, mp_obj_t ciphersuite)
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_context_set_ciphers_obj, ssl_context_set_ciphers);

#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
// SSLContext.set_psk(username, password)
STATIC mp_obj_t ssl_context_set_psk(mp_obj_t self_in, mp_obj_t username, mp_obj_t password) {
mp_obj_ssl_context_t *ssl_context = MP_OBJ_TO_PTR(self_in);
int ret;

size_t psk_identity_len;
const byte *psk_identity = (const byte *)mp_obj_str_get_data(username, &psk_identity_len);

size_t psk_key_len;
const byte *psk_key = (const byte *)mp_obj_str_get_data(password, &psk_key_len);

ret = mbedtls_ssl_conf_psk(&ssl_context->conf, (const unsigned char *) psk_key, psk_key_len, (const unsigned char *) psk_identity, psk_identity_len);
if (ret != 0) {
mbedtls_raise_error(ret);
}

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(ssl_context_set_psk_obj, ssl_context_set_psk);
#endif

STATIC void ssl_context_load_key(mp_obj_ssl_context_t *self, mp_obj_t key_obj, mp_obj_t cert_obj) {
size_t key_len;
const byte *key = (const byte *)mp_obj_str_get_data(key_obj, &key_len);
Expand Down Expand Up @@ -487,6 +509,9 @@ STATIC const mp_rom_map_elem_t ssl_context_locals_dict_table[] = {
#endif
{ MP_ROM_QSTR(MP_QSTR_get_ciphers), MP_ROM_PTR(&ssl_context_get_ciphers_obj)},
{ MP_ROM_QSTR(MP_QSTR_set_ciphers), MP_ROM_PTR(&ssl_context_set_ciphers_obj)},
#ifdef MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED
{ MP_ROM_QSTR(MP_QSTR_set_psk), MP_ROM_PTR(&ssl_context_set_psk_obj)},
#endif
{ MP_ROM_QSTR(MP_QSTR_load_cert_chain), MP_ROM_PTR(&ssl_context_load_cert_chain_obj)},
{ MP_ROM_QSTR(MP_QSTR_load_verify_locations), MP_ROM_PTR(&ssl_context_load_verify_locations_obj)},
{ MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_context_wrap_socket_obj) },
Expand Down
3 changes: 3 additions & 0 deletions ports/esp32/boards/sdkconfig.base
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ CONFIG_MBEDTLS_PLATFORM_TIME_ALT=y
CONFIG_MBEDTLS_HAVE_TIME=y
# Enable DTLS
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
# Enable PSK support
CONFIG_MBEDTLS_PSK_MODES=y
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK=y

# Disable ALPN support as it's not implemented in MicroPython
CONFIG_MBEDTLS_SSL_ALPN=n
Expand Down
4 changes: 4 additions & 0 deletions ports/unix/mbedtls/mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
// Enable DTLS
#define MBEDTLS_SSL_PROTO_DTLS

// Enable PSK modes and ciphers
#define MBEDTLS_PSK_MODES
#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED

// Enable mbedtls modules
#define MBEDTLS_HAVEGE_C
#define MBEDTLS_TIMING_C
Expand Down

0 comments on commit 12f3c0f

Please sign in to comment.