Path: blob/master/modules/mbedtls/tls_context_mbedtls.cpp
10277 views
/**************************************************************************/1/* tls_context_mbedtls.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tls_context_mbedtls.h"3132#include "core/config/project_settings.h"3334#ifdef TOOLS_ENABLED35#include "editor/settings/editor_settings.h"36#endif // TOOLS_ENABLED3738static void my_debug(void *ctx, int level,39const char *file, int line,40const char *str) {41printf("%s:%04d: %s", file, line, str);42fflush(stdout);43}4445void TLSContextMbedTLS::print_mbedtls_error(int p_ret) {46printf("mbedtls error: returned -0x%x\n\n", -p_ret);47fflush(stdout);48}4950/// CookieContextMbedTLS5152Error CookieContextMbedTLS::setup() {53ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This cookie context is already in use");5455mbedtls_ctr_drbg_init(&ctr_drbg);56mbedtls_entropy_init(&entropy);57mbedtls_ssl_cookie_init(&cookie_ctx);58inited = true;5960int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);61if (ret != 0) {62clear(); // Never leave unusable resources around.63ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));64}6566ret = mbedtls_ssl_cookie_setup(&cookie_ctx, mbedtls_ctr_drbg_random, &ctr_drbg);67if (ret != 0) {68clear();69ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_cookie_setup returned an error " + itos(ret));70}71return OK;72}7374void CookieContextMbedTLS::clear() {75if (!inited) {76return;77}78mbedtls_ctr_drbg_free(&ctr_drbg);79mbedtls_entropy_free(&entropy);80mbedtls_ssl_cookie_free(&cookie_ctx);81}8283CookieContextMbedTLS::CookieContextMbedTLS() {84}8586CookieContextMbedTLS::~CookieContextMbedTLS() {87clear();88}8990/// TLSContextMbedTLS9192Error TLSContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {93ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");9495mbedtls_ssl_init(&tls);96mbedtls_ssl_config_init(&conf);97mbedtls_ctr_drbg_init(&ctr_drbg);98mbedtls_entropy_init(&entropy);99inited = true;100101int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);102if (ret != 0) {103clear(); // Never leave unusable resources around.104ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));105}106107ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);108if (ret != 0) {109clear();110ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));111}112mbedtls_ssl_conf_authmode(&conf, p_authmode);113mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);114mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);115return OK;116}117118Error TLSContextMbedTLS::init_server(int p_transport, Ref<TLSOptions> p_options, Ref<CookieContextMbedTLS> p_cookies) {119ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);120121// Check key and certificate(s)122pkey = p_options->get_private_key();123certs = p_options->get_own_certificate();124ERR_FAIL_COND_V(pkey.is_null() || certs.is_null(), ERR_INVALID_PARAMETER);125126Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, MBEDTLS_SSL_VERIFY_NONE); // TODO client auth.127ERR_FAIL_COND_V(err != OK, err);128129// Locking key and certificate(s)130pkey->lock();131certs->lock();132133// Adding key and certificate134int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));135if (ret != 0) {136clear();137ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));138}139// Adding CA chain if available.140if (certs->cert.next) {141mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, nullptr);142}143// DTLS Cookies144if (p_transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {145if (p_cookies.is_null() || !p_cookies->inited) {146clear();147ERR_FAIL_V(ERR_BUG);148}149cookies = p_cookies;150mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &(cookies->cookie_ctx));151}152153#if MBEDTLS_VERSION_MAJOR >= 3154#ifdef TOOLS_ENABLED155if (EditorSettings::get_singleton()) {156if (!EditorSettings::get_singleton()->get_setting("network/tls/enable_tls_v1.3").operator bool()) {157mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);158}159} else160#endif161{162if (!GLOBAL_GET("network/tls/enable_tls_v1.3").operator bool()) {163mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);164}165}166#endif167168mbedtls_ssl_setup(&tls, &conf);169return OK;170}171172Error TLSContextMbedTLS::init_client(int p_transport, const String &p_hostname, Ref<TLSOptions> p_options) {173ERR_FAIL_COND_V(p_options.is_null() || p_options->is_server(), ERR_INVALID_PARAMETER);174175int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;176bool unsafe = p_options->is_unsafe_client();177if (unsafe && p_options->get_trusted_ca_chain().is_null()) {178authmode = MBEDTLS_SSL_VERIFY_NONE;179}180181Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, authmode);182ERR_FAIL_COND_V(err != OK, err);183184if (unsafe) {185// No hostname verification for unsafe clients.186mbedtls_ssl_set_hostname(&tls, nullptr);187} else {188String cn = p_options->get_common_name_override();189if (cn.is_empty()) {190cn = p_hostname;191}192mbedtls_ssl_set_hostname(&tls, cn.utf8().get_data());193}194195X509CertificateMbedTLS *cas = nullptr;196197if (p_options->get_trusted_ca_chain().is_valid()) {198// Locking CA certificates199certs = p_options->get_trusted_ca_chain();200certs->lock();201cas = certs.ptr();202} else {203// Fall back to default certificates (no need to lock those).204cas = CryptoMbedTLS::get_default_certificates();205if (cas == nullptr) {206clear();207ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");208}209}210211#if MBEDTLS_VERSION_MAJOR >= 3212#ifdef TOOLS_ENABLED213if (EditorSettings::get_singleton()) {214if (!EditorSettings::get_singleton()->get_setting("network/tls/enable_tls_v1.3").operator bool()) {215mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);216}217} else218#endif219{220if (!GLOBAL_GET("network/tls/enable_tls_v1.3").operator bool()) {221mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);222}223}224#endif225226// Set valid CAs227mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);228mbedtls_ssl_setup(&tls, &conf);229return OK;230}231232void TLSContextMbedTLS::clear() {233if (!inited) {234return;235}236mbedtls_ssl_free(&tls);237mbedtls_ssl_config_free(&conf);238mbedtls_ctr_drbg_free(&ctr_drbg);239mbedtls_entropy_free(&entropy);240241// Unlock and key and certificates242if (certs.is_valid()) {243certs->unlock();244}245certs = Ref<X509Certificate>();246if (pkey.is_valid()) {247pkey->unlock();248}249pkey = Ref<CryptoKeyMbedTLS>();250cookies = Ref<CookieContextMbedTLS>();251inited = false;252}253254mbedtls_ssl_context *TLSContextMbedTLS::get_context() {255ERR_FAIL_COND_V(!inited, nullptr);256return &tls;257}258259TLSContextMbedTLS::TLSContextMbedTLS() {260}261262TLSContextMbedTLS::~TLSContextMbedTLS() {263clear();264}265266267