]>
Commit | Line | Data |
---|---|---|
19571483 | 1 | /* |
da1c088f | 2 | * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. |
19571483 HL |
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 | #ifndef OSSL_QUIC_RECORD_TX_H | |
11 | # define OSSL_QUIC_RECORD_TX_H | |
12 | ||
13 | # include <openssl/ssl.h> | |
14 | # include "internal/quic_wire_pkt.h" | |
15 | # include "internal/quic_types.h" | |
16 | # include "internal/quic_record_util.h" | |
17 | ||
6292519c HL |
18 | # ifndef OPENSSL_NO_QUIC |
19 | ||
19571483 HL |
20 | /* |
21 | * QUIC Record Layer - TX | |
22 | * ====================== | |
23 | */ | |
14e31409 MC |
24 | typedef struct ossl_qtx_iovec_st { |
25 | const unsigned char *buf; | |
26 | size_t buf_len; | |
27 | } OSSL_QTX_IOVEC; | |
28 | ||
19571483 HL |
29 | typedef struct ossl_qtx_st OSSL_QTX; |
30 | ||
14e31409 MC |
31 | typedef int (*ossl_mutate_packet_cb)(const QUIC_PKT_HDR *hdrin, |
32 | const OSSL_QTX_IOVEC *iovecin, size_t numin, | |
33 | QUIC_PKT_HDR **hdrout, | |
34 | const OSSL_QTX_IOVEC **iovecout, | |
35 | size_t *numout, | |
36 | void *arg); | |
37 | ||
38 | typedef void (*ossl_finish_mutate_cb)(void *arg); | |
39 | ||
19571483 HL |
40 | typedef struct ossl_qtx_args_st { |
41 | OSSL_LIB_CTX *libctx; | |
42 | const char *propq; | |
43 | ||
44 | /* BIO to transmit to. */ | |
45 | BIO *bio; | |
46 | ||
47 | /* Maximum datagram payload length (MDPL) for TX purposes. */ | |
48 | size_t mdpl; | |
49 | } OSSL_QTX_ARGS; | |
50 | ||
51 | /* Instantiates a new QTX. */ | |
52 | OSSL_QTX *ossl_qtx_new(const OSSL_QTX_ARGS *args); | |
53 | ||
54 | /* Frees the QTX. */ | |
55 | void ossl_qtx_free(OSSL_QTX *qtx); | |
56 | ||
14e31409 MC |
57 | /* Set mutator callbacks for test framework support */ |
58 | void ossl_qtx_set_mutator(OSSL_QTX *qtx, ossl_mutate_packet_cb mutatecb, | |
59 | ossl_finish_mutate_cb finishmutatecb, void *mutatearg); | |
60 | ||
5cf99b40 MC |
61 | /* Setters for the msg_callback and the msg_callback_arg */ |
62 | void ossl_qtx_set_msg_callback(OSSL_QTX *qtx, ossl_msg_cb msg_callback, | |
c2786c8e | 63 | SSL *msg_callback_ssl); |
5cf99b40 MC |
64 | void ossl_qtx_set_msg_callback_arg(OSSL_QTX *qtx, void *msg_callback_arg); |
65 | ||
19571483 HL |
66 | /* |
67 | * Secret Management | |
68 | * ----------------- | |
69 | */ | |
70 | ||
71 | /* | |
72 | * Provides a secret to the QTX, which arises due to an encryption level change. | |
73 | * enc_level is a QUIC_ENC_LEVEL_* value. | |
74 | * | |
75 | * This function can be used to initialise the INITIAL encryption level, but you | |
76 | * should not do so directly; see the utility function | |
77 | * ossl_qrl_provide_initial_secret() instead, which can initialise the INITIAL | |
78 | * encryption level of a QRX and QTX simultaneously without duplicating certain | |
79 | * key derivation steps. | |
80 | * | |
81 | * You must call this function for a given EL before transmitting packets at | |
82 | * that EL using this QTX, otherwise ossl_qtx_write_pkt will fail. | |
83 | * | |
84 | * suite_id is a QRL_SUITE_* value which determines the AEAD function used for | |
85 | * the QTX. | |
86 | * | |
87 | * The secret passed is used directly to derive the "quic key", "quic iv" and | |
88 | * "quic hp" values. | |
89 | * | |
90 | * secret_len is the length of the secret buffer in bytes. The buffer must be | |
91 | * sized correctly to the chosen suite, else the function fails. | |
92 | * | |
b2c94b93 HL |
93 | * This function can only be called once for a given EL, except for the INITIAL |
94 | * EL, as the INITIAL EL can need to be rekeyed if connection retry occurs. | |
95 | * Subsequent calls for non-INITIAL ELs fail. Calls made after a corresponding | |
96 | * call to ossl_qtx_discard_enc_level for a given EL also fail, including for | |
97 | * the INITIAL EL. The secret for a non-INITIAL EL cannot be changed after it is | |
98 | * set because QUIC has no facility for introducing additional key material | |
99 | * after an EL is setup. (QUIC key updates generate new keys from existing key | |
100 | * material and do not introduce new entropy into a connection's key material.) | |
19571483 HL |
101 | * |
102 | * Returns 1 on success or 0 on failure. | |
103 | */ | |
104 | int ossl_qtx_provide_secret(OSSL_QTX *qtx, | |
105 | uint32_t enc_level, | |
106 | uint32_t suite_id, | |
107 | EVP_MD *md, | |
108 | const unsigned char *secret, | |
109 | size_t secret_len); | |
110 | ||
111 | /* | |
112 | * Informs the QTX that it can now discard key material for a given EL. The QTX | |
113 | * will no longer be able to generate packets at that EL. This function is | |
114 | * idempotent and succeeds if the EL has already been discarded. | |
115 | * | |
116 | * Returns 1 on success and 0 on failure. | |
117 | */ | |
118 | int ossl_qtx_discard_enc_level(OSSL_QTX *qtx, uint32_t enc_level); | |
119 | ||
a73078b7 HL |
120 | /* Returns 1 if the given encryption level is provisioned. */ |
121 | int ossl_qtx_is_enc_level_provisioned(OSSL_QTX *qtx, uint32_t enc_level); | |
122 | ||
123 | /* | |
124 | * Given the value ciphertext_len representing an encrypted packet payload | |
125 | * length in bytes, determines how many plaintext bytes it will decrypt to. | |
126 | * Returns 0 if the specified EL is not provisioned or ciphertext_len is too | |
127 | * small. The result is written to *plaintext_len. | |
128 | */ | |
129 | int ossl_qtx_calculate_plaintext_payload_len(OSSL_QTX *qtx, uint32_t enc_level, | |
130 | size_t ciphertext_len, | |
131 | size_t *plaintext_len); | |
132 | ||
41d39984 HL |
133 | /* |
134 | * Given the value plaintext_len represented a plaintext packet payload length | |
135 | * in bytes, determines how many ciphertext bytes it will encrypt to. The value | |
136 | * output does not include packet headers. Returns 0 if the specified EL is not | |
137 | * provisioned. The result is written to *ciphertext_len. | |
138 | */ | |
139 | int ossl_qtx_calculate_ciphertext_payload_len(OSSL_QTX *qtx, uint32_t enc_level, | |
140 | size_t plaintext_len, | |
141 | size_t *ciphertext_len); | |
142 | ||
a73078b7 HL |
143 | uint32_t ossl_qrl_get_suite_cipher_tag_len(uint32_t suite_id); |
144 | ||
19571483 HL |
145 | |
146 | /* | |
147 | * Packet Transmission | |
148 | * ------------------- | |
149 | */ | |
19571483 HL |
150 | |
151 | typedef struct ossl_qtx_pkt_st { | |
152 | /* Logical packet header to be serialized. */ | |
153 | QUIC_PKT_HDR *hdr; | |
154 | ||
155 | /* | |
156 | * iovecs expressing the logical packet payload buffer. Zero-length entries | |
157 | * are permitted. | |
158 | */ | |
159 | const OSSL_QTX_IOVEC *iovec; | |
160 | size_t num_iovec; | |
161 | ||
162 | /* Destination address. Will be passed through to the BIO if non-NULL. */ | |
163 | const BIO_ADDR *peer; | |
164 | ||
165 | /* | |
166 | * Local address (optional). Specify as non-NULL only if TX BIO | |
167 | * has local address support enabled. | |
168 | */ | |
169 | const BIO_ADDR *local; | |
170 | ||
171 | /* | |
172 | * Logical PN. Used for encryption. This will automatically be encoded to | |
173 | * hdr->pn, which need not be initialized. | |
174 | */ | |
175 | QUIC_PN pn; | |
176 | ||
177 | /* Packet flags. Zero or more OSSL_QTX_PKT_FLAG_* values. */ | |
178 | uint32_t flags; | |
179 | } OSSL_QTX_PKT; | |
180 | ||
181 | /* | |
182 | * More packets will be written which should be coalesced into a single | |
183 | * datagram; do not send this packet yet. To use this, set this flag for all | |
184 | * packets but the final packet in a datagram, then send the final packet | |
185 | * without this flag set. | |
186 | * | |
187 | * This flag is not a guarantee and the QTX may transmit immediately anyway if | |
188 | * it is not possible to fit any more packets in the current datagram. | |
189 | * | |
190 | * If the caller change its mind and needs to cause a packet queued with | |
191 | * COALESCE after having passed it to this function but without writing another | |
192 | * packet, it should call ossl_qtx_flush_pkt(). | |
193 | */ | |
194 | #define OSSL_QTX_PKT_FLAG_COALESCE (1U << 0) | |
195 | ||
196 | /* | |
197 | * Writes a packet. | |
198 | * | |
199 | * *pkt need be valid only for the duration of the call to this function. | |
200 | * | |
201 | * pkt->hdr->data and pkt->hdr->len are unused. The payload buffer is specified | |
202 | * via an array of OSSL_QTX_IOVEC structures. The API is designed to support | |
203 | * single-copy transmission; data is copied from the iovecs as it is encrypted | |
204 | * into an internal staging buffer for transmission. | |
205 | * | |
206 | * The function may modify and clobber pkt->hdr->data, pkt->hdr->len, | |
207 | * pkt->hdr->key_phase and pkt->hdr->pn for its own internal use. No other | |
208 | * fields of pkt or pkt->hdr will be modified. | |
209 | * | |
210 | * It is the callers responsibility to determine how long the PN field in the | |
211 | * encoded packet should be by setting pkt->hdr->pn_len. This function takes | |
212 | * care of the PN encoding. Set pkt->pn to the desired PN. | |
213 | * | |
948c656c HL |
214 | * Note that 1-RTT packets do not have a DCID Length field, therefore the DCID |
215 | * length must be understood contextually. This function assumes the caller | |
216 | * knows what it is doing and will serialize a DCID of whatever length is given. | |
217 | * It is the caller's responsibility to ensure it uses a consistent DCID length | |
218 | * for communication with any given set of remote peers. | |
219 | * | |
19571483 | 220 | * The packet is queued regardless of whether it is able to be sent immediately. |
948c656c HL |
221 | * This enables packets to be batched and sent at once on systems which support |
222 | * system calls to send multiple datagrams in a single system call (see | |
223 | * BIO_sendmmsg). To flush queued datagrams to the network, see | |
224 | * ossl_qtx_flush_net(). | |
225 | * | |
19571483 HL |
226 | * Returns 1 on success or 0 on failure. |
227 | */ | |
228 | int ossl_qtx_write_pkt(OSSL_QTX *qtx, const OSSL_QTX_PKT *pkt); | |
229 | ||
230 | /* | |
231 | * Finish any incomplete datagrams for transmission which were flagged for | |
232 | * coalescing. If there is no current coalescing datagram, this is a no-op. | |
233 | */ | |
234 | void ossl_qtx_finish_dgram(OSSL_QTX *qtx); | |
235 | ||
236 | /* | |
237 | * (Attempt to) flush any datagrams which are queued for transmission. Note that | |
238 | * this does not cancel coalescing; call ossl_qtx_finish_dgram() first if that | |
239 | * is desired. The queue is drained into the OS's sockets as much as possible. | |
240 | * To determine if there is still data to be sent after calling this function, | |
241 | * use ossl_qtx_get_queue_len_bytes(). | |
0550829f HL |
242 | * |
243 | * Returns one of the following values: | |
244 | * | |
245 | * QTX_FLUSH_NET_RES_OK | |
246 | * Either no packets are currently queued for transmission, | |
247 | * or at least one packet was successfully submitted. | |
248 | * | |
249 | * QTX_FLUSH_NET_RES_TRANSIENT_FAIL | |
250 | * The underlying network write BIO indicated a transient error | |
251 | * (e.g. buffers full). | |
252 | * | |
253 | * QTX_FLUSH_NET_RES_PERMANENT_FAIL | |
254 | * Internal error (e.g. assertion or allocation error) | |
255 | * or the underlying network write BIO indicated a non-transient | |
256 | * error. | |
19571483 | 257 | */ |
0550829f HL |
258 | #define QTX_FLUSH_NET_RES_OK 1 |
259 | #define QTX_FLUSH_NET_RES_TRANSIENT_FAIL (-1) | |
260 | #define QTX_FLUSH_NET_RES_PERMANENT_FAIL (-2) | |
261 | ||
262 | int ossl_qtx_flush_net(OSSL_QTX *qtx); | |
19571483 HL |
263 | |
264 | /* | |
265 | * Diagnostic function. If there is any datagram pending transmission, pops it | |
266 | * and writes the details of the datagram as they would have been passed to | |
267 | * *msg. Returns 1, or 0 if there are no datagrams pending. For test use only. | |
268 | */ | |
269 | int ossl_qtx_pop_net(OSSL_QTX *qtx, BIO_MSG *msg); | |
270 | ||
271 | /* Returns number of datagrams which are fully-formed but not yet sent. */ | |
272 | size_t ossl_qtx_get_queue_len_datagrams(OSSL_QTX *qtx); | |
273 | ||
274 | /* | |
275 | * Returns number of payload bytes across all datagrams which are fully-formed | |
276 | * but not yet sent. Does not count any incomplete coalescing datagram. | |
277 | */ | |
278 | size_t ossl_qtx_get_queue_len_bytes(OSSL_QTX *qtx); | |
279 | ||
280 | /* | |
281 | * Returns number of bytes in the current coalescing datagram, or 0 if there is | |
282 | * no current coalescing datagram. Returns 0 after a call to | |
283 | * ossl_qtx_finish_dgram(). | |
284 | */ | |
285 | size_t ossl_qtx_get_cur_dgram_len_bytes(OSSL_QTX *qtx); | |
286 | ||
287 | /* | |
288 | * Returns number of queued coalesced packets which have not been put into a | |
289 | * datagram yet. If this is non-zero, ossl_qtx_flush_pkt() needs to be called. | |
290 | */ | |
291 | size_t ossl_qtx_get_unflushed_pkt_count(OSSL_QTX *qtx); | |
292 | ||
293 | /* | |
294 | * Change the BIO being used by the QTX. May be NULL if actual transmission is | |
81b6b43c HL |
295 | * not currently required. Does not up-ref the BIO; the caller is responsible |
296 | * for ensuring the lifetime of the BIO exceeds the lifetime of the QTX. | |
19571483 | 297 | */ |
cdd3f732 | 298 | void ossl_qtx_set_bio(OSSL_QTX *qtx, BIO *bio); |
19571483 HL |
299 | |
300 | /* Changes the MDPL. */ | |
301 | int ossl_qtx_set_mdpl(OSSL_QTX *qtx, size_t mdpl); | |
302 | ||
a73078b7 HL |
303 | /* Retrieves the current MDPL. */ |
304 | size_t ossl_qtx_get_mdpl(OSSL_QTX *qtx); | |
305 | ||
19571483 HL |
306 | |
307 | /* | |
308 | * Key Update | |
309 | * ---------- | |
310 | * | |
311 | * For additional discussion of key update considerations, see QRX header file. | |
312 | */ | |
313 | ||
314 | /* | |
315 | * Triggers a key update. The key update will be started by inverting the Key | |
316 | * Phase bit of the next packet transmitted; no key update occurs until the next | |
317 | * packet is transmitted. Thus, this function should generally be called | |
318 | * immediately before queueing the next packet. | |
319 | * | |
320 | * There are substantial requirements imposed by RFC 9001 on under what | |
321 | * circumstances a key update can be initiated. The caller is responsible for | |
322 | * meeting most of these requirements. For example, this function cannot be | |
323 | * called too soon after a previous key update has occurred. Key updates also | |
324 | * cannot be initiated until the 1-RTT encryption level is reached. | |
325 | * | |
326 | * As a sanity check, this function will fail and return 0 if the non-1RTT | |
327 | * encryption levels have not yet been dropped. | |
328 | * | |
329 | * The caller may decide itself to initiate a key update, but it also MUST | |
330 | * initiate a key update where it detects that the peer has initiated a key | |
331 | * update. The caller is responsible for initiating a TX key update by calling | |
332 | * this function in this circumstance; thus, the caller is responsible for | |
333 | * coupling the RX and TX QUIC record layers in this way. | |
334 | */ | |
335 | int ossl_qtx_trigger_key_update(OSSL_QTX *qtx); | |
336 | ||
337 | ||
338 | /* | |
339 | * Key Expiration | |
340 | * -------------- | |
341 | */ | |
342 | ||
343 | /* | |
344 | * Returns the number of packets which have been encrypted for transmission with | |
345 | * the current set of TX keys (the current "TX key epoch"). Reset to zero after | |
346 | * a key update and incremented for each packet queued. If enc_level is not | |
347 | * valid or relates to an EL which is not currently available, returns | |
348 | * UINT64_MAX. | |
349 | */ | |
350 | uint64_t ossl_qtx_get_cur_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level); | |
351 | ||
352 | /* | |
353 | * Returns the maximum number of packets which the record layer will permit to | |
354 | * be encrypted using the current set of TX keys. If this limit is reached (that | |
355 | * is, if the counter returned by ossl_qrx_tx_get_cur_epoch_pkt_count() reaches | |
356 | * this value), as a safety measure, the QTX will not permit any further packets | |
357 | * to be queued. All calls to ossl_qrx_write_pkt that try to send packets of a | |
358 | * kind which need to be encrypted will fail. It is not possible to recover from | |
359 | * this condition and the QTX must then be destroyed; therefore, callers should | |
360 | * ensure they always trigger a key update well in advance of reaching this | |
361 | * limit. | |
362 | * | |
363 | * The value returned by this function is based on the ciphersuite configured | |
364 | * for the given encryption level. If keys have not been provisioned for the | |
365 | * specified enc_level or the enc_level argument is invalid, this function | |
366 | * returns UINT64_MAX, which is not a valid value. Note that it is not possible | |
367 | * to perform a key update at any encryption level other than 1-RTT, therefore | |
368 | * if this limit is reached at earlier encryption levels (which should not be | |
369 | * possible) the connection must be terminated. Since this condition precludes | |
370 | * the transmission of further packets, the only possible signalling of such an | |
371 | * error condition to a peer is a Stateless Reset packet. | |
372 | */ | |
373 | uint64_t ossl_qtx_get_max_epoch_pkt_count(OSSL_QTX *qtx, uint32_t enc_level); | |
374 | ||
16f3b542 HL |
375 | /* |
376 | * Get the 1-RTT EL key epoch number for the QTX. This is intended for | |
377 | * diagnostic purposes. Returns 0 if 1-RTT EL is not provisioned yet. | |
378 | */ | |
379 | uint64_t ossl_qtx_get_key_epoch(OSSL_QTX *qtx); | |
380 | ||
6292519c HL |
381 | # endif |
382 | ||
19571483 | 383 | #endif |