1#! /usr/bin/env perl
2# Copyright 2015-2018 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
9package checkhandshake;
10
11use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
12use OpenSSL::Test::Utils;
13use TLSProxy::Proxy;
14
15use Exporter;
16our @ISA = 'Exporter';
17our @EXPORT = qw(@handmessages @extensions checkhandshake);
18
19use 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
33use 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    POST_HANDSHAKE_AUTH_CLI_EXTENSION => 0x00200000
58};
59
60our @handmessages = ();
61our @extensions = ();
62
63sub checkhandshake($$$$)
64{
65    my ($proxy, $handtype, $exttype, $testname) = @_;
66
67    subtest $testname => sub {
68        my $loop = 0;
69        my $numtests;
70        my $extcount;
71        my $clienthelloseen = 0;
72
73        my $lastmt = 0;
74        my $numsh = 0;
75        if (TLSProxy::Proxy::is_tls13()) {
76            #How many ServerHellos are we expecting?
77            for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
78                next if (($handmessages[$loop][1] & $handtype) == 0);
79                $numsh++ if ($lastmt != TLSProxy::Message::MT_SERVER_HELLO
80                             && $handmessages[$loop][0] == TLSProxy::Message::MT_SERVER_HELLO);
81                $lastmt = $handmessages[$loop][0];
82            }
83        }
84
85        #First count the number of tests
86        my $nextmess = 0;
87        my $message = undef;
88        my $chnum = 0;
89        my $shnum = 0;
90        if (!TLSProxy::Proxy::is_tls13()) {
91            # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
92            # and SH
93            $chnum = 1;
94            $shnum = 1;
95        }
96        #If we're only expecting one ServerHello out of two then we skip the
97        #first ServerHello in the list completely
98        $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
99        $loop = 0;
100        for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
101            next if (($handmessages[$loop][1] & $handtype) == 0);
102            if (scalar @{$proxy->message_list} > $nextmess) {
103                $message = ${$proxy->message_list}[$nextmess];
104                $nextmess++;
105            } else {
106                $message = undef;
107            }
108            $numtests++;
109
110            next if (!defined $message);
111            if (TLSProxy::Proxy::is_tls13()) {
112                $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
113                $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
114            }
115            next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
116                    && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
117                    && $message->mt() !=
118                       TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
119                    && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
120
121            next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
122                    && !TLSProxy::Proxy::is_tls13();
123
124            my $extchnum = 1;
125            my $extshnum = 1;
126            for (my $extloop = 0;
127                    $extensions[$extloop][2] != 0;
128                    $extloop++) {
129                $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
130                                 && TLSProxy::Proxy::is_tls13();
131                $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
132                                 && $extchnum == 2;
133                next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
134                                 && $extchnum != $chnum;
135                next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
136                                 && $extshnum != $shnum;
137                next if ($message->mt() != $extensions[$extloop][0]);
138                $numtests++;
139            }
140            $numtests++;
141        }
142
143        plan tests => $numtests;
144
145        $nextmess = 0;
146        $message = undef;
147        if (TLSProxy::Proxy::is_tls13()) {
148            $chnum = 0;
149            $shnum = 0;
150        } else {
151            # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
152            # and SH
153            $chnum = 1;
154            $shnum = 1;
155        }
156        #If we're only expecting one ServerHello out of two then we skip the
157        #first ServerHello in the list completely
158        $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
159        for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
160            next if (($handmessages[$loop][1] & $handtype) == 0);
161            if (scalar @{$proxy->message_list} > $nextmess) {
162                $message = ${$proxy->message_list}[$nextmess];
163                $nextmess++;
164            } else {
165                $message = undef;
166            }
167            if (!defined $message) {
168                fail("Message type check. Got nothing, expected "
169                     .$handmessages[$loop][0]);
170                next;
171            } else {
172                ok($message->mt == $handmessages[$loop][0],
173                   "Message type check. Got ".$message->mt
174                   .", expected ".$handmessages[$loop][0]);
175            }
176            if (TLSProxy::Proxy::is_tls13()) {
177                $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
178                $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
179            }
180
181            next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
182                    && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
183                    && $message->mt() !=
184                       TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
185                    && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
186
187            next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
188                    && !TLSProxy::Proxy::is_tls13();
189
190            if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
191                #Add renegotiate extension we will expect if renegotiating
192                $exttype |= RENEGOTIATE_CLI_EXTENSION
193                    if ($clienthelloseen && !TLSProxy::Proxy::is_tls13());
194                $clienthelloseen = 1;
195            }
196            #Now check that we saw the extensions we expected
197            my $msgexts = $message->extension_data();
198            my $extchnum = 1;
199            my $extshnum = 1;
200            for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
201                                $extloop++) {
202                #In TLSv1.3 we can have two ClientHellos if there has been a
203                #HelloRetryRequest, and they may have different extensions. Skip
204                #if these are extensions for a different ClientHello
205                $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
206                                 && TLSProxy::Proxy::is_tls13();
207                $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
208                                 && $extchnum == 2;
209                next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
210                                 && $extchnum != $chnum;
211                next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
212                                 && $extshnum != $shnum;
213                next if ($message->mt() != $extensions[$extloop][0]);
214                ok (($extensions[$extloop][2] & $exttype) == 0
215                      || defined ($msgexts->{$extensions[$extloop][1]}),
216                    "Extension presence check (Message: ".$message->mt()
217                    ." Extension: ".($extensions[$extloop][2] & $exttype).", "
218                    .$extloop.")");
219                $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
220            }
221            ok($extcount == keys %$msgexts, "Extensions count mismatch ("
222                                            .$extcount.", ".(keys %$msgexts)
223                                            .")");
224        }
225    }
226}
227
2281;
229