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 not succeed
73 $testtype = EMPTY_EXTENSION;
74 $direction = CLIENT_TO_SERVER;
75 $proxy->filter(\&modify_key_shares_filter);
76 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
78 #TODO(TLS1.3): Actually this should succeed after a HelloRetryRequest - but
79 #we've not implemented that yet, so for now we look for a fail
80 ok(TLSProxy::Message->fail(), "Empty key_shares");
82 #Test 2: A missing key_shares extension should not succeed
84 $testtype = MISSING_EXTENSION;
86 #TODO(TLS1.3): As above this should really succeed after a HelloRetryRequest,
87 #but we look for fail for now
88 ok(TLSProxy::Message->fail(), "Missing key_shares extension");
90 #Test 3: No acceptable key_shares should fail
92 $testtype = NO_ACCEPTABLE_KEY_SHARES;
94 #TODO(TLS1.3): Again this should go around the loop of a HelloRetryRequest but
96 ok(TLSProxy::Message->fail(), "No acceptable key_shares");
98 #Test 4: A non preferred but acceptable key_share should succeed
100 $proxy->filter(undef);
101 $proxy->clientflags("-curves P-256");
103 ok(TLSProxy::Message->success(), "Non preferred key_share");
104 $proxy->filter(\&modify_key_shares_filter);
106 #Test 5: An acceptable key_share after a list of non-acceptable ones should
109 $testtype = ACCEPTABLE_AT_END;
111 ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
113 #Test 6: An acceptable key_share but for a group not in supported_groups should
116 $testtype = NOT_IN_SUPPORTED_GROUPS;
118 ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
120 #Test 7: Too short group_id should fail
122 $testtype = GROUP_ID_TOO_SHORT;
124 ok(TLSProxy::Message->fail(), "Group id too short");
126 #Test 8: key_exchange length mismatch should fail
128 $testtype = KEX_LEN_MISMATCH;
130 ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
132 #Test 9: Zero length key_exchange should fail
134 $testtype = ZERO_LEN_KEX_DATA;
136 ok(TLSProxy::Message->fail(), "zero length key_exchange data");
138 #Test 10: Trailing data on key_share list should fail
140 $testtype = TRAILING_DATA;
142 ok(TLSProxy::Message->fail(), "key_share list trailing data");
144 #Test 11: Multiple acceptable key_shares - we choose the first one
146 $direction = SERVER_TO_CLIENT;
147 $testtype = LOOK_ONLY;
148 $proxy->clientflags("-curves P-256:X25519");
150 ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
151 "Multiple acceptable key_shares");
153 #Test 12: Multiple acceptable key_shares - we choose the first one (part 2)
155 $proxy->clientflags("-curves X25519:P-256");
157 ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
158 "Multiple acceptable key_shares (part 2)");
160 #Test 13: Server sends key_share that wasn't offerred should fail
162 $testtype = SELECT_X25519;
163 $proxy->clientflags("-curves P-256");
165 ok(TLSProxy::Message->fail(), "Non offered key_share");
167 #Test 14: Too short group_id in ServerHello should fail
169 $testtype = GROUP_ID_TOO_SHORT;
171 ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
173 #Test 15: key_exchange length mismatch in ServerHello should fail
175 $testtype = KEX_LEN_MISMATCH;
177 ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
179 #Test 16: Zero length key_exchange in ServerHello should fail
181 $testtype = ZERO_LEN_KEX_DATA;
183 ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
185 #Test 17: Trailing data on key_share in ServerHello should fail
187 $testtype = TRAILING_DATA;
189 ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
191 #Test 18: key_share should not be sent if the client is not capable of
192 # negotiating TLSv1.3
194 $proxy->filter(undef);
195 $proxy->clientflags("-no_tls1_3");
197 my $clienthello = ${$proxy->message_list}[0];
198 ok(TLSProxy::Message->success()
199 && !defined ${$clienthello->extension_data}{TLSProxy::Message::EXT_KEY_SHARE},
200 "No key_share for TLS<=1.2 client");
201 $proxy->filter(\&modify_key_shares_filter);
203 #Test 19: A server not capable of negotiating TLSv1.3 should not attempt to
204 # process a key_share
206 $direction = CLIENT_TO_SERVER;
207 $testtype = NO_ACCEPTABLE_KEY_SHARES;
208 $proxy->serverflags("-no_tls1_3");
210 ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
212 sub modify_key_shares_filter
216 # We're only interested in the initial ClientHello
217 if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0)
218 || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
222 foreach my $message (@{$proxy->message_list}) {
223 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
224 && $direction == CLIENT_TO_SERVER) {
228 #Setup supported groups to include some unrecognised groups
229 $suppgroups = pack "C8",
230 0x00, 0x06, #List Length
231 0xff, 0xfe, #Non existing group 1
232 0xff, 0xff, #Non existing group 2
235 if ($testtype == EMPTY_EXTENSION) {
238 } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
240 0x00, 0x0a, #List Length
241 0xff, 0xfe, #Non existing group 1
242 0x00, 0x01, 0xff, #key_exchange data
243 0xff, 0xff, #Non existing group 2
244 0x00, 0x01, 0xff; #key_exchange data
245 } elsif ($testtype == ACCEPTABLE_AT_END) {
246 $ext = pack "C11H64",
247 0x00, 0x29, #List Length
248 0xff, 0xfe, #Non existing group 1
249 0x00, 0x01, 0xff, #key_exchange data
251 0x00, 0x20, #key_exchange data length
252 "155155B95269ED5C87EAA99C2EF5A593".
253 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
254 } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
255 $suppgroups = pack "C4",
256 0x00, 0x02, #List Length
257 0x00, 0xfe; #Non existing group 1
258 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
259 $ext = pack "C6H64C1",
260 0x00, 0x25, #List Length
262 0x00, 0x20, #key_exchange data length
263 "155155B95269ED5C87EAA99C2EF5A593".
264 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
265 0x00; #Group id too short
266 } elsif ($testtype == KEX_LEN_MISMATCH) {
268 0x00, 0x06, #List Length
270 0x00, 0x20, #key_exchange data length
271 0x15, 0x51; #Only two bytes of data, but length should be 32
272 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
273 $ext = pack "C10H64",
274 0x00, 0x28, #List Length
275 0xff, 0xfe, #Non existing group 1
276 0x00, 0x00, #zero length key_exchange data is invalid
278 0x00, 0x20, #key_exchange data length
279 "155155B95269ED5C87EAA99C2EF5A593".
280 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
281 } elsif ($testtype == TRAILING_DATA) {
282 $ext = pack "C6H64C1",
283 0x00, 0x24, #List Length
285 0x00, 0x20, #key_exchange data length
286 "155155B95269ED5C87EAA99C2EF5A593".
287 "EDF83495E80380089F831B94D14B1421", #key_exchange data
288 0x00; #Trailing garbage
291 $message->set_extension(
292 TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
294 if ($testtype == MISSING_EXTENSION) {
295 $message->delete_extension(
296 TLSProxy::Message::EXT_KEY_SHARE);
297 } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
298 $message->set_extension(
299 TLSProxy::Message::EXT_KEY_SHARE, $ext);
303 } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
304 && $direction == SERVER_TO_CLIENT) {
307 ${$message->extension_data}{TLSProxy::Message::EXT_KEY_SHARE};
308 $selectedgroupid = unpack("n", $key_share);
310 if ($testtype == LOOK_ONLY) {
313 if ($testtype == SELECT_X25519) {
316 0x00, 0x20, #key_exchange data length
317 "155155B95269ED5C87EAA99C2EF5A593".
318 "EDF83495E80380089F831B94D14B1421"; #key_exchange data
319 } elsif ($testtype == GROUP_ID_TOO_SHORT) {
322 } elsif ($testtype == KEX_LEN_MISMATCH) {
325 0x00, 0x20, #key_exchange data length
326 0x15, 0x51; #Only two bytes of data, but length should be 32
327 } elsif ($testtype == ZERO_LEN_KEX_DATA) {
330 0x00, 0x00, #zero length key_exchange data is invalid
331 } elsif ($testtype == TRAILING_DATA) {
332 $ext = pack "C4H64C1",
334 0x00, 0x20, #key_exchange data length
335 "155155B95269ED5C87EAA99C2EF5A593".
336 "EDF83495E80380089F831B94D14B1421", #key_exchange data
337 0x00; #Trailing garbage
339 $message->set_extension( TLSProxy::Message::EXT_KEY_SHARE, $ext);