Test CBC mode padding.
authorDavid Benjamin <davidben@google.com>
Wed, 10 Aug 2016 04:45:51 +0000 (00:45 -0400)
committerMatt Caswell <matt@openssl.org>
Mon, 26 Sep 2016 22:10:29 +0000 (23:10 +0100)
This is a regression test for
https://github.com/openssl/openssl/pull/1431. It tests a
maximally-padded record with each possible invalid offset.

This required fixing a bug in Message.pm where the client sending a
fatal alert followed by close_notify was still treated as success.

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
test/recipes/70-test_sslcbcpadding.t [new file with mode: 0644]
util/TLSProxy/Message.pm
util/TLSProxy/Proxy.pm

diff --git a/test/recipes/70-test_sslcbcpadding.t b/test/recipes/70-test_sslcbcpadding.t
new file mode 100644 (file)
index 0000000..c6eca2f
--- /dev/null
@@ -0,0 +1,107 @@
+#! /usr/bin/env perl
+# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the OpenSSL license (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+use strict;
+use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
+use OpenSSL::Test::Utils;
+use TLSProxy::Proxy;
+
+my $test_name = "test_sslcbcpadding";
+setup($test_name);
+
+plan skip_all => "TLSProxy isn't usable on $^O"
+    if $^O =~ /^(VMS|MSWin32)$/;
+
+plan skip_all => "$test_name needs the dynamic engine feature enabled"
+    if disabled("engine") || disabled("dynamic-engine");
+
+plan skip_all => "$test_name needs the sock feature enabled"
+    if disabled("sock");
+
+plan skip_all => "$test_name needs TLSv1.2 enabled"
+    if disabled("tls1_2");
+
+$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
+my $proxy = TLSProxy::Proxy->new(
+    \&add_maximal_padding_filter,
+    cmdstr(app(["openssl"]), display => 1),
+    srctop_file("apps", "server.pem"),
+    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
+);
+
+plan tests => 1 + 256;
+
+my $bad_padding_offset = -1;
+
+# Test 1: Maximally-padded records are accepted.
+$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
+ok(TLSProxy::Message->success(), "Maximally-padded record test");
+
+# Tests 2 through 257: Invalid padding.
+for ($bad_padding_offset = 0; $bad_padding_offset < 256;
+     $bad_padding_offset++) {
+    $proxy->clear();
+    $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";;
+    ok(TLSProxy::Message->fail(), "Invalid padding byte $bad_padding_offset");
+}
+
+sub add_maximal_padding_filter
+{
+    my $proxy = shift;
+
+    if ($proxy->flight == 0) {
+        # Disable Encrypt-then-MAC.
+        foreach my $message (@{$proxy->message_list}) {
+            if ($message->mt != TLSProxy::Message::MT_CLIENT_HELLO) {
+                next;
+            }
+
+            $message->delete_extension(TLSProxy::Message::EXT_ENCRYPT_THEN_MAC);
+            $message->process_extensions();
+            $message->repack();
+        }
+    }
+
+    if ($proxy->flight == 3) {
+        # Insert a maximally-padded record. Assume a block size of 16 (AES) and
+        # a MAC length of 20 (SHA-1).
+        my $block_size = 16;
+        my $mac_len = 20;
+
+        # Size the plaintext so that 256 is a valid padding.
+        my $plaintext_len = $block_size - ($mac_len % $block_size);
+        my $plaintext = "A" x $plaintext_len;
+
+        my $data = "B" x $block_size; # Explicit IV.
+        $data .= $plaintext;
+        $data .= TLSProxy::Proxy::fill_known_data($mac_len); # MAC.
+
+        # Add padding.
+        for (my $i = 0; $i < 256; $i++) {
+            if ($i == $bad_padding_offset) {
+                $data .= "\xfe";
+            } else {
+                $data .= "\xff";
+            }
+        }
+
+        my $record = TLSProxy::Record->new(
+            $proxy->flight,
+            TLSProxy::Record::RT_APPLICATION_DATA,
+            TLSProxy::Record::VERS_TLS_1_2,
+            length($data),
+            length($data),
+            $plaintext_len,
+            $data,
+            $plaintext,
+        );
+
+        # Send the record immediately after the server Finished.
+        push @{$proxy->record_list}, $record;
+    }
+}
index 321e080ea32cc7a487cc4258ad7d4c82175fee09..1810d8c30ecde0bd9012431eb067124e2e170eca 100644 (file)
@@ -199,14 +199,14 @@ sub get_messages
         print "  [".$record->decrypt_data."]\n";
     } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
         my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
-        #All alerts end the test
-        $end = 1;
         #A CloseNotify from the client indicates we have finished successfully
         #(we assume)
-        if (!$server && $alertlev == AL_LEVEL_WARN
+        if (!$end && !$server && $alertlev == AL_LEVEL_WARN
             && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
             $success = 1;
         }
+        #All alerts end the test
+        $end = 1;
     }
 
     return @messages;
index e0ce43aa7793f647bd964546645b1d97f0d2aa2b..eeb83ed74f88950c1f67dabde9276fe5993a8eda 100644 (file)
@@ -493,4 +493,15 @@ sub serverpid
     }
     return $self->{serverpid};
 }
+
+sub fill_known_data
+{
+    my $length = shift;
+    my $ret = "";
+    for (my $i = 0; $i < $length; $i++) {
+        $ret .= chr($i);
+    }
+    return $ret;
+}
+
 1;