1#! /usr/bin/env perl
2# Copyright 2017-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_tls13downgrade";
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 TLS1.3 and TLS1.2 enabled"
27    if disabled("tls1_3") || disabled("tls1_2")
28        || (disabled("ec") && disabled("dh"));
29
30my $proxy = TLSProxy::Proxy->new(
31    undef,
32    cmdstr(app(["openssl"]), display => 1),
33    srctop_file("apps", "server.pem"),
34    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35);
36
37use constant {
38    DOWNGRADE_TO_TLS_1_2 => 0,
39    DOWNGRADE_TO_TLS_1_1 => 1,
40    FALLBACK_FROM_TLS_1_3 => 2,
41    DOWNGRADE_TO_TLS_1_2_WITH_TLS_1_1_SIGNAL => 3,
42    DOWNGRADE_TO_TLS_1_1_WITH_TLS_1_2_SIGNAL => 4,
43};
44
45#Test 1: Downgrade from TLSv1.3 to TLSv1.2
46$proxy->filter(\&downgrade_filter);
47my $testtype = DOWNGRADE_TO_TLS_1_2;
48$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
49plan tests => 8;
50ok(is_illegal_parameter_client_alert(), "Downgrade TLSv1.3 to TLSv1.2");
51
52#Test 2: Downgrade from TLSv1.3 to TLSv1.2 (server sends TLSv1.1 signal)
53$proxy->clear();
54$testtype = DOWNGRADE_TO_TLS_1_2_WITH_TLS_1_1_SIGNAL;
55$proxy->start();
56ok(is_illegal_parameter_client_alert(),
57   "Downgrade from TLSv1.3 to TLSv1.2 (server sends TLSv1.1 signal)");
58
59#Test 3: Client falls back from TLSv1.3 (server does not support the fallback
60#        SCSV)
61$proxy->clear();
62$testtype = FALLBACK_FROM_TLS_1_3;
63$proxy->clientflags("-fallback_scsv -no_tls1_3");
64$proxy->start();
65ok(is_illegal_parameter_client_alert(), "Fallback from TLSv1.3");
66
67SKIP: {
68    skip "TLSv1.1 disabled", 5 if disabled("tls1_1");
69
70    my $client_flags = "-min_protocol TLSv1.1 -cipher DEFAULT:\@SECLEVEL=0";
71    my $server_flags = "-min_protocol TLSv1.1";
72    my $ciphers = "AES128-SHA:\@SECLEVEL=0";
73
74    #Test 4: Downgrade from TLSv1.3 to TLSv1.1
75    $proxy->clear();
76    $testtype = DOWNGRADE_TO_TLS_1_1;
77    $proxy->clientflags($client_flags);
78    $proxy->serverflags($server_flags);
79    $proxy->ciphers($ciphers);
80    $proxy->start();
81    ok(is_illegal_parameter_client_alert(), "Downgrade TLSv1.3 to TLSv1.1");
82
83    #Test 5: Downgrade from TLSv1.3 to TLSv1.1 (server sends TLSv1.2 signal)
84    $proxy->clear();
85    $testtype = DOWNGRADE_TO_TLS_1_1_WITH_TLS_1_2_SIGNAL;
86    $proxy->clientflags($client_flags);
87    $proxy->serverflags($server_flags);
88    $proxy->ciphers($ciphers);
89    $proxy->start();
90    ok(is_illegal_parameter_client_alert(),
91       "Downgrade TLSv1.3 to TLSv1.1 (server sends TLSv1.2 signal)");
92
93    #Test 6: Downgrade from TLSv1.2 to TLSv1.1
94    $proxy->clear();
95    $testtype = DOWNGRADE_TO_TLS_1_1;
96    $proxy->clientflags($client_flags." -max_protocol TLSv1.2");
97    $proxy->serverflags($server_flags." -max_protocol TLSv1.2");
98    $proxy->ciphers($ciphers);
99    $proxy->start();
100    ok(is_illegal_parameter_client_alert(), "Downgrade TLSv1.2 to TLSv1.1");
101
102    #Test 7: A client side protocol "hole" should not be detected as a downgrade
103    $proxy->clear();
104    $proxy->filter(undef);
105    $proxy->clientflags($client_flags." -no_tls1_2");
106    $proxy->serverflags($server_flags);
107    $proxy->ciphers($ciphers);
108    $proxy->start();
109    ok(TLSProxy::Message->success(), "TLSv1.2 client-side protocol hole");
110
111    #Test 8: A server side protocol "hole" should not be detected as a downgrade
112    $proxy->clear();
113    $proxy->filter(undef);
114    $proxy->clientflags($client_flags);
115    $proxy->serverflags($server_flags." -no_tls1_2");
116    $proxy->ciphers($ciphers);
117    $proxy->start();
118    ok(TLSProxy::Message->success(), "TLSv1.2 server-side protocol hole");
119}
120
121# Validate that the exchange fails with an illegal parameter alert from
122#  the client
123sub is_illegal_parameter_client_alert
124{
125    return 0 unless TLSProxy::Message->fail();
126    my $alert = TLSProxy::Message->alert();
127    return 1 if !$alert->server()
128                && $alert->description()
129                   == TLSProxy::Message::AL_DESC_ILLEGAL_PARAMETER;
130    return 0;
131}
132
133sub downgrade_filter
134{
135    my $proxy = shift;
136
137    # We're only interested in the initial ClientHello and ServerHello
138    if ($proxy->flight > 1) {
139        return;
140    }
141
142    my $message = ${$proxy->message_list}[$proxy->flight];
143
144    # ServerHello
145    if ($proxy->flight == 1 && defined($message)) {
146        # Update the last byte of the downgrade signal
147        if ($testtype == DOWNGRADE_TO_TLS_1_2_WITH_TLS_1_1_SIGNAL) {
148            $message->random(substr($message->random, 0, 31) . "\0");
149            $message->repack();
150        } elsif ($testtype == DOWNGRADE_TO_TLS_1_1_WITH_TLS_1_2_SIGNAL) {
151            $message->random(substr($message->random, 0, 31) . "\1");
152            $message->repack();
153        }
154
155        return;
156    }
157
158    # ClientHello
159    if ($proxy->flight == 0) {
160        my $ext;
161        if ($testtype == FALLBACK_FROM_TLS_1_3) {
162            #The default ciphersuite we use for TLSv1.2 without any SCSV
163            my @ciphersuites = (TLSProxy::Message::CIPHER_RSA_WITH_AES_128_CBC_SHA);
164            $message->ciphersuite_len(2 * scalar @ciphersuites);
165            $message->ciphersuites(\@ciphersuites);
166        }
167        else {
168            if ($testtype == DOWNGRADE_TO_TLS_1_2
169                || $testtype == DOWNGRADE_TO_TLS_1_2_WITH_TLS_1_1_SIGNAL) {
170                $ext = pack "C3",
171                    0x02,       # Length
172                    0x03, 0x03; #TLSv1.2
173            }
174            else {
175                $ext = pack "C3",
176                    0x02,       # Length
177                    0x03, 0x02; #TLSv1.1
178            }
179
180            $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
181                                    $ext);
182        }
183
184        $message->repack();
185    }
186}
187
188