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