protocol_version.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. # -*- mode: perl; -*-
  2. # Copyright 2016-2020 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. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  128. "MinProtocol" => $min_protocols[$c_min],
  129. "MaxProtocol" => $max_protocols[$c_max],
  130. },
  131. "server" => {
  132. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  133. "MinProtocol" => $min_protocols[$s_min],
  134. "MaxProtocol" => $max_protocols[$s_max],
  135. },
  136. "test" => {
  137. "ExpectedResult" => $result,
  138. "ExpectedProtocol" => $protocol,
  139. "Method" => $method,
  140. }
  141. };
  142. $tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. return @tests if disabled("tls1_3") || disabled("tls1_2") || $dtls;
  149. #Add some version/ciphersuite sanity check tests
  150. push @tests, {
  151. "name" => "ciphersuite-sanity-check-client",
  152. "client" => {
  153. #Offering only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
  154. "CipherString" => "AES128-SHA",
  155. "Ciphersuites" => "",
  156. },
  157. "server" => {
  158. "MaxProtocol" => "TLSv1.2"
  159. },
  160. "test" => {
  161. "ExpectedResult" => "ClientFail",
  162. }
  163. };
  164. push @tests, {
  165. "name" => "ciphersuite-sanity-check-server",
  166. "client" => {
  167. "CipherString" => "AES128-SHA",
  168. "MaxProtocol" => "TLSv1.2"
  169. },
  170. "server" => {
  171. #Allowing only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
  172. "CipherString" => "AES128-SHA",
  173. "Ciphersuites" => "",
  174. },
  175. "test" => {
  176. "ExpectedResult" => "ServerFail",
  177. }
  178. };
  179. return @tests;
  180. }
  181. sub generate_resumption_tests {
  182. my $method = shift;
  183. my $fips = shift;
  184. my $dtls = $method eq "DTLS";
  185. # Don't write the redundant "Method = TLS" into the configuration.
  186. undef $method if !$dtls;
  187. my @protocols;
  188. my $min_enabled;
  189. my $max_enabled;
  190. if ($fips) {
  191. @protocols = $dtls ? @dtls_protocols_fips : @tls_protocols_fips;
  192. $min_enabled = $dtls ? $min_dtls_enabled_fips : $min_tls_enabled_fips;
  193. $max_enabled = $dtls ? $max_dtls_enabled_fips : $max_tls_enabled_fips;
  194. } else {
  195. @protocols = $dtls ? @dtls_protocols : @tls_protocols;
  196. $min_enabled = $dtls ? $min_dtls_enabled : $min_tls_enabled;
  197. $max_enabled = $dtls ? $max_dtls_enabled : $max_tls_enabled;
  198. }
  199. if (no_tests($dtls)) {
  200. return;
  201. }
  202. my @server_tests = ();
  203. my @client_tests = ();
  204. # Obtain the first session against a fixed-version server/client.
  205. foreach my $original_protocol($min_enabled..$max_enabled) {
  206. # Upgrade or downgrade the server/client max version support and test
  207. # that it upgrades, downgrades or resumes the session as well.
  208. foreach my $resume_protocol($min_enabled..$max_enabled) {
  209. my $resumption_expected;
  210. # We should only resume on exact version match.
  211. if ($original_protocol eq $resume_protocol) {
  212. $resumption_expected = "Yes";
  213. } else {
  214. $resumption_expected = "No";
  215. }
  216. for (my $sctp = 0; $sctp < ($dtls && !disabled("sctp") ? 2 : 1);
  217. $sctp++) {
  218. foreach my $ticket ("SessionTicket", "-SessionTicket") {
  219. # Client is flexible, server upgrades/downgrades.
  220. push @server_tests, {
  221. "name" => "resumption",
  222. "client" => {
  223. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  224. },
  225. "server" => {
  226. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  227. "MinProtocol" => $protocols[$original_protocol],
  228. "MaxProtocol" => $protocols[$original_protocol],
  229. "Options" => $ticket,
  230. },
  231. "resume_server" => {
  232. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  233. "MaxProtocol" => $protocols[$resume_protocol],
  234. "Options" => $ticket,
  235. },
  236. "test" => {
  237. "ExpectedProtocol" => $protocols[$resume_protocol],
  238. "Method" => $method,
  239. "HandshakeMode" => "Resume",
  240. "ResumptionExpected" => $resumption_expected,
  241. }
  242. };
  243. $server_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  244. # Server is flexible, client upgrades/downgrades.
  245. push @client_tests, {
  246. "name" => "resumption",
  247. "client" => {
  248. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  249. "MinProtocol" => $protocols[$original_protocol],
  250. "MaxProtocol" => $protocols[$original_protocol],
  251. },
  252. "server" => {
  253. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  254. "Options" => $ticket,
  255. },
  256. "resume_client" => {
  257. "CipherString" => "DEFAULT:\@SECLEVEL=0",
  258. "MaxProtocol" => $protocols[$resume_protocol],
  259. },
  260. "test" => {
  261. "ExpectedProtocol" => $protocols[$resume_protocol],
  262. "Method" => $method,
  263. "HandshakeMode" => "Resume",
  264. "ResumptionExpected" => $resumption_expected,
  265. }
  266. };
  267. $client_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
  268. }
  269. }
  270. }
  271. }
  272. if (!disabled("tls1_3") && !$dtls) {
  273. push @client_tests, {
  274. "name" => "resumption-with-hrr",
  275. "client" => {
  276. },
  277. "server" => {
  278. "Curves" => disabled("ec") ? "ffdhe3072" : "P-256"
  279. },
  280. "resume_client" => {
  281. },
  282. "test" => {
  283. "ExpectedProtocol" => "TLSv1.3",
  284. "Method" => "TLS",
  285. "HandshakeMode" => "Resume",
  286. "ResumptionExpected" => "Yes",
  287. }
  288. };
  289. }
  290. return (@server_tests, @client_tests);
  291. }
  292. sub expected_result {
  293. my ($c_min, $c_max, $s_min, $s_max, $min_enabled, $max_enabled,
  294. $protocols) = @_;
  295. # Adjust for "undef" (no limit).
  296. $c_min = $c_min == 0 ? 0 : $c_min - 1;
  297. $c_max = $c_max == scalar @$protocols ? $c_max - 1 : $c_max;
  298. $s_min = $s_min == 0 ? 0 : $s_min - 1;
  299. $s_max = $s_max == scalar @$protocols ? $s_max - 1 : $s_max;
  300. # We now have at least one protocol enabled, so $min_enabled and
  301. # $max_enabled are well-defined.
  302. $c_min = max $c_min, $min_enabled;
  303. $s_min = max $s_min, $min_enabled;
  304. $c_max = min $c_max, $max_enabled;
  305. $s_max = min $s_max, $max_enabled;
  306. if ($c_min > $c_max) {
  307. # Client should fail to even send a hello.
  308. return ("ClientFail", undef);
  309. } elsif ($s_min > $s_max) {
  310. # Server has no protocols, should always fail.
  311. return ("ServerFail", undef);
  312. } elsif ($s_min > $c_max) {
  313. # Server doesn't support the client range.
  314. return ("ServerFail", undef);
  315. } elsif ($c_min > $s_max) {
  316. my @prots = @$protocols;
  317. if ($prots[$c_max] eq "TLSv1.3") {
  318. # Client will have sent supported_versions, so server will know
  319. # that there are no overlapping versions.
  320. return ("ServerFail", undef);
  321. } else {
  322. # Server will try with a version that is lower than the lowest
  323. # supported client version.
  324. return ("ClientFail", undef);
  325. }
  326. } else {
  327. # Server and client ranges overlap.
  328. my $max_common = $s_max < $c_max ? $s_max : $c_max;
  329. return ("Success", $protocols->[$max_common]);
  330. }
  331. }
  332. 1;