|
|
@@ -34,6 +34,7 @@
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
#include "py/stream.h"
|
|
|
+#include "py/objstr.h"
|
|
|
|
|
|
// mbedtls_time_t
|
|
|
#include "mbedtls/platform.h"
|
|
|
@@ -43,6 +44,7 @@
|
|
|
#include "mbedtls/entropy.h"
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
#include "mbedtls/debug.h"
|
|
|
+#include "mbedtls/error.h"
|
|
|
|
|
|
typedef struct _mp_obj_ssl_socket_t {
|
|
|
mp_obj_base_t base;
|
|
|
@@ -74,8 +76,48 @@ STATIC void mbedtls_debug(void *ctx, int level, const char *file, int line, cons
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
+STATIC NORETURN void mbedtls_raise_error(int err) {
|
|
|
+ // _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.
|
|
|
+ if (err < 0 && err > -256) {
|
|
|
+ mp_raise_OSError(-err);
|
|
|
+ }
|
|
|
+
|
|
|
+ #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.
|
|
|
+
|
|
|
+ // Try to allocate memory for the message
|
|
|
+ #define ERR_STR_MAX 80 // mbedtls_strerror truncates if it doesn't fit
|
|
|
+ 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);
|
|
|
+ size_t len = strlen((char *)o_str_buf);
|
|
|
+
|
|
|
+ // 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
|
|
|
+ mp_raise_OSError(err); // err is typically a large negative number
|
|
|
+ #endif
|
|
|
+}
|
|
|
+
|
|
|
STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
|
|
|
- mp_obj_t sock = *(mp_obj_t*)ctx;
|
|
|
+ mp_obj_t sock = *(mp_obj_t *)ctx;
|
|
|
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
|
|
int err;
|
|
|
@@ -85,14 +127,14 @@ STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
|
|
|
if (mp_is_nonblocking_error(err)) {
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
}
|
|
|
- return -err;
|
|
|
+ return -err; // convert an MP_ERRNO to something mbedtls passes through as error
|
|
|
} else {
|
|
|
return out_sz;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
|
|
|
- mp_obj_t sock = *(mp_obj_t*)ctx;
|
|
|
+ mp_obj_t sock = *(mp_obj_t *)ctx;
|
|
|
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
|
|
int err;
|
|
|
@@ -113,11 +155,11 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
|
|
// 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);
|
|
|
|
|
|
-#if MICROPY_PY_USSL_FINALISER
|
|
|
+ #if MICROPY_PY_USSL_FINALISER
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj_with_finaliser(mp_obj_ssl_socket_t);
|
|
|
-#else
|
|
|
+ #else
|
|
|
mp_obj_ssl_socket_t *o = m_new_obj(mp_obj_ssl_socket_t);
|
|
|
-#endif
|
|
|
+ #endif
|
|
|
o->base.type = &ussl_socket_type;
|
|
|
o->sock = sock;
|
|
|
|
|
|
@@ -141,9 +183,9 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
|
|
}
|
|
|
|
|
|
ret = mbedtls_ssl_config_defaults(&o->conf,
|
|
|
- args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
|
|
|
- MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
- MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
+ args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
|
|
|
+ MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
+ MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
if (ret != 0) {
|
|
|
goto cleanup;
|
|
|
}
|
|
|
@@ -171,7 +213,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
|
|
|
|
|
if (args->key.u_obj != mp_const_none) {
|
|
|
size_t key_len;
|
|
|
- const byte *key = (const byte*)mp_obj_str_get_data(args->key.u_obj, &key_len);
|
|
|
+ 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);
|
|
|
if (ret != 0) {
|
|
|
@@ -180,7 +222,7 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
|
|
}
|
|
|
|
|
|
size_t cert_len;
|
|
|
- const byte *cert = (const byte*)mp_obj_str_get_data(args->cert.u_obj, &cert_len);
|
|
|
+ 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);
|
|
|
if (ret != 0) {
|
|
|
@@ -197,7 +239,6 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
|
|
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) {
|
|
|
- printf("mbedtls_ssl_handshake error: -%x\n", -ret);
|
|
|
goto cleanup;
|
|
|
}
|
|
|
}
|
|
|
@@ -217,11 +258,11 @@ cleanup:
|
|
|
if (ret == MBEDTLS_ERR_SSL_ALLOC_FAILED) {
|
|
|
mp_raise_OSError(MP_ENOMEM);
|
|
|
} else if (ret == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
|
|
|
- mp_raise_ValueError("invalid key");
|
|
|
+ mp_raise_ValueError(MP_ERROR_TEXT("invalid key"));
|
|
|
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
|
|
|
- mp_raise_ValueError("invalid cert");
|
|
|
+ mp_raise_ValueError(MP_ERROR_TEXT("invalid cert"));
|
|
|
} else {
|
|
|
- mp_raise_OSError(MP_EIO);
|
|
|
+ mbedtls_raise_error(ret);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -230,7 +271,7 @@ STATIC mp_obj_t mod_ssl_getpeercert(mp_obj_t o_in, mp_obj_t binary_form) {
|
|
|
if (!mp_obj_is_true(binary_form)) {
|
|
|
mp_raise_NotImplementedError(NULL);
|
|
|
}
|
|
|
- const mbedtls_x509_crt* peer_cert = mbedtls_ssl_get_peer_cert(&o->ssl);
|
|
|
+ const mbedtls_x509_crt *peer_cert = mbedtls_ssl_get_peer_cert(&o->ssl);
|
|
|
if (peer_cert == NULL) {
|
|
|
return mp_const_none;
|
|
|
}
|
|
|
@@ -318,9 +359,9 @@ STATIC const mp_rom_map_elem_t ussl_socket_locals_dict_table[] = {
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
|
|
-#if MICROPY_PY_USSL_FINALISER
|
|
|
+ #if MICROPY_PY_USSL_FINALISER
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
|
|
|
-#endif
|
|
|
+ #endif
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
|
|
|
};
|
|
|
|
|
|
@@ -340,7 +381,7 @@ STATIC const mp_obj_type_t ussl_socket_type = {
|
|
|
.getiter = NULL,
|
|
|
.iternext = NULL,
|
|
|
.protocol = &ussl_socket_stream_p,
|
|
|
- .locals_dict = (void*)&ussl_socket_locals_dict,
|
|
|
+ .locals_dict = (void *)&ussl_socket_locals_dict,
|
|
|
};
|
|
|
|
|
|
STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
@@ -358,7 +399,7 @@ STATIC mp_obj_t mod_ssl_wrap_socket(size_t n_args, const mp_obj_t *pos_args, mp_
|
|
|
|
|
|
struct ssl_args args;
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
|
|
- MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
|
|
|
+ MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t *)&args);
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(socket_new(sock, &args));
|
|
|
}
|
|
|
@@ -373,7 +414,7 @@ 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,
|
|
|
+ .globals = (mp_obj_dict_t *)&mp_module_ssl_globals,
|
|
|
};
|
|
|
|
|
|
#endif // MICROPY_PY_USSL
|