quic_rx_depack.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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 "internal/packet.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_record_rx_wrap.h"
  16. #include "internal/quic_error.h"
  17. #include "internal/quic_fc.h"
  18. #include "internal/sockets.h"
  19. #include "quic_local.h"
  20. #include "../ssl_local.h"
  21. /*
  22. * TODO(QUIC): ASSUMPTION: the QUIC_CONNECTION structure refers to other related
  23. * components, such as OSSL_ACKM and OSSL_QRX, in some manner. These macros
  24. * should be used to get those components.
  25. */
  26. #define GET_CONN_ACKM(c) ((c)->ackm)
  27. #define GET_CONN_QRX(c) ((c)->qrx)
  28. #define GET_CONN_STATEM(c) ((c)->ssl.statem)
  29. #if 0 /* Currently unimplemented */
  30. # define GET_CONN_ACK_DELAY_EXP(c) (QUIC_CONNECTION_get_ack_delay_exponent(c))
  31. #else
  32. /* 3 is the default, see RFC 9000, 18.2. Transport Parameter Definitions */
  33. # define GET_CONN_ACK_DELAY_EXP(c) 3
  34. #endif
  35. /*
  36. * TODO(QUIC): In MVP the QUIC_CONNECTION is the only supported stream.
  37. */
  38. static QUIC_STREAM *ssl_get_stream(QUIC_CONNECTION *conn, uint64_t stream_id)
  39. {
  40. return stream_id == 0 ? &conn->stream : NULL;
  41. }
  42. /*
  43. * TODO(QUIC): ASSUMPTION: ssl_get_stream_type() gets a stream type from a
  44. * QUIC_STREAM
  45. */
  46. /* Receive */
  47. #define SSL_STREAM_TYPE_R 1
  48. /* Send */
  49. #define SSL_STREAM_TYPE_S 2
  50. /* Bidirectional */
  51. #define SSL_STREAM_TYPE_B (SSL_STREAM_TYPE_R|SSL_STREAM_TYPE_S)
  52. static int ssl_get_stream_type(QUIC_STREAM *stream)
  53. {
  54. return SSL_STREAM_TYPE_B;
  55. }
  56. /*
  57. * We assume that queuing of the data has to be done without copying, thus
  58. * we get the reference counting QRX packet wrapper so it can increment the
  59. * reference count. When the data is consumed (i.e. as a result of, say,
  60. * SSL_read()), ossl_qrx_pkt_wrap_free() must be called.
  61. */
  62. static int ssl_queue_data(QUIC_STREAM *stream, OSSL_QRX_PKT_WRAP *pkt_wrap,
  63. const unsigned char *data, uint64_t data_len,
  64. uint64_t logical_offset, int is_fin)
  65. {
  66. /* Notify stream flow controller */
  67. if (stream->rxfc != NULL
  68. && (!ossl_quic_rxfc_on_rx_stream_frame(stream->rxfc,
  69. logical_offset + data_len,
  70. is_fin)
  71. || ossl_quic_rxfc_get_error(stream->rxfc, 0) != QUIC_ERR_NO_ERROR))
  72. /* QUIC_ERR_FLOW_CONTROL_ERROR or QUIC_ERR_FINAL_SIZE detected */
  73. return 0;
  74. return stream->rstream == NULL
  75. || ossl_quic_rstream_queue_data(stream->rstream, pkt_wrap,
  76. logical_offset, data, data_len,
  77. is_fin);
  78. }
  79. /*
  80. * TODO(QUIC): ASSUMPTION: ssl_close_stream() detaches the QUIC_STREAM from
  81. * the QUIC_CONNECTION it's attached to, and then destroys that QUIC_STREAM
  82. * (as well as its SSL object). |how| works the same way as in shutdown(2),
  83. * i.e. |SHUT_RD| closes the reader part, |SHUT_WR| closes the writer part.
  84. */
  85. static int ssl_close_stream(QUIC_STREAM *stream, int how)
  86. {
  87. return 1;
  88. }
  89. /*
  90. * TODO(QUIC): ASSUMPTION: ssl_close_connection() closes all the streams that
  91. * are attached to it, then closes the QUIC_CONNECTION as well.
  92. * Actual cleanup / destruction of the QUIC_CONNECTION is assumed to be done
  93. * higher up in the call stack (state machine, for example?).
  94. */
  95. static int ssl_close_connection(QUIC_CONNECTION *connection)
  96. {
  97. return 1;
  98. }
  99. /*
  100. * TODO(QUIC): ASSUMPTION: ossl_statem_set_error_state() sets an overall error
  101. * state in the state machine. It's up to the state machine to determine what
  102. * to do with it.
  103. */
  104. #define QUIC_STREAM_STATE_ERROR 1
  105. /*
  106. * QUICfatal() et al is the same as SSLfatal(), but for QUIC. We define a
  107. * placeholder here as long as it's not defined elsewhere.
  108. *
  109. * ossl_quic_fatal() is an error reporting building block used instead of
  110. * ERR_set_error(). In addition to what ERR_set_error() does, this puts
  111. * the state machine into an error state and sends an alert if appropriate,
  112. * and also closes the current connection.
  113. * This is a permanent error for the current connection.
  114. */
  115. #ifndef QUICfatal
  116. static void ossl_quic_fatal(QUIC_CONNECTION *c, int al, int reason,
  117. const char *fmt, ...)
  118. {
  119. va_list args;
  120. va_start(args, fmt);
  121. ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
  122. va_end(args);
  123. /*
  124. * TODO(QUIC): ADD CODE to set the state machine error.
  125. * It's assumed that you can get the state machine with
  126. * GET_CONN_STATEM(c)
  127. */
  128. ssl_close_connection(c);
  129. }
  130. # define QUICfatal(c, al, r) QUICfatal_data((c), (al), (r), NULL)
  131. # define QUICfatal_data \
  132. (ERR_new(), \
  133. ERR_set_debug(OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC), \
  134. ossl_quic_fatal)
  135. #endif
  136. /* TODO(QUIC): [END: TO BE REMOVED] */
  137. /*
  138. * Helper functions to process different frame types.
  139. *
  140. * Typically, those that are ACK eliciting will take an OSSL_ACKM_RX_PKT
  141. * pointer argument, the few that aren't ACK eliciting will not. This makes
  142. * them a verifiable pattern against tables where this is specified.
  143. */
  144. static int depack_do_frame_padding(PACKET *pkt)
  145. {
  146. /* We ignore this frame */
  147. return ossl_quic_wire_decode_padding(pkt);
  148. }
  149. static int depack_do_frame_ping(PACKET *pkt, OSSL_ACKM_RX_PKT *ackm_data)
  150. {
  151. /* We ignore this frame, apart from eliciting an ACK */
  152. if (!ossl_quic_wire_decode_frame_ping(pkt))
  153. return 0;
  154. /* This frame makes the packet ACK eliciting */
  155. ackm_data->is_ack_eliciting = 1;
  156. return 1;
  157. }
  158. static int depack_do_frame_ack(PACKET *pkt, QUIC_CONNECTION *connection,
  159. int packet_space, OSSL_TIME received)
  160. {
  161. OSSL_QUIC_FRAME_ACK ack;
  162. OSSL_QUIC_ACK_RANGE *ack_ranges;
  163. uint64_t total_ranges = 0;
  164. uint32_t ack_delay_exp = GET_CONN_ACK_DELAY_EXP(connection);
  165. int ok = 1; /* Assume the best */
  166. if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
  167. /* In case sizeof(uint64_t) > sizeof(size_t) */
  168. || total_ranges > SIZE_MAX / sizeof(ack_ranges[0])
  169. || (ack_ranges = OPENSSL_zalloc(sizeof(ack_ranges[0])
  170. * (size_t)total_ranges)) == NULL)
  171. return 0;
  172. ack.ack_ranges = ack_ranges;
  173. ack.num_ack_ranges = (size_t)total_ranges;
  174. if (!ossl_quic_wire_decode_frame_ack(pkt, ack_delay_exp, &ack, NULL))
  175. ok = 0;
  176. if (ok
  177. && !ossl_ackm_on_rx_ack_frame(GET_CONN_ACKM(connection), &ack,
  178. packet_space, received))
  179. ok = 0;
  180. OPENSSL_free(ack_ranges);
  181. if (!ok)
  182. return 0;
  183. return 1;
  184. }
  185. static int depack_do_frame_reset_stream(PACKET *pkt,
  186. QUIC_CONNECTION *connection,
  187. OSSL_ACKM_RX_PKT *ackm_data)
  188. {
  189. OSSL_QUIC_FRAME_RESET_STREAM frame_data;
  190. QUIC_STREAM *stream = NULL;
  191. int stream_type = 0;
  192. if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data))
  193. return 0;
  194. /* This frame makes the packet ACK eliciting */
  195. ackm_data->is_ack_eliciting = 1;
  196. if ((stream = ssl_get_stream(connection, frame_data.stream_id)) == NULL)
  197. return 0;
  198. stream_type = ssl_get_stream_type(stream);
  199. ssl_close_stream(stream, SHUT_WR); /* Reuse shutdown(2) symbols */
  200. if ((stream_type & SSL_STREAM_TYPE_S) != 0) {
  201. QUICfatal(connection, QUIC_STREAM_STATE_ERROR, ERR_R_INTERNAL_ERROR);
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. static int depack_do_frame_stop_sending(PACKET *pkt,
  207. QUIC_CONNECTION *connection,
  208. OSSL_ACKM_RX_PKT *ackm_data)
  209. {
  210. OSSL_QUIC_FRAME_STOP_SENDING frame_data;
  211. QUIC_STREAM *stream = NULL;
  212. int stream_type = 0;
  213. if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data))
  214. return 0;
  215. /* This frame makes the packet ACK eliciting */
  216. ackm_data->is_ack_eliciting = 1;
  217. if ((stream = ssl_get_stream(connection, frame_data.stream_id)) == NULL)
  218. return 0;
  219. stream_type = ssl_get_stream_type(stream);
  220. ssl_close_stream(stream, SHUT_RD); /* Reuse shutdown(2) symbols */
  221. if ((stream_type & SSL_STREAM_TYPE_R) != 0) {
  222. QUICfatal(connection, QUIC_STREAM_STATE_ERROR, ERR_R_INTERNAL_ERROR);
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. static int depack_do_frame_crypto(PACKET *pkt, QUIC_CONNECTION *connection,
  228. OSSL_ACKM_RX_PKT *ackm_data)
  229. {
  230. OSSL_QUIC_FRAME_CRYPTO frame_data;
  231. if (!ossl_quic_wire_decode_frame_crypto(pkt, &frame_data))
  232. return 0;
  233. /* This frame makes the packet ACK eliciting */
  234. ackm_data->is_ack_eliciting = 1;
  235. /* TODO(QUIC): ADD CODE to send |frame_data.data| to the handshake manager */
  236. return 1;
  237. }
  238. static int depack_do_frame_new_token(PACKET *pkt, QUIC_CONNECTION *connection,
  239. OSSL_ACKM_RX_PKT *ackm_data)
  240. {
  241. const uint8_t *token;
  242. size_t token_len;
  243. if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len))
  244. return 0;
  245. /* This frame makes the packet ACK eliciting */
  246. ackm_data->is_ack_eliciting = 1;
  247. /* TODO(QUIC): ADD CODE to send |token| to the session manager */
  248. return 1;
  249. }
  250. static int depack_do_frame_stream(PACKET *pkt, QUIC_CONNECTION *connection,
  251. OSSL_QRX_PKT_WRAP *parent_pkt,
  252. OSSL_ACKM_RX_PKT *ackm_data)
  253. {
  254. OSSL_QUIC_FRAME_STREAM frame_data;
  255. QUIC_STREAM *stream;
  256. if (!ossl_quic_wire_decode_frame_stream(pkt, &frame_data))
  257. return 0;
  258. /* This frame makes the packet ACK eliciting */
  259. ackm_data->is_ack_eliciting = 1;
  260. /*
  261. * TODO(QUIC): ASSUMPTION: ssl_get_stream() gets a QUIC_STREAM from a
  262. * QUIC_CONNECTION by stream ID.
  263. */
  264. if ((stream = ssl_get_stream(connection, frame_data.stream_id)) == NULL)
  265. return 0;
  266. /*
  267. * TODO(QUIC): ASSUMPTION: ssl_queue_data() knows what to do with
  268. * |frame_data.offset| and |frame_data.is_fin|.
  269. */
  270. if (!ssl_queue_data(stream, parent_pkt, frame_data.data, frame_data.len,
  271. frame_data.offset, frame_data.is_fin))
  272. return 0;
  273. return 1;
  274. }
  275. static int depack_do_frame_max_data(PACKET *pkt, QUIC_CONNECTION *connection,
  276. OSSL_ACKM_RX_PKT *ackm_data)
  277. {
  278. uint64_t max_data = 0;
  279. if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data))
  280. return 0;
  281. /* This frame makes the packet ACK eliciting */
  282. ackm_data->is_ack_eliciting = 1;
  283. /* TODO(QUIC): ADD CODE to send |max_data| to flow control */
  284. return 1;
  285. }
  286. static int depack_do_frame_max_stream_data(PACKET *pkt,
  287. QUIC_CONNECTION *connection,
  288. OSSL_ACKM_RX_PKT *ackm_data)
  289. {
  290. uint64_t stream_id = 0;
  291. uint64_t max_stream_data = 0;
  292. if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
  293. &max_stream_data))
  294. return 0;
  295. /* This frame makes the packet ACK eliciting */
  296. ackm_data->is_ack_eliciting = 1;
  297. /* TODO(QUIC): ADD CODE to send |max_stream_data| to flow control */
  298. return 1;
  299. }
  300. static int depack_do_frame_max_streams(PACKET *pkt,
  301. QUIC_CONNECTION *connection,
  302. OSSL_ACKM_RX_PKT *ackm_data)
  303. {
  304. uint64_t max_streams = 0;
  305. if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams))
  306. return 0;
  307. /* This frame makes the packet ACK eliciting */
  308. ackm_data->is_ack_eliciting = 1;
  309. /* TODO(QUIC): ADD CODE to send |max_streams| to the connection manager */
  310. return 1;
  311. }
  312. static int depack_do_frame_data_blocked(PACKET *pkt,
  313. QUIC_CONNECTION *connection,
  314. OSSL_ACKM_RX_PKT *ackm_data)
  315. {
  316. uint64_t max_data = 0;
  317. if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data))
  318. return 0;
  319. /* This frame makes the packet ACK eliciting */
  320. ackm_data->is_ack_eliciting = 1;
  321. /* TODO(QUIC): ADD CODE to send |max_data| to flow control */
  322. return 1;
  323. }
  324. static int depack_do_frame_stream_data_blocked(PACKET *pkt,
  325. QUIC_CONNECTION *connection,
  326. OSSL_ACKM_RX_PKT *ackm_data)
  327. {
  328. uint64_t stream_id = 0;
  329. uint64_t max_data = 0;
  330. if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
  331. &max_data))
  332. return 0;
  333. /* This frame makes the packet ACK eliciting */
  334. ackm_data->is_ack_eliciting = 1;
  335. /* TODO(QUIC): ADD CODE to send |max_data| to flow control */
  336. return 1;
  337. }
  338. static int depack_do_frame_streams_blocked(PACKET *pkt,
  339. QUIC_CONNECTION *connection,
  340. OSSL_ACKM_RX_PKT *ackm_data)
  341. {
  342. uint64_t max_data = 0;
  343. if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data))
  344. return 0;
  345. /* This frame makes the packet ACK eliciting */
  346. ackm_data->is_ack_eliciting = 1;
  347. /* TODO(QUIC): ADD CODE to send |max_data| to connection manager */
  348. return 1;
  349. }
  350. static int depack_do_frame_new_conn_id(PACKET *pkt,
  351. QUIC_CONNECTION *connection,
  352. OSSL_ACKM_RX_PKT *ackm_data)
  353. {
  354. OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
  355. if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data))
  356. return 0;
  357. /* This frame makes the packet ACK eliciting */
  358. ackm_data->is_ack_eliciting = 1;
  359. /* TODO(QUIC): ADD CODE to send |frame_data.data| to the connection manager */
  360. return 1;
  361. }
  362. static int depack_do_frame_retire_conn_id(PACKET *pkt,
  363. QUIC_CONNECTION *connection,
  364. OSSL_ACKM_RX_PKT *ackm_data)
  365. {
  366. uint64_t seq_num;
  367. if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num))
  368. return 0;
  369. /* This frame makes the packet ACK eliciting */
  370. ackm_data->is_ack_eliciting = 1;
  371. /* TODO(QUIC): ADD CODE to send |seq_num| to the connection manager */
  372. return 1;
  373. }
  374. static int depack_do_frame_path_challenge(PACKET *pkt,
  375. QUIC_CONNECTION *connection,
  376. OSSL_ACKM_RX_PKT *ackm_data)
  377. {
  378. uint64_t frame_data = 0;
  379. if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data))
  380. return 0;
  381. /* This frame makes the packet ACK eliciting */
  382. ackm_data->is_ack_eliciting = 1;
  383. /* TODO(QUIC): ADD CODE to send |frame_data| to the connection manager */
  384. return 1;
  385. }
  386. static int depack_do_frame_path_response(PACKET *pkt,
  387. QUIC_CONNECTION *connection,
  388. OSSL_ACKM_RX_PKT *ackm_data)
  389. {
  390. uint64_t frame_data = 0;
  391. if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data))
  392. return 0;
  393. /* This frame makes the packet ACK eliciting */
  394. ackm_data->is_ack_eliciting = 1;
  395. /* TODO(QUIC): ADD CODE to send |frame_data| to the connection manager */
  396. return 1;
  397. }
  398. static int depack_do_frame_conn_close(PACKET *pkt, QUIC_CONNECTION *connection)
  399. {
  400. OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
  401. if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data))
  402. return 0;
  403. /* TODO(QUIC): ADD CODE to send |frame_data| to the connection manager */
  404. return 1;
  405. }
  406. static int depack_do_frame_handshake_done(PACKET *pkt,
  407. QUIC_CONNECTION *connection,
  408. OSSL_ACKM_RX_PKT *ackm_data)
  409. {
  410. if (!ossl_quic_wire_decode_frame_handshake_done(pkt))
  411. return 0;
  412. /* This frame makes the packet ACK eliciting */
  413. ackm_data->is_ack_eliciting = 1;
  414. /* TODO(QUIC): ADD CODE to tell the handshake manager that we're done */
  415. return 1;
  416. }
  417. static int depack_do_frame_unknown_extension(PACKET *pkt,
  418. QUIC_CONNECTION *connection,
  419. OSSL_ACKM_RX_PKT *ackm_data)
  420. {
  421. /*
  422. * According to RFC 9000, 19.21. Extension Frames, extension frames
  423. * should be ACK eliciting. It might be over zealous to do so for
  424. * extensions OpenSSL doesn't know how to handle, but shouldn't hurt
  425. * either.
  426. */
  427. /* This frame makes the packet ACK eliciting */
  428. ackm_data->is_ack_eliciting = 1;
  429. /*
  430. * Because we have no idea how to advance to the next frame, we return 0
  431. * everywhere, thereby stopping the depacketizing process.
  432. */
  433. return 0;
  434. }
  435. /* Main frame processor */
  436. static int depack_process_frames(QUIC_CONNECTION *connection, PACKET *pkt,
  437. OSSL_QRX_PKT_WRAP *parent_pkt, int packet_space,
  438. OSSL_TIME received, OSSL_ACKM_RX_PKT *ackm_data)
  439. {
  440. uint32_t pkt_type = parent_pkt->pkt->hdr->type;
  441. while (PACKET_remaining(pkt) > 0) {
  442. uint64_t frame_type;
  443. if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type))
  444. return 0;
  445. switch (frame_type) {
  446. case OSSL_QUIC_FRAME_TYPE_PING:
  447. /* Allowed in all packet types */
  448. if (!depack_do_frame_ping(pkt, ackm_data))
  449. return 0;
  450. break;
  451. case OSSL_QUIC_FRAME_TYPE_PADDING:
  452. /* Allowed in all packet types */
  453. if (!depack_do_frame_padding(pkt))
  454. return 0;
  455. break;
  456. case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
  457. case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
  458. /* ACK frames are valid everywhere except in 0RTT packets */
  459. if (pkt_type == QUIC_PKT_TYPE_0RTT)
  460. return 0;
  461. if (!depack_do_frame_ack(pkt, connection, packet_space, received))
  462. return 0;
  463. break;
  464. case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
  465. /* RESET_STREAM frames are valid in 0RTT and 1RTT packets */
  466. if (pkt_type != QUIC_PKT_TYPE_0RTT
  467. && pkt_type != QUIC_PKT_TYPE_1RTT)
  468. return 0;
  469. if (!depack_do_frame_reset_stream(pkt, connection, ackm_data))
  470. return 0;
  471. break;
  472. case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
  473. /* STOP_SENDING frames are valid in 0RTT and 1RTT packets */
  474. if (pkt_type != QUIC_PKT_TYPE_0RTT
  475. && pkt_type != QUIC_PKT_TYPE_1RTT)
  476. return 0;
  477. if (!depack_do_frame_stop_sending(pkt, connection, ackm_data))
  478. return 0;
  479. break;
  480. case OSSL_QUIC_FRAME_TYPE_CRYPTO:
  481. /* CRYPTO frames are valid everywhere except in 0RTT packets */
  482. if (pkt_type == QUIC_PKT_TYPE_0RTT)
  483. return 0;
  484. if (!depack_do_frame_crypto(pkt, connection, ackm_data))
  485. return 0;
  486. break;
  487. case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
  488. /* NEW_TOKEN frames are valid in 1RTT packets */
  489. if (pkt_type != QUIC_PKT_TYPE_1RTT)
  490. return 0;
  491. if (!depack_do_frame_new_token(pkt, connection, ackm_data))
  492. return 0;
  493. break;
  494. case OSSL_QUIC_FRAME_TYPE_STREAM:
  495. case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
  496. case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
  497. case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
  498. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
  499. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
  500. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
  501. case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
  502. /* STREAM frames are valid in 0RTT and 1RTT packets */
  503. if (pkt_type != QUIC_PKT_TYPE_0RTT
  504. && pkt_type != QUIC_PKT_TYPE_1RTT)
  505. return 0;
  506. if (!depack_do_frame_stream(pkt, connection, parent_pkt, ackm_data))
  507. return 0;
  508. break;
  509. case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
  510. /* MAX_DATA frames are valid in 0RTT and 1RTT packets */
  511. if (pkt_type != QUIC_PKT_TYPE_0RTT
  512. && pkt_type != QUIC_PKT_TYPE_1RTT)
  513. return 0;
  514. if (!depack_do_frame_max_data(pkt, connection, ackm_data))
  515. return 0;
  516. break;
  517. case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
  518. /* MAX_STREAM_DATA frames are valid in 0RTT and 1RTT packets */
  519. if (pkt_type != QUIC_PKT_TYPE_0RTT
  520. && pkt_type != QUIC_PKT_TYPE_1RTT)
  521. return 0;
  522. if (!depack_do_frame_max_stream_data(pkt, connection, ackm_data))
  523. return 0;
  524. break;
  525. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
  526. case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
  527. /* MAX_STREAMS frames are valid in 0RTT and 1RTT packets */
  528. if (pkt_type != QUIC_PKT_TYPE_0RTT
  529. && pkt_type != QUIC_PKT_TYPE_1RTT)
  530. return 0;
  531. if (!depack_do_frame_max_streams(pkt, connection, ackm_data))
  532. return 0;
  533. break;
  534. case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
  535. /* DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
  536. if (pkt_type != QUIC_PKT_TYPE_0RTT
  537. && pkt_type != QUIC_PKT_TYPE_1RTT)
  538. return 0;
  539. if (!depack_do_frame_data_blocked(pkt, connection, ackm_data))
  540. return 0;
  541. break;
  542. case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
  543. /* STREAM_DATA_BLOCKED frames are valid in 0RTT and 1RTT packets */
  544. if (pkt_type != QUIC_PKT_TYPE_0RTT
  545. && pkt_type != QUIC_PKT_TYPE_1RTT)
  546. return 0;
  547. if (!depack_do_frame_stream_data_blocked(pkt, connection, ackm_data))
  548. return 0;
  549. break;
  550. case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
  551. case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
  552. /* STREAMS_BLOCKED frames are valid in 0RTT and 1RTT packets */
  553. if (pkt_type != QUIC_PKT_TYPE_0RTT
  554. && pkt_type != QUIC_PKT_TYPE_1RTT)
  555. return 0;
  556. if (!depack_do_frame_streams_blocked(pkt, connection, ackm_data))
  557. return 0;
  558. break;
  559. case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
  560. /* NEW_CONN_ID frames are valid in 0RTT and 1RTT packets */
  561. if (pkt_type != QUIC_PKT_TYPE_0RTT
  562. && pkt_type != QUIC_PKT_TYPE_1RTT)
  563. return 0;
  564. if (!depack_do_frame_new_conn_id(pkt, connection, ackm_data))
  565. return 0;
  566. break;
  567. case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
  568. /* RETIRE_CONN_ID frames are valid in 0RTT and 1RTT packets */
  569. if (pkt_type != QUIC_PKT_TYPE_0RTT
  570. && pkt_type != QUIC_PKT_TYPE_1RTT)
  571. return 0;
  572. if (!depack_do_frame_retire_conn_id(pkt, connection, ackm_data))
  573. return 0;
  574. break;
  575. case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
  576. /* PATH_CHALLENGE frames are valid in 0RTT and 1RTT packets */
  577. if (pkt_type != QUIC_PKT_TYPE_0RTT
  578. && pkt_type != QUIC_PKT_TYPE_1RTT)
  579. return 0;
  580. if (!depack_do_frame_path_challenge(pkt, connection, ackm_data))
  581. return 0;
  582. break;
  583. case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
  584. /* PATH_RESPONSE frames are valid in 1RTT packets */
  585. if (pkt_type != QUIC_PKT_TYPE_1RTT)
  586. return 0;
  587. if (!depack_do_frame_path_response(pkt, connection, ackm_data))
  588. return 0;
  589. break;
  590. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
  591. /* CONN_CLOSE_APP frames are valid in 0RTT and 1RTT packets */
  592. if (pkt_type != QUIC_PKT_TYPE_0RTT
  593. && pkt_type != QUIC_PKT_TYPE_1RTT)
  594. return 0;
  595. /* FALLTHRU */
  596. case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
  597. /* CONN_CLOSE_TRANSPORT frames are valid in all packets */
  598. if (!depack_do_frame_conn_close(pkt, connection))
  599. return 0;
  600. break;
  601. case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
  602. /* HANDSHAKE_DONE frames are valid in 1RTT packets */
  603. if (pkt_type != QUIC_PKT_TYPE_1RTT)
  604. return 0;
  605. if (!depack_do_frame_handshake_done(pkt, connection, ackm_data))
  606. return 0;
  607. break;
  608. default:
  609. /* Unknown frame type. */
  610. if (!depack_do_frame_unknown_extension(pkt, connection, ackm_data))
  611. return 0;
  612. break;
  613. }
  614. }
  615. return 1;
  616. }
  617. int ossl_quic_handle_frames(QUIC_CONNECTION *connection, OSSL_QRX_PKT *qpacket)
  618. {
  619. PACKET pkt;
  620. OSSL_ACKM_RX_PKT ackm_data;
  621. OSSL_QRX_PKT_WRAP *qpkt_wrap = NULL;
  622. /*
  623. * ok has three states:
  624. * -1 error with ackm_data uninitialized
  625. * 0 error with ackm_data initialized
  626. * 1 success (ackm_data initialized)
  627. */
  628. int ok = -1; /* Assume the worst */
  629. if (connection == NULL)
  630. goto end;
  631. if ((qpkt_wrap = ossl_qrx_pkt_wrap_new(qpacket)) == NULL)
  632. goto end;
  633. /* Initialize |ackm_data| (and reinitialize |ok|)*/
  634. memset(&ackm_data, 0, sizeof(ackm_data));
  635. /*
  636. * TODO(QUIC): ASSUMPTION: All packets that aren't special case have a
  637. * packet number
  638. */
  639. ackm_data.pkt_num = qpacket->pn;
  640. ackm_data.time = qpacket->time;
  641. switch (qpacket->hdr->type) {
  642. case QUIC_PKT_TYPE_INITIAL:
  643. ackm_data.pkt_space = QUIC_PN_SPACE_INITIAL;
  644. break;
  645. case QUIC_PKT_TYPE_HANDSHAKE:
  646. ackm_data.pkt_space = QUIC_PN_SPACE_HANDSHAKE;
  647. break;
  648. case QUIC_PKT_TYPE_0RTT:
  649. case QUIC_PKT_TYPE_1RTT:
  650. ackm_data.pkt_space = QUIC_PN_SPACE_APP;
  651. break;
  652. }
  653. ok = 0; /* Still assume the worst */
  654. /* Handle special cases */
  655. if (qpacket->hdr->type == QUIC_PKT_TYPE_RETRY) {
  656. /* TODO(QUIC): ADD CODE to handle a retry */
  657. goto success;
  658. } else if (qpacket->hdr->type == QUIC_PKT_TYPE_VERSION_NEG) {
  659. /* TODO(QUIC): ADD CODE to handle version negotiation */
  660. goto success;
  661. }
  662. /* Now that special cases are out of the way, parse frames */
  663. if (!PACKET_buf_init(&pkt, qpacket->hdr->data, qpacket->hdr->len)
  664. || !depack_process_frames(connection, &pkt, qpkt_wrap,
  665. ackm_data.pkt_space, qpacket->time,
  666. &ackm_data))
  667. goto end;
  668. success:
  669. ok = 1;
  670. end:
  671. /*
  672. * TODO(QUIC): ASSUMPTION: If this function is called at all, |qpacket| is
  673. * a legitimate packet, even if its contents aren't.
  674. * Therefore, we call ossl_ackm_on_rx_packet() unconditionally, as long as
  675. * |ackm_data| has at least been initialized.
  676. */
  677. if (ok >= 0)
  678. ossl_ackm_on_rx_packet(GET_CONN_ACKM(connection), &ackm_data);
  679. /*
  680. * Let go of the packet pointer in |qpkt_wrap|. This means that the
  681. * reference counter can't be incremented any more.
  682. */
  683. if (qpkt_wrap != NULL)
  684. qpkt_wrap->pkt = NULL;
  685. ossl_qrx_pkt_wrap_free(GET_CONN_QRX(connection), qpkt_wrap);
  686. return ok > 0;
  687. }
  688. int ossl_quic_depacketize(QUIC_CONNECTION *connection)
  689. {
  690. OSSL_QRX_PKT qpacket;
  691. if (connection == NULL)
  692. return 0;
  693. /* Try to read a packet from the read record layer */
  694. memset(&qpacket, 0, sizeof(qpacket));
  695. if (ossl_qrx_read_pkt(GET_CONN_QRX(connection), &qpacket) <= 0)
  696. return 0;
  697. return ossl_quic_handle_frames(connection, &qpacket);
  698. }