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 "extmod/vfs.h"
41 : :
42 : : // mbedtls_time_t
43 : : #include "mbedtls/platform.h"
44 : : #include "mbedtls/ssl.h"
45 : : #include "mbedtls/x509_crt.h"
46 : : #include "mbedtls/pk.h"
47 : : #include "mbedtls/entropy.h"
48 : : #include "mbedtls/ctr_drbg.h"
49 : : #include "mbedtls/debug.h"
50 : : #include "mbedtls/error.h"
51 : : #if MBEDTLS_VERSION_NUMBER >= 0x03000000
52 : : #include "mbedtls/build_info.h"
53 : : #else
54 : : #include "mbedtls/version.h"
55 : : #endif
56 : :
57 : : #define MP_STREAM_POLL_RDWR (MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)
58 : :
59 : : // This corresponds to an SSLContext object.
60 : : typedef struct _mp_obj_ssl_context_t {
61 : : mp_obj_base_t base;
62 : : mbedtls_entropy_context entropy;
63 : : mbedtls_ctr_drbg_context ctr_drbg;
64 : : mbedtls_ssl_config conf;
65 : : mbedtls_x509_crt cacert;
66 : : mbedtls_x509_crt cert;
67 : : mbedtls_pk_context pkey;
68 : : int authmode;
69 : : int *ciphersuites;
70 : : mp_obj_t handler;
71 : : } mp_obj_ssl_context_t;
72 : :
73 : : // This corresponds to an SSLSocket object.
74 : : typedef struct _mp_obj_ssl_socket_t {
75 : : mp_obj_base_t base;
76 : : mp_obj_ssl_context_t *ssl_context;
77 : : mp_obj_t sock;
78 : : mbedtls_ssl_context ssl;
79 : :
80 : : uintptr_t poll_mask; // Indicates which read or write operations the protocol needs next
81 : : int last_error; // The last error code, if any
82 : : } mp_obj_ssl_socket_t;
83 : :
84 : : static const mp_obj_type_t ssl_context_type;
85 : : static const mp_obj_type_t ssl_socket_type;
86 : :
87 : : static const MP_DEFINE_STR_OBJ(mbedtls_version_obj, MBEDTLS_VERSION_STRING_FULL);
88 : :
89 : : static mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
90 : : bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname);
91 : :
92 : : /******************************************************************************/
93 : : // Helper functions.
94 : :
95 : : #ifdef MBEDTLS_DEBUG_C
96 : : static void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) {
97 : : (void)ctx;
98 : : (void)level;
99 : : mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str);
100 : : }
101 : : #endif
102 : :
103 : 12 : static NORETURN void mbedtls_raise_error(int err) {
104 : : // Handle special cases.
105 [ - + ]: 12 : if (err == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
106 : 0 : mp_raise_OSError(MP_ENOMEM);
107 [ + + ]: 12 : } else if (err == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
108 : 2 : mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
109 [ + + ]: 10 : } else if (err == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
110 : 4 : mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
111 : : }
112 : :
113 : : // _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the
114 : : // underlying socket into negative codes to pass them through mbedtls. Here we turn them
115 : : // positive again so they get interpreted as the OSError they really are. The
116 : : // cut-off of -256 is a bit hacky, sigh.
117 [ - + ]: 6 : if (err < 0 && err > -256) {
118 : 0 : mp_raise_OSError(-err);
119 : : }
120 : :
121 : : #if defined(MBEDTLS_ERROR_C)
122 : : // Including mbedtls_strerror takes about 1.5KB due to the error strings.
123 : : // MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
124 : : // It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
125 : :
126 : : // Try to allocate memory for the message
127 : : #define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit
128 : 6 : mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
129 : 6 : byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX);
130 [ - + ]: 6 : if (o_str == NULL || o_str_buf == NULL) {
131 : 0 : mp_raise_OSError(err);
132 : : }
133 : :
134 : : // print the error message into the allocated buffer
135 : 6 : mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX);
136 : 6 : size_t len = strlen((char *)o_str_buf);
137 : :
138 : : // Put the exception object together
139 : 6 : o_str->base.type = &mp_type_str;
140 : 6 : o_str->data = o_str_buf;
141 : 6 : o_str->len = len;
142 : 6 : o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
143 : : // raise
144 : 6 : mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)};
145 : 6 : nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
146 : : #else
147 : : // mbedtls is compiled without error strings so we simply return the err number
148 : : mp_raise_OSError(err); // err is typically a large negative number
149 : : #endif
150 : : }
151 : :
152 : 18 : static void ssl_check_async_handshake_failure(mp_obj_ssl_socket_t *sslsock, int *errcode) {
153 : 18 : if (
154 : : #if MBEDTLS_VERSION_NUMBER >= 0x03000000
155 [ + + + - : 18 : (*errcode < 0) && (mbedtls_ssl_is_handshake_over(&sslsock->ssl) == 0) && (*errcode != MBEDTLS_ERR_SSL_CONN_EOF)
+ + ]
156 : : #else
157 : : (*errcode < 0) && (*errcode != MBEDTLS_ERR_SSL_CONN_EOF)
158 : : #endif
159 : : ) {
160 : : // Asynchronous handshake is done by mbdetls_ssl_read/write. If the return code is
161 : : // MBEDTLS_ERR_XX (i.e < 0) and the handshake is not done due to a handshake failure,
162 : : // then notify peer with proper error code and raise local error with mbedtls_raise_error.
163 : :
164 [ - + ]: 2 : if (*errcode == MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE) {
165 : : // Check if TLSv1.3 and use proper alert for this case (to be implemented)
166 : : // uint8_t alert = MBEDTLS_SSL_ALERT_MSG_CERT_REQUIRED; tlsv1.3
167 : : // uint8_t alert = MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE; tlsv1.2
168 : 0 : mbedtls_ssl_send_alert_message(&sslsock->ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
169 : : MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
170 : : }
171 : :
172 [ - + ]: 2 : if (*errcode == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
173 : : // The certificate may have been rejected for several reasons.
174 : 0 : char xcbuf[256];
175 : 0 : uint32_t flags = mbedtls_ssl_get_verify_result(&sslsock->ssl);
176 : 0 : int ret = mbedtls_x509_crt_verify_info(xcbuf, sizeof(xcbuf), "\n", flags);
177 : : // The length of the string written (not including the terminated nul byte),
178 : : // or a negative err code.
179 [ # # ]: 0 : if (ret > 0) {
180 : 0 : sslsock->sock = MP_OBJ_NULL;
181 : 0 : mbedtls_ssl_free(&sslsock->ssl);
182 : 0 : mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%s"), xcbuf);
183 : : }
184 : : }
185 : :
186 : 2 : sslsock->sock = MP_OBJ_NULL;
187 : 2 : mbedtls_ssl_free(&sslsock->ssl);
188 : 2 : mbedtls_raise_error(*errcode);
189 : : }
190 : 16 : }
191 : :
192 : 0 : static int ssl_sock_cert_verify(void *ptr, mbedtls_x509_crt *crt, int depth, uint32_t *flags) {
193 : 0 : mp_obj_ssl_context_t *o = ptr;
194 [ # # ]: 0 : if (o->handler == mp_const_none) {
195 : : return 0;
196 : : }
197 : 0 : mp_obj_array_t cert;
198 : 0 : mp_obj_memoryview_init(&cert, 'B', 0, crt->raw.len, crt->raw.p);
199 : 0 : return mp_obj_get_int(mp_call_function_2(o->handler, MP_OBJ_FROM_PTR(&cert), MP_OBJ_NEW_SMALL_INT(depth)));
200 : : }
201 : :
202 : : /******************************************************************************/
203 : : // SSLContext type.
204 : :
205 : 40 : 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) {
206 : 40 : mp_arg_check_num(n_args, n_kw, 1, 1, false);
207 : :
208 : : // This is the "protocol" argument.
209 : 38 : mp_int_t endpoint = mp_obj_get_int(args[0]);
210 : :
211 : : // Create SSLContext object.
212 : : #if MICROPY_PY_SSL_FINALISER
213 : 38 : mp_obj_ssl_context_t *self = mp_obj_malloc_with_finaliser(mp_obj_ssl_context_t, type_in);
214 : : #else
215 : : mp_obj_ssl_context_t *self = mp_obj_malloc(mp_obj_ssl_context_t, type_in);
216 : : #endif
217 : :
218 : : // Initialise mbedTLS state.
219 : 38 : mbedtls_ssl_config_init(&self->conf);
220 : 38 : mbedtls_entropy_init(&self->entropy);
221 : 38 : mbedtls_ctr_drbg_init(&self->ctr_drbg);
222 : 38 : mbedtls_x509_crt_init(&self->cacert);
223 : 38 : mbedtls_x509_crt_init(&self->cert);
224 : 38 : mbedtls_pk_init(&self->pkey);
225 : 38 : self->ciphersuites = NULL;
226 : 38 : self->handler = mp_const_none;
227 : :
228 : : #ifdef MBEDTLS_DEBUG_C
229 : : // Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
230 : : mbedtls_debug_set_threshold(3);
231 : : #endif
232 : :
233 : : // Whenever the PSA interface is used (if MBEDTLS_PSA_CRYPTO), psa_crypto_init() needs to be called before any TLS related operations.
234 : : // TLSv1.3 depends on the PSA interface, TLSv1.2 only uses the PSA stack if MBEDTLS_USE_PSA_CRYPTO is defined.
235 : : #if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_USE_PSA_CRYPTO)
236 : : psa_crypto_init();
237 : : #endif
238 : :
239 : 38 : const byte seed[] = "upy";
240 : 38 : int ret = mbedtls_ctr_drbg_seed(&self->ctr_drbg, mbedtls_entropy_func, &self->entropy, seed, sizeof(seed));
241 [ - + ]: 38 : if (ret != 0) {
242 : 0 : mbedtls_raise_error(ret);
243 : : }
244 : :
245 : 38 : ret = mbedtls_ssl_config_defaults(&self->conf, endpoint,
246 : : MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
247 [ - + ]: 38 : if (ret != 0) {
248 : 0 : mbedtls_raise_error(ret);
249 : : }
250 : :
251 [ + + ]: 38 : if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
252 : 26 : self->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
253 : : } else {
254 : 12 : self->authmode = MBEDTLS_SSL_VERIFY_NONE;
255 : : }
256 : 38 : mbedtls_ssl_conf_authmode(&self->conf, self->authmode);
257 : 38 : mbedtls_ssl_conf_verify(&self->conf, &ssl_sock_cert_verify, self);
258 : 38 : mbedtls_ssl_conf_rng(&self->conf, mbedtls_ctr_drbg_random, &self->ctr_drbg);
259 : : #ifdef MBEDTLS_DEBUG_C
260 : : mbedtls_ssl_conf_dbg(&self->conf, mbedtls_debug, NULL);
261 : : #endif
262 : :
263 : 38 : return MP_OBJ_FROM_PTR(self);
264 : : }
265 : :
266 : 144 : static void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
267 : 144 : mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
268 [ + + ]: 144 : if (dest[0] == MP_OBJ_NULL) {
269 : : // Load attribute.
270 [ + + ]: 86 : if (attr == MP_QSTR_verify_mode) {
271 : 10 : dest[0] = MP_OBJ_NEW_SMALL_INT(self->authmode);
272 [ - + ]: 76 : } else if (attr == MP_QSTR_verify_callback) {
273 : 0 : dest[0] = self->handler;
274 : : } else {
275 : : // Continue lookup in locals_dict.
276 : 76 : dest[1] = MP_OBJ_SENTINEL;
277 : : }
278 [ + + ]: 58 : } else if (dest[1] != MP_OBJ_NULL) {
279 : : // Store attribute.
280 [ + + ]: 56 : if (attr == MP_QSTR_verify_mode) {
281 : 54 : self->authmode = mp_obj_get_int(dest[1]);
282 : 54 : dest[0] = MP_OBJ_NULL;
283 : 54 : mbedtls_ssl_conf_authmode(&self->conf, self->authmode);
284 [ - + ]: 2 : } else if (attr == MP_QSTR_verify_callback) {
285 : 0 : dest[0] = MP_OBJ_NULL;
286 : 0 : self->handler = dest[1];
287 : : }
288 : : }
289 : 144 : }
290 : :
291 : : #if MICROPY_PY_SSL_FINALISER
292 : 38 : static mp_obj_t ssl_context___del__(mp_obj_t self_in) {
293 : 38 : mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
294 : 38 : mbedtls_pk_free(&self->pkey);
295 : 38 : mbedtls_x509_crt_free(&self->cert);
296 : 38 : mbedtls_x509_crt_free(&self->cacert);
297 : 38 : mbedtls_ctr_drbg_free(&self->ctr_drbg);
298 : 38 : mbedtls_entropy_free(&self->entropy);
299 : 38 : mbedtls_ssl_config_free(&self->conf);
300 : 38 : return mp_const_none;
301 : : }
302 : : static MP_DEFINE_CONST_FUN_OBJ_1(ssl_context___del___obj, ssl_context___del__);
303 : : #endif
304 : :
305 : : // SSLContext.get_ciphers()
306 : 2 : static mp_obj_t ssl_context_get_ciphers(mp_obj_t self_in) {
307 : 2 : mp_obj_t list = mp_obj_new_list(0, NULL);
308 [ + + ]: 18 : for (const int *cipher_list = mbedtls_ssl_list_ciphersuites(); *cipher_list; ++cipher_list) {
309 : 16 : const char *cipher_name = mbedtls_ssl_get_ciphersuite_name(*cipher_list);
310 : 16 : mp_obj_list_append(list, MP_OBJ_FROM_PTR(mp_obj_new_str(cipher_name, strlen(cipher_name))));
311 : : }
312 : 2 : return list;
313 : : }
314 : : static MP_DEFINE_CONST_FUN_OBJ_1(ssl_context_get_ciphers_obj, ssl_context_get_ciphers);
315 : :
316 : : // SSLContext.set_ciphers(ciphersuite)
317 : 6 : static mp_obj_t ssl_context_set_ciphers(mp_obj_t self_in, mp_obj_t ciphersuite) {
318 : 6 : mp_obj_ssl_context_t *ssl_context = MP_OBJ_TO_PTR(self_in);
319 : :
320 : : // Check that ciphersuite is a list or tuple.
321 : 6 : size_t len = 0;
322 : 6 : mp_obj_t *ciphers;
323 : 6 : mp_obj_get_array(ciphersuite, &len, &ciphers);
324 [ - + ]: 4 : if (len == 0) {
325 : 0 : mbedtls_raise_error(MBEDTLS_ERR_SSL_BAD_CONFIG);
326 : : }
327 : :
328 : : // Parse list of ciphers.
329 : 4 : ssl_context->ciphersuites = m_new(int, len + 1);
330 [ + + ]: 6 : for (size_t i = 0; i < len; ++i) {
331 : 4 : const char *ciphername = mp_obj_str_get_str(ciphers[i]);
332 : 4 : const int id = mbedtls_ssl_get_ciphersuite_id(ciphername);
333 [ + + ]: 4 : if (id == 0) {
334 : 2 : mbedtls_raise_error(MBEDTLS_ERR_SSL_BAD_CONFIG);
335 : : }
336 : 2 : ssl_context->ciphersuites[i] = id;
337 : : }
338 : 2 : ssl_context->ciphersuites[len] = 0;
339 : :
340 : : // Configure ciphersuite.
341 : 2 : mbedtls_ssl_conf_ciphersuites(&ssl_context->conf, (const int *)ssl_context->ciphersuites);
342 : :
343 : 2 : return mp_const_none;
344 : : }
345 : : static MP_DEFINE_CONST_FUN_OBJ_2(ssl_context_set_ciphers_obj, ssl_context_set_ciphers);
346 : :
347 : 8 : static void ssl_context_load_key(mp_obj_ssl_context_t *self, mp_obj_t key_obj, mp_obj_t cert_obj) {
348 : 8 : size_t key_len;
349 : 8 : const byte *key = (const byte *)mp_obj_str_get_data(key_obj, &key_len);
350 : : // len should include terminating null
351 : 8 : int ret;
352 : : #if MBEDTLS_VERSION_NUMBER >= 0x03000000
353 : 8 : ret = mbedtls_pk_parse_key(&self->pkey, key, key_len + 1, NULL, 0, mbedtls_ctr_drbg_random, &self->ctr_drbg);
354 : : #else
355 : : ret = mbedtls_pk_parse_key(&self->pkey, key, key_len + 1, NULL, 0);
356 : : #endif
357 [ + + ]: 8 : if (ret != 0) {
358 : 2 : mbedtls_raise_error(MBEDTLS_ERR_PK_BAD_INPUT_DATA); // use general error for all key errors
359 : : }
360 : :
361 : 6 : size_t cert_len;
362 : 6 : const byte *cert = (const byte *)mp_obj_str_get_data(cert_obj, &cert_len);
363 : : // len should include terminating null
364 : 4 : ret = mbedtls_x509_crt_parse(&self->cert, cert, cert_len + 1);
365 [ + + ]: 4 : if (ret != 0) {
366 : 2 : mbedtls_raise_error(MBEDTLS_ERR_X509_BAD_INPUT_DATA); // use general error for all cert errors
367 : : }
368 : :
369 : 2 : ret = mbedtls_ssl_conf_own_cert(&self->conf, &self->cert, &self->pkey);
370 [ - + ]: 2 : if (ret != 0) {
371 : 0 : mbedtls_raise_error(ret);
372 : : }
373 : 2 : }
374 : :
375 : : // SSLContext.load_cert_chain(certfile, keyfile)
376 : 8 : static mp_obj_t ssl_context_load_cert_chain(mp_obj_t self_in, mp_obj_t cert, mp_obj_t pkey) {
377 : 8 : mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
378 : 8 : ssl_context_load_key(self, pkey, cert);
379 : 2 : return mp_const_none;
380 : : }
381 : : static MP_DEFINE_CONST_FUN_OBJ_3(ssl_context_load_cert_chain_obj, ssl_context_load_cert_chain);
382 : :
383 : 2 : static void ssl_context_load_cadata(mp_obj_ssl_context_t *self, mp_obj_t cadata_obj) {
384 : 2 : size_t cacert_len;
385 : 2 : const byte *cacert = (const byte *)mp_obj_str_get_data(cadata_obj, &cacert_len);
386 : : // len should include terminating null
387 : 2 : int ret = mbedtls_x509_crt_parse(&self->cacert, cacert, cacert_len + 1);
388 [ + - ]: 2 : if (ret != 0) {
389 : 2 : mbedtls_raise_error(MBEDTLS_ERR_X509_BAD_INPUT_DATA); // use general error for all cert errors
390 : : }
391 : :
392 : 0 : mbedtls_ssl_conf_ca_chain(&self->conf, &self->cacert, NULL);
393 : 0 : }
394 : :
395 : : // SSLContext.load_verify_locations(cadata)
396 : 2 : static mp_obj_t ssl_context_load_verify_locations(mp_obj_t self_in, mp_obj_t cadata) {
397 : :
398 : 2 : mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(self_in);
399 : 2 : ssl_context_load_cadata(self, cadata);
400 : 0 : return mp_const_none;
401 : : }
402 : : static MP_DEFINE_CONST_FUN_OBJ_2(ssl_context_load_verify_locations_obj, ssl_context_load_verify_locations);
403 : :
404 : 18 : static mp_obj_t ssl_context_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
405 : 18 : enum { ARG_server_side, ARG_do_handshake_on_connect, ARG_server_hostname };
406 : 18 : static const mp_arg_t allowed_args[] = {
407 : : { MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
408 : : { MP_QSTR_do_handshake_on_connect, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
409 : : { MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
410 : : };
411 : :
412 : : // Parse arguments.
413 : 18 : mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(pos_args[0]);
414 : 18 : mp_obj_t sock = pos_args[1];
415 : 18 : mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
416 : 18 : mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
417 : :
418 : : // Create and return the new SSLSocket object.
419 : 34 : return ssl_socket_make_new(self, sock, args[ARG_server_side].u_bool,
420 : 18 : args[ARG_do_handshake_on_connect].u_bool, args[ARG_server_hostname].u_obj);
421 : : }
422 : : static MP_DEFINE_CONST_FUN_OBJ_KW(ssl_context_wrap_socket_obj, 2, ssl_context_wrap_socket);
423 : :
424 : : static const mp_rom_map_elem_t ssl_context_locals_dict_table[] = {
425 : : #if MICROPY_PY_SSL_FINALISER
426 : : { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ssl_context___del___obj) },
427 : : #endif
428 : : { MP_ROM_QSTR(MP_QSTR_get_ciphers), MP_ROM_PTR(&ssl_context_get_ciphers_obj)},
429 : : { MP_ROM_QSTR(MP_QSTR_set_ciphers), MP_ROM_PTR(&ssl_context_set_ciphers_obj)},
430 : : { MP_ROM_QSTR(MP_QSTR_load_cert_chain), MP_ROM_PTR(&ssl_context_load_cert_chain_obj)},
431 : : { MP_ROM_QSTR(MP_QSTR_load_verify_locations), MP_ROM_PTR(&ssl_context_load_verify_locations_obj)},
432 : : { MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_context_wrap_socket_obj) },
433 : : };
434 : : static MP_DEFINE_CONST_DICT(ssl_context_locals_dict, ssl_context_locals_dict_table);
435 : :
436 : : static MP_DEFINE_CONST_OBJ_TYPE(
437 : : ssl_context_type,
438 : : MP_QSTR_SSLContext,
439 : : MP_TYPE_FLAG_NONE,
440 : : make_new, ssl_context_make_new,
441 : : attr, ssl_context_attr,
442 : : locals_dict, &ssl_context_locals_dict
443 : : );
444 : :
445 : : /******************************************************************************/
446 : : // SSLSocket type.
447 : :
448 : 28 : static int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
449 : 28 : mp_obj_t sock = *(mp_obj_t *)ctx;
450 : :
451 : 28 : const mp_stream_p_t *sock_stream = mp_get_stream(sock);
452 : 28 : int err;
453 : :
454 : 28 : mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
455 [ + + ]: 28 : if (out_sz == MP_STREAM_ERROR) {
456 [ - + ]: 2 : if (mp_is_nonblocking_error(err)) {
457 : : return MBEDTLS_ERR_SSL_WANT_WRITE;
458 : : }
459 : 0 : return -err; // convert an MP_ERRNO to something mbedtls passes through as error
460 : : } else {
461 : 26 : return out_sz;
462 : : }
463 : : }
464 : :
465 : : // _mbedtls_ssl_recv is called by mbedtls to receive bytes from the underlying socket
466 : 62 : static int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
467 : 62 : mp_obj_t sock = *(mp_obj_t *)ctx;
468 : :
469 : 62 : const mp_stream_p_t *sock_stream = mp_get_stream(sock);
470 : 62 : int err;
471 : :
472 : 62 : mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err);
473 [ + + ]: 62 : if (out_sz == MP_STREAM_ERROR) {
474 [ - + ]: 12 : if (mp_is_nonblocking_error(err)) {
475 : : return MBEDTLS_ERR_SSL_WANT_READ;
476 : : }
477 : 0 : return -err;
478 : : } else {
479 : 50 : return out_sz;
480 : : }
481 : : }
482 : :
483 : 18 : static mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
484 : : bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname) {
485 : :
486 : : // Verify the socket object has the full stream protocol
487 : 18 : mp_get_stream_raise(sock, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
488 : :
489 : : #if MICROPY_PY_SSL_FINALISER
490 : 18 : mp_obj_ssl_socket_t *o = mp_obj_malloc_with_finaliser(mp_obj_ssl_socket_t, &ssl_socket_type);
491 : : #else
492 : : mp_obj_ssl_socket_t *o = mp_obj_malloc(mp_obj_ssl_socket_t, &ssl_socket_type);
493 : : #endif
494 : 18 : o->ssl_context = ssl_context;
495 : 18 : o->sock = sock;
496 : 18 : o->poll_mask = 0;
497 : 18 : o->last_error = 0;
498 : :
499 : 18 : int ret;
500 : 18 : uint32_t flags = 0;
501 : :
502 : 18 : mbedtls_ssl_init(&o->ssl);
503 : :
504 : 18 : ret = mbedtls_ssl_setup(&o->ssl, &ssl_context->conf);
505 [ - + ]: 18 : if (ret != 0) {
506 : 0 : goto cleanup;
507 : : }
508 : :
509 [ + + ]: 18 : if (server_hostname != mp_const_none) {
510 : 2 : const char *sni = mp_obj_str_get_str(server_hostname);
511 : 2 : ret = mbedtls_ssl_set_hostname(&o->ssl, sni);
512 [ - + ]: 2 : if (ret != 0) {
513 : 0 : goto cleanup;
514 : : }
515 [ - + - - ]: 16 : } else if (ssl_context->authmode == MBEDTLS_SSL_VERIFY_REQUIRED && server_side == false) {
516 : :
517 : 0 : o->sock = MP_OBJ_NULL;
518 : 0 : mbedtls_ssl_free(&o->ssl);
519 : 0 : mp_raise_ValueError(MP_ERROR_TEXT("CERT_REQUIRED requires server_hostname"));
520 : : }
521 : :
522 : 18 : mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
523 : :
524 [ + + ]: 18 : if (do_handshake_on_connect) {
525 [ + - ]: 2 : while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) {
526 [ + - ]: 2 : if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
527 : 2 : goto cleanup;
528 : : }
529 : 0 : mp_event_wait_ms(1);
530 : : }
531 : : }
532 : :
533 : 16 : return MP_OBJ_FROM_PTR(o);
534 : :
535 : 2 : cleanup:
536 [ - + ]: 2 : if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
537 : 0 : flags = mbedtls_ssl_get_verify_result(&o->ssl);
538 : : }
539 : :
540 : 2 : o->sock = MP_OBJ_NULL;
541 : 2 : mbedtls_ssl_free(&o->ssl);
542 : :
543 [ - + ]: 2 : if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
544 : 0 : char xcbuf[256];
545 : 0 : int ret_info = mbedtls_x509_crt_verify_info(xcbuf, sizeof(xcbuf), "\n", flags);
546 : : // The length of the string written (not including the terminated nul byte),
547 : : // or a negative err code.
548 [ # # ]: 0 : if (ret_info > 0) {
549 : 0 : mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%s"), xcbuf);
550 : : }
551 : : }
552 : :
553 : 2 : mbedtls_raise_error(ret);
554 : : }
555 : :
556 : : #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
557 : : static mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) {
558 : : mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
559 : : if (!mp_obj_is_true(binary_form)) {
560 : : mp_raise_NotImplementedError(NULL);
561 : : }
562 : : const mbedtls_x509_crt *peer_cert = mbedtls_ssl_get_peer_cert(&o->ssl);
563 : : if (peer_cert == NULL) {
564 : : return mp_const_none;
565 : : }
566 : : return mp_obj_new_bytes(peer_cert->raw.p, peer_cert->raw.len);
567 : : }
568 : : static MP_DEFINE_CONST_FUN_OBJ_2(mod_ssl_getpeercert_obj, mod_ssl_getpeercert);
569 : : #endif
570 : :
571 : 0 : static mp_obj_t mod_ssl_cipher(mp_obj_t o_in) {
572 : 0 : mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
573 : 0 : const char *cipher_suite = mbedtls_ssl_get_ciphersuite(&o->ssl);
574 : 0 : const char *tls_version = mbedtls_ssl_get_version(&o->ssl);
575 : 0 : mp_obj_t tuple[2] = {mp_obj_new_str(cipher_suite, strlen(cipher_suite)),
576 : 0 : mp_obj_new_str(tls_version, strlen(tls_version))};
577 : :
578 : 0 : return mp_obj_new_tuple(2, tuple);
579 : : }
580 : : static MP_DEFINE_CONST_FUN_OBJ_1(mod_ssl_cipher_obj, mod_ssl_cipher);
581 : :
582 : 24 : static mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
583 : 24 : mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
584 : 24 : o->poll_mask = 0;
585 : :
586 [ + + ]: 24 : if (o->last_error) {
587 : 4 : *errcode = o->last_error;
588 : 4 : return MP_STREAM_ERROR;
589 : : }
590 : :
591 : 20 : int ret = mbedtls_ssl_read(&o->ssl, buf, size);
592 [ + - ]: 20 : if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
593 : : // end of stream
594 : : return 0;
595 : : }
596 [ + + ]: 20 : if (ret >= 0) {
597 : 6 : return ret;
598 : : }
599 [ + + ]: 14 : if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
600 : 10 : ret = MP_EWOULDBLOCK;
601 [ + + ]: 4 : } else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
602 : : // If handshake is not finished, read attempt may end up in protocol
603 : : // wanting to write next handshake message. The same may happen with
604 : : // renegotiation.
605 : 2 : ret = MP_EWOULDBLOCK;
606 : 2 : o->poll_mask = MP_STREAM_POLL_WR;
607 : : #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
608 : : } else if (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) {
609 : : // It appears a new session ticket being issued by the server right after
610 : : // completed handshake is not uncommon and shouldn't be treated as fatal.
611 : : // mbedtls itself states "This error code is experimental and may be
612 : : // changed or removed without notice."
613 : : ret = MP_EWOULDBLOCK;
614 : : #endif
615 : : } else {
616 : 2 : o->last_error = ret;
617 : : }
618 : 14 : ssl_check_async_handshake_failure(o, &ret);
619 : 12 : *errcode = ret;
620 : 12 : return MP_STREAM_ERROR;
621 : : }
622 : :
623 : 10 : static mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
624 : 10 : mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
625 : 10 : o->poll_mask = 0;
626 : :
627 [ + + ]: 10 : if (o->last_error) {
628 : 2 : *errcode = o->last_error;
629 : 2 : return MP_STREAM_ERROR;
630 : : }
631 : :
632 : 8 : int ret = mbedtls_ssl_write(&o->ssl, buf, size);
633 [ + + ]: 8 : if (ret >= 0) {
634 : 4 : return ret;
635 : : }
636 [ - + ]: 4 : if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
637 : 0 : ret = MP_EWOULDBLOCK;
638 [ + + ]: 4 : } else if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
639 : : // If handshake is not finished, write attempt may end up in protocol
640 : : // wanting to read next handshake message. The same may happen with
641 : : // renegotiation.
642 : 2 : ret = MP_EWOULDBLOCK;
643 : 2 : o->poll_mask = MP_STREAM_POLL_RD;
644 : : } else {
645 : 2 : o->last_error = ret;
646 : : }
647 : 4 : ssl_check_async_handshake_failure(o, &ret);
648 : 4 : *errcode = ret;
649 : 4 : return MP_STREAM_ERROR;
650 : : }
651 : :
652 : 4 : static mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
653 : 4 : mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(self_in);
654 : 4 : mp_obj_t sock = o->sock;
655 : 4 : mp_obj_t dest[3];
656 : 4 : mp_load_method(sock, MP_QSTR_setblocking, dest);
657 : 4 : dest[2] = flag_in;
658 : 4 : return mp_call_method_n_kw(1, 0, dest);
659 : : }
660 : : static MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
661 : :
662 : 56 : static mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
663 : 56 : mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
664 : 56 : mp_uint_t ret = 0;
665 : 56 : uintptr_t saved_arg = 0;
666 : 56 : mp_obj_t sock = self->sock;
667 : :
668 [ + + ]: 56 : if (request == MP_STREAM_CLOSE) {
669 [ + + ]: 28 : if (sock == MP_OBJ_NULL) {
670 : : // Already closed socket, do nothing.
671 : : return 0;
672 : : }
673 : 14 : self->sock = MP_OBJ_NULL;
674 : 14 : mbedtls_ssl_free(&self->ssl);
675 [ + + ]: 28 : } else if (request == MP_STREAM_POLL) {
676 [ + + + - ]: 22 : if (sock == MP_OBJ_NULL || self->last_error != 0) {
677 : : // Closed or error socket, return NVAL flag.
678 : : return MP_STREAM_POLL_NVAL;
679 : : }
680 : :
681 : : // If the library signaled us that it needs reading or writing, only check that direction,
682 : : // but save what the caller asked because we need to restore it later
683 [ + + + - ]: 16 : if (self->poll_mask && (arg & MP_STREAM_POLL_RDWR)) {
684 : 8 : saved_arg = arg & MP_STREAM_POLL_RDWR;
685 : 8 : arg = (arg & ~saved_arg) | self->poll_mask;
686 : : }
687 : :
688 : : // Take into account that the library might have buffered data already
689 : 16 : int has_pending = 0;
690 [ + + ]: 16 : if (arg & MP_STREAM_POLL_RD) {
691 : 10 : has_pending = mbedtls_ssl_check_pending(&self->ssl);
692 [ + + ]: 10 : if (has_pending) {
693 : 2 : ret |= MP_STREAM_POLL_RD;
694 [ - + ]: 2 : if (arg == MP_STREAM_POLL_RD) {
695 : : // Shortcut if we only need to read and we have buffered data, no need to go to the underlying socket
696 : : return MP_STREAM_POLL_RD;
697 : : }
698 : : }
699 : : }
700 : : } else {
701 : : // Unsupported ioctl.
702 : 6 : *errcode = MP_EINVAL;
703 : 6 : return MP_STREAM_ERROR;
704 : : }
705 : :
706 : : // Pass all requests down to the underlying socket
707 : 28 : ret |= mp_get_stream(sock)->ioctl(sock, request, arg, errcode);
708 : :
709 [ + + ]: 28 : if (request == MP_STREAM_POLL) {
710 : : // The direction the library needed is available, return a fake result to the caller so that
711 : : // it reenters a read or a write to allow the handshake to progress
712 [ - + ]: 14 : if (ret & self->poll_mask) {
713 : 0 : ret |= saved_arg;
714 : : }
715 : : }
716 : : return ret;
717 : : }
718 : :
719 : : static const mp_rom_map_elem_t ssl_socket_locals_dict_table[] = {
720 : : { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
721 : : { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
722 : : { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
723 : : { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
724 : : { MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
725 : : { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
726 : : #if MICROPY_PY_SSL_FINALISER
727 : : { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
728 : : #endif
729 : : #if MICROPY_UNIX_COVERAGE
730 : : { MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
731 : : #endif
732 : : #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
733 : : { MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
734 : : #endif
735 : : { MP_ROM_QSTR(MP_QSTR_cipher), MP_ROM_PTR(&mod_ssl_cipher_obj) },
736 : : };
737 : : static MP_DEFINE_CONST_DICT(ssl_socket_locals_dict, ssl_socket_locals_dict_table);
738 : :
739 : : static const mp_stream_p_t ssl_socket_stream_p = {
740 : : .read = socket_read,
741 : : .write = socket_write,
742 : : .ioctl = socket_ioctl,
743 : : };
744 : :
745 : : static MP_DEFINE_CONST_OBJ_TYPE(
746 : : ssl_socket_type,
747 : : MP_QSTR_SSLSocket,
748 : : MP_TYPE_FLAG_NONE,
749 : : protocol, &ssl_socket_stream_p,
750 : : locals_dict, &ssl_socket_locals_dict
751 : : );
752 : :
753 : : /******************************************************************************/
754 : : // ssl module.
755 : :
756 : : static const mp_rom_map_elem_t mp_module_tls_globals_table[] = {
757 : : { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tls) },
758 : :
759 : : // Classes.
760 : : { MP_ROM_QSTR(MP_QSTR_SSLContext), MP_ROM_PTR(&ssl_context_type) },
761 : :
762 : : // Constants.
763 : : { MP_ROM_QSTR(MP_QSTR_MBEDTLS_VERSION), MP_ROM_PTR(&mbedtls_version_obj)},
764 : : { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_CLIENT), MP_ROM_INT(MBEDTLS_SSL_IS_CLIENT) },
765 : : { MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_SERVER), MP_ROM_INT(MBEDTLS_SSL_IS_SERVER) },
766 : : { MP_ROM_QSTR(MP_QSTR_CERT_NONE), MP_ROM_INT(MBEDTLS_SSL_VERIFY_NONE) },
767 : : { MP_ROM_QSTR(MP_QSTR_CERT_OPTIONAL), MP_ROM_INT(MBEDTLS_SSL_VERIFY_OPTIONAL) },
768 : : { MP_ROM_QSTR(MP_QSTR_CERT_REQUIRED), MP_ROM_INT(MBEDTLS_SSL_VERIFY_REQUIRED) },
769 : : };
770 : : static MP_DEFINE_CONST_DICT(mp_module_tls_globals, mp_module_tls_globals_table);
771 : :
772 : : const mp_obj_module_t mp_module_tls = {
773 : : .base = { &mp_type_module },
774 : : .globals = (mp_obj_dict_t *)&mp_module_tls_globals,
775 : : };
776 : :
777 : : MP_REGISTER_MODULE(MP_QSTR_tls, mp_module_tls);
778 : :
779 : : #endif // MICROPY_PY_SSL && MICROPY_SSL_MBEDTLS
|