]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/recipes/70-test_key_share.t
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,
46 my $test_name = "test_key_share";
49 plan skip_all
=> "TLSProxy isn't usable on $^O"
52 plan skip_all
=> "$test_name needs the dynamic engine feature enabled"
53 if disabled
("engine") || disabled
("dynamic-engine");
55 plan skip_all
=> "$test_name needs the sock feature enabled"
58 plan skip_all
=> "$test_name needs TLS1.3 enabled"
59 if disabled
("tls1_3");
61 $ENV{OPENSSL_ia32cap
} = '~0x200000200000000';
63 my $proxy = TLSProxy
::Proxy
->new(
65 cmdstr
(app
(["openssl"]), display
=> 1),
66 srctop_file
("apps", "server.pem"),
67 (!$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
})
70 #We assume that test_ssl_new and friends will test the happy path for this,
71 #so we concentrate on the less common scenarios
73 #Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
74 $testtype = EMPTY_EXTENSION
;
75 $direction = CLIENT_TO_SERVER
;
76 $proxy->filter(\
&modify_key_shares_filter
);
77 $proxy->serverflags("-curves P-256");
78 $proxy->start() or plan skip_all
=> "Unable to start up Proxy for tests";
80 ok
(TLSProxy
::Message
->success(), "Success after HRR");
82 #Test 2: The server sending an HRR requesting a group the client already sent
86 ok
(TLSProxy
::Message
->fail(), "Server asks for group already provided");
88 #Test 3: A missing key_shares extension should not succeed
90 $testtype = MISSING_EXTENSION
;
92 ok
(TLSProxy
::Message
->fail(), "Missing key_shares extension");
94 #Test 4: No initial acceptable key_shares should succeed after a
97 $proxy->filter(undef);
98 $proxy->serverflags("-curves P-256");
100 ok
(TLSProxy
::Message
->success(), "No initial acceptable key_shares");
102 #Test 5: No acceptable key_shares and no shared groups should fail
104 $proxy->filter(undef);
105 $proxy->serverflags("-curves P-256");
106 $proxy->clientflags("-curves P-384");
108 ok
(TLSProxy
::Message
->fail(), "No acceptable key_shares");
110 #Test 6: A non preferred but acceptable key_share should succeed
112 $proxy->clientflags("-curves P-256");
114 ok
(TLSProxy
::Message
->success(), "Non preferred key_share");
115 $proxy->filter(\
&modify_key_shares_filter
);
117 #Test 7: An acceptable key_share after a list of non-acceptable ones should
120 $testtype = ACCEPTABLE_AT_END
;
122 ok
(TLSProxy
::Message
->success(), "Acceptable key_share at end of list");
124 #Test 8: An acceptable key_share but for a group not in supported_groups should
127 $testtype = NOT_IN_SUPPORTED_GROUPS
;
129 ok
(TLSProxy
::Message
->fail(), "Acceptable key_share not in supported_groups");
131 #Test 9: Too short group_id should fail
133 $testtype = GROUP_ID_TOO_SHORT
;
135 ok
(TLSProxy
::Message
->fail(), "Group id too short");
137 #Test 10: key_exchange length mismatch should fail
139 $testtype = KEX_LEN_MISMATCH
;
141 ok
(TLSProxy
::Message
->fail(), "key_exchange length mismatch");
143 #Test 11: Zero length key_exchange should fail
145 $testtype = ZERO_LEN_KEX_DATA
;
147 ok
(TLSProxy
::Message
->fail(), "zero length key_exchange data");
149 #Test 12: Trailing data on key_share list should fail
151 $testtype = TRAILING_DATA
;
153 ok
(TLSProxy
::Message
->fail(), "key_share list trailing data");
155 #Test 13: Multiple acceptable key_shares - we choose the first one
157 $direction = SERVER_TO_CLIENT
;
158 $testtype = LOOK_ONLY
;
159 $proxy->clientflags("-curves P-256:X25519");
161 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == P_256
),
162 "Multiple acceptable key_shares");
164 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
166 $proxy->clientflags("-curves X25519:P-256");
168 ok
(TLSProxy
::Message
->success() && ($selectedgroupid == X25519
),
169 "Multiple acceptable key_shares (part 2)");
171 #Test 15: Server sends key_share that wasn't offered should fail
173 $testtype = SELECT_X25519
;
174 $proxy->clientflags("-curves P-256");
176 ok
(TLSProxy
::Message
->fail(), "Non offered key_share");
178 #Test 16: Too short group_id in ServerHello should fail
180 $testtype = GROUP_ID_TOO_SHORT
;
182 ok
(TLSProxy
::Message
->fail(), "Group id too short in ServerHello");
184 #Test 17: key_exchange length mismatch in ServerHello should fail
186 $testtype = KEX_LEN_MISMATCH
;
188 ok
(TLSProxy
::Message
->fail(), "key_exchange length mismatch in ServerHello");
190 #Test 18: Zero length key_exchange in ServerHello should fail
192 $testtype = ZERO_LEN_KEX_DATA
;
194 ok
(TLSProxy
::Message
->fail(), "zero length key_exchange data in ServerHello");
196 #Test 19: Trailing data on key_share in ServerHello should fail
198 $testtype = TRAILING_DATA
;
200 ok
(TLSProxy
::Message
->fail(), "key_share trailing data in ServerHello");
203 skip
"No TLSv1.2 support in this OpenSSL build", 2 if disabled
("tls1_2");
205 #Test 20: key_share should not be sent if the client is not capable of
206 # negotiating TLSv1.3
208 $proxy->filter(undef);
209 $proxy->clientflags("-no_tls1_3");
211 my $clienthello = $proxy->message_list->[0];
212 ok
(TLSProxy
::Message
->success()
213 && !defined $clienthello->extension_data->{TLSProxy
::Message
::EXT_KEY_SHARE
},
214 "No key_share for TLS<=1.2 client");
215 $proxy->filter(\
&modify_key_shares_filter
);
217 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
218 # process a key_share
220 $direction = CLIENT_TO_SERVER
;
221 $testtype = NO_ACCEPTABLE_KEY_SHARES
;
222 $proxy->serverflags("-no_tls1_3");
224 ok
(TLSProxy
::Message
->success(), "Ignore key_share for TLS<=1.2 server");
227 #Test 22: The server sending an HRR but not requesting a new key_share should
230 $direction = SERVER_TO_CLIENT
;
231 $testtype = NO_KEY_SHARES_IN_HRR
;
232 $proxy->serverflags("-curves X25519");
234 ok
(TLSProxy
::Message
->fail(), "Server sends HRR with no key_shares");
236 sub modify_key_shares_filter
240 # We're only interested in the initial ClientHello
241 if (($direction == CLIENT_TO_SERVER
&& $proxy->flight != 0
242 && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR
))
243 || ($direction == SERVER_TO_CLIENT
&& $proxy->flight != 1)) {
247 foreach my $message (@
{$proxy->message_list}) {
248 if ($message->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
249 && $direction == CLIENT_TO_SERVER
) {
253 #Setup supported groups to include some unrecognised groups
254 $suppgroups = pack "C8",
255 0x00, 0x06, #List Length
256 0xff, 0xfe, #Non existing group 1
257 0xff, 0xff, #Non existing group 2
260 if ($testtype == EMPTY_EXTENSION
) {
263 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES
) {
265 0x00, 0x0a, #List Length
266 0xff, 0xfe, #Non existing group 1
267 0x00, 0x01, 0xff, #key_exchange data
268 0xff, 0xff, #Non existing group 2
269 0x00, 0x01, 0xff; #key_exchange data
270 } elsif ($testtype == ACCEPTABLE_AT_END
) {
271 $ext = pack "C11H64",
272 0x00, 0x29, #List Length
273 0xff, 0xfe, #Non existing group 1
274 0x00, 0x01, 0xff, #key_exchange data
276 0x00, 0x20, #key_exchange data length
277 "155155B95269ED5C87EAA99C2EF5A593".
278 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
279 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS
) {
280 $suppgroups = pack "C4",
281 0x00, 0x02, #List Length
282 0x00, 0xfe; #Non existing group 1
283 } elsif ($testtype == GROUP_ID_TOO_SHORT
) {
284 $ext = pack "C6H64C1",
285 0x00, 0x25, #List Length
287 0x00, 0x20, #key_exchange data length
288 "155155B95269ED5C87EAA99C2EF5A593".
289 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
290 0x00; #Group id too short
291 } elsif ($testtype == KEX_LEN_MISMATCH
) {
293 0x00, 0x06, #List Length
295 0x00, 0x20, #key_exchange data length
296 0x15, 0x51; #Only two bytes of data, but length should be 32
297 } elsif ($testtype == ZERO_LEN_KEX_DATA
) {
298 $ext = pack "C10H64",
299 0x00, 0x28, #List Length
300 0xff, 0xfe, #Non existing group 1
301 0x00, 0x00, #zero length key_exchange data is invalid
303 0x00, 0x20, #key_exchange data length
304 "155155B95269ED5C87EAA99C2EF5A593".
305 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
306 } elsif ($testtype == TRAILING_DATA
) {
307 $ext = pack "C6H64C1",
308 0x00, 0x24, #List Length
310 0x00, 0x20, #key_exchange data length
311 "155155B95269ED5C87EAA99C2EF5A593".
312 "EDF83495E80380089F831B94D14B1421", #key_exchange data
313 0x00; #Trailing garbage
314 } elsif ($testtype == NO_KEY_SHARES_IN_HRR
) {
315 #We trick the server into thinking we sent a P-256 key_share -
316 #but the client actually sent X25519
318 0x00, 0x05, #List Length
320 0x00, 0x01, #key_exchange data length
321 0xff; #Dummy key_share data
324 if ($testtype != EMPTY_EXTENSION
325 && $testtype != NO_KEY_SHARES_IN_HRR
) {
326 $message->set_extension(
327 TLSProxy
::Message
::EXT_SUPPORTED_GROUPS
, $suppgroups);
330 if ($testtype == MISSING_EXTENSION
) {
331 $message->delete_extension(
332 TLSProxy
::Message
::EXT_KEY_SHARE
);
333 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS
) {
334 $message->set_extension(
335 TLSProxy
::Message
::EXT_KEY_SHARE
, $ext);
339 } elsif ($message->mt == TLSProxy
::Message
::MT_SERVER_HELLO
340 && $direction == SERVER_TO_CLIENT
) {
343 $message->extension_data->{TLSProxy
::Message
::EXT_KEY_SHARE
};
344 $selectedgroupid = unpack("n", $key_share);
346 if ($testtype == LOOK_ONLY
) {
349 if ($testtype == NO_KEY_SHARES_IN_HRR
) {
350 $message->delete_extension(TLSProxy
::Message
::EXT_KEY_SHARE
);
351 $message->set_extension(TLSProxy
::Message
::EXT_UNKNOWN
, "");
355 if ($testtype == SELECT_X25519
) {
358 0x00, 0x20, #key_exchange data length
359 "155155B95269ED5C87EAA99C2EF5A593".
360 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
361 } elsif ($testtype == GROUP_ID_TOO_SHORT
) {
364 } elsif ($testtype == KEX_LEN_MISMATCH
) {
367 0x00, 0x20, #key_exchange data length
368 0x15, 0x51; #Only two bytes of data, but length should be 32
369 } elsif ($testtype == ZERO_LEN_KEX_DATA
) {
372 0x00, 0x00, #zero length key_exchange data is invalid
373 } elsif ($testtype == TRAILING_DATA
) {
374 $ext = pack "C4H64C1",
376 0x00, 0x20, #key_exchange data length
377 "155155B95269ED5C87EAA99C2EF5A593".
378 "EDF83495E80380089F831B94D14B1421", #key_exchange data
379 0x00; #Trailing garbage
381 $message->set_extension(TLSProxy
::Message
::EXT_KEY_SHARE
, $ext);