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