mbedtls-ssl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "private-lib-tls-mbedtls.h"
  26. void
  27. lws_ssl_destroy(struct lws_vhost *vhost)
  28. {
  29. if (!lws_check_opt(vhost->context->options,
  30. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
  31. return;
  32. if (vhost->tls.ssl_ctx)
  33. SSL_CTX_free(vhost->tls.ssl_ctx);
  34. if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
  35. SSL_CTX_free(vhost->tls.ssl_client_ctx);
  36. if (vhost->tls.x509_client_CA)
  37. X509_free(vhost->tls.x509_client_CA);
  38. }
  39. int
  40. lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
  41. {
  42. struct lws_context *context = wsi->a.context;
  43. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  44. int n = 0, m;
  45. if (!wsi->tls.ssl)
  46. return lws_ssl_capable_read_no_ssl(wsi, buf, len);
  47. lws_stats_bump(pt, LWSSTATS_C_API_READ, 1);
  48. errno = 0;
  49. n = SSL_read(wsi->tls.ssl, buf, len);
  50. #if defined(LWS_PLAT_FREERTOS)
  51. if (!n && errno == LWS_ENOTCONN) {
  52. lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
  53. return LWS_SSL_CAPABLE_ERROR;
  54. }
  55. #endif
  56. #if defined(LWS_WITH_STATS)
  57. if (!wsi->seen_rx && wsi->accept_start_us) {
  58. lws_stats_bump(pt, LWSSTATS_US_SSL_RX_DELAY_AVG,
  59. lws_now_usecs() - wsi->accept_start_us);
  60. lws_stats_bump(pt, LWSSTATS_C_SSL_CONNS_HAD_RX, 1);
  61. wsi->seen_rx = 1;
  62. }
  63. #endif
  64. lwsl_debug("%p: SSL_read says %d\n", wsi, n);
  65. /* manpage: returning 0 means connection shut down */
  66. if (!n) {
  67. wsi->socket_is_permanently_unusable = 1;
  68. return LWS_SSL_CAPABLE_ERROR;
  69. }
  70. if (n < 0) {
  71. m = SSL_get_error(wsi->tls.ssl, n);
  72. lwsl_debug("%p: ssl err %d errno %d\n", wsi, m, errno);
  73. if (errno == LWS_ENOTCONN) {
  74. /* If the socket isn't connected anymore, bail out. */
  75. wsi->socket_is_permanently_unusable = 1;
  76. return LWS_SSL_CAPABLE_ERROR;
  77. }
  78. if (m == SSL_ERROR_ZERO_RETURN ||
  79. m == SSL_ERROR_SYSCALL)
  80. return LWS_SSL_CAPABLE_ERROR;
  81. if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
  82. lwsl_debug("%s: WANT_READ\n", __func__);
  83. lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
  84. return LWS_SSL_CAPABLE_MORE_SERVICE;
  85. }
  86. if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
  87. lwsl_debug("%s: WANT_WRITE\n", __func__);
  88. lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
  89. return LWS_SSL_CAPABLE_MORE_SERVICE;
  90. }
  91. wsi->socket_is_permanently_unusable = 1;
  92. return LWS_SSL_CAPABLE_ERROR;
  93. }
  94. #if 0
  95. /*
  96. * If using mbedtls type tls library, this is the earliest point for all
  97. * paths to dump what was received as decrypted data from the tls tunnel
  98. */
  99. lwsl_notice("%s: len %d\n", __func__, n);
  100. lwsl_hexdump_notice(buf, n);
  101. #endif
  102. lws_stats_bump(pt, LWSSTATS_B_READ, n);
  103. #if defined(LWS_WITH_SERVER_STATUS)
  104. if (wsi->a.vhost)
  105. wsi->a.vhost->conn_stats.rx += n;
  106. #endif
  107. #if defined(LWS_WITH_DETAILED_LATENCY)
  108. if (context->detailed_latency_cb) {
  109. wsi->detlat.req_size = len;
  110. wsi->detlat.acc_size = n;
  111. wsi->detlat.type = LDLT_READ;
  112. wsi->detlat.latencies[LAT_DUR_PROXY_RX_TO_ONWARD_TX] =
  113. lws_now_usecs() - pt->ust_left_poll;
  114. wsi->detlat.latencies[LAT_DUR_USERCB] = 0;
  115. lws_det_lat_cb(wsi->a.context, &wsi->detlat);
  116. }
  117. #endif
  118. /*
  119. * if it was our buffer that limited what we read,
  120. * check if SSL has additional data pending inside SSL buffers.
  121. *
  122. * Because these won't signal at the network layer with POLLIN
  123. * and if we don't realize, this data will sit there forever
  124. */
  125. if (n != len)
  126. goto bail;
  127. if (!wsi->tls.ssl)
  128. goto bail;
  129. if (SSL_pending(wsi->tls.ssl)) {
  130. if (lws_dll2_is_detached(&wsi->tls.dll_pending_tls))
  131. lws_dll2_add_head(&wsi->tls.dll_pending_tls,
  132. &pt->tls.dll_pending_tls_owner);
  133. } else
  134. __lws_ssl_remove_wsi_from_buffered_list(wsi);
  135. return n;
  136. bail:
  137. lws_ssl_remove_wsi_from_buffered_list(wsi);
  138. return n;
  139. }
  140. int
  141. lws_ssl_pending(struct lws *wsi)
  142. {
  143. if (!wsi->tls.ssl)
  144. return 0;
  145. return SSL_pending(wsi->tls.ssl);
  146. }
  147. int
  148. lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len)
  149. {
  150. int n, m;
  151. #if 0
  152. /*
  153. * If using mbedtls type tls library, this is the last point for all
  154. * paths before sending data into the tls tunnel, where you can dump it
  155. * and see what is being sent.
  156. */
  157. lwsl_notice("%s: len %d\n", __func__, len);
  158. lwsl_hexdump_notice(buf, len);
  159. #endif
  160. if (!wsi->tls.ssl)
  161. return lws_ssl_capable_write_no_ssl(wsi, buf, len);
  162. n = SSL_write(wsi->tls.ssl, buf, len);
  163. if (n > 0)
  164. return n;
  165. m = SSL_get_error(wsi->tls.ssl, n);
  166. if (m != SSL_ERROR_SYSCALL) {
  167. if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
  168. lwsl_notice("%s: want read\n", __func__);
  169. return LWS_SSL_CAPABLE_MORE_SERVICE;
  170. }
  171. if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
  172. lws_set_blocking_send(wsi);
  173. lwsl_debug("%s: want write\n", __func__);
  174. return LWS_SSL_CAPABLE_MORE_SERVICE;
  175. }
  176. }
  177. lwsl_debug("%s failed: %d\n",__func__, m);
  178. wsi->socket_is_permanently_unusable = 1;
  179. return LWS_SSL_CAPABLE_ERROR;
  180. }
  181. int openssl_SSL_CTX_private_data_index;
  182. void
  183. lws_ssl_info_callback(const SSL *ssl, int where, int ret)
  184. {
  185. struct lws *wsi;
  186. struct lws_context *context;
  187. struct lws_ssl_info si;
  188. context = (struct lws_context *)SSL_CTX_get_ex_data(
  189. SSL_get_SSL_CTX(ssl),
  190. openssl_SSL_CTX_private_data_index);
  191. if (!context)
  192. return;
  193. wsi = wsi_from_fd(context, SSL_get_fd(ssl));
  194. if (!wsi)
  195. return;
  196. if (!(where & wsi->a.vhost->tls.ssl_info_event_mask))
  197. return;
  198. si.where = where;
  199. si.ret = ret;
  200. if (user_callback_handle_rxflow(wsi->a.protocol->callback,
  201. wsi, LWS_CALLBACK_SSL_INFO,
  202. wsi->user_space, &si, 0))
  203. lws_set_timeout(wsi, PENDING_TIMEOUT_KILLED_BY_SSL_INFO, -1);
  204. }
  205. int
  206. lws_ssl_close(struct lws *wsi)
  207. {
  208. lws_sockfd_type n;
  209. if (!wsi->tls.ssl)
  210. return 0; /* not handled */
  211. #if defined (LWS_HAVE_SSL_SET_INFO_CALLBACK)
  212. /* kill ssl callbacks, becausse we will remove the fd from the
  213. * table linking it to the wsi
  214. */
  215. if (wsi->a.vhost->tls.ssl_info_event_mask)
  216. SSL_set_info_callback(wsi->tls.ssl, NULL);
  217. #endif
  218. n = SSL_get_fd(wsi->tls.ssl);
  219. if (!wsi->socket_is_permanently_unusable)
  220. SSL_shutdown(wsi->tls.ssl);
  221. compatible_close(n);
  222. SSL_free(wsi->tls.ssl);
  223. wsi->tls.ssl = NULL;
  224. lws_tls_restrict_return(wsi->a.context);
  225. return 1; /* handled */
  226. }
  227. void
  228. lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
  229. {
  230. if (vhost->tls.ssl_ctx)
  231. SSL_CTX_free(vhost->tls.ssl_ctx);
  232. if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
  233. SSL_CTX_free(vhost->tls.ssl_client_ctx);
  234. #if defined(LWS_WITH_ACME)
  235. lws_tls_acme_sni_cert_destroy(vhost);
  236. #endif
  237. }
  238. void
  239. lws_ssl_context_destroy(struct lws_context *context)
  240. {
  241. }
  242. lws_tls_ctx *
  243. lws_tls_ctx_from_wsi(struct lws *wsi)
  244. {
  245. if (!wsi->tls.ssl)
  246. return NULL;
  247. return SSL_get_SSL_CTX(wsi->tls.ssl);
  248. }
  249. enum lws_ssl_capable_status
  250. __lws_tls_shutdown(struct lws *wsi)
  251. {
  252. int n = SSL_shutdown(wsi->tls.ssl);
  253. lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
  254. switch (n) {
  255. case 1: /* successful completion */
  256. (void)shutdown(wsi->desc.sockfd, SHUT_WR);
  257. return LWS_SSL_CAPABLE_DONE;
  258. case 0: /* needs a retry */
  259. __lws_change_pollfd(wsi, 0, LWS_POLLIN);
  260. return LWS_SSL_CAPABLE_MORE_SERVICE;
  261. default: /* fatal error, or WANT */
  262. n = SSL_get_error(wsi->tls.ssl, n);
  263. if (n != SSL_ERROR_SYSCALL && n != SSL_ERROR_SSL) {
  264. if (SSL_want_read(wsi->tls.ssl)) {
  265. lwsl_debug("(wants read)\n");
  266. __lws_change_pollfd(wsi, 0, LWS_POLLIN);
  267. return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
  268. }
  269. if (SSL_want_write(wsi->tls.ssl)) {
  270. lwsl_debug("(wants write)\n");
  271. __lws_change_pollfd(wsi, 0, LWS_POLLOUT);
  272. return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
  273. }
  274. }
  275. return LWS_SSL_CAPABLE_ERROR;
  276. }
  277. }
  278. static int
  279. tops_fake_POLLIN_for_buffered_mbedtls(struct lws_context_per_thread *pt)
  280. {
  281. return lws_tls_fake_POLLIN_for_buffered(pt);
  282. }
  283. const struct lws_tls_ops tls_ops_mbedtls = {
  284. /* fake_POLLIN_for_buffered */ tops_fake_POLLIN_for_buffered_mbedtls,
  285. };