Fix a test failure with no-tls1_1
[openssl.git] / test / recipes / 70-test_key_share.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 use File::Temp qw(tempfile);
14
15 use constant {
16     LOOK_ONLY => 0,
17     EMPTY_EXTENSION => 1,
18     MISSING_EXTENSION => 2,
19     NO_ACCEPTABLE_KEY_SHARES => 3,
20     NON_PREFERRED_KEY_SHARE => 4,
21     ACCEPTABLE_AT_END => 5,
22     NOT_IN_SUPPORTED_GROUPS => 6,
23     GROUP_ID_TOO_SHORT => 7,
24     KEX_LEN_MISMATCH => 8,
25     ZERO_LEN_KEX_DATA => 9,
26     TRAILING_DATA => 10,
27     SELECT_X25519 => 11
28 };
29
30 use constant {
31     CLIENT_TO_SERVER => 1,
32     SERVER_TO_CLIENT => 2
33 };
34
35
36 use constant {
37     X25519 => 0x1d,
38     P_256 => 0x17
39 };
40
41 my $testtype;
42 my $direction;
43 my $selectedgroupid;
44
45 my $test_name = "test_key_share";
46 setup($test_name);
47
48 plan skip_all => "TLSProxy isn't usable on $^O"
49     if $^O =~ /^(VMS|MSWin32)$/;
50
51 plan skip_all => "$test_name needs the dynamic engine feature enabled"
52     if disabled("engine") || disabled("dynamic-engine");
53
54 plan skip_all => "$test_name needs the sock feature enabled"
55     if disabled("sock");
56
57 plan skip_all => "$test_name needs TLS1.3 enabled"
58     if disabled("tls1_3");
59
60 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
61
62 my $proxy = TLSProxy::Proxy->new(
63     undef,
64     cmdstr(app(["openssl"]), display => 1),
65     srctop_file("apps", "server.pem"),
66     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
67 );
68
69 #We assume that test_ssl_new and friends will test the happy path for this,
70 #so we concentrate on the less common scenarios
71
72 #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
73 $testtype = EMPTY_EXTENSION;
74 $direction = CLIENT_TO_SERVER;
75 $proxy->filter(\&modify_key_shares_filter);
76 $proxy->serverflags("-curves P-256");
77 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
78 plan tests => 21;
79 ok(TLSProxy::Message->success(), "Success after HRR");
80
81 #Test 2: The server sending an HRR requesting a group the client already sent
82 #        should fail
83 $proxy->clear();
84 $proxy->start();
85 ok(TLSProxy::Message->fail(), "Server asks for group already provided");
86
87 #Test 3: A missing key_shares extension should not succeed
88 $proxy->clear();
89 $testtype = MISSING_EXTENSION;
90 $proxy->start();
91 ok(TLSProxy::Message->fail(), "Missing key_shares extension");
92
93 #Test 4: No initial acceptable key_shares should succeed after a
94 #        HelloRetryRequest
95 $proxy->clear();
96 $proxy->filter(undef);
97 $proxy->serverflags("-curves P-256");
98 $proxy->start();
99 ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
100
101 #Test 5: No acceptable key_shares and no shared groups should fail
102 $proxy->clear();
103 $proxy->filter(undef);
104 $proxy->serverflags("-curves P-256");
105 $proxy->clientflags("-curves P-384");
106 $proxy->start();
107 ok(TLSProxy::Message->fail(), "No acceptable key_shares");
108
109 #Test 6: A non preferred but acceptable key_share should succeed
110 $proxy->clear();
111 $proxy->clientflags("-curves P-256");
112 $proxy->start();
113 ok(TLSProxy::Message->success(), "Non preferred key_share");
114 $proxy->filter(\&modify_key_shares_filter);
115
116 #Test 7: An acceptable key_share after a list of non-acceptable ones should
117 #succeed
118 $proxy->clear();
119 $testtype = ACCEPTABLE_AT_END;
120 $proxy->start();
121 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
122
123 #Test 8: An acceptable key_share but for a group not in supported_groups should
124 #fail
125 $proxy->clear();
126 $testtype = NOT_IN_SUPPORTED_GROUPS;
127 $proxy->start();
128 ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
129
130 #Test 9: Too short group_id should fail
131 $proxy->clear();
132 $testtype = GROUP_ID_TOO_SHORT;
133 $proxy->start();
134 ok(TLSProxy::Message->fail(), "Group id too short");
135
136 #Test 10: key_exchange length mismatch should fail
137 $proxy->clear();
138 $testtype = KEX_LEN_MISMATCH;
139 $proxy->start();
140 ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
141
142 #Test 11: Zero length key_exchange should fail
143 $proxy->clear();
144 $testtype = ZERO_LEN_KEX_DATA;
145 $proxy->start();
146 ok(TLSProxy::Message->fail(), "zero length key_exchange data");
147
148 #Test 12: Trailing data on key_share list should fail
149 $proxy->clear();
150 $testtype = TRAILING_DATA;
151 $proxy->start();
152 ok(TLSProxy::Message->fail(), "key_share list trailing data");
153
154 #Test 13: Multiple acceptable key_shares - we choose the first one
155 $proxy->clear();
156 $direction = SERVER_TO_CLIENT;
157 $testtype = LOOK_ONLY;
158 $proxy->clientflags("-curves P-256:X25519");
159 $proxy->start();
160 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
161    "Multiple acceptable key_shares");
162
163 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
164 $proxy->clear();
165 $proxy->clientflags("-curves X25519:P-256");
166 $proxy->start();
167 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
168    "Multiple acceptable key_shares (part 2)");
169
170 #Test 15: Server sends key_share that wasn't offerred should fail
171 $proxy->clear();
172 $testtype = SELECT_X25519;
173 $proxy->clientflags("-curves P-256");
174 $proxy->start();
175 ok(TLSProxy::Message->fail(), "Non offered key_share");
176
177 #Test 16: Too short group_id in ServerHello should fail
178 $proxy->clear();
179 $testtype = GROUP_ID_TOO_SHORT;
180 $proxy->start();
181 ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
182
183 #Test 17: key_exchange length mismatch in ServerHello should fail
184 $proxy->clear();
185 $testtype = KEX_LEN_MISMATCH;
186 $proxy->start();
187 ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
188
189 #Test 18: Zero length key_exchange in ServerHello should fail
190 $proxy->clear();
191 $testtype = ZERO_LEN_KEX_DATA;
192 $proxy->start();
193 ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
194
195 #Test 19: Trailing data on key_share in ServerHello should fail
196 $proxy->clear();
197 $testtype = TRAILING_DATA;
198 $proxy->start();
199 ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
200
201 #Test 20: key_share should not be sent if the client is not capable of
202 #         negotiating TLSv1.3
203 $proxy->clear();
204 $proxy->filter(undef);
205 $proxy->clientflags("-no_tls1_3");
206 $proxy->start();
207 my $clienthello = $proxy->message_list->[0];
208 ok(TLSProxy::Message->success()
209    && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
210    "No key_share for TLS<=1.2 client");
211 $proxy->filter(\&modify_key_shares_filter);
212
213 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
214 #         process a key_share
215 $proxy->clear();
216 $direction = CLIENT_TO_SERVER;
217 $testtype = NO_ACCEPTABLE_KEY_SHARES;
218 $proxy->serverflags("-no_tls1_3");
219 $proxy->start();
220 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
221
222 sub modify_key_shares_filter
223 {
224     my $proxy = shift;
225
226     # We're only interested in the initial ClientHello
227     if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0)
228             || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
229         return;
230     }
231
232     foreach my $message (@{$proxy->message_list}) {
233         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
234                 && $direction == CLIENT_TO_SERVER) {
235             my $ext;
236             my $suppgroups;
237
238             #Setup supported groups to include some unrecognised groups
239             $suppgroups = pack "C8",
240                 0x00, 0x06, #List Length
241                 0xff, 0xfe, #Non existing group 1
242                 0xff, 0xff, #Non existing group 2
243                 0x00, 0x1d; #x25519
244
245             if ($testtype == EMPTY_EXTENSION) {
246                 $ext = pack "C2",
247                     0x00, 0x00;
248             } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
249                 $ext = pack "C12",
250                     0x00, 0x0a, #List Length
251                     0xff, 0xfe, #Non existing group 1
252                     0x00, 0x01, 0xff, #key_exchange data
253                     0xff, 0xff, #Non existing group 2
254                     0x00, 0x01, 0xff; #key_exchange data
255             } elsif ($testtype == ACCEPTABLE_AT_END) {
256                 $ext = pack "C11H64",
257                     0x00, 0x29, #List Length
258                     0xff, 0xfe, #Non existing group 1
259                     0x00, 0x01, 0xff, #key_exchange data
260                     0x00, 0x1d, #x25519
261                     0x00, 0x20, #key_exchange data length
262                     "155155B95269ED5C87EAA99C2EF5A593".
263                     "EDF83495E80380089F831B94D14B1421";  #key_exchange data
264             } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
265                 $suppgroups = pack "C4",
266                     0x00, 0x02, #List Length
267                     0x00, 0xfe; #Non existing group 1
268             } elsif ($testtype == GROUP_ID_TOO_SHORT) {
269                 $ext = pack "C6H64C1",
270                     0x00, 0x25, #List Length
271                     0x00, 0x1d, #x25519
272                     0x00, 0x20, #key_exchange data length
273                     "155155B95269ED5C87EAA99C2EF5A593".
274                     "EDF83495E80380089F831B94D14B1421";  #key_exchange data
275                     0x00;       #Group id too short
276             } elsif ($testtype == KEX_LEN_MISMATCH) {
277                 $ext = pack "C8",
278                     0x00, 0x06, #List Length
279                     0x00, 0x1d, #x25519
280                     0x00, 0x20, #key_exchange data length
281                     0x15, 0x51; #Only two bytes of data, but length should be 32
282             } elsif ($testtype == ZERO_LEN_KEX_DATA) {
283                 $ext = pack "C10H64",
284                     0x00, 0x28, #List Length
285                     0xff, 0xfe, #Non existing group 1
286                     0x00, 0x00, #zero length key_exchange data is invalid
287                     0x00, 0x1d, #x25519
288                     0x00, 0x20, #key_exchange data length
289                     "155155B95269ED5C87EAA99C2EF5A593".
290                     "EDF83495E80380089F831B94D14B1421";  #key_exchange data
291             } elsif ($testtype == TRAILING_DATA) {
292                 $ext = pack "C6H64C1",
293                     0x00, 0x24, #List Length
294                     0x00, 0x1d, #x25519
295                     0x00, 0x20, #key_exchange data length
296                     "155155B95269ED5C87EAA99C2EF5A593".
297                     "EDF83495E80380089F831B94D14B1421", #key_exchange data
298                     0x00; #Trailing garbage
299             }
300
301             if ($testtype != EMPTY_EXTENSION) {
302                 $message->set_extension(
303                     TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
304             }
305
306             if ($testtype == MISSING_EXTENSION) {
307                 $message->delete_extension(
308                     TLSProxy::Message::EXT_KEY_SHARE);
309             } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
310                 $message->set_extension(
311                     TLSProxy::Message::EXT_KEY_SHARE, $ext);
312             }
313
314             $message->repack();
315         } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
316                      && $direction == SERVER_TO_CLIENT) {
317             my $ext;
318             my $key_share =
319                 $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
320             $selectedgroupid = unpack("n", $key_share);
321
322             if ($testtype == LOOK_ONLY) {
323                 return;
324             }
325             if ($testtype == SELECT_X25519) {
326                 $ext = pack "C4H64",
327                     0x00, 0x1d, #x25519
328                     0x00, 0x20, #key_exchange data length
329                     "155155B95269ED5C87EAA99C2EF5A593".
330                     "EDF83495E80380089F831B94D14B1421";  #key_exchange data
331             } elsif ($testtype == GROUP_ID_TOO_SHORT) {
332                 $ext = pack "C1",
333                     0x00;
334             } elsif ($testtype == KEX_LEN_MISMATCH) {
335                 $ext = pack "C6",
336                     0x00, 0x1d, #x25519
337                     0x00, 0x20, #key_exchange data length
338                     0x15, 0x51; #Only two bytes of data, but length should be 32
339             } elsif ($testtype == ZERO_LEN_KEX_DATA) {
340                 $ext = pack "C4",
341                     0x00, 0x1d, #x25519
342                     0x00, 0x00, #zero length key_exchange data is invalid
343             } elsif ($testtype == TRAILING_DATA) {
344                 $ext = pack "C4H64C1",
345                     0x00, 0x1d, #x25519
346                     0x00, 0x20, #key_exchange data length
347                     "155155B95269ED5C87EAA99C2EF5A593".
348                     "EDF83495E80380089F831B94D14B1421", #key_exchange data
349                     0x00; #Trailing garbage
350             }
351             $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
352
353             $message->repack();
354         }
355     }
356 }
357
358