2017-10-18 09:41:44 +08:00
|
|
|
/*
|
2019-08-14 10:44:14 +01:00
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-01-13 06:58:21 +08:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2017-10-18 09:41:44 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2017-10-18 09:41:44 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
|
|
|
#include "private-lib-tls-mbedtls.h"
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_ssl_destroy(struct lws_vhost *vhost)
|
|
|
|
{
|
|
|
|
if (!lws_check_opt(vhost->context->options,
|
|
|
|
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
|
|
|
|
return;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.ssl_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_ctx);
|
|
|
|
if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_client_ctx);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.x509_client_CA)
|
|
|
|
X509_free(vhost->tls.x509_client_CA);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
struct lws_context *context = wsi->context;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
|
|
|
int n = 0, m;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return lws_ssl_capable_read_no_ssl(wsi, buf, len);
|
|
|
|
|
2019-08-10 07:32:32 +01:00
|
|
|
lws_stats_bump(pt, LWSSTATS_C_API_READ, 1);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
errno = 0;
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_read(wsi->tls.ssl, buf, len);
|
2017-10-18 09:41:44 +08:00
|
|
|
#if defined(LWS_WITH_ESP32)
|
2018-09-05 14:45:10 +08:00
|
|
|
if (!n && errno == LWS_ENOTCONN) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%p: SSL_read ENOTCONN\n", wsi);
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(LWS_WITH_STATS)
|
2018-08-25 06:38:28 +08:00
|
|
|
if (!wsi->seen_rx && wsi->accept_start_us) {
|
2019-08-10 07:32:32 +01:00
|
|
|
lws_stats_bump(pt, LWSSTATS_US_SSL_RX_DELAY_AVG,
|
|
|
|
lws_now_usecs() - wsi->accept_start_us);
|
|
|
|
lws_stats_bump(pt, LWSSTATS_C_SSL_CONNS_HAD_RX, 1);
|
2017-10-18 09:41:44 +08:00
|
|
|
wsi->seen_rx = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
lwsl_debug("%p: SSL_read says %d\n", wsi, n);
|
|
|
|
/* manpage: returning 0 means connection shut down */
|
|
|
|
if (!n) {
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < 0) {
|
2018-05-01 12:41:42 +08:00
|
|
|
m = SSL_get_error(wsi->tls.ssl, n);
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%p: ssl err %d errno %d\n", wsi, m, errno);
|
2019-03-14 12:05:35 +01:00
|
|
|
if (errno == LWS_ENOTCONN) {
|
|
|
|
/* If the socket isn't connected anymore, bail out. */
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
2017-10-18 09:41:44 +08:00
|
|
|
if (m == SSL_ERROR_ZERO_RETURN ||
|
|
|
|
m == SSL_ERROR_SYSCALL)
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%s: WANT_READ\n", __func__);
|
|
|
|
lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%s: WANT_WRITE\n", __func__);
|
|
|
|
lwsl_debug("%p: LWS_SSL_CAPABLE_MORE_SERVICE\n", wsi);
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
2019-08-10 07:32:32 +01:00
|
|
|
lws_stats_bump(pt, LWSSTATS_B_READ, n);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
if (wsi->vhost)
|
|
|
|
wsi->vhost->conn_stats.rx += n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if it was our buffer that limited what we read,
|
|
|
|
* check if SSL has additional data pending inside SSL buffers.
|
|
|
|
*
|
|
|
|
* Because these won't signal at the network layer with POLLIN
|
|
|
|
* and if we don't realize, this data will sit there forever
|
|
|
|
*/
|
|
|
|
if (n != len)
|
|
|
|
goto bail;
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
goto bail;
|
|
|
|
|
2018-11-05 14:43:50 +08:00
|
|
|
if (SSL_pending(wsi->tls.ssl) &&
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_dll2_is_detached(&wsi->tls.dll_pending_tls))
|
|
|
|
lws_dll2_add_head(&wsi->tls.dll_pending_tls,
|
|
|
|
&pt->tls.dll_pending_tls_owner);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
return n;
|
|
|
|
bail:
|
|
|
|
lws_ssl_remove_wsi_from_buffered_list(wsi);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_pending(struct lws *wsi)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
return SSL_pending(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int n, m;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return lws_ssl_capable_write_no_ssl(wsi, buf, len);
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_write(wsi->tls.ssl, buf, len);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (n > 0)
|
|
|
|
return n;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
m = SSL_get_error(wsi->tls.ssl, n);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (m != SSL_ERROR_SYSCALL) {
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_READ || SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_notice("%s: want read\n", __func__);
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (m == SSL_ERROR_WANT_WRITE || SSL_want_write(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lws_set_blocking_send(wsi);
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
lwsl_debug("%s: want write\n", __func__);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_debug("%s failed: %d\n",__func__, m);
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int openssl_SSL_CTX_private_data_index;
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_info_callback(const SSL *ssl, int where, int ret)
|
|
|
|
{
|
|
|
|
struct lws *wsi;
|
|
|
|
struct lws_context *context;
|
|
|
|
struct lws_ssl_info si;
|
|
|
|
|
|
|
|
context = (struct lws_context *)SSL_CTX_get_ex_data(
|
|
|
|
SSL_get_SSL_CTX(ssl),
|
|
|
|
openssl_SSL_CTX_private_data_index);
|
|
|
|
if (!context)
|
|
|
|
return;
|
|
|
|
wsi = wsi_from_fd(context, SSL_get_fd(ssl));
|
|
|
|
if (!wsi)
|
|
|
|
return;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!(where & wsi->vhost->tls.ssl_info_event_mask))
|
2017-10-18 09:41:44 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
si.where = where;
|
|
|
|
si.ret = ret;
|
|
|
|
|
|
|
|
if (user_callback_handle_rxflow(wsi->protocol->callback,
|
|
|
|
wsi, LWS_CALLBACK_SSL_INFO,
|
|
|
|
wsi->user_space, &si, 0))
|
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_KILLED_BY_SSL_INFO, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_ssl_close(struct lws *wsi)
|
|
|
|
{
|
|
|
|
lws_sockfd_type n;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2017-10-18 09:41:44 +08:00
|
|
|
return 0; /* not handled */
|
|
|
|
|
|
|
|
#if defined (LWS_HAVE_SSL_SET_INFO_CALLBACK)
|
|
|
|
/* kill ssl callbacks, becausse we will remove the fd from the
|
|
|
|
* table linking it to the wsi
|
|
|
|
*/
|
2018-05-01 12:41:42 +08:00
|
|
|
if (wsi->vhost->tls.ssl_info_event_mask)
|
|
|
|
SSL_set_info_callback(wsi->tls.ssl, NULL);
|
2017-10-18 09:41:44 +08:00
|
|
|
#endif
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_get_fd(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (!wsi->socket_is_permanently_unusable)
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_shutdown(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
compatible_close(n);
|
2018-05-01 12:41:42 +08:00
|
|
|
SSL_free(wsi->tls.ssl);
|
|
|
|
wsi->tls.ssl = NULL;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
if (!lwsi_role_client(wsi) &&
|
2017-11-26 09:22:42 +08:00
|
|
|
wsi->context->simultaneous_ssl_restriction &&
|
2017-10-18 09:41:44 +08:00
|
|
|
wsi->context->simultaneous_ssl-- ==
|
|
|
|
wsi->context->simultaneous_ssl_restriction)
|
|
|
|
/* we made space and can do an accept */
|
|
|
|
lws_gate_accepts(wsi->context, 1);
|
2017-11-26 09:22:42 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
#if defined(LWS_WITH_STATS)
|
|
|
|
wsi->context->updated = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1; /* handled */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.ssl_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_ctx);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!vhost->tls.user_supplied_ssl_ctx && vhost->tls.ssl_client_ctx)
|
|
|
|
SSL_CTX_free(vhost->tls.ssl_client_ctx);
|
2017-11-14 11:25:54 +08:00
|
|
|
#if defined(LWS_WITH_ACME)
|
2017-10-28 11:33:34 +08:00
|
|
|
lws_tls_acme_sni_cert_destroy(vhost);
|
2017-11-14 11:25:54 +08:00
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_context_destroy(struct lws_context *context)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_tls_ctx *
|
|
|
|
lws_tls_ctx_from_wsi(struct lws *wsi)
|
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
if (!wsi->tls.ssl)
|
2018-02-27 07:48:25 +08:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
return SSL_get_SSL_CTX(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum lws_ssl_capable_status
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_tls_shutdown(struct lws *wsi)
|
2017-10-18 09:41:44 +08:00
|
|
|
{
|
2018-05-01 12:41:42 +08:00
|
|
|
int n = SSL_shutdown(wsi->tls.ssl);
|
2017-10-18 09:41:44 +08:00
|
|
|
|
|
|
|
lwsl_debug("SSL_shutdown=%d for fd %d\n", n, wsi->desc.sockfd);
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 1: /* successful completion */
|
|
|
|
n = shutdown(wsi->desc.sockfd, SHUT_WR);
|
|
|
|
return LWS_SSL_CAPABLE_DONE;
|
|
|
|
|
|
|
|
case 0: /* needs a retry */
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
|
|
|
|
default: /* fatal error, or WANT */
|
2018-05-01 12:41:42 +08:00
|
|
|
n = SSL_get_error(wsi->tls.ssl, n);
|
2017-10-18 09:41:44 +08:00
|
|
|
if (n != SSL_ERROR_SYSCALL && n != SSL_ERROR_SSL) {
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_read(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("(wants read)\n");
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLIN);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_READ;
|
|
|
|
}
|
2018-05-01 12:41:42 +08:00
|
|
|
if (SSL_want_write(wsi->tls.ssl)) {
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("(wants write)\n");
|
2018-03-11 11:26:06 +08:00
|
|
|
__lws_change_pollfd(wsi, 0, LWS_POLLOUT);
|
2017-10-18 09:41:44 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE_WRITE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 09:54:25 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
tops_fake_POLLIN_for_buffered_mbedtls(struct lws_context_per_thread *pt)
|
|
|
|
{
|
|
|
|
return lws_tls_fake_POLLIN_for_buffered(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct lws_tls_ops tls_ops_mbedtls = {
|
|
|
|
/* fake_POLLIN_for_buffered */ tops_fake_POLLIN_for_buffered_mbedtls,
|
|
|
|
};
|