2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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,
31 CLIENT_TO_SERVER => 1,
45 my $test_name = "test_key_share";
48 plan skip_all => "TLSProxy isn't usable on $^O"
49 if $^O =~ /^(VMS|MSWin32)$/;
51 plan skip_all => "$test_name needs the dynamic engine feature enabled"
52 if disabled("engine") || disabled("dynamic-engine");
54 plan skip_all => "$test_name needs the sock feature enabled"
57 plan skip_all => "$test_name needs TLS1.3 enabled"
58 if disabled("tls1_3");
60 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
62 my $proxy = TLSProxy::Proxy->new(
64 cmdstr(app(["openssl"]), display => 1),
65 srctop_file("apps", "server.pem"),
66 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
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
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";
79 ok(TLSProxy::Message->success(), "Success after HRR");
81 #Test 2: The server sending an HRR requesting a group the client already sent
85 ok(TLSProxy::Message->fail(), "Server asks for group already provided");
87 #Test 3: A missing key_shares extension should not succeed
89 $testtype = MISSING_EXTENSION;
91 ok(TLSProxy::Message->fail(), "Missing key_shares extension");
93 #Test 4: No initial acceptable key_shares should succeed after a
96 $proxy->filter(undef);
97 $proxy->serverflags("-curves P-256");
99 ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
101 #Test 5: No acceptable key_shares and no shared groups should fail
103 $proxy->filter(undef);
104 $proxy->serverflags("-curves P-256");
105 $proxy->clientflags("-curves P-384");
107 ok(TLSProxy::Message->fail(), "No acceptable key_shares");
109 #Test 6: A non preferred but acceptable key_share should succeed
111 $proxy->clientflags("-curves P-256");
113 ok(TLSProxy::Message->success(), "Non preferred key_share");
114 $proxy->filter(\&modify_key_shares_filter);
116 #Test 7: An acceptable key_share after a list of non-acceptable ones should
119 $testtype = ACCEPTABLE_AT_END;
121 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
123 #Test 8: An acceptable key_share but for a group not in supported_groups should
126 $testtype = NOT_IN_SUPPORTED_GROUPS;
128 ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
130 #Test 9: Too short group_id should fail
132 $testtype = GROUP_ID_TOO_SHORT;
134 ok(TLSProxy::Message->fail(), "Group id too short");
136 #Test 10: key_exchange length mismatch should fail
138 $testtype = KEX_LEN_MISMATCH;
140 ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
142 #Test 11: Zero length key_exchange should fail
144 $testtype = ZERO_LEN_KEX_DATA;
146 ok(TLSProxy::Message->fail(), "zero length key_exchange data");
148 #Test 12: Trailing data on key_share list should fail
150 $testtype = TRAILING_DATA;
152 ok(TLSProxy::Message->fail(), "key_share list trailing data");
154 #Test 13: Multiple acceptable key_shares - we choose the first one
156 $direction = SERVER_TO_CLIENT;
157 $testtype = LOOK_ONLY;
158 $proxy->clientflags("-curves P-256:X25519");
160 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
161 "Multiple acceptable key_shares");
163 #Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
165 $proxy->clientflags("-curves X25519:P-256");
167 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
168 "Multiple acceptable key_shares (part 2)");
170 #Test 15: Server sends key_share that wasn't offerred should fail
172 $testtype = SELECT_X25519;
173 $proxy->clientflags("-curves P-256");
175 ok(TLSProxy::Message->fail(), "Non offered key_share");
177 #Test 16: Too short group_id in ServerHello should fail
179 $testtype = GROUP_ID_TOO_SHORT;
181 ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
183 #Test 17: key_exchange length mismatch in ServerHello should fail
185 $testtype = KEX_LEN_MISMATCH;
187 ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
189 #Test 18: Zero length key_exchange in ServerHello should fail
191 $testtype = ZERO_LEN_KEX_DATA;
193 ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
195 #Test 19: Trailing data on key_share in ServerHello should fail
197 $testtype = TRAILING_DATA;
199 ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
201 #Test 20: key_share should not be sent if the client is not capable of
202 # negotiating TLSv1.3
204 $proxy->filter(undef);
205 $proxy->clientflags("-no_tls1_3");
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);
213 #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
214 # process a key_share
216 $direction = CLIENT_TO_SERVER;
217 $testtype = NO_ACCEPTABLE_KEY_SHARES;
218 $proxy->serverflags("-no_tls1_3");
220 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
222 sub modify_key_shares_filter
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)) {
232 foreach my $message (@{$proxy->message_list}) {
233 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
234 && $direction == CLIENT_TO_SERVER) {
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
245 if ($testtype == EMPTY_EXTENSION) {
248 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
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
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
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) {
278 0x00, 0x06, #List Length
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
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
295 0x00, 0x20, #key_exchange data length
296 "155155B95269ED5C87EAA99C2EF5A593".
297 "EDF83495E80380089F831B94D14B1421", #key_exchange data
298 0x00; #Trailing garbage
301 if ($testtype != EMPTY_EXTENSION) {
302 $message->set_extension(
303 TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
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);
315 } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
316 && $direction == SERVER_TO_CLIENT) {
319 $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
320 $selectedgroupid = unpack("n", $key_share);
322 if ($testtype == LOOK_ONLY) {
325 if ($testtype == SELECT_X25519) {
328 0x00, 0x20, #key_exchange data length
329 "155155B95269ED5C87EAA99C2EF5A593".
330 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
331 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
334 } elsif ($testtype == KEX_LEN_MISMATCH) {
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) {
342 0x00, 0x00, #zero length key_exchange data is invalid
343 } elsif ($testtype == TRAILING_DATA) {
344 $ext = pack "C4H64C1",
346 0x00, 0x20, #key_exchange data length
347 "155155B95269ED5C87EAA99C2EF5A593".
348 "EDF83495E80380089F831B94D14B1421", #key_exchange data
349 0x00; #Trailing garbage
351 $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);