2015-10-06 16:10:00 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-10-06 16:10:00 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
* Copyright (c) 2015-2019 Paul Sokolovsky
|
2023-06-24 16:06:00 +01:00
|
|
|
* Copyright (c) 2023 Damien P. George
|
2015-10-06 16:10:00 +01: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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
2020-03-27 06:17:35 +00:00
|
|
|
#include "py/objstr.h"
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_SSL && MICROPY_SSL_AXTLS
|
2015-10-06 16:10:00 +01:00
|
|
|
|
|
|
|
#include "ssl.h"
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
#define PROTOCOL_TLS_CLIENT (0)
|
|
|
|
#define PROTOCOL_TLS_SERVER (1)
|
|
|
|
|
|
|
|
// This corresponds to an SSLContext object.
|
|
|
|
typedef struct _mp_obj_ssl_context_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t key;
|
|
|
|
mp_obj_t cert;
|
|
|
|
} mp_obj_ssl_context_t;
|
|
|
|
|
|
|
|
// This corresponds to an SSLSocket object.
|
2015-10-06 16:10:00 +01:00
|
|
|
typedef struct _mp_obj_ssl_socket_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t sock;
|
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
SSL *ssl_sock;
|
|
|
|
byte *buf;
|
|
|
|
uint32_t bytes_left;
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
bool blocking;
|
2015-10-06 16:10:00 +01:00
|
|
|
} mp_obj_ssl_socket_t;
|
|
|
|
|
2017-06-13 23:01:12 +01:00
|
|
|
struct ssl_args {
|
2017-10-30 01:48:14 +00:00
|
|
|
mp_arg_val_t key;
|
|
|
|
mp_arg_val_t cert;
|
2017-06-13 23:01:12 +01:00
|
|
|
mp_arg_val_t server_side;
|
|
|
|
mp_arg_val_t server_hostname;
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
mp_arg_val_t do_handshake;
|
2017-06-13 23:01:12 +01:00
|
|
|
};
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
STATIC const mp_obj_type_t ssl_context_type;
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC const mp_obj_type_t ssl_socket_type;
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
STATIC mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
|
|
|
|
bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Helper functions.
|
|
|
|
|
2020-09-03 15:53:18 +01:00
|
|
|
// Table of error strings corresponding to SSL_xxx error codes.
|
|
|
|
STATIC const char *const ssl_error_tab1[] = {
|
|
|
|
"NOT_OK",
|
|
|
|
"DEAD",
|
|
|
|
"CLOSE_NOTIFY",
|
|
|
|
"EAGAIN",
|
2020-03-27 06:17:35 +00:00
|
|
|
};
|
2020-09-03 15:53:18 +01:00
|
|
|
STATIC const char *const ssl_error_tab2[] = {
|
|
|
|
"CONN_LOST",
|
|
|
|
"RECORD_OVERFLOW",
|
|
|
|
"SOCK_SETUP_FAILURE",
|
|
|
|
NULL,
|
|
|
|
"INVALID_HANDSHAKE",
|
|
|
|
"INVALID_PROT_MSG",
|
|
|
|
"INVALID_HMAC",
|
|
|
|
"INVALID_VERSION",
|
|
|
|
"UNSUPPORTED_EXTENSION",
|
|
|
|
"INVALID_SESSION",
|
|
|
|
"NO_CIPHER",
|
|
|
|
"INVALID_CERT_HASH_ALG",
|
|
|
|
"BAD_CERTIFICATE",
|
|
|
|
"INVALID_KEY",
|
|
|
|
NULL,
|
|
|
|
"FINISHED_INVALID",
|
|
|
|
"NO_CERT_DEFINED",
|
|
|
|
"NO_CLIENT_RENOG",
|
|
|
|
"NOT_SUPPORTED",
|
2020-03-27 06:17:35 +00:00
|
|
|
};
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC NORETURN void ssl_raise_error(int err) {
|
2020-09-03 15:53:18 +01:00
|
|
|
MP_STATIC_ASSERT(SSL_NOT_OK - 3 == SSL_EAGAIN);
|
|
|
|
MP_STATIC_ASSERT(SSL_ERROR_CONN_LOST - 18 == SSL_ERROR_NOT_SUPPORTED);
|
|
|
|
|
|
|
|
// Check if err corresponds to something in one of the error string tables.
|
|
|
|
const char *errstr = NULL;
|
|
|
|
if (SSL_NOT_OK >= err && err >= SSL_EAGAIN) {
|
|
|
|
errstr = ssl_error_tab1[SSL_NOT_OK - err];
|
|
|
|
} else if (SSL_ERROR_CONN_LOST >= err && err >= SSL_ERROR_NOT_SUPPORTED) {
|
|
|
|
errstr = ssl_error_tab2[SSL_ERROR_CONN_LOST - err];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown error, just raise the error code.
|
|
|
|
if (errstr == NULL) {
|
|
|
|
mp_raise_OSError(err);
|
2020-03-27 06:17:35 +00:00
|
|
|
}
|
2020-09-03 15:53:18 +01:00
|
|
|
|
|
|
|
// Construct string object.
|
|
|
|
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
|
|
|
|
if (o_str == NULL) {
|
|
|
|
mp_raise_OSError(err);
|
|
|
|
}
|
|
|
|
o_str->base.type = &mp_type_str;
|
|
|
|
o_str->data = (const byte *)errstr;
|
|
|
|
o_str->len = strlen((char *)o_str->data);
|
|
|
|
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
|
|
|
|
|
|
|
|
// Raise OSError(err, str).
|
|
|
|
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));
|
2020-03-27 06:17:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
// SSLContext type.
|
|
|
|
|
|
|
|
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) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
|
|
|
|
|
|
// The "protocol" argument is ignored in this implementation.
|
|
|
|
|
|
|
|
// Create SSLContext object.
|
|
|
|
#if MICROPY_PY_SSL_FINALISER
|
|
|
|
mp_obj_ssl_context_t *self = m_new_obj_with_finaliser(mp_obj_ssl_context_t);
|
|
|
|
#else
|
|
|
|
mp_obj_ssl_context_t *self = m_new_obj(mp_obj_ssl_context_t);
|
|
|
|
#endif
|
|
|
|
self->base.type = type_in;
|
|
|
|
self->key = mp_const_none;
|
|
|
|
self->cert = mp_const_none;
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void ssl_context_load_key(mp_obj_ssl_context_t *self, mp_obj_t key_obj, mp_obj_t cert_obj) {
|
|
|
|
self->key = key_obj;
|
|
|
|
self->cert = cert_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t ssl_context_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_server_side, ARG_do_handshake_on_connect, ARG_server_hostname };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_do_handshake_on_connect, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
|
|
|
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse arguments.
|
|
|
|
mp_obj_ssl_context_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
mp_obj_t sock = pos_args[1];
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// Create and return the new SSLSocket object.
|
|
|
|
return ssl_socket_make_new(self, sock, args[ARG_server_side].u_bool,
|
|
|
|
args[ARG_do_handshake_on_connect].u_bool, args[ARG_server_hostname].u_obj);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ssl_context_wrap_socket_obj, 2, ssl_context_wrap_socket);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t ssl_context_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&ssl_context_wrap_socket_obj) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ssl_context_locals_dict, ssl_context_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
ssl_context_type,
|
|
|
|
MP_QSTR_SSLContext,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
make_new, ssl_context_make_new,
|
|
|
|
locals_dict, &ssl_context_locals_dict
|
|
|
|
);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// SSLSocket type.
|
|
|
|
|
|
|
|
STATIC mp_obj_t ssl_socket_make_new(mp_obj_ssl_context_t *ssl_context, mp_obj_t sock,
|
|
|
|
bool server_side, bool do_handshake_on_connect, mp_obj_t server_hostname) {
|
2020-03-27 06:17:35 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_SSL_FINALISER
|
2017-10-27 05:17:35 +01:00
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
|
|
|
|
#else
|
2015-10-06 16:10:00 +01:00
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
|
2017-10-27 05:17:35 +01:00
|
|
|
#endif
|
2022-08-18 05:49:57 +01:00
|
|
|
o->base.type = &ssl_socket_type;
|
2015-10-06 16:10:00 +01:00
|
|
|
o->buf = NULL;
|
|
|
|
o->bytes_left = 0;
|
2023-09-27 04:36:25 +01:00
|
|
|
o->sock = MP_OBJ_NULL;
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
o->blocking = true;
|
2015-10-06 16:10:00 +01:00
|
|
|
|
|
|
|
uint32_t options = SSL_SERVER_VERIFY_LATER;
|
2023-06-24 16:06:00 +01:00
|
|
|
if (!do_handshake_on_connect) {
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
options |= SSL_CONNECT_IN_PARTS;
|
|
|
|
}
|
2023-06-24 16:06:00 +01:00
|
|
|
if (ssl_context->key != mp_const_none) {
|
2017-10-30 01:48:14 +00:00
|
|
|
options |= SSL_NO_DEFAULT_KEY;
|
|
|
|
}
|
2016-04-27 22:49:54 +01:00
|
|
|
if ((o->ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_CLNT_SESS)) == NULL) {
|
2016-10-07 03:52:14 +01:00
|
|
|
mp_raise_OSError(MP_EINVAL);
|
2015-10-06 16:10:00 +01:00
|
|
|
}
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
if (ssl_context->key != mp_const_none) {
|
2017-10-30 01:48:14 +00:00
|
|
|
size_t len;
|
2023-06-24 16:06:00 +01:00
|
|
|
const byte *data = (const byte *)mp_obj_str_get_data(ssl_context->key, &len);
|
2017-10-30 01:48:14 +00:00
|
|
|
int res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_RSA_KEY, data, len, NULL);
|
|
|
|
if (res != SSL_OK) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
|
2017-10-30 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
data = (const byte *)mp_obj_str_get_data(ssl_context->cert, &len);
|
2017-10-30 01:48:14 +00:00
|
|
|
res = ssl_obj_memory_load(o->ssl_ctx, SSL_OBJ_X509_CERT, data, len, NULL);
|
|
|
|
if (res != SSL_OK) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
|
2017-10-30 01:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
if (server_side) {
|
2016-04-28 15:27:20 +01:00
|
|
|
o->ssl_sock = ssl_server_new(o->ssl_ctx, (long)sock);
|
|
|
|
} else {
|
2017-06-13 23:01:12 +01:00
|
|
|
SSL_EXTENSIONS *ext = ssl_ext_new();
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
if (server_hostname != mp_const_none) {
|
|
|
|
ext->host_name = (char *)mp_obj_str_get_str(server_hostname);
|
2017-06-13 23:01:12 +01:00
|
|
|
}
|
2016-04-28 15:27:20 +01:00
|
|
|
|
2017-06-13 23:01:12 +01:00
|
|
|
o->ssl_sock = ssl_client_new(o->ssl_ctx, (long)sock, NULL, 0, ext);
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
if (do_handshake_on_connect) {
|
2020-04-02 18:01:16 +01:00
|
|
|
int r = ssl_handshake_status(o->ssl_sock);
|
|
|
|
|
|
|
|
if (r != SSL_OK) {
|
|
|
|
if (r == SSL_CLOSE_NOTIFY) { // EOF
|
|
|
|
r = MP_ENOTCONN;
|
|
|
|
} else if (r == SSL_EAGAIN) {
|
|
|
|
r = MP_EAGAIN;
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
ssl_raise_error(r);
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
}
|
2016-04-28 15:27:20 +01:00
|
|
|
}
|
2015-10-06 16:10:00 +01:00
|
|
|
}
|
|
|
|
|
2023-09-27 04:36:25 +01:00
|
|
|
// Populate the socket entry now that the SSLSocket is fully set up.
|
|
|
|
// This prevents closing the socket if an exception is raised above.
|
|
|
|
o->sock = sock;
|
|
|
|
|
2015-10-06 16:10:00 +01:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_uint_t ssl_socket_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
2016-05-25 23:38:37 +01:00
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2017-07-19 22:20:53 +01:00
|
|
|
if (o->ssl_sock == NULL) {
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-10-06 16:10:00 +01:00
|
|
|
while (o->bytes_left == 0) {
|
|
|
|
mp_int_t r = ssl_read(o->ssl_sock, &o->buf);
|
2016-04-28 15:29:11 +01:00
|
|
|
if (r == SSL_OK) {
|
|
|
|
// SSL_OK from ssl_read() means "everything is ok, but there's
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
// no user data yet". It may happen e.g. if handshake is not
|
|
|
|
// finished yet. The best way we can treat it is by returning
|
|
|
|
// EAGAIN. This may be a bit unexpected in blocking mode, but
|
|
|
|
// default is to perform complete handshake in constructor, so
|
|
|
|
// this should not happen in blocking mode. On the other hand,
|
|
|
|
// in nonblocking mode EAGAIN (comparing to the alternative of
|
|
|
|
// looping) is really preferable.
|
|
|
|
if (o->blocking) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto eagain;
|
|
|
|
}
|
2016-04-28 15:29:11 +01:00
|
|
|
}
|
2015-10-06 16:10:00 +01:00
|
|
|
if (r < 0) {
|
|
|
|
if (r == SSL_CLOSE_NOTIFY || r == SSL_ERROR_CONN_LOST) {
|
|
|
|
// EOF
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-01 22:14:11 +00:00
|
|
|
if (r == SSL_EAGAIN) {
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
eagain:
|
2017-11-01 22:14:11 +00:00
|
|
|
r = MP_EAGAIN;
|
|
|
|
}
|
2015-10-06 16:10:00 +01:00
|
|
|
*errcode = r;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
o->bytes_left = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > o->bytes_left) {
|
|
|
|
size = o->bytes_left;
|
|
|
|
}
|
|
|
|
memcpy(buf, o->buf, size);
|
|
|
|
o->buf += size;
|
|
|
|
o->bytes_left -= size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_uint_t ssl_socket_write(mp_obj_t o_in, const void *buf, mp_uint_t size, int *errcode) {
|
2016-05-25 23:38:37 +01:00
|
|
|
mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in);
|
2017-07-19 22:20:53 +01:00
|
|
|
|
|
|
|
if (o->ssl_sock == NULL) {
|
|
|
|
*errcode = EBADF;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:01:16 +01:00
|
|
|
mp_int_t r;
|
|
|
|
eagain:
|
|
|
|
r = ssl_write(o->ssl_sock, buf, size);
|
|
|
|
if (r == 0) {
|
2022-08-18 05:49:57 +01:00
|
|
|
// see comment in ssl_socket_read above
|
2020-04-02 18:01:16 +01:00
|
|
|
if (o->blocking) {
|
|
|
|
goto eagain;
|
|
|
|
} else {
|
|
|
|
r = SSL_EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 16:10:00 +01:00
|
|
|
if (r < 0) {
|
2020-04-02 18:01:16 +01:00
|
|
|
if (r == SSL_CLOSE_NOTIFY || r == SSL_ERROR_CONN_LOST) {
|
|
|
|
return 0; // EOF
|
|
|
|
}
|
|
|
|
if (r == SSL_EAGAIN) {
|
|
|
|
r = MP_EAGAIN;
|
|
|
|
}
|
2015-10-06 16:10:00 +01:00
|
|
|
*errcode = r;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_uint_t ssl_socket_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
2018-03-07 06:48:53 +00:00
|
|
|
mp_obj_ssl_socket_t *self = MP_OBJ_TO_PTR(o_in);
|
2023-09-27 04:36:25 +01:00
|
|
|
if (request == MP_STREAM_CLOSE) {
|
|
|
|
if (self->ssl_sock == NULL) {
|
|
|
|
// Already closed socket, do nothing.
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-20 04:05:04 +01:00
|
|
|
ssl_free(self->ssl_sock);
|
|
|
|
ssl_ctx_free(self->ssl_ctx);
|
|
|
|
self->ssl_sock = NULL;
|
2018-03-07 06:48:53 +00:00
|
|
|
}
|
2023-09-27 04:36:25 +01:00
|
|
|
|
|
|
|
if (self->sock == MP_OBJ_NULL) {
|
|
|
|
// Underlying socket may be null if the constructor raised an exception.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-20 04:05:04 +01:00
|
|
|
// Pass all requests down to the underlying socket
|
|
|
|
return mp_get_stream(self->sock)->ioctl(self->sock, request, arg, errcode);
|
2018-03-07 06:48:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t ssl_socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00: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;
|
|
|
|
mp_obj_t res = mp_call_method_n_kw(1, 0, dest);
|
|
|
|
o->blocking = mp_obj_is_true(flag_in);
|
|
|
|
return res;
|
2016-07-14 23:05:06 +01:00
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_socket_setblocking_obj, ssl_socket_setblocking);
|
2016-07-14 23:05:06 +01:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC const mp_rom_map_elem_t ssl_socket_locals_dict_table[] = {
|
2016-05-25 23:38:37 +01:00
|
|
|
{ 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) },
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&ssl_socket_setblocking_obj) },
|
2018-03-07 06:48:53 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_SSL_FINALISER
|
2018-06-13 04:16:21 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
|
2017-10-27 05:17:35 +01:00
|
|
|
#endif
|
2015-10-06 16:10:00 +01:00
|
|
|
};
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(ssl_socket_locals_dict, ssl_socket_locals_dict_table);
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC const mp_stream_p_t ssl_socket_stream_p = {
|
|
|
|
.read = ssl_socket_read,
|
|
|
|
.write = ssl_socket_write,
|
|
|
|
.ioctl = ssl_socket_ioctl,
|
2015-10-06 16:10:00 +01:00
|
|
|
};
|
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
2022-08-18 05:49:57 +01:00
|
|
|
ssl_socket_type,
|
2023-06-24 16:06:00 +01:00
|
|
|
MP_QSTR_SSLSocket,
|
2022-08-17 07:18:31 +01:00
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-08-18 05:49:57 +01:00
|
|
|
protocol, &ssl_socket_stream_p,
|
|
|
|
locals_dict, &ssl_socket_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
// ssl module.
|
|
|
|
|
2016-05-25 23:38:37 +01:00
|
|
|
STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2023-06-24 16:06:00 +01:00
|
|
|
enum {
|
|
|
|
ARG_key,
|
|
|
|
ARG_cert,
|
|
|
|
ARG_server_side,
|
|
|
|
ARG_server_hostname,
|
|
|
|
ARG_do_handshake,
|
|
|
|
};
|
2016-04-28 15:27:20 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-12-16 04:40:05 +00: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-04-28 15:27:20 +01:00
|
|
|
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2019-12-16 04:40:05 +00:00
|
|
|
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
extmod/modussl_axtls: Add non-blocking mode support.
It consists of:
1. "do_handhake" param (default True) to wrap_socket(). If it's False,
handshake won't be performed by wrap_socket(), as it would be done in
blocking way normally. Instead, SSL socket can be set to non-blocking mode,
and handshake would be performed before the first read/write request (by
just returning EAGAIN to these requests, while instead reading/writing/
processing handshake over the connection). Unfortunately, axTLS doesn't
really support non-blocking handshake correctly. So, while framework for
this is implemented on MicroPython's module side, in case of axTLS, it
won't work reliably.
2. Implementation of .setblocking() method. It must be called on SSL socket
for blocking vs non-blocking operation to be handled correctly (for
example, it's not enough to wrap non-blocking socket with wrap_socket()
call - resulting SSL socket won't be itself non-blocking). Note that
.setblocking() propagates call to the underlying socket object, as
expected.
2019-03-01 20:48:16 +00:00
|
|
|
{ MP_QSTR_do_handshake, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
2016-04-28 15:27:20 +01:00
|
|
|
};
|
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
// Parse arguments.
|
2016-04-28 15:27:20 +01:00
|
|
|
mp_obj_t sock = pos_args[0];
|
2023-06-24 16:06:00 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
// Create SSLContext.
|
|
|
|
mp_int_t protocol = args[ARG_server_side].u_bool ? PROTOCOL_TLS_SERVER : PROTOCOL_TLS_CLIENT;
|
|
|
|
mp_obj_t ssl_context_args[1] = { MP_OBJ_NEW_SMALL_INT(protocol) };
|
|
|
|
mp_obj_ssl_context_t *ssl_context = MP_OBJ_TO_PTR(ssl_context_make_new(&ssl_context_type, 1, 0, ssl_context_args));
|
2016-04-28 15:27:20 +01:00
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
// Load key and cert if given.
|
|
|
|
if (args[ARG_key].u_obj != mp_const_none) {
|
|
|
|
ssl_context_load_key(ssl_context, args[ARG_key].u_obj, args[ARG_cert].u_obj);
|
|
|
|
}
|
2016-04-28 15:27:20 +01:00
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
// Create and return the new SSLSocket object.
|
|
|
|
return ssl_socket_make_new(ssl_context, sock, args[ARG_server_side].u_bool,
|
|
|
|
args[ARG_do_handshake].u_bool, args[ARG_server_hostname].u_obj);
|
2015-10-06 16:10:00 +01:00
|
|
|
}
|
2016-04-28 15:27:20 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_ssl_wrap_socket_obj, 1, mod_ssl_wrap_socket);
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2016-05-25 23:38:37 +01:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_ssl_globals_table[] = {
|
2022-08-17 07:18:31 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ssl) },
|
2023-06-24 16:06:00 +01:00
|
|
|
|
|
|
|
// Functions.
|
2016-05-25 23:38:37 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_wrap_socket), MP_ROM_PTR(&mod_ssl_wrap_socket_obj) },
|
2015-10-06 16:10:00 +01:00
|
|
|
|
2023-06-24 16:06:00 +01:00
|
|
|
// Classes.
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SSLContext), MP_ROM_PTR(&ssl_context_type) },
|
|
|
|
|
|
|
|
// Constants.
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_CLIENT), MP_ROM_INT(PROTOCOL_TLS_CLIENT) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_SERVER), MP_ROM_INT(PROTOCOL_TLS_SERVER) },
|
|
|
|
};
|
2015-10-06 16:10:00 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_ssl_globals, mp_module_ssl_globals_table);
|
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
const mp_obj_module_t mp_module_ssl = {
|
2015-10-06 16:10:00 +01:00
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
|
|
|
|
};
|
|
|
|
|
2023-06-02 03:33:25 +01:00
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_ssl, mp_module_ssl);
|
2022-04-20 07:06:22 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_SSL && MICROPY_SSL_AXTLS
|