xref: /freebsd/crypto/openssl/test/recipes/70-test_renegotiation.t (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1#! /usr/bin/env perl
2# Copyright 2016-2024 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
9use strict;
10use List::Util 'first';
11use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
12use OpenSSL::Test::Utils;
13use TLSProxy::Proxy;
14
15my $test_name = "test_renegotiation";
16setup($test_name);
17
18plan skip_all => "TLSProxy isn't usable on $^O"
19    if $^O =~ /^(VMS)$/;
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 TLS <= 1.2 enabled"
28    if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
29
30plan tests => 9;
31
32my $proxy = TLSProxy::Proxy->new(
33    undef,
34    cmdstr(app(["openssl"]), display => 1),
35    srctop_file("apps", "server.pem"),
36    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
37);
38
39#Test 1: A basic renegotiation test
40$proxy->clientflags("-no_tls1_3");
41$proxy->serverflags("-client_renegotiation");
42$proxy->reneg(1);
43$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
44ok(TLSProxy::Message->success(), "Basic renegotiation");
45
46#Test 2: Seclevel 0 client does not send the Reneg SCSV. Reneg should fail
47$proxy->clear();
48$proxy->filter(\&reneg_scsv_filter);
49$proxy->cipherc("DEFAULT:\@SECLEVEL=0");
50$proxy->clientflags("-no_tls1_3");
51$proxy->serverflags("-client_renegotiation");
52$proxy->reneg(1);
53$proxy->start();
54ok(TLSProxy::Message->fail(), "No client SCSV");
55
56SKIP: {
57    skip "TLSv1.2 disabled", 1
58        if disabled("tls1_2");
59
60    #Test 3: TLS 1.2 client does not send the Reneg extension. Reneg should fail
61
62    $proxy->clear();
63    $proxy->cipherc("DEFAULT:\@SECLEVEL=2");
64    $proxy->filter(\&reneg_ext_filter);
65    $proxy->clientflags("-no_tls1_3");
66    $proxy->serverflags("-client_renegotiation");
67    $proxy->reneg(1);
68    $proxy->start();
69    ok(TLSProxy::Message->fail(), "No client extension");
70}
71
72SKIP: {
73    skip "TLSv1.2 or TLSv1.1 disabled", 1
74        if disabled("tls1_2") || disabled("tls1_1");
75    #Test 4: Check that the ClientHello version remains the same in the reneg
76    #        handshake
77    $proxy->clear();
78    $proxy->filter(undef);
79    $proxy->ciphers("DEFAULT:\@SECLEVEL=0");
80    $proxy->clientflags("-no_tls1_3 -cipher AES128-SHA:\@SECLEVEL=0");
81    $proxy->serverflags("-no_tls1_3 -no_tls1_2 -client_renegotiation");
82    $proxy->reneg(1);
83    $proxy->start();
84    my $chversion;
85    my $chmatch = 0;
86    foreach my $message (@{$proxy->message_list}) {
87        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
88            if (!defined $chversion) {
89                $chversion = $message->client_version;
90            } else {
91                if ($chversion == $message->client_version) {
92                    $chmatch = 1;
93                }
94            }
95        }
96    }
97    ok(TLSProxy::Message->success() && $chmatch,
98       "Check ClientHello version is the same");
99}
100
101SKIP: {
102    skip "TLSv1.2 disabled", 1
103        if disabled("tls1_2");
104
105    #Test 5: Test for CVE-2021-3449. client_sig_algs instead of sig_algs in
106    #        resumption ClientHello
107    $proxy->clear();
108    $proxy->filter(\&sigalgs_filter);
109    $proxy->clientflags("-tls1_2");
110    $proxy->serverflags("-client_renegotiation");
111    $proxy->reneg(1);
112    $proxy->start();
113    ok(TLSProxy::Message->fail(), "client_sig_algs instead of sig_algs");
114}
115
116SKIP: {
117    skip "TLSv1.2 and TLSv1.1 disabled", 1
118        if disabled("tls1_2") && disabled("tls1_1");
119    #Test 6: Client fails to do renegotiation
120    $proxy->clear();
121    $proxy->filter(undef);
122    $proxy->serverflags("-no_tls1_3");
123    $proxy->clientflags("-no_tls1_3");
124    $proxy->reneg(1);
125    $proxy->start();
126    ok(TLSProxy::Message->fail(),
127        "Check client renegotiation failed");
128}
129
130SKIP: {
131    skip "TLSv1 disabled", 1
132        if disabled("tls1");
133
134    #Test 7: Check that SECLEVEL 0 sends SCSV not RI extension
135    $proxy->clear();
136    $proxy->filter(undef);
137    $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
138    $proxy->start();
139
140    my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
141    my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
142    my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
143
144    ok($has_scsv && !$has_ri_extension, "SECLEVEL=0 should use SCSV not RI extension by default");
145}
146
147SKIP: {
148    skip "TLSv1.2 disabled", 1
149        if disabled("tls1_2");
150
151    #Test 8: Check that SECLEVEL0 + TLS 1.2 sends RI extension not SCSV
152    $proxy->clear();
153    $proxy->filter(undef);
154    $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
155    $proxy->clientflags("-tls1_2");
156    $proxy->start();
157
158    my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
159    my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
160    my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
161
162    ok(!$has_scsv && $has_ri_extension, "TLS1.2 should use RI extension despite SECLEVEL=0");
163}
164
165
166SKIP: {
167    skip "TLSv1.3 disabled", 1
168        if disabled("tls1_3");
169
170    #Test 9: Check that TLS 1.3 sends neither RI extension nor SCSV
171    $proxy->clear();
172    $proxy->filter(undef);
173    $proxy->clientflags("-tls1_3");
174    $proxy->start();
175
176    my $clientHello = first { $_->mt == TLSProxy::Message::MT_CLIENT_HELLO } @{$proxy->message_list};
177    my $has_scsv = 255 ~~ @{$clientHello->ciphersuites};
178    my $has_ri_extension = exists $clientHello->extension_data()->{TLSProxy::Message::EXT_RENEGOTIATE};
179
180    ok(!$has_scsv && !$has_ri_extension, "TLS1.3 should not use RI extension or SCSV");
181}
182
183sub reneg_scsv_filter
184{
185    my $proxy = shift;
186
187    # We're only interested in the initial ClientHello message
188    if ($proxy->flight != 0) {
189        return;
190    }
191
192    foreach my $message (@{$proxy->message_list}) {
193        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
194            #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
195            my @ciphersuite = (0x002f);
196            $message->ciphersuites(\@ciphersuite);
197            $message->ciphersuite_len(2);
198            $message->repack();
199        }
200    }
201}
202
203sub reneg_ext_filter
204{
205    my $proxy = shift;
206
207    # We're only interested in the initial ClientHello message
208    if ($proxy->flight != 0) {
209        return;
210    }
211
212    foreach my $message (@{$proxy->message_list}) {
213        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
214            $message->delete_extension(TLSProxy::Message::EXT_RENEGOTIATE);
215            $message->repack();
216        }
217    }
218}
219
220sub sigalgs_filter
221{
222    my $proxy = shift;
223    my $cnt = 0;
224
225    # We're only interested in the second ClientHello message
226    foreach my $message (@{$proxy->message_list}) {
227        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
228            next if ($cnt++ == 0);
229
230            my $sigs = pack "C10", 0x00, 0x08,
231                            # rsa_pkcs_sha{256,384,512,1}
232                            0x04, 0x01,  0x05, 0x01,  0x06, 0x01,  0x02, 0x01;
233            $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
234            $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
235            $message->repack();
236        }
237    }
238}
239