stream_peer_mbedtls.cpp 9.0 KB

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