| 1 | /**************************************************************************/ |
| 2 | /* register_types.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "register_types.h" |
| 32 | |
| 33 | #include "crypto_mbedtls.h" |
| 34 | #include "dtls_server_mbedtls.h" |
| 35 | #include "packet_peer_mbed_dtls.h" |
| 36 | #include "stream_peer_mbedtls.h" |
| 37 | |
| 38 | #include "core/config/project_settings.h" |
| 39 | |
| 40 | #if MBEDTLS_VERSION_MAJOR >= 3 |
| 41 | #include <psa/crypto.h> |
| 42 | #endif |
| 43 | |
| 44 | #ifdef TESTS_ENABLED |
| 45 | #include "tests/test_crypto_mbedtls.h" |
| 46 | #endif |
| 47 | |
| 48 | #ifdef GODOT_MBEDTLS_THREADING_ALT |
| 49 | extern "C" { |
| 50 | void godot_mbedtls_mutex_init(mbedtls_threading_mutex_t *p_mutex) { |
| 51 | ERR_FAIL_NULL(p_mutex); |
| 52 | p_mutex->mutex = memnew(Mutex); |
| 53 | } |
| 54 | |
| 55 | void godot_mbedtls_mutex_free(mbedtls_threading_mutex_t *p_mutex) { |
| 56 | ERR_FAIL_NULL(p_mutex); |
| 57 | ERR_FAIL_NULL(p_mutex->mutex); |
| 58 | memdelete((Mutex *)p_mutex->mutex); |
| 59 | } |
| 60 | |
| 61 | int godot_mbedtls_mutex_lock(mbedtls_threading_mutex_t *p_mutex) { |
| 62 | ERR_FAIL_NULL_V(p_mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA); |
| 63 | ERR_FAIL_NULL_V(p_mutex->mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA); |
| 64 | ((Mutex *)p_mutex->mutex)->lock(); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int godot_mbedtls_mutex_unlock(mbedtls_threading_mutex_t *p_mutex) { |
| 69 | ERR_FAIL_NULL_V(p_mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA); |
| 70 | ERR_FAIL_NULL_V(p_mutex->mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA); |
| 71 | ((Mutex *)p_mutex->mutex)->unlock(); |
| 72 | return 0; |
| 73 | } |
| 74 | }; |
| 75 | #endif |
| 76 | |
| 77 | static bool godot_mbedtls_initialized = false; |
| 78 | |
| 79 | void initialize_mbedtls_module(ModuleInitializationLevel p_level) { |
| 80 | if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | GLOBAL_DEF("network/tls/enable_tls_v1.3", true); |
| 85 | |
| 86 | #ifdef GODOT_MBEDTLS_THREADING_ALT |
| 87 | mbedtls_threading_set_alt( |
| 88 | godot_mbedtls_mutex_init, |
| 89 | godot_mbedtls_mutex_free, |
| 90 | godot_mbedtls_mutex_lock, |
| 91 | godot_mbedtls_mutex_unlock); |
| 92 | #endif |
| 93 | |
| 94 | #if MBEDTLS_VERSION_MAJOR >= 3 |
| 95 | int status = psa_crypto_init(); |
| 96 | ERR_FAIL_COND_MSG(status != PSA_SUCCESS, "Failed to initialize psa crypto. The mbedTLS modules will not work."); |
| 97 | #endif |
| 98 | |
| 99 | #ifdef DEBUG_ENABLED |
| 100 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 101 | mbedtls_debug_set_threshold(1); |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | godot_mbedtls_initialized = true; |
| 106 | |
| 107 | CryptoMbedTLS::initialize_crypto(); |
| 108 | StreamPeerMbedTLS::initialize_tls(); |
| 109 | PacketPeerMbedDTLS::initialize_dtls(); |
| 110 | DTLSServerMbedTLS::initialize(); |
| 111 | } |
| 112 | |
| 113 | void uninitialize_mbedtls_module(ModuleInitializationLevel p_level) { |
| 114 | if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | if (!godot_mbedtls_initialized) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | #if MBEDTLS_VERSION_MAJOR >= 3 |
| 123 | mbedtls_psa_crypto_free(); |
| 124 | #endif |
| 125 | |
| 126 | DTLSServerMbedTLS::finalize(); |
| 127 | PacketPeerMbedDTLS::finalize_dtls(); |
| 128 | StreamPeerMbedTLS::finalize_tls(); |
| 129 | CryptoMbedTLS::finalize_crypto(); |
| 130 | |
| 131 | #ifdef GODOT_MBEDTLS_THREADING_ALT |
| 132 | mbedtls_threading_free_alt(); |
| 133 | #endif |
| 134 | } |