1 /* 2 * Copyright 2023-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 /* 11 * We need access to the deprecated low level HMAC APIs for legacy purposes 12 * when the deprecated calls are not hidden 13 */ 14 #ifndef OPENSSL_NO_DEPRECATED_3_0 15 # define OPENSSL_SUPPRESS_DEPRECATED 16 #endif 17 18 #include <stdio.h> 19 #include <string.h> 20 21 #include <openssl/opensslconf.h> 22 #include <openssl/bio.h> 23 #include <openssl/crypto.h> 24 #include <openssl/ssl.h> 25 #include <openssl/engine.h> 26 27 #include "helpers/ssltestlib.h" 28 #include "testutil.h" 29 #include "testutil/output.h" 30 #include "internal/ktls.h" 31 #include "../ssl/ssl_local.h" 32 #include "../ssl/statem/statem_local.h" 33 #include "internal/ssl_unwrap.h" 34 35 static OSSL_LIB_CTX *libctx = NULL; 36 static char *cert = NULL; 37 static char *privkey = NULL; 38 39 /* 40 * Test 0: Clientside handshake RTT (TLSv1.2) 41 * Test 1: Serverside handshake RTT (TLSv1.2) 42 * Test 2: Clientside handshake RTT (TLSv1.3) 43 * Test 3: Serverside handshake RTT (TLSv1.3) 44 * Test 4: Clientside handshake RTT with Early Data (TLSv1.3) 45 */ 46 static int test_handshake_rtt(int tst) 47 { 48 SSL_CTX *cctx = NULL, *sctx = NULL; 49 SSL *clientssl = NULL, *serverssl = NULL; 50 int testresult = 0; 51 SSL_CONNECTION *s = NULL; 52 OSSL_STATEM *st = NULL; 53 uint64_t rtt; 54 55 #ifdef OPENSSL_NO_TLS1_2 56 if (tst <= 1) 57 return 1; 58 #endif 59 #ifdef OSSL_NO_USABLE_TLS1_3 60 if (tst >= 2) 61 return 1; 62 #endif 63 64 if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), 65 TLS_client_method(), 66 TLS1_VERSION, 67 (tst <= 1) ? TLS1_2_VERSION 68 : TLS1_3_VERSION, 69 &sctx, &cctx, cert, privkey)) 70 || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, 71 NULL, NULL))) 72 goto end; 73 74 s = SSL_CONNECTION_FROM_SSL(tst % 2 == 0 ? clientssl : serverssl); 75 if (!TEST_ptr(s) || !TEST_ptr(st = &s->statem)) 76 return 0; 77 78 /* implicitly set handshake rtt with a delay */ 79 switch (tst) { 80 case 0: 81 st->hand_state = TLS_ST_CW_CLNT_HELLO; 82 ossl_statem_client_write_transition(s); 83 OSSL_sleep(1); 84 st->hand_state = TLS_ST_CR_SRVR_DONE; 85 ossl_statem_client_write_transition(s); 86 break; 87 case 1: 88 st->hand_state = TLS_ST_SW_SRVR_DONE; 89 ossl_statem_server_write_transition(s); 90 OSSL_sleep(1); 91 st->hand_state = TLS_ST_SR_FINISHED; 92 ossl_statem_server_write_transition(s); 93 break; 94 case 2: 95 st->hand_state = TLS_ST_CW_CLNT_HELLO; 96 ossl_statem_client_write_transition(s); 97 OSSL_sleep(1); 98 st->hand_state = TLS_ST_CR_SRVR_DONE; 99 ossl_statem_client_write_transition(s); 100 break; 101 case 3: 102 st->hand_state = TLS_ST_SW_SRVR_DONE; 103 ossl_statem_server_write_transition(s); 104 OSSL_sleep(1); 105 st->hand_state = TLS_ST_SR_FINISHED; 106 ossl_statem_server_write_transition(s); 107 break; 108 case 4: 109 st->hand_state = TLS_ST_EARLY_DATA; 110 ossl_statem_client_write_transition(s); 111 OSSL_sleep(1); 112 st->hand_state = TLS_ST_CR_SRVR_DONE; 113 ossl_statem_client_write_transition(s); 114 break; 115 } 116 117 if (!TEST_int_gt(SSL_get_handshake_rtt(SSL_CONNECTION_GET_SSL(s), &rtt), 0)) 118 goto end; 119 /* 1 millisec is the absolute minimum it could be given the delay */ 120 if (!TEST_uint64_t_ge(rtt, 1000)) 121 goto end; 122 123 testresult = 1; 124 125 end: 126 SSL_free(serverssl); 127 SSL_free(clientssl); 128 SSL_CTX_free(sctx); 129 SSL_CTX_free(cctx); 130 131 return testresult; 132 } 133 134 int setup_tests(void) 135 { 136 ADD_ALL_TESTS(test_handshake_rtt, 5); 137 138 return 1; 139 } 140