SSL test framework: port NPN and ALPN tests
[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");
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);
27
28 my @is_tls_disabled = anydisabled("ssl3", "tls1", "tls1_1", "tls1_2");
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 generate_tests {
75     my ($method) = @_;
76
77     my $dtls = $method eq "DTLS";
78     # Don't write the redundant "Method = TLS" into the configuration.
79     undef $method if !$dtls; 
80
81     my @protocols = $dtls ? @dtls_protocols : @tls_protocols;
82     my @min_protocols = $dtls ? @min_dtls_protocols : @min_tls_protocols;
83     my @max_protocols = $dtls ? @max_dtls_protocols : @max_tls_protocols;
84     my $min_enabled  = $dtls ? $min_dtls_enabled : $min_tls_enabled;
85     my $max_enabled  = $dtls ? $max_dtls_enabled : $max_tls_enabled;
86
87     my $no_tests = $dtls ? alldisabled("dtls1", "dtls1_2") :
88       alldisabled("ssl3", "tls1", "tls1_1", "tls1_2");
89     if ($no_tests) {
90         return;
91     }
92
93     my @tests = ();
94
95     foreach my $c_min (0..$#min_protocols) {
96         my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
97         foreach my $c_max ($c_max_min..$#max_protocols) {
98             foreach my $s_min (0..$#min_protocols) {
99                 my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
100                 foreach my $s_max ($s_max_min..$#max_protocols) {
101                     my ($result, $protocol) =
102                         expected_result($c_min, $c_max, $s_min, $s_max,
103                                         $min_enabled, $max_enabled, \@protocols);
104                     push @tests, {
105                         "name" => "version-negotiation",
106                         "client" => {
107                             "MinProtocol" => $min_protocols[$c_min],
108                             "MaxProtocol" => $max_protocols[$c_max],
109                         },
110                         "server" => {
111                             "MinProtocol" => $min_protocols[$s_min],
112                             "MaxProtocol" => $max_protocols[$s_max],
113                         },
114                         "test" => {
115                             "ExpectedResult" => $result,
116                             "Protocol" => $protocol,
117                             "Method" => $method,
118                         }
119                     };
120                 }
121             }
122         }
123     }
124     return @tests;
125 }
126
127 sub expected_result {
128     my ($c_min, $c_max, $s_min, $s_max, $min_enabled, $max_enabled,
129         $protocols) = @_;
130
131     # Adjust for "undef" (no limit).
132     $c_min = $c_min == 0 ? 0 : $c_min - 1;
133     $c_max = $c_max == scalar @$protocols ? $c_max - 1 : $c_max;
134     $s_min = $s_min == 0 ? 0 : $s_min - 1;
135     $s_max = $s_max == scalar @$protocols ? $s_max - 1 : $s_max;
136
137     # We now have at least one protocol enabled, so $min_enabled and
138     # $max_enabled are well-defined.
139     $c_min = max $c_min, $min_enabled;
140     $s_min = max $s_min, $min_enabled;
141     $c_max = min $c_max, $max_enabled;
142     $s_max = min $s_max, $max_enabled;
143
144     if ($c_min > $c_max) {
145         # Client should fail to even send a hello.
146         # This results in an internal error since the server will be
147         # waiting for input that never arrives.
148         return ("InternalError", undef);
149     } elsif ($s_min > $s_max) {
150         # Server has no protocols, should always fail.
151         return ("ServerFail", undef);
152     } elsif ($s_min > $c_max) {
153         # Server doesn't support the client range.
154         return ("ServerFail", undef);
155     } elsif ($c_min > $s_max) {
156         # Server will try with a version that is lower than the lowest
157         # supported client version.
158         return ("ClientFail", undef);
159     } else {
160         # Server and client ranges overlap.
161         my $max_common = $s_max < $c_max ? $s_max : $c_max;
162         return ("Success", $protocols->[$max_common]);
163     }
164 }
165
166 1;