ssl_context_mbedtls.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. Error SSLContextMbedTLS::_setup(int p_endpoint, int p_transport, int p_authmode) {
  42. ERR_FAIL_COND_V_MSG(inited, ERR_ALREADY_IN_USE, "This SSL context is already active");
  43. mbedtls_ssl_init(&ssl);
  44. mbedtls_ssl_config_init(&conf);
  45. mbedtls_ctr_drbg_init(&ctr_drbg);
  46. mbedtls_entropy_init(&entropy);
  47. inited = true;
  48. int ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 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_config_defaults(&conf, p_endpoint, p_transport, MBEDTLS_SSL_PRESET_DEFAULT);
  54. if (ret != 0) {
  55. clear();
  56. ERR_FAIL_V_MSG(FAILED, "mbedtls_ssl_config_defaults returned an error" + itos(ret));
  57. }
  58. mbedtls_ssl_conf_authmode(&conf, p_authmode);
  59. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  60. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  61. return OK;
  62. }
  63. Error SSLContextMbedTLS::init_server(int p_transport, int p_authmode, Ref<CryptoKeyMbedTLS> p_pkey, Ref<X509CertificateMbedTLS> p_cert) {
  64. ERR_FAIL_COND_V(!p_pkey.is_valid(), ERR_INVALID_PARAMETER);
  65. ERR_FAIL_COND_V(!p_cert.is_valid(), ERR_INVALID_PARAMETER);
  66. Error err = _setup(MBEDTLS_SSL_IS_SERVER, p_transport, p_authmode);
  67. ERR_FAIL_COND_V(err != OK, err);
  68. // Locking key and certificate(s)
  69. pkey = p_pkey;
  70. certs = p_cert;
  71. if (pkey.is_valid())
  72. pkey->lock();
  73. if (certs.is_valid())
  74. certs->lock();
  75. // Adding key and certificate
  76. int ret = mbedtls_ssl_conf_own_cert(&conf, &(certs->cert), &(pkey->pkey));
  77. if (ret != 0) {
  78. clear();
  79. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Invalid cert/key combination " + itos(ret));
  80. }
  81. // Adding CA chain if available.
  82. if (certs->cert.next) {
  83. mbedtls_ssl_conf_ca_chain(&conf, certs->cert.next, NULL);
  84. }
  85. mbedtls_ssl_setup(&ssl, &conf);
  86. return OK;
  87. }
  88. Error SSLContextMbedTLS::init_client(int p_transport, int p_authmode, Ref<X509CertificateMbedTLS> p_valid_cas) {
  89. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode);
  90. ERR_FAIL_COND_V(err != OK, err);
  91. X509CertificateMbedTLS *cas = NULL;
  92. if (p_valid_cas.is_valid()) {
  93. // Locking CA certificates
  94. certs = p_valid_cas;
  95. certs->lock();
  96. cas = certs.ptr();
  97. } else {
  98. // Fall back to default certificates (no need to lock those).
  99. cas = CryptoMbedTLS::get_default_certificates();
  100. if (cas == NULL) {
  101. clear();
  102. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "SSL module failed to initialize!");
  103. }
  104. }
  105. // Set valid CAs
  106. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), NULL);
  107. mbedtls_ssl_setup(&ssl, &conf);
  108. return OK;
  109. }
  110. void SSLContextMbedTLS::clear() {
  111. if (!inited)
  112. return;
  113. mbedtls_ssl_free(&ssl);
  114. mbedtls_ssl_config_free(&conf);
  115. mbedtls_ctr_drbg_free(&ctr_drbg);
  116. mbedtls_entropy_free(&entropy);
  117. // Unlock and key and certificates
  118. if (certs.is_valid())
  119. certs->unlock();
  120. certs = Ref<X509Certificate>();
  121. if (pkey.is_valid())
  122. pkey->unlock();
  123. pkey = Ref<CryptoKeyMbedTLS>();
  124. inited = false;
  125. }
  126. mbedtls_ssl_context *SSLContextMbedTLS::get_context() {
  127. ERR_FAIL_COND_V(!inited, NULL);
  128. return &ssl;
  129. }
  130. SSLContextMbedTLS::SSLContextMbedTLS() {
  131. inited = false;
  132. }
  133. SSLContextMbedTLS::~SSLContextMbedTLS() {
  134. clear();
  135. }