From: Matt Caswell Date: Tue, 17 Jan 2023 15:16:42 +0000 (+0000) Subject: Add a helper function to prepend a frame to a packet X-Git-Tag: openssl-3.2.0-alpha1~1246 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7eaaaaaa559d56edc9732d768dc374a4f829b187;p=thirdparty%2Fopenssl.git Add a helper function to prepend a frame to a packet Reviewed-by: Hugo Landau Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/20030) --- diff --git a/test/helpers/quictestlib.c b/test/helpers/quictestlib.c index ca3719c267..7fdd9cab31 100644 --- a/test/helpers/quictestlib.c +++ b/test/helpers/quictestlib.c @@ -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) diff --git a/test/helpers/quictestlib.h b/test/helpers/quictestlib.h index 09638ee503..165b3af79d 100644 --- a/test/helpers/quictestlib.h +++ b/test/helpers/quictestlib.h @@ -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 diff --git a/test/quicfaultstest.c b/test/quicfaultstest.c index 15f9312fb1..1bfc921256 100644 --- a/test/quicfaultstest.c +++ b/test/quicfaultstest.c @@ -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)