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