Fixes for no-tls1_2 and no-tls1_2-method
[openssl.git] / test / recipes / 70-test_sslextension.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2018 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)$/;
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 my $no_below_tls13 = alldisabled(("tls1", "tls1_1", "tls1_2"))
30                      || (!disabled("tls1_3") && disabled("tls1_2"));
31
32 use constant {
33     UNSOLICITED_SERVER_NAME => 0,
34     UNSOLICITED_SERVER_NAME_TLS13 => 1,
35     UNSOLICITED_SCT => 2,
36     NONCOMPLIANT_SUPPORTED_GROUPS => 3
37 };
38
39 my $testtype;
40
41 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
42 my $proxy = TLSProxy::Proxy->new(
43     \&inject_duplicate_extension_clienthello,
44     cmdstr(app(["openssl"]), display => 1),
45     srctop_file("apps", "server.pem"),
46     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
47 );
48
49
50 sub extension_filter
51 {
52     my $proxy = shift;
53
54     if ($proxy->flight == 1) {
55         # Change the ServerRandom so that the downgrade sentinel doesn't cause
56         # the connection to fail
57         my $message = ${$proxy->message_list}[1];
58         $message->random("\0"x32);
59         $message->repack();
60         return;
61     }
62
63     # We're only interested in the initial ClientHello
64     if ($proxy->flight != 0) {
65         return;
66     }
67
68     foreach my $message (@{$proxy->message_list}) {
69         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
70             # Remove all extensions and set the extension len to zero
71             $message->extension_data({});
72             $message->extensions_len(0);
73             # Extensions have been removed so make sure we don't try to use them
74             $message->process_extensions();
75
76             $message->repack();
77         }
78     }
79 }
80
81 sub inject_duplicate_extension
82 {
83   my ($proxy, $message_type) = @_;
84
85     foreach my $message (@{$proxy->message_list}) {
86         if ($message->mt == $message_type) {
87           my %extensions = %{$message->extension_data};
88             # Add a duplicate (unknown) extension.
89             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
90             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
91             $message->repack();
92         }
93     }
94 }
95
96 sub inject_duplicate_extension_clienthello
97 {
98     my $proxy = shift;
99
100     # We're only interested in the initial ClientHello
101     if ($proxy->flight != 0) {
102         return;
103     }
104
105     inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
106 }
107
108 sub inject_duplicate_extension_serverhello
109 {
110     my $proxy = shift;
111
112     # We're only interested in the initial ServerHello
113     if ($proxy->flight != 1) {
114         return;
115     }
116
117     inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
118 }
119
120 sub inject_unsolicited_extension
121 {
122     my $proxy = shift;
123     my $message;
124
125     # We're only interested in the initial ServerHello/EncryptedExtensions
126     if ($proxy->flight != 1) {
127         return;
128     }
129
130     if ($testtype == UNSOLICITED_SERVER_NAME_TLS13) {
131         $message = ${$proxy->message_list}[2];
132         die "Expecting EE message ".($message->mt).", ".${$proxy->message_list}[1]->mt.", ".${$proxy->message_list}[3]->mt if $message->mt != TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS;
133     } else {
134         $message = ${$proxy->message_list}[1];
135     }
136
137     my $ext = pack "C2",
138         0x00, 0x00; #Extension length
139
140     my $type;
141     if ($testtype == UNSOLICITED_SERVER_NAME
142             || $testtype == UNSOLICITED_SERVER_NAME_TLS13) {
143         $type = TLSProxy::Message::EXT_SERVER_NAME;
144     } elsif ($testtype == UNSOLICITED_SCT) {
145         $type = TLSProxy::Message::EXT_SCT;
146     } elsif ($testtype == NONCOMPLIANT_SUPPORTED_GROUPS) {
147         $type = TLSProxy::Message::EXT_SUPPORTED_GROUPS;
148     }
149     $message->set_extension($type, $ext);
150     $message->repack();
151 }
152
153 # Test 1-2: Sending a duplicate extension should fail.
154 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
155 plan tests => 7;
156 ok(TLSProxy::Message->fail(), "Duplicate ClientHello extension");
157
158 $proxy->clear();
159 $proxy->filter(\&inject_duplicate_extension_serverhello);
160 $proxy->start();
161 ok(TLSProxy::Message->fail(), "Duplicate ServerHello extension");
162
163 SKIP: {
164     skip "TLS <= 1.2 disabled", 3 if $no_below_tls13;
165
166     #Test 3: Sending a zero length extension block should pass
167     $proxy->clear();
168     $proxy->filter(\&extension_filter);
169     $proxy->start();
170     ok(TLSProxy::Message->success, "Zero extension length test");
171
172     #Test 4: Inject an unsolicited extension (<= TLSv1.2)
173     $proxy->clear();
174     $proxy->filter(\&inject_unsolicited_extension);
175     $testtype = UNSOLICITED_SERVER_NAME;
176     $proxy->clientflags("-no_tls1_3 -noservername");
177     $proxy->start();
178     ok(TLSProxy::Message->fail(), "Unsolicited server name extension");
179
180     #Test 5: Inject a noncompliant supported_groups extension (<= TLSv1.2)
181     $proxy->clear();
182     $proxy->filter(\&inject_unsolicited_extension);
183     $testtype = NONCOMPLIANT_SUPPORTED_GROUPS;
184     $proxy->clientflags("-no_tls1_3");
185     $proxy->start();
186     ok(TLSProxy::Message->success(), "Noncompliant supported_groups extension");
187 }
188
189 SKIP: {
190     skip "TLS <= 1.2 or CT disabled", 1
191         if $no_below_tls13 || disabled("ct");
192     #Test 6: Same as above for the SCT extension which has special handling
193     $proxy->clear();
194     $testtype = UNSOLICITED_SCT;
195     $proxy->clientflags("-no_tls1_3");
196     $proxy->start();
197     ok(TLSProxy::Message->fail(), "Unsolicited sct extension");
198 }
199
200 SKIP: {
201     skip "TLS 1.3 disabled", 1 if disabled("tls1_3");
202     #Test 7: Inject an unsolicited extension (TLSv1.3)
203     $proxy->clear();
204     $proxy->filter(\&inject_unsolicited_extension);
205     $testtype = UNSOLICITED_SERVER_NAME_TLS13;
206     $proxy->clientflags("-noservername");
207     $proxy->start();
208     ok(TLSProxy::Message->fail(), "Unsolicited server name extension (TLSv1.3)");
209 }