quic_wire.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. #include <openssl/macros.h>
  10. #include <openssl/objects.h>
  11. #include "internal/quic_ssl.h"
  12. #include "internal/quic_vlint.h"
  13. #include "internal/quic_wire.h"
  14. #include "internal/quic_error.h"
  15. OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)
  16. int ossl_quic_frame_ack_contains_pn(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pn)
  17. {
  18. size_t i;
  19. for (i = 0; i < ack->num_ack_ranges; ++i)
  20. if (pn >= ack->ack_ranges[i].start
  21. && pn <= ack->ack_ranges[i].end)
  22. return 1;
  23. return 0;
  24. }
  25. /*
  26. * QUIC Wire Format Encoding
  27. * =========================
  28. */
  29. int ossl_quic_wire_encode_padding(WPACKET *pkt, size_t num_bytes)
  30. {
  31. /*
  32. * PADDING is frame type zero, which as a variable-length integer is
  33. * represented as a single zero byte. As an optimisation, just use memset.
  34. */
  35. return WPACKET_memset(pkt, 0, num_bytes);
  36. }
  37. static int encode_frame_hdr(WPACKET *pkt, uint64_t frame_type)
  38. {
  39. return WPACKET_quic_write_vlint(pkt, frame_type);
  40. }
  41. int ossl_quic_wire_encode_frame_ping(WPACKET *pkt)
  42. {
  43. return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PING);
  44. }
  45. int ossl_quic_wire_encode_frame_ack(WPACKET *pkt,
  46. uint32_t ack_delay_exponent,
  47. const OSSL_QUIC_FRAME_ACK *ack)
  48. {
  49. uint64_t frame_type = ack->ecn_present ? OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN
  50. : OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;
  51. uint64_t largest_ackd, first_ack_range, ack_delay_enc;
  52. uint64_t i, num_ack_ranges = ack->num_ack_ranges;
  53. OSSL_TIME delay;
  54. if (num_ack_ranges == 0)
  55. return 0;
  56. delay = ossl_time_divide(ossl_time_divide(ack->delay_time, OSSL_TIME_US),
  57. (uint64_t)1 << ack_delay_exponent);
  58. ack_delay_enc = ossl_time2ticks(delay);
  59. largest_ackd = ack->ack_ranges[0].end;
  60. first_ack_range = ack->ack_ranges[0].end - ack->ack_ranges[0].start;
  61. if (!encode_frame_hdr(pkt, frame_type)
  62. || !WPACKET_quic_write_vlint(pkt, largest_ackd)
  63. || !WPACKET_quic_write_vlint(pkt, ack_delay_enc)
  64. || !WPACKET_quic_write_vlint(pkt, num_ack_ranges - 1)
  65. || !WPACKET_quic_write_vlint(pkt, first_ack_range))
  66. return 0;
  67. for (i = 1; i < num_ack_ranges; ++i) {
  68. uint64_t gap, range_len;
  69. gap = ack->ack_ranges[i - 1].start - ack->ack_ranges[i].end - 2;
  70. range_len = ack->ack_ranges[i].end - ack->ack_ranges[i].start;
  71. if (!WPACKET_quic_write_vlint(pkt, gap)
  72. || !WPACKET_quic_write_vlint(pkt, range_len))
  73. return 0;
  74. }
  75. if (ack->ecn_present)
  76. if (!WPACKET_quic_write_vlint(pkt, ack->ect0)
  77. || !WPACKET_quic_write_vlint(pkt, ack->ect1)
  78. || !WPACKET_quic_write_vlint(pkt, ack->ecnce))
  79. return 0;
  80. return 1;
  81. }
  82. int ossl_quic_wire_encode_frame_reset_stream(WPACKET *pkt,
  83. const OSSL_QUIC_FRAME_RESET_STREAM *f)
  84. {
  85. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  86. || !WPACKET_quic_write_vlint(pkt, f->stream_id)
  87. || !WPACKET_quic_write_vlint(pkt, f->app_error_code)
  88. || !WPACKET_quic_write_vlint(pkt, f->final_size))
  89. return 0;
  90. return 1;
  91. }
  92. int ossl_quic_wire_encode_frame_stop_sending(WPACKET *pkt,
  93. const OSSL_QUIC_FRAME_STOP_SENDING *f)
  94. {
  95. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  96. || !WPACKET_quic_write_vlint(pkt, f->stream_id)
  97. || !WPACKET_quic_write_vlint(pkt, f->app_error_code))
  98. return 0;
  99. return 1;
  100. }
  101. int ossl_quic_wire_encode_frame_crypto_hdr(WPACKET *pkt,
  102. const OSSL_QUIC_FRAME_CRYPTO *f)
  103. {
  104. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
  105. || !WPACKET_quic_write_vlint(pkt, f->offset)
  106. || !WPACKET_quic_write_vlint(pkt, f->len))
  107. return 0;
  108. return 1;
  109. }
  110. size_t ossl_quic_wire_get_encoded_frame_len_crypto_hdr(const OSSL_QUIC_FRAME_CRYPTO *f)
  111. {
  112. size_t a, b, c;
  113. a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_CRYPTO);
  114. b = ossl_quic_vlint_encode_len(f->offset);
  115. c = ossl_quic_vlint_encode_len(f->len);
  116. if (a == 0 || b == 0 || c == 0)
  117. return 0;
  118. return a + b + c;
  119. }
  120. void *ossl_quic_wire_encode_frame_crypto(WPACKET *pkt,
  121. const OSSL_QUIC_FRAME_CRYPTO *f)
  122. {
  123. unsigned char *p = NULL;
  124. if (!ossl_quic_wire_encode_frame_crypto_hdr(pkt, f)
  125. || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */
  126. || !WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))
  127. return NULL;
  128. if (f->data != NULL)
  129. memcpy(p, f->data, (size_t)f->len);
  130. return p;
  131. }
  132. int ossl_quic_wire_encode_frame_new_token(WPACKET *pkt,
  133. const unsigned char *token,
  134. size_t token_len)
  135. {
  136. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
  137. || !WPACKET_quic_write_vlint(pkt, token_len)
  138. || !WPACKET_memcpy(pkt, token, token_len))
  139. return 0;
  140. return 1;
  141. }
  142. int ossl_quic_wire_encode_frame_stream_hdr(WPACKET *pkt,
  143. const OSSL_QUIC_FRAME_STREAM *f)
  144. {
  145. uint64_t frame_type = OSSL_QUIC_FRAME_TYPE_STREAM;
  146. if (f->offset != 0)
  147. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_OFF;
  148. if (f->has_explicit_len)
  149. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_LEN;
  150. if (f->is_fin)
  151. frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_FIN;
  152. if (!encode_frame_hdr(pkt, frame_type)
  153. || !WPACKET_quic_write_vlint(pkt, f->stream_id))
  154. return 0;
  155. if (f->offset != 0 && !WPACKET_quic_write_vlint(pkt, f->offset))
  156. return 0;
  157. if (f->has_explicit_len && !WPACKET_quic_write_vlint(pkt, f->len))
  158. return 0;
  159. return 1;
  160. }
  161. size_t ossl_quic_wire_get_encoded_frame_len_stream_hdr(const OSSL_QUIC_FRAME_STREAM *f)
  162. {
  163. size_t a, b, c, d;
  164. a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_STREAM);
  165. b = ossl_quic_vlint_encode_len(f->stream_id);
  166. if (a == 0 || b == 0)
  167. return 0;
  168. if (f->offset > 0) {
  169. c = ossl_quic_vlint_encode_len(f->offset);
  170. if (c == 0)
  171. return 0;
  172. } else {
  173. c = 0;
  174. }
  175. if (f->has_explicit_len) {
  176. d = ossl_quic_vlint_encode_len(f->len);
  177. if (d == 0)
  178. return 0;
  179. } else {
  180. d = 0;
  181. }
  182. return a + b + c + d;
  183. }
  184. void *ossl_quic_wire_encode_frame_stream(WPACKET *pkt,
  185. const OSSL_QUIC_FRAME_STREAM *f)
  186. {
  187. unsigned char *p = NULL;
  188. if (!ossl_quic_wire_encode_frame_stream_hdr(pkt, f)
  189. || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
  190. return NULL;
  191. if (!WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))
  192. return NULL;
  193. if (f->data != NULL)
  194. memcpy(p, f->data, (size_t)f->len);
  195. return p;
  196. }
  197. int ossl_quic_wire_encode_frame_max_data(WPACKET *pkt,
  198. uint64_t max_data)
  199. {
  200. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  201. || !WPACKET_quic_write_vlint(pkt, max_data))
  202. return 0;
  203. return 1;
  204. }
  205. int ossl_quic_wire_encode_frame_max_stream_data(WPACKET *pkt,
  206. uint64_t stream_id,
  207. uint64_t max_data)
  208. {
  209. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  210. || !WPACKET_quic_write_vlint(pkt, stream_id)
  211. || !WPACKET_quic_write_vlint(pkt, max_data))
  212. return 0;
  213. return 1;
  214. }
  215. int ossl_quic_wire_encode_frame_max_streams(WPACKET *pkt,
  216. char is_uni,
  217. uint64_t max_streams)
  218. {
  219. if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI
  220. : OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
  221. || !WPACKET_quic_write_vlint(pkt, max_streams))
  222. return 0;
  223. return 1;
  224. }
  225. int ossl_quic_wire_encode_frame_data_blocked(WPACKET *pkt,
  226. uint64_t max_data)
  227. {
  228. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  229. || !WPACKET_quic_write_vlint(pkt, max_data))
  230. return 0;
  231. return 1;
  232. }
  233. int ossl_quic_wire_encode_frame_stream_data_blocked(WPACKET *pkt,
  234. uint64_t stream_id,
  235. uint64_t max_stream_data)
  236. {
  237. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  238. || !WPACKET_quic_write_vlint(pkt, stream_id)
  239. || !WPACKET_quic_write_vlint(pkt, max_stream_data))
  240. return 0;
  241. return 1;
  242. }
  243. int ossl_quic_wire_encode_frame_streams_blocked(WPACKET *pkt,
  244. char is_uni,
  245. uint64_t max_streams)
  246. {
  247. if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI
  248. : OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
  249. || !WPACKET_quic_write_vlint(pkt, max_streams))
  250. return 0;
  251. return 1;
  252. }
  253. int ossl_quic_wire_encode_frame_new_conn_id(WPACKET *pkt,
  254. const OSSL_QUIC_FRAME_NEW_CONN_ID *f)
  255. {
  256. if (f->conn_id.id_len < 1
  257. || f->conn_id.id_len > QUIC_MAX_CONN_ID_LEN)
  258. return 0;
  259. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
  260. || !WPACKET_quic_write_vlint(pkt, f->seq_num)
  261. || !WPACKET_quic_write_vlint(pkt, f->retire_prior_to)
  262. || !WPACKET_put_bytes_u8(pkt, f->conn_id.id_len)
  263. || !WPACKET_memcpy(pkt, f->conn_id.id, f->conn_id.id_len)
  264. || !WPACKET_memcpy(pkt, f->stateless_reset.token,
  265. sizeof(f->stateless_reset.token)))
  266. return 0;
  267. return 1;
  268. }
  269. int ossl_quic_wire_encode_frame_retire_conn_id(WPACKET *pkt,
  270. uint64_t seq_num)
  271. {
  272. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
  273. || !WPACKET_quic_write_vlint(pkt, seq_num))
  274. return 0;
  275. return 1;
  276. }
  277. int ossl_quic_wire_encode_frame_path_challenge(WPACKET *pkt,
  278. uint64_t data)
  279. {
  280. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  281. || !WPACKET_put_bytes_u64(pkt, data))
  282. return 0;
  283. return 1;
  284. }
  285. int ossl_quic_wire_encode_frame_path_response(WPACKET *pkt,
  286. uint64_t data)
  287. {
  288. if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  289. || !WPACKET_put_bytes_u64(pkt, data))
  290. return 0;
  291. return 1;
  292. }
  293. int ossl_quic_wire_encode_frame_conn_close(WPACKET *pkt,
  294. const OSSL_QUIC_FRAME_CONN_CLOSE *f)
  295. {
  296. if (!encode_frame_hdr(pkt, f->is_app ? OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP
  297. : OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)
  298. || !WPACKET_quic_write_vlint(pkt, f->error_code))
  299. return 0;
  300. /*
  301. * RFC 9000 s. 19.19: The application-specific variant of CONNECTION_CLOSE
  302. * (type 0x1d) does not include this field.
  303. */
  304. if (!f->is_app && !WPACKET_quic_write_vlint(pkt, f->frame_type))
  305. return 0;
  306. if (!WPACKET_quic_write_vlint(pkt, f->reason_len)
  307. || !WPACKET_memcpy(pkt, f->reason, f->reason_len))
  308. return 0;
  309. return 1;
  310. }
  311. int ossl_quic_wire_encode_frame_handshake_done(WPACKET *pkt)
  312. {
  313. return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
  314. }
  315. unsigned char *ossl_quic_wire_encode_transport_param_bytes(WPACKET *pkt,
  316. uint64_t id,
  317. const unsigned char *value,
  318. size_t value_len)
  319. {
  320. unsigned char *b = NULL;
  321. if (!WPACKET_quic_write_vlint(pkt, id)
  322. || !WPACKET_quic_write_vlint(pkt, value_len))
  323. return NULL;
  324. if (value_len == 0)
  325. b = WPACKET_get_curr(pkt);
  326. else if (!WPACKET_allocate_bytes(pkt, value_len, (unsigned char **)&b))
  327. return NULL;
  328. if (value != NULL)
  329. memcpy(b, value, value_len);
  330. return b;
  331. }
  332. int ossl_quic_wire_encode_transport_param_int(WPACKET *pkt,
  333. uint64_t id,
  334. uint64_t value)
  335. {
  336. if (!WPACKET_quic_write_vlint(pkt, id)
  337. || !WPACKET_quic_write_vlint(pkt, ossl_quic_vlint_encode_len(value))
  338. || !WPACKET_quic_write_vlint(pkt, value))
  339. return 0;
  340. return 1;
  341. }
  342. int ossl_quic_wire_encode_transport_param_cid(WPACKET *wpkt,
  343. uint64_t id,
  344. const QUIC_CONN_ID *cid)
  345. {
  346. if (cid->id_len > QUIC_MAX_CONN_ID_LEN)
  347. return 0;
  348. if (ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  349. cid->id,
  350. cid->id_len) == NULL)
  351. return 0;
  352. return 1;
  353. }
  354. /*
  355. * QUIC Wire Format Decoding
  356. * =========================
  357. */
  358. int ossl_quic_wire_peek_frame_header(PACKET *pkt, uint64_t *type,
  359. int *was_minimal)
  360. {
  361. return PACKET_peek_quic_vlint_ex(pkt, type, was_minimal);
  362. }
  363. int ossl_quic_wire_skip_frame_header(PACKET *pkt, uint64_t *type)
  364. {
  365. return PACKET_get_quic_vlint(pkt, type);
  366. }
  367. static int expect_frame_header_mask(PACKET *pkt,
  368. uint64_t expected_frame_type,
  369. uint64_t mask_bits,
  370. uint64_t *actual_frame_type)
  371. {
  372. uint64_t actual_frame_type_;
  373. if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type_)
  374. || (actual_frame_type_ & ~mask_bits) != expected_frame_type)
  375. return 0;
  376. if (actual_frame_type != NULL)
  377. *actual_frame_type = actual_frame_type_;
  378. return 1;
  379. }
  380. static int expect_frame_header(PACKET *pkt, uint64_t expected_frame_type)
  381. {
  382. uint64_t actual_frame_type;
  383. if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type)
  384. || actual_frame_type != expected_frame_type)
  385. return 0;
  386. return 1;
  387. }
  388. int ossl_quic_wire_peek_frame_ack_num_ranges(const PACKET *orig_pkt,
  389. uint64_t *total_ranges)
  390. {
  391. PACKET pkt = *orig_pkt;
  392. uint64_t ack_range_count, i;
  393. if (!expect_frame_header_mask(&pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
  394. 1, NULL)
  395. || !PACKET_skip_quic_vlint(&pkt)
  396. || !PACKET_skip_quic_vlint(&pkt)
  397. || !PACKET_get_quic_vlint(&pkt, &ack_range_count))
  398. return 0;
  399. /*
  400. * Ensure the specified number of ack ranges listed in the ACK frame header
  401. * actually are available in the frame data. This naturally bounds the
  402. * number of ACK ranges which can be requested by the MDPL, and therefore by
  403. * the MTU. This ensures we do not allocate memory for an excessive number
  404. * of ACK ranges.
  405. */
  406. for (i = 0; i < ack_range_count; ++i)
  407. if (!PACKET_skip_quic_vlint(&pkt)
  408. || !PACKET_skip_quic_vlint(&pkt))
  409. return 0;
  410. /* (cannot overflow because QUIC vlints can only encode up to 2**62-1) */
  411. *total_ranges = ack_range_count + 1;
  412. return 1;
  413. }
  414. int ossl_quic_wire_decode_frame_ack(PACKET *pkt,
  415. uint32_t ack_delay_exponent,
  416. OSSL_QUIC_FRAME_ACK *ack,
  417. uint64_t *total_ranges) {
  418. uint64_t frame_type, largest_ackd, ack_delay_raw;
  419. uint64_t ack_range_count, first_ack_range, start, end, i;
  420. /* This call matches both ACK_WITHOUT_ECN and ACK_WITH_ECN. */
  421. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,
  422. 1, &frame_type)
  423. || !PACKET_get_quic_vlint(pkt, &largest_ackd)
  424. || !PACKET_get_quic_vlint(pkt, &ack_delay_raw)
  425. || !PACKET_get_quic_vlint(pkt, &ack_range_count)
  426. || !PACKET_get_quic_vlint(pkt, &first_ack_range))
  427. return 0;
  428. if (first_ack_range > largest_ackd)
  429. return 0;
  430. if (ack_range_count > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
  431. return 0;
  432. start = largest_ackd - first_ack_range;
  433. if (ack != NULL) {
  434. int err = 0;
  435. ack->delay_time
  436. = ossl_time_multiply(ossl_ticks2time(OSSL_TIME_US),
  437. safe_mul_uint64_t(ack_delay_raw,
  438. (uint64_t)1 << ack_delay_exponent,
  439. &err));
  440. if (err)
  441. ack->delay_time = ossl_time_infinite();
  442. if (ack->num_ack_ranges > 0) {
  443. ack->ack_ranges[0].end = largest_ackd;
  444. ack->ack_ranges[0].start = start;
  445. }
  446. }
  447. for (i = 0; i < ack_range_count; ++i) {
  448. uint64_t gap, len;
  449. if (!PACKET_get_quic_vlint(pkt, &gap)
  450. || !PACKET_get_quic_vlint(pkt, &len))
  451. return 0;
  452. end = start - gap - 2;
  453. if (start < gap + 2 || len > end)
  454. return 0;
  455. if (ack != NULL && i + 1 < ack->num_ack_ranges) {
  456. ack->ack_ranges[i + 1].start = start = end - len;
  457. ack->ack_ranges[i + 1].end = end;
  458. }
  459. }
  460. if (ack != NULL && ack_range_count + 1 < ack->num_ack_ranges)
  461. ack->num_ack_ranges = (size_t)ack_range_count + 1;
  462. if (total_ranges != NULL)
  463. *total_ranges = ack_range_count + 1;
  464. if (frame_type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) {
  465. uint64_t ect0, ect1, ecnce;
  466. if (!PACKET_get_quic_vlint(pkt, &ect0)
  467. || !PACKET_get_quic_vlint(pkt, &ect1)
  468. || !PACKET_get_quic_vlint(pkt, &ecnce))
  469. return 0;
  470. if (ack != NULL) {
  471. ack->ect0 = ect0;
  472. ack->ect1 = ect1;
  473. ack->ecnce = ecnce;
  474. ack->ecn_present = 1;
  475. }
  476. } else if (ack != NULL) {
  477. ack->ecn_present = 0;
  478. }
  479. return 1;
  480. }
  481. int ossl_quic_wire_decode_frame_reset_stream(PACKET *pkt,
  482. OSSL_QUIC_FRAME_RESET_STREAM *f)
  483. {
  484. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)
  485. || !PACKET_get_quic_vlint(pkt, &f->stream_id)
  486. || !PACKET_get_quic_vlint(pkt, &f->app_error_code)
  487. || !PACKET_get_quic_vlint(pkt, &f->final_size))
  488. return 0;
  489. return 1;
  490. }
  491. int ossl_quic_wire_decode_frame_stop_sending(PACKET *pkt,
  492. OSSL_QUIC_FRAME_STOP_SENDING *f)
  493. {
  494. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)
  495. || !PACKET_get_quic_vlint(pkt, &f->stream_id)
  496. || !PACKET_get_quic_vlint(pkt, &f->app_error_code))
  497. return 0;
  498. return 1;
  499. }
  500. int ossl_quic_wire_decode_frame_crypto(PACKET *pkt,
  501. int nodata,
  502. OSSL_QUIC_FRAME_CRYPTO *f)
  503. {
  504. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)
  505. || !PACKET_get_quic_vlint(pkt, &f->offset)
  506. || !PACKET_get_quic_vlint(pkt, &f->len)
  507. || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)
  508. return 0;
  509. if (f->offset + f->len > (((uint64_t)1) << 62) - 1)
  510. /* RFC 9000 s. 19.6 */
  511. return 0;
  512. if (nodata) {
  513. f->data = NULL;
  514. } else {
  515. if (PACKET_remaining(pkt) < f->len)
  516. return 0;
  517. f->data = PACKET_data(pkt);
  518. if (!PACKET_forward(pkt, (size_t)f->len))
  519. return 0;
  520. }
  521. return 1;
  522. }
  523. int ossl_quic_wire_decode_frame_new_token(PACKET *pkt,
  524. const unsigned char **token,
  525. size_t *token_len)
  526. {
  527. uint64_t token_len_;
  528. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)
  529. || !PACKET_get_quic_vlint(pkt, &token_len_))
  530. return 0;
  531. if (token_len_ > SIZE_MAX)
  532. return 0;
  533. *token = PACKET_data(pkt);
  534. *token_len = (size_t)token_len_;
  535. if (!PACKET_forward(pkt, (size_t)token_len_))
  536. return 0;
  537. return 1;
  538. }
  539. int ossl_quic_wire_decode_frame_stream(PACKET *pkt,
  540. int nodata,
  541. OSSL_QUIC_FRAME_STREAM *f)
  542. {
  543. uint64_t frame_type;
  544. /* This call matches all STREAM values (low 3 bits are masked). */
  545. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAM,
  546. OSSL_QUIC_FRAME_FLAG_STREAM_MASK,
  547. &frame_type)
  548. || !PACKET_get_quic_vlint(pkt, &f->stream_id))
  549. return 0;
  550. if ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_OFF) != 0) {
  551. if (!PACKET_get_quic_vlint(pkt, &f->offset))
  552. return 0;
  553. } else {
  554. f->offset = 0;
  555. }
  556. f->has_explicit_len = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_LEN) != 0);
  557. f->is_fin = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_FIN) != 0);
  558. if (f->has_explicit_len) {
  559. if (!PACKET_get_quic_vlint(pkt, &f->len))
  560. return 0;
  561. } else {
  562. if (nodata)
  563. f->len = 0;
  564. else
  565. f->len = PACKET_remaining(pkt);
  566. }
  567. /*
  568. * RFC 9000 s. 19.8: "The largest offset delivered on a stream -- the sum of
  569. * the offset and data length -- cannot exceed 2**62 - 1, as it is not
  570. * possible to provide flow control credit for that data."
  571. */
  572. if (f->offset + f->len > (((uint64_t)1) << 62) - 1)
  573. return 0;
  574. if (nodata) {
  575. f->data = NULL;
  576. } else {
  577. f->data = PACKET_data(pkt);
  578. if (f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */
  579. || !PACKET_forward(pkt, (size_t)f->len))
  580. return 0;
  581. }
  582. return 1;
  583. }
  584. int ossl_quic_wire_decode_frame_max_data(PACKET *pkt,
  585. uint64_t *max_data)
  586. {
  587. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)
  588. || !PACKET_get_quic_vlint(pkt, max_data))
  589. return 0;
  590. return 1;
  591. }
  592. int ossl_quic_wire_decode_frame_max_stream_data(PACKET *pkt,
  593. uint64_t *stream_id,
  594. uint64_t *max_stream_data)
  595. {
  596. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)
  597. || !PACKET_get_quic_vlint(pkt, stream_id)
  598. || !PACKET_get_quic_vlint(pkt, max_stream_data))
  599. return 0;
  600. return 1;
  601. }
  602. int ossl_quic_wire_decode_frame_max_streams(PACKET *pkt,
  603. uint64_t *max_streams)
  604. {
  605. /* This call matches both MAX_STREAMS_BIDI and MAX_STREAMS_UNI. */
  606. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI,
  607. 1, NULL)
  608. || !PACKET_get_quic_vlint(pkt, max_streams))
  609. return 0;
  610. return 1;
  611. }
  612. int ossl_quic_wire_decode_frame_data_blocked(PACKET *pkt,
  613. uint64_t *max_data)
  614. {
  615. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)
  616. || !PACKET_get_quic_vlint(pkt, max_data))
  617. return 0;
  618. return 1;
  619. }
  620. int ossl_quic_wire_decode_frame_stream_data_blocked(PACKET *pkt,
  621. uint64_t *stream_id,
  622. uint64_t *max_stream_data)
  623. {
  624. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)
  625. || !PACKET_get_quic_vlint(pkt, stream_id)
  626. || !PACKET_get_quic_vlint(pkt, max_stream_data))
  627. return 0;
  628. return 1;
  629. }
  630. int ossl_quic_wire_decode_frame_streams_blocked(PACKET *pkt,
  631. uint64_t *max_streams)
  632. {
  633. /* This call matches both STREAMS_BLOCKED_BIDI and STREAMS_BLOCKED_UNI. */
  634. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI,
  635. 1, NULL)
  636. || !PACKET_get_quic_vlint(pkt, max_streams))
  637. return 0;
  638. return 1;
  639. }
  640. int ossl_quic_wire_decode_frame_new_conn_id(PACKET *pkt,
  641. OSSL_QUIC_FRAME_NEW_CONN_ID *f)
  642. {
  643. unsigned int len;
  644. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)
  645. || !PACKET_get_quic_vlint(pkt, &f->seq_num)
  646. || !PACKET_get_quic_vlint(pkt, &f->retire_prior_to)
  647. || f->seq_num < f->retire_prior_to
  648. || !PACKET_get_1(pkt, &len)
  649. || len < 1
  650. || len > QUIC_MAX_CONN_ID_LEN)
  651. return 0;
  652. f->conn_id.id_len = (unsigned char)len;
  653. if (!PACKET_copy_bytes(pkt, f->conn_id.id, len))
  654. return 0;
  655. /* Clear unused bytes to allow consistent memcmp. */
  656. if (len < QUIC_MAX_CONN_ID_LEN)
  657. memset(f->conn_id.id + len, 0, QUIC_MAX_CONN_ID_LEN - len);
  658. if (!PACKET_copy_bytes(pkt, f->stateless_reset.token,
  659. sizeof(f->stateless_reset.token)))
  660. return 0;
  661. return 1;
  662. }
  663. int ossl_quic_wire_decode_frame_retire_conn_id(PACKET *pkt,
  664. uint64_t *seq_num)
  665. {
  666. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)
  667. || !PACKET_get_quic_vlint(pkt, seq_num))
  668. return 0;
  669. return 1;
  670. }
  671. int ossl_quic_wire_decode_frame_path_challenge(PACKET *pkt,
  672. uint64_t *data)
  673. {
  674. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)
  675. || !PACKET_get_net_8(pkt, data))
  676. return 0;
  677. return 1;
  678. }
  679. int ossl_quic_wire_decode_frame_path_response(PACKET *pkt,
  680. uint64_t *data)
  681. {
  682. if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)
  683. || !PACKET_get_net_8(pkt, data))
  684. return 0;
  685. return 1;
  686. }
  687. int ossl_quic_wire_decode_frame_conn_close(PACKET *pkt,
  688. OSSL_QUIC_FRAME_CONN_CLOSE *f)
  689. {
  690. uint64_t frame_type, reason_len;
  691. /* This call matches both CONN_CLOSE_TRANSPORT and CONN_CLOSE_APP. */
  692. if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT,
  693. 1, &frame_type)
  694. || !PACKET_get_quic_vlint(pkt, &f->error_code))
  695. return 0;
  696. f->is_app = ((frame_type & 1) != 0);
  697. if (!f->is_app) {
  698. if (!PACKET_get_quic_vlint(pkt, &f->frame_type))
  699. return 0;
  700. } else {
  701. f->frame_type = 0;
  702. }
  703. if (!PACKET_get_quic_vlint(pkt, &reason_len)
  704. || reason_len > SIZE_MAX)
  705. return 0;
  706. if (!PACKET_get_bytes(pkt, (const unsigned char **)&f->reason,
  707. (size_t)reason_len))
  708. return 0;
  709. f->reason_len = (size_t)reason_len;
  710. return 1;
  711. }
  712. size_t ossl_quic_wire_decode_padding(PACKET *pkt)
  713. {
  714. const unsigned char *start = PACKET_data(pkt), *end = PACKET_end(pkt),
  715. *p = start;
  716. while (p < end && *p == 0)
  717. ++p;
  718. if (!PACKET_forward(pkt, p - start))
  719. return 0;
  720. return p - start;
  721. }
  722. int ossl_quic_wire_decode_frame_ping(PACKET *pkt)
  723. {
  724. return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PING);
  725. }
  726. int ossl_quic_wire_decode_frame_handshake_done(PACKET *pkt)
  727. {
  728. return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);
  729. }
  730. int ossl_quic_wire_peek_transport_param(PACKET *pkt, uint64_t *id)
  731. {
  732. return PACKET_peek_quic_vlint(pkt, id);
  733. }
  734. const unsigned char *ossl_quic_wire_decode_transport_param_bytes(PACKET *pkt,
  735. uint64_t *id,
  736. size_t *len)
  737. {
  738. uint64_t len_;
  739. const unsigned char *b = NULL;
  740. uint64_t id_;
  741. if (!PACKET_get_quic_vlint(pkt, &id_)
  742. || !PACKET_get_quic_vlint(pkt, &len_))
  743. return NULL;
  744. if (len_ > SIZE_MAX
  745. || !PACKET_get_bytes(pkt, (const unsigned char **)&b, (size_t)len_))
  746. return NULL;
  747. *len = (size_t)len_;
  748. if (id != NULL)
  749. *id = id_;
  750. return b;
  751. }
  752. int ossl_quic_wire_decode_transport_param_int(PACKET *pkt,
  753. uint64_t *id,
  754. uint64_t *value)
  755. {
  756. PACKET sub;
  757. sub.curr = ossl_quic_wire_decode_transport_param_bytes(pkt,
  758. id, &sub.remaining);
  759. if (sub.curr == NULL)
  760. return 0;
  761. if (!PACKET_get_quic_vlint(&sub, value))
  762. return 0;
  763. if (PACKET_remaining(&sub) > 0)
  764. return 0;
  765. return 1;
  766. }
  767. int ossl_quic_wire_decode_transport_param_cid(PACKET *pkt,
  768. uint64_t *id,
  769. QUIC_CONN_ID *cid)
  770. {
  771. const unsigned char *body;
  772. size_t len = 0;
  773. body = ossl_quic_wire_decode_transport_param_bytes(pkt, id, &len);
  774. if (body == NULL || len > QUIC_MAX_CONN_ID_LEN)
  775. return 0;
  776. cid->id_len = (unsigned char)len;
  777. memcpy(cid->id, body, cid->id_len);
  778. return 1;
  779. }
  780. int ossl_quic_wire_decode_transport_param_preferred_addr(PACKET *pkt,
  781. QUIC_PREFERRED_ADDR *p)
  782. {
  783. const unsigned char *body;
  784. uint64_t id;
  785. size_t len = 0;
  786. PACKET pkt2;
  787. unsigned int ipv4_port, ipv6_port, cidl;
  788. body = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
  789. if (body == NULL
  790. || len < QUIC_MIN_ENCODED_PREFERRED_ADDR_LEN
  791. || len > QUIC_MAX_ENCODED_PREFERRED_ADDR_LEN
  792. || id != QUIC_TPARAM_PREFERRED_ADDR)
  793. return 0;
  794. if (!PACKET_buf_init(&pkt2, body, len))
  795. return 0;
  796. if (!PACKET_copy_bytes(&pkt2, p->ipv4, sizeof(p->ipv4))
  797. || !PACKET_get_net_2(&pkt2, &ipv4_port)
  798. || !PACKET_copy_bytes(&pkt2, p->ipv6, sizeof(p->ipv6))
  799. || !PACKET_get_net_2(&pkt2, &ipv6_port)
  800. || !PACKET_get_1(&pkt2, &cidl)
  801. || cidl > QUIC_MAX_CONN_ID_LEN
  802. || !PACKET_copy_bytes(&pkt2, p->cid.id, cidl)
  803. || !PACKET_copy_bytes(&pkt2, p->stateless_reset.token,
  804. sizeof(p->stateless_reset.token)))
  805. return 0;
  806. p->ipv4_port = (uint16_t)ipv4_port;
  807. p->ipv6_port = (uint16_t)ipv6_port;
  808. p->cid.id_len = (unsigned char)cidl;
  809. return 1;
  810. }
  811. const char *
  812. ossl_quic_frame_type_to_string(uint64_t frame_type)
  813. {
  814. switch (frame_type) {
  815. #define X(name) case OSSL_QUIC_FRAME_TYPE_##name: return #name;
  816. X(PADDING)
  817. X(PING)
  818. X(ACK_WITHOUT_ECN)
  819. X(ACK_WITH_ECN)
  820. X(RESET_STREAM)
  821. X(STOP_SENDING)
  822. X(CRYPTO)
  823. X(NEW_TOKEN)
  824. X(MAX_DATA)
  825. X(MAX_STREAM_DATA)
  826. X(MAX_STREAMS_BIDI)
  827. X(MAX_STREAMS_UNI)
  828. X(DATA_BLOCKED)
  829. X(STREAM_DATA_BLOCKED)
  830. X(STREAMS_BLOCKED_BIDI)
  831. X(STREAMS_BLOCKED_UNI)
  832. X(NEW_CONN_ID)
  833. X(RETIRE_CONN_ID)
  834. X(PATH_CHALLENGE)
  835. X(PATH_RESPONSE)
  836. X(CONN_CLOSE_TRANSPORT)
  837. X(CONN_CLOSE_APP)
  838. X(HANDSHAKE_DONE)
  839. X(STREAM)
  840. X(STREAM_FIN)
  841. X(STREAM_LEN)
  842. X(STREAM_LEN_FIN)
  843. X(STREAM_OFF)
  844. X(STREAM_OFF_FIN)
  845. X(STREAM_OFF_LEN)
  846. X(STREAM_OFF_LEN_FIN)
  847. #undef X
  848. default:
  849. return NULL;
  850. }
  851. }
  852. const char *ossl_quic_err_to_string(uint64_t error_code)
  853. {
  854. switch (error_code) {
  855. #define X(name) case QUIC_ERR_##name: return #name;
  856. X(NO_ERROR)
  857. X(INTERNAL_ERROR)
  858. X(CONNECTION_REFUSED)
  859. X(FLOW_CONTROL_ERROR)
  860. X(STREAM_LIMIT_ERROR)
  861. X(STREAM_STATE_ERROR)
  862. X(FINAL_SIZE_ERROR)
  863. X(FRAME_ENCODING_ERROR)
  864. X(TRANSPORT_PARAMETER_ERROR)
  865. X(CONNECTION_ID_LIMIT_ERROR)
  866. X(PROTOCOL_VIOLATION)
  867. X(INVALID_TOKEN)
  868. X(APPLICATION_ERROR)
  869. X(CRYPTO_BUFFER_EXCEEDED)
  870. X(KEY_UPDATE_ERROR)
  871. X(AEAD_LIMIT_REACHED)
  872. X(NO_VIABLE_PATH)
  873. #undef X
  874. default:
  875. return NULL;
  876. }
  877. }