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