c6eca2f713fd9c2eb9629f057f278ad8390d803b
[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 plan tests => 1 + 256;
38
39 my $bad_padding_offset = -1;
40
41 # Test 1: Maximally-padded records are accepted.
42 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
43 ok(TLSProxy::Message->success(), "Maximally-padded record test");
44
45 # Tests 2 through 257: Invalid padding.
46 for ($bad_padding_offset = 0; $bad_padding_offset < 256;
47      $bad_padding_offset++) {
48     $proxy->clear();
49     $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";;
50     ok(TLSProxy::Message->fail(), "Invalid padding byte $bad_padding_offset");
51 }
52
53 sub add_maximal_padding_filter
54 {
55     my $proxy = shift;
56
57     if ($proxy->flight == 0) {
58         # Disable Encrypt-then-MAC.
59         foreach my $message (@{$proxy->message_list}) {
60             if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
61                 next;
62             }
63
64             $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
65             $message->process_extensions();
66             $message->repack();
67         }
68     }
69
70     if ($proxy->flight == 3) {
71         # Insert a maximally-padded record. Assume a block size of 16 (AES) and
72         # a MAC length of 20 (SHA-1).
73         my $block_size = 16;
74         my $mac_len = 20;
75
76         # Size the plaintext so that 256 is a valid padding.
77         my $plaintext_len = $block_size - ($mac_len % $block_size);
78         my $plaintext = "A" x $plaintext_len;
79
80         my $data = "B" x $block_size; # Explicit IV.
81         $data .= $plaintext;
82         $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
83
84         # Add padding.
85         for (my $i = 0; $i < 256; $i++) {
86             if ($i == $bad_padding_offset) {
87                 $data .= "\xfe";
88             } else {
89                 $data .= "\xff";
90             }
91         }
92
93         my $record = TLSProxy::Record->new(
94             $proxy->flight,
95             TLSProxy::Record::RT_APPLICATION_DATA,
96             TLSProxy::Record::VERS_TLS_1_2,
97             length($data),
98             length($data),
99             $plaintext_len,
100             $data,
101             $plaintext,
102         );
103
104         # Send the record immediately after the server Finished.
105         push @{$proxy->record_list}, $record;
106     }
107 }