2019-01-13 06:58:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
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:
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +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.
|
2019-01-13 06:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fakes POLLIN on all tls guys with buffered rx
|
|
|
|
*
|
|
|
|
* returns nonzero if any tls guys had POLLIN faked
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_tls_fake_POLLIN_for_buffered(struct lws_context_per_thread *pt)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_start_foreach_dll_safe(struct lws_dll2 *, p, p1,
|
|
|
|
lws_dll2_get_head(&pt->tls.dll_pending_tls_owner)) {
|
2019-01-13 06:58:21 +08:00
|
|
|
struct lws *wsi = lws_container_of(p, struct lws,
|
2019-03-20 07:39:55 +08:00
|
|
|
tls.dll_pending_tls);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2024-09-30 12:33:39 +01:00
|
|
|
/*
|
|
|
|
* ... allow custom event loop to override our POLLIN-setting
|
|
|
|
* implementation if it knows how to do it better for its case
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pt->context->event_loop_ops &&
|
|
|
|
pt->context->event_loop_ops->fake_POLLIN_override)
|
|
|
|
pt->context->event_loop_ops->fake_POLLIN_override(
|
|
|
|
pt->context, pt->tid);
|
|
|
|
else {
|
|
|
|
if (wsi->position_in_fds_table >= 0) {
|
|
|
|
|
|
|
|
pt->fds[wsi->position_in_fds_table].revents = (short)
|
|
|
|
(pt->fds[wsi->position_in_fds_table].revents |
|
|
|
|
(pt->fds[wsi->position_in_fds_table].events & LWS_POLLIN));
|
|
|
|
ret |= pt->fds[wsi->position_in_fds_table].revents & LWS_POLLIN;
|
|
|
|
}
|
2019-08-26 18:41:40 +01:00
|
|
|
}
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
} lws_end_foreach_dll_safe(p, p1);
|
|
|
|
|
|
|
|
return !!ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
|
|
|
|
{
|
2019-08-08 16:58:55 +01:00
|
|
|
lws_dll2_remove(&wsi->tls.dll_pending_tls);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi)
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
__lws_ssl_remove_wsi_from_buffered_list(wsi);
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2019-01-13 06:58:21 +08:00
|
|
|
int
|
|
|
|
lws_tls_check_cert_lifetime(struct lws_vhost *v)
|
|
|
|
{
|
|
|
|
time_t now = (time_t)lws_now_secs(), life = 0;
|
|
|
|
struct lws_acme_cert_aging_args caa;
|
|
|
|
union lws_tls_cert_info_results ir;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (v->tls.ssl_ctx && !v->tls.skipped_certs) {
|
|
|
|
|
|
|
|
if (now < 1542933698) /* Nov 23 2018 00:42 UTC */
|
|
|
|
/* our clock is wrong and we can't judge the certs */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n = lws_tls_vhost_cert_info(v, LWS_TLS_CERT_INFO_VALIDITY_TO,
|
|
|
|
&ir, 0);
|
|
|
|
if (n)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
life = (ir.time - now) / (24 * 3600);
|
2024-01-15 15:32:56 +00:00
|
|
|
lwsl_vhost_notice(v, " vhost %s: cert expiry: %lldd", v->name,
|
|
|
|
(long long)life);
|
2019-01-13 06:58:21 +08:00
|
|
|
} else
|
2021-10-15 08:44:57 -04:00
|
|
|
lwsl_vhost_info(v, " vhost %s: no cert", v->name);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
memset(&caa, 0, sizeof(caa));
|
|
|
|
caa.vh = v;
|
2019-08-09 10:12:09 +01:00
|
|
|
lws_broadcast(&v->context->pt[0], LWS_CALLBACK_VHOST_CERT_AGING, (void *)&caa,
|
2019-01-13 06:58:21 +08:00
|
|
|
(size_t)(ssize_t)life);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_tls_check_all_cert_lifetimes(struct lws_context *context)
|
|
|
|
{
|
|
|
|
struct lws_vhost *v = context->vhost_list;
|
|
|
|
|
|
|
|
while (v) {
|
|
|
|
if (lws_tls_check_cert_lifetime(v) < 0)
|
|
|
|
return -1;
|
|
|
|
v = v->vhost_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LWS_TLS_EXTANT_NO : skip adding the cert
|
|
|
|
* LWS_TLS_EXTANT_YES : use the cert and private key paths normally
|
|
|
|
* LWS_TLS_EXTANT_ALTERNATIVE: normal paths not usable, try alternate if poss
|
|
|
|
*/
|
|
|
|
enum lws_tls_extant
|
|
|
|
lws_tls_generic_cert_checks(struct lws_vhost *vhost, const char *cert,
|
|
|
|
const char *private_key)
|
|
|
|
{
|
|
|
|
int n, m;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The user code can choose to either pass the cert and
|
|
|
|
* key filepaths using the info members like this, or it can
|
|
|
|
* leave them NULL; force the vhost SSL_CTX init using the info
|
|
|
|
* options flag LWS_SERVER_OPTION_CREATE_VHOST_SSL_CTX; and
|
|
|
|
* set up the cert himself using the user callback
|
|
|
|
* LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, which
|
|
|
|
* happened just above and has the vhost SSL_CTX * in the user
|
|
|
|
* parameter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!cert || !private_key)
|
|
|
|
return LWS_TLS_EXTANT_NO;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)lws_tls_use_any_upgrade_check_extant(cert);
|
2019-01-13 06:58:21 +08:00
|
|
|
if (n == LWS_TLS_EXTANT_ALTERNATIVE)
|
|
|
|
return LWS_TLS_EXTANT_ALTERNATIVE;
|
2020-12-12 06:21:40 +00:00
|
|
|
m = (int)lws_tls_use_any_upgrade_check_extant(private_key);
|
2019-01-13 06:58:21 +08:00
|
|
|
if (m == LWS_TLS_EXTANT_ALTERNATIVE)
|
|
|
|
return LWS_TLS_EXTANT_ALTERNATIVE;
|
|
|
|
|
|
|
|
if ((n == LWS_TLS_EXTANT_NO || m == LWS_TLS_EXTANT_NO) &&
|
|
|
|
(vhost->options & LWS_SERVER_OPTION_IGNORE_MISSING_CERT)) {
|
2021-10-15 08:44:57 -04:00
|
|
|
lwsl_vhost_notice(vhost, "Ignoring missing %s or %s", cert, private_key);
|
2019-01-13 06:58:21 +08:00
|
|
|
vhost->tls.skipped_certs = 1;
|
|
|
|
|
|
|
|
return LWS_TLS_EXTANT_NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the cert + key exist
|
|
|
|
*/
|
|
|
|
|
|
|
|
return LWS_TLS_EXTANT_YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update the cert for every vhost using the given path
|
|
|
|
*/
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
int
|
2019-01-13 06:58:21 +08:00
|
|
|
lws_tls_cert_updated(struct lws_context *context, const char *certpath,
|
|
|
|
const char *keypath,
|
|
|
|
const char *mem_cert, size_t len_mem_cert,
|
|
|
|
const char *mem_privkey, size_t len_mem_privkey)
|
|
|
|
{
|
|
|
|
struct lws wsi;
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi.a.context = context;
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
lws_start_foreach_ll(struct lws_vhost *, v, context->vhost_list) {
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi.a.vhost = v; /* not a real bound wsi */
|
2019-01-13 06:58:21 +08:00
|
|
|
if (v->tls.alloc_cert_path && v->tls.key_path &&
|
|
|
|
!strcmp(v->tls.alloc_cert_path, certpath) &&
|
|
|
|
!strcmp(v->tls.key_path, keypath)) {
|
|
|
|
lws_tls_server_certs_load(v, &wsi, certpath, keypath,
|
|
|
|
mem_cert, len_mem_cert,
|
|
|
|
mem_privkey, len_mem_privkey);
|
|
|
|
|
|
|
|
if (v->tls.skipped_certs)
|
2021-10-15 08:44:57 -04:00
|
|
|
lwsl_vhost_notice(v, "vhost %s: cert unset", v->name);
|
2019-01-13 06:58:21 +08:00
|
|
|
}
|
|
|
|
} lws_end_foreach_ll(v, vhost_next);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_gate_accepts(struct lws_context *context, int on)
|
|
|
|
{
|
|
|
|
struct lws_vhost *v = context->vhost_list;
|
|
|
|
|
2021-09-22 09:34:56 +01:00
|
|
|
if (context->tls_gate_accepts == (char)on)
|
|
|
|
return 0;
|
|
|
|
|
2021-12-09 05:13:15 +00:00
|
|
|
lwsl_cx_info(context, "on = %d", on);
|
2021-09-18 09:34:36 +01:00
|
|
|
|
2021-09-22 09:34:56 +01:00
|
|
|
context->tls_gate_accepts = (char)on;
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
while (v) {
|
2021-06-21 08:50:13 +01:00
|
|
|
lws_start_foreach_dll(struct lws_dll2 *, d,
|
|
|
|
lws_dll2_get_head(&v->listen_wsi)) {
|
|
|
|
struct lws *wsi = lws_container_of(d, struct lws,
|
|
|
|
listen_list);
|
|
|
|
|
|
|
|
if (v->tls.use_ssl &&
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_change_pollfd(wsi, on ? LWS_POLLIN : 0,
|
|
|
|
on ? 0 : LWS_POLLIN))
|
2021-10-15 08:44:57 -04:00
|
|
|
lwsl_cx_notice(context, "Unable to set POLLIN %d", on);
|
2021-06-21 08:50:13 +01:00
|
|
|
} lws_end_foreach_dll(d);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
v = v->vhost_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-21 10:36:36 +01:00
|
|
|
#endif
|
2019-01-13 06:58:21 +08:00
|
|
|
|
|
|
|
/* comma-separated alpn list, like "h2,http/1.1" to openssl alpn format */
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_alpn_comma_to_openssl(const char *comma, uint8_t *os, int len)
|
|
|
|
{
|
|
|
|
uint8_t *oos = os, *plen = NULL;
|
|
|
|
|
2020-01-02 08:32:23 +00:00
|
|
|
if (!comma)
|
|
|
|
return 0;
|
|
|
|
|
2020-01-30 13:19:11 +00:00
|
|
|
while (*comma && len > 2) {
|
2019-01-13 06:58:21 +08:00
|
|
|
if (!plen && *comma == ' ') {
|
|
|
|
comma++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!plen) {
|
|
|
|
plen = os++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*comma == ',') {
|
2020-12-12 06:21:40 +00:00
|
|
|
*plen = (uint8_t)lws_ptr_diff(os, plen + 1);
|
2019-01-13 06:58:21 +08:00
|
|
|
plen = NULL;
|
|
|
|
comma++;
|
|
|
|
} else {
|
2020-12-12 06:21:40 +00:00
|
|
|
*os++ = (uint8_t)*comma++;
|
2019-01-13 06:58:21 +08:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plen)
|
2020-12-12 06:21:40 +00:00
|
|
|
*plen = (uint8_t)lws_ptr_diff(os, plen + 1);
|
2019-01-13 06:58:21 +08:00
|
|
|
|
2020-01-30 13:19:11 +00:00
|
|
|
*os = 0;
|
|
|
|
|
2019-01-13 06:58:21 +08:00
|
|
|
return lws_ptr_diff(os, oos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|