2 # Copyright 2015-2021 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 plan skip_all => "$test_name needs EC or DH enabled"
64 if disabled("ec") && disabled("dh");
66 my $proxy = TLSProxy::Proxy->new(
68 cmdstr(app(["openssl"]), display => 1),
69 srctop_file("apps", "server.pem"),
70 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
73 #We assume that test_ssl_new and friends will test the happy path for this,
74 #so we concentrate on the less common scenarios
76 #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
77 $testtype = EMPTY_EXTENSION;
78 $direction = CLIENT_TO_SERVER;
79 $proxy->filter(\&modify_key_shares_filter);
81 $proxy->serverflags("-groups ffdhe3072");
83 $proxy->serverflags("-groups P-256");
85 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
87 ok(TLSProxy::Message->success(), "Success after HRR");
89 #Test 2: The server sending an HRR requesting a group the client already sent
93 ok(TLSProxy::Message->fail(), "Server asks for group already provided");
95 #Test 3: A missing key_shares extension should not succeed
97 $testtype = MISSING_EXTENSION;
99 ok(TLSProxy::Message->fail(), "Missing key_shares extension");
101 #Test 4: No initial acceptable key_shares should succeed after a
104 $proxy->filter(undef);
105 if (disabled("ec")) {
106 $proxy->serverflags("-groups ffdhe3072");
108 $proxy->serverflags("-groups P-256");
111 ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
113 #Test 5: No acceptable key_shares and no shared groups should fail
115 $proxy->filter(undef);
116 if (disabled("ec")) {
117 $proxy->serverflags("-groups ffdhe2048");
119 $proxy->serverflags("-groups P-256");
121 if (disabled("ec")) {
122 $proxy->clientflags("-groups ffdhe3072");
124 $proxy->clientflags("-groups P-384");
127 ok(TLSProxy::Message->fail(), "No acceptable key_shares");
129 #Test 6: A non preferred but acceptable key_share should succeed
131 $proxy->clientflags("-curves P-256");
132 if (disabled("ec")) {
133 $proxy->clientflags("-groups ffdhe3072");
135 $proxy->clientflags("-groups P-256");
138 ok(TLSProxy::Message->success(), "Non preferred key_share");
139 $proxy->filter(\&modify_key_shares_filter);
142 skip "No ec support in this OpenSSL build", 1 if disabled("ec");
144 #Test 7: An acceptable key_share after a list of non-acceptable ones should
147 $testtype = ACCEPTABLE_AT_END;
149 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
152 #Test 8: An acceptable key_share but for a group not in supported_groups should
155 $testtype = NOT_IN_SUPPORTED_GROUPS;
157 ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
159 #Test 9: Too short group_id should fail
161 $testtype = GROUP_ID_TOO_SHORT;
163 ok(TLSProxy::Message->fail(), "Group id too short");
165 #Test 10: key_exchange length mismatch should fail
167 $testtype = KEX_LEN_MISMATCH;
169 ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
171 #Test 11: Zero length key_exchange should fail
173 $testtype = ZERO_LEN_KEX_DATA;
175 ok(TLSProxy::Message->fail(), "zero length key_exchange data");
177 #Test 12: Trailing data on key_share list should fail
179 $testtype = TRAILING_DATA;
181 ok(TLSProxy::Message->fail(), "key_share list trailing data");
183 #Test 13: Multiple acceptable key_shares - we choose the first one
185 $direction = SERVER_TO_CLIENT;
186 $testtype = LOOK_ONLY;
187 $selectedgroupid = 0;
188 if (disabled("ec")) {
189 $proxy->clientflags("-groups ffdhe3072:ffdhe2048");
191 $proxy->clientflags("-groups P-256:X25519");
194 if (disabled("ec")) {
195 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE3072),
196 "Multiple acceptable key_shares");
198 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
199 "Multiple acceptable key_shares");
202 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
204 if (disabled("ec")) {
205 $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
207 $proxy->clientflags("-curves X25519:P-256");
210 if (disabled("ec")) {
211 ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE2048),
212 "Multiple acceptable key_shares (part 2)");
214 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
215 "Multiple acceptable key_shares (part 2)");
218 #Test 15: Server sends key_share that wasn't offered should fail
220 $testtype = SELECT_X25519;
221 if (disabled("ec")) {
222 $proxy->clientflags("-groups ffdhe3072");
224 $proxy->clientflags("-groups P-256");
227 ok(TLSProxy::Message->fail(), "Non offered key_share");
229 #Test 16: Too short group_id in ServerHello should fail
231 $testtype = GROUP_ID_TOO_SHORT;
233 ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
235 #Test 17: key_exchange length mismatch in ServerHello should fail
237 $testtype = KEX_LEN_MISMATCH;
239 ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
241 #Test 18: Zero length key_exchange in ServerHello should fail
243 $testtype = ZERO_LEN_KEX_DATA;
245 ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
247 #Test 19: Trailing data on key_share in ServerHello should fail
249 $testtype = TRAILING_DATA;
251 ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
254 skip "No TLSv1.2 support in this OpenSSL build", 2 if disabled("tls1_2");
256 #Test 20: key_share should not be sent if the client is not capable of
257 # negotiating TLSv1.3
259 $proxy->filter(undef);
260 $proxy->clientflags("-no_tls1_3");
262 my $clienthello = $proxy->message_list->[0];
263 ok(TLSProxy::Message->success()
264 && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
265 "No key_share for TLS<=1.2 client");
266 $proxy->filter(\&modify_key_shares_filter);
268 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
269 # process a key_share
271 $direction = CLIENT_TO_SERVER;
272 $testtype = NO_ACCEPTABLE_KEY_SHARES;
273 $proxy->serverflags("-no_tls1_3");
275 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
278 #Test 22: The server sending an HRR but not requesting a new key_share should
281 $direction = SERVER_TO_CLIENT;
282 $testtype = NO_KEY_SHARES_IN_HRR;
283 if (disabled("ec")) {
284 $proxy->serverflags("-groups ffdhe2048");
286 $proxy->serverflags("-groups X25519");
289 ok(TLSProxy::Message->fail(), "Server sends HRR with no key_shares");
291 sub modify_key_shares_filter
295 # We're only interested in the initial ClientHello
296 if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
297 && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
298 || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
302 foreach my $message (@{$proxy->message_list}) {
303 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
304 && $direction == CLIENT_TO_SERVER) {
308 #Setup supported groups to include some unrecognised groups
309 $suppgroups = pack "C8",
310 0x00, 0x06, #List Length
311 0xff, 0xfe, #Non existing group 1
312 0xff, 0xff, #Non existing group 2
315 if ($testtype == EMPTY_EXTENSION) {
318 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
320 0x00, 0x0a, #List Length
321 0xff, 0xfe, #Non existing group 1
322 0x00, 0x01, 0xff, #key_exchange data
323 0xff, 0xff, #Non existing group 2
324 0x00, 0x01, 0xff; #key_exchange data
325 } elsif ($testtype == ACCEPTABLE_AT_END) {
326 $ext = pack "C11H64",
327 0x00, 0x29, #List Length
328 0xff, 0xfe, #Non existing group 1
329 0x00, 0x01, 0xff, #key_exchange data
331 0x00, 0x20, #key_exchange data length
332 "155155B95269ED5C87EAA99C2EF5A593".
333 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
334 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
335 $suppgroups = pack "C4",
336 0x00, 0x02, #List Length
337 0x00, 0xfe; #Non existing group 1
338 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
339 $ext = pack "C6H64C1",
340 0x00, 0x25, #List Length
342 0x00, 0x20, #key_exchange data length
343 "155155B95269ED5C87EAA99C2EF5A593".
344 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
345 0x00; #Group id too short
346 } elsif ($testtype == KEX_LEN_MISMATCH) {
348 0x00, 0x06, #List Length
350 0x00, 0x20, #key_exchange data length
351 0x15, 0x51; #Only two bytes of data, but length should be 32
352 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
353 $ext = pack "C10H64",
354 0x00, 0x28, #List Length
355 0xff, 0xfe, #Non existing group 1
356 0x00, 0x00, #zero length key_exchange data is invalid
358 0x00, 0x20, #key_exchange data length
359 "155155B95269ED5C87EAA99C2EF5A593".
360 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
361 } elsif ($testtype == TRAILING_DATA) {
362 $ext = pack "C6H64C1",
363 0x00, 0x24, #List Length
365 0x00, 0x20, #key_exchange data length
366 "155155B95269ED5C87EAA99C2EF5A593".
367 "EDF83495E80380089F831B94D14B1421", #key_exchange data
368 0x00; #Trailing garbage
369 } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
370 #We trick the server into thinking we sent a P-256 key_share -
371 #but the client actually sent X25519
373 0x00, 0x05, #List Length
375 0x00, 0x01, #key_exchange data length
376 0xff; #Dummy key_share data
379 if ($testtype != EMPTY_EXTENSION
380 && $testtype != NO_KEY_SHARES_IN_HRR) {
381 $message->set_extension(
382 TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
385 if ($testtype == MISSING_EXTENSION) {
386 $message->delete_extension(
387 TLSProxy::Message::EXT_KEY_SHARE);
388 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
389 $message->set_extension(
390 TLSProxy::Message::EXT_KEY_SHARE, $ext);
394 } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
395 && $direction == SERVER_TO_CLIENT) {
398 $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
399 $selectedgroupid = unpack("n", $key_share);
401 if ($testtype == LOOK_ONLY) {
404 if ($testtype == NO_KEY_SHARES_IN_HRR) {
405 $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
406 $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
410 if ($testtype == SELECT_X25519) {
413 0x00, 0x20, #key_exchange data length
414 "155155B95269ED5C87EAA99C2EF5A593".
415 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
416 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
419 } elsif ($testtype == KEX_LEN_MISMATCH) {
422 0x00, 0x20, #key_exchange data length
423 0x15, 0x51; #Only two bytes of data, but length should be 32
424 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
427 0x00, 0x00, #zero length key_exchange data is invalid
428 } elsif ($testtype == TRAILING_DATA) {
429 $ext = pack "C4H64C1",
431 0x00, 0x20, #key_exchange data length
432 "155155B95269ED5C87EAA99C2EF5A593".
433 "EDF83495E80380089F831B94D14B1421", #key_exchange data
434 0x00; #Trailing garbage
436 $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);