quic_record_tx.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright 2022-2025 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_record_tx.h"
  10. #include "internal/qlog_event_helpers.h"
  11. #include "internal/bio_addr.h"
  12. #include "internal/common.h"
  13. #include "quic_record_shared.h"
  14. #include "internal/list.h"
  15. #include "../ssl_local.h"
  16. /*
  17. * TXE
  18. * ===
  19. * Encrypted packets awaiting transmission are kept in TX Entries (TXEs), which
  20. * are queued in linked lists just like TXEs.
  21. */
  22. typedef struct txe_st TXE;
  23. struct txe_st {
  24. OSSL_LIST_MEMBER(txe, TXE);
  25. size_t data_len, alloc_len;
  26. /*
  27. * Destination and local addresses, as applicable. Both of these are only
  28. * used if the family is not AF_UNSPEC.
  29. */
  30. BIO_ADDR peer, local;
  31. /*
  32. * alloc_len allocated bytes (of which data_len bytes are valid) follow this
  33. * structure.
  34. */
  35. };
  36. DEFINE_LIST_OF(txe, TXE);
  37. typedef OSSL_LIST(txe) TXE_LIST;
  38. static ossl_inline unsigned char *txe_data(const TXE *e)
  39. {
  40. return (unsigned char *)(e + 1);
  41. }
  42. /*
  43. * QTX
  44. * ===
  45. */
  46. struct ossl_qtx_st {
  47. OSSL_LIB_CTX *libctx;
  48. const char *propq;
  49. /* Per encryption-level state. */
  50. OSSL_QRL_ENC_LEVEL_SET el_set;
  51. /* TX BIO. */
  52. BIO *bio;
  53. /* QLOG instance retrieval callback if in use, or NULL. */
  54. QLOG *(*get_qlog_cb)(void *arg);
  55. void *get_qlog_cb_arg;
  56. /* TX maximum datagram payload length. */
  57. size_t mdpl;
  58. /*
  59. * List of TXEs which are not currently in use. These are moved to the
  60. * pending list (possibly via tx_cons first) as they are filled.
  61. */
  62. TXE_LIST free;
  63. /*
  64. * List of TXEs which are filled with completed datagrams ready to be
  65. * transmitted.
  66. */
  67. TXE_LIST pending;
  68. size_t pending_count; /* items in list */
  69. size_t pending_bytes; /* sum(txe->data_len) in pending */
  70. /*
  71. * TXE which is under construction for coalescing purposes, if any.
  72. * This TXE is neither on the free nor pending list. Once the datagram
  73. * is completed, it is moved to the pending list.
  74. */
  75. TXE *cons;
  76. size_t cons_count; /* num packets */
  77. /*
  78. * Number of packets transmitted in this key epoch. Used to enforce AEAD
  79. * confidentiality limit.
  80. */
  81. uint64_t epoch_pkt_count;
  82. /* Datagram counter. Increases monotonically per datagram (not per packet). */
  83. uint64_t datagram_count;
  84. ossl_mutate_packet_cb mutatecb;
  85. ossl_finish_mutate_cb finishmutatecb;
  86. void *mutatearg;
  87. /* Message callback related arguments */
  88. ossl_msg_cb msg_callback;
  89. void *msg_callback_arg;
  90. SSL *msg_callback_ssl;
  91. };
  92. /* Instantiates a new QTX. */
  93. OSSL_QTX *ossl_qtx_new(const OSSL_QTX_ARGS *args)
  94. {
  95. OSSL_QTX *qtx;
  96. if (args->mdpl < QUIC_MIN_INITIAL_DGRAM_LEN)
  97. return 0;
  98. qtx = OPENSSL_zalloc(sizeof(OSSL_QTX));
  99. if (qtx == NULL)
  100. return 0;
  101. qtx->libctx = args->libctx;
  102. qtx->propq = args->propq;
  103. qtx->bio = args->bio;
  104. qtx->mdpl = args->mdpl;
  105. qtx->get_qlog_cb = args->get_qlog_cb;
  106. qtx->get_qlog_cb_arg = args->get_qlog_cb_arg;
  107. return qtx;
  108. }
  109. static void qtx_cleanup_txl(TXE_LIST *l)
  110. {
  111. TXE *e, *enext;
  112. for (e = ossl_list_txe_head(l); e != NULL; e = enext) {
  113. enext = ossl_list_txe_next(e);
  114. OPENSSL_free(e);
  115. }
  116. }
  117. /* Frees the QTX. */
  118. void ossl_qtx_free(OSSL_QTX *qtx)
  119. {
  120. uint32_t i;
  121. if (qtx == NULL)
  122. return;
  123. /* Free TXE queue data. */
  124. qtx_cleanup_txl(&qtx->pending);
  125. qtx_cleanup_txl(&qtx->free);
  126. OPENSSL_free(qtx->cons);
  127. /* Drop keying material and crypto resources. */
  128. for (i = 0; i < QUIC_ENC_LEVEL_NUM; ++i)
  129. ossl_qrl_enc_level_set_discard(&qtx->el_set, i);
  130. OPENSSL_free(qtx);
  131. }
  132. /* Set mutator callbacks for test framework support */
  133. void ossl_qtx_set_mutator(OSSL_QTX *qtx, ossl_mutate_packet_cb mutatecb,
  134. ossl_finish_mutate_cb finishmutatecb, void *mutatearg)
  135. {
  136. qtx->mutatecb = mutatecb;
  137. qtx->finishmutatecb = finishmutatecb;
  138. qtx->mutatearg = mutatearg;
  139. }
  140. void ossl_qtx_set_qlog_cb(OSSL_QTX *qtx, QLOG *(*get_qlog_cb)(void *arg),
  141. void *get_qlog_cb_arg)
  142. {
  143. qtx->get_qlog_cb = get_qlog_cb;
  144. qtx->get_qlog_cb_arg = get_qlog_cb_arg;
  145. }
  146. int ossl_qtx_provide_secret(OSSL_QTX *qtx,
  147. uint32_t enc_level,
  148. uint32_t suite_id,
  149. EVP_MD *md,
  150. const unsigned char *secret,
  151. size_t secret_len)
  152. {
  153. if (enc_level >= QUIC_ENC_LEVEL_NUM)
  154. return 0;
  155. return ossl_qrl_enc_level_set_provide_secret(&qtx->el_set,
  156. qtx->libctx,
  157. qtx->propq,
  158. enc_level,
  159. suite_id,
  160. md,
  161. secret,
  162. secret_len,
  163. 0,
  164. /*is_tx=*/1);
  165. }
  166. int ossl_qtx_discard_enc_level(OSSL_QTX *qtx, uint32_t enc_level)
  167. {
  168. if (enc_level >= QUIC_ENC_LEVEL_NUM)
  169. return 0;
  170. ossl_qrl_enc_level_set_discard(&qtx->el_set, enc_level);
  171. return 1;
  172. }
  173. int ossl_qtx_is_enc_level_provisioned(OSSL_QTX *qtx, uint32_t enc_level)
  174. {
  175. return ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1) != NULL;
  176. }
  177. /* Allocate a new TXE. */
  178. static TXE *qtx_alloc_txe(size_t alloc_len)
  179. {
  180. TXE *txe;
  181. if (alloc_len >= SIZE_MAX - sizeof(TXE))
  182. return NULL;
  183. txe = OPENSSL_malloc(sizeof(TXE) + alloc_len);
  184. if (txe == NULL)
  185. return NULL;
  186. ossl_list_txe_init_elem(txe);
  187. txe->alloc_len = alloc_len;
  188. txe->data_len = 0;
  189. return txe;
  190. }
  191. /*
  192. * Ensures there is at least one TXE in the free list, allocating a new entry
  193. * if necessary. The returned TXE is in the free list; it is not popped.
  194. *
  195. * alloc_len is a hint which may be used to determine the TXE size if allocation
  196. * is necessary. Returns NULL on allocation failure.
  197. */
  198. static TXE *qtx_ensure_free_txe(OSSL_QTX *qtx, size_t alloc_len)
  199. {
  200. TXE *txe;
  201. txe = ossl_list_txe_head(&qtx->free);
  202. if (txe != NULL)
  203. return txe;
  204. txe = qtx_alloc_txe(alloc_len);
  205. if (txe == NULL)
  206. return NULL;
  207. ossl_list_txe_insert_tail(&qtx->free, txe);
  208. return txe;
  209. }
  210. /*
  211. * Resize the data buffer attached to an TXE to be n bytes in size. The address
  212. * of the TXE might change; the new address is returned, or NULL on failure, in
  213. * which case the original TXE remains valid.
  214. */
  215. static TXE *qtx_resize_txe(OSSL_QTX *qtx, TXE_LIST *txl, TXE *txe, size_t n)
  216. {
  217. TXE *txe2, *p;
  218. /* Should never happen. */
  219. if (txe == NULL)
  220. return NULL;
  221. if (n >= SIZE_MAX - sizeof(TXE))
  222. return NULL;
  223. /* Remove the item from the list to avoid accessing freed memory */
  224. p = ossl_list_txe_prev(txe);
  225. ossl_list_txe_remove(txl, txe);
  226. /*
  227. * NOTE: We do not clear old memory, although it does contain decrypted
  228. * data.
  229. */
  230. txe2 = OPENSSL_realloc(txe, sizeof(TXE) + n);
  231. if (txe2 == NULL) {
  232. if (p == NULL)
  233. ossl_list_txe_insert_head(txl, txe);
  234. else
  235. ossl_list_txe_insert_after(txl, p, txe);
  236. return NULL;
  237. }
  238. if (p == NULL)
  239. ossl_list_txe_insert_head(txl, txe2);
  240. else
  241. ossl_list_txe_insert_after(txl, p, txe2);
  242. if (qtx->cons == txe)
  243. qtx->cons = txe2;
  244. txe2->alloc_len = n;
  245. return txe2;
  246. }
  247. /*
  248. * Ensure the data buffer attached to an TXE is at least n bytes in size.
  249. * Returns NULL on failure.
  250. */
  251. static TXE *qtx_reserve_txe(OSSL_QTX *qtx, TXE_LIST *txl,
  252. TXE *txe, size_t n)
  253. {
  254. if (txe->alloc_len >= n)
  255. return txe;
  256. return qtx_resize_txe(qtx, txl, txe, n);
  257. }
  258. /* Move a TXE from pending to free. */
  259. static void qtx_pending_to_free(OSSL_QTX *qtx)
  260. {
  261. TXE *txe = ossl_list_txe_head(&qtx->pending);
  262. assert(txe != NULL);
  263. ossl_list_txe_remove(&qtx->pending, txe);
  264. --qtx->pending_count;
  265. qtx->pending_bytes -= txe->data_len;
  266. ossl_list_txe_insert_tail(&qtx->free, txe);
  267. }
  268. /* Add a TXE not currently in any list to the pending list. */
  269. static void qtx_add_to_pending(OSSL_QTX *qtx, TXE *txe)
  270. {
  271. ossl_list_txe_insert_tail(&qtx->pending, txe);
  272. ++qtx->pending_count;
  273. qtx->pending_bytes += txe->data_len;
  274. }
  275. struct iovec_cur {
  276. const OSSL_QTX_IOVEC *iovec;
  277. size_t num_iovec, idx, byte_off, bytes_remaining;
  278. };
  279. static size_t iovec_total_bytes(const OSSL_QTX_IOVEC *iovec,
  280. size_t num_iovec)
  281. {
  282. size_t i, l = 0;
  283. for (i = 0; i < num_iovec; ++i)
  284. l += iovec[i].buf_len;
  285. return l;
  286. }
  287. static void iovec_cur_init(struct iovec_cur *cur,
  288. const OSSL_QTX_IOVEC *iovec,
  289. size_t num_iovec)
  290. {
  291. cur->iovec = iovec;
  292. cur->num_iovec = num_iovec;
  293. cur->idx = 0;
  294. cur->byte_off = 0;
  295. cur->bytes_remaining = iovec_total_bytes(iovec, num_iovec);
  296. }
  297. /*
  298. * Get an extent of bytes from the iovec cursor. *buf is set to point to the
  299. * buffer and the number of bytes in length of the buffer is returned. This
  300. * value may be less than the max_buf_len argument. If no more data is
  301. * available, returns 0.
  302. */
  303. static size_t iovec_cur_get_buffer(struct iovec_cur *cur,
  304. const unsigned char **buf,
  305. size_t max_buf_len)
  306. {
  307. size_t l;
  308. if (max_buf_len == 0) {
  309. *buf = NULL;
  310. return 0;
  311. }
  312. for (;;) {
  313. if (cur->idx >= cur->num_iovec)
  314. return 0;
  315. l = cur->iovec[cur->idx].buf_len - cur->byte_off;
  316. if (l > max_buf_len)
  317. l = max_buf_len;
  318. if (l > 0) {
  319. *buf = cur->iovec[cur->idx].buf + cur->byte_off;
  320. cur->byte_off += l;
  321. cur->bytes_remaining -= l;
  322. return l;
  323. }
  324. /*
  325. * Zero-length iovec entry or we already consumed all of it, try the
  326. * next iovec.
  327. */
  328. ++cur->idx;
  329. cur->byte_off = 0;
  330. }
  331. }
  332. /* Determines the size of the AEAD output given the input size. */
  333. int ossl_qtx_calculate_ciphertext_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
  334. size_t plaintext_len,
  335. size_t *ciphertext_len)
  336. {
  337. OSSL_QRL_ENC_LEVEL *el
  338. = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  339. size_t tag_len;
  340. if (el == NULL) {
  341. *ciphertext_len = 0;
  342. return 0;
  343. }
  344. /*
  345. * We currently only support ciphers with a 1:1 mapping between plaintext
  346. * and ciphertext size, save for authentication tag.
  347. */
  348. tag_len = ossl_qrl_get_suite_cipher_tag_len(el->suite_id);
  349. *ciphertext_len = plaintext_len + tag_len;
  350. return 1;
  351. }
  352. /* Determines the size of the AEAD input given the output size. */
  353. int ossl_qtx_calculate_plaintext_payload_len(OSSL_QTX *qtx, uint32_t enc_level,
  354. size_t ciphertext_len,
  355. size_t *plaintext_len)
  356. {
  357. OSSL_QRL_ENC_LEVEL *el
  358. = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  359. size_t tag_len;
  360. if (el == NULL) {
  361. *plaintext_len = 0;
  362. return 0;
  363. }
  364. tag_len = ossl_qrl_get_suite_cipher_tag_len(el->suite_id);
  365. if (ciphertext_len <= tag_len) {
  366. *plaintext_len = 0;
  367. return 0;
  368. }
  369. *plaintext_len = ciphertext_len - tag_len;
  370. return 1;
  371. }
  372. /* Any other error (including packet being too big for MDPL). */
  373. #define QTX_FAIL_GENERIC (-1)
  374. /*
  375. * Returned where there is insufficient room in the datagram to write the
  376. * packet.
  377. */
  378. #define QTX_FAIL_INSUFFICIENT_LEN (-2)
  379. static int qtx_write_hdr(OSSL_QTX *qtx, const QUIC_PKT_HDR *hdr, TXE *txe,
  380. QUIC_PKT_HDR_PTRS *ptrs)
  381. {
  382. WPACKET wpkt;
  383. size_t l = 0;
  384. unsigned char *data = txe_data(txe) + txe->data_len;
  385. if (!WPACKET_init_static_len(&wpkt, data, txe->alloc_len - txe->data_len, 0))
  386. return 0;
  387. if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, hdr->dst_conn_id.id_len,
  388. hdr, ptrs)
  389. || !WPACKET_get_total_written(&wpkt, &l)) {
  390. WPACKET_finish(&wpkt);
  391. return 0;
  392. }
  393. WPACKET_finish(&wpkt);
  394. if (qtx->msg_callback != NULL)
  395. qtx->msg_callback(1, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_PACKET, data, l,
  396. qtx->msg_callback_ssl, qtx->msg_callback_arg);
  397. txe->data_len += l;
  398. return 1;
  399. }
  400. static int qtx_encrypt_into_txe(OSSL_QTX *qtx, struct iovec_cur *cur, TXE *txe,
  401. uint32_t enc_level, QUIC_PN pn,
  402. const unsigned char *hdr, size_t hdr_len,
  403. QUIC_PKT_HDR_PTRS *ptrs)
  404. {
  405. int l = 0, l2 = 0, nonce_len;
  406. OSSL_QRL_ENC_LEVEL *el
  407. = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  408. unsigned char nonce[EVP_MAX_IV_LENGTH];
  409. size_t i;
  410. EVP_CIPHER_CTX *cctx = NULL;
  411. /* We should not have been called if we do not have key material. */
  412. if (!ossl_assert(el != NULL)) {
  413. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  414. return 0;
  415. }
  416. /*
  417. * Have we already encrypted the maximum number of packets using the current
  418. * key?
  419. */
  420. if (el->op_count >= ossl_qrl_get_suite_max_pkt(el->suite_id)) {
  421. ERR_raise(ERR_LIB_SSL, SSL_R_MAXIMUM_ENCRYPTED_PKTS_REACHED);
  422. return 0;
  423. }
  424. /*
  425. * TX key update is simpler than for RX; once we initiate a key update, we
  426. * never need the old keys, as we never deliberately send a packet with old
  427. * keys. Thus the EL always uses keyslot 0 for the TX side.
  428. */
  429. cctx = el->cctx[0];
  430. if (!ossl_assert(cctx != NULL)) {
  431. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  432. return 0;
  433. }
  434. /* Construct nonce (nonce=IV ^ PN). */
  435. nonce_len = EVP_CIPHER_CTX_get_iv_length(cctx);
  436. if (!ossl_assert(nonce_len >= (int)sizeof(QUIC_PN))) {
  437. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  438. return 0;
  439. }
  440. memcpy(nonce, el->iv[0], (size_t)nonce_len);
  441. for (i = 0; i < sizeof(QUIC_PN); ++i)
  442. nonce[nonce_len - i - 1] ^= (unsigned char)(pn >> (i * 8));
  443. /* type and key will already have been setup; feed the IV. */
  444. if (EVP_CipherInit_ex(cctx, NULL, NULL, NULL, nonce, /*enc=*/1) != 1) {
  445. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  446. return 0;
  447. }
  448. /* Feed AAD data. */
  449. if (EVP_CipherUpdate(cctx, NULL, &l, hdr, hdr_len) != 1) {
  450. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  451. return 0;
  452. }
  453. /* Encrypt plaintext directly into TXE. */
  454. for (;;) {
  455. const unsigned char *src;
  456. size_t src_len;
  457. src_len = iovec_cur_get_buffer(cur, &src, SIZE_MAX);
  458. if (src_len == 0)
  459. break;
  460. if (EVP_CipherUpdate(cctx, txe_data(txe) + txe->data_len,
  461. &l, src, src_len) != 1) {
  462. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  463. return 0;
  464. }
  465. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  466. /* Ignore what we just encrypted and overwrite it with the plaintext */
  467. memcpy(txe_data(txe) + txe->data_len, src, l);
  468. #endif
  469. assert(l > 0 && src_len == (size_t)l);
  470. txe->data_len += src_len;
  471. }
  472. /* Finalise and get tag. */
  473. if (EVP_CipherFinal_ex(cctx, NULL, &l2) != 1) {
  474. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  475. return 0;
  476. }
  477. if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_GET_TAG,
  478. el->tag_len, txe_data(txe) + txe->data_len) != 1) {
  479. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  480. return 0;
  481. }
  482. txe->data_len += el->tag_len;
  483. /* Apply header protection. */
  484. if (!ossl_quic_hdr_protector_encrypt(&el->hpr, ptrs))
  485. return 0;
  486. ++el->op_count;
  487. return 1;
  488. }
  489. /*
  490. * Append a packet to the TXE buffer, serializing and encrypting it in the
  491. * process.
  492. */
  493. static int qtx_write(OSSL_QTX *qtx, const OSSL_QTX_PKT *pkt, TXE *txe,
  494. uint32_t enc_level, QUIC_PKT_HDR *hdr,
  495. const OSSL_QTX_IOVEC *iovec, size_t num_iovec)
  496. {
  497. int ret, needs_encrypt;
  498. size_t hdr_len, pred_hdr_len, payload_len, pkt_len, space_left;
  499. size_t min_len, orig_data_len;
  500. struct iovec_cur cur;
  501. QUIC_PKT_HDR_PTRS ptrs;
  502. unsigned char *hdr_start;
  503. OSSL_QRL_ENC_LEVEL *el = NULL;
  504. /*
  505. * Determine if the packet needs encryption and the minimum conceivable
  506. * serialization length.
  507. */
  508. if (!ossl_quic_pkt_type_is_encrypted(hdr->type)) {
  509. needs_encrypt = 0;
  510. min_len = QUIC_MIN_VALID_PKT_LEN;
  511. } else {
  512. needs_encrypt = 1;
  513. min_len = QUIC_MIN_VALID_PKT_LEN_CRYPTO;
  514. el = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  515. if (!ossl_assert(el != NULL)) /* should already have been checked */
  516. return 0;
  517. }
  518. orig_data_len = txe->data_len;
  519. space_left = txe->alloc_len - txe->data_len;
  520. if (space_left < min_len) {
  521. /* Not even a possibility of it fitting. */
  522. ret = QTX_FAIL_INSUFFICIENT_LEN;
  523. goto err;
  524. }
  525. /* Set some fields in the header we are responsible for. */
  526. if (hdr->type == QUIC_PKT_TYPE_1RTT)
  527. hdr->key_phase = (unsigned char)(el->key_epoch & 1);
  528. /* Walk the iovecs to determine actual input payload length. */
  529. iovec_cur_init(&cur, iovec, num_iovec);
  530. if (cur.bytes_remaining == 0) {
  531. /* No zero-length payloads allowed. */
  532. ret = QTX_FAIL_GENERIC;
  533. goto err;
  534. }
  535. /* Determine encrypted payload length. */
  536. if (needs_encrypt)
  537. ossl_qtx_calculate_ciphertext_payload_len(qtx, enc_level,
  538. cur.bytes_remaining,
  539. &payload_len);
  540. else
  541. payload_len = cur.bytes_remaining;
  542. /* Determine header length. */
  543. hdr->data = NULL;
  544. hdr->len = payload_len;
  545. pred_hdr_len = ossl_quic_wire_get_encoded_pkt_hdr_len(hdr->dst_conn_id.id_len,
  546. hdr);
  547. if (pred_hdr_len == 0) {
  548. ret = QTX_FAIL_GENERIC;
  549. goto err;
  550. }
  551. /* We now definitively know our packet length. */
  552. pkt_len = pred_hdr_len + payload_len;
  553. if (pkt_len > space_left) {
  554. ret = QTX_FAIL_INSUFFICIENT_LEN;
  555. goto err;
  556. }
  557. if (ossl_quic_pkt_type_has_pn(hdr->type)) {
  558. if (!ossl_quic_wire_encode_pkt_hdr_pn(pkt->pn,
  559. hdr->pn,
  560. hdr->pn_len)) {
  561. ret = QTX_FAIL_GENERIC;
  562. goto err;
  563. }
  564. }
  565. /* Append the header to the TXE. */
  566. hdr_start = txe_data(txe) + txe->data_len;
  567. if (!qtx_write_hdr(qtx, hdr, txe, &ptrs)) {
  568. ret = QTX_FAIL_GENERIC;
  569. goto err;
  570. }
  571. hdr_len = (txe_data(txe) + txe->data_len) - hdr_start;
  572. assert(hdr_len == pred_hdr_len);
  573. if (!needs_encrypt) {
  574. /* Just copy the payload across. */
  575. const unsigned char *src;
  576. size_t src_len;
  577. for (;;) {
  578. /* Buffer length has already been checked above. */
  579. src_len = iovec_cur_get_buffer(&cur, &src, SIZE_MAX);
  580. if (src_len == 0)
  581. break;
  582. memcpy(txe_data(txe) + txe->data_len, src, src_len);
  583. txe->data_len += src_len;
  584. }
  585. } else {
  586. /* Encrypt into TXE. */
  587. if (!qtx_encrypt_into_txe(qtx, &cur, txe, enc_level, pkt->pn,
  588. hdr_start, hdr_len, &ptrs)) {
  589. ret = QTX_FAIL_GENERIC;
  590. goto err;
  591. }
  592. assert(txe->data_len - orig_data_len == pkt_len);
  593. }
  594. return 1;
  595. err:
  596. /*
  597. * Restore original length so we don't leave a half-written packet in the
  598. * TXE.
  599. */
  600. txe->data_len = orig_data_len;
  601. return ret;
  602. }
  603. static TXE *qtx_ensure_cons(OSSL_QTX *qtx)
  604. {
  605. TXE *txe = qtx->cons;
  606. if (txe != NULL)
  607. return txe;
  608. txe = qtx_ensure_free_txe(qtx, qtx->mdpl);
  609. if (txe == NULL)
  610. return NULL;
  611. ossl_list_txe_remove(&qtx->free, txe);
  612. qtx->cons = txe;
  613. qtx->cons_count = 0;
  614. txe->data_len = 0;
  615. return txe;
  616. }
  617. static QLOG *qtx_get_qlog(OSSL_QTX *qtx)
  618. {
  619. if (qtx->get_qlog_cb == NULL)
  620. return NULL;
  621. return qtx->get_qlog_cb(qtx->get_qlog_cb_arg);
  622. }
  623. static int qtx_mutate_write(OSSL_QTX *qtx, const OSSL_QTX_PKT *pkt, TXE *txe,
  624. uint32_t enc_level)
  625. {
  626. int ret;
  627. QUIC_PKT_HDR *hdr;
  628. const OSSL_QTX_IOVEC *iovec;
  629. size_t num_iovec;
  630. /* If we are running tests then mutate_packet may be non NULL */
  631. if (qtx->mutatecb != NULL) {
  632. if (!qtx->mutatecb(pkt->hdr, pkt->iovec, pkt->num_iovec, &hdr,
  633. &iovec, &num_iovec, qtx->mutatearg))
  634. return QTX_FAIL_GENERIC;
  635. } else {
  636. hdr = pkt->hdr;
  637. iovec = pkt->iovec;
  638. num_iovec = pkt->num_iovec;
  639. }
  640. ret = qtx_write(qtx, pkt, txe, enc_level,
  641. hdr, iovec, num_iovec);
  642. if (ret == 1)
  643. ossl_qlog_event_transport_packet_sent(qtx_get_qlog(qtx), hdr, pkt->pn,
  644. iovec, num_iovec,
  645. qtx->datagram_count);
  646. if (qtx->finishmutatecb != NULL)
  647. qtx->finishmutatecb(qtx->mutatearg);
  648. return ret;
  649. }
  650. static int addr_eq(const BIO_ADDR *a, const BIO_ADDR *b)
  651. {
  652. return ((a == NULL || BIO_ADDR_family(a) == AF_UNSPEC)
  653. && (b == NULL || BIO_ADDR_family(b) == AF_UNSPEC))
  654. || (a != NULL && b != NULL && memcmp(a, b, sizeof(*a)) == 0);
  655. }
  656. int ossl_qtx_write_pkt(OSSL_QTX *qtx, const OSSL_QTX_PKT *pkt)
  657. {
  658. int ret;
  659. int coalescing = (pkt->flags & OSSL_QTX_PKT_FLAG_COALESCE) != 0;
  660. int was_coalescing;
  661. TXE *txe;
  662. uint32_t enc_level;
  663. /* Must have EL configured, must have header. */
  664. if (pkt->hdr == NULL)
  665. return 0;
  666. enc_level = ossl_quic_pkt_type_to_enc_level(pkt->hdr->type);
  667. /* Some packet types must be in a packet all by themselves. */
  668. if (!ossl_quic_pkt_type_can_share_dgram(pkt->hdr->type))
  669. ossl_qtx_finish_dgram(qtx);
  670. else if (enc_level >= QUIC_ENC_LEVEL_NUM
  671. || ossl_qrl_enc_level_set_have_el(&qtx->el_set, enc_level) != 1) {
  672. /* All other packet types are encrypted. */
  673. return 0;
  674. }
  675. was_coalescing = (qtx->cons != NULL && qtx->cons->data_len > 0);
  676. if (was_coalescing)
  677. if (!addr_eq(&qtx->cons->peer, pkt->peer)
  678. || !addr_eq(&qtx->cons->local, pkt->local)) {
  679. /* Must stop coalescing if addresses have changed */
  680. ossl_qtx_finish_dgram(qtx);
  681. was_coalescing = 0;
  682. }
  683. for (;;) {
  684. /*
  685. * Start a new coalescing session or continue using the existing one and
  686. * serialize/encrypt the packet. We always encrypt packets as soon as
  687. * our caller gives them to us, which relieves the caller of any need to
  688. * keep the plaintext around.
  689. */
  690. txe = qtx_ensure_cons(qtx);
  691. if (txe == NULL)
  692. return 0; /* allocation failure */
  693. /*
  694. * Ensure TXE has at least MDPL bytes allocated. This should only be
  695. * possible if the MDPL has increased.
  696. */
  697. if (!qtx_reserve_txe(qtx, NULL, txe, qtx->mdpl))
  698. return 0;
  699. if (!was_coalescing) {
  700. /* Set addresses in TXE. */
  701. if (pkt->peer != NULL) {
  702. if (!BIO_ADDR_copy(&txe->peer, pkt->peer))
  703. return 0;
  704. } else {
  705. BIO_ADDR_clear(&txe->peer);
  706. }
  707. if (pkt->local != NULL) {
  708. if (!BIO_ADDR_copy(&txe->local, pkt->local))
  709. return 0;
  710. } else {
  711. BIO_ADDR_clear(&txe->local);
  712. }
  713. }
  714. ret = qtx_mutate_write(qtx, pkt, txe, enc_level);
  715. if (ret == 1) {
  716. break;
  717. } else if (ret == QTX_FAIL_INSUFFICIENT_LEN) {
  718. if (was_coalescing) {
  719. /*
  720. * We failed due to insufficient length, so end the current
  721. * datagram and try again.
  722. */
  723. ossl_qtx_finish_dgram(qtx);
  724. was_coalescing = 0;
  725. } else {
  726. /*
  727. * We failed due to insufficient length, but we were not
  728. * coalescing/started with an empty datagram, so any future
  729. * attempt to write this packet must also fail.
  730. */
  731. return 0;
  732. }
  733. } else {
  734. return 0; /* other error */
  735. }
  736. }
  737. ++qtx->cons_count;
  738. /*
  739. * Some packet types cannot have another packet come after them.
  740. */
  741. if (ossl_quic_pkt_type_must_be_last(pkt->hdr->type))
  742. coalescing = 0;
  743. if (!coalescing)
  744. ossl_qtx_finish_dgram(qtx);
  745. return 1;
  746. }
  747. /*
  748. * Finish any incomplete datagrams for transmission which were flagged for
  749. * coalescing. If there is no current coalescing datagram, this is a no-op.
  750. */
  751. void ossl_qtx_finish_dgram(OSSL_QTX *qtx)
  752. {
  753. TXE *txe = qtx->cons;
  754. if (txe == NULL)
  755. return;
  756. if (txe->data_len == 0)
  757. /*
  758. * If we did not put anything in the datagram, just move it back to the
  759. * free list.
  760. */
  761. ossl_list_txe_insert_tail(&qtx->free, txe);
  762. else
  763. qtx_add_to_pending(qtx, txe);
  764. qtx->cons = NULL;
  765. qtx->cons_count = 0;
  766. ++qtx->datagram_count;
  767. }
  768. static void txe_to_msg(TXE *txe, BIO_MSG *msg)
  769. {
  770. msg->data = txe_data(txe);
  771. msg->data_len = txe->data_len;
  772. msg->flags = 0;
  773. msg->peer
  774. = BIO_ADDR_family(&txe->peer) != AF_UNSPEC ? &txe->peer : NULL;
  775. msg->local
  776. = BIO_ADDR_family(&txe->local) != AF_UNSPEC ? &txe->local : NULL;
  777. }
  778. #define MAX_MSGS_PER_SEND 32
  779. int ossl_qtx_flush_net(OSSL_QTX *qtx)
  780. {
  781. BIO_MSG msg[MAX_MSGS_PER_SEND];
  782. size_t wr, i, total_written = 0;
  783. TXE *txe;
  784. int res;
  785. if (ossl_list_txe_head(&qtx->pending) == NULL)
  786. return QTX_FLUSH_NET_RES_OK; /* Nothing to send. */
  787. if (qtx->bio == NULL)
  788. return QTX_FLUSH_NET_RES_PERMANENT_FAIL;
  789. for (;;) {
  790. for (txe = ossl_list_txe_head(&qtx->pending), i = 0;
  791. txe != NULL && i < OSSL_NELEM(msg);
  792. txe = ossl_list_txe_next(txe), ++i)
  793. txe_to_msg(txe, &msg[i]);
  794. if (!i)
  795. /* Nothing to send. */
  796. break;
  797. ERR_set_mark();
  798. res = BIO_sendmmsg(qtx->bio, msg, sizeof(BIO_MSG), i, 0, &wr);
  799. if (res && wr == 0) {
  800. /*
  801. * Treat 0 messages sent as a transient error and just stop for now.
  802. */
  803. ERR_clear_last_mark();
  804. break;
  805. } else if (!res) {
  806. /*
  807. * We did not get anything, so further calls will probably not
  808. * succeed either.
  809. */
  810. if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
  811. /* Transient error, just stop for now, clearing the error. */
  812. ERR_pop_to_mark();
  813. break;
  814. } else {
  815. /* Non-transient error, fail and do not clear the error. */
  816. ERR_clear_last_mark();
  817. return QTX_FLUSH_NET_RES_PERMANENT_FAIL;
  818. }
  819. }
  820. ERR_clear_last_mark();
  821. /*
  822. * Remove everything which was successfully sent from the pending queue.
  823. */
  824. for (i = 0; i < wr; ++i) {
  825. if (qtx->msg_callback != NULL)
  826. qtx->msg_callback(1, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_DATAGRAM,
  827. msg[i].data, msg[i].data_len,
  828. qtx->msg_callback_ssl,
  829. qtx->msg_callback_arg);
  830. qtx_pending_to_free(qtx);
  831. }
  832. total_written += wr;
  833. }
  834. return total_written > 0
  835. ? QTX_FLUSH_NET_RES_OK
  836. : QTX_FLUSH_NET_RES_TRANSIENT_FAIL;
  837. }
  838. int ossl_qtx_pop_net(OSSL_QTX *qtx, BIO_MSG *msg)
  839. {
  840. TXE *txe = ossl_list_txe_head(&qtx->pending);
  841. if (txe == NULL)
  842. return 0;
  843. txe_to_msg(txe, msg);
  844. qtx_pending_to_free(qtx);
  845. return 1;
  846. }
  847. void ossl_qtx_set_bio(OSSL_QTX *qtx, BIO *bio)
  848. {
  849. qtx->bio = bio;
  850. }
  851. int ossl_qtx_set_mdpl(OSSL_QTX *qtx, size_t mdpl)
  852. {
  853. if (mdpl < QUIC_MIN_INITIAL_DGRAM_LEN)
  854. return 0;
  855. qtx->mdpl = mdpl;
  856. return 1;
  857. }
  858. size_t ossl_qtx_get_mdpl(OSSL_QTX *qtx)
  859. {
  860. return qtx->mdpl;
  861. }
  862. size_t ossl_qtx_get_queue_len_datagrams(OSSL_QTX *qtx)
  863. {
  864. return qtx->pending_count;
  865. }
  866. size_t ossl_qtx_get_queue_len_bytes(OSSL_QTX *qtx)
  867. {
  868. return qtx->pending_bytes;
  869. }
  870. size_t ossl_qtx_get_cur_dgram_len_bytes(OSSL_QTX *qtx)
  871. {
  872. return qtx->cons != NULL ? qtx->cons->data_len : 0;
  873. }
  874. size_t ossl_qtx_get_unflushed_pkt_count(OSSL_QTX *qtx)
  875. {
  876. return qtx->cons_count;
  877. }
  878. int ossl_qtx_trigger_key_update(OSSL_QTX *qtx)
  879. {
  880. return ossl_qrl_enc_level_set_key_update(&qtx->el_set,
  881. QUIC_ENC_LEVEL_1RTT);
  882. }
  883. uint64_t ossl_qtx_get_cur_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level)
  884. {
  885. OSSL_QRL_ENC_LEVEL *el;
  886. el = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  887. if (el == NULL)
  888. return UINT64_MAX;
  889. return el->op_count;
  890. }
  891. uint64_t ossl_qtx_get_max_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level)
  892. {
  893. OSSL_QRL_ENC_LEVEL *el;
  894. el = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  895. if (el == NULL)
  896. return UINT64_MAX;
  897. return ossl_qrl_get_suite_max_pkt(el->suite_id);
  898. }
  899. void ossl_qtx_set_msg_callback(OSSL_QTX *qtx, ossl_msg_cb msg_callback,
  900. SSL *msg_callback_ssl)
  901. {
  902. qtx->msg_callback = msg_callback;
  903. qtx->msg_callback_ssl = msg_callback_ssl;
  904. }
  905. void ossl_qtx_set_msg_callback_arg(OSSL_QTX *qtx, void *msg_callback_arg)
  906. {
  907. qtx->msg_callback_arg = msg_callback_arg;
  908. }
  909. uint64_t ossl_qtx_get_key_epoch(OSSL_QTX *qtx)
  910. {
  911. OSSL_QRL_ENC_LEVEL *el;
  912. el = ossl_qrl_enc_level_set_get(&qtx->el_set, QUIC_ENC_LEVEL_1RTT, 1);
  913. if (el == NULL)
  914. return 0;
  915. return el->key_epoch;
  916. }