2
0

quic_lcidm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 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_INTERNAL_QUIC_LCIDM_H
  10. # define OSSL_INTERNAL_QUIC_LCIDM_H
  11. # pragma once
  12. # include "internal/e_os.h"
  13. # include "internal/time.h"
  14. # include "internal/quic_types.h"
  15. # include "internal/quic_wire.h"
  16. # include "internal/quic_predef.h"
  17. # ifndef OPENSSL_NO_QUIC
  18. /*
  19. * QUIC Local Connection ID Manager
  20. * ================================
  21. *
  22. * This manages connection IDs for the RX side, which is to say that it issues
  23. * local CIDs (LCIDs) to a peer which that peer can then use to address us via a
  24. * packet DCID. This is as opposed to CID management for the TX side, which
  25. * determines which CIDs we use to transmit based on remote CIDs (RCIDs) the
  26. * peer sent to us.
  27. *
  28. * An opaque pointer can be associated with each LCID. Pointer identity
  29. * (equality) is used to distinguish distinct connections.
  30. *
  31. * LCIDs fall into three categories:
  32. *
  33. * 1. A client's Initial ODCID (1)
  34. * 2. Our local Initial SCID (1)
  35. * 3. A CID issued via a NEW_CONNECTION_ID frame (n)
  36. * 4. A server's Retry SCID (0..1)
  37. *
  38. * (1) is enrolled using ossl_quic_lcidm_enrol_odcid() and retired by the time
  39. * of handshake completion at the latest. It is needed in case the first
  40. * response packet from a server is lost and the client keeps using its Initial
  41. * ODCID. There is never more than one of these, and no sequence number is
  42. * associated with this temporary LCID.
  43. *
  44. * (2) is created by a client when it begins connecting, or by a server when it
  45. * responds to a new connection request. In the latter case, it is generated by
  46. * the server as the preferred DCID for traffic directed towards it. A client
  47. * should switch to using this as a RCID as soon as it receives a valid packet
  48. * from the server. This LCID has a sequence number of 0.
  49. *
  50. * (3) is created when we issue a NEW_CONNECTION_ID frame. Arbitrarily many of
  51. * these can exist.
  52. *
  53. * (4) is a special case. When a server issues a retry it generates a new SCID
  54. * much as it does for (2). However since retries are supposed to be stateless,
  55. * we don't actually register it as an LCID. When the client subsequently
  56. * replies with an Initial packet with token in response to the Retry, the
  57. * server will handle this as a new connection attempt due to not recognising
  58. * the DCID, which is what we want anyway. (The Retry SCID is subsequently
  59. * validated as matching the new Initial ODCID via attestation in the encrypted
  60. * contents of the opaque retry token.) Thus, the LCIDM is not actually involved
  61. * at all here.
  62. *
  63. * Retirement is as follows:
  64. *
  65. * (1) is retired automatically when we know it won't be needed anymore. This is
  66. * when the handshake is completed at the latest, and could potentially be
  67. * earlier.
  68. *
  69. * Both (2) and (3) are retired normally via RETIRE_CONNECTION_ID frames, as it
  70. * has a sequence number of 0.
  71. *
  72. *
  73. * ODCID Peculiarities
  74. * -------------------
  75. *
  76. * Almost all LCIDs are issued by the receiver responsible for routing them,
  77. * which means that almost all LCIDs will have the same length (specified in
  78. * lcid_len below). The only exception to this is (1); the ODCID is the only
  79. * case where we recognise an LCID we didn't ourselves generate. Since an ODCID
  80. * is chosen by the peer, it can be any length and doesn't necessarily match the
  81. * length we use for LCIDs we generate ourselves.
  82. *
  83. * Since DCID decoding for short-header packets requires an implicitly known
  84. * DCID length, it logically follows that an ODCID can never be used in a 1-RTT
  85. * packet. This is fine as by the time the 1-RTT EL is reached the peer should
  86. * already have switched away from the ODCID to a CID we generated ourselves,
  87. * and if this has not happened we can consider that a protocol violation.
  88. *
  89. * In any case, this means that the LCIDM must necessarily support LCIDs of
  90. * different lengths, even if it always generates LCIDs of a given length.
  91. *
  92. * An ODCID has no sequence number associated with it. It is the only CID to
  93. * lack one.
  94. */
  95. /*
  96. * Creates a new LCIDM. lcid_len is the length to use for LCIDs in bytes, which
  97. * may be zero.
  98. *
  99. * Returns NULL on failure.
  100. */
  101. QUIC_LCIDM *ossl_quic_lcidm_new(OSSL_LIB_CTX *libctx, size_t lcid_len);
  102. /* Frees a LCIDM. */
  103. void ossl_quic_lcidm_free(QUIC_LCIDM *lcidm);
  104. /* Gets the local CID length this LCIDM was configured to use. */
  105. size_t ossl_quic_lcidm_get_lcid_len(const QUIC_LCIDM *lcidm);
  106. /*
  107. * Determines the number of active LCIDs (i.e,. LCIDs which can be used for
  108. * reception) currently associated with the given opaque pointer.
  109. */
  110. size_t ossl_quic_lcidm_get_num_active_lcid(const QUIC_LCIDM *lcidm,
  111. void *opaque);
  112. /*
  113. * Enrol an Initial ODCID sent by the peer. This is the DCID in the first
  114. * Initial packet sent by a client. When we receive a client's first Initial
  115. * packet, we immediately respond with our own SCID (generated using
  116. * ossl_quic_lcidm_generate_initial) to tell the client to switch to using that,
  117. * so ideally the ODCID will only be used for a single packet. However since
  118. * that response might be lost, we also need to accept additional packets using
  119. * the ODCID and need to make sure they get routed to the same connection and
  120. * not interpreted as another new connection attempt. Thus before the CID
  121. * switchover is confirmed, we also have to handle incoming packets addressed to
  122. * the ODCID. This function is used to temporarily enroll the ODCID for a
  123. * connection. Such a LCID is considered to have a sequence number of
  124. * LCIDM_ODCID_SEQ_NUM internally for our purposes.
  125. *
  126. * Note that this is the *only* circumstance where we recognise an LCID we did
  127. * not generate ourselves, or allow an LCID with a different length to lcid_len.
  128. *
  129. * An ODCID MUST be at least 8 bytes in length (RFC 9000 s. 7.2).
  130. *
  131. * This function may only be called once for a given connection.
  132. * Returns 1 on success or 0 on failure.
  133. */
  134. int ossl_quic_lcidm_enrol_odcid(QUIC_LCIDM *lcidm, void *opaque,
  135. const QUIC_CONN_ID *initial_odcid);
  136. /*
  137. * Retire a previously enrolled ODCID for a connection. This is generally done
  138. * when we know the peer won't be using it any more (when the handshake is
  139. * completed at the absolute latest, possibly earlier).
  140. *
  141. * Returns 1 if there was an enrolled ODCID which was retired and 0 if there was
  142. * not or on other failure.
  143. */
  144. int ossl_quic_lcidm_retire_odcid(QUIC_LCIDM *lcidm, void *opaque);
  145. /*
  146. * Create the first LCID for a given opaque pointer. The generated LCID is
  147. * written to *initial_lcid and associated with the given opaque pointer.
  148. *
  149. * After this function returns successfully, the caller can for example
  150. * register the new LCID with a DEMUX.
  151. *
  152. * May not be called more than once for a given opaque pointer value.
  153. */
  154. int ossl_quic_lcidm_generate_initial(QUIC_LCIDM *lcidm,
  155. void *opaque,
  156. QUIC_CONN_ID *initial_lcid);
  157. /*
  158. * Create a subsequent LCID for a given opaque pointer. The information needed
  159. * for a NEW_CONN_ID frame informing the peer of the new LCID, including the
  160. * LCID itself, is written to *ncid_frame.
  161. *
  162. * ncid_frame->stateless_reset is not initialised and the caller is responsible
  163. * for setting it.
  164. *
  165. * After this function returns successfully, the caller can for example
  166. * register the new LCID with a DEMUX and queue the NEW_CONN_ID frame.
  167. */
  168. int ossl_quic_lcidm_generate(QUIC_LCIDM *lcidm,
  169. void *opaque,
  170. OSSL_QUIC_FRAME_NEW_CONN_ID *ncid_frame);
  171. /*
  172. * Retire up to one LCID for a given opaque pointer value. Called repeatedly to
  173. * handle a RETIRE_CONN_ID frame.
  174. *
  175. * If containing_pkt_dcid is non-NULL, this function enforces the requirement
  176. * that a CID not be retired by a packet using that CID as the DCID. If
  177. * containing_pkt_dcid is NULL, this check is skipped.
  178. *
  179. * If a LCID is retired as a result of a call to this function, the LCID which
  180. * was retired is written to *retired_lcid, the sequence number of the LCID is
  181. * written to *retired_seq_num and *did_retire is set to 1. Otherwise,
  182. * *did_retire is set to 0. This enables a caller to e.g. unregister the LCID
  183. * from a DEMUX. A caller should call this function repeatedly until the
  184. * function returns with *did_retire set to 0.
  185. *
  186. * This call is likely to cause the value returned by
  187. * ossl_quic_lcidm_get_num_active_lcid() to go down. A caller may wish to call
  188. * ossl_quic_lcidm_generate() repeatedly to bring the number of active LCIDs
  189. * back up to some threshold in response after calling this function.
  190. *
  191. * Returns 1 on success and 0 on failure. If arguments are valid but zero LCIDs
  192. * are retired, this is considered a success condition.
  193. */
  194. int ossl_quic_lcidm_retire(QUIC_LCIDM *lcidm,
  195. void *opaque,
  196. uint64_t retire_prior_to,
  197. const QUIC_CONN_ID *containing_pkt_dcid,
  198. QUIC_CONN_ID *retired_lcid,
  199. uint64_t *retired_seq_num,
  200. int *did_retire);
  201. /*
  202. * Cull all LCIDM state relating to a given opaque pointer value. This is useful
  203. * if connection state is spontaneously freed. The caller is responsible for
  204. * e.g. DEMUX state updates.
  205. */
  206. int ossl_quic_lcidm_cull(QUIC_LCIDM *lcidm, void *opaque);
  207. /*
  208. * Lookup a LCID. If the LCID is found, writes the associated opaque pointer to
  209. * *opaque and the associated sequence number to *seq_num. Returns 1 on success
  210. * and 0 if an entry is not found. An output argument may be set to NULL if its
  211. * value is not required.
  212. *
  213. * If the LCID is for an Initial ODCID, *seq_num is set to
  214. * LCIDM_ODCID_SEQ_NUM.
  215. */
  216. #define LCIDM_ODCID_SEQ_NUM UINT64_MAX
  217. int ossl_quic_lcidm_lookup(QUIC_LCIDM *lcidm,
  218. const QUIC_CONN_ID *lcid,
  219. uint64_t *seq_num,
  220. void **opaque);
  221. /*
  222. * Debug call to manually remove a specific LCID. Should not be needed in normal
  223. * usage. Returns 1 if the LCID was successfully found and removed and 0
  224. * otherwise.
  225. */
  226. int ossl_quic_lcidm_debug_remove(QUIC_LCIDM *lcidm,
  227. const QUIC_CONN_ID *lcid);
  228. /*
  229. * Debug call to manually add a numbered LCID with a specific CID value and
  230. * sequence number. Should not be needed in normal usage. Returns 1 on success
  231. * and 0 on failure.
  232. */
  233. int ossl_quic_lcidm_debug_add(QUIC_LCIDM *lcidm, void *opaque,
  234. const QUIC_CONN_ID *lcid,
  235. uint64_t seq_num);
  236. # endif
  237. #endif