stream_peer_mbed_tls.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*************************************************************************/
  2. /* stream_peer_openssl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "stream_peer_mbed_tls.h"
  31. #include "mbedtls/platform_util.h"
  32. #include "os/file_access.h"
  33. static void my_debug(void *ctx, int level,
  34. const char *file, int line,
  35. const char *str) {
  36. printf("%s:%04d: %s", file, line, str);
  37. fflush(stdout);
  38. }
  39. void _print_error(int ret) {
  40. printf("mbedtls error: returned -0x%x\n\n", -ret);
  41. fflush(stdout);
  42. }
  43. int StreamPeerMbedTLS::bio_send(void *ctx, const unsigned char *buf, size_t len) {
  44. if (buf == NULL || len <= 0) return 0;
  45. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  46. ERR_FAIL_COND_V(sp == NULL, 0);
  47. int sent;
  48. Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
  49. if (err != OK) {
  50. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  51. }
  52. if (sent == 0) {
  53. return MBEDTLS_ERR_SSL_WANT_WRITE;
  54. }
  55. return sent;
  56. }
  57. int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  58. if (buf == NULL || len <= 0) return 0;
  59. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  60. ERR_FAIL_COND_V(sp == NULL, 0);
  61. int got;
  62. Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
  63. if (err != OK) {
  64. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  65. }
  66. if (got == 0) {
  67. return MBEDTLS_ERR_SSL_WANT_READ;
  68. }
  69. return got;
  70. }
  71. void StreamPeerMbedTLS::_cleanup() {
  72. mbedtls_ssl_free(&ssl);
  73. mbedtls_ssl_config_free(&conf);
  74. mbedtls_ctr_drbg_free(&ctr_drbg);
  75. mbedtls_entropy_free(&entropy);
  76. base = Ref<StreamPeer>();
  77. status = STATUS_DISCONNECTED;
  78. }
  79. Error StreamPeerMbedTLS::_do_handshake() {
  80. int ret = 0;
  81. while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
  82. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  83. ERR_PRINTS("TLS handshake error: " + itos(ret));
  84. _print_error(ret);
  85. disconnect_from_stream();
  86. status = STATUS_ERROR;
  87. return FAILED;
  88. } else if (!blocking_handshake) {
  89. // Will retry via poll later
  90. return OK;
  91. }
  92. }
  93. status = STATUS_CONNECTED;
  94. return OK;
  95. }
  96. Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname) {
  97. base = p_base;
  98. int ret = 0;
  99. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  100. mbedtls_ssl_init(&ssl);
  101. mbedtls_ssl_config_init(&conf);
  102. mbedtls_ctr_drbg_init(&ctr_drbg);
  103. mbedtls_entropy_init(&entropy);
  104. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
  105. if (ret != 0) {
  106. ERR_PRINTS(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
  107. _cleanup();
  108. return FAILED;
  109. }
  110. mbedtls_ssl_config_defaults(&conf,
  111. MBEDTLS_SSL_IS_CLIENT,
  112. MBEDTLS_SSL_TRANSPORT_STREAM,
  113. MBEDTLS_SSL_PRESET_DEFAULT);
  114. mbedtls_ssl_conf_authmode(&conf, authmode);
  115. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  116. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  117. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  118. mbedtls_ssl_setup(&ssl, &conf);
  119. mbedtls_ssl_set_hostname(&ssl, p_for_hostname.utf8().get_data());
  120. mbedtls_ssl_set_bio(&ssl, this, bio_send, bio_recv, NULL);
  121. status = STATUS_HANDSHAKING;
  122. if ((ret = _do_handshake()) != OK) {
  123. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  124. return FAILED;
  125. }
  126. return OK;
  127. }
  128. Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base) {
  129. return OK;
  130. }
  131. Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
  132. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  133. Error err;
  134. int sent = 0;
  135. while (p_bytes > 0) {
  136. err = put_partial_data(p_data, p_bytes, sent);
  137. if (err != OK) {
  138. return err;
  139. }
  140. p_data += sent;
  141. p_bytes -= sent;
  142. }
  143. return OK;
  144. }
  145. Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  146. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  147. r_sent = 0;
  148. if (p_bytes == 0)
  149. return OK;
  150. int ret = mbedtls_ssl_write(&ssl, p_data, p_bytes);
  151. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  152. ret = 0; // non blocking io
  153. } else if (ret <= 0) {
  154. _print_error(ret);
  155. disconnect_from_stream();
  156. return ERR_CONNECTION_ERROR;
  157. }
  158. r_sent = ret;
  159. return OK;
  160. }
  161. Error StreamPeerMbedTLS::get_data(uint8_t *p_buffer, int p_bytes) {
  162. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  163. Error err;
  164. int got = 0;
  165. while (p_bytes > 0) {
  166. err = get_partial_data(p_buffer, p_bytes, got);
  167. if (err != OK) {
  168. return err;
  169. }
  170. p_buffer += got;
  171. p_bytes -= got;
  172. }
  173. return OK;
  174. }
  175. Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  176. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  177. r_received = 0;
  178. int ret = mbedtls_ssl_read(&ssl, p_buffer, p_bytes);
  179. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  180. ret = 0; // non blocking io
  181. } else if (ret <= 0) {
  182. _print_error(ret);
  183. disconnect_from_stream();
  184. return ERR_CONNECTION_ERROR;
  185. }
  186. r_received = ret;
  187. return OK;
  188. }
  189. void StreamPeerMbedTLS::poll() {
  190. ERR_FAIL_COND(status != STATUS_CONNECTED && status != STATUS_HANDSHAKING);
  191. ERR_FAIL_COND(!base.is_valid());
  192. if (status == STATUS_HANDSHAKING) {
  193. _do_handshake();
  194. return;
  195. }
  196. int ret = mbedtls_ssl_read(&ssl, NULL, 0);
  197. if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  198. _print_error(ret);
  199. disconnect_from_stream();
  200. }
  201. }
  202. int StreamPeerMbedTLS::get_available_bytes() const {
  203. ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
  204. return mbedtls_ssl_get_bytes_avail(&ssl);
  205. }
  206. StreamPeerMbedTLS::StreamPeerMbedTLS() {
  207. status = STATUS_DISCONNECTED;
  208. }
  209. StreamPeerMbedTLS::~StreamPeerMbedTLS() {
  210. disconnect_from_stream();
  211. }
  212. void StreamPeerMbedTLS::disconnect_from_stream() {
  213. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING)
  214. return;
  215. _cleanup();
  216. }
  217. StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
  218. return status;
  219. }
  220. StreamPeerSSL *StreamPeerMbedTLS::_create_func() {
  221. return memnew(StreamPeerMbedTLS);
  222. }
  223. mbedtls_x509_crt StreamPeerMbedTLS::cacert;
  224. void StreamPeerMbedTLS::_load_certs(const PoolByteArray &p_array) {
  225. int arr_len = p_array.size();
  226. PoolByteArray::Read r = p_array.read();
  227. int err = mbedtls_x509_crt_parse(&cacert, &r[0], arr_len);
  228. if (err != 0) {
  229. WARN_PRINTS("Error parsing some certificates: " + itos(err));
  230. }
  231. }
  232. void StreamPeerMbedTLS::initialize_ssl() {
  233. _create = _create_func;
  234. load_certs_func = _load_certs;
  235. mbedtls_x509_crt_init(&cacert);
  236. #ifdef DEBUG_ENABLED
  237. mbedtls_debug_set_threshold(1);
  238. #endif
  239. PoolByteArray cert_array = StreamPeerSSL::get_project_cert_array();
  240. if (cert_array.size() > 0)
  241. _load_certs(cert_array);
  242. available = true;
  243. }
  244. void StreamPeerMbedTLS::finalize_ssl() {
  245. mbedtls_x509_crt_free(&cacert);
  246. }