ssl_context_mbedtls.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. static void my_debug(void *ctx, int level,
  32. const char *file, int line,
  33. const char *str) {
  34. printf("%s:%04d: %s", file, line, str);
  35. fflush(stdout);
  36. }
  37. void SSLContextMbedTLS::print_mbedtls_error(int p_ret) {
  38. printf("mbedtls error: returned -0x%x\n\n", -p_ret);
  39. fflush(stdout);
  40. }
  41. /// CookieContextMbedTLS
  42. Error CookieContextMbedTLS::setup() {
  43. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This cookie context is already in use");
  44. mbedtls_ctr_drbg_init(&ctr_drbg);
  45. mbedtls_entropy_init(&entropy);
  46. mbedtls_ssl_cookie_init(&cookie_ctx);
  47. inited = true;
  48. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  49. if (ret != 0) {
  50. clear(); // Never leave unusable resources around.
  51. ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
  52. }
  53. ret = mbedtls_ssl_cookie_setup(&cookie_ctx, mbedtls_ctr_drbg_random, &ctr_drbg);
  54. if (ret != 0) {
  55. clear();
  56. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_cookie_setup returned an error " + itos(ret));
  57. }
  58. return OK;
  59. }
  60. void CookieContextMbedTLS::clear() {
  61. if (!inited) {
  62. return;
  63. }
  64. mbedtls_ctr_drbg_free(&ctr_drbg);
  65. mbedtls_entropy_free(&entropy);
  66. mbedtls_ssl_cookie_free(&cookie_ctx);
  67. }
  68. CookieContextMbedTLS::CookieContextMbedTLS() {
  69. inited = false;
  70. }
  71. CookieContextMbedTLS::~CookieContextMbedTLS() {
  72. clear();
  73. }
  74. /// SSLContextMbedTLS
  75. Error SSLContextMbedTLS::_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(&ssl);
  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 SSLContextMbedTLS::init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert, Ref<CookieContextMbedTLS> p_cookies) {
  98. ERR_FAIL_COND_V(!p_pkey.is_valid(), ERR_INVALID_PARAMETER);
  99. ERR_FAIL_COND_V(!p_cert.is_valid(), ERR_INVALID_PARAMETER);
  100. Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, p_authmode);
  101. ERR_FAIL_COND_V(err != OK, err);
  102. // Locking key and certificate(s)
  103. pkey = p_pkey;
  104. certs = p_cert;
  105. if (pkey.is_valid()) {
  106. pkey->lock();
  107. }
  108. if (certs.is_valid()) {
  109. certs->lock();
  110. }
  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. mbedtls_ssl_setup(&ssl, &conf);
  131. return OK;
  132. }
  133. Error SSLContextMbedTLS::init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas) {
  134. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode);
  135. ERR_FAIL_COND_V(err != OK, err);
  136. X509CertificateMbedTLS *cas = nullptr;
  137. if (p_valid_cas.is_valid()) {
  138. // Locking CA certificates
  139. certs = p_valid_cas;
  140. certs->lock();
  141. cas = certs.ptr();
  142. } else {
  143. // Fall back to default certificates (no need to lock those).
  144. cas = CryptoMbedTLS::get_default_certificates();
  145. if (cas == nullptr) {
  146. clear();
  147. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
  148. }
  149. }
  150. // Set valid CAs
  151. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);
  152. mbedtls_ssl_setup(&ssl, &conf);
  153. return OK;
  154. }
  155. void SSLContextMbedTLS::clear() {
  156. if (!inited) {
  157. return;
  158. }
  159. mbedtls_ssl_free(&ssl);
  160. mbedtls_ssl_config_free(&conf);
  161. mbedtls_ctr_drbg_free(&ctr_drbg);
  162. mbedtls_entropy_free(&entropy);
  163. // Unlock and key and certificates
  164. if (certs.is_valid()) {
  165. certs->unlock();
  166. }
  167. certs = Ref<X509Certificate>();
  168. if (pkey.is_valid()) {
  169. pkey->unlock();
  170. }
  171. pkey = Ref<CryptoKeyMbedTLS>();
  172. cookies = Ref<CookieContextMbedTLS>();
  173. inited = false;
  174. }
  175. mbedtls_ssl_context *SSLContextMbedTLS::get_context() {
  176. ERR_FAIL_COND_V(!inited, nullptr);
  177. return &ssl;
  178. }
  179. SSLContextMbedTLS::SSLContextMbedTLS() {
  180. inited = false;
  181. }
  182. SSLContextMbedTLS::~SSLContextMbedTLS() {
  183. clear();
  184. }