xref: /freebsd/crypto/openssl/test/recipes/70-test_tls13downgrade.t (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubertuse strict;
10*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
12*e0c4386eSCy Schubertuse TLSProxy::Proxy;
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubertmy $test_name = "test_tls13downgrade";
15*e0c4386eSCy Schubertsetup($test_name);
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubertplan skip_all => "TLSProxy isn't usable on $^O"
18*e0c4386eSCy Schubert    if $^O =~ /^(VMS)$/;
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubertplan skip_all => "$test_name needs the dynamic engine feature enabled"
21*e0c4386eSCy Schubert    if disabled("engine") || disabled("dynamic-engine");
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertplan skip_all => "$test_name needs the sock feature enabled"
24*e0c4386eSCy Schubert    if disabled("sock");
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubertplan skip_all => "$test_name needs TLS1.3 and TLS1.2 enabled"
27*e0c4386eSCy Schubert    if disabled("tls1_3")
28*e0c4386eSCy Schubert       || (disabled("ec") && disabled("dh"))
29*e0c4386eSCy Schubert       || disabled("tls1_2");
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32*e0c4386eSCy Schubert
33*e0c4386eSCy Schubertmy $proxy = TLSProxy::Proxy->new(
34*e0c4386eSCy Schubert    undef,
35*e0c4386eSCy Schubert    cmdstr(app(["openssl"]), display => 1),
36*e0c4386eSCy Schubert    srctop_file("apps", "server.pem"),
37*e0c4386eSCy Schubert    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
38*e0c4386eSCy Schubert);
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubertuse constant {
41*e0c4386eSCy Schubert    DOWNGRADE_TO_TLS_1_2 => 0,
42*e0c4386eSCy Schubert    DOWNGRADE_TO_TLS_1_1 => 1,
43*e0c4386eSCy Schubert    FALLBACK_FROM_TLS_1_3 => 2,
44*e0c4386eSCy Schubert};
45*e0c4386eSCy Schubert
46*e0c4386eSCy Schubert#Test 1: Downgrade from TLSv1.3 to TLSv1.2
47*e0c4386eSCy Schubert$proxy->filter(\&downgrade_filter);
48*e0c4386eSCy Schubertmy $testtype = DOWNGRADE_TO_TLS_1_2;
49*e0c4386eSCy Schubert$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
50*e0c4386eSCy Schubertplan tests => 6;
51*e0c4386eSCy Schubertok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.2");
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert#Test 2: Downgrade from TLSv1.3 to TLSv1.1
54*e0c4386eSCy Schubert$proxy->clear();
55*e0c4386eSCy Schubert$testtype = DOWNGRADE_TO_TLS_1_1;
56*e0c4386eSCy Schubert$proxy->start();
57*e0c4386eSCy Schubertok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.1");
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert#Test 3: Downgrade from TLSv1.2 to TLSv1.1
60*e0c4386eSCy Schubert$proxy->clear();
61*e0c4386eSCy Schubert$proxy->clientflags("-no_tls1_3");
62*e0c4386eSCy Schubert$proxy->serverflags("-no_tls1_3");
63*e0c4386eSCy Schubert$proxy->start();
64*e0c4386eSCy Schubertok(TLSProxy::Message->fail(), "Downgrade TLSv1.2 to TLSv1.1");
65*e0c4386eSCy Schubert
66*e0c4386eSCy Schubert#Test 4: Client falls back from TLSv1.3 (server does not support the fallback
67*e0c4386eSCy Schubert#        SCSV)
68*e0c4386eSCy Schubert$proxy->clear();
69*e0c4386eSCy Schubert$testtype = FALLBACK_FROM_TLS_1_3;
70*e0c4386eSCy Schubert$proxy->clientflags("-fallback_scsv -no_tls1_3");
71*e0c4386eSCy Schubert$proxy->start();
72*e0c4386eSCy Schubertmy $alert = TLSProxy::Message->alert();
73*e0c4386eSCy Schubertok(TLSProxy::Message->fail()
74*e0c4386eSCy Schubert   && !$alert->server()
75*e0c4386eSCy Schubert   && $alert->description() == TLSProxy::Message::AL_DESC_ILLEGAL_PARAMETER,
76*e0c4386eSCy Schubert   "Fallback from TLSv1.3");
77*e0c4386eSCy Schubert
78*e0c4386eSCy SchubertSKIP: {
79*e0c4386eSCy Schubert    skip "TLSv1.1 disabled", 2 if disabled("tls1_1");
80*e0c4386eSCy Schubert    #Test 5: A client side protocol "hole" should not be detected as a downgrade
81*e0c4386eSCy Schubert    $proxy->clear();
82*e0c4386eSCy Schubert    $proxy->filter(undef);
83*e0c4386eSCy Schubert    $proxy->clientflags("-no_tls1_2");
84*e0c4386eSCy Schubert    $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
85*e0c4386eSCy Schubert    $proxy->start();
86*e0c4386eSCy Schubert    ok(TLSProxy::Message->success(), "TLSv1.2 client-side protocol hole");
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubert    #Test 6: A server side protocol "hole" should not be detected as a downgrade
89*e0c4386eSCy Schubert    $proxy->clear();
90*e0c4386eSCy Schubert    $proxy->filter(undef);
91*e0c4386eSCy Schubert    $proxy->serverflags("-no_tls1_2");
92*e0c4386eSCy Schubert    $proxy->start();
93*e0c4386eSCy Schubert    ok(TLSProxy::Message->success(), "TLSv1.2 server-side protocol hole");
94*e0c4386eSCy Schubert}
95*e0c4386eSCy Schubert
96*e0c4386eSCy Schubertsub downgrade_filter
97*e0c4386eSCy Schubert{
98*e0c4386eSCy Schubert    my $proxy = shift;
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert    # We're only interested in the initial ClientHello
101*e0c4386eSCy Schubert    if ($proxy->flight != 0) {
102*e0c4386eSCy Schubert        return;
103*e0c4386eSCy Schubert    }
104*e0c4386eSCy Schubert
105*e0c4386eSCy Schubert    my $message = ${$proxy->message_list}[0];
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert    my $ext;
108*e0c4386eSCy Schubert    if ($testtype == FALLBACK_FROM_TLS_1_3) {
109*e0c4386eSCy Schubert        #The default ciphersuite we use for TLSv1.2 without any SCSV
110*e0c4386eSCy Schubert        my @ciphersuites = (TLSProxy::Message::CIPHER_RSA_WITH_AES_128_CBC_SHA);
111*e0c4386eSCy Schubert        $message->ciphersuite_len(2 * scalar @ciphersuites);
112*e0c4386eSCy Schubert        $message->ciphersuites(\@ciphersuites);
113*e0c4386eSCy Schubert    } else {
114*e0c4386eSCy Schubert        if ($testtype == DOWNGRADE_TO_TLS_1_2) {
115*e0c4386eSCy Schubert            $ext = pack "C3",
116*e0c4386eSCy Schubert                0x02, # Length
117*e0c4386eSCy Schubert                0x03, 0x03; #TLSv1.2
118*e0c4386eSCy Schubert        } else {
119*e0c4386eSCy Schubert            $ext = pack "C3",
120*e0c4386eSCy Schubert                0x02, # Length
121*e0c4386eSCy Schubert                0x03, 0x02; #TLSv1.1
122*e0c4386eSCy Schubert        }
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert        $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
125*e0c4386eSCy Schubert    }
126*e0c4386eSCy Schubert
127*e0c4386eSCy Schubert    $message->repack();
128*e0c4386eSCy Schubert}
129*e0c4386eSCy Schubert
130