From 8189fe242bba319dfccd8805fd7703d973bf9649 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Tue, 28 Mar 2023 16:25:22 +0100 Subject: [PATCH] Add a test for an app data record appearing before epoch change in DTLS We had a test for a handshake record appearing before epoch change, and a test for an app data record appearing before Finished - but not one for the app data record appearing before epoch change. Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/20628) --- test/dtlstest.c | 136 +++++++++++++------------------------- test/helpers/ssltestlib.c | 37 ++++++----- test/helpers/ssltestlib.h | 2 +- 3 files changed, 67 insertions(+), 108 deletions(-) diff --git a/test/dtlstest.c b/test/dtlstest.c index b6775b5c502..2378b26f40d 100644 --- a/test/dtlstest.c +++ b/test/dtlstest.c @@ -469,84 +469,13 @@ static int test_just_finished(void) } /* - * Test that swapping a record from the next epoch into the current epoch still - * works. Libssl should buffer the record until it needs it. + * Test that swapping later records before Finished or CCS still works + * Test 0: Test receiving a handshake record early from next epoch on server side + * Test 1: Test receiving a handshake record early from next epoch on client side + * Test 2: Test receiving an app data record early from next epoch on client side + * Test 3: Test receiving an app data before Finished on client side */ -static int test_swap_epoch(void) -{ - SSL_CTX *sctx = NULL, *cctx = NULL; - SSL *sssl = NULL, *cssl = NULL; - int testresult = 0; - BIO *bio; - - if (!TEST_true(create_ssl_ctx_pair(NULL, DTLS_server_method(), - DTLS_client_method(), - DTLS1_VERSION, 0, - &sctx, &cctx, cert, privkey))) - return 0; - -#ifndef OPENSSL_NO_DTLS1_2 - if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA"))) - goto end; -#else - /* Default sigalgs are SHA1 based in data, outl); - mempacket_free(thispkt); return outl; } /* - * Look for records from different epochs and swap them around + * Look for records from different epochs in the last datagram and swap them + * around */ int mempacket_swap_epoch(BIO *bio) { @@ -487,36 +487,39 @@ int mempacket_swap_epoch(BIO *bio) return 0; } -/* Take the last and penultimate packets and swap them around */ -int mempacket_swap_recent(BIO *bio) +/* Move packet from position s to position d in the list (d < s) */ +int mempacket_move_packet(BIO *bio, int d, int s) { MEMPACKET_TEST_CTX *ctx = BIO_get_data(bio); MEMPACKET *thispkt; int numpkts = sk_MEMPACKET_num(ctx->pkts); + int i; - /* We need at least 2 packets to be able to swap them */ - if (numpkts <= 1) + if (d >= s) return 0; - /* Get the penultimate packet */ - thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 2); - if (thispkt == NULL) + /* We need at least s + 1 packets to be able to swap them */ + if (numpkts <= s) return 0; - if (sk_MEMPACKET_delete(ctx->pkts, numpkts - 2) != thispkt) + /* Get the packet at position s */ + thispkt = sk_MEMPACKET_value(ctx->pkts, s); + if (thispkt == NULL) return 0; - /* Re-add it to the end of the list */ - thispkt->num++; - if (sk_MEMPACKET_insert(ctx->pkts, thispkt, numpkts - 1) <= 0) + /* Remove and re-add it */ + if (sk_MEMPACKET_delete(ctx->pkts, s) != thispkt) return 0; - /* We also have to adjust the packet number of the other packet */ - thispkt = sk_MEMPACKET_value(ctx->pkts, numpkts - 2); - if (thispkt == NULL) + thispkt->num -= (s - d); + if (sk_MEMPACKET_insert(ctx->pkts, thispkt, d) <= 0) return 0; - thispkt->num--; + /* Increment the packet numbers for moved packets */ + for (i = d + 1; i <= s; i++) { + thispkt = sk_MEMPACKET_value(ctx->pkts, i); + thispkt->num++; + } return 1; } diff --git a/test/helpers/ssltestlib.h b/test/helpers/ssltestlib.h index bc06813e725..38cf161c868 100644 --- a/test/helpers/ssltestlib.h +++ b/test/helpers/ssltestlib.h @@ -51,7 +51,7 @@ void bio_s_always_retry_free(void); #define MEMPACKET_CTRL_SET_DUPLICATE_REC (4 << 15) int mempacket_swap_epoch(BIO *bio); -int mempacket_swap_recent(BIO *bio); +int mempacket_move_packet(BIO *bio, int d, int s); int mempacket_test_inject(BIO *bio, const char *in, int inl, int pktnum, int type); -- 2.47.2