1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse strict;
10e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
12e1051a39Sopenharmony_ciuse TLSProxy::Proxy;
13e1051a39Sopenharmony_ciuse File::Temp qw(tempfile);
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_ciuse constant {
16e1051a39Sopenharmony_ci    LOOK_ONLY => 0,
17e1051a39Sopenharmony_ci    EMPTY_EXTENSION => 1,
18e1051a39Sopenharmony_ci    MISSING_EXTENSION => 2,
19e1051a39Sopenharmony_ci    NO_ACCEPTABLE_KEY_SHARES => 3,
20e1051a39Sopenharmony_ci    NON_PREFERRED_KEY_SHARE => 4,
21e1051a39Sopenharmony_ci    ACCEPTABLE_AT_END => 5,
22e1051a39Sopenharmony_ci    NOT_IN_SUPPORTED_GROUPS => 6,
23e1051a39Sopenharmony_ci    GROUP_ID_TOO_SHORT => 7,
24e1051a39Sopenharmony_ci    KEX_LEN_MISMATCH => 8,
25e1051a39Sopenharmony_ci    ZERO_LEN_KEX_DATA => 9,
26e1051a39Sopenharmony_ci    TRAILING_DATA => 10,
27e1051a39Sopenharmony_ci    SELECT_X25519 => 11,
28e1051a39Sopenharmony_ci    NO_KEY_SHARES_IN_HRR => 12,
29e1051a39Sopenharmony_ci    NON_TLS1_3_KEY_SHARE => 13
30e1051a39Sopenharmony_ci};
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciuse constant {
33e1051a39Sopenharmony_ci    CLIENT_TO_SERVER => 1,
34e1051a39Sopenharmony_ci    SERVER_TO_CLIENT => 2
35e1051a39Sopenharmony_ci};
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ciuse constant {
39e1051a39Sopenharmony_ci    X25519 => 0x1d,
40e1051a39Sopenharmony_ci    P_256 => 0x17,
41e1051a39Sopenharmony_ci    FFDHE2048 => 0x0100,
42e1051a39Sopenharmony_ci    FFDHE3072 => 0x0101
43e1051a39Sopenharmony_ci};
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_cimy $testtype;
46e1051a39Sopenharmony_cimy $direction;
47e1051a39Sopenharmony_cimy $selectedgroupid;
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_cimy $test_name = "test_key_share";
50e1051a39Sopenharmony_cisetup($test_name);
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O"
53e1051a39Sopenharmony_ci    if $^O =~ /^(VMS)$/;
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled"
56e1051a39Sopenharmony_ci    if disabled("engine") || disabled("dynamic-engine");
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled"
59e1051a39Sopenharmony_ci    if disabled("sock");
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLS1.3 enabled"
62e1051a39Sopenharmony_ci    if disabled("tls1_3");
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ciplan skip_all => "$test_name needs EC or DH enabled"
65e1051a39Sopenharmony_ci    if disabled("ec") && disabled("dh");
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new(
70e1051a39Sopenharmony_ci    undef,
71e1051a39Sopenharmony_ci    cmdstr(app(["openssl"]), display => 1),
72e1051a39Sopenharmony_ci    srctop_file("apps", "server.pem"),
73e1051a39Sopenharmony_ci    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
74e1051a39Sopenharmony_ci);
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci#We assume that test_ssl_new and friends will test the happy path for this,
77e1051a39Sopenharmony_ci#so we concentrate on the less common scenarios
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci#Test 1: An empty key_shares extension should succeed after a HelloRetryRequest
80e1051a39Sopenharmony_ci$testtype = EMPTY_EXTENSION;
81e1051a39Sopenharmony_ci$direction = CLIENT_TO_SERVER;
82e1051a39Sopenharmony_ci$proxy->filter(\&modify_key_shares_filter);
83e1051a39Sopenharmony_ciif (disabled("ec")) {
84e1051a39Sopenharmony_ci    $proxy->serverflags("-groups ffdhe3072");
85e1051a39Sopenharmony_ci} else {
86e1051a39Sopenharmony_ci    $proxy->serverflags("-groups P-256");
87e1051a39Sopenharmony_ci}
88e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
89e1051a39Sopenharmony_ciplan tests => 23;
90e1051a39Sopenharmony_ciok(TLSProxy::Message->success(), "Success after HRR");
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci#Test 2: The server sending an HRR requesting a group the client already sent
93e1051a39Sopenharmony_ci#        should fail
94e1051a39Sopenharmony_ci$proxy->clear();
95e1051a39Sopenharmony_ci$proxy->start();
96e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Server asks for group already provided");
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci#Test 3: A missing key_shares extension should not succeed
99e1051a39Sopenharmony_ci$proxy->clear();
100e1051a39Sopenharmony_ci$testtype = MISSING_EXTENSION;
101e1051a39Sopenharmony_ci$proxy->start();
102e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Missing key_shares extension");
103e1051a39Sopenharmony_ci
104e1051a39Sopenharmony_ci#Test 4: No initial acceptable key_shares should succeed after a
105e1051a39Sopenharmony_ci#        HelloRetryRequest
106e1051a39Sopenharmony_ci$proxy->clear();
107e1051a39Sopenharmony_ci$proxy->filter(undef);
108e1051a39Sopenharmony_ciif (disabled("ec")) {
109e1051a39Sopenharmony_ci    $proxy->serverflags("-groups ffdhe3072");
110e1051a39Sopenharmony_ci} else {
111e1051a39Sopenharmony_ci    $proxy->serverflags("-groups P-256");
112e1051a39Sopenharmony_ci}
113e1051a39Sopenharmony_ci$proxy->start();
114e1051a39Sopenharmony_ciok(TLSProxy::Message->success(), "No initial acceptable key_shares");
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ci#Test 5: No acceptable key_shares and no shared groups should fail
117e1051a39Sopenharmony_ci$proxy->clear();
118e1051a39Sopenharmony_ci$proxy->filter(undef);
119e1051a39Sopenharmony_ciif (disabled("ec")) {
120e1051a39Sopenharmony_ci    $proxy->serverflags("-groups ffdhe2048");
121e1051a39Sopenharmony_ci} else {
122e1051a39Sopenharmony_ci    $proxy->serverflags("-groups P-256");
123e1051a39Sopenharmony_ci}
124e1051a39Sopenharmony_ciif (disabled("ec")) {
125e1051a39Sopenharmony_ci    $proxy->clientflags("-groups ffdhe3072");
126e1051a39Sopenharmony_ci} else {
127e1051a39Sopenharmony_ci    $proxy->clientflags("-groups P-384");
128e1051a39Sopenharmony_ci}
129e1051a39Sopenharmony_ci$proxy->start();
130e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "No acceptable key_shares");
131e1051a39Sopenharmony_ci
132e1051a39Sopenharmony_ci#Test 6: A non preferred but acceptable key_share should succeed
133e1051a39Sopenharmony_ci$proxy->clear();
134e1051a39Sopenharmony_ci$proxy->clientflags("-curves P-256");
135e1051a39Sopenharmony_ciif (disabled("ec")) {
136e1051a39Sopenharmony_ci    $proxy->clientflags("-groups ffdhe3072");
137e1051a39Sopenharmony_ci} else {
138e1051a39Sopenharmony_ci    $proxy->clientflags("-groups P-256");
139e1051a39Sopenharmony_ci}
140e1051a39Sopenharmony_ci$proxy->start();
141e1051a39Sopenharmony_ciok(TLSProxy::Message->success(), "Non preferred key_share");
142e1051a39Sopenharmony_ci$proxy->filter(\&modify_key_shares_filter);
143e1051a39Sopenharmony_ci
144e1051a39Sopenharmony_ciSKIP: {
145e1051a39Sopenharmony_ci    skip "No ec support in this OpenSSL build", 1 if disabled("ec");
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_ci    #Test 7: An acceptable key_share after a list of non-acceptable ones should
148e1051a39Sopenharmony_ci    #succeed
149e1051a39Sopenharmony_ci    $proxy->clear();
150e1051a39Sopenharmony_ci    $testtype = ACCEPTABLE_AT_END;
151e1051a39Sopenharmony_ci    $proxy->start();
152e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success(), "Acceptable key_share at end of list");
153e1051a39Sopenharmony_ci}
154e1051a39Sopenharmony_ci
155e1051a39Sopenharmony_ci#Test 8: An acceptable key_share but for a group not in supported_groups should
156e1051a39Sopenharmony_ci#fail
157e1051a39Sopenharmony_ci$proxy->clear();
158e1051a39Sopenharmony_ci$testtype = NOT_IN_SUPPORTED_GROUPS;
159e1051a39Sopenharmony_ci$proxy->start();
160e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Acceptable key_share not in supported_groups");
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci#Test 9: Too short group_id should fail
163e1051a39Sopenharmony_ci$proxy->clear();
164e1051a39Sopenharmony_ci$testtype = GROUP_ID_TOO_SHORT;
165e1051a39Sopenharmony_ci$proxy->start();
166e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Group id too short");
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci#Test 10: key_exchange length mismatch should fail
169e1051a39Sopenharmony_ci$proxy->clear();
170e1051a39Sopenharmony_ci$testtype = KEX_LEN_MISMATCH;
171e1051a39Sopenharmony_ci$proxy->start();
172e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "key_exchange length mismatch");
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_ci#Test 11: Zero length key_exchange should fail
175e1051a39Sopenharmony_ci$proxy->clear();
176e1051a39Sopenharmony_ci$testtype = ZERO_LEN_KEX_DATA;
177e1051a39Sopenharmony_ci$proxy->start();
178e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "zero length key_exchange data");
179e1051a39Sopenharmony_ci
180e1051a39Sopenharmony_ci#Test 12: Trailing data on key_share list should fail
181e1051a39Sopenharmony_ci$proxy->clear();
182e1051a39Sopenharmony_ci$testtype = TRAILING_DATA;
183e1051a39Sopenharmony_ci$proxy->start();
184e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "key_share list trailing data");
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci#Test 13: Multiple acceptable key_shares - we choose the first one
187e1051a39Sopenharmony_ci$proxy->clear();
188e1051a39Sopenharmony_ci$direction = SERVER_TO_CLIENT;
189e1051a39Sopenharmony_ci$testtype = LOOK_ONLY;
190e1051a39Sopenharmony_ci$selectedgroupid = 0;
191e1051a39Sopenharmony_ciif (disabled("ec")) {
192e1051a39Sopenharmony_ci    $proxy->clientflags("-groups ffdhe3072:ffdhe2048");
193e1051a39Sopenharmony_ci} else {
194e1051a39Sopenharmony_ci    $proxy->clientflags("-groups P-256:X25519");
195e1051a39Sopenharmony_ci}
196e1051a39Sopenharmony_ci$proxy->start();
197e1051a39Sopenharmony_ciif (disabled("ec")) {
198e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE3072),
199e1051a39Sopenharmony_ci       "Multiple acceptable key_shares");
200e1051a39Sopenharmony_ci} else {
201e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && ($selectedgroupid == P_256),
202e1051a39Sopenharmony_ci       "Multiple acceptable key_shares");
203e1051a39Sopenharmony_ci}
204e1051a39Sopenharmony_ci
205e1051a39Sopenharmony_ci#Test 14: Multiple acceptable key_shares - we choose the first one (part 2)
206e1051a39Sopenharmony_ci$proxy->clear();
207e1051a39Sopenharmony_ciif (disabled("ec")) {
208e1051a39Sopenharmony_ci    $proxy->clientflags("-curves ffdhe2048:ffdhe3072");
209e1051a39Sopenharmony_ci} else {
210e1051a39Sopenharmony_ci    $proxy->clientflags("-curves X25519:P-256");
211e1051a39Sopenharmony_ci}
212e1051a39Sopenharmony_ci$proxy->start();
213e1051a39Sopenharmony_ciif (disabled("ec")) {
214e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && ($selectedgroupid == FFDHE2048),
215e1051a39Sopenharmony_ci       "Multiple acceptable key_shares (part 2)");
216e1051a39Sopenharmony_ci} else {
217e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && ($selectedgroupid == X25519),
218e1051a39Sopenharmony_ci       "Multiple acceptable key_shares (part 2)");
219e1051a39Sopenharmony_ci}
220e1051a39Sopenharmony_ci
221e1051a39Sopenharmony_ci#Test 15: Server sends key_share that wasn't offered should fail
222e1051a39Sopenharmony_ci$proxy->clear();
223e1051a39Sopenharmony_ci$testtype = SELECT_X25519;
224e1051a39Sopenharmony_ciif (disabled("ec")) {
225e1051a39Sopenharmony_ci    $proxy->clientflags("-groups ffdhe3072");
226e1051a39Sopenharmony_ci} else {
227e1051a39Sopenharmony_ci    $proxy->clientflags("-groups P-256");
228e1051a39Sopenharmony_ci}
229e1051a39Sopenharmony_ci$proxy->start();
230e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Non offered key_share");
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci#Test 16: Too short group_id in ServerHello should fail
233e1051a39Sopenharmony_ci$proxy->clear();
234e1051a39Sopenharmony_ci$testtype = GROUP_ID_TOO_SHORT;
235e1051a39Sopenharmony_ci$proxy->start();
236e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Group id too short in ServerHello");
237e1051a39Sopenharmony_ci
238e1051a39Sopenharmony_ci#Test 17: key_exchange length mismatch in ServerHello should fail
239e1051a39Sopenharmony_ci$proxy->clear();
240e1051a39Sopenharmony_ci$testtype = KEX_LEN_MISMATCH;
241e1051a39Sopenharmony_ci$proxy->start();
242e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "key_exchange length mismatch in ServerHello");
243e1051a39Sopenharmony_ci
244e1051a39Sopenharmony_ci#Test 18: Zero length key_exchange in ServerHello should fail
245e1051a39Sopenharmony_ci$proxy->clear();
246e1051a39Sopenharmony_ci$testtype = ZERO_LEN_KEX_DATA;
247e1051a39Sopenharmony_ci$proxy->start();
248e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "zero length key_exchange data in ServerHello");
249e1051a39Sopenharmony_ci
250e1051a39Sopenharmony_ci#Test 19: Trailing data on key_share in ServerHello should fail
251e1051a39Sopenharmony_ci$proxy->clear();
252e1051a39Sopenharmony_ci$testtype = TRAILING_DATA;
253e1051a39Sopenharmony_ci$proxy->start();
254e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "key_share trailing data in ServerHello");
255e1051a39Sopenharmony_ci
256e1051a39Sopenharmony_ciSKIP: {
257e1051a39Sopenharmony_ci    skip "No TLSv1.2 support in this OpenSSL build", 2 if disabled("tls1_2");
258e1051a39Sopenharmony_ci
259e1051a39Sopenharmony_ci    #Test 20: key_share should not be sent if the client is not capable of
260e1051a39Sopenharmony_ci    #         negotiating TLSv1.3
261e1051a39Sopenharmony_ci    $proxy->clear();
262e1051a39Sopenharmony_ci    $proxy->filter(undef);
263e1051a39Sopenharmony_ci    $proxy->clientflags("-no_tls1_3");
264e1051a39Sopenharmony_ci    $proxy->start();
265e1051a39Sopenharmony_ci    my $clienthello = $proxy->message_list->[0];
266e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success()
267e1051a39Sopenharmony_ci       && !defined $clienthello->extension_data->{TLSProxy::Message::EXT_KEY_SHARE},
268e1051a39Sopenharmony_ci       "No key_share for TLS<=1.2 client");
269e1051a39Sopenharmony_ci    $proxy->filter(\&modify_key_shares_filter);
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_ci    #Test 21: A server not capable of negotiating TLSv1.3 should not attempt to
272e1051a39Sopenharmony_ci    #         process a key_share
273e1051a39Sopenharmony_ci    $proxy->clear();
274e1051a39Sopenharmony_ci    $direction = CLIENT_TO_SERVER;
275e1051a39Sopenharmony_ci    $testtype = NO_ACCEPTABLE_KEY_SHARES;
276e1051a39Sopenharmony_ci    $proxy->serverflags("-no_tls1_3");
277e1051a39Sopenharmony_ci    $proxy->start();
278e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success(), "Ignore key_share for TLS<=1.2 server");
279e1051a39Sopenharmony_ci}
280e1051a39Sopenharmony_ci
281e1051a39Sopenharmony_ci#Test 22: The server sending an HRR but not requesting a new key_share should
282e1051a39Sopenharmony_ci#         fail
283e1051a39Sopenharmony_ci$proxy->clear();
284e1051a39Sopenharmony_ci$direction = SERVER_TO_CLIENT;
285e1051a39Sopenharmony_ci$testtype = NO_KEY_SHARES_IN_HRR;
286e1051a39Sopenharmony_ciif (disabled("ec")) {
287e1051a39Sopenharmony_ci    $proxy->serverflags("-groups ffdhe2048");
288e1051a39Sopenharmony_ci} else {
289e1051a39Sopenharmony_ci    $proxy->serverflags("-groups X25519");
290e1051a39Sopenharmony_ci}
291e1051a39Sopenharmony_ci$proxy->start();
292e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Server sends HRR with no key_shares");
293e1051a39Sopenharmony_ci
294e1051a39Sopenharmony_ciSKIP: {
295e1051a39Sopenharmony_ci    skip "No EC support in this OpenSSL build", 1 if disabled("ec");
296e1051a39Sopenharmony_ci    #Test 23: Trailing data on key_share in ServerHello should fail
297e1051a39Sopenharmony_ci    $proxy->clear();
298e1051a39Sopenharmony_ci    $direction = CLIENT_TO_SERVER;
299e1051a39Sopenharmony_ci    $proxy->clientflags("-groups secp192r1:P-256:X25519");
300e1051a39Sopenharmony_ci    $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
301e1051a39Sopenharmony_ci    $testtype = NON_TLS1_3_KEY_SHARE;
302e1051a39Sopenharmony_ci    $proxy->start();
303e1051a39Sopenharmony_ci    my $ishrr = defined ${$proxy->message_list}[2]
304e1051a39Sopenharmony_ci                &&(${$proxy->message_list}[0]->mt == TLSProxy::Message::MT_CLIENT_HELLO)
305e1051a39Sopenharmony_ci                && (${$proxy->message_list}[2]->mt == TLSProxy::Message::MT_CLIENT_HELLO);
306e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success() && $ishrr,
307e1051a39Sopenharmony_ci       "Client sends a key_share for a Non TLSv1.3 group");
308e1051a39Sopenharmony_ci}
309e1051a39Sopenharmony_ci
310e1051a39Sopenharmony_cisub modify_key_shares_filter
311e1051a39Sopenharmony_ci{
312e1051a39Sopenharmony_ci    my $proxy = shift;
313e1051a39Sopenharmony_ci
314e1051a39Sopenharmony_ci    # We're only interested in the initial ClientHello/SererHello/HRR
315e1051a39Sopenharmony_ci    if (($direction == CLIENT_TO_SERVER && $proxy->flight != 0
316e1051a39Sopenharmony_ci                && ($proxy->flight != 1 || $testtype != NO_KEY_SHARES_IN_HRR))
317e1051a39Sopenharmony_ci            || ($direction == SERVER_TO_CLIENT && $proxy->flight != 1)) {
318e1051a39Sopenharmony_ci        return;
319e1051a39Sopenharmony_ci    }
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    foreach my $message (@{$proxy->message_list}) {
322e1051a39Sopenharmony_ci        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
323e1051a39Sopenharmony_ci                && $direction == CLIENT_TO_SERVER) {
324e1051a39Sopenharmony_ci            my $ext;
325e1051a39Sopenharmony_ci            my $suppgroups;
326e1051a39Sopenharmony_ci
327e1051a39Sopenharmony_ci            if ($testtype != NON_TLS1_3_KEY_SHARE) {
328e1051a39Sopenharmony_ci                #Setup supported groups to include some unrecognised groups
329e1051a39Sopenharmony_ci                $suppgroups = pack "C8",
330e1051a39Sopenharmony_ci                    0x00, 0x06, #List Length
331e1051a39Sopenharmony_ci                    0xff, 0xfe, #Non existing group 1
332e1051a39Sopenharmony_ci                    0xff, 0xff, #Non existing group 2
333e1051a39Sopenharmony_ci                    0x00, 0x1d; #x25519
334e1051a39Sopenharmony_ci            } else {
335e1051a39Sopenharmony_ci                $suppgroups = pack "C6",
336e1051a39Sopenharmony_ci                    0x00, 0x04, #List Length
337e1051a39Sopenharmony_ci                    0x00, 0x13,
338e1051a39Sopenharmony_ci                    0x00, 0x1d; #x25519
339e1051a39Sopenharmony_ci            }
340e1051a39Sopenharmony_ci
341e1051a39Sopenharmony_ci            if ($testtype == EMPTY_EXTENSION) {
342e1051a39Sopenharmony_ci                $ext = pack "C2",
343e1051a39Sopenharmony_ci                    0x00, 0x00;
344e1051a39Sopenharmony_ci            } elsif ($testtype == NO_ACCEPTABLE_KEY_SHARES) {
345e1051a39Sopenharmony_ci                $ext = pack "C12",
346e1051a39Sopenharmony_ci                    0x00, 0x0a, #List Length
347e1051a39Sopenharmony_ci                    0xff, 0xfe, #Non existing group 1
348e1051a39Sopenharmony_ci                    0x00, 0x01, 0xff, #key_exchange data
349e1051a39Sopenharmony_ci                    0xff, 0xff, #Non existing group 2
350e1051a39Sopenharmony_ci                    0x00, 0x01, 0xff; #key_exchange data
351e1051a39Sopenharmony_ci            } elsif ($testtype == ACCEPTABLE_AT_END) {
352e1051a39Sopenharmony_ci                $ext = pack "C11H64",
353e1051a39Sopenharmony_ci                    0x00, 0x29, #List Length
354e1051a39Sopenharmony_ci                    0xff, 0xfe, #Non existing group 1
355e1051a39Sopenharmony_ci                    0x00, 0x01, 0xff, #key_exchange data
356e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
357e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
358e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
359e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421";  #key_exchange data
360e1051a39Sopenharmony_ci            } elsif ($testtype == NOT_IN_SUPPORTED_GROUPS) {
361e1051a39Sopenharmony_ci                $suppgroups = pack "C4",
362e1051a39Sopenharmony_ci                    0x00, 0x02, #List Length
363e1051a39Sopenharmony_ci                    0x00, 0xfe; #Non existing group 1
364e1051a39Sopenharmony_ci            } elsif ($testtype == GROUP_ID_TOO_SHORT) {
365e1051a39Sopenharmony_ci                $ext = pack "C6H64C1",
366e1051a39Sopenharmony_ci                    0x00, 0x25, #List Length
367e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
368e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
369e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
370e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421";  #key_exchange data
371e1051a39Sopenharmony_ci                    0x00;       #Group id too short
372e1051a39Sopenharmony_ci            } elsif ($testtype == KEX_LEN_MISMATCH) {
373e1051a39Sopenharmony_ci                $ext = pack "C8",
374e1051a39Sopenharmony_ci                    0x00, 0x06, #List Length
375e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
376e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
377e1051a39Sopenharmony_ci                    0x15, 0x51; #Only two bytes of data, but length should be 32
378e1051a39Sopenharmony_ci            } elsif ($testtype == ZERO_LEN_KEX_DATA) {
379e1051a39Sopenharmony_ci                $ext = pack "C10H64",
380e1051a39Sopenharmony_ci                    0x00, 0x28, #List Length
381e1051a39Sopenharmony_ci                    0xff, 0xfe, #Non existing group 1
382e1051a39Sopenharmony_ci                    0x00, 0x00, #zero length key_exchange data is invalid
383e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
384e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
385e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
386e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421";  #key_exchange data
387e1051a39Sopenharmony_ci            } elsif ($testtype == TRAILING_DATA) {
388e1051a39Sopenharmony_ci                $ext = pack "C6H64C1",
389e1051a39Sopenharmony_ci                    0x00, 0x24, #List Length
390e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
391e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
392e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
393e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421", #key_exchange data
394e1051a39Sopenharmony_ci                    0x00; #Trailing garbage
395e1051a39Sopenharmony_ci            } elsif ($testtype == NO_KEY_SHARES_IN_HRR) {
396e1051a39Sopenharmony_ci                #We trick the server into thinking we sent a P-256 key_share -
397e1051a39Sopenharmony_ci                #but the client actually sent X25519
398e1051a39Sopenharmony_ci                $ext = pack "C7",
399e1051a39Sopenharmony_ci                    0x00, 0x05, #List Length
400e1051a39Sopenharmony_ci                    0x00, 0x17, #P-256
401e1051a39Sopenharmony_ci                    0x00, 0x01, #key_exchange data length
402e1051a39Sopenharmony_ci                    0xff;       #Dummy key_share data
403e1051a39Sopenharmony_ci            } elsif ($testtype == NON_TLS1_3_KEY_SHARE) {
404e1051a39Sopenharmony_ci                $ext = pack "C6H98",
405e1051a39Sopenharmony_ci                    0x00, 0x35, #List Length
406e1051a39Sopenharmony_ci                    0x00, 0x13, #P-192
407e1051a39Sopenharmony_ci                    0x00, 0x31, #key_exchange data length
408e1051a39Sopenharmony_ci                    "04EE3B38D1CB800A1A2B702FC8423599F2AC7161E175C865F8".
409e1051a39Sopenharmony_ci                    "3DAF78BCBAE561464E8144359BE70CB7989D28A2F43F8F2C";  #key_exchange data
410e1051a39Sopenharmony_ci            }
411e1051a39Sopenharmony_ci
412e1051a39Sopenharmony_ci            if ($testtype != EMPTY_EXTENSION
413e1051a39Sopenharmony_ci                    && $testtype != NO_KEY_SHARES_IN_HRR) {
414e1051a39Sopenharmony_ci                $message->set_extension(
415e1051a39Sopenharmony_ci                    TLSProxy::Message::EXT_SUPPORTED_GROUPS, $suppgroups);
416e1051a39Sopenharmony_ci            }
417e1051a39Sopenharmony_ci            if ($testtype == MISSING_EXTENSION) {
418e1051a39Sopenharmony_ci                $message->delete_extension(
419e1051a39Sopenharmony_ci                    TLSProxy::Message::EXT_KEY_SHARE);
420e1051a39Sopenharmony_ci            } elsif ($testtype != NOT_IN_SUPPORTED_GROUPS) {
421e1051a39Sopenharmony_ci                $message->set_extension(
422e1051a39Sopenharmony_ci                    TLSProxy::Message::EXT_KEY_SHARE, $ext);
423e1051a39Sopenharmony_ci            }
424e1051a39Sopenharmony_ci
425e1051a39Sopenharmony_ci            $message->repack();
426e1051a39Sopenharmony_ci        } elsif ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
427e1051a39Sopenharmony_ci                     && $direction == SERVER_TO_CLIENT) {
428e1051a39Sopenharmony_ci            my $ext;
429e1051a39Sopenharmony_ci            my $key_share =
430e1051a39Sopenharmony_ci                $message->extension_data->{TLSProxy::Message::EXT_KEY_SHARE};
431e1051a39Sopenharmony_ci            $selectedgroupid = unpack("n", $key_share);
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ci            if ($testtype == LOOK_ONLY) {
434e1051a39Sopenharmony_ci                return;
435e1051a39Sopenharmony_ci            }
436e1051a39Sopenharmony_ci            if ($testtype == NO_KEY_SHARES_IN_HRR) {
437e1051a39Sopenharmony_ci                $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE);
438e1051a39Sopenharmony_ci                $message->set_extension(TLSProxy::Message::EXT_UNKNOWN, "");
439e1051a39Sopenharmony_ci                $message->repack();
440e1051a39Sopenharmony_ci                return;
441e1051a39Sopenharmony_ci            }
442e1051a39Sopenharmony_ci            if ($testtype == SELECT_X25519) {
443e1051a39Sopenharmony_ci                $ext = pack "C4H64",
444e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
445e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
446e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
447e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421";  #key_exchange data
448e1051a39Sopenharmony_ci            } elsif ($testtype == GROUP_ID_TOO_SHORT) {
449e1051a39Sopenharmony_ci                $ext = pack "C1",
450e1051a39Sopenharmony_ci                    0x00;
451e1051a39Sopenharmony_ci            } elsif ($testtype == KEX_LEN_MISMATCH) {
452e1051a39Sopenharmony_ci                $ext = pack "C6",
453e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
454e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
455e1051a39Sopenharmony_ci                    0x15, 0x51; #Only two bytes of data, but length should be 32
456e1051a39Sopenharmony_ci            } elsif ($testtype == ZERO_LEN_KEX_DATA) {
457e1051a39Sopenharmony_ci                $ext = pack "C4",
458e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
459e1051a39Sopenharmony_ci                    0x00, 0x00, #zero length key_exchange data is invalid
460e1051a39Sopenharmony_ci            } elsif ($testtype == TRAILING_DATA) {
461e1051a39Sopenharmony_ci                $ext = pack "C4H64C1",
462e1051a39Sopenharmony_ci                    0x00, 0x1d, #x25519
463e1051a39Sopenharmony_ci                    0x00, 0x20, #key_exchange data length
464e1051a39Sopenharmony_ci                    "155155B95269ED5C87EAA99C2EF5A593".
465e1051a39Sopenharmony_ci                    "EDF83495E80380089F831B94D14B1421", #key_exchange data
466e1051a39Sopenharmony_ci                    0x00; #Trailing garbage
467e1051a39Sopenharmony_ci            }
468e1051a39Sopenharmony_ci            $message->set_extension(TLSProxy::Message::EXT_KEY_SHARE, $ext);
469e1051a39Sopenharmony_ci
470e1051a39Sopenharmony_ci            $message->repack();
471e1051a39Sopenharmony_ci        }
472e1051a39Sopenharmony_ci    }
473e1051a39Sopenharmony_ci}
474e1051a39Sopenharmony_ci
475e1051a39Sopenharmony_ci
476