libwebsockets
Lightweight C library for HTML5 websockets
lws-secure-streams-policy.h
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  * included from libwebsockets.h
25  */
26 
27 typedef int (*plugin_auth_status_cb)(struct lws_ss_handle *ss, int status);
28 
54 /* the public, const metrics policy definition */
55 
56 typedef struct lws_metric_policy {
57  /* order of first two mandated by JSON policy parsing scope union */
58  const struct lws_metric_policy *next;
59  const char *name;
60 
61  const char *report;
62 
64  uint64_t us_schedule;
67  uint32_t us_decay_unit;
72 
73 typedef struct lws_ss_x509 {
74  struct lws_ss_x509 *next;
75  const char *vhost_name;
76  const uint8_t *ca_der;
77  size_t ca_der_len;
78  uint8_t keep:1;
80 
81 enum {
82  LWSSSPOLF_OPPORTUNISTIC = (1 << 0),
84  LWSSSPOLF_NAILED_UP = (1 << 1),
86  LWSSSPOLF_URGENT_TX = (1 << 2),
88  LWSSSPOLF_URGENT_RX = (1 << 3),
90  LWSSSPOLF_TLS = (1 << 4),
92  LWSSSPOLF_LONG_POLL = (1 << 5),
94  LWSSSPOLF_AUTH_BEARER = (1 << 6),
96  LWSSSPOLF_HTTP_NO_CONTENT_LENGTH = (1 << 7),
98  LWSSSPOLF_QUIRK_NGHTTP2_END_STREAM = (1 << 8),
100  LWSSSPOLF_H2_QUIRK_OVERFLOWS_TXCR = (1 << 9),
102  LWSSSPOLF_H2_QUIRK_UNCLEAN_HPACK_STATE = (1 << 10),
104  LWSSSPOLF_HTTP_MULTIPART = (1 << 11),
110  LWSSSPOLF_HTTP_X_WWW_FORM_URLENCODED = (1 << 12),
112  LWSSSPOLF_LOCAL_SINK = (1 << 13),
114  LWSSSPOLF_WAKE_SUSPEND__VALIDITY = (1 << 14),
118  LWSSSPOLF_SERVER = (1 << 15),
120  LWSSSPOLF_ALLOW_REDIRECTS = (1 << 16),
122  LWSSSPOLF_HTTP_MULTIPART_IN = (1 << 17),
125  LWSSSPOLF_ATTR_LOW_LATENCY = (1 << 18),
127  LWSSSPOLF_ATTR_HIGH_THROUGHPUT = (1 << 19),
129  LWSSSPOLF_ATTR_HIGH_RELIABILITY = (1 << 20),
131  LWSSSPOLF_ATTR_LOW_COST = (1 << 21),
133  LWSSSPOLF_PERF = (1 << 22),
135  LWSSSPOLF_DIRECT_PROTO_STR = (1 << 23),
137  LWSSSPOLF_HTTP_CACHE_COOKIES = (1 << 24),
139  LWSSSPOLF_PRIORITIZE_READS = (1 << 25),
142 };
143 
144 typedef struct lws_ss_trust_store {
145  struct lws_ss_trust_store *next;
146  const char *name;
147 
148  const lws_ss_x509_t *ssx509[6];
149  int count;
151 
152 enum {
153  LWSSSP_H1,
154  LWSSSP_H2,
155  LWSSSP_WS,
156  LWSSSP_MQTT,
157  LWSSSP_RAW,
158 
159 
160  LWSSS_HBI_AUTH = 0,
161  LWSSS_HBI_DSN,
162  LWSSS_HBI_FWV,
163  LWSSS_HBI_TYPE,
164 
165  _LWSSS_HBI_COUNT /* always last */
166 };
167 
168 /*
169  * This does for both the static policy metadata entry, and the runtime metadata
170  * handling object.
171  */
172 
173 typedef struct lws_ss_metadata {
174  struct lws_ss_metadata *next;
175  const char *name;
176  void *value__may_own_heap;
177  size_t length;
178 
179  uint8_t value_length; /* only valid if set by policy */
180  uint8_t value_is_http_token; /* valid if set by policy */
181 #if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR)
182  uint8_t name_on_lws_heap:1; /* proxy metatadata does this */
183 #endif
184  uint8_t value_on_lws_heap:1; /* proxy + rx metadata does this */
185 #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
186  uint8_t pending_onward:1;
187 #endif
189 
190 typedef struct lws_ss_http_respmap {
191  uint16_t resp; /* the http response code */
192  uint16_t state; /* low 16-bits of associated state */
194 
195 /*
196  * This is a mapping between an auth streamtype and a name and other information
197  * that can be independently instantiated. Other streamtypes can indicate they
198  * require this authentication on their connection.
199  */
200 
201 typedef struct lws_ss_auth {
202  struct lws_ss_auth *next;
203  const char *name;
204 
205  const char *type;
206  const char *streamtype;
207  uint8_t blob_index;
208 } lws_ss_auth_t;
209 
223 typedef struct lws_ss_policy {
224  struct lws_ss_policy *next;
225  const char *streamtype;
227  const char *endpoint;
228  const char *rideshare_streamtype;
231  const char *payload_fmt;
232  const char *socks5_proxy;
233  lws_ss_metadata_t *metadata; /* linked-list of metadata */
234  const lws_metric_policy_t *metrics; /* linked-list of metric policies */
235  const lws_ss_auth_t *auth; /* NULL or auth object we bind to */
236 
237 #if defined(LWS_WITH_SERVER)
238  const struct lws_protocol_vhost_options *pvo;
239 #endif
240 
241  /* protocol-specific connection policy details */
242 
243  union {
244 
245 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2) || defined(LWS_ROLE_WS)
246 
247  /* details for http-related protocols... */
248 
249  struct {
250 
251  /* common to all http-related protocols */
252 
253  const char *method;
254  const char *url;
255 
256  const char *multipart_name;
257  const char *multipart_filename;
258  const char *multipart_content_type;
259 
260  const char *blob_header[_LWSSS_HBI_COUNT];
261  const char *auth_preamble;
262 
263  const lws_ss_http_respmap_t *respmap;
264 
265  union {
266 // struct { /* LWSSSP_H1 */
267 // } h1;
268 // struct { /* LWSSSP_H2 */
269 // } h2;
270  struct { /* LWSSSP_WS */
271  const char *subprotocol;
272  uint8_t binary;
273  /* false = TEXT, true = BINARY */
274  } ws;
275  } u;
276 
277  uint16_t resp_expect;
278  uint8_t count_respmap;
279  uint8_t fail_redirect:1;
280  } http;
281 
282 #endif
283 
284 #if defined(LWS_ROLE_MQTT)
285 
286  struct {
287  const char *topic; /* stream sends on this topic */
288  const char *subscribe; /* stream subscribes to this topic */
289 
290  const char *will_topic;
291  const char *will_message;
292 
293  const char *birth_topic;
294  const char *birth_message;
295 
296  uint16_t keep_alive;
297  uint8_t qos;
298  uint8_t clean_start;
299  uint8_t will_qos;
300  uint8_t will_retain;
301  uint8_t birth_qos;
302  uint8_t birth_retain;
303  uint8_t aws_iot;
304 
305  } mqtt;
306 
307 #endif
308 
309  /* details for non-http related protocols... */
310  } u;
311 
312 #if defined(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4)
313  /* directly point to the metadata name, no need to expand */
314  const char *aws_region;
315  const char *aws_service;
316 #endif
317  /*
318  * We're either a client connection policy that wants a trust store,
319  * or we're a server policy that wants a mem cert and key... Hold
320  * these mutually-exclusive things in a union.
321  */
322 
323  union {
327  struct {
332  } server;
333  } trust;
334 
337  int32_t txc;
338  int32_t txc_peer;
339 
340  uint32_t proxy_buflen;
341  uint32_t proxy_buflen_rxflow_on_above;
342  uint32_t proxy_buflen_rxflow_off_below;
343 
344  uint32_t client_buflen;
345  uint32_t client_buflen_rxflow_on_above;
346  uint32_t client_buflen_rxflow_off_below;
347 
348  uint32_t timeout_ms;
350  uint32_t flags;
352  uint16_t port;
354  uint8_t metadata_count;
355  uint8_t protocol;
356  uint8_t client_cert;
358  uint8_t priority; /* 0 = normal, 6 = max normal,
359  * 7 = network management */
361 
362 #if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
363 
364 /*
365  * These only exist / have meaning if there's a dynamic JSON policy enabled
366  */
367 
368 LWS_VISIBLE LWS_EXTERN int
369 lws_ss_policy_parse_begin(struct lws_context *context, int overlay);
370 
371 LWS_VISIBLE LWS_EXTERN int
372 lws_ss_policy_parse_abandon(struct lws_context *context);
373 
374 LWS_VISIBLE LWS_EXTERN int
375 lws_ss_policy_parse(struct lws_context *context, const uint8_t *buf, size_t len);
376 
377 LWS_VISIBLE LWS_EXTERN int
378 lws_ss_policy_overlay(struct lws_context *context, const char *overlay);
379 
380 /*
381  * You almost certainly don't want these, they return the first policy or auth
382  * object in a linked-list of objects created by lws_ss_policy_parse above,
383  * they are exported to generate static policy with
384  */
385 LWS_VISIBLE LWS_EXTERN const lws_ss_policy_t *
386 lws_ss_policy_get(struct lws_context *context);
387 
388 LWS_VISIBLE LWS_EXTERN const lws_ss_auth_t *
389 lws_ss_auth_get(struct lws_context *context);
390 
391 #endif
Definition: lws-secure-streams-policy.h:56
uint32_t us_decay_unit
Definition: lws-secure-streams-policy.h:67
uint8_t min_contributors
Definition: lws-secure-streams-policy.h:69
uint64_t us_schedule
Definition: lws-secure-streams-policy.h:64
const char * report
Definition: lws-secure-streams-policy.h:61
Definition: lws-context-vhost.h:1284
Definition: lws-retry.h:25
Definition: lws-secure-streams-policy.h:201
Definition: lws-secure-streams-policy.h:190
Definition: lws-secure-streams-policy.h:173
Definition: lws-secure-streams-policy.h:223
uint32_t flags
Definition: lws-secure-streams-policy.h:350
uint8_t metadata_count
Definition: lws-secure-streams-policy.h:354
uint32_t timeout_ms
Definition: lws-secure-streams-policy.h:348
const lws_ss_x509_t * cert
Definition: lws-secure-streams-policy.h:328
const char * streamtype
Definition: lws-secure-streams-policy.h:225
const char * rideshare_streamtype
Definition: lws-secure-streams-policy.h:228
uint8_t client_cert
Definition: lws-secure-streams-policy.h:356
const char * endpoint
Definition: lws-secure-streams-policy.h:227
const lws_ss_trust_store_t * store
Definition: lws-secure-streams-policy.h:324
const lws_retry_bo_t * retry_bo
Definition: lws-secure-streams-policy.h:335
uint16_t port
Definition: lws-secure-streams-policy.h:352
uint8_t protocol
Definition: lws-secure-streams-policy.h:355
uint32_t client_buflen
Definition: lws-secure-streams-policy.h:344
const lws_ss_x509_t * key
Definition: lws-secure-streams-policy.h:330
uint32_t proxy_buflen
Definition: lws-secure-streams-policy.h:340
Definition: lws-secure-streams-policy.h:144
Definition: lws-secure-streams-policy.h:73
uint8_t keep
Definition: lws-secure-streams-policy.h:78
size_t ca_der_len
Definition: lws-secure-streams-policy.h:77
const uint8_t * ca_der
Definition: lws-secure-streams-policy.h:76
const char * vhost_name
Definition: lws-secure-streams-policy.h:75