2 # Copyright 2016-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 ## Test version negotiation
14 use List::Util qw/max min/;
17 use OpenSSL::Test::Utils qw/anydisabled alldisabled/;
18 setup("no_test_here");
20 my @protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
21 # undef stands for "no limit".
22 my @min_protocols = (undef, "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
23 my @max_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", undef);
25 my @is_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2");
27 my $min_enabled; my $max_enabled;
29 # Protocol configuration works in cascades, i.e.,
30 # $no_tls1_1 disables TLSv1.1 and below.
32 # $min_enabled and $max_enabled will be correct if there is at least one
34 foreach my $i (0..$#protocols) {
35 if (!$is_disabled[$i]) {
41 foreach my $i (0..$#protocols) {
42 if (!$is_disabled[$i]) {
49 sub generate_tests() {
50 foreach my $c_min (0..$#min_protocols) {
51 my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
52 foreach my $c_max ($c_max_min..$#max_protocols) {
53 foreach my $s_min (0..$#min_protocols) {
54 my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
55 foreach my $s_max ($s_max_min..$#max_protocols) {
56 my ($result, $protocol) =
57 expected_result($c_min, $c_max, $s_min, $s_max);
59 "name" => "version-negotiation",
61 "MinProtocol" => $min_protocols[$c_min],
62 "MaxProtocol" => $max_protocols[$c_max],
65 "MinProtocol" => $min_protocols[$s_min],
66 "MaxProtocol" => $max_protocols[$s_max],
69 "ExpectedResult" => $result,
70 "Protocol" => $protocol
80 my $no_tls = alldisabled("ssl3", "tls1", "tls1_1", "tls1_2");
82 return ("InternalError", undef);
85 my ($c_min, $c_max, $s_min, $s_max) = @_;
87 # Adjust for "undef" (no limit).
88 $c_min = $c_min == 0 ? 0 : $c_min - 1;
89 $c_max = $c_max == scalar(@max_protocols) - 1 ? $c_max - 1 : $c_max;
90 $s_min = $s_min == 0 ? 0 : $s_min - 1;
91 $s_max = $s_max == scalar(@max_protocols) - 1 ? $s_max - 1 : $s_max;
93 # We now have at least one protocol enabled, so $min_enabled and
94 # $max_enabled are well-defined.
95 $c_min = max $c_min, $min_enabled;
96 $s_min = max $s_min, $min_enabled;
97 $c_max = min $c_max, $max_enabled;
98 $s_max = min $s_max, $max_enabled;
100 if ($c_min > $c_max) {
101 # Client should fail to even send a hello.
102 # This results in an internal error since the server will be
103 # waiting for input that never arrives.
104 return ("InternalError", undef);
105 } elsif ($s_min > $s_max) {
106 # Server has no protocols, should always fail.
107 return ("ServerFail", undef);
108 } elsif ($s_min > $c_max) {
109 # Server doesn't support the client range.
110 return ("ServerFail", undef);
111 } elsif ($c_min > $s_max) {
112 # Server will try with a version that is lower than the lowest
113 # supported client version.
114 return ("ClientFail", undef);
116 # Server and client ranges overlap.
117 my $max_common = $s_max < $c_max ? $s_max : $c_max;
118 return ("Success", $protocols[$max_common]);