Create one permanent proxy socket per TLSProxy::Proxy instance
[openssl.git] / util / perl / checkhandshake.pm
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 package checkhandshake;
10
11 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
12 use OpenSSL::Test::Utils;
13 use TLSProxy::Proxy;
14
15 use Exporter;
16 our @ISA = 'Exporter';
17 our @EXPORT = qw(@handmessages @extensions checkhandshake);
18
19 use constant {
20     DEFAULT_HANDSHAKE => 1,
21     OCSP_HANDSHAKE => 2,
22     RESUME_HANDSHAKE => 4,
23     CLIENT_AUTH_HANDSHAKE => 8,
24     RENEG_HANDSHAKE => 16,
25     NPN_HANDSHAKE => 32,
26     EC_HANDSHAKE => 64,
27     HRR_HANDSHAKE => 128,
28     HRR_RESUME_HANDSHAKE => 256,
29
30     ALL_HANDSHAKES => 511
31 };
32
33 use constant {
34     #DEFAULT also includes SESSION_TICKET_SRV_EXTENSION and SERVER_NAME_CLI
35     DEFAULT_EXTENSIONS => 0x00000007,
36     SESSION_TICKET_SRV_EXTENSION => 0x00000002,
37     SERVER_NAME_CLI_EXTENSION => 0x00000004,
38     SERVER_NAME_SRV_EXTENSION => 0x00000008,
39     STATUS_REQUEST_CLI_EXTENSION => 0x00000010,
40     STATUS_REQUEST_SRV_EXTENSION => 0x00000020,
41     ALPN_CLI_EXTENSION => 0x00000040,
42     ALPN_SRV_EXTENSION => 0x00000080,
43     SCT_CLI_EXTENSION => 0x00000100,
44     SCT_SRV_EXTENSION => 0x00000200,
45     RENEGOTIATE_CLI_EXTENSION => 0x00000400,
46     NPN_CLI_EXTENSION => 0x00000800,
47     NPN_SRV_EXTENSION => 0x00001000,
48     SRP_CLI_EXTENSION => 0x00002000,
49     #Client side for ec point formats is a default extension
50     EC_POINT_FORMAT_SRV_EXTENSION => 0x00004000,
51     PSK_CLI_EXTENSION => 0x00008000,
52     PSK_SRV_EXTENSION => 0x00010000,
53     KEY_SHARE_SRV_EXTENSION => 0x00020000,
54     PSK_KEX_MODES_EXTENSION => 0x00040000,
55     KEY_SHARE_HRR_EXTENSION => 0x00080000,
56     SUPPORTED_GROUPS_SRV_EXTENSION => 0x00100000
57 };
58
59 our @handmessages = ();
60 our @extensions = ();
61
62 sub checkhandshake($$$$)
63 {
64     my ($proxy, $handtype, $exttype, $testname) = @_;
65
66     subtest $testname => sub {
67         my $loop = 0;
68         my $numtests;
69         my $extcount;
70         my $clienthelloseen = 0;
71
72         my $lastmt = 0;
73         my $numsh = 0;
74         if (TLSProxy::Proxy::is_tls13()) {
75             #How many ServerHellos are we expecting?
76             for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
77                 next if (($handmessages[$loop][1] & $handtype) == 0);
78                 $numsh++ if ($lastmt != TLSProxy::Message::MT_SERVER_HELLO
79                              && $handmessages[$loop][0] == TLSProxy::Message::MT_SERVER_HELLO);
80                 $lastmt = $handmessages[$loop][0];
81             }
82         }
83
84         #First count the number of tests
85         my $nextmess = 0;
86         my $message = undef;
87         my $chnum = 0;
88         my $shnum = 0;
89         if (!TLSProxy::Proxy::is_tls13()) {
90             # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
91             # and SH
92             $chnum = 1;
93             $shnum = 1;
94         }
95         #If we're only expecting one ServerHello out of two then we skip the
96         #first ServerHello in the list completely
97         $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
98         $loop = 0;
99         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
100             next if (($handmessages[$loop][1] & $handtype) == 0);
101             if (scalar @{$proxy->message_list} > $nextmess) {
102                 $message = ${$proxy->message_list}[$nextmess];
103                 $nextmess++;
104             } else {
105                 $message = undef;
106             }
107             $numtests++;
108
109             next if (!defined $message);
110             if (TLSProxy::Proxy::is_tls13()) {
111                 $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
112                 $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
113             }
114             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
115                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
116                     && $message->mt() !=
117                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
118                     && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
119
120             next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
121                     && !TLSProxy::Proxy::is_tls13();
122
123             my $extchnum = 1;
124             my $extshnum = 1;
125             for (my $extloop = 0;
126                     $extensions[$extloop][2] != 0;
127                     $extloop++) {
128                 $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
129                                  && TLSProxy::Proxy::is_tls13();
130                 $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
131                                  && $extchnum == 2;
132                 next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
133                                  && $extchnum != $chnum;
134                 next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
135                                  && $extshnum != $shnum;
136                 next if ($message->mt() != $extensions[$extloop][0]);
137                 $numtests++;
138             }
139             $numtests++;
140         }
141
142         plan tests => $numtests;
143
144         $nextmess = 0;
145         $message = undef;
146         if (TLSProxy::Proxy::is_tls13()) {
147             $chnum = 0;
148             $shnum = 0;
149         } else {
150             # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
151             # and SH
152             $chnum = 1;
153             $shnum = 1;
154         }
155         #If we're only expecting one ServerHello out of two then we skip the
156         #first ServerHello in the list completely
157         $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
158         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
159             next if (($handmessages[$loop][1] & $handtype) == 0);
160             if (scalar @{$proxy->message_list} > $nextmess) {
161                 $message = ${$proxy->message_list}[$nextmess];
162                 $nextmess++;
163             } else {
164                 $message = undef;
165             }
166             if (!defined $message) {
167                 fail("Message type check. Got nothing, expected "
168                      .$handmessages[$loop][0]);
169                 next;
170             } else {
171                 ok($message->mt == $handmessages[$loop][0],
172                    "Message type check. Got ".$message->mt
173                    .", expected ".$handmessages[$loop][0]);
174             }
175             if (TLSProxy::Proxy::is_tls13()) {
176                 $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
177                 $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
178             }
179
180             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
181                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
182                     && $message->mt() !=
183                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
184                     && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
185
186             next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
187                     && !TLSProxy::Proxy::is_tls13();
188
189             if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
190                 #Add renegotiate extension we will expect if renegotiating
191                 $exttype |= RENEGOTIATE_CLI_EXTENSION
192                     if ($clienthelloseen && !TLSProxy::Proxy::is_tls13());
193                 $clienthelloseen = 1;
194             }
195             #Now check that we saw the extensions we expected
196             my $msgexts = $message->extension_data();
197             my $extchnum = 1;
198             my $extshnum = 1;
199             for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
200                                 $extloop++) {
201                 #In TLSv1.3 we can have two ClientHellos if there has been a
202                 #HelloRetryRequest, and they may have different extensions. Skip
203                 #if these are extensions for a different ClientHello
204                 $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
205                                  && TLSProxy::Proxy::is_tls13();
206                 $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
207                                  && $extchnum == 2;
208                 next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
209                                  && $extchnum != $chnum;
210                 next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
211                                  && $extshnum != $shnum;
212                 next if ($message->mt() != $extensions[$extloop][0]);
213                 ok (($extensions[$extloop][2] & $exttype) == 0
214                       || defined ($msgexts->{$extensions[$extloop][1]}),
215                     "Extension presence check (Message: ".$message->mt()
216                     ." Extension: ".($extensions[$extloop][2] & $exttype).", "
217                     .$extloop.")");
218                 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
219             }
220             ok($extcount == keys %$msgexts, "Extensions count mismatch ("
221                                             .$extcount.", ".(keys %$msgexts)
222                                             .")");
223         }
224     }
225 }
226
227 1;