2
0

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