/src/libwebsockets/lib/tls/tls-server.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
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 | | |
25 | | #include "private-lib-core.h" |
26 | | |
27 | | #if defined(LWS_WITH_SERVER) |
28 | | |
29 | | static void |
30 | | lws_sul_tls_cb(lws_sorted_usec_list_t *sul) |
31 | 0 | { |
32 | 0 | struct lws_context_per_thread *pt = lws_container_of(sul, |
33 | 0 | struct lws_context_per_thread, sul_tls); |
34 | |
|
35 | 0 | lws_tls_check_all_cert_lifetimes(pt->context); |
36 | |
|
37 | 0 | __lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED], |
38 | 0 | &pt->sul_tls, |
39 | 0 | (lws_usec_t)24 * 3600 * LWS_US_PER_SEC); |
40 | 0 | } |
41 | | |
42 | | int |
43 | | lws_context_init_server_ssl(const struct lws_context_creation_info *info, |
44 | | struct lws_vhost *vhost) |
45 | 0 | { |
46 | 0 | struct lws_context *context = vhost->context; |
47 | 0 | lws_fakewsi_def_plwsa(&vhost->context->pt[0]); |
48 | |
|
49 | 0 | lws_fakewsi_prep_plwsa_ctx(vhost->context); |
50 | |
|
51 | 0 | if (!lws_check_opt(info->options, |
52 | 0 | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) { |
53 | 0 | vhost->tls.use_ssl = 0; |
54 | |
|
55 | 0 | return 0; |
56 | 0 | } |
57 | | |
58 | | /* |
59 | | * If he is giving a server cert, take it as a sign he wants to use |
60 | | * it on this vhost. User code can leave the cert filepath NULL and |
61 | | * set the LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX option itself, in |
62 | | * which case he's expected to set up the cert himself at |
63 | | * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, which |
64 | | * provides the vhost SSL_CTX * in the user parameter. |
65 | | */ |
66 | 0 | if (info->ssl_cert_filepath || info->server_ssl_cert_mem) |
67 | 0 | vhost->options |= LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX; |
68 | |
|
69 | 0 | if (info->port != CONTEXT_PORT_NO_LISTEN) { |
70 | |
|
71 | 0 | vhost->tls.use_ssl = lws_check_opt(vhost->options, |
72 | 0 | LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX); |
73 | |
|
74 | 0 | if (vhost->tls.use_ssl && info->ssl_cipher_list) |
75 | 0 | lwsl_notice(" SSL ciphers: '%s'\n", |
76 | 0 | info->ssl_cipher_list); |
77 | |
|
78 | 0 | lwsl_notice(" Vhost '%s' using %sTLS mode\n", |
79 | 0 | vhost->name, vhost->tls.use_ssl ? "" : "non-"); |
80 | 0 | } |
81 | | |
82 | | /* |
83 | | * give him a fake wsi with context + vhost set, so he can use |
84 | | * lws_get_context() in the callback |
85 | | */ |
86 | 0 | plwsa->vhost = vhost; /* not a real bound wsi */ |
87 | | |
88 | | /* |
89 | | * as a server, if we are requiring clients to identify themselves |
90 | | * then set the backend up for it |
91 | | */ |
92 | 0 | if (lws_check_opt(info->options, |
93 | 0 | LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT)) |
94 | | /* Normally SSL listener rejects non-ssl, optionally allow */ |
95 | 0 | vhost->tls.allow_non_ssl_on_ssl_port = 1; |
96 | | |
97 | | /* |
98 | | * give user code a chance to load certs into the server |
99 | | * allowing it to verify incoming client certs |
100 | | */ |
101 | 0 | if (vhost->tls.use_ssl) { |
102 | 0 | if (lws_tls_server_vhost_backend_init(info, vhost, (struct lws *)plwsa)) |
103 | 0 | return -1; |
104 | | |
105 | 0 | lws_tls_server_client_cert_verify_config(vhost); |
106 | |
|
107 | 0 | if (vhost->protocols[0].callback((struct lws *)plwsa, |
108 | 0 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
109 | 0 | vhost->tls.ssl_ctx, vhost, 0)) |
110 | 0 | return -1; |
111 | 0 | } |
112 | | |
113 | 0 | if (vhost->tls.use_ssl) |
114 | 0 | lws_context_init_alpn(vhost); |
115 | | |
116 | | /* check certs in a few seconds (after protocol init) and then once a day */ |
117 | |
|
118 | 0 | context->pt[0].sul_tls.cb = lws_sul_tls_cb; |
119 | 0 | __lws_sul_insert_us(&context->pt[0].pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED], |
120 | 0 | &context->pt[0].sul_tls, |
121 | 0 | (lws_usec_t)5 * LWS_US_PER_SEC); |
122 | |
|
123 | 0 | return 0; |
124 | 0 | } |
125 | | #endif |
126 | | |
127 | | int |
128 | | lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd, char from_pollin) |
129 | 0 | { |
130 | 0 | struct lws_context *context = wsi->a.context; |
131 | 0 | struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi]; |
132 | 0 | struct lws_vhost *vh; |
133 | 0 | ssize_t s; |
134 | 0 | int n; |
135 | |
|
136 | 0 | if (!LWS_SSL_ENABLED(wsi->a.vhost)) |
137 | 0 | return 0; |
138 | | |
139 | 0 | switch (lwsi_state(wsi)) { |
140 | 0 | case LRS_SSL_INIT: |
141 | |
|
142 | 0 | if (wsi->tls.ssl) |
143 | 0 | lwsl_err("%s: leaking ssl\n", __func__); |
144 | 0 | if (accept_fd == LWS_SOCK_INVALID) |
145 | 0 | assert(0); |
146 | | |
147 | 0 | if (lws_tls_restrict_borrow(wsi)) { |
148 | 0 | lwsl_err("%s: failed on ssl restriction\n", __func__); |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | 0 | if (lws_tls_server_new_nonblocking(wsi, accept_fd)) { |
153 | 0 | lwsl_err("%s: failed on lws_tls_server_new_nonblocking\n", __func__); |
154 | 0 | if (accept_fd != LWS_SOCK_INVALID) |
155 | 0 | compatible_close(accept_fd); |
156 | 0 | lws_tls_restrict_return(wsi); |
157 | 0 | goto fail; |
158 | 0 | } |
159 | | |
160 | | /* |
161 | | * we are not accepted yet, but we need to enter ourselves |
162 | | * as a live connection. That way we can retry when more |
163 | | * pieces come if we're not sorted yet |
164 | | */ |
165 | 0 | lwsi_set_state(wsi, LRS_SSL_ACK_PENDING); |
166 | |
|
167 | 0 | lws_pt_lock(pt, __func__); |
168 | 0 | if (__insert_wsi_socket_into_fds(context, wsi)) { |
169 | 0 | lwsl_err("%s: failed to insert into fds\n", __func__); |
170 | 0 | goto fail; |
171 | 0 | } |
172 | 0 | lws_pt_unlock(pt); |
173 | |
|
174 | 0 | lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT, |
175 | 0 | (int)context->timeout_secs); |
176 | |
|
177 | 0 | lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n"); |
178 | | |
179 | | /* fallthru */ |
180 | |
|
181 | 0 | case LRS_SSL_ACK_PENDING: |
182 | |
|
183 | 0 | if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) { |
184 | 0 | lwsl_err("%s: lws_change_pollfd failed\n", __func__); |
185 | 0 | goto fail; |
186 | 0 | } |
187 | | |
188 | 0 | if (wsi->a.vhost->tls.allow_non_ssl_on_ssl_port && !wsi->skip_fallback) { |
189 | | /* |
190 | | * We came here by POLLIN, so there is supposed to be |
191 | | * something to read... |
192 | | */ |
193 | |
|
194 | 0 | s = recv(wsi->desc.sockfd, (char *)pt->serv_buf, |
195 | 0 | context->pt_serv_buf_size, MSG_PEEK); |
196 | | /* |
197 | | * We have LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT.. |
198 | | * this just means don't hang up on him because of no |
199 | | * tls hello... what happens next is driven by |
200 | | * additional option flags: |
201 | | * |
202 | | * none: fail the connection |
203 | | * |
204 | | * LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS: |
205 | | * Destroy the TLS, issue a redirect using plaintext |
206 | | * http (this may not be accepted by a client that |
207 | | * has visited the site before and received an STS |
208 | | * header). |
209 | | * |
210 | | * LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER: |
211 | | * Destroy the TLS, continue and serve normally |
212 | | * using http |
213 | | * |
214 | | * LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG: |
215 | | * Destroy the TLS, apply whatever role and protocol |
216 | | * were told in the vhost info struct |
217 | | * .listen_accept_role / .listen_accept_protocol and |
218 | | * continue with that |
219 | | */ |
220 | |
|
221 | 0 | if (s >= 1 && pt->serv_buf[0] >= ' ') { |
222 | | /* |
223 | | * TLS content-type for Handshake is 0x16, and |
224 | | * for ChangeCipherSpec Record, it's 0x14 |
225 | | * |
226 | | * A non-ssl session will start with the HTTP |
227 | | * method in ASCII. If we see it's not a legit |
228 | | * SSL handshake kill the SSL for this |
229 | | * connection and try to handle as a HTTP |
230 | | * connection upgrade directly. |
231 | | */ |
232 | 0 | wsi->tls.use_ssl = 0; |
233 | |
|
234 | 0 | lws_tls_server_abort_connection(wsi); |
235 | | /* |
236 | | * care... this creates wsi with no ssl when ssl |
237 | | * is enabled and normally mandatory |
238 | | */ |
239 | 0 | wsi->tls.ssl = NULL; |
240 | |
|
241 | 0 | if (lws_check_opt(wsi->a.vhost->options, |
242 | 0 | LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS)) { |
243 | 0 | lwsl_info("%s: redirecting from http " |
244 | 0 | "to https\n", __func__); |
245 | 0 | wsi->tls.redirect_to_https = 1; |
246 | 0 | goto notls_accepted; |
247 | 0 | } |
248 | | |
249 | 0 | if (lws_check_opt(wsi->a.vhost->options, |
250 | 0 | LWS_SERVER_OPTION_ALLOW_HTTP_ON_HTTPS_LISTENER)) { |
251 | 0 | lwsl_info("%s: allowing unencrypted " |
252 | 0 | "http service on tls port\n", |
253 | 0 | __func__); |
254 | 0 | goto notls_accepted; |
255 | 0 | } |
256 | | |
257 | 0 | if (lws_check_opt(wsi->a.vhost->options, |
258 | 0 | LWS_SERVER_OPTION_FALLBACK_TO_APPLY_LISTEN_ACCEPT_CONFIG)) { |
259 | 0 | if (lws_http_to_fallback(wsi, NULL, 0)) |
260 | 0 | goto fail; |
261 | 0 | lwsl_info("%s: allowing non-tls " |
262 | 0 | "fallback\n", __func__); |
263 | 0 | goto notls_accepted; |
264 | 0 | } |
265 | | |
266 | 0 | lwsl_notice("%s: client did not send a valid " |
267 | 0 | "tls hello (default vhost %s)\n", |
268 | 0 | __func__, wsi->a.vhost->name); |
269 | 0 | goto fail; |
270 | 0 | } |
271 | 0 | if (!s) { |
272 | | /* |
273 | | * POLLIN but nothing to read is supposed to |
274 | | * mean the connection is gone, we should |
275 | | * fail out... |
276 | | * |
277 | | */ |
278 | 0 | lwsl_debug("%s: PEEKed 0 (from_pollin %d)\n", |
279 | 0 | __func__, from_pollin); |
280 | 0 | if (!from_pollin) |
281 | | /* |
282 | | * If this wasn't actually info from a |
283 | | * pollin let it go around again until |
284 | | * either data came or we still get told |
285 | | * zero length peek AND POLLIN |
286 | | */ |
287 | 0 | goto punt; |
288 | | |
289 | | /* |
290 | | * treat as remote closed |
291 | | */ |
292 | | |
293 | 0 | goto fail; |
294 | 0 | } |
295 | 0 | if (s < 0 && (LWS_ERRNO == LWS_EAGAIN || |
296 | 0 | LWS_ERRNO == LWS_EWOULDBLOCK)) { |
297 | |
|
298 | 0 | punt: |
299 | | /* |
300 | | * well, we get no way to know ssl or not |
301 | | * so go around again waiting for something |
302 | | * to come and give us a hint, or timeout the |
303 | | * connection. |
304 | | */ |
305 | 0 | if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) { |
306 | 0 | lwsl_err("%s: change_pollfd failed\n", |
307 | 0 | __func__); |
308 | 0 | return -1; |
309 | 0 | } |
310 | | |
311 | 0 | lwsl_info("SSL_ERROR_WANT_READ\n"); |
312 | 0 | return 0; |
313 | 0 | } |
314 | 0 | } |
315 | | |
316 | | /* normal SSL connection processing path */ |
317 | | |
318 | 0 | errno = 0; |
319 | 0 | n = lws_tls_server_accept(wsi); |
320 | 0 | lwsl_info("SSL_accept says %d\n", n); |
321 | 0 | switch (n) { |
322 | 0 | case LWS_SSL_CAPABLE_DONE: |
323 | 0 | lws_tls_restrict_return_handshake(wsi); |
324 | 0 | break; |
325 | 0 | case LWS_SSL_CAPABLE_ERROR: |
326 | 0 | lws_tls_restrict_return_handshake(wsi); |
327 | 0 | lwsl_info("%s: SSL_accept failed socket %u: %d\n", |
328 | 0 | __func__, wsi->desc.sockfd, n); |
329 | 0 | wsi->socket_is_permanently_unusable = 1; |
330 | 0 | goto fail; |
331 | | |
332 | 0 | default: /* MORE_SERVICE */ |
333 | 0 | return 0; |
334 | 0 | } |
335 | | |
336 | | /* adapt our vhost to match the SNI SSL_CTX that was chosen */ |
337 | 0 | vh = context->vhost_list; |
338 | 0 | while (vh) { |
339 | 0 | if (!vh->being_destroyed && wsi->tls.ssl && |
340 | 0 | vh->tls.ssl_ctx == lws_tls_ctx_from_wsi(wsi)) { |
341 | 0 | lwsl_info("setting wsi to vh %s\n", vh->name); |
342 | 0 | lws_vhost_bind_wsi(vh, wsi); |
343 | 0 | break; |
344 | 0 | } |
345 | 0 | vh = vh->vhost_next; |
346 | 0 | } |
347 | | |
348 | | /* OK, we are accepted... give him some time to negotiate */ |
349 | 0 | lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, |
350 | 0 | (int)context->timeout_secs); |
351 | |
|
352 | 0 | lwsi_set_state(wsi, LRS_ESTABLISHED); |
353 | 0 | if (lws_tls_server_conn_alpn(wsi)) { |
354 | 0 | lwsl_warn("%s: fail on alpn\n", __func__); |
355 | 0 | goto fail; |
356 | 0 | } |
357 | 0 | lwsl_debug("accepted new SSL conn\n"); |
358 | 0 | break; |
359 | | |
360 | 0 | default: |
361 | 0 | break; |
362 | 0 | } |
363 | | |
364 | 0 | return 0; |
365 | | |
366 | 0 | notls_accepted: |
367 | 0 | lwsi_set_state(wsi, LRS_ESTABLISHED); |
368 | |
|
369 | 0 | return 0; |
370 | | |
371 | 0 | fail: |
372 | 0 | return 1; |
373 | 0 | } |
374 | | |