xref: /openssl/test/ssl_handshake_rtt_test.c (revision cee0628e)
1 /*
2  * Copyright 2023 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 
34 static OSSL_LIB_CTX *libctx = NULL;
35 static char *cert = NULL;
36 static char *privkey = NULL;
37 
38 /*
39  * Test 0: Clientside handshake RTT (TLSv1.2)
40  * Test 1: Serverside handshake RTT (TLSv1.2)
41  * Test 2: Clientside handshake RTT (TLSv1.3)
42  * Test 3: Serverside handshake RTT (TLSv1.3)
43  * Test 4: Clientside handshake RTT with Early Data (TLSv1.3)
44  */
test_handshake_rtt(int tst)45 static int test_handshake_rtt(int tst)
46 {
47     SSL_CTX *cctx = NULL, *sctx = NULL;
48     SSL *clientssl = NULL, *serverssl = NULL;
49     int testresult = 0;
50     SSL_CONNECTION *s = NULL;
51     OSSL_STATEM *st = NULL;
52     uint64_t rtt;
53 
54 #ifdef OPENSSL_NO_TLS1_2
55     if (tst <= 1)
56         return 1;
57 #endif
58 #ifdef OSSL_NO_USABLE_TLS1_3
59     if (tst >= 2)
60         return 1;
61 #endif
62 
63     if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
64                                        TLS_client_method(),
65                                        TLS1_VERSION,
66                                        (tst <= 1) ? TLS1_2_VERSION
67                                                   : TLS1_3_VERSION,
68                                        &sctx, &cctx, cert, privkey))
69             || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
70                                              NULL, NULL)))
71         goto end;
72 
73     s = SSL_CONNECTION_FROM_SSL(tst % 2 == 0 ? clientssl : serverssl);
74     if (!TEST_ptr(s) || !TEST_ptr(st = &s->statem))
75         return 0;
76 
77     /* implicitly set handshake rtt with a delay */
78     switch (tst) {
79     case 0:
80         st->hand_state = TLS_ST_CW_CLNT_HELLO;
81         ossl_statem_client_write_transition(s);
82         OSSL_sleep(1);
83         st->hand_state = TLS_ST_CR_SRVR_DONE;
84         ossl_statem_client_write_transition(s);
85         break;
86     case 1:
87         st->hand_state = TLS_ST_SW_SRVR_DONE;
88         ossl_statem_server_write_transition(s);
89         OSSL_sleep(1);
90         st->hand_state = TLS_ST_SR_FINISHED;
91         ossl_statem_server_write_transition(s);
92         break;
93     case 2:
94         st->hand_state = TLS_ST_CW_CLNT_HELLO;
95         ossl_statem_client_write_transition(s);
96         OSSL_sleep(1);
97         st->hand_state = TLS_ST_CR_SRVR_DONE;
98         ossl_statem_client_write_transition(s);
99         break;
100     case 3:
101         st->hand_state = TLS_ST_SW_SRVR_DONE;
102         ossl_statem_server_write_transition(s);
103         OSSL_sleep(1);
104         st->hand_state = TLS_ST_SR_FINISHED;
105         ossl_statem_server_write_transition(s);
106         break;
107     case 4:
108         st->hand_state = TLS_ST_EARLY_DATA;
109         ossl_statem_client_write_transition(s);
110         OSSL_sleep(1);
111         st->hand_state = TLS_ST_CR_SRVR_DONE;
112         ossl_statem_client_write_transition(s);
113         break;
114     }
115 
116     if (!TEST_int_gt(SSL_get_handshake_rtt(SSL_CONNECTION_GET_SSL(s), &rtt), 0))
117         goto end;
118     /* 1 millisec is the absolute minimum it could be given the delay */
119     if (!TEST_uint64_t_ge(rtt, 1000))
120         goto end;
121 
122     testresult = 1;
123 
124  end:
125     SSL_free(serverssl);
126     SSL_free(clientssl);
127     SSL_CTX_free(sctx);
128     SSL_CTX_free(cctx);
129 
130     return testresult;
131 }
132 
setup_tests(void)133 int setup_tests(void)
134 {
135     ADD_ALL_TESTS(test_handshake_rtt, 5);
136 
137     return 1;
138 }
139