packet_peer_mbed_dtls.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**************************************************************************/
  2. /* packet_peer_mbed_dtls.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 "packet_peer_mbed_dtls.h"
  31. #include "core/io/file_access.h"
  32. #include "core/io/stream_peer_tls.h"
  33. int PacketPeerMbedDTLS::bio_send(void *ctx, const unsigned char *buf, size_t len) {
  34. if (buf == nullptr || len == 0) {
  35. return 0;
  36. }
  37. PacketPeerMbedDTLS *sp = static_cast<PacketPeerMbedDTLS *>(ctx);
  38. ERR_FAIL_NULL_V(sp, 0);
  39. Error err = sp->base->put_packet((const uint8_t *)buf, len);
  40. if (err == ERR_BUSY) {
  41. return MBEDTLS_ERR_SSL_WANT_WRITE;
  42. } else if (err != OK) {
  43. ERR_FAIL_V(MBEDTLS_ERR_SSL_INTERNAL_ERROR);
  44. }
  45. return len;
  46. }
  47. int PacketPeerMbedDTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  48. if (buf == nullptr || len == 0) {
  49. return 0;
  50. }
  51. PacketPeerMbedDTLS *sp = static_cast<PacketPeerMbedDTLS *>(ctx);
  52. ERR_FAIL_NULL_V(sp, 0);
  53. int pc = sp->base->get_available_packet_count();
  54. if (pc == 0) {
  55. return MBEDTLS_ERR_SSL_WANT_READ;
  56. } else if (pc < 0) {
  57. ERR_FAIL_V(MBEDTLS_ERR_SSL_INTERNAL_ERROR);
  58. }
  59. const uint8_t *buffer;
  60. int buffer_size = 0;
  61. Error err = sp->base->get_packet(&buffer, buffer_size);
  62. if (err != OK) {
  63. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  64. }
  65. memcpy(buf, buffer, buffer_size);
  66. return buffer_size;
  67. }
  68. void PacketPeerMbedDTLS::_cleanup() {
  69. tls_ctx->clear();
  70. base = Ref<PacketPeer>();
  71. status = STATUS_DISCONNECTED;
  72. }
  73. int PacketPeerMbedDTLS::_set_cookie() {
  74. // Setup DTLS session cookie for this client
  75. uint8_t client_id[18];
  76. IPAddress addr = base->get_packet_address();
  77. uint16_t port = base->get_packet_port();
  78. memcpy(client_id, addr.get_ipv6(), 16);
  79. memcpy(&client_id[16], (uint8_t *)&port, 2);
  80. return mbedtls_ssl_set_client_transport_id(tls_ctx->get_context(), client_id, 18);
  81. }
  82. Error PacketPeerMbedDTLS::_do_handshake() {
  83. int ret = 0;
  84. while ((ret = mbedtls_ssl_handshake(tls_ctx->get_context())) != 0) {
  85. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  86. if (ret != MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
  87. ERR_PRINT("TLS handshake error: " + itos(ret));
  88. TLSContextMbedTLS::print_mbedtls_error(ret);
  89. }
  90. _cleanup();
  91. status = STATUS_ERROR;
  92. return FAILED;
  93. }
  94. // Will retry via poll later
  95. return OK;
  96. }
  97. status = STATUS_CONNECTED;
  98. return OK;
  99. }
  100. Error PacketPeerMbedDTLS::connect_to_peer(Ref<PacketPeerUDP> p_base, const String &p_hostname, Ref<TLSOptions> p_options) {
  101. ERR_FAIL_COND_V(!p_base.is_valid() || !p_base->is_socket_connected(), ERR_INVALID_PARAMETER);
  102. Error err = tls_ctx->init_client(MBEDTLS_SSL_TRANSPORT_DATAGRAM, p_hostname, p_options.is_valid() ? p_options : TLSOptions::client());
  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. mbedtls_ssl_set_timer_cb(tls_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  107. status = STATUS_HANDSHAKING;
  108. if (_do_handshake() != OK) {
  109. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  110. return FAILED;
  111. }
  112. return OK;
  113. }
  114. Error PacketPeerMbedDTLS::accept_peer(Ref<PacketPeerUDP> p_base, Ref<TLSOptions> p_options, Ref<CookieContextMbedTLS> p_cookies) {
  115. ERR_FAIL_COND_V(!p_base.is_valid() || !p_base->is_socket_connected(), ERR_INVALID_PARAMETER);
  116. Error err = tls_ctx->init_server(MBEDTLS_SSL_TRANSPORT_DATAGRAM, p_options, p_cookies);
  117. ERR_FAIL_COND_V(err != OK, err);
  118. base = p_base;
  119. base->set_blocking_mode(false);
  120. mbedtls_ssl_session_reset(tls_ctx->get_context());
  121. int ret = _set_cookie();
  122. if (ret != 0) {
  123. _cleanup();
  124. ERR_FAIL_V_MSG(FAILED, "Error setting DTLS client cookie");
  125. }
  126. mbedtls_ssl_set_bio(tls_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  127. mbedtls_ssl_set_timer_cb(tls_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  128. status = STATUS_HANDSHAKING;
  129. if (_do_handshake() != OK) {
  130. status = STATUS_ERROR;
  131. return FAILED;
  132. }
  133. return OK;
  134. }
  135. Error PacketPeerMbedDTLS::put_packet(const uint8_t *p_buffer, int p_bytes) {
  136. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  137. if (p_bytes == 0) {
  138. return OK;
  139. }
  140. int ret = mbedtls_ssl_write(tls_ctx->get_context(), p_buffer, p_bytes);
  141. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  142. // Non blocking io.
  143. } else if (ret <= 0) {
  144. TLSContextMbedTLS::print_mbedtls_error(ret);
  145. _cleanup();
  146. return ERR_CONNECTION_ERROR;
  147. }
  148. return OK;
  149. }
  150. Error PacketPeerMbedDTLS::get_packet(const uint8_t **r_buffer, int &r_bytes) {
  151. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  152. r_bytes = 0;
  153. int ret = mbedtls_ssl_read(tls_ctx->get_context(), packet_buffer, PACKET_BUFFER_SIZE);
  154. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  155. ret = 0; // non blocking io
  156. } else if (ret <= 0) {
  157. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  158. // Also send close notify back
  159. disconnect_from_peer();
  160. } else {
  161. _cleanup();
  162. status = STATUS_ERROR;
  163. TLSContextMbedTLS::print_mbedtls_error(ret);
  164. }
  165. return ERR_CONNECTION_ERROR;
  166. }
  167. *r_buffer = packet_buffer;
  168. r_bytes = ret;
  169. return OK;
  170. }
  171. void PacketPeerMbedDTLS::poll() {
  172. if (status == STATUS_HANDSHAKING) {
  173. _do_handshake();
  174. return;
  175. } else if (status != STATUS_CONNECTED) {
  176. return;
  177. }
  178. ERR_FAIL_COND(!base.is_valid());
  179. int ret = mbedtls_ssl_read(tls_ctx->get_context(), nullptr, 0);
  180. if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  181. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  182. // Also send close notify back
  183. disconnect_from_peer();
  184. } else {
  185. _cleanup();
  186. status = STATUS_ERROR;
  187. TLSContextMbedTLS::print_mbedtls_error(ret);
  188. }
  189. }
  190. }
  191. int PacketPeerMbedDTLS::get_available_packet_count() const {
  192. ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
  193. return mbedtls_ssl_get_bytes_avail(&(tls_ctx->tls)) > 0 ? 1 : 0;
  194. }
  195. int PacketPeerMbedDTLS::get_max_packet_size() const {
  196. return 488; // 512 (UDP in Godot) - 24 (DTLS header)
  197. }
  198. PacketPeerMbedDTLS::PacketPeerMbedDTLS() {
  199. tls_ctx.instantiate();
  200. }
  201. PacketPeerMbedDTLS::~PacketPeerMbedDTLS() {
  202. disconnect_from_peer();
  203. }
  204. void PacketPeerMbedDTLS::disconnect_from_peer() {
  205. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING) {
  206. return;
  207. }
  208. if (status == STATUS_CONNECTED) {
  209. int ret = 0;
  210. // Send SSL close notification, blocking, but ignore other errors.
  211. do {
  212. ret = mbedtls_ssl_close_notify(tls_ctx->get_context());
  213. } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  214. }
  215. _cleanup();
  216. }
  217. PacketPeerMbedDTLS::Status PacketPeerMbedDTLS::get_status() const {
  218. return status;
  219. }
  220. PacketPeerDTLS *PacketPeerMbedDTLS::_create_func(bool p_notify_postinitialize) {
  221. return static_cast<PacketPeerDTLS *>(ClassDB::creator<PacketPeerMbedDTLS>(p_notify_postinitialize));
  222. }
  223. void PacketPeerMbedDTLS::initialize_dtls() {
  224. _create = _create_func;
  225. available = true;
  226. }
  227. void PacketPeerMbedDTLS::finalize_dtls() {
  228. _create = nullptr;
  229. available = false;
  230. }