quic_wire.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. #include <openssl/macros.h>
  10. #include <openssl/objects.h>
  11. #include "quic_local.h"
  12. #include "internal/quic_vlint.h"
  13. #include "internal/quic_wire.h"
  14. OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)
  15. /*
  16. * QUIC Wire Format Encoding
  17. * =========================
  18. */
  19. int ossl_quic_wire_encode_padding(WPACKET *pkt, size_t num_bytes)
  20. {
  21. /*
  22. * PADDING is frame type zero, which as a variable-length integer is
  23. * represented as a single zero byte. As an optimisation, just use memset.
  24. */
  25. return WPACKET_memset(pkt, 0, num_bytes);
  26. }
  27. static int encode_frame_hdr(WPACKET *pkt, uint64_t frame_type)
  28. {
  29. return WPACKET_quic_write_vlint(pkt, frame_type);
  30. }
  31. int ossl_quic_wire_encode_frame_ping(WPACKET *pkt)
  32. {
  33. return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PING);
  34. }
  35. int ossl_quic_wire_encode_frame_ack(WPACKET *pkt,
  36. uint32_t ack_delay_exponent,
  37. const OSSL_QUIC_FRAME_ACK *ack)
  38. {
  39. uint64_t frame_type = ack->ecn_present ? OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN
  40. : OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
  41. uint64_t largest_ackd, first_ack_range, ack_delay_enc;
  42. size_t i, num_ack_ranges = ack->num_ack_ranges;
  43. OSSL_TIME delay;
  44. if (num_ack_ranges == 0)
  45. return 0;
  46. delay = ossl_time_divide(ossl_time_divide(ack->delay_time, OSSL_TIME_US),
  47. 1UL << ack_delay_exponent);
  48. ack_delay_enc = ossl_time2ticks(delay);
  49. largest_ackd = ack->ack_ranges[0].end;
  50. first_ack_range = ack->ack_ranges[0].end - ack->ack_ranges[0].start;
  51. if (!encode_frame_hdr(pkt, frame_type)
  52. || !WPACKET_quic_write_vlint(pkt, largest_ackd)
  53. || !WPACKET_quic_write_vlint(pkt, ack_delay_enc)
  54. || !WPACKET_quic_write_vlint(pkt, num_ack_ranges - 1)
  55. || !WPACKET_quic_write_vlint(pkt, first_ack_range))
  56. return 0;
  57. for (i = 1; i < num_ack_ranges; ++i) {
  58. uint64_t gap, range_len;
  59. gap = ack->ack_ranges[i - 1].start - ack->ack_ranges[i].end - 2;
  60. range_len = ack->ack_ranges[i].end - ack->ack_ranges[i].start;
  61. if (!WPACKET_quic_write_vlint(pkt, gap)
  62. || !WPACKET_quic_write_vlint(pkt, range_len))
  63. return 0;
  64. }
  65. if (ack->ecn_present)
  66. if (!WPACKET_quic_write_vlint(pkt, ack->ect0)
  67. || !WPACKET_quic_write_vlint(pkt, ack->ect1)
  68. || !WPACKET_quic_write_vlint(pkt, ack->ecnce))
  69. return 0;
  70. return 1;
  71. }
  72. int ossl_quic_wire_encode_frame_reset_stream(WPACKET *pkt,
  73. const OSSL_QUIC_FRAME_RESET_STREAM *f)
  74. {
  75. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  76. || !WPACKET_quic_write_vlint(pkt, f->stream_id)
  77. || !WPACKET_quic_write_vlint(pkt, f->app_error_code)
  78. || !WPACKET_quic_write_vlint(pkt, f->final_size))
  79. return 0;
  80. return 1;
  81. }
  82. int ossl_quic_wire_encode_frame_stop_sending(WPACKET *pkt,
  83. const OSSL_QUIC_FRAME_STOP_SENDING *f)
  84. {
  85. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  86. || !WPACKET_quic_write_vlint(pkt, f->stream_id)
  87. || !WPACKET_quic_write_vlint(pkt, f->app_error_code))
  88. return 0;
  89. return 1;
  90. }
  91. int ossl_quic_wire_encode_frame_crypto_hdr(WPACKET *pkt,
  92. const OSSL_QUIC_FRAME_CRYPTO *f)
  93. {
  94. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
  95. || !WPACKET_quic_write_vlint(pkt, f->offset)
  96. || !WPACKET_quic_write_vlint(pkt, f->len))
  97. return 0;
  98. return 1;
  99. }
  100. void *ossl_quic_wire_encode_frame_crypto(WPACKET *pkt,
  101. const OSSL_QUIC_FRAME_CRYPTO *f)
  102. {
  103. unsigned char *p = NULL;
  104. if (!ossl_quic_wire_encode_frame_crypto_hdr(pkt, f)
  105. || !WPACKET_allocate_bytes(pkt, f->len, &p))
  106. return NULL;
  107. if (f->data != NULL)
  108. memcpy(p, f->data, f->len);
  109. return p;
  110. }
  111. int ossl_quic_wire_encode_frame_new_token(WPACKET *pkt,
  112. const unsigned char *token,
  113. size_t token_len)
  114. {
  115. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
  116. || !WPACKET_quic_write_vlint(pkt, token_len)
  117. || !WPACKET_memcpy(pkt, token, token_len))
  118. return 0;
  119. return 1;
  120. }
  121. int ossl_quic_wire_encode_frame_stream_hdr(WPACKET *pkt,
  122. const OSSL_QUIC_FRAME_STREAM *f)
  123. {
  124. uint64_t frame_type = OSSL_QUIC_FRAME_TYPE_STREAM;
  125. if (f->offset != 0)
  126. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_OFF;
  127. if (f->has_explicit_len)
  128. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_LEN;
  129. if (f->is_fin)
  130. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_FIN;
  131. if (!encode_frame_hdr(pkt, frame_type)
  132. || !WPACKET_quic_write_vlint(pkt, f->stream_id))
  133. return 0;
  134. if (f->offset != 0 && !WPACKET_quic_write_vlint(pkt, f->offset))
  135. return 0;
  136. if (f->has_explicit_len && !WPACKET_quic_write_vlint(pkt, f->len))
  137. return 0;
  138. return 1;
  139. }
  140. void *ossl_quic_wire_encode_frame_stream(WPACKET *pkt,
  141. const OSSL_QUIC_FRAME_STREAM *f)
  142. {
  143. unsigned char *p = NULL;
  144. if (!ossl_quic_wire_encode_frame_stream_hdr(pkt, f))
  145. return NULL;
  146. if (!WPACKET_allocate_bytes(pkt, f->len, &p))
  147. return NULL;
  148. if (f->data != NULL)
  149. memcpy(p, f->data, f->len);
  150. return p;
  151. }
  152. int ossl_quic_wire_encode_frame_max_data(WPACKET *pkt,
  153. uint64_t max_data)
  154. {
  155. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  156. || !WPACKET_quic_write_vlint(pkt, max_data))
  157. return 0;
  158. return 1;
  159. }
  160. int ossl_quic_wire_encode_frame_max_stream_data(WPACKET *pkt,
  161. uint64_t stream_id,
  162. uint64_t max_data)
  163. {
  164. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  165. || !WPACKET_quic_write_vlint(pkt, stream_id)
  166. || !WPACKET_quic_write_vlint(pkt, max_data))
  167. return 0;
  168. return 1;
  169. }
  170. int ossl_quic_wire_encode_frame_max_streams(WPACKET *pkt,
  171. char is_uni,
  172. uint64_t max_streams)
  173. {
  174. if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI
  175. : OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
  176. || !WPACKET_quic_write_vlint(pkt, max_streams))
  177. return 0;
  178. return 1;
  179. }
  180. int ossl_quic_wire_encode_frame_data_blocked(WPACKET *pkt,
  181. uint64_t max_data)
  182. {
  183. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  184. || !WPACKET_quic_write_vlint(pkt, max_data))
  185. return 0;
  186. return 1;
  187. }
  188. int ossl_quic_wire_encode_frame_stream_data_blocked(WPACKET *pkt,
  189. uint64_t stream_id,
  190. uint64_t max_stream_data)
  191. {
  192. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  193. || !WPACKET_quic_write_vlint(pkt, stream_id)
  194. || !WPACKET_quic_write_vlint(pkt, max_stream_data))
  195. return 0;
  196. return 1;
  197. }
  198. int ossl_quic_wire_encode_frame_streams_blocked(WPACKET *pkt,
  199. char is_uni,
  200. uint64_t max_streams)
  201. {
  202. if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI
  203. : OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  204. || !WPACKET_quic_write_vlint(pkt, max_streams))
  205. return 0;
  206. return 1;
  207. }
  208. int ossl_quic_wire_encode_frame_new_conn_id(WPACKET *pkt,
  209. const OSSL_QUIC_FRAME_NEW_CONN_ID *f)
  210. {
  211. if (f->conn_id.id_len > QUIC_MAX_CONN_ID_LEN)
  212. return 0;
  213. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
  214. || !WPACKET_quic_write_vlint(pkt, f->seq_num)
  215. || !WPACKET_quic_write_vlint(pkt, f->retire_prior_to)
  216. || !WPACKET_put_bytes_u8(pkt, f->conn_id.id_len)
  217. || !WPACKET_memcpy(pkt, f->conn_id.id, f->conn_id.id_len)
  218. || !WPACKET_memcpy(pkt, f->stateless_reset_token,
  219. sizeof(f->stateless_reset_token)))
  220. return 0;
  221. return 1;
  222. }
  223. int ossl_quic_wire_encode_frame_retire_conn_id(WPACKET *pkt,
  224. uint64_t seq_num)
  225. {
  226. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
  227. || !WPACKET_quic_write_vlint(pkt, seq_num))
  228. return 0;
  229. return 1;
  230. }
  231. int ossl_quic_wire_encode_frame_path_challenge(WPACKET *pkt,
  232. uint64_t data)
  233. {
  234. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  235. || !WPACKET_put_bytes_u64(pkt, data))
  236. return 0;
  237. return 1;
  238. }
  239. int ossl_quic_wire_encode_frame_path_response(WPACKET *pkt,
  240. uint64_t data)
  241. {
  242. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  243. || !WPACKET_put_bytes_u64(pkt, data))
  244. return 0;
  245. return 1;
  246. }
  247. int ossl_quic_wire_encode_frame_conn_close(WPACKET *pkt,
  248. const OSSL_QUIC_FRAME_CONN_CLOSE *f)
  249. {
  250. if (!encode_frame_hdr(pkt, f->is_app ? OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP
  251. : OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)
  252. || !WPACKET_quic_write_vlint(pkt, f->error_code))
  253. return 0;
  254. if (!f->is_app && !WPACKET_quic_write_vlint(pkt, f->frame_type))
  255. return 0;
  256. if (!WPACKET_quic_write_vlint(pkt, f->reason_len)
  257. || !WPACKET_memcpy(pkt, f->reason, f->reason_len))
  258. return 0;
  259. return 1;
  260. }
  261. int ossl_quic_wire_encode_frame_handshake_done(WPACKET *pkt)
  262. {
  263. return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
  264. }
  265. unsigned char *ossl_quic_wire_encode_transport_param_bytes(WPACKET *pkt,
  266. uint64_t id,
  267. const unsigned char *value,
  268. size_t value_len)
  269. {
  270. unsigned char *b = NULL;
  271. if (!WPACKET_quic_write_vlint(pkt, id)
  272. || !WPACKET_quic_write_vlint(pkt, value_len)
  273. || !WPACKET_allocate_bytes(pkt, value_len, (unsigned char **)&b))
  274. return NULL;
  275. if (value != NULL)
  276. memcpy(b, value, value_len);
  277. return b;
  278. }
  279. int ossl_quic_wire_encode_transport_param_int(WPACKET *pkt,
  280. uint64_t id,
  281. uint64_t value)
  282. {
  283. if (!WPACKET_quic_write_vlint(pkt, id)
  284. || !WPACKET_quic_write_vlint(pkt, ossl_quic_vlint_encode_len(value))
  285. || !WPACKET_quic_write_vlint(pkt, value))
  286. return 0;
  287. return 1;
  288. }
  289. /*
  290. * QUIC Wire Format Decoding
  291. * =========================
  292. */
  293. int ossl_quic_wire_peek_frame_header(PACKET *pkt, uint64_t *type)
  294. {
  295. return PACKET_peek_quic_vlint(pkt, type);
  296. }
  297. int ossl_quic_wire_skip_frame_header(PACKET *pkt, uint64_t *type)
  298. {
  299. return PACKET_get_quic_vlint(pkt, type);
  300. }
  301. static int expect_frame_header_mask(PACKET *pkt,
  302. uint64_t expected_frame_type,
  303. uint64_t mask_bits,
  304. uint64_t *actual_frame_type)
  305. {
  306. uint64_t actual_frame_type_;
  307. if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type_)
  308. || (actual_frame_type_ & ~mask_bits) != expected_frame_type)
  309. return 0;
  310. if (actual_frame_type != NULL)
  311. *actual_frame_type = actual_frame_type_;
  312. return 1;
  313. }
  314. static int expect_frame_header(PACKET *pkt, uint64_t expected_frame_type)
  315. {
  316. uint64_t actual_frame_type;
  317. if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type)
  318. || actual_frame_type != expected_frame_type)
  319. return 0;
  320. return 1;
  321. }
  322. int ossl_quic_wire_peek_frame_ack_num_ranges(const PACKET *orig_pkt,
  323. uint64_t *total_ranges)
  324. {
  325. PACKET pkt = *orig_pkt;
  326. uint64_t ack_range_count;
  327. if (!expect_frame_header_mask(&pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
  328. 1, NULL)
  329. || !PACKET_skip_quic_vlint(&pkt)
  330. || !PACKET_skip_quic_vlint(&pkt)
  331. || !PACKET_get_quic_vlint(&pkt, &ack_range_count))
  332. return 0;
  333. /* (cannot overflow because QUIC vlints can only encode up to 2**62-1) */
  334. *total_ranges = ack_range_count + 1;
  335. return 1;
  336. }
  337. int ossl_quic_wire_decode_frame_ack(PACKET *pkt,
  338. uint32_t ack_delay_exponent,
  339. OSSL_QUIC_FRAME_ACK *ack,
  340. uint64_t *total_ranges) {
  341. uint64_t frame_type, largest_ackd, ack_delay_raw;
  342. uint64_t ack_range_count, first_ack_range, start, end, i;
  343. /* This call matches both ACK_WITHOUT_ECN and ACK_WITH_ECN. */
  344. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
  345. 1, &frame_type)
  346. || !PACKET_get_quic_vlint(pkt, &largest_ackd)
  347. || !PACKET_get_quic_vlint(pkt, &ack_delay_raw)
  348. || !PACKET_get_quic_vlint(pkt, &ack_range_count)
  349. || !PACKET_get_quic_vlint(pkt, &first_ack_range))
  350. return 0;
  351. if (first_ack_range > largest_ackd)
  352. return 0;
  353. start = largest_ackd - first_ack_range;
  354. if (ack != NULL) {
  355. int err = 0;
  356. ack->delay_time
  357. = ossl_time_multiply(ossl_ticks2time(OSSL_TIME_US),
  358. safe_mul_uint64_t(ack_delay_raw,
  359. 1UL << ack_delay_exponent,
  360. &err));
  361. if (err)
  362. ack->delay_time = ossl_time_infinite();
  363. if (ack->num_ack_ranges > 0) {
  364. ack->ack_ranges[0].end = largest_ackd;
  365. ack->ack_ranges[0].start = start;
  366. }
  367. }
  368. for (i = 0; i < ack_range_count; ++i) {
  369. uint64_t gap, len;
  370. if (!PACKET_get_quic_vlint(pkt, &gap)
  371. || !PACKET_get_quic_vlint(pkt, &len))
  372. return 0;
  373. end = start - gap - 2;
  374. if (start < gap + 2 || len > end)
  375. return 0;
  376. if (ack != NULL && i + 1 < ack->num_ack_ranges) {
  377. ack->ack_ranges[i + 1].start = start = end - len;
  378. ack->ack_ranges[i + 1].end = end;
  379. }
  380. }
  381. if (ack != NULL && ack_range_count + 1 < ack->num_ack_ranges)
  382. ack->num_ack_ranges = ack_range_count + 1;
  383. if (total_ranges != NULL)
  384. *total_ranges = ack_range_count + 1;
  385. if (frame_type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) {
  386. uint64_t ect0, ect1, ecnce;
  387. if (!PACKET_get_quic_vlint(pkt, &ect0)
  388. || !PACKET_get_quic_vlint(pkt, &ect1)
  389. || !PACKET_get_quic_vlint(pkt, &ecnce))
  390. return 0;
  391. if (ack != NULL) {
  392. ack->ect0 = ect0;
  393. ack->ect1 = ect1;
  394. ack->ecnce = ecnce;
  395. ack->ecn_present = 1;
  396. }
  397. } else if (ack != NULL) {
  398. ack->ecn_present = 0;
  399. }
  400. return 1;
  401. }
  402. int ossl_quic_wire_decode_frame_reset_stream(PACKET *pkt,
  403. OSSL_QUIC_FRAME_RESET_STREAM *f)
  404. {
  405. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  406. || !PACKET_get_quic_vlint(pkt, &f->stream_id)
  407. || !PACKET_get_quic_vlint(pkt, &f->app_error_code)
  408. || !PACKET_get_quic_vlint(pkt, &f->final_size))
  409. return 0;
  410. return 1;
  411. }
  412. int ossl_quic_wire_decode_frame_stop_sending(PACKET *pkt,
  413. OSSL_QUIC_FRAME_STOP_SENDING *f)
  414. {
  415. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  416. || !PACKET_get_quic_vlint(pkt, &f->stream_id)
  417. || !PACKET_get_quic_vlint(pkt, &f->app_error_code))
  418. return 0;
  419. return 1;
  420. }
  421. int ossl_quic_wire_decode_frame_crypto(PACKET *pkt,
  422. OSSL_QUIC_FRAME_CRYPTO *f)
  423. {
  424. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
  425. || !PACKET_get_quic_vlint(pkt, &f->offset)
  426. || !PACKET_get_quic_vlint(pkt, &f->len))
  427. return 0;
  428. if (PACKET_remaining(pkt) < f->len)
  429. return 0;
  430. f->data = PACKET_data(pkt);
  431. if (!PACKET_forward(pkt, f->len))
  432. return 0;
  433. return 1;
  434. }
  435. int ossl_quic_wire_decode_frame_new_token(PACKET *pkt,
  436. const unsigned char **token,
  437. size_t *token_len)
  438. {
  439. uint64_t token_len_;
  440. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
  441. || !PACKET_get_quic_vlint(pkt, &token_len_))
  442. return 0;
  443. if (token_len_ > SIZE_MAX)
  444. return 0;
  445. *token = PACKET_data(pkt);
  446. *token_len = token_len_;
  447. if (!PACKET_forward(pkt, token_len_))
  448. return 0;
  449. return 1;
  450. }
  451. int ossl_quic_wire_decode_frame_stream(PACKET *pkt,
  452. OSSL_QUIC_FRAME_STREAM *f)
  453. {
  454. uint64_t frame_type;
  455. /* This call matches all STREAM values (low 3 bits are masked). */
  456. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAM,
  457. OSSL_QUIC_FRAME_FLAG_STREAM_MASK,
  458. &frame_type)
  459. || !PACKET_get_quic_vlint(pkt, &f->stream_id))
  460. return 0;
  461. if ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_OFF) != 0) {
  462. if (!PACKET_get_quic_vlint(pkt, &f->offset))
  463. return 0;
  464. } else {
  465. f->offset = 0;
  466. }
  467. f->has_explicit_len = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_LEN) != 0);
  468. f->is_fin = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_FIN) != 0);
  469. if (f->has_explicit_len) {
  470. if (!PACKET_get_quic_vlint(pkt, &f->len))
  471. return 0;
  472. } else {
  473. f->len = PACKET_remaining(pkt);
  474. }
  475. f->data = PACKET_data(pkt);
  476. if (!PACKET_forward(pkt, f->len))
  477. return 0;
  478. return 1;
  479. }
  480. int ossl_quic_wire_decode_frame_max_data(PACKET *pkt,
  481. uint64_t *max_data)
  482. {
  483. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  484. || !PACKET_get_quic_vlint(pkt, max_data))
  485. return 0;
  486. return 1;
  487. }
  488. int ossl_quic_wire_decode_frame_max_stream_data(PACKET *pkt,
  489. uint64_t *stream_id,
  490. uint64_t *max_stream_data)
  491. {
  492. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  493. || !PACKET_get_quic_vlint(pkt, stream_id)
  494. || !PACKET_get_quic_vlint(pkt, max_stream_data))
  495. return 0;
  496. return 1;
  497. }
  498. int ossl_quic_wire_decode_frame_max_streams(PACKET *pkt,
  499. uint64_t *max_streams)
  500. {
  501. /* This call matches both MAX_STREAMS_BIDI and MAX_STREAMS_UNI. */
  502. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI,
  503. 1, NULL)
  504. || !PACKET_get_quic_vlint(pkt, max_streams))
  505. return 0;
  506. return 1;
  507. }
  508. int ossl_quic_wire_decode_frame_data_blocked(PACKET *pkt,
  509. uint64_t *max_data)
  510. {
  511. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  512. || !PACKET_get_quic_vlint(pkt, max_data))
  513. return 0;
  514. return 1;
  515. }
  516. int ossl_quic_wire_decode_frame_stream_data_blocked(PACKET *pkt,
  517. uint64_t *stream_id,
  518. uint64_t *max_stream_data)
  519. {
  520. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  521. || !PACKET_get_quic_vlint(pkt, stream_id)
  522. || !PACKET_get_quic_vlint(pkt, max_stream_data))
  523. return 0;
  524. return 1;
  525. }
  526. int ossl_quic_wire_decode_frame_streams_blocked(PACKET *pkt,
  527. uint64_t *max_streams)
  528. {
  529. /* This call matches both STREAMS_BLOCKED_BIDI and STREAMS_BLOCKED_UNI. */
  530. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI,
  531. 1, NULL)
  532. || !PACKET_get_quic_vlint(pkt, max_streams))
  533. return 0;
  534. return 1;
  535. }
  536. int ossl_quic_wire_decode_frame_new_conn_id(PACKET *pkt,
  537. OSSL_QUIC_FRAME_NEW_CONN_ID *f)
  538. {
  539. unsigned int len;
  540. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
  541. || !PACKET_get_quic_vlint(pkt, &f->seq_num)
  542. || !PACKET_get_quic_vlint(pkt, &f->retire_prior_to)
  543. || !PACKET_get_1(pkt, &len)
  544. || len > QUIC_MAX_CONN_ID_LEN)
  545. return 0;
  546. f->conn_id.id_len = (unsigned char)len;
  547. if (!PACKET_copy_bytes(pkt, f->conn_id.id, len))
  548. return 0;
  549. /* Clear unused bytes to allow consistent memcmp. */
  550. if (len < QUIC_MAX_CONN_ID_LEN)
  551. memset(f->conn_id.id + len, 0, QUIC_MAX_CONN_ID_LEN - len);
  552. if (!PACKET_copy_bytes(pkt, f->stateless_reset_token,
  553. sizeof(f->stateless_reset_token)))
  554. return 0;
  555. return 1;
  556. }
  557. int ossl_quic_wire_decode_frame_retire_conn_id(PACKET *pkt,
  558. uint64_t *seq_num)
  559. {
  560. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
  561. || !PACKET_get_quic_vlint(pkt, seq_num))
  562. return 0;
  563. return 1;
  564. }
  565. int ossl_quic_wire_decode_frame_path_challenge(PACKET *pkt,
  566. uint64_t *data)
  567. {
  568. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  569. || !PACKET_get_net_8(pkt, data))
  570. return 0;
  571. return 1;
  572. }
  573. int ossl_quic_wire_decode_frame_path_response(PACKET *pkt,
  574. uint64_t *data)
  575. {
  576. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  577. || !PACKET_get_net_8(pkt, data))
  578. return 0;
  579. return 1;
  580. }
  581. int ossl_quic_wire_decode_frame_conn_close(PACKET *pkt,
  582. OSSL_QUIC_FRAME_CONN_CLOSE *f)
  583. {
  584. uint64_t frame_type, reason_len;
  585. /* This call matches both CONN_CLOSE_TRANSPORT and CONN_CLOSE_APP. */
  586. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT,
  587. 1, &frame_type)
  588. || !PACKET_get_quic_vlint(pkt, &f->error_code))
  589. return 0;
  590. f->is_app = ((frame_type & 1) != 0);
  591. if (!f->is_app) {
  592. if (!PACKET_get_quic_vlint(pkt, &f->frame_type))
  593. return 0;
  594. } else {
  595. f->frame_type = 0;
  596. }
  597. if (!PACKET_get_quic_vlint(pkt, &reason_len)
  598. || reason_len > SIZE_MAX)
  599. return 0;
  600. if (!PACKET_get_bytes(pkt, (const unsigned char **)&f->reason, reason_len))
  601. return 0;
  602. f->reason_len = reason_len;
  603. return 1;
  604. }
  605. size_t ossl_quic_wire_decode_padding(PACKET *pkt)
  606. {
  607. const unsigned char *start = PACKET_data(pkt), *end = PACKET_end(pkt),
  608. *p = start;
  609. while (p < end && *p == 0)
  610. ++p;
  611. if (!PACKET_forward(pkt, p - start))
  612. return 0;
  613. return p - start;
  614. }
  615. int ossl_quic_wire_decode_frame_ping(PACKET *pkt)
  616. {
  617. return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PING);
  618. }
  619. int ossl_quic_wire_decode_frame_handshake_done(PACKET *pkt)
  620. {
  621. return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
  622. }
  623. int ossl_quic_wire_peek_transport_param(PACKET *pkt, uint64_t *id)
  624. {
  625. return PACKET_peek_quic_vlint(pkt, id);
  626. }
  627. const unsigned char *ossl_quic_wire_decode_transport_param_bytes(PACKET *pkt,
  628. uint64_t *id,
  629. size_t *len)
  630. {
  631. uint64_t len_;
  632. const unsigned char *b = NULL;
  633. if (!PACKET_get_quic_vlint(pkt, id)
  634. || !PACKET_get_quic_vlint(pkt, &len_))
  635. return NULL;
  636. if (len_ > SIZE_MAX
  637. || !PACKET_get_bytes(pkt, (const unsigned char **)&b, (size_t)len_))
  638. return NULL;
  639. *len = (size_t)len_;
  640. return b;
  641. }
  642. int ossl_quic_wire_decode_transport_param_int(PACKET *pkt,
  643. uint64_t *id,
  644. uint64_t *value)
  645. {
  646. PACKET sub;
  647. sub.curr = ossl_quic_wire_decode_transport_param_bytes(pkt,
  648. id, &sub.remaining);
  649. if (sub.curr == NULL)
  650. return 0;
  651. if (!PACKET_get_quic_vlint(&sub, value))
  652. return 0;
  653. return 1;
  654. }