]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Use reported short conn id len in qtestlib
authorNeil Horman <nhorman@openssl.org>
Wed, 29 Jan 2025 18:18:57 +0000 (13:18 -0500)
committerNeil Horman <nhorman@openssl.org>
Tue, 4 Feb 2025 18:33:21 +0000 (13:33 -0500)
Use the new short conn id internal api to record and use the connections
short conn id len when decoding packets in qtestlib

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26592)

test/helpers/quictestlib.c
test/helpers/quictestlib.h
test/quic_multistream_test.c

index 7e2882a75f7f795df76a8a0471997df2b85b9f19..88d8cfdc15bf3b94eb6ad48606b399292176df24 100644 (file)
@@ -134,6 +134,11 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
     BIO_ADDR *peeraddr = NULL;
     struct in_addr ina = {0};
     BIO *tmpbio = NULL;
+    QTEST_DATA *bdata = NULL;
+
+    bdata = OPENSSL_zalloc(sizeof(QTEST_DATA));
+    if (bdata == NULL)
+        return 0;
 
     *qtserv = NULL;
     if (*cssl == NULL) {
@@ -146,6 +151,7 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
         *fault = OPENSSL_zalloc(sizeof(**fault));
         if (*fault == NULL)
             goto err;
+        bdata->fault = *fault;
     }
 
 #ifndef OPENSSL_NO_SSL_TRACE
@@ -226,11 +232,13 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
         if (!TEST_ptr(pktsplitbio))
             goto err;
         cbio = BIO_push(pktsplitbio, cbio);
+        BIO_set_data(pktsplitbio, bdata);
 
         pktsplitbio = BIO_new(bio_f_pkt_split_dgram_filter());
         if (!TEST_ptr(pktsplitbio))
             goto err;
         sbio = BIO_push(pktsplitbio, sbio);
+        BIO_set_data(pktsplitbio, bdata);
     }
 
     if ((flags & QTEST_FLAG_NOISE) != 0) {
@@ -289,7 +297,7 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
     if (!TEST_ptr(fisbio))
         goto err;
 
-    BIO_set_data(fisbio, fault == NULL ? NULL : *fault);
+    BIO_set_data(fisbio, bdata);
 
     if (!BIO_up_ref(sbio))
         goto err;
@@ -323,6 +331,7 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
                                                   keyfile)))
         goto err;
 
+    bdata->short_conn_id_len = ossl_quic_tserver_get_short_header_conn_id_len(*qtserv);
     /* Ownership of fisbio and sbio is now held by *qtserv */
     sbio = NULL;
     fisbio = NULL;
@@ -348,6 +357,7 @@ int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
     ossl_quic_tserver_free(*qtserv);
     if (fault != NULL)
         OPENSSL_free(*fault);
+    OPENSSL_free(bdata);
     BIO_free(tmpbio);
     if (tracebio != NULL)
         *tracebio = NULL;
@@ -1080,20 +1090,20 @@ static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
                             size_t num_msg, uint64_t flags,
                             size_t *num_processed)
 {
-    QTEST_FAULT *fault;
     BIO *next = BIO_next(b);
     ossl_ssize_t ret = 0;
     size_t i = 0, tmpnump;
     QUIC_PKT_HDR hdr;
     PACKET pkt;
     unsigned char *tmpdata;
+    QTEST_DATA *bdata = NULL;
 
     if (next == NULL)
         return 0;
 
-    fault = BIO_get_data(b);
-    if (fault == NULL
-            || (fault->pciphercb == NULL && fault->datagramcb == NULL))
+    bdata = BIO_get_data(b);
+    if (bdata == NULL || bdata->fault == NULL
+            || (bdata->fault->pciphercb == NULL && bdata->fault->datagramcb == NULL))
         return BIO_sendmmsg(next, msg, stride, num_msg, flags, num_processed);
 
     if (num_msg == 0) {
@@ -1102,38 +1112,33 @@ static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
     }
 
     for (i = 0; i < num_msg; ++i) {
-        fault->msg = BIO_MSG_N(msg, stride, i);
+        bdata->fault->msg = BIO_MSG_N(msg, stride, i);
 
         /* Take a copy of the data so that callbacks can modify it */
-        tmpdata = OPENSSL_malloc(fault->msg.data_len + GROWTH_ALLOWANCE);
+        tmpdata = OPENSSL_malloc(bdata->fault->msg.data_len + GROWTH_ALLOWANCE);
         if (tmpdata == NULL)
             return 0;
-        memcpy(tmpdata, fault->msg.data, fault->msg.data_len);
-        fault->msg.data = tmpdata;
-        fault->msgalloc = fault->msg.data_len + GROWTH_ALLOWANCE;
+        memcpy(tmpdata, bdata->fault->msg.data, bdata->fault->msg.data_len);
+        bdata->fault->msg.data = tmpdata;
+        bdata->fault->msgalloc = bdata->fault->msg.data_len + GROWTH_ALLOWANCE;
 
-        if (fault->pciphercb != NULL) {
-            if (!PACKET_buf_init(&pkt, fault->msg.data, fault->msg.data_len))
+        if (bdata->fault->pciphercb != NULL) {
+            if (!PACKET_buf_init(&pkt, bdata->fault->msg.data, bdata->fault->msg.data_len))
                 return 0;
 
             do {
                 if (!ossl_quic_wire_decode_pkt_hdr(&pkt,
-                        /*
-                         * TODO(QUIC SERVER):
-                         * Needs to be set to the actual short header CID length
-                         * when testing the server implementation.
-                         */
-                        0,
-                        1,
-                        0, &hdr, NULL, NULL))
+                                                   bdata->short_conn_id_len,
+                                                   1, 0, &hdr, NULL, NULL))
                     goto out;
 
                 /*
                  * hdr.data is const - but its our buffer so casting away the
                  * const is safe
                  */
-                if (!fault->pciphercb(fault, &hdr, (unsigned char *)hdr.data,
-                                    hdr.len, fault->pciphercbarg))
+                if (!bdata->fault->pciphercb(bdata->fault, &hdr,
+                                             (unsigned char *)hdr.data, hdr.len,
+                                             bdata->fault->pciphercbarg))
                     goto out;
 
                 /*
@@ -1146,26 +1151,26 @@ static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
             } while (PACKET_remaining(&pkt) > 0);
         }
 
-        if (fault->datagramcb != NULL
-                && !fault->datagramcb(fault, &fault->msg, stride,
-                                      fault->datagramcbarg))
+        if (bdata->fault->datagramcb != NULL
+                && !bdata->fault->datagramcb(bdata->fault, &bdata->fault->msg, stride,
+                                             bdata->fault->datagramcbarg))
             goto out;
 
-        if (!BIO_sendmmsg(next, &fault->msg, stride, 1, flags, &tmpnump)) {
+        if (!BIO_sendmmsg(next, &bdata->fault->msg, stride, 1, flags, &tmpnump)) {
             *num_processed = i;
             goto out;
         }
 
-        OPENSSL_free(fault->msg.data);
-        fault->msg.data = NULL;
-        fault->msgalloc = 0;
+        OPENSSL_free(bdata->fault->msg.data);
+        bdata->fault->msg.data = NULL;
+        bdata->fault->msgalloc = 0;
     }
 
     *num_processed = i;
 out:
     ret = i > 0;
-    OPENSSL_free(fault->msg.data);
-    fault->msg.data = NULL;
+    OPENSSL_free(bdata->fault->msg.data);
+    bdata->fault->msg.data = NULL;
     return ret;
 }
 
@@ -1179,6 +1184,12 @@ static long pcipher_ctrl(BIO *b, int cmd, long larg, void *parg)
     return BIO_ctrl(next, cmd, larg, parg);
 }
 
+static int pcipher_destroy(BIO *b)
+{
+    OPENSSL_free(BIO_get_data(b));
+    return 1;
+}
+
 BIO_METHOD *qtest_get_bio_method(void)
 {
     BIO_METHOD *tmp;
@@ -1192,7 +1203,8 @@ BIO_METHOD *qtest_get_bio_method(void)
         return NULL;
 
     if (!TEST_true(BIO_meth_set_sendmmsg(tmp, pcipher_sendmmsg))
-            || !TEST_true(BIO_meth_set_ctrl(tmp, pcipher_ctrl)))
+            || !TEST_true(BIO_meth_set_ctrl(tmp, pcipher_ctrl))
+            || !TEST_true(BIO_meth_set_destroy(tmp, pcipher_destroy)))
         goto err;
 
     pcipherbiometh = tmp;
index 6bb745ba36f63c20f38e6a0087977845532f361e..11c31e336fada6b93cffa0a52ec30a1aceed8da9 100644 (file)
 /* Type to represent the Fault Injector */
 typedef struct qtest_fault QTEST_FAULT;
 
+typedef struct bio_qtest_data {
+    size_t short_conn_id_len;
+    struct qtest_fault *fault;
+} QTEST_DATA;
+
 /*
  * Structure representing a parsed EncryptedExtension message. Listeners can
  * make changes to the contents of structure objects as required and the fault
index 3c9a50820208bd0a265d4b17dd80cd44ffd58760..e241b5cca860d729ea58956a7dcf0c52d675f9e6 100644 (file)
@@ -681,6 +681,7 @@ static int helper_init(struct helper *h, const char *script_name,
     QUIC_TSERVER_ARGS s_args = {0};
     union BIO_sock_info_u info;
     char title[128];
+    QTEST_DATA *bdata = NULL;
 
     memset(h, 0, sizeof(*h));
     h->c_fd = -1;
@@ -690,6 +691,10 @@ static int helper_init(struct helper *h, const char *script_name,
     h->need_injector = need_injector;
     h->time_slip = ossl_time_zero();
 
+    bdata = OPENSSL_zalloc(sizeof(QTEST_DATA));
+    if (bdata == NULL)
+        goto err;
+
     if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new()))
         goto err;
 
@@ -763,8 +768,8 @@ static int helper_init(struct helper *h, const char *script_name,
         h->qtf = qtest_create_injector(h->s_priv);
         if (!TEST_ptr(h->qtf))
             goto err;
-
-        BIO_set_data(h->s_qtf_wbio, h->qtf);
+        bdata->fault = h->qtf;
+        BIO_set_data(h->s_qtf_wbio, bdata);
     }
 
     h->s_net_bio_own = NULL;