quic_record_tx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 "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 || txe == txe2) {
  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 txe2;
  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. txe->peer = *pkt->peer;
  703. else
  704. BIO_ADDR_clear(&txe->peer);
  705. if (pkt->local != NULL)
  706. txe->local = *pkt->local;
  707. else
  708. BIO_ADDR_clear(&txe->local);
  709. }
  710. ret = qtx_mutate_write(qtx, pkt, txe, enc_level);
  711. if (ret == 1) {
  712. break;
  713. } else if (ret == QTX_FAIL_INSUFFICIENT_LEN) {
  714. if (was_coalescing) {
  715. /*
  716. * We failed due to insufficient length, so end the current
  717. * datagram and try again.
  718. */
  719. ossl_qtx_finish_dgram(qtx);
  720. was_coalescing = 0;
  721. } else {
  722. /*
  723. * We failed due to insufficient length, but we were not
  724. * coalescing/started with an empty datagram, so any future
  725. * attempt to write this packet must also fail.
  726. */
  727. return 0;
  728. }
  729. } else {
  730. return 0; /* other error */
  731. }
  732. }
  733. ++qtx->cons_count;
  734. /*
  735. * Some packet types cannot have another packet come after them.
  736. */
  737. if (ossl_quic_pkt_type_must_be_last(pkt->hdr->type))
  738. coalescing = 0;
  739. if (!coalescing)
  740. ossl_qtx_finish_dgram(qtx);
  741. return 1;
  742. }
  743. /*
  744. * Finish any incomplete datagrams for transmission which were flagged for
  745. * coalescing. If there is no current coalescing datagram, this is a no-op.
  746. */
  747. void ossl_qtx_finish_dgram(OSSL_QTX *qtx)
  748. {
  749. TXE *txe = qtx->cons;
  750. if (txe == NULL)
  751. return;
  752. if (txe->data_len == 0)
  753. /*
  754. * If we did not put anything in the datagram, just move it back to the
  755. * free list.
  756. */
  757. ossl_list_txe_insert_tail(&qtx->free, txe);
  758. else
  759. qtx_add_to_pending(qtx, txe);
  760. qtx->cons = NULL;
  761. qtx->cons_count = 0;
  762. ++qtx->datagram_count;
  763. }
  764. static void txe_to_msg(TXE *txe, BIO_MSG *msg)
  765. {
  766. msg->data = txe_data(txe);
  767. msg->data_len = txe->data_len;
  768. msg->flags = 0;
  769. msg->peer
  770. = BIO_ADDR_family(&txe->peer) != AF_UNSPEC ? &txe->peer : NULL;
  771. msg->local
  772. = BIO_ADDR_family(&txe->local) != AF_UNSPEC ? &txe->local : NULL;
  773. }
  774. #define MAX_MSGS_PER_SEND 32
  775. int ossl_qtx_flush_net(OSSL_QTX *qtx)
  776. {
  777. BIO_MSG msg[MAX_MSGS_PER_SEND];
  778. size_t wr, i, total_written = 0;
  779. TXE *txe;
  780. int res;
  781. if (ossl_list_txe_head(&qtx->pending) == NULL)
  782. return QTX_FLUSH_NET_RES_OK; /* Nothing to send. */
  783. if (qtx->bio == NULL)
  784. return QTX_FLUSH_NET_RES_PERMANENT_FAIL;
  785. for (;;) {
  786. for (txe = ossl_list_txe_head(&qtx->pending), i = 0;
  787. txe != NULL && i < OSSL_NELEM(msg);
  788. txe = ossl_list_txe_next(txe), ++i)
  789. txe_to_msg(txe, &msg[i]);
  790. if (!i)
  791. /* Nothing to send. */
  792. break;
  793. ERR_set_mark();
  794. res = BIO_sendmmsg(qtx->bio, msg, sizeof(BIO_MSG), i, 0, &wr);
  795. if (res && wr == 0) {
  796. /*
  797. * Treat 0 messages sent as a transient error and just stop for now.
  798. */
  799. ERR_clear_last_mark();
  800. break;
  801. } else if (!res) {
  802. /*
  803. * We did not get anything, so further calls will probably not
  804. * succeed either.
  805. */
  806. if (BIO_err_is_non_fatal(ERR_peek_last_error())) {
  807. /* Transient error, just stop for now, clearing the error. */
  808. ERR_pop_to_mark();
  809. break;
  810. } else {
  811. /* Non-transient error, fail and do not clear the error. */
  812. ERR_clear_last_mark();
  813. return QTX_FLUSH_NET_RES_PERMANENT_FAIL;
  814. }
  815. }
  816. ERR_clear_last_mark();
  817. /*
  818. * Remove everything which was successfully sent from the pending queue.
  819. */
  820. for (i = 0; i < wr; ++i) {
  821. if (qtx->msg_callback != NULL)
  822. qtx->msg_callback(1, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_DATAGRAM,
  823. msg[i].data, msg[i].data_len,
  824. qtx->msg_callback_ssl,
  825. qtx->msg_callback_arg);
  826. qtx_pending_to_free(qtx);
  827. }
  828. total_written += wr;
  829. }
  830. return total_written > 0
  831. ? QTX_FLUSH_NET_RES_OK
  832. : QTX_FLUSH_NET_RES_TRANSIENT_FAIL;
  833. }
  834. int ossl_qtx_pop_net(OSSL_QTX *qtx, BIO_MSG *msg)
  835. {
  836. TXE *txe = ossl_list_txe_head(&qtx->pending);
  837. if (txe == NULL)
  838. return 0;
  839. txe_to_msg(txe, msg);
  840. qtx_pending_to_free(qtx);
  841. return 1;
  842. }
  843. void ossl_qtx_set_bio(OSSL_QTX *qtx, BIO *bio)
  844. {
  845. qtx->bio = bio;
  846. }
  847. int ossl_qtx_set_mdpl(OSSL_QTX *qtx, size_t mdpl)
  848. {
  849. if (mdpl < QUIC_MIN_INITIAL_DGRAM_LEN)
  850. return 0;
  851. qtx->mdpl = mdpl;
  852. return 1;
  853. }
  854. size_t ossl_qtx_get_mdpl(OSSL_QTX *qtx)
  855. {
  856. return qtx->mdpl;
  857. }
  858. size_t ossl_qtx_get_queue_len_datagrams(OSSL_QTX *qtx)
  859. {
  860. return qtx->pending_count;
  861. }
  862. size_t ossl_qtx_get_queue_len_bytes(OSSL_QTX *qtx)
  863. {
  864. return qtx->pending_bytes;
  865. }
  866. size_t ossl_qtx_get_cur_dgram_len_bytes(OSSL_QTX *qtx)
  867. {
  868. return qtx->cons != NULL ? qtx->cons->data_len : 0;
  869. }
  870. size_t ossl_qtx_get_unflushed_pkt_count(OSSL_QTX *qtx)
  871. {
  872. return qtx->cons_count;
  873. }
  874. int ossl_qtx_trigger_key_update(OSSL_QTX *qtx)
  875. {
  876. return ossl_qrl_enc_level_set_key_update(&qtx->el_set,
  877. QUIC_ENC_LEVEL_1RTT);
  878. }
  879. uint64_t ossl_qtx_get_cur_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level)
  880. {
  881. OSSL_QRL_ENC_LEVEL *el;
  882. el = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  883. if (el == NULL)
  884. return UINT64_MAX;
  885. return el->op_count;
  886. }
  887. uint64_t ossl_qtx_get_max_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level)
  888. {
  889. OSSL_QRL_ENC_LEVEL *el;
  890. el = ossl_qrl_enc_level_set_get(&qtx->el_set, enc_level, 1);
  891. if (el == NULL)
  892. return UINT64_MAX;
  893. return ossl_qrl_get_suite_max_pkt(el->suite_id);
  894. }
  895. void ossl_qtx_set_msg_callback(OSSL_QTX *qtx, ossl_msg_cb msg_callback,
  896. SSL *msg_callback_ssl)
  897. {
  898. qtx->msg_callback = msg_callback;
  899. qtx->msg_callback_ssl = msg_callback_ssl;
  900. }
  901. void ossl_qtx_set_msg_callback_arg(OSSL_QTX *qtx, void *msg_callback_arg)
  902. {
  903. qtx->msg_callback_arg = msg_callback_arg;
  904. }
  905. uint64_t ossl_qtx_get_key_epoch(OSSL_QTX *qtx)
  906. {
  907. OSSL_QRL_ENC_LEVEL *el;
  908. el = ossl_qrl_enc_level_set_get(&qtx->el_set, QUIC_ENC_LEVEL_1RTT, 1);
  909. if (el == NULL)
  910. return 0;
  911. return el->key_epoch;
  912. }