| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-09-07 16:59:15 +08:00
										 |  |  |  * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							|  |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include <openssl/ssl.h>
 | 
					
						
							|  |  |  | #include <openssl/quic.h>
 | 
					
						
							|  |  |  | #include <openssl/bio.h>
 | 
					
						
							|  |  |  | #include "internal/common.h"
 | 
					
						
							|  |  |  | #include "internal/sockets.h"
 | 
					
						
							|  |  |  | #include "internal/quic_tserver.h"
 | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  | #include "internal/quic_thread_assist.h"
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | #include "internal/quic_ssl.h"
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | #include "internal/time.h"
 | 
					
						
							|  |  |  | #include "testutil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char msg1[] = "The quick brown fox jumped over the lazy dogs."; | 
					
						
							|  |  |  | static char msg2[1024], msg3[1024]; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | static OSSL_TIME fake_time; | 
					
						
							| 
									
										
										
										
											2023-02-23 01:01:28 +08:00
										 |  |  | static CRYPTO_RWLOCK *fake_time_lock; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  | static const char *certfile, *keyfile; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | static int is_want(SSL *s, int ret) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ec = SSL_get_error(s, ret); | 
					
						
							| 
									
										
										
										
											2023-01-18 19:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  | static unsigned char scratch_buf[2048]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | static OSSL_TIME fake_now(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-23 01:01:28 +08:00
										 |  |  |     OSSL_TIME t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!CRYPTO_THREAD_read_lock(fake_time_lock)) | 
					
						
							|  |  |  |         return ossl_time_zero(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     t = fake_time; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CRYPTO_THREAD_unlock(fake_time_lock); | 
					
						
							|  |  |  |     return t; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static OSSL_TIME real_now(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ossl_time_now(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int do_test(int use_thread_assist, int use_fake_time, int use_inject) | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int testresult = 0, ret; | 
					
						
							|  |  |  |     int s_fd = -1, c_fd = -1; | 
					
						
							|  |  |  |     BIO *s_net_bio = NULL, *s_net_bio_own = NULL; | 
					
						
							|  |  |  |     BIO *c_net_bio = NULL, *c_net_bio_own = NULL; | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |     BIO *c_pair_own = NULL, *s_pair_own = NULL; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     QUIC_TSERVER_ARGS tserver_args = {0}; | 
					
						
							|  |  |  |     QUIC_TSERVER *tserver = NULL; | 
					
						
							|  |  |  |     BIO_ADDR *s_addr_ = NULL; | 
					
						
							|  |  |  |     struct in_addr ina = {0}; | 
					
						
							|  |  |  |     union BIO_sock_info_u s_info = {0}; | 
					
						
							|  |  |  |     SSL_CTX *c_ctx = NULL; | 
					
						
							|  |  |  |     SSL *c_ssl = NULL; | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |     int c_connected = 0, c_write_done = 0, c_begin_read = 0, s_read_done = 0; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     int c_wait_eos = 0, c_done_eos = 0; | 
					
						
							|  |  |  |     int c_start_idle_test = 0, c_done_idle_test = 0; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     size_t l = 0, s_total_read = 0, s_total_written = 0, c_total_read = 0; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     size_t idle_units_done = 0; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     int s_begin_write = 0; | 
					
						
							|  |  |  |     OSSL_TIME start_time; | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  |     unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' }; | 
					
						
							| 
									
										
										
										
											2023-10-23 22:16:50 +08:00
										 |  |  |     size_t limit_ms = 10000; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-17 20:15:01 +08:00
										 |  |  | #if defined(OPENSSL_NO_QUIC_THREAD_ASSIST)
 | 
					
						
							|  |  |  |     if (use_thread_assist) { | 
					
						
							|  |  |  |         TEST_skip("thread assisted mode not enabled"); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     ina.s_addr = htonl(0x7f000001UL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Setup test server. */ | 
					
						
							|  |  |  |     s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); | 
					
						
							|  |  |  |     if (!TEST_int_ge(s_fd, 0)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(BIO_socket_nbio(s_fd, 1))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(s_addr_ = BIO_ADDR_new())) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:21:07 +08:00
										 |  |  |     if (!TEST_true(BIO_ADDR_rawmake(s_addr_, AF_INET, &ina, sizeof(ina), 0))) | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(BIO_bind(s_fd, s_addr_, 0))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s_info.addr = s_addr_; | 
					
						
							|  |  |  |     if (!TEST_true(BIO_sock_info(s_fd, BIO_SOCK_INFO_ADDRESS, &s_info))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_int_gt(BIO_ADDR_rawport(s_addr_), 0)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(s_net_bio = s_net_bio_own = BIO_new_dgram(s_fd, 0))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!BIO_up_ref(s_net_bio)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     fake_time = ossl_ms2time(1000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     tserver_args.net_rbio = s_net_bio; | 
					
						
							|  |  |  |     tserver_args.net_wbio = s_net_bio; | 
					
						
							| 
									
										
										
										
											2023-06-08 19:18:38 +08:00
										 |  |  |     tserver_args.alpn = NULL; | 
					
						
							| 
									
										
										
										
											2023-07-28 16:22:38 +08:00
										 |  |  |     tserver_args.ctx = NULL; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     if (use_fake_time) | 
					
						
							|  |  |  |         tserver_args.now_cb = fake_now; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  |     if (!TEST_ptr(tserver = ossl_quic_tserver_new(&tserver_args, certfile, | 
					
						
							|  |  |  |                                                   keyfile))) { | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         BIO_free(s_net_bio); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s_net_bio_own = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     if (use_inject) { | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * In inject mode we create a dgram pair to feed to the QUIC client on | 
					
						
							|  |  |  |          * the read side. We don't feed anything to this, it is just a | 
					
						
							|  |  |  |          * placeholder to give the client something which never returns any | 
					
						
							| 
									
										
										
										
											2023-03-21 00:27:08 +08:00
										 |  |  |          * datagrams. | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |          */ | 
					
						
							|  |  |  |         if (!TEST_true(BIO_new_bio_dgram_pair(&c_pair_own, 5000, | 
					
						
							|  |  |  |                                               &s_pair_own, 5000))) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     /* Setup test client. */ | 
					
						
							|  |  |  |     c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); | 
					
						
							|  |  |  |     if (!TEST_int_ge(c_fd, 0)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(BIO_socket_nbio(c_fd, 1))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(c_net_bio = c_net_bio_own = BIO_new_dgram(c_fd, 0))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!BIO_dgram_set_peer(c_net_bio, s_addr_)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     if (!TEST_ptr(c_ctx = SSL_CTX_new(use_thread_assist | 
					
						
							|  |  |  |                                       ? OSSL_QUIC_client_thread_method() | 
					
						
							|  |  |  |                                       : OSSL_QUIC_client_method()))) | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(c_ssl = SSL_new(c_ctx))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     if (use_fake_time) | 
					
						
							| 
									
										
										
										
											2023-05-23 19:23:06 +08:00
										 |  |  |         if (!TEST_true(ossl_quic_conn_set_override_now_cb(c_ssl, fake_now, NULL))) | 
					
						
							|  |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  |     /* 0 is a success for SSL_set_alpn_protos() */ | 
					
						
							|  |  |  |     if (!TEST_false(SSL_set_alpn_protos(c_ssl, alpn, sizeof(alpn)))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     /* Takes ownership of our reference to the BIO. */ | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     if (use_inject) { | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |         SSL_set0_rbio(c_ssl, c_pair_own); | 
					
						
							|  |  |  |         c_pair_own = NULL; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         SSL_set0_rbio(c_ssl, c_net_bio); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Get another reference to be transferred in the SSL_set0_wbio call. */ | 
					
						
							|  |  |  |         if (!TEST_true(BIO_up_ref(c_net_bio))) { | 
					
						
							|  |  |  |             c_net_bio_own = NULL; /* SSL_free will free the first reference. */ | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     SSL_set0_wbio(c_ssl, c_net_bio); | 
					
						
							|  |  |  |     c_net_bio_own = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_true(SSL_set_blocking_mode(c_ssl, 0))) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-05 00:55:33 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * We use real time for the timeout not fake time. Otherwise with fake time | 
					
						
							|  |  |  |      * we could hit a hang if we never increment the fake time | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     start_time = real_now(NULL); | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2023-10-05 00:55:33 +08:00
										 |  |  |         if (ossl_time_compare(ossl_time_subtract(real_now(NULL), start_time), | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |                               ossl_ms2time(limit_ms)) >= 0) { | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |             TEST_error("timeout while attempting QUIC server test"); | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |         if (!c_start_idle_test) { | 
					
						
							|  |  |  |             ret = SSL_connect(c_ssl); | 
					
						
							|  |  |  |             if (!TEST_true(ret == 1 || is_want(c_ssl, ret))) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |             if (ret == 1) | 
					
						
							|  |  |  |                 c_connected = 1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (c_connected && !c_write_done) { | 
					
						
							|  |  |  |             if (!TEST_int_eq(SSL_write(c_ssl, msg1, sizeof(msg1) - 1), | 
					
						
							|  |  |  |                              (int)sizeof(msg1) - 1)) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |             if (!TEST_true(SSL_stream_conclude(c_ssl, 0))) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |             c_write_done = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |         if (c_connected && c_write_done && !s_read_done) { | 
					
						
							| 
									
										
										
										
											2023-04-19 02:30:54 +08:00
										 |  |  |             if (!ossl_quic_tserver_read(tserver, 0, | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |                                         (unsigned char *)msg2 + s_total_read, | 
					
						
							|  |  |  |                                         sizeof(msg2) - s_total_read, &l)) { | 
					
						
							| 
									
										
										
										
											2023-04-19 02:30:54 +08:00
										 |  |  |                 if (!TEST_true(ossl_quic_tserver_has_read_ended(tserver, 0))) | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |                     goto err; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |                 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1, msg2, s_total_read)) | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |                     goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 s_begin_write = 1; | 
					
						
							| 
									
										
										
										
											2023-04-19 02:30:54 +08:00
										 |  |  |                 s_read_done   = 1; | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 s_total_read += l; | 
					
						
							|  |  |  |                 if (!TEST_size_t_le(s_total_read, sizeof(msg1) - 1)) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (s_begin_write && s_total_written < sizeof(msg1) - 1) { | 
					
						
							| 
									
										
										
										
											2023-04-19 02:30:54 +08:00
										 |  |  |             if (!TEST_true(ossl_quic_tserver_write(tserver, 0, | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |                                                    (unsigned char *)msg2 + s_total_written, | 
					
						
							|  |  |  |                                                    sizeof(msg1) - 1 - s_total_written, &l))) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             s_total_written += l; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |             if (s_total_written == sizeof(msg1) - 1) { | 
					
						
							| 
									
										
										
										
											2023-04-19 02:30:54 +08:00
										 |  |  |                 ossl_quic_tserver_conclude(tserver, 0); | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |                 c_begin_read = 1; | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (c_begin_read && c_total_read < sizeof(msg1) - 1) { | 
					
						
							|  |  |  |             ret = SSL_read_ex(c_ssl, msg3 + c_total_read, | 
					
						
							|  |  |  |                               sizeof(msg1) - 1 - c_total_read, &l); | 
					
						
							|  |  |  |             if (!TEST_true(ret == 1 || is_want(c_ssl, ret))) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             c_total_read += l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (c_total_read == sizeof(msg1) - 1) { | 
					
						
							|  |  |  |                 if (!TEST_mem_eq(msg1, sizeof(msg1) - 1, | 
					
						
							|  |  |  |                                  msg3, c_total_read)) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |                 c_wait_eos = 1; | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |         if (c_wait_eos && !c_done_eos) { | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |             unsigned char c; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ret = SSL_read_ex(c_ssl, &c, sizeof(c), &l); | 
					
						
							|  |  |  |             if (!TEST_false(ret)) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-05 18:51:14 +08:00
										 |  |  |             /*
 | 
					
						
							|  |  |  |              * Allow the implementation to take as long as it wants to finally | 
					
						
							|  |  |  |              * notice EOS. Account for varied timings in OS networking stacks. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             if (SSL_get_error(c_ssl, ret) != SSL_ERROR_WANT_READ) { | 
					
						
							|  |  |  |                 if (!TEST_int_eq(SSL_get_error(c_ssl, ret), | 
					
						
							|  |  |  |                                  SSL_ERROR_ZERO_RETURN)) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |                 c_done_eos = 1; | 
					
						
							|  |  |  |                 if (use_thread_assist && use_fake_time) { | 
					
						
							|  |  |  |                     if (!TEST_true(ossl_quic_tserver_is_connected(tserver))) | 
					
						
							|  |  |  |                         goto err; | 
					
						
							|  |  |  |                     c_start_idle_test = 1; | 
					
						
							|  |  |  |                     limit_ms = 120000; /* extend time limit */ | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     /* DONE */ | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (c_start_idle_test && !c_done_idle_test) { | 
					
						
							|  |  |  |             /* This is more than our default idle timeout of 30s. */ | 
					
						
							|  |  |  |             if (idle_units_done < 600) { | 
					
						
							| 
									
										
										
										
											2023-10-05 00:50:53 +08:00
										 |  |  |                 struct timeval tv; | 
					
						
							|  |  |  |                 int isinf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 01:01:28 +08:00
										 |  |  |                 if (!TEST_true(CRYPTO_THREAD_write_lock(fake_time_lock))) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |                 fake_time = ossl_time_add(fake_time, ossl_ms2time(100)); | 
					
						
							| 
									
										
										
										
											2023-02-23 01:01:28 +08:00
										 |  |  |                 CRYPTO_THREAD_unlock(fake_time_lock); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |                 ++idle_units_done; | 
					
						
							|  |  |  |                 ossl_quic_conn_force_assist_thread_wake(c_ssl); | 
					
						
							| 
									
										
										
										
											2023-10-05 00:50:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 /*
 | 
					
						
							|  |  |  |                  * If the event timeout has expired then give the assistance | 
					
						
							|  |  |  |                  * thread a chance to catch up | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 if (!TEST_true(SSL_get_event_timeout(c_ssl, &tv, &isinf))) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							|  |  |  |                 if (!isinf && ossl_time_compare(ossl_time_zero(), | 
					
						
							|  |  |  |                                                 ossl_time_from_timeval(tv)) >= 0) | 
					
						
							|  |  |  |                     OSSL_sleep(100); /* Ensure CPU scheduling for test purposes */ | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 c_done_idle_test = 1; | 
					
						
							| 
									
										
										
										
											2023-01-05 18:51:14 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-12-13 20:29:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |         if (c_done_idle_test) { | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * If we have finished the fake idling duration, the connection | 
					
						
							|  |  |  |              * should still be healthy in TA mode. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             if (!TEST_true(ossl_quic_tserver_is_connected(tserver))) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* DONE */ | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * This is inefficient because we spin until things work without | 
					
						
							|  |  |  |          * blocking but this is just a test. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2023-02-23 03:15:16 +08:00
										 |  |  |         if (!c_start_idle_test || c_done_idle_test) { | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |             /* Inhibit manual ticking during idle test to test TA mode. */ | 
					
						
							| 
									
										
										
										
											2023-05-04 02:01:12 +08:00
										 |  |  |             SSL_handle_events(c_ssl); | 
					
						
							| 
									
										
										
										
											2023-02-23 03:15:16 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |         ossl_quic_tserver_tick(tserver); | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |         if (use_inject) { | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |             BIO_MSG rmsg = {0}; | 
					
						
							|  |  |  |             size_t msgs_processed = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (;;) { | 
					
						
							|  |  |  |                 /*
 | 
					
						
							|  |  |  |                  * Manually spoonfeed received datagrams from the real BIO_dgram | 
					
						
							|  |  |  |                  * into QUIC via the injection interface, thereby testing the | 
					
						
							|  |  |  |                  * injection interface. | 
					
						
							|  |  |  |                  */ | 
					
						
							|  |  |  |                 rmsg.data       = scratch_buf; | 
					
						
							|  |  |  |                 rmsg.data_len   = sizeof(scratch_buf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!BIO_recvmmsg(c_net_bio, &rmsg, sizeof(rmsg), 1, 0, &msgs_processed) | 
					
						
							|  |  |  |                     || msgs_processed == 0 || rmsg.data_len == 0) | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!TEST_true(SSL_inject_net_dgram(c_ssl, rmsg.data, rmsg.data_len, | 
					
						
							|  |  |  |                                                     NULL, NULL))) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     testresult = 1; | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     SSL_free(c_ssl); | 
					
						
							|  |  |  |     SSL_CTX_free(c_ctx); | 
					
						
							|  |  |  |     ossl_quic_tserver_free(tserver); | 
					
						
							|  |  |  |     BIO_ADDR_free(s_addr_); | 
					
						
							|  |  |  |     BIO_free(s_net_bio_own); | 
					
						
							|  |  |  |     BIO_free(c_net_bio_own); | 
					
						
							| 
									
										
										
										
											2023-03-08 03:07:50 +08:00
										 |  |  |     BIO_free(c_pair_own); | 
					
						
							|  |  |  |     BIO_free(s_pair_own); | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     if (s_fd >= 0) | 
					
						
							|  |  |  |         BIO_closesocket(s_fd); | 
					
						
							|  |  |  |     if (c_fd >= 0) | 
					
						
							|  |  |  |         BIO_closesocket(c_fd); | 
					
						
							|  |  |  |     return testresult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | static int test_tserver(int idx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     int thread_assisted, use_fake_time, use_inject; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     thread_assisted = idx % 2; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  |     idx /= 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     use_inject = idx % 2; | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     idx /= 2; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     use_fake_time = idx % 2; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     if (use_fake_time && !thread_assisted) | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     return do_test(thread_assisted, use_fake_time, use_inject); | 
					
						
							| 
									
										
										
										
											2023-02-21 18:18:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  | OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-29 19:26:08 +08:00
										 |  |  |     if (!test_skip_common_options()) { | 
					
						
							|  |  |  |         TEST_error("Error parsing test options\n"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(certfile = test_get_argument(0)) | 
					
						
							|  |  |  |             || !TEST_ptr(keyfile = test_get_argument(1))) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-23 01:01:28 +08:00
										 |  |  |     if ((fake_time_lock = CRYPTO_THREAD_lock_new()) == NULL) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 23:19:34 +08:00
										 |  |  |     ADD_ALL_TESTS(test_tserver, 2 * 2 * 2); | 
					
						
							| 
									
										
										
										
											2022-11-23 01:49:36 +08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } |