stream_peer_mbedtls.cpp 8.8 KB

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