protocol_version.pm 11 KB

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