70-test_sslvertol.t: Make sure to check a max TLS version that matches configuration
[openssl.git] / test / recipes / 70-test_sslvertol.t
1 #! /usr/bin/env perl
2 # Copyright 2015-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 use strict;
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use TLSProxy::Proxy;
13
14 my $test_name = "test_sslextension";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS|MSWin32)$/;
19
20 plan skip_all => "$test_name needs the dynamic engine feature enabled"
21     if disabled("engine") || disabled("dynamic-engine");
22
23 plan skip_all => "$test_name needs the sock feature enabled"
24     if disabled("sock");
25
26 plan skip_all => "$test_name needs TLS enabled"
27     if alldisabled(available_protocols("tls"));
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     \&vers_tolerance_filter,
32     cmdstr(app(["openssl"]), display => 1),
33     srctop_file("apps", "server.pem"),
34     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35 );
36
37 my @available_tls_versions = ();
38 foreach (available_protocols("tls")) {
39     unless (disabled($_)) {
40         note("Checking enabled protocol $_");
41         m|^([a-z]+)(\d)(_\d)?|;
42         my $versionname;
43         if (defined $3) {
44             $versionname = 'TLSProxy::Record::VERS_'.uc($1).'_'.$2.$3;
45             note("'$1', '$2', '$3' => $versionname");
46         } else {
47             $versionname = 'TLSProxy::Record::VERS_'.uc($1).'_'.$2.'_0';
48             note("'$1', '$2' => $versionname");
49         }
50         push @available_tls_versions, eval $versionname;
51     }
52 }
53 note("TLS versions we can expect: ", join(", ", @available_tls_versions));
54
55 #This file does tests without the supported_versions extension.
56 #See 70-test_sslversions.t for tests with supported versions.
57 #Test 1: Asking for TLS1.4 should pass and negotiate the maximum
58 #available TLS version according to configuration below TLS1.3
59 my $client_version = TLSProxy::Record::VERS_TLS_1_4;
60 $proxy->clientflags("-no_tls1_3");
61 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
62 plan tests => 3;
63 my $record = pop @{$proxy->record_list};
64 ok((note("Record version received: ".
65          (defined $record ? $record->version() : "none")),
66     TLSProxy::Message->success())
67    && $record->version() == tls_version_below(TLSProxy::Record::VERS_TLS_1_3),
68    "Version tolerance test, below TLS 1.4 and not TLS 1.3");
69
70 #Test 2: Asking for TLS1.3 with that disabled should succeed and negotiate
71 #the highest configured TLS version below that.
72 $client_version = TLSProxy::Record::VERS_TLS_1_3;
73 $proxy->clear();
74 $proxy->clientflags("-no_tls1_3");
75 $proxy->start();
76 $record = pop @{$proxy->record_list};
77 ok((note("Record version received: ".
78          (defined $record ? $record->version() : "none")),
79     TLSProxy::Message->success())
80    && $record->version() == tls_version_below(TLSProxy::Record::VERS_TLS_1_3),
81    "Version tolerance test, max version but not TLS 1.3");
82
83 #Test 3: Testing something below SSLv3 should fail.  We must disable TLS 1.3
84 #to avoid having the 'supported_versions' extension kick in and override our
85 #desires.
86 $client_version = TLSProxy::Record::VERS_SSL_3_0 - 1;
87 $proxy->clear();
88 $proxy->clientflags("-no_tls1_3");
89 $proxy->start();
90 $record = pop @{$proxy->record_list};
91 ok((note("Record version received: ".
92          (defined $record ? $record->version() : "none")),
93     TLSProxy::Message->fail()),
94    "Version tolerance test, SSL < 3.0");
95
96 sub vers_tolerance_filter
97 {
98     my $proxy = shift;
99
100     # We're only interested in the initial ClientHello
101     if ($proxy->flight != 0) {
102         return;
103     }
104
105     foreach my $message (@{$proxy->message_list}) {
106         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
107             #Set the client version
108             #Anything above the max supported version should succeed
109             #Anything below SSLv3 should fail
110             $message->client_version($client_version);
111             $message->repack();
112         }
113     }
114 }
115
116 sub tls_version_below {
117     if (@_) {
118         my $term = shift;
119         my $res = undef;
120
121         foreach (@available_tls_versions) {
122             $res = $_ if $_ < $term;
123         }
124         return $res;
125     }
126     return $available_tls_versions[-1];
127 }