ssl_context_mbedtls.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* ssl_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 "ssl_context_mbedtls.h"
  31. #include "core/project_settings.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif
  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 SSLContextMbedTLS::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. inited = false;
  74. }
  75. CookieContextMbedTLS::~CookieContextMbedTLS() {
  76. clear();
  77. }
  78. /// SSLContextMbedTLS
  79. Error SSLContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {
  80. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");
  81. mbedtls_ssl_init(&ssl);
  82. mbedtls_ssl_config_init(&conf);
  83. mbedtls_ctr_drbg_init(&ctr_drbg);
  84. mbedtls_entropy_init(&entropy);
  85. inited = true;
  86. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  87. if (ret != 0) {
  88. clear(); // Never leave unusable resources around.
  89. ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
  90. }
  91. ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);
  92. if (ret != 0) {
  93. clear();
  94. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));
  95. }
  96. mbedtls_ssl_conf_authmode(&conf, p_authmode);
  97. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  98. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  99. return OK;
  100. }
  101. Error SSLContextMbedTLS::init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert, Ref<CookieContextMbedTLS> p_cookies) {
  102. ERR_FAIL_COND_V(!p_pkey.is_valid(), ERR_INVALID_PARAMETER);
  103. ERR_FAIL_COND_V(!p_cert.is_valid(), ERR_INVALID_PARAMETER);
  104. Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, p_authmode);
  105. ERR_FAIL_COND_V(err != OK, err);
  106. // Locking key and certificate(s)
  107. pkey = p_pkey;
  108. certs = p_cert;
  109. if (pkey.is_valid()) {
  110. pkey->lock();
  111. }
  112. if (certs.is_valid()) {
  113. certs->lock();
  114. }
  115. // Adding key and certificate
  116. int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));
  117. if (ret != 0) {
  118. clear();
  119. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));
  120. }
  121. // Adding CA chain if available.
  122. if (certs->cert.next) {
  123. mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, nullptr);
  124. }
  125. // DTLS Cookies
  126. if (p_transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  127. if (p_cookies.is_null() || !p_cookies->inited) {
  128. clear();
  129. ERR_FAIL_V(ERR_BUG);
  130. }
  131. cookies = p_cookies;
  132. mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &(cookies->cookie_ctx));
  133. }
  134. #if MBEDTLS_VERSION_MAJOR >= 3
  135. #ifdef TOOLS_ENABLED
  136. if (EditorSettings::get_singleton()) {
  137. if (!EditorSettings::get_singleton()->get_setting("network/ssl/enable_tls_v1.3").operator bool()) {
  138. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  139. }
  140. } else
  141. #endif
  142. {
  143. if (!GLOBAL_GET("network/ssl/enable_tls_v1.3").operator bool()) {
  144. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  145. }
  146. }
  147. #endif
  148. mbedtls_ssl_setup(&ssl, &conf);
  149. return OK;
  150. }
  151. Error SSLContextMbedTLS::init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas) {
  152. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode);
  153. ERR_FAIL_COND_V(err != OK, err);
  154. X509CertificateMbedTLS *cas = nullptr;
  155. if (p_valid_cas.is_valid()) {
  156. // Locking CA certificates
  157. certs = p_valid_cas;
  158. certs->lock();
  159. cas = certs.ptr();
  160. } else {
  161. // Fall back to default certificates (no need to lock those).
  162. cas = CryptoMbedTLS::get_default_certificates();
  163. if (cas == nullptr) {
  164. clear();
  165. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
  166. }
  167. }
  168. // Set valid CAs
  169. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);
  170. #if MBEDTLS_VERSION_MAJOR >= 3
  171. #ifdef TOOLS_ENABLED
  172. if (EditorSettings::get_singleton()) {
  173. if (!EditorSettings::get_singleton()->get_setting("network/ssl/enable_tls_v1.3").operator bool()) {
  174. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  175. }
  176. } else
  177. #endif
  178. {
  179. if (!GLOBAL_GET("network/ssl/enable_tls_v1.3").operator bool()) {
  180. mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
  181. }
  182. }
  183. #endif
  184. mbedtls_ssl_setup(&ssl, &conf);
  185. return OK;
  186. }
  187. void SSLContextMbedTLS::clear() {
  188. if (!inited) {
  189. return;
  190. }
  191. mbedtls_ssl_free(&ssl);
  192. mbedtls_ssl_config_free(&conf);
  193. mbedtls_ctr_drbg_free(&ctr_drbg);
  194. mbedtls_entropy_free(&entropy);
  195. // Unlock and key and certificates
  196. if (certs.is_valid()) {
  197. certs->unlock();
  198. }
  199. certs = Ref<X509Certificate>();
  200. if (pkey.is_valid()) {
  201. pkey->unlock();
  202. }
  203. pkey = Ref<CryptoKeyMbedTLS>();
  204. cookies = Ref<CookieContextMbedTLS>();
  205. inited = false;
  206. }
  207. mbedtls_ssl_context *SSLContextMbedTLS::get_context() {
  208. ERR_FAIL_COND_V(!inited, nullptr);
  209. return &ssl;
  210. }
  211. SSLContextMbedTLS::SSLContextMbedTLS() {
  212. inited = false;
  213. }
  214. SSLContextMbedTLS::~SSLContextMbedTLS() {
  215. clear();
  216. }