239eabfd5e7ac2be9f03e4e192225c2c78d1b26e
[openssl.git] / test / recipes / 70-test_tls13messages.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2016 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_tls13messages";
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::DEFAULT_EXTENSIONS],
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_EC_POINT_FORMATS,
103         checkhandshake::DEFAULT_EXTENSIONS],
104     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
105         checkhandshake::DEFAULT_EXTENSIONS],
106     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
107         checkhandshake::ALPN_CLI_EXTENSION],
108     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
109         checkhandshake::SCT_CLI_EXTENSION],
110     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
111         checkhandshake::DEFAULT_EXTENSIONS],
112     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
113         checkhandshake::DEFAULT_EXTENSIONS],
114     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
115         checkhandshake::DEFAULT_EXTENSIONS],
116     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
117         checkhandshake::DEFAULT_EXTENSIONS],
118     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
119         checkhandshake::DEFAULT_EXTENSIONS],
120     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
121         checkhandshake::DEFAULT_EXTENSIONS],
122     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
123         checkhandshake::PSK_CLI_EXTENSION],
124
125     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
126         checkhandshake::DEFAULT_EXTENSIONS],
127     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
128         checkhandshake::PSK_SRV_EXTENSION],
129
130     [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_SERVER_NAME,
131         checkhandshake::SERVER_NAME_SRV_EXTENSION],
132     [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_ALPN,
133         checkhandshake::ALPN_SRV_EXTENSION],
134     [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
135         checkhandshake::SUPPORTED_GROUPS_SRV_EXTENSION],
136
137     [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
138         checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
139     [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_SCT,
140         checkhandshake::SCT_SRV_EXTENSION],
141
142     [0,0,0]
143 );
144
145 my $proxy = TLSProxy::Proxy->new(
146     undef,
147     cmdstr(app(["openssl"]), display => 1),
148     srctop_file("apps", "server.pem"),
149     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
150 );
151
152 #Test 1: Check we get all the right messages for a default handshake
153 (undef, my $session) = tempfile();
154 $proxy->serverconnects(2);
155 $proxy->clientflags("-sess_out ".$session);
156 $proxy->sessionfile($session);
157 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
158 plan tests => 16;
159 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
160                checkhandshake::DEFAULT_EXTENSIONS,
161                "Default handshake test");
162
163 #Test 2: Resumption handshake
164 $proxy->clearClient();
165 $proxy->clientflags("-sess_in ".$session);
166 $proxy->clientstart();
167 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
168                (checkhandshake::DEFAULT_EXTENSIONS
169                 | checkhandshake::PSK_CLI_EXTENSION
170                 | checkhandshake::PSK_SRV_EXTENSION)
171                & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
172                "Resumption handshake test");
173
174 #Test 3: A status_request handshake (client request only)
175 $proxy->clear();
176 $proxy->clientflags("-status");
177 $proxy->start();
178 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
179                checkhandshake::DEFAULT_EXTENSIONS
180                | checkhandshake::STATUS_REQUEST_CLI_EXTENSION,
181                "status_request handshake test (client)");
182
183 #Test 4: A status_request handshake (server support only)
184 $proxy->clear();
185 $proxy->serverflags("-status_file "
186                     .srctop_file("test", "recipes", "ocsp-response.der"));
187 $proxy->start();
188 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
189                checkhandshake::DEFAULT_EXTENSIONS,
190                "status_request handshake test (server)");
191
192 #Test 5: A status_request handshake (client and server)
193 #TODO(TLS1.3): TLS1.3 doesn't actually have CertificateStatus messages. This is
194 #a temporary test until such time as we do proper TLS1.3 style certificate
195 #status
196 $proxy->clear();
197 $proxy->clientflags("-status");
198 $proxy->serverflags("-status_file "
199                     .srctop_file("test", "recipes", "ocsp-response.der"));
200 $proxy->start();
201 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
202                checkhandshake::DEFAULT_EXTENSIONS
203                | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
204                | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
205                "status_request handshake test");
206
207 #Test 6: A client auth handshake
208 $proxy->clear();
209 $proxy->clientflags("-cert ".srctop_file("apps", "server.pem"));
210 $proxy->serverflags("-Verify 5");
211 $proxy->start();
212 checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
213                checkhandshake::DEFAULT_EXTENSIONS,
214                "Client auth handshake test");
215
216 #Test 7: Server name handshake (no client request)
217 $proxy->clear();
218 $proxy->clientflags("-noservername");
219 $proxy->start();
220 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
221                checkhandshake::DEFAULT_EXTENSIONS
222                & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
223                "Server name handshake test (client)");
224
225 #Test 8: Server name handshake (server support only)
226 $proxy->clear();
227 $proxy->clientflags("-noservername");
228 $proxy->serverflags("-servername testhost");
229 $proxy->start();
230 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
231                checkhandshake::DEFAULT_EXTENSIONS
232                & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
233                "Server name handshake test (server)");
234
235 #Test 9: Server name handshake (client and server)
236 $proxy->clear();
237 $proxy->clientflags("-servername testhost");
238 $proxy->serverflags("-servername testhost");
239 $proxy->start();
240 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
241                checkhandshake::DEFAULT_EXTENSIONS
242                | checkhandshake::SERVER_NAME_SRV_EXTENSION,
243                "Server name handshake test");
244
245 #Test 10: ALPN handshake (client request only)
246 $proxy->clear();
247 $proxy->clientflags("-alpn test");
248 $proxy->start();
249 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
250                checkhandshake::DEFAULT_EXTENSIONS
251                | checkhandshake::ALPN_CLI_EXTENSION,
252                "ALPN handshake test (client)");
253
254 #Test 11: ALPN handshake (server support only)
255 $proxy->clear();
256 $proxy->serverflags("-alpn test");
257 $proxy->start();
258 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
259                checkhandshake::DEFAULT_EXTENSIONS,
260                "ALPN handshake test (server)");
261
262 #Test 12: ALPN handshake (client and server)
263 $proxy->clear();
264 $proxy->clientflags("-alpn test");
265 $proxy->serverflags("-alpn test");
266 $proxy->start();
267 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
268                checkhandshake::DEFAULT_EXTENSIONS
269                | checkhandshake::ALPN_CLI_EXTENSION
270                | checkhandshake::ALPN_SRV_EXTENSION,
271                "ALPN handshake test");
272
273 SKIP: {
274     skip "No CT, EC or OCSP support in this OpenSSL build", 1
275         if disabled("ct") || disabled("ec") || disabled("ocsp");
276
277     #Test 13: SCT handshake (client request only)
278     $proxy->clear();
279     #Note: -ct also sends status_request
280     $proxy->clientflags("-ct");
281     $proxy->serverflags("-status_file "
282                         .srctop_file("test", "recipes", "ocsp-response.der")
283                         ." -serverinfo ".srctop_file("test", "serverinfo2.pem"));
284     $proxy->start();
285     checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
286                    checkhandshake::DEFAULT_EXTENSIONS
287                    | checkhandshake::SCT_CLI_EXTENSION
288                    | checkhandshake::SCT_SRV_EXTENSION
289                    | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
290                    | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
291                    "SCT handshake test");
292 }
293
294
295
296
297 #Test 14: HRR Handshake
298 $proxy->clear();
299 $proxy->serverflags("-curves P-256");
300 $proxy->start();
301 checkhandshake($proxy, checkhandshake::HRR_HANDSHAKE,
302                checkhandshake::DEFAULT_EXTENSIONS
303                | checkhandshake::KEY_SHARE_HRR_EXTENSION,
304                "HRR handshake test");
305
306 #Test 15: Resumption handshake with HRR
307 $proxy->clear();
308 $proxy->clientflags("-sess_in ".$session);
309 $proxy->serverflags("-curves P-256");
310 $proxy->start();
311 checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
312                (checkhandshake::DEFAULT_EXTENSIONS
313                 | checkhandshake::KEY_SHARE_HRR_EXTENSION
314                 | checkhandshake::PSK_CLI_EXTENSION
315                 | checkhandshake::PSK_SRV_EXTENSION)
316                & ~checkhandshake::SERVER_NAME_CLI_EXTENSION,
317                "Resumption handshake with HRR test");
318
319 #Test 16: Acceptable but non preferred key_share
320 $proxy->clear();
321 $proxy->clientflags("-curves P-256");
322 $proxy->start();
323 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
324                checkhandshake::DEFAULT_EXTENSIONS
325                | checkhandshake::SUPPORTED_GROUPS_SRV_EXTENSION,
326                "Default handshake test");
327
328 unlink $session;