stream_peer_mbed_tls.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*************************************************************************/
  2. /* stream_peer_openssl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "stream_peer_mbed_tls.h"
  31. static void my_debug(void *ctx, int level,
  32. const char *file, int line,
  33. const char *str) {
  34. printf("%s:%04d: %s", file, line, str);
  35. fflush(stdout);
  36. }
  37. void _print_error(int ret) {
  38. printf("mbedtls error: returned -0x%x\n\n", -ret);
  39. fflush(stdout);
  40. }
  41. int StreamPeerMbedTLS::bio_send(void *ctx, const unsigned char *buf, size_t len) {
  42. if (buf == NULL || len <= 0) return 0;
  43. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  44. ERR_FAIL_COND_V(sp == NULL, 0);
  45. int sent;
  46. Error err = sp->base->put_partial_data((const uint8_t *)buf, len, sent);
  47. if (err != OK) {
  48. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  49. }
  50. if (sent == 0) {
  51. return MBEDTLS_ERR_SSL_WANT_WRITE;
  52. }
  53. return sent;
  54. }
  55. int StreamPeerMbedTLS::bio_recv(void *ctx, unsigned char *buf, size_t len) {
  56. if (buf == NULL || len <= 0) return 0;
  57. StreamPeerMbedTLS *sp = (StreamPeerMbedTLS *)ctx;
  58. ERR_FAIL_COND_V(sp == NULL, 0);
  59. int got;
  60. Error err = sp->base->get_partial_data((uint8_t *)buf, len, got);
  61. if (err != OK) {
  62. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  63. }
  64. if (got == 0) {
  65. return MBEDTLS_ERR_SSL_WANT_READ;
  66. }
  67. return got;
  68. }
  69. Error StreamPeerMbedTLS::connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs, const String &p_for_hostname) {
  70. base = p_base;
  71. int ret = 0;
  72. int authmode = p_validate_certs ? MBEDTLS_SSL_VERIFY_REQUIRED : MBEDTLS_SSL_VERIFY_NONE;
  73. mbedtls_ssl_init(&ssl);
  74. mbedtls_ssl_config_init(&conf);
  75. mbedtls_ctr_drbg_init(&ctr_drbg);
  76. mbedtls_entropy_init(&entropy);
  77. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
  78. if (ret != 0) {
  79. ERR_PRINTS(" failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
  80. return FAILED;
  81. }
  82. mbedtls_ssl_config_defaults(&conf,
  83. MBEDTLS_SSL_IS_CLIENT,
  84. MBEDTLS_SSL_TRANSPORT_STREAM,
  85. MBEDTLS_SSL_PRESET_DEFAULT);
  86. mbedtls_ssl_conf_authmode(&conf, authmode);
  87. mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
  88. mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
  89. mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
  90. mbedtls_ssl_setup(&ssl, &conf);
  91. mbedtls_ssl_set_hostname(&ssl, p_for_hostname.utf8().get_data());
  92. mbedtls_ssl_set_bio(&ssl, this, bio_send, bio_recv, NULL);
  93. while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {
  94. if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  95. ERR_PRINTS("TLS handshake error: " + itos(ret));
  96. _print_error(ret);
  97. status = STATUS_ERROR_HOSTNAME_MISMATCH;
  98. return FAILED;
  99. }
  100. }
  101. connected = true;
  102. status = STATUS_CONNECTED;
  103. return OK;
  104. }
  105. Error StreamPeerMbedTLS::accept_stream(Ref<StreamPeer> p_base) {
  106. return ERR_UNAVAILABLE;
  107. }
  108. Error StreamPeerMbedTLS::put_data(const uint8_t *p_data, int p_bytes) {
  109. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  110. Error err;
  111. int sent = 0;
  112. while (p_bytes > 0) {
  113. err = put_partial_data(p_data, p_bytes, sent);
  114. if (err != OK) {
  115. return err;
  116. }
  117. p_data += sent;
  118. p_bytes -= sent;
  119. }
  120. return OK;
  121. }
  122. Error StreamPeerMbedTLS::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  123. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  124. r_sent = 0;
  125. if (p_bytes == 0)
  126. return OK;
  127. int ret = mbedtls_ssl_write(&ssl, p_data, p_bytes);
  128. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  129. ret = 0; // non blocking io
  130. } else if (ret <= 0) {
  131. _print_error(ret);
  132. disconnect_from_stream();
  133. return ERR_CONNECTION_ERROR;
  134. }
  135. r_sent = ret;
  136. return OK;
  137. }
  138. Error StreamPeerMbedTLS::get_data(uint8_t *p_buffer, int p_bytes) {
  139. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  140. Error err;
  141. int got = 0;
  142. while (p_bytes > 0) {
  143. err = get_partial_data(p_buffer, p_bytes, got);
  144. if (err != OK) {
  145. return err;
  146. }
  147. p_buffer += got;
  148. p_bytes -= got;
  149. }
  150. return OK;
  151. }
  152. Error StreamPeerMbedTLS::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  153. ERR_FAIL_COND_V(!connected, ERR_UNCONFIGURED);
  154. r_received = 0;
  155. int ret = mbedtls_ssl_read(&ssl, p_buffer, p_bytes);
  156. if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  157. ret = 0; // non blocking io
  158. } else if (ret <= 0) {
  159. _print_error(ret);
  160. disconnect_from_stream();
  161. return ERR_CONNECTION_ERROR;
  162. }
  163. r_received = ret;
  164. return OK;
  165. }
  166. void StreamPeerMbedTLS::poll() {
  167. ERR_FAIL_COND(!connected);
  168. ERR_FAIL_COND(!base.is_valid());
  169. int ret = mbedtls_ssl_read(&ssl, NULL, 0);
  170. if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  171. _print_error(ret);
  172. disconnect_from_stream();
  173. return;
  174. }
  175. }
  176. int StreamPeerMbedTLS::get_available_bytes() const {
  177. ERR_FAIL_COND_V(!connected, 0);
  178. return mbedtls_ssl_get_bytes_avail(&ssl);
  179. }
  180. StreamPeerMbedTLS::StreamPeerMbedTLS() {
  181. connected = false;
  182. status = STATUS_DISCONNECTED;
  183. }
  184. StreamPeerMbedTLS::~StreamPeerMbedTLS() {
  185. disconnect_from_stream();
  186. }
  187. void StreamPeerMbedTLS::disconnect_from_stream() {
  188. if (!connected)
  189. return;
  190. mbedtls_ssl_free(&ssl);
  191. mbedtls_ssl_config_free(&conf);
  192. mbedtls_ctr_drbg_free(&ctr_drbg);
  193. mbedtls_entropy_free(&entropy);
  194. base = Ref<StreamPeer>();
  195. connected = false;
  196. status = STATUS_DISCONNECTED;
  197. }
  198. StreamPeerMbedTLS::Status StreamPeerMbedTLS::get_status() const {
  199. return status;
  200. }
  201. StreamPeerSSL *StreamPeerMbedTLS::_create_func() {
  202. return memnew(StreamPeerMbedTLS);
  203. }
  204. mbedtls_x509_crt StreamPeerMbedTLS::cacert;
  205. void StreamPeerMbedTLS::_load_certs(const PoolByteArray &p_array) {
  206. int arr_len = p_array.size();
  207. PoolByteArray::Read r = p_array.read();
  208. int err = mbedtls_x509_crt_parse(&cacert, &r[0], arr_len);
  209. if (err != 0) {
  210. WARN_PRINTS("Error parsing some certificates: " + itos(err));
  211. }
  212. }
  213. void StreamPeerMbedTLS::initialize_ssl() {
  214. _create = _create_func;
  215. load_certs_func = _load_certs;
  216. mbedtls_x509_crt_init(&cacert);
  217. #ifdef DEBUG_ENABLED
  218. mbedtls_debug_set_threshold(1);
  219. #endif
  220. String certs_path = GLOBAL_DEF("network/ssl/certificates", "");
  221. ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt"));
  222. if (certs_path != "") {
  223. FileAccess *f = FileAccess::open(certs_path, FileAccess::READ);
  224. if (f) {
  225. PoolByteArray arr;
  226. int flen = f->get_len();
  227. arr.resize(flen + 1);
  228. {
  229. PoolByteArray::Write w = arr.write();
  230. f->get_buffer(w.ptr(), flen);
  231. w[flen] = 0; //end f string
  232. }
  233. memdelete(f);
  234. _load_certs(arr);
  235. print_line("Loaded certs from '" + certs_path);
  236. }
  237. }
  238. available = true;
  239. }
  240. void StreamPeerMbedTLS::finalize_ssl() {
  241. mbedtls_x509_crt_free(&cacert);
  242. }