1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2016-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 feature 'state';
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
13*e0c4386eSCy Schubertuse OpenSSL::Test::Utils;
14*e0c4386eSCy Schubertuse TLSProxy::Proxy;
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubertmy $test_name = "test_sslcbcpadding";
17*e0c4386eSCy Schubertsetup($test_name);
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubertplan skip_all => "TLSProxy isn't usable on $^O"
20*e0c4386eSCy Schubert    if $^O =~ /^(VMS)$/;
21*e0c4386eSCy Schubert
22*e0c4386eSCy Schubertplan skip_all => "$test_name needs the dynamic engine feature enabled"
23*e0c4386eSCy Schubert    if disabled("engine") || disabled("dynamic-engine");
24*e0c4386eSCy Schubert
25*e0c4386eSCy Schubertplan skip_all => "$test_name needs the sock feature enabled"
26*e0c4386eSCy Schubert    if disabled("sock");
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubertplan skip_all => "$test_name needs TLSv1.2 enabled"
29*e0c4386eSCy Schubert    if disabled("tls1_2");
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32*e0c4386eSCy Schubertmy $proxy = TLSProxy::Proxy->new(
33*e0c4386eSCy Schubert    \&add_maximal_padding_filter,
34*e0c4386eSCy Schubert    cmdstr(app(["openssl"]), display => 1),
35*e0c4386eSCy Schubert    srctop_file("apps", "server.pem"),
36*e0c4386eSCy Schubert    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
37*e0c4386eSCy Schubert);
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert# TODO: We could test all 256 values, but then the log file gets too large for
40*e0c4386eSCy Schubert# CI. See https://github.com/openssl/openssl/issues/1440.
41*e0c4386eSCy Schubertmy @test_offsets = (0, 128, 254, 255);
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert# Test that maximally-padded records are accepted.
44*e0c4386eSCy Schubertmy $bad_padding_offset = -1;
45*e0c4386eSCy Schubert$proxy->serverflags("-tls1_2");
46*e0c4386eSCy Schubert$proxy->clientflags("-no_tls1_3");
47*e0c4386eSCy Schubert$proxy->serverconnects(1 + scalar(@test_offsets));
48*e0c4386eSCy Schubert$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
49*e0c4386eSCy Schubertplan tests => 1 + scalar(@test_offsets);
50*e0c4386eSCy Schubertok(TLSProxy::Message->success(), "Maximally-padded record test");
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert# Test that invalid padding is rejected.
53*e0c4386eSCy Schubertmy $fatal_alert;    # set by add_maximal_padding_filter on client's fatal alert
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubertforeach my $offset (@test_offsets) {
56*e0c4386eSCy Schubert    $bad_padding_offset = $offset;
57*e0c4386eSCy Schubert    $fatal_alert = 0;
58*e0c4386eSCy Schubert    $proxy->clearClient();
59*e0c4386eSCy Schubert    $proxy->clientflags("-no_tls1_3");
60*e0c4386eSCy Schubert    $proxy->clientstart();
61*e0c4386eSCy Schubert    ok($fatal_alert, "Invalid padding byte $bad_padding_offset");
62*e0c4386eSCy Schubert}
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubertsub add_maximal_padding_filter
65*e0c4386eSCy Schubert{
66*e0c4386eSCy Schubert    my $proxy = shift;
67*e0c4386eSCy Schubert    my $messages = $proxy->message_list;
68*e0c4386eSCy Schubert    state $sent_corrupted_payload;
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert    if ($proxy->flight == 0) {
71*e0c4386eSCy Schubert        # Disable Encrypt-then-MAC.
72*e0c4386eSCy Schubert        foreach my $message (@{$messages}) {
73*e0c4386eSCy Schubert            if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
74*e0c4386eSCy Schubert                next;
75*e0c4386eSCy Schubert            }
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert            $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
78*e0c4386eSCy Schubert            $message->process_extensions();
79*e0c4386eSCy Schubert            $message->repack();
80*e0c4386eSCy Schubert        }
81*e0c4386eSCy Schubert        $sent_corrupted_payload = 0;
82*e0c4386eSCy Schubert        return;
83*e0c4386eSCy Schubert    }
84*e0c4386eSCy Schubert
85*e0c4386eSCy Schubert    my $last_message = @{$messages}[-1];
86*e0c4386eSCy Schubert    if (defined($last_message)
87*e0c4386eSCy Schubert        && $last_message->server
88*e0c4386eSCy Schubert        && $last_message->mt == TLSProxy::Message::MT_FINISHED
89*e0c4386eSCy Schubert        && !@{$last_message->records}[0]->{sent}) {
90*e0c4386eSCy Schubert
91*e0c4386eSCy Schubert        # Insert a maximally-padded record. Assume a block size of 16 (AES) and
92*e0c4386eSCy Schubert        # a MAC length of 20 (SHA-1).
93*e0c4386eSCy Schubert        my $block_size = 16;
94*e0c4386eSCy Schubert        my $mac_len = 20;
95*e0c4386eSCy Schubert
96*e0c4386eSCy Schubert        # Size the plaintext so that 256 is a valid padding.
97*e0c4386eSCy Schubert        my $plaintext_len = $block_size - ($mac_len % $block_size);
98*e0c4386eSCy Schubert        my $plaintext = "A" x $plaintext_len;
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert        my $data = "B" x $block_size; # Explicit IV.
101*e0c4386eSCy Schubert        $data .= $plaintext;
102*e0c4386eSCy Schubert        $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
103*e0c4386eSCy Schubert
104*e0c4386eSCy Schubert        # Add padding.
105*e0c4386eSCy Schubert        for (my $i = 0; $i < 256; $i++) {
106*e0c4386eSCy Schubert            if ($i == $bad_padding_offset) {
107*e0c4386eSCy Schubert                $sent_corrupted_payload = 1;
108*e0c4386eSCy Schubert                $data .= "\xfe";
109*e0c4386eSCy Schubert            } else {
110*e0c4386eSCy Schubert                $data .= "\xff";
111*e0c4386eSCy Schubert            }
112*e0c4386eSCy Schubert        }
113*e0c4386eSCy Schubert
114*e0c4386eSCy Schubert        my $record = TLSProxy::Record->new(
115*e0c4386eSCy Schubert            $proxy->flight,
116*e0c4386eSCy Schubert            TLSProxy::Record::RT_APPLICATION_DATA,
117*e0c4386eSCy Schubert            TLSProxy::Record::VERS_TLS_1_2,
118*e0c4386eSCy Schubert            length($data),
119*e0c4386eSCy Schubert            0,
120*e0c4386eSCy Schubert            length($data),
121*e0c4386eSCy Schubert            $plaintext_len,
122*e0c4386eSCy Schubert            $data,
123*e0c4386eSCy Schubert            $plaintext,
124*e0c4386eSCy Schubert        );
125*e0c4386eSCy Schubert
126*e0c4386eSCy Schubert        # Send the record immediately after the server Finished.
127*e0c4386eSCy Schubert        push @{$proxy->record_list}, $record;
128*e0c4386eSCy Schubert    } elsif ($sent_corrupted_payload) {
129*e0c4386eSCy Schubert        # Check for bad_record_mac from client
130*e0c4386eSCy Schubert        my $last_record = @{$proxy->record_list}[-1];
131*e0c4386eSCy Schubert        $fatal_alert = 1 if $last_record->is_fatal_alert(0) == 20;
132*e0c4386eSCy Schubert    }
133*e0c4386eSCy Schubert}
134