quic_record_tx.c 29 KB

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