Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / tls / tls-server.c
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
22 #include "core/private.h"
23
24 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
25                                   OPENSSL_VERSION_NUMBER >= 0x10002000L)
26 static int
27 alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen,
28         const unsigned char *in, unsigned int inlen, void *arg)
29 {
30 #if !defined(LWS_WITH_MBEDTLS)
31         struct alpn_ctx *alpn_ctx = (struct alpn_ctx *)arg;
32
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;
37 #endif
38
39         return SSL_TLSEXT_ERR_OK;
40 }
41 #endif
42
43 void
44 lws_context_init_alpn(struct lws_vhost *vhost)
45 {
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;
49
50         if (vhost->tls.alpn)
51                 alpn_comma = vhost->tls.alpn;
52
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);
58
59         SSL_CTX_set_alpn_select_cb(vhost->tls.ssl_ctx, alpn_cb,
60                                    &vhost->tls.alpn_ctx);
61 #else
62         lwsl_err(
63                 " HTTP2 / ALPN configured but not supported by OpenSSL 0x%lx\n",
64                     OPENSSL_VERSION_NUMBER);
65 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
66 }
67
68 int
69 lws_tls_server_conn_alpn(struct lws *wsi)
70 {
71 #if defined(LWS_WITH_MBEDTLS) || (defined(OPENSSL_VERSION_NUMBER) && \
72                                   OPENSSL_VERSION_NUMBER >= 0x10002000L)
73         const unsigned char *name = NULL;
74         char cstr[10];
75         unsigned len;
76
77         if (!wsi->tls.ssl)
78                 return 0;
79
80         SSL_get0_alpn_selected(wsi->tls.ssl, &name, &len);
81         if (!len) {
82                 lwsl_info("no ALPN upgrade\n");
83                 return 0;
84         }
85
86         if (len > sizeof(cstr) - 1)
87                 len = sizeof(cstr) - 1;
88
89         memcpy(cstr, name, len);
90         cstr[len] = '\0';
91
92         lwsl_info("negotiated '%s' using ALPN\n", cstr);
93         wsi->tls.use_ssl |= LCCSCF_USE_SSL;
94
95         return lws_role_call_alpn_negotiated(wsi, (const char *)cstr);
96 #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
97
98         return 0;
99 }
100
101 #if !defined(LWS_NO_SERVER)
102
103 static void
104 lws_sul_tls_cb(lws_sorted_usec_list_t *sul)
105 {
106         struct lws_context_per_thread *pt = lws_container_of(sul,
107                         struct lws_context_per_thread, sul_tls);
108
109         lws_tls_check_all_cert_lifetimes(pt->context);
110
111         __lws_sul_insert(&pt->pt_sul_owner, &pt->sul_tls,
112                          (lws_usec_t)24 * 3600 * LWS_US_PER_SEC);
113 }
114
115 LWS_VISIBLE int
116 lws_context_init_server_ssl(const struct lws_context_creation_info *info,
117                             struct lws_vhost *vhost)
118 {
119         struct lws_context *context = vhost->context;
120         struct lws wsi;
121
122         if (!lws_check_opt(info->options,
123                            LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
124                 vhost->tls.use_ssl = 0;
125
126                 return 0;
127         }
128
129         /*
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.
136          */
137         if (info->ssl_cert_filepath || info->server_ssl_cert_mem)
138                 vhost->options |= LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX;
139
140         if (info->port != CONTEXT_PORT_NO_LISTEN) {
141
142                 vhost->tls.use_ssl = lws_check_opt(vhost->options,
143                                         LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX);
144
145                 if (vhost->tls.use_ssl && info->ssl_cipher_list)
146                         lwsl_notice(" SSL ciphers: '%s'\n",
147                                                 info->ssl_cipher_list);
148
149                 if (vhost->tls.use_ssl)
150                         lwsl_notice(" Using SSL mode\n");
151                 else
152                         lwsl_notice(" Using non-SSL mode\n");
153         }
154
155         /*
156          * give him a fake wsi with context + vhost set, so he can use
157          * lws_get_context() in the callback
158          */
159         memset(&wsi, 0, sizeof(wsi));
160         wsi.vhost = vhost; /* not a real bound wsi */
161         wsi.context = context;
162
163         /*
164          * as a server, if we are requiring clients to identify themselves
165          * then set the backend up for it
166          */
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;
171
172         /*
173          * give user code a chance to load certs into the server
174          * allowing it to verify incoming client certs
175          */
176         if (vhost->tls.use_ssl) {
177                 if (lws_tls_server_vhost_backend_init(info, vhost, &wsi))
178                         return -1;
179
180                 lws_tls_server_client_cert_verify_config(vhost);
181
182                 if (vhost->protocols[0].callback(&wsi,
183                             LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
184                             vhost->tls.ssl_ctx, vhost, 0))
185                         return -1;
186         }
187
188         if (vhost->tls.use_ssl)
189                 lws_context_init_alpn(vhost);
190
191         /* check certs once a day */
192
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);
196
197         return 0;
198 }
199 #endif
200
201 LWS_VISIBLE int
202 lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
203 {
204         struct lws_context *context = wsi->context;
205         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
206         struct lws_vhost *vh;
207         char buf[256];
208         int n;
209
210         (void)buf;
211
212         if (!LWS_SSL_ENABLED(wsi->vhost))
213                 return 0;
214
215         switch (lwsi_state(wsi)) {
216         case LRS_SSL_INIT:
217
218                 if (wsi->tls.ssl)
219                         lwsl_err("%s: leaking ssl\n", __func__);
220                 if (accept_fd == LWS_SOCK_INVALID)
221                         assert(0);
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");
226                         return 1;
227                 }
228
229                 if (lws_tls_server_new_nonblocking(wsi, accept_fd)) {
230                         if (accept_fd != LWS_SOCK_INVALID)
231                                 compatible_close(accept_fd);
232                         goto fail;
233                 }
234
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);
240
241 #if defined(LWS_WITH_STATS)
242                 context->updated = 1;
243 #endif
244                 /*
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
248                  */
249                 lwsi_set_state(wsi, LRS_SSL_ACK_PENDING);
250
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__);
254                         goto fail;
255                 }
256                 lws_pt_unlock(pt);
257
258                 lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
259                                 context->timeout_secs);
260
261                 lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n");
262
263                 /* fallthru */
264
265         case LRS_SSL_ACK_PENDING:
266
267                 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
268                         lwsl_err("%s: lws_change_pollfd failed\n", __func__);
269                         goto fail;
270                 }
271
272                 lws_latency_pre(context, wsi);
273
274                 if (wsi->vhost->tls.allow_non_ssl_on_ssl_port) {
275
276                         n = recv(wsi->desc.sockfd, (char *)pt->serv_buf,
277                                  context->pt_serv_buf_size, MSG_PEEK);
278
279                         /*
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:
284                          *
285                          * none: fail the connection
286                          *
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
291                          *     header).
292                          *
293                          * LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER:
294                          *     Destroy the TLS, continue and serve normally
295                          *     using http
296                          *
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
301                          *     continue with that
302                          */
303
304                         if (n >= 1 && pt->serv_buf[0] >= ' ') {
305                                 /*
306                                 * TLS content-type for Handshake is 0x16, and
307                                 * for ChangeCipherSpec Record, it's 0x14
308                                 *
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.
314                                 */
315                                 wsi->tls.use_ssl = 0;
316
317                                 lws_tls_server_abort_connection(wsi);
318                                 /*
319                                  * care... this creates wsi with no ssl when ssl
320                                  * is enabled and normally mandatory
321                                  */
322                                 wsi->tls.ssl = NULL;
323
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;
329                                         goto notls_accepted;
330                                 }
331
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",
336                                                   __func__);
337                                         goto notls_accepted;
338                                 }
339
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))
343                                                 goto fail;
344                                         lwsl_info("%s: allowing non-tls "
345                                                   "fallback\n", __func__);
346                                         goto notls_accepted;
347                                 }
348
349                                 lwsl_notice("%s: client did not send a valid "
350                                             "tls hello (default vhost %s)\n",
351                                             __func__, wsi->vhost->name);
352                                 goto fail;
353                         }
354                         if (!n) {
355                                 /*
356                                  * connection is gone, fail out
357                                  */
358                                 lwsl_debug("PEEKed 0\n");
359                                 goto fail;
360                         }
361                         if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
362                                       LWS_ERRNO == LWS_EWOULDBLOCK)) {
363                                 /*
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
367                                  * connection.
368                                  */
369                                 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
370                                         lwsl_info("%s: change_pollfd failed\n",
371                                                   __func__);
372                                         return -1;
373                                 }
374
375                                 lwsl_info("SSL_ERROR_WANT_READ\n");
376                                 return 0;
377                         }
378                 }
379
380                 /* normal SSL connection processing path */
381
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();
386 #endif
387                 errno = 0;
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);
393                 switch (n) {
394                 case LWS_SSL_CAPABLE_DONE:
395                         break;
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;
401                         goto fail;
402
403                 default: /* MORE_SERVICE */
404                         return 0;
405                 }
406
407                 lws_stats_bump(pt, LWSSTATS_C_SSL_CONNECTIONS_ACCEPTED, 1);
408 #if defined(LWS_WITH_STATS)
409                 if (wsi->accept_start_us)
410                         lws_stats_bump(pt,
411                                       LWSSTATS_US_SSL_ACCEPT_LATENCY_AVG,
412                                       lws_now_usecs() -
413                                               wsi->accept_start_us);
414                 wsi->accept_start_us = lws_now_usecs();
415 #endif
416
417                 /* adapt our vhost to match the SNI SSL_CTX that was chosen */
418                 vh = context->vhost_list;
419                 while (vh) {
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);
424                                 break;
425                         }
426                         vh = vh->vhost_next;
427                 }
428
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);
432
433                 lwsi_set_state(wsi, LRS_ESTABLISHED);
434                 if (lws_tls_server_conn_alpn(wsi))
435                         goto fail;
436                 lwsl_debug("accepted new SSL conn\n");
437                 break;
438
439         default:
440                 break;
441         }
442
443         return 0;
444
445 notls_accepted:
446         lwsi_set_state(wsi, LRS_ESTABLISHED);
447
448         return 0;
449
450 fail:
451         return 1;
452 }
453