packet_peer_mbed_dtls.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "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 == NULL || len <= 0) return 0;
  36. PacketPeerMbedDTLS *sp = (PacketPeerMbedDTLS *)ctx;
  37. ERR_FAIL_COND_V(sp == NULL, 0);
  38. Error err = sp->base->put_packet((const uint8_t *)buf, len);
  39. if (err == ERR_BUSY) {
  40. return MBEDTLS_ERR_SSL_WANT_WRITE;
  41. } else if (err != OK) {
  42. ERR_FAIL_V(MBEDTLS_ERR_SSL_INTERNAL_ERROR);
  43. }
  44. return len;
  45. }
  46. int PacketPeerMbedDTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  47. if (buf == NULL || len <= 0) return 0;
  48. PacketPeerMbedDTLS *sp = (PacketPeerMbedDTLS *)ctx;
  49. ERR_FAIL_COND_V(sp == NULL, 0);
  50. int pc = sp->base->get_available_packet_count();
  51. if (pc == 0) {
  52. return MBEDTLS_ERR_SSL_WANT_READ;
  53. } else if (pc < 0) {
  54. ERR_FAIL_V(MBEDTLS_ERR_SSL_INTERNAL_ERROR);
  55. }
  56. const uint8_t *buffer;
  57. int buffer_size = 0;
  58. Error err = sp->base->get_packet(&buffer, buffer_size);
  59. if (err != OK) {
  60. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  61. }
  62. memcpy(buf, buffer, buffer_size);
  63. return buffer_size;
  64. }
  65. void PacketPeerMbedDTLS::_cleanup() {
  66. ssl_ctx->clear();
  67. base = Ref<PacketPeer>();
  68. status = STATUS_DISCONNECTED;
  69. }
  70. int PacketPeerMbedDTLS::_set_cookie() {
  71. // Setup DTLS session cookie for this client
  72. uint8_t client_id[18];
  73. IP_Address addr = base->get_packet_address();
  74. uint16_t port = base->get_packet_port();
  75. memcpy(client_id, addr.get_ipv6(), 16);
  76. memcpy(&client_id[16], (uint8_t *)&port, 2);
  77. return mbedtls_ssl_set_client_transport_id(ssl_ctx->get_context(), client_id, 18);
  78. }
  79. Error PacketPeerMbedDTLS::_do_handshake() {
  80. int ret = 0;
  81. while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) {
  82. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  83. if (ret != MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
  84. ERR_PRINT("TLS handshake error: " + itos(ret));
  85. SSLContextMbedTLS::print_mbedtls_error(ret);
  86. }
  87. _cleanup();
  88. status = STATUS_ERROR;
  89. return FAILED;
  90. }
  91. // Will retry via poll later
  92. return OK;
  93. }
  94. status = STATUS_CONNECTED;
  95. return OK;
  96. }
  97. Error PacketPeerMbedDTLS::connect_to_peer(Ref<PacketPeerUDP> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
  98. ERR_FAIL_COND_V(!p_base.is_valid() || !p_base->is_connected_to_host(), ERR_INVALID_PARAMETER);
  99. base = p_base;
  100. int ret = 0;
  101. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  102. Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_DATAGRAM, authmode, p_ca_certs);
  103. ERR_FAIL_COND_V(err != OK, err);
  104. mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
  105. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
  106. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  107. status = STATUS_HANDSHAKING;
  108. if ((ret = _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<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain, Ref<CookieContextMbedTLS> p_cookies) {
  115. Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert, p_cookies);
  116. ERR_FAIL_COND_V(err != OK, err);
  117. base = p_base;
  118. base->set_blocking_mode(false);
  119. mbedtls_ssl_session_reset(ssl_ctx->get_context());
  120. int ret = _set_cookie();
  121. if (ret != 0) {
  122. _cleanup();
  123. ERR_FAIL_V_MSG(FAILED, "Error setting DTLS client cookie");
  124. }
  125. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, NULL);
  126. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  127. status = STATUS_HANDSHAKING;
  128. if ((ret = _do_handshake()) != OK) {
  129. status = STATUS_ERROR;
  130. return FAILED;
  131. }
  132. return OK;
  133. }
  134. Error PacketPeerMbedDTLS::put_packet(const uint8_t *p_buffer, int p_bytes) {
  135. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_UNCONFIGURED);
  136. if (p_bytes == 0)
  137. return OK;
  138. int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_buffer, p_bytes);
  139. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  140. ret = 0; // non blocking io
  141. } else if (ret <= 0) {
  142. SSLContextMbedTLS::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(ssl_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. SSLContextMbedTLS::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(ssl_ctx->get_context(), NULL, 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. SSLContextMbedTLS::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(&(ssl_ctx->ssl)) > 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. ssl_ctx.instance();
  198. status = STATUS_DISCONNECTED;
  199. }
  200. PacketPeerMbedDTLS::~PacketPeerMbedDTLS() {
  201. disconnect_from_peer();
  202. }
  203. void PacketPeerMbedDTLS::disconnect_from_peer() {
  204. if (status != STATUS_CONNECTED && status != STATUS_HANDSHAKING)
  205. return;
  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(ssl_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() {
  219. return memnew(PacketPeerMbedDTLS);
  220. }
  221. void PacketPeerMbedDTLS::initialize_dtls() {
  222. _create = _create_func;
  223. available = true;
  224. }
  225. void PacketPeerMbedDTLS::finalize_dtls() {
  226. _create = NULL;
  227. available = false;
  228. }