quic_record_tx.h 12 KB

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