packet_peer_mbed_dtls.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/stream_peer_ssl.h"
  33. #include "core/os/file_access.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 = (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 = (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. ssl_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. IP_Address 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(ssl_ctx->get_context(), client_id, 18);
  82. }
  83. Error PacketPeerMbedDTLS::_do_handshake() {
  84. int ret = 0;
  85. while ((ret = mbedtls_ssl_handshake(ssl_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. SSLContextMbedTLS::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, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
  102. ERR_FAIL_COND_V(!p_base.is_valid() || !p_base->is_connected_to_host(), ERR_INVALID_PARAMETER);
  103. base = p_base;
  104. int ret = 0;
  105. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  106. Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_DATAGRAM, authmode, p_ca_certs);
  107. ERR_FAIL_COND_V(err != OK, err);
  108. mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
  109. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  110. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  111. status = STATUS_HANDSHAKING;
  112. if ((ret = _do_handshake()) != OK) {
  113. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  114. return FAILED;
  115. }
  116. return OK;
  117. }
  118. Error PacketPeerMbedDTLS::accept_peer(Ref<PacketPeerUDP> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain, Ref<CookieContextMbedTLS> p_cookies) {
  119. Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert, p_cookies);
  120. ERR_FAIL_COND_V(err != OK, err);
  121. base = p_base;
  122. base->set_blocking_mode(false);
  123. mbedtls_ssl_session_reset(ssl_ctx->get_context());
  124. int ret = _set_cookie();
  125. if (ret != 0) {
  126. _cleanup();
  127. ERR_FAIL_V_MSG(FAILED, "Error setting DTLS client cookie");
  128. }
  129. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  130. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  131. status = STATUS_HANDSHAKING;
  132. if ((ret = _do_handshake()) != OK) {
  133. status = STATUS_ERROR;
  134. return FAILED;
  135. }
  136. return OK;
  137. }
  138. Error PacketPeerMbedDTLS::put_packet(const uint8_t *p_buffer, int p_bytes) {
  139. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  140. if (p_bytes == 0) {
  141. return OK;
  142. }
  143. int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_buffer, p_bytes);
  144. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  145. ret = 0; // non blocking io
  146. } else if (ret <= 0) {
  147. SSLContextMbedTLS::print_mbedtls_error(ret);
  148. _cleanup();
  149. return ERR_CONNECTION_ERROR;
  150. }
  151. return OK;
  152. }
  153. Error PacketPeerMbedDTLS::get_packet(const uint8_t **r_buffer, int &r_bytes) {
  154. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  155. r_bytes = 0;
  156. int ret = mbedtls_ssl_read(ssl_ctx->get_context(), packet_buffer, PACKET_BUFFER_SIZE);
  157. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  158. ret = 0; // non blocking io
  159. } else if (ret <= 0) {
  160. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  161. // Also send close notify back
  162. disconnect_from_peer();
  163. } else {
  164. _cleanup();
  165. status = STATUS_ERROR;
  166. SSLContextMbedTLS::print_mbedtls_error(ret);
  167. }
  168. return ERR_CONNECTION_ERROR;
  169. }
  170. *r_buffer = packet_buffer;
  171. r_bytes = ret;
  172. return OK;
  173. }
  174. void PacketPeerMbedDTLS::poll() {
  175. if (status == STATUS_HANDSHAKING) {
  176. _do_handshake();
  177. return;
  178. } else if (status != STATUS_CONNECTED) {
  179. return;
  180. }
  181. ERR_FAIL_COND(!base.is_valid());
  182. int ret = mbedtls_ssl_read(ssl_ctx->get_context(), nullptr, 0);
  183. if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  184. if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
  185. // Also send close notify back
  186. disconnect_from_peer();
  187. } else {
  188. _cleanup();
  189. status = STATUS_ERROR;
  190. SSLContextMbedTLS::print_mbedtls_error(ret);
  191. }
  192. }
  193. }
  194. int PacketPeerMbedDTLS::get_available_packet_count() const {
  195. ERR_FAIL_COND_V(status != STATUS_CONNECTED, 0);
  196. return mbedtls_ssl_get_bytes_avail(&(ssl_ctx->ssl)) > 0 ? 1 : 0;
  197. }
  198. int PacketPeerMbedDTLS::get_max_packet_size() const {
  199. return 488; // 512 (UDP in Godot) - 24 (DTLS header)
  200. }
  201. PacketPeerMbedDTLS::PacketPeerMbedDTLS() {
  202. ssl_ctx.instance();
  203. status = STATUS_DISCONNECTED;
  204. }
  205. PacketPeerMbedDTLS::~PacketPeerMbedDTLS() {
  206. disconnect_from_peer();
  207. }
  208. void PacketPeerMbedDTLS::disconnect_from_peer() {
  209. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING) {
  210. return;
  211. }
  212. if (status == STATUS_CONNECTED) {
  213. int ret = 0;
  214. // Send SSL close notification, blocking, but ignore other errors.
  215. do {
  216. ret = mbedtls_ssl_close_notify(ssl_ctx->get_context());
  217. } while (ret == MBEDTLS_ERR_SSL_WANT_WRITE);
  218. }
  219. _cleanup();
  220. }
  221. PacketPeerMbedDTLS::Status PacketPeerMbedDTLS::get_status() const {
  222. return status;
  223. }
  224. PacketPeerDTLS *PacketPeerMbedDTLS::_create_func() {
  225. return memnew(PacketPeerMbedDTLS);
  226. }
  227. void PacketPeerMbedDTLS::initialize_dtls() {
  228. _create = _create_func;
  229. available = true;
  230. }
  231. void PacketPeerMbedDTLS::finalize_dtls() {
  232. _create = nullptr;
  233. available = false;
  234. }