tls_context_mbedtls.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**************************************************************************/
  2. /* tls_context_mbedtls.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 "tls_context_mbedtls.h"
  31. #include "core/config/project_settings.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif // TOOLS_ENABLED
  35. static void my_debug(void *ctx, int level,
  36. const char *file, int line,
  37. const char *str) {
  38. printf("%s:%04d: %s", file, line, str);
  39. fflush(stdout);
  40. }
  41. void TLSContextMbedTLS::print_mbedtls_error(int p_ret) {
  42. printf("mbedtls error: returned -0x%x\n\n", -p_ret);
  43. fflush(stdout);
  44. }
  45. /// CookieContextMbedTLS
  46. Error CookieContextMbedTLS::setup() {
  47. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This cookie context is already in use");
  48. mbedtls_ctr_drbg_init(&ctr_drbg);
  49. mbedtls_entropy_init(&entropy);
  50. mbedtls_ssl_cookie_init(&cookie_ctx);
  51. inited = true;
  52. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  53. if (ret != 0) {
  54. clear(); // Never leave unusable resources around.
  55. ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
  56. }
  57. ret = mbedtls_ssl_cookie_setup(&cookie_ctx, mbedtls_ctr_drbg_random, &ctr_drbg);
  58. if (ret != 0) {
  59. clear();
  60. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_cookie_setup returned an error " + itos(ret));
  61. }
  62. return OK;
  63. }
  64. void CookieContextMbedTLS::clear() {
  65. if (!inited) {
  66. return;
  67. }
  68. mbedtls_ctr_drbg_free(&ctr_drbg);
  69. mbedtls_entropy_free(&entropy);
  70. mbedtls_ssl_cookie_free(&cookie_ctx);
  71. }
  72. CookieContextMbedTLS::CookieContextMbedTLS() {
  73. }
  74. CookieContextMbedTLS::~CookieContextMbedTLS() {
  75. clear();
  76. }
  77. /// TLSContextMbedTLS
  78. Error TLSContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {
  79. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");
  80. mbedtls_ssl_init(&tls);
  81. mbedtls_ssl_config_init(&conf);
  82. mbedtls_ctr_drbg_init(&ctr_drbg);
  83. mbedtls_entropy_init(&entropy);
  84. inited = true;
  85. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  86. if (ret != 0) {
  87. clear(); // Never leave unusable resources around.
  88. ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
  89. }
  90. ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);
  91. if (ret != 0) {
  92. clear();
  93. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));
  94. }
  95. mbedtls_ssl_conf_authmode(&conf, p_authmode);
  96. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  97. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  98. return OK;
  99. }
  100. Error TLSContextMbedTLS::init_server(int p_transport, Ref<TLSOptions> p_options, Ref<CookieContextMbedTLS> p_cookies) {
  101. ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);
  102. // Check key and certificate(s)
  103. pkey = p_options->get_private_key();
  104. certs = p_options->get_own_certificate();
  105. ERR_FAIL_COND_V(pkey.is_null() || certs.is_null(), ERR_INVALID_PARAMETER);
  106. Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, MBEDTLS_SSL_VERIFY_NONE); // TODO client auth.
  107. ERR_FAIL_COND_V(err != OK, err);
  108. // Locking key and certificate(s)
  109. pkey->lock();
  110. certs->lock();
  111. // Adding key and certificate
  112. int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));
  113. if (ret != 0) {
  114. clear();
  115. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));
  116. }
  117. // Adding CA chain if available.
  118. if (certs->cert.next) {
  119. mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, nullptr);
  120. }
  121. // DTLS Cookies
  122. if (p_transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  123. if (p_cookies.is_null() || !p_cookies->inited) {
  124. clear();
  125. ERR_FAIL_V(ERR_BUG);
  126. }
  127. cookies = p_cookies;
  128. mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &(cookies->cookie_ctx));
  129. }
  130. #if MBEDTLS_VERSION_MAJOR >= 3
  131. #ifdef TOOLS_ENABLED
  132. if (Engine::get_singleton()->is_editor_hint()) {
  133. if (!EditorSettings::get_singleton()->get_setting("network/tls/enable_tls_v1.3").operator bool()) {
  134. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  135. }
  136. } else
  137. #endif
  138. {
  139. if (!GLOBAL_GET("network/tls/enable_tls_v1.3").operator bool()) {
  140. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  141. }
  142. }
  143. #endif
  144. mbedtls_ssl_setup(&tls, &conf);
  145. return OK;
  146. }
  147. Error TLSContextMbedTLS::init_client(int p_transport, const String &p_hostname, Ref<TLSOptions> p_options) {
  148. ERR_FAIL_COND_V(p_options.is_null() || p_options->is_server(), ERR_INVALID_PARAMETER);
  149. int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
  150. bool unsafe = p_options->is_unsafe_client();
  151. if (unsafe && p_options->get_trusted_ca_chain().is_null()) {
  152. authmode = MBEDTLS_SSL_VERIFY_NONE;
  153. }
  154. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, authmode);
  155. ERR_FAIL_COND_V(err != OK, err);
  156. if (unsafe) {
  157. // No hostname verification for unsafe clients.
  158. mbedtls_ssl_set_hostname(&tls, nullptr);
  159. } else {
  160. String cn = p_options->get_common_name_override();
  161. if (cn.is_empty()) {
  162. cn = p_hostname;
  163. }
  164. mbedtls_ssl_set_hostname(&tls, cn.utf8().get_data());
  165. }
  166. X509CertificateMbedTLS *cas = nullptr;
  167. if (p_options->get_trusted_ca_chain().is_valid()) {
  168. // Locking CA certificates
  169. certs = p_options->get_trusted_ca_chain();
  170. certs->lock();
  171. cas = certs.ptr();
  172. } else {
  173. // Fall back to default certificates (no need to lock those).
  174. cas = CryptoMbedTLS::get_default_certificates();
  175. if (cas == nullptr) {
  176. clear();
  177. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
  178. }
  179. }
  180. #if MBEDTLS_VERSION_MAJOR >= 3
  181. #ifdef TOOLS_ENABLED
  182. if (Engine::get_singleton()->is_editor_hint()) {
  183. if (!EditorSettings::get_singleton()->get_setting("network/tls/enable_tls_v1.3").operator bool()) {
  184. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  185. }
  186. } else
  187. #endif
  188. {
  189. if (!GLOBAL_GET("network/tls/enable_tls_v1.3").operator bool()) {
  190. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  191. }
  192. }
  193. #endif
  194. // Set valid CAs
  195. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);
  196. mbedtls_ssl_setup(&tls, &conf);
  197. return OK;
  198. }
  199. void TLSContextMbedTLS::clear() {
  200. if (!inited) {
  201. return;
  202. }
  203. mbedtls_ssl_free(&tls);
  204. mbedtls_ssl_config_free(&conf);
  205. mbedtls_ctr_drbg_free(&ctr_drbg);
  206. mbedtls_entropy_free(&entropy);
  207. // Unlock and key and certificates
  208. if (certs.is_valid()) {
  209. certs->unlock();
  210. }
  211. certs = Ref<X509Certificate>();
  212. if (pkey.is_valid()) {
  213. pkey->unlock();
  214. }
  215. pkey = Ref<CryptoKeyMbedTLS>();
  216. cookies = Ref<CookieContextMbedTLS>();
  217. inited = false;
  218. }
  219. mbedtls_ssl_context *TLSContextMbedTLS::get_context() {
  220. ERR_FAIL_COND_V(!inited, nullptr);
  221. return &tls;
  222. }
  223. TLSContextMbedTLS::TLSContextMbedTLS() {
  224. }
  225. TLSContextMbedTLS::~TLSContextMbedTLS() {
  226. clear();
  227. }