stream_peer_mbedtls.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**************************************************************************/
  2. /* stream_peer_mbedtls.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_mbedtls.h"
  31. #include "core/io/stream_peer_tcp.h"
  32. int StreamPeerMbedTLS::bio_send(void *ctx, const unsigned char *buf, size_t len) {
  33. if (buf == nullptr || len == 0) {
  34. return 0;
  35. }
  36. StreamPeerMbedTLS *sp = static_cast<StreamPeerMbedTLS *>(ctx);
  37. ERR_FAIL_NULL_V(sp, 0);
  38. int sent;
  39. Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
  40. if (err != OK) {
  41. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  42. }
  43. if (sent == 0) {
  44. return MBEDTLS_ERR_SSL_WANT_WRITE;
  45. }
  46. return sent;
  47. }
  48. int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  49. if (buf == nullptr || len == 0) {
  50. return 0;
  51. }
  52. StreamPeerMbedTLS *sp = static_cast<StreamPeerMbedTLS *>(ctx);
  53. ERR_FAIL_NULL_V(sp, 0);
  54. int got;
  55. Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
  56. if (err != OK) {
  57. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  58. }
  59. if (got == 0) {
  60. return MBEDTLS_ERR_SSL_WANT_READ;
  61. }
  62. return got;
  63. }
  64. void StreamPeerMbedTLS::_cleanup() {
  65. tls_ctx->clear();
  66. base = Ref<StreamPeer>();
  67. status = STATUS_DISCONNECTED;
  68. }
  69. Error StreamPeerMbedTLS::_do_handshake() {
  70. int ret = mbedtls_ssl_handshake(tls_ctx->get_context());
  71. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  72. // Handshake is still in progress, will retry via poll later.
  73. return OK;
  74. } else if (ret != 0) {
  75. // An error occurred.
  76. ERR_PRINT("TLS handshake error: " + itos(ret));
  77. TLSContextMbedTLS::print_mbedtls_error(ret);
  78. disconnect_from_stream();
  79. status = STATUS_ERROR;
  80. return FAILED;
  81. }
  82. status = STATUS_CONNECTED;
  83. return OK;
  84. }
  85. Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, const String &p_common_name, Ref<TLSOptions> p_options) {
  86. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  87. Error err = tls_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, p_common_name, p_options.is_valid() ? p_options : TLSOptions::client());
  88. ERR_FAIL_COND_V(err != OK, err);
  89. base = p_base;
  90. mbedtls_ssl_set_bio(tls_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  91. status = STATUS_HANDSHAKING;
  92. if (_do_handshake() != OK) {
  93. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  94. return FAILED;
  95. }
  96. return OK;
  97. }
  98. Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<TLSOptions> p_options) {
  99. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  100. ERR_FAIL_COND_V(p_options.is_null() || !p_options->is_server(), ERR_INVALID_PARAMETER);
  101. Error err = tls_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, p_options);
  102. ERR_FAIL_COND_V(err != OK, err);
  103. base = p_base;
  104. mbedtls_ssl_set_bio(tls_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  105. status = STATUS_HANDSHAKING;
  106. if (_do_handshake() != OK) {
  107. return FAILED;
  108. }
  109. status = STATUS_CONNECTED;
  110. return OK;
  111. }
  112. Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
  113. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  114. Error err;
  115. int sent = 0;
  116. while (p_bytes > 0) {
  117. err = put_partial_data(p_data, p_bytes, sent);
  118. if (err != OK) {
  119. return err;
  120. }
  121. p_data += sent;
  122. p_bytes -= sent;
  123. }
  124. return OK;
  125. }
  126. Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  127. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  128. r_sent = 0;
  129. if (p_bytes == 0) {
  130. return OK;
  131. }
  132. do {
  133. int ret = mbedtls_ssl_write(tls_ctx->get_context(), &p_data[r_sent], p_bytes - r_sent);
  134. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  135. // Non blocking IO.
  136. break;
  137. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  138. // Clean close
  139. disconnect_from_stream();
  140. return ERR_FILE_EOF;
  141. } else if (ret <= 0) {
  142. TLSContextMbedTLS::print_mbedtls_error(ret);
  143. disconnect_from_stream();
  144. return ERR_CONNECTION_ERROR;
  145. }
  146. r_sent += ret;
  147. } while (r_sent < p_bytes);
  148. return OK;
  149. }
  150. Error StreamPeerMbedTLS::get_data(uint8_t *p_buffer, int p_bytes) {
  151. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  152. Error err;
  153. int got = 0;
  154. while (p_bytes > 0) {
  155. err = get_partial_data(p_buffer, p_bytes, got);
  156. if (err != OK) {
  157. return err;
  158. }
  159. p_buffer += got;
  160. p_bytes -= got;
  161. }
  162. return OK;
  163. }
  164. Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  165. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  166. r_received = 0;
  167. do {
  168. int ret = mbedtls_ssl_read(tls_ctx->get_context(), &p_buffer[r_received], p_bytes - r_received);
  169. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  170. // Non blocking IO.
  171. break;
  172. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  173. // Clean close
  174. disconnect_from_stream();
  175. return ERR_FILE_EOF;
  176. } else if (ret <= 0) {
  177. TLSContextMbedTLS::print_mbedtls_error(ret);
  178. disconnect_from_stream();
  179. return ERR_CONNECTION_ERROR;
  180. }
  181. r_received += ret;
  182. } while (r_received < p_bytes);
  183. return OK;
  184. }
  185. void StreamPeerMbedTLS::poll() {
  186. ERR_FAIL_COND(status != STATUS_CONNECTED && status != STATUS_HANDSHAKING);
  187. ERR_FAIL_COND(base.is_null());
  188. if (status == STATUS_HANDSHAKING) {
  189. _do_handshake();
  190. return;
  191. }
  192. // We could pass nullptr as second parameter, but some behavior sanitizers don't seem to like that.
  193. // Passing a 1 byte buffer to workaround it.
  194. uint8_t byte;
  195. int ret = mbedtls_ssl_read(tls_ctx->get_context(), &byte, 0);
  196. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  197. // Nothing to read/write (non blocking IO)
  198. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  199. // Clean close (disconnect)
  200. disconnect_from_stream();
  201. return;
  202. } else if (ret < 0) {
  203. TLSContextMbedTLS::print_mbedtls_error(ret);
  204. disconnect_from_stream();
  205. return;
  206. }
  207. Ref<StreamPeerTCP> tcp = base;
  208. if (tcp.is_valid() && tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  209. disconnect_from_stream();
  210. return;
  211. }
  212. }
  213. int StreamPeerMbedTLS::get_available_bytes() const {
  214. ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
  215. return mbedtls_ssl_get_bytes_avail(&(tls_ctx->tls));
  216. }
  217. StreamPeerMbedTLS::StreamPeerMbedTLS() {
  218. tls_ctx.instantiate();
  219. }
  220. StreamPeerMbedTLS::~StreamPeerMbedTLS() {
  221. disconnect_from_stream();
  222. }
  223. void StreamPeerMbedTLS::disconnect_from_stream() {
  224. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING) {
  225. return;
  226. }
  227. Ref<StreamPeerTCP> tcp = base;
  228. if (tcp.is_valid() && tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  229. // We are still connected on the socket, try to send close notify.
  230. mbedtls_ssl_close_notify(tls_ctx->get_context());
  231. }
  232. _cleanup();
  233. }
  234. StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
  235. return status;
  236. }
  237. Ref<StreamPeer> StreamPeerMbedTLS::get_stream() const {
  238. return base;
  239. }
  240. StreamPeerTLS *StreamPeerMbedTLS::_create_func(bool p_notify_postinitialize) {
  241. return static_cast<StreamPeerTLS *>(ClassDB::creator<StreamPeerMbedTLS>(p_notify_postinitialize));
  242. }
  243. void StreamPeerMbedTLS::initialize_tls() {
  244. _create = _create_func;
  245. }
  246. void StreamPeerMbedTLS::finalize_tls() {
  247. _create = nullptr;
  248. }