Add a test for TLSv1.3 fallback
[openssl.git] / test / recipes / 70-test_tls13downgrade.t
1 #! /usr/bin/env perl
2 # Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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_tls13downgrade";
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 TLS1.3 and TLS1.2 enabled"
27     if disabled("tls1_3") || disabled("tls1_2");
28
29 # TODO(TLS1.3): Enable this when TLSv1.3 comes out of draft
30 plan skip_all => "$test_name not run in pre TLSv1.3 RFC implementation"
31     if disabled("tls13downgrade");
32
33 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
34
35 my $proxy = TLSProxy::Proxy->new(
36     undef,
37     cmdstr(app(["openssl"]), display => 1),
38     srctop_file("apps", "server.pem"),
39     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
40 );
41
42 use constant {
43     DOWNGRADE_TO_TLS_1_2 => 0,
44     DOWNGRADE_TO_TLS_1_1 => 1,
45     FALLBACK_FROM_TLS_1_3 => 2,
46 };
47
48 #Test 1: Downgrade from TLSv1.3 to TLSv1.2
49 $proxy->filter(\&downgrade_filter);
50 my $testtype = DOWNGRADE_TO_TLS_1_2;
51 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
52 plan tests => 4;
53 ok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.2");
54
55 #Test 2: Downgrade from TLSv1.3 to TLSv1.1
56 $proxy->clear();
57 $testtype = DOWNGRADE_TO_TLS_1_1;
58 $proxy->start();
59 ok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.1");
60
61 #Test 3: Downgrade from TLSv1.2 to TLSv1.1
62 $proxy->clear();
63 $proxy->clientflags("-no_tls1_3");
64 $proxy->serverflags("-no_tls1_3");
65 $proxy->start();
66 ok(TLSProxy::Message->fail(), "Downgrade TLSv1.2 to TLSv1.1");
67
68 #Test 4: Client falls back from TLSv1.3 (server does not support the fallback
69 #        SCSV)
70 $proxy->clear();
71 $testtype = FALLBACK_FROM_TLS_1_3;
72 $proxy->clientflags("-fallback_scsv -no_tls1_3");
73 $proxy->start();
74 my $alert = TLSProxy::Message->alert();
75 ok(TLSProxy::Message->fail()
76    && !$alert->server()
77    && $alert->description() == TLSProxy::Message::AL_DESC_ILLEGAL_PARAMETER,
78    "Fallback from TLSv1.3");
79
80 sub downgrade_filter
81 {
82     my $proxy = shift;
83
84     # We're only interested in the initial ClientHello
85     if ($proxy->flight != 0) {
86         return;
87     }
88
89     my $message = ${$proxy->message_list}[0];
90
91     my $ext;
92     if ($testtype == FALLBACK_FROM_TLS_1_3) {
93         #The default ciphersuite we use for TLSv1.2 without any SCSV
94         my @ciphersuites = (TLSProxy::Message::CIPHER_RSA_WITH_AES_128_CBC_SHA);
95         $message->ciphersuite_len(2 * scalar @ciphersuites);
96         $message->ciphersuites(\@ciphersuites);
97     } else {
98         if ($testtype == DOWNGRADE_TO_TLS_1_2) {
99             $ext = pack "C3",
100                 0x02, # Length
101                 0x03, 0x03; #TLSv1.2
102         } else {
103             $ext = pack "C3",
104                 0x02, # Length
105                 0x03, 0x02; #TLSv1.1
106         }
107
108         $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
109     }
110
111     $message->repack();
112 }
113