]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Enable tracing of packets that have been sent
authorMatt Caswell <matt@openssl.org>
Fri, 5 May 2023 15:51:43 +0000 (16:51 +0100)
committerMatt Caswell <matt@openssl.org>
Wed, 24 May 2023 11:18:33 +0000 (12:18 +0100)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20914)

12 files changed:
include/internal/quic_record_rx.h
include/internal/quic_record_tx.h
include/internal/quic_types.h
include/internal/quic_wire_pkt.h
ssl/quic/quic_channel.c
ssl/quic/quic_record_rx.c
ssl/quic/quic_record_tx.c
ssl/quic/quic_trace.c
ssl/quic/quic_wire_pkt.c
test/helpers/quictestlib.c
test/quic_record_test.c
test/quic_wire_test.c

index 48173479936e667e32f4bc6d1e79d51a8a275687..f372887f1b2ce136d365c0289ed3178e10b5e05c 100644 (file)
@@ -18,9 +18,6 @@
 
 # ifndef OPENSSL_NO_QUIC
 
-typedef void (*ossl_msg_cb)(int write_p, int version, int content_type,
-                            const void *buf, size_t len, SSL *ssl, void *arg);
-
 /*
  * QUIC Record Layer - RX
  * ======================
index 2cc7333c029a9e0d86c3e0a48f1664e31627d670..21bfb9d01b03427c5f5f4b586e99a2e3b2ed4827 100644 (file)
@@ -46,6 +46,11 @@ typedef struct ossl_qtx_args_st {
 
     /* Maximum datagram payload length (MDPL) for TX purposes. */
     size_t          mdpl;
+
+    /* Message callback related arguments */
+    ossl_msg_cb msg_callback;
+    void *msg_callback_arg;
+    SSL *msg_callback_s;
 } OSSL_QTX_ARGS;
 
 /* Instantiates a new QTX. */
index 2cf8ebefc8ab2cd8806b6b4da28a4ac67f5359d8..2fa36d46e96035f09440d0ae72062e1e756dcd50 100644 (file)
@@ -95,6 +95,9 @@ static ossl_unused ossl_inline int ossl_quic_conn_id_eq(const QUIC_CONN_ID *a,
 
 #  define QUIC_STATELESS_RESET_TOKEN_LEN    16
 
+typedef void (*ossl_msg_cb)(int write_p, int version, int content_type,
+                            const void *buf, size_t len, SSL *ssl, void *arg);
+
 # endif
 
 #endif
index ce3c63057e43ea7d35ad5e0e68b8ca00f7c95a3c..5979a5ceb536b83e71d49577d6d9064a80958e60 100644 (file)
@@ -425,6 +425,9 @@ struct quic_pkt_hdr_ptrs_st {
  * If partial is 0, the input is assumed to have already had header protection
  * removed, and all header fields are decoded.
  *
+ * If nodata is 1, the input is assumed to have no payload data in it. Otherwise
+ * payload data must be present.
+ *
  * On success, the logical decode of the packet header is written to *hdr.
  * hdr->partial is set or cleared according to whether a partial decode was
  * performed. *ptrs is filled with pointers to various parts of the packet
@@ -441,6 +444,7 @@ struct quic_pkt_hdr_ptrs_st {
 int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
                                   size_t short_conn_id_len,
                                   int partial,
+                                  int nodata,
                                   QUIC_PKT_HDR *hdr,
                                   QUIC_PKT_HDR_PTRS *ptrs);
 
index cb24bd0c1ae7cb9f817015e63ff5778d054276a6..76546e2bd3bb58d9980eceb1fa99b19687ac999d 100644 (file)
@@ -130,6 +130,10 @@ static int ch_init(QUIC_CHANNEL *ch)
     /* We plug in a network write BIO to the QTX later when we get one. */
     qtx_args.libctx = ch->libctx;
     qtx_args.mdpl = QUIC_MIN_INITIAL_DGRAM_LEN;
+    /* Callback related arguments */
+    qtx_args.msg_callback       = ch->msg_callback;
+    qtx_args.msg_callback_arg   = ch->msg_callback_arg;
+    qtx_args.msg_callback_s     = ch->msg_callback_s;
     ch->rx_max_udp_payload_size = qtx_args.mdpl;
 
     ch->qtx = ossl_qtx_new(&qtx_args);
@@ -1602,7 +1606,7 @@ static void ch_default_packet_handler(QUIC_URXE *e, void *arg)
      * operation to fail if we get a 1-RTT packet. This is fine since we only
      * care about Initial packets.
      */
-    if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, &hdr, NULL))
+    if (!ossl_quic_wire_decode_pkt_hdr(&pkt, SIZE_MAX, 1, 0, &hdr, NULL))
         goto undesirable;
 
     switch (hdr.version) {
index 40c76a6bc1a0ab29b51a8af28cf292a490c2dd7e..266dee9d314ab7e7079b77b1e8176b3e52d98b9b 100644 (file)
@@ -726,7 +726,7 @@ static int qrx_process_pkt(OSSL_QRX *qrx, QUIC_URXE *urxe,
     need_second_decode = !pkt_is_marked(&urxe->hpr_removed, pkt_idx);
     if (!ossl_quic_wire_decode_pkt_hdr(pkt,
                                        qrx->short_conn_id_len,
-                                       need_second_decode, &rxe->hdr, &ptrs))
+                                       need_second_decode, 0, &rxe->hdr, &ptrs))
         goto malformed;
 
     /*
@@ -838,18 +838,19 @@ static int qrx_process_pkt(OSSL_QRX *qrx, QUIC_URXE *urxe,
 
         /* Decode the now unprotected header. */
         if (ossl_quic_wire_decode_pkt_hdr(pkt, qrx->short_conn_id_len,
-                                          0, &rxe->hdr, NULL) != 1)
+                                          0, 0, &rxe->hdr, NULL) != 1)
             goto malformed;
-
-        if (qrx->msg_callback != NULL)
-            qrx->msg_callback(0, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_PACKET, sop,
-                              eop - sop, qrx->msg_callback_s, qrx->msg_callback_arg);
     }
 
     /* Validate header and decode PN. */
     if (!qrx_validate_hdr(qrx, rxe))
         goto malformed;
 
+    if (qrx->msg_callback != NULL)
+        qrx->msg_callback(0, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_PACKET, sop,
+                          eop - sop - rxe->hdr.len, qrx->msg_callback_s,
+                          qrx->msg_callback_arg);
+
     /*
      * The AAD data is the entire (unprotected) packet header including the PN.
      * The packet header has been unprotected in place, so we can just reuse the
index 28ebc436bb2dccf01cd22eb87147322cc44a1c08..9040f2f904b625015208266b391687c606f74707 100644 (file)
@@ -94,6 +94,11 @@ struct ossl_qtx_st {
     ossl_mutate_packet_cb mutatecb;
     ossl_finish_mutate_cb finishmutatecb;
     void *mutatearg;
+
+    /* Message callback related arguments */
+    ossl_msg_cb msg_callback;
+    void *msg_callback_arg;
+    SSL *msg_callback_s;
 };
 
 /* Instantiates a new QTX. */
@@ -112,6 +117,9 @@ OSSL_QTX *ossl_qtx_new(const OSSL_QTX_ARGS *args)
     qtx->propq              = args->propq;
     qtx->bio                = args->bio;
     qtx->mdpl               = args->mdpl;
+    qtx->msg_callback       = args->msg_callback;
+    qtx->msg_callback_arg   = args->msg_callback_arg;
+    qtx->msg_callback_s     = args->msg_callback_s;
     return qtx;
 }
 
@@ -432,9 +440,9 @@ static int qtx_write_hdr(OSSL_QTX *qtx, const QUIC_PKT_HDR *hdr, TXE *txe,
 {
     WPACKET wpkt;
     size_t l = 0;
+    unsigned char *data = txe_data(txe) + txe->data_len;
 
-    if (!WPACKET_init_static_len(&wpkt, txe_data(txe) + txe->data_len,
-                                 txe->alloc_len - txe->data_len, 0))
+    if (!WPACKET_init_static_len(&wpkt, data, txe->alloc_len - txe->data_len, 0))
         return 0;
 
     if (!ossl_quic_wire_encode_pkt_hdr(&wpkt, hdr->dst_conn_id.id_len,
@@ -443,9 +451,14 @@ static int qtx_write_hdr(OSSL_QTX *qtx, const QUIC_PKT_HDR *hdr, TXE *txe,
         WPACKET_finish(&wpkt);
         return 0;
     }
+    WPACKET_finish(&wpkt);
+
+    if (qtx->msg_callback != NULL)
+        qtx->msg_callback(1, OSSL_QUIC1_VERSION, SSL3_RT_QUIC_PACKET, data, l,
+                          qtx->msg_callback_s, qtx->msg_callback_arg);
 
     txe->data_len += l;
-    WPACKET_finish(&wpkt);
+
     return 1;
 }
 
index 025e8189ef719af4361cc7374c2bb8cdaed9a5db..aa25e8f028823d98ed0c38eaf6fdbff3702da612 100644 (file)
@@ -495,7 +495,7 @@ int ossl_quic_trace(int write_p, int version, int content_type,
              * TODO(QUIC): We need to query the short connection id len here,
              *             e.g. via some API SSL_get_short_conn_id_len()
              */
-            if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, &hdr, NULL) != 1)
+            if (ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 1, &hdr, NULL) != 1)
                 return 0;
 
             BIO_puts(bio, write_p ? "Sent" : "Received");
index e0180ec0604f7db9602616dd79e16ad68fc017df..d63101095fc65bc456cd26fd5cfffc182c3e0408 100644 (file)
@@ -162,6 +162,7 @@ int ossl_quic_hdr_protector_encrypt_fields(QUIC_HDR_PROTECTOR *hpr,
 int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
                                   size_t short_conn_id_len,
                                   int partial,
+                                  int nodata,
                                   QUIC_PKT_HDR *hdr,
                                   QUIC_PKT_HDR_PTRS *ptrs)
 {
@@ -369,8 +370,10 @@ int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
                 hdr->pn_len = partial ? 0 : (b0 & 3) + 1;
 
                 if (!PACKET_get_quic_vlint(pkt, &len)
-                    || len < sizeof(hdr->pn)
-                    || len > PACKET_remaining(pkt))
+                        || len < sizeof(hdr->pn))
+                    return 0;
+
+                if (!nodata && len > PACKET_remaining(pkt))
                     return 0;
 
                 /*
@@ -393,11 +396,15 @@ int ossl_quic_wire_decode_pkt_hdr(PACKET *pkt,
                     hdr->len = (size_t)(len - hdr->pn_len);
                 }
 
-                hdr->data = PACKET_data(pkt);
+                if (nodata) {
+                    hdr->data = NULL;
+                } else {
+                    hdr->data = PACKET_data(pkt);
 
-                /* Skip over packet body. */
-                if (!PACKET_forward(pkt, hdr->len))
-                    return 0;
+                    /* Skip over packet body. */
+                    if (!PACKET_forward(pkt, hdr->len))
+                        return 0;
+                }
             }
         }
     }
index c973a8cc658d1232c03147aff9672002bf4b7c08..c33518805dd18046aada409cbf625423f414c800 100644 (file)
@@ -737,7 +737,7 @@ static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
             do {
                 if (!ossl_quic_wire_decode_pkt_hdr(&pkt,
                         0 /* TODO(QUIC): Not sure how this should be set*/, 1,
-                        &hdr, NULL))
+                        0, &hdr, NULL))
                     goto out;
 
                 /*
index 1dc0eec5e598eb364fe03e5553af321bf116f181..088e730af1fb9f53db465a25d334d59835476366 100644 (file)
@@ -2522,7 +2522,7 @@ static int test_wire_pkt_hdr_actual(int tidx, int repeat, int cipher,
         goto err;
 
     if (!TEST_int_eq(ossl_quic_wire_decode_pkt_hdr(&pkt, t->short_conn_id_len,
-                                                   0, &hdr, &ptrs),
+                                                   0, 0, &hdr, &ptrs),
                      !expect_fail))
         goto err;
 
index d6eef296a3423f3f743db6be6a48ea887eb32809..04e287fbf7591ade9866f98fbf20d8f4b6f3d8c3 100644 (file)
@@ -1513,7 +1513,7 @@ static int test_wire_retry_integrity_tag(void)
     if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded))))
         goto err;
 
-    if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, &hdr, NULL)))
+    if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL)))
         goto err;
 
     if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY))