2016-09-21 19:25:33 +01: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 20:48:16 +00:00
|
|
|
* Copyright (c) 2019 Paul Sokolovsky
|
2016-09-21 19:25:33 +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 "py/mpconfig.h"
|
|
|
|
#if MICROPY_PY_USSL && MICROPY_SSL_MBEDTLS
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-07-21 18:30:35 +01:00
|
|
|
#include <errno.h> // needed because mp_is_nonblocking_error uses system error codes
|
2016-09-21 19:25:33 +01:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
|
|
|
|
// mbedtls_time_t
|
2016-09-21 23:30:48 +01: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"
|
2016-09-21 19:25:33 +01: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 22:17:44 +01:00
|
|
|
mbedtls_x509_crt cert;
|
|
|
|
mbedtls_pk_context pkey;
|
2016-09-21 19:25:33 +01:00
|
|
|
} mp_obj_ssl_socket_t;
|
|
|
|
|
2016-09-21 22:17:44 +01:00
|
|
|
struct ssl_args {
|
|
|
|
mp_arg_val_t key;
|
|
|
|
mp_arg_val_t cert;
|
|
|
|
mp_arg_val_t server_side;
|
2016-09-22 23:44:23 +01:00
|
|
|
mp_arg_val_t server_hostname;
|
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 20:48:16 +00:00
|
|
|
mp_arg_val_t do_handshake;
|
2016-09-21 22:17:44 +01:00
|
|
|
};
|
|
|
|
|
2016-09-21 19:25:33 +01:00
|
|
|
STATIC const mp_obj_type_t ussl_socket_type;
|
|
|
|
|
2017-10-30 04:41:37 +00: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;
|
2016-09-21 19:25:33 +01:00
|
|
|
printf("DBG:%s:%04d: %s\n", file, line, str);
|
|
|
|
}
|
2017-10-30 04:41:37 +00:00
|
|
|
#endif
|
2016-09-21 19:25:33 +01:00
|
|
|
|
2017-10-30 04:41:37 +00:00
|
|
|
STATIC int _mbedtls_ssl_send(void *ctx, const byte *buf, size_t len) {
|
2016-09-21 19:25:33 +01:00
|
|
|
mp_obj_t sock = *(mp_obj_t*)ctx;
|
|
|
|
|
2018-06-13 03:37:49 +01:00
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
2016-09-21 19:25:33 +01:00
|
|
|
int err;
|
|
|
|
|
2017-10-30 04:41:37 +00:00
|
|
|
mp_uint_t out_sz = sock_stream->write(sock, buf, len, &err);
|
2016-09-21 19:25:33 +01:00
|
|
|
if (out_sz == MP_STREAM_ERROR) {
|
2017-07-21 18:30:35 +01:00
|
|
|
if (mp_is_nonblocking_error(err)) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
return -err;
|
|
|
|
} else {
|
|
|
|
return out_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-30 04:41:37 +00:00
|
|
|
STATIC int _mbedtls_ssl_recv(void *ctx, byte *buf, size_t len) {
|
2016-09-21 19:25:33 +01:00
|
|
|
mp_obj_t sock = *(mp_obj_t*)ctx;
|
|
|
|
|
2018-06-13 03:37:49 +01:00
|
|
|
const mp_stream_p_t *sock_stream = mp_get_stream(sock);
|
2016-09-21 19:25:33 +01:00
|
|
|
int err;
|
|
|
|
|
2017-10-30 04:41:37 +00:00
|
|
|
mp_uint_t out_sz = sock_stream->read(sock, buf, len, &err);
|
2016-09-21 19:25:33 +01:00
|
|
|
if (out_sz == MP_STREAM_ERROR) {
|
2017-07-21 18:30:35 +01:00
|
|
|
if (mp_is_nonblocking_error(err)) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
return -err;
|
|
|
|
} else {
|
|
|
|
return out_sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-21 22:17:44 +01:00
|
|
|
STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) {
|
2018-06-13 03:37:49 +01: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 05:17:35 +01: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 19:25:33 +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
|
2016-09-21 19:25:33 +01:00
|
|
|
o->base.type = &ussl_socket_type;
|
2018-05-31 12:47:26 +01:00
|
|
|
o->sock = sock;
|
2016-09-21 19:25:33 +01:00
|
|
|
|
|
|
|
int ret;
|
|
|
|
mbedtls_ssl_init(&o->ssl);
|
|
|
|
mbedtls_ssl_config_init(&o->conf);
|
|
|
|
mbedtls_x509_crt_init(&o->cacert);
|
2016-09-21 22:17:44 +01:00
|
|
|
mbedtls_x509_crt_init(&o->cert);
|
|
|
|
mbedtls_pk_init(&o->pkey);
|
2016-09-21 19:25:33 +01:00
|
|
|
mbedtls_ctr_drbg_init(&o->ctr_drbg);
|
2017-09-06 08:34:45 +01:00
|
|
|
#ifdef MBEDTLS_DEBUG_C
|
2016-09-21 19:25:33 +01:00
|
|
|
// Debug level (0-4)
|
|
|
|
mbedtls_debug_set_threshold(0);
|
2017-09-06 08:34:45 +01:00
|
|
|
#endif
|
2016-09-21 19:25:33 +01:00
|
|
|
|
|
|
|
mbedtls_entropy_init(&o->entropy);
|
|
|
|
const byte seed[] = "upy";
|
2018-05-31 12:52:29 +01:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&o->ctr_drbg, mbedtls_entropy_func, &o->entropy, seed, sizeof(seed));
|
2016-09-21 19:25:33 +01:00
|
|
|
if (ret != 0) {
|
2017-10-31 04:54:15 +00:00
|
|
|
goto cleanup;
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_config_defaults(&o->conf,
|
2017-07-24 06:07:48 +01:00
|
|
|
args->server_side.u_bool ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT,
|
2016-09-21 19:25:33 +01:00
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
|
if (ret != 0) {
|
2017-10-31 04:54:15 +00:00
|
|
|
goto cleanup;
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_authmode(&o->conf, MBEDTLS_SSL_VERIFY_NONE);
|
|
|
|
mbedtls_ssl_conf_rng(&o->conf, mbedtls_ctr_drbg_random, &o->ctr_drbg);
|
2017-09-06 08:34:45 +01:00
|
|
|
#ifdef MBEDTLS_DEBUG_C
|
2016-09-21 19:25:33 +01:00
|
|
|
mbedtls_ssl_conf_dbg(&o->conf, mbedtls_debug, NULL);
|
2017-09-06 08:34:45 +01:00
|
|
|
#endif
|
2016-09-21 19:25:33 +01:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_setup(&o->ssl, &o->conf);
|
|
|
|
if (ret != 0) {
|
2017-10-31 04:54:15 +00:00
|
|
|
goto cleanup;
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 23:44:23 +01: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 04:54:15 +00:00
|
|
|
goto cleanup;
|
2016-09-22 23:44:23 +01:00
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL);
|
|
|
|
|
2019-10-31 05:22:42 +00:00
|
|
|
if (args->key.u_obj != mp_const_none) {
|
2017-03-25 08:48:44 +00:00
|
|
|
size_t key_len;
|
2016-09-21 22:17:44 +01: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 05:22:42 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; // use general error for all key errors
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 22:17:44 +01:00
|
|
|
|
2017-03-25 08:48:44 +00:00
|
|
|
size_t cert_len;
|
2016-09-21 22:17:44 +01: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 05:22:42 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; // use general error for all cert errors
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 22:17:44 +01:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_conf_own_cert(&o->conf, &o->cert, &o->pkey);
|
2019-10-31 05:22:42 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 22:17:44 +01:00
|
|
|
}
|
|
|
|
|
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 20:48:16 +00: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) {
|
|
|
|
printf("mbedtls_ssl_handshake error: -%x\n", -ret);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
2017-10-31 04:54:15 +00: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 05:22:42 +00:00
|
|
|
} else if (ret == MBEDTLS_ERR_PK_BAD_INPUT_DATA) {
|
|
|
|
mp_raise_ValueError("invalid key");
|
|
|
|
} else if (ret == MBEDTLS_ERR_X509_BAD_INPUT_DATA) {
|
|
|
|
mp_raise_ValueError("invalid cert");
|
2017-10-31 04:54:15 +00:00
|
|
|
} else {
|
|
|
|
mp_raise_OSError(MP_EIO);
|
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
}
|
|
|
|
|
2017-08-15 15:49:11 +01: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-31 02:42:24 +00:00
|
|
|
if (peer_cert == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-08-15 15:49:11 +01: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 19:25:33 +01: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);
|
|
|
|
|
|
|
|
int ret = mbedtls_ssl_read(&o->ssl, buf, size);
|
2017-03-03 04:46:23 +00:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
|
|
// end of stream
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-21 18:30:35 +01: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 20:48:16 +00: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
|
|
|
|
// renegotation.
|
|
|
|
ret = MP_EWOULDBLOCK;
|
2017-07-21 18:30:35 +01:00
|
|
|
}
|
2016-09-21 19:25:33 +01: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);
|
|
|
|
|
|
|
|
int ret = mbedtls_ssl_write(&o->ssl, buf, size);
|
|
|
|
if (ret >= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-21 18:30:35 +01: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 20:48:16 +00: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
|
|
|
|
// renegotation.
|
|
|
|
ret = MP_EWOULDBLOCK;
|
2017-07-21 18:30:35 +01:00
|
|
|
}
|
2016-09-21 19:25:33 +01:00
|
|
|
*errcode = ret;
|
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-10-15 21:46:13 +01:00
|
|
|
STATIC mp_obj_t socket_setblocking(mp_obj_t self_in, mp_obj_t flag_in) {
|
2017-07-21 18:30:35 +01: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 21:46:13 +01:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_setblocking_obj, socket_setblocking);
|
|
|
|
|
2018-03-07 06:48:53 +00: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);
|
2018-07-20 04:05:04 +01:00
|
|
|
if (request == MP_STREAM_CLOSE) {
|
|
|
|
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);
|
2018-03-07 06:48:53 +00:00
|
|
|
}
|
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);
|
2016-09-21 19:25:33 +01: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 21:46:13 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_setblocking), MP_ROM_PTR(&socket_setblocking_obj) },
|
2018-03-07 06:48:53 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
2017-10-27 05:17:35 +01:00
|
|
|
#if MICROPY_PY_USSL_FINALISER
|
2018-03-07 06:48:53 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_stream_close_obj) },
|
2017-10-27 05:17:35 +01:00
|
|
|
#endif
|
2017-08-15 15:49:11 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getpeercert), MP_ROM_PTR(&mod_ssl_getpeercert_obj) },
|
2016-09-21 19:25:33 +01: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 06:48:53 +00:00
|
|
|
.ioctl = socket_ioctl,
|
2016-09-21 19:25:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t ussl_socket_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
// Save on qstr's, reuse same as for module
|
|
|
|
.name = MP_QSTR_ussl,
|
|
|
|
.print = socket_print,
|
|
|
|
.getiter = NULL,
|
|
|
|
.iternext = NULL,
|
|
|
|
.protocol = &ussl_socket_stream_p,
|
|
|
|
.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) {
|
|
|
|
// TODO: Implement more args
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
2019-10-31 05:22:42 +00:00
|
|
|
{ MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
|
|
|
|
{ MP_QSTR_cert, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
|
2016-09-21 19:25:33 +01:00
|
|
|
{ MP_QSTR_server_side, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2019-06-05 06:33:15 +01:00
|
|
|
{ MP_QSTR_server_hostname, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
|
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 20:48:16 +00:00
|
|
|
{ MP_QSTR_do_handshake, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
2016-09-21 19:25:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Check that sock implements stream protocol
|
|
|
|
mp_obj_t sock = pos_args[0];
|
|
|
|
|
2016-09-21 22:17:44 +01:00
|
|
|
struct ssl_args args;
|
2016-09-21 19:25:33 +01: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 22:17:44 +01:00
|
|
|
return MP_OBJ_FROM_PTR(socket_new(sock, &args));
|
2016-09-21 19:25:33 +01: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) },
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_USSL
|