quic_record_rx.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*
  2. * Copyright 2022-2024 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 <openssl/ssl.h>
  10. #include "internal/quic_record_rx.h"
  11. #include "quic_record_shared.h"
  12. #include "internal/common.h"
  13. #include "internal/list.h"
  14. #include "../ssl_local.h"
  15. /*
  16. * Mark a packet in a bitfield.
  17. *
  18. * pkt_idx: index of packet within datagram.
  19. */
  20. static ossl_inline void pkt_mark(uint64_t *bitf, size_t pkt_idx)
  21. {
  22. assert(pkt_idx < QUIC_MAX_PKT_PER_URXE);
  23. *bitf |= ((uint64_t)1) << pkt_idx;
  24. }
  25. /* Returns 1 if a packet is in the bitfield. */
  26. static ossl_inline int pkt_is_marked(const uint64_t *bitf, size_t pkt_idx)
  27. {
  28. assert(pkt_idx < QUIC_MAX_PKT_PER_URXE);
  29. return (*bitf & (((uint64_t)1) << pkt_idx)) != 0;
  30. }
  31. /*
  32. * RXE
  33. * ===
  34. *
  35. * RX Entries (RXEs) store processed (i.e., decrypted) data received from the
  36. * network. One RXE is used per received QUIC packet.
  37. */
  38. typedef struct rxe_st RXE;
  39. struct rxe_st {
  40. OSSL_QRX_PKT pkt;
  41. OSSL_LIST_MEMBER(rxe, RXE);
  42. size_t data_len, alloc_len, refcount;
  43. /* Extra fields for per-packet information. */
  44. QUIC_PKT_HDR hdr; /* data/len are decrypted payload */
  45. /* Decoded packet number. */
  46. QUIC_PN pn;
  47. /* Addresses copied from URXE. */
  48. BIO_ADDR peer, local;
  49. /* Time we received the packet (not when we processed it). */
  50. OSSL_TIME time;
  51. /* Total length of the datagram which contained this packet. */
  52. size_t datagram_len;
  53. /*
  54. * The key epoch the packet was received with. Always 0 for non-1-RTT
  55. * packets.
  56. */
  57. uint64_t key_epoch;
  58. /*
  59. * Monotonically increases with each datagram received.
  60. * For diagnostic use only.
  61. */
  62. uint64_t datagram_id;
  63. /*
  64. * alloc_len allocated bytes (of which data_len bytes are valid) follow this
  65. * structure.
  66. */
  67. };
  68. DEFINE_LIST_OF(rxe, RXE);
  69. typedef OSSL_LIST(rxe) RXE_LIST;
  70. static ossl_inline unsigned char *rxe_data(const RXE *e)
  71. {
  72. return (unsigned char *)(e + 1);
  73. }
  74. /*
  75. * QRL
  76. * ===
  77. */
  78. struct ossl_qrx_st {
  79. OSSL_LIB_CTX *libctx;
  80. const char *propq;
  81. /* Demux to receive datagrams from. */
  82. QUIC_DEMUX *demux;
  83. /* Length of connection IDs used in short-header packets in bytes. */
  84. size_t short_conn_id_len;
  85. /* Maximum number of deferred datagrams buffered at any one time. */
  86. size_t max_deferred;
  87. /* Current count of deferred datagrams. */
  88. size_t num_deferred;
  89. /*
  90. * List of URXEs which are filled with received encrypted data.
  91. * These are returned to the DEMUX's free list as they are processed.
  92. */
  93. QUIC_URXE_LIST urx_pending;
  94. /*
  95. * List of URXEs which we could not decrypt immediately and which are being
  96. * kept in case they can be decrypted later.
  97. */
  98. QUIC_URXE_LIST urx_deferred;
  99. /*
  100. * List of RXEs which are not currently in use. These are moved
  101. * to the pending list as they are filled.
  102. */
  103. RXE_LIST rx_free;
  104. /*
  105. * List of RXEs which are filled with decrypted packets ready to be passed
  106. * to the user. A RXE is removed from all lists inside the QRL when passed
  107. * to the user, then returned to the free list when the user returns it.
  108. */
  109. RXE_LIST rx_pending;
  110. /* Largest PN we have received and processed in a given PN space. */
  111. QUIC_PN largest_pn[QUIC_PN_SPACE_NUM];
  112. /* Per encryption-level state. */
  113. OSSL_QRL_ENC_LEVEL_SET el_set;
  114. /* Bytes we have received since this counter was last cleared. */
  115. uint64_t bytes_received;
  116. /*
  117. * Number of forged packets we have received since the QRX was instantiated.
  118. * Note that as per RFC 9001, this is connection-level state; it is not per
  119. * EL and is not reset by a key update.
  120. */
  121. uint64_t forged_pkt_count;
  122. /*
  123. * The PN the current key epoch started at, inclusive.
  124. */
  125. uint64_t cur_epoch_start_pn;
  126. /* Validation callback. */
  127. ossl_qrx_late_validation_cb *validation_cb;
  128. void *validation_cb_arg;
  129. /* Key update callback. */
  130. ossl_qrx_key_update_cb *key_update_cb;
  131. void *key_update_cb_arg;
  132. /* Initial key phase. For debugging use only; always 0 in real use. */
  133. unsigned char init_key_phase_bit;
  134. /* Are we allowed to process 1-RTT packets yet? */
  135. unsigned char allow_1rtt;
  136. /* Message callback related arguments */
  137. ossl_msg_cb msg_callback;
  138. void *msg_callback_arg;
  139. SSL *msg_callback_ssl;
  140. };
  141. OSSL_QRX *ossl_qrx_new(const OSSL_QRX_ARGS *args)
  142. {
  143. OSSL_QRX *qrx;
  144. size_t i;
  145. if (args->demux == NULL || args->max_deferred == 0)
  146. return NULL;
  147. qrx = OPENSSL_zalloc(sizeof(OSSL_QRX));
  148. if (qrx == NULL)
  149. return NULL;
  150. for (i = 0; i < OSSL_NELEM(qrx->largest_pn); ++i)
  151. qrx->largest_pn[i] = args->init_largest_pn[i];
  152. qrx->libctx = args->libctx;
  153. qrx->propq = args->propq;
  154. qrx->demux = args->demux;
  155. qrx->short_conn_id_len = args->short_conn_id_len;
  156. qrx->init_key_phase_bit = args->init_key_phase_bit;
  157. qrx->max_deferred = args->max_deferred;
  158. return qrx;
  159. }
  160. static void qrx_cleanup_rxl(RXE_LIST *l)
  161. {
  162. RXE *e, *enext;
  163. for (e = ossl_list_rxe_head(l); e != NULL; e = enext) {
  164. enext = ossl_list_rxe_next(e);
  165. ossl_list_rxe_remove(l, e);
  166. OPENSSL_free(e);
  167. }
  168. }
  169. static void qrx_cleanup_urxl(OSSL_QRX *qrx, QUIC_URXE_LIST *l)
  170. {
  171. QUIC_URXE *e, *enext;
  172. for (e = ossl_list_urxe_head(l); e != NULL; e = enext) {
  173. enext = ossl_list_urxe_next(e);
  174. ossl_list_urxe_remove(l, e);
  175. ossl_quic_demux_release_urxe(qrx->demux, e);
  176. }
  177. }
  178. void ossl_qrx_free(OSSL_QRX *qrx)
  179. {
  180. uint32_t i;
  181. if (qrx == NULL)
  182. return;
  183. /* Free RXE queue data. */
  184. qrx_cleanup_rxl(&qrx->rx_free);
  185. qrx_cleanup_rxl(&qrx->rx_pending);
  186. qrx_cleanup_urxl(qrx, &qrx->urx_pending);
  187. qrx_cleanup_urxl(qrx, &qrx->urx_deferred);
  188. /* Drop keying material and crypto resources. */
  189. for (i = 0; i < QUIC_ENC_LEVEL_NUM; ++i)
  190. ossl_qrl_enc_level_set_discard(&qrx->el_set, i);
  191. OPENSSL_free(qrx);
  192. }
  193. void ossl_qrx_inject_urxe(OSSL_QRX *qrx, QUIC_URXE *urxe)
  194. {
  195. /* Initialize our own fields inside the URXE and add to the pending list. */
  196. urxe->processed = 0;
  197. urxe->hpr_removed = 0;
  198. urxe->deferred = 0;
  199. ossl_list_urxe_insert_tail(&qrx->urx_pending, urxe);
  200. if (qrx->msg_callback != NULL)
  201. qrx->msg_callback(0, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_DATAGRAM, urxe + 1,
  202. urxe->data_len, qrx->msg_callback_ssl,
  203. qrx->msg_callback_arg);
  204. }
  205. static void qrx_requeue_deferred(OSSL_QRX *qrx)
  206. {
  207. QUIC_URXE *e;
  208. while ((e = ossl_list_urxe_head(&qrx->urx_deferred)) != NULL) {
  209. ossl_list_urxe_remove(&qrx->urx_deferred, e);
  210. ossl_list_urxe_insert_tail(&qrx->urx_pending, e);
  211. }
  212. }
  213. int ossl_qrx_provide_secret(OSSL_QRX *qrx, uint32_t enc_level,
  214. uint32_t suite_id, EVP_MD *md,
  215. const unsigned char *secret, size_t secret_len)
  216. {
  217. if (enc_level >= QUIC_ENC_LEVEL_NUM)
  218. return 0;
  219. if (!ossl_qrl_enc_level_set_provide_secret(&qrx->el_set,
  220. qrx->libctx,
  221. qrx->propq,
  222. enc_level,
  223. suite_id,
  224. md,
  225. secret,
  226. secret_len,
  227. qrx->init_key_phase_bit,
  228. /*is_tx=*/0))
  229. return 0;
  230. /*
  231. * Any packets we previously could not decrypt, we may now be able to
  232. * decrypt, so move any datagrams containing deferred packets from the
  233. * deferred to the pending queue.
  234. */
  235. qrx_requeue_deferred(qrx);
  236. return 1;
  237. }
  238. int ossl_qrx_discard_enc_level(OSSL_QRX *qrx, uint32_t enc_level)
  239. {
  240. if (enc_level >= QUIC_ENC_LEVEL_NUM)
  241. return 0;
  242. ossl_qrl_enc_level_set_discard(&qrx->el_set, enc_level);
  243. return 1;
  244. }
  245. /* Returns 1 if there are one or more pending RXEs. */
  246. int ossl_qrx_processed_read_pending(OSSL_QRX *qrx)
  247. {
  248. return !ossl_list_rxe_is_empty(&qrx->rx_pending);
  249. }
  250. /* Returns 1 if there are yet-unprocessed packets. */
  251. int ossl_qrx_unprocessed_read_pending(OSSL_QRX *qrx)
  252. {
  253. return !ossl_list_urxe_is_empty(&qrx->urx_pending)
  254. || !ossl_list_urxe_is_empty(&qrx->urx_deferred);
  255. }
  256. /* Pop the next pending RXE. Returns NULL if no RXE is pending. */
  257. static RXE *qrx_pop_pending_rxe(OSSL_QRX *qrx)
  258. {
  259. RXE *rxe = ossl_list_rxe_head(&qrx->rx_pending);
  260. if (rxe == NULL)
  261. return NULL;
  262. ossl_list_rxe_remove(&qrx->rx_pending, rxe);
  263. return rxe;
  264. }
  265. /* Allocate a new RXE. */
  266. static RXE *qrx_alloc_rxe(size_t alloc_len)
  267. {
  268. RXE *rxe;
  269. if (alloc_len >= SIZE_MAX - sizeof(RXE))
  270. return NULL;
  271. rxe = OPENSSL_malloc(sizeof(RXE) + alloc_len);
  272. if (rxe == NULL)
  273. return NULL;
  274. ossl_list_rxe_init_elem(rxe);
  275. rxe->alloc_len = alloc_len;
  276. rxe->data_len = 0;
  277. rxe->refcount = 0;
  278. return rxe;
  279. }
  280. /*
  281. * Ensures there is at least one RXE in the RX free list, allocating a new entry
  282. * if necessary. The returned RXE is in the RX free list; it is not popped.
  283. *
  284. * alloc_len is a hint which may be used to determine the RXE size if allocation
  285. * is necessary. Returns NULL on allocation failure.
  286. */
  287. static RXE *qrx_ensure_free_rxe(OSSL_QRX *qrx, size_t alloc_len)
  288. {
  289. RXE *rxe;
  290. if (ossl_list_rxe_head(&qrx->rx_free) != NULL)
  291. return ossl_list_rxe_head(&qrx->rx_free);
  292. rxe = qrx_alloc_rxe(alloc_len);
  293. if (rxe == NULL)
  294. return NULL;
  295. ossl_list_rxe_insert_tail(&qrx->rx_free, rxe);
  296. return rxe;
  297. }
  298. /*
  299. * Resize the data buffer attached to an RXE to be n bytes in size. The address
  300. * of the RXE might change; the new address is returned, or NULL on failure, in
  301. * which case the original RXE remains valid.
  302. */
  303. static RXE *qrx_resize_rxe(RXE_LIST *rxl, RXE *rxe, size_t n)
  304. {
  305. RXE *rxe2, *p;
  306. /* Should never happen. */
  307. if (rxe == NULL)
  308. return NULL;
  309. if (n >= SIZE_MAX - sizeof(RXE))
  310. return NULL;
  311. /* Remove the item from the list to avoid accessing freed memory */
  312. p = ossl_list_rxe_prev(rxe);
  313. ossl_list_rxe_remove(rxl, rxe);
  314. /* Should never resize an RXE which has been handed out. */
  315. if (!ossl_assert(rxe->refcount == 0))
  316. return NULL;
  317. /*
  318. * NOTE: We do not clear old memory, although it does contain decrypted
  319. * data.
  320. */
  321. rxe2 = OPENSSL_realloc(rxe, sizeof(RXE) + n);
  322. if (rxe2 == NULL) {
  323. /* Resize failed, restore old allocation. */
  324. if (p == NULL)
  325. ossl_list_rxe_insert_head(rxl, rxe);
  326. else
  327. ossl_list_rxe_insert_after(rxl, p, rxe);
  328. return NULL;
  329. }
  330. if (p == NULL)
  331. ossl_list_rxe_insert_head(rxl, rxe2);
  332. else
  333. ossl_list_rxe_insert_after(rxl, p, rxe2);
  334. rxe2->alloc_len = n;
  335. return rxe2;
  336. }
  337. /*
  338. * Ensure the data buffer attached to an RXE is at least n bytes in size.
  339. * Returns NULL on failure.
  340. */
  341. static RXE *qrx_reserve_rxe(RXE_LIST *rxl,
  342. RXE *rxe, size_t n)
  343. {
  344. if (rxe->alloc_len >= n)
  345. return rxe;
  346. return qrx_resize_rxe(rxl, rxe, n);
  347. }
  348. /* Return a RXE handed out to the user back to our freelist. */
  349. static void qrx_recycle_rxe(OSSL_QRX *qrx, RXE *rxe)
  350. {
  351. /* RXE should not be in any list */
  352. assert(ossl_list_rxe_prev(rxe) == NULL && ossl_list_rxe_next(rxe) == NULL);
  353. rxe->pkt.hdr = NULL;
  354. rxe->pkt.peer = NULL;
  355. rxe->pkt.local = NULL;
  356. ossl_list_rxe_insert_tail(&qrx->rx_free, rxe);
  357. }
  358. /*
  359. * Given a pointer to a pointer pointing to a buffer and the size of that
  360. * buffer, copy the buffer into *prxe, expanding the RXE if necessary (its
  361. * pointer may change due to realloc). *pi is the offset in bytes to copy the
  362. * buffer to, and on success is updated to be the offset pointing after the
  363. * copied buffer. *pptr is updated to point to the new location of the buffer.
  364. */
  365. static int qrx_relocate_buffer(OSSL_QRX *qrx, RXE **prxe, size_t *pi,
  366. const unsigned char **pptr, size_t buf_len)
  367. {
  368. RXE *rxe;
  369. unsigned char *dst;
  370. if (!buf_len)
  371. return 1;
  372. if ((rxe = qrx_reserve_rxe(&qrx->rx_free, *prxe, *pi + buf_len)) == NULL)
  373. return 0;
  374. *prxe = rxe;
  375. dst = (unsigned char *)rxe_data(rxe) + *pi;
  376. memcpy(dst, *pptr, buf_len);
  377. *pi += buf_len;
  378. *pptr = dst;
  379. return 1;
  380. }
  381. static uint32_t qrx_determine_enc_level(const QUIC_PKT_HDR *hdr)
  382. {
  383. switch (hdr->type) {
  384. case QUIC_PKT_TYPE_INITIAL:
  385. return QUIC_ENC_LEVEL_INITIAL;
  386. case QUIC_PKT_TYPE_HANDSHAKE:
  387. return QUIC_ENC_LEVEL_HANDSHAKE;
  388. case QUIC_PKT_TYPE_0RTT:
  389. return QUIC_ENC_LEVEL_0RTT;
  390. case QUIC_PKT_TYPE_1RTT:
  391. return QUIC_ENC_LEVEL_1RTT;
  392. default:
  393. assert(0);
  394. case QUIC_PKT_TYPE_RETRY:
  395. case QUIC_PKT_TYPE_VERSION_NEG:
  396. return QUIC_ENC_LEVEL_INITIAL; /* not used */
  397. }
  398. }
  399. static uint32_t rxe_determine_pn_space(RXE *rxe)
  400. {
  401. uint32_t enc_level;
  402. enc_level = qrx_determine_enc_level(&rxe->hdr);
  403. return ossl_quic_enc_level_to_pn_space(enc_level);
  404. }
  405. static int qrx_validate_hdr_early(OSSL_QRX *qrx, RXE *rxe,
  406. const QUIC_CONN_ID *first_dcid)
  407. {
  408. /* Ensure version is what we want. */
  409. if (rxe->hdr.version != QUIC_VERSION_1
  410. && rxe->hdr.version != QUIC_VERSION_NONE)
  411. return 0;
  412. /* Clients should never receive 0-RTT packets. */
  413. if (rxe->hdr.type == QUIC_PKT_TYPE_0RTT)
  414. return 0;
  415. /* Version negotiation and retry packets must be the first packet. */
  416. if (first_dcid != NULL && !ossl_quic_pkt_type_can_share_dgram(rxe->hdr.type))
  417. return 0;
  418. /*
  419. * If this is not the first packet in a datagram, the destination connection
  420. * ID must match the one in that packet.
  421. */
  422. if (first_dcid != NULL) {
  423. if (!ossl_assert(first_dcid->id_len < QUIC_MAX_CONN_ID_LEN)
  424. || !ossl_quic_conn_id_eq(first_dcid,
  425. &rxe->hdr.dst_conn_id))
  426. return 0;
  427. }
  428. return 1;
  429. }
  430. /* Validate header and decode PN. */
  431. static int qrx_validate_hdr(OSSL_QRX *qrx, RXE *rxe)
  432. {
  433. int pn_space = rxe_determine_pn_space(rxe);
  434. if (!ossl_quic_wire_decode_pkt_hdr_pn(rxe->hdr.pn, rxe->hdr.pn_len,
  435. qrx->largest_pn[pn_space],
  436. &rxe->pn))
  437. return 0;
  438. return 1;
  439. }
  440. /* Late packet header validation. */
  441. static int qrx_validate_hdr_late(OSSL_QRX *qrx, RXE *rxe)
  442. {
  443. int pn_space = rxe_determine_pn_space(rxe);
  444. /*
  445. * Allow our user to decide whether to discard the packet before we try and
  446. * decrypt it.
  447. */
  448. if (qrx->validation_cb != NULL
  449. && !qrx->validation_cb(rxe->pn, pn_space, qrx->validation_cb_arg))
  450. return 0;
  451. return 1;
  452. }
  453. /*
  454. * Retrieves the correct cipher context for an EL and key phase. Writes the key
  455. * epoch number actually used for packet decryption to *rx_key_epoch.
  456. */
  457. static size_t qrx_get_cipher_ctx_idx(OSSL_QRX *qrx, OSSL_QRL_ENC_LEVEL *el,
  458. uint32_t enc_level,
  459. unsigned char key_phase_bit,
  460. uint64_t *rx_key_epoch,
  461. int *is_old_key)
  462. {
  463. size_t idx;
  464. *is_old_key = 0;
  465. if (enc_level != QUIC_ENC_LEVEL_1RTT) {
  466. *rx_key_epoch = 0;
  467. return 0;
  468. }
  469. if (!ossl_assert(key_phase_bit <= 1))
  470. return SIZE_MAX;
  471. /*
  472. * RFC 9001 requires that we not create timing channels which could reveal
  473. * the decrypted value of the Key Phase bit. We usually handle this by
  474. * keeping the cipher contexts for both the current and next key epochs
  475. * around, so that we just select a cipher context blindly using the key
  476. * phase bit, which is time-invariant.
  477. *
  478. * In the COOLDOWN state, we only have one keyslot/cipher context. RFC 9001
  479. * suggests an implementation strategy to avoid creating a timing channel in
  480. * this case:
  481. *
  482. * Endpoints can use randomized packet protection keys in place of
  483. * discarded keys when key updates are not yet permitted.
  484. *
  485. * Rather than use a randomised key, we simply use our existing key as it
  486. * will fail AEAD verification anyway. This avoids the need to keep around a
  487. * dedicated garbage key.
  488. *
  489. * Note: Accessing different cipher contexts is technically not
  490. * timing-channel safe due to microarchitectural side channels, but this is
  491. * the best we can reasonably do and appears to be directly suggested by the
  492. * RFC.
  493. */
  494. idx = (el->state == QRL_EL_STATE_PROV_COOLDOWN ? el->key_epoch & 1
  495. : key_phase_bit);
  496. /*
  497. * We also need to determine the key epoch number which this index
  498. * corresponds to. This is so we can report the key epoch number in the
  499. * OSSL_QRX_PKT structure, which callers need to validate whether it was OK
  500. * for a packet to be sent using a given key epoch's keys.
  501. */
  502. switch (el->state) {
  503. case QRL_EL_STATE_PROV_NORMAL:
  504. /*
  505. * If we are in the NORMAL state, usually the KP bit will match the LSB
  506. * of our key epoch, meaning no new key update is being signalled. If it
  507. * does not match, this means the packet (purports to) belong to
  508. * the next key epoch.
  509. *
  510. * IMPORTANT: The AEAD tag has not been verified yet when this function
  511. * is called, so this code must be timing-channel safe, hence use of
  512. * XOR. Moreover, the value output below is not yet authenticated.
  513. */
  514. *rx_key_epoch
  515. = el->key_epoch + ((el->key_epoch & 1) ^ (uint64_t)key_phase_bit);
  516. break;
  517. case QRL_EL_STATE_PROV_UPDATING:
  518. /*
  519. * If we are in the UPDATING state, usually the KP bit will match the
  520. * LSB of our key epoch. If it does not match, this means that the
  521. * packet (purports to) belong to the previous key epoch.
  522. *
  523. * As above, must be timing-channel safe.
  524. */
  525. *is_old_key = (el->key_epoch & 1) ^ (uint64_t)key_phase_bit;
  526. *rx_key_epoch = el->key_epoch - (uint64_t)*is_old_key;
  527. break;
  528. case QRL_EL_STATE_PROV_COOLDOWN:
  529. /*
  530. * If we are in COOLDOWN, there is only one key epoch we can possibly
  531. * decrypt with, so just try that. If AEAD decryption fails, the
  532. * value we output here isn't used anyway.
  533. */
  534. *rx_key_epoch = el->key_epoch;
  535. break;
  536. }
  537. return idx;
  538. }
  539. /*
  540. * Tries to decrypt a packet payload.
  541. *
  542. * Returns 1 on success or 0 on failure (which is permanent). The payload is
  543. * decrypted from src and written to dst. The buffer dst must be of at least
  544. * src_len bytes in length. The actual length of the output in bytes is written
  545. * to *dec_len on success, which will always be equal to or less than (usually
  546. * less than) src_len.
  547. */
  548. static int qrx_decrypt_pkt_body(OSSL_QRX *qrx, unsigned char *dst,
  549. const unsigned char *src,
  550. size_t src_len, size_t *dec_len,
  551. const unsigned char *aad, size_t aad_len,
  552. QUIC_PN pn, uint32_t enc_level,
  553. unsigned char key_phase_bit,
  554. uint64_t *rx_key_epoch)
  555. {
  556. int l = 0, l2 = 0, is_old_key, nonce_len;
  557. unsigned char nonce[EVP_MAX_IV_LENGTH];
  558. size_t i, cctx_idx;
  559. OSSL_QRL_ENC_LEVEL *el = ossl_qrl_enc_level_set_get(&qrx->el_set,
  560. enc_level, 1);
  561. EVP_CIPHER_CTX *cctx;
  562. if (src_len > INT_MAX || aad_len > INT_MAX)
  563. return 0;
  564. /* We should not have been called if we do not have key material. */
  565. if (!ossl_assert(el != NULL))
  566. return 0;
  567. if (el->tag_len >= src_len)
  568. return 0;
  569. /*
  570. * If we have failed to authenticate a certain number of ciphertexts, refuse
  571. * to decrypt any more ciphertexts.
  572. */
  573. if (qrx->forged_pkt_count >= ossl_qrl_get_suite_max_forged_pkt(el->suite_id))
  574. return 0;
  575. cctx_idx = qrx_get_cipher_ctx_idx(qrx, el, enc_level, key_phase_bit,
  576. rx_key_epoch, &is_old_key);
  577. if (!ossl_assert(cctx_idx < OSSL_NELEM(el->cctx)))
  578. return 0;
  579. if (is_old_key && pn >= qrx->cur_epoch_start_pn)
  580. /*
  581. * RFC 9001 s. 5.5: Once an endpoint successfully receives a packet with
  582. * a given PN, it MUST discard all packets in the same PN space with
  583. * higher PNs if they cannot be successfully unprotected with the same
  584. * key, or -- if there is a key update -- a subsequent packet protection
  585. * key.
  586. *
  587. * In other words, once a PN x triggers a KU, it is invalid for us to
  588. * receive a packet with a newer PN y (y > x) using the old keys.
  589. */
  590. return 0;
  591. cctx = el->cctx[cctx_idx];
  592. /* Construct nonce (nonce=IV ^ PN). */
  593. nonce_len = EVP_CIPHER_CTX_get_iv_length(cctx);
  594. if (!ossl_assert(nonce_len >= (int)sizeof(QUIC_PN)))
  595. return 0;
  596. memcpy(nonce, el->iv[cctx_idx], nonce_len);
  597. for (i = 0; i < sizeof(QUIC_PN); ++i)
  598. nonce[nonce_len - i - 1] ^= (unsigned char)(pn >> (i * 8));
  599. /* type and key will already have been setup; feed the IV. */
  600. if (EVP_CipherInit_ex(cctx, NULL,
  601. NULL, NULL, nonce, /*enc=*/0) != 1)
  602. return 0;
  603. /* Feed the AEAD tag we got so the cipher can validate it. */
  604. if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_SET_TAG,
  605. el->tag_len,
  606. (unsigned char *)src + src_len - el->tag_len) != 1)
  607. return 0;
  608. /* Feed AAD data. */
  609. if (EVP_CipherUpdate(cctx, NULL, &l, aad, aad_len) != 1)
  610. return 0;
  611. /* Feed encrypted packet body. */
  612. if (EVP_CipherUpdate(cctx, dst, &l, src, src_len - el->tag_len) != 1)
  613. return 0;
  614. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  615. /*
  616. * Throw away what we just decrypted and just use the ciphertext instead
  617. * (which should be unencrypted)
  618. */
  619. memcpy(dst, src, l);
  620. /* Pretend to authenticate the tag but ignore it */
  621. if (EVP_CipherFinal_ex(cctx, NULL, &l2) != 1) {
  622. /* We don't care */
  623. }
  624. #else
  625. /* Ensure authentication succeeded. */
  626. if (EVP_CipherFinal_ex(cctx, NULL, &l2) != 1) {
  627. /* Authentication failed, increment failed auth counter. */
  628. ++qrx->forged_pkt_count;
  629. return 0;
  630. }
  631. #endif
  632. *dec_len = l;
  633. return 1;
  634. }
  635. static ossl_inline void ignore_res(int x)
  636. {
  637. /* No-op. */
  638. }
  639. static void qrx_key_update_initiated(OSSL_QRX *qrx, QUIC_PN pn)
  640. {
  641. if (!ossl_qrl_enc_level_set_key_update(&qrx->el_set, QUIC_ENC_LEVEL_1RTT))
  642. /* We are already in RXKU, so we don't call the callback again. */
  643. return;
  644. qrx->cur_epoch_start_pn = pn;
  645. if (qrx->key_update_cb != NULL)
  646. qrx->key_update_cb(pn, qrx->key_update_cb_arg);
  647. }
  648. /* Process a single packet in a datagram. */
  649. static int qrx_process_pkt(OSSL_QRX *qrx, QUIC_URXE *urxe,
  650. PACKET *pkt, size_t pkt_idx,
  651. QUIC_CONN_ID *first_dcid,
  652. size_t datagram_len)
  653. {
  654. RXE *rxe;
  655. const unsigned char *eop = NULL;
  656. size_t i, aad_len = 0, dec_len = 0;
  657. PACKET orig_pkt = *pkt;
  658. const unsigned char *sop = PACKET_data(pkt);
  659. unsigned char *dst;
  660. char need_second_decode = 0, already_processed = 0;
  661. QUIC_PKT_HDR_PTRS ptrs;
  662. uint32_t pn_space, enc_level;
  663. OSSL_QRL_ENC_LEVEL *el = NULL;
  664. uint64_t rx_key_epoch = UINT64_MAX;
  665. /*
  666. * Get a free RXE. If we need to allocate a new one, use the packet length
  667. * as a good ballpark figure.
  668. */
  669. rxe = qrx_ensure_free_rxe(qrx, PACKET_remaining(pkt));
  670. if (rxe == NULL)
  671. return 0;
  672. /* Have we already processed this packet? */
  673. if (pkt_is_marked(&urxe->processed, pkt_idx))
  674. already_processed = 1;
  675. /*
  676. * Decode the header into the RXE structure. We first decrypt and read the
  677. * unprotected part of the packet header (unless we already removed header
  678. * protection, in which case we decode all of it).
  679. */
  680. need_second_decode = !pkt_is_marked(&urxe->hpr_removed, pkt_idx);
  681. if (!ossl_quic_wire_decode_pkt_hdr(pkt,
  682. qrx->short_conn_id_len,
  683. need_second_decode, 0, &rxe->hdr, &ptrs))
  684. goto malformed;
  685. /*
  686. * Our successful decode above included an intelligible length and the
  687. * PACKET is now pointing to the end of the QUIC packet.
  688. */
  689. eop = PACKET_data(pkt);
  690. /*
  691. * Make a note of the first packet's DCID so we can later ensure the
  692. * destination connection IDs of all packets in a datagram match.
  693. */
  694. if (pkt_idx == 0)
  695. *first_dcid = rxe->hdr.dst_conn_id;
  696. /*
  697. * Early header validation. Since we now know the packet length, we can also
  698. * now skip over it if we already processed it.
  699. */
  700. if (already_processed
  701. || !qrx_validate_hdr_early(qrx, rxe, pkt_idx == 0 ? NULL : first_dcid))
  702. /*
  703. * Already processed packets are handled identically to malformed
  704. * packets; i.e., they are ignored.
  705. */
  706. goto malformed;
  707. if (!ossl_quic_pkt_type_is_encrypted(rxe->hdr.type)) {
  708. /*
  709. * Version negotiation and retry packets are a special case. They do not
  710. * contain a payload which needs decrypting and have no header
  711. * protection.
  712. */
  713. /* Just copy the payload from the URXE to the RXE. */
  714. if ((rxe = qrx_reserve_rxe(&qrx->rx_free, rxe, rxe->hdr.len)) == NULL)
  715. /*
  716. * Allocation failure. EOP will be pointing to the end of the
  717. * datagram so processing of this datagram will end here.
  718. */
  719. goto malformed;
  720. /* We are now committed to returning the packet. */
  721. memcpy(rxe_data(rxe), rxe->hdr.data, rxe->hdr.len);
  722. pkt_mark(&urxe->processed, pkt_idx);
  723. rxe->hdr.data = rxe_data(rxe);
  724. rxe->pn = QUIC_PN_INVALID;
  725. rxe->data_len = rxe->hdr.len;
  726. rxe->datagram_len = datagram_len;
  727. rxe->key_epoch = 0;
  728. rxe->peer = urxe->peer;
  729. rxe->local = urxe->local;
  730. rxe->time = urxe->time;
  731. rxe->datagram_id = urxe->datagram_id;
  732. /* Move RXE to pending. */
  733. ossl_list_rxe_remove(&qrx->rx_free, rxe);
  734. ossl_list_rxe_insert_tail(&qrx->rx_pending, rxe);
  735. return 0; /* success, did not defer */
  736. }
  737. /* Determine encryption level of packet. */
  738. enc_level = qrx_determine_enc_level(&rxe->hdr);
  739. /* If we do not have keying material for this encryption level yet, defer. */
  740. switch (ossl_qrl_enc_level_set_have_el(&qrx->el_set, enc_level)) {
  741. case 1:
  742. /* We have keys. */
  743. if (enc_level == QUIC_ENC_LEVEL_1RTT && !qrx->allow_1rtt)
  744. /*
  745. * But we cannot process 1-RTT packets until the handshake is
  746. * completed (RFC 9000 s. 5.7).
  747. */
  748. goto cannot_decrypt;
  749. break;
  750. case 0:
  751. /* No keys yet. */
  752. goto cannot_decrypt;
  753. default:
  754. /* We already discarded keys for this EL, we will never process this.*/
  755. goto malformed;
  756. }
  757. /*
  758. * We will copy any token included in the packet to the start of our RXE
  759. * data buffer (so that we don't reference the URXE buffer any more and can
  760. * recycle it). Track our position in the RXE buffer by index instead of
  761. * pointer as the pointer may change as reallocs occur.
  762. */
  763. i = 0;
  764. /*
  765. * rxe->hdr.data is now pointing at the (encrypted) packet payload. rxe->hdr
  766. * also has fields pointing into the PACKET buffer which will be going away
  767. * soon (the URXE will be reused for another incoming packet).
  768. *
  769. * Firstly, relocate some of these fields into the RXE as needed.
  770. *
  771. * Relocate token buffer and fix pointer.
  772. */
  773. if (rxe->hdr.type == QUIC_PKT_TYPE_INITIAL) {
  774. const unsigned char *token = rxe->hdr.token;
  775. /*
  776. * This may change the value of rxe and change the value of the token
  777. * pointer as well. So we must make a temporary copy of the pointer to
  778. * the token, and then copy it back into the new location of the rxe
  779. */
  780. if (!qrx_relocate_buffer(qrx, &rxe, &i, &token, rxe->hdr.token_len))
  781. goto malformed;
  782. rxe->hdr.token = token;
  783. }
  784. /* Now remove header protection. */
  785. *pkt = orig_pkt;
  786. el = ossl_qrl_enc_level_set_get(&qrx->el_set, enc_level, 1);
  787. assert(el != NULL); /* Already checked above */
  788. if (need_second_decode) {
  789. if (!ossl_quic_hdr_protector_decrypt(&el->hpr, &ptrs))
  790. goto malformed;
  791. /*
  792. * We have removed header protection, so don't attempt to do it again if
  793. * the packet gets deferred and processed again.
  794. */
  795. pkt_mark(&urxe->hpr_removed, pkt_idx);
  796. /* Decode the now unprotected header. */
  797. if (ossl_quic_wire_decode_pkt_hdr(pkt, qrx->short_conn_id_len,
  798. 0, 0, &rxe->hdr, NULL) != 1)
  799. goto malformed;
  800. }
  801. /* Validate header and decode PN. */
  802. if (!qrx_validate_hdr(qrx, rxe))
  803. goto malformed;
  804. if (qrx->msg_callback != NULL)
  805. qrx->msg_callback(0, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_PACKET, sop,
  806. eop - sop - rxe->hdr.len, qrx->msg_callback_ssl,
  807. qrx->msg_callback_arg);
  808. /*
  809. * The AAD data is the entire (unprotected) packet header including the PN.
  810. * The packet header has been unprotected in place, so we can just reuse the
  811. * PACKET buffer. The header ends where the payload begins.
  812. */
  813. aad_len = rxe->hdr.data - sop;
  814. /* Ensure the RXE buffer size is adequate for our payload. */
  815. if ((rxe = qrx_reserve_rxe(&qrx->rx_free, rxe, rxe->hdr.len + i)) == NULL) {
  816. /*
  817. * Allocation failure, treat as malformed and do not bother processing
  818. * any further packets in the datagram as they are likely to also
  819. * encounter allocation failures.
  820. */
  821. eop = NULL;
  822. goto malformed;
  823. }
  824. /*
  825. * We decrypt the packet body to immediately after the token at the start of
  826. * the RXE buffer (where present).
  827. *
  828. * Do the decryption from the PACKET (which points into URXE memory) to our
  829. * RXE payload (single-copy decryption), then fixup the pointers in the
  830. * header to point to our new buffer.
  831. *
  832. * If decryption fails this is considered a permanent error; we defer
  833. * packets we don't yet have decryption keys for above, so if this fails,
  834. * something has gone wrong with the handshake process or a packet has been
  835. * corrupted.
  836. */
  837. dst = (unsigned char *)rxe_data(rxe) + i;
  838. if (!qrx_decrypt_pkt_body(qrx, dst, rxe->hdr.data, rxe->hdr.len,
  839. &dec_len, sop, aad_len, rxe->pn, enc_level,
  840. rxe->hdr.key_phase, &rx_key_epoch))
  841. goto malformed;
  842. /*
  843. * -----------------------------------------------------
  844. * IMPORTANT: ANYTHING ABOVE THIS LINE IS UNVERIFIED
  845. * AND MUST BE TIMING-CHANNEL SAFE.
  846. * -----------------------------------------------------
  847. *
  848. * At this point, we have successfully authenticated the AEAD tag and no
  849. * longer need to worry about exposing the PN, PN length or Key Phase bit in
  850. * timing channels. Invoke any configured validation callback to allow for
  851. * rejection of duplicate PNs.
  852. */
  853. if (!qrx_validate_hdr_late(qrx, rxe))
  854. goto malformed;
  855. /* Check for a Key Phase bit differing from our expectation. */
  856. if (rxe->hdr.type == QUIC_PKT_TYPE_1RTT
  857. && rxe->hdr.key_phase != (el->key_epoch & 1))
  858. qrx_key_update_initiated(qrx, rxe->pn);
  859. /*
  860. * We have now successfully decrypted the packet payload. If there are
  861. * additional packets in the datagram, it is possible we will fail to
  862. * decrypt them and need to defer them until we have some key material we
  863. * don't currently possess. If this happens, the URXE will be moved to the
  864. * deferred queue. Since a URXE corresponds to one datagram, which may
  865. * contain multiple packets, we must ensure any packets we have already
  866. * processed in the URXE are not processed again (this is an RFC
  867. * requirement). We do this by marking the nth packet in the datagram as
  868. * processed.
  869. *
  870. * We are now committed to returning this decrypted packet to the user,
  871. * meaning we now consider the packet processed and must mark it
  872. * accordingly.
  873. */
  874. pkt_mark(&urxe->processed, pkt_idx);
  875. /*
  876. * Update header to point to the decrypted buffer, which may be shorter
  877. * due to AEAD tags, block padding, etc.
  878. */
  879. rxe->hdr.data = dst;
  880. rxe->hdr.len = dec_len;
  881. rxe->data_len = dec_len;
  882. rxe->datagram_len = datagram_len;
  883. rxe->key_epoch = rx_key_epoch;
  884. /* We processed the PN successfully, so update largest processed PN. */
  885. pn_space = rxe_determine_pn_space(rxe);
  886. if (rxe->pn > qrx->largest_pn[pn_space])
  887. qrx->largest_pn[pn_space] = rxe->pn;
  888. /* Copy across network addresses and RX time from URXE to RXE. */
  889. rxe->peer = urxe->peer;
  890. rxe->local = urxe->local;
  891. rxe->time = urxe->time;
  892. rxe->datagram_id = urxe->datagram_id;
  893. /* Move RXE to pending. */
  894. ossl_list_rxe_remove(&qrx->rx_free, rxe);
  895. ossl_list_rxe_insert_tail(&qrx->rx_pending, rxe);
  896. return 0; /* success, did not defer; not distinguished from failure */
  897. cannot_decrypt:
  898. /*
  899. * We cannot process this packet right now (but might be able to later). We
  900. * MUST attempt to process any other packets in the datagram, so defer it
  901. * and skip over it.
  902. */
  903. assert(eop != NULL && eop >= PACKET_data(pkt));
  904. /*
  905. * We don't care if this fails as it will just result in the packet being at
  906. * the end of the datagram buffer.
  907. */
  908. ignore_res(PACKET_forward(pkt, eop - PACKET_data(pkt)));
  909. return 1; /* deferred */
  910. malformed:
  911. if (eop != NULL) {
  912. /*
  913. * This packet cannot be processed and will never be processable. We
  914. * were at least able to decode its header and determine its length, so
  915. * we can skip over it and try to process any subsequent packets in the
  916. * datagram.
  917. *
  918. * Mark as processed as an optimization.
  919. */
  920. assert(eop >= PACKET_data(pkt));
  921. pkt_mark(&urxe->processed, pkt_idx);
  922. /* We don't care if this fails (see above) */
  923. ignore_res(PACKET_forward(pkt, eop - PACKET_data(pkt)));
  924. } else {
  925. /*
  926. * This packet cannot be processed and will never be processable.
  927. * Because even its header is not intelligible, we cannot examine any
  928. * further packets in the datagram because its length cannot be
  929. * discerned.
  930. *
  931. * Advance over the entire remainder of the datagram, and mark it as
  932. * processed as an optimization.
  933. */
  934. pkt_mark(&urxe->processed, pkt_idx);
  935. /* We don't care if this fails (see above) */
  936. ignore_res(PACKET_forward(pkt, PACKET_remaining(pkt)));
  937. }
  938. return 0; /* failure, did not defer; not distinguished from success */
  939. }
  940. /* Process a datagram which was received. */
  941. static int qrx_process_datagram(OSSL_QRX *qrx, QUIC_URXE *e,
  942. const unsigned char *data,
  943. size_t data_len)
  944. {
  945. int have_deferred = 0;
  946. PACKET pkt;
  947. size_t pkt_idx = 0;
  948. QUIC_CONN_ID first_dcid = { 255 };
  949. qrx->bytes_received += data_len;
  950. if (!PACKET_buf_init(&pkt, data, data_len))
  951. return 0;
  952. for (; PACKET_remaining(&pkt) > 0; ++pkt_idx) {
  953. /*
  954. * A packet smaller than the minimum possible QUIC packet size is not
  955. * considered valid. We also ignore more than a certain number of
  956. * packets within the same datagram.
  957. */
  958. if (PACKET_remaining(&pkt) < QUIC_MIN_VALID_PKT_LEN
  959. || pkt_idx >= QUIC_MAX_PKT_PER_URXE)
  960. break;
  961. /*
  962. * We note whether packet processing resulted in a deferral since
  963. * this means we need to move the URXE to the deferred list rather
  964. * than the free list after we're finished dealing with it for now.
  965. *
  966. * However, we don't otherwise care here whether processing succeeded or
  967. * failed, as the RFC says even if a packet in a datagram is malformed,
  968. * we should still try to process any packets following it.
  969. *
  970. * In the case where the packet is so malformed we can't determine its
  971. * length, qrx_process_pkt will take care of advancing to the end of
  972. * the packet, so we will exit the loop automatically in this case.
  973. */
  974. if (qrx_process_pkt(qrx, e, &pkt, pkt_idx, &first_dcid, data_len))
  975. have_deferred = 1;
  976. }
  977. /* Only report whether there were any deferrals. */
  978. return have_deferred;
  979. }
  980. /* Process a single pending URXE. */
  981. static int qrx_process_one_urxe(OSSL_QRX *qrx, QUIC_URXE *e)
  982. {
  983. int was_deferred;
  984. /* The next URXE we process should be at the head of the pending list. */
  985. if (!ossl_assert(e == ossl_list_urxe_head(&qrx->urx_pending)))
  986. return 0;
  987. /*
  988. * Attempt to process the datagram. The return value indicates only if
  989. * processing of the datagram was deferred. If we failed to process the
  990. * datagram, we do not attempt to process it again and silently eat the
  991. * error.
  992. */
  993. was_deferred = qrx_process_datagram(qrx, e, ossl_quic_urxe_data(e),
  994. e->data_len);
  995. /*
  996. * Remove the URXE from the pending list and return it to
  997. * either the free or deferred list.
  998. */
  999. ossl_list_urxe_remove(&qrx->urx_pending, e);
  1000. if (was_deferred > 0 &&
  1001. (e->deferred || qrx->num_deferred < qrx->max_deferred)) {
  1002. ossl_list_urxe_insert_tail(&qrx->urx_deferred, e);
  1003. if (!e->deferred) {
  1004. e->deferred = 1;
  1005. ++qrx->num_deferred;
  1006. }
  1007. } else {
  1008. if (e->deferred) {
  1009. e->deferred = 0;
  1010. --qrx->num_deferred;
  1011. }
  1012. ossl_quic_demux_release_urxe(qrx->demux, e);
  1013. }
  1014. return 1;
  1015. }
  1016. /* Process any pending URXEs to generate pending RXEs. */
  1017. static int qrx_process_pending_urxl(OSSL_QRX *qrx)
  1018. {
  1019. QUIC_URXE *e;
  1020. while ((e = ossl_list_urxe_head(&qrx->urx_pending)) != NULL)
  1021. if (!qrx_process_one_urxe(qrx, e))
  1022. return 0;
  1023. return 1;
  1024. }
  1025. int ossl_qrx_read_pkt(OSSL_QRX *qrx, OSSL_QRX_PKT **ppkt)
  1026. {
  1027. RXE *rxe;
  1028. if (!ossl_qrx_processed_read_pending(qrx)) {
  1029. if (!qrx_process_pending_urxl(qrx))
  1030. return 0;
  1031. if (!ossl_qrx_processed_read_pending(qrx))
  1032. return 0;
  1033. }
  1034. rxe = qrx_pop_pending_rxe(qrx);
  1035. if (!ossl_assert(rxe != NULL))
  1036. return 0;
  1037. assert(rxe->refcount == 0);
  1038. rxe->refcount = 1;
  1039. rxe->pkt.hdr = &rxe->hdr;
  1040. rxe->pkt.pn = rxe->pn;
  1041. rxe->pkt.time = rxe->time;
  1042. rxe->pkt.datagram_len = rxe->datagram_len;
  1043. rxe->pkt.peer
  1044. = BIO_ADDR_family(&rxe->peer) != AF_UNSPEC ? &rxe->peer : NULL;
  1045. rxe->pkt.local
  1046. = BIO_ADDR_family(&rxe->local) != AF_UNSPEC ? &rxe->local : NULL;
  1047. rxe->pkt.key_epoch = rxe->key_epoch;
  1048. rxe->pkt.datagram_id = rxe->datagram_id;
  1049. rxe->pkt.qrx = qrx;
  1050. *ppkt = &rxe->pkt;
  1051. return 1;
  1052. }
  1053. void ossl_qrx_pkt_release(OSSL_QRX_PKT *pkt)
  1054. {
  1055. RXE *rxe;
  1056. if (pkt == NULL)
  1057. return;
  1058. rxe = (RXE *)pkt;
  1059. assert(rxe->refcount > 0);
  1060. if (--rxe->refcount == 0)
  1061. qrx_recycle_rxe(pkt->qrx, rxe);
  1062. }
  1063. void ossl_qrx_pkt_up_ref(OSSL_QRX_PKT *pkt)
  1064. {
  1065. RXE *rxe = (RXE *)pkt;
  1066. assert(rxe->refcount > 0);
  1067. ++rxe->refcount;
  1068. }
  1069. uint64_t ossl_qrx_get_bytes_received(OSSL_QRX *qrx, int clear)
  1070. {
  1071. uint64_t v = qrx->bytes_received;
  1072. if (clear)
  1073. qrx->bytes_received = 0;
  1074. return v;
  1075. }
  1076. int ossl_qrx_set_late_validation_cb(OSSL_QRX *qrx,
  1077. ossl_qrx_late_validation_cb *cb,
  1078. void *cb_arg)
  1079. {
  1080. qrx->validation_cb = cb;
  1081. qrx->validation_cb_arg = cb_arg;
  1082. return 1;
  1083. }
  1084. int ossl_qrx_set_key_update_cb(OSSL_QRX *qrx,
  1085. ossl_qrx_key_update_cb *cb,
  1086. void *cb_arg)
  1087. {
  1088. qrx->key_update_cb = cb;
  1089. qrx->key_update_cb_arg = cb_arg;
  1090. return 1;
  1091. }
  1092. uint64_t ossl_qrx_get_key_epoch(OSSL_QRX *qrx)
  1093. {
  1094. OSSL_QRL_ENC_LEVEL *el = ossl_qrl_enc_level_set_get(&qrx->el_set,
  1095. QUIC_ENC_LEVEL_1RTT, 1);
  1096. return el == NULL ? UINT64_MAX : el->key_epoch;
  1097. }
  1098. int ossl_qrx_key_update_timeout(OSSL_QRX *qrx, int normal)
  1099. {
  1100. OSSL_QRL_ENC_LEVEL *el = ossl_qrl_enc_level_set_get(&qrx->el_set,
  1101. QUIC_ENC_LEVEL_1RTT, 1);
  1102. if (el == NULL)
  1103. return 0;
  1104. if (el->state == QRL_EL_STATE_PROV_UPDATING
  1105. && !ossl_qrl_enc_level_set_key_update_done(&qrx->el_set,
  1106. QUIC_ENC_LEVEL_1RTT))
  1107. return 0;
  1108. if (normal && el->state == QRL_EL_STATE_PROV_COOLDOWN
  1109. && !ossl_qrl_enc_level_set_key_cooldown_done(&qrx->el_set,
  1110. QUIC_ENC_LEVEL_1RTT))
  1111. return 0;
  1112. return 1;
  1113. }
  1114. uint64_t ossl_qrx_get_cur_forged_pkt_count(OSSL_QRX *qrx)
  1115. {
  1116. return qrx->forged_pkt_count;
  1117. }
  1118. uint64_t ossl_qrx_get_max_forged_pkt_count(OSSL_QRX *qrx,
  1119. uint32_t enc_level)
  1120. {
  1121. OSSL_QRL_ENC_LEVEL *el = ossl_qrl_enc_level_set_get(&qrx->el_set,
  1122. enc_level, 1);
  1123. return el == NULL ? UINT64_MAX
  1124. : ossl_qrl_get_suite_max_forged_pkt(el->suite_id);
  1125. }
  1126. void ossl_qrx_allow_1rtt_processing(OSSL_QRX *qrx)
  1127. {
  1128. if (qrx->allow_1rtt)
  1129. return;
  1130. qrx->allow_1rtt = 1;
  1131. qrx_requeue_deferred(qrx);
  1132. }
  1133. void ossl_qrx_set_msg_callback(OSSL_QRX *qrx, ossl_msg_cb msg_callback,
  1134. SSL *msg_callback_ssl)
  1135. {
  1136. qrx->msg_callback = msg_callback;
  1137. qrx->msg_callback_ssl = msg_callback_ssl;
  1138. }
  1139. void ossl_qrx_set_msg_callback_arg(OSSL_QRX *qrx, void *msg_callback_arg)
  1140. {
  1141. qrx->msg_callback_arg = msg_callback_arg;
  1142. }