quic_record_tx.h 15 KB

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