LCOV - code coverage report
Current view: top level - extmod - modtls_mbedtls.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-298-gb675c8799.info Lines: 255 304 83.9 %
Date: 2025-02-12 10:59:09 Functions: 20 22 90.9 %
Branches: 94 130 72.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2016 Linaro Ltd.
       7                 :            :  * Copyright (c) 2019 Paul Sokolovsky
       8                 :            :  * Copyright (c) 2023 Damien P. George
       9                 :            :  *
      10                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
      11                 :            :  * of this software and associated documentation files (the "Software"), to deal
      12                 :            :  * in the Software without restriction, including without limitation the rights
      13                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      14                 :            :  * copies of the Software, and to permit persons to whom the Software is
      15                 :            :  * furnished to do so, subject to the following conditions:
      16                 :            :  *
      17                 :            :  * The above copyright notice and this permission notice shall be included in
      18                 :            :  * all copies or substantial portions of the Software.
      19                 :            :  *
      20                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      21                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      22                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      23                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      24                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      25                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      26                 :            :  * THE SOFTWARE.
      27                 :            :  */
      28                 :            : 
      29                 :            : #include "py/mpconfig.h"
      30                 :            : #if MICROPY_PY_SSL && MICROPY_SSL_MBEDTLS
      31                 :            : 
      32                 :            : #include <stdio.h>
      33                 :            : #include <string.h>
      34                 :            : #include <errno.h> // needed because mp_is_nonblocking_error uses system error codes
      35                 :            : 
      36                 :            : #include "py/runtime.h"
      37                 :            : #include "py/stream.h"
      38                 :            : #include "py/objstr.h"
      39                 :            : #include "py/reader.h"
      40                 :            : #include "py/gc.h"
      41                 :            : #include "extmod/vfs.h"
      42                 :            : 
      43                 :            : // mbedtls_time_t
      44                 :            : #include "mbedtls/platform.h"
      45                 :            : #include "mbedtls/ssl.h"
      46                 :            : #include "mbedtls/x509_crt.h"
      47                 :            : #include "mbedtls/pk.h"
      48                 :            : #include "mbedtls/entropy.h"
      49                 :            : #include "mbedtls/ctr_drbg.h"
      50                 :            : #include "mbedtls/debug.h"
      51                 :            : #include "mbedtls/error.h"
      52                 :            : #if MBEDTLS_VERSION_NUMBER >= 0x03000000
      53                 :            : #include "mbedtls/build_info.h"
      54                 :            : #else
      55                 :            : #include "mbedtls/version.h"
      56                 :            : #endif
      57                 :            : #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
      58                 :            : #include "mbedtls/ecdsa.h"
      59                 :            : #include "mbedtls/asn1.h"
      60                 :            : #endif
      61                 :            : 
      62                 :            : #ifndef MICROPY_MBEDTLS_CONFIG_BARE_METAL
      63                 :            : #define MICROPY_MBEDTLS_CONFIG_BARE_METAL (0)
      64                 :            : #endif
      65                 :            : 
      66                 :            : #define MP_STREAM_POLL_RDWR (MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)
      67                 :            : 
      68                 :            : // This corresponds to an SSLContext object.
      69                 :            : typedef struct _mp_obj_ssl_context_t {
      70                 :            :     mp_obj_base_t base;
      71                 :            :     mbedtls_entropy_context entropy;
      72                 :            :     mbedtls_ctr_drbg_context ctr_drbg;
      73                 :            :     mbedtls_ssl_config conf;
      74                 :            :     mbedtls_x509_crt cacert;
      75                 :            :     mbedtls_x509_crt cert;
      76                 :            :     mbedtls_pk_context pkey;
      77                 :            :     int authmode;
      78                 :            :     int *ciphersuites;
      79                 :            :     mp_obj_t handler;
      80                 :            :     #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
      81                 :            :     mp_obj_t ecdsa_sign_callback;
      82                 :            :     #endif
      83                 :            : } mp_obj_ssl_context_t;
      84                 :            : 
      85                 :            : // This corresponds to an SSLSocket object.
      86                 :            : typedef struct _mp_obj_ssl_socket_t {
      87                 :            :     mp_obj_base_t base;
      88                 :            :     mp_obj_ssl_context_t *ssl_context;
      89                 :            :     mp_obj_t sock;
      90                 :            :     mbedtls_ssl_context ssl;
      91                 :            : 
      92                 :            :     uintptr_t poll_mask; // Indicates which read or write operations the protocol needs next
      93                 :            :     int last_error; // The last error code, if any
      94                 :            : } mp_obj_ssl_socket_t;
      95                 :            : 
      96                 :            : static const mp_obj_type_t ssl_context_type;
      97                 :            : static const mp_obj_type_t ssl_socket_type;
      98                 :            : 
      99                 :            : static const MP_DEFINE_STR_OBJ(mbedtls_version_obj, MBEDTLS_VERSION_STRING_FULL);
     100                 :            : 
     101                 :            : static mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
     102                 :            :     bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname);
     103                 :            : 
     104                 :            : /******************************************************************************/
     105                 :            : // Helper functions.
     106                 :            : 
     107                 :            : #ifdef MBEDTLS_DEBUG_C
     108                 :            : static void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) {
     109                 :            :     (void)ctx;
     110                 :            :     (void)level;
     111                 :            :     mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str);
     112                 :            : }
     113                 :            : #endif
     114                 :            : 
     115                 :            : // Given a string-like object holding PEM or DER formatted ASN.1 data, return a
     116                 :            : // pointer to its buffer and the correct length for mbedTLS APIs.
     117                 :            : //
     118                 :            : // (mbedTLS >= 3.5 rejects DER formatted data with trailing bytes within keylen,
     119                 :            : // but PEM must include a terminating NUL byte in the keylen...)
     120                 :         20 : static const unsigned char *asn1_get_data(mp_obj_t obj, size_t *out_len) {
     121                 :         20 :     size_t len;
     122                 :         20 :     const char *str = mp_obj_str_get_data(obj, &len);
     123                 :            :     #if defined(MBEDTLS_PEM_PARSE_C)
     124                 :            :     if (strstr(str, "-----BEGIN ") != NULL) {
     125                 :            :         ++len;
     126                 :            :     }
     127                 :            :     #endif
     128                 :         18 :     *out_len = len;
     129                 :         18 :     return (const unsigned char *)str;
     130                 :            : }
     131                 :            : 
     132                 :         14 : static NORETURN void mbedtls_raise_error(int err) {
     133                 :            :     // Handle special cases.
     134         [ -  + ]:         14 :     if (err == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
     135                 :          0 :         mp_raise_OSError(MP_ENOMEM);
     136         [ +  + ]:         14 :     } else if (err == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
     137                 :          2 :         mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
     138         [ +  + ]:         12 :     } else if (err == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
     139                 :          6 :         mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
     140                 :            :     }
     141                 :            : 
     142                 :            :     // _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the
     143                 :            :     // underlying socket into negative codes to pass them through mbedtls. Here we turn them
     144                 :            :     // positive again so they get interpreted as the OSError they really are. The
     145                 :            :     // cut-off of -256 is a bit hacky, sigh.
     146         [ -  + ]:          6 :     if (err < 0 && err > -256) {
     147                 :          0 :         mp_raise_OSError(-err);
     148                 :            :     }
     149                 :            : 
     150                 :            :     #if defined(MBEDTLS_ERROR_C)
     151                 :            :     // Including mbedtls_strerror takes about 1.5KB due to the error strings.
     152                 :            :     // MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
     153                 :            :     // It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
     154                 :            : 
     155                 :            :     // Try to allocate memory for the message
     156                 :            :     #define ERR_STR_MAX 80  // mbedtls_strerror truncates if it doesn't fit
     157                 :          6 :     mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
     158                 :          6 :     byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX);
     159         [ -  + ]:          6 :     if (o_str == NULL || o_str_buf == NULL) {
     160                 :          0 :         mp_raise_OSError(err);
     161                 :            :     }
     162                 :            : 
     163                 :            :     // print the error message into the allocated buffer
     164                 :          6 :     mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX);
     165                 :          6 :     size_t len = strlen((char *)o_str_buf);
     166                 :            : 
     167                 :            :     // Put the exception object together
     168                 :          6 :     o_str->base.type = &mp_type_str;
     169                 :          6 :     o_str->data = o_str_buf;
     170                 :          6 :     o_str->len = len;
     171                 :          6 :     o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
     172                 :            :     // raise
     173                 :          6 :     mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)};
     174                 :          6 :     nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
     175                 :            :     #else
     176                 :            :     // mbedtls is compiled without error strings so we simply return the err number
     177                 :            :     mp_raise_OSError(err); // err is typically a large negative number
     178                 :            :     #endif
     179                 :            : }
     180                 :            : 
     181                 :            : // Stores the current SSLContext for use in mbedtls callbacks where the current state is not passed.
     182                 :            : static inline void store_active_context(mp_obj_ssl_context_t *ssl_context) {
     183                 :            :     #if MICROPY_PY_SSL_MBEDTLS_NEED_ACTIVE_CONTEXT
     184                 :            :     MP_STATE_THREAD(tls_ssl_context) = ssl_context;
     185                 :            :     #endif
     186                 :            : }
     187                 :            : 
     188                 :         18 : static void ssl_check_async_handshake_failure(mp_obj_ssl_socket_t *sslsock, int *errcode) {
     189                 :         18 :     if (
     190                 :            :         #if MBEDTLS_VERSION_NUMBER >= 0x03000000
     191   [ +  +  +  -  :         18 :         (*errcode < 0) && (mbedtls_ssl_is_handshake_over(&sslsock->ssl) == 0) && (*errcode != MBEDTLS_ERR_SSL_CONN_EOF)
                   +  + ]
     192                 :            :         #else
     193                 :            :         (*errcode < 0) && (*errcode != MBEDTLS_ERR_SSL_CONN_EOF)
     194                 :            :         #endif
     195                 :            :         ) {
     196                 :            :         // Asynchronous handshake is done by mbdetls_ssl_read/write.  If the return code is
     197                 :            :         // MBEDTLS_ERR_XX (i.e < 0) and the handshake is not done due to a handshake failure,
     198                 :            :         // then notify peer with proper error code and raise local error with mbedtls_raise_error.
     199                 :            : 
     200         [ -  + ]:          2 :         if (*errcode == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE) {
     201                 :            :             // Check if TLSv1.3 and use proper alert for this case (to be implemented)
     202                 :            :             // uint8_t alert = MBEDTLS_SSL_ALERT_MSG_CERT_REQUIRED; tlsv1.3
     203                 :            :             // uint8_t alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE; tlsv1.2
     204                 :          0 :             mbedtls_ssl_send_alert_message(&sslsock->ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
     205                 :            :                 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
     206                 :            :         }
     207                 :            : 
     208         [ -  + ]:          2 :         if (*errcode == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
     209                 :            :             // The certificate may have been rejected for several reasons.
     210                 :          0 :             char xcbuf[256];
     211                 :          0 :             uint32_t flags = mbedtls_ssl_get_verify_result(&sslsock->ssl);
     212                 :          0 :             int ret = mbedtls_x509_crt_verify_info(xcbuf, sizeof(xcbuf), "\n", flags);
     213                 :            :             // The length of the string written (not including the terminated nul byte),
     214                 :            :             // or a negative err code.
     215         [ #  # ]:          0 :             if (ret > 0) {
     216                 :          0 :                 sslsock->sock = MP_OBJ_NULL;
     217                 :          0 :                 mbedtls_ssl_free(&sslsock->ssl);
     218                 :          0 :                 mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%s"), xcbuf);
     219                 :            :             }
     220                 :            :         }
     221                 :            : 
     222                 :          2 :         sslsock->sock = MP_OBJ_NULL;
     223                 :          2 :         mbedtls_ssl_free(&sslsock->ssl);
     224                 :          2 :         mbedtls_raise_error(*errcode);
     225                 :            :     }
     226                 :         16 : }
     227                 :            : 
     228                 :          0 : static int ssl_sock_cert_verify(void *ptr, mbedtls_x509_crt *crt, int depth, uint32_t *flags) {
     229                 :          0 :     mp_obj_ssl_context_t *o = ptr;
     230         [ #  # ]:          0 :     if (o->handler == mp_const_none) {
     231                 :            :         return 0;
     232                 :            :     }
     233                 :          0 :     mp_obj_array_t cert;
     234                 :          0 :     mp_obj_memoryview_init(&cert, 'B', 0, crt->raw.len, crt->raw.p);
     235                 :          0 :     return mp_obj_get_int(mp_call_function_2(o->handler, MP_OBJ_FROM_PTR(&cert), MP_OBJ_NEW_SMALL_INT(depth)));
     236                 :            : }
     237                 :            : 
     238                 :            : /******************************************************************************/
     239                 :            : // SSLContext type.
     240                 :            : 
     241                 :        300 : static mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
     242                 :        300 :     mp_arg_check_num(n_args, n_kw, 1, 1, false);
     243                 :            : 
     244                 :            :     // This is the "protocol" argument.
     245                 :        298 :     mp_int_t endpoint = mp_obj_get_int(args[0]);
     246                 :            : 
     247                 :            :     // Create SSLContext object.
     248                 :            :     #if MICROPY_PY_SSL_FINALISER
     249                 :        298 :     mp_obj_ssl_context_t *self = mp_obj_malloc_with_finaliser(mp_obj_ssl_context_t, type_in);
     250                 :            :     #else
     251                 :            :     mp_obj_ssl_context_t *self = mp_obj_malloc(mp_obj_ssl_context_t, type_in);
     252                 :            :     #endif
     253                 :            : 
     254                 :            :     // Initialise mbedTLS state.
     255                 :        298 :     mbedtls_ssl_config_init(&self->conf);
     256                 :        298 :     mbedtls_entropy_init(&self->entropy);
     257                 :        298 :     mbedtls_ctr_drbg_init(&self->ctr_drbg);
     258                 :        298 :     mbedtls_x509_crt_init(&self->cacert);
     259                 :        298 :     mbedtls_x509_crt_init(&self->cert);
     260                 :        298 :     mbedtls_pk_init(&self->pkey);
     261                 :        298 :     self->ciphersuites = NULL;
     262                 :        298 :     self->handler = mp_const_none;
     263                 :            :     #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
     264                 :            :     self->ecdsa_sign_callback = mp_const_none;
     265                 :            :     #endif
     266                 :            : 
     267                 :            :     #ifdef MBEDTLS_DEBUG_C
     268                 :            :     // Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
     269                 :            :     mbedtls_debug_set_threshold(3);
     270                 :            :     #endif
     271                 :            : 
     272                 :            :     // Whenever the PSA interface is used (if MBEDTLS_PSA_CRYPTO), psa_crypto_init() needs to be called before any TLS related operations.
     273                 :            :     // TLSv1.3 depends on the PSA interface, TLSv1.2 only uses the PSA stack if MBEDTLS_USE_PSA_CRYPTO is defined.
     274                 :            :     #if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_USE_PSA_CRYPTO)
     275                 :            :     psa_crypto_init();
     276                 :            :     #endif
     277                 :            : 
     278                 :        298 :     const byte seed[] = "upy";
     279                 :        298 :     int ret = mbedtls_ctr_drbg_seed(&self->ctr_drbg, mbedtls_entropy_func, &self->entropy, seed, sizeof(seed));
     280         [ -  + ]:        298 :     if (ret != 0) {
     281                 :          0 :         mbedtls_raise_error(ret);
     282                 :            :     }
     283                 :            : 
     284                 :        298 :     ret = mbedtls_ssl_config_defaults(&self->conf, endpoint,
     285                 :            :         MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
     286         [ -  + ]:        298 :     if (ret != 0) {
     287                 :          0 :         mbedtls_raise_error(ret);
     288                 :            :     }
     289                 :            : 
     290         [ +  + ]:        298 :     if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
     291                 :        286 :         self->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
     292                 :            :     } else {
     293                 :         12 :         self->authmode = MBEDTLS_SSL_VERIFY_NONE;
     294                 :            :     }
     295                 :        298 :     mbedtls_ssl_conf_authmode(&self->conf, self->authmode);
     296                 :        298 :     mbedtls_ssl_conf_verify(&self->conf, &ssl_sock_cert_verify, self);
     297                 :        298 :     mbedtls_ssl_conf_rng(&self->conf, mbedtls_ctr_drbg_random, &self->ctr_drbg);
     298                 :            :     #ifdef MBEDTLS_DEBUG_C
     299                 :            :     mbedtls_ssl_conf_dbg(&self->conf, mbedtls_debug, NULL);
     300                 :            :     #endif
     301                 :            : 
     302                 :        298 :     return MP_OBJ_FROM_PTR(self);
     303                 :            : }
     304                 :            : 
     305                 :       1176 : static void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
     306                 :       1176 :     mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
     307         [ +  + ]:       1176 :     if (dest[0] == MP_OBJ_NULL) {
     308                 :            :         // Load attribute.
     309         [ +  + ]:        858 :         if (attr == MP_QSTR_verify_mode) {
     310                 :          8 :             dest[0] = MP_OBJ_NEW_SMALL_INT(self->authmode);
     311         [ -  + ]:        850 :         } else if (attr == MP_QSTR_verify_callback) {
     312                 :          0 :             dest[0] = self->handler;
     313                 :            :         #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
     314                 :            :         } else if (attr == MP_QSTR_ecdsa_sign_callback) {
     315                 :            :             dest[0] = self->ecdsa_sign_callback;
     316                 :            :         #endif
     317                 :            :         } else {
     318                 :            :             // Continue lookup in locals_dict.
     319                 :        850 :             dest[1] = MP_OBJ_SENTINEL;
     320                 :            :         }
     321         [ +  + ]:        318 :     } else if (dest[1] != MP_OBJ_NULL) {
     322                 :            :         // Store attribute.
     323         [ +  + ]:        316 :         if (attr == MP_QSTR_verify_mode) {
     324                 :        314 :             self->authmode = mp_obj_get_int(dest[1]);
     325                 :        314 :             dest[0] = MP_OBJ_NULL;
     326                 :        314 :             mbedtls_ssl_conf_authmode(&self->conf, self->authmode);
     327                 :            :         #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
     328                 :            :         } else if (attr == MP_QSTR_ecdsa_sign_callback) {
     329                 :            :             dest[0] = MP_OBJ_NULL;
     330                 :            :             self->ecdsa_sign_callback = dest[1];
     331                 :            :         #endif
     332         [ -  + ]:          2 :         } else if (attr == MP_QSTR_verify_callback) {
     333                 :          0 :             dest[0] = MP_OBJ_NULL;
     334                 :          0 :             self->handler = dest[1];
     335                 :            :         }
     336                 :            :     }
     337                 :       1176 : }
     338                 :            : 
     339                 :            : #if MICROPY_PY_SSL_FINALISER
     340                 :        298 : static mp_obj_t ssl_context___del__(mp_obj_t self_in) {
     341                 :        298 :     mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
     342                 :        298 :     mbedtls_pk_free(&self->pkey);
     343                 :        298 :     mbedtls_x509_crt_free(&self->cert);
     344                 :        298 :     mbedtls_x509_crt_free(&self->cacert);
     345                 :        298 :     mbedtls_ctr_drbg_free(&self->ctr_drbg);
     346                 :        298 :     mbedtls_entropy_free(&self->entropy);
     347                 :        298 :     mbedtls_ssl_config_free(&self->conf);
     348                 :        298 :     return mp_const_none;
     349                 :            : }
     350                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(ssl_context___del___obj, ssl_context___del__);
     351                 :            : #endif
     352                 :            : 
     353                 :            : // SSLContext.get_ciphers()
     354                 :          2 : static mp_obj_t ssl_context_get_ciphers(mp_obj_t self_in) {
     355                 :          2 :     mp_obj_t list = mp_obj_new_list(0, NULL);
     356         [ +  + ]:         38 :     for (const int *cipher_list = mbedtls_ssl_list_ciphersuites(); *cipher_list; ++cipher_list) {
     357                 :         36 :         const char *cipher_name = mbedtls_ssl_get_ciphersuite_name(*cipher_list);
     358                 :         36 :         mp_obj_list_append(list, MP_OBJ_FROM_PTR(mp_obj_new_str_from_cstr(cipher_name)));
     359                 :            :     }
     360                 :          2 :     return list;
     361                 :            : }
     362                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(ssl_context_get_ciphers_obj, ssl_context_get_ciphers);
     363                 :            : 
     364                 :            : // SSLContext.set_ciphers(ciphersuite)
     365                 :          6 : static mp_obj_t ssl_context_set_ciphers(mp_obj_t self_in, mp_obj_t ciphersuite) {
     366                 :          6 :     mp_obj_ssl_context_t *ssl_context = MP_OBJ_TO_PTR(self_in);
     367                 :            : 
     368                 :            :     // Check that ciphersuite is a list or tuple.
     369                 :          6 :     size_t len = 0;
     370                 :          6 :     mp_obj_t *ciphers;
     371                 :          6 :     mp_obj_get_array(ciphersuite, &len, &ciphers);
     372         [ -  + ]:          4 :     if (len == 0) {
     373                 :          0 :         mbedtls_raise_error(MBEDTLS_ERR_SSL_BAD_CONFIG);
     374                 :            :     }
     375                 :            : 
     376                 :            :     // Parse list of ciphers.
     377                 :          4 :     ssl_context->ciphersuites = m_new(int, len + 1);
     378         [ +  + ]:          6 :     for (size_t i = 0; i < len; ++i) {
     379                 :          4 :         const char *ciphername = mp_obj_str_get_str(ciphers[i]);
     380                 :          4 :         const int id = mbedtls_ssl_get_ciphersuite_id(ciphername);
     381         [ +  + ]:          4 :         if (id == 0) {
     382                 :          2 :             mbedtls_raise_error(MBEDTLS_ERR_SSL_BAD_CONFIG);
     383                 :            :         }
     384                 :          2 :         ssl_context->ciphersuites[i] = id;
     385                 :            :     }
     386                 :          2 :     ssl_context->ciphersuites[len] = 0;
     387                 :            : 
     388                 :            :     // Configure ciphersuite.
     389                 :          2 :     mbedtls_ssl_conf_ciphersuites(&ssl_context->conf, (const int *)ssl_context->ciphersuites);
     390                 :            : 
     391                 :          2 :     return mp_const_none;
     392                 :            : }
     393                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(ssl_context_set_ciphers_obj, ssl_context_set_ciphers);
     394                 :            : 
     395                 :         10 : static void ssl_context_load_key(mp_obj_ssl_context_t *self, mp_obj_t key_obj, mp_obj_t cert_obj) {
     396                 :         10 :     size_t key_len;
     397                 :         10 :     const unsigned char *key = asn1_get_data(key_obj, &key_len);
     398                 :         10 :     int ret;
     399                 :            :     #if MBEDTLS_VERSION_NUMBER >= 0x03000000
     400                 :         10 :     ret = mbedtls_pk_parse_key(&self->pkey, key, key_len, NULL, 0, mbedtls_ctr_drbg_random, &self->ctr_drbg);
     401                 :            :     #else
     402                 :            :     ret = mbedtls_pk_parse_key(&self->pkey, key, key_len, NULL, 0);
     403                 :            :     #endif
     404         [ +  + ]:         10 :     if (ret != 0) {
     405                 :          2 :         mbedtls_raise_error(MBEDTLS_ERR_PK_BAD_INPUT_DATA); // use general error for all key errors
     406                 :            :     }
     407                 :            : 
     408                 :          8 :     size_t cert_len;
     409                 :          8 :     const unsigned char *cert = asn1_get_data(cert_obj, &cert_len);
     410                 :          6 :     ret = mbedtls_x509_crt_parse(&self->cert, cert, cert_len);
     411         [ +  + ]:          6 :     if (ret != 0) {
     412                 :          4 :         mbedtls_raise_error(MBEDTLS_ERR_X509_BAD_INPUT_DATA); // use general error for all cert errors
     413                 :            :     }
     414                 :            : 
     415                 :          2 :     ret = mbedtls_ssl_conf_own_cert(&self->conf, &self->cert, &self->pkey);
     416         [ -  + ]:          2 :     if (ret != 0) {
     417                 :          0 :         mbedtls_raise_error(ret);
     418                 :            :     }
     419                 :          2 : }
     420                 :            : 
     421                 :            : // SSLContext.load_cert_chain(certfile, keyfile)
     422                 :         10 : static mp_obj_t ssl_context_load_cert_chain(mp_obj_t self_in, mp_obj_t cert, mp_obj_t pkey) {
     423                 :         10 :     mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
     424                 :         10 :     ssl_context_load_key(self, pkey, cert);
     425                 :          2 :     return mp_const_none;
     426                 :            : }
     427                 :            : static MP_DEFINE_CONST_FUN_OBJ_3(ssl_context_load_cert_chain_obj, ssl_context_load_cert_chain);
     428                 :            : 
     429                 :          2 : static void ssl_context_load_cadata(mp_obj_ssl_context_t *self, mp_obj_t cadata_obj) {
     430                 :          2 :     size_t cacert_len;
     431                 :          2 :     const unsigned char *cacert = asn1_get_data(cadata_obj, &cacert_len);
     432                 :          2 :     int ret = mbedtls_x509_crt_parse(&self->cacert, cacert, cacert_len);
     433         [ +  - ]:          2 :     if (ret != 0) {
     434                 :          2 :         mbedtls_raise_error(MBEDTLS_ERR_X509_BAD_INPUT_DATA); // use general error for all cert errors
     435                 :            :     }
     436                 :            : 
     437                 :          0 :     mbedtls_ssl_conf_ca_chain(&self->conf, &self->cacert, NULL);
     438                 :          0 : }
     439                 :            : 
     440                 :            : // SSLContext.load_verify_locations(cadata)
     441                 :          2 : static mp_obj_t ssl_context_load_verify_locations(mp_obj_t self_in, mp_obj_t cadata) {
     442                 :            : 
     443                 :          2 :     mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
     444                 :          2 :     ssl_context_load_cadata(self, cadata);
     445                 :          0 :     return mp_const_none;
     446                 :            : }
     447                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(ssl_context_load_verify_locations_obj, ssl_context_load_verify_locations);
     448                 :            : 
     449                 :        530 : static mp_obj_t ssl_context_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
     450                 :        530 :     enum { ARG_server_side, ARG_do_handshake_on_connect, ARG_server_hostname };
     451                 :        530 :     static const mp_arg_t allowed_args[] = {
     452                 :            :         { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
     453                 :            :         { MP_QSTR_do_handshake_on_connect, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
     454                 :            :         { MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
     455                 :            :     };
     456                 :            : 
     457                 :            :     // Parse arguments.
     458                 :        530 :     mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(pos_args[0]);
     459                 :        530 :     mp_obj_t sock = pos_args[1];
     460                 :        530 :     mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
     461                 :        530 :     mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
     462                 :            : 
     463                 :            :     // Create and return the new SSLSocket object.
     464                 :       1058 :     return ssl_socket_make_new(self, sock, args[ARG_server_side].u_bool,
     465                 :        530 :         args[ARG_do_handshake_on_connect].u_bool, args[ARG_server_hostname].u_obj);
     466                 :            : }
     467                 :            : static MP_DEFINE_CONST_FUN_OBJ_KW(ssl_context_wrap_socket_obj, 2, ssl_context_wrap_socket);
     468                 :            : 
     469                 :            : static const mp_rom_map_elem_t ssl_context_locals_dict_table[] = {
     470                 :            :     #if MICROPY_PY_SSL_FINALISER
     471                 :            :     { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ssl_context___del___obj) },
     472                 :            :     #endif
     473                 :            :     { MP_ROM_QSTR(MP_QSTR_get_ciphers), MP_ROM_PTR(&ssl_context_get_ciphers_obj)},
     474                 :            :     { MP_ROM_QSTR(MP_QSTR_set_ciphers), MP_ROM_PTR(&ssl_context_set_ciphers_obj)},
     475                 :            :     { MP_ROM_QSTR(MP_QSTR_load_cert_chain), MP_ROM_PTR(&ssl_context_load_cert_chain_obj)},
     476                 :            :     { MP_ROM_QSTR(MP_QSTR_load_verify_locations), MP_ROM_PTR(&ssl_context_load_verify_locations_obj)},
     477                 :            :     { MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_context_wrap_socket_obj) },
     478                 :            : };
     479                 :            : static MP_DEFINE_CONST_DICT(ssl_context_locals_dict, ssl_context_locals_dict_table);
     480                 :            : 
     481                 :            : static MP_DEFINE_CONST_OBJ_TYPE(
     482                 :            :     ssl_context_type,
     483                 :            :     MP_QSTR_SSLContext,
     484                 :            :     MP_TYPE_FLAG_NONE,
     485                 :            :     make_new, ssl_context_make_new,
     486                 :            :     attr, ssl_context_attr,
     487                 :            :     locals_dict, &ssl_context_locals_dict
     488                 :            :     );
     489                 :            : 
     490                 :            : /******************************************************************************/
     491                 :            : // SSLSocket type.
     492                 :            : 
     493                 :         30 : static int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
     494                 :         30 :     mp_obj_t sock = *(mp_obj_t *)ctx;
     495                 :            : 
     496                 :         30 :     const mp_stream_p_t *sock_stream = mp_get_stream(sock);
     497                 :         30 :     int err;
     498                 :            : 
     499                 :         30 :     mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
     500         [ +  + ]:         30 :     if (out_sz == MP_STREAM_ERROR) {
     501         [ -  + ]:          2 :         if (mp_is_nonblocking_error(err)) {
     502                 :            :             return MBEDTLS_ERR_SSL_WANT_WRITE;
     503                 :            :         }
     504                 :          0 :         return -err; // convert an MP_ERRNO to something mbedtls passes through as error
     505                 :            :     } else {
     506                 :         28 :         return out_sz;
     507                 :            :     }
     508                 :            : }
     509                 :            : 
     510                 :            : // _mbedtls_ssl_recv is called by mbedtls to receive bytes from the underlying socket
     511                 :         66 : static int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
     512                 :         66 :     mp_obj_t sock = *(mp_obj_t *)ctx;
     513                 :            : 
     514                 :         66 :     const mp_stream_p_t *sock_stream = mp_get_stream(sock);
     515                 :         66 :     int err;
     516                 :            : 
     517                 :         66 :     mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err);
     518         [ +  + ]:         66 :     if (out_sz == MP_STREAM_ERROR) {
     519         [ -  + ]:         12 :         if (mp_is_nonblocking_error(err)) {
     520                 :            :             return MBEDTLS_ERR_SSL_WANT_READ;
     521                 :            :         }
     522                 :          0 :         return -err;
     523                 :            :     } else {
     524                 :         54 :         return out_sz;
     525                 :            :     }
     526                 :            : }
     527                 :            : 
     528                 :        530 : static mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
     529                 :            :     bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname) {
     530                 :            : 
     531                 :            :     // Store the current SSL context.
     532                 :        530 :     store_active_context(ssl_context);
     533                 :            : 
     534                 :            :     // Verify the socket object has the full stream protocol
     535                 :        530 :     mp_get_stream_raise(sock, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
     536                 :            : 
     537                 :            :     #if MICROPY_PY_SSL_FINALISER
     538                 :        530 :     mp_obj_ssl_socket_t *o = mp_obj_malloc_with_finaliser(mp_obj_ssl_socket_t, &ssl_socket_type);
     539                 :            :     #else
     540                 :            :     mp_obj_ssl_socket_t *o = mp_obj_malloc(mp_obj_ssl_socket_t, &ssl_socket_type);
     541                 :            :     #endif
     542                 :        530 :     o->ssl_context = ssl_context;
     543                 :        530 :     o->sock = sock;
     544                 :        530 :     o->poll_mask = 0;
     545                 :        530 :     o->last_error = 0;
     546                 :            : 
     547                 :        530 :     int ret;
     548                 :        530 :     uint32_t flags = 0;
     549                 :            : 
     550                 :        530 :     mbedtls_ssl_init(&o->ssl);
     551                 :            : 
     552                 :        530 :     ret = mbedtls_ssl_setup(&o->ssl, &ssl_context->conf);
     553                 :            :     #if !MICROPY_MBEDTLS_CONFIG_BARE_METAL
     554                 :            :     if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
     555                 :            :         // If mbedTLS relies on platform libc heap for buffers (i.e. esp32
     556                 :            :         // port), then run a GC pass and then try again. This is useful because
     557                 :            :         // it may free a Python object (like an old SSL socket) whose finaliser
     558                 :            :         // frees some platform-level heap.
     559                 :            :         gc_collect();
     560                 :            :         ret = mbedtls_ssl_setup(&o->ssl, &ssl_context->conf);
     561                 :            :     }
     562                 :            :     #endif
     563         [ -  + ]:        530 :     if (ret != 0) {
     564                 :          0 :         goto cleanup;
     565                 :            :     }
     566                 :            : 
     567         [ +  + ]:        530 :     if (server_hostname != mp_const_none) {
     568                 :          2 :         const char *sni = mp_obj_str_get_str(server_hostname);
     569                 :          2 :         ret = mbedtls_ssl_set_hostname(&o->ssl, sni);
     570         [ -  + ]:          2 :         if (ret != 0) {
     571                 :          0 :             goto cleanup;
     572                 :            :         }
     573   [ -  +  -  - ]:        528 :     } else if (ssl_context->authmode == MBEDTLS_SSL_VERIFY_REQUIRED && server_side == false) {
     574                 :            : 
     575                 :          0 :         o->sock = MP_OBJ_NULL;
     576                 :          0 :         mbedtls_ssl_free(&o->ssl);
     577                 :          0 :         mp_raise_ValueError(MP_ERROR_TEXT("CERT_REQUIRED requires server_hostname"));
     578                 :            :     }
     579                 :            : 
     580                 :        530 :     mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
     581                 :            : 
     582         [ +  + ]:        530 :     if (do_handshake_on_connect) {
     583         [ +  - ]:          2 :         while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) {
     584         [ +  - ]:          2 :             if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
     585                 :          2 :                 goto cleanup;
     586                 :            :             }
     587                 :          0 :             mp_event_wait_ms(1);
     588                 :            :         }
     589                 :            :     }
     590                 :            : 
     591                 :        528 :     return MP_OBJ_FROM_PTR(o);
     592                 :            : 
     593                 :          2 : cleanup:
     594         [ -  + ]:          2 :     if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
     595                 :          0 :         flags = mbedtls_ssl_get_verify_result(&o->ssl);
     596                 :            :     }
     597                 :            : 
     598                 :          2 :     o->sock = MP_OBJ_NULL;
     599                 :          2 :     mbedtls_ssl_free(&o->ssl);
     600                 :            : 
     601         [ -  + ]:          2 :     if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
     602                 :          0 :         char xcbuf[256];
     603                 :          0 :         int ret_info = mbedtls_x509_crt_verify_info(xcbuf, sizeof(xcbuf), "\n", flags);
     604                 :            :         // The length of the string written (not including the terminated nul byte),
     605                 :            :         // or a negative err code.
     606         [ #  # ]:          0 :         if (ret_info > 0) {
     607                 :          0 :             mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%s"), xcbuf);
     608                 :            :         }
     609                 :            :     }
     610                 :            : 
     611                 :          2 :     mbedtls_raise_error(ret);
     612                 :            : }
     613                 :            : 
     614                 :            : #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
     615                 :            : static mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) {
     616                 :            :     mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
     617                 :            :     if (!mp_obj_is_true(binary_form)) {
     618                 :            :         mp_raise_NotImplementedError(NULL);
     619                 :            :     }
     620                 :            :     const mbedtls_x509_crt *peer_cert = mbedtls_ssl_get_peer_cert(&o->ssl);
     621                 :            :     if (peer_cert == NULL) {
     622                 :            :         return mp_const_none;
     623                 :            :     }
     624                 :            :     return mp_obj_new_bytes(peer_cert->raw.p, peer_cert->raw.len);
     625                 :            : }
     626                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(mod_ssl_getpeercert_obj, mod_ssl_getpeercert);
     627                 :            : #endif
     628                 :            : 
     629                 :          0 : static mp_obj_t mod_ssl_cipher(mp_obj_t o_in) {
     630                 :          0 :     mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
     631                 :          0 :     const char *cipher_suite = mbedtls_ssl_get_ciphersuite(&o->ssl);
     632                 :          0 :     const char *tls_version = mbedtls_ssl_get_version(&o->ssl);
     633                 :          0 :     mp_obj_t tuple[2] = {mp_obj_new_str_from_cstr(cipher_suite),
     634                 :          0 :                          mp_obj_new_str_from_cstr(tls_version)};
     635                 :            : 
     636                 :          0 :     return mp_obj_new_tuple(2, tuple);
     637                 :            : }
     638                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mod_ssl_cipher_obj, mod_ssl_cipher);
     639                 :            : 
     640                 :         24 : static mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
     641                 :         24 :     mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
     642                 :         24 :     o->poll_mask = 0;
     643                 :            : 
     644         [ +  + ]:         24 :     if (o->last_error) {
     645                 :          4 :         *errcode = o->last_error;
     646                 :          4 :         return MP_STREAM_ERROR;
     647                 :            :     }
     648                 :            : 
     649                 :            :     // Store the current SSL context.
     650                 :         20 :     store_active_context(o->ssl_context);
     651                 :            : 
     652                 :         20 :     int ret = mbedtls_ssl_read(&o->ssl, buf, size);
     653         [ +  - ]:         20 :     if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
     654                 :            :         // end of stream
     655                 :            :         return 0;
     656                 :            :     }
     657         [ +  + ]:         20 :     if (ret >= 0) {
     658                 :          6 :         return ret;
     659                 :            :     }
     660         [ +  + ]:         14 :     if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
     661                 :         10 :         ret = MP_EWOULDBLOCK;
     662         [ +  + ]:          4 :     } else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
     663                 :            :         // If handshake is not finished, read attempt may end up in protocol
     664                 :            :         // wanting to write next handshake message. The same may happen with
     665                 :            :         // renegotiation.
     666                 :          2 :         ret = MP_EWOULDBLOCK;
     667                 :          2 :         o->poll_mask = MP_STREAM_POLL_WR;
     668                 :            :     #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
     669                 :            :     } else if (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) {
     670                 :            :         // It appears a new session ticket being issued by the server right after
     671                 :            :         // completed handshake is not uncommon and shouldn't be treated as fatal.
     672                 :            :         // mbedtls itself states "This error code is experimental and may be
     673                 :            :         // changed or removed without notice."
     674                 :            :         ret = MP_EWOULDBLOCK;
     675                 :            :     #endif
     676                 :            :     } else {
     677                 :          2 :         o->last_error = ret;
     678                 :            :     }
     679                 :         14 :     ssl_check_async_handshake_failure(o, &ret);
     680                 :         12 :     *errcode = ret;
     681                 :         12 :     return MP_STREAM_ERROR;
     682                 :            : }
     683                 :            : 
     684                 :         10 : static mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
     685                 :         10 :     mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
     686                 :         10 :     o->poll_mask = 0;
     687                 :            : 
     688         [ +  + ]:         10 :     if (o->last_error) {
     689                 :          2 :         *errcode = o->last_error;
     690                 :          2 :         return MP_STREAM_ERROR;
     691                 :            :     }
     692                 :            : 
     693                 :            :     // Store the current SSL context.
     694                 :          8 :     store_active_context(o->ssl_context);
     695                 :            : 
     696                 :          8 :     int ret = mbedtls_ssl_write(&o->ssl, buf, size);
     697         [ +  + ]:          8 :     if (ret >= 0) {
     698                 :          4 :         return ret;
     699                 :            :     }
     700         [ -  + ]:          4 :     if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
     701                 :          0 :         ret = MP_EWOULDBLOCK;
     702         [ +  + ]:          4 :     } else if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
     703                 :            :         // If handshake is not finished, write attempt may end up in protocol
     704                 :            :         // wanting to read next handshake message. The same may happen with
     705                 :            :         // renegotiation.
     706                 :          2 :         ret = MP_EWOULDBLOCK;
     707                 :          2 :         o->poll_mask = MP_STREAM_POLL_RD;
     708                 :            :     } else {
     709                 :          2 :         o->last_error = ret;
     710                 :            :     }
     711                 :          4 :     ssl_check_async_handshake_failure(o, &ret);
     712                 :          4 :     *errcode = ret;
     713                 :          4 :     return MP_STREAM_ERROR;
     714                 :            : }
     715                 :            : 
     716                 :          4 : static mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
     717                 :          4 :     mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(self_in);
     718                 :          4 :     mp_obj_t sock = o->sock;
     719                 :          4 :     mp_obj_t dest[3];
     720                 :          4 :     mp_load_method(sock, MP_QSTR_setblocking, dest);
     721                 :          4 :     dest[2] = flag_in;
     722                 :          4 :     return mp_call_method_n_kw(1, 0, dest);
     723                 :            : }
     724                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
     725                 :            : 
     726                 :        568 : static mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
     727                 :        568 :     mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
     728                 :        568 :     mp_uint_t ret = 0;
     729                 :        568 :     uintptr_t saved_arg = 0;
     730                 :        568 :     mp_obj_t sock = self->sock;
     731                 :            : 
     732         [ +  + ]:        568 :     if (request == MP_STREAM_CLOSE) {
     733                 :            :         // Clear the SSL context.
     734                 :        540 :         store_active_context(NULL);
     735                 :            : 
     736         [ +  + ]:        540 :         if (sock == MP_OBJ_NULL) {
     737                 :            :             // Already closed socket, do nothing.
     738                 :            :             return 0;
     739                 :            :         }
     740                 :        526 :         self->sock = MP_OBJ_NULL;
     741                 :        526 :         mbedtls_ssl_free(&self->ssl);
     742         [ +  + ]:         28 :     } else if (request == MP_STREAM_POLL) {
     743   [ +  +  +  - ]:         22 :         if (sock == MP_OBJ_NULL || self->last_error != 0) {
     744                 :            :             // Closed or error socket, return NVAL flag.
     745                 :            :             return MP_STREAM_POLL_NVAL;
     746                 :            :         }
     747                 :            : 
     748                 :            :         // If the library signaled us that it needs reading or writing, only check that direction,
     749                 :            :         // but save what the caller asked because we need to restore it later
     750   [ +  +  +  - ]:         16 :         if (self->poll_mask && (arg & MP_STREAM_POLL_RDWR)) {
     751                 :          8 :             saved_arg = arg & MP_STREAM_POLL_RDWR;
     752                 :          8 :             arg = (arg & ~saved_arg) | self->poll_mask;
     753                 :            :         }
     754                 :            : 
     755                 :            :         // Take into account that the library might have buffered data already
     756                 :        556 :         int has_pending = 0;
     757         [ +  + ]:         16 :         if (arg & MP_STREAM_POLL_RD) {
     758                 :         10 :             has_pending = mbedtls_ssl_check_pending(&self->ssl);
     759         [ +  + ]:         10 :             if (has_pending) {
     760                 :          2 :                 ret |= MP_STREAM_POLL_RD;
     761         [ -  + ]:          2 :                 if (arg == MP_STREAM_POLL_RD) {
     762                 :            :                     // Shortcut if we only need to read and we have buffered data, no need to go to the underlying socket
     763                 :            :                     return MP_STREAM_POLL_RD;
     764                 :            :                 }
     765                 :            :             }
     766                 :            :         }
     767                 :            :     } else {
     768                 :            :         // Unsupported ioctl.
     769                 :          6 :         *errcode = MP_EINVAL;
     770                 :          6 :         return MP_STREAM_ERROR;
     771                 :            :     }
     772                 :            : 
     773                 :            :     // Pass all requests down to the underlying socket
     774                 :        540 :     ret |= mp_get_stream(sock)->ioctl(sock, request, arg, errcode);
     775                 :            : 
     776         [ +  + ]:        540 :     if (request == MP_STREAM_POLL) {
     777                 :            :         // The direction the library needed is available, return a fake result to the caller so that
     778                 :            :         // it reenters a read or a write to allow the handshake to progress
     779         [ -  + ]:         14 :         if (ret & self->poll_mask) {
     780                 :          0 :             ret |= saved_arg;
     781                 :            :         }
     782                 :            :     }
     783                 :            :     return ret;
     784                 :            : }
     785                 :            : 
     786                 :            : static const mp_rom_map_elem_t ssl_socket_locals_dict_table[] = {
     787                 :            :     { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
     788                 :            :     { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
     789                 :            :     { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
     790                 :            :     { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
     791                 :            :     { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
     792                 :            :     { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
     793                 :            :     #if MICROPY_PY_SSL_FINALISER
     794                 :            :     { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
     795                 :            :     #endif
     796                 :            :     #if MICROPY_UNIX_COVERAGE
     797                 :            :     { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
     798                 :            :     #endif
     799                 :            :     #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
     800                 :            :     { MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
     801                 :            :     #endif
     802                 :            :     { MP_ROM_QSTR(MP_QSTR_cipher), MP_ROM_PTR(&mod_ssl_cipher_obj) },
     803                 :            : };
     804                 :            : static MP_DEFINE_CONST_DICT(ssl_socket_locals_dict, ssl_socket_locals_dict_table);
     805                 :            : 
     806                 :            : static const mp_stream_p_t ssl_socket_stream_p = {
     807                 :            :     .read = socket_read,
     808                 :            :     .write = socket_write,
     809                 :            :     .ioctl = socket_ioctl,
     810                 :            : };
     811                 :            : 
     812                 :            : static MP_DEFINE_CONST_OBJ_TYPE(
     813                 :            :     ssl_socket_type,
     814                 :            :     MP_QSTR_SSLSocket,
     815                 :            :     MP_TYPE_FLAG_NONE,
     816                 :            :     protocol, &ssl_socket_stream_p,
     817                 :            :     locals_dict, &ssl_socket_locals_dict
     818                 :            :     );
     819                 :            : 
     820                 :            : /******************************************************************************/
     821                 :            : // ssl module.
     822                 :            : 
     823                 :            : #if MICROPY_PY_SSL_ECDSA_SIGN_ALT
     824                 :            : int micropy_mbedtls_ecdsa_sign_alt(const mbedtls_mpi *d, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t sig_size, size_t *slen) {
     825                 :            :     uint8_t key[256];
     826                 :            : 
     827                 :            :     // Check if the current context has an alternative sign function.
     828                 :            :     mp_obj_ssl_context_t *ssl_ctx = MP_STATE_THREAD(tls_ssl_context);
     829                 :            :     if (ssl_ctx == NULL || ssl_ctx->ecdsa_sign_callback == mp_const_none) {
     830                 :            :         return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
     831                 :            :     }
     832                 :            : 
     833                 :            :     size_t klen = mbedtls_mpi_size(d);
     834                 :            :     if (klen > sizeof(key)) {
     835                 :            :         return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
     836                 :            :     }
     837                 :            : 
     838                 :            :     // Convert the MPI private key (d) to a binary array
     839                 :            :     if (mbedtls_mpi_write_binary(d, key, klen) != 0) {
     840                 :            :         return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
     841                 :            :     }
     842                 :            : 
     843                 :            :     nlr_buf_t nlr;
     844                 :            :     mp_buffer_info_t sig_buf;
     845                 :            :     if (nlr_push(&nlr) == 0) {
     846                 :            :         mp_obj_t ret = mp_call_function_2(ssl_ctx->ecdsa_sign_callback,
     847                 :            :             mp_obj_new_bytearray_by_ref(klen, (void *)key),
     848                 :            :             mp_obj_new_bytearray_by_ref(hlen, (void *)hash));
     849                 :            :         if (ret == mp_const_none) {
     850                 :            :             // key couldn't be used by the alternative implementation.
     851                 :            :             nlr_pop();
     852                 :            :             return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
     853                 :            :         }
     854                 :            :         mp_get_buffer_raise(ret, &sig_buf, MP_BUFFER_READ);
     855                 :            :         nlr_pop();
     856                 :            :     } else {
     857                 :            :         // The alternative implementation failed to sign.
     858                 :            :         mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
     859                 :            :         return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
     860                 :            :     }
     861                 :            : 
     862                 :            :     // Check if the buffer fits.
     863                 :            :     if (sig_buf.len > sig_size) {
     864                 :            :         return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
     865                 :            :     }
     866                 :            : 
     867                 :            :     // Copy ASN.1 signature to buffer.
     868                 :            :     *slen = sig_buf.len;
     869                 :            :     memcpy(sig, sig_buf.buf, sig_buf.len);
     870                 :            :     return 0;
     871                 :            : }
     872                 :            : #endif
     873                 :            : 
     874                 :            : static const mp_rom_map_elem_t mp_module_tls_globals_table[] = {
     875                 :            :     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tls) },
     876                 :            : 
     877                 :            :     // Classes.
     878                 :            :     { MP_ROM_QSTR(MP_QSTR_SSLContext), MP_ROM_PTR(&ssl_context_type) },
     879                 :            : 
     880                 :            :     // Constants.
     881                 :            :     { MP_ROM_QSTR(MP_QSTR_MBEDTLS_VERSION), MP_ROM_PTR(&mbedtls_version_obj)},
     882                 :            :     { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_CLIENT), MP_ROM_INT(MBEDTLS_SSL_IS_CLIENT) },
     883                 :            :     { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_SERVER), MP_ROM_INT(MBEDTLS_SSL_IS_SERVER) },
     884                 :            :     { MP_ROM_QSTR(MP_QSTR_CERT_NONE), MP_ROM_INT(MBEDTLS_SSL_VERIFY_NONE) },
     885                 :            :     { MP_ROM_QSTR(MP_QSTR_CERT_OPTIONAL), MP_ROM_INT(MBEDTLS_SSL_VERIFY_OPTIONAL) },
     886                 :            :     { MP_ROM_QSTR(MP_QSTR_CERT_REQUIRED), MP_ROM_INT(MBEDTLS_SSL_VERIFY_REQUIRED) },
     887                 :            : };
     888                 :            : static MP_DEFINE_CONST_DICT(mp_module_tls_globals, mp_module_tls_globals_table);
     889                 :            : 
     890                 :            : const mp_obj_module_t mp_module_tls = {
     891                 :            :     .base = { &mp_type_module },
     892                 :            :     .globals = (mp_obj_dict_t *)&mp_module_tls_globals,
     893                 :            : };
     894                 :            : 
     895                 :            : MP_REGISTER_MODULE(MP_QSTR_tls, mp_module_tls);
     896                 :            : 
     897                 :            : #endif // MICROPY_PY_SSL && MICROPY_SSL_MBEDTLS

Generated by: LCOV version 1.15-5-g462f71d