stream_peer_mbedtls.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "core/os/file_access.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 = (StreamPeerMbedTLS *)ctx;
  38. ERR_FAIL_COND_V(sp == nullptr, 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 = (StreamPeerMbedTLS *)ctx;
  54. ERR_FAIL_COND_V(sp == nullptr, 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. ssl_ctx->clear();
  67. base = Ref<StreamPeer>();
  68. status = STATUS_DISCONNECTED;
  69. }
  70. Error StreamPeerMbedTLS::_do_handshake() {
  71. int ret = 0;
  72. while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) {
  73. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  74. // An error occurred.
  75. ERR_PRINT("TLS handshake error: " + itos(ret));
  76. SSLContextMbedTLS::print_mbedtls_error(ret);
  77. disconnect_from_stream();
  78. status = STATUS_ERROR;
  79. return FAILED;
  80. }
  81. // Handshake is still in progress.
  82. if (!blocking_handshake) {
  83. // Will retry via poll later
  84. return OK;
  85. }
  86. }
  87. status = STATUS_CONNECTED;
  88. return OK;
  89. }
  90. Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
  91. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  92. base = p_base;
  93. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  94. Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, authmode, p_ca_certs);
  95. ERR_FAIL_COND_V(err != OK, err);
  96. mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
  97. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  98. status = STATUS_HANDSHAKING;
  99. if (_do_handshake() != OK) {
  100. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  101. return FAILED;
  102. }
  103. return OK;
  104. }
  105. Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain) {
  106. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  107. Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert);
  108. ERR_FAIL_COND_V(err != OK, err);
  109. base = p_base;
  110. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  111. status = STATUS_HANDSHAKING;
  112. if (_do_handshake() != OK) {
  113. return FAILED;
  114. }
  115. status = STATUS_CONNECTED;
  116. return OK;
  117. }
  118. Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
  119. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  120. Error err;
  121. int sent = 0;
  122. while (p_bytes > 0) {
  123. err = put_partial_data(p_data, p_bytes, sent);
  124. if (err != OK) {
  125. return err;
  126. }
  127. p_data += sent;
  128. p_bytes -= sent;
  129. }
  130. return OK;
  131. }
  132. Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  133. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  134. r_sent = 0;
  135. if (p_bytes == 0) {
  136. return OK;
  137. }
  138. int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_data, p_bytes);
  139. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  140. // Non blocking IO
  141. ret = 0;
  142. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  143. // Clean close
  144. disconnect_from_stream();
  145. return ERR_FILE_EOF;
  146. } else if (ret <= 0) {
  147. SSLContextMbedTLS::print_mbedtls_error(ret);
  148. disconnect_from_stream();
  149. return ERR_CONNECTION_ERROR;
  150. }
  151. r_sent = ret;
  152. return OK;
  153. }
  154. Error StreamPeerMbedTLS::get_data(uint8_t *p_buffer, int p_bytes) {
  155. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  156. Error err;
  157. int got = 0;
  158. while (p_bytes > 0) {
  159. err = get_partial_data(p_buffer, p_bytes, got);
  160. if (err != OK) {
  161. return err;
  162. }
  163. p_buffer += got;
  164. p_bytes -= got;
  165. }
  166. return OK;
  167. }
  168. Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  169. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  170. r_received = 0;
  171. int ret = mbedtls_ssl_read(ssl_ctx->get_context(), p_buffer, p_bytes);
  172. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  173. ret = 0; // non blocking io
  174. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  175. // Clean close
  176. disconnect_from_stream();
  177. return ERR_FILE_EOF;
  178. } else if (ret <= 0) {
  179. SSLContextMbedTLS::print_mbedtls_error(ret);
  180. disconnect_from_stream();
  181. return ERR_CONNECTION_ERROR;
  182. }
  183. r_received = ret;
  184. return OK;
  185. }
  186. void StreamPeerMbedTLS::poll() {
  187. ERR_FAIL_COND(status != STATUS_CONNECTED && status != STATUS_HANDSHAKING);
  188. ERR_FAIL_COND(!base.is_valid());
  189. if (status == STATUS_HANDSHAKING) {
  190. _do_handshake();
  191. return;
  192. }
  193. // We could pass NULL as second parameter, but some behaviour sanitizers doesn't seem to like that.
  194. // Passing a 1 byte buffer to workaround it.
  195. uint8_t byte;
  196. int ret = mbedtls_ssl_read(ssl_ctx->get_context(), &byte, 0);
  197. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  198. // Nothing to read/write (non blocking IO)
  199. } else if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  200. // Clean close (disconnect)
  201. disconnect_from_stream();
  202. return;
  203. } else if (ret < 0) {
  204. SSLContextMbedTLS::print_mbedtls_error(ret);
  205. disconnect_from_stream();
  206. return;
  207. }
  208. Ref<StreamPeerTCP> tcp = base;
  209. if (tcp.is_valid() && tcp->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  210. disconnect_from_stream();
  211. return;
  212. }
  213. }
  214. int StreamPeerMbedTLS::get_available_bytes() const {
  215. ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
  216. return mbedtls_ssl_get_bytes_avail(&(ssl_ctx->ssl));
  217. }
  218. StreamPeerMbedTLS::StreamPeerMbedTLS() {
  219. ssl_ctx.instance();
  220. status = STATUS_DISCONNECTED;
  221. }
  222. StreamPeerMbedTLS::~StreamPeerMbedTLS() {
  223. disconnect_from_stream();
  224. }
  225. void StreamPeerMbedTLS::disconnect_from_stream() {
  226. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING) {
  227. return;
  228. }
  229. Ref<StreamPeerTCP> tcp = base;
  230. if (tcp.is_valid() && tcp->get_status() == StreamPeerTCP::STATUS_CONNECTED) {
  231. // We are still connected on the socket, try to send close notify.
  232. mbedtls_ssl_close_notify(ssl_ctx->get_context());
  233. }
  234. _cleanup();
  235. }
  236. StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
  237. return status;
  238. }
  239. StreamPeerSSL *StreamPeerMbedTLS::_create_func() {
  240. return memnew(StreamPeerMbedTLS);
  241. }
  242. void StreamPeerMbedTLS::initialize_ssl() {
  243. _create = _create_func;
  244. available = true;
  245. }
  246. void StreamPeerMbedTLS::finalize_ssl() {
  247. available = false;
  248. _create = nullptr;
  249. }