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