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