summaryrefslogtreecommitdiffstats
path: root/test/recipes/70-test_tls13hrr.t
blob: 9d0694c3d6ecd135577efc5c9e6e862f1864da4b (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/env perl
# Copyright 2017-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

use strict;
use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
use OpenSSL::Test::Utils;
use TLSProxy::Proxy;

my $test_name = "test_tls13hrr";
setup($test_name);

plan skip_all => "TLSProxy isn't usable on $^O"
    if $^O =~ /^(VMS)$/;

plan skip_all => "$test_name needs the dynamic engine feature enabled"
    if disabled("engine") || disabled("dynamic-engine");

plan skip_all => "$test_name needs the sock feature enabled"
    if disabled("sock");

plan skip_all => "$test_name needs TLS1.3 enabled"
    if disabled("tls1_3") || (disabled("ec") && disabled("dh"));

$ENV{OPENSSL_ia32cap} = '~0x200000200000000';

my $proxy = TLSProxy::Proxy->new(
    undef,
    cmdstr(app(["openssl"]), display => 1),
    srctop_file("apps", "server.pem"),
    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
);

use constant {
    CHANGE_HRR_CIPHERSUITE => 0,
    CHANGE_CH1_CIPHERSUITE => 1,
    DUPLICATE_HRR => 2,
    INVALID_GROUP => 3
};

#Test 1: A client should fail if the server changes the ciphersuite between the
#        HRR and the SH
$proxy->filter(\&hrr_filter);
if (disabled("ec")) {
    $proxy->serverflags("-curves ffdhe3072");
} else {
    $proxy->serverflags("-curves P-256");
}
my $testtype = CHANGE_HRR_CIPHERSUITE;
$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
plan tests => 4;
ok(TLSProxy::Message->fail(), "Server ciphersuite changes");

#Test 2: It is an error if the client changes the offered ciphersuites so that
#        we end up selecting a different ciphersuite between HRR and the SH
$proxy->clear();
if (disabled("ec")) {
    $proxy->serverflags("-curves ffdhe3072");
} else {
    $proxy->serverflags("-curves P-256");
}
$proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
$testtype = CHANGE_CH1_CIPHERSUITE;
$proxy->start();
ok(TLSProxy::Message->fail(), "Client ciphersuite changes");

#Test 3: A client should fail with unexpected_message alert if the server
#        sends more than 1 HRR
my $fatal_alert = 0;
$proxy->clear();
if (disabled("ec")) {
    $proxy->serverflags("-curves ffdhe3072");
} else {
    $proxy->serverflags("-curves P-256");
}
$testtype = DUPLICATE_HRR;
$proxy->start();
ok($fatal_alert, "Server duplicated HRR");

#Test 4: If the client sends a group that is in the supported_groups list but
#        otherwise not valid (e.g. not suitable for TLSv1.3) we should reject it
#        and not consider it when sending the HRR. We send brainpoolP512r1 in
#        the ClientHello, which is acceptable to the server but is not valid in
#        TLSv1.3. We expect the server to select X25519 in the HRR and the
#        handshake to complete successfully
SKIP: {
    skip "EC/TLSv1.2 is disabled in this build", 1
        if disabled("ec") || disabled("tls1_2");

    $proxy->clear();
    $proxy->clientflags("-groups P-256:brainpoolP512r1:X25519");
    $proxy->serverflags("-groups brainpoolP512r1:X25519");
    $testtype = INVALID_GROUP;
    $proxy->start();
    ok(TLSProxy::Message->success(), "Invalid group with HRR");
}

sub hrr_filter
{
    my $proxy = shift;

    if ($testtype == CHANGE_HRR_CIPHERSUITE) {
        # We're only interested in the HRR
        if ($proxy->flight != 1) {
            return;
        }

        my $hrr = ${$proxy->message_list}[1];

        # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
        # because that's what Proxy tells s_server to do. Setting as below means
        # the ciphersuite will change will we get the ServerHello
        $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
        $hrr->repack();
        return;
    }

    if ($testtype == DUPLICATE_HRR) {
        # We're only interested in the HRR
        # and the unexpected_message alert from client
        if ($proxy->flight == 4) {
            $fatal_alert = 1
                if @{$proxy->record_list}[-1]->is_fatal_alert(0) == 10;
            return;
        }
        if ($proxy->flight != 3) {
            return;
        }

        # Find ServerHello record (HRR actually) and insert after that
        my $i;
        for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) {
            next;
        }
        my $hrr_record = ${$proxy->record_list}[$i];
        my $dup_hrr = TLSProxy::Record->new(3,
            $hrr_record->content_type(),
            $hrr_record->version(),
            $hrr_record->len(),
            $hrr_record->sslv2(),
            $hrr_record->len_real(),
            $hrr_record->decrypt_len(),
            $hrr_record->data(),
            $hrr_record->decrypt_data());

        $i++;
        splice @{$proxy->record_list}, $i, 0, $dup_hrr;
        return;
    }

    if ($proxy->flight != 0) {
        return;
    }

    my $ch1 = ${$proxy->message_list}[0];

    if ($testtype == CHANGE_CH1_CIPHERSUITE) {
        # The server will always pick TLS_AES_256_GCM_SHA384
        my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
        $ch1->ciphersuite_len(2 * scalar @ciphersuites);
        $ch1->ciphersuites(\@ciphersuites);
    } elsif ($testtype == INVALID_GROUP) {
        # INVALID_GROUP
        my $ext = pack "C7",
            0x00, 0x05, #List Length
            0x00, 0x1c, #brainpoolP512r1 (not compatible with TLSv1.3)
            0x00, 0x01, 0xff; #key_exchange data
        $ch1->set_extension(
            TLSProxy::Message::EXT_KEY_SHARE, $ext);
    }
    $ch1->repack();
}