Fix make update issues
[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
27     ALL_HANDSHAKES => 63
28 };
29
30 use constant {
31     #DEFAULT ALSO INCLUDES SESSION_TICKET_SRV_EXTENSION
32     DEFAULT_EXTENSIONS => 0x00000003,
33     SESSION_TICKET_SRV_EXTENSION => 0x00000002,
34     SERVER_NAME_CLI_EXTENSION => 0x00000004,
35     SERVER_NAME_SRV_EXTENSION => 0x00000008,
36     STATUS_REQUEST_CLI_EXTENSION => 0x00000010,
37     STATUS_REQUEST_SRV_EXTENSION => 0x00000020,
38     ALPN_CLI_EXTENSION => 0x00000040,
39     ALPN_SRV_EXTENSION => 0x00000080,
40     SCT_CLI_EXTENSION => 0x00000100,
41     SCT_SRV_EXTENSION => 0x00000200,
42     RENEGOTIATE_CLI_EXTENSION => 0x00000400,
43     NPN_CLI_EXTENSION => 0x00000800,
44     NPN_SRV_EXTENSION => 0x00001000,
45     SRP_CLI_EXTENSION => 0x00002000,
46 };
47
48 our @handmessages = ();
49 our @extensions = ();
50
51 sub checkhandshake($$$$)
52 {
53     my ($proxy, $handtype, $exttype, $testname) = @_;
54
55     subtest $testname => sub {
56         my $loop = 0;
57         my $numtests;
58         my $extcount;
59         my $clienthelloseen = 0;
60
61         #First count the number of tests
62         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
63             $numtests++ if (($handmessages[$loop][1] & $handtype) != 0);
64         }
65
66         #Add number of extensions we check plus 2 for the number of messages
67         #that contain extensions
68         $numtests += $#extensions + 2;
69         #In a renegotiation we will have double the number of extension tests
70         if (($handtype & RENEG_HANDSHAKE) != 0) {
71             $numtests += $#extensions + 2;
72         }
73         #In TLS1.3 there are 3 messages with extensions (and no renegotiations)
74         $numtests += 1 if ($proxy->is_tls13());
75
76         plan tests => $numtests;
77
78         my $nextmess = 0;
79         my $message = undef;
80         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
81             next if (($handmessages[$loop][1] & $handtype) == 0);
82             if (scalar @{$proxy->message_list} > $nextmess) {
83                 $message = ${$proxy->message_list}[$nextmess];
84                 $nextmess++;
85             } else {
86                 $message = undef;
87             }
88             if (!defined $message) {
89                 fail("Message type check. Got nothing, expected "
90                      .$handmessages[$loop][0]);
91                 next;
92             } else {
93                 ok($message->mt == $handmessages[$loop][0],
94                    "Message type check. Got ".$message->mt
95                    .", expected ".$handmessages[$loop][0]);
96             }
97
98             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
99                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
100                     && $message->mt() !=
101                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS);
102
103             if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
104                 #Add renegotiate extension we will expect if renegotiating
105                 $exttype |= RENEGOTIATE_CLI_EXTENSION if ($clienthelloseen);
106                 $clienthelloseen = 1;
107             }
108             #Now check that we saw the extensions we expected
109             my $msgexts = $message->extension_data();
110
111             for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
112                                 $extloop++) {
113                 next if ($message->mt() != $extensions[$extloop][0]);
114                 ok (($extensions[$extloop][2] & $exttype) == 0
115                       || defined ($msgexts->{$extensions[$extloop][1]}),
116                     "Extension presence check (Message: ".$message->mt()
117                     ." Extension: ".($extensions[$extloop][2] & $exttype).", "
118                     .$extloop.")");
119                 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
120              }
121             ok($extcount == keys %$msgexts, "Extensions count mismatch ("
122                                             .$extcount.", ".(keys %$msgexts)
123                                             .")");
124         }
125     }
126 }
127
128 1;