22825a096d055c6acf778afc505ca7c5e91feecc
[openssl.git] / test / recipes / 70-test_sslcbcpadding.t
1 #! /usr/bin/env perl
2 # Copyright 2016 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_sslcbcpadding";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS|MSWin32)$/;
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 TLSv1.2 enabled"
27     if disabled("tls1_2");
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     \&add_maximal_padding_filter,
32     cmdstr(app(["openssl"]), display => 1),
33     srctop_file("apps", "server.pem"),
34     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35 );
36
37 # TODO: We could test all 256 values, but then the log file gets too large for
38 # CI. See https://github.com/openssl/openssl/issues/1440.
39 my @test_offsets = (0, 128, 254, 255);
40
41 # Test that maximally-padded records are accepted.
42 my $bad_padding_offset = -1;
43 $proxy->serverflags("-tls1_2");
44 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
45 plan tests => 1 + scalar(@test_offsets);
46 ok(TLSProxy::Message->success(), "Maximally-padded record test");
47
48 # Test that invalid padding is rejected.
49 foreach my $offset (@test_offsets) {
50     $proxy->clear();
51     $bad_padding_offset = $offset;
52     $proxy->start();
53     ok(TLSProxy::Message->fail(), "Invalid padding byte $bad_padding_offset");
54 }
55
56 sub add_maximal_padding_filter
57 {
58     my $proxy = shift;
59
60     if ($proxy->flight == 0) {
61         # Disable Encrypt-then-MAC.
62         foreach my $message (@{$proxy->message_list}) {
63             if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
64                 next;
65             }
66
67             $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
68             $message->process_extensions();
69             $message->repack();
70         }
71     }
72
73     if ($proxy->flight == 3) {
74         # Insert a maximally-padded record. Assume a block size of 16 (AES) and
75         # a MAC length of 20 (SHA-1).
76         my $block_size = 16;
77         my $mac_len = 20;
78
79         # Size the plaintext so that 256 is a valid padding.
80         my $plaintext_len = $block_size - ($mac_len % $block_size);
81         my $plaintext = "A" x $plaintext_len;
82
83         my $data = "B" x $block_size; # Explicit IV.
84         $data .= $plaintext;
85         $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
86
87         # Add padding.
88         for (my $i = 0; $i < 256; $i++) {
89             if ($i == $bad_padding_offset) {
90                 $data .= "\xfe";
91             } else {
92                 $data .= "\xff";
93             }
94         }
95
96         my $record = TLSProxy::Record->new(
97             $proxy->flight,
98             TLSProxy::Record::RT_APPLICATION_DATA,
99             TLSProxy::Record::VERS_TLS_1_2,
100             length($data),
101             0,
102             length($data),
103             $plaintext_len,
104             $data,
105             $plaintext,
106         );
107
108         # Send the record immediately after the server Finished.
109         push @{$proxy->record_list}, $record;
110     }
111 }