2019-07-19 15:29:57 +02:00
|
|
|
/**************************************************************************/
|
2022-09-07 08:25:47 +02:00
|
|
|
/* tls_context_mbedtls.cpp */
|
2019-07-19 15:29:57 +02: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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
#include "tls_context_mbedtls.h"
|
2019-07-19 15:29:57 +02:00
|
|
|
|
|
|
|
static void my_debug(void *ctx, int level,
|
|
|
|
const char *file, int line,
|
|
|
|
const char *str) {
|
|
|
|
printf("%s:%04d: %s", file, line, str);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
void TLSContextMbedTLS::print_mbedtls_error(int p_ret) {
|
2020-01-13 17:10:18 +01:00
|
|
|
printf("mbedtls error: returned -0x%x\n\n", -p_ret);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:52:24 +02:00
|
|
|
/// CookieContextMbedTLS
|
|
|
|
|
|
|
|
Error CookieContextMbedTLS::setup() {
|
|
|
|
ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This cookie context is already in use");
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_ssl_cookie_init(&cookie_ctx);
|
|
|
|
inited = true;
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
|
2019-08-21 22:52:24 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
clear(); // Never leave unusable resources around.
|
|
|
|
ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_cookie_setup(&cookie_ctx, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
|
if (ret != 0) {
|
|
|
|
clear();
|
|
|
|
ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_cookie_setup returned an error " + itos(ret));
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieContextMbedTLS::clear() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!inited) {
|
2019-08-21 22:52:24 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-08-21 22:52:24 +02:00
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
|
|
|
mbedtls_ssl_cookie_free(&cookie_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieContextMbedTLS::CookieContextMbedTLS() {
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieContextMbedTLS::~CookieContextMbedTLS() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
/// TLSContextMbedTLS
|
2019-08-21 22:52:24 +02:00
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
Error TLSContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {
|
2019-07-19 15:29:57 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
mbedtls_ssl_init(&tls);
|
2019-07-19 15:29:57 +02:00
|
|
|
mbedtls_ssl_config_init(&conf);
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
inited = true;
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
|
2019-07-19 15:29:57 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
clear(); // Never leave unusable resources around.
|
2019-08-21 22:52:24 +02:00
|
|
|
ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
|
2019-07-19 15:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
|
if (ret != 0) {
|
|
|
|
clear();
|
|
|
|
ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));
|
|
|
|
}
|
|
|
|
mbedtls_ssl_conf_authmode(&conf, p_authmode);
|
|
|
|
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
|
mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-01-20 01:51:35 +01:00
|
|
|
Error TLSContextMbedTLS::init_server(int p_transport, Ref<TLSOptions> p_options, Ref<CookieContextMbedTLS> p_cookies) {
|
|
|
|
ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);
|
2019-07-19 15:29:57 +02:00
|
|
|
|
2023-01-20 01:51:35 +01:00
|
|
|
// Check key and certificate(s)
|
|
|
|
pkey = p_options->get_private_key();
|
|
|
|
certs = p_options->get_own_certificate();
|
|
|
|
ERR_FAIL_COND_V(pkey.is_null() || certs.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, MBEDTLS_SSL_VERIFY_NONE); // TODO client auth.
|
2019-07-19 15:29:57 +02:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
|
|
|
|
// Locking key and certificate(s)
|
2023-01-20 01:51:35 +01:00
|
|
|
pkey->lock();
|
|
|
|
certs->lock();
|
2019-07-19 15:29:57 +02:00
|
|
|
|
|
|
|
// Adding key and certificate
|
|
|
|
int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));
|
|
|
|
if (ret != 0) {
|
|
|
|
clear();
|
|
|
|
ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));
|
|
|
|
}
|
|
|
|
// Adding CA chain if available.
|
|
|
|
if (certs->cert.next) {
|
2020-04-02 01:20:12 +02:00
|
|
|
mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, nullptr);
|
2019-07-19 15:29:57 +02:00
|
|
|
}
|
2019-08-21 22:52:24 +02:00
|
|
|
// DTLS Cookies
|
|
|
|
if (p_transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
if (p_cookies.is_null() || !p_cookies->inited) {
|
|
|
|
clear();
|
|
|
|
ERR_FAIL_V(ERR_BUG);
|
|
|
|
}
|
|
|
|
cookies = p_cookies;
|
|
|
|
mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &(cookies->cookie_ctx));
|
|
|
|
}
|
2022-09-07 08:25:47 +02:00
|
|
|
mbedtls_ssl_setup(&tls, &conf);
|
2019-07-19 15:29:57 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2023-01-20 01:51:35 +01:00
|
|
|
Error TLSContextMbedTLS::init_client(int p_transport, const String &p_hostname, Ref<TLSOptions> p_options) {
|
|
|
|
ERR_FAIL_COND_V(p_options.is_null() || p_options->is_server(), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
|
|
|
if (p_options->get_verify_mode() == TLSOptions::TLS_VERIFY_NONE) {
|
|
|
|
authmode = MBEDTLS_SSL_VERIFY_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, authmode);
|
2019-08-22 16:40:43 +02:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
|
|
|
|
2023-01-20 01:51:35 +01:00
|
|
|
if (p_options->get_verify_mode() == TLSOptions::TLS_VERIFY_FULL) {
|
|
|
|
String cn = p_options->get_common_name();
|
|
|
|
if (cn.is_empty()) {
|
|
|
|
cn = p_hostname;
|
|
|
|
}
|
|
|
|
mbedtls_ssl_set_hostname(&tls, cn.utf8().get_data());
|
|
|
|
} else {
|
|
|
|
mbedtls_ssl_set_hostname(&tls, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
X509CertificateMbedTLS *cas = nullptr;
|
2019-07-19 15:29:57 +02:00
|
|
|
|
2023-01-20 01:51:35 +01:00
|
|
|
if (p_options->get_trusted_ca_chain().is_valid()) {
|
2019-07-19 15:29:57 +02:00
|
|
|
// Locking CA certificates
|
2023-01-20 01:51:35 +01:00
|
|
|
certs = p_options->get_trusted_ca_chain();
|
2019-07-19 15:29:57 +02:00
|
|
|
certs->lock();
|
|
|
|
cas = certs.ptr();
|
|
|
|
} else {
|
|
|
|
// Fall back to default certificates (no need to lock those).
|
|
|
|
cas = CryptoMbedTLS::get_default_certificates();
|
2020-04-02 01:20:12 +02:00
|
|
|
if (cas == nullptr) {
|
2019-08-22 16:40:43 +02:00
|
|
|
clear();
|
|
|
|
ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
|
|
|
|
}
|
2019-07-19 15:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set valid CAs
|
2020-04-02 01:20:12 +02:00
|
|
|
mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);
|
2022-09-07 08:25:47 +02:00
|
|
|
mbedtls_ssl_setup(&tls, &conf);
|
2019-07-19 15:29:57 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
void TLSContextMbedTLS::clear() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!inited) {
|
2019-07-19 15:29:57 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-09-07 08:25:47 +02:00
|
|
|
mbedtls_ssl_free(&tls);
|
2019-07-19 15:29:57 +02:00
|
|
|
mbedtls_ssl_config_free(&conf);
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
|
|
|
|
|
|
|
// Unlock and key and certificates
|
2020-05-14 16:41:43 +02:00
|
|
|
if (certs.is_valid()) {
|
2019-07-19 15:29:57 +02:00
|
|
|
certs->unlock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-19 15:29:57 +02:00
|
|
|
certs = Ref<X509Certificate>();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (pkey.is_valid()) {
|
2019-07-19 15:29:57 +02:00
|
|
|
pkey->unlock();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-19 15:29:57 +02:00
|
|
|
pkey = Ref<CryptoKeyMbedTLS>();
|
2019-08-21 22:52:24 +02:00
|
|
|
cookies = Ref<CookieContextMbedTLS>();
|
2019-07-19 15:29:57 +02:00
|
|
|
inited = false;
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
mbedtls_ssl_context *TLSContextMbedTLS::get_context() {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(!inited, nullptr);
|
2022-09-07 08:25:47 +02:00
|
|
|
return &tls;
|
2019-07-19 15:29:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
TLSContextMbedTLS::TLSContextMbedTLS() {
|
2019-07-19 15:29:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
TLSContextMbedTLS::~TLSContextMbedTLS() {
|
2019-07-19 15:29:57 +02:00
|
|
|
clear();
|
|
|
|
}
|