2018-02-09 14:03:34 +00:00
|
|
|
/**************************************************************************/
|
2019-08-28 07:19:52 +00:00
|
|
|
/* stream_peer_mbedtls.cpp */
|
2018-02-09 14:03:34 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2019-07-19 13:29:57 +00:00
|
|
|
#include "stream_peer_mbedtls.h"
|
2018-09-27 08:43:12 +00:00
|
|
|
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-14 12:13:11 +00:00
|
|
|
#include "core/io/stream_peer_tcp.h"
|
2018-09-27 08:43:12 +00:00
|
|
|
|
2018-02-09 14:03:34 +00:00
|
|
|
int StreamPeerMbedTLS::bio_send(void *ctx, const unsigned char *buf, size_t len) {
|
2022-04-05 10:40:26 +00:00
|
|
|
if (buf == nullptr || len == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
StreamPeerMbedTLS *sp = static_cast<StreamPeerMbedTLS *>(ctx);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2023-09-09 15:40:07 +00:00
|
|
|
ERR_FAIL_NULL_V(sp, 0);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
int sent;
|
|
|
|
Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
|
|
|
|
if (err != OK) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
if (sent == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
|
2022-04-05 10:40:26 +00:00
|
|
|
if (buf == nullptr || len == 0) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
StreamPeerMbedTLS *sp = static_cast<StreamPeerMbedTLS *>(ctx);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2023-09-09 15:40:07 +00:00
|
|
|
ERR_FAIL_NULL_V(sp, 0);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
int got;
|
|
|
|
Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
|
|
|
|
if (err != OK) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
if (got == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2018-07-14 20:33:30 +00:00
|
|
|
void StreamPeerMbedTLS::_cleanup() {
|
2022-09-07 06:25:47 +00:00
|
|
|
tls_ctx->clear();
|
2018-07-14 20:33:30 +00:00
|
|
|
base = Ref<StreamPeer>();
|
|
|
|
status = STATUS_DISCONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error StreamPeerMbedTLS::_do_handshake() {
|
2023-01-28 08:39:31 +00:00
|
|
|
int ret = mbedtls_ssl_handshake(tls_ctx->get_context());
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
// Handshake is still in progress, will retry via poll later.
|
|
|
|
return OK;
|
|
|
|
} else if (ret != 0) {
|
|
|
|
// An error occurred.
|
|
|
|
ERR_PRINT("TLS handshake error: " + itos(ret));
|
|
|
|
TLSContextMbedTLS::print_mbedtls_error(ret);
|
|
|
|
disconnect_from_stream();
|
|
|
|
status = STATUS_ERROR;
|
|
|
|
return FAILED;
|
2018-07-14 20:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, const String &p_common_name, Ref<TLSOptions> p_options) {
|
2019-08-21 21:59:25 +00:00
|
|
|
ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error err = tls_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, p_common_name, p_options.is_valid() ? p_options : TLSOptions::client());
|
2019-07-19 13:29:57 +00:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
base = p_base;
|
2022-09-07 06:25:47 +00:00
|
|
|
mbedtls_ssl_set_bio(tls_ctx->get_context(), this, bio_send, bio_recv, nullptr);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2018-07-14 20:33:30 +00:00
|
|
|
status = STATUS_HANDSHAKING;
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2019-12-10 04:13:02 +00:00
|
|
|
if (_do_handshake() != OK) {
|
2018-07-14 20:33:30 +00:00
|
|
|
status = STATUS_ERROR_HOSTNAME_MISMATCH;
|
|
|
|
return FAILED;
|
|
|
|
}
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<TLSOptions> p_options) {
|
2019-08-21 21:59:25 +00:00
|
|
|
ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
|
2023-01-20 00:51:35 +00:00
|
|
|
ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);
|
2019-08-21 21:59:25 +00:00
|
|
|
|
2023-01-20 00:51:35 +00:00
|
|
|
Error err = tls_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, p_options);
|
2019-07-19 13:29:57 +00:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
|
|
|
|
base = p_base;
|
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
mbedtls_ssl_set_bio(tls_ctx->get_context(), this, bio_send, bio_recv, nullptr);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2019-07-19 13:29:57 +00:00
|
|
|
status = STATUS_HANDSHAKING;
|
|
|
|
|
2019-12-10 04:13:02 +00:00
|
|
|
if (_do_handshake() != OK) {
|
2019-07-19 13:29:57 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = STATUS_CONNECTED;
|
2018-07-14 20:33:30 +00:00
|
|
|
return OK;
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-02-09 14:03:34 +00:00
|
|
|
Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
Error err;
|
|
|
|
int sent = 0;
|
|
|
|
|
|
|
|
while (p_bytes > 0) {
|
|
|
|
err = put_partial_data(p_data, p_bytes, sent);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_data += sent;
|
|
|
|
p_bytes -= sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
r_sent = 0;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_bytes == 0) {
|
2018-02-09 14:03:34 +00:00
|
|
|
return OK;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
int ret = mbedtls_ssl_write(tls_ctx->get_context(), p_data, p_bytes);
|
2018-02-09 14:03:34 +00:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
2018-09-14 12:13:11 +00:00
|
|
|
// Non blocking IO
|
|
|
|
ret = 0;
|
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
|
|
// Clean close
|
|
|
|
disconnect_from_stream();
|
|
|
|
return ERR_FILE_EOF;
|
2018-02-09 14:03:34 +00:00
|
|
|
} else if (ret <= 0) {
|
2022-09-07 06:25:47 +00:00
|
|
|
TLSContextMbedTLS::print_mbedtls_error(ret);
|
2018-02-09 14:03:34 +00:00
|
|
|
disconnect_from_stream();
|
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_sent = ret;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error StreamPeerMbedTLS::get_data(uint8_t *p_buffer, int p_bytes) {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
Error err;
|
|
|
|
|
|
|
|
int got = 0;
|
|
|
|
while (p_bytes > 0) {
|
|
|
|
err = get_partial_data(p_buffer, p_bytes, got);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_buffer += got;
|
|
|
|
p_bytes -= got;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
|
|
|
r_received = 0;
|
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
int ret = mbedtls_ssl_read(tls_ctx->get_context(), p_buffer, p_bytes);
|
2018-02-09 14:03:34 +00:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
ret = 0; // non blocking io
|
2018-09-14 12:13:11 +00:00
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
|
|
// Clean close
|
|
|
|
disconnect_from_stream();
|
|
|
|
return ERR_FILE_EOF;
|
2018-02-09 14:03:34 +00:00
|
|
|
} else if (ret <= 0) {
|
2022-09-07 06:25:47 +00:00
|
|
|
TLSContextMbedTLS::print_mbedtls_error(ret);
|
2018-02-09 14:03:34 +00:00
|
|
|
disconnect_from_stream();
|
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_received = ret;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamPeerMbedTLS::poll() {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND(status != STATUS_CONNECTED && status != STATUS_HANDSHAKING);
|
2018-02-09 14:03:34 +00:00
|
|
|
ERR_FAIL_COND(!base.is_valid());
|
|
|
|
|
2018-07-14 20:33:30 +00:00
|
|
|
if (status == STATUS_HANDSHAKING) {
|
|
|
|
_do_handshake();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-01 14:29:38 +00:00
|
|
|
// We could pass nullptr as second parameter, but some behavior sanitizers don't seem to like that.
|
2019-02-20 00:34:10 +00:00
|
|
|
// Passing a 1 byte buffer to workaround it.
|
|
|
|
uint8_t byte;
|
2022-09-07 06:25:47 +00:00
|
|
|
int ret = mbedtls_ssl_read(tls_ctx->get_context(), &byte, 0);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2018-09-14 12:13:11 +00:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
// Nothing to read/write (non blocking IO)
|
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
|
|
|
// Clean close (disconnect)
|
|
|
|
disconnect_from_stream();
|
|
|
|
return;
|
|
|
|
} else if (ret < 0) {
|
2022-09-07 06:25:47 +00:00
|
|
|
TLSContextMbedTLS::print_mbedtls_error(ret);
|
2018-02-09 14:03:34 +00:00
|
|
|
disconnect_from_stream();
|
2018-09-14 12:13:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<StreamPeerTCP> tcp = base;
|
2018-09-27 08:43:12 +00:00
|
|
|
if (tcp.is_valid() && tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
2018-09-14 12:13:11 +00:00
|
|
|
disconnect_from_stream();
|
|
|
|
return;
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int StreamPeerMbedTLS::get_available_bytes() const {
|
2018-07-14 20:33:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
return mbedtls_ssl_get_bytes_avail(&(tls_ctx->tls));
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-02-09 14:03:34 +00:00
|
|
|
StreamPeerMbedTLS::StreamPeerMbedTLS() {
|
2022-09-07 06:25:47 +00:00
|
|
|
tls_ctx.instantiate();
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamPeerMbedTLS::~StreamPeerMbedTLS() {
|
|
|
|
disconnect_from_stream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamPeerMbedTLS::disconnect_from_stream() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING) {
|
2018-02-09 14:03:34 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-02-09 14:03:34 +00:00
|
|
|
|
2018-09-14 12:13:11 +00:00
|
|
|
Ref<StreamPeerTCP> tcp = base;
|
2018-09-27 08:43:12 +00:00
|
|
|
if (tcp.is_valid() && tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
|
2019-02-13 08:23:29 +00:00
|
|
|
// We are still connected on the socket, try to send close notify.
|
2022-09-07 06:25:47 +00:00
|
|
|
mbedtls_ssl_close_notify(tls_ctx->get_context());
|
2018-09-14 12:13:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 20:33:30 +00:00
|
|
|
_cleanup();
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:07:44 +00:00
|
|
|
Ref<StreamPeer> StreamPeerMbedTLS::get_stream() const {
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2024-04-22 14:34:44 +00:00
|
|
|
StreamPeerTLS *StreamPeerMbedTLS::_create_func(bool p_notify_postinitialize) {
|
|
|
|
return static_cast<StreamPeerTLS *>(ClassDB::creator<StreamPeerMbedTLS>(p_notify_postinitialize));
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
void StreamPeerMbedTLS::initialize_tls() {
|
2018-02-09 14:03:34 +00:00
|
|
|
_create = _create_func;
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
void StreamPeerMbedTLS::finalize_tls() {
|
2020-04-01 23:20:12 +00:00
|
|
|
_create = nullptr;
|
2018-02-09 14:03:34 +00:00
|
|
|
}
|