packet_peer_mbed_dtls.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 == nullptr || len <= 0)
  36. return 0;
  37. PacketPeerMbedDTLS *sp = (PacketPeerMbedDTLS *)ctx;
  38. ERR_FAIL_COND_V(sp == nullptr, 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. PacketPeerMbedDTLS *sp = (PacketPeerMbedDTLS *)ctx;
  51. ERR_FAIL_COND_V(sp == nullptr, 0);
  52. int pc = sp->base->get_available_packet_count();
  53. if (pc == 0) {
  54. return MBEDTLS_ERR_SSL_WANT_READ;
  55. } else if (pc < 0) {
  56. ERR_FAIL_V(MBEDTLS_ERR_SSL_INTERNAL_ERROR);
  57. }
  58. const uint8_t *buffer;
  59. int buffer_size = 0;
  60. Error err = sp->base->get_packet(&buffer, buffer_size);
  61. if (err != OK) {
  62. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  63. }
  64. copymem(buf, buffer, buffer_size);
  65. return buffer_size;
  66. }
  67. void PacketPeerMbedDTLS::_cleanup() {
  68. ssl_ctx->clear();
  69. base = Ref<PacketPeer>();
  70. status = STATUS_DISCONNECTED;
  71. }
  72. int PacketPeerMbedDTLS::_set_cookie() {
  73. // Setup DTLS session cookie for this client
  74. uint8_t client_id[18];
  75. IP_Address addr = base->get_packet_address();
  76. uint16_t port = base->get_packet_port();
  77. copymem(client_id, addr.get_ipv6(), 16);
  78. copymem(&client_id[16], (uint8_t *)&port, 2);
  79. return mbedtls_ssl_set_client_transport_id(ssl_ctx->get_context(), client_id, 18);
  80. }
  81. Error PacketPeerMbedDTLS::_do_handshake() {
  82. int ret = 0;
  83. while ((ret = mbedtls_ssl_handshake(ssl_ctx->get_context())) != 0) {
  84. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  85. if (ret != MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
  86. ERR_PRINT("TLS handshake error: " + itos(ret));
  87. SSLContextMbedTLS::print_mbedtls_error(ret);
  88. }
  89. _cleanup();
  90. status = STATUS_ERROR;
  91. return FAILED;
  92. }
  93. // Will retry via poll later
  94. return OK;
  95. }
  96. status = STATUS_CONNECTED;
  97. return OK;
  98. }
  99. Error PacketPeerMbedDTLS::connect_to_peer(Ref<PacketPeerUDP> p_base, bool p_validate_certs, const String &p_for_hostname, Ref<X509Certificate> p_ca_certs) {
  100. ERR_FAIL_COND_V(!p_base.is_valid() || !p_base->is_connected_to_host(), ERR_INVALID_PARAMETER);
  101. base = p_base;
  102. int ret = 0;
  103. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  104. Error err = ssl_ctx->init_client(MBEDTLS_SSL_TRANSPORT_DATAGRAM, authmode, p_ca_certs);
  105. ERR_FAIL_COND_V(err != OK, err);
  106. mbedtls_ssl_set_hostname(ssl_ctx->get_context(), p_for_hostname.utf8().get_data());
  107. mbedtls_ssl_set_bio(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  108. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  109. status = STATUS_HANDSHAKING;
  110. if ((ret = _do_handshake()) != OK) {
  111. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  112. return FAILED;
  113. }
  114. return OK;
  115. }
  116. 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) {
  117. Error err = ssl_ctx->init_server(MBEDTLS_SSL_TRANSPORT_DATAGRAM, MBEDTLS_SSL_VERIFY_NONE, p_key, p_cert, 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(ssl_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(ssl_ctx->get_context(), this, bio_send, bio_recv, nullptr);
  128. mbedtls_ssl_set_timer_cb(ssl_ctx->get_context(), &timer, mbedtls_timing_set_delay, mbedtls_timing_get_delay);
  129. status = STATUS_HANDSHAKING;
  130. if ((ret = _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. int ret = mbedtls_ssl_write(ssl_ctx->get_context(), p_buffer, p_bytes);
  141. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  142. ret = 0; // non blocking io
  143. } else if (ret <= 0) {
  144. SSLContextMbedTLS::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(ssl_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. SSLContextMbedTLS::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(ssl_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. SSLContextMbedTLS::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(&(ssl_ctx->ssl)) > 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. ssl_ctx.instance();
  200. status = STATUS_DISCONNECTED;
  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. 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(ssl_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() {
  221. return memnew(PacketPeerMbedDTLS);
  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. }