packet_peer_mbed_dtls.cpp 8.8 KB

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