protocol_version.pm 10.8 KB
Newer Older
1
# -*- mode: perl; -*-
M
Matt Caswell 已提交
2
# Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html


## Test version negotiation

package ssltests;

use strict;
use warnings;

use List::Util qw/max min/;

use OpenSSL::Test;
20
use OpenSSL::Test::Utils qw/anydisabled alldisabled disabled/;
21 22
setup("no_test_here");

23
my @tls_protocols = ("SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3");
24
# undef stands for "no limit".
25 26
my @min_tls_protocols = (undef, "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3");
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

my $min_tls_enabled; my $max_tls_enabled;

# Protocol configuration works in cascades, i.e.,
# $no_tls1_1 disables TLSv1.1 and below.
#
# $min_enabled and $max_enabled will be correct if there is at least one
# protocol enabled.
foreach my $i (0..$#tls_protocols) {
    if (!$is_tls_disabled[$i]) {
        $min_tls_enabled = $i;
        last;
    }
}

foreach my $i (0..$#tls_protocols) {
    if (!$is_tls_disabled[$i]) {
        $max_tls_enabled = $i;
    }
}

my @dtls_protocols = ("DTLSv1", "DTLSv1.2");
# undef stands for "no limit".
my @min_dtls_protocols = (undef, "DTLSv1", "DTLSv1.2");
my @max_dtls_protocols = ("DTLSv1", "DTLSv1.2", undef);

my @is_dtls_disabled = anydisabled("dtls1", "dtls1_2");

my $min_dtls_enabled; my $max_dtls_enabled;

# $min_enabled and $max_enabled will be correct if there is at least one
# protocol enabled.
foreach my $i (0..$#dtls_protocols) {
    if (!$is_dtls_disabled[$i]) {
        $min_dtls_enabled = $i;
        last;
    }
}

foreach my $i (0..$#dtls_protocols) {
    if (!$is_dtls_disabled[$i]) {
        $max_dtls_enabled = $i;
    }
}

74 75 76
sub no_tests {
    my ($dtls) = @_;
    return $dtls ? alldisabled("dtls1", "dtls1_2") :
77
      alldisabled("ssl3", "tls1", "tls1_1", "tls1_2", "tls1_3");
78 79 80
}

sub generate_version_tests {
81 82 83 84
    my ($method) = @_;

    my $dtls = $method eq "DTLS";
    # Don't write the redundant "Method = TLS" into the configuration.
85
    undef $method if !$dtls;
86 87 88 89 90 91 92

    my @protocols = $dtls ? @dtls_protocols : @tls_protocols;
    my @min_protocols = $dtls ? @min_dtls_protocols : @min_tls_protocols;
    my @max_protocols = $dtls ? @max_dtls_protocols : @max_tls_protocols;
    my $min_enabled  = $dtls ? $min_dtls_enabled : $min_tls_enabled;
    my $max_enabled  = $dtls ? $max_dtls_enabled : $max_tls_enabled;

93
    if (no_tests($dtls)) {
94 95 96 97 98
        return;
    }

    my @tests = ();

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    for (my $sctp = 0; $sctp < ($dtls && !disabled("sctp") ? 2 : 1); $sctp++) {
        foreach my $c_min (0..$#min_protocols) {
            my $c_max_min = $c_min == 0 ? 0 : $c_min - 1;
            foreach my $c_max ($c_max_min..$#max_protocols) {
                foreach my $s_min (0..$#min_protocols) {
                    my $s_max_min = $s_min == 0 ? 0 : $s_min - 1;
                    foreach my $s_max ($s_max_min..$#max_protocols) {
                        my ($result, $protocol) =
                            expected_result($c_min, $c_max, $s_min, $s_max,
                                            $min_enabled, $max_enabled,
                                            \@protocols);
                        push @tests, {
                            "name" => "version-negotiation",
                            "client" => {
                                "MinProtocol" => $min_protocols[$c_min],
                                "MaxProtocol" => $max_protocols[$c_max],
                            },
                            "server" => {
                                "MinProtocol" => $min_protocols[$s_min],
                                "MaxProtocol" => $max_protocols[$s_max],
                            },
                            "test" => {
                                "ExpectedResult" => $result,
                                "ExpectedProtocol" => $protocol,
                                "Method" => $method,
                            }
                        };
                        $tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
                    }
128 129 130 131
                }
            }
        }
    }
132 133 134 135 136 137 138 139
    return @tests if disabled("tls1_3") || disabled("tls1_2") || $dtls;

    #Add some version/ciphersuite sanity check tests
    push @tests, {
        "name" => "ciphersuite-sanity-check-client",
        "client" => {
            #Offering only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
            "CipherString" => "AES128-SHA",
140
            "Ciphersuites" => "",
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        },
        "server" => {
            "MaxProtocol" => "TLSv1.2"
        },
        "test" => {
            "ExpectedResult" => "ClientFail",
        }
    };
    push @tests, {
        "name" => "ciphersuite-sanity-check-server",
        "client" => {
            "CipherString" => "AES128-SHA",
            "MaxProtocol" => "TLSv1.2"
        },
        "server" => {
            #Allowing only <=TLSv1.2 ciphersuites with TLSv1.3 should fail
            "CipherString" => "AES128-SHA",
158
            "Ciphersuites" => "",
159 160 161 162 163 164
        },
        "test" => {
            "ExpectedResult" => "ServerFail",
        }
    };

165 166 167
    return @tests;
}

168 169 170 171 172 173 174 175 176
sub generate_resumption_tests {
    my ($method) = @_;

    my $dtls = $method eq "DTLS";
    # Don't write the redundant "Method = TLS" into the configuration.
    undef $method if !$dtls;

    my @protocols = $dtls ? @dtls_protocols : @tls_protocols;
    my $min_enabled  = $dtls ? $min_dtls_enabled : $min_tls_enabled;
177
    my $max_enabled = $dtls ? $max_dtls_enabled : $max_tls_enabled;
178 179 180 181 182

    if (no_tests($dtls)) {
        return;
    }

E
Emilia Kasper 已提交
183 184
    my @server_tests = ();
    my @client_tests = ();
185

E
Emilia Kasper 已提交
186
    # Obtain the first session against a fixed-version server/client.
187
    foreach my $original_protocol($min_enabled..$max_enabled) {
E
Emilia Kasper 已提交
188
        # Upgrade or downgrade the server/client max version support and test
189
        # that it upgrades, downgrades or resumes the session as well.
190
        foreach my $resume_protocol($min_enabled..$max_enabled) {
191 192 193 194 195 196 197 198
            my $resumption_expected;
            # We should only resume on exact version match.
            if ($original_protocol eq $resume_protocol) {
                $resumption_expected = "Yes";
            } else {
                $resumption_expected = "No";
            }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
            for (my $sctp = 0; $sctp < ($dtls && !disabled("sctp") ? 2 : 1);
                 $sctp++) {
                foreach my $ticket ("SessionTicket", "-SessionTicket") {
                    # Client is flexible, server upgrades/downgrades.
                    push @server_tests, {
                        "name" => "resumption",
                        "client" => { },
                        "server" => {
                            "MinProtocol" => $protocols[$original_protocol],
                            "MaxProtocol" => $protocols[$original_protocol],
                            "Options" => $ticket,
                        },
                        "resume_server" => {
                            "MaxProtocol" => $protocols[$resume_protocol],
                        },
                        "test" => {
                            "ExpectedProtocol" => $protocols[$resume_protocol],
                            "Method" => $method,
                            "HandshakeMode" => "Resume",
                            "ResumptionExpected" => $resumption_expected,
                        }
                    };
                    $server_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
                    # Server is flexible, client upgrades/downgrades.
                    push @client_tests, {
                        "name" => "resumption",
                        "client" => {
                            "MinProtocol" => $protocols[$original_protocol],
                            "MaxProtocol" => $protocols[$original_protocol],
                        },
                        "server" => {
                            "Options" => $ticket,
                        },
                        "resume_client" => {
                            "MaxProtocol" => $protocols[$resume_protocol],
                        },
                        "test" => {
                            "ExpectedProtocol" => $protocols[$resume_protocol],
                            "Method" => $method,
                            "HandshakeMode" => "Resume",
                            "ResumptionExpected" => $resumption_expected,
                        }
                    };
                    $client_tests[-1]{"test"}{"UseSCTP"} = "Yes" if $sctp;
                }
244 245 246 247
            }
        }
    }

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    if (!disabled("tls1_3") && !$dtls) {
        push @client_tests, {
            "name" => "resumption-with-hrr",
            "client" => {
            },
            "server" => {
                "Curves" => "P-256"
            },
            "resume_client" => {
            },
            "test" => {
                "ExpectedProtocol" => "TLSv1.3",
                "Method" => "TLS",
                "HandshakeMode" => "Resume",
                "ResumptionExpected" => "Yes",
            }
        };
    }

E
Emilia Kasper 已提交
267
    return (@server_tests, @client_tests);
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
sub expected_result {
    my ($c_min, $c_max, $s_min, $s_max, $min_enabled, $max_enabled,
        $protocols) = @_;

    # Adjust for "undef" (no limit).
    $c_min = $c_min == 0 ? 0 : $c_min - 1;
    $c_max = $c_max == scalar @$protocols ? $c_max - 1 : $c_max;
    $s_min = $s_min == 0 ? 0 : $s_min - 1;
    $s_max = $s_max == scalar @$protocols ? $s_max - 1 : $s_max;

    # We now have at least one protocol enabled, so $min_enabled and
    # $max_enabled are well-defined.
    $c_min = max $c_min, $min_enabled;
    $s_min = max $s_min, $min_enabled;
    $c_max = min $c_max, $max_enabled;
    $s_max = min $s_max, $max_enabled;

    if ($c_min > $c_max) {
        # Client should fail to even send a hello.
289
        return ("ClientFail", undef);
290 291 292 293 294 295 296
    } elsif ($s_min > $s_max) {
        # Server has no protocols, should always fail.
        return ("ServerFail", undef);
    } elsif ($s_min > $c_max) {
        # Server doesn't support the client range.
        return ("ServerFail", undef);
    } elsif ($c_min > $s_max) {
297
        my @prots = @$protocols;
298 299 300 301
        if ($prots[$c_max] eq "TLSv1.3") {
            # Client will have sent supported_versions, so server will know
            # that there are no overlapping versions.
            return ("ServerFail", undef);
302 303 304 305 306
        } else {
            # Server will try with a version that is lower than the lowest
            # supported client version.
            return ("ClientFail", undef);
        }
307 308 309 310 311 312 313 314
    } else {
        # Server and client ranges overlap.
        my $max_common = $s_max < $c_max ? $s_max : $c_max;
        return ("Success", $protocols->[$max_common]);
    }
}

1;