2 # Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
13 use File::Temp qw(tempfile);
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,
28 NO_KEY_SHARES_IN_HRR => 12
32 CLIENT_TO_SERVER => 1,
48 my $test_name = "test_key_share";
51 plan skip_all => "TLSProxy isn't usable on $^O"
54 plan skip_all => "$test_name needs the dynamic engine feature enabled"
55 if disabled("engine") || disabled("dynamic-engine");
57 plan skip_all => "$test_name needs the sock feature enabled"
60 plan skip_all => "$test_name needs TLS1.3 enabled"
61 if disabled("tls1_3");
63 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
65 my $proxy = TLSProxy::Proxy->new(
67 cmdstr(app(["openssl"]), display => 1),
68 srctop_file("apps", "server.pem"),
69 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
72 #We assume that test_ssl_new and friends will test the happy path for this,
73 #so we concentrate on the less common scenarios
75 #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
76 $testtype = EMPTY_EXTENSION;
77 $direction = CLIENT_TO_SERVER;
78 $proxy->filter(\&modify_key_shares_filter);
80 $proxy->serverflags("-groups ffdhe3072");
82 $proxy->serverflags("-groups P-256");
84 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
86 ok(TLSProxy::Message->success(), "Success after HRR");
88 #Test 2: The server sending an HRR requesting a group the client already sent
92 ok(TLSProxy::Message->fail(), "Server asks for group already provided");
94 #Test 3: A missing key_shares extension should not succeed
96 $testtype = MISSING_EXTENSION;
98 ok(TLSProxy::Message->fail(), "Missing key_shares extension");
100 #Test 4: No initial acceptable key_shares should succeed after a
103 $proxy->filter(undef);
104 if (disabled("ec")) {
105 $proxy->serverflags("-groups ffdhe3072");
107 $proxy->serverflags("-groups P-256");
110 ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
112 #Test 5: No acceptable key_shares and no shared groups should fail
114 $proxy->filter(undef);
115 if (disabled("ec")) {
116 $proxy->serverflags("-groups ffdhe2048");
118 $proxy->serverflags("-groups P-256");
120 if (disabled("ec")) {
121 $proxy->clientflags("-groups ffdhe3072");
123 $proxy->clientflags("-groups P-384");
126 ok(TLSProxy::Message->fail(), "No acceptable key_shares");
128 #Test 6: A non preferred but acceptable key_share should succeed
130 $proxy->clientflags("-curves P-256");
131 if (disabled("ec")) {
132 $proxy->clientflags("-groups ffdhe3072");
134 $proxy->clientflags("-groups P-256");
137 ok(TLSProxy::Message->success(), "Non preferred key_share");
138 $proxy->filter(\&modify_key_shares_filter);
141 skip "No ec support in this OpenSSL build", 1 if disabled("ec");
143 #Test 7: An acceptable key_share after a list of non-acceptable ones should
146 $testtype = ACCEPTABLE_AT_END;
148 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
151 #Test 8: An acceptable key_share but for a group not in supported_groups should
154 $testtype = NOT_IN_SUPPORTED_GROUPS;
156 ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
158 #Test 9: Too short group_id should fail
160 $testtype = GROUP_ID_TOO_SHORT;
162 ok(TLSProxy::Message->fail(), "Group id too short");
164 #Test 10: key_exchange length mismatch should fail
166 $testtype = KEX_LEN_MISMATCH;
168 ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
170 #Test 11: Zero length key_exchange should fail
172 $testtype = ZERO_LEN_KEX_DATA;
174 ok(TLSProxy::Message->fail(), "zero length key_exchange data");
176 #Test 12: Trailing data on key_share list should fail
178 $testtype = TRAILING_DATA;
180 ok(TLSProxy::Message->fail(), "key_share list trailing data");
182 #Test 13: Multiple acceptable key_shares - we choose the first one
184 $direction = SERVER_TO_CLIENT;
185 $testtype = LOOK_ONLY;
186 $selectedgroupid = 0;
187 if (disabled("ec")) {
188 $proxy->clientflags("-groups ffdhe3072:ffdhe2048");
190 $proxy->clientflags("-groups P-256:X25519");
193 if (disabled("ec")) {
194 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE3072),
195 "Multiple acceptable key_shares");
197 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
198 "Multiple acceptable key_shares");
201 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
203 if (disabled("ec")) {
204 $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
206 $proxy->clientflags("-curves X25519:P-256");
209 if (disabled("ec")) {
210 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE2048),
211 "Multiple acceptable key_shares (part 2)");
213 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
214 "Multiple acceptable key_shares (part 2)");
217 #Test 15: Server sends key_share that wasn't offered should fail
219 $testtype = SELECT_X25519;
220 if (disabled("ec")) {
221 $proxy->clientflags("-groups ffdhe3072");
223 $proxy->clientflags("-groups P-256");
226 ok(TLSProxy::Message->fail(), "Non offered key_share");
228 #Test 16: Too short group_id in ServerHello should fail
230 $testtype = GROUP_ID_TOO_SHORT;
232 ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
234 #Test 17: key_exchange length mismatch in ServerHello should fail
236 $testtype = KEX_LEN_MISMATCH;
238 ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
240 #Test 18: Zero length key_exchange in ServerHello should fail
242 $testtype = ZERO_LEN_KEX_DATA;
244 ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
246 #Test 19: Trailing data on key_share in ServerHello should fail
248 $testtype = TRAILING_DATA;
250 ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
253 skip "No TLSv1.2 support in this OpenSSL build", 2 if disabled("tls1_2");
255 #Test 20: key_share should not be sent if the client is not capable of
256 # negotiating TLSv1.3
258 $proxy->filter(undef);
259 $proxy->clientflags("-no_tls1_3");
261 my $clienthello = $proxy->message_list->[0];
262 ok(TLSProxy::Message->success()
263 && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
264 "No key_share for TLS<=1.2 client");
265 $proxy->filter(\&modify_key_shares_filter);
267 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
268 # process a key_share
270 $direction = CLIENT_TO_SERVER;
271 $testtype = NO_ACCEPTABLE_KEY_SHARES;
272 $proxy->serverflags("-no_tls1_3");
274 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
277 #Test 22: The server sending an HRR but not requesting a new key_share should
280 $direction = SERVER_TO_CLIENT;
281 $testtype = NO_KEY_SHARES_IN_HRR;
282 if (disabled("ec")) {
283 $proxy->serverflags("-groups ffdhe2048");
285 $proxy->serverflags("-groups X25519");
288 ok(TLSProxy::Message->fail(), "Server sends HRR with no key_shares");
290 sub modify_key_shares_filter
294 # We're only interested in the initial ClientHello
295 if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
296 && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
297 || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
301 foreach my $message (@{$proxy->message_list}) {
302 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
303 && $direction == CLIENT_TO_SERVER) {
307 #Setup supported groups to include some unrecognised groups
308 $suppgroups = pack "C8",
309 0x00, 0x06, #List Length
310 0xff, 0xfe, #Non existing group 1
311 0xff, 0xff, #Non existing group 2
314 if ($testtype == EMPTY_EXTENSION) {
317 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
319 0x00, 0x0a, #List Length
320 0xff, 0xfe, #Non existing group 1
321 0x00, 0x01, 0xff, #key_exchange data
322 0xff, 0xff, #Non existing group 2
323 0x00, 0x01, 0xff; #key_exchange data
324 } elsif ($testtype == ACCEPTABLE_AT_END) {
325 $ext = pack "C11H64",
326 0x00, 0x29, #List Length
327 0xff, 0xfe, #Non existing group 1
328 0x00, 0x01, 0xff, #key_exchange data
330 0x00, 0x20, #key_exchange data length
331 "155155B95269ED5C87EAA99C2EF5A593".
332 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
333 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
334 $suppgroups = pack "C4",
335 0x00, 0x02, #List Length
336 0x00, 0xfe; #Non existing group 1
337 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
338 $ext = pack "C6H64C1",
339 0x00, 0x25, #List Length
341 0x00, 0x20, #key_exchange data length
342 "155155B95269ED5C87EAA99C2EF5A593".
343 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
344 0x00; #Group id too short
345 } elsif ($testtype == KEX_LEN_MISMATCH) {
347 0x00, 0x06, #List Length
349 0x00, 0x20, #key_exchange data length
350 0x15, 0x51; #Only two bytes of data, but length should be 32
351 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
352 $ext = pack "C10H64",
353 0x00, 0x28, #List Length
354 0xff, 0xfe, #Non existing group 1
355 0x00, 0x00, #zero length key_exchange data is invalid
357 0x00, 0x20, #key_exchange data length
358 "155155B95269ED5C87EAA99C2EF5A593".
359 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
360 } elsif ($testtype == TRAILING_DATA) {
361 $ext = pack "C6H64C1",
362 0x00, 0x24, #List Length
364 0x00, 0x20, #key_exchange data length
365 "155155B95269ED5C87EAA99C2EF5A593".
366 "EDF83495E80380089F831B94D14B1421", #key_exchange data
367 0x00; #Trailing garbage
368 } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
369 #We trick the server into thinking we sent a P-256 key_share -
370 #but the client actually sent X25519
372 0x00, 0x05, #List Length
374 0x00, 0x01, #key_exchange data length
375 0xff; #Dummy key_share data
378 if ($testtype != EMPTY_EXTENSION
379 && $testtype != NO_KEY_SHARES_IN_HRR) {
380 $message->set_extension(
381 TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
384 if ($testtype == MISSING_EXTENSION) {
385 $message->delete_extension(
386 TLSProxy::Message::EXT_KEY_SHARE);
387 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
388 $message->set_extension(
389 TLSProxy::Message::EXT_KEY_SHARE, $ext);
393 } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
394 && $direction == SERVER_TO_CLIENT) {
397 $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
398 $selectedgroupid = unpack("n", $key_share);
400 if ($testtype == LOOK_ONLY) {
403 if ($testtype == NO_KEY_SHARES_IN_HRR) {
404 $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
405 $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
409 if ($testtype == SELECT_X25519) {
412 0x00, 0x20, #key_exchange data length
413 "155155B95269ED5C87EAA99C2EF5A593".
414 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
415 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
418 } elsif ($testtype == KEX_LEN_MISMATCH) {
421 0x00, 0x20, #key_exchange data length
422 0x15, 0x51; #Only two bytes of data, but length should be 32
423 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
426 0x00, 0x00, #zero length key_exchange data is invalid
427 } elsif ($testtype == TRAILING_DATA) {
428 $ext = pack "C4H64C1",
430 0x00, 0x20, #key_exchange data length
431 "155155B95269ED5C87EAA99C2EF5A593".
432 "EDF83495E80380089F831B94D14B1421", #key_exchange data
433 0x00; #Trailing garbage
435 $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);