quic_wire.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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_INTERNAL_QUIC_WIRE_H
  10. # define OSSL_INTERNAL_QUIC_WIRE_H
  11. # pragma once
  12. #include "internal/e_os.h"
  13. #include "internal/time.h"
  14. #include "internal/quic_types.h"
  15. #include "internal/packet.h"
  16. #define OSSL_QUIC_FRAME_TYPE_PADDING 0x00
  17. #define OSSL_QUIC_FRAME_TYPE_PING 0x01
  18. #define OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN 0x02
  19. #define OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN 0x03
  20. #define OSSL_QUIC_FRAME_TYPE_RESET_STREAM 0x04
  21. #define OSSL_QUIC_FRAME_TYPE_STOP_SENDING 0x05
  22. #define OSSL_QUIC_FRAME_TYPE_CRYPTO 0x06
  23. #define OSSL_QUIC_FRAME_TYPE_NEW_TOKEN 0x07
  24. #define OSSL_QUIC_FRAME_TYPE_MAX_DATA 0x10
  25. #define OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA 0x11
  26. #define OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI 0x12
  27. #define OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI 0x13
  28. #define OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED 0x14
  29. #define OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED 0x15
  30. #define OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI 0x16
  31. #define OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI 0x17
  32. #define OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID 0x18
  33. #define OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID 0x19
  34. #define OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE 0x1A
  35. #define OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE 0x1B
  36. #define OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT 0x1C
  37. #define OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP 0x1D
  38. #define OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE 0x1E
  39. #define OSSL_QUIC_FRAME_FLAG_STREAM_FIN 0x01
  40. #define OSSL_QUIC_FRAME_FLAG_STREAM_LEN 0x02
  41. #define OSSL_QUIC_FRAME_FLAG_STREAM_OFF 0x04
  42. #define OSSL_QUIC_FRAME_FLAG_STREAM_MASK ((uint64_t)0x07)
  43. /* Low 3 bits of the type contain flags */
  44. #define OSSL_QUIC_FRAME_TYPE_STREAM 0x08 /* base ID */
  45. #define OSSL_QUIC_FRAME_TYPE_STREAM_FIN \
  46. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  47. OSSL_QUIC_FRAME_FLAG_STREAM_FIN)
  48. #define OSSL_QUIC_FRAME_TYPE_STREAM_LEN \
  49. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  50. OSSL_QUIC_FRAME_FLAG_STREAM_LEN)
  51. #define OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN \
  52. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  53. OSSL_QUIC_FRAME_FLAG_STREAM_LEN | \
  54. OSSL_QUIC_FRAME_FLAG_STREAM_FIN)
  55. #define OSSL_QUIC_FRAME_TYPE_STREAM_OFF \
  56. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  57. OSSL_QUIC_FRAME_FLAG_STREAM_OFF)
  58. #define OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN \
  59. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  60. OSSL_QUIC_FRAME_FLAG_STREAM_OFF | \
  61. OSSL_QUIC_FRAME_FLAG_STREAM_FIN)
  62. #define OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN \
  63. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  64. OSSL_QUIC_FRAME_FLAG_STREAM_OFF | \
  65. OSSL_QUIC_FRAME_FLAG_STREAM_LEN)
  66. #define OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN \
  67. (OSSL_QUIC_FRAME_TYPE_STREAM | \
  68. OSSL_QUIC_FRAME_FLAG_STREAM_OFF | \
  69. OSSL_QUIC_FRAME_FLAG_STREAM_LEN | \
  70. OSSL_QUIC_FRAME_FLAG_STREAM_FIN)
  71. #define OSSL_QUIC_FRAME_TYPE_IS_STREAM(x) \
  72. (((x) & ~OSSL_QUIC_FRAME_FLAG_STREAM_MASK) == OSSL_QUIC_FRAME_TYPE_STREAM)
  73. #define OSSL_QUIC_FRAME_TYPE_IS_ACK(x) \
  74. (((x) & ~(uint64_t)1) == OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN)
  75. #define OSSL_QUIC_FRAME_TYPE_IS_MAX_STREAMS(x) \
  76. (((x) & ~(uint64_t)1) == OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
  77. #define OSSL_QUIC_FRAME_TYPE_IS_STREAMS_BLOCKED(x) \
  78. (((x) & ~(uint64_t)1) == OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  79. #define OSSL_QUIC_FRAME_TYPE_IS_CONN_CLOSE(x) \
  80. (((x) & ~(uint64_t)1) == OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)
  81. static ossl_unused ossl_inline int
  82. ossl_quic_frame_type_is_ack_eliciting(uint64_t frame_type)
  83. {
  84. switch (frame_type) {
  85. case OSSL_QUIC_FRAME_TYPE_PADDING:
  86. case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
  87. case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
  88. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
  89. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
  90. return 0;
  91. default:
  92. return 1;
  93. }
  94. }
  95. /*
  96. * QUIC Frame Logical Representations
  97. * ==================================
  98. */
  99. /* QUIC Frame: ACK */
  100. typedef struct ossl_quic_ack_range_st {
  101. /*
  102. * Represents an inclusive range of packet numbers [start, end].
  103. * start must be <= end.
  104. */
  105. QUIC_PN start, end;
  106. } OSSL_QUIC_ACK_RANGE;
  107. typedef struct ossl_quic_frame_ack_st {
  108. /*
  109. * A sequence of packet number ranges [[start, end]...].
  110. *
  111. * The ranges must be sorted in descending order, for example:
  112. * [ 95, 100]
  113. * [ 90, 92]
  114. * etc.
  115. *
  116. * As such, ack_ranges[0].end is always the highest packet number
  117. * being acknowledged and ack_ranges[num_ack_ranges-1].start is
  118. * always the lowest packet number being acknowledged.
  119. *
  120. * num_ack_ranges must be greater than zero, as an ACK frame must
  121. * acknowledge at least one packet number.
  122. */
  123. OSSL_QUIC_ACK_RANGE *ack_ranges;
  124. size_t num_ack_ranges;
  125. OSSL_TIME delay_time;
  126. uint64_t ect0, ect1, ecnce;
  127. unsigned int ecn_present : 1;
  128. } OSSL_QUIC_FRAME_ACK;
  129. /* QUIC Frame: STREAM */
  130. typedef struct ossl_quic_frame_stream_st {
  131. uint64_t stream_id; /* Stream ID */
  132. uint64_t offset; /* Logical offset in stream */
  133. uint64_t len; /* Length of data in bytes */
  134. const unsigned char *data;
  135. /*
  136. * On encode, this determines whether the len field should be encoded or
  137. * not. If zero, the len field is not encoded and it is assumed the frame
  138. * runs to the end of the packet.
  139. *
  140. * On decode, this determines whether the frame had an explicitly encoded
  141. * length. If not set, the frame runs to the end of the packet and len has
  142. * been set accordingly.
  143. */
  144. unsigned int has_explicit_len : 1;
  145. /* 1 if this is the end of the stream */
  146. unsigned int is_fin : 1;
  147. } OSSL_QUIC_FRAME_STREAM;
  148. /* QUIC Frame: CRYPTO */
  149. typedef struct ossl_quic_frame_crypto_st {
  150. uint64_t offset; /* Logical offset in stream */
  151. uint64_t len; /* Length of the data in bytes */
  152. const unsigned char *data;
  153. } OSSL_QUIC_FRAME_CRYPTO;
  154. /* QUIC Frame: RESET_STREAM */
  155. typedef struct ossl_quic_frame_reset_stream_st {
  156. uint64_t stream_id;
  157. uint64_t app_error_code;
  158. uint64_t final_size;
  159. } OSSL_QUIC_FRAME_RESET_STREAM;
  160. /* QUIC Frame: STOP_SENDING */
  161. typedef struct ossl_quic_frame_stop_sending_st {
  162. uint64_t stream_id;
  163. uint64_t app_error_code;
  164. } OSSL_QUIC_FRAME_STOP_SENDING;
  165. /* QUIC Frame: NEW_CONNECTION_ID */
  166. typedef struct ossl_quic_frame_new_conn_id_st {
  167. uint64_t seq_num;
  168. uint64_t retire_prior_to;
  169. QUIC_CONN_ID conn_id;
  170. unsigned char stateless_reset_token[16];
  171. } OSSL_QUIC_FRAME_NEW_CONN_ID;
  172. /* QUIC Frame: CONNECTION_CLOSE */
  173. typedef struct ossl_quic_frame_conn_close_st {
  174. unsigned int is_app : 1; /* 0: transport error, 1: app error */
  175. uint64_t error_code; /* 62-bit transport or app error code */
  176. uint64_t frame_type; /* transport errors only */
  177. char *reason; /* UTF-8 string, not necessarily zero-terminated */
  178. size_t reason_len; /* Length of reason in bytes */
  179. } OSSL_QUIC_FRAME_CONN_CLOSE;
  180. /*
  181. * QUIC Wire Format Encoding
  182. * =========================
  183. *
  184. * These functions return 1 on success and 0 on failure.
  185. */
  186. /*
  187. * Encodes zero or more QUIC PADDING frames to the packet writer. Each PADDING
  188. * frame consumes one byte; num_bytes specifies the number of bytes of padding
  189. * to write.
  190. */
  191. int ossl_quic_wire_encode_padding(WPACKET *pkt, size_t num_bytes);
  192. /*
  193. * Encodes a QUIC PING frame to the packet writer. This frame type takes
  194. * no arguments.
  195. */
  196. int ossl_quic_wire_encode_frame_ping(WPACKET *pkt);
  197. /*
  198. * Encodes a QUIC ACK frame to the packet writer, given a logical representation
  199. * of the ACK frame.
  200. *
  201. * The ACK ranges passed must be sorted in descending order.
  202. *
  203. * The logical representation stores a list of packet number ranges. The wire
  204. * encoding is slightly different and stores the first range in the list
  205. * in a different manner.
  206. *
  207. * The ack_delay_exponent argument specifies the index of a power of two by
  208. * which the ack->ack_delay field is be divided. This exponent value must match
  209. * the value used when decoding.
  210. */
  211. int ossl_quic_wire_encode_frame_ack(WPACKET *pkt,
  212. uint32_t ack_delay_exponent,
  213. const OSSL_QUIC_FRAME_ACK *ack);
  214. /*
  215. * Encodes a QUIC RESET_STREAM frame to the packet writer, given a logical
  216. * representation of the RESET_STREAM frame.
  217. */
  218. int ossl_quic_wire_encode_frame_reset_stream(WPACKET *pkt,
  219. const OSSL_QUIC_FRAME_RESET_STREAM *f);
  220. /*
  221. * Encodes a QUIC STOP_SENDING frame to the packet writer, given a logical
  222. * representation of the STOP_SENDING frame.
  223. */
  224. int ossl_quic_wire_encode_frame_stop_sending(WPACKET *pkt,
  225. const OSSL_QUIC_FRAME_STOP_SENDING *f);
  226. /*
  227. * Encodes a QUIC CRYPTO frame header to the packet writer.
  228. *
  229. * To create a well-formed frame, the data written using this function must be
  230. * immediately followed by f->len bytes of data.
  231. */
  232. int ossl_quic_wire_encode_frame_crypto_hdr(WPACKET *hdr,
  233. const OSSL_QUIC_FRAME_CRYPTO *f);
  234. /*
  235. * Returns the number of bytes which will be required to encode the given
  236. * CRYPTO frame header. Does not include the payload bytes in the count.
  237. * Returns 0 if input is invalid.
  238. */
  239. size_t ossl_quic_wire_get_encoded_frame_len_crypto_hdr(const OSSL_QUIC_FRAME_CRYPTO *f);
  240. /*
  241. * Encodes a QUIC CRYPTO frame to the packet writer.
  242. *
  243. * This function returns a pointer to a buffer of f->len bytes which the caller
  244. * should fill however it wishes. If f->data is non-NULL, it is automatically
  245. * copied to the target buffer, otherwise the caller must fill the returned
  246. * buffer. Returns NULL on failure.
  247. */
  248. void *ossl_quic_wire_encode_frame_crypto(WPACKET *pkt,
  249. const OSSL_QUIC_FRAME_CRYPTO *f);
  250. /*
  251. * Encodes a QUIC NEW_TOKEN frame to the packet writer.
  252. */
  253. int ossl_quic_wire_encode_frame_new_token(WPACKET *pkt,
  254. const unsigned char *token,
  255. size_t token_len);
  256. /*
  257. * Encodes a QUIC STREAM frame's header to the packet writer. The f->stream_id,
  258. * f->offset and f->len fields are the values for the respective Stream ID,
  259. * Offset and Length fields.
  260. *
  261. * If f->is_fin is non-zero, the frame is marked as the final frame in the
  262. * stream.
  263. *
  264. * If f->has_explicit_len is zerro, the frame is assumed to be the final frame
  265. * in the packet, which the caller is responsible for ensuring; the Length
  266. * field is then omitted.
  267. *
  268. * To create a well-formed frame, the data written using this function must be
  269. * immediately followed by f->len bytes of stream data.
  270. */
  271. int ossl_quic_wire_encode_frame_stream_hdr(WPACKET *pkt,
  272. const OSSL_QUIC_FRAME_STREAM *f);
  273. /*
  274. * Returns the number of bytes which will be required to encode the given
  275. * STREAM frame header. Does not include the payload bytes in the count.
  276. * Returns 0 if input is invalid.
  277. */
  278. size_t ossl_quic_wire_get_encoded_frame_len_stream_hdr(const OSSL_QUIC_FRAME_STREAM *f);
  279. /*
  280. * Functions similarly to ossl_quic_wire_encode_frame_stream_hdr, but it also
  281. * allocates space for f->len bytes of data after the header, creating a
  282. * well-formed QUIC STREAM frame in one call.
  283. *
  284. * A pointer to the bytes allocated for the framme payload is returned,
  285. * which the caller can fill however it wishes. If f->data is non-NULL,
  286. * it is automatically copied to the target buffer, otherwise the caller
  287. * must fill the returned buffer. Returns NULL on failure.
  288. */
  289. void *ossl_quic_wire_encode_frame_stream(WPACKET *pkt,
  290. const OSSL_QUIC_FRAME_STREAM *f);
  291. /*
  292. * Encodes a QUIC MAX_DATA frame to the packet writer.
  293. */
  294. int ossl_quic_wire_encode_frame_max_data(WPACKET *pkt,
  295. uint64_t max_data);
  296. /*
  297. * Encodes a QUIC MAX_STREAM_DATA frame to the packet writer.
  298. */
  299. int ossl_quic_wire_encode_frame_max_stream_data(WPACKET *pkt,
  300. uint64_t stream_id,
  301. uint64_t max_data);
  302. /*
  303. * Encodes a QUIC MAX_STREAMS frame to the packet writer.
  304. *
  305. * If is_uni is 0, the count specifies the maximum number of
  306. * bidirectional streams; else it specifies the maximum number of unidirectional
  307. * streams.
  308. */
  309. int ossl_quic_wire_encode_frame_max_streams(WPACKET *pkt,
  310. char is_uni,
  311. uint64_t max_streams);
  312. /*
  313. * Encodes a QUIC DATA_BLOCKED frame to the packet writer.
  314. */
  315. int ossl_quic_wire_encode_frame_data_blocked(WPACKET *pkt,
  316. uint64_t max_data);
  317. /*
  318. * Encodes a QUIC STREAM_DATA_BLOCKED frame to the packet writer.
  319. */
  320. int ossl_quic_wire_encode_frame_stream_data_blocked(WPACKET *pkt,
  321. uint64_t stream_id,
  322. uint64_t max_stream_data);
  323. /*
  324. * Encodes a QUIC STREAMS_BLOCKED frame to the packet writer.
  325. *
  326. * If is_uni is 0, the count specifies the maximum number of
  327. * bidirectional streams; else it specifies the maximum number of unidirectional
  328. * streams.
  329. */
  330. int ossl_quic_wire_encode_frame_streams_blocked(WPACKET *pkt,
  331. char is_uni,
  332. uint64_t max_streams);
  333. /*
  334. * Encodes a QUIC NEW_CONNECTION_ID frame to the packet writer, given a logical
  335. * representation of the NEW_CONNECTION_ID frame.
  336. *
  337. * The buffer pointed to by the conn_id field must be valid for the duration of
  338. * the call.
  339. */
  340. int ossl_quic_wire_encode_frame_new_conn_id(WPACKET *pkt,
  341. const OSSL_QUIC_FRAME_NEW_CONN_ID *f);
  342. /*
  343. * Encodes a QUIC RETIRE_CONNECTION_ID frame to the packet writer.
  344. */
  345. int ossl_quic_wire_encode_frame_retire_conn_id(WPACKET *pkt,
  346. uint64_t seq_num);
  347. /*
  348. * Encodes a QUIC PATH_CHALLENGE frame to the packet writer.
  349. */
  350. int ossl_quic_wire_encode_frame_path_challenge(WPACKET *pkt,
  351. uint64_t data);
  352. /*
  353. * Encodes a QUIC PATH_RESPONSE frame to the packet writer.
  354. */
  355. int ossl_quic_wire_encode_frame_path_response(WPACKET *pkt,
  356. uint64_t data);
  357. /*
  358. * Encodes a QUIC CONNECTION_CLOSE frame to the packet writer, given a logical
  359. * representation of the CONNECTION_CLOSE frame.
  360. *
  361. * The reason field may be NULL, in which case no reason is encoded. If the
  362. * reason field is non-NULL, it must point to a valid UTF-8 string and
  363. * reason_len must be set to the length of the reason string in bytes. The
  364. * reason string need not be zero terminated.
  365. */
  366. int ossl_quic_wire_encode_frame_conn_close(WPACKET *pkt,
  367. const OSSL_QUIC_FRAME_CONN_CLOSE *f);
  368. /*
  369. * Encodes a QUIC HANDSHAKE_DONE frame to the packet writer. This frame type
  370. * takes no arguiments.
  371. */
  372. int ossl_quic_wire_encode_frame_handshake_done(WPACKET *pkt);
  373. /*
  374. * Encodes a QUIC transport parameter TLV with the given ID into the WPACKET.
  375. * The payload is an arbitrary buffer.
  376. *
  377. * If value is non-NULL, the value is copied into the packet.
  378. * If it is NULL, value_len bytes are allocated for the payload and the caller
  379. * should fill the buffer using the returned pointer.
  380. *
  381. * Returns a pointer to the start of the payload on success, or NULL on failure.
  382. */
  383. unsigned char *ossl_quic_wire_encode_transport_param_bytes(WPACKET *pkt,
  384. uint64_t id,
  385. const unsigned char *value,
  386. size_t value_len);
  387. /*
  388. * Encodes a QUIC transport parameter TLV with the given ID into the WPACKET.
  389. * The payload is a QUIC variable-length integer with the given value.
  390. */
  391. int ossl_quic_wire_encode_transport_param_int(WPACKET *pkt,
  392. uint64_t id,
  393. uint64_t value);
  394. /*
  395. * QUIC Wire Format Decoding
  396. * =========================
  397. *
  398. * These functions return 1 on success or 0 for failure. Typical reasons
  399. * why these functions may fail include:
  400. *
  401. * - A frame decode function is called but the frame in the PACKET's buffer
  402. * is not of the correct type.
  403. *
  404. * - A variable-length field in the encoded frame appears to exceed the bounds
  405. * of the PACKET's buffer.
  406. *
  407. * These functions should be called with the PACKET pointing to the start of the
  408. * frame (including the initial type field), and consume an entire frame
  409. * including its type field. The expectation is that the caller will have
  410. * already discerned the frame type using ossl_quic_wire_peek_frame_header().
  411. */
  412. /*
  413. * Decodes the type field header of a QUIC frame (without advancing the current
  414. * position). This can be used to determine the frame type and determine which
  415. * frame decoding function to call.
  416. */
  417. int ossl_quic_wire_peek_frame_header(PACKET *pkt, uint64_t *type);
  418. /*
  419. * Like ossl_quic_wire_peek_frame_header, but advances the current position
  420. * so that the type field is consumed. For advanced use only.
  421. */
  422. int ossl_quic_wire_skip_frame_header(PACKET *pkt, uint64_t *type);
  423. /*
  424. * Determines how many ranges are needed to decode a QUIC ACK frame.
  425. *
  426. * The number of ranges which must be allocated before the call to
  427. * ossl_quic_wire_decode_frame_ack is written to *total_ranges.
  428. *
  429. * The PACKET is not advanced.
  430. */
  431. int ossl_quic_wire_peek_frame_ack_num_ranges(const PACKET *pkt,
  432. uint64_t *total_ranges);
  433. /*
  434. * Decodes a QUIC ACK frame. The ack_ranges field of the passed structure should
  435. * point to a preallocated array of ACK ranges and the num_ack_ranges field
  436. * should specify the length of allocation.
  437. *
  438. * *total_ranges is written with the number of ranges in the decoded frame,
  439. * which may be greater than the number of ranges which were decoded (i.e. if
  440. * num_ack_ranges was too small to decode all ranges).
  441. *
  442. * On success, this function modifies the num_ack_ranges field to indicate the
  443. * number of ranges in the decoded frame. This is the number of entries in the
  444. * ACK ranges array written by this function; any additional entries are not
  445. * modified.
  446. *
  447. * If the number of ACK ranges in the decoded frame exceeds that in
  448. * num_ack_ranges, as many ACK ranges as possible are decoded into the range
  449. * array. The caller can use the value written to *total_ranges to detect this
  450. * condition, as *total_ranges will exceed num_ack_ranges.
  451. *
  452. * If ack is NULL, the frame is still decoded, but only *total_ranges is
  453. * written. This can be used to determine the number of ranges which must be
  454. * allocated.
  455. *
  456. * The ack_delay_exponent argument specifies the index of a power of two used to
  457. * decode the ack_delay field. This must match the ack_delay_exponent value used
  458. * to encode the frame.
  459. */
  460. int ossl_quic_wire_decode_frame_ack(PACKET *pkt,
  461. uint32_t ack_delay_exponent,
  462. OSSL_QUIC_FRAME_ACK *ack,
  463. uint64_t *total_ranges);
  464. /*
  465. * Decodes a QUIC RESET_STREAM frame.
  466. */
  467. int ossl_quic_wire_decode_frame_reset_stream(PACKET *pkt,
  468. OSSL_QUIC_FRAME_RESET_STREAM *f);
  469. /*
  470. * Decodes a QUIC STOP_SENDING frame.
  471. */
  472. int ossl_quic_wire_decode_frame_stop_sending(PACKET *pkt,
  473. OSSL_QUIC_FRAME_STOP_SENDING *f);
  474. /*
  475. * Decodes a QUIC CRYPTO frame.
  476. *
  477. * f->data is set to point inside the packet buffer inside the PACKET, therefore
  478. * it is safe to access for as long as the packet buffer exists.
  479. */
  480. int ossl_quic_wire_decode_frame_crypto(PACKET *pkt,
  481. OSSL_QUIC_FRAME_CRYPTO *f);
  482. /*
  483. * Decodes a QUIC NEW_TOKEN frame. *token is written with a pointer to the token
  484. * bytes and *token_len is written with the length of the token in bytes.
  485. */
  486. int ossl_quic_wire_decode_frame_new_token(PACKET *pkt,
  487. const unsigned char **token,
  488. size_t *token_len);
  489. /*
  490. * Decodes a QUIC STREAM frame.
  491. *
  492. * If the frame did not contain an offset field, f->offset is set to 0, as the
  493. * absence of an offset field is equivalent to an offset of 0.
  494. *
  495. * If the frame contained a length field, f->has_explicit_len is set to 1 and
  496. * the length of the data is placed in f->len. This function ensures that the
  497. * length does not exceed the packet buffer, thus it is safe to access f->data.
  498. *
  499. * If the frame did not contain a length field, this means that the frame runs
  500. * until the end of the packet. This function sets f->has_explicit_len to zero,
  501. * and f->len to the amount of data remaining in the input buffer. Therefore,
  502. * this function should be used with a PACKET representing a single packet (and
  503. * not e.g. multiple packets).
  504. *
  505. * Note also that this means f->len is always valid after this function returns
  506. * successfully, regardless of the value of f->has_explicit_len.
  507. *
  508. * f->data points inside the packet buffer inside the PACKET, therefore it is
  509. * safe to access for as long as the packet buffer exists.
  510. *
  511. * f->is_fin is set according to whether the frame was marked as ending the
  512. * stream.
  513. */
  514. int ossl_quic_wire_decode_frame_stream(PACKET *pkt,
  515. OSSL_QUIC_FRAME_STREAM *f);
  516. /*
  517. * Decodes a QUIC MAX_DATA frame. The Maximum Data field is written to
  518. * *max_data.
  519. */
  520. int ossl_quic_wire_decode_frame_max_data(PACKET *pkt,
  521. uint64_t *max_data);
  522. /*
  523. * Decodes a QUIC MAX_STREAM_DATA frame. The Stream ID is written to *stream_id
  524. * and Maximum Stream Data field is written to *max_stream_data.
  525. */
  526. int ossl_quic_wire_decode_frame_max_stream_data(PACKET *pkt,
  527. uint64_t *stream_id,
  528. uint64_t *max_stream_data);
  529. /*
  530. * Decodes a QUIC MAX_STREAMS frame. The Maximum Streams field is written to
  531. * *max_streams.
  532. *
  533. * Whether the limit concerns bidirectional streams or unidirectional streams is
  534. * denoted by the frame type; the caller should examine the frame type to
  535. * determine this.
  536. */
  537. int ossl_quic_wire_decode_frame_max_streams(PACKET *pkt,
  538. uint64_t *max_streams);
  539. /*
  540. * Decodes a QUIC DATA_BLOCKED frame. The Maximum Data field is written to
  541. * *max_data.
  542. */
  543. int ossl_quic_wire_decode_frame_data_blocked(PACKET *pkt,
  544. uint64_t *max_data);
  545. /*
  546. * Decodes a QUIC STREAM_DATA_BLOCKED frame. The Stream ID and Maximum Stream
  547. * Data fields are written to *stream_id and *max_stream_data respectively.
  548. */
  549. int ossl_quic_wire_decode_frame_stream_data_blocked(PACKET *pkt,
  550. uint64_t *stream_id,
  551. uint64_t *max_stream_data);
  552. /*
  553. * Decodes a QUIC STREAMS_BLOCKED frame. The Maximum Streams field is written to
  554. * *max_streams.
  555. *
  556. * Whether the limit concerns bidirectional streams or unidirectional streams is
  557. * denoted by the frame type; the caller should examine the frame type to
  558. * determine this.
  559. */
  560. int ossl_quic_wire_decode_frame_streams_blocked(PACKET *pkt,
  561. uint64_t *max_streams);
  562. /*
  563. * Decodes a QUIC NEW_CONNECTION_ID frame. The logical representation of the
  564. * frame is written to *f.
  565. *
  566. * The conn_id field is set to point to the connection ID string inside the
  567. * packet buffer; it is therefore valid for as long as the PACKET's buffer is
  568. * valid. The conn_id_len field is set to the length of the connection ID string
  569. * in bytes.
  570. */
  571. int ossl_quic_wire_decode_frame_new_conn_id(PACKET *pkt,
  572. OSSL_QUIC_FRAME_NEW_CONN_ID *f);
  573. /*
  574. * Decodes a QUIC RETIRE_CONNECTION_ID frame. The Sequence Number field
  575. * is written to *seq_num.
  576. */
  577. int ossl_quic_wire_decode_frame_retire_conn_id(PACKET *pkt,
  578. uint64_t *seq_num);
  579. /*
  580. * Decodes a QUIC PATH_CHALLENGE frame. The Data field is written to *data.
  581. */
  582. int ossl_quic_wire_decode_frame_path_challenge(PACKET *pkt,
  583. uint64_t *data);
  584. /*
  585. * Decodes a QUIC PATH_CHALLENGE frame. The Data field is written to *data.
  586. */
  587. int ossl_quic_wire_decode_frame_path_response(PACKET *pkt,
  588. uint64_t *data);
  589. /*
  590. * Decodes a QUIC CONNECTION_CLOSE frame. The logical representation
  591. * of the frame is written to *f.
  592. *
  593. * The reason field is set to point to the UTF-8 reason string inside
  594. * the packet buffer; it is therefore valid for as long as the PACKET's
  595. * buffer is valid. The reason_len field is set to the length of the
  596. * reason string in bytes.
  597. *
  598. * IMPORTANT: The reason string is not zero-terminated.
  599. *
  600. * Returns 1 on success or 0 on failure.
  601. */
  602. int ossl_quic_wire_decode_frame_conn_close(PACKET *pkt,
  603. OSSL_QUIC_FRAME_CONN_CLOSE *f);
  604. /*
  605. * Decodes one or more PADDING frames. PADDING frames have no arguments.
  606. *
  607. * Returns the number of PADDING frames decoded or 0 on error.
  608. */
  609. size_t ossl_quic_wire_decode_padding(PACKET *pkt);
  610. /*
  611. * Decodes a PING frame. The frame has no arguments.
  612. */
  613. int ossl_quic_wire_decode_frame_ping(PACKET *pkt);
  614. /*
  615. * Decodes a HANDSHAKE_DONE frame. The frame has no arguments.
  616. */
  617. int ossl_quic_wire_decode_frame_handshake_done(PACKET *pkt);
  618. /*
  619. * Peeks at the ID of the next QUIC transport parameter TLV in the stream.
  620. * The ID is written to *id.
  621. */
  622. int ossl_quic_wire_peek_transport_param(PACKET *pkt, uint64_t *id);
  623. /*
  624. * Decodes a QUIC transport parameter TLV. A pointer to the value buffer is
  625. * returned on success. This points inside the PACKET's buffer and is therefore
  626. * valid as long as the PACKET's buffer is valid.
  627. *
  628. * The transport parameter ID is written to *id and the length of the payload
  629. * in bytes is written to *len.
  630. *
  631. * Returns NULL on failure.
  632. */
  633. const unsigned char *ossl_quic_wire_decode_transport_param_bytes(PACKET *pkt,
  634. uint64_t *id,
  635. size_t *len);
  636. /*
  637. * Decodes a QUIC transport parameter TLV containing a variable-length integer.
  638. *
  639. * The transport parameter ID is written to *id and the value is written to
  640. * *value.
  641. */
  642. int ossl_quic_wire_decode_transport_param_int(PACKET *pkt,
  643. uint64_t *id,
  644. uint64_t *value);
  645. #endif