Add extension tests in test_sslmessages
[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 bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use File::Temp qw(tempfile);
13 use TLSProxy::Proxy;
14 my $test_name = "test_tls13messages";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS|MSWin32)$/;
19
20 plan skip_all => "$test_name needs the dynamic engine feature enabled"
21     if disabled("engine") || disabled("dynamic-engine");
22
23 plan skip_all => "$test_name needs the sock feature enabled"
24     if disabled("sock");
25
26 plan skip_all => "$test_name needs TLS enabled"
27     if alldisabled(available_protocols("tls"));
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30
31 use constant {
32     DEFAULT_HANDSHAKE => 1,
33     OCSP_HANDSHAKE => 2,
34     RESUME_HANDSHAKE => 4,
35     CLIENT_AUTH_HANDSHAKE => 8,
36     RENEG_HANDSHAKE => 16,
37
38     ALL_HANDSHAKES => 31
39 };
40
41 use constant {
42     #DEFAULT ALSO INCLUDES SESSION_TICKET_SRV_EXTENSION
43     DEFAULT_EXTENSIONS => 0x00000003,
44     SESSION_TICKET_SRV_EXTENSION => 0x00000002,
45     SERVER_NAME_CLI_EXTENSION => 0x00000004,
46     SERVER_NAME_SRV_EXTENSION => 0x00000008,
47     STATUS_REQUEST_CLI_EXTENSION => 0x00000010,
48     STATUS_REQUEST_SRV_EXTENSION => 0x00000020,
49     ALPN_CLI_EXTENSION => 0x00000040,
50     ALPN_SRV_EXTENSION => 0x00000080,
51     SCT_CLI_EXTENSION => 0x00000100,
52     RENEGOTIATE_CLI_EXTENSION => 0x00000200
53 };
54
55 my @handmessages = (
56     [TLSProxy::Message::MT_CLIENT_HELLO, ALL_HANDSHAKES],
57     [TLSProxy::Message::MT_SERVER_HELLO, ALL_HANDSHAKES],
58     [TLSProxy::Message::MT_CERTIFICATE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
59     [TLSProxy::Message::MT_CERTIFICATE_STATUS, OCSP_HANDSHAKE],
60     #ServerKeyExchange handshakes not currently supported by TLSProxy
61     [TLSProxy::Message::MT_CERTIFICATE_REQUEST, CLIENT_AUTH_HANDSHAKE],
62     [TLSProxy::Message::MT_SERVER_HELLO_DONE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
63     [TLSProxy::Message::MT_CERTIFICATE, CLIENT_AUTH_HANDSHAKE],
64     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
65     [TLSProxy::Message::MT_CERTIFICATE_VERIFY, CLIENT_AUTH_HANDSHAKE],
66     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
67     [TLSProxy::Message::MT_NEW_SESSION_TICKET, ALL_HANDSHAKES & ~RESUME_HANDSHAKE],
68     [TLSProxy::Message::MT_FINISHED, ALL_HANDSHAKES],
69     [TLSProxy::Message::MT_CLIENT_HELLO, RENEG_HANDSHAKE],
70     [TLSProxy::Message::MT_SERVER_HELLO, RENEG_HANDSHAKE],
71     [TLSProxy::Message::MT_CERTIFICATE, RENEG_HANDSHAKE],
72     [TLSProxy::Message::MT_SERVER_HELLO_DONE, RENEG_HANDSHAKE],
73     [TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE, RENEG_HANDSHAKE],
74     [TLSProxy::Message::MT_FINISHED, RENEG_HANDSHAKE],
75     [TLSProxy::Message::MT_NEW_SESSION_TICKET, RENEG_HANDSHAKE],
76     [TLSProxy::Message::MT_FINISHED, RENEG_HANDSHAKE],
77     [0, 0]
78 );
79
80 my @extensions = (
81     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME, SERVER_NAME_CLI_EXTENSION],
82     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST, STATUS_REQUEST_CLI_EXTENSION],
83     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS, DEFAULT_EXTENSIONS],
84     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS, DEFAULT_EXTENSIONS],
85     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS, DEFAULT_EXTENSIONS],
86     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN, ALPN_CLI_EXTENSION],
87     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT, SCT_CLI_EXTENSION],
88     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC, DEFAULT_EXTENSIONS],
89     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET, DEFAULT_EXTENSIONS],
90     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET, DEFAULT_EXTENSIONS],
91     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_RENEGOTIATE, RENEGOTIATE_CLI_EXTENSION],
92
93     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_RENEGOTIATE, DEFAULT_EXTENSIONS],
94     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC, DEFAULT_EXTENSIONS],
95     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET, DEFAULT_EXTENSIONS],
96     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SESSION_TICKET, SESSION_TICKET_SRV_EXTENSION],
97     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_SERVER_NAME, SERVER_NAME_SRV_EXTENSION],
98     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST, STATUS_REQUEST_SRV_EXTENSION],
99     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_ALPN, ALPN_SRV_EXTENSION],
100     [0,0,0]
101 );
102
103 my $proxy = TLSProxy::Proxy->new(
104     undef,
105     cmdstr(app(["openssl"]), display => 1),
106     srctop_file("apps", "server.pem"),
107     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
108 );
109
110 sub checkmessages($$$);
111
112 #Test 1: Check we get all the right messages for a default handshake
113 (undef, my $session) = tempfile();
114 $proxy->serverconnects(2);
115 $proxy->clientflags("-no_tls1_3 -sess_out ".$session);
116 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
117 plan tests => 5;
118 checkmessages(DEFAULT_HANDSHAKE, DEFAULT_EXTENSIONS, "Default handshake test");
119
120 #Test 2: Resumption handshake
121 $proxy->clearClient();
122 $proxy->clientflags("-no_tls1_3 -sess_in ".$session);
123 $proxy->clientstart();
124 checkmessages(RESUME_HANDSHAKE,
125               DEFAULT_EXTENSIONS & ~SESSION_TICKET_SRV_EXTENSION,
126               "Resumption handshake test");
127 unlink $session;
128
129 #Test 3: A default handshake, but with a CertificateStatus message
130 $proxy->clear();
131 $proxy->clientflags("-no_tls1_3 -status");
132 $proxy->serverflags("-status_file "
133                     .srctop_file("test", "recipes", "ocsp-response.der"));
134 $proxy->start();
135 checkmessages(OCSP_HANDSHAKE,
136               DEFAULT_EXTENSIONS | STATUS_REQUEST_CLI_EXTENSION
137               | STATUS_REQUEST_SRV_EXTENSION,
138               "OCSP handshake test");
139
140 #Test 4: A client auth handshake
141 $proxy->clear();
142 $proxy->clientflags("-no_tls1_3 -cert ".srctop_file("apps", "server.pem"));
143 $proxy->serverflags("-Verify 5");
144 $proxy->start();
145 checkmessages(CLIENT_AUTH_HANDSHAKE, DEFAULT_EXTENSIONS,
146               "Client auth handshake test");
147
148 #Test 5: A handshake with a renegotiation
149 $proxy->clear();
150 $proxy->clientflags("-no_tls1_3");
151 $proxy->reneg(1);
152 $proxy->start();
153 checkmessages(RENEG_HANDSHAKE, DEFAULT_EXTENSIONS,
154               "Rengotiation handshake test");
155
156 sub checkmessages($$$)
157 {
158     my ($handtype, $exttype, $testname) = @_;
159
160     subtest $testname => sub {
161         my $loop = 0;
162         my $numtests;
163         my $extcount;
164         my $clienthelloseen = 0;
165
166         #First count the number of tests
167         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
168             $numtests++ if (($handmessages[$loop][1] & $handtype) != 0);
169         }
170
171         #Add number of extensions we check plus 2 for the number of messages
172         #that contain extensions
173         $numtests += $#extensions + 2;
174         #In a renegotiation we will have double the number of extension tests
175         if (($handtype & RENEG_HANDSHAKE) != 0) {
176             $numtests += $#extensions + 2;
177         }
178
179         plan tests => $numtests;
180
181         my $nextmess = 0;
182         my $message = undef;
183         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
184             next if (($handmessages[$loop][1] & $handtype) == 0);
185             if (scalar @{$proxy->message_list} > $nextmess) {
186                 $message = ${$proxy->message_list}[$nextmess];
187                 $nextmess++;
188             } else {
189                 $message = undef;
190             }
191             if (!defined $message) {
192                 fail("Message type check. Got nothing, expected "
193                      .$handmessages[$loop][0]);
194                 next;
195             } else {
196                 ok($message->mt == $handmessages[$loop][0],
197                    "Message type check. Got ".$message->mt
198                    .", expected ".$handmessages[$loop][0]);
199             }
200
201             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
202                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
203                     && $message->mt() !=
204                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS);
205
206             if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
207                 #Add renegotiate extension we will expect if renegotiating
208                 $exttype |= RENEGOTIATE_CLI_EXTENSION if ($clienthelloseen);
209                 $clienthelloseen = 1;
210             }
211              #Now check that we saw the extensions we expected
212              my $msgexts = $message->extension_data();
213              for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
214                                 $extloop++) {
215                 next if ($message->mt() != $extensions[$extloop][0]);
216                 ok (($extensions[$extloop][2] & $exttype) == 0
217                       || defined ($msgexts->{$extensions[$extloop][1]}),
218                     "Extension presence check (Message: ".$message->mt()
219                     ." Extension: ".($extensions[$extloop][2] & $exttype).", "
220                     .$extloop.")");
221                 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
222              }
223             ok($extcount == keys %$msgexts, "Extensions count mismatch ("
224                                             .$extcount.", ".(keys %$msgexts)
225                                             .")");
226         }
227     }
228 }