ssl_context_mbedtls.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* ssl_context_mbedtls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. }
  70. CookieContextMbedTLS::~CookieContextMbedTLS() {
  71. clear();
  72. }
  73. /// SSLContextMbedTLS
  74. Error SSLContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {
  75. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");
  76. mbedtls_ssl_init(&ssl);
  77. mbedtls_ssl_config_init(&conf);
  78. mbedtls_ctr_drbg_init(&ctr_drbg);
  79. mbedtls_entropy_init(&entropy);
  80. inited = true;
  81. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, nullptr, 0);
  82. if (ret != 0) {
  83. clear(); // Never leave unusable resources around.
  84. ERR_FAIL_V_MSG(FAILED, "mbedtls_ctr_drbg_seed returned an error " + itos(ret));
  85. }
  86. ret = mbedtls_ssl_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);
  87. if (ret != 0) {
  88. clear();
  89. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));
  90. }
  91. mbedtls_ssl_conf_authmode(&conf, p_authmode);
  92. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  93. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  94. return OK;
  95. }
  96. Error SSLContextMbedTLS::init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert, Ref<CookieContextMbedTLS> p_cookies) {
  97. ERR_FAIL_COND_V(!p_pkey.is_valid(), ERR_INVALID_PARAMETER);
  98. ERR_FAIL_COND_V(!p_cert.is_valid(), ERR_INVALID_PARAMETER);
  99. Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, p_authmode);
  100. ERR_FAIL_COND_V(err != OK, err);
  101. // Locking key and certificate(s)
  102. pkey = p_pkey;
  103. certs = p_cert;
  104. if (pkey.is_valid()) {
  105. pkey->lock();
  106. }
  107. if (certs.is_valid()) {
  108. certs->lock();
  109. }
  110. // Adding key and certificate
  111. int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));
  112. if (ret != 0) {
  113. clear();
  114. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));
  115. }
  116. // Adding CA chain if available.
  117. if (certs->cert.next) {
  118. mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, nullptr);
  119. }
  120. // DTLS Cookies
  121. if (p_transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  122. if (p_cookies.is_null() || !p_cookies->inited) {
  123. clear();
  124. ERR_FAIL_V(ERR_BUG);
  125. }
  126. cookies = p_cookies;
  127. mbedtls_ssl_conf_dtls_cookies(&conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &(cookies->cookie_ctx));
  128. }
  129. mbedtls_ssl_setup(&ssl, &conf);
  130. return OK;
  131. }
  132. Error SSLContextMbedTLS::init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas) {
  133. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode);
  134. ERR_FAIL_COND_V(err != OK, err);
  135. X509CertificateMbedTLS *cas = nullptr;
  136. if (p_valid_cas.is_valid()) {
  137. // Locking CA certificates
  138. certs = p_valid_cas;
  139. certs->lock();
  140. cas = certs.ptr();
  141. } else {
  142. // Fall back to default certificates (no need to lock those).
  143. cas = CryptoMbedTLS::get_default_certificates();
  144. if (cas == nullptr) {
  145. clear();
  146. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
  147. }
  148. }
  149. // Set valid CAs
  150. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), nullptr);
  151. mbedtls_ssl_setup(&ssl, &conf);
  152. return OK;
  153. }
  154. void SSLContextMbedTLS::clear() {
  155. if (!inited) {
  156. return;
  157. }
  158. mbedtls_ssl_free(&ssl);
  159. mbedtls_ssl_config_free(&conf);
  160. mbedtls_ctr_drbg_free(&ctr_drbg);
  161. mbedtls_entropy_free(&entropy);
  162. // Unlock and key and certificates
  163. if (certs.is_valid()) {
  164. certs->unlock();
  165. }
  166. certs = Ref<X509Certificate>();
  167. if (pkey.is_valid()) {
  168. pkey->unlock();
  169. }
  170. pkey = Ref<CryptoKeyMbedTLS>();
  171. cookies = Ref<CookieContextMbedTLS>();
  172. inited = false;
  173. }
  174. mbedtls_ssl_context *SSLContextMbedTLS::get_context() {
  175. ERR_FAIL_COND_V(!inited, nullptr);
  176. return &ssl;
  177. }
  178. SSLContextMbedTLS::SSLContextMbedTLS() {
  179. }
  180. SSLContextMbedTLS::~SSLContextMbedTLS() {
  181. clear();
  182. }