quic_rx_depack.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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 "internal/packet_quic.h"
  10. #include "internal/nelem.h"
  11. #include "internal/quic_wire.h"
  12. #include "internal/quic_record_rx.h"
  13. #include "internal/quic_ackm.h"
  14. #include "internal/quic_rx_depack.h"
  15. #include "internal/quic_error.h"
  16. #include "internal/quic_fc.h"
  17. #include "internal/quic_channel.h"
  18. #include "internal/sockets.h"
  19. #include "quic_local.h"
  20. #include "quic_channel_local.h"
  21. #include "../ssl_local.h"
  22. /*
  23. * Helper functions to process different frame types.
  24. *
  25. * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
  26. * pointer argument, the few that aren't ACK eliciting will not. This makes
  27. * them a verifiable pattern against tables where this is specified.
  28. */
  29. static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
  30. uint64_t stream_id,
  31. uint64_t frame_type,
  32. QUIC_STREAM **result);
  33. static int depack_do_frame_padding(PACKET *pkt)
  34. {
  35. /* We ignore this frame */
  36. ossl_quic_wire_decode_padding(pkt);
  37. return 1;
  38. }
  39. static int depack_do_frame_ping(PACKET *pkt, QUIC_CHANNEL *ch,
  40. uint32_t enc_level,
  41. OSSL_ACKM_RX_PKT *ackm_data)
  42. {
  43. /* We ignore this frame, apart from eliciting an ACK */
  44. if (!ossl_quic_wire_decode_frame_ping(pkt)) {
  45. ossl_quic_channel_raise_protocol_error(ch,
  46. QUIC_ERR_FRAME_ENCODING_ERROR,
  47. OSSL_QUIC_FRAME_TYPE_PING,
  48. "decode error");
  49. return 0;
  50. }
  51. ossl_quic_tx_packetiser_schedule_ack_eliciting(ch->txp, enc_level);
  52. return 1;
  53. }
  54. static int depack_do_frame_ack(PACKET *pkt, QUIC_CHANNEL *ch,
  55. int packet_space, OSSL_TIME received,
  56. uint64_t frame_type,
  57. OSSL_QRX_PKT *qpacket)
  58. {
  59. OSSL_QUIC_FRAME_ACK ack;
  60. OSSL_QUIC_ACK_RANGE *p;
  61. uint64_t total_ranges = 0;
  62. uint32_t ack_delay_exp = ch->rx_ack_delay_exp;
  63. if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
  64. /* In case sizeof(uint64_t) > sizeof(size_t) */
  65. || total_ranges > SIZE_MAX / sizeof(OSSL_QUIC_ACK_RANGE))
  66. goto malformed;
  67. if (ch->num_ack_range_scratch < (size_t)total_ranges) {
  68. if ((p = OPENSSL_realloc(ch->ack_range_scratch,
  69. sizeof(OSSL_QUIC_ACK_RANGE)
  70. * (size_t)total_ranges)) == NULL)
  71. goto malformed;
  72. ch->ack_range_scratch = p;
  73. ch->num_ack_range_scratch = (size_t)total_ranges;
  74. }
  75. ack.ack_ranges = ch->ack_range_scratch;
  76. ack.num_ack_ranges = (size_t)total_ranges;
  77. if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
  78. goto malformed;
  79. if (qpacket->hdr->type == QUIC_PKT_TYPE_1RTT
  80. && (qpacket->key_epoch < ossl_qrx_get_key_epoch(ch->qrx)
  81. || ch->rxku_expected)
  82. && ack.ack_ranges[0].end >= ch->txku_pn) {
  83. /*
  84. * RFC 9001 s. 6.2: An endpoint that receives an acknowledgment that is
  85. * carried in a packet protected with old keys where any acknowledged
  86. * packet was protected with newer keys MAY treat that as a connection
  87. * error of type KEY_UPDATE_ERROR.
  88. *
  89. * Two cases to handle here:
  90. *
  91. * - We did spontaneous TXKU, the peer has responded in kind and we
  92. * have detected RXKU; !ch->rxku_expected, but then it sent a packet
  93. * with old keys acknowledging a packet in the new key epoch.
  94. *
  95. * This also covers the case where we got RXKU and triggered
  96. * solicited TXKU, and then for some reason the peer sent an ACK of
  97. * a PN in our new TX key epoch with old keys.
  98. *
  99. * - We did spontaneous TXKU; ch->txku_pn is the starting PN of our
  100. * new TX key epoch; the peer has not initiated a solicited TXKU in
  101. * response (so we have not detected RXKU); in this case the RX key
  102. * epoch has not incremented and ch->rxku_expected is still 1.
  103. */
  104. ossl_quic_channel_raise_protocol_error(ch,
  105. QUIC_ERR_KEY_UPDATE_ERROR,
  106. frame_type,
  107. "acked packet which initiated a "
  108. "key update without a "
  109. "corresponding key update");
  110. return 0;
  111. }
  112. if (!ossl_ackm_on_rx_ack_frame(ch->ackm, &ack,
  113. packet_space, received))
  114. goto malformed;
  115. ++ch->diag_num_rx_ack;
  116. return 1;
  117. malformed:
  118. ossl_quic_channel_raise_protocol_error(ch,
  119. QUIC_ERR_FRAME_ENCODING_ERROR,
  120. frame_type,
  121. "decode error");
  122. return 0;
  123. }
  124. static int depack_do_frame_reset_stream(PACKET *pkt,
  125. QUIC_CHANNEL *ch,
  126. OSSL_ACKM_RX_PKT *ackm_data)
  127. {
  128. OSSL_QUIC_FRAME_RESET_STREAM frame_data;
  129. QUIC_STREAM *stream = NULL;
  130. uint64_t fce;
  131. if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data)) {
  132. ossl_quic_channel_raise_protocol_error(ch,
  133. QUIC_ERR_FRAME_ENCODING_ERROR,
  134. OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  135. "decode error");
  136. return 0;
  137. }
  138. if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
  139. OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  140. &stream))
  141. return 0; /* error already raised for us */
  142. if (stream == NULL)
  143. return 1; /* old deleted stream, not a protocol violation, ignore */
  144. if (!ossl_quic_stream_has_recv(stream)) {
  145. ossl_quic_channel_raise_protocol_error(ch,
  146. QUIC_ERR_STREAM_STATE_ERROR,
  147. OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  148. "RESET_STREAM frame for "
  149. "TX only stream");
  150. return 0;
  151. }
  152. /*
  153. * The final size field of the RESET_STREAM frame must be used to determine
  154. * how much flow control credit the aborted stream was considered to have
  155. * consumed.
  156. *
  157. * We also need to ensure that if we already have a final size for the
  158. * stream, the RESET_STREAM frame's Final Size field matches this; we SHOULD
  159. * terminate the connection otherwise (RFC 9000 s. 4.5). The RXFC takes care
  160. * of this for us.
  161. */
  162. if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
  163. frame_data.final_size, /*is_fin=*/1)) {
  164. ossl_quic_channel_raise_protocol_error(ch,
  165. QUIC_ERR_INTERNAL_ERROR,
  166. OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  167. "internal error (flow control)");
  168. return 0;
  169. }
  170. /* Has a flow control error occurred? */
  171. fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
  172. if (fce != QUIC_ERR_NO_ERROR) {
  173. ossl_quic_channel_raise_protocol_error(ch,
  174. fce,
  175. OSSL_QUIC_FRAME_TYPE_RESET_STREAM,
  176. "flow control violation");
  177. return 0;
  178. }
  179. /*
  180. * Depending on the receive part state this is handled either as a reset
  181. * transition or a no-op (e.g. if a reset has already been received before,
  182. * or the application already retired a FIN). Best effort - there are no
  183. * protocol error conditions we need to check for here.
  184. */
  185. ossl_quic_stream_map_notify_reset_recv_part(&ch->qsm, stream,
  186. frame_data.app_error_code,
  187. frame_data.final_size);
  188. ossl_quic_stream_map_update_state(&ch->qsm, stream);
  189. return 1;
  190. }
  191. static int depack_do_frame_stop_sending(PACKET *pkt,
  192. QUIC_CHANNEL *ch,
  193. OSSL_ACKM_RX_PKT *ackm_data)
  194. {
  195. OSSL_QUIC_FRAME_STOP_SENDING frame_data;
  196. QUIC_STREAM *stream = NULL;
  197. if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data)) {
  198. ossl_quic_channel_raise_protocol_error(ch,
  199. QUIC_ERR_FRAME_ENCODING_ERROR,
  200. OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
  201. "decode error");
  202. return 0;
  203. }
  204. if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
  205. OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
  206. &stream))
  207. return 0; /* error already raised for us */
  208. if (stream == NULL)
  209. return 1; /* old deleted stream, not a protocol violation, ignore */
  210. if (!ossl_quic_stream_has_send(stream)) {
  211. ossl_quic_channel_raise_protocol_error(ch,
  212. QUIC_ERR_STREAM_STATE_ERROR,
  213. OSSL_QUIC_FRAME_TYPE_STOP_SENDING,
  214. "STOP_SENDING frame for "
  215. "RX only stream");
  216. return 0;
  217. }
  218. stream->peer_stop_sending = 1;
  219. stream->peer_stop_sending_aec = frame_data.app_error_code;
  220. /*
  221. * RFC 9000 s. 3.5: Receiving a STOP_SENDING frame means we must respond in
  222. * turn with a RESET_STREAM frame for the same part of the stream. The other
  223. * part is unaffected.
  224. */
  225. ossl_quic_stream_map_reset_stream_send_part(&ch->qsm, stream,
  226. frame_data.app_error_code);
  227. return 1;
  228. }
  229. static int depack_do_frame_crypto(PACKET *pkt, QUIC_CHANNEL *ch,
  230. OSSL_QRX_PKT *parent_pkt,
  231. OSSL_ACKM_RX_PKT *ackm_data,
  232. uint64_t *datalen)
  233. {
  234. OSSL_QUIC_FRAME_CRYPTO f;
  235. QUIC_RSTREAM *rstream;
  236. QUIC_RXFC *rxfc;
  237. *datalen = 0;
  238. if (!ossl_quic_wire_decode_frame_crypto(pkt, 0, &f)) {
  239. ossl_quic_channel_raise_protocol_error(ch,
  240. QUIC_ERR_FRAME_ENCODING_ERROR,
  241. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  242. "decode error");
  243. return 0;
  244. }
  245. if (f.len == 0)
  246. return 1; /* nothing to do */
  247. rstream = ch->crypto_recv[ackm_data->pkt_space];
  248. if (!ossl_assert(rstream != NULL))
  249. /*
  250. * This should not happen; we should only have a NULL stream here if
  251. * the EL has been discarded, and if the EL has been discarded we
  252. * shouldn't be here.
  253. */
  254. return 0;
  255. rxfc = &ch->crypto_rxfc[ackm_data->pkt_space];
  256. if (!ossl_quic_rxfc_on_rx_stream_frame(rxfc, f.offset + f.len,
  257. /*is_fin=*/0)) {
  258. ossl_quic_channel_raise_protocol_error(ch,
  259. QUIC_ERR_INTERNAL_ERROR,
  260. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  261. "internal error (crypto RXFC)");
  262. return 0;
  263. }
  264. if (ossl_quic_rxfc_get_error(rxfc, 0) != QUIC_ERR_NO_ERROR) {
  265. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_CRYPTO_BUFFER_EXCEEDED,
  266. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  267. "exceeded maximum crypto buffer");
  268. return 0;
  269. }
  270. if (!ossl_quic_rstream_queue_data(rstream, parent_pkt,
  271. f.offset, f.data, f.len, 0)) {
  272. ossl_quic_channel_raise_protocol_error(ch,
  273. QUIC_ERR_INTERNAL_ERROR,
  274. OSSL_QUIC_FRAME_TYPE_CRYPTO,
  275. "internal error (rstream queue)");
  276. return 0;
  277. }
  278. ch->did_crypto_frame = 1;
  279. *datalen = f.len;
  280. return 1;
  281. }
  282. static int depack_do_frame_new_token(PACKET *pkt, QUIC_CHANNEL *ch,
  283. OSSL_ACKM_RX_PKT *ackm_data)
  284. {
  285. const uint8_t *token;
  286. size_t token_len;
  287. if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len)) {
  288. ossl_quic_channel_raise_protocol_error(ch,
  289. QUIC_ERR_FRAME_ENCODING_ERROR,
  290. OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
  291. "decode error");
  292. return 0;
  293. }
  294. if (token_len == 0) {
  295. /*
  296. * RFC 9000 s. 19.7: "A client MUST treat receipt of a NEW_TOKEN frame
  297. * with an empty Token field as a connection error of type
  298. * FRAME_ENCODING_ERROR."
  299. */
  300. ossl_quic_channel_raise_protocol_error(ch,
  301. QUIC_ERR_FRAME_ENCODING_ERROR,
  302. OSSL_QUIC_FRAME_TYPE_NEW_TOKEN,
  303. "zero-length NEW_TOKEN");
  304. return 0;
  305. }
  306. /* TODO(QUIC FUTURE): ADD CODE to send |token| to the session manager */
  307. return 1;
  308. }
  309. /*
  310. * Returns 1 if no protocol violation has occurred. In this case *result will be
  311. * non-NULL unless this is an old deleted stream and we should ignore the frame
  312. * causing this function to be called. Returns 0 on protocol violation.
  313. */
  314. static int depack_do_implicit_stream_create(QUIC_CHANNEL *ch,
  315. uint64_t stream_id,
  316. uint64_t frame_type,
  317. QUIC_STREAM **result)
  318. {
  319. QUIC_STREAM *stream;
  320. uint64_t peer_role, stream_ordinal;
  321. uint64_t *p_next_ordinal_local, *p_next_ordinal_remote;
  322. QUIC_RXFC *max_streams_fc;
  323. int is_uni, is_remote_init;
  324. stream = ossl_quic_stream_map_get_by_id(&ch->qsm, stream_id);
  325. if (stream != NULL) {
  326. *result = stream;
  327. return 1;
  328. }
  329. /*
  330. * If we do not yet have a stream with the given ID, there are three
  331. * possibilities:
  332. *
  333. * (a) The stream ID is for a remotely-created stream and the peer
  334. * is creating a stream.
  335. *
  336. * (b) The stream ID is for a locally-created stream which has
  337. * previously been deleted.
  338. *
  339. * (c) The stream ID is for a locally-created stream which does
  340. * not exist yet. This is a protocol violation and we must
  341. * terminate the connection in this case.
  342. *
  343. * We distinguish between (b) and (c) using the stream ID allocator
  344. * variable. Since stream ordinals are allocated monotonically, we
  345. * simply determine if the stream ordinal is in the future.
  346. */
  347. peer_role = ch->is_server
  348. ? QUIC_STREAM_INITIATOR_CLIENT
  349. : QUIC_STREAM_INITIATOR_SERVER;
  350. is_remote_init = ((stream_id & QUIC_STREAM_INITIATOR_MASK) == peer_role);
  351. is_uni = ((stream_id & QUIC_STREAM_DIR_MASK) == QUIC_STREAM_DIR_UNI);
  352. stream_ordinal = stream_id >> 2;
  353. if (is_remote_init) {
  354. /*
  355. * Peer-created stream which does not yet exist. Create it. QUIC stream
  356. * ordinals within a given stream type MUST be used in sequence and
  357. * receiving a STREAM frame for ordinal n must implicitly create streams
  358. * with ordinals [0, n) within that stream type even if no explicit
  359. * STREAM frames are received for those ordinals.
  360. */
  361. p_next_ordinal_remote = is_uni
  362. ? &ch->next_remote_stream_ordinal_uni
  363. : &ch->next_remote_stream_ordinal_bidi;
  364. /* Check this isn't violating stream count flow control. */
  365. max_streams_fc = is_uni
  366. ? &ch->max_streams_uni_rxfc
  367. : &ch->max_streams_bidi_rxfc;
  368. if (!ossl_quic_rxfc_on_rx_stream_frame(max_streams_fc,
  369. stream_ordinal + 1,
  370. /*is_fin=*/0)) {
  371. ossl_quic_channel_raise_protocol_error(ch,
  372. QUIC_ERR_INTERNAL_ERROR,
  373. frame_type,
  374. "internal error (stream count RXFC)");
  375. return 0;
  376. }
  377. if (ossl_quic_rxfc_get_error(max_streams_fc, 0) != QUIC_ERR_NO_ERROR) {
  378. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_STREAM_LIMIT_ERROR,
  379. frame_type,
  380. "exceeded maximum allowed streams");
  381. return 0;
  382. }
  383. /*
  384. * Create the named stream and any streams coming before it yet to be
  385. * created.
  386. */
  387. while (*p_next_ordinal_remote <= stream_ordinal) {
  388. uint64_t cur_stream_id = (*p_next_ordinal_remote << 2) |
  389. (stream_id
  390. & (QUIC_STREAM_DIR_MASK | QUIC_STREAM_INITIATOR_MASK));
  391. stream = ossl_quic_channel_new_stream_remote(ch, cur_stream_id);
  392. if (stream == NULL) {
  393. ossl_quic_channel_raise_protocol_error(ch,
  394. QUIC_ERR_INTERNAL_ERROR,
  395. frame_type,
  396. "internal error (stream allocation)");
  397. return 0;
  398. }
  399. ++*p_next_ordinal_remote;
  400. }
  401. *result = stream;
  402. } else {
  403. /* Locally-created stream which does not yet exist. */
  404. p_next_ordinal_local = is_uni
  405. ? &ch->next_local_stream_ordinal_uni
  406. : &ch->next_local_stream_ordinal_bidi;
  407. if (stream_ordinal >= *p_next_ordinal_local) {
  408. /*
  409. * We never created this stream yet, this is a protocol
  410. * violation.
  411. */
  412. ossl_quic_channel_raise_protocol_error(ch,
  413. QUIC_ERR_STREAM_STATE_ERROR,
  414. frame_type,
  415. "STREAM frame for nonexistent "
  416. "stream");
  417. return 0;
  418. }
  419. /*
  420. * Otherwise this is for an old locally-initiated stream which we
  421. * have subsequently deleted. Ignore the data; it may simply be a
  422. * retransmission. We already take care of notifying the peer of the
  423. * termination of the stream during the stream deletion lifecycle.
  424. */
  425. *result = NULL;
  426. }
  427. return 1;
  428. }
  429. static int depack_do_frame_stream(PACKET *pkt, QUIC_CHANNEL *ch,
  430. OSSL_QRX_PKT *parent_pkt,
  431. OSSL_ACKM_RX_PKT *ackm_data,
  432. uint64_t frame_type,
  433. uint64_t *datalen)
  434. {
  435. OSSL_QUIC_FRAME_STREAM frame_data;
  436. QUIC_STREAM *stream;
  437. uint64_t fce;
  438. size_t rs_avail;
  439. int rs_fin = 0;
  440. *datalen = 0;
  441. if (!ossl_quic_wire_decode_frame_stream(pkt, 0, &frame_data)) {
  442. ossl_quic_channel_raise_protocol_error(ch,
  443. QUIC_ERR_FRAME_ENCODING_ERROR,
  444. frame_type,
  445. "decode error");
  446. return 0;
  447. }
  448. if (!depack_do_implicit_stream_create(ch, frame_data.stream_id,
  449. frame_type, &stream))
  450. return 0; /* protocol error raised by above call */
  451. if (stream == NULL)
  452. /*
  453. * Data for old stream which is not a protocol violation but should be
  454. * ignored, so stop here.
  455. */
  456. return 1;
  457. if (!ossl_quic_stream_has_recv(stream)) {
  458. ossl_quic_channel_raise_protocol_error(ch,
  459. QUIC_ERR_STREAM_STATE_ERROR,
  460. frame_type,
  461. "STREAM frame for TX only "
  462. "stream");
  463. return 0;
  464. }
  465. /* Notify stream flow controller. */
  466. if (!ossl_quic_rxfc_on_rx_stream_frame(&stream->rxfc,
  467. frame_data.offset + frame_data.len,
  468. frame_data.is_fin)) {
  469. ossl_quic_channel_raise_protocol_error(ch,
  470. QUIC_ERR_INTERNAL_ERROR,
  471. frame_type,
  472. "internal error (flow control)");
  473. return 0;
  474. }
  475. /* Has a flow control error occurred? */
  476. fce = ossl_quic_rxfc_get_error(&stream->rxfc, 0);
  477. if (fce != QUIC_ERR_NO_ERROR) {
  478. ossl_quic_channel_raise_protocol_error(ch,
  479. fce,
  480. frame_type,
  481. "flow control violation");
  482. return 0;
  483. }
  484. switch (stream->recv_state) {
  485. case QUIC_RSTREAM_STATE_RECV:
  486. case QUIC_RSTREAM_STATE_SIZE_KNOWN:
  487. /*
  488. * It only makes sense to process incoming STREAM frames in these
  489. * states.
  490. */
  491. break;
  492. case QUIC_RSTREAM_STATE_DATA_RECVD:
  493. case QUIC_RSTREAM_STATE_DATA_READ:
  494. case QUIC_RSTREAM_STATE_RESET_RECVD:
  495. case QUIC_RSTREAM_STATE_RESET_READ:
  496. default:
  497. /*
  498. * We have no use for STREAM frames once the receive part reaches any of
  499. * these states, so just ignore.
  500. */
  501. return 1;
  502. }
  503. /* If we are in RECV, auto-transition to SIZE_KNOWN on FIN. */
  504. if (frame_data.is_fin
  505. && !ossl_quic_stream_recv_get_final_size(stream, NULL)) {
  506. /* State was already checked above, so can't fail. */
  507. ossl_quic_stream_map_notify_size_known_recv_part(&ch->qsm, stream,
  508. frame_data.offset
  509. + frame_data.len);
  510. }
  511. /*
  512. * If we requested STOP_SENDING do not bother buffering the data. Note that
  513. * this must happen after RXFC checks above as even if we sent STOP_SENDING
  514. * we must still enforce correct flow control (RFC 9000 s. 3.5).
  515. */
  516. if (stream->stop_sending)
  517. return 1; /* not an error - packet reordering, etc. */
  518. /*
  519. * The receive stream buffer may or may not choose to consume the data
  520. * without copying by reffing the OSSL_QRX_PKT. In this case
  521. * ossl_qrx_pkt_release() will be eventually called when the data is no
  522. * longer needed.
  523. *
  524. * It is OK for the peer to send us a zero-length non-FIN STREAM frame,
  525. * which is a no-op, aside from the fact that it ensures the stream exists.
  526. * In this case we have nothing to report to the receive buffer.
  527. */
  528. if ((frame_data.len > 0 || frame_data.is_fin)
  529. && !ossl_quic_rstream_queue_data(stream->rstream, parent_pkt,
  530. frame_data.offset,
  531. frame_data.data,
  532. frame_data.len,
  533. frame_data.is_fin)) {
  534. ossl_quic_channel_raise_protocol_error(ch,
  535. QUIC_ERR_INTERNAL_ERROR,
  536. frame_type,
  537. "internal error (rstream queue)");
  538. return 0;
  539. }
  540. /*
  541. * rs_fin will be 1 only if we can read all data up to and including the FIN
  542. * without any gaps before it; this implies we have received all data. Avoid
  543. * calling ossl_quic_rstream_available() where it is not necessary as it is
  544. * more expensive.
  545. */
  546. if (stream->recv_state == QUIC_RSTREAM_STATE_SIZE_KNOWN
  547. && !ossl_quic_rstream_available(stream->rstream, &rs_avail, &rs_fin)) {
  548. ossl_quic_channel_raise_protocol_error(ch,
  549. QUIC_ERR_INTERNAL_ERROR,
  550. frame_type,
  551. "internal error (rstream available)");
  552. return 0;
  553. }
  554. if (rs_fin)
  555. ossl_quic_stream_map_notify_totally_received(&ch->qsm, stream);
  556. *datalen = frame_data.len;
  557. return 1;
  558. }
  559. static void update_streams(QUIC_STREAM *s, void *arg)
  560. {
  561. QUIC_CHANNEL *ch = arg;
  562. ossl_quic_stream_map_update_state(&ch->qsm, s);
  563. }
  564. static void update_streams_bidi(QUIC_STREAM *s, void *arg)
  565. {
  566. QUIC_CHANNEL *ch = arg;
  567. if (!ossl_quic_stream_is_bidi(s))
  568. return;
  569. ossl_quic_stream_map_update_state(&ch->qsm, s);
  570. }
  571. static void update_streams_uni(QUIC_STREAM *s, void *arg)
  572. {
  573. QUIC_CHANNEL *ch = arg;
  574. if (ossl_quic_stream_is_bidi(s))
  575. return;
  576. ossl_quic_stream_map_update_state(&ch->qsm, s);
  577. }
  578. static int depack_do_frame_max_data(PACKET *pkt, QUIC_CHANNEL *ch,
  579. OSSL_ACKM_RX_PKT *ackm_data)
  580. {
  581. uint64_t max_data = 0;
  582. if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data)) {
  583. ossl_quic_channel_raise_protocol_error(ch,
  584. QUIC_ERR_FRAME_ENCODING_ERROR,
  585. OSSL_QUIC_FRAME_TYPE_MAX_DATA,
  586. "decode error");
  587. return 0;
  588. }
  589. ossl_quic_txfc_bump_cwm(&ch->conn_txfc, max_data);
  590. ossl_quic_stream_map_visit(&ch->qsm, update_streams, ch);
  591. return 1;
  592. }
  593. static int depack_do_frame_max_stream_data(PACKET *pkt,
  594. QUIC_CHANNEL *ch,
  595. OSSL_ACKM_RX_PKT *ackm_data)
  596. {
  597. uint64_t stream_id = 0;
  598. uint64_t max_stream_data = 0;
  599. QUIC_STREAM *stream;
  600. if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
  601. &max_stream_data)) {
  602. ossl_quic_channel_raise_protocol_error(ch,
  603. QUIC_ERR_FRAME_ENCODING_ERROR,
  604. OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
  605. "decode error");
  606. return 0;
  607. }
  608. if (!depack_do_implicit_stream_create(ch, stream_id,
  609. OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
  610. &stream))
  611. return 0; /* error already raised for us */
  612. if (stream == NULL)
  613. return 1; /* old deleted stream, not a protocol violation, ignore */
  614. if (!ossl_quic_stream_has_send(stream)) {
  615. ossl_quic_channel_raise_protocol_error(ch,
  616. QUIC_ERR_STREAM_STATE_ERROR,
  617. OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA,
  618. "MAX_STREAM_DATA for TX only "
  619. "stream");
  620. return 0;
  621. }
  622. ossl_quic_txfc_bump_cwm(&stream->txfc, max_stream_data);
  623. ossl_quic_stream_map_update_state(&ch->qsm, stream);
  624. return 1;
  625. }
  626. static int depack_do_frame_max_streams(PACKET *pkt,
  627. QUIC_CHANNEL *ch,
  628. OSSL_ACKM_RX_PKT *ackm_data,
  629. uint64_t frame_type)
  630. {
  631. uint64_t max_streams = 0;
  632. if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams)) {
  633. ossl_quic_channel_raise_protocol_error(ch,
  634. QUIC_ERR_FRAME_ENCODING_ERROR,
  635. frame_type,
  636. "decode error");
  637. return 0;
  638. }
  639. if (max_streams > (((uint64_t)1) << 60)) {
  640. ossl_quic_channel_raise_protocol_error(ch,
  641. QUIC_ERR_FRAME_ENCODING_ERROR,
  642. frame_type,
  643. "invalid max streams value");
  644. return 0;
  645. }
  646. switch (frame_type) {
  647. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
  648. if (max_streams > ch->max_local_streams_bidi)
  649. ch->max_local_streams_bidi = max_streams;
  650. /* Some streams may now be able to send. */
  651. ossl_quic_stream_map_visit(&ch->qsm, update_streams_bidi, ch);
  652. break;
  653. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
  654. if (max_streams > ch->max_local_streams_uni)
  655. ch->max_local_streams_uni = max_streams;
  656. /* Some streams may now be able to send. */
  657. ossl_quic_stream_map_visit(&ch->qsm, update_streams_uni, ch);
  658. break;
  659. default:
  660. ossl_quic_channel_raise_protocol_error(ch,
  661. QUIC_ERR_FRAME_ENCODING_ERROR,
  662. frame_type,
  663. "decode error");
  664. return 0;
  665. }
  666. return 1;
  667. }
  668. static int depack_do_frame_data_blocked(PACKET *pkt,
  669. QUIC_CHANNEL *ch,
  670. OSSL_ACKM_RX_PKT *ackm_data)
  671. {
  672. uint64_t max_data = 0;
  673. if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data)) {
  674. ossl_quic_channel_raise_protocol_error(ch,
  675. QUIC_ERR_FRAME_ENCODING_ERROR,
  676. OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED,
  677. "decode error");
  678. return 0;
  679. }
  680. /* No-op - informative/debugging frame. */
  681. return 1;
  682. }
  683. static int depack_do_frame_stream_data_blocked(PACKET *pkt,
  684. QUIC_CHANNEL *ch,
  685. OSSL_ACKM_RX_PKT *ackm_data)
  686. {
  687. uint64_t stream_id = 0;
  688. uint64_t max_data = 0;
  689. QUIC_STREAM *stream;
  690. if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
  691. &max_data)) {
  692. ossl_quic_channel_raise_protocol_error(ch,
  693. QUIC_ERR_FRAME_ENCODING_ERROR,
  694. OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
  695. "decode error");
  696. return 0;
  697. }
  698. /*
  699. * This is an informative/debugging frame, so we don't have to do anything,
  700. * but it does trigger stream creation.
  701. */
  702. if (!depack_do_implicit_stream_create(ch, stream_id,
  703. OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
  704. &stream))
  705. return 0; /* error already raised for us */
  706. if (stream == NULL)
  707. return 1; /* old deleted stream, not a protocol violation, ignore */
  708. if (!ossl_quic_stream_has_recv(stream)) {
  709. /*
  710. * RFC 9000 s. 19.14: "An endpoint that receives a STREAM_DATA_BLOCKED
  711. * frame for a send-only stream MUST terminate the connection with error
  712. * STREAM_STATE_ERROR."
  713. */
  714. ossl_quic_channel_raise_protocol_error(ch,
  715. QUIC_ERR_STREAM_STATE_ERROR,
  716. OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED,
  717. "STREAM_DATA_BLOCKED frame for "
  718. "TX only stream");
  719. return 0;
  720. }
  721. /* No-op - informative/debugging frame. */
  722. return 1;
  723. }
  724. static int depack_do_frame_streams_blocked(PACKET *pkt,
  725. QUIC_CHANNEL *ch,
  726. OSSL_ACKM_RX_PKT *ackm_data,
  727. uint64_t frame_type)
  728. {
  729. uint64_t max_data = 0;
  730. if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data)) {
  731. ossl_quic_channel_raise_protocol_error(ch,
  732. QUIC_ERR_FRAME_ENCODING_ERROR,
  733. frame_type,
  734. "decode error");
  735. return 0;
  736. }
  737. if (max_data > (((uint64_t)1) << 60)) {
  738. /*
  739. * RFC 9000 s. 19.14: "This value cannot exceed 2**60, as it is not
  740. * possible to encode stream IDs larger than 2**62 - 1. Receipt of a
  741. * frame that encodes a larger stream ID MUST be treated as a connection
  742. * error of type STREAM_LIMIT_ERROR or FRAME_ENCODING_ERROR."
  743. */
  744. ossl_quic_channel_raise_protocol_error(ch,
  745. QUIC_ERR_STREAM_LIMIT_ERROR,
  746. frame_type,
  747. "invalid stream count limit");
  748. return 0;
  749. }
  750. /* No-op - informative/debugging frame. */
  751. return 1;
  752. }
  753. static int depack_do_frame_new_conn_id(PACKET *pkt,
  754. QUIC_CHANNEL *ch,
  755. OSSL_ACKM_RX_PKT *ackm_data)
  756. {
  757. OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
  758. if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data)) {
  759. ossl_quic_channel_raise_protocol_error(ch,
  760. QUIC_ERR_FRAME_ENCODING_ERROR,
  761. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  762. "decode error");
  763. return 0;
  764. }
  765. ossl_quic_channel_on_new_conn_id(ch, &frame_data);
  766. return 1;
  767. }
  768. static int depack_do_frame_retire_conn_id(PACKET *pkt,
  769. QUIC_CHANNEL *ch,
  770. OSSL_ACKM_RX_PKT *ackm_data)
  771. {
  772. uint64_t seq_num;
  773. if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num)) {
  774. ossl_quic_channel_raise_protocol_error(ch,
  775. QUIC_ERR_FRAME_ENCODING_ERROR,
  776. OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
  777. "decode error");
  778. return 0;
  779. }
  780. /*
  781. * RFC 9000 s. 19.16: "An endpoint cannot send this frame if it was provided
  782. * with a zero-length connection ID by its peer. An endpoint that provides a
  783. * zero-length connection ID MUST treat receipt of a RETIRE_CONNECTION_ID
  784. * frame as a connection error of type PROTOCOL_VIOLATION."
  785. *
  786. * Since we always use a zero-length SCID as a client, there is no case
  787. * where it is valid for a server to send this. Our server support is
  788. * currently non-conformant and for internal testing use; simply handle it
  789. * as a no-op in this case.
  790. *
  791. * TODO(QUIC SERVER): Revise and implement correctly for server support.
  792. */
  793. if (!ch->is_server) {
  794. ossl_quic_channel_raise_protocol_error(ch,
  795. QUIC_ERR_PROTOCOL_VIOLATION,
  796. OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
  797. "conn has zero-length CID");
  798. return 0;
  799. }
  800. return 1;
  801. }
  802. static void free_path_response(unsigned char *buf, size_t buf_len, void *arg)
  803. {
  804. OPENSSL_free(buf);
  805. }
  806. static int depack_do_frame_path_challenge(PACKET *pkt,
  807. QUIC_CHANNEL *ch,
  808. OSSL_ACKM_RX_PKT *ackm_data)
  809. {
  810. uint64_t frame_data = 0;
  811. unsigned char *encoded = NULL;
  812. size_t encoded_len;
  813. WPACKET wpkt;
  814. if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data)) {
  815. ossl_quic_channel_raise_protocol_error(ch,
  816. QUIC_ERR_FRAME_ENCODING_ERROR,
  817. OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
  818. "decode error");
  819. return 0;
  820. }
  821. /*
  822. * RFC 9000 s. 8.2.2: On receiving a PATH_CHALLENGE frame, an endpoint MUST
  823. * respond by echoing the data contained in the PATH_CHALLENGE frame in a
  824. * PATH_RESPONSE frame.
  825. *
  826. * TODO(QUIC FUTURE): We should try to avoid allocation here in the future.
  827. */
  828. encoded_len = sizeof(uint64_t) + 1;
  829. if ((encoded = OPENSSL_malloc(encoded_len)) == NULL)
  830. goto err;
  831. if (!WPACKET_init_static_len(&wpkt, encoded, encoded_len, 0))
  832. goto err;
  833. if (!ossl_quic_wire_encode_frame_path_response(&wpkt, frame_data)) {
  834. WPACKET_cleanup(&wpkt);
  835. goto err;
  836. }
  837. WPACKET_finish(&wpkt);
  838. if (!ossl_quic_cfq_add_frame(ch->cfq, 0, QUIC_PN_SPACE_APP,
  839. OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
  840. QUIC_CFQ_ITEM_FLAG_UNRELIABLE,
  841. encoded, encoded_len,
  842. free_path_response, NULL))
  843. goto err;
  844. return 1;
  845. err:
  846. OPENSSL_free(encoded);
  847. ossl_quic_channel_raise_protocol_error(ch, QUIC_ERR_INTERNAL_ERROR,
  848. OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE,
  849. "internal error");
  850. return 0;
  851. }
  852. static int depack_do_frame_path_response(PACKET *pkt,
  853. QUIC_CHANNEL *ch,
  854. OSSL_ACKM_RX_PKT *ackm_data)
  855. {
  856. uint64_t frame_data = 0;
  857. if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data)) {
  858. ossl_quic_channel_raise_protocol_error(ch,
  859. QUIC_ERR_FRAME_ENCODING_ERROR,
  860. OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE,
  861. "decode error");
  862. return 0;
  863. }
  864. /* TODO(QUIC MULTIPATH): ADD CODE to send |frame_data| to the ch manager */
  865. return 1;
  866. }
  867. static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CHANNEL *ch,
  868. uint64_t frame_type)
  869. {
  870. OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
  871. if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data)) {
  872. ossl_quic_channel_raise_protocol_error(ch,
  873. QUIC_ERR_FRAME_ENCODING_ERROR,
  874. frame_type,
  875. "decode error");
  876. return 0;
  877. }
  878. ossl_quic_channel_on_remote_conn_close(ch, &frame_data);
  879. return 1;
  880. }
  881. static int depack_do_frame_handshake_done(PACKET *pkt,
  882. QUIC_CHANNEL *ch,
  883. OSSL_ACKM_RX_PKT *ackm_data)
  884. {
  885. if (!ossl_quic_wire_decode_frame_handshake_done(pkt)) {
  886. /* This can fail only with an internal error. */
  887. ossl_quic_channel_raise_protocol_error(ch,
  888. QUIC_ERR_INTERNAL_ERROR,
  889. OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE,
  890. "internal error (decode frame handshake done)");
  891. return 0;
  892. }
  893. ossl_quic_channel_on_handshake_confirmed(ch);
  894. return 1;
  895. }
  896. /* Main frame processor */
  897. static int depack_process_frames(QUIC_CHANNEL *ch, PACKET *pkt,
  898. OSSL_QRX_PKT *parent_pkt, uint32_t enc_level,
  899. OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
  900. {
  901. uint32_t pkt_type = parent_pkt->hdr->type;
  902. uint32_t packet_space = ossl_quic_enc_level_to_pn_space(enc_level);
  903. if (PACKET_remaining(pkt) == 0) {
  904. /*
  905. * RFC 9000 s. 12.4: An endpoint MUST treat receipt of a packet
  906. * containing no frames as a connection error of type
  907. * PROTOCOL_VIOLATION.
  908. */
  909. ossl_quic_channel_raise_protocol_error(ch,
  910. QUIC_ERR_PROTOCOL_VIOLATION,
  911. 0,
  912. "empty packet payload");
  913. return 0;
  914. }
  915. while (PACKET_remaining(pkt) > 0) {
  916. int was_minimal;
  917. uint64_t frame_type;
  918. const unsigned char *sof = NULL;
  919. uint64_t datalen = 0;
  920. if (ch->msg_callback != NULL)
  921. sof = PACKET_data(pkt);
  922. if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type, &was_minimal)) {
  923. ossl_quic_channel_raise_protocol_error(ch,
  924. QUIC_ERR_PROTOCOL_VIOLATION,
  925. 0,
  926. "malformed frame header");
  927. return 0;
  928. }
  929. if (!was_minimal) {
  930. ossl_quic_channel_raise_protocol_error(ch,
  931. QUIC_ERR_PROTOCOL_VIOLATION,
  932. frame_type,
  933. "non-minimal frame type encoding");
  934. return 0;
  935. }
  936. /*
  937. * There are only a few frame types which are not ACK-eliciting. Handle
  938. * these centrally to make error handling cases more resilient, as we
  939. * should tell the ACKM about an ACK-eliciting frame even if it was not
  940. * successfully handled.
  941. */
  942. switch (frame_type) {
  943. case OSSL_QUIC_FRAME_TYPE_PADDING:
  944. case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
  945. case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
  946. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
  947. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
  948. break;
  949. default:
  950. ackm_data->is_ack_eliciting = 1;
  951. break;
  952. }
  953. switch (frame_type) {
  954. case OSSL_QUIC_FRAME_TYPE_PING:
  955. /* Allowed in all packet types */
  956. if (!depack_do_frame_ping(pkt, ch, enc_level, ackm_data))
  957. return 0;
  958. break;
  959. case OSSL_QUIC_FRAME_TYPE_PADDING:
  960. /* Allowed in all packet types */
  961. if (!depack_do_frame_padding(pkt))
  962. return 0;
  963. break;
  964. case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
  965. case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
  966. /* ACK frames are valid everywhere except in 0RTT packets */
  967. if (pkt_type == QUIC_PKT_TYPE_0RTT) {
  968. ossl_quic_channel_raise_protocol_error(ch,
  969. QUIC_ERR_PROTOCOL_VIOLATION,
  970. frame_type,
  971. "ACK not valid in 0-RTT");
  972. return 0;
  973. }
  974. if (!depack_do_frame_ack(pkt, ch, packet_space, received,
  975. frame_type, parent_pkt))
  976. return 0;
  977. break;
  978. case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
  979. /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
  980. if (pkt_type != QUIC_PKT_TYPE_0RTT
  981. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  982. ossl_quic_channel_raise_protocol_error(ch,
  983. QUIC_ERR_PROTOCOL_VIOLATION,
  984. frame_type,
  985. "RESET_STREAM not valid in "
  986. "INITIAL/HANDSHAKE");
  987. return 0;
  988. }
  989. if (!depack_do_frame_reset_stream(pkt, ch, ackm_data))
  990. return 0;
  991. break;
  992. case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
  993. /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
  994. if (pkt_type != QUIC_PKT_TYPE_0RTT
  995. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  996. ossl_quic_channel_raise_protocol_error(ch,
  997. QUIC_ERR_PROTOCOL_VIOLATION,
  998. frame_type,
  999. "STOP_SENDING not valid in "
  1000. "INITIAL/HANDSHAKE");
  1001. return 0;
  1002. }
  1003. if (!depack_do_frame_stop_sending(pkt, ch, ackm_data))
  1004. return 0;
  1005. break;
  1006. case OSSL_QUIC_FRAME_TYPE_CRYPTO:
  1007. /* CRYPTO frames are valid everywhere except in 0RTT packets */
  1008. if (pkt_type == QUIC_PKT_TYPE_0RTT) {
  1009. ossl_quic_channel_raise_protocol_error(ch,
  1010. QUIC_ERR_PROTOCOL_VIOLATION,
  1011. frame_type,
  1012. "CRYPTO frame not valid in 0-RTT");
  1013. return 0;
  1014. }
  1015. if (!depack_do_frame_crypto(pkt, ch, parent_pkt, ackm_data, &datalen))
  1016. return 0;
  1017. break;
  1018. case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
  1019. /* NEW_TOKEN frames are valid in 1RTT packets */
  1020. if (pkt_type != QUIC_PKT_TYPE_1RTT) {
  1021. ossl_quic_channel_raise_protocol_error(ch,
  1022. QUIC_ERR_PROTOCOL_VIOLATION,
  1023. frame_type,
  1024. "NEW_TOKEN valid only in 1-RTT");
  1025. return 0;
  1026. }
  1027. if (!depack_do_frame_new_token(pkt, ch, ackm_data))
  1028. return 0;
  1029. break;
  1030. case OSSL_QUIC_FRAME_TYPE_STREAM:
  1031. case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
  1032. case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
  1033. case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
  1034. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
  1035. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
  1036. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
  1037. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
  1038. /* STREAM frames are valid in 0RTT and 1RTT packets */
  1039. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1040. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1041. ossl_quic_channel_raise_protocol_error(ch,
  1042. QUIC_ERR_PROTOCOL_VIOLATION,
  1043. frame_type,
  1044. "STREAM valid only in 0/1-RTT");
  1045. return 0;
  1046. }
  1047. if (!depack_do_frame_stream(pkt, ch, parent_pkt, ackm_data,
  1048. frame_type, &datalen))
  1049. return 0;
  1050. break;
  1051. case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
  1052. /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
  1053. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1054. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1055. ossl_quic_channel_raise_protocol_error(ch,
  1056. QUIC_ERR_PROTOCOL_VIOLATION,
  1057. frame_type,
  1058. "MAX_DATA valid only in 0/1-RTT");
  1059. return 0;
  1060. }
  1061. if (!depack_do_frame_max_data(pkt, ch, ackm_data))
  1062. return 0;
  1063. break;
  1064. case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
  1065. /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
  1066. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1067. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1068. ossl_quic_channel_raise_protocol_error(ch,
  1069. QUIC_ERR_PROTOCOL_VIOLATION,
  1070. frame_type,
  1071. "MAX_STREAM_DATA valid only in 0/1-RTT");
  1072. return 0;
  1073. }
  1074. if (!depack_do_frame_max_stream_data(pkt, ch, ackm_data))
  1075. return 0;
  1076. break;
  1077. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
  1078. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
  1079. /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
  1080. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1081. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1082. ossl_quic_channel_raise_protocol_error(ch,
  1083. QUIC_ERR_PROTOCOL_VIOLATION,
  1084. frame_type,
  1085. "MAX_STREAMS valid only in 0/1-RTT");
  1086. return 0;
  1087. }
  1088. if (!depack_do_frame_max_streams(pkt, ch, ackm_data,
  1089. frame_type))
  1090. return 0;
  1091. break;
  1092. case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
  1093. /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
  1094. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1095. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1096. ossl_quic_channel_raise_protocol_error(ch,
  1097. QUIC_ERR_PROTOCOL_VIOLATION,
  1098. frame_type,
  1099. "DATA_BLOCKED valid only in 0/1-RTT");
  1100. return 0;
  1101. }
  1102. if (!depack_do_frame_data_blocked(pkt, ch, ackm_data))
  1103. return 0;
  1104. break;
  1105. case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
  1106. /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
  1107. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1108. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1109. ossl_quic_channel_raise_protocol_error(ch,
  1110. QUIC_ERR_PROTOCOL_VIOLATION,
  1111. frame_type,
  1112. "STREAM_DATA_BLOCKED valid only in 0/1-RTT");
  1113. return 0;
  1114. }
  1115. if (!depack_do_frame_stream_data_blocked(pkt, ch, ackm_data))
  1116. return 0;
  1117. break;
  1118. case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
  1119. case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
  1120. /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
  1121. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1122. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1123. ossl_quic_channel_raise_protocol_error(ch,
  1124. QUIC_ERR_PROTOCOL_VIOLATION,
  1125. frame_type,
  1126. "STREAMS valid only in 0/1-RTT");
  1127. return 0;
  1128. }
  1129. if (!depack_do_frame_streams_blocked(pkt, ch, ackm_data,
  1130. frame_type))
  1131. return 0;
  1132. break;
  1133. case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
  1134. /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
  1135. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1136. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1137. ossl_quic_channel_raise_protocol_error(ch,
  1138. QUIC_ERR_PROTOCOL_VIOLATION,
  1139. frame_type,
  1140. "NEW_CONN_ID valid only in 0/1-RTT");
  1141. }
  1142. if (!depack_do_frame_new_conn_id(pkt, ch, ackm_data))
  1143. return 0;
  1144. break;
  1145. case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
  1146. /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
  1147. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1148. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1149. ossl_quic_channel_raise_protocol_error(ch,
  1150. QUIC_ERR_PROTOCOL_VIOLATION,
  1151. frame_type,
  1152. "RETIRE_CONN_ID valid only in 0/1-RTT");
  1153. return 0;
  1154. }
  1155. if (!depack_do_frame_retire_conn_id(pkt, ch, ackm_data))
  1156. return 0;
  1157. break;
  1158. case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
  1159. /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
  1160. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1161. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1162. ossl_quic_channel_raise_protocol_error(ch,
  1163. QUIC_ERR_PROTOCOL_VIOLATION,
  1164. frame_type,
  1165. "PATH_CHALLENGE valid only in 0/1-RTT");
  1166. return 0;
  1167. }
  1168. if (!depack_do_frame_path_challenge(pkt, ch, ackm_data))
  1169. return 0;
  1170. break;
  1171. case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
  1172. /* PATH_RESPONSE frames are valid in 1RTT packets */
  1173. if (pkt_type != QUIC_PKT_TYPE_1RTT) {
  1174. ossl_quic_channel_raise_protocol_error(ch,
  1175. QUIC_ERR_PROTOCOL_VIOLATION,
  1176. frame_type,
  1177. "PATH_CHALLENGE valid only in 1-RTT");
  1178. return 0;
  1179. }
  1180. if (!depack_do_frame_path_response(pkt, ch, ackm_data))
  1181. return 0;
  1182. break;
  1183. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
  1184. /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
  1185. if (pkt_type != QUIC_PKT_TYPE_0RTT
  1186. && pkt_type != QUIC_PKT_TYPE_1RTT) {
  1187. ossl_quic_channel_raise_protocol_error(ch,
  1188. QUIC_ERR_PROTOCOL_VIOLATION,
  1189. frame_type,
  1190. "CONN_CLOSE (APP) valid only in 0/1-RTT");
  1191. return 0;
  1192. }
  1193. /* FALLTHRU */
  1194. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
  1195. /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
  1196. if (!depack_do_frame_conn_close(pkt, ch, frame_type))
  1197. return 0;
  1198. break;
  1199. case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
  1200. /* HANDSHAKE_DONE frames are valid in 1RTT packets */
  1201. if (pkt_type != QUIC_PKT_TYPE_1RTT) {
  1202. ossl_quic_channel_raise_protocol_error(ch,
  1203. QUIC_ERR_PROTOCOL_VIOLATION,
  1204. frame_type,
  1205. "HANDSHAKE_DONE valid only in 1-RTT");
  1206. return 0;
  1207. }
  1208. if (!depack_do_frame_handshake_done(pkt, ch, ackm_data))
  1209. return 0;
  1210. break;
  1211. default:
  1212. /* Unknown frame type */
  1213. ossl_quic_channel_raise_protocol_error(ch,
  1214. QUIC_ERR_FRAME_ENCODING_ERROR,
  1215. frame_type,
  1216. "Unknown frame type received");
  1217. return 0;
  1218. }
  1219. if (ch->msg_callback != NULL) {
  1220. int ctype = SSL3_RT_QUIC_FRAME_FULL;
  1221. size_t framelen = PACKET_data(pkt) - sof;
  1222. if (frame_type == OSSL_QUIC_FRAME_TYPE_PADDING) {
  1223. ctype = SSL3_RT_QUIC_FRAME_PADDING;
  1224. } else if (OSSL_QUIC_FRAME_TYPE_IS_STREAM(frame_type)
  1225. || frame_type == OSSL_QUIC_FRAME_TYPE_CRYPTO) {
  1226. ctype = SSL3_RT_QUIC_FRAME_HEADER;
  1227. framelen -= (size_t)datalen;
  1228. }
  1229. ch->msg_callback(0, OSSL_QUIC1_VERSION, ctype, sof, framelen,
  1230. ch->msg_callback_ssl, ch->msg_callback_arg);
  1231. }
  1232. }
  1233. return 1;
  1234. }
  1235. QUIC_NEEDS_LOCK
  1236. int ossl_quic_handle_frames(QUIC_CHANNEL *ch, OSSL_QRX_PKT *qpacket)
  1237. {
  1238. PACKET pkt;
  1239. OSSL_ACKM_RX_PKT ackm_data;
  1240. uint32_t enc_level;
  1241. /*
  1242. * ok has three states:
  1243. * -1 error with ackm_data uninitialized
  1244. * 0 error with ackm_data initialized
  1245. * 1 success (ackm_data initialized)
  1246. */
  1247. int ok = -1; /* Assume the worst */
  1248. if (ch == NULL)
  1249. goto end;
  1250. ch->did_crypto_frame = 0;
  1251. /* Initialize |ackm_data| (and reinitialize |ok|)*/
  1252. memset(&ackm_data, 0, sizeof(ackm_data));
  1253. /*
  1254. * ASSUMPTION: All packets that aren't special case have a
  1255. * packet number.
  1256. */
  1257. ackm_data.pkt_num = qpacket->pn;
  1258. ackm_data.time = qpacket->time;
  1259. enc_level = ossl_quic_pkt_type_to_enc_level(qpacket->hdr->type);
  1260. if (enc_level >= QUIC_ENC_LEVEL_NUM)
  1261. /*
  1262. * Retry and Version Negotiation packets should not be passed to this
  1263. * function.
  1264. */
  1265. goto end;
  1266. ok = 0; /* Still assume the worst */
  1267. ackm_data.pkt_space = ossl_quic_enc_level_to_pn_space(enc_level);
  1268. /* Now that special cases are out of the way, parse frames */
  1269. if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
  1270. || !depack_process_frames(ch, &pkt, qpacket,
  1271. enc_level,
  1272. qpacket->time,
  1273. &ackm_data))
  1274. goto end;
  1275. ok = 1;
  1276. end:
  1277. /*
  1278. * ASSUMPTION: If this function is called at all, |qpacket| is
  1279. * a legitimate packet, even if its contents aren't.
  1280. * Therefore, we call ossl_ackm_on_rx_packet() unconditionally, as long as
  1281. * |ackm_data| has at least been initialized.
  1282. */
  1283. if (ok >= 0)
  1284. ossl_ackm_on_rx_packet(ch->ackm, &ackm_data);
  1285. return ok > 0;
  1286. }