2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2017 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 "private-libwebsockets.h"
24 int lws_alloc_vfs_file(struct lws_context *context, const char *filename, uint8_t **buf,
25 lws_filepos_t *amount)
28 lws_fop_flags_t flags = LWS_O_RDONLY;
29 lws_fop_fd_t fops_fd = lws_vfs_file_open(
30 lws_get_fops(context), filename, &flags);
36 len = lws_vfs_get_length(fops_fd);
42 if (lws_vfs_file_read(fops_fd, amount, *buf, len))
47 lws_vfs_file_close(&fops_fd);
52 #if defined(LWS_WITH_ESP32)
53 int alloc_file(struct lws_context *context, const char *filename, uint8_t **buf,
54 lws_filepos_t *amount)
60 ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
61 if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK) {
70 if (nvs_get_blob(nvh, filename, (char *)*buf, &s) != ESP_OK)
82 int openssl_websocket_private_data_index,
83 openssl_SSL_CTX_private_data_index;
85 int lws_ssl_get_error(struct lws *wsi, int n)
89 lwsl_debug("%s: %p %d\n", __func__, wsi->ssl, n);
90 return SSL_get_error(wsi->ssl, n);
93 /* Copies a string describing the code returned by lws_ssl_get_error(),
94 * which may also contain system error information in the case of SSL_ERROR_SYSCALL,
96 * Returns a pointer to buf.
98 * Note: the lws_ssl_get_error() code is *not* an error code that can be passed
99 * to ERR_error_string(),
101 * ret is the return value originally passed to lws_ssl_get_error(), needed to disambiguate
104 * See man page for SSL_get_error().
106 * Not thread safe, uses strerror()
108 char* lws_ssl_get_error_string(int status, int ret, char *buf, size_t len) {
110 case SSL_ERROR_NONE: return strncpy(buf, "SSL_ERROR_NONE", len);
111 case SSL_ERROR_ZERO_RETURN: return strncpy(buf, "SSL_ERROR_ZERO_RETURN", len);
112 case SSL_ERROR_WANT_READ: return strncpy(buf, "SSL_ERROR_WANT_READ", len);
113 case SSL_ERROR_WANT_WRITE: return strncpy(buf, "SSL_ERROR_WANT_WRITE", len);
114 case SSL_ERROR_WANT_CONNECT: return strncpy(buf, "SSL_ERROR_WANT_CONNECT", len);
115 case SSL_ERROR_WANT_ACCEPT: return strncpy(buf, "SSL_ERROR_WANT_ACCEPT", len);
116 case SSL_ERROR_WANT_X509_LOOKUP: return strncpy(buf, "SSL_ERROR_WANT_X509_LOOKUP", len);
117 case SSL_ERROR_SYSCALL:
120 snprintf(buf, len, "SSL_ERROR_SYSCALL: EOF");
123 #ifndef LWS_PLAT_OPTEE
124 snprintf(buf, len, "SSL_ERROR_SYSCALL: %s", strerror(errno));
126 snprintf(buf, len, "SSL_ERROR_SYSCALL: %d", errno);
130 return strncpy(buf, "SSL_ERROR_SYSCALL", len);
132 case SSL_ERROR_SSL: return "SSL_ERROR_SSL";
133 default: return "SSL_ERROR_UNKNOWN";
138 lws_ssl_elaborate_error(void)
140 #if defined(LWS_WITH_ESP32)
145 while ((err = ERR_get_error()) != 0) {
146 ERR_error_string_n(err, buf, sizeof(buf));
147 lwsl_err("*** %s\n", buf);
152 #if !defined(LWS_WITH_ESP32)
155 lws_context_init_ssl_pem_passwd_cb(char * buf, int size, int rwflag, void *userdata)
157 struct lws_context_creation_info * info =
158 (struct lws_context_creation_info *)userdata;
160 strncpy(buf, info->ssl_private_key_password, size);
161 buf[size - 1] = '\0';
167 lws_ssl_bind_passphrase(SSL_CTX *ssl_ctx, struct lws_context_creation_info *info)
169 if (!info->ssl_private_key_password)
172 * password provided, set ssl callback and user data
173 * for checking password which will be trigered during
174 * SSL_CTX_use_PrivateKey_file function
176 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, (void *)info);
177 SSL_CTX_set_default_passwd_cb(ssl_ctx, lws_context_init_ssl_pem_passwd_cb);
182 lws_context_init_ssl_library(struct lws_context_creation_info *info)
185 #ifdef USE_OLD_CYASSL
186 lwsl_notice(" Compiled with CyaSSL support\n");
188 lwsl_notice(" Compiled with wolfSSL support\n");
191 #if defined(LWS_USE_BORINGSSL)
192 lwsl_notice(" Compiled with BoringSSL support\n");
194 lwsl_notice(" Compiled with OpenSSL support\n");
197 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT)) {
198 lwsl_notice(" SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT\n");
202 /* basic openssl init */
204 lwsl_notice("Doing SSL library init\n");
206 #if !defined(LWS_WITH_ESP32)
208 OpenSSL_add_all_algorithms();
209 SSL_load_error_strings();
211 openssl_websocket_private_data_index =
212 SSL_get_ex_new_index(0, "lws", NULL, NULL, NULL);
214 openssl_SSL_CTX_private_data_index = SSL_CTX_get_ex_new_index(0,
215 NULL, NULL, NULL, NULL);
222 lws_ssl_destroy(struct lws_vhost *vhost)
224 if (!lws_check_opt(vhost->context->options,
225 LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
229 SSL_CTX_free(vhost->ssl_ctx);
230 if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
231 SSL_CTX_free(vhost->ssl_client_ctx);
232 #if !defined(LWS_WITH_ESP32)
234 // after 1.1.0 no need
235 #if (OPENSSL_VERSION_NUMBER < 0x10100000)
236 // <= 1.0.1f = old api, 1.0.1g+ = new api
237 #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
240 #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
241 !defined(LIBRESSL_VERSION_NUMBER) && \
242 !defined(OPENSSL_IS_BORINGSSL)
243 ERR_remove_thread_state();
245 ERR_remove_thread_state(NULL);
250 CRYPTO_cleanup_all_ex_data();
256 lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
258 struct lws_context *context = wsi->context;
259 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
261 if (!wsi->pending_read_list_prev &&
262 !wsi->pending_read_list_next &&
263 pt->pending_read_list != wsi)
264 /* we are not on the list */
267 /* point previous guy's next to our next */
268 if (!wsi->pending_read_list_prev)
269 pt->pending_read_list = wsi->pending_read_list_next;
271 wsi->pending_read_list_prev->pending_read_list_next =
272 wsi->pending_read_list_next;
274 /* point next guy's previous to our previous */
275 if (wsi->pending_read_list_next)
276 wsi->pending_read_list_next->pending_read_list_prev =
277 wsi->pending_read_list_prev;
279 wsi->pending_read_list_prev = NULL;
280 wsi->pending_read_list_next = NULL;
284 lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
286 struct lws_context *context = wsi->context;
287 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
289 #if !defined(LWS_WITH_ESP32)
290 int ssl_read_errno = 0;
294 return lws_ssl_capable_read_no_ssl(wsi, buf, len);
297 n = SSL_read(wsi->ssl, buf, len);
298 #if defined(LWS_WITH_ESP32)
299 if (!n && errno == ENOTCONN) {
300 lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
301 return LWS_SSL_CAPABLE_ERROR;
305 lwsl_debug("%p: SSL_read says %d\n", wsi, n);
306 /* manpage: returning 0 means connection shut down */
308 n = lws_ssl_get_error(wsi, n);
309 lwsl_debug("%p: ssl err %d errno %d\n", wsi, n, errno);
310 if (n == SSL_ERROR_ZERO_RETURN)
311 return LWS_SSL_CAPABLE_ERROR;
313 if (n == SSL_ERROR_SYSCALL) {
314 #if !defined(LWS_WITH_ESP32)
315 int err = ERR_get_error();
316 if (err == 0 && (ssl_read_errno == EPIPE ||
317 ssl_read_errno == ECONNABORTED ||
318 ssl_read_errno == 0))
319 return LWS_SSL_CAPABLE_ERROR;
323 lwsl_err("%s failed: %s\n",__func__,
324 ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
325 lws_ssl_elaborate_error();
327 return LWS_SSL_CAPABLE_ERROR;
331 n = lws_ssl_get_error(wsi, n);
332 if (n == SSL_ERROR_WANT_READ || SSL_want_read(wsi->ssl)) {
333 lwsl_debug("%s: WANT_READ\n", __func__);
334 lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
335 return LWS_SSL_CAPABLE_MORE_SERVICE;
337 if (n == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->ssl)) {
338 lwsl_debug("%s: WANT_WRITE\n", __func__);
339 lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
340 return LWS_SSL_CAPABLE_MORE_SERVICE;
344 lwsl_err("%s failed2: %s\n",__func__,
345 ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
346 lws_ssl_elaborate_error();
348 return LWS_SSL_CAPABLE_ERROR;
352 wsi->vhost->conn_stats.rx += n;
354 lws_restart_ws_ping_pong_timer(wsi);
357 * if it was our buffer that limited what we read,
358 * check if SSL has additional data pending inside SSL buffers.
360 * Because these won't signal at the network layer with POLLIN
361 * and if we don't realize, this data will sit there forever
368 if (!SSL_pending(wsi->ssl))
371 if (wsi->pending_read_list_next)
373 if (wsi->pending_read_list_prev)
375 if (pt->pending_read_list == wsi)
378 /* add us to the linked list of guys with pending ssl */
379 if (pt->pending_read_list)
380 pt->pending_read_list->pending_read_list_prev = wsi;
382 wsi->pending_read_list_next = pt->pending_read_list;
383 wsi->pending_read_list_prev = NULL;
384 pt->pending_read_list = wsi;
388 lws_ssl_remove_wsi_from_buffered_list(wsi);
394 lws_ssl_pending(struct lws *wsi)
399 return SSL_pending(wsi->ssl);
403 lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len)
406 #if !defined(LWS_WITH_ESP32)
407 int ssl_read_errno = 0;
411 return lws_ssl_capable_write_no_ssl(wsi, buf, len);
413 n = SSL_write(wsi->ssl, buf, len);
417 n = lws_ssl_get_error(wsi, n);
418 if (n == SSL_ERROR_WANT_READ || n == SSL_ERROR_WANT_WRITE) {
419 if (n == SSL_ERROR_WANT_WRITE) {
420 lwsl_debug("%s: WANT_WRITE\n", __func__);
421 lws_set_blocking_send(wsi);
423 return LWS_SSL_CAPABLE_MORE_SERVICE;
426 if (n == SSL_ERROR_ZERO_RETURN)
427 return LWS_SSL_CAPABLE_ERROR;
429 #if !defined(LWS_WITH_ESP32)
430 if (n == SSL_ERROR_SYSCALL) {
432 int err = ERR_get_error();
434 && (ssl_read_errno == EPIPE
435 || ssl_read_errno == ECONNABORTED
436 || ssl_read_errno == 0))
437 return LWS_SSL_CAPABLE_ERROR;
441 lwsl_err("%s failed: %s\n",__func__,
442 ERR_error_string(lws_ssl_get_error(wsi, 0), NULL));
443 lws_ssl_elaborate_error();
445 return LWS_SSL_CAPABLE_ERROR;
449 lws_gate_accepts(struct lws_context *context, int on)
451 struct lws_vhost *v = context->vhost_list;
453 lwsl_info("gating accepts %d\n", on);
456 if (v->use_ssl && v->lserv_wsi) /* gate ability to accept incoming connections */
457 if (lws_change_pollfd(v->lserv_wsi, (LWS_POLLIN) * !on, (LWS_POLLIN) * on))
458 lwsl_err("Unable to set accept POLLIN %d\n", on);
467 lws_ssl_close(struct lws *wsi)
472 return 0; /* not handled */
474 n = SSL_get_fd(wsi->ssl);
475 SSL_shutdown(wsi->ssl);
480 if (wsi->context->simultaneous_ssl_restriction &&
481 wsi->context->simultaneous_ssl-- ==
482 wsi->context->simultaneous_ssl_restriction)
483 /* we made space and can do an accept */
484 lws_gate_accepts(wsi->context, 1);
486 return 1; /* handled */
489 /* leave all wsi close processing to the caller */
492 lws_server_socket_service_ssl(struct lws *wsi, lws_sockfd_type accept_fd)
494 struct lws_context *context = wsi->context;
495 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
497 #if !defined(USE_WOLFSSL) && !defined(LWS_WITH_ESP32)
502 if (!LWS_SSL_ENABLED(wsi->vhost))
507 case LWSCM_SSL_INIT_RAW:
509 lwsl_err("%s: leaking ssl\n", __func__);
510 if (accept_fd == LWS_SOCK_INVALID)
512 if (context->simultaneous_ssl_restriction &&
513 context->simultaneous_ssl >= context->simultaneous_ssl_restriction) {
514 lwsl_notice("unable to deal with SSL connection\n");
518 wsi->ssl = SSL_new(wsi->vhost->ssl_ctx);
519 if (wsi->ssl == NULL) {
520 lwsl_err("SSL_new failed: %d (errno %d)\n",
521 lws_ssl_get_error(wsi, 0), errno);
523 lws_ssl_elaborate_error();
524 if (accept_fd != LWS_SOCK_INVALID)
525 compatible_close(accept_fd);
528 if (context->simultaneous_ssl_restriction &&
529 ++context->simultaneous_ssl == context->simultaneous_ssl_restriction)
530 /* that was the last allowed SSL connection */
531 lws_gate_accepts(context, 0);
533 #if !defined(LWS_WITH_ESP32)
534 SSL_set_ex_data(wsi->ssl,
535 openssl_websocket_private_data_index, wsi);
537 SSL_set_fd(wsi->ssl, accept_fd);
540 #ifdef USE_OLD_CYASSL
541 CyaSSL_set_using_nonblock(wsi->ssl, 1);
543 wolfSSL_set_using_nonblock(wsi->ssl, 1);
546 #if defined(LWS_WITH_ESP32)
547 lws_plat_set_socket_options(wsi->vhost, accept_fd);
549 SSL_set_mode(wsi->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
550 bio = SSL_get_rbio(wsi->ssl);
552 BIO_set_nbio(bio, 1); /* nonblocking */
554 lwsl_notice("NULL rbio\n");
555 bio = SSL_get_wbio(wsi->ssl);
557 BIO_set_nbio(bio, 1); /* nonblocking */
559 lwsl_notice("NULL rbio\n");
564 * we are not accepted yet, but we need to enter ourselves
565 * as a live connection. That way we can retry when more
566 * pieces come if we're not sorted yet
569 if (wsi->mode == LWSCM_SSL_INIT)
570 wsi->mode = LWSCM_SSL_ACK_PENDING;
572 wsi->mode = LWSCM_SSL_ACK_PENDING_RAW;
574 if (insert_wsi_socket_into_fds(context, wsi)) {
575 lwsl_err("%s: failed to insert into fds\n", __func__);
579 lws_set_timeout(wsi, PENDING_TIMEOUT_SSL_ACCEPT,
580 context->timeout_secs);
582 lwsl_debug("inserted SSL accept into fds, trying SSL_accept\n");
586 case LWSCM_SSL_ACK_PENDING:
587 case LWSCM_SSL_ACK_PENDING_RAW:
588 if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
589 lwsl_err("%s: lws_change_pollfd failed\n", __func__);
593 lws_latency_pre(context, wsi);
595 if (wsi->vhost->allow_non_ssl_on_ssl_port) {
597 n = recv(wsi->desc.sockfd, (char *)pt->serv_buf, context->pt_serv_buf_size,
601 * optionally allow non-SSL connect on SSL listening socket
602 * This is disabled by default, if enabled it goes around any
603 * SSL-level access control (eg, client-side certs) so leave
604 * it disabled unless you know it's not a problem for you
607 if (n >= 1 && pt->serv_buf[0] >= ' ') {
609 * TLS content-type for Handshake is 0x16, and
610 * for ChangeCipherSpec Record, it's 0x14
612 * A non-ssl session will start with the HTTP
613 * method in ASCII. If we see it's not a legit
614 * SSL handshake kill the SSL for this
615 * connection and try to handle as a HTTP
616 * connection upgrade directly.
620 SSL_shutdown(wsi->ssl);
623 if (lws_check_opt(context->options,
624 LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS))
625 wsi->redirect_to_https = 1;
629 * connection is gone, or nothing to read
630 * if it's gone, we will timeout on
631 * PENDING_TIMEOUT_SSL_ACCEPT
634 if (n < 0 && (LWS_ERRNO == LWS_EAGAIN ||
635 LWS_ERRNO == LWS_EWOULDBLOCK)) {
637 * well, we get no way to know ssl or not
638 * so go around again waiting for something
639 * to come and give us a hint, or timeout the
642 m = SSL_ERROR_WANT_READ;
647 /* normal SSL connection processing path */
649 n = SSL_accept(wsi->ssl);
650 lws_latency(context, wsi,
651 "SSL_accept LWSCM_SSL_ACK_PENDING\n", n, n == 1);
652 lwsl_info("SSL_accept says %d\n", n);
656 m = lws_ssl_get_error(wsi, n);
658 #if defined(LWS_WITH_ESP32)
659 if (m == 5 && errno == 11)
660 m = SSL_ERROR_WANT_READ;
664 if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->ssl)) {
665 if (lws_change_pollfd(wsi, 0, LWS_POLLIN)) {
666 lwsl_err("%s: WANT_READ change_pollfd failed\n", __func__);
670 lwsl_info("SSL_ERROR_WANT_READ\n");
673 if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->ssl)) {
674 lwsl_debug("%s: WANT_WRITE\n", __func__);
676 if (lws_change_pollfd(wsi, 0, LWS_POLLOUT)) {
677 lwsl_err("%s: WANT_WRITE change_pollfd failed\n", __func__);
684 lwsl_err("SSL_accept failed socket %u: %s\n", wsi->desc.sockfd,
685 lws_ssl_get_error_string(m, n, buf, sizeof(buf)));
686 lws_ssl_elaborate_error();
690 /* OK, we are accepted... give him some time to negotiate */
691 lws_set_timeout(wsi, PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
692 context->timeout_secs);
694 if (wsi->mode == LWSCM_SSL_ACK_PENDING_RAW)
695 wsi->mode = LWSCM_RAW;
697 wsi->mode = LWSCM_HTTP_SERVING;
699 lws_http2_configure_if_upgraded(wsi);
701 lwsl_debug("accepted new SSL conn\n");
712 lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
715 SSL_CTX_free(vhost->ssl_ctx);
717 if (!vhost->user_supplied_ssl_ctx && vhost->ssl_client_ctx)
718 SSL_CTX_free(vhost->ssl_client_ctx);
722 lws_ssl_context_destroy(struct lws_context *context)
725 #if !defined(LWS_WITH_ESP32)
727 // after 1.1.0 no need
728 #if (OPENSSL_VERSION_NUMBER < 0x10100000)
729 // <= 1.0.1f = old api, 1.0.1g+ = new api
730 #if (OPENSSL_VERSION_NUMBER <= 0x1000106f) || defined(USE_WOLFSSL)
733 #if OPENSSL_VERSION_NUMBER >= 0x1010005f && \
734 !defined(LIBRESSL_VERSION_NUMBER) && \
735 !defined(OPENSSL_IS_BORINGSSL)
736 ERR_remove_thread_state();
738 ERR_remove_thread_state(NULL);
743 CRYPTO_cleanup_all_ex_data();