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