Add a test for the TLSv1.3 downgrade mechanism
[openssl.git] / test / recipes / 70-test_sslextension.t
1 #! /usr/bin/env perl
2 # Copyright 2015-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_sslextension";
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 TLS enabled"
27     if alldisabled(available_protocols("tls"));
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     \&extension_filter,
32     cmdstr(app(["openssl"]), display => 1),
33     srctop_file("apps", "server.pem"),
34     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35 );
36
37 # Test 1: Sending a zero length extension block should pass
38 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
39 plan tests => 3;
40 ok(TLSProxy::Message->success, "Zero extension length test");
41
42 sub extension_filter
43 {
44     my $proxy = shift;
45
46     if ($proxy->flight == 1) {
47         # Change the ServerRandom so that the downgrade sentinel doesn't cause
48         # the connection to fail
49         my $message = ${$proxy->message_list}[1];
50         $message->random("\0"x32);
51         $message->repack();
52         return;
53     }
54
55     # We're only interested in the initial ClientHello
56     if ($proxy->flight != 0) {
57         return;
58     }
59
60     foreach my $message (@{$proxy->message_list}) {
61         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
62             # Remove all extensions and set the extension len to zero
63             $message->extension_data({});
64             $message->extensions_len(0);
65             # Extensions have been removed so make sure we don't try to use them
66             $message->process_extensions();
67
68             $message->repack();
69         }
70     }
71 }
72
73 # Test 2-3: Sending a duplicate extension should fail.
74 sub inject_duplicate_extension
75 {
76   my ($proxy, $message_type) = @_;
77
78     foreach my $message (@{$proxy->message_list}) {
79         if ($message->mt == $message_type) {
80           my %extensions = %{$message->extension_data};
81             # Add a duplicate (unknown) extension.
82             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
83             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
84             $message->repack();
85         }
86     }
87 }
88
89 sub inject_duplicate_extension_clienthello
90 {
91     my $proxy = shift;
92
93     # We're only interested in the initial ClientHello
94     if ($proxy->flight != 0) {
95         return;
96     }
97
98     inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
99 }
100
101 sub inject_duplicate_extension_serverhello
102 {
103     my $proxy = shift;
104
105     # We're only interested in the initial ServerHello
106     if ($proxy->flight != 1) {
107         return;
108     }
109
110     inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
111 }
112
113 $proxy->clear();
114 $proxy->filter(\&inject_duplicate_extension_clienthello);
115 $proxy->start();
116 ok(TLSProxy::Message->fail(), "Duplicate ClientHello extension");
117
118 $proxy->clear();
119 $proxy->filter(\&inject_duplicate_extension_serverhello);
120 $proxy->start();
121 ok(TLSProxy::Message->fail(), "Duplicate ServerHello extension");