2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "core/private.h"
24 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
25 OPENSSL_VERSION_NUMBER >= 0x10002000L)
27 alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
28 const unsigned char *in, unsigned int inlen, void *arg)
30 #if !defined(LWS_WITH_MBEDTLS)
31 struct alpn_ctx *alpn_ctx = (struct alpn_ctx *)arg;
33 if (SSL_select_next_proto((unsigned char **)out, outlen, alpn_ctx->data,
34 alpn_ctx->len, in, inlen) !=
35 OPENSSL_NPN_NEGOTIATED)
36 return SSL_TLSEXT_ERR_NOACK;
39 return SSL_TLSEXT_ERR_OK;
44 lws_context_init_alpn(struct lws_vhost *vhost)
46 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
47 OPENSSL_VERSION_NUMBER >= 0x10002000L)
48 const char *alpn_comma = vhost->context->tls.alpn_default;
51 alpn_comma = vhost->tls.alpn;
53 lwsl_info(" Server '%s' advertising ALPN: %s\n",
54 vhost->name, alpn_comma);
55 vhost->tls.alpn_ctx.len = lws_alpn_comma_to_openssl(alpn_comma,
56 vhost->tls.alpn_ctx.data,
57 sizeof(vhost->tls.alpn_ctx.data) - 1);
59 SSL_CTX_set_alpn_select_cb(vhost->tls.ssl_ctx, alpn_cb,
60 &vhost->tls.alpn_ctx);
63 " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
64 OPENSSL_VERSION_NUMBER);
65 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
69 lws_tls_server_conn_alpn(struct lws *wsi)
71 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
72 OPENSSL_VERSION_NUMBER >= 0x10002000L)
73 const unsigned char *name = NULL;
80 SSL_get0_alpn_selected(wsi->tls.ssl, &name, &len);
82 lwsl_info("no ALPN upgrade\n");
86 if (len > sizeof(cstr) - 1)
87 len = sizeof(cstr) - 1;
89 memcpy(cstr, name, len);
92 lwsl_info("negotiated '%s' using ALPN\n", cstr);
93 wsi->tls.use_ssl |= LCCSCF_USE_SSL;
95 return lws_role_call_alpn_negotiated(wsi, (const char *)cstr);
96 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
101 #if !defined(LWS_NO_SERVER)
104 lws_sul_tls_cb(lws_sorted_usec_list_t *sul)
106 struct lws_context_per_thread *pt = lws_container_of(sul,
107 struct lws_context_per_thread, sul_tls);
109 lws_tls_check_all_cert_lifetimes(pt->context);
111 __lws_sul_insert(&pt->pt_sul_owner, &pt->sul_tls,
112 (lws_usec_t)24 * 3600 * LWS_US_PER_SEC);
116 lws_context_init_server_ssl(const struct lws_context_creation_info *info,
117 struct lws_vhost *vhost)
119 struct lws_context *context = vhost->context;
122 if (!lws_check_opt(info->options,
123 LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
124 vhost->tls.use_ssl = 0;
130 * If he is giving a server cert, take it as a sign he wants to use
131 * it on this vhost. User code can leave the cert filepath NULL and
132 * set the LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX option itself, in
133 * which case he's expected to set up the cert himself at
134 * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, which
135 * provides the vhost SSL_CTX * in the user parameter.
137 if (info->ssl_cert_filepath || info->server_ssl_cert_mem)
138 vhost->options |= LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX;
140 if (info->port != CONTEXT_PORT_NO_LISTEN) {
142 vhost->tls.use_ssl = lws_check_opt(vhost->options,
143 LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX);
145 if (vhost->tls.use_ssl && info->ssl_cipher_list)
146 lwsl_notice(" SSL ciphers: '%s'\n",
147 info->ssl_cipher_list);
149 if (vhost->tls.use_ssl)
150 lwsl_notice(" Using SSL mode\n");
152 lwsl_notice(" Using non-SSL mode\n");
156 * give him a fake wsi with context + vhost set, so he can use
157 * lws_get_context() in the callback
159 memset(&wsi, 0, sizeof(wsi));
160 wsi.vhost = vhost; /* not a real bound wsi */
161 wsi.context = context;
164 * as a server, if we are requiring clients to identify themselves
165 * then set the backend up for it
167 if (lws_check_opt(info->options,
168 LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT))
169 /* Normally SSL listener rejects non-ssl, optionally allow */
170 vhost->tls.allow_non_ssl_on_ssl_port = 1;
173 * give user code a chance to load certs into the server
174 * allowing it to verify incoming client certs
176 if (vhost->tls.use_ssl) {
177 if (lws_tls_server_vhost_backend_init(info, vhost, &wsi))
180 lws_tls_server_client_cert_verify_config(vhost);
182 if (vhost->protocols[0].callback(&wsi,
183 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
184 vhost->tls.ssl_ctx, vhost, 0))
188 if (vhost->tls.use_ssl)
189 lws_context_init_alpn(vhost);
191 /* check certs once a day */
193 context->pt[0].sul_tls.cb = lws_sul_tls_cb;
194 __lws_sul_insert(&context->pt[0].pt_sul_owner, &context->pt[0].sul_tls,
195 (lws_usec_t)24 * 3600 * LWS_US_PER_SEC);
202 lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
204 struct lws_context *context = wsi->context;
205 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
206 struct lws_vhost *vh;
212 if (!LWS_SSL_ENABLED(wsi->vhost))
215 switch (lwsi_state(wsi)) {
219 lwsl_err("%s: leaking ssl\n", __func__);
220 if (accept_fd == LWS_SOCK_INVALID)
222 if (context->simultaneous_ssl_restriction &&
223 context->simultaneous_ssl >=
224 context->simultaneous_ssl_restriction) {
225 lwsl_notice("unable to deal with SSL connection\n");
229 if (lws_tls_server_new_nonblocking(wsi, accept_fd)) {
230 if (accept_fd != LWS_SOCK_INVALID)
231 compatible_close(accept_fd);
235 if (context->simultaneous_ssl_restriction &&
236 ++context->simultaneous_ssl ==
237 context->simultaneous_ssl_restriction)
238 /* that was the last allowed SSL connection */
239 lws_gate_accepts(context, 0);
241 #if defined(LWS_WITH_STATS)
242 context->updated = 1;
245 * we are not accepted yet, but we need to enter ourselves
246 * as a live connection. That way we can retry when more
247 * pieces come if we're not sorted yet
249 lwsi_set_state(wsi, LRS_SSL_ACK_PENDING);
251 lws_pt_lock(pt, __func__);
252 if (__insert_wsi_socket_into_fds(context, wsi)) {
253 lwsl_err("%s: failed to insert into fds\n", __func__);
258 lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
259 context->timeout_secs);
261 lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n");
265 case LRS_SSL_ACK_PENDING:
267 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
268 lwsl_err("%s: lws_change_pollfd failed\n", __func__);
272 lws_latency_pre(context, wsi);
274 if (wsi->vhost->tls.allow_non_ssl_on_ssl_port) {
276 n = recv(wsi->desc.sockfd, (char *)pt->serv_buf,
277 context->pt_serv_buf_size, MSG_PEEK);
280 * We have LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT..
281 * this just means don't hang up on him because of no
282 * tls hello... what happens next is driven by
283 * additional option flags:
285 * none: fail the connection
287 * LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS:
288 * Destroy the TLS, issue a redirect using plaintext
289 * http (this may not be accepted by a client that
290 * has visited the site before and received an STS
293 * LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER:
294 * Destroy the TLS, continue and serve normally
297 * LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG:
298 * Destroy the TLS, apply whatever role and protocol
299 * were told in the vhost info struct
300 * .listen_accept_role / .listen_accept_protocol and
304 if (n >= 1 && pt->serv_buf[0] >= ' ') {
306 * TLS content-type for Handshake is 0x16, and
307 * for ChangeCipherSpec Record, it's 0x14
309 * A non-ssl session will start with the HTTP
310 * method in ASCII. If we see it's not a legit
311 * SSL handshake kill the SSL for this
312 * connection and try to handle as a HTTP
313 * connection upgrade directly.
315 wsi->tls.use_ssl = 0;
317 lws_tls_server_abort_connection(wsi);
319 * care... this creates wsi with no ssl when ssl
320 * is enabled and normally mandatory
324 if (lws_check_opt(wsi->vhost->options,
325 LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS)) {
326 lwsl_info("%s: redirecting from http "
327 "to https\n", __func__);
328 wsi->tls.redirect_to_https = 1;
332 if (lws_check_opt(wsi->vhost->options,
333 LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER)) {
334 lwsl_info("%s: allowing unencrypted "
335 "http service on tls port\n",
340 if (lws_check_opt(wsi->vhost->options,
341 LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG)) {
342 if (lws_http_to_fallback(wsi, NULL, 0))
344 lwsl_info("%s: allowing non-tls "
345 "fallback\n", __func__);
349 lwsl_notice("%s: client did not send a valid "
350 "tls hello (default vhost %s)\n",
351 __func__, wsi->vhost->name);
356 * connection is gone, fail out
358 lwsl_debug("PEEKed 0\n");
361 if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
362 LWS_ERRNO == LWS_EWOULDBLOCK)) {
364 * well, we get no way to know ssl or not
365 * so go around again waiting for something
366 * to come and give us a hint, or timeout the
369 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
370 lwsl_info("%s: change_pollfd failed\n",
375 lwsl_info("SSL_ERROR_WANT_READ\n");
380 /* normal SSL connection processing path */
382 #if defined(LWS_WITH_STATS)
383 /* only set this the first time around */
384 if (!wsi->accept_start_us)
385 wsi->accept_start_us = lws_now_usecs();
388 lws_stats_bump(pt, LWSSTATS_C_SSL_ACCEPT_SPIN, 1);
389 n = lws_tls_server_accept(wsi);
390 lws_latency(context, wsi,
391 "SSL_accept LRS_SSL_ACK_PENDING\n", n, n == 1);
392 lwsl_info("SSL_accept says %d\n", n);
394 case LWS_SSL_CAPABLE_DONE:
396 case LWS_SSL_CAPABLE_ERROR:
397 lws_stats_bump(pt, LWSSTATS_C_SSL_CONNECTIONS_FAILED, 1);
398 lwsl_info("SSL_accept failed socket %u: %d\n",
399 wsi->desc.sockfd, n);
400 wsi->socket_is_permanently_unusable = 1;
403 default: /* MORE_SERVICE */
407 lws_stats_bump(pt, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED, 1);
408 #if defined(LWS_WITH_STATS)
409 if (wsi->accept_start_us)
411 LWSSTATS_US_SSL_ACCEPT_LATENCY_AVG,
413 wsi->accept_start_us);
414 wsi->accept_start_us = lws_now_usecs();
417 /* adapt our vhost to match the SNI SSL_CTX that was chosen */
418 vh = context->vhost_list;
420 if (!vh->being_destroyed && wsi->tls.ssl &&
421 vh->tls.ssl_ctx == lws_tls_ctx_from_wsi(wsi)) {
422 lwsl_info("setting wsi to vh %s\n", vh->name);
423 lws_vhost_bind_wsi(vh, wsi);
429 /* OK, we are accepted... give him some time to negotiate */
430 lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
431 context->timeout_secs);
433 lwsi_set_state(wsi, LRS_ESTABLISHED);
434 if (lws_tls_server_conn_alpn(wsi))
436 lwsl_debug("accepted new SSL conn\n");
446 lwsi_set_state(wsi, LRS_ESTABLISHED);