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
31$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
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: 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();
53ok(TLSProxy::Message->fail(), "No client SCSV");
54
55SKIP: {
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
84SKIP: {
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
99SKIP: {
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
113sub reneg_filter
114{
115    my $proxy = shift;
116
117    # We're only interested in the initial ClientHello message
118    if ($proxy->flight != 0) {
119        return;
120    }
121
122    foreach my $message (@{$proxy->message_list}) {
123        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
124            #Remove any SCSV ciphersuites - just leave AES128-SHA (0x002f)
125            my @ciphersuite = (0x002f);
126            $message->ciphersuites(\@ciphersuite);
127            $message->ciphersuite_len(2);
128            $message->repack();
129        }
130    }
131}
132
133sub sigalgs_filter
134{
135    my $proxy = shift;
136    my $cnt = 0;
137
138    # We're only interested in the second ClientHello message
139    foreach my $message (@{$proxy->message_list}) {
140        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
141            next if ($cnt++ == 0);
142
143            my $sigs = pack "C10", 0x00, 0x08,
144                            # rsa_pkcs_sha{256,384,512,1}
145                            0x04, 0x01,  0x05, 0x01,  0x06, 0x01,  0x02, 0x01;
146            $message->set_extension(TLSProxy::Message::EXT_SIG_ALGS_CERT, $sigs);
147            $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
148            $message->repack();
149        }
150    }
151}
152