3 ## Test version negotiation
7 use List::Util qw/max min/;
10 use OpenSSL::Test::Utils qw/anydisabled alldisabled/;
11 setup("no_test_here");
13 my @protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
14 # undef stands for "no limit".
15 my @min_protocols = (undef, "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
16 my @max_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", undef);
18 my @is_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2");
20 my $min_enabled; my $max_enabled;
22 # Protocol configuration works in cascades, i.e.,
23 # $no_tls1_1 disables TLSv1.1 and below.
25 # $min_enabled and $max_enabled will be correct if there is at least one
27 foreach my $i (0..$#protocols) {
28 if (!$is_disabled[$i]) {
34 foreach my $i (0..$#protocols) {
35 if (!$is_disabled[$i]) {
42 sub generate_tests() {
43 foreach my $c_min (0..$#min_protocols) {
44 my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
45 foreach my $c_max ($c_max_min..$#max_protocols) {
46 foreach my $s_min (0..$#min_protocols) {
47 my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
48 foreach my $s_max ($s_max_min..$#max_protocols) {
49 my ($result, $protocol) =
50 expected_result($c_min, $c_max, $s_min, $s_max);
52 "name" => "version-negotiation",
54 "MinProtocol" => $min_protocols[$c_min],
55 "MaxProtocol" => $max_protocols[$c_max],
58 "MinProtocol" => $min_protocols[$s_min],
59 "MaxProtocol" => $max_protocols[$s_max],
62 "ExpectedResult" => $result,
63 "Protocol" => $protocol
73 my $no_tls = alldisabled("ssl3", "tls1", "tls1_1", "tls1_2");
75 return ("InternalError", undef);
78 my ($c_min, $c_max, $s_min, $s_max) = @_;
80 # Adjust for "undef" (no limit).
81 $c_min = $c_min == 0 ? 0 : $c_min - 1;
82 $c_max = $c_max == scalar(@max_protocols) - 1 ? $c_max - 1 : $c_max;
83 $s_min = $s_min == 0 ? 0 : $s_min - 1;
84 $s_max = $s_max == scalar(@max_protocols) - 1 ? $s_max - 1 : $s_max;
86 # We now have at least one protocol enabled, so $min_enabled and
87 # $max_enabled are well-defined.
88 $c_min = max $c_min, $min_enabled;
89 $s_min = max $s_min, $min_enabled;
90 $c_max = min $c_max, $max_enabled;
91 $s_max = min $s_max, $max_enabled;
93 if ($c_min > $c_max) {
94 # Client should fail to even send a hello.
95 # This results in an internal error since the server will be
96 # waiting for input that never arrives.
97 return ("InternalError", undef);
98 } elsif ($s_min > $s_max) {
99 # Server has no protocols, should always fail.
100 return ("ServerFail", undef);
101 } elsif ($s_min > $c_max) {
102 # Server doesn't support the client range.
103 return ("ServerFail", undef);
104 } elsif ($c_min > $s_max) {
105 # Server will try with a version that is lower than the lowest
106 # supported client version.
107 return ("ClientFail", undef);
109 # Server and client ranges overlap.
110 my $max_common = $s_max < $c_max ? $s_max : $c_max;
111 return ("Success", $protocols[$max_common]);