aboutsummaryrefslogtreecommitdiff
path: root/test/ssl_handshake_rtt_test.c
blob: 0e54284f04a43fc5c0da76ce6902ed7cb4d69b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
 *
 * 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
 */

/*
 * We need access to the deprecated low level HMAC APIs for legacy purposes
 * when the deprecated calls are not hidden
 */
#ifndef OPENSSL_NO_DEPRECATED_3_0
# define OPENSSL_SUPPRESS_DEPRECATED
#endif

#include <stdio.h>
#include <string.h>

#include <openssl/opensslconf.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/ssl.h>
#include <openssl/engine.h>

#include "helpers/ssltestlib.h"
#include "testutil.h"
#include "testutil/output.h"
#include "internal/ktls.h"
#include "../ssl/ssl_local.h"
#include "../ssl/statem/statem_local.h"

static OSSL_LIB_CTX *libctx = NULL;
static char *cert = NULL;
static char *privkey = NULL;

/*
 * Test 0: Clientside handshake RTT (TLSv1.2)
 * Test 1: Serverside handshake RTT (TLSv1.2)
 * Test 2: Clientside handshake RTT (TLSv1.3)
 * Test 3: Serverside handshake RTT (TLSv1.3)
 * Test 4: Clientside handshake RTT with Early Data (TLSv1.3)
 */
static int test_handshake_rtt(int tst)
{
    SSL_CTX *cctx = NULL, *sctx = NULL;
    SSL *clientssl = NULL, *serverssl = NULL;
    int testresult = 0;
    SSL_CONNECTION *s = NULL;
    OSSL_STATEM *st = NULL;
    uint64_t rtt;

#ifdef OPENSSL_NO_TLS1_2
    if (tst <= 1)
        return 1;
#endif
#ifdef OSSL_NO_USABLE_TLS1_3
    if (tst >= 2)
        return 1;
#endif

    if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
                                       TLS_client_method(),
                                       TLS1_VERSION,
                                       (tst <= 1) ? TLS1_2_VERSION
                                                  : TLS1_3_VERSION,
                                       &sctx, &cctx, cert, privkey))
            || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
                                             NULL, NULL)))
        goto end;

    s = SSL_CONNECTION_FROM_SSL(tst % 2 == 0 ? clientssl : serverssl);
    if (!TEST_ptr(s) || !TEST_ptr(st = &s->statem))
        return 0;

    /* implicitly set handshake rtt with a delay */
    switch (tst) {
    case 0:
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
        ossl_statem_client_write_transition(s);
        OSSL_sleep(1);
        st->hand_state = TLS_ST_CR_SRVR_DONE;
        ossl_statem_client_write_transition(s);
        break;
    case 1:
        st->hand_state = TLS_ST_SW_SRVR_DONE;
        ossl_statem_server_write_transition(s);
        OSSL_sleep(1);
        st->hand_state = TLS_ST_SR_FINISHED;
        ossl_statem_server_write_transition(s);
        break;
    case 2:
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
        ossl_statem_client_write_transition(s);
        OSSL_sleep(1);
        st->hand_state = TLS_ST_CR_SRVR_DONE;
        ossl_statem_client_write_transition(s);
        break;
    case 3:
        st->hand_state = TLS_ST_SW_SRVR_DONE;
        ossl_statem_server_write_transition(s);
        OSSL_sleep(1);
        st->hand_state = TLS_ST_SR_FINISHED;
        ossl_statem_server_write_transition(s);
        break;
    case 4:
        st->hand_state = TLS_ST_EARLY_DATA;
        ossl_statem_client_write_transition(s);
        OSSL_sleep(1);
        st->hand_state = TLS_ST_CR_SRVR_DONE;
        ossl_statem_client_write_transition(s);
        break;
    }

    if (!TEST_int_gt(SSL_get_handshake_rtt(SSL_CONNECTION_GET_SSL(s), &rtt), 0))
        goto end;
    /* 1 millisec is the absolute minimum it could be given the delay */
    if (!TEST_uint64_t_ge(rtt, 1000))
        goto end;

    testresult = 1;

 end:
    SSL_free(serverssl);
    SSL_free(clientssl);
    SSL_CTX_free(sctx);
    SSL_CTX_free(cctx);

    return testresult;
}

int setup_tests(void)
{
    ADD_ALL_TESTS(test_handshake_rtt, 5);

    return 1;
}