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 25 /* current SS Serialization protocol version */ 26 #define LWS_SSS_CLIENT_PROTOCOL_VERSION 1 27 28 /* 29 * Secure Stream state 30 */ 31 32 typedef enum { 33 SSSEQ_IDLE, 34 SSSEQ_TRY_CONNECT, 35 SSSEQ_TRY_CONNECT_NAUTH, 36 SSSEQ_TRY_CONNECT_SAUTH, 37 SSSEQ_RECONNECT_WAIT, 38 SSSEQ_DO_RETRY, 39 SSSEQ_CONNECTED, 40 } lws_ss_seq_state_t; 41 42 struct conn; 43 44 /** 45 * lws_ss_handle_t: publicly-opaque secure stream object implementation 46 */ 47 48 typedef struct lws_ss_handle { 49 lws_ss_info_t info; /**< copy of stream creation info */ 50 51 lws_lifecycle_t lc; 52 53 #if defined(LWS_WITH_SYS_METRICS) 54 lws_metrics_caliper_compose(cal_txn) 55 #endif 56 57 struct lws_dll2 list; /**< pt lists active ss */ 58 struct lws_dll2 to_list; /**< pt lists ss with pending to-s */ 59 #if defined(LWS_WITH_SERVER) 60 struct lws_dll2 cli_list; /**< same server clients list */ 61 #endif 62 #if defined(LWS_WITH_SYS_FAULT_INJECTION) 63 lws_fi_ctx_t fic; /**< Fault Injection context */ 64 #endif 65 66 struct lws_dll2_owner src_list; /**< sink's list of bound sources */ 67 68 struct lws_context *context; /**< lws context we are created on */ 69 const lws_ss_policy_t *policy; /**< system policy for stream */ 70 71 struct lws_sequencer *seq; /**< owning sequencer if any */ 72 struct lws *wsi; /**< the stream wsi if any */ 73 74 struct conn *conn_if_sspc_onw; 75 76 #if defined(LWS_WITH_SSPLUGINS) 77 void *nauthi; /**< the nauth plugin instance data */ 78 void *sauthi; /**< the sauth plugin instance data */ 79 #endif 80 81 lws_ss_metadata_t *metadata; 82 #if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR) 83 lws_ss_metadata_t *instant_metadata; /**< for set instant metadata */ 84 struct lwsac *imd_ac; /**< for get custom header */ 85 #endif 86 const lws_ss_policy_t *rideshare; 87 struct lws_ss_handle *h_in_svc; 88 89 #if defined(LWS_WITH_CONMON) 90 char *conmon_json; 91 #endif 92 93 //struct lws_ss_handle *h_sink; /**< sink we are bound to, or NULL */ 94 //void *sink_obj;/**< sink's private object representing us */ 95 96 lws_sorted_usec_list_t sul_timeout; 97 lws_sorted_usec_list_t sul; 98 lws_ss_tx_ordinal_t txord; 99 100 /* protocol-specific connection helpers */ 101 102 union { 103 104 /* ...for http-related protocols... */ 105 106 struct { 107 108 /* common to all http-related protocols */ 109 110 /* incoming multipart parsing */ 111 112 char boundary[24]; /* --boundary from headers */ 113 uint8_t boundary_len; /* length of --boundary */ 114 uint8_t boundary_seq; /* current match amount */ 115 uint8_t boundary_dashes; /* check for -- after */ 116 uint8_t boundary_post; /* swallow post CRLF */ 117 118 uint8_t som:1; /* SOM has been sent */ 119 uint8_t eom:1; /* EOM has been sent */ 120 uint8_t any:1; /* any content has been sent */ 121 122 123 uint8_t good_respcode:1; /* 200 type response code */ 124 125 union { 126 struct { /* LWSSSP_H1 */ 127 #if defined(WIN32) 128 uint8_t dummy; 129 #endif 130 } h1; 131 struct { /* LWSSSP_H2 */ 132 #if defined(WIN32) 133 uint8_t dummy; 134 #endif 135 } h2; 136 struct { /* LWSSSP_WS */ 137 #if defined(WIN32) 138 uint8_t dummy; 139 #endif 140 } ws; 141 } u; 142 } http; 143 144 /* details for non-http related protocols... */ 145 #if defined(LWS_ROLE_MQTT) 146 struct { 147 lws_mqtt_topic_elem_t topic_qos; 148 lws_mqtt_topic_elem_t sub_top; 149 lws_mqtt_subscribe_param_t sub_info; 150 /* allocation that must be destroyed with conn */ 151 void *heap_baggage; 152 const char *subscribe_to; 153 size_t subscribe_to_len; 154 } mqtt; 155 #endif 156 #if defined(LWS_WITH_SYS_SMD) 157 struct { 158 struct lws_smd_peer *smd_peer; 159 lws_sorted_usec_list_t sul_write; 160 } smd; 161 #endif 162 } u; 163 164 unsigned long writeable_len; 165 166 lws_ss_constate_t connstate;/**< public connection state */ 167 lws_ss_seq_state_t seqstate; /**< private connection state */ 168 lws_ss_state_return_t pending_ret; /**< holds desired disposition 169 * for ss during CCE */ 170 171 #if defined(LWS_WITH_SERVER) 172 int txn_resp; 173 #endif 174 175 uint16_t retry; /**< retry / backoff tracking */ 176 #if defined(LWS_WITH_CONMON) 177 uint16_t conmon_len; 178 #endif 179 int16_t temp16; 180 181 uint8_t tsi; /**< service thread idx, usually 0 */ 182 uint8_t subseq; /**< emulate SOM tracking */ 183 uint8_t txn_ok; /**< 1 = transaction was OK */ 184 uint8_t prev_ss_state; 185 186 uint8_t txn_resp_set:1; /**< user code set one */ 187 uint8_t txn_resp_pending:1; /**< we have yet to send */ 188 uint8_t hanging_som:1; 189 uint8_t inside_msg:1; 190 uint8_t being_serialized:1; /* we are not the consumer */ 191 uint8_t destroying:1; 192 uint8_t ss_dangling_connected:1; 193 uint8_t proxy_onward:1; /* opaque is conn */ 194 uint8_t inside_connect:1; /* set if we are currently 195 * creating the onward 196 * connect */ 197 } lws_ss_handle_t; 198 199 /* connection helper that doesn't need to hang around after connection starts */ 200 201 union lws_ss_contemp { 202 #if defined(LWS_ROLE_MQTT) 203 lws_mqtt_client_connect_param_t ccp; 204 #else 205 #if defined(WIN32) 206 uint8_t dummy; 207 #endif 208 #endif 209 }; 210 211 /* 212 * When allocating the opaque handle, we overallocate for: 213 * 214 * 1) policy->nauth_plugin->alloc (.nauthi) if any 215 * 2) policy->sauth_plugin->alloc (.sauthi) if any 216 * 3) copy of creation info stream type pointed to by info.streamtype... this 217 * may be arbitrarily long and since it may be coming from socket ipc and be 218 * temporary at creation time, we need a place for the copy to stay in scope 219 * 4) copy of info->streamtype contents 220 */ 221 222 223 /* the user object allocation is immediately after the ss object allocation */ 224 #define ss_to_userobj(ss) ((void *)&(ss)[1]) 225 226 /* 227 * serialization parser state 228 */ 229 230 enum { 231 KIND_C_TO_P, 232 KIND_SS_TO_P, 233 }; 234 235 struct lws_ss_serialization_parser { 236 char streamtype[32]; 237 char rideshare[32]; 238 char metadata_name[32]; 239 240 uint64_t ust_pwait; 241 242 lws_ss_metadata_t *ssmd; 243 uint8_t *rxmetaval; 244 245 int ps; 246 int ctr; 247 248 uint32_t usd_phandling; 249 uint32_t flags; 250 uint32_t client_pid; 251 int32_t temp32; 252 253 int32_t txcr_out; 254 int32_t txcr_in; 255 uint16_t rem; 256 257 uint8_t type; 258 uint8_t frag1; 259 uint8_t slen; 260 uint8_t rsl_pos; 261 uint8_t rsl_idx; 262 uint8_t protocol_version; 263 }; 264 265 /* 266 * Unlike locally-fulfilled SS, SSS doesn't have to hold metadata on client side 267 * but pass it through to the proxy. The client side doesn't know the real 268 * metadata names that are available in the policy (since it's hardcoded in code 269 * no point passing them back to the client from the policy). Because of that, 270 * it doesn't know how many to allocate when we create the sspc_handle either. 271 * 272 * So we use a linked-list of changed-but-not-yet-proxied metadata allocated 273 * on the heap and items removed as they are proxied out. Anything on the list 274 * is sent to the proxy before any requested tx is handled. 275 * 276 * This is also used to queue tx credit changes 277 */ 278 279 typedef struct lws_sspc_metadata { 280 lws_dll2_t list; 281 char name[32]; /* empty string, then actually TCXR */ 282 size_t len; 283 int tx_cr_adjust; 284 285 /* the value of length .len is overallocated after this */ 286 } lws_sspc_metadata_t; 287 288 /* state of the upstream proxy onward connection */ 289 290 enum { 291 LWSSSPC_ONW_NONE, 292 LWSSSPC_ONW_REQ, 293 LWSSSPC_ONW_ONGOING, 294 LWSSSPC_ONW_CONN, 295 }; 296 297 typedef struct lws_sspc_handle { 298 char rideshare_list[128]; 299 300 lws_lifecycle_t lc; 301 302 lws_ss_info_t ssi; 303 lws_sorted_usec_list_t sul_retry; 304 305 struct lws_ss_serialization_parser parser; 306 307 #if defined(LWS_WITH_SYS_FAULT_INJECTION) 308 lws_fi_ctx_t fic; /**< Fault Injection context */ 309 #endif 310 311 lws_dll2_owner_t metadata_owner; 312 lws_dll2_owner_t metadata_owner_rx; 313 314 struct lws_dll2 client_list; 315 struct lws_tx_credit txc; 316 317 #if defined(LWS_WITH_SYS_METRICS) 318 lws_metrics_caliper_compose(cal_txn) 319 #endif 320 321 struct lws *cwsi; 322 323 struct lws_dsh *dsh; 324 struct lws_context *context; 325 326 struct lws_sspc_handle *h_in_svc; 327 /* 328 * Used to detect illegal lws_sspc_destroy() calls while still 329 * being serviced 330 */ 331 332 lws_usec_t us_earliest_write_req; 333 lws_usec_t us_start_upstream; 334 335 unsigned long writeable_len; 336 337 lws_ss_conn_states_t state; 338 339 uint32_t timeout_ms; 340 uint32_t ord; 341 342 int16_t temp16; 343 344 uint8_t rideshare_ofs[4]; 345 uint8_t rsidx; 346 347 uint8_t prev_ss_state; 348 349 uint8_t conn_req_state:2; 350 uint8_t destroying:1; 351 uint8_t non_wsi:1; 352 uint8_t ignore_txc:1; 353 uint8_t pending_timeout_update:1; 354 uint8_t pending_writeable_len:1; 355 uint8_t creating_cb_done:1; 356 uint8_t ss_dangling_connected:1; 357 } lws_sspc_handle_t; 358 359 typedef struct backoffs { 360 struct backoffs *next; 361 const char *name; 362 lws_retry_bo_t r; 363 } backoff_t; 364 365 union u { 366 backoff_t *b; 367 lws_ss_x509_t *x; 368 lws_ss_trust_store_t *t; 369 lws_ss_policy_t *p; 370 lws_ss_auth_t *a; 371 lws_metric_policy_t *m; 372 }; 373 374 enum { 375 LTY_BACKOFF, 376 LTY_X509, 377 LTY_TRUSTSTORE, 378 LTY_POLICY, 379 LTY_AUTH, 380 LTY_METRICS, 381 382 _LTY_COUNT /* always last */ 383 }; 384 385 386 struct policy_cb_args { 387 struct lejp_ctx jctx; 388 struct lws_context *context; 389 struct lwsac *ac; 390 391 const char *socks5_proxy; 392 393 struct lws_b64state b64; 394 395 lws_ss_http_respmap_t respmap[16]; 396 397 union u heads[_LTY_COUNT]; 398 union u curr[_LTY_COUNT]; 399 400 uint8_t *p; 401 402 int count; 403 char pending_respmap; 404 405 uint8_t parse_data:1; 406 }; 407 408 #if defined(LWS_WITH_SYS_SMD) 409 extern const lws_ss_policy_t pol_smd; 410 #endif 411 412 413 /* 414 * returns one of 415 * 416 * LWSSSSRET_OK 417 * LWSSSSRET_DISCONNECT_ME 418 * LWSSSSRET_DESTROY_ME 419 */ 420 int 421 lws_ss_deserialize_parse(struct lws_ss_serialization_parser *par, 422 struct lws_context *context, 423 struct lws_dsh *dsh, const uint8_t *cp, size_t len, 424 lws_ss_conn_states_t *state, void *parconn, 425 lws_ss_handle_t **pss, lws_ss_info_t *ssi, char client); 426 int 427 lws_ss_serialize_rx_payload(struct lws_dsh *dsh, const uint8_t *buf, 428 size_t len, int flags, const char *rsp); 429 int 430 lws_ss_deserialize_tx_payload(struct lws_dsh *dsh, struct lws *wsi, 431 lws_ss_tx_ordinal_t ord, uint8_t *buf, 432 size_t *len, int *flags); 433 int 434 lws_ss_serialize_state(struct lws *wsi, struct lws_dsh *dsh, lws_ss_constate_t state, 435 lws_ss_tx_ordinal_t ack); 436 437 const lws_ss_policy_t * 438 lws_ss_policy_lookup(const struct lws_context *context, const char *streamtype); 439 440 /* can be used as a cb from lws_dll2_foreach_safe() to destroy ss */ 441 int 442 lws_ss_destroy_dll(struct lws_dll2 *d, void *user); 443 444 int 445 lws_sspc_destroy_dll(struct lws_dll2 *d, void *user); 446 447 void 448 lws_sspc_rxmetadata_destroy(lws_sspc_handle_t *h); 449 450 int 451 lws_ss_policy_set(struct lws_context *context, const char *name); 452 453 int 454 lws_ss_sys_fetch_policy(struct lws_context *context); 455 456 lws_ss_state_return_t 457 lws_ss_event_helper(lws_ss_handle_t *h, lws_ss_constate_t cs); 458 459 lws_ss_state_return_t 460 _lws_ss_backoff(lws_ss_handle_t *h, lws_usec_t us_override); 461 462 lws_ss_state_return_t 463 lws_ss_backoff(lws_ss_handle_t *h); 464 465 int 466 _lws_ss_handle_state_ret_CAN_DESTROY_HANDLE(lws_ss_state_return_t r, struct lws *wsi, 467 lws_ss_handle_t **ph); 468 469 int 470 lws_ss_set_timeout_us(lws_ss_handle_t *h, lws_usec_t us); 471 472 void 473 ss_proxy_onward_txcr(void *userobj, int bump); 474 475 int 476 lws_ss_serialize_txcr(struct lws_dsh *dsh, int txcr); 477 478 int 479 lws_ss_sys_auth_api_amazon_com(struct lws_context *context); 480 481 lws_ss_metadata_t * 482 lws_ss_get_handle_metadata(struct lws_ss_handle *h, const char *name); 483 lws_ss_metadata_t * 484 lws_ss_policy_metadata_index(const lws_ss_policy_t *p, size_t index); 485 486 #if defined(LWS_WITH_SS_DIRECT_PROTOCOL_STR) 487 lws_ss_metadata_t * 488 lws_ss_get_handle_instant_metadata(struct lws_ss_handle *h, const char *name); 489 #endif 490 491 lws_ss_metadata_t * 492 lws_ss_policy_metadata(const lws_ss_policy_t *p, const char *name); 493 494 int 495 lws_ss_exp_cb_metadata(void *priv, const char *name, char *out, size_t *pos, 496 size_t olen, size_t *exp_ofs); 497 498 int 499 _lws_ss_set_metadata(lws_ss_metadata_t *omd, const char *name, 500 const void *value, size_t len); 501 502 int 503 _lws_ss_alloc_set_metadata(lws_ss_metadata_t *omd, const char *name, 504 const void *value, size_t len); 505 506 lws_ss_state_return_t 507 _lws_ss_client_connect(lws_ss_handle_t *h, int is_retry, void *conn_if_sspc_onw); 508 509 lws_ss_state_return_t 510 _lws_ss_request_tx(lws_ss_handle_t *h); 511 512 int 513 __lws_ss_proxy_bind_ss_to_conn_wsi(void *parconn, size_t dsh_size); 514 515 struct lws_vhost * 516 lws_ss_policy_ref_trust_store(struct lws_context *context, 517 const lws_ss_policy_t *pol, char doref); 518 519 lws_ss_state_return_t 520 lws_sspc_event_helper(lws_sspc_handle_t *h, lws_ss_constate_t cs, 521 lws_ss_tx_ordinal_t flags); 522 523 int 524 lws_ss_check_next_state(lws_lifecycle_t *lc, uint8_t *prevstate, 525 lws_ss_constate_t cs); 526 527 int 528 lws_ss_check_next_state_ss(lws_ss_handle_t *ss, uint8_t *prevstate, 529 lws_ss_constate_t cs); 530 531 int 532 lws_ss_check_next_state_sspc(lws_sspc_handle_t *ss, uint8_t *prevstate, 533 lws_ss_constate_t cs); 534 535 void 536 lws_proxy_clean_conn_ss(struct lws *wsi); 537 538 int 539 lws_ss_cancel_notify_dll(struct lws_dll2 *d, void *user); 540 541 int 542 lws_sspc_cancel_notify_dll(struct lws_dll2 *d, void *user); 543 544 #if defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY) || defined(LWS_WITH_SECURE_STREAMS_CPP) 545 int 546 lws_ss_policy_unref_trust_store(struct lws_context *context, 547 const lws_ss_policy_t *pol); 548 #endif 549 550 int 551 lws_ss_sys_cpd(struct lws_context *cx); 552 553 #if defined(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4) 554 int lws_ss_apply_sigv4(struct lws *wsi, struct lws_ss_handle *h, 555 unsigned char **p, unsigned char *end); 556 #endif 557 558 #if defined(_DEBUG) 559 void 560 lws_ss_assert_extant(struct lws_context *cx, int tsi, struct lws_ss_handle *h); 561 #else 562 #define lws_ss_assert_extant(_a, _b, _c) 563 #endif 564 565 typedef int (* const secstream_protocol_connect_munge_t)(lws_ss_handle_t *h, 566 char *buf, size_t len, struct lws_client_connect_info *i, 567 union lws_ss_contemp *ct); 568 569 typedef int (* const secstream_protocol_add_txcr_t)(lws_ss_handle_t *h, int add); 570 571 typedef int (* const secstream_protocol_get_txcr_t)(lws_ss_handle_t *h); 572 573 struct ss_pcols { 574 const char *name; 575 const char *alpn; 576 const struct lws_protocols *protocol; 577 secstream_protocol_connect_munge_t munge; 578 secstream_protocol_add_txcr_t tx_cr_add; 579 secstream_protocol_get_txcr_t tx_cr_est; 580 }; 581 582 /* 583 * Because both sides of the connection share the conn, we allocate it 584 * during accepted adoption, and both sides point to it. 585 * 586 * When .ss or .wsi close, they must NULL their entry here so no dangling 587 * refereneces. 588 * 589 * The last one of the accepted side and the onward side to close frees it. 590 */ 591 592 lws_ss_state_return_t 593 lws_conmon_ss_json(lws_ss_handle_t *h); 594 595 void 596 ss_proxy_onward_link_req_writeable(lws_ss_handle_t *h_onward); 597 598 struct conn { 599 struct lws_ss_serialization_parser parser; 600 601 lws_dsh_t *dsh; /* unified buffer for both sides */ 602 struct lws *wsi; /* the proxy's client side */ 603 lws_ss_handle_t *ss; /* the onward, ss side */ 604 605 lws_ss_conn_states_t state; 606 607 char onward_in_flow_control; 608 }; 609 610 extern const struct ss_pcols ss_pcol_h1; 611 extern const struct ss_pcols ss_pcol_h2; 612 extern const struct ss_pcols ss_pcol_ws; 613 extern const struct ss_pcols ss_pcol_mqtt; 614 extern const struct ss_pcols ss_pcol_raw; 615 616 extern const struct lws_protocols protocol_secstream_h1; 617 extern const struct lws_protocols protocol_secstream_h2; 618 extern const struct lws_protocols protocol_secstream_ws; 619 extern const struct lws_protocols protocol_secstream_mqtt; 620 extern const struct lws_protocols protocol_secstream_raw; 621 622