1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#! /usr/bin/env perl
# Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
use strict;
use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
use OpenSSL::Test::Utils;
use TLSProxy::Proxy;
use File::Temp qw(tempfile);
use constant {
LOOK_ONLY => 0,
EMPTY_EXTENSION => 1,
MISSING_EXTENSION => 2,
NO_ACCEPTABLE_KEY_SHARES => 3,
NON_PREFERRED_KEY_SHARE => 4,
ACCEPTABLE_AT_END => 5,
NOT_IN_SUPPORTED_GROUPS => 6,
GROUP_ID_TOO_SHORT => 7,
KEX_LEN_MISMATCH => 8,
ZERO_LEN_KEX_DATA => 9,
TRAILING_DATA => 10,
SELECT_X25519 => 11
};
use constant {
CLIENT_TO_SERVER => 1,
SERVER_TO_CLIENT => 2
};
use constant {
X25519 => 0x1d,
P_256 => 0x17
};
my $testtype;
my $direction;
my $selectedgroupid;
my $test_name = "test_key_share";
setup($test_name);
plan skip_all => "TLSProxy isn't usable on $^O"
if $^O =~ /^(VMS|MSWin32)$/;
plan skip_all => "$test_name needs the dynamic engine feature enabled"
if disabled("engine") || disabled("dynamic-engine");
plan skip_all => "$test_name needs the sock feature enabled"
if disabled("sock");
plan skip_all => "$test_name needs TLS1.3 enabled"
if disabled("tls1_3");
$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
my $proxy = TLSProxy::Proxy->new(
undef,
cmdstr(app(["openssl"]), display => 1),
srctop_file("apps", "server.pem"),
(!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
);
#We assume that test_ssl_new and friends will test the happy path for this,
#so we concentrate on the less common scenarios
#Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
$testtype = EMPTY_EXTENSION;
$direction = CLIENT_TO_SERVER;
$proxy->filter(\&modify_key_shares_filter);
$proxy->serverflags("-curves P-256");
$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
plan tests => 21;
ok(TLSProxy::Message->success(), "Success after HRR");
#Test 2: The server sending an HRR requesting a group the client already sent
# should fail
$proxy->clear();
$proxy->start();
ok(TLSProxy::Message->fail(), "Server asks for group already provided");
#Test 3: A missing key_shares extension should not succeed
$proxy->clear();
$testtype = MISSING_EXTENSION;
$proxy->start();
ok(TLSProxy::Message->fail(), "Missing key_shares extension");
#Test 4: No initial acceptable key_shares should succeed after a
# HelloRetryRequest
$proxy->clear();
$proxy->filter(undef);
$proxy->serverflags("-curves P-256");
$proxy->start();
ok(TLSProxy::Message->success(), "No initial acceptable key_shares");
#Test 5: No acceptable key_shares and no shared groups should fail
$proxy->clear();
$proxy->filter(undef);
$proxy->serverflags("-curves P-256");
$proxy->clientflags("-curves P-384");
$proxy->start();
ok(TLSProxy::Message->fail(), "No acceptable key_shares");
#Test 6: A non preferred but acceptable key_share should succeed
$proxy->clear();
$proxy->clientflags("-curves P-256");
$proxy->start();
ok(TLSProxy::Message->success(), "Non preferred key_share");
$proxy->filter(\&modify_key_shares_filter);
#Test 7: An acceptable key_share after a list of non-acceptable ones should
#succeed
$proxy->clear();
$testtype = ACCEPTABLE_AT_END;
$proxy->start();
ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
#Test 8: An acceptable key_share but for a group not in supported_groups should
#fail
$proxy->clear();
$testtype = NOT_IN_SUPPORTED_GROUPS;
$proxy->start();
ok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
#Test 9: Too short group_id should fail
$proxy->clear();
$testtype = GROUP_ID_TOO_SHORT;
$proxy->start();
ok(TLSProxy::Message->fail(), "Group id too short");
#Test 10: key_exchange length mismatch should fail
$proxy->clear();
$testtype = KEX_LEN_MISMATCH;
$proxy->start();
ok(TLSProxy::Message->fail(), "key_exchange length mismatch");
#Test 11: Zero length key_exchange should fail
$proxy->clear();
$testtype = ZERO_LEN_KEX_DATA;
$proxy->start();
ok(TLSProxy::Message->fail(), "zero length key_exchange data");
#Test 12: Trailing data on key_share list should fail
$proxy->clear();
$testtype = TRAILING_DATA;
$proxy->start();
ok(TLSProxy::Message->fail(), "key_share list trailing data");
#Test 13: Multiple acceptable key_shares - we choose the first one
$proxy->clear();
$direction = SERVER_TO_CLIENT;
$testtype = LOOK_ONLY;
$proxy->clientflags("-curves P-256:X25519");
$proxy->start();
ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
"Multiple acceptable key_shares");
#Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
$proxy->clear();
$proxy->clientflags("-curves X25519:P-256");
$proxy->start();
ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
"Multiple acceptable key_shares (part 2)");
#Test 15: Server sends key_share that wasn't offerred should fail
$proxy->clear();
$testtype = SELECT_X25519;
$proxy->clientflags("-curves P-256");
$proxy->start();
ok(TLSProxy::Message->fail(), "Non offered key_share");
#Test 16: Too short group_id in ServerHello should fail
$proxy->clear();
$testtype = GROUP_ID_TOO_SHORT;
$proxy->start();
ok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
#Test 17: key_exchange length mismatch in ServerHello should fail
$proxy->clear();
$testtype = KEX_LEN_MISMATCH;
$proxy->start();
ok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
#Test 18: Zero length key_exchange in ServerHello should fail
$proxy->clear();
$testtype = ZERO_LEN_KEX_DATA;
$proxy->start();
ok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
#Test 19: Trailing data on key_share in ServerHello should fail
$proxy->clear();
$testtype = TRAILING_DATA;
$proxy->start();
ok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
#Test 20: key_share should not be sent if the client is not capable of
# negotiating TLSv1.3
$proxy->clear();
$proxy->filter(undef);
$proxy->clientflags("-no_tls1_3");
$proxy->start();
my $clienthello = $proxy->message_list->[0];
ok(TLSProxy::Message->success()
&& !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
"No key_share for TLS<=1.2 client");
$proxy->filter(\&modify_key_shares_filter);
#Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
# process a key_share
$proxy->clear();
$direction = CLIENT_TO_SERVER;
$testtype = NO_ACCEPTABLE_KEY_SHARES;
$proxy->serverflags("-no_tls1_3");
$proxy->start();
ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
sub modify_key_shares_filter
{
my $proxy = shift;
# We're only interested in the initial ClientHello
if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0)
|| ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
return;
}
foreach my $message (@{$proxy->message_list}) {
if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
&& $direction == CLIENT_TO_SERVER) {
my $ext;
my $suppgroups;
#Setup supported groups to include some unrecognised groups
$suppgroups = pack "C8",
0x00, 0x06, #List Length
0xff, 0xfe, #Non existing group 1
0xff, 0xff, #Non existing group 2
0x00, 0x1d; #x25519
if ($testtype == EMPTY_EXTENSION) {
$ext = pack "C2",
0x00, 0x00;
} elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
$ext = pack "C12",
0x00, 0x0a, #List Length
0xff, 0xfe, #Non existing group 1
0x00, 0x01, 0xff, #key_exchange data
0xff, 0xff, #Non existing group 2
0x00, 0x01, 0xff; #key_exchange data
} elsif ($testtype == ACCEPTABLE_AT_END) {
$ext = pack "C11H64",
0x00, 0x29, #List Length
0xff, 0xfe, #Non existing group 1
0x00, 0x01, 0xff, #key_exchange data
0x00, 0x1d, #x25519
0x00, 0x20, #key_exchange data length
"155155B95269ED5C87EAA99C2EF5A593".
"EDF83495E80380089F831B94D14B1421"; #key_exchange data
} elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
$suppgroups = pack "C4",
0x00, 0x02, #List Length
0x00, 0xfe; #Non existing group 1
} elsif ($testtype == GROUP_ID_TOO_SHORT) {
$ext = pack "C6H64C1",
0x00, 0x25, #List Length
0x00, 0x1d, #x25519
0x00, 0x20, #key_exchange data length
"155155B95269ED5C87EAA99C2EF5A593".
"EDF83495E80380089F831B94D14B1421"; #key_exchange data
0x00; #Group id too short
} elsif ($testtype == KEX_LEN_MISMATCH) {
$ext = pack "C8",
0x00, 0x06,
|