checkhandshake.pm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. package checkhandshake;
  9. use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
  10. use OpenSSL::Test::Utils;
  11. use TLSProxy::Proxy;
  12. use Exporter;
  13. our @ISA = 'Exporter';
  14. our @EXPORT = qw(@handmessages @extensions checkhandshake);
  15. use constant {
  16. DEFAULT_HANDSHAKE => 1,
  17. OCSP_HANDSHAKE => 2,
  18. RESUME_HANDSHAKE => 4,
  19. CLIENT_AUTH_HANDSHAKE => 8,
  20. RENEG_HANDSHAKE => 16,
  21. NPN_HANDSHAKE => 32,
  22. EC_HANDSHAKE => 64,
  23. HRR_HANDSHAKE => 128,
  24. HRR_RESUME_HANDSHAKE => 256,
  25. ALL_HANDSHAKES => 511
  26. };
  27. use constant {
  28. #DEFAULT also includes SESSION_TICKET_SRV_EXTENSION and SERVER_NAME_CLI
  29. DEFAULT_EXTENSIONS => 0x00000007,
  30. SESSION_TICKET_SRV_EXTENSION => 0x00000002,
  31. SERVER_NAME_CLI_EXTENSION => 0x00000004,
  32. SERVER_NAME_SRV_EXTENSION => 0x00000008,
  33. STATUS_REQUEST_CLI_EXTENSION => 0x00000010,
  34. STATUS_REQUEST_SRV_EXTENSION => 0x00000020,
  35. ALPN_CLI_EXTENSION => 0x00000040,
  36. ALPN_SRV_EXTENSION => 0x00000080,
  37. SCT_CLI_EXTENSION => 0x00000100,
  38. SCT_SRV_EXTENSION => 0x00000200,
  39. RENEGOTIATE_CLI_EXTENSION => 0x00000400,
  40. NPN_CLI_EXTENSION => 0x00000800,
  41. NPN_SRV_EXTENSION => 0x00001000,
  42. SRP_CLI_EXTENSION => 0x00002000,
  43. #Client side for ec point formats is a default extension
  44. EC_POINT_FORMAT_SRV_EXTENSION => 0x00004000,
  45. PSK_CLI_EXTENSION => 0x00008000,
  46. PSK_SRV_EXTENSION => 0x00010000,
  47. KEY_SHARE_SRV_EXTENSION => 0x00020000,
  48. PSK_KEX_MODES_EXTENSION => 0x00040000,
  49. KEY_SHARE_HRR_EXTENSION => 0x00080000,
  50. SUPPORTED_GROUPS_SRV_EXTENSION => 0x00100000,
  51. POST_HANDSHAKE_AUTH_CLI_EXTENSION => 0x00200000
  52. };
  53. our @handmessages = ();
  54. our @extensions = ();
  55. sub checkhandshake($$$$)
  56. {
  57. my ($proxy, $handtype, $exttype, $testname) = @_;
  58. subtest $testname => sub {
  59. my $loop = 0;
  60. my $numtests;
  61. my $extcount;
  62. my $clienthelloseen = 0;
  63. my $lastmt = 0;
  64. my $numsh = 0;
  65. if (TLSProxy::Proxy::is_tls13()) {
  66. #How many ServerHellos are we expecting?
  67. for ($numtests = 0; $handmessages[$loop][1] != 0; $loop++) {
  68. next if (($handmessages[$loop][1] & $handtype) == 0);
  69. $numsh++ if ($lastmt != TLSProxy::Message::MT_SERVER_HELLO
  70. && $handmessages[$loop][0] == TLSProxy::Message::MT_SERVER_HELLO);
  71. $lastmt = $handmessages[$loop][0];
  72. }
  73. }
  74. #First count the number of tests
  75. my $nextmess = 0;
  76. my $message = undef;
  77. my $chnum = 0;
  78. my $shnum = 0;
  79. if (!TLSProxy::Proxy::is_tls13()) {
  80. # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
  81. # and SH
  82. $chnum = 1;
  83. $shnum = 1;
  84. }
  85. #If we're only expecting one ServerHello out of two then we skip the
  86. #first ServerHello in the list completely
  87. $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
  88. $loop = 0;
  89. for ($numtests = 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. $numtests++;
  98. next if (!defined $message);
  99. if (TLSProxy::Proxy::is_tls13()) {
  100. $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  101. $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
  102. }
  103. next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
  104. && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
  105. && $message->mt() !=
  106. TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
  107. && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
  108. next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
  109. && !TLSProxy::Proxy::is_tls13();
  110. my $extchnum = 1;
  111. my $extshnum = 1;
  112. for (my $extloop = 0;
  113. $extensions[$extloop][2] != 0;
  114. $extloop++) {
  115. $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
  116. && TLSProxy::Proxy::is_tls13();
  117. $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
  118. && $extchnum == 2;
  119. next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
  120. && $extchnum != $chnum;
  121. next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
  122. && $extshnum != $shnum;
  123. next if ($message->mt() != $extensions[$extloop][0]);
  124. $numtests++;
  125. }
  126. $numtests++;
  127. }
  128. plan tests => $numtests;
  129. $nextmess = 0;
  130. $message = undef;
  131. if (TLSProxy::Proxy::is_tls13()) {
  132. $chnum = 0;
  133. $shnum = 0;
  134. } else {
  135. # In non-TLSv1.3 we always treat reneg CH and SH like the first CH
  136. # and SH
  137. $chnum = 1;
  138. $shnum = 1;
  139. }
  140. #If we're only expecting one ServerHello out of two then we skip the
  141. #first ServerHello in the list completely
  142. $shnum++ if ($numsh == 1 && TLSProxy::Proxy::is_tls13());
  143. for ($loop = 0; $handmessages[$loop][1] != 0; $loop++) {
  144. next if (($handmessages[$loop][1] & $handtype) == 0);
  145. if (scalar @{$proxy->message_list} > $nextmess) {
  146. $message = ${$proxy->message_list}[$nextmess];
  147. $nextmess++;
  148. } else {
  149. $message = undef;
  150. }
  151. if (!defined $message) {
  152. fail("Message type check. Got nothing, expected "
  153. .$handmessages[$loop][0]);
  154. next;
  155. } else {
  156. ok($message->mt == $handmessages[$loop][0],
  157. "Message type check. Got ".$message->mt
  158. .", expected ".$handmessages[$loop][0]);
  159. }
  160. if (TLSProxy::Proxy::is_tls13()) {
  161. $chnum++ if $message->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
  162. $shnum++ if $message->mt() == TLSProxy::Message::MT_SERVER_HELLO;
  163. }
  164. next if ($message->mt() != TLSProxy::Message::MT_CLIENT_HELLO
  165. && $message->mt() != TLSProxy::Message::MT_SERVER_HELLO
  166. && $message->mt() !=
  167. TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS
  168. && $message->mt() != TLSProxy::Message::MT_CERTIFICATE);
  169. next if $message->mt() == TLSProxy::Message::MT_CERTIFICATE
  170. && !TLSProxy::Proxy::is_tls13();
  171. if ($message->mt() == TLSProxy::Message::MT_CLIENT_HELLO) {
  172. #Add renegotiate extension we will expect if renegotiating
  173. $exttype |= RENEGOTIATE_CLI_EXTENSION
  174. if ($clienthelloseen && !TLSProxy::Proxy::is_tls13());
  175. $clienthelloseen = 1;
  176. }
  177. #Now check that we saw the extensions we expected
  178. my $msgexts = $message->extension_data();
  179. my $extchnum = 1;
  180. my $extshnum = 1;
  181. for (my $extloop = 0, $extcount = 0; $extensions[$extloop][2] != 0;
  182. $extloop++) {
  183. #In TLSv1.3 we can have two ClientHellos if there has been a
  184. #HelloRetryRequest, and they may have different extensions. Skip
  185. #if these are extensions for a different ClientHello
  186. $extchnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_CLIENT_HELLO
  187. && TLSProxy::Proxy::is_tls13();
  188. $extshnum = 2 if $extensions[$extloop][0] != TLSProxy::Message::MT_SERVER_HELLO
  189. && $extchnum == 2;
  190. next if $extensions[$extloop][0] == TLSProxy::Message::MT_CLIENT_HELLO
  191. && $extchnum != $chnum;
  192. next if $extensions[$extloop][0] == TLSProxy::Message::MT_SERVER_HELLO
  193. && $extshnum != $shnum;
  194. next if ($message->mt() != $extensions[$extloop][0]);
  195. ok (($extensions[$extloop][2] & $exttype) == 0
  196. || defined ($msgexts->{$extensions[$extloop][1]}),
  197. "Extension presence check (Message: ".$message->mt()
  198. ." Extension: ".($extensions[$extloop][2] & $exttype).", "
  199. .$extloop.")");
  200. $extcount++ if (($extensions[$extloop][2] & $exttype) != 0);
  201. }
  202. ok($extcount == keys %$msgexts, "Extensions count mismatch ("
  203. .$extcount.", ".(keys %$msgexts)
  204. .")");
  205. }
  206. }
  207. }
  208. 1;