1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse strict;
10e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
12e1051a39Sopenharmony_ciuse TLSProxy::Proxy;
13e1051a39Sopenharmony_ci
14e1051a39Sopenharmony_cimy $test_name = "test_renegotiation";
15e1051a39Sopenharmony_cisetup($test_name);
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O"
18e1051a39Sopenharmony_ci    if $^O =~ /^(VMS)$/;
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled"
21e1051a39Sopenharmony_ci    if disabled("engine") || disabled("dynamic-engine");
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled"
24e1051a39Sopenharmony_ci    if disabled("sock");
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLS <= 1.2 enabled"
27e1051a39Sopenharmony_ci    if alldisabled(("ssl3", "tls1", "tls1_1", "tls1_2"));
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ciplan tests => 5;
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new(
33e1051a39Sopenharmony_ci    undef,
34e1051a39Sopenharmony_ci    cmdstr(app(["openssl"]), display => 1),
35e1051a39Sopenharmony_ci    srctop_file("apps", "server.pem"),
36e1051a39Sopenharmony_ci    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
37e1051a39Sopenharmony_ci);
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci#Test 1: A basic renegotiation test
40e1051a39Sopenharmony_ci$proxy->clientflags("-no_tls1_3");
41e1051a39Sopenharmony_ci$proxy->serverflags("-client_renegotiation");
42e1051a39Sopenharmony_ci$proxy->reneg(1);
43e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
44e1051a39Sopenharmony_ciok(TLSProxy::Message->success(), "Basic renegotiation");
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci#Test 2: Client does not send the Reneg SCSV. Reneg should fail
47e1051a39Sopenharmony_ci$proxy->clear();
48e1051a39Sopenharmony_ci$proxy->filter(\&reneg_filter);
49e1051a39Sopenharmony_ci$proxy->clientflags("-no_tls1_3");
50e1051a39Sopenharmony_ci$proxy->serverflags("-client_renegotiation");
51e1051a39Sopenharmony_ci$proxy->reneg(1);
52e1051a39Sopenharmony_ci$proxy->start();
53e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "No client SCSV");
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ciSKIP: {
56e1051a39Sopenharmony_ci    skip "TLSv1.2 or TLSv1.1 disabled", 1
57e1051a39Sopenharmony_ci        if disabled("tls1_2") || disabled("tls1_1");
58e1051a39Sopenharmony_ci    #Test 3: Check that the ClientHello version remains the same in the reneg
59e1051a39Sopenharmony_ci    #        handshake
60e1051a39Sopenharmony_ci    $proxy->clear();
61e1051a39Sopenharmony_ci    $proxy->filter(undef);
62e1051a39Sopenharmony_ci    $proxy->ciphers("DEFAULT:\@SECLEVEL=0");
63e1051a39Sopenharmony_ci    $proxy->clientflags("-no_tls1_3 -cipher AES128-SHA:\@SECLEVEL=0");
64e1051a39Sopenharmony_ci    $proxy->serverflags("-no_tls1_3 -no_tls1_2 -client_renegotiation");
65e1051a39Sopenharmony_ci    $proxy->reneg(1);
66e1051a39Sopenharmony_ci    $proxy->start();
67e1051a39Sopenharmony_ci    my $chversion;
68e1051a39Sopenharmony_ci    my $chmatch = 0;
69e1051a39Sopenharmony_ci    foreach my $message (@{$proxy->message_list}) {
70e1051a39Sopenharmony_ci        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
71e1051a39Sopenharmony_ci            if (!defined $chversion) {
72e1051a39Sopenharmony_ci                $chversion = $message->client_version;
73e1051a39Sopenharmony_ci            } else {
74e1051a39Sopenharmony_ci                if ($chversion == $message->client_version) {
75e1051a39Sopenharmony_ci                    $chmatch = 1;
76e1051a39Sopenharmony_ci                }
77e1051a39Sopenharmony_ci            }
78e1051a39Sopenharmony_ci        }
79e1051a39Sopenharmony_ci    }
80e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && $chmatch,
81e1051a39Sopenharmony_ci       "Check ClientHello version is the same");
82e1051a39Sopenharmony_ci}
83e1051a39Sopenharmony_ci
84e1051a39Sopenharmony_ciSKIP: {
85e1051a39Sopenharmony_ci    skip "TLSv1.2 disabled", 1
86e1051a39Sopenharmony_ci        if disabled("tls1_2");
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    #Test 4: Test for CVE-2021-3449. client_sig_algs instead of sig_algs in
89e1051a39Sopenharmony_ci    #        resumption ClientHello
90e1051a39Sopenharmony_ci    $proxy->clear();
91e1051a39Sopenharmony_ci    $proxy->filter(\&sigalgs_filter);
92e1051a39Sopenharmony_ci    $proxy->clientflags("-tls1_2");
93e1051a39Sopenharmony_ci    $proxy->serverflags("-client_renegotiation");
94e1051a39Sopenharmony_ci    $proxy->reneg(1);
95e1051a39Sopenharmony_ci    $proxy->start();
96e1051a39Sopenharmony_ci    ok(TLSProxy::Message->fail(), "client_sig_algs instead of sig_algs");
97e1051a39Sopenharmony_ci}
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_ciSKIP: {
100e1051a39Sopenharmony_ci    skip "TLSv1.2 and TLSv1.1 disabled", 1
101e1051a39Sopenharmony_ci        if disabled("tls1_2") && disabled("tls1_1");
102e1051a39Sopenharmony_ci    #Test 5: Client fails to do renegotiation
103e1051a39Sopenharmony_ci    $proxy->clear();
104e1051a39Sopenharmony_ci    $proxy->filter(undef);
105e1051a39Sopenharmony_ci    $proxy->serverflags("-no_tls1_3");
106e1051a39Sopenharmony_ci    $proxy->clientflags("-no_tls1_3");
107e1051a39Sopenharmony_ci    $proxy->reneg(1);
108e1051a39Sopenharmony_ci    $proxy->start();
109e1051a39Sopenharmony_ci    ok(TLSProxy::Message->fail(),
110e1051a39Sopenharmony_ci        "Check client renegotiation failed");
111e1051a39Sopenharmony_ci}
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_cisub reneg_filter
114e1051a39Sopenharmony_ci{
115e1051a39Sopenharmony_ci    my $proxy = shift;
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_ci    # We're only interested in the initial ClientHello message
118e1051a39Sopenharmony_ci    if ($proxy->flight != 0) {
119e1051a39Sopenharmony_ci        return;
120e1051a39Sopenharmony_ci    }
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ci    foreach my $message (@{$proxy->message_list}) {
123e1051a39Sopenharmony_ci        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
124e1051a39Sopenharmony_ci            #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
125e1051a39Sopenharmony_ci            my @ciphersuite = (0x002f);
126e1051a39Sopenharmony_ci            $message->ciphersuites(\@ciphersuite);
127e1051a39Sopenharmony_ci            $message->ciphersuite_len(2);
128e1051a39Sopenharmony_ci            $message->repack();
129e1051a39Sopenharmony_ci        }
130e1051a39Sopenharmony_ci    }
131e1051a39Sopenharmony_ci}
132e1051a39Sopenharmony_ci
133e1051a39Sopenharmony_cisub sigalgs_filter
134e1051a39Sopenharmony_ci{
135e1051a39Sopenharmony_ci    my $proxy = shift;
136e1051a39Sopenharmony_ci    my $cnt = 0;
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ci    # We're only interested in the second ClientHello message
139e1051a39Sopenharmony_ci    foreach my $message (@{$proxy->message_list}) {
140e1051a39Sopenharmony_ci        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
141e1051a39Sopenharmony_ci            next if ($cnt++ == 0);
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci            my $sigs = pack "C10", 0x00, 0x08,
144e1051a39Sopenharmony_ci                            # rsa_pkcs_sha{256,384,512,1}
145e1051a39Sopenharmony_ci                            0x04, 0x01,  0x05, 0x01,  0x06, 0x01,  0x02, 0x01;
146e1051a39Sopenharmony_ci            $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
147e1051a39Sopenharmony_ci            $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
148e1051a39Sopenharmony_ci            $message->repack();
149e1051a39Sopenharmony_ci        }
150e1051a39Sopenharmony_ci    }
151e1051a39Sopenharmony_ci}
152