22e1f360e910a17a651ab83384d273c121d083dc
[openssl.git] / test / ssl-tests / 02-protocol-version.conf.in
1 # -*- mode: perl; -*-
2 # Copyright 2016-2016 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
9
10 ## Test version negotiation
11
12 package ssltests;
13
14 use List::Util qw/max min/;
15
16 use OpenSSL::Test;
17 use OpenSSL::Test::Utils qw/anydisabled alldisabled/;
18 setup("no_test_here");
19
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);
24
25 my @is_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2");
26
27 my $min_enabled; my $max_enabled;
28
29 # Protocol configuration works in cascades, i.e.,
30 # $no_tls1_1 disables TLSv1.1 and below.
31 #
32 # $min_enabled and $max_enabled will be correct if there is at least one
33 # protocol enabled.
34 foreach my $i (0..$#protocols) {
35     if (!$is_disabled[$i]) {
36         $min_enabled = $i;
37         last;
38     }
39 }
40
41 foreach my $i (0..$#protocols) {
42     if (!$is_disabled[$i]) {
43         $max_enabled = $i;
44     }
45 }
46
47 our @tests = ();
48
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);
58                     push @tests, {
59                         "name" => "version-negotiation",
60                         "client" => {
61                             "MinProtocol" => $min_protocols[$c_min],
62                             "MaxProtocol" => $max_protocols[$c_max],
63                         },
64                         "server" => {
65                             "MinProtocol" => $min_protocols[$s_min],
66                             "MaxProtocol" => $max_protocols[$s_max],
67                         },
68                         "test" => {
69                             "ExpectedResult" => $result,
70                             "Protocol" => $protocol
71                         }
72                     };
73                 }
74             }
75         }
76     }
77 }
78
79 sub expected_result {
80     my $no_tls = alldisabled("ssl3", "tls1", "tls1_1", "tls1_2");
81     if ($no_tls) {
82         return ("InternalError", undef);
83     }
84
85     my ($c_min, $c_max, $s_min, $s_max) = @_;
86
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;
92
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;
99
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);
115     } else {
116         # Server and client ranges overlap.
117         my $max_common = $s_max < $c_max ? $s_max : $c_max;
118         return ("Success", $protocols[$max_common]);
119     }
120 }
121
122 generate_tests();