2016-03-29 08:51:42 +08:00
|
|
|
/*
|
2019-08-14 10:44:14 +01:00
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2016-03-29 08:51:42 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2016-03-29 08:51:42 +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:
|
2016-03-29 08:51:42 +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.
|
2016-03-29 08:51:42 +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.
|
2016-03-29 08:51:42 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
static int
|
|
|
|
lws_ssl_client_connect1(struct lws *wsi, char *errbuf, size_t len)
|
2016-03-29 08:51:42 +08:00
|
|
|
{
|
2019-08-23 16:10:36 +01:00
|
|
|
int n;
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2020-04-14 19:04:13 +01:00
|
|
|
n = lws_tls_client_connect(wsi, errbuf, len);
|
2017-10-18 09:41:44 +08:00
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_tls_restrict_return_handshake(wsi);
|
2017-10-18 09:41:44 +08:00
|
|
|
return -1;
|
|
|
|
case LWS_SSL_CAPABLE_DONE:
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_tls_restrict_return_handshake(wsi);
|
2021-01-06 15:08:22 +00:00
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_GO);
|
2021-03-15 05:20:15 +00:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
|
|
|
wsi->conmon.ciu_tls = (lws_conmon_interval_us_t)
|
|
|
|
(lws_now_usecs() - wsi->conmon_datum);
|
|
|
|
#endif
|
2017-10-18 09:41:44 +08:00
|
|
|
return 1; /* connected */
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
/* fallthru */
|
2019-11-15 09:12:55 +00:00
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
2017-10-18 09:41:44 +08:00
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_WAITING_SSL);
|
2017-10-18 09:41:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* retry */
|
2016-03-29 08:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_ssl_client_connect2(struct lws *wsi, char *errbuf, size_t len)
|
2016-03-29 08:51:42 +08:00
|
|
|
{
|
2020-01-02 08:32:23 +00:00
|
|
|
int n;
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_SSL) {
|
2020-04-14 19:04:13 +01:00
|
|
|
n = lws_tls_client_connect(wsi, errbuf, len);
|
2017-10-18 09:41:44 +08:00
|
|
|
lwsl_debug("%s: SSL_connect says %d\n", __func__, n);
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_tls_restrict_return_handshake(wsi);
|
2022-02-04 07:34:54 +00:00
|
|
|
|
|
|
|
if (lws_tls_client_confirm_peer_cert(wsi, errbuf, len)) {
|
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-04-14 19:04:13 +01:00
|
|
|
// lws_snprintf(errbuf, len, "client connect failed");
|
2016-05-03 07:26:10 +08:00
|
|
|
return -1;
|
2017-10-18 09:41:44 +08:00
|
|
|
case LWS_SSL_CAPABLE_DONE:
|
|
|
|
break; /* connected */
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_WRITE:
|
|
|
|
lws_callback_on_writable(wsi);
|
|
|
|
/* fallthru */
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE_READ:
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_WAITING_SSL);
|
2017-10-18 09:41:44 +08:00
|
|
|
/* fallthru */
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
2021-06-17 10:07:04 +01:00
|
|
|
return 0; /* retry */
|
2016-03-29 08:51:42 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 09:31:23 +08:00
|
|
|
|
2021-09-22 09:34:56 +01:00
|
|
|
lws_tls_restrict_return_handshake(wsi);
|
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
if (lws_tls_client_confirm_peer_cert(wsi, errbuf, len)) {
|
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
|
2017-10-18 09:41:44 +08:00
|
|
|
return -1;
|
2021-01-06 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_GO);
|
2021-03-15 05:20:15 +00:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
|
|
|
wsi->conmon.ciu_tls = (lws_conmon_interval_us_t)
|
|
|
|
(lws_now_usecs() - wsi->conmon_datum);
|
|
|
|
#endif
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
return 1; /* connected */
|
2016-03-29 08:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-27 08:27:16 +08:00
|
|
|
int lws_context_init_client_ssl(const struct lws_context_creation_info *info,
|
2016-05-12 19:39:29 +08:00
|
|
|
struct lws_vhost *vhost)
|
2016-03-29 08:51:42 +08:00
|
|
|
{
|
2017-02-22 07:28:13 +08:00
|
|
|
const char *private_key_filepath = info->ssl_private_key_filepath;
|
2017-03-16 10:46:31 +08:00
|
|
|
const char *cert_filepath = info->ssl_cert_filepath;
|
2018-11-23 08:47:56 +08:00
|
|
|
const char *ca_filepath = info->ssl_ca_filepath;
|
|
|
|
const char *cipher_list = info->ssl_cipher_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
|
|
|
lws_fakewsi_def_plwsa(&vhost->context->pt[0]);
|
|
|
|
|
|
|
|
lws_fakewsi_prep_plwsa_ctx(vhost->context);
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2018-11-29 08:47:49 +08:00
|
|
|
if (vhost->options & LWS_SERVER_OPTION_ADOPT_APPLY_LISTEN_ACCEPT_CONFIG)
|
2017-10-16 16:59:57 +08:00
|
|
|
return 0;
|
|
|
|
|
2019-03-08 15:26:33 +08:00
|
|
|
if (vhost->tls.ssl_ctx) {
|
|
|
|
cert_filepath = NULL;
|
|
|
|
private_key_filepath = NULL;
|
|
|
|
ca_filepath = NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-22 07:28:13 +08:00
|
|
|
/*
|
|
|
|
* for backwards-compatibility default to using ssl_... members, but
|
|
|
|
* if the newer client-specific ones are given, use those
|
|
|
|
*/
|
|
|
|
if (info->client_ssl_cipher_list)
|
|
|
|
cipher_list = info->client_ssl_cipher_list;
|
|
|
|
if (info->client_ssl_cert_filepath)
|
|
|
|
cert_filepath = info->client_ssl_cert_filepath;
|
|
|
|
if (info->client_ssl_private_key_filepath)
|
|
|
|
private_key_filepath = info->client_ssl_private_key_filepath;
|
2017-10-18 09:41:44 +08:00
|
|
|
|
2017-09-02 10:50:54 +08:00
|
|
|
if (info->client_ssl_ca_filepath)
|
|
|
|
ca_filepath = info->client_ssl_ca_filepath;
|
2017-02-22 07:28:13 +08:00
|
|
|
|
2018-05-01 12:41:42 +08:00
|
|
|
if (vhost->tls.ssl_client_ctx)
|
2017-07-08 16:01:34 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-06-17 11:13:01 +01:00
|
|
|
#if !defined(LWS_WITH_MBEDTLS)
|
2016-03-29 08:51:42 +08:00
|
|
|
if (info->provided_client_ssl_ctx) {
|
|
|
|
/* use the provided OpenSSL context if given one */
|
2018-05-01 12:41:42 +08:00
|
|
|
vhost->tls.ssl_client_ctx = info->provided_client_ssl_ctx;
|
2016-03-29 08:51:42 +08:00
|
|
|
/* nothing for lib to delete */
|
2018-05-01 12:41:42 +08:00
|
|
|
vhost->tls.user_supplied_ssl_ctx = 1;
|
2016-05-12 19:39:29 +08:00
|
|
|
|
2016-03-29 08:51:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-17 11:13:01 +01:00
|
|
|
#endif
|
2016-03-29 08:51:42 +08:00
|
|
|
|
2024-09-23 13:10:28 +01:00
|
|
|
if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT))
|
|
|
|
return 0;
|
|
|
|
|
2017-10-18 09:41:44 +08:00
|
|
|
if (lws_tls_client_create_vhost_context(vhost, info, cipher_list,
|
2018-10-26 09:44:58 +08:00
|
|
|
ca_filepath,
|
|
|
|
info->client_ssl_ca_mem,
|
|
|
|
info->client_ssl_ca_mem_len,
|
|
|
|
cert_filepath,
|
2019-01-11 13:13:40 +08:00
|
|
|
info->client_ssl_cert_mem,
|
|
|
|
info->client_ssl_cert_mem_len,
|
2020-06-04 19:59:37 -07:00
|
|
|
private_key_filepath,
|
|
|
|
info->client_ssl_key_mem,
|
|
|
|
info->client_ssl_key_mem_len
|
|
|
|
))
|
2016-03-29 08:51:42 +08:00
|
|
|
return 1;
|
|
|
|
|
2019-08-07 10:41:03 +01:00
|
|
|
lwsl_info("created client ssl context for %s\n", vhost->name);
|
2017-09-02 10:50:54 +08:00
|
|
|
|
2016-03-29 08:51:42 +08:00
|
|
|
/*
|
|
|
|
* give him a fake wsi with context set, so he can use
|
|
|
|
* lws_get_context() in the callback
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
plwsa->vhost = vhost; /* not a real bound wsi */
|
2019-08-23 16:10:36 +01:00
|
|
|
|
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
|
|
|
vhost->protocols[0].callback((struct lws *)plwsa,
|
2016-03-29 08:51:42 +08:00
|
|
|
LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
|
2018-11-23 08:47:56 +08:00
|
|
|
vhost->tls.ssl_client_ctx, NULL, 0);
|
2016-03-29 08:51:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-21 20:22:40 +01:00
|
|
|
|
2020-09-19 13:27:33 +01:00
|
|
|
int
|
|
|
|
lws_client_create_tls(struct lws *wsi, const char **pcce, int do_c1)
|
|
|
|
{
|
|
|
|
/* we can retry this... just cook the SSL BIO the first time */
|
|
|
|
|
|
|
|
if (wsi->tls.use_ssl & LCCSCF_USE_SSL) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!wsi->tls.ssl) {
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_TLS)
|
2021-09-22 09:34:56 +01:00
|
|
|
if (!wsi->transaction_from_pipeline_queue &&
|
|
|
|
lws_tls_restrict_borrow(wsi)) {
|
|
|
|
*pcce = "tls restriction limit";
|
|
|
|
return CCTLS_RETURN_ERROR;
|
2020-09-19 13:27:33 +01:00
|
|
|
}
|
|
|
|
#endif
|
2021-06-25 07:43:09 +01:00
|
|
|
if (lws_ssl_client_bio_create(wsi) < 0) {
|
|
|
|
*pcce = "bio_create failed";
|
|
|
|
return CCTLS_RETURN_ERROR;
|
|
|
|
}
|
2020-09-19 13:27:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!do_c1)
|
2021-06-17 10:07:04 +01:00
|
|
|
return CCTLS_RETURN_DONE;
|
2020-09-19 13:27:33 +01:00
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_GO);
|
|
|
|
lws_metrics_caliper_bind(wsi->cal_conn, wsi->a.context->mt_conn_tls);
|
2021-03-15 05:20:15 +00:00
|
|
|
#if defined(LWS_WITH_CONMON)
|
|
|
|
wsi->conmon_datum = lws_now_usecs();
|
|
|
|
#endif
|
2021-01-06 15:08:22 +00:00
|
|
|
|
2020-09-19 13:27:33 +01:00
|
|
|
n = lws_ssl_client_connect1(wsi, (char *)wsi->a.context->pt[(int)wsi->tsi].serv_buf,
|
|
|
|
wsi->a.context->pt_serv_buf_size);
|
|
|
|
lwsl_debug("%s: lws_ssl_client_connect1: %d\n", __func__, n);
|
|
|
|
if (!n)
|
|
|
|
return CCTLS_RETURN_RETRY; /* caller should return 0 */
|
2021-06-17 10:07:04 +01:00
|
|
|
|
2020-09-19 13:27:33 +01:00
|
|
|
if (n < 0) {
|
|
|
|
*pcce = (const char *)wsi->a.context->pt[(int)wsi->tsi].serv_buf;
|
2021-01-06 15:08:22 +00:00
|
|
|
lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);
|
2020-09-19 13:27:33 +01:00
|
|
|
return CCTLS_RETURN_ERROR;
|
|
|
|
}
|
2021-01-06 15:08:22 +00:00
|
|
|
/* ...connect1 already handled caliper if SSL_accept done */
|
2020-09-19 13:27:33 +01:00
|
|
|
|
|
|
|
lws_tls_server_conn_alpn(wsi);
|
2020-12-19 09:13:24 +00:00
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
} else
|
|
|
|
wsi->tls.ssl = NULL;
|
2020-09-19 13:27:33 +01:00
|
|
|
|
|
|
|
return CCTLS_RETURN_DONE; /* OK */
|
|
|
|
}
|