stream_peer_mbedtls.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*************************************************************************/
  2. /* stream_peer_mbedtls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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 == NULL || len <= 0) return 0;
  35. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  36. ERR_FAIL_COND_V(sp == NULL, 0);
  37. int sent;
  38. Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
  39. if (err != OK) {
  40. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  41. }
  42. if (sent == 0) {
  43. return MBEDTLS_ERR_SSL_WANT_WRITE;
  44. }
  45. return sent;
  46. }
  47. int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  48. if (buf == NULL || len <= 0) return 0;
  49. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  50. ERR_FAIL_COND_V(sp == NULL, 0);
  51. int got;
  52. Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
  53. if (err != OK) {
  54. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  55. }
  56. if (got == 0) {
  57. return MBEDTLS_ERR_SSL_WANT_READ;
  58. }
  59. return got;
  60. }
  61. void StreamPeerMbedTLS::_cleanup() {
  62. ssl_ctx->clear();
  63. base = Ref<StreamPeer>();
  64. status = STATUS_DISCONNECTED;
  65. }
  66. Error StreamPeerMbedTLS::_do_handshake() {
  67. int ret = 0;
  68. while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) {
  69. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  70. // An error occurred.
  71. ERR_PRINT("TLS handshake error: " + itos(ret));
  72. SSLContextMbedTLS::print_mbedtls_error(ret);
  73. disconnect_from_stream();
  74. status = STATUS_ERROR;
  75. return FAILED;
  76. }
  77. // Handshake is still in progress.
  78. if (!blocking_handshake) {
  79. // Will retry via poll later
  80. return OK;
  81. }
  82. }
  83. status = STATUS_CONNECTED;
  84. return OK;
  85. }
  86. Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
  87. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  88. base = p_base;
  89. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  90. Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_STREAM, authmode, p_ca_certs);
  91. ERR_FAIL_COND_V(err != OK, err);
  92. mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
  93. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
  94. status = STATUS_HANDSHAKING;
  95. if (_do_handshake() != OK) {
  96. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  97. return FAILED;
  98. }
  99. return OK;
  100. }
  101. Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain) {
  102. ERR_FAIL_COND_V(p_base.is_null(), ERR_INVALID_PARAMETER);
  103. Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert);
  104. ERR_FAIL_COND_V(err != OK, err);
  105. base = p_base;
  106. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
  107. status = STATUS_HANDSHAKING;
  108. if (_do_handshake() != OK) {
  109. return FAILED;
  110. }
  111. status = STATUS_CONNECTED;
  112. return OK;
  113. }
  114. Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
  115. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  116. Error err;
  117. int sent = 0;
  118. while (p_bytes > 0) {
  119. err = put_partial_data(p_data, p_bytes, sent);
  120. if (err != OK) {
  121. return err;
  122. }
  123. p_data += sent;
  124. p_bytes -= sent;
  125. }
  126. return OK;
  127. }
  128. Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  129. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  130. r_sent = 0;
  131. if (p_bytes == 0)
  132. return OK;
  133. int ret = mbedtls_ssl_write(ssl_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. SSLContextMbedTLS::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(ssl_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. SSLContextMbedTLS::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 NULL as second parameter, but some behaviour sanitizers doesn't seem to like that.
  189. // Passing a 1 byte buffer to workaround it.
  190. uint8_t byte;
  191. int ret = mbedtls_ssl_read(ssl_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. SSLContextMbedTLS::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(&(ssl_ctx->ssl));
  212. }
  213. StreamPeerMbedTLS::StreamPeerMbedTLS() {
  214. ssl_ctx.instance();
  215. status = STATUS_DISCONNECTED;
  216. }
  217. StreamPeerMbedTLS::~StreamPeerMbedTLS() {
  218. disconnect_from_stream();
  219. }
  220. void StreamPeerMbedTLS::disconnect_from_stream() {
  221. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING)
  222. return;
  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(ssl_ctx->get_context());
  227. }
  228. _cleanup();
  229. }
  230. StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
  231. return status;
  232. }
  233. StreamPeerSSL *StreamPeerMbedTLS::_create_func() {
  234. return memnew(StreamPeerMbedTLS);
  235. }
  236. void StreamPeerMbedTLS::initialize_ssl() {
  237. _create = _create_func;
  238. available = true;
  239. }
  240. void StreamPeerMbedTLS::finalize_ssl() {
  241. available = false;
  242. _create = NULL;
  243. }