tls_context_mbedtls.cpp 8.1 KB

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