a023f5ec45e3c8c8c40bc9c592e138e6fe43369a
[openssl.git] / test / testlib / 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
28     ALL_HANDSHAKES => 127
29 };
30
31 use constant {
32     #DEFAULT ALSO INCLUDES SESSION_TICKET_SRV_EXTENSION
33     DEFAULT_EXTENSIONS => 0x00000003,
34     SESSION_TICKET_SRV_EXTENSION => 0x00000002,
35     SERVER_NAME_CLI_EXTENSION => 0x00000004,
36     SERVER_NAME_SRV_EXTENSION => 0x00000008,
37     STATUS_REQUEST_CLI_EXTENSION => 0x00000010,
38     STATUS_REQUEST_SRV_EXTENSION => 0x00000020,
39     ALPN_CLI_EXTENSION => 0x00000040,
40     ALPN_SRV_EXTENSION => 0x00000080,
41     SCT_CLI_EXTENSION => 0x00000100,
42     SCT_SRV_EXTENSION => 0x00000200,
43     RENEGOTIATE_CLI_EXTENSION => 0x00000400,
44     NPN_CLI_EXTENSION => 0x00000800,
45     NPN_SRV_EXTENSION => 0x00001000,
46     SRP_CLI_EXTENSION => 0x00002000,
47     #Client side for ec point formats is a default extension
48     EC_POINT_FORMAT_SRV_EXTENSION => 0x00004000,
49     PSK_CLI_EXTENSION => 0x00008000,
50     PSK_SRV_EXTENSION => 0x00010000,
51     KEY_SHARE_SRV_EXTENSION => 0x00020000,
52     PSK_KEX_MODES_EXTENSION => 0x00040000
53 };
54
55 our @handmessages = ();
56 our @extensions = ();
57
58 sub checkhandshake($$$$)
59 {
60     my ($proxy, $handtype, $exttype, $testname) = @_;
61
62     subtest $testname => sub {
63         my $loop = 0;
64         my $numtests;
65         my $extcount;
66         my $clienthelloseen = 0;
67
68         #First count the number of tests
69         my $nextmess = 0;
70         my $message = undef;
71         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
72             next if (($handmessages[$loop][1] & $handtype) == 0);
73             if (scalar @{$proxy->message_list} > $nextmess) {
74                 $message = ${$proxy->message_list}[$nextmess];
75                 $nextmess++;
76             } else {
77                 $message = undef;
78             }
79             $numtests++;
80
81             next if (!defined $message);
82             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
83                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
84                     && $message->mt() !=
85                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
86                     && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
87
88             next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
89                     && !TLSProxy::Proxy::is_tls13();
90
91             for (my $extloop = 0;
92                     $extensions[$extloop][2] != 0;
93                     $extloop++) {
94                 next if ($message->mt() != $extensions[$extloop][0]);
95                 $numtests++;
96             }
97             $numtests++;
98         }
99
100         plan tests => $numtests;
101
102         $nextmess = 0;
103         $message = undef;
104         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
105             next if (($handmessages[$loop][1] & $handtype) == 0);
106             if (scalar @{$proxy->message_list} > $nextmess) {
107                 $message = ${$proxy->message_list}[$nextmess];
108                 $nextmess++;
109             } else {
110                 $message = undef;
111             }
112             if (!defined $message) {
113                 fail("Message type check. Got nothing, expected "
114                      .$handmessages[$loop][0]);
115                 next;
116             } else {
117                 ok($message->mt == $handmessages[$loop][0],
118                    "Message type check. Got ".$message->mt
119                    .", expected ".$handmessages[$loop][0]);
120             }
121
122             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
123                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
124                     && $message->mt() !=
125                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
126                     && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
127
128             next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
129                     && !TLSProxy::Proxy::is_tls13();
130
131             if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
132                 #Add renegotiate extension we will expect if renegotiating
133                 $exttype |= RENEGOTIATE_CLI_EXTENSION
134                     if ($clienthelloseen && !TLSProxy::Proxy::is_tls13());
135                 $clienthelloseen = 1;
136             }
137             #Now check that we saw the extensions we expected
138             my $msgexts = $message->extension_data();
139
140             for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
141                                 $extloop++) {
142                 next if ($message->mt() != $extensions[$extloop][0]);
143                 ok (($extensions[$extloop][2] & $exttype) == 0
144                       || defined ($msgexts->{$extensions[$extloop][1]}),
145                     "Extension presence check (Message: ".$message->mt()
146                     ." Extension: ".($extensions[$extloop][2] & $exttype).", "
147                     .$extloop.")");
148                 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
149             }
150             ok($extcount == keys %$msgexts, "Extensions count mismatch ("
151                                             .$extcount.", ".(keys %$msgexts)
152                                             .")");
153         }
154     }
155 }
156
157 1;