protocol_version.pm 14 KB

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