1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2019 - 2021 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * This file contains the stuff related to JSON-provided policy, it's not built
25 * if LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY enabled.
26 */
27
28 #include <private-lib-core.h>
29
30 static const char * const lejp_tokens_policy[] = {
31 "release",
32 "product",
33 "schema-version",
34 "via-socks5",
35 "retry[].*.backoff",
36 "retry[].*.conceal",
37 "retry[].*.jitterpc",
38 "retry[].*.svalidping",
39 "retry[].*.svalidhup",
40 "retry[].*",
41 "certs[].*",
42 "trust_stores[].name",
43 "trust_stores[].stack",
44 "metrics[].name",
45 "metrics[].us_schedule",
46 "metrics[].us_halflife",
47 "metrics[].min_outlier",
48 "metrics[].report",
49 "s[].*.endpoint",
50 "s[].*.via-socks5",
51 "s[].*.protocol",
52 "s[].*.port",
53 "s[].*.plugins",
54 "s[].*.tls",
55 "s[].*.client_cert",
56 "s[].*.opportunistic",
57 "s[].*.nailed_up",
58 "s[].*.allow_redirects",
59 "s[].*.urgent_tx",
60 "s[].*.urgent_rx",
61 "s[].*.attr_priority",
62 "s[].*.attr_low_latency",
63 "s[].*.attr_high_throughput",
64 "s[].*.attr_high_reliability",
65 "s[].*.attr_low_cost",
66 "s[].*.long_poll",
67 "s[].*.ws_prioritize_reads",
68 "s[].*.retry",
69 "s[].*.timeout_ms",
70 "s[].*.perf",
71 "s[].*.tls_trust_store",
72 "s[].*.proxy_buflen",
73 "s[].*.proxy_buflen_rxflow_on_above",
74 "s[].*.proxy_buflen_rxflow_off_below",
75 "s[].*.client_buflen",
76 "s[].*.client_buflen_rxflow_on_above",
77 "s[].*.client_buflen_rxflow_off_below",
78 "s[].*.metadata",
79 "s[].*.metadata[].*",
80 "s[].*.http_resp_map",
81 "s[].*.http_resp_map[].*",
82
83 "s[].*.http_auth_header",
84 "s[].*.http_dsn_header",
85 "s[].*.http_fwv_header",
86 "s[].*.http_devtype_header",
87
88 "s[].*.http_auth_preamble",
89
90 "s[].*.http_no_content_length",
91 "s[].*.rideshare", /* streamtype name this rides shotgun with */
92 "s[].*.payload_fmt",
93 "s[].*.http_method",
94 "s[].*.http_url",
95 "s[].*.nghttp2_quirk_end_stream",
96 "s[].*.h2q_oflow_txcr",
97 "s[].*.http_multipart_name",
98 "s[].*.http_multipart_filename",
99 "s[].*.http_mime_content_type",
100 "s[].*.http_www_form_urlencoded",
101 "s[].*.http_expect",
102 "s[].*.http_cookies",
103 "s[].*.http_fail_redirect",
104 "s[].*.http_multipart_ss_in",
105 "s[].*.ws_subprotocol",
106 "s[].*.ws_binary",
107 "s[].*.local_sink",
108 "s[].*.server",
109 "s[].*.server_cert",
110 "s[].*.server_key",
111 "s[].*.mqtt_topic",
112 "s[].*.mqtt_subscribe",
113 "s[].*.mqtt_qos",
114 "s[].*.mqtt_keep_alive",
115 "s[].*.mqtt_clean_start",
116 "s[].*.mqtt_will_topic",
117 "s[].*.mqtt_will_message",
118 "s[].*.mqtt_will_qos",
119 "s[].*.mqtt_will_retain",
120 "s[].*.mqtt_birth_topic",
121 "s[].*.mqtt_birth_message",
122 "s[].*.mqtt_birth_qos",
123 "s[].*.mqtt_birth_retain",
124 "s[].*.aws_iot",
125 "s[].*.swake_validity",
126 "s[].*.use_auth",
127 "s[].*.aws_region",
128 "s[].*.aws_service",
129 "s[].*.direct_proto_str",
130 "s[].*",
131 "auth[].name",
132 "auth[].type",
133 "auth[].streamtype",
134 "auth[].blob",
135 "auth[]",
136 };
137
138 typedef enum {
139 LSSPPT_RELEASE,
140 LSSPPT_PRODUCT,
141 LSSPPT_SCHEMA_VERSION,
142 LSSPPT_VIA_SOCKS5,
143 LSSPPT_BACKOFF,
144 LSSPPT_CONCEAL,
145 LSSPPT_JITTERPC,
146 LSSPPT_VALIDPING_S,
147 LSSPPT_VALIDHUP_S,
148 LSSPPT_RETRY,
149 LSSPPT_CERTS,
150 LSSPPT_TRUST_STORES_NAME,
151 LSSPPT_TRUST_STORES_STACK,
152 LSSPPT_METRICS_NAME,
153 LSSPPT_METRICS_US_SCHEDULE,
154 LSSPPT_METRICS_US_HALFLIFE,
155 LSSPPT_METRICS_MIN_OUTLIER,
156 LSSPPT_METRICS_REPORT,
157 LSSPPT_ENDPOINT,
158 LSSPPT_VH_VIA_SOCKS5,
159 LSSPPT_PROTOCOL,
160 LSSPPT_PORT,
161 LSSPPT_PLUGINS,
162 LSSPPT_TLS,
163 LSSPPT_TLS_CLIENT_CERT,
164 LSSPPT_OPPORTUNISTIC,
165 LSSPPT_NAILED_UP,
166 LSSPPT_ALLOW_REDIRECTS,
167 LSSPPT_URGENT_TX,
168 LSSPPT_URGENT_RX,
169 LSSPPT_ATTR_PRIORITY,
170 LSSPPT_ATTR_LOW_LATENCY,
171 LSSPPT_ATTR_HIGH_THROUGHPUT,
172 LSSPPT_ATTR_HIGH_RELIABILITY,
173 LSSPPT_ATTR_LOW_COST,
174 LSSPPT_LONG_POLL,
175 LSSPPT_PRIORITIZE_READS,
176 LSSPPT_RETRYPTR,
177 LSSPPT_DEFAULT_TIMEOUT_MS,
178 LSSPPT_PERF,
179 LSSPPT_TRUST,
180 LSSPPT_PROXY_BUFLEN,
181 LSSPPT_PROXY_BUFLEN_RXFLOW_ON_ABOVE,
182 LSSPPT_PROXY_BUFLEN_RXFLOW_OFF_BELOW,
183 LSSPPT_CLIENT_BUFLEN,
184 LSSPPT_CLIENT_BUFLEN_RXFLOW_ON_ABOVE,
185 LSSPPT_CLIENT_BUFLEN_RXFLOW_OFF_BELOW,
186 LSSPPT_METADATA,
187 LSSPPT_METADATA_ITEM,
188 LSSPPT_HTTPRESPMAP,
189 LSSPPT_HTTPRESPMAP_ITEM,
190
191 LSSPPT_HTTP_AUTH_HEADER,
192 LSSPPT_HTTP_DSN_HEADER,
193 LSSPPT_HTTP_FWV_HEADER,
194 LSSPPT_HTTP_TYPE_HEADER,
195
196 LSSPPT_HTTP_AUTH_PREAMBLE,
197 LSSPPT_HTTP_NO_CONTENT_LENGTH,
198 LSSPPT_RIDESHARE,
199 LSSPPT_PAYLOAD_FORMAT,
200 LSSPPT_HTTP_METHOD,
201 LSSPPT_HTTP_URL,
202 LSSPPT_NGHTTP2_QUIRK_END_STREAM,
203 LSSPPT_H2_QUIRK_OVERFLOWS_TXCR,
204 LSSPPT_HTTP_MULTIPART_NAME,
205 LSSPPT_HTTP_MULTIPART_FILENAME,
206 LSSPPT_HTTP_MULTIPART_CONTENT_TYPE,
207 LSSPPT_HTTP_WWW_FORM_URLENCODED,
208 LSSPPT_HTTP_EXPECT,
209 LSSPPT_HTTP_COOKIES,
210 LSSPPT_HTTP_FAIL_REDIRECT,
211 LSSPPT_HTTP_MULTIPART_SS_IN,
212 LSSPPT_WS_SUBPROTOCOL,
213 LSSPPT_WS_BINARY,
214 LSSPPT_LOCAL_SINK,
215 LSSPPT_SERVER,
216 LSSPPT_SERVER_CERT,
217 LSSPPT_SERVER_KEY,
218 LSSPPT_MQTT_TOPIC,
219 LSSPPT_MQTT_SUBSCRIBE,
220 LSSPPT_MQTT_QOS,
221 LSSPPT_MQTT_KEEPALIVE,
222 LSSPPT_MQTT_CLEAN_START,
223 LSSPPT_MQTT_WILL_TOPIC,
224 LSSPPT_MQTT_WILL_MESSAGE,
225 LSSPPT_MQTT_WILL_QOS,
226 LSSPPT_MQTT_WILL_RETAIN,
227 LSSPPT_MQTT_BIRTH_TOPIC,
228 LSSPPT_MQTT_BIRTH_MESSAGE,
229 LSSPPT_MQTT_BIRTH_QOS,
230 LSSPPT_MQTT_BIRTH_RETAIN,
231 LSSPPT_MQTT_AWS_IOT,
232 LSSPPT_SWAKE_VALIDITY,
233 LSSPPT_USE_AUTH,
234 LSSPPT_AWS_REGION,
235 LSSPPT_AWS_SERVICE,
236 LSSPPT_DIRECT_PROTO_STR,
237 LSSPPT_STREAMTYPES,
238 LSSPPT_AUTH_NAME,
239 LSSPPT_AUTH_TYPE,
240 LSSPPT_AUTH_STREAMTYPE,
241 LSSPPT_AUTH_BLOB,
242 LSSPPT_AUTH,
243
244 } policy_token_t;
245
246 #define POL_AC_INITIAL 2048
247 #define POL_AC_GRAIN 800
248 #define MAX_CERT_TEMP 3072 /* used to discover actual cert size for realloc */
249
250 static uint16_t sizes[] = {
251 sizeof(backoff_t),
252 sizeof(lws_ss_x509_t),
253 sizeof(lws_ss_trust_store_t),
254 sizeof(lws_ss_policy_t),
255 sizeof(lws_ss_auth_t),
256 sizeof(lws_metric_policy_t),
257 };
258
259 static const char * const protonames[] = {
260 "h1", /* LWSSSP_H1 */
261 "h2", /* LWSSSP_H2 */
262 "ws", /* LWSSSP_WS */
263 "mqtt", /* LWSSSP_MQTT */
264 "raw", /* LWSSSP_RAW */
265 };
266
267 static const lws_ss_auth_t *
lws_ss_policy_find_auth_by_name(struct policy_cb_args * a,const char * name,size_t len)268 lws_ss_policy_find_auth_by_name(struct policy_cb_args *a,
269 const char *name, size_t len)
270 {
271 const lws_ss_auth_t *auth = a->heads[LTY_AUTH].a;
272
273 while (auth) {
274 if (auth->name &&
275 len == strlen(auth->name) &&
276 !strncmp(auth->name, name, len))
277 return auth;
278
279 auth = auth->next;
280 }
281
282 return NULL;
283 }
284
285 static int
lws_ss_policy_alloc_helper(struct policy_cb_args * a,int type)286 lws_ss_policy_alloc_helper(struct policy_cb_args *a, int type)
287 {
288 /*
289 * We do the pointers always as .b union member, all of the
290 * participating structs begin with .next and .name the same
291 */
292
293 a->curr[type].b = lwsac_use_zero(&a->ac,
294 sizes[type], POL_AC_GRAIN);
295 if (!a->curr[type].b)
296 return 1;
297
298 a->curr[type].b->next = a->heads[type].b;
299 a->heads[type].b = a->curr[type].b;
300
301 return 0;
302 }
303
304 static signed char
lws_ss_policy_parser_cb(struct lejp_ctx * ctx,char reason)305 lws_ss_policy_parser_cb(struct lejp_ctx *ctx, char reason)
306 {
307 struct policy_cb_args *a = (struct policy_cb_args *)ctx->user;
308 #if defined(LWS_WITH_SSPLUGINS)
309 const lws_ss_plugin_t **pin;
310 #endif
311 char **pp, dotstar[32], *q;
312 lws_ss_trust_store_t *ts;
313 lws_ss_metadata_t *pmd;
314 lws_ss_x509_t *x, **py;
315 lws_ss_policy_t *p2;
316 lws_retry_bo_t *b;
317 size_t inl, outl;
318 uint8_t *extant;
319 backoff_t *bot;
320 int n = -1;
321
322 // lwsl_debug("%s: %d %d %s\n", __func__, reason, ctx->path_match - 1,
323 // ctx->path);
324
325 switch (ctx->path_match - 1) {
326 case LSSPPT_RETRY:
327 n = LTY_BACKOFF;
328 break;
329 case LSSPPT_CERTS:
330 n = LTY_X509;
331 break;
332 case LSSPPT_TRUST_STORES_NAME:
333 case LSSPPT_TRUST_STORES_STACK:
334 n = LTY_TRUSTSTORE;
335 break;
336 case LSSPPT_STREAMTYPES:
337 n = LTY_POLICY;
338 break;
339 case LSSPPT_AUTH:
340 n = LTY_AUTH;
341 break;
342 case LSSPPT_METRICS_NAME:
343 case LSSPPT_METRICS_US_SCHEDULE:
344 case LSSPPT_METRICS_US_HALFLIFE:
345 case LSSPPT_METRICS_MIN_OUTLIER:
346 case LSSPPT_METRICS_REPORT:
347 n = LTY_METRICS;
348 break;
349 }
350
351 if (reason == LEJPCB_ARRAY_START &&
352 (ctx->path_match - 1 == LSSPPT_PLUGINS ||
353 ctx->path_match - 1 == LSSPPT_METADATA ||
354 ctx->path_match - 1 == LSSPPT_HTTPRESPMAP))
355 a->count = 0;
356
357 if (reason == LEJPCB_OBJECT_START && n == LTY_AUTH) {
358 if (lws_ss_policy_alloc_helper(a, LTY_AUTH))
359 goto oom;
360 return 0;
361 }
362
363 if (reason == LEJPCB_ARRAY_END &&
364 ctx->path_match - 1 == LSSPPT_TRUST_STORES_STACK && !a->count) {
365 lwsl_err("%s: at least one cert required in trust store\n",
366 __func__);
367 goto oom;
368 }
369
370 if (reason == LEJPCB_ARRAY_END && a->count && a->pending_respmap) {
371
372 // lwsl_notice("%s: allocating respmap %d\n", __func__, a->count);
373
374 a->curr[LTY_POLICY].p->u.http.respmap = lwsac_use_zero(&a->ac,
375 sizeof(lws_ss_http_respmap_t) * (unsigned int)a->count, POL_AC_GRAIN);
376
377 if (!a->curr[LTY_POLICY].p->u.http.respmap)
378 goto oom;
379
380 memcpy((void *)a->curr[LTY_POLICY].p->u.http.respmap,
381 a->respmap, sizeof(lws_ss_http_respmap_t) * (unsigned int)a->count);
382 a->curr[LTY_POLICY].p->u.http.count_respmap = (uint8_t)a->count;
383 a->count = 0;
384 a->pending_respmap = 0;
385
386 return 0;
387 }
388
389 if (reason == LEJPCB_OBJECT_END && a->p) {
390 /*
391 * Allocate a just-the-right-size buf for the cert DER now
392 * we decoded it into the a->p temp buffer and know the exact
393 * size.
394 *
395 * The struct *x is in the lwsac... the ca_der it points to
396 * is individually allocated from the heap
397 */
398 a->curr[LTY_X509].x->ca_der = lws_malloc((unsigned int)a->count, "ssx509");
399 if (!a->curr[LTY_X509].x->ca_der)
400 goto oom;
401 memcpy((uint8_t *)a->curr[LTY_X509].x->ca_der, a->p, (unsigned int)a->count);
402 a->curr[LTY_X509].x->ca_der_len = (unsigned int)a->count;
403
404 /*
405 * ... and then we can free the temp buffer
406 */
407 lws_free_set_NULL(a->p);
408
409 return 0;
410 }
411
412 if (reason == LEJPCB_PAIR_NAME && n != -1 &&
413 (n != LTY_TRUSTSTORE && n != LTY_AUTH && n != LTY_METRICS)) {
414
415 p2 = NULL;
416 if (n == LTY_POLICY) {
417 /*
418 * We want to allow for the possibility of overlays...
419 * eg, we come later with a JSON snippet that overrides
420 * select streamtype members of a streamtype that was
421 * already defined
422 */
423 p2 = (lws_ss_policy_t *)a->context->pss_policies;
424
425 while (p2) {
426 if (!strncmp(p2->streamtype,
427 ctx->path + ctx->st[ctx->sp].p,
428 (unsigned int)(ctx->path_match_len -
429 ctx->st[ctx->sp].p))) {
430 lwsl_info("%s: overriding s[] %s\n",
431 __func__, p2->streamtype);
432 break;
433 }
434
435 p2 = p2->next;
436 }
437 }
438
439 /*
440 * We do the pointers always as .b union member, all of the
441 * participating structs begin with .next and .name the same
442 */
443 if (p2) /* we may be overriding existing streamtype... */
444 a->curr[n].b = (backoff_t *)p2;
445 else
446 a->curr[n].b = lwsac_use_zero(&a->ac, sizes[n],
447 POL_AC_GRAIN);
448 if (!a->curr[n].b)
449 goto oom;
450
451 if (n == LTY_X509) {
452 a->p = lws_malloc(MAX_CERT_TEMP, "cert temp");
453 if (!a->p)
454 goto oom;
455 memset(&a->b64, 0, sizeof(a->b64));
456 }
457
458 a->count = 0;
459 if (!p2) {
460 a->curr[n].b->next = a->heads[n].b;
461 a->heads[n].b = a->curr[n].b;
462 pp = (char **)&a->curr[n].b->name;
463
464 goto string1;
465 }
466
467 return 0; /* overriding */
468 }
469
470 if (!(reason & LEJP_FLAG_CB_IS_VALUE) || !ctx->path_match)
471 return 0;
472
473 switch (ctx->path_match - 1) {
474
475 /* strings */
476
477 case LSSPPT_RELEASE:
478 break;
479
480 case LSSPPT_PRODUCT:
481 break;
482
483 case LSSPPT_SCHEMA_VERSION:
484 break;
485
486 case LSSPPT_VIA_SOCKS5:
487 /* the global / default proxy */
488 pp = (char **)&a->socks5_proxy;
489 goto string2;
490
491 case LSSPPT_BACKOFF:
492 b = &a->curr[LTY_BACKOFF].b->r;
493 if (b->retry_ms_table_count == 8) {
494 lwsl_err("%s: > 8 backoff levels\n", __func__);
495 return 1;
496 }
497 if (!b->retry_ms_table_count) {
498 b->retry_ms_table = (uint32_t *)lwsac_use_zero(&a->ac,
499 sizeof(uint32_t) * 8, POL_AC_GRAIN);
500 if (!b->retry_ms_table)
501 goto oom;
502 }
503
504 ((uint32_t *)b->retry_ms_table)
505 [b->retry_ms_table_count++] = (uint32_t)atoi(ctx->buf);
506 break;
507
508 case LSSPPT_CONCEAL:
509 a->curr[LTY_BACKOFF].b->r.conceal_count = (uint16_t)atoi(ctx->buf);
510 break;
511
512 case LSSPPT_JITTERPC:
513 a->curr[LTY_BACKOFF].b->r.jitter_percent = (uint8_t)atoi(ctx->buf);
514 break;
515
516 case LSSPPT_VALIDPING_S:
517 a->curr[LTY_BACKOFF].b->r.secs_since_valid_ping = (uint16_t)atoi(ctx->buf);
518 break;
519
520 case LSSPPT_VALIDHUP_S:
521 a->curr[LTY_BACKOFF].b->r.secs_since_valid_hangup = (uint16_t)atoi(ctx->buf);
522 break;
523
524 case LSSPPT_CERTS:
525 if (a->count + ctx->npos >= MAX_CERT_TEMP) {
526 lwsl_err("%s: cert too big\n", __func__);
527 goto oom;
528 }
529 inl = ctx->npos;
530 outl = MAX_CERT_TEMP - (unsigned int)a->count;
531
532 lws_b64_decode_stateful(&a->b64, ctx->buf, &inl,
533 a->p + a->count, &outl,
534 reason == LEJPCB_VAL_STR_END);
535 a->count += (int)outl;
536 if (inl != ctx->npos) {
537 lwsl_err("%s: b64 decode fail\n", __func__);
538 goto oom;
539 }
540 break;
541
542 case LSSPPT_TRUST_STORES_NAME:
543 if (lws_ss_policy_alloc_helper(a, LTY_TRUSTSTORE))
544 goto oom;
545
546 a->count = 0;
547 pp = (char **)&a->curr[LTY_TRUSTSTORE].b->name;
548
549 goto string2;
550
551 case LSSPPT_TRUST_STORES_STACK:
552 if (a->count >= (int)LWS_ARRAY_SIZE(
553 a->curr[LTY_TRUSTSTORE].t->ssx509)) {
554 lwsl_err("%s: trust store too big\n", __func__);
555 goto oom;
556 }
557 lwsl_debug("%s: trust stores stack %.*s\n", __func__,
558 ctx->npos, ctx->buf);
559 x = a->heads[LTY_X509].x;
560 while (x) {
561 if (!strncmp(x->vhost_name, ctx->buf, ctx->npos)) {
562 a->curr[LTY_TRUSTSTORE].t->ssx509[a->count++] = x;
563 a->curr[LTY_TRUSTSTORE].t->count++;
564
565 return 0;
566 }
567 x = x->next;
568 }
569 lws_strnncpy(dotstar, ctx->buf, ctx->npos, sizeof(dotstar));
570 lwsl_err("%s: unknown trust store entry %s\n", __func__,
571 dotstar);
572 goto oom;
573 #if defined(LWS_WITH_SYS_METRICS)
574 case LSSPPT_METRICS_NAME:
575 if (lws_ss_policy_alloc_helper(a, LTY_METRICS))
576 goto oom;
577
578 pp = (char **)&a->curr[LTY_METRICS].b->name;
579
580 goto string2;
581
582 case LSSPPT_METRICS_US_SCHEDULE:
583 a->curr[LTY_METRICS].m->us_schedule = (uint64_t)atoll(ctx->buf);
584 break;
585
586 case LSSPPT_METRICS_US_HALFLIFE:
587 a->curr[LTY_METRICS].m->us_decay_unit = (uint32_t)atol(ctx->buf);
588 break;
589
590 case LSSPPT_METRICS_MIN_OUTLIER:
591 a->curr[LTY_METRICS].m->min_contributors = (uint8_t)atoi(ctx->buf);
592 break;
593
594 case LSSPPT_METRICS_REPORT:
595 pp = (char **)&a->curr[LTY_METRICS].m->report;
596 goto string2;
597 #endif
598
599 case LSSPPT_SERVER_CERT:
600 case LSSPPT_SERVER_KEY:
601
602 /* iterate through the certs */
603
604 py = &a->heads[LTY_X509].x;
605 x = a->heads[LTY_X509].x;
606 while (x) {
607 if (!strncmp(x->vhost_name, ctx->buf, ctx->npos) &&
608 !x->vhost_name[ctx->npos]) {
609 if ((ctx->path_match - 1) == LSSPPT_SERVER_CERT)
610 a->curr[LTY_POLICY].p->trust.server.cert = x;
611 else
612 a->curr[LTY_POLICY].p->trust.server.key = x;
613 /*
614 * Certs that are for servers need to stick
615 * around in DER form, so the vhost can be
616 * instantiated when the server is brought up
617 */
618 x->keep = 1;
619 lwsl_notice("%s: server '%s' keep %d %p\n",
620 __func__, x->vhost_name,
621 ctx->path_match - 1, x);
622
623 /*
624 * Server DER we need to move it to another
625 * list just for destroying it when the context
626 * is destroyed... snip us out of the live
627 * X.509 list
628 */
629
630 *py = x->next;
631
632 /*
633 * ... and instead put us on the list of things
634 * to keep hold of for context destruction
635 */
636
637 x->next = a->context->server_der_list;
638 a->context->server_der_list = x;
639
640 return 0;
641 }
642 py = &x->next;
643 x = x->next;
644 }
645 lws_strnncpy(dotstar, ctx->buf, ctx->npos, sizeof(dotstar));
646 lwsl_err("%s: unknown cert / key %s\n", __func__, dotstar);
647 goto oom;
648
649 case LSSPPT_ENDPOINT:
650 pp = (char **)&a->curr[LTY_POLICY].p->endpoint;
651 goto string2;
652
653 case LSSPPT_VH_VIA_SOCKS5:
654 pp = (char **)&a->curr[LTY_POLICY].p->socks5_proxy;
655 goto string2;
656
657 case LSSPPT_PORT:
658 a->curr[LTY_POLICY].p->port = (uint16_t)atoi(ctx->buf);
659 break;
660
661 case LSSPPT_PROXY_BUFLEN:
662 a->curr[LTY_POLICY].p->proxy_buflen = (uint32_t)atol(ctx->buf);
663 break;
664
665 case LSSPPT_PROXY_BUFLEN_RXFLOW_ON_ABOVE:
666 a->curr[LTY_POLICY].p->proxy_buflen_rxflow_on_above =
667 (uint32_t)atol(ctx->buf);
668 break;
669 case LSSPPT_PROXY_BUFLEN_RXFLOW_OFF_BELOW:
670 a->curr[LTY_POLICY].p->proxy_buflen_rxflow_off_below =
671 (uint32_t)atol(ctx->buf);
672 break;
673
674 case LSSPPT_CLIENT_BUFLEN:
675 a->curr[LTY_POLICY].p->client_buflen = (uint32_t)atol(ctx->buf);
676 break;
677
678 case LSSPPT_CLIENT_BUFLEN_RXFLOW_ON_ABOVE:
679 a->curr[LTY_POLICY].p->client_buflen_rxflow_on_above =
680 (uint32_t)atol(ctx->buf);
681 break;
682 case LSSPPT_CLIENT_BUFLEN_RXFLOW_OFF_BELOW:
683 a->curr[LTY_POLICY].p->client_buflen_rxflow_off_below =
684 (uint32_t)atol(ctx->buf);
685 break;
686
687 case LSSPPT_HTTP_METHOD:
688 pp = (char **)&a->curr[LTY_POLICY].p->u.http.method;
689 goto string2;
690
691 case LSSPPT_HTTP_URL:
692 pp = (char **)&a->curr[LTY_POLICY].p->u.http.url;
693 goto string2;
694
695 case LSSPPT_RIDESHARE:
696 pp = (char **)&a->curr[LTY_POLICY].p->rideshare_streamtype;
697 goto string2;
698
699 case LSSPPT_PAYLOAD_FORMAT:
700 pp = (char **)&a->curr[LTY_POLICY].p->payload_fmt;
701 goto string2;
702
703 case LSSPPT_PLUGINS:
704 #if defined(LWS_WITH_SSPLUGINS)
705 pin = a->context->pss_plugins;
706 if (a->count ==
707 (int)LWS_ARRAY_SIZE(a->curr[LTY_POLICY].p->plugins)) {
708 lwsl_err("%s: too many plugins\n", __func__);
709
710 goto oom;
711 }
712 if (!pin)
713 break;
714 while (*pin) {
715 if (!strncmp((*pin)->name, ctx->buf, ctx->npos)) {
716 a->curr[LTY_POLICY].p->plugins[a->count++] = *pin;
717 return 0;
718 }
719 pin++;
720 }
721 lwsl_err("%s: unknown plugin\n", __func__);
722 goto oom;
723 #else
724 break;
725 #endif
726
727 case LSSPPT_TLS:
728 if (reason == LEJPCB_VAL_TRUE)
729 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_TLS;
730 break;
731
732 case LSSPPT_TLS_CLIENT_CERT:
733 a->curr[LTY_POLICY].p->client_cert = (uint8_t)(atoi(ctx->buf) + 1);
734 break;
735
736 case LSSPPT_AUTH_BLOB:
737 a->curr[LTY_AUTH].a->blob_index = (uint8_t)atoi(ctx->buf);
738 break;
739 case LSSPPT_HTTP_EXPECT:
740 a->curr[LTY_POLICY].p->u.http.resp_expect = (uint16_t)atoi(ctx->buf);
741 break;
742
743 case LSSPPT_DEFAULT_TIMEOUT_MS:
744 a->curr[LTY_POLICY].p->timeout_ms = (uint32_t)atoi(ctx->buf);
745 break;
746
747 case LSSPPT_ATTR_PRIORITY:
748 a->curr[LTY_POLICY].p->priority = (uint8_t)atoi(ctx->buf);
749 break;
750
751 case LSSPPT_OPPORTUNISTIC:
752 if (reason == LEJPCB_VAL_TRUE)
753 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_OPPORTUNISTIC;
754 break;
755 case LSSPPT_NAILED_UP:
756 if (reason == LEJPCB_VAL_TRUE)
757 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_NAILED_UP;
758 break;
759 case LSSPPT_URGENT_TX:
760 if (reason == LEJPCB_VAL_TRUE)
761 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_URGENT_TX;
762 break;
763 case LSSPPT_URGENT_RX:
764 if (reason == LEJPCB_VAL_TRUE)
765 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_URGENT_RX;
766 break;
767 case LSSPPT_LONG_POLL:
768 if (reason == LEJPCB_VAL_TRUE)
769 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_LONG_POLL;
770 break;
771 case LSSPPT_PRIORITIZE_READS:
772 if (reason == LEJPCB_VAL_TRUE)
773 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_PRIORITIZE_READS;
774 break;
775
776 case LSSPPT_HTTP_WWW_FORM_URLENCODED:
777 if (reason == LEJPCB_VAL_TRUE)
778 a->curr[LTY_POLICY].p->flags |=
779 LWSSSPOLF_HTTP_X_WWW_FORM_URLENCODED;
780 break;
781 case LSSPPT_SWAKE_VALIDITY:
782 if (reason == LEJPCB_VAL_TRUE)
783 a->curr[LTY_POLICY].p->flags |=
784 LWSSSPOLF_WAKE_SUSPEND__VALIDITY;
785 break;
786 case LSSPPT_ALLOW_REDIRECTS:
787 if (reason == LEJPCB_VAL_TRUE)
788 a->curr[LTY_POLICY].p->flags |=
789 LWSSSPOLF_ALLOW_REDIRECTS;
790 break;
791 case LSSPPT_HTTP_COOKIES:
792 if (reason == LEJPCB_VAL_TRUE)
793 a->curr[LTY_POLICY].p->flags |=
794 LWSSSPOLF_HTTP_CACHE_COOKIES;
795 break;
796 case LSSPPT_HTTP_MULTIPART_SS_IN:
797 if (reason == LEJPCB_VAL_TRUE)
798 a->curr[LTY_POLICY].p->flags |=
799 LWSSSPOLF_HTTP_MULTIPART_IN;
800 return 0;
801
802 case LSSPPT_ATTR_LOW_LATENCY:
803 if (reason == LEJPCB_VAL_TRUE)
804 a->curr[LTY_POLICY].p->flags |=
805 LWSSSPOLF_ATTR_LOW_LATENCY;
806 return 0;
807
808 case LSSPPT_ATTR_HIGH_THROUGHPUT:
809 if (reason == LEJPCB_VAL_TRUE)
810 a->curr[LTY_POLICY].p->flags |=
811 LWSSSPOLF_ATTR_HIGH_THROUGHPUT;
812 return 0;
813
814 case LSSPPT_ATTR_HIGH_RELIABILITY:
815 if (reason == LEJPCB_VAL_TRUE)
816 a->curr[LTY_POLICY].p->flags |=
817 LWSSSPOLF_ATTR_HIGH_RELIABILITY;
818 return 0;
819
820 case LSSPPT_ATTR_LOW_COST:
821 if (reason == LEJPCB_VAL_TRUE)
822 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_ATTR_LOW_COST;
823 return 0;
824
825 case LSSPPT_PERF:
826 if (reason == LEJPCB_VAL_TRUE)
827 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_PERF;
828 return 0;
829
830 case LSSPPT_RETRYPTR:
831 bot = a->heads[LTY_BACKOFF].b;
832 while (bot) {
833 if (!strncmp(ctx->buf, bot->name, ctx->npos)) {
834 a->curr[LTY_POLICY].p->retry_bo = &bot->r;
835
836 return 0;
837 }
838 bot = bot->next;
839 }
840 lwsl_err("%s: unknown backoff scheme\n", __func__);
841
842 return -1;
843
844 case LSSPPT_TRUST:
845 ts = a->heads[LTY_TRUSTSTORE].t;
846 while (ts) {
847 if (!strncmp(ctx->buf, ts->name, ctx->npos)) {
848 a->curr[LTY_POLICY].p->trust.store = ts;
849 return 0;
850 }
851 ts = ts->next;
852 }
853 lws_strnncpy(dotstar, ctx->buf, ctx->npos, sizeof(dotstar));
854 lwsl_err("%s: unknown trust store name %s\n", __func__,
855 dotstar);
856
857 return -1;
858
859 case LSSPPT_METADATA:
860 break;
861
862 case LSSPPT_USE_AUTH:
863 a->curr[LTY_POLICY].p->auth =
864 lws_ss_policy_find_auth_by_name(a, ctx->buf, ctx->npos);
865 if (!a->curr[LTY_POLICY].p->auth) {
866 lws_strnncpy(dotstar, ctx->buf, ctx->npos, sizeof(dotstar));
867 lwsl_err("%s: unknown auth '%s'\n", __func__, dotstar);
868 return -1;
869 }
870 break;
871
872
873 case LSSPPT_METADATA_ITEM:
874 pmd = a->curr[LTY_POLICY].p->metadata;
875 a->curr[LTY_POLICY].p->metadata = lwsac_use_zero(&a->ac,
876 sizeof(lws_ss_metadata_t) + ctx->npos +
877 (unsigned int)(ctx->path_match_len - ctx->st[ctx->sp - 2].p + 1) + 2,
878 POL_AC_GRAIN);
879 a->curr[LTY_POLICY].p->metadata->next = pmd;
880
881 q = (char *)a->curr[LTY_POLICY].p->metadata +
882 sizeof(lws_ss_metadata_t);
883 a->curr[LTY_POLICY].p->metadata->name = q;
884 memcpy(q, ctx->path + ctx->st[ctx->sp - 2].p + 1,
885 (unsigned int)(ctx->path_match_len - ctx->st[ctx->sp - 2].p));
886
887 q += ctx->path_match_len - ctx->st[ctx->sp - 2].p;
888 a->curr[LTY_POLICY].p->metadata->value__may_own_heap = q;
889 memcpy(q, ctx->buf, ctx->npos);
890
891 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
892 /*
893 * Check the metadata value part to see if it's a well-known
894 * http header... if so, LWS_HTTP_NO_KNOWN_HEADER (0xff) means
895 * no header string match else it's the well-known header index
896 */
897 a->curr[LTY_POLICY].p->metadata->value_is_http_token = (uint8_t)
898 lws_http_string_to_known_header(ctx->buf, ctx->npos);
899 #endif
900
901 a->curr[LTY_POLICY].p->metadata->length = /* the index in handle->metadata */
902 a->curr[LTY_POLICY].p->metadata_count++;
903
904 a->curr[LTY_POLICY].p->metadata->value_length = ctx->npos;
905 break;
906
907 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
908
909 case LSSPPT_HTTPRESPMAP_ITEM:
910 if (a->count >= (int)LWS_ARRAY_SIZE(a->respmap)) {
911 lwsl_err("%s: respmap too big\n", __func__);
912 return -1;
913 }
914 a->respmap[a->count].resp = (uint16_t)
915 atoi(ctx->path + ctx->st[ctx->sp - 2].p + 1);
916 a->respmap[a->count].state = (uint16_t)atoi(ctx->buf);
917 a->pending_respmap = 1;
918 a->count++;
919 break;
920
921 case LSSPPT_HTTP_AUTH_HEADER:
922 case LSSPPT_HTTP_DSN_HEADER:
923 case LSSPPT_HTTP_FWV_HEADER:
924 case LSSPPT_HTTP_TYPE_HEADER:
925 pp = (char **)&a->curr[LTY_POLICY].p->u.http.blob_header[
926 (ctx->path_match - 1) - LSSPPT_HTTP_AUTH_HEADER];
927 goto string2;
928
929 case LSSPPT_HTTP_AUTH_PREAMBLE:
930 pp = (char **)&a->curr[LTY_POLICY].p->u.http.auth_preamble;
931 goto string2;
932
933 case LSSPPT_HTTP_NO_CONTENT_LENGTH:
934 if (reason == LEJPCB_VAL_TRUE)
935 a->curr[LTY_POLICY].p->flags |=
936 LWSSSPOLF_HTTP_NO_CONTENT_LENGTH;
937 break;
938
939 case LSSPPT_NGHTTP2_QUIRK_END_STREAM:
940 if (reason == LEJPCB_VAL_TRUE)
941 a->curr[LTY_POLICY].p->flags |=
942 LWSSSPOLF_QUIRK_NGHTTP2_END_STREAM;
943 break;
944 case LSSPPT_H2_QUIRK_OVERFLOWS_TXCR:
945 if (reason == LEJPCB_VAL_TRUE)
946 a->curr[LTY_POLICY].p->flags |=
947 LWSSSPOLF_H2_QUIRK_OVERFLOWS_TXCR;
948 break;
949 case LSSPPT_HTTP_MULTIPART_NAME:
950 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_HTTP_MULTIPART;
951 pp = (char **)&a->curr[LTY_POLICY].p->u.http.multipart_name;
952 goto string2;
953 case LSSPPT_HTTP_MULTIPART_FILENAME:
954 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_HTTP_MULTIPART;
955 pp = (char **)&a->curr[LTY_POLICY].p->u.http.multipart_filename;
956 goto string2;
957 case LSSPPT_HTTP_MULTIPART_CONTENT_TYPE:
958 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_HTTP_MULTIPART;
959 pp = (char **)&a->curr[LTY_POLICY].p->u.http.multipart_content_type;
960 goto string2;
961
962 case LSSPPT_AUTH_NAME:
963 pp = (char **)&a->curr[LTY_AUTH].a->name;
964 goto string2;
965
966 case LSSPPT_AUTH_STREAMTYPE:
967 pp = (char **)&a->curr[LTY_AUTH].a->streamtype;
968 goto string2;
969 case LSSPPT_AUTH_TYPE:
970 pp = (char **)&a->curr[LTY_AUTH].a->type;
971 goto string2;
972 case LSSPPT_HTTP_FAIL_REDIRECT:
973 a->curr[LTY_POLICY].p->u.http.fail_redirect =
974 reason == LEJPCB_VAL_TRUE;
975 break;
976 #if defined(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4)
977 case LSSPPT_AWS_REGION:
978 pp = (char **)&a->curr[LTY_POLICY].p->aws_region;
979 goto string2;
980
981 case LSSPPT_AWS_SERVICE:
982 pp = (char **)&a->curr[LTY_POLICY].p->aws_service;
983 goto string2;
984 #endif
985
986 #endif
987
988 #if defined(LWS_ROLE_WS)
989
990 case LSSPPT_WS_SUBPROTOCOL:
991 pp = (char **)&a->curr[LTY_POLICY].p->u.http.u.ws.subprotocol;
992 goto string2;
993
994 case LSSPPT_WS_BINARY:
995 a->curr[LTY_POLICY].p->u.http.u.ws.binary =
996 reason == LEJPCB_VAL_TRUE;
997 break;
998 #endif
999
1000 case LSSPPT_LOCAL_SINK:
1001 if (reason == LEJPCB_VAL_TRUE)
1002 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_LOCAL_SINK;
1003 break;
1004
1005 case LSSPPT_SERVER:
1006 if (reason == LEJPCB_VAL_TRUE)
1007 a->curr[LTY_POLICY].p->flags |= LWSSSPOLF_SERVER;
1008 break;
1009
1010 #if defined(LWS_ROLE_MQTT)
1011 case LSSPPT_MQTT_TOPIC:
1012 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.topic;
1013 goto string2;
1014
1015 case LSSPPT_MQTT_SUBSCRIBE:
1016 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.subscribe;
1017 goto string2;
1018
1019 case LSSPPT_MQTT_QOS:
1020 a->curr[LTY_POLICY].p->u.mqtt.qos = (uint8_t)atoi(ctx->buf);
1021 break;
1022
1023 case LSSPPT_MQTT_KEEPALIVE:
1024 a->curr[LTY_POLICY].p->u.mqtt.keep_alive = (uint16_t)atoi(ctx->buf);
1025 break;
1026
1027 case LSSPPT_MQTT_CLEAN_START:
1028 a->curr[LTY_POLICY].p->u.mqtt.clean_start =
1029 reason == LEJPCB_VAL_TRUE;
1030 break;
1031 case LSSPPT_MQTT_WILL_TOPIC:
1032 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.will_topic;
1033 goto string2;
1034
1035 case LSSPPT_MQTT_WILL_MESSAGE:
1036 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.will_message;
1037 goto string2;
1038
1039 case LSSPPT_MQTT_WILL_QOS:
1040 a->curr[LTY_POLICY].p->u.mqtt.will_qos = (uint8_t)atoi(ctx->buf);
1041 break;
1042 case LSSPPT_MQTT_WILL_RETAIN:
1043 a->curr[LTY_POLICY].p->u.mqtt.will_retain =
1044 reason == LEJPCB_VAL_TRUE;
1045 break;
1046 case LSSPPT_MQTT_BIRTH_TOPIC:
1047 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.birth_topic;
1048 goto string2;
1049
1050 case LSSPPT_MQTT_BIRTH_MESSAGE:
1051 pp = (char **)&a->curr[LTY_POLICY].p->u.mqtt.birth_message;
1052 goto string2;
1053
1054 case LSSPPT_MQTT_BIRTH_QOS:
1055 a->curr[LTY_POLICY].p->u.mqtt.birth_qos = (uint8_t)atoi(ctx->buf);
1056 break;
1057 case LSSPPT_MQTT_BIRTH_RETAIN:
1058 a->curr[LTY_POLICY].p->u.mqtt.birth_retain =
1059 reason == LEJPCB_VAL_TRUE;
1060 break;
1061 case LSSPPT_MQTT_AWS_IOT:
1062 if (reason == LEJPCB_VAL_TRUE)
1063 a->curr[LTY_POLICY].p->u.mqtt.aws_iot =
1064 reason == LEJPCB_VAL_TRUE;
1065 break;
1066 #endif
1067 case LSSPPT_DIRECT_PROTO_STR:
1068 if (reason == LEJPCB_VAL_TRUE)
1069 a->curr[LTY_POLICY].p->flags |=
1070 LWSSSPOLF_DIRECT_PROTO_STR;
1071 break;
1072
1073
1074 case LSSPPT_PROTOCOL:
1075 a->curr[LTY_POLICY].p->protocol = 0xff;
1076 for (n = 0; n < (int)LWS_ARRAY_SIZE(protonames); n++)
1077 if (strlen(protonames[n]) == ctx->npos &&
1078 !strncmp(ctx->buf, protonames[n], ctx->npos))
1079 a->curr[LTY_POLICY].p->protocol = (uint8_t)n;
1080
1081 if (a->curr[LTY_POLICY].p->protocol != 0xff)
1082 break;
1083 lws_strnncpy(dotstar, ctx->buf, ctx->npos, sizeof(dotstar));
1084 lwsl_err("%s: unknown protocol name %s\n", __func__, dotstar);
1085 return -1;
1086
1087 default:
1088 break;
1089 }
1090
1091 return 0;
1092
1093 string2:
1094 /*
1095 * If we can do const string folding, reuse the existing string rather
1096 * than make a new entry
1097 */
1098 extant = lwsac_scan_extant(a->ac, (uint8_t *)ctx->buf, (size_t)ctx->npos, 1);
1099 if (extant) {
1100 *pp = (char *)extant;
1101
1102 return 0;
1103 }
1104 *pp = lwsac_use_backfill(&a->ac, (size_t)(ctx->npos + 1), POL_AC_GRAIN);
1105 if (!*pp)
1106 goto oom;
1107 memcpy(*pp, ctx->buf, ctx->npos);
1108 (*pp)[ctx->npos] = '\0';
1109
1110 return 0;
1111
1112 string1:
1113 n = ctx->st[ctx->sp].p;
1114 *pp = lwsac_use_backfill(&a->ac, (size_t)ctx->path_match_len + (size_t)1 - (size_t)n,
1115 POL_AC_GRAIN);
1116 if (!*pp)
1117 goto oom;
1118 memcpy(*pp, ctx->path + n, ctx->path_match_len - (unsigned int)n);
1119 (*pp)[ctx->path_match_len - n] = '\0';
1120
1121 return 0;
1122
1123 oom:
1124 lwsl_err("%s: OOM\n", __func__);
1125 lws_free_set_NULL(a->p);
1126 lwsac_free(&a->ac);
1127
1128 return -1;
1129 }
1130
1131 int
lws_ss_policy_parse_begin(struct lws_context * context,int overlay)1132 lws_ss_policy_parse_begin(struct lws_context *context, int overlay)
1133 {
1134 struct policy_cb_args *args;
1135 char *p;
1136
1137 args = lws_zalloc(sizeof(struct policy_cb_args), __func__);
1138 if (!args) {
1139 lwsl_err("%s: OOM\n", __func__);
1140
1141 return 1;
1142 }
1143 if (overlay)
1144 /* continue to use the existing lwsac */
1145 args->ac = context->ac_policy;
1146 else
1147 /* we don't want to see any old policy */
1148 context->pss_policies = NULL;
1149
1150 context->pol_args = args;
1151 args->context = context;
1152 p = lwsac_use(&args->ac, 1, POL_AC_INITIAL);
1153 if (!p) {
1154 lwsl_err("%s: OOM\n", __func__);
1155 lws_free_set_NULL(context->pol_args);
1156
1157 return -1;
1158 }
1159 *p = 0;
1160 lejp_construct(&args->jctx, lws_ss_policy_parser_cb, args,
1161 lejp_tokens_policy, LWS_ARRAY_SIZE(lejp_tokens_policy));
1162
1163 return 0;
1164 }
1165
1166 int
lws_ss_policy_parse_abandon(struct lws_context * context)1167 lws_ss_policy_parse_abandon(struct lws_context *context)
1168 {
1169 struct policy_cb_args *args = (struct policy_cb_args *)context->pol_args;
1170 lws_ss_x509_t *x;
1171
1172 x = args->heads[LTY_X509].x;
1173 while (x) {
1174 /*
1175 * Free all the client DER buffers now they have been parsed
1176 * into tls library X.509 objects
1177 */
1178 lws_free((void *)x->ca_der);
1179 x->ca_der = NULL;
1180
1181 x = x->next;
1182 }
1183
1184 x = context->server_der_list;
1185 while (x) {
1186 lws_free((void *)x->ca_der);
1187 x->ca_der = NULL;
1188
1189 x = x->next;
1190 }
1191
1192 lejp_destruct(&args->jctx);
1193 lwsac_free(&args->ac);
1194 lws_free_set_NULL(context->pol_args);
1195
1196 context->server_der_list = NULL;
1197
1198 return 0;
1199 }
1200
1201 #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
1202 int
lws_ss_policy_parse_file(struct lws_context * cx,const char * filepath)1203 lws_ss_policy_parse_file(struct lws_context *cx, const char *filepath)
1204 {
1205 struct policy_cb_args *args = (struct policy_cb_args *)cx->pol_args;
1206 uint8_t buf[512];
1207 int n, m, fd = lws_open(filepath, LWS_O_RDONLY);
1208
1209 if (fd < 0)
1210 return LEJP_REJECT_UNKNOWN;
1211
1212 do {
1213 n = (int)read(fd, buf, sizeof(buf));
1214 if (n < 0) {
1215 m = -1;
1216 goto bail;
1217 }
1218
1219 m = lejp_parse(&args->jctx, buf, n);
1220 if (m != LEJP_CONTINUE && m < 0) {
1221 lwsl_err("%s: parse failed line %u: %d: %s\n", __func__,
1222 (unsigned int)args->jctx.line, m,
1223 lejp_error_to_string(m));
1224 lws_ss_policy_parse_abandon(cx);
1225
1226 m = -1;
1227 goto bail;
1228 }
1229
1230 if (m != LEJP_CONTINUE)
1231 break;
1232 } while (n);
1233
1234 m = 0;
1235 bail:
1236 close(fd);
1237
1238 return m;
1239 }
1240 #endif
1241
1242 int
lws_ss_policy_parse(struct lws_context * context,const uint8_t * buf,size_t len)1243 lws_ss_policy_parse(struct lws_context *context, const uint8_t *buf, size_t len)
1244 {
1245 struct policy_cb_args *args = (struct policy_cb_args *)context->pol_args;
1246 int m;
1247
1248 #if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE)
1249 if (args->jctx.line < 2 && buf[0] != '{' && !args->parse_data)
1250 return lws_ss_policy_parse_file(context, (const char *)buf);
1251 #endif
1252
1253 args->parse_data = 1;
1254 m = lejp_parse(&args->jctx, buf, (int)len);
1255 if (m == LEJP_CONTINUE || m >= 0)
1256 return m;
1257
1258 lwsl_err("%s: parse failed line %u: %d: %s\n", __func__,
1259 (unsigned int)args->jctx.line, m, lejp_error_to_string(m));
1260 lws_ss_policy_parse_abandon(context);
1261 assert(0);
1262
1263 return m;
1264 }
1265
1266 int
lws_ss_policy_overlay(struct lws_context * context,const char * overlay)1267 lws_ss_policy_overlay(struct lws_context *context, const char *overlay)
1268 {
1269 lws_ss_policy_parse_begin(context, 1);
1270 return lws_ss_policy_parse(context, (const uint8_t *)overlay,
1271 strlen(overlay));
1272 }
1273
1274 const lws_ss_policy_t *
lws_ss_policy_get(struct lws_context * context)1275 lws_ss_policy_get(struct lws_context *context)
1276 {
1277 struct policy_cb_args *args = (struct policy_cb_args *)context->pol_args;
1278
1279 if (!args)
1280 return NULL;
1281
1282 return args->heads[LTY_POLICY].p;
1283 }
1284
1285 const lws_ss_auth_t *
lws_ss_auth_get(struct lws_context * context)1286 lws_ss_auth_get(struct lws_context *context)
1287 {
1288 struct policy_cb_args *args = (struct policy_cb_args *)context->pol_args;
1289
1290 if (!args)
1291 return NULL;
1292
1293 return args->heads[LTY_AUTH].a;
1294 }
1295