Add SSL_OP_ALLOW_CLIENT_RENEGOTIATION
[openssl.git] / test / recipes / 70-test_renegotiation.t
1 #! /usr/bin/env perl
2 # Copyright 2016-2021 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
9 use strict;
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use TLSProxy::Proxy;
13
14 my $test_name = "test_renegotiation";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS)$/;
19
20 plan skip_all => "$test_name needs the dynamic engine feature enabled"
21     if disabled("engine") || disabled("dynamic-engine");
22
23 plan skip_all => "$test_name needs the sock feature enabled"
24     if disabled("sock");
25
26 plan skip_all => "$test_name needs TLS <= 1.2 enabled"
27     if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
28
29 plan tests => 6;
30
31 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32 my $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";
44 ok(TLSProxy::Message->success(), "Basic renegotiation");
45
46 #Test 2: Client does not send the Reneg SCSV. Reneg should fail
47 $proxy->clear();
48 $proxy->filter(\&reneg_filter);
49 $proxy->clientflags("-no_tls1_3");
50 $proxy->serverflags("-client_renegotiation");
51 $proxy->reneg(1);
52 $proxy->start();
53 ok(TLSProxy::Message->fail(), "No client SCSV");
54
55 SKIP: {
56     skip "TLSv1.2 or TLSv1.1 disabled", 1
57         if disabled("tls1_2") || disabled("tls1_1");
58     #Test 3: Check that the ClientHello version remains the same in the reneg
59     #        handshake
60     $proxy->clear();
61     $proxy->filter(undef);
62     $proxy->ciphers("DEFAULT:\@SECLEVEL=0");
63     $proxy->clientflags("-no_tls1_3 -cipher AES128-SHA:\@SECLEVEL=0");
64     $proxy->serverflags("-no_tls1_3 -no_tls1_2 -client_renegotiation");
65     $proxy->reneg(1);
66     $proxy->start();
67     my $chversion;
68     my $chmatch = 0;
69     foreach my $message (@{$proxy->message_list}) {
70         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
71             if (!defined $chversion) {
72                 $chversion = $message->client_version;
73             } else {
74                 if ($chversion == $message->client_version) {
75                     $chmatch = 1;
76                 }
77             }
78         }
79     }
80     ok(TLSProxy::Message->success() && $chmatch,
81        "Check ClientHello version is the same");
82 }
83
84 SKIP: {
85     skip "TLSv1.2 disabled", 1
86         if disabled("tls1_2");
87
88     #Test 4: Test for CVE-2021-3449. client_sig_algs instead of sig_algs in
89     #        resumption ClientHello
90     $proxy->clear();
91     $proxy->filter(\&sigalgs_filter);
92     $proxy->clientflags("-tls1_2");
93     $proxy->serverflags("-client_renegotiation");
94     $proxy->reneg(1);
95     $proxy->start();
96     ok(TLSProxy::Message->fail(), "client_sig_algs instead of sig_algs");
97 }
98
99 SKIP: {
100     skip "TLSv1.2 and TLSv1.1 disabled", 1
101         if disabled("tls1_2") && disabled("tls1_1");
102     #Test 5: Client fails to do renegotiation
103     $proxy->clear();
104     $proxy->filter(undef);
105     $proxy->serverflags("-no_tls1_3");
106     $proxy->clientflags("-no_tls1_3");
107     $proxy->reneg(1);
108     $proxy->start();
109     ok(TLSProxy::Message->fail(),
110         "Check client renegotiation failed");
111 }
112
113 SKIP: {
114     skip "TLSv1.2 and TLSv1.1 disabled", 1
115         if disabled("tls1_2") && disabled("tls1_1");
116     #Test 6: Server can do renegotiation
117     $proxy->clear();
118     $proxy->filter(undef);
119     $proxy->serverflags("-no_tls1_3 -immediate_renegotiation");
120     $proxy->clientflags("-no_tls1_3");
121     $proxy->start();
122     ok(TLSProxy::Message->success(),
123         "Check server renegotiation succeeded");
124 }
125
126 sub reneg_filter
127 {
128     my $proxy = shift;
129
130     # We're only interested in the initial ClientHello message
131     if ($proxy->flight != 0) {
132         return;
133     }
134
135     foreach my $message (@{$proxy->message_list}) {
136         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
137             #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
138             my @ciphersuite = (0x002f);
139             $message->ciphersuites(\@ciphersuite);
140             $message->ciphersuite_len(2);
141             $message->repack();
142         }
143     }
144 }
145
146 sub sigalgs_filter
147 {
148     my $proxy = shift;
149     my $cnt = 0;
150
151     # We're only interested in the second ClientHello message
152     foreach my $message (@{$proxy->message_list}) {
153         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
154             next if ($cnt++ == 0);
155
156             my $sigs = pack "C10", 0x00, 0x08,
157                             # rsa_pkcs_sha{256,384,512,1}
158                             0x04, 0x01,  0x05, 0x01,  0x06, 0x01,  0x02, 0x01;
159             $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
160             $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
161             $message->repack();
162         }
163     }
164 }