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
17 use List::Util qw/max min/;
20 use OpenSSL::Test::Utils qw/anydisabled alldisabled/;
21 setup("no_test_here");
23 my @tls_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
24 # undef stands for "no limit".
25 my @min_tls_protocols = (undef, "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2");
26 my @max_tls_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", undef);
28 my @is_tls_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2");
30 my $min_tls_enabled; my $max_tls_enabled;
32 # Protocol configuration works in cascades, i.e.,
33 # $no_tls1_1 disables TLSv1.1 and below.
35 # $min_enabled and $max_enabled will be correct if there is at least one
37 foreach my $i (0..$#tls_protocols) {
38 if (!$is_tls_disabled[$i]) {
39 $min_tls_enabled = $i;
44 foreach my $i (0..$#tls_protocols) {
45 if (!$is_tls_disabled[$i]) {
46 $max_tls_enabled = $i;
50 my @dtls_protocols = ("DTLSv1", "DTLSv1.2");
51 # undef stands for "no limit".
52 my @min_dtls_protocols = (undef, "DTLSv1", "DTLSv1.2");
53 my @max_dtls_protocols = ("DTLSv1", "DTLSv1.2", undef);
55 my @is_dtls_disabled = anydisabled("dtls1", "dtls1_2");
57 my $min_dtls_enabled; my $max_dtls_enabled;
59 # $min_enabled and $max_enabled will be correct if there is at least one
61 foreach my $i (0..$#dtls_protocols) {
62 if (!$is_dtls_disabled[$i]) {
63 $min_dtls_enabled = $i;
68 foreach my $i (0..$#dtls_protocols) {
69 if (!$is_dtls_disabled[$i]) {
70 $max_dtls_enabled = $i;
76 return $dtls ? alldisabled("dtls1", "dtls1_2") :
77 alldisabled("ssl3", "tls1", "tls1_1", "tls1_2");
80 sub generate_version_tests {
83 my $dtls = $method eq "DTLS";
84 # Don't write the redundant "Method = TLS" into the configuration.
85 undef $method if !$dtls;
87 my @protocols = $dtls ? @dtls_protocols : @tls_protocols;
88 my @min_protocols = $dtls ? @min_dtls_protocols : @min_tls_protocols;
89 my @max_protocols = $dtls ? @max_dtls_protocols : @max_tls_protocols;
90 my $min_enabled = $dtls ? $min_dtls_enabled : $min_tls_enabled;
91 my $max_enabled = $dtls ? $max_dtls_enabled : $max_tls_enabled;
93 if (no_tests($dtls)) {
99 foreach my $c_min (0..$#min_protocols) {
100 my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
101 foreach my $c_max ($c_max_min..$#max_protocols) {
102 foreach my $s_min (0..$#min_protocols) {
103 my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
104 foreach my $s_max ($s_max_min..$#max_protocols) {
105 my ($result, $protocol) =
106 expected_result($c_min, $c_max, $s_min, $s_max,
107 $min_enabled, $max_enabled, \@protocols);
109 "name" => "version-negotiation",
111 "MinProtocol" => $min_protocols[$c_min],
112 "MaxProtocol" => $max_protocols[$c_max],
115 "MinProtocol" => $min_protocols[$s_min],
116 "MaxProtocol" => $max_protocols[$s_max],
119 "ExpectedResult" => $result,
120 "Protocol" => $protocol,
131 sub generate_resumption_tests {
134 my $dtls = $method eq "DTLS";
135 # Don't write the redundant "Method = TLS" into the configuration.
136 undef $method if !$dtls;
138 my @protocols = $dtls ? @dtls_protocols : @tls_protocols;
139 my $min_enabled = $dtls ? $min_dtls_enabled : $min_tls_enabled;
141 if (no_tests($dtls)) {
147 # Obtain the first session against a fixed-version server.
148 foreach my $original_protocol($min_enabled..$#protocols) {
149 # Upgrade or downgrade the server max version support and test
150 # that it upgrades, downgrades or resumes the session as well.
151 foreach my $resume_protocol($min_enabled..$#protocols) {
152 my $resumption_expected;
153 # We should only resume on exact version match.
154 if ($original_protocol eq $resume_protocol) {
155 $resumption_expected = "Yes";
157 $resumption_expected = "No";
160 foreach my $ticket ("SessionTicket", "-SessionTicket") {
162 "name" => "resumption",
165 "MinProtocol" => $protocols[$original_protocol],
166 "MaxProtocol" => $protocols[$original_protocol],
167 "Options" => $ticket,
170 "MaxProtocol" => $protocols[$resume_protocol],
173 "Protocol" => $protocols[$resume_protocol],
175 "HandshakeMode" => "Resume",
176 "ResumptionExpected" => $resumption_expected,
186 sub expected_result {
187 my ($c_min, $c_max, $s_min, $s_max, $min_enabled, $max_enabled,
190 # Adjust for "undef" (no limit).
191 $c_min = $c_min == 0 ? 0 : $c_min - 1;
192 $c_max = $c_max == scalar @$protocols ? $c_max - 1 : $c_max;
193 $s_min = $s_min == 0 ? 0 : $s_min - 1;
194 $s_max = $s_max == scalar @$protocols ? $s_max - 1 : $s_max;
196 # We now have at least one protocol enabled, so $min_enabled and
197 # $max_enabled are well-defined.
198 $c_min = max $c_min, $min_enabled;
199 $s_min = max $s_min, $min_enabled;
200 $c_max = min $c_max, $max_enabled;
201 $s_max = min $s_max, $max_enabled;
203 if ($c_min > $c_max) {
204 # Client should fail to even send a hello.
205 # This results in an internal error since the server will be
206 # waiting for input that never arrives.
207 return ("InternalError", undef);
208 } elsif ($s_min > $s_max) {
209 # Server has no protocols, should always fail.
210 return ("ServerFail", undef);
211 } elsif ($s_min > $c_max) {
212 # Server doesn't support the client range.
213 return ("ServerFail", undef);
214 } elsif ($c_min > $s_max) {
215 # Server will try with a version that is lower than the lowest
216 # supported client version.
217 return ("ClientFail", undef);
219 # Server and client ranges overlap.
220 my $max_common = $s_max < $c_max ? $s_max : $c_max;
221 return ("Success", $protocols->[$max_common]);