ssl_context_mbedtls.cpp 5.5 KB

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