quic_wire.h 30 KB

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