]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/recipes/70-test_tls13hrr.t
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / test / recipes / 70-test_tls13hrr.t
CommitLineData
c35cb287 1#! /usr/bin/env perl
b6461792 2# Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved.
c35cb287 3#
909f1a2e 4# Licensed under the Apache License 2.0 (the "License"). You may not use
c35cb287
MC
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
9use strict;
10use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11use OpenSSL::Test::Utils;
12use TLSProxy::Proxy;
a1c72cc2 13use TLSProxy::Message;
c35cb287
MC
14
15my $test_name = "test_tls13hrr";
16setup($test_name);
17
18plan skip_all => "TLSProxy isn't usable on $^O"
c5856878 19 if $^O =~ /^(VMS)$/;
c35cb287
MC
20
21plan skip_all => "$test_name needs the dynamic engine feature enabled"
22 if disabled("engine") || disabled("dynamic-engine");
23
24plan skip_all => "$test_name needs the sock feature enabled"
25 if disabled("sock");
26
27plan skip_all => "$test_name needs TLS1.3 enabled"
a763ca11 28 if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
c35cb287 29
c35cb287
MC
30my $proxy = TLSProxy::Proxy->new(
31 undef,
32 cmdstr(app(["openssl"]), display => 1),
33 srctop_file("apps", "server.pem"),
34 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35);
36
37use constant {
38 CHANGE_HRR_CIPHERSUITE => 0,
db44b55a 39 CHANGE_CH1_CIPHERSUITE => 1,
adf33f9e
MC
40 DUPLICATE_HRR => 2,
41 INVALID_GROUP => 3
c35cb287
MC
42};
43
44#Test 1: A client should fail if the server changes the ciphersuite between the
45# HRR and the SH
46$proxy->filter(\&hrr_filter);
dbc6268f
MC
47if (disabled("ec")) {
48 $proxy->serverflags("-curves ffdhe3072");
49} else {
50 $proxy->serverflags("-curves P-256");
51}
c35cb287
MC
52my $testtype = CHANGE_HRR_CIPHERSUITE;
53$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
adf33f9e 54plan tests => 4;
c35cb287
MC
55ok(TLSProxy::Message->fail(), "Server ciphersuite changes");
56
57#Test 2: It is an error if the client changes the offered ciphersuites so that
58# we end up selecting a different ciphersuite between HRR and the SH
59$proxy->clear();
dbc6268f
MC
60if (disabled("ec")) {
61 $proxy->serverflags("-curves ffdhe3072");
62} else {
4032cd9a 63 $proxy->serverflags("-curves P-384");
dbc6268f 64}
f865b081 65$proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
c35cb287
MC
66$testtype = CHANGE_CH1_CIPHERSUITE;
67$proxy->start();
68ok(TLSProxy::Message->fail(), "Client ciphersuite changes");
69
db44b55a
TM
70#Test 3: A client should fail with unexpected_message alert if the server
71# sends more than 1 HRR
72my $fatal_alert = 0;
73$proxy->clear();
74if (disabled("ec")) {
75 $proxy->serverflags("-curves ffdhe3072");
76} else {
c3832d79 77 $proxy->serverflags("-curves P-384");
db44b55a
TM
78}
79$testtype = DUPLICATE_HRR;
80$proxy->start();
81ok($fatal_alert, "Server duplicated HRR");
82
adf33f9e
MC
83#Test 4: If the client sends a group that is in the supported_groups list but
84# otherwise not valid (e.g. not suitable for TLSv1.3) we should reject it
85# and not consider it when sending the HRR. We send brainpoolP512r1 in
86# the ClientHello, which is acceptable to the server but is not valid in
5c3c8369 87# TLSv1.3. We expect the server to select P-521 in the HRR and the
adf33f9e
MC
88# handshake to complete successfully
89SKIP: {
90 skip "EC/TLSv1.2 is disabled in this build", 1
91 if disabled("ec") || disabled("tls1_2");
92
93 $proxy->clear();
5c3c8369
TM
94 $proxy->clientflags("-groups P-256:brainpoolP512r1:P-521");
95 $proxy->serverflags("-groups brainpoolP512r1:P-521");
adf33f9e
MC
96 $testtype = INVALID_GROUP;
97 $proxy->start();
98 ok(TLSProxy::Message->success(), "Invalid group with HRR");
99}
100
c35cb287
MC
101sub hrr_filter
102{
103 my $proxy = shift;
104
105 if ($testtype == CHANGE_HRR_CIPHERSUITE) {
106 # We're only interested in the HRR
107 if ($proxy->flight != 1) {
108 return;
109 }
110
111 my $hrr = ${$proxy->message_list}[1];
112
113 # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256
114 # because that's what Proxy tells s_server to do. Setting as below means
115 # the ciphersuite will change will we get the ServerHello
116 $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384);
117 $hrr->repack();
118 return;
119 }
120
db44b55a
TM
121 if ($testtype == DUPLICATE_HRR) {
122 # We're only interested in the HRR
123 # and the unexpected_message alert from client
124 if ($proxy->flight == 4) {
125 $fatal_alert = 1
a1c72cc2 126 if @{$proxy->record_list}[-1]->is_fatal_alert(0) == TLSProxy::Message::AL_DESC_UNEXPECTED_MESSAGE;
db44b55a
TM
127 return;
128 }
129 if ($proxy->flight != 3) {
130 return;
131 }
132
133 # Find ServerHello record (HRR actually) and insert after that
134 my $i;
135 for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) {
136 next;
137 }
138 my $hrr_record = ${$proxy->record_list}[$i];
139 my $dup_hrr = TLSProxy::Record->new(3,
140 $hrr_record->content_type(),
141 $hrr_record->version(),
142 $hrr_record->len(),
143 $hrr_record->sslv2(),
144 $hrr_record->len_real(),
145 $hrr_record->decrypt_len(),
146 $hrr_record->data(),
147 $hrr_record->decrypt_data());
148
149 $i++;
150 splice @{$proxy->record_list}, $i, 0, $dup_hrr;
151 return;
152 }
153
c35cb287
MC
154 if ($proxy->flight != 0) {
155 return;
156 }
157
158 my $ch1 = ${$proxy->message_list}[0];
159
adf33f9e
MC
160 if ($testtype == CHANGE_CH1_CIPHERSUITE) {
161 # The server will always pick TLS_AES_256_GCM_SHA384
162 my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256);
163 $ch1->ciphersuite_len(2 * scalar @ciphersuites);
164 $ch1->ciphersuites(\@ciphersuites);
165 } elsif ($testtype == INVALID_GROUP) {
166 # INVALID_GROUP
167 my $ext = pack "C7",
168 0x00, 0x05, #List Length
169 0x00, 0x1c, #brainpoolP512r1 (not compatible with TLSv1.3)
170 0x00, 0x01, 0xff; #key_exchange data
171 $ch1->set_extension(
172 TLSProxy::Message::EXT_KEY_SHARE, $ext);
173 }
c35cb287
MC
174 $ch1->repack();
175}