Move the checkhandshake.pm module into test/testlib
[openssl.git] / test / recipes / 70-test_sslmessages.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_sslmessages";
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 TLS enabled"
29     if alldisabled(available_protocols("tls"));
30
31 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
33
34 my $proxy = TLSProxy::Proxy->new(
35     undef,
36     cmdstr(app(["openssl"]), display => 1),
37     srctop_file("apps", "server.pem"),
38     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
39 );
40
41 @handmessages = (
42     [TLSProxy::Message::MT_CLIENT_HELLO,
43         checkhandshake::ALL_HANDSHAKES],
44     [TLSProxy::Message::MT_SERVER_HELLO,
45         checkhandshake::ALL_HANDSHAKES],
46     [TLSProxy::Message::MT_CERTIFICATE,
47         checkhandshake::ALL_HANDSHAKES
48         & ~checkhandshake::RESUME_HANDSHAKE],
49     [TLSProxy::Message::MT_CERTIFICATE_STATUS,
50         checkhandshake::OCSP_HANDSHAKE],
51     #ServerKeyExchange handshakes not currently supported by TLSProxy
52     [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
53         checkhandshake::CLIENT_AUTH_HANDSHAKE],
54     [TLSProxy::Message::MT_SERVER_HELLO_DONE,
55         checkhandshake::ALL_HANDSHAKES
56         & ~checkhandshake::RESUME_HANDSHAKE],
57     [TLSProxy::Message::MT_CERTIFICATE,
58         checkhandshake::CLIENT_AUTH_HANDSHAKE],
59     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
60         checkhandshake::ALL_HANDSHAKES
61         & ~checkhandshake::RESUME_HANDSHAKE],
62     [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
63         checkhandshake::CLIENT_AUTH_HANDSHAKE],
64     [TLSProxy::Message::MT_NEXT_PROTO,
65         checkhandshake::NPN_HANDSHAKE],
66     [TLSProxy::Message::MT_FINISHED,
67         checkhandshake::ALL_HANDSHAKES],
68     [TLSProxy::Message::MT_NEW_SESSION_TICKET,
69         checkhandshake::ALL_HANDSHAKES
70         & ~checkhandshake::RESUME_HANDSHAKE],
71     [TLSProxy::Message::MT_FINISHED,
72         checkhandshake::ALL_HANDSHAKES],
73     [TLSProxy::Message::MT_CLIENT_HELLO,
74         checkhandshake::RENEG_HANDSHAKE],
75     [TLSProxy::Message::MT_SERVER_HELLO,
76         checkhandshake::RENEG_HANDSHAKE],
77     [TLSProxy::Message::MT_CERTIFICATE,
78         checkhandshake::RENEG_HANDSHAKE],
79     [TLSProxy::Message::MT_SERVER_HELLO_DONE,
80         checkhandshake::RENEG_HANDSHAKE],
81     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE,
82         checkhandshake::RENEG_HANDSHAKE],
83     [TLSProxy::Message::MT_FINISHED,
84         checkhandshake::RENEG_HANDSHAKE],
85     [TLSProxy::Message::MT_NEW_SESSION_TICKET,
86         checkhandshake::RENEG_HANDSHAKE],
87     [TLSProxy::Message::MT_FINISHED,
88         checkhandshake::RENEG_HANDSHAKE],
89     [0, 0]
90 );
91
92 @extensions = (
93     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
94         checkhandshake::SERVER_NAME_CLI_EXTENSION],
95     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
96         checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
97     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
98         checkhandshake::DEFAULT_EXTENSIONS],
99     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
100         checkhandshake::DEFAULT_EXTENSIONS],
101     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
102         checkhandshake::DEFAULT_EXTENSIONS],
103     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
104         checkhandshake::ALPN_CLI_EXTENSION],
105     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
106         checkhandshake::SCT_CLI_EXTENSION],
107     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
108         checkhandshake::DEFAULT_EXTENSIONS],
109     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
110         checkhandshake::DEFAULT_EXTENSIONS],
111     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
112         checkhandshake::DEFAULT_EXTENSIONS],
113     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
114         checkhandshake::RENEGOTIATE_CLI_EXTENSION],
115     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_NPN,
116         checkhandshake::NPN_CLI_EXTENSION],
117     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SRP,
118         checkhandshake::SRP_CLI_EXTENSION],
119
120     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_RENEGOTIATE,
121         checkhandshake::DEFAULT_EXTENSIONS],
122     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
123         checkhandshake::DEFAULT_EXTENSIONS],
124     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
125         checkhandshake::DEFAULT_EXTENSIONS],
126     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
127         checkhandshake::SESSION_TICKET_SRV_EXTENSION],
128     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
129         checkhandshake::SERVER_NAME_SRV_EXTENSION],
130     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
131         checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
132     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ALPN,
133         checkhandshake::ALPN_SRV_EXTENSION],
134     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SCT,
135         checkhandshake::SCT_SRV_EXTENSION],
136     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_NPN,
137         checkhandshake::NPN_SRV_EXTENSION],
138     [0,0,0]
139 );
140
141 #Test 1: Check we get all the right messages for a default handshake
142 (undef, my $session) = tempfile();
143 $proxy->serverconnects(2);
144 $proxy->clientflags("-no_tls1_3 -sess_out ".$session);
145 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
146 plan tests => 20;
147 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
148                checkhandshake::DEFAULT_EXTENSIONS,
149                "Default handshake test");
150
151 #Test 2: Resumption handshake
152 $proxy->clearClient();
153 $proxy->clientflags("-no_tls1_3 -sess_in ".$session);
154 $proxy->clientstart();
155 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
156                checkhandshake::DEFAULT_EXTENSIONS
157                & ~checkhandshake::SESSION_TICKET_SRV_EXTENSION,
158                "Resumption handshake test");
159 unlink $session;
160
161 #Test 3: A status_request handshake (client request only)
162 $proxy->clear();
163 $proxy->clientflags("-no_tls1_3 -status");
164 $proxy->start();
165 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
166               checkhandshake::DEFAULT_EXTENSIONS
167               | checkhandshake::STATUS_REQUEST_CLI_EXTENSION,
168               "status_request handshake test (client)");
169
170 #Test 4: A status_request handshake (server support only)
171 $proxy->clear();
172 $proxy->clientflags("-no_tls1_3");
173 $proxy->serverflags("-status_file "
174                     .srctop_file("test", "recipes", "ocsp-response.der"));
175 $proxy->start();
176 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
177                checkhandshake::DEFAULT_EXTENSIONS,
178               "status_request handshake test (server)");
179
180 #Test 5: A status_request handshake (client and server)
181 $proxy->clear();
182 $proxy->clientflags("-no_tls1_3 -status");
183 $proxy->serverflags("-status_file "
184                     .srctop_file("test", "recipes", "ocsp-response.der"));
185 $proxy->start();
186 checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
187               checkhandshake::DEFAULT_EXTENSIONS
188               | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
189               | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
190               "status_request handshake test");
191
192 #Test 6: A client auth handshake
193 $proxy->clear();
194 $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
195 $proxy->serverflags("-Verify 5");
196 $proxy->start();
197 checkhandshake($proxy, checkhandshake::CLIENT_AUTH_HANDSHAKE,
198                checkhandshake::DEFAULT_EXTENSIONS,
199                "Client auth handshake test");
200
201 #Test 7: A handshake with a renegotiation
202 $proxy->clear();
203 $proxy->clientflags("-no_tls1_3");
204 $proxy->reneg(1);
205 $proxy->start();
206 checkhandshake($proxy, checkhandshake::RENEG_HANDSHAKE,
207                checkhandshake::DEFAULT_EXTENSIONS,
208                "Rengotiation handshake test");
209
210 #Test 8: Server name handshake (client request only)
211 $proxy->clear();
212 $proxy->clientflags("-no_tls1_3 -servername testhost");
213 $proxy->start();
214 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
215                checkhandshake::DEFAULT_EXTENSIONS
216                | checkhandshake::SERVER_NAME_CLI_EXTENSION,
217               "Server name handshake test (client)");
218
219 #Test 9: Server name handshake (server support only)
220 $proxy->clear();
221 $proxy->clientflags("-no_tls1_3");
222 $proxy->serverflags("-servername testhost");
223 $proxy->start();
224 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
225                checkhandshake::DEFAULT_EXTENSIONS,
226               "Server name handshake test (server)");
227
228 #Test 10: Server name handshake (client and server)
229 $proxy->clear();
230 $proxy->clientflags("-no_tls1_3 -servername testhost");
231 $proxy->serverflags("-servername testhost");
232 $proxy->start();
233 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
234               checkhandshake::DEFAULT_EXTENSIONS
235               | checkhandshake::SERVER_NAME_CLI_EXTENSION
236               | checkhandshake::SERVER_NAME_SRV_EXTENSION,
237               "Server name handshake test");
238
239 #Test 11: ALPN handshake (client request only)
240 $proxy->clear();
241 $proxy->clientflags("-no_tls1_3 -alpn test");
242 $proxy->start();
243 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
244                checkhandshake::DEFAULT_EXTENSIONS
245                | checkhandshake::ALPN_CLI_EXTENSION,
246               "ALPN handshake test (client)");
247
248 #Test 12: ALPN handshake (server support only)
249 $proxy->clear();
250 $proxy->clientflags("-no_tls1_3");
251 $proxy->serverflags("-alpn test");
252 $proxy->start();
253 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
254                checkhandshake::DEFAULT_EXTENSIONS,
255               "ALPN handshake test (server)");
256
257 #Test 13: ALPN handshake (client and server)
258 $proxy->clear();
259 $proxy->clientflags("-no_tls1_3 -alpn test");
260 $proxy->serverflags("-alpn test");
261 $proxy->start();
262 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
263               checkhandshake::DEFAULT_EXTENSIONS
264               | checkhandshake::ALPN_CLI_EXTENSION
265               | checkhandshake::ALPN_SRV_EXTENSION,
266               "ALPN handshake test");
267
268 #Test 14: SCT handshake (client request only)
269 $proxy->clear();
270 #Note: -ct also sends status_request
271 $proxy->clientflags("-no_tls1_3 -ct");
272 $proxy->serverflags("-status_file "
273                     .srctop_file("test", "recipes", "ocsp-response.der"));
274 $proxy->start();
275 checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
276               checkhandshake::DEFAULT_EXTENSIONS
277               | checkhandshake::SCT_CLI_EXTENSION
278               | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
279               | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
280               "SCT handshake test (client)");
281
282 #Test 15: SCT handshake (server support only)
283 $proxy->clear();
284 #Note: -ct also sends status_request
285 $proxy->clientflags("-no_tls1_3");
286 $proxy->serverflags("-status_file "
287                     .srctop_file("test", "recipes", "ocsp-response.der"));
288 $proxy->start();
289 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
290               checkhandshake::DEFAULT_EXTENSIONS,
291               "SCT handshake test (server)");
292
293 #Test 16: SCT handshake (client and server)
294 #There is no built-in server side support for this so we are actually also
295 #testing custom extensions here
296 $proxy->clear();
297 #Note: -ct also sends status_request
298 $proxy->clientflags("-no_tls1_3 -ct");
299 $proxy->serverflags("-status_file "
300                     .srctop_file("test", "recipes", "ocsp-response.der")
301                     ." -serverinfo ".srctop_file("test", "serverinfo.pem"));
302 $proxy->start();
303 checkhandshake($proxy, checkhandshake::OCSP_HANDSHAKE,
304               checkhandshake::DEFAULT_EXTENSIONS
305               | checkhandshake::SCT_CLI_EXTENSION
306               | checkhandshake::SCT_SRV_EXTENSION
307               | checkhandshake::STATUS_REQUEST_CLI_EXTENSION
308               | checkhandshake::STATUS_REQUEST_SRV_EXTENSION,
309               "SCT handshake test");
310
311
312 #Test 17: NPN handshake (client request only)
313 $proxy->clear();
314 $proxy->clientflags("-no_tls1_3 -nextprotoneg test");
315 $proxy->start();
316 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
317                checkhandshake::DEFAULT_EXTENSIONS
318                | checkhandshake::NPN_CLI_EXTENSION,
319               "NPN handshake test (client)");
320
321 #Test 18: NPN handshake (server support only)
322 $proxy->clear();
323 $proxy->clientflags("-no_tls1_3");
324 $proxy->serverflags("-nextprotoneg test");
325 $proxy->start();
326 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
327                checkhandshake::DEFAULT_EXTENSIONS,
328               "NPN handshake test (server)");
329
330 #Test 19: NPN handshake (client and server)
331 $proxy->clear();
332 $proxy->clientflags("-no_tls1_3 -nextprotoneg test");
333 $proxy->serverflags("-nextprotoneg test");
334 $proxy->start();
335 checkhandshake($proxy, checkhandshake::NPN_HANDSHAKE,
336               checkhandshake::DEFAULT_EXTENSIONS
337               | checkhandshake::NPN_CLI_EXTENSION
338               | checkhandshake::NPN_SRV_EXTENSION,
339               "NPN handshake test");
340
341 #Test 20: SRP extension
342 #Note: We are not actually going to perform an SRP handshake (TLSProxy does not
343 #support it). However it is sufficient for us to check that the SRP extension
344 #gets added on the client side. There is no SRP extension generated on the
345 #server side anyway.
346 $proxy->clear();
347 $proxy->clientflags("-no_tls1_3 -srpuser user -srppass pass:pass");
348 $proxy->start();
349 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
350               checkhandshake::DEFAULT_EXTENSIONS
351               | checkhandshake::SRP_CLI_EXTENSION,
352               "SRP extension test");