quic_channel.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/quic_channel.h"
  10. #include "internal/quic_error.h"
  11. #include "internal/quic_rx_depack.h"
  12. #include "../ssl_local.h"
  13. #include "quic_channel_local.h"
  14. #include <openssl/rand.h>
  15. #define INIT_DCID_LEN 8
  16. #define INIT_CRYPTO_BUF_LEN 8192
  17. #define INIT_APP_BUF_LEN 8192
  18. static int ch_rx(QUIC_CHANNEL *ch);
  19. static int ch_tx(QUIC_CHANNEL *ch);
  20. static void ch_tick(QUIC_TICK_RESULT *res, void *arg);
  21. static void ch_rx_handle_packet(QUIC_CHANNEL *ch);
  22. static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch);
  23. static int ch_retry(QUIC_CHANNEL *ch,
  24. const unsigned char *retry_token,
  25. size_t retry_token_len,
  26. const QUIC_CONN_ID *retry_scid);
  27. static void ch_cleanup(QUIC_CHANNEL *ch);
  28. static int ch_generate_transport_params(QUIC_CHANNEL *ch);
  29. static int ch_on_transport_params(const unsigned char *params,
  30. size_t params_len,
  31. void *arg);
  32. static int ch_on_handshake_alert(void *arg, unsigned char alert_code);
  33. static int ch_on_handshake_complete(void *arg);
  34. static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction,
  35. uint32_t suite_id, EVP_MD *md,
  36. const unsigned char *secret,
  37. size_t secret_len,
  38. void *arg);
  39. static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len,
  40. size_t *bytes_read, void *arg);
  41. static int crypto_ensure_empty(QUIC_RSTREAM *rstream);
  42. static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len,
  43. size_t *consumed, void *arg);
  44. static OSSL_TIME get_time(void *arg);
  45. static uint64_t get_stream_limit(int uni, void *arg);
  46. static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg);
  47. static int ch_retry(QUIC_CHANNEL *ch,
  48. const unsigned char *retry_token,
  49. size_t retry_token_len,
  50. const QUIC_CONN_ID *retry_scid);
  51. static void ch_update_idle(QUIC_CHANNEL *ch);
  52. static int ch_discard_el(QUIC_CHANNEL *ch,
  53. uint32_t enc_level);
  54. static void ch_on_idle_timeout(QUIC_CHANNEL *ch);
  55. static void ch_update_idle(QUIC_CHANNEL *ch);
  56. static void ch_on_terminating_timeout(QUIC_CHANNEL *ch);
  57. static void ch_start_terminating(QUIC_CHANNEL *ch,
  58. const QUIC_TERMINATE_CAUSE *tcause);
  59. static int gen_rand_conn_id(OSSL_LIB_CTX *libctx, size_t len, QUIC_CONN_ID *cid)
  60. {
  61. if (len > QUIC_MAX_CONN_ID_LEN)
  62. return 0;
  63. cid->id_len = (unsigned char)len;
  64. if (RAND_bytes_ex(libctx, cid->id, len, len * 8) != 1) {
  65. cid->id_len = 0;
  66. return 0;
  67. }
  68. return 1;
  69. }
  70. /*
  71. * QUIC Channel Initialization and Teardown
  72. * ========================================
  73. */
  74. static int ch_init(QUIC_CHANNEL *ch)
  75. {
  76. OSSL_QUIC_TX_PACKETISER_ARGS txp_args = {0};
  77. OSSL_QTX_ARGS qtx_args = {0};
  78. OSSL_QRX_ARGS qrx_args = {0};
  79. QUIC_DHS_ARGS dhs_args = {0};
  80. uint32_t pn_space;
  81. /* TODO(QUIC): This is only applicable to clients. */
  82. if (!gen_rand_conn_id(ch->libctx, INIT_DCID_LEN, &ch->init_dcid))
  83. goto err;
  84. /* We plug in a network write BIO to the QTX later when we get one. */
  85. qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN;
  86. ch->rx_max_udp_payload_size = qtx_args.mdpl;
  87. ch->qtx = ossl_qtx_new(&qtx_args);
  88. if (ch->qtx == NULL)
  89. goto err;
  90. ch->txpim = ossl_quic_txpim_new();
  91. if (ch->txpim == NULL)
  92. goto err;
  93. ch->cfq = ossl_quic_cfq_new();
  94. if (ch->cfq == NULL)
  95. goto err;
  96. if (!ossl_quic_txfc_init(&ch->conn_txfc, NULL))
  97. goto err;
  98. if (!ossl_quic_rxfc_init(&ch->conn_rxfc, NULL,
  99. 2 * 1024 * 1024,
  100. 10 * 1024 * 1024,
  101. get_time, NULL))
  102. goto err;
  103. if (!ossl_statm_init(&ch->statm))
  104. goto err;
  105. ch->have_statm = 1;
  106. ch->cc_method = &ossl_cc_dummy_method;
  107. if ((ch->cc_data = ch->cc_method->new(NULL, NULL, NULL)) == NULL)
  108. goto err;
  109. if ((ch->ackm = ossl_ackm_new(get_time, NULL, &ch->statm,
  110. ch->cc_method, ch->cc_data)) == NULL)
  111. goto err;
  112. if (!ossl_quic_stream_map_init(&ch->qsm, get_stream_limit, ch))
  113. goto err;
  114. ch->have_qsm = 1;
  115. /* We use a zero-length SCID. */
  116. txp_args.cur_dcid = ch->init_dcid;
  117. txp_args.ack_delay_exponent = 3;
  118. txp_args.qtx = ch->qtx;
  119. txp_args.txpim = ch->txpim;
  120. txp_args.cfq = ch->cfq;
  121. txp_args.ackm = ch->ackm;
  122. txp_args.qsm = &ch->qsm;
  123. txp_args.conn_txfc = &ch->conn_txfc;
  124. txp_args.conn_rxfc = &ch->conn_rxfc;
  125. txp_args.cc_method = ch->cc_method;
  126. txp_args.cc_data = ch->cc_data;
  127. txp_args.now = get_time;
  128. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
  129. ch->crypto_send[pn_space] = ossl_quic_sstream_new(INIT_CRYPTO_BUF_LEN);
  130. if (ch->crypto_send[pn_space] == NULL)
  131. goto err;
  132. txp_args.crypto[pn_space] = ch->crypto_send[pn_space];
  133. }
  134. ch->txp = ossl_quic_tx_packetiser_new(&txp_args);
  135. if (ch->txp == NULL)
  136. goto err;
  137. if ((ch->demux = ossl_quic_demux_new(/*BIO=*/NULL, /*Short CID Len=*/0,
  138. 1200, get_time, NULL)) == NULL)
  139. goto err;
  140. qrx_args.demux = ch->demux;
  141. qrx_args.short_conn_id_len = 0; /* We use a zero-length SCID. */
  142. qrx_args.max_deferred = 32;
  143. if ((ch->qrx = ossl_qrx_new(&qrx_args)) == NULL)
  144. goto err;
  145. if (!ossl_qrx_set_early_validation_cb(ch->qrx,
  146. rx_early_validate,
  147. ch))
  148. goto err;
  149. if (!ossl_qrx_add_dst_conn_id(ch->qrx, &txp_args.cur_scid))
  150. goto err;
  151. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
  152. ch->crypto_recv[pn_space] = ossl_quic_rstream_new(NULL, NULL);
  153. if (ch->crypto_recv[pn_space] == NULL)
  154. goto err;
  155. }
  156. if ((ch->stream0 = ossl_quic_stream_map_alloc(&ch->qsm, 0,
  157. QUIC_STREAM_INITIATOR_CLIENT
  158. | QUIC_STREAM_DIR_BIDI)) == NULL)
  159. goto err;
  160. if ((ch->stream0->sstream = ossl_quic_sstream_new(INIT_APP_BUF_LEN)) == NULL)
  161. goto err;
  162. if ((ch->stream0->rstream = ossl_quic_rstream_new(NULL, NULL)) == NULL)
  163. goto err;
  164. if (!ossl_quic_txfc_init(&ch->stream0->txfc, &ch->conn_txfc))
  165. goto err;
  166. if (!ossl_quic_rxfc_init(&ch->stream0->rxfc, &ch->conn_rxfc,
  167. 1 * 1024 * 1024,
  168. 5 * 1024 * 1024,
  169. get_time, NULL))
  170. goto err;
  171. /* Plug in the dummy handshake layer. */
  172. dhs_args.crypto_send_cb = ch_on_crypto_send;
  173. dhs_args.crypto_send_cb_arg = ch;
  174. dhs_args.crypto_recv_cb = ch_on_crypto_recv;
  175. dhs_args.crypto_recv_cb_arg = ch;
  176. dhs_args.yield_secret_cb = ch_on_handshake_yield_secret;
  177. dhs_args.yield_secret_cb_arg = ch;
  178. dhs_args.got_transport_params_cb = ch_on_transport_params;
  179. dhs_args.got_transport_params_cb_arg= ch;
  180. dhs_args.handshake_complete_cb = ch_on_handshake_complete;
  181. dhs_args.handshake_complete_cb_arg = ch;
  182. dhs_args.alert_cb = ch_on_handshake_alert;
  183. dhs_args.alert_cb_arg = ch;
  184. if ((ch->dhs = ossl_quic_dhs_new(&dhs_args)) == NULL)
  185. goto err;
  186. /*
  187. * Determine the QUIC Transport Parameters and serialize the transport
  188. * parameters block. (For servers, we do this later as we must defer
  189. * generation until we have received the client's transport parameters.)
  190. */
  191. if (!ch->is_server && !ch_generate_transport_params(ch))
  192. goto err;
  193. ch->rx_max_ack_delay = QUIC_DEFAULT_MAX_ACK_DELAY;
  194. ch->rx_ack_delay_exp = QUIC_DEFAULT_ACK_DELAY_EXP;
  195. ch->rx_active_conn_id_limit = QUIC_MIN_ACTIVE_CONN_ID_LIMIT;
  196. ch->max_idle_timeout = QUIC_DEFAULT_IDLE_TIMEOUT;
  197. ch->tx_enc_level = QUIC_ENC_LEVEL_INITIAL;
  198. ch_update_idle(ch);
  199. ossl_quic_reactor_init(&ch->rtor, ch_tick, ch,
  200. ch_determine_next_tick_deadline(ch));
  201. return 1;
  202. err:
  203. ch_cleanup(ch);
  204. return 0;
  205. }
  206. static void ch_cleanup(QUIC_CHANNEL *ch)
  207. {
  208. uint32_t pn_space;
  209. if (ch->ackm != NULL)
  210. for (pn_space = QUIC_PN_SPACE_INITIAL;
  211. pn_space < QUIC_PN_SPACE_NUM;
  212. ++pn_space)
  213. ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
  214. ossl_quic_tx_packetiser_free(ch->txp);
  215. ossl_quic_txpim_free(ch->txpim);
  216. ossl_quic_cfq_free(ch->cfq);
  217. ossl_qtx_free(ch->qtx);
  218. if (ch->cc_data != NULL)
  219. ch->cc_method->free(ch->cc_data);
  220. if (ch->have_statm)
  221. ossl_statm_destroy(&ch->statm);
  222. ossl_ackm_free(ch->ackm);
  223. if (ch->stream0 != NULL) {
  224. assert(ch->have_qsm);
  225. ossl_quic_stream_map_release(&ch->qsm, ch->stream0); /* frees sstream */
  226. }
  227. if (ch->have_qsm)
  228. ossl_quic_stream_map_cleanup(&ch->qsm);
  229. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space) {
  230. ossl_quic_sstream_free(ch->crypto_send[pn_space]);
  231. ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
  232. }
  233. ossl_qrx_pkt_release(ch->qrx_pkt);
  234. ch->qrx_pkt = NULL;
  235. ossl_quic_dhs_free(ch->dhs);
  236. ossl_qrx_free(ch->qrx);
  237. ossl_quic_demux_free(ch->demux);
  238. OPENSSL_free(ch->local_transport_params);
  239. BIO_free(ch->net_rbio);
  240. BIO_free(ch->net_wbio);
  241. }
  242. QUIC_CHANNEL *ossl_quic_channel_new(const QUIC_CHANNEL_ARGS *args)
  243. {
  244. QUIC_CHANNEL *ch = NULL;
  245. if ((ch = OPENSSL_zalloc(sizeof(*ch))) == NULL)
  246. return NULL;
  247. ch->libctx = args->libctx;
  248. ch->propq = args->propq;
  249. ch->is_server = args->is_server;
  250. if (!ch_init(ch)) {
  251. OPENSSL_free(ch);
  252. return NULL;
  253. }
  254. return ch;
  255. }
  256. void ossl_quic_channel_free(QUIC_CHANNEL *ch)
  257. {
  258. if (ch == NULL)
  259. return;
  260. ch_cleanup(ch);
  261. OPENSSL_free(ch);
  262. }
  263. int ossl_quic_channel_get_peer_addr(QUIC_CHANNEL *ch, BIO_ADDR *peer_addr)
  264. {
  265. *peer_addr = ch->cur_peer_addr;
  266. return 1;
  267. }
  268. int ossl_quic_channel_set_peer_addr(QUIC_CHANNEL *ch, const BIO_ADDR *peer_addr)
  269. {
  270. ch->cur_peer_addr = *peer_addr;
  271. return 1;
  272. }
  273. QUIC_REACTOR *ossl_quic_channel_get_reactor(QUIC_CHANNEL *ch)
  274. {
  275. return &ch->rtor;
  276. }
  277. QUIC_STREAM_MAP *ossl_quic_channel_get_qsm(QUIC_CHANNEL *ch)
  278. {
  279. return &ch->qsm;
  280. }
  281. OSSL_STATM *ossl_quic_channel_get_statm(QUIC_CHANNEL *ch)
  282. {
  283. return &ch->statm;
  284. }
  285. QUIC_STREAM *ossl_quic_channel_get_stream_by_id(QUIC_CHANNEL *ch,
  286. uint64_t stream_id)
  287. {
  288. return ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
  289. }
  290. int ossl_quic_channel_is_active(const QUIC_CHANNEL *ch)
  291. {
  292. return ch != NULL && ch->state == QUIC_CHANNEL_STATE_ACTIVE;
  293. }
  294. int ossl_quic_channel_is_terminating(const QUIC_CHANNEL *ch)
  295. {
  296. return ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING
  297. || ch->state == QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
  298. }
  299. int ossl_quic_channel_is_terminated(const QUIC_CHANNEL *ch)
  300. {
  301. return ch->state == QUIC_CHANNEL_STATE_TERMINATED;
  302. }
  303. int ossl_quic_channel_is_term_any(const QUIC_CHANNEL *ch)
  304. {
  305. return ossl_quic_channel_is_terminating(ch)
  306. || ossl_quic_channel_is_terminated(ch);
  307. }
  308. int ossl_quic_channel_is_handshake_complete(const QUIC_CHANNEL *ch)
  309. {
  310. return ch->handshake_complete;
  311. }
  312. /*
  313. * QUIC Channel: Callbacks from Miscellaneous Subsidiary Components
  314. * ================================================================
  315. */
  316. /* Used by various components. */
  317. static OSSL_TIME get_time(void *arg)
  318. {
  319. return ossl_time_now();
  320. }
  321. /* Used by QSM. */
  322. static uint64_t get_stream_limit(int uni, void *arg)
  323. {
  324. QUIC_CHANNEL *ch = arg;
  325. return uni ? ch->max_local_streams_uni : ch->max_local_streams_bidi;
  326. }
  327. /*
  328. * Called by QRX to determine if a packet is potentially invalid before trying
  329. * to decrypt it.
  330. */
  331. static int rx_early_validate(QUIC_PN pn, int pn_space, void *arg)
  332. {
  333. QUIC_CHANNEL *ch = arg;
  334. /* Potential duplicates should not be processed. */
  335. if (!ossl_ackm_is_rx_pn_processable(ch->ackm, pn, pn_space))
  336. return 0;
  337. return 1;
  338. }
  339. /*
  340. * QUIC Channel: Handshake Layer Event Handling
  341. * ============================================
  342. */
  343. static int ch_on_crypto_send(const unsigned char *buf, size_t buf_len,
  344. size_t *consumed, void *arg)
  345. {
  346. int ret;
  347. QUIC_CHANNEL *ch = arg;
  348. uint32_t enc_level = ch->tx_enc_level;
  349. uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  350. QUIC_SSTREAM *sstream = ch->crypto_send[pn_space];
  351. if (!ossl_assert(sstream != NULL))
  352. return 0;
  353. ret = ossl_quic_sstream_append(sstream, buf, buf_len, consumed);
  354. return ret;
  355. }
  356. static int crypto_ensure_empty(QUIC_RSTREAM *rstream)
  357. {
  358. size_t avail = 0;
  359. int is_fin = 0;
  360. if (rstream == NULL)
  361. return 1;
  362. if (!ossl_quic_rstream_available(rstream, &avail, &is_fin))
  363. return 0;
  364. return avail == 0;
  365. }
  366. static int ch_on_crypto_recv(unsigned char *buf, size_t buf_len,
  367. size_t *bytes_read, void *arg)
  368. {
  369. QUIC_CHANNEL *ch = arg;
  370. QUIC_RSTREAM *rstream;
  371. int is_fin = 0; /* crypto stream is never finished, so we don't use this */
  372. uint32_t i;
  373. /*
  374. * After we move to a later EL we must not allow our peer to send any new
  375. * bytes in the crypto stream on a previous EL. Retransmissions of old bytes
  376. * are allowed.
  377. *
  378. * In practice we will only move to a new EL when we have consumed all bytes
  379. * which should be sent on the crypto stream at a previous EL. For example,
  380. * the Handshake EL should not be provisioned until we have completely
  381. * consumed a TLS 1.3 ServerHello. Thus when we provision an EL the output
  382. * of ossl_quic_rstream_available() should be 0 for all lower ELs. Thus if a
  383. * given EL is available we simply ensure we have not received any further
  384. * bytes at a lower EL.
  385. */
  386. for (i = QUIC_ENC_LEVEL_INITIAL; i < ch->tx_enc_level; ++i)
  387. if (i != QUIC_ENC_LEVEL_0RTT &&
  388. !crypto_ensure_empty(ch->crypto_recv[ossl_quic_enc_level_to_pn_space(i)])) {
  389. /* Protocol violation (RFC 9001 s. 4.1.3) */
  390. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
  391. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  392. "crypto stream data in wrong EL");
  393. return 0;
  394. }
  395. rstream = ch->crypto_recv[ossl_quic_enc_level_to_pn_space(ch->tx_enc_level)];
  396. if (rstream == NULL)
  397. return 0;
  398. return ossl_quic_rstream_read(rstream, buf, buf_len, bytes_read,
  399. &is_fin);
  400. }
  401. static int ch_on_handshake_yield_secret(uint32_t enc_level, int direction,
  402. uint32_t suite_id, EVP_MD *md,
  403. const unsigned char *secret,
  404. size_t secret_len,
  405. void *arg)
  406. {
  407. QUIC_CHANNEL *ch = arg;
  408. uint32_t i;
  409. if (enc_level < QUIC_ENC_LEVEL_HANDSHAKE || enc_level >= QUIC_ENC_LEVEL_NUM)
  410. /* Invalid EL. */
  411. return 0;
  412. if (enc_level <= ch->tx_enc_level)
  413. /*
  414. * Does not make sense for us to try and provision an EL we have already
  415. * attained.
  416. */
  417. return 0;
  418. /*
  419. * Ensure all crypto streams for previous ELs are now empty of available
  420. * data.
  421. */
  422. for (i = QUIC_ENC_LEVEL_INITIAL; i < enc_level; ++i)
  423. if (!crypto_ensure_empty(ch->crypto_recv[i])) {
  424. /* Protocol violation (RFC 9001 s. 4.1.3) */
  425. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
  426. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  427. "crypto stream data in wrong EL");
  428. return 0;
  429. }
  430. if (direction) {
  431. /* TX */
  432. if (!ossl_qtx_provide_secret(ch->qtx, enc_level,
  433. suite_id, md,
  434. secret, secret_len))
  435. return 0;
  436. ch->tx_enc_level = enc_level;
  437. } else {
  438. /* RX */
  439. if (!ossl_qrx_provide_secret(ch->qrx, enc_level,
  440. suite_id, md,
  441. secret, secret_len))
  442. return 0;
  443. }
  444. return 1;
  445. }
  446. static int ch_on_handshake_complete(void *arg)
  447. {
  448. QUIC_CHANNEL *ch = arg;
  449. if (ch->handshake_complete)
  450. return 0; /* this should not happen twice */
  451. if (!ossl_assert(ch->tx_enc_level == QUIC_ENC_LEVEL_1RTT))
  452. return 0;
  453. if (!ch->got_remote_transport_params)
  454. /*
  455. * Was not a valid QUIC handshake if we did not get valid transport
  456. * params.
  457. */
  458. return 0;
  459. /* Don't need transport parameters anymore. */
  460. OPENSSL_free(ch->local_transport_params);
  461. ch->local_transport_params = NULL;
  462. /* Tell TXP the handshake is complete. */
  463. ossl_quic_tx_packetiser_notify_handshake_complete(ch->txp);
  464. ch->handshake_complete = 1;
  465. return 1;
  466. }
  467. static int ch_on_handshake_alert(void *arg, unsigned char alert_code)
  468. {
  469. QUIC_CHANNEL *ch = arg;
  470. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_ERR_BEGIN + alert_code,
  471. 0, "handshake alert");
  472. return 1;
  473. }
  474. /*
  475. * QUIC Channel: Transport Parameter Handling
  476. * ==========================================
  477. */
  478. /*
  479. * Called by handshake layer when we receive QUIC Transport Parameters from the
  480. * peer. Note that these are not authenticated until the handshake is marked
  481. * as complete.
  482. */
  483. static int ch_on_transport_params(const unsigned char *params,
  484. size_t params_len,
  485. void *arg)
  486. {
  487. QUIC_CHANNEL *ch = arg;
  488. PACKET pkt;
  489. uint64_t id, v;
  490. size_t len;
  491. const unsigned char *body;
  492. int got_orig_dcid = 0;
  493. int got_initial_scid = 0;
  494. int got_retry_scid = 0;
  495. int got_initial_max_data = 0;
  496. int got_initial_max_stream_data_bidi_local = 0;
  497. int got_initial_max_stream_data_bidi_remote = 0;
  498. int got_initial_max_stream_data_uni = 0;
  499. int got_initial_max_streams_bidi = 0;
  500. int got_initial_max_streams_uni = 0;
  501. int got_ack_delay_exp = 0;
  502. int got_max_ack_delay = 0;
  503. int got_max_udp_payload_size = 0;
  504. int got_max_idle_timeout = 0;
  505. int got_active_conn_id_limit = 0;
  506. QUIC_CONN_ID cid;
  507. if (ch->got_remote_transport_params)
  508. goto malformed;
  509. if (!PACKET_buf_init(&pkt, params, params_len))
  510. return 0;
  511. while (PACKET_remaining(&pkt) > 0) {
  512. if (!ossl_quic_wire_peek_transport_param(&pkt, &id))
  513. goto malformed;
  514. switch (id) {
  515. case QUIC_TPARAM_ORIG_DCID:
  516. if (got_orig_dcid)
  517. /* must not appear more than once */
  518. goto malformed;
  519. if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
  520. goto malformed;
  521. /* Must match our initial DCID. */
  522. if (!ossl_quic_conn_id_eq(&ch->init_dcid, &cid))
  523. goto malformed;
  524. got_orig_dcid = 1;
  525. break;
  526. case QUIC_TPARAM_RETRY_SCID:
  527. if (got_retry_scid || !ch->doing_retry)
  528. /* must not appear more than once or if retry not done */
  529. goto malformed;
  530. if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
  531. goto malformed;
  532. /* Must match Retry packet SCID. */
  533. if (!ossl_quic_conn_id_eq(&ch->retry_scid, &cid))
  534. goto malformed;
  535. got_retry_scid = 1;
  536. break;
  537. case QUIC_TPARAM_INITIAL_SCID:
  538. if (got_initial_scid)
  539. /* must not appear more than once */
  540. goto malformed;
  541. if (!ossl_quic_wire_decode_transport_param_cid(&pkt, NULL, &cid))
  542. goto malformed;
  543. /* Must match SCID of first Initial packet from server. */
  544. if (!ossl_quic_conn_id_eq(&ch->init_scid, &cid))
  545. goto malformed;
  546. got_initial_scid = 1;
  547. break;
  548. case QUIC_TPARAM_INITIAL_MAX_DATA:
  549. if (got_initial_max_data)
  550. /* must not appear more than once */
  551. goto malformed;
  552. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
  553. goto malformed;
  554. ossl_quic_txfc_bump_cwm(&ch->conn_txfc, v);
  555. got_initial_max_data = 1;
  556. break;
  557. case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL:
  558. if (got_initial_max_stream_data_bidi_local)
  559. /* must not appear more than once */
  560. goto malformed;
  561. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
  562. goto malformed;
  563. /*
  564. * This is correct; the BIDI_LOCAL TP governs streams created by
  565. * the endpoint which sends the TP, i.e., our peer.
  566. */
  567. ch->init_max_stream_data_bidi_remote = v;
  568. got_initial_max_stream_data_bidi_local = 1;
  569. break;
  570. case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE:
  571. if (got_initial_max_stream_data_bidi_remote)
  572. /* must not appear more than once */
  573. goto malformed;
  574. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
  575. goto malformed;
  576. /*
  577. * This is correct; the BIDI_REMOTE TP governs streams created
  578. * by the endpoint which receives the TP, i.e., us.
  579. */
  580. ch->init_max_stream_data_bidi_local = v;
  581. /* Apply to stream 0. */
  582. ossl_quic_txfc_bump_cwm(&ch->stream0->txfc, v);
  583. got_initial_max_stream_data_bidi_remote = 1;
  584. break;
  585. case QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI:
  586. if (got_initial_max_stream_data_uni)
  587. /* must not appear more than once */
  588. goto malformed;
  589. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
  590. goto malformed;
  591. ch->init_max_stream_data_uni_remote = v;
  592. got_initial_max_stream_data_uni = 1;
  593. break;
  594. case QUIC_TPARAM_ACK_DELAY_EXP:
  595. if (got_ack_delay_exp)
  596. /* must not appear more than once */
  597. goto malformed;
  598. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  599. || v > QUIC_MAX_ACK_DELAY_EXP)
  600. goto malformed;
  601. ch->rx_ack_delay_exp = (unsigned char)v;
  602. got_ack_delay_exp = 1;
  603. break;
  604. case QUIC_TPARAM_MAX_ACK_DELAY:
  605. if (got_max_ack_delay)
  606. /* must not appear more than once */
  607. return 0;
  608. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  609. || v >= (((uint64_t)1) << 14))
  610. goto malformed;
  611. ch->rx_max_ack_delay = v;
  612. got_max_ack_delay = 1;
  613. break;
  614. case QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI:
  615. if (got_initial_max_streams_bidi)
  616. /* must not appear more than once */
  617. return 0;
  618. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  619. || v > (((uint64_t)1) << 60))
  620. goto malformed;
  621. assert(ch->max_local_streams_bidi == 0);
  622. ch->max_local_streams_bidi = v;
  623. got_initial_max_streams_bidi = 1;
  624. break;
  625. case QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI:
  626. if (got_initial_max_streams_uni)
  627. /* must not appear more than once */
  628. goto malformed;
  629. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  630. || v > (((uint64_t)1) << 60))
  631. goto malformed;
  632. assert(ch->max_local_streams_uni == 0);
  633. ch->max_local_streams_uni = v;
  634. got_initial_max_streams_uni = 1;
  635. break;
  636. case QUIC_TPARAM_MAX_IDLE_TIMEOUT:
  637. if (got_max_idle_timeout)
  638. /* must not appear more than once */
  639. goto malformed;
  640. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v))
  641. goto malformed;
  642. if (v < ch->max_idle_timeout)
  643. ch->max_idle_timeout = v;
  644. ch_update_idle(ch);
  645. got_max_idle_timeout = 1;
  646. break;
  647. case QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE:
  648. if (got_max_udp_payload_size)
  649. /* must not appear more than once */
  650. goto malformed;
  651. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  652. || v < QUIC_MIN_INITIAL_DGRAM_LEN)
  653. goto malformed;
  654. ch->rx_max_udp_payload_size = v;
  655. got_max_udp_payload_size = 1;
  656. break;
  657. case QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT:
  658. if (got_active_conn_id_limit)
  659. /* must not appear more than once */
  660. goto malformed;
  661. if (!ossl_quic_wire_decode_transport_param_int(&pkt, &id, &v)
  662. || v < QUIC_MIN_ACTIVE_CONN_ID_LIMIT)
  663. goto malformed;
  664. ch->rx_active_conn_id_limit = v;
  665. got_active_conn_id_limit = 1;
  666. break;
  667. /*
  668. * TODO(QUIC): Handle:
  669. * QUIC_TPARAM_STATELESS_RESET_TOKEN
  670. * QUIC_TPARAM_PREFERRED_ADDR
  671. */
  672. case QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION:
  673. /* We do not currently handle migration, so nothing to do. */
  674. default:
  675. /* Skip over and ignore. */
  676. body = ossl_quic_wire_decode_transport_param_bytes(&pkt, &id,
  677. &len);
  678. if (body == NULL)
  679. goto malformed;
  680. break;
  681. }
  682. }
  683. if (!got_orig_dcid || !got_initial_scid || got_retry_scid != ch->doing_retry)
  684. /* Transport parameters were not valid. */
  685. goto malformed;
  686. ch->got_remote_transport_params = 1;
  687. if (got_initial_max_data || got_initial_max_stream_data_bidi_remote
  688. || got_initial_max_streams_bidi || got_initial_max_streams_uni)
  689. /* If FC credit was bumped, we may now be able to send. */
  690. ossl_quic_stream_map_update_state(&ch->qsm, ch->stream0);
  691. /* If we are a server, we now generate our own transport parameters. */
  692. if (ch->is_server && !ch_generate_transport_params(ch)) {
  693. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR, 0,
  694. "internal error");
  695. return 0;
  696. }
  697. return 1;
  698. malformed:
  699. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_TRANSPORT_PARAMETER_ERROR,
  700. 0, "bad transport parameter");
  701. return 0;
  702. }
  703. /*
  704. * Called when we want to generate transport parameters. This is called
  705. * immediately at instantiation time for a client and after we receive the
  706. * client's transport parameters for a server.
  707. */
  708. static int ch_generate_transport_params(QUIC_CHANNEL *ch)
  709. {
  710. int ok = 0;
  711. BUF_MEM *buf_mem = NULL;
  712. WPACKET wpkt;
  713. int wpkt_valid = 0;
  714. size_t buf_len = 0;
  715. if (ch->local_transport_params != NULL)
  716. goto err;
  717. if ((buf_mem = BUF_MEM_new()) == NULL)
  718. goto err;
  719. if (!WPACKET_init(&wpkt, buf_mem))
  720. goto err;
  721. wpkt_valid = 1;
  722. if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_DISABLE_ACTIVE_MIGRATION,
  723. NULL, 0) == NULL)
  724. goto err;
  725. if (ossl_quic_wire_encode_transport_param_bytes(&wpkt, QUIC_TPARAM_INITIAL_SCID,
  726. NULL, 0) == NULL)
  727. goto err;
  728. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_IDLE_TIMEOUT,
  729. ch->max_idle_timeout))
  730. goto err;
  731. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_MAX_UDP_PAYLOAD_SIZE,
  732. QUIC_MIN_INITIAL_DGRAM_LEN))
  733. goto err;
  734. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_ACTIVE_CONN_ID_LIMIT,
  735. 4))
  736. goto err;
  737. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_DATA,
  738. ossl_quic_rxfc_get_cwm(&ch->conn_rxfc)))
  739. goto err;
  740. /*
  741. * We actually want the default CWM for a new RXFC, but here we just use
  742. * stream0 as a representative specimen. TODO(QUIC): revisit this when we
  743. * support multiple streams.
  744. */
  745. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
  746. ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
  747. goto err;
  748. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
  749. ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
  750. goto err;
  751. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAM_DATA_UNI,
  752. ossl_quic_rxfc_get_cwm(&ch->stream0->rxfc)))
  753. goto err;
  754. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_BIDI,
  755. 0))
  756. goto err;
  757. if (!ossl_quic_wire_encode_transport_param_int(&wpkt, QUIC_TPARAM_INITIAL_MAX_STREAMS_UNI,
  758. 0))
  759. goto err;
  760. if (!WPACKET_get_total_written(&wpkt, &buf_len))
  761. goto err;
  762. ch->local_transport_params = (unsigned char *)buf_mem->data;
  763. buf_mem->data = NULL;
  764. if (!WPACKET_finish(&wpkt))
  765. goto err;
  766. wpkt_valid = 0;
  767. if (!ossl_quic_dhs_set_transport_params(ch->dhs, ch->local_transport_params,
  768. buf_len))
  769. goto err;
  770. ok = 1;
  771. err:
  772. if (wpkt_valid)
  773. WPACKET_cleanup(&wpkt);
  774. BUF_MEM_free(buf_mem);
  775. return ok;
  776. }
  777. /*
  778. * QUIC Channel: Ticker-Mutator
  779. * ============================
  780. */
  781. /*
  782. * The central ticker function called by the reactor. This does everything, or
  783. * at least everything network I/O related. Best effort - not allowed to fail
  784. * "loudly".
  785. */
  786. static void ch_tick(QUIC_TICK_RESULT *res, void *arg)
  787. {
  788. OSSL_TIME now, deadline;
  789. QUIC_CHANNEL *ch = arg;
  790. /*
  791. * When we tick the QUIC connection, we do everything we need to do
  792. * periodically. In order, we:
  793. *
  794. * - handle any incoming data from the network;
  795. * - handle any timer events which are due to fire (ACKM, etc.)
  796. * - write any data to the network due to be sent, to the extent
  797. * possible;
  798. * - determine the time at which we should next be ticked.
  799. */
  800. /* If we are in the TERMINATED state, there is nothing to do. */
  801. if (ossl_quic_channel_is_terminated(ch)) {
  802. res->want_net_read = 0;
  803. res->want_net_write = 0;
  804. res->tick_deadline = ossl_time_infinite();
  805. return;
  806. }
  807. /*
  808. * If we are in the TERMINATING state, check if the terminating timer has
  809. * expired.
  810. */
  811. if (ossl_quic_channel_is_terminating(ch)) {
  812. now = ossl_time_now();
  813. if (ossl_time_compare(now, ch->terminate_deadline) >= 0) {
  814. ch_on_terminating_timeout(ch);
  815. res->want_net_read = 0;
  816. res->want_net_write = 0;
  817. res->tick_deadline = ossl_time_infinite();
  818. return; /* abort normal processing, nothing to do */
  819. }
  820. }
  821. /* Handle any incoming data from the network. */
  822. ch_rx(ch);
  823. /*
  824. * Allow the handshake layer to check for any new incoming data and generate
  825. * new outgoing data.
  826. */
  827. ossl_quic_dhs_tick(ch->dhs);
  828. /*
  829. * Handle any timer events which are due to fire; namely, the loss detection
  830. * deadline and the idle timeout.
  831. *
  832. * ACKM ACK generation deadline is polled by TXP, so we don't need to handle
  833. * it here.
  834. */
  835. now = ossl_time_now();
  836. if (ossl_time_compare(now, ch->idle_deadline) >= 0) {
  837. /*
  838. * Idle timeout differs from normal protocol violation because we do not
  839. * send a CONN_CLOSE frame; go straight to TERMINATED.
  840. */
  841. ch_on_idle_timeout(ch);
  842. res->want_net_read = 0;
  843. res->want_net_write = 0;
  844. res->tick_deadline = ossl_time_infinite();
  845. return;
  846. }
  847. deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
  848. if (!ossl_time_is_zero(deadline) && ossl_time_compare(now, deadline) >= 0)
  849. ossl_ackm_on_timeout(ch->ackm);
  850. /* Write any data to the network due to be sent. */
  851. ch_tx(ch);
  852. /* Determine the time at which we should next be ticked. */
  853. res->tick_deadline = ch_determine_next_tick_deadline(ch);
  854. /* Always process network input. */
  855. res->want_net_read = 1;
  856. /* We want to write to the network if we have any in our queue. */
  857. res->want_net_write = (ossl_qtx_get_queue_len_datagrams(ch->qtx) > 0);
  858. }
  859. /* Process incoming packets and handle frames, if any. */
  860. static int ch_rx(QUIC_CHANNEL *ch)
  861. {
  862. int handled_any = 0;
  863. if (!ch->have_sent_any_pkt)
  864. /*
  865. * We have not sent anything yet, therefore there is no need to check
  866. * for incoming data. TODO SERVER
  867. */
  868. return 1;
  869. /*
  870. * Get DEMUX to BIO_recvmmsg from the network and queue incoming datagrams
  871. * to the appropriate QRX instance.
  872. */
  873. ossl_quic_demux_pump(ch->demux); /* best effort */
  874. for (;;) {
  875. assert(ch->qrx_pkt == NULL);
  876. if (!ossl_qrx_read_pkt(ch->qrx, &ch->qrx_pkt))
  877. break;
  878. if (!handled_any)
  879. ch_update_idle(ch);
  880. ch_rx_handle_packet(ch); /* best effort */
  881. /*
  882. * Regardless of the outcome of frame handling, unref the packet.
  883. * This will free the packet unless something added another
  884. * reference to it during frame processing.
  885. */
  886. ossl_qrx_pkt_release(ch->qrx_pkt);
  887. ch->qrx_pkt = NULL;
  888. handled_any = 1;
  889. }
  890. /*
  891. * When in TERMINATING - CLOSING, generate a CONN_CLOSE frame whenever we
  892. * process one or more incoming packets.
  893. */
  894. if (handled_any && ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING)
  895. ch->conn_close_queued = 1;
  896. return 1;
  897. }
  898. /* Handles the packet currently in ch->qrx_pkt->hdr. */
  899. static void ch_rx_handle_packet(QUIC_CHANNEL *ch)
  900. {
  901. uint32_t enc_level;
  902. assert(ch->qrx_pkt != NULL);
  903. if (ossl_quic_pkt_type_is_encrypted(ch->qrx_pkt->hdr->type)) {
  904. if (!ch->have_received_enc_pkt) {
  905. ch->init_scid = ch->qrx_pkt->hdr->src_conn_id;
  906. ch->have_received_enc_pkt = 1;
  907. /*
  908. * We change to using the SCID in the first Initial packet as the
  909. * DCID.
  910. */
  911. ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, &ch->init_scid);
  912. }
  913. enc_level = ossl_quic_pkt_type_to_enc_level(ch->qrx_pkt->hdr->type);
  914. if ((ch->el_discarded & (1U << enc_level)) != 0)
  915. /* Do not process packets from ELs we have already discarded. */
  916. return;
  917. }
  918. /* Handle incoming packet. */
  919. switch (ch->qrx_pkt->hdr->type) {
  920. case QUIC_PKT_TYPE_RETRY:
  921. if (ch->doing_retry)
  922. /* It is not allowed to ask a client to do a retry more than
  923. * once. */
  924. return;
  925. /* TODO if server */
  926. if (ch->qrx_pkt->hdr->len <= QUIC_RETRY_INTEGRITY_TAG_LEN)
  927. /* Packets with zero-length Retry Tokens are invalid. */
  928. return;
  929. /*
  930. * TODO(QUIC): Theoretically this should probably be in the QRX.
  931. * However because validation is dependent on context (namely the
  932. * client's initial DCID) we can't do this cleanly. In the future we
  933. * should probably add a callback to the QRX to let it call us (via
  934. * the DEMUX) and ask us about the correct original DCID, rather
  935. * than allow the QRX to emit a potentially malformed packet to the
  936. * upper layers. However, special casing this will do for now.
  937. */
  938. if (!ossl_quic_validate_retry_integrity_tag(ch->libctx,
  939. ch->propq,
  940. ch->qrx_pkt->hdr,
  941. &ch->init_dcid))
  942. /* Malformed retry packet, ignore. */
  943. return;
  944. ch_retry(ch, ch->qrx_pkt->hdr->data,
  945. ch->qrx_pkt->hdr->len - QUIC_RETRY_INTEGRITY_TAG_LEN,
  946. &ch->qrx_pkt->hdr->src_conn_id);
  947. break;
  948. case QUIC_PKT_TYPE_VERSION_NEG:
  949. /* TODO(QUIC): Implement version negotiation */
  950. break;
  951. case QUIC_PKT_TYPE_0RTT:
  952. /* TODO if server */
  953. /* Clients should never receive 0-RTT packets */
  954. break;
  955. default:
  956. if (ch->qrx_pkt->hdr->type == QUIC_PKT_TYPE_HANDSHAKE)
  957. /*
  958. * We automatically drop INITIAL EL keys when first successfully
  959. * decrypting a HANDSHAKE packet, as per the RFC.
  960. */
  961. ch_discard_el(ch, QUIC_ENC_LEVEL_INITIAL);
  962. /* This packet contains frames, pass to the RXDP. */
  963. ossl_quic_handle_frames(ch, ch->qrx_pkt); /* best effort */
  964. break;
  965. }
  966. }
  967. /* Try to generate packets and if possible, flush them to the network. */
  968. static int ch_tx(QUIC_CHANNEL *ch)
  969. {
  970. if (ch->state == QUIC_CHANNEL_STATE_TERMINATING_CLOSING) {
  971. /*
  972. * While closing, only send CONN_CLOSE if we've received more traffic
  973. * from the peer. Once we tell the TXP to generate CONN_CLOSE, all
  974. * future calls to it generate CONN_CLOSE frames, so otherwise we would
  975. * just constantly generate CONN_CLOSE frames.
  976. */
  977. if (!ch->conn_close_queued)
  978. return 0;
  979. ch->conn_close_queued = 0;
  980. }
  981. /*
  982. * Send a packet, if we need to. Best effort. The TXP consults the CC and
  983. * applies any limitations imposed by it, so we don't need to do it here.
  984. *
  985. * Best effort. In particular if TXP fails for some reason we should still
  986. * flush any queued packets which we already generated.
  987. */
  988. if (ossl_quic_tx_packetiser_generate(ch->txp,
  989. TX_PACKETISER_ARCHETYPE_NORMAL)
  990. == TX_PACKETISER_RES_SENT_PKT)
  991. ch->have_sent_any_pkt = 1;
  992. ossl_qtx_flush_net(ch->qtx); /* best effort */
  993. return 1;
  994. }
  995. /* Determine next tick deadline. */
  996. static OSSL_TIME ch_determine_next_tick_deadline(QUIC_CHANNEL *ch)
  997. {
  998. OSSL_TIME deadline;
  999. uint32_t pn_space;
  1000. deadline = ossl_ackm_get_loss_detection_deadline(ch->ackm);
  1001. if (ossl_time_is_zero(deadline))
  1002. deadline = ossl_time_infinite();
  1003. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  1004. deadline = ossl_time_min(deadline,
  1005. ossl_ackm_get_ack_deadline(ch->ackm, pn_space));
  1006. /* When will CC let us send more? */
  1007. if (ossl_quic_tx_packetiser_has_pending(ch->txp, TX_PACKETISER_ARCHETYPE_NORMAL,
  1008. TX_PACKETISER_BYPASS_CC))
  1009. deadline = ossl_time_min(deadline,
  1010. ch->cc_method->get_next_credit_time(ch->cc_data));
  1011. /* Is the terminating timer armed? */
  1012. if (ossl_quic_channel_is_terminating(ch))
  1013. deadline = ossl_time_min(deadline,
  1014. ch->terminate_deadline);
  1015. else if (!ossl_time_is_infinite(ch->idle_deadline))
  1016. deadline = ossl_time_min(deadline,
  1017. ch->idle_deadline);
  1018. return deadline;
  1019. }
  1020. /*
  1021. * QUIC Channel: Network BIO Configuration
  1022. * =======================================
  1023. */
  1024. /* Determines whether we can support a given poll descriptor. */
  1025. static int validate_poll_descriptor(const BIO_POLL_DESCRIPTOR *d)
  1026. {
  1027. if (d->type == BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD && d->value.fd < 0)
  1028. return 0;
  1029. return 1;
  1030. }
  1031. BIO *ossl_quic_channel_get_net_rbio(QUIC_CHANNEL *ch)
  1032. {
  1033. return ch->net_rbio;
  1034. }
  1035. BIO *ossl_quic_channel_get_net_wbio(QUIC_CHANNEL *ch)
  1036. {
  1037. return ch->net_wbio;
  1038. }
  1039. int ossl_quic_channel_set0_net_rbio(QUIC_CHANNEL *ch, BIO *net_rbio)
  1040. {
  1041. BIO_POLL_DESCRIPTOR d = {0};
  1042. if (ch->net_rbio == net_rbio)
  1043. return 1;
  1044. if (net_rbio != NULL) {
  1045. if (!BIO_get_rpoll_descriptor(net_rbio, &d))
  1046. /* Non-pollable BIO */
  1047. d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
  1048. if (!validate_poll_descriptor(&d))
  1049. return 0;
  1050. }
  1051. ossl_quic_reactor_set_poll_r(&ch->rtor, &d);
  1052. BIO_free(ch->net_rbio);
  1053. ossl_quic_demux_set_bio(ch->demux, net_rbio);
  1054. ch->net_rbio = net_rbio;
  1055. return 1;
  1056. }
  1057. int ossl_quic_channel_set0_net_wbio(QUIC_CHANNEL *ch, BIO *net_wbio)
  1058. {
  1059. BIO_POLL_DESCRIPTOR d = {0};
  1060. if (ch->net_wbio == net_wbio)
  1061. return 1;
  1062. if (net_wbio != NULL) {
  1063. if (!BIO_get_wpoll_descriptor(net_wbio, &d))
  1064. /* Non-pollable BIO */
  1065. d.type = BIO_POLL_DESCRIPTOR_TYPE_NONE;
  1066. if (!validate_poll_descriptor(&d))
  1067. return 0;
  1068. }
  1069. ossl_quic_reactor_set_poll_w(&ch->rtor, &d);
  1070. BIO_free(ch->net_wbio);
  1071. ossl_qtx_set_bio(ch->qtx, net_wbio);
  1072. ch->net_wbio = net_wbio;
  1073. return 1;
  1074. }
  1075. /*
  1076. * QUIC Channel: Lifecycle Events
  1077. * ==============================
  1078. */
  1079. int ossl_quic_channel_start(QUIC_CHANNEL *ch)
  1080. {
  1081. /* TODO SERVER */
  1082. if (ch->state != QUIC_CHANNEL_STATE_IDLE)
  1083. /* Calls to connect are idempotent */
  1084. return 1;
  1085. /* Inform QTX of peer address. */
  1086. if (!ossl_quic_tx_packetiser_set_peer(ch->txp, &ch->cur_peer_addr))
  1087. return 0;
  1088. /* Plug in secrets for the Initial EL. */
  1089. if (!ossl_quic_provide_initial_secret(ch->libctx,
  1090. ch->propq,
  1091. &ch->init_dcid,
  1092. /*is_server=*/0,
  1093. ch->qrx, ch->qtx))
  1094. return 0;
  1095. /* Change state. */
  1096. ch->state = QUIC_CHANNEL_STATE_ACTIVE;
  1097. ch->doing_proactive_ver_neg = 0; /* not currently supported */
  1098. /* Handshake layer: start (e.g. send CH). */
  1099. if (!ossl_quic_dhs_tick(ch->dhs))
  1100. return 0;
  1101. ossl_quic_reactor_tick(&ch->rtor); /* best effort */
  1102. return 1;
  1103. }
  1104. /* Start a locally initiated connection shutdown. */
  1105. void ossl_quic_channel_local_close(QUIC_CHANNEL *ch)
  1106. {
  1107. QUIC_TERMINATE_CAUSE tcause = {0};
  1108. if (ossl_quic_channel_is_term_any(ch))
  1109. return;
  1110. tcause.app = 1;
  1111. ch_start_terminating(ch, &tcause);
  1112. }
  1113. static void free_token(const unsigned char *buf, size_t buf_len, void *arg)
  1114. {
  1115. OPENSSL_free((unsigned char *)buf);
  1116. }
  1117. /* Called when a server asks us to do a retry. */
  1118. static int ch_retry(QUIC_CHANNEL *ch,
  1119. const unsigned char *retry_token,
  1120. size_t retry_token_len,
  1121. const QUIC_CONN_ID *retry_scid)
  1122. {
  1123. void *buf;
  1124. /* We change to using the SCID in the Retry packet as the DCID. */
  1125. if (!ossl_quic_tx_packetiser_set_cur_dcid(ch->txp, retry_scid))
  1126. return 0;
  1127. /*
  1128. * Now we retry. We will release the Retry packet immediately, so copy
  1129. * the token.
  1130. */
  1131. if ((buf = OPENSSL_malloc(retry_token_len)) == NULL)
  1132. return 0;
  1133. memcpy(buf, retry_token, retry_token_len);
  1134. ossl_quic_tx_packetiser_set_initial_token(ch->txp, buf, retry_token_len,
  1135. free_token, NULL);
  1136. ch->retry_scid = *retry_scid;
  1137. ch->doing_retry = 1;
  1138. /*
  1139. * We need to stimulate the Initial EL to generate the first CRYPTO frame
  1140. * again. We can do this most cleanly by simply forcing the ACKM to consider
  1141. * the first Initial packet as lost, which it effectively was as the server
  1142. * hasn't processed it. This also maintains the desired behaviour with e.g.
  1143. * PNs not resetting and so on.
  1144. *
  1145. * The PN we used initially is always zero, because QUIC does not allow
  1146. * repeated retries.
  1147. */
  1148. if (!ossl_ackm_mark_packet_pseudo_lost(ch->ackm, QUIC_PN_SPACE_INITIAL,
  1149. /*PN=*/0))
  1150. return 0;
  1151. /*
  1152. * Plug in new secrets for the Initial EL. This is the only time we change
  1153. * the secrets for an EL after we already provisioned it.
  1154. */
  1155. if (!ossl_quic_provide_initial_secret(ch->libctx,
  1156. ch->propq,
  1157. &ch->retry_scid,
  1158. /*is_server=*/0,
  1159. ch->qrx, ch->qtx))
  1160. return 0;
  1161. return 1;
  1162. }
  1163. /* Called when an EL is to be discarded. */
  1164. static int ch_discard_el(QUIC_CHANNEL *ch,
  1165. uint32_t enc_level)
  1166. {
  1167. if (!ossl_assert(enc_level < QUIC_ENC_LEVEL_1RTT))
  1168. return 0;
  1169. if ((ch->el_discarded & (1U << enc_level)) != 0)
  1170. /* Already done. */
  1171. return 1;
  1172. /* Best effort for all of these. */
  1173. ossl_quic_tx_packetiser_discard_enc_level(ch->txp, enc_level);
  1174. ossl_qrx_discard_enc_level(ch->qrx, enc_level);
  1175. ossl_qtx_discard_enc_level(ch->qtx, enc_level);
  1176. if (enc_level != QUIC_ENC_LEVEL_0RTT) {
  1177. uint32_t pn_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1178. ossl_ackm_on_pkt_space_discarded(ch->ackm, pn_space);
  1179. /* We should still have crypto streams at this point. */
  1180. assert(ch->crypto_send[pn_space] != NULL);
  1181. assert(ch->crypto_recv[pn_space] != NULL);
  1182. /* Get rid of the crypto stream state for the EL. */
  1183. ossl_quic_sstream_free(ch->crypto_send[pn_space]);
  1184. ch->crypto_send[pn_space] = NULL;
  1185. ossl_quic_rstream_free(ch->crypto_recv[pn_space]);
  1186. ch->crypto_recv[pn_space] = NULL;
  1187. }
  1188. ch->el_discarded |= (1U << enc_level);
  1189. return 1;
  1190. }
  1191. /* Intended to be called by the RXDP. */
  1192. int ossl_quic_channel_on_handshake_confirmed(QUIC_CHANNEL *ch)
  1193. {
  1194. if (ch->handshake_confirmed)
  1195. return 1;
  1196. if (!ch->handshake_complete) {
  1197. /*
  1198. * Does not make sense for handshake to be confirmed before it is
  1199. * completed.
  1200. */
  1201. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_PROTOCOL_VIOLATION,
  1202. OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
  1203. "handshake cannot be confirmed "
  1204. "before it is completed");
  1205. return 0;
  1206. }
  1207. ch_discard_el(ch, QUIC_ENC_LEVEL_HANDSHAKE);
  1208. ch->handshake_confirmed = 1;
  1209. return 1;
  1210. }
  1211. /*
  1212. * Master function used when we want to start tearing down a connection:
  1213. *
  1214. * - If the connection is still IDLE we can go straight to TERMINATED;
  1215. *
  1216. * - If we are already TERMINATED this is a no-op.
  1217. *
  1218. * - If we are TERMINATING - CLOSING and we have now got a CONNECTION_CLOSE
  1219. * from the peer (tcause->remote == 1), we move to TERMINATING - CLOSING.
  1220. *
  1221. * - If we are TERMINATING - DRAINING, we remain here until the terminating
  1222. * timer expires.
  1223. *
  1224. * - Otherwise, we are in ACTIVE and move to TERMINATING - CLOSING.
  1225. * if we caused the termination (e.g. we have sent a CONNECTION_CLOSE). Note
  1226. * that we are considered to have caused a termination if we sent the first
  1227. * CONNECTION_CLOSE frame, even if it is caused by a peer protocol
  1228. * violation. If the peer sent the first CONNECTION_CLOSE frame, we move to
  1229. * TERMINATING - DRAINING.
  1230. *
  1231. * We record the termination cause structure passed on the first call only.
  1232. * Any successive calls have their termination cause data discarded;
  1233. * once we start sending a CONNECTION_CLOSE frame, we don't change the details
  1234. * in it.
  1235. */
  1236. static void ch_start_terminating(QUIC_CHANNEL *ch,
  1237. const QUIC_TERMINATE_CAUSE *tcause)
  1238. {
  1239. switch (ch->state) {
  1240. default:
  1241. case QUIC_CHANNEL_STATE_IDLE:
  1242. ch->terminate_cause = *tcause;
  1243. ch_on_terminating_timeout(ch);
  1244. break;
  1245. case QUIC_CHANNEL_STATE_ACTIVE:
  1246. ch->state = tcause->remote ? QUIC_CHANNEL_STATE_TERMINATING_DRAINING
  1247. : QUIC_CHANNEL_STATE_TERMINATING_CLOSING;
  1248. ch->terminate_cause = *tcause;
  1249. ch->terminate_deadline
  1250. = ossl_time_add(ossl_time_now(),
  1251. ossl_time_multiply(ossl_ackm_get_pto_duration(ch->ackm),
  1252. 3));
  1253. if (!tcause->remote) {
  1254. OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
  1255. /* best effort */
  1256. f.error_code = ch->terminate_cause.error_code;
  1257. f.frame_type = ch->terminate_cause.frame_type;
  1258. f.is_app = ch->terminate_cause.app;
  1259. ossl_quic_tx_packetiser_schedule_conn_close(ch->txp, &f);
  1260. ch->conn_close_queued = 1;
  1261. }
  1262. break;
  1263. case QUIC_CHANNEL_STATE_TERMINATING_CLOSING:
  1264. if (tcause->remote)
  1265. ch->state = QUIC_CHANNEL_STATE_TERMINATING_DRAINING;
  1266. break;
  1267. case QUIC_CHANNEL_STATE_TERMINATING_DRAINING:
  1268. /* We remain here until the timout expires. */
  1269. break;
  1270. case QUIC_CHANNEL_STATE_TERMINATED:
  1271. /* No-op. */
  1272. break;
  1273. }
  1274. }
  1275. /* For RXDP use. */
  1276. void ossl_quic_channel_on_remote_conn_close(QUIC_CHANNEL *ch,
  1277. OSSL_QUIC_FRAME_CONN_CLOSE *f)
  1278. {
  1279. QUIC_TERMINATE_CAUSE tcause = {0};
  1280. if (!ossl_quic_channel_is_active(ch))
  1281. return;
  1282. tcause.remote = 1;
  1283. tcause.app = f->is_app;
  1284. tcause.error_code = f->error_code;
  1285. tcause.frame_type = f->frame_type;
  1286. ch_start_terminating(ch, &tcause);
  1287. }
  1288. void ossl_quic_channel_raise_protocol_error(QUIC_CHANNEL *ch,
  1289. uint64_t error_code,
  1290. uint64_t frame_type,
  1291. const char *reason)
  1292. {
  1293. QUIC_TERMINATE_CAUSE tcause = {0};
  1294. tcause.error_code = error_code;
  1295. tcause.frame_type = frame_type;
  1296. ch_start_terminating(ch, &tcause);
  1297. }
  1298. /*
  1299. * Called once the terminating timer expires, meaning we move from TERMINATING
  1300. * to TERMINATED.
  1301. */
  1302. static void ch_on_terminating_timeout(QUIC_CHANNEL *ch)
  1303. {
  1304. ch->state = QUIC_CHANNEL_STATE_TERMINATED;
  1305. }
  1306. /*
  1307. * Updates our idle deadline. Called when an event happens which should bump the
  1308. * idle timeout.
  1309. */
  1310. static void ch_update_idle(QUIC_CHANNEL *ch)
  1311. {
  1312. if (ch->max_idle_timeout == 0)
  1313. ch->idle_deadline = ossl_time_infinite();
  1314. else
  1315. ch->idle_deadline = ossl_time_add(ossl_time_now(),
  1316. ossl_ms2time(ch->max_idle_timeout));
  1317. }
  1318. /* Called when the idle timeout expires. */
  1319. static void ch_on_idle_timeout(QUIC_CHANNEL *ch)
  1320. {
  1321. /*
  1322. * Idle timeout does not have an error code associated with it because a
  1323. * CONN_CLOSE is never sent for it. We shouldn't use this data once we reach
  1324. * TERMINATED anyway.
  1325. */
  1326. ch->terminate_cause.app = 0;
  1327. ch->terminate_cause.error_code = UINT64_MAX;
  1328. ch->terminate_cause.frame_type = 0;
  1329. ch->state = QUIC_CHANNEL_STATE_TERMINATED;
  1330. }