2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
9 package checkhandshake;
11 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
12 use OpenSSL::Test::Utils;
16 our @ISA = 'Exporter';
17 our @EXPORT = qw(@handmessages @extensions checkhandshake);
20 DEFAULT_HANDSHAKE => 1,
22 RESUME_HANDSHAKE => 4,
23 CLIENT_AUTH_HANDSHAKE => 8,
24 RENEG_HANDSHAKE => 16,
28 HRR_RESUME_HANDSHAKE => 256,
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
59 our @handmessages = ();
62 sub checkhandshake($$$$)
64 my ($proxy, $handtype, $exttype, $testname) = @_;
66 subtest $testname => sub {
70 my $clienthelloseen = 0;
74 if (TLSProxy::Proxy::is_tls13()) {
75 #How many ServerHellos are we expecting?
76 for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
77 next if (($handmessages[$loop][1] & $handtype) == 0);
78 $numsh++ if ($lastmt != TLSProxy::Message::MT_SERVER_HELLO
79 && $handmessages[$loop][0] == TLSProxy::Message::MT_SERVER_HELLO);
80 $lastmt = $handmessages[$loop][0];
84 #First count the number of tests
89 if (!TLSProxy::Proxy::is_tls13()) {
90 # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
95 #If we're only expecting one ServerHello out of two then we skip the
96 #first ServerHello in the list completely
97 $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
99 for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
100 next if (($handmessages[$loop][1] & $handtype) == 0);
101 if (scalar @{$proxy->message_list} > $nextmess) {
102 $message = ${$proxy->message_list}[$nextmess];
109 next if (!defined $message);
110 if (TLSProxy::Proxy::is_tls13()) {
111 $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
112 $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
114 next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
115 && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
117 TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
118 && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
120 next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
121 && !TLSProxy::Proxy::is_tls13();
125 for (my $extloop = 0;
126 $extensions[$extloop][2] != 0;
128 $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
129 && TLSProxy::Proxy::is_tls13();
130 $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
132 next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
133 && $extchnum != $chnum;
134 next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
135 && $extshnum != $shnum;
136 next if ($message->mt() != $extensions[$extloop][0]);
142 plan tests => $numtests;
146 if (TLSProxy::Proxy::is_tls13()) {
150 # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
155 #If we're only expecting one ServerHello out of two then we skip the
156 #first ServerHello in the list completely
157 $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
158 for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
159 next if (($handmessages[$loop][1] & $handtype) == 0);
160 if (scalar @{$proxy->message_list} > $nextmess) {
161 $message = ${$proxy->message_list}[$nextmess];
166 if (!defined $message) {
167 fail("Message type check. Got nothing, expected "
168 .$handmessages[$loop][0]);
171 ok($message->mt == $handmessages[$loop][0],
172 "Message type check. Got ".$message->mt
173 .", expected ".$handmessages[$loop][0]);
175 if (TLSProxy::Proxy::is_tls13()) {
176 $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
177 $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
180 next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
181 && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
183 TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
184 && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
186 next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
187 && !TLSProxy::Proxy::is_tls13();
189 if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
190 #Add renegotiate extension we will expect if renegotiating
191 $exttype |= RENEGOTIATE_CLI_EXTENSION
192 if ($clienthelloseen && !TLSProxy::Proxy::is_tls13());
193 $clienthelloseen = 1;
195 #Now check that we saw the extensions we expected
196 my $msgexts = $message->extension_data();
199 for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
201 #In TLSv1.3 we can have two ClientHellos if there has been a
202 #HelloRetryRequest, and they may have different extensions. Skip
203 #if these are extensions for a different ClientHello
204 $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
205 && TLSProxy::Proxy::is_tls13();
206 $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
208 next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
209 && $extchnum != $chnum;
210 next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
211 && $extshnum != $shnum;
212 next if ($message->mt() != $extensions[$extloop][0]);
213 ok (($extensions[$extloop][2] & $exttype) == 0
214 || defined ($msgexts->{$extensions[$extloop][1]}),
215 "Extension presence check (Message: ".$message->mt()
216 ." Extension: ".($extensions[$extloop][2] & $exttype).", "
218 $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
220 ok($extcount == keys %$msgexts, "Extensions count mismatch ("
221 .$extcount.", ".(keys %$msgexts)