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