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