2020-02-29 12:37:24 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <private-lib-core.h>
|
|
|
|
|
|
|
|
extern int
|
|
|
|
secstream_h1(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len);
|
|
|
|
|
|
|
|
static int
|
|
|
|
secstream_h2(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len)
|
|
|
|
{
|
|
|
|
lws_ss_handle_t *h = (lws_ss_handle_t *)lws_get_opaque_user_data(wsi);
|
2020-08-26 11:05:41 +01:00
|
|
|
lws_ss_state_return_t r;
|
2020-02-29 12:37:24 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
|
|
|
|
if (h->being_serialized) {
|
|
|
|
/*
|
|
|
|
* We are the proxy-side SS for a remote client... we
|
|
|
|
* need to inform the client about the initial tx credit
|
|
|
|
* to write to it that the remote h2 server set up
|
|
|
|
*/
|
|
|
|
lwsl_info("%s: reporting initial tx cr from server %d\n",
|
|
|
|
__func__, wsi->txc.tx_cr);
|
|
|
|
ss_proxy_onward_txcr((void *)&h[1], wsi->txc.tx_cr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
n = secstream_h1(wsi, reason, user, in, len);
|
|
|
|
|
|
|
|
if (!n && (h->policy->flags & LWSSSPOLF_LONG_POLL)) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_notice("%s: h2 client %s entering LONG_POLL\n",
|
|
|
|
__func__, lws_wsi_tag(wsi));
|
2020-02-29 12:37:24 +00:00
|
|
|
lws_h2_client_stream_long_poll_rxonly(wsi);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
|
2020-12-15 16:05:37 +00:00
|
|
|
case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
|
|
|
|
if (lws_get_network_wsi(wsi) == wsi)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
|
|
|
|
// lwsl_err("%s: h2 COMPLETED_CLIENT_HTTP\n", __func__);
|
2020-12-25 05:54:19 +00:00
|
|
|
if (h->hanging_som)
|
|
|
|
r = h->info.rx(ss_to_userobj(h), NULL, 0, LWSSS_FLAG_EOM);
|
ss: static policy: dynamic vhost instantiation
Presently a vh is allocated per trust store at policy parsing-time, this
is no problem on a linux-class device or if you decide you need a dynamic
policy for functionality reasons.
However if you're in a constrained enough situation that the static policy
makes sense, in the case your trust stores do not have 100% duty cycle, ie,
are anyway always in use, the currently-unused vhosts and their x.509 stack
are sitting there taking up heap for no immediate benefit.
This patch modifies behaviour in ..._STATIC_POLICY_ONLY so that vhosts and
associated x.509 tls contexts are not instantiated until a secure stream using
them is created; they are refcounted, and when the last logical secure
stream using a vhost is destroyed, the vhost and its tls context is also
destroyed.
If another ss connection is created that wants to use the trust store, the
vhost and x.509 context is regenerated again as needed.
Currently the refcounting is by ss, it's also possible to move the refcounting
to be by connection. The choice is between the delay to generate the vh
being visisble at logical ss creation-time, or at connection-time. It's anyway
not preferable to have ss instantiated and taking up space with no associated
connection or connection attempt underway.
NB you will need to reprocess any static policies after this patch so they
conform to the trust_store changes.
2020-07-20 07:28:28 +01:00
|
|
|
/* decouple the fates of the wsi and the ss */
|
2020-02-29 12:37:24 +00:00
|
|
|
h->wsi = NULL;
|
|
|
|
h->txn_ok = 1;
|
|
|
|
lws_cancel_service(lws_get_context(wsi)); /* abort poll wait */
|
2020-12-25 05:54:19 +00:00
|
|
|
if (h->hanging_som && r == LWSSSSRET_DESTROY_ME)
|
2020-12-22 15:56:41 +00:00
|
|
|
return _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(r, wsi, &h);
|
2020-12-25 05:54:19 +00:00
|
|
|
h->hanging_som = 0;
|
2020-11-02 17:37:32 +08:00
|
|
|
break;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
case LWS_CALLBACK_WSI_TX_CREDIT_GET:
|
|
|
|
/*
|
|
|
|
* The peer has sent us additional tx credit...
|
|
|
|
*/
|
|
|
|
lwsl_info("%s: LWS_CALLBACK_WSI_TX_CREDIT_GET: %d\n",
|
|
|
|
__func__, (int32_t)len);
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
|
|
|
|
if (h->being_serialized)
|
|
|
|
/* we are the proxy-side SS for a remote client */
|
|
|
|
ss_proxy_onward_txcr((void *)&h[1], (int)len);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return secstream_h1(wsi, reason, user, in, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct lws_protocols protocol_secstream_h2 = {
|
|
|
|
"lws-secstream-h2",
|
|
|
|
secstream_h2,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Munge connect info according to protocol-specific considerations... this
|
|
|
|
* usually means interpreting aux in a protocol-specific way and using the
|
|
|
|
* pieces at connection setup time, eg, http url pieces.
|
|
|
|
*
|
|
|
|
* len bytes of buf can be used for things with scope until after the actual
|
|
|
|
* connect.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
secstream_connect_munge_h2(lws_ss_handle_t *h, char *buf, size_t len,
|
|
|
|
struct lws_client_connect_info *i,
|
|
|
|
union lws_ss_contemp *ct)
|
|
|
|
{
|
2020-03-14 06:56:41 +00:00
|
|
|
const char *pbasis = h->policy->u.http.url;
|
|
|
|
size_t used_in, used_out;
|
|
|
|
lws_strexp_t exp;
|
|
|
|
|
|
|
|
/* i.path on entry is used to override the policy urlpath if not "" */
|
|
|
|
|
|
|
|
if (i->path[0])
|
|
|
|
pbasis = i->path;
|
|
|
|
|
2020-02-29 12:37:24 +00:00
|
|
|
if (h->policy->flags & LWSSSPOLF_QUIRK_NGHTTP2_END_STREAM)
|
|
|
|
i->ssl_connection |= LCCSCF_H2_QUIRK_NGHTTP2_END_STREAM;
|
|
|
|
|
|
|
|
if (h->policy->flags & LWSSSPOLF_H2_QUIRK_OVERFLOWS_TXCR)
|
|
|
|
i->ssl_connection |= LCCSCF_H2_QUIRK_OVERFLOWS_TXCR;
|
|
|
|
|
|
|
|
if (h->policy->flags & LWSSSPOLF_HTTP_MULTIPART)
|
|
|
|
i->ssl_connection |= LCCSCF_HTTP_MULTIPART_MIME;
|
|
|
|
|
|
|
|
if (h->policy->flags & LWSSSPOLF_HTTP_X_WWW_FORM_URLENCODED)
|
|
|
|
i->ssl_connection |= LCCSCF_HTTP_X_WWW_FORM_URLENCODED;
|
|
|
|
|
|
|
|
i->ssl_connection |= LCCSCF_PIPELINE;
|
|
|
|
|
|
|
|
i->alpn = "h2";
|
|
|
|
|
|
|
|
/* initial peer tx credit */
|
|
|
|
|
|
|
|
if (h->info.manual_initial_tx_credit) {
|
|
|
|
i->ssl_connection |= LCCSCF_H2_MANUAL_RXFLOW;
|
|
|
|
i->manual_initial_tx_credit = h->info.manual_initial_tx_credit;
|
|
|
|
lwsl_info("%s: initial txcr %d\n", __func__,
|
|
|
|
i->manual_initial_tx_credit);
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:56:41 +00:00
|
|
|
if (!pbasis)
|
2020-02-29 12:37:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* protocol aux is the path part */
|
|
|
|
|
|
|
|
i->path = buf;
|
2020-03-14 06:56:41 +00:00
|
|
|
buf[0] = '/';
|
|
|
|
|
|
|
|
lws_strexp_init(&exp, (void *)h, lws_ss_exp_cb_metadata, buf + 1, len - 1);
|
|
|
|
|
|
|
|
if (lws_strexp_expand(&exp, pbasis, strlen(pbasis),
|
|
|
|
&used_in, &used_out) != LSTRX_DONE)
|
|
|
|
return 1;
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
secstream_tx_credit_add_h2(lws_ss_handle_t *h, int add)
|
|
|
|
{
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: add %d\n", __func__, lws_ss_tag(h), add);
|
2020-02-29 12:37:24 +00:00
|
|
|
if (h->wsi)
|
|
|
|
return lws_h2_update_peer_txcredit(h->wsi, LWS_H2_STREAM_SID, add);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
secstream_tx_credit_est_h2(lws_ss_handle_t *h)
|
|
|
|
{
|
|
|
|
if (h->wsi) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: est %d\n", __func__, lws_ss_tag(h),
|
2020-02-29 12:37:24 +00:00
|
|
|
lws_h2_get_peer_txcredit_estimate(h->wsi));
|
|
|
|
|
|
|
|
return lws_h2_get_peer_txcredit_estimate(h->wsi);
|
|
|
|
}
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: Unknown (0)\n", __func__, lws_ss_tag(h));
|
2020-02-29 12:37:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ss_pcols ss_pcol_h2 = {
|
|
|
|
"h2",
|
2020-12-15 11:50:46 +00:00
|
|
|
"h2",
|
2020-07-27 10:03:12 +01:00
|
|
|
&protocol_secstream_h2,
|
2020-02-29 12:37:24 +00:00
|
|
|
secstream_connect_munge_h2,
|
|
|
|
secstream_tx_credit_add_h2,
|
|
|
|
secstream_tx_credit_est_h2
|
|
|
|
};
|