]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add a helper function to prepend a frame to a packet
authorMatt Caswell <matt@openssl.org>
Tue, 17 Jan 2023 15:16:42 +0000 (15:16 +0000)
committerHugo Landau <hlandau@openssl.org>
Wed, 22 Feb 2023 05:34:05 +0000 (05:34 +0000)
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20030)

test/helpers/quictestlib.c
test/helpers/quictestlib.h
test/quicfaultstest.c

index ca3719c267553ed2b6780a71799cd2c876e0b529..7fdd9cab31095e679c9e71b063c11ed520947d1b 100644 (file)
@@ -347,6 +347,38 @@ int ossl_quic_fault_resize_plain_packet(OSSL_QUIC_FAULT *fault, size_t newlen)
     return 1;
 }
 
+/*
+ * Prepend frame data into a packet. To be called from a packet_plain_listener
+ * callback
+ */
+int ossl_quic_fault_prepend_frame(OSSL_QUIC_FAULT *fault, unsigned char *frame,
+                                  size_t frame_len)
+{
+    unsigned char *buf;
+    size_t old_len;
+
+    /*
+     * Alloc'd size should always be non-zero, so if this fails we've been
+     * incorrectly called
+     */
+    if (fault->pplainbuf_alloc == 0)
+        return 0;
+
+    /* Cast below is safe because we allocated the buffer */
+    buf = (unsigned char *)fault->pplainio.buf;
+    old_len = fault->pplainio.buf_len;
+
+    /* Extend the size of the packet by the size of the new frame */
+    if (!TEST_true(ossl_quic_fault_resize_plain_packet(fault,
+                                                       old_len + frame_len)))
+        return 0;
+
+    memmove(buf + frame_len, buf, old_len);
+    memcpy(buf, frame, frame_len);
+
+    return 1;
+}
+
 static int handshake_mutate(const unsigned char *msgin, size_t msginlen,
                             unsigned char **msgout, size_t *msgoutlen,
                             void *arg)
index 09638ee503e6391c65e599725e3708155cb2af20..165b3af79d1f2de310a155d90984f67b3c2558c7 100644 (file)
@@ -68,6 +68,7 @@ int ossl_quic_fault_set_packet_plain_listener(OSSL_QUIC_FAULT *fault,
                                               ossl_quic_fault_on_packet_plain_cb pplaincb,
                                               void *pplaincbarg);
 
+
 /*
  * Helper function to be called from a packet_plain_listener callback if it
  * wants to resize the packet (either to add new data to it, or to truncate it).
@@ -78,6 +79,13 @@ int ossl_quic_fault_set_packet_plain_listener(OSSL_QUIC_FAULT *fault,
  */
 int ossl_quic_fault_resize_plain_packet(OSSL_QUIC_FAULT *fault, size_t newlen);
 
+/*
+ * Prepend frame data into a packet. To be called from a packet_plain_listener
+ * callback
+ */
+int ossl_quic_fault_prepend_frame(OSSL_QUIC_FAULT *fault, unsigned char *frame,
+                                  size_t frame_len);
+
 /*
  * The general handshake message listener is sent the entire handshake message
  * data block, including the handshake header itself
index 15f9312fb1c9c271403a571fd0109b1136bc3a56..1bfc921256bbeb33fe6569745c0e11de39d729fb 100644 (file)
@@ -70,8 +70,7 @@ static int test_basic(void)
 static int add_unknown_frame_cb(OSSL_QUIC_FAULT *fault, QUIC_PKT_HDR *hdr,
                                 unsigned char *buf, size_t len, void *cbarg)
 {
-    size_t done = 0;
-
+    static size_t done = 0;
     /*
      * There are no "reserved" frame types which are definitately safe for us
      * to use for testing purposes - but we just use the highest possible
@@ -82,28 +81,11 @@ static int add_unknown_frame_cb(OSSL_QUIC_FAULT *fault, QUIC_PKT_HDR *hdr,
     };
 
     /* We only ever add the unknown frame to one packet */
-    if (done)
+    if (done++)
         return 1;
-    done++;
-
-    /* Extend the size of the packet by the size of the new frame */
-    if (!TEST_true(ossl_quic_fault_resize_plain_packet(fault,
-                                                       len + sizeof(unknown_frame))))
-        return 0;
 
-    /*
-     * We prepend the new frame to the start of the packet. We add it to the
-     * start rather than the end because stream frames that are already in the
-     * packet may not have an explicit length, and instead may just extend to
-     * the end of the packet. We could fix-up such frames to have an explicit
-     * length and add our new frame after it. But it is probably simpler just to
-     * add it to the beginning of the packet. This means moving the existing
-     * packet data.
-     */
-    memmove(buf + sizeof(unknown_frame), buf, len);
-    memcpy(buf, unknown_frame, sizeof(unknown_frame));
-
-    return 1;
+    return ossl_quic_fault_prepend_frame(fault, unknown_frame,
+                                         sizeof(unknown_frame));
 }
 
 static int test_unknown_frame(void)