Create Certificate messages in TLS1.3 format
[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 };
50
51 our @handmessages = ();
52 our @extensions = ();
53
54 sub checkhandshake($$$$)
55 {
56     my ($proxy, $handtype, $exttype, $testname) = @_;
57
58     subtest $testname => sub {
59         my $loop = 0;
60         my $numtests;
61         my $extcount;
62         my $clienthelloseen = 0;
63
64         #First count the number of tests
65         for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
66             $numtests++ if (($handmessages[$loop][1] & $handtype) != 0);
67         }
68
69         #Add number of extensions we check plus 2 for the number of messages
70         #that contain extensions
71         $numtests += $#extensions + 2;
72         #In a renegotiation we will have double the number of extension tests
73         if (($handtype & RENEG_HANDSHAKE) != 0) {
74             $numtests += $#extensions + 2;
75         }
76         #In TLS1.3 there are 4 messages with extensions (i.e. 2 extra) and no
77         #renegotiations: 1 ClientHello, 1 ServerHello, 1 EncryptedExtensions,
78         #1 Certificate
79         $numtests += 2 if ($proxy->is_tls13());
80         #Except in Client auth where we have an extra Certificate message, and
81         #one extension gets checked twice (once in each Certificate message)
82         $numtests += 2 if ($proxy->is_tls13()
83                           && ($handtype & CLIENT_AUTH_HANDSHAKE) != 0);
84
85         plan tests => $numtests;
86
87         my $nextmess = 0;
88         my $message = undef;
89         for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
90             next if (($handmessages[$loop][1] & $handtype) == 0);
91             if (scalar @{$proxy->message_list} > $nextmess) {
92                 $message = ${$proxy->message_list}[$nextmess];
93                 $nextmess++;
94             } else {
95                 $message = undef;
96             }
97             if (!defined $message) {
98                 fail("Message type check. Got nothing, expected "
99                      .$handmessages[$loop][0]);
100                 next;
101             } else {
102                 ok($message->mt == $handmessages[$loop][0],
103                    "Message type check. Got ".$message->mt
104                    .", expected ".$handmessages[$loop][0]);
105             }
106
107             next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
108                     && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
109                     && $message->mt() !=
110                        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
111                     && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
112
113             next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
114                     && !TLSProxy::Proxy::is_tls13();
115
116             if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
117                 #Add renegotiate extension we will expect if renegotiating
118                 $exttype |= RENEGOTIATE_CLI_EXTENSION if ($clienthelloseen);
119                 $clienthelloseen = 1;
120             }
121             #Now check that we saw the extensions we expected
122             my $msgexts = $message->extension_data();
123
124             for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
125                                 $extloop++) {
126                 next if ($message->mt() != $extensions[$extloop][0]);
127                 ok (($extensions[$extloop][2] & $exttype) == 0
128                       || defined ($msgexts->{$extensions[$extloop][1]}),
129                     "Extension presence check (Message: ".$message->mt()
130                     ." Extension: ".($extensions[$extloop][2] & $exttype).", "
131                     .$extloop.")");
132                 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
133              }
134             ok($extcount == keys %$msgexts, "Extensions count mismatch ("
135                                             .$extcount.", ".(keys %$msgexts)
136                                             .")");
137         }
138     }
139 }
140
141 1;