2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/bio.h>
11 #include "../ssl_local.h"
12 #include "internal/quic_wire_pkt.h"
14 static const char *packet_type(int type)
17 case QUIC_PKT_TYPE_INITIAL:
20 case QUIC_PKT_TYPE_0RTT:
23 case QUIC_PKT_TYPE_HANDSHAKE:
26 case QUIC_PKT_TYPE_RETRY:
29 case QUIC_PKT_TYPE_1RTT:
32 case QUIC_PKT_TYPE_VERSION_NEG:
40 static const char *conn_id(QUIC_CONN_ID *id, char *buf, size_t buflen)
46 return "<zero length id>";
48 if ((((size_t)id->id_len * 2) + 2) > buflen - 1)
49 return "<id too long>"; /* Should never happen */
56 for (i = 0; i < id->id_len; i++, buflen -= 2, buf += 2)
57 BIO_snprintf(buf, buflen, "%02x", id->id[i]);
62 static int frame_ack(BIO *bio, PACKET *pkt)
64 OSSL_QUIC_FRAME_ACK ack;
65 OSSL_QUIC_ACK_RANGE *ack_ranges = NULL;
66 uint64_t total_ranges = 0;
68 if (!ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &total_ranges)
69 /* In case sizeof(uint64_t) > sizeof(size_t) */
70 || total_ranges > SIZE_MAX / sizeof(ack_ranges[0])
71 || (ack_ranges = OPENSSL_zalloc(sizeof(ack_ranges[0])
72 * (size_t)total_ranges)) == NULL)
75 ack.ack_ranges = ack_ranges;
76 ack.num_ack_ranges = (size_t)total_ranges;
78 if (!ossl_quic_wire_decode_frame_ack(pkt, 0, &ack, NULL))
81 /* TODO(QUIC): Display the ack data here */
83 OPENSSL_free(ack_ranges);
87 static int frame_reset_stream(BIO *bio, PACKET *pkt)
89 OSSL_QUIC_FRAME_RESET_STREAM frame_data;
91 if (!ossl_quic_wire_decode_frame_reset_stream(pkt, &frame_data))
94 /* TODO(QUIC): Display reset stream data here */
99 static int frame_stop_sending(BIO *bio, PACKET *pkt)
101 OSSL_QUIC_FRAME_STOP_SENDING frame_data;
103 if (!ossl_quic_wire_decode_frame_stop_sending(pkt, &frame_data))
109 static int frame_crypto(BIO *bio, PACKET *pkt)
111 OSSL_QUIC_FRAME_CRYPTO frame_data;
113 if (!ossl_quic_wire_decode_frame_crypto(pkt, &frame_data))
116 BIO_printf(bio, " Offset: %lu\n", frame_data.offset);
117 BIO_printf(bio, " Len: %lu\n", frame_data.len);
122 static int frame_new_token(BIO *bio, PACKET *pkt)
124 const uint8_t *token;
127 if (!ossl_quic_wire_decode_frame_new_token(pkt, &token, &token_len))
133 static int frame_stream(BIO *bio, PACKET *pkt, uint64_t frame_type)
136 OSSL_QUIC_FRAME_STREAM frame_data;
138 BIO_puts(bio, "Stream");
140 case OSSL_QUIC_FRAME_TYPE_STREAM:
144 case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
145 BIO_puts(bio, " (Fin)\n");
148 case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
149 BIO_puts(bio, " (Len)\n");
152 case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
153 BIO_puts(bio, " (Len, Fin)\n");
156 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
157 BIO_puts(bio, " (Off)\n");
160 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
161 BIO_puts(bio, " (Off, Fin)\n");
164 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
165 BIO_puts(bio, " (Off, Len)\n");
168 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
169 BIO_puts(bio, " (Off, Len, Fin)\n");
176 if (!ossl_quic_wire_decode_frame_stream(pkt, &frame_data))
179 BIO_printf(bio, " Stream id: %lu\n", frame_data.stream_id);
180 BIO_printf(bio, " Offset: %lu\n", frame_data.offset);
181 BIO_printf(bio, " Len: %lu\n", frame_data.len);
186 static int frame_max_data(BIO *bio, PACKET *pkt)
188 uint64_t max_data = 0;
190 if (!ossl_quic_wire_decode_frame_max_data(pkt, &max_data))
196 static int frame_max_stream_data(BIO *bio, PACKET *pkt)
198 uint64_t stream_id = 0;
199 uint64_t max_stream_data = 0;
201 if (!ossl_quic_wire_decode_frame_max_stream_data(pkt, &stream_id,
208 static int frame_max_streams(BIO *bio, PACKET *pkt)
210 uint64_t max_streams = 0;
212 if (!ossl_quic_wire_decode_frame_max_streams(pkt, &max_streams))
218 static int frame_data_blocked(BIO *bio, PACKET *pkt)
220 uint64_t max_data = 0;
222 if (!ossl_quic_wire_decode_frame_data_blocked(pkt, &max_data))
228 static int frame_stream_data_blocked(BIO *bio, PACKET *pkt)
230 uint64_t stream_id = 0;
231 uint64_t max_data = 0;
233 if (!ossl_quic_wire_decode_frame_stream_data_blocked(pkt, &stream_id,
240 static int frame_streams_blocked(BIO *bio, PACKET *pkt)
242 uint64_t max_data = 0;
244 if (!ossl_quic_wire_decode_frame_streams_blocked(pkt, &max_data))
250 static int frame_new_conn_id(BIO *bio, PACKET *pkt)
252 OSSL_QUIC_FRAME_NEW_CONN_ID frame_data;
254 if (!ossl_quic_wire_decode_frame_new_conn_id(pkt, &frame_data))
260 static int frame_retire_conn_id(BIO *bio, PACKET *pkt)
264 if (!ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num))
270 static int frame_path_challenge(BIO *bio, PACKET *pkt)
272 uint64_t frame_data = 0;
274 if (!ossl_quic_wire_decode_frame_path_challenge(pkt, &frame_data))
280 static int frame_path_response(BIO *bio, PACKET *pkt)
282 uint64_t frame_data = 0;
284 if (!ossl_quic_wire_decode_frame_path_response(pkt, &frame_data))
290 static int frame_conn_closed(BIO *bio, PACKET *pkt)
292 OSSL_QUIC_FRAME_CONN_CLOSE frame_data;
294 if (!ossl_quic_wire_decode_frame_conn_close(pkt, &frame_data))
300 static int trace_frame_data(BIO *bio, PACKET *pkt)
304 if (!ossl_quic_wire_peek_frame_header(pkt, &frame_type))
307 switch (frame_type) {
308 case OSSL_QUIC_FRAME_TYPE_PING:
309 BIO_puts(bio, "Ping\n");
310 if (!ossl_quic_wire_decode_frame_ping(pkt))
314 case OSSL_QUIC_FRAME_TYPE_PADDING:
315 BIO_puts(bio, "Padding\n");
316 ossl_quic_wire_decode_padding(pkt);
319 case OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN:
320 case OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN:
321 BIO_puts(bio, "Ack ");
322 if (frame_type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN)
323 BIO_puts(bio, " (with ECN)\n");
325 BIO_puts(bio, " (without ECN)\n");
326 if (!frame_ack(bio, pkt))
330 case OSSL_QUIC_FRAME_TYPE_RESET_STREAM:
331 BIO_puts(bio, "Reset stream\n");
332 if (!frame_reset_stream(bio, pkt))
336 case OSSL_QUIC_FRAME_TYPE_STOP_SENDING:
337 BIO_puts(bio, "Stop sending\n");
338 if (!frame_stop_sending(bio, pkt))
342 case OSSL_QUIC_FRAME_TYPE_CRYPTO:
343 BIO_puts(bio, "Crypto\n");
344 if (!frame_crypto(bio, pkt))
348 case OSSL_QUIC_FRAME_TYPE_NEW_TOKEN:
349 BIO_puts(bio, "New token\n");
350 if (!frame_new_token(bio, pkt))
354 case OSSL_QUIC_FRAME_TYPE_STREAM:
355 case OSSL_QUIC_FRAME_TYPE_STREAM_FIN:
356 case OSSL_QUIC_FRAME_TYPE_STREAM_LEN:
357 case OSSL_QUIC_FRAME_TYPE_STREAM_LEN_FIN:
358 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF:
359 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_FIN:
360 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN:
361 case OSSL_QUIC_FRAME_TYPE_STREAM_OFF_LEN_FIN:
362 /* frame_stream() prints the frame type string */
363 if (!frame_stream(bio, pkt, frame_type))
367 case OSSL_QUIC_FRAME_TYPE_MAX_DATA:
368 BIO_puts(bio, "Max data\n");
369 if (!frame_max_data(bio, pkt))
373 case OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA:
374 BIO_puts(bio, "Max stream data\n");
375 if (!frame_max_stream_data(bio, pkt))
379 case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI:
380 case OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI:
381 BIO_puts(bio, "Max streams ");
382 if (frame_type == OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)
383 BIO_puts(bio, " (Bidi)\n");
385 BIO_puts(bio, " (Uni)\n");
386 if (!frame_max_streams(bio, pkt))
390 case OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED:
391 BIO_puts(bio, "Data blocked\n");
392 if (!frame_data_blocked(bio, pkt))
396 case OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED:
397 BIO_puts(bio, "Stream data blocked\n");
398 if (!frame_stream_data_blocked(bio, pkt))
402 case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI:
403 case OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI:
404 BIO_puts(bio, "Streams blocked");
405 if (frame_type == OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)
406 BIO_puts(bio, " (Bidi)\n");
408 BIO_puts(bio, " (Uni)\n");
409 if (!frame_streams_blocked(bio, pkt))
413 case OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID:
414 BIO_puts(bio, "New conn id\n");
415 if (!frame_new_conn_id(bio, pkt))
419 case OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID:
420 BIO_puts(bio, "Retire conn id\n");
421 if (!frame_retire_conn_id(bio, pkt))
425 case OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE:
426 BIO_puts(bio, "Path challenge\n");
427 if (!frame_path_challenge(bio, pkt))
431 case OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE:
432 BIO_puts(bio, "Path response\n");
433 if (!frame_path_response(bio, pkt))
437 case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP:
438 case OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT:
439 BIO_puts(bio, "Connection close");
440 if (frame_type == OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP)
441 BIO_puts(bio, " (app)\n");
443 BIO_puts(bio, " (transport)\n");
444 if (!frame_conn_closed(bio, pkt))
448 case OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE:
449 BIO_puts(bio, "Handshake done\n");
450 if (!ossl_quic_wire_decode_frame_handshake_done(pkt))
458 if (PACKET_remaining(pkt) != 0)
459 BIO_puts(bio, " <unexpected trailing frame data skipped>\n");
464 int ossl_quic_trace(int write_p, int version, int content_type,
465 const void *buf, size_t msglen, SSL *ssl, void *arg)
470 switch (content_type) {
471 case SSL3_RT_QUIC_DATAGRAM:
472 BIO_puts(bio, write_p ? "Sent" : "Received");
474 * Unfortunately there is no way of receiving auxilliary information
475 * about the datagram through the msg_callback API such as the peer
478 BIO_printf(bio, " Datagram\n Length: %zu\n", msglen);
481 case SSL3_RT_QUIC_PACKET:
485 * Max Conn id is 20 bytes (40 hex digits) plus "0x" bytes plus NUL
491 if (!PACKET_buf_init(&pkt, buf, msglen))
493 /* Decode the packet header */
495 * TODO(QUIC): We need to query the short connection id len here,
496 * e.g. via some API SSL_get_short_conn_id_len()
498 if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 1, &hdr, NULL) != 1)
501 BIO_puts(bio, write_p ? "Sent" : "Received");
502 BIO_puts(bio, " Packet\n");
503 BIO_printf(bio, " Packet Type: %s\n", packet_type(hdr.type));
504 if (hdr.type != QUIC_PKT_TYPE_1RTT)
505 BIO_printf(bio, " Version: 0x%08x\n", hdr.version);
506 BIO_printf(bio, " Destination Conn Id: %s\n",
507 conn_id(&hdr.dst_conn_id, tmpbuf, sizeof(tmpbuf)));
508 if (hdr.type != QUIC_PKT_TYPE_1RTT)
509 BIO_printf(bio, " Source Conn Id: %s\n",
510 conn_id(&hdr.src_conn_id, tmpbuf, sizeof(tmpbuf)));
511 BIO_printf(bio, " Payload length: %zu\n", hdr.len);
512 if (hdr.type == QUIC_PKT_TYPE_INITIAL) {
513 BIO_puts(bio, " Token: ");
514 if (hdr.token_len == 0) {
515 BIO_puts(bio, "<zerlo length token>");
517 for (i = 0; i < hdr.token_len; i++)
518 BIO_printf(bio, "%02x", hdr.token[i]);
522 if (hdr.type != QUIC_PKT_TYPE_VERSION_NEG
523 && hdr.type != QUIC_PKT_TYPE_RETRY) {
524 BIO_puts(bio, " Packet Number: 0x");
525 /* Will always be at least 1 byte */
526 for (i = 0; i < hdr.pn_len; i++)
527 BIO_printf(bio, "%02x", hdr.pn[i]);
533 case SSL3_RT_QUIC_FRAME_PADDING:
534 case SSL3_RT_QUIC_FRAME_FULL:
536 BIO_puts(bio, write_p ? "Sent" : "Received");
537 BIO_puts(bio, " Frame: ");
539 if (!PACKET_buf_init(&pkt, buf, msglen))
541 if (!trace_frame_data(bio, &pkt)) {
542 BIO_puts(bio, " <error processing frame data>\n");
548 case SSL3_RT_QUIC_FRAME_HEADER:
550 BIO_puts(bio, write_p ? "Sent" : "Received");
551 BIO_puts(bio, " Frame Data\n");
553 /* TODO(QUIC): Implement me */
554 BIO_puts(bio, " <content skipped>\n");
559 /* Unrecognised content_type. We defer to SSL_trace */