tls-server.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "core/private.h"
  22. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  23. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  24. static int
  25. alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
  26. const unsigned char *in, unsigned int inlen, void *arg)
  27. {
  28. #if !defined(LWS_WITH_MBEDTLS)
  29. struct alpn_ctx *alpn_ctx = (struct alpn_ctx *)arg;
  30. if (SSL_select_next_proto((unsigned char **)out, outlen, alpn_ctx->data,
  31. alpn_ctx->len, in, inlen) !=
  32. OPENSSL_NPN_NEGOTIATED)
  33. return SSL_TLSEXT_ERR_NOACK;
  34. #endif
  35. return SSL_TLSEXT_ERR_OK;
  36. }
  37. #endif
  38. void
  39. lws_context_init_alpn(struct lws_vhost *vhost)
  40. {
  41. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  42. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  43. const char *alpn_comma = vhost->context->tls.alpn_default;
  44. if (vhost->tls.alpn)
  45. alpn_comma = vhost->tls.alpn;
  46. lwsl_info(" Server '%s' advertising ALPN: %s\n",
  47. vhost->name, alpn_comma);
  48. vhost->tls.alpn_ctx.len = lws_alpn_comma_to_openssl(alpn_comma,
  49. vhost->tls.alpn_ctx.data,
  50. sizeof(vhost->tls.alpn_ctx.data) - 1);
  51. SSL_CTX_set_alpn_select_cb(vhost->tls.ssl_ctx, alpn_cb, &vhost->tls.alpn_ctx);
  52. #else
  53. lwsl_err(
  54. " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
  55. OPENSSL_VERSION_NUMBER);
  56. #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
  57. }
  58. int
  59. lws_tls_server_conn_alpn(struct lws *wsi)
  60. {
  61. #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
  62. OPENSSL_VERSION_NUMBER >= 0x10002000L)
  63. const unsigned char *name = NULL;
  64. char cstr[10];
  65. unsigned len;
  66. SSL_get0_alpn_selected(wsi->tls.ssl, &name, &len);
  67. if (!len) {
  68. lwsl_info("no ALPN upgrade\n");
  69. return 0;
  70. }
  71. if (len > sizeof(cstr) - 1)
  72. len = sizeof(cstr) - 1;
  73. memcpy(cstr, name, len);
  74. cstr[len] = '\0';
  75. lwsl_info("negotiated '%s' using ALPN\n", cstr);
  76. wsi->tls.use_ssl |= LCCSCF_USE_SSL;
  77. return lws_role_call_alpn_negotiated(wsi, (const char *)cstr);
  78. #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
  79. return 0;
  80. }
  81. LWS_VISIBLE int
  82. lws_context_init_server_ssl(const struct lws_context_creation_info *info,
  83. struct lws_vhost *vhost)
  84. {
  85. struct lws_context *context = vhost->context;
  86. struct lws wsi;
  87. if (!lws_check_opt(info->options,
  88. LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
  89. vhost->tls.use_ssl = 0;
  90. return 0;
  91. }
  92. /*
  93. * If he is giving a cert filepath, take it as a sign he wants to use
  94. * it on this vhost. User code can leave the cert filepath NULL and
  95. * set the LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX option itself, in
  96. * which case he's expected to set up the cert himself at
  97. * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, which
  98. * provides the vhost SSL_CTX * in the user parameter.
  99. */
  100. if (info->ssl_cert_filepath)
  101. vhost->options |= LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX;
  102. if (info->port != CONTEXT_PORT_NO_LISTEN) {
  103. vhost->tls.use_ssl = lws_check_opt(vhost->options,
  104. LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX);
  105. if (vhost->tls.use_ssl && info->ssl_cipher_list)
  106. lwsl_notice(" SSL ciphers: '%s'\n",
  107. info->ssl_cipher_list);
  108. if (vhost->tls.use_ssl)
  109. lwsl_notice(" Using SSL mode\n");
  110. else
  111. lwsl_notice(" Using non-SSL mode\n");
  112. }
  113. /*
  114. * give him a fake wsi with context + vhost set, so he can use
  115. * lws_get_context() in the callback
  116. */
  117. memset(&wsi, 0, sizeof(wsi));
  118. wsi.vhost = vhost;
  119. wsi.context = context;
  120. /*
  121. * as a server, if we are requiring clients to identify themselves
  122. * then set the backend up for it
  123. */
  124. if (lws_check_opt(info->options,
  125. LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT))
  126. /* Normally SSL listener rejects non-ssl, optionally allow */
  127. vhost->tls.allow_non_ssl_on_ssl_port = 1;
  128. /*
  129. * give user code a chance to load certs into the server
  130. * allowing it to verify incoming client certs
  131. */
  132. if (vhost->tls.use_ssl) {
  133. if (lws_tls_server_vhost_backend_init(info, vhost, &wsi))
  134. return -1;
  135. lws_tls_server_client_cert_verify_config(vhost);
  136. if (vhost->protocols[0].callback(&wsi,
  137. LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
  138. vhost->tls.ssl_ctx, vhost, 0))
  139. return -1;
  140. }
  141. if (vhost->tls.use_ssl)
  142. lws_context_init_alpn(vhost);
  143. return 0;
  144. }
  145. LWS_VISIBLE int
  146. lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
  147. {
  148. struct lws_context *context = wsi->context;
  149. struct lws_vhost *vh;
  150. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  151. int n;
  152. char buf[256];
  153. (void)buf;
  154. if (!LWS_SSL_ENABLED(wsi->vhost))
  155. return 0;
  156. switch (lwsi_state(wsi)) {
  157. case LRS_SSL_INIT:
  158. if (wsi->tls.ssl)
  159. lwsl_err("%s: leaking ssl\n", __func__);
  160. if (accept_fd == LWS_SOCK_INVALID)
  161. assert(0);
  162. if (context->simultaneous_ssl_restriction &&
  163. context->simultaneous_ssl >=
  164. context->simultaneous_ssl_restriction) {
  165. lwsl_notice("unable to deal with SSL connection\n");
  166. return 1;
  167. }
  168. if (lws_tls_server_new_nonblocking(wsi, accept_fd)) {
  169. if (accept_fd != LWS_SOCK_INVALID)
  170. compatible_close(accept_fd);
  171. goto fail;
  172. }
  173. if (context->simultaneous_ssl_restriction &&
  174. ++context->simultaneous_ssl ==
  175. context->simultaneous_ssl_restriction)
  176. /* that was the last allowed SSL connection */
  177. lws_gate_accepts(context, 0);
  178. #if defined(LWS_WITH_STATS)
  179. context->updated = 1;
  180. #endif
  181. /*
  182. * we are not accepted yet, but we need to enter ourselves
  183. * as a live connection. That way we can retry when more
  184. * pieces come if we're not sorted yet
  185. */
  186. lwsi_set_state(wsi, LRS_SSL_ACK_PENDING);
  187. lws_pt_lock(pt, __func__);
  188. if (__insert_wsi_socket_into_fds(context, wsi)) {
  189. lwsl_err("%s: failed to insert into fds\n", __func__);
  190. goto fail;
  191. }
  192. lws_pt_unlock(pt);
  193. lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
  194. context->timeout_secs);
  195. lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n");
  196. /* fallthru */
  197. case LRS_SSL_ACK_PENDING:
  198. if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
  199. lwsl_err("%s: lws_change_pollfd failed\n", __func__);
  200. goto fail;
  201. }
  202. lws_latency_pre(context, wsi);
  203. if (wsi->vhost->tls.allow_non_ssl_on_ssl_port) {
  204. n = recv(wsi->desc.sockfd, (char *)pt->serv_buf,
  205. context->pt_serv_buf_size, MSG_PEEK);
  206. /*
  207. * optionally allow non-SSL connect on SSL listening socket
  208. * This is disabled by default, if enabled it goes around any
  209. * SSL-level access control (eg, client-side certs) so leave
  210. * it disabled unless you know it's not a problem for you
  211. */
  212. if (n >= 1 && pt->serv_buf[0] >= ' ') {
  213. /*
  214. * TLS content-type for Handshake is 0x16, and
  215. * for ChangeCipherSpec Record, it's 0x14
  216. *
  217. * A non-ssl session will start with the HTTP
  218. * method in ASCII. If we see it's not a legit
  219. * SSL handshake kill the SSL for this
  220. * connection and try to handle as a HTTP
  221. * connection upgrade directly.
  222. */
  223. wsi->tls.use_ssl = 0;
  224. lws_tls_server_abort_connection(wsi);
  225. /*
  226. * care... this creates wsi with no ssl
  227. * when ssl is enabled and normally
  228. * mandatory
  229. */
  230. wsi->tls.ssl = NULL;
  231. if (lws_check_opt(context->options,
  232. LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS))
  233. wsi->tls.redirect_to_https = 1;
  234. lwsl_debug("accepted as non-ssl\n");
  235. goto accepted;
  236. }
  237. if (!n) {
  238. /*
  239. * connection is gone, fail out
  240. */
  241. lwsl_debug("PEEKed 0\n");
  242. goto fail;
  243. }
  244. if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
  245. LWS_ERRNO == LWS_EWOULDBLOCK)) {
  246. /*
  247. * well, we get no way to know ssl or not
  248. * so go around again waiting for something
  249. * to come and give us a hint, or timeout the
  250. * connection.
  251. */
  252. if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
  253. lwsl_info("%s: change_pollfd failed\n",
  254. __func__);
  255. return -1;
  256. }
  257. lwsl_info("SSL_ERROR_WANT_READ\n");
  258. return 0;
  259. }
  260. }
  261. /* normal SSL connection processing path */
  262. #if defined(LWS_WITH_STATS)
  263. if (!wsi->accept_start_us)
  264. wsi->accept_start_us = time_in_microseconds();
  265. #endif
  266. errno = 0;
  267. lws_stats_atomic_bump(wsi->context, pt,
  268. LWSSTATS_C_SSL_CONNECTIONS_ACCEPT_SPIN, 1);
  269. n = lws_tls_server_accept(wsi);
  270. lws_latency(context, wsi,
  271. "SSL_accept LRS_SSL_ACK_PENDING\n", n, n == 1);
  272. lwsl_info("SSL_accept says %d\n", n);
  273. switch (n) {
  274. case LWS_SSL_CAPABLE_DONE:
  275. break;
  276. case LWS_SSL_CAPABLE_ERROR:
  277. lws_stats_atomic_bump(wsi->context, pt,
  278. LWSSTATS_C_SSL_CONNECTIONS_FAILED, 1);
  279. lwsl_info("SSL_accept failed socket %u: %d\n",
  280. wsi->desc.sockfd, n);
  281. wsi->socket_is_permanently_unusable = 1;
  282. goto fail;
  283. default: /* MORE_SERVICE */
  284. return 0;
  285. }
  286. lws_stats_atomic_bump(wsi->context, pt,
  287. LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED, 1);
  288. #if defined(LWS_WITH_STATS)
  289. lws_stats_atomic_bump(wsi->context, pt,
  290. LWSSTATS_MS_SSL_CONNECTIONS_ACCEPTED_DELAY,
  291. time_in_microseconds() - wsi->accept_start_us);
  292. wsi->accept_start_us = time_in_microseconds();
  293. #endif
  294. accepted:
  295. /* adapt our vhost to match the SNI SSL_CTX that was chosen */
  296. vh = context->vhost_list;
  297. while (vh) {
  298. if (!vh->being_destroyed && wsi->tls.ssl &&
  299. vh->tls.ssl_ctx == lws_tls_ctx_from_wsi(wsi)) {
  300. lwsl_info("setting wsi to vh %s\n", vh->name);
  301. wsi->vhost = vh;
  302. break;
  303. }
  304. vh = vh->vhost_next;
  305. }
  306. /* OK, we are accepted... give him some time to negotiate */
  307. lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
  308. context->timeout_secs);
  309. lwsi_set_state(wsi, LRS_ESTABLISHED);
  310. if (lws_tls_server_conn_alpn(wsi))
  311. goto fail;
  312. lwsl_debug("accepted new SSL conn\n");
  313. break;
  314. default:
  315. break;
  316. }
  317. return 0;
  318. fail:
  319. return 1;
  320. }