2016-09-21 14:25:33 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Linaro Ltd.
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
* Copyright (c) 2019 Paul Sokolovsky
|
2016-09-21 14:25:33 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#if MICROPY_PY_USSL && MICROPY_SSL_MBEDTLS
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-07-21 13:30:35 -04:00
|
|
|
#include <errno.h> // needed because mp_is_nonblocking_error uses system error codes
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
2020-03-27 02:17:35 -04:00
|
|
|
#include "py/objstr.h"
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
// mbedtls_time_t
|
2016-09-21 18:30:48 -04:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#include "mbedtls/ssl.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/debug.h"
|
2020-03-27 02:17:35 -04:00
|
|
|
#include "mbedtls/error.h"
|
2016-09-21 14:25:33 -04:00
|
|
|
|
2022-12-15 13:15:18 -05:00
|
|
|
#define MP_STREAM_POLL_RDWR (MP_STREAM_POLL_RD | MP_STREAM_POLL_WR)
|
|
|
|
|
2016-09-21 14:25:33 -04:00
|
|
|
typedef struct _mp_obj_ssl_socket_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t sock;
|
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
mbedtls_ssl_config conf;
|
|
|
|
mbedtls_x509_crt cacert;
|
2016-09-21 17:17:44 -04:00
|
|
|
mbedtls_x509_crt cert;
|
|
|
|
mbedtls_pk_context pkey;
|
2022-12-15 13:15:18 -05:00
|
|
|
|
|
|
|
uintptr_t poll_mask; // Indicates which read or write operations the protocol needs next
|
|
|
|
int last_error; // The last error code, if any
|
2016-09-21 14:25:33 -04:00
|
|
|
} mp_obj_ssl_socket_t;
|
|
|
|
|
2016-09-21 17:17:44 -04:00
|
|
|
struct ssl_args {
|
|
|
|
mp_arg_val_t key;
|
|
|
|
mp_arg_val_t cert;
|
|
|
|
mp_arg_val_t server_side;
|
2016-09-22 18:44:23 -04:00
|
|
|
mp_arg_val_t server_hostname;
|
2022-01-27 23:42:46 -05:00
|
|
|
mp_arg_val_t cert_reqs;
|
|
|
|
mp_arg_val_t cadata;
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
mp_arg_val_t do_handshake;
|
2016-09-21 17:17:44 -04:00
|
|
|
};
|
|
|
|
|
2016-09-21 14:25:33 -04:00
|
|
|
STATIC const mp_obj_type_t ussl_socket_type;
|
|
|
|
|
2017-10-30 00:41:37 -04:00
|
|
|
#ifdef MBEDTLS_DEBUG_C
|
|
|
|
STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) {
|
|
|
|
(void)ctx;
|
|
|
|
(void)level;
|
2022-07-21 16:00:24 -04:00
|
|
|
mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str);
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
2017-10-30 00:41:37 -04:00
|
|
|
#endif
|
2016-09-21 14:25:33 -04:00
|
|
|
|
2020-03-27 02:17:35 -04:00
|
|
|
STATIC NORETURN void mbedtls_raise_error(int err) {
|
2020-07-02 15:34:36 -04:00
|
|
|
// _mbedtls_ssl_send and _mbedtls_ssl_recv (below) turn positive error codes from the
|
|
|
|
// underlying socket into negative codes to pass them through mbedtls. Here we turn them
|
|
|
|
// positive again so they get interpreted as the OSError they really are. The
|
|
|
|
// cut-off of -256 is a bit hacky, sigh.
|
2020-03-27 02:17:35 -04:00
|
|
|
if (err < 0 && err > -256) {
|
|
|
|
mp_raise_OSError(-err);
|
|
|
|
}
|
|
|
|
|
2020-07-02 15:34:36 -04:00
|
|
|
#if defined(MBEDTLS_ERROR_C)
|
|
|
|
// Including mbedtls_strerror takes about 1.5KB due to the error strings.
|
|
|
|
// MBEDTLS_ERROR_C is the define used by mbedtls to conditionally include mbedtls_strerror.
|
|
|
|
// It is set/unset in the MBEDTLS_CONFIG_FILE which is defined in the Makefile.
|
|
|
|
|
2020-03-27 02:17:35 -04:00
|
|
|
// Try to allocate memory for the message
|
2020-07-02 15:34:36 -04:00
|
|
|
#define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit
|
2020-03-27 02:17:35 -04:00
|
|
|
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
|
|
|
|
byte *o_str_buf = m_new_maybe(byte, ERR_STR_MAX);
|
|
|
|
if (o_str == NULL || o_str_buf == NULL) {
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// print the error message into the allocated buffer
|
|
|
|
mbedtls_strerror(err, (char *)o_str_buf, ERR_STR_MAX);
|
2020-07-02 15:34:36 -04:00
|
|
|
size_t len = strlen((char *)o_str_buf);
|
2020-03-27 02:17:35 -04:00
|
|
|
|
|
|
|
// Put the exception object together
|
|
|
|
o_str->base.type = &mp_type_str;
|
|
|
|
o_str->data = o_str_buf;
|
|
|
|
o_str->len = len;
|
|
|
|
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
|
|
|
|
// raise
|
|
|
|
mp_obj_t args[2] = { MP_OBJ_NEW_SMALL_INT(err), MP_OBJ_FROM_PTR(o_str)};
|
|
|
|
nlr_raise(mp_obj_exception_make_new(&mp_type_OSError, 2, 0, args));
|
|
|
|
#else
|
|
|
|
// mbedtls is compiled without error strings so we simply return the err number
|
2020-07-02 15:34:36 -04:00
|
|
|
mp_raise_OSError(err); // err is typically a large negative number
|
2020-03-27 02:17:35 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-30 00:41:37 -04:00
|
|
|
STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
|
2016-09-21 14:25:33 -04:00
|
|
|
mp_obj_t sock = *(mp_obj_t *)ctx;
|
|
|
|
|
2018-06-12 22:37:49 -04:00
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
2016-09-21 14:25:33 -04:00
|
|
|
int err;
|
|
|
|
|
2017-10-30 00:41:37 -04:00
|
|
|
mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
|
2016-09-21 14:25:33 -04:00
|
|
|
if (out_sz == MP_STREAM_ERROR) {
|
2017-07-21 13:30:35 -04:00
|
|
|
if (mp_is_nonblocking_error(err)) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
2020-03-27 02:17:35 -04:00
|
|
|
return -err; // convert an MP_ERRNO to something mbedtls passes through as error
|
2016-09-21 14:25:33 -04:00
|
|
|
} else {
|
|
|
|
return out_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:01:16 -04:00
|
|
|
// _mbedtls_ssl_recv is called by mbedtls to receive bytes from the underlying socket
|
2017-10-30 00:41:37 -04:00
|
|
|
STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
|
2016-09-21 14:25:33 -04:00
|
|
|
mp_obj_t sock = *(mp_obj_t *)ctx;
|
|
|
|
|
2018-06-12 22:37:49 -04:00
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
2016-09-21 14:25:33 -04:00
|
|
|
int err;
|
|
|
|
|
2017-10-30 00:41:37 -04:00
|
|
|
mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err);
|
2016-09-21 14:25:33 -04:00
|
|
|
if (out_sz == MP_STREAM_ERROR) {
|
2017-07-21 13:30:35 -04:00
|
|
|
if (mp_is_nonblocking_error(err)) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
return -err;
|
|
|
|
} else {
|
|
|
|
return out_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 17:17:44 -04:00
|
|
|
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
2018-06-12 22:37:49 -04:00
|
|
|
// Verify the socket object has the full stream protocol
|
|
|
|
mp_get_stream_raise(sock, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE | MP_STREAM_OP_IOCTL);
|
|
|
|
|
2017-10-27 00:17:35 -04:00
|
|
|
#if MICROPY_PY_USSL_FINALISER
|
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
|
|
|
|
#else
|
2016-09-21 14:25:33 -04:00
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
|
2017-10-27 00:17:35 -04:00
|
|
|
#endif
|
2016-09-21 14:25:33 -04:00
|
|
|
o->base.type = &ussl_socket_type;
|
2018-05-31 07:47:26 -04:00
|
|
|
o->sock = sock;
|
2022-12-15 13:15:18 -05:00
|
|
|
o->poll_mask = 0;
|
|
|
|
o->last_error = 0;
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
mbedtls_ssl_init(&o->ssl);
|
|
|
|
mbedtls_ssl_config_init(&o->conf);
|
|
|
|
mbedtls_x509_crt_init(&o->cacert);
|
2016-09-21 17:17:44 -04:00
|
|
|
mbedtls_x509_crt_init(&o->cert);
|
|
|
|
mbedtls_pk_init(&o->pkey);
|
2016-09-21 14:25:33 -04:00
|
|
|
mbedtls_ctr_drbg_init(&o->ctr_drbg);
|
2017-09-06 03:34:45 -04:00
|
|
|
#ifdef MBEDTLS_DEBUG_C
|
2020-04-02 13:01:16 -04:00
|
|
|
// Debug level (0-4) 1=warning, 2=info, 3=debug, 4=verbose
|
2022-07-21 16:00:24 -04:00
|
|
|
mbedtls_debug_set_threshold(3);
|
2017-09-06 03:34:45 -04:00
|
|
|
#endif
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
mbedtls_entropy_init(&o->entropy);
|
|
|
|
const byte seed[] = "upy";
|
2018-05-31 07:52:29 -04:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&o->ctr_drbg, mbedtls_entropy_func, &o->entropy, seed, sizeof(seed));
|
2016-09-21 14:25:33 -04:00
|
|
|
if (ret != 0) {
|
2017-10-31 00:54:15 -04:00
|
|
|
goto cleanup;
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_config_defaults(&o->conf,
|
2017-07-24 01:07:48 -04:00
|
|
|
args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
|
2016-09-21 14:25:33 -04:00
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
|
if (ret != 0) {
|
2017-10-31 00:54:15 -04:00
|
|
|
goto cleanup;
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:42:46 -05:00
|
|
|
mbedtls_ssl_conf_authmode(&o->conf, args->cert_reqs.u_int);
|
2016-09-21 14:25:33 -04:00
|
|
|
mbedtls_ssl_conf_rng(&o->conf, mbedtls_ctr_drbg_random, &o->ctr_drbg);
|
2017-09-06 03:34:45 -04:00
|
|
|
#ifdef MBEDTLS_DEBUG_C
|
2016-09-21 14:25:33 -04:00
|
|
|
mbedtls_ssl_conf_dbg(&o->conf, mbedtls_debug, NULL);
|
2017-09-06 03:34:45 -04:00
|
|
|
#endif
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_setup(&o->ssl, &o->conf);
|
|
|
|
if (ret != 0) {
|
2017-10-31 00:54:15 -04:00
|
|
|
goto cleanup;
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
2016-09-22 18:44:23 -04:00
|
|
|
if (args->server_hostname.u_obj != mp_const_none) {
|
|
|
|
const char *sni = mp_obj_str_get_str(args->server_hostname.u_obj);
|
|
|
|
ret = mbedtls_ssl_set_hostname(&o->ssl, sni);
|
|
|
|
if (ret != 0) {
|
2017-10-31 00:54:15 -04:00
|
|
|
goto cleanup;
|
2016-09-22 18:44:23 -04:00
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
|
|
|
|
|
2019-10-31 01:22:42 -04:00
|
|
|
if (args->key.u_obj != mp_const_none) {
|
2017-03-25 04:48:44 -04:00
|
|
|
size_t key_len;
|
2016-09-21 17:17:44 -04:00
|
|
|
const byte *key = (const byte *)mp_obj_str_get_data(args->key.u_obj, &key_len);
|
|
|
|
// len should include terminating null
|
|
|
|
ret = mbedtls_pk_parse_key(&o->pkey, key, key_len + 1, NULL, 0);
|
2019-10-31 01:22:42 -04:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; // use general error for all key errors
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 17:17:44 -04:00
|
|
|
|
2017-03-25 04:48:44 -04:00
|
|
|
size_t cert_len;
|
2016-09-21 17:17:44 -04:00
|
|
|
const byte *cert = (const byte *)mp_obj_str_get_data(args->cert.u_obj, &cert_len);
|
|
|
|
// len should include terminating null
|
|
|
|
ret = mbedtls_x509_crt_parse(&o->cert, cert, cert_len + 1);
|
2019-10-31 01:22:42 -04:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; // use general error for all cert errors
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 17:17:44 -04:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_conf_own_cert(&o->conf, &o->cert, &o->pkey);
|
2019-10-31 01:22:42 -04:00
|
|
|
if (ret != 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 17:17:44 -04:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:42:46 -05:00
|
|
|
if (args->cadata.u_obj != mp_const_none) {
|
|
|
|
size_t cacert_len;
|
|
|
|
const byte *cacert = (const byte *)mp_obj_str_get_data(args->cadata.u_obj, &cacert_len);
|
|
|
|
// len should include terminating null
|
|
|
|
ret = mbedtls_x509_crt_parse(&o->cacert, cacert, cacert_len + 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; // use general error for all cert errors
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain(&o->conf, &o->cacert, NULL);
|
|
|
|
}
|
|
|
|
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
if (args->do_handshake.u_bool) {
|
|
|
|
while ((ret = mbedtls_ssl_handshake(&o->ssl)) != 0) {
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2022-04-29 06:24:19 -04:00
|
|
|
#ifdef MICROPY_EVENT_POLL_HOOK
|
|
|
|
MICROPY_EVENT_POLL_HOOK
|
|
|
|
#endif
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
2017-10-31 00:54:15 -04:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_pk_free(&o->pkey);
|
|
|
|
mbedtls_x509_crt_free(&o->cert);
|
|
|
|
mbedtls_x509_crt_free(&o->cacert);
|
|
|
|
mbedtls_ssl_free(&o->ssl);
|
|
|
|
mbedtls_ssl_config_free(&o->conf);
|
|
|
|
mbedtls_ctr_drbg_free(&o->ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&o->entropy);
|
|
|
|
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
|
|
|
|
mp_raise_OSError(MP_ENOMEM);
|
2019-10-31 01:22:42 -04:00
|
|
|
} else if (ret == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
|
2019-10-31 01:22:42 -04:00
|
|
|
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
|
2017-10-31 00:54:15 -04:00
|
|
|
} else {
|
2020-03-27 02:17:35 -04:00
|
|
|
mbedtls_raise_error(ret);
|
2017-10-31 00:54:15 -04:00
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
2017-08-15 10:49:11 -04:00
|
|
|
STATIC mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) {
|
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
if (!mp_obj_is_true(binary_form)) {
|
|
|
|
mp_raise_NotImplementedError(NULL);
|
|
|
|
}
|
|
|
|
const mbedtls_x509_crt *peer_cert = mbedtls_ssl_get_peer_cert(&o->ssl);
|
2019-10-30 22:42:24 -04:00
|
|
|
if (peer_cert == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-08-15 10:49:11 -04:00
|
|
|
return mp_obj_new_bytes(peer_cert->raw.p, peer_cert->raw.len);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_ssl_getpeercert_obj, mod_ssl_getpeercert);
|
|
|
|
|
2016-09-21 14:25:33 -04:00
|
|
|
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
(void)kind;
|
|
|
|
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_printf(print, "<_SSLSocket %p>", self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2022-12-15 13:15:18 -05:00
|
|
|
o->poll_mask = 0;
|
|
|
|
|
|
|
|
if (o->last_error) {
|
|
|
|
*errcode = o->last_error;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
int ret = mbedtls_ssl_read(&o->ssl, buf, size);
|
2017-03-02 23:46:23 -05:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
|
|
// end of stream
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-21 13:30:35 -04:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
|
|
|
ret = MP_EWOULDBLOCK;
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
// If handshake is not finished, read attempt may end up in protocol
|
|
|
|
// wanting to write next handshake message. The same may happen with
|
|
|
|
// renegotiation.
|
|
|
|
ret = MP_EWOULDBLOCK;
|
2022-12-15 13:15:18 -05:00
|
|
|
o->poll_mask = MP_STREAM_POLL_WR;
|
|
|
|
} else {
|
|
|
|
o->last_error = ret;
|
2017-07-21 13:30:35 -04:00
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
*errcode = ret;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2022-12-15 13:15:18 -05:00
|
|
|
o->poll_mask = 0;
|
|
|
|
|
|
|
|
if (o->last_error) {
|
|
|
|
*errcode = o->last_error;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
int ret = mbedtls_ssl_write(&o->ssl, buf, size);
|
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-21 13:30:35 -04:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
ret = MP_EWOULDBLOCK;
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
|
|
|
// If handshake is not finished, write attempt may end up in protocol
|
|
|
|
// wanting to read next handshake message. The same may happen with
|
|
|
|
// renegotiation.
|
|
|
|
ret = MP_EWOULDBLOCK;
|
2022-12-15 13:15:18 -05:00
|
|
|
o->poll_mask = MP_STREAM_POLL_RD;
|
|
|
|
} else {
|
|
|
|
o->last_error = ret;
|
2017-07-21 13:30:35 -04:00
|
|
|
}
|
2016-09-21 14:25:33 -04:00
|
|
|
*errcode = ret;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:46:13 -04:00
|
|
|
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
|
2017-07-21 13:30:35 -04:00
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_obj_t sock = o->sock;
|
|
|
|
mp_obj_t dest[3];
|
|
|
|
mp_load_method(sock, MP_QSTR_setblocking, dest);
|
|
|
|
dest[2] = flag_in;
|
|
|
|
return mp_call_method_n_kw(1, 0, dest);
|
2016-10-15 16:46:13 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
|
|
|
|
2018-03-07 01:48:53 -05:00
|
|
|
STATIC mp_uint_t socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
|
2022-12-15 13:15:18 -05:00
|
|
|
mp_uint_t ret = 0;
|
|
|
|
uintptr_t saved_arg = 0;
|
|
|
|
mp_obj_t sock = self->sock;
|
|
|
|
if (sock == MP_OBJ_NULL || (request != MP_STREAM_CLOSE && self->last_error != 0)) {
|
|
|
|
// Closed or error socket:
|
|
|
|
return MP_STREAM_POLL_NVAL;
|
|
|
|
}
|
|
|
|
|
2018-07-19 23:05:04 -04:00
|
|
|
if (request == MP_STREAM_CLOSE) {
|
2022-12-15 13:15:18 -05:00
|
|
|
self->sock = MP_OBJ_NULL;
|
2018-07-19 23:05:04 -04:00
|
|
|
mbedtls_pk_free(&self->pkey);
|
|
|
|
mbedtls_x509_crt_free(&self->cert);
|
|
|
|
mbedtls_x509_crt_free(&self->cacert);
|
|
|
|
mbedtls_ssl_free(&self->ssl);
|
|
|
|
mbedtls_ssl_config_free(&self->conf);
|
|
|
|
mbedtls_ctr_drbg_free(&self->ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&self->entropy);
|
2022-12-15 13:15:18 -05:00
|
|
|
} else if (request == MP_STREAM_POLL) {
|
|
|
|
// If the library signaled us that it needs reading or writing, only check that direction,
|
|
|
|
// but save what the caller asked because we need to restore it later
|
|
|
|
if (self->poll_mask && (arg & MP_STREAM_POLL_RDWR)) {
|
|
|
|
saved_arg = arg & MP_STREAM_POLL_RDWR;
|
|
|
|
arg = (arg & ~saved_arg) | self->poll_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take into account that the library might have buffered data already
|
|
|
|
int has_pending = 0;
|
|
|
|
if (arg & MP_STREAM_POLL_RD) {
|
|
|
|
has_pending = mbedtls_ssl_check_pending(&self->ssl);
|
|
|
|
if (has_pending) {
|
|
|
|
ret |= MP_STREAM_POLL_RD;
|
|
|
|
if (arg == MP_STREAM_POLL_RD) {
|
|
|
|
// Shortcut if we only need to read and we have buffered data, no need to go to the underlying socket
|
|
|
|
return MP_STREAM_POLL_RD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 01:48:53 -05:00
|
|
|
}
|
2022-12-15 13:15:18 -05:00
|
|
|
|
2018-07-19 23:05:04 -04:00
|
|
|
// Pass all requests down to the underlying socket
|
2022-12-15 13:15:18 -05:00
|
|
|
ret |= mp_get_stream(sock)->ioctl(sock, request, arg, errcode);
|
|
|
|
|
|
|
|
if (request == MP_STREAM_POLL) {
|
|
|
|
// The direction the library needed is available, return a fake result to the caller so that
|
|
|
|
// it reenters a read or a write to allow the handshake to progress
|
|
|
|
if (ret & self->poll_mask) {
|
|
|
|
ret |= saved_arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
2016-10-15 16:46:13 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
|
2018-03-07 01:48:53 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2017-10-27 00:17:35 -04:00
|
|
|
#if MICROPY_PY_USSL_FINALISER
|
2018-03-07 01:48:53 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
|
2017-10-27 00:17:35 -04:00
|
|
|
#endif
|
2022-12-15 13:15:18 -05:00
|
|
|
#if MICROPY_UNIX_COVERAGE
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&mp_stream_ioctl_obj) },
|
|
|
|
#endif
|
2017-08-15 10:49:11 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
|
2016-09-21 14:25:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ussl_socket_locals_dict, ussl_socket_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_stream_p_t ussl_socket_stream_p = {
|
|
|
|
.read = socket_read,
|
|
|
|
.write = socket_write,
|
2018-03-07 01:48:53 -05:00
|
|
|
.ioctl = socket_ioctl,
|
2016-09-21 14:25:33 -04:00
|
|
|
};
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
ussl_socket_type,
|
|
|
|
MP_QSTR_ussl,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2016-09-21 14:25:33 -04:00
|
|
|
// Save on qstr's, reuse same as for module
|
2021-07-14 00:38:38 -04:00
|
|
|
print, socket_print,
|
|
|
|
protocol, &ussl_socket_stream_p,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &ussl_socket_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2016-09-21 14:25:33 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// TODO: Implement more args
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-12-15 23:40:05 -05:00
|
|
|
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_cert, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2016-09-21 14:25:33 -04:00
|
|
|
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2019-12-15 23:40:05 -05:00
|
|
|
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2022-01-27 23:42:46 -05:00
|
|
|
{ MP_QSTR_cert_reqs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MBEDTLS_SSL_VERIFY_NONE}},
|
|
|
|
{ MP_QSTR_cadata, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
extmod/modussl_mbedtls: Support non-blocking handshake.
For this, add wrap_socket(do_handshake=False) param. CPython doesn't have
such a param at a module's global function, and at SSLContext.wrap_socket()
it has do_handshake_on_connect param, but that uselessly long.
Beyond that, make write() handle not just MBEDTLS_ERR_SSL_WANT_WRITE, but
also MBEDTLS_ERR_SSL_WANT_READ, as during handshake, write call may be
actually preempted by need to read next handshake message from peer.
Likewise, for read(). And even after the initial negotiation, situations
like that may happen e.g. with renegotiation. Both
MBEDTLS_ERR_SSL_WANT_READ and MBEDTLS_ERR_SSL_WANT_WRITE are however mapped
to the same None return code. The idea is that if the same read()/write()
method is called repeatedly, the progress will be made step by step anyway.
The caveat is if user wants to add the underlying socket to uselect.poll().
To be reliable, in this case, the socket should be polled for both POLL_IN
and POLL_OUT, as we don't know the actual expected direction. But that's
actually problematic. Consider for example that write() ends with
MBEDTLS_ERR_SSL_WANT_READ, but gets converted to None. We put the
underlying socket on pull using POLL_IN|POLL_OUT but that probably returns
immediately with POLL_OUT, as underlyings socket is writable. We call the
same ussl write() again, which again results in MBEDTLS_ERR_SSL_WANT_READ,
etc. We thus go into busy-loop.
So, the handling in this patch is temporary and needs fixing. But exact way
to fix it is not clear. One way is to provide explicit function for
handshake (CPython has do_handshake()), and let *that* return distinct
codes like WANT_READ/WANT_WRITE. But as mentioned above, past the initial
handshake, such situation may happen again with at least renegotiation. So
apparently, the only robust solution is to return "out of bound" special
sentinels like WANT_READ/WANT_WRITE from read()/write() directly. CPython
throws exceptions for these, but those are expensive to adopt that way for
efficiency-conscious implementation like MicroPython.
2019-03-01 15:48:16 -05:00
|
|
|
{ MP_QSTR_do_handshake, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
2016-09-21 14:25:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Check that sock implements stream protocol
|
|
|
|
mp_obj_t sock = pos_args[0];
|
|
|
|
|
2016-09-21 17:17:44 -04:00
|
|
|
struct ssl_args args;
|
2016-09-21 14:25:33 -04:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
|
|
|
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
|
|
|
|
|
2016-09-21 17:17:44 -04:00
|
|
|
return MP_OBJ_FROM_PTR(socket_new(sock, &args));
|
2016-09-21 14:25:33 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 1, mod_ssl_wrap_socket);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t mp_module_ssl_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ussl) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&mod_ssl_wrap_socket_obj) },
|
2022-01-27 23:42:46 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_CERT_NONE), MP_ROM_INT(MBEDTLS_SSL_VERIFY_NONE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_CERT_OPTIONAL), MP_ROM_INT(MBEDTLS_SSL_VERIFY_OPTIONAL) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_CERT_REQUIRED), MP_ROM_INT(MBEDTLS_SSL_VERIFY_REQUIRED) },
|
2016-09-21 14:25:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_ssl_globals, mp_module_ssl_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ussl = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
|
|
|
|
};
|
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_ussl, mp_module_ussl);
|
2022-04-20 02:06:22 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_USSL && MICROPY_SSL_MBEDTLS
|