quic_record_tx.h 15 KB

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