1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2017-2021 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 srctop_dir bldtop_dir/;
11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
12e1051a39Sopenharmony_ciuse File::Temp qw(tempfile);
13e1051a39Sopenharmony_ciuse TLSProxy::Proxy;
14e1051a39Sopenharmony_ciuse checkhandshake qw(checkhandshake @handmessages @extensions);
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_cimy $test_name = "test_tls13kexmodes";
17e1051a39Sopenharmony_cisetup($test_name);
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O"
20e1051a39Sopenharmony_ci    if $^O =~ /^(VMS)$/;
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled"
23e1051a39Sopenharmony_ci    if disabled("engine") || disabled("dynamic-engine");
24e1051a39Sopenharmony_ci
25e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled"
26e1051a39Sopenharmony_ci    if disabled("sock");
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLSv1.3 enabled"
29e1051a39Sopenharmony_ci    if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ciplan skip_all => "$test_name needs EC enabled"
32e1051a39Sopenharmony_ci    if disabled("ec");
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ci@handmessages = (
38e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO,
39e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES],
40e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO,
41e1051a39Sopenharmony_ci        checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
42e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO,
43e1051a39Sopenharmony_ci        checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
44e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO,
45e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES],
46e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
47e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES],
48e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
49e1051a39Sopenharmony_ci        checkhandshake::CLIENT_AUTH_HANDSHAKE],
50e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE,
51e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
52e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
53e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
54e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_FINISHED,
55e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES],
56e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE,
57e1051a39Sopenharmony_ci        checkhandshake::CLIENT_AUTH_HANDSHAKE],
58e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
59e1051a39Sopenharmony_ci        checkhandshake::CLIENT_AUTH_HANDSHAKE],
60e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_FINISHED,
61e1051a39Sopenharmony_ci        checkhandshake::ALL_HANDSHAKES],
62e1051a39Sopenharmony_ci    [0, 0]
63e1051a39Sopenharmony_ci);
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci@extensions = (
66e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
67e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
68e1051a39Sopenharmony_ci        checkhandshake::SERVER_NAME_CLI_EXTENSION],
69e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
70e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
71e1051a39Sopenharmony_ci        checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
72e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
73e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
74e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
75e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
76e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
77e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
78e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
79e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
80e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
81e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
82e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
83e1051a39Sopenharmony_ci        checkhandshake::ALPN_CLI_EXTENSION],
84e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
85e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
86e1051a39Sopenharmony_ci        checkhandshake::SCT_CLI_EXTENSION],
87e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
88e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
89e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
90e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
91e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
92e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
93e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
94e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
95e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
96e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
97e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
98e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
99e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
100e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
101e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
102e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
103e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
104e1051a39Sopenharmony_ci        checkhandshake::PSK_KEX_MODES_EXTENSION],
105e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
106e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
107e1051a39Sopenharmony_ci        checkhandshake::PSK_CLI_EXTENSION],
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
110e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
111e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
112e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
113e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
114e1051a39Sopenharmony_ci        checkhandshake::KEY_SHARE_HRR_EXTENSION],
115e1051a39Sopenharmony_ci
116e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
117e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
118e1051a39Sopenharmony_ci        checkhandshake::SERVER_NAME_CLI_EXTENSION],
119e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
120e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
121e1051a39Sopenharmony_ci        checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
122e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
123e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
124e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
125e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
126e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
127e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
128e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
129e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
130e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
131e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
132e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
133e1051a39Sopenharmony_ci        checkhandshake::ALPN_CLI_EXTENSION],
134e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
135e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
136e1051a39Sopenharmony_ci        checkhandshake::SCT_CLI_EXTENSION],
137e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
138e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
139e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
140e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
141e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
142e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
143e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
144e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
145e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
146e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
147e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
148e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
149e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
150e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
151e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
152e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
153e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
154e1051a39Sopenharmony_ci        checkhandshake::PSK_KEX_MODES_EXTENSION],
155e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
156e1051a39Sopenharmony_ci        TLSProxy::Message::CLIENT,
157e1051a39Sopenharmony_ci        checkhandshake::PSK_CLI_EXTENSION],
158e1051a39Sopenharmony_ci
159e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
160e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
161e1051a39Sopenharmony_ci        checkhandshake::DEFAULT_EXTENSIONS],
162e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
163e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
164e1051a39Sopenharmony_ci        checkhandshake::KEY_SHARE_SRV_EXTENSION],
165e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
166e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
167e1051a39Sopenharmony_ci        checkhandshake::PSK_SRV_EXTENSION],
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ci    [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
170e1051a39Sopenharmony_ci        TLSProxy::Message::SERVER,
171e1051a39Sopenharmony_ci        checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
172e1051a39Sopenharmony_ci    [0,0,0,0]
173e1051a39Sopenharmony_ci);
174e1051a39Sopenharmony_ci
175e1051a39Sopenharmony_ciuse constant {
176e1051a39Sopenharmony_ci    DELETE_EXTENSION => 0,
177e1051a39Sopenharmony_ci    EMPTY_EXTENSION => 1,
178e1051a39Sopenharmony_ci    NON_DHE_KEX_MODE_ONLY => 2,
179e1051a39Sopenharmony_ci    DHE_KEX_MODE_ONLY => 3,
180e1051a39Sopenharmony_ci    UNKNOWN_KEX_MODES => 4,
181e1051a39Sopenharmony_ci    BOTH_KEX_MODES => 5
182e1051a39Sopenharmony_ci};
183e1051a39Sopenharmony_ci
184e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new(
185e1051a39Sopenharmony_ci    undef,
186e1051a39Sopenharmony_ci    cmdstr(app(["openssl"]), display => 1),
187e1051a39Sopenharmony_ci    srctop_file("apps", "server.pem"),
188e1051a39Sopenharmony_ci    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
189e1051a39Sopenharmony_ci);
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci#Test 1: First get a session
192e1051a39Sopenharmony_ci(undef, my $session) = tempfile();
193e1051a39Sopenharmony_ci$proxy->clientflags("-sess_out ".$session);
194e1051a39Sopenharmony_ci$proxy->serverflags("-servername localhost");
195e1051a39Sopenharmony_ci$proxy->sessionfile($session);
196e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
197e1051a39Sopenharmony_ciplan tests => 11;
198e1051a39Sopenharmony_ciok(TLSProxy::Message->success(), "Initial connection");
199e1051a39Sopenharmony_ci
200e1051a39Sopenharmony_ci#Test 2: Attempt a resume with no kex modes extension. Should fail (server
201e1051a39Sopenharmony_ci#        MUST abort handshake with pre_shared key and no psk_kex_modes)
202e1051a39Sopenharmony_ci$proxy->clear();
203e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
204e1051a39Sopenharmony_cimy $testtype = DELETE_EXTENSION;
205e1051a39Sopenharmony_ci$proxy->filter(\&modify_kex_modes_filter);
206e1051a39Sopenharmony_ci$proxy->start();
207e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Resume with no kex modes");
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ci#Test 3: Attempt a resume with empty kex modes extension. Should fail (empty
210e1051a39Sopenharmony_ci#        extension is invalid)
211e1051a39Sopenharmony_ci$proxy->clear();
212e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
213e1051a39Sopenharmony_ci$testtype = EMPTY_EXTENSION;
214e1051a39Sopenharmony_ci$proxy->start();
215e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Resume with empty kex modes");
216e1051a39Sopenharmony_ci
217e1051a39Sopenharmony_ci#Test 4: Attempt a resume with non-dhe kex mode only. Should resume without a
218e1051a39Sopenharmony_ci#        key_share
219e1051a39Sopenharmony_ci$proxy->clear();
220e1051a39Sopenharmony_ci$proxy->clientflags("-allow_no_dhe_kex -sess_in ".$session);
221e1051a39Sopenharmony_ci$proxy->serverflags("-allow_no_dhe_kex");
222e1051a39Sopenharmony_ci$testtype = NON_DHE_KEX_MODE_ONLY;
223e1051a39Sopenharmony_ci$proxy->start();
224e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
225e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
226e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
227e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
228e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
229e1051a39Sopenharmony_ci               "Resume with non-dhe kex mode");
230e1051a39Sopenharmony_ci
231e1051a39Sopenharmony_ci#Test 5: Attempt a resume with dhe kex mode only. Should resume with a key_share
232e1051a39Sopenharmony_ci$proxy->clear();
233e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
234e1051a39Sopenharmony_ci$testtype = DHE_KEX_MODE_ONLY;
235e1051a39Sopenharmony_ci$proxy->start();
236e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
237e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
238e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
239e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_SRV_EXTENSION
240e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
241e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
242e1051a39Sopenharmony_ci               "Resume with non-dhe kex mode");
243e1051a39Sopenharmony_ci
244e1051a39Sopenharmony_ci#Test 6: Attempt a resume with only unrecognised kex modes. Should not resume
245e1051a39Sopenharmony_ci#        but rather fall back to full handshake
246e1051a39Sopenharmony_ci$proxy->clear();
247e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
248e1051a39Sopenharmony_ci$testtype = UNKNOWN_KEX_MODES;
249e1051a39Sopenharmony_ci$proxy->start();
250e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
251e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
252e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
253e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_SRV_EXTENSION
254e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION,
255e1051a39Sopenharmony_ci               "Resume with unrecognized kex mode");
256e1051a39Sopenharmony_ci
257e1051a39Sopenharmony_ci#Test 7: Attempt a resume with both non-dhe and dhe kex mode. Should resume with
258e1051a39Sopenharmony_ci#        a key_share
259e1051a39Sopenharmony_ci$proxy->clear();
260e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
261e1051a39Sopenharmony_ci$testtype = BOTH_KEX_MODES;
262e1051a39Sopenharmony_ci$proxy->start();
263e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
264e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
265e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
266e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_SRV_EXTENSION
267e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
268e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
269e1051a39Sopenharmony_ci               "Resume with non-dhe kex mode");
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_ci#Test 8: Attempt a resume with both non-dhe and dhe kex mode, but unacceptable
272e1051a39Sopenharmony_ci#        initial key_share. Should resume with a key_share following an HRR
273e1051a39Sopenharmony_ci$proxy->clear();
274e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
275e1051a39Sopenharmony_ci$proxy->serverflags("-curves P-256");
276e1051a39Sopenharmony_ci$testtype = BOTH_KEX_MODES;
277e1051a39Sopenharmony_ci$proxy->start();
278e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
279e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
280e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
281e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_SRV_EXTENSION
282e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_HRR_EXTENSION
283e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
284e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
285e1051a39Sopenharmony_ci               "Resume with both kex modes and HRR");
286e1051a39Sopenharmony_ci
287e1051a39Sopenharmony_ci#Test 9: Attempt a resume with dhe kex mode only and an unacceptable initial
288e1051a39Sopenharmony_ci#        key_share. Should resume with a key_share following an HRR
289e1051a39Sopenharmony_ci$proxy->clear();
290e1051a39Sopenharmony_ci$proxy->clientflags("-sess_in ".$session);
291e1051a39Sopenharmony_ci$proxy->serverflags("-curves P-256");
292e1051a39Sopenharmony_ci$testtype = DHE_KEX_MODE_ONLY;
293e1051a39Sopenharmony_ci$proxy->start();
294e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
295e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
296e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
297e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_SRV_EXTENSION
298e1051a39Sopenharmony_ci               | checkhandshake::KEY_SHARE_HRR_EXTENSION
299e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
300e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
301e1051a39Sopenharmony_ci               "Resume with dhe kex mode and HRR");
302e1051a39Sopenharmony_ci
303e1051a39Sopenharmony_ci#Test 10: Attempt a resume with both non-dhe and dhe kex mode, unacceptable
304e1051a39Sopenharmony_ci#         initial key_share and no overlapping groups. Should resume without a
305e1051a39Sopenharmony_ci#         key_share
306e1051a39Sopenharmony_ci$proxy->clear();
307e1051a39Sopenharmony_ci$proxy->clientflags("-allow_no_dhe_kex -curves P-384 -sess_in ".$session);
308e1051a39Sopenharmony_ci$proxy->serverflags("-allow_no_dhe_kex -curves P-256");
309e1051a39Sopenharmony_ci$testtype = BOTH_KEX_MODES;
310e1051a39Sopenharmony_ci$proxy->start();
311e1051a39Sopenharmony_cicheckhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
312e1051a39Sopenharmony_ci               checkhandshake::DEFAULT_EXTENSIONS
313e1051a39Sopenharmony_ci               | checkhandshake::PSK_KEX_MODES_EXTENSION
314e1051a39Sopenharmony_ci               | checkhandshake::PSK_CLI_EXTENSION
315e1051a39Sopenharmony_ci               | checkhandshake::PSK_SRV_EXTENSION,
316e1051a39Sopenharmony_ci               "Resume with both kex modes, no overlapping groups");
317e1051a39Sopenharmony_ci
318e1051a39Sopenharmony_ci#Test 11: Attempt a resume with dhe kex mode only, unacceptable
319e1051a39Sopenharmony_ci#         initial key_share and no overlapping groups. Should fail
320e1051a39Sopenharmony_ci$proxy->clear();
321e1051a39Sopenharmony_ci$proxy->clientflags("-curves P-384 -sess_in ".$session);
322e1051a39Sopenharmony_ci$proxy->serverflags("-curves P-256");
323e1051a39Sopenharmony_ci$testtype = DHE_KEX_MODE_ONLY;
324e1051a39Sopenharmony_ci$proxy->start();
325e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Resume with dhe kex mode, no overlapping groups");
326e1051a39Sopenharmony_ci
327e1051a39Sopenharmony_ciunlink $session;
328e1051a39Sopenharmony_ci
329e1051a39Sopenharmony_cisub modify_kex_modes_filter
330e1051a39Sopenharmony_ci{
331e1051a39Sopenharmony_ci    my $proxy = shift;
332e1051a39Sopenharmony_ci
333e1051a39Sopenharmony_ci    # We're only interested in the initial ClientHello
334e1051a39Sopenharmony_ci    return if ($proxy->flight != 0);
335e1051a39Sopenharmony_ci
336e1051a39Sopenharmony_ci    foreach my $message (@{$proxy->message_list}) {
337e1051a39Sopenharmony_ci        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
338e1051a39Sopenharmony_ci            my $ext;
339e1051a39Sopenharmony_ci
340e1051a39Sopenharmony_ci            if ($testtype == EMPTY_EXTENSION) {
341e1051a39Sopenharmony_ci                $ext = pack "C",
342e1051a39Sopenharmony_ci                    0x00;       #List length
343e1051a39Sopenharmony_ci            } elsif ($testtype == NON_DHE_KEX_MODE_ONLY) {
344e1051a39Sopenharmony_ci                $ext = pack "C2",
345e1051a39Sopenharmony_ci                    0x01,       #List length
346e1051a39Sopenharmony_ci                    0x00;       #psk_ke
347e1051a39Sopenharmony_ci            } elsif ($testtype == DHE_KEX_MODE_ONLY) {
348e1051a39Sopenharmony_ci                $ext = pack "C2",
349e1051a39Sopenharmony_ci                    0x01,       #List length
350e1051a39Sopenharmony_ci                    0x01;       #psk_dhe_ke
351e1051a39Sopenharmony_ci            } elsif ($testtype == UNKNOWN_KEX_MODES) {
352e1051a39Sopenharmony_ci                $ext = pack "C3",
353e1051a39Sopenharmony_ci                    0x02,       #List length
354e1051a39Sopenharmony_ci                    0xfe,       #unknown
355e1051a39Sopenharmony_ci                    0xff;       #unknown
356e1051a39Sopenharmony_ci            } elsif ($testtype == BOTH_KEX_MODES) {
357e1051a39Sopenharmony_ci                #We deliberately list psk_ke first...should still use psk_dhe_ke
358e1051a39Sopenharmony_ci                $ext = pack "C3",
359e1051a39Sopenharmony_ci                    0x02,       #List length
360e1051a39Sopenharmony_ci                    0x00,       #psk_ke
361e1051a39Sopenharmony_ci                    0x01;       #psk_dhe_ke
362e1051a39Sopenharmony_ci            }
363e1051a39Sopenharmony_ci
364e1051a39Sopenharmony_ci            if ($testtype == DELETE_EXTENSION) {
365e1051a39Sopenharmony_ci                $message->delete_extension(
366e1051a39Sopenharmony_ci                    TLSProxy::Message::EXT_PSK_KEX_MODES);
367e1051a39Sopenharmony_ci            } else {
368e1051a39Sopenharmony_ci                $message->set_extension(
369e1051a39Sopenharmony_ci                    TLSProxy::Message::EXT_PSK_KEX_MODES, $ext);
370e1051a39Sopenharmony_ci            }
371e1051a39Sopenharmony_ci
372e1051a39Sopenharmony_ci            $message->repack();
373e1051a39Sopenharmony_ci        }
374e1051a39Sopenharmony_ci    }
375e1051a39Sopenharmony_ci}
376