2 # Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 use List::Util 'first';
11 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
12 use OpenSSL::Test::Utils;
15 my $test_name = "test_renegotiation";
18 plan skip_all => "TLSProxy isn't usable on $^O"
21 plan skip_all => "$test_name needs the dynamic engine feature enabled"
22 if disabled("engine") || disabled("dynamic-engine");
24 plan skip_all => "$test_name needs the sock feature enabled"
27 plan skip_all => "$test_name needs TLS <= 1.2 enabled"
28 if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
32 my $proxy = TLSProxy::Proxy->new(
34 cmdstr(app(["openssl"]), display => 1),
35 srctop_file("apps", "server.pem"),
36 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
39 #Test 1: A basic renegotiation test
40 $proxy->clientflags("-no_tls1_3");
41 $proxy->serverflags("-client_renegotiation");
43 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
44 ok(TLSProxy::Message->success(), "Basic renegotiation");
46 #Test 2: Seclevel 0 client does not send the Reneg SCSV. Reneg should fail
48 $proxy->filter(\&reneg_scsv_filter);
49 $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
50 $proxy->clientflags("-no_tls1_3");
51 $proxy->serverflags("-client_renegotiation");
54 ok(TLSProxy::Message->fail(), "No client SCSV");
57 skip "TLSv1.2 disabled", 1
58 if disabled("tls1_2");
60 #Test 3: TLS 1.2 client does not send the Reneg extension. Reneg should fail
63 $proxy->filter(\&reneg_ext_filter);
64 $proxy->clientflags("-no_tls1_3");
65 $proxy->serverflags("-client_renegotiation");
68 ok(TLSProxy::Message->fail(), "No client extension");
72 skip "TLSv1.2 or TLSv1.1 disabled", 1
73 if disabled("tls1_2") || disabled("tls1_1");
74 #Test 4: Check that the ClientHello version remains the same in the reneg
77 $proxy->filter(undef);
78 $proxy->ciphers("DEFAULT:\@SECLEVEL=0");
79 $proxy->clientflags("-no_tls1_3 -cipher AES128-SHA:\@SECLEVEL=0");
80 $proxy->serverflags("-no_tls1_3 -no_tls1_2 -client_renegotiation");
85 foreach my $message (@{$proxy->message_list}) {
86 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
87 if (!defined $chversion) {
88 $chversion = $message->client_version;
90 if ($chversion == $message->client_version) {
96 ok(TLSProxy::Message->success() && $chmatch,
97 "Check ClientHello version is the same");
101 skip "TLSv1.2 disabled", 1
102 if disabled("tls1_2");
104 #Test 5: Test for CVE-2021-3449. client_sig_algs instead of sig_algs in
105 # resumption ClientHello
107 $proxy->filter(\&sigalgs_filter);
108 $proxy->clientflags("-tls1_2");
109 $proxy->serverflags("-client_renegotiation");
112 ok(TLSProxy::Message->fail(), "client_sig_algs instead of sig_algs");
116 skip "TLSv1.2 and TLSv1.1 disabled", 1
117 if disabled("tls1_2") && disabled("tls1_1");
118 #Test 6: Client fails to do renegotiation
120 $proxy->filter(undef);
121 $proxy->serverflags("-no_tls1_3");
122 $proxy->clientflags("-no_tls1_3");
125 ok(TLSProxy::Message->fail(),
126 "Check client renegotiation failed");
130 skip "TLSv1 disabled", 1
133 #Test 7: Check that SECLEVEL 0 sends SCSV not RI extension
135 $proxy->filter(undef);
136 $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
139 my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
140 my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
141 my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
143 ok($has_scsv && !$has_ri_extension, "SECLEVEL=0 should use SCSV not RI extension by default");
147 skip "TLSv1.2 disabled", 1
148 if disabled("tls1_2");
150 #Test 8: Check that SECLEVEL0 + TLS 1.2 sends RI extension not SCSV
152 $proxy->filter(undef);
153 $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
154 $proxy->clientflags("-tls1_2");
157 my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
158 my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
159 my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
161 ok(!$has_scsv && $has_ri_extension, "TLS1.2 should use RI extension despite SECLEVEL=0");
166 skip "TLSv1.3 disabled", 1
167 if disabled("tls1_3");
169 #Test 9: Check that TLS 1.3 sends neither RI extension nor SCSV
171 $proxy->filter(undef);
172 $proxy->clientflags("-tls1_3");
175 my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
176 my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
177 my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
179 ok(!$has_scsv && !$has_ri_extension, "TLS1.3 should not use RI extension or SCSV");
182 sub reneg_scsv_filter
186 # We're only interested in the initial ClientHello message
187 if ($proxy->flight != 0) {
191 foreach my $message (@{$proxy->message_list}) {
192 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
193 #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
194 my @ciphersuite = (0x002f);
195 $message->ciphersuites(\@ciphersuite);
196 $message->ciphersuite_len(2);
206 # We're only interested in the initial ClientHello message
207 if ($proxy->flight != 0) {
211 foreach my $message (@{$proxy->message_list}) {
212 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
213 $message->delete_extension(TLSProxy::Message::EXT_RENEGOTIATE);
224 # We're only interested in the second ClientHello message
225 foreach my $message (@{$proxy->message_list}) {
226 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
227 next if ($cnt++ == 0);
229 my $sigs = pack "C10", 0x00, 0x08,
230 # rsa_pkcs_sha{256,384,512,1}
231 0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x02, 0x01;
232 $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
233 $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);