protocol_version.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # -*- mode: perl; -*-
  2. # Copyright 2016-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. ## Test version negotiation
  9. package ssltests;
  10. use strict;
  11. use warnings;
  12. use List::Util qw/max min/;
  13. use OpenSSL::Test;
  14. use OpenSSL::Test::Utils qw/anydisabled alldisabled disabled/;
  15. setup("no_test_here");
  16. my @tls_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3");
  17. my @tls_protocols_fips = ("TLSv1.2", "TLSv1.3");
  18. # undef stands for "no limit".
  19. my @min_tls_protocols = (undef, "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3");
  20. my @min_tls_protocols_fips = (undef, "TLSv1.2", "TLSv1.3");
  21. my @max_tls_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3", undef);
  22. my @max_tls_protocols_fips = ("TLSv1.2", "TLSv1.3", undef);
  23. my @is_tls_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2", "tls1_3");
  24. my @is_tls_disabled_fips = anydisabled("tls1_2", "tls1_3");
  25. my $min_tls_enabled; my $max_tls_enabled;
  26. my $min_tls_enabled_fips; my $max_tls_enabled_fips;
  27. # Protocol configuration works in cascades, i.e.,
  28. # $no_tls1_1 disables TLSv1.1 and below.
  29. #
  30. # $min_enabled and $max_enabled will be correct if there is at least one
  31. # protocol enabled.
  32. sub min_prot_enabled {
  33. my $protref = shift;
  34. my $disabledref = shift;
  35. my @protocols = @{$protref};
  36. my @is_disabled = @{$disabledref};
  37. my $min_enabled;
  38. foreach my $i (0..$#protocols) {
  39. if (!$is_disabled[$i]) {
  40. $min_enabled = $i;
  41. last;
  42. }
  43. }
  44. return $min_enabled;
  45. }
  46. sub max_prot_enabled {
  47. my $protref = shift;
  48. my $disabledref = shift;
  49. my @protocols = @{$protref};
  50. my @is_disabled = @{$disabledref};
  51. my $max_enabled;
  52. foreach my $i (0..$#protocols) {
  53. if (!$is_disabled[$i]) {
  54. $max_enabled = $i;
  55. }
  56. }
  57. return $max_enabled;
  58. }
  59. $min_tls_enabled = min_prot_enabled(\@tls_protocols, \@is_tls_disabled);
  60. $max_tls_enabled = max_prot_enabled(\@tls_protocols, \@is_tls_disabled);
  61. $min_tls_enabled_fips = min_prot_enabled(\@tls_protocols_fips, \@is_tls_disabled_fips);
  62. $max_tls_enabled_fips = max_prot_enabled(\@tls_protocols_fips, \@is_tls_disabled_fips);
  63. my @dtls_protocols = ("DTLSv1", "DTLSv1.2");
  64. my @dtls_protocols_fips = ("DTLSv1.2");
  65. # undef stands for "no limit".
  66. my @min_dtls_protocols = (undef, "DTLSv1", "DTLSv1.2");
  67. my @min_dtls_protocols_fips = (undef, "DTLSv1.2");
  68. my @max_dtls_protocols = ("DTLSv1", "DTLSv1.2", undef);
  69. my @max_dtls_protocols_fips = ("DTLSv1.2", undef);
  70. my @is_dtls_disabled = anydisabled("dtls1", "dtls1_2");
  71. my @is_dtls_disabled_fips = anydisabled("dtls1_2");
  72. my $min_dtls_enabled; my $max_dtls_enabled;
  73. my $min_dtls_enabled_fips; my $max_dtls_enabled_fips;
  74. # $min_enabled and $max_enabled will be correct if there is at least one
  75. # protocol enabled.
  76. $min_dtls_enabled = min_prot_enabled(\@dtls_protocols, \@is_dtls_disabled);
  77. $max_dtls_enabled = max_prot_enabled(\@dtls_protocols, \@is_dtls_disabled);
  78. $min_dtls_enabled_fips = min_prot_enabled(\@dtls_protocols_fips, \@is_dtls_disabled_fips);
  79. $max_dtls_enabled_fips = max_prot_enabled(\@dtls_protocols_fips, \@is_dtls_disabled_fips);
  80. sub no_tests {
  81. my ($dtls) = @_;
  82. return $dtls ? alldisabled("dtls1", "dtls1_2") :
  83. alldisabled("ssl3", "tls1", "tls1_1", "tls1_2", "tls1_3");
  84. }
  85. sub generate_version_tests {
  86. my $method = shift;
  87. my $fips = shift;
  88. my $dtls = $method eq "DTLS";
  89. # Don't write the redundant "Method = TLS" into the configuration.
  90. undef $method if !$dtls;
  91. my @protocols;
  92. my @min_protocols;
  93. my @max_protocols;
  94. my $min_enabled;
  95. my $max_enabled;
  96. if ($fips) {
  97. @protocols = $dtls ? @dtls_protocols_fips : @tls_protocols_fips;
  98. @min_protocols = $dtls ? @min_dtls_protocols_fips : @min_tls_protocols_fips;
  99. @max_protocols = $dtls ? @max_dtls_protocols_fips : @max_tls_protocols_fips;
  100. $min_enabled = $dtls ? $min_dtls_enabled_fips : $min_tls_enabled_fips;
  101. $max_enabled = $dtls ? $max_dtls_enabled_fips : $max_tls_enabled_fips;
  102. } else {
  103. @protocols = $dtls ? @dtls_protocols : @tls_protocols;
  104. @min_protocols = $dtls ? @min_dtls_protocols : @min_tls_protocols;
  105. @max_protocols = $dtls ? @max_dtls_protocols : @max_tls_protocols;
  106. $min_enabled = $dtls ? $min_dtls_enabled : $min_tls_enabled;
  107. $max_enabled = $dtls ? $max_dtls_enabled : $max_tls_enabled;
  108. }
  109. if (no_tests($dtls)) {
  110. return;
  111. }
  112. my @tests = ();
  113. for (my $sctp = 0; $sctp < ($dtls && !disabled("sctp") ? 2 : 1); $sctp++) {
  114. foreach my $c_min (0..$#min_protocols) {
  115. my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
  116. foreach my $c_max ($c_max_min..$#max_protocols) {
  117. foreach my $s_min (0..$#min_protocols) {
  118. my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
  119. foreach my $s_max ($s_max_min..$#max_protocols) {
  120. my ($result, $protocol) =
  121. expected_result($c_min, $c_max, $s_min, $s_max,
  122. $min_enabled, $max_enabled,
  123. \@protocols);
  124. push @tests, {
  125. "name" => "version-negotiation",
  126. "client" => {
  127. "MinProtocol" => $min_protocols[$c_min],
  128. "MaxProtocol" => $max_protocols[$c_max],
  129. },
  130. "server" => {
  131. "MinProtocol" => $min_protocols[$s_min],
  132. "MaxProtocol" => $max_protocols[$s_max],
  133. },
  134. "test" => {
  135. "ExpectedResult" => $result,
  136. "ExpectedProtocol" => $protocol,
  137. "Method" => $method,
  138. }
  139. };
  140. $tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. return @tests if disabled("tls1_3") || disabled("tls1_2") || $dtls;
  147. #Add some version/ciphersuite sanity check tests
  148. push @tests, {
  149. "name" => "ciphersuite-sanity-check-client",
  150. "client" => {
  151. #Offering only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
  152. "CipherString" => "AES128-SHA",
  153. "Ciphersuites" => "",
  154. },
  155. "server" => {
  156. "MaxProtocol" => "TLSv1.2"
  157. },
  158. "test" => {
  159. "ExpectedResult" => "ClientFail",
  160. }
  161. };
  162. push @tests, {
  163. "name" => "ciphersuite-sanity-check-server",
  164. "client" => {
  165. "CipherString" => "AES128-SHA",
  166. "MaxProtocol" => "TLSv1.2"
  167. },
  168. "server" => {
  169. #Allowing only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
  170. "CipherString" => "AES128-SHA",
  171. "Ciphersuites" => "",
  172. },
  173. "test" => {
  174. "ExpectedResult" => "ServerFail",
  175. }
  176. };
  177. return @tests;
  178. }
  179. sub generate_resumption_tests {
  180. my $method = shift;
  181. my $fips = shift;
  182. my $dtls = $method eq "DTLS";
  183. # Don't write the redundant "Method = TLS" into the configuration.
  184. undef $method if !$dtls;
  185. my @protocols;
  186. my $min_enabled;
  187. my $max_enabled;
  188. if ($fips) {
  189. @protocols = $dtls ? @dtls_protocols_fips : @tls_protocols_fips;
  190. $min_enabled = $dtls ? $min_dtls_enabled_fips : $min_tls_enabled_fips;
  191. $max_enabled = $dtls ? $max_dtls_enabled_fips : $max_tls_enabled_fips;
  192. } else {
  193. @protocols = $dtls ? @dtls_protocols : @tls_protocols;
  194. $min_enabled = $dtls ? $min_dtls_enabled : $min_tls_enabled;
  195. $max_enabled = $dtls ? $max_dtls_enabled : $max_tls_enabled;
  196. }
  197. if (no_tests($dtls)) {
  198. return;
  199. }
  200. my @server_tests = ();
  201. my @client_tests = ();
  202. # Obtain the first session against a fixed-version server/client.
  203. foreach my $original_protocol($min_enabled..$max_enabled) {
  204. # Upgrade or downgrade the server/client max version support and test
  205. # that it upgrades, downgrades or resumes the session as well.
  206. foreach my $resume_protocol($min_enabled..$max_enabled) {
  207. my $resumption_expected;
  208. # We should only resume on exact version match.
  209. if ($original_protocol eq $resume_protocol) {
  210. $resumption_expected = "Yes";
  211. } else {
  212. $resumption_expected = "No";
  213. }
  214. for (my $sctp = 0; $sctp < ($dtls && !disabled("sctp") ? 2 : 1);
  215. $sctp++) {
  216. foreach my $ticket ("SessionTicket", "-SessionTicket") {
  217. # Client is flexible, server upgrades/downgrades.
  218. push @server_tests, {
  219. "name" => "resumption",
  220. "client" => { },
  221. "server" => {
  222. "MinProtocol" => $protocols[$original_protocol],
  223. "MaxProtocol" => $protocols[$original_protocol],
  224. "Options" => $ticket,
  225. },
  226. "resume_server" => {
  227. "MaxProtocol" => $protocols[$resume_protocol],
  228. "Options" => $ticket,
  229. },
  230. "test" => {
  231. "ExpectedProtocol" => $protocols[$resume_protocol],
  232. "Method" => $method,
  233. "HandshakeMode" => "Resume",
  234. "ResumptionExpected" => $resumption_expected,
  235. }
  236. };
  237. $server_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  238. # Server is flexible, client upgrades/downgrades.
  239. push @client_tests, {
  240. "name" => "resumption",
  241. "client" => {
  242. "MinProtocol" => $protocols[$original_protocol],
  243. "MaxProtocol" => $protocols[$original_protocol],
  244. },
  245. "server" => {
  246. "Options" => $ticket,
  247. },
  248. "resume_client" => {
  249. "MaxProtocol" => $protocols[$resume_protocol],
  250. },
  251. "test" => {
  252. "ExpectedProtocol" => $protocols[$resume_protocol],
  253. "Method" => $method,
  254. "HandshakeMode" => "Resume",
  255. "ResumptionExpected" => $resumption_expected,
  256. }
  257. };
  258. $client_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  259. }
  260. }
  261. }
  262. }
  263. if (!disabled("tls1_3") && !$dtls) {
  264. push @client_tests, {
  265. "name" => "resumption-with-hrr",
  266. "client" => {
  267. },
  268. "server" => {
  269. "Curves" => disabled("ec") ? "ffdhe3072" : "P-256"
  270. },
  271. "resume_client" => {
  272. },
  273. "test" => {
  274. "ExpectedProtocol" => "TLSv1.3",
  275. "Method" => "TLS",
  276. "HandshakeMode" => "Resume",
  277. "ResumptionExpected" => "Yes",
  278. }
  279. };
  280. }
  281. return (@server_tests, @client_tests);
  282. }
  283. sub expected_result {
  284. my ($c_min, $c_max, $s_min, $s_max, $min_enabled, $max_enabled,
  285. $protocols) = @_;
  286. # Adjust for "undef" (no limit).
  287. $c_min = $c_min == 0 ? 0 : $c_min - 1;
  288. $c_max = $c_max == scalar @$protocols ? $c_max - 1 : $c_max;
  289. $s_min = $s_min == 0 ? 0 : $s_min - 1;
  290. $s_max = $s_max == scalar @$protocols ? $s_max - 1 : $s_max;
  291. # We now have at least one protocol enabled, so $min_enabled and
  292. # $max_enabled are well-defined.
  293. $c_min = max $c_min, $min_enabled;
  294. $s_min = max $s_min, $min_enabled;
  295. $c_max = min $c_max, $max_enabled;
  296. $s_max = min $s_max, $max_enabled;
  297. if ($c_min > $c_max) {
  298. # Client should fail to even send a hello.
  299. return ("ClientFail", undef);
  300. } elsif ($s_min > $s_max) {
  301. # Server has no protocols, should always fail.
  302. return ("ServerFail", undef);
  303. } elsif ($s_min > $c_max) {
  304. # Server doesn't support the client range.
  305. return ("ServerFail", undef);
  306. } elsif ($c_min > $s_max) {
  307. my @prots = @$protocols;
  308. if ($prots[$c_max] eq "TLSv1.3") {
  309. # Client will have sent supported_versions, so server will know
  310. # that there are no overlapping versions.
  311. return ("ServerFail", undef);
  312. } else {
  313. # Server will try with a version that is lower than the lowest
  314. # supported client version.
  315. return ("ClientFail", undef);
  316. }
  317. } else {
  318. # Server and client ranges overlap.
  319. my $max_common = $s_max < $c_max ? $s_max : $c_max;
  320. return ("Success", $protocols->[$max_common]);
  321. }
  322. }
  323. 1;