register_types.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "register_types.h"
  31. #include "crypto_mbedtls.h"
  32. #include "dtls_server_mbedtls.h"
  33. #include "packet_peer_mbed_dtls.h"
  34. #include "stream_peer_mbedtls.h"
  35. #include "core/config/project_settings.h"
  36. #if MBEDTLS_VERSION_MAJOR >= 3
  37. #include <psa/crypto.h>
  38. #endif
  39. #ifdef TESTS_ENABLED
  40. #include "tests/test_crypto_mbedtls.h"
  41. #endif
  42. #ifdef GODOT_MBEDTLS_THREADING_ALT
  43. extern "C" {
  44. void godot_mbedtls_mutex_init(mbedtls_threading_mutex_t *p_mutex) {
  45. ERR_FAIL_NULL(p_mutex);
  46. p_mutex->mutex = memnew(Mutex);
  47. }
  48. void godot_mbedtls_mutex_free(mbedtls_threading_mutex_t *p_mutex) {
  49. ERR_FAIL_NULL(p_mutex);
  50. ERR_FAIL_NULL(p_mutex->mutex);
  51. memdelete((Mutex *)p_mutex->mutex);
  52. }
  53. int godot_mbedtls_mutex_lock(mbedtls_threading_mutex_t *p_mutex) {
  54. ERR_FAIL_NULL_V(p_mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
  55. ERR_FAIL_NULL_V(p_mutex->mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
  56. ((Mutex *)p_mutex->mutex)->lock();
  57. return 0;
  58. }
  59. int godot_mbedtls_mutex_unlock(mbedtls_threading_mutex_t *p_mutex) {
  60. ERR_FAIL_NULL_V(p_mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
  61. ERR_FAIL_NULL_V(p_mutex->mutex, MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
  62. ((Mutex *)p_mutex->mutex)->unlock();
  63. return 0;
  64. }
  65. };
  66. #endif
  67. static bool godot_mbedtls_initialized = false;
  68. void initialize_mbedtls_module(ModuleInitializationLevel p_level) {
  69. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  70. return;
  71. }
  72. GLOBAL_DEF("network/tls/enable_tls_v1.3", true);
  73. #ifdef GODOT_MBEDTLS_THREADING_ALT
  74. mbedtls_threading_set_alt(
  75. godot_mbedtls_mutex_init,
  76. godot_mbedtls_mutex_free,
  77. godot_mbedtls_mutex_lock,
  78. godot_mbedtls_mutex_unlock);
  79. #endif
  80. #if MBEDTLS_VERSION_MAJOR >= 3
  81. int status = psa_crypto_init();
  82. ERR_FAIL_COND_MSG(status != PSA_SUCCESS, "Failed to initialize psa crypto. The mbedTLS modules will not work.");
  83. #endif
  84. #ifdef DEBUG_ENABLED
  85. if (OS::get_singleton()->is_stdout_verbose()) {
  86. mbedtls_debug_set_threshold(1);
  87. }
  88. #endif
  89. godot_mbedtls_initialized = true;
  90. CryptoMbedTLS::initialize_crypto();
  91. StreamPeerMbedTLS::initialize_tls();
  92. PacketPeerMbedDTLS::initialize_dtls();
  93. DTLSServerMbedTLS::initialize();
  94. }
  95. void uninitialize_mbedtls_module(ModuleInitializationLevel p_level) {
  96. if (p_level != MODULE_INITIALIZATION_LEVEL_CORE) {
  97. return;
  98. }
  99. if (!godot_mbedtls_initialized) {
  100. return;
  101. }
  102. #if MBEDTLS_VERSION_MAJOR >= 3
  103. mbedtls_psa_crypto_free();
  104. #endif
  105. DTLSServerMbedTLS::finalize();
  106. PacketPeerMbedDTLS::finalize_dtls();
  107. StreamPeerMbedTLS::finalize_tls();
  108. CryptoMbedTLS::finalize_crypto();
  109. #ifdef GODOT_MBEDTLS_THREADING_ALT
  110. mbedtls_threading_free_alt();
  111. #endif
  112. }