You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
289 lines
9.2 KiB
289 lines
9.2 KiB
/*
|
|
* lws-minimal-secure-streams-sink
|
|
*
|
|
* Written in 2010-2020 by Andy Green <andy@warmcat.com>
|
|
*
|
|
* This file is made available under the Creative Commons CC0 1.0
|
|
* Universal Public Domain Dedication.
|
|
*/
|
|
|
|
#include <libwebsockets.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
|
|
static int interrupted, bad = 1;
|
|
static const char * const default_ss_policy =
|
|
"{"
|
|
"\"release\":" "\"01234567\","
|
|
"\"product\":" "\"myproduct\","
|
|
"\"schema-version\":" "1,"
|
|
"\"retry\": [" /* named backoff / retry strategies */
|
|
"{\"default\": {"
|
|
"\"backoff\": [" "1000,"
|
|
"2000,"
|
|
"3000,"
|
|
"5000,"
|
|
"10000"
|
|
"],"
|
|
"\"conceal\":" "5,"
|
|
"\"jitterpc\":" "20,"
|
|
"\"svalidping\":" "300,"
|
|
"\"svalidhup\":" "310"
|
|
"}}"
|
|
"],"
|
|
"\"certs\": [" /* named individual certificates in BASE64 DER */
|
|
/*
|
|
* Need to be in order from root cert... notice sometimes as
|
|
* with Let's Encrypt there are multiple possible validation
|
|
* paths, all the pieces for one validation path must be
|
|
* given, excluding the server cert itself. Let's Encrypt
|
|
* intermediate is signed by their ISRG Root CA but also is
|
|
* cross-signed by an IdenTrust intermediate that's widely
|
|
* deployed in browsers. We use the ISRG path because that
|
|
* way we can skip the extra IdenTrust root cert.
|
|
*/
|
|
"{\"isrg_root_x1\": \"" /* ISRG ROOT X1 */
|
|
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw"
|
|
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh"
|
|
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MT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"
|
|
"VR0jBBgwFoAUebRZ5nu25eQBc4AIiMgaWPbpm24wDQYJKoZIhvcNAQELBQADggIB"
|
|
"ABnPdSA0LTqmRf/Q1eaM2jLonG4bQdEnqOJQ8nCqxOeTRrToEKtwT++36gTSlBGx"
|
|
"A/5dut82jJQ2jxN8RI8L9QFXrWi4xXnA2EqA10yjHiR6H9cj6MFiOnb5In1eWsRM"
|
|
"UM2v3e9tNsCAgBukPHAg1lQh07rvFKm/Bz9BCjaxorALINUfZ9DD64j2igLIxle2"
|
|
"DPxW8dI/F2loHMjXZjqG8RkqZUdoxtID5+90FgsGIfkMpqgRS05f4zPbCEHqCXl1"
|
|
"eO5HyELTgcVlLXXQDgAWnRzut1hFJeczY1tjQQno6f6s+nMydLN26WuU4s3UYvOu"
|
|
"OsUxRlJu7TSRHqDC3lSE5XggVkzdaPkuKGQbGpny+01/47hfXXNB7HntWNZ6N2Vw"
|
|
"p7G6OfY+YQrZwIaQmhrIqJZuigsrbe3W+gdn5ykE9+Ky0VgVUsfxo52mwFYs1JKY"
|
|
"2PGDuWx8M6DlS6qQkvHaRUo0FMd8TsSlbF0/v965qGFKhSDeQoMpYnwcmQilRh/0"
|
|
"ayLThlHLN81gSkJjVrPI0Y8xCVPB4twb1PFUd2fPM3sA1tJ83sZ5v8vgFv2yofKR"
|
|
"PB0t6JzUA81mSqM3kxl5e+IZwhYAyO0OTg3/fs8HqGTNKd9BqoUwSRBzp06JMg5b"
|
|
"rUCGwbCUDI0mxadJ3Bz4WxR6fyNpBK2yAinWEsikxqEt"
|
|
"\"}"
|
|
"],"
|
|
"\"trust_stores\": [" /* named cert chains */
|
|
"{"
|
|
"\"name\": \"le_via_isrg\","
|
|
"\"stack\": ["
|
|
"\"isrg_root_x1\","
|
|
"\"LEX3_isrg_root_x1\""
|
|
"]"
|
|
"}"
|
|
"],"
|
|
"\"s\": [" /* the supported stream types */
|
|
"{\"\": {"
|
|
"\"endpoint\":" "\"warmcat.com\","
|
|
"\"port\":" "443,"
|
|
"\"protocol\":" "\"h2\","
|
|
"\"http_method\":" "\"GET\","
|
|
"\"http_url\":" "\"index.html\","
|
|
"\"plugins\":" "[],"
|
|
"\"tls\":" "true,"
|
|
"\"nailed_up\":" "true,"
|
|
"\"long_poll\":" "true,"
|
|
"\"retry\":" "\"default\","
|
|
"\"tls_trust_store\":" "\"le_via_isrg\""
|
|
"}}"
|
|
"]"
|
|
"}"
|
|
;
|
|
|
|
typedef struct myss {
|
|
struct lws_ss_handle *ss;
|
|
void *opaque_data;
|
|
/* ... application specific state ... */
|
|
lws_sorted_usec_list_t sul;
|
|
|
|
int count;
|
|
} myss_t;
|
|
|
|
/* secure streams payload interface */
|
|
|
|
static int
|
|
myss_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
|
|
{
|
|
// myss_t *m = (myss_t *)userobj;
|
|
|
|
lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
|
|
lwsl_hexdump_info(buf, len);
|
|
|
|
/*
|
|
* If we received the whole message, for our example it means
|
|
* we are done.
|
|
*/
|
|
if (flags & LWSSS_FLAG_EOM) {
|
|
bad = 0;
|
|
interrupted = 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
myss_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
|
|
int *flags)
|
|
{
|
|
//myss_t *m = (myss_t *)userobj;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
myss_state(void *userobj, void *sh, lws_ss_constate_t state,
|
|
lws_ss_tx_ordinal_t ack)
|
|
{
|
|
myss_t *m = (myss_t *)userobj;
|
|
|
|
lwsl_user("%s: %s, ord 0x%x\n", __func__, lws_ss_state_name(state),
|
|
(unsigned int)ack);
|
|
|
|
switch (state) {
|
|
case LWSSSCS_CREATING:
|
|
lws_ss_request_tx(m->ss);
|
|
break;
|
|
case LWSSSCS_ALL_RETRIES_FAILED:
|
|
/* if we're out of retries, we want to close the app and FAIL */
|
|
interrupted = 1;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
sigint_handler(int sig)
|
|
{
|
|
interrupted = 1;
|
|
}
|
|
|
|
int main(int argc, const char **argv)
|
|
{
|
|
struct lws_context_creation_info info;
|
|
struct lws_context *context;
|
|
int n = 0;
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
|
|
lws_cmdline_option_handle_builtin(argc, argv, &info);
|
|
lwsl_user("LWS secure streams [-d<verb>] [-f] [-p] [--h1post]\n");
|
|
|
|
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
|
|
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
info.fd_limit_per_thread = 1 + 6 + 1;
|
|
info.pss_policies_json = default_ss_policy;
|
|
info.port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
context = lws_create_context(&info);
|
|
if (!context) {
|
|
lwsl_err("lws init failed\n");
|
|
return 1;
|
|
}
|
|
|
|
// puts(default_ss_policy);
|
|
|
|
if (lws_cmdline_option(argc, argv, "-p")) {
|
|
|
|
/* we are being the proxy */
|
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
|
|
if (lws_ss_proxy_create(context, NULL, 0)) {
|
|
lwsl_err("%s: failed to create ss proxy\n", __func__);
|
|
goto bail;
|
|
}
|
|
lwsl_notice("%s: secure streams proxy mode\n", __func__);
|
|
#else
|
|
lwsl_err("%s: needs cmake LWS_WITH_SECURE_STREAMS_PROXY_API\n",
|
|
__func__);
|
|
#endif
|
|
} else {
|
|
lws_ss_info_t ssi;
|
|
|
|
/* We're making an outgoing secure stream ourselves */
|
|
|
|
memset(&ssi, 0, sizeof(ssi));
|
|
ssi.handle_offset = offsetof(myss_t, ss);
|
|
ssi.opaque_user_data_offset = offsetof(myss_t, opaque_data);
|
|
ssi.rx = myss_rx;
|
|
ssi.tx = myss_tx;
|
|
ssi.state = myss_state;
|
|
ssi.user_alloc = sizeof(myss_t);
|
|
|
|
/* requested to fail (to check backoff)? */
|
|
if (lws_cmdline_option(argc, argv, "-f"))
|
|
ssi.streamtype = "mintest-fail";
|
|
else
|
|
/* request to check h1 POST */
|
|
if (lws_cmdline_option(argc, argv, "--h1post"))
|
|
ssi.streamtype = "minpost";
|
|
else
|
|
/* default to h1 GET */
|
|
ssi.streamtype = "mintest";
|
|
|
|
if (lws_ss_create(context, 0, &ssi, NULL, NULL, NULL, NULL)) {
|
|
lwsl_err("%s: failed to create secure stream\n",
|
|
__func__);
|
|
goto bail;
|
|
}
|
|
}
|
|
|
|
/* the event loop */
|
|
|
|
while (n >= 0 && !interrupted)
|
|
n = lws_service(context, 0);
|
|
|
|
bail:
|
|
|
|
lws_context_destroy(context);
|
|
lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
|
|
|
|
return bad;
|
|
}
|