ssl_context_mbedtls.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. X509CertificateMbedTLS *cas = NULL;
  86. if (p_valid_cas.is_valid()) {
  87. // Locking CA certificates
  88. certs = p_valid_cas;
  89. certs->lock();
  90. cas = certs.ptr();
  91. } else {
  92. // Fall back to default certificates (no need to lock those).
  93. cas = CryptoMbedTLS::get_default_certificates();
  94. ERR_FAIL_COND_V(cas == NULL, ERR_UNCONFIGURED);
  95. }
  96. Error err = _setup(MBEDTLS_SSL_IS_CLIENT, p_transport, p_authmode);
  97. ERR_FAIL_COND_V(err != OK, err);
  98. // Set valid CAs
  99. mbedtls_ssl_conf_ca_chain(&conf, &(cas->cert), NULL);
  100. mbedtls_ssl_setup(&ssl, &conf);
  101. return OK;
  102. }
  103. void SSLContextMbedTLS::clear() {
  104. if (!inited)
  105. return;
  106. mbedtls_ssl_free(&ssl);
  107. mbedtls_ssl_config_free(&conf);
  108. mbedtls_ctr_drbg_free(&ctr_drbg);
  109. mbedtls_entropy_free(&entropy);
  110. // Unlock and key and certificates
  111. if (certs.is_valid())
  112. certs->unlock();
  113. certs = Ref<X509Certificate>();
  114. if (pkey.is_valid())
  115. pkey->unlock();
  116. pkey = Ref<CryptoKeyMbedTLS>();
  117. inited = false;
  118. }
  119. mbedtls_ssl_context *SSLContextMbedTLS::get_context() {
  120. ERR_FAIL_COND_V(!inited, NULL);
  121. return &ssl;
  122. }
  123. SSLContextMbedTLS::SSLContextMbedTLS() {
  124. inited = false;
  125. }
  126. SSLContextMbedTLS::~SSLContextMbedTLS() {
  127. clear();
  128. }