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