quic_record_tx.h 13 KB

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