]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/recipes/70-test_key_share.t
2 # Copyright 2015-2023 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,
29 NON_TLS1_3_KEY_SHARE
=> 13
33 CLIENT_TO_SERVER
=> 1,
49 my $test_name = "test_key_share";
52 plan skip_all
=> "TLSProxy isn't usable on $^O"
55 plan skip_all
=> "$test_name needs the dynamic engine feature enabled"
56 if disabled
("engine") || disabled
("dynamic-engine");
58 plan skip_all
=> "$test_name needs the sock feature enabled"
61 plan skip_all
=> "$test_name needs TLS1.3 enabled"
62 if disabled
("tls1_3");
64 plan skip_all
=> "$test_name needs EC or DH enabled"
65 if disabled
("ec") && disabled
("dh");
67 my $proxy = TLSProxy
::Proxy
->new(
69 cmdstr
(app
(["openssl"]), display
=> 1),
70 srctop_file
("apps", "server.pem"),
71 (!$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
})
74 #We assume that test_ssl_new and friends will test the happy path for this,
75 #so we concentrate on the less common scenarios
77 #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
78 $testtype = EMPTY_EXTENSION
;
79 $direction = CLIENT_TO_SERVER
;
80 $proxy->filter(\
&modify_key_shares_filter
);
82 $proxy->serverflags("-groups ffdhe3072");
84 $proxy->serverflags("-groups P-384");
86 $proxy->start() or plan skip_all
=> "Unable to start up Proxy for tests";
88 ok
(TLSProxy
::Message
->success(), "Success after HRR");
90 #Test 2: The server sending an HRR requesting a group the client already sent
94 ok
(TLSProxy
::Message
->fail(), "Server asks for group already provided");
96 #Test 3: A missing key_shares extension should not succeed
98 $testtype = MISSING_EXTENSION
;
100 ok
(TLSProxy
::Message
->fail(), "Missing key_shares extension");
102 #Test 4: No initial acceptable key_shares should succeed after a
105 $proxy->filter(undef);
106 if (disabled
("ec")) {
107 $proxy->serverflags("-groups ffdhe3072");
109 $proxy->serverflags("-groups P-256");
112 ok
(TLSProxy
::Message
->success(), "No initial acceptable key_shares");
114 #Test 5: No acceptable key_shares and no shared groups should fail
116 $proxy->filter(undef);
117 if (disabled
("ec")) {
118 $proxy->serverflags("-groups ffdhe2048");
120 $proxy->serverflags("-groups P-256");
122 if (disabled
("ec")) {
123 $proxy->clientflags("-groups ffdhe3072");
125 $proxy->clientflags("-groups P-384");
128 ok
(TLSProxy
::Message
->fail(), "No acceptable key_shares");
130 #Test 6: A non preferred but acceptable key_share should succeed
132 $proxy->clientflags("-curves P-256");
133 if (disabled
("ec")) {
134 $proxy->clientflags("-groups ffdhe3072");
136 $proxy->clientflags("-groups P-256");
139 ok
(TLSProxy
::Message
->success(), "Non preferred key_share");
140 $proxy->filter(\
&modify_key_shares_filter
);
143 skip
"No ec support in this OpenSSL build", 1 if disabled
("ec");
145 #Test 7: An acceptable key_share after a list of non-acceptable ones should
148 $testtype = ACCEPTABLE_AT_END
;
150 ok
(TLSProxy
::Message
->success(), "Acceptable key_share at end of list");
153 #Test 8: An acceptable key_share but for a group not in supported_groups should
156 $testtype = NOT_IN_SUPPORTED_GROUPS
;
158 ok
(TLSProxy
::Message
->fail(), "Acceptable key_share not in supported_groups");
160 #Test 9: Too short group_id should fail
162 $testtype = GROUP_ID_TOO_SHORT
;
164 ok
(TLSProxy
::Message
->fail(), "Group id too short");
166 #Test 10: key_exchange length mismatch should fail
168 $testtype = KEX_LEN_MISMATCH
;
170 ok
(TLSProxy
::Message
->fail(), "key_exchange length mismatch");
172 #Test 11: Zero length key_exchange should fail
174 $testtype = ZERO_LEN_KEX_DATA
;
176 ok
(TLSProxy
::Message
->fail(), "zero length key_exchange data");
178 #Test 12: Trailing data on key_share list should fail
180 $testtype = TRAILING_DATA
;
182 ok
(TLSProxy
::Message
->fail(), "key_share list trailing data");
184 #Test 13: Multiple acceptable key_shares - we choose the first one
186 $direction = SERVER_TO_CLIENT
;
187 $testtype = LOOK_ONLY
;
188 $selectedgroupid = 0;
189 if (disabled
("ec")) {
190 $proxy->clientflags("-groups ffdhe3072:ffdhe2048");
192 $proxy->clientflags("-groups P-256:P-384");
195 if (disabled
("ec")) {
196 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == FFDHE3072
),
197 "Multiple acceptable key_shares");
199 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == P_256
),
200 "Multiple acceptable key_shares");
203 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
205 if (disabled
("ecx")) {
206 $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
208 $proxy->clientflags("-curves X25519:P-256");
211 if (disabled
("ecx")) {
212 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == FFDHE2048
),
213 "Multiple acceptable key_shares (part 2)");
215 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == X25519
),
216 "Multiple acceptable key_shares (part 2)");
219 #Test 15: Server sends key_share that wasn't offered should fail
221 $testtype = SELECT_X25519
;
222 if (disabled
("ecx")) {
223 $proxy->clientflags("-groups ffdhe3072");
225 $proxy->clientflags("-groups P-256");
228 ok
(TLSProxy
::Message
->fail(), "Non offered key_share");
230 #Test 16: Too short group_id in ServerHello should fail
232 $testtype = GROUP_ID_TOO_SHORT
;
234 ok
(TLSProxy
::Message
->fail(), "Group id too short in ServerHello");
236 #Test 17: key_exchange length mismatch in ServerHello should fail
238 $testtype = KEX_LEN_MISMATCH
;
240 ok
(TLSProxy
::Message
->fail(), "key_exchange length mismatch in ServerHello");
242 #Test 18: Zero length key_exchange in ServerHello should fail
244 $testtype = ZERO_LEN_KEX_DATA
;
246 ok
(TLSProxy
::Message
->fail(), "zero length key_exchange data in ServerHello");
248 #Test 19: Trailing data on key_share in ServerHello should fail
250 $testtype = TRAILING_DATA
;
252 ok
(TLSProxy
::Message
->fail(), "key_share trailing data in ServerHello");
255 skip
"No TLSv1.2 support in this OpenSSL build", 2 if disabled
("tls1_2");
257 #Test 20: key_share should not be sent if the client is not capable of
258 # negotiating TLSv1.3
260 $proxy->filter(undef);
261 $proxy->clientflags("-no_tls1_3");
263 my $clienthello = $proxy->message_list->[0];
264 ok
(TLSProxy
::Message
->success()
265 && !defined $clienthello->extension_data->{TLSProxy
::Message
::EXT_KEY_SHARE
},
266 "No key_share for TLS<=1.2 client");
267 $proxy->filter(\
&modify_key_shares_filter
);
269 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
270 # process a key_share
272 $direction = CLIENT_TO_SERVER
;
273 $testtype = NO_ACCEPTABLE_KEY_SHARES
;
274 $proxy->serverflags("-no_tls1_3");
276 ok
(TLSProxy
::Message
->success(), "Ignore key_share for TLS<=1.2 server");
279 #Test 22: The server sending an HRR but not requesting a new key_share should
282 $direction = SERVER_TO_CLIENT
;
283 $testtype = NO_KEY_SHARES_IN_HRR
;
284 if (disabled
("ecx")) {
285 $proxy->serverflags("-groups ffdhe2048");
287 $proxy->serverflags("-groups X25519");
290 ok
(TLSProxy
::Message
->fail(), "Server sends HRR with no key_shares");
293 skip
"No EC support in this OpenSSL build", 1 if disabled
("ec");
294 #Test 23: Trailing data on key_share in ServerHello should fail
296 $direction = CLIENT_TO_SERVER
;
297 if (disabled
("ecx")) {
298 $proxy->clientflags("-groups secp192r1:P-256:P-384");
300 $proxy->clientflags("-groups secp192r1:P-256:X25519");
302 $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
303 $testtype = NON_TLS1_3_KEY_SHARE
;
305 my $ishrr = defined ${$proxy->message_list}[2]
306 &&(${$proxy->message_list}[0]->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
)
307 && (${$proxy->message_list}[2]->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
);
308 ok
(TLSProxy
::Message
->success() && $ishrr,
309 "Client sends a key_share for a Non TLSv1.3 group");
312 sub modify_key_shares_filter
316 # We're only interested in the initial ClientHello/SererHello/HRR
317 if (($direction == CLIENT_TO_SERVER
&& $proxy->flight != 0
318 && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR
))
319 || ($direction == SERVER_TO_CLIENT
&& $proxy->flight != 1)) {
323 foreach my $message (@
{$proxy->message_list}) {
324 if ($message->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
325 && $direction == CLIENT_TO_SERVER
) {
329 if ($testtype != NON_TLS1_3_KEY_SHARE
) {
330 #Setup supported groups to include some unrecognised groups
331 if (disabled
("ecx")) {
332 $suppgroups = pack "C8",
333 0x00, 0x06, #List Length
334 0xff, 0xfe, #Non existing group 1
335 0xff, 0xff, #Non existing group 2
338 $suppgroups = pack "C8",
339 0x00, 0x06, #List Length
340 0xff, 0xfe, #Non existing group 1
341 0xff, 0xff, #Non existing group 2
345 if (disabled
("ecx")) {
346 $suppgroups = pack "C6",
347 0x00, 0x04, #List Length
351 $suppgroups = pack "C6",
352 0x00, 0x04, #List Length
358 if ($testtype == EMPTY_EXTENSION
) {
361 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES
) {
363 0x00, 0x0a, #List Length
364 0xff, 0xfe, #Non existing group 1
365 0x00, 0x01, 0xff, #key_exchange data
366 0xff, 0xff, #Non existing group 2
367 0x00, 0x01, 0xff; #key_exchange data
368 } elsif ($testtype == ACCEPTABLE_AT_END
) {
369 if (disabled
("ecx")) {
370 $ext = pack "C11H130",
371 0x00, 0x4A, #List Length
372 0xff, 0xfe, #Non existing group 1
373 0x00, 0x01, 0xff, #key_exchange data
375 0x00, 0x41, #key_exchange data length
376 "04A798ACF80B2991A0A53D084F4F649A46BE49D061EB5B8CFF9C8EC6AE792507B6".
377 "F77FE6E446AF3645FD86BB7CFFD2644E45CC00183343C5CEAD67BB017B082007"; #key_exchange data
379 $ext = pack "C11H64",
380 0x00, 0x29, #List Length
381 0xff, 0xfe, #Non existing group 1
382 0x00, 0x01, 0xff, #key_exchange data
384 0x00, 0x20, #key_exchange data length
385 "155155B95269ED5C87EAA99C2EF5A593".
386 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
388 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS
) {
389 $suppgroups = pack "C4",
390 0x00, 0x02, #List Length
391 0x00, 0xfe; #Non existing group 1
392 } elsif ($testtype == GROUP_ID_TOO_SHORT
) {
393 $ext = pack "C6H64C1",
394 0x00, 0x25, #List Length
396 0x00, 0x20, #key_exchange data length
397 "155155B95269ED5C87EAA99C2EF5A593".
398 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
399 0x00; #Group id too short
400 } elsif ($testtype == KEX_LEN_MISMATCH
) {
402 0x00, 0x06, #List Length
404 0x00, 0x20, #key_exchange data length
405 0x15, 0x51; #Only two bytes of data, but length should be 32
406 } elsif ($testtype == ZERO_LEN_KEX_DATA
) {
407 $ext = pack "C10H64",
408 0x00, 0x28, #List Length
409 0xff, 0xfe, #Non existing group 1
410 0x00, 0x00, #zero length key_exchange data is invalid
412 0x00, 0x20, #key_exchange data length
413 "155155B95269ED5C87EAA99C2EF5A593".
414 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
415 } elsif ($testtype == TRAILING_DATA
) {
416 $ext = pack "C6H64C1",
417 0x00, 0x24, #List Length
419 0x00, 0x20, #key_exchange data length
420 "155155B95269ED5C87EAA99C2EF5A593".
421 "EDF83495E80380089F831B94D14B1421", #key_exchange data
422 0x00; #Trailing garbage
423 } elsif ($testtype == NO_KEY_SHARES_IN_HRR
) {
424 #We trick the server into thinking we sent a P-256 key_share -
425 #but the client actually sent X25519
427 0x00, 0x05, #List Length
429 0x00, 0x01, #key_exchange data length
430 0xff; #Dummy key_share data
431 } elsif ($testtype == NON_TLS1_3_KEY_SHARE
) {
433 0x00, 0x35, #List Length
435 0x00, 0x31, #key_exchange data length
436 "04EE3B38D1CB800A1A2B702FC8423599F2AC7161E175C865F8".
437 "3DAF78BCBAE561464E8144359BE70CB7989D28A2F43F8F2C"; #key_exchange data
440 if ($testtype != EMPTY_EXTENSION
441 && $testtype != NO_KEY_SHARES_IN_HRR
) {
442 $message->set_extension(
443 TLSProxy
::Message
::EXT_SUPPORTED_GROUPS
, $suppgroups);
445 if ($testtype == MISSING_EXTENSION
) {
446 $message->delete_extension(
447 TLSProxy
::Message
::EXT_KEY_SHARE
);
448 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS
) {
449 $message->set_extension(
450 TLSProxy
::Message
::EXT_KEY_SHARE
, $ext);
454 } elsif ($message->mt == TLSProxy
::Message
::MT_SERVER_HELLO
455 && $direction == SERVER_TO_CLIENT
) {
458 $message->extension_data->{TLSProxy
::Message
::EXT_KEY_SHARE
};
459 $selectedgroupid = unpack("n", $key_share);
461 if ($testtype == LOOK_ONLY
) {
464 if ($testtype == NO_KEY_SHARES_IN_HRR
) {
465 $message->delete_extension(TLSProxy
::Message
::EXT_KEY_SHARE
);
466 $message->set_extension(TLSProxy
::Message
::EXT_UNKNOWN
, "");
470 if ($testtype == SELECT_X25519
) {
473 0x00, 0x20, #key_exchange data length
474 "155155B95269ED5C87EAA99C2EF5A593".
475 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
476 } elsif ($testtype == GROUP_ID_TOO_SHORT
) {
479 } elsif ($testtype == KEX_LEN_MISMATCH
) {
482 0x00, 0x20, #key_exchange data length
483 0x15, 0x51; #Only two bytes of data, but length should be 32
484 } elsif ($testtype == ZERO_LEN_KEX_DATA
) {
487 0x00, 0x00, #zero length key_exchange data is invalid
488 } elsif ($testtype == TRAILING_DATA
) {
489 $ext = pack "C4H64C1",
491 0x00, 0x20, #key_exchange data length
492 "155155B95269ED5C87EAA99C2EF5A593".
493 "EDF83495E80380089F831B94D14B1421", #key_exchange data
494 0x00; #Trailing garbage
496 $message->set_extension(TLSProxy
::Message
::EXT_KEY_SHARE
, $ext);