2 # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11 use OpenSSL::Test::Utils;
13 use File::Temp qw(tempfile);
16 REVERSE_ORDER_VERSIONS => 1,
17 UNRECOGNISED_VERSIONS => 2,
20 TLS1_1_AND_1_0_ONLY => 5,
22 BAD_LEGACY_VERSION => 7
27 my $test_name = "test_sslversions";
30 plan skip_all => "TLSProxy isn't usable on $^O"
33 plan skip_all => "$test_name needs the dynamic engine feature enabled"
34 if disabled("engine") || disabled("dynamic-engine");
36 plan skip_all => "$test_name needs the sock feature enabled"
39 plan skip_all => "$test_name needs TLS1.3, TLS1.2 and TLS1.1 enabled"
40 if disabled("tls1_3") || disabled("tls1_2") || disabled("tls1_1");
42 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
44 my $proxy = TLSProxy::Proxy->new(
46 cmdstr(app(["openssl"]), display => 1),
47 srctop_file("apps", "server.pem"),
48 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
51 #We're just testing various negative and unusual scenarios here. ssltest with
52 #02-protocol-version.cnf should check all the various combinations of normal
55 #Test 1: An empty supported_versions extension should not succeed
56 $testtype = EMPTY_EXTENSION;
57 $proxy->filter(\&modify_supported_versions_filter);
58 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
60 ok(TLSProxy::Message->fail(), "Empty supported versions");
62 #Test 2: supported_versions extension with no recognised versions should not
65 $testtype = UNRECOGNISED_VERSIONS;
67 ok(TLSProxy::Message->fail(), "No recognised versions");
69 #Test 3: No supported versions extensions should succeed and select TLSv1.2
71 $testtype = NO_EXTENSION;
73 my $record = pop @{$proxy->record_list};
74 ok(TLSProxy::Message->success()
75 && $record->version() == TLSProxy::Record::VERS_TLS_1_2,
76 "No supported versions extension");
78 #Test 4: No supported versions extensions should fail if only TLS1.3 available
80 $proxy->serverflags("-tls1_3");
82 ok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)");
84 #Test 5: supported versions extension with best version last should succeed
87 $testtype = REVERSE_ORDER_VERSIONS;
89 $record = pop @{$proxy->record_list};
90 ok(TLSProxy::Message->success()
91 && $record->version() == TLSProxy::Record::VERS_TLS_1_2
92 && TLSProxy::Proxy->is_tls13(),
93 "Reverse order versions");
95 #Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but
96 #TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed
98 $proxy->clientflags("-cipher DEFAULT:\@SECLEVEL=0");
99 $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
100 $testtype = TLS1_1_AND_1_0_ONLY;
102 $record = pop @{$proxy->record_list};
103 ok(TLSProxy::Message->success()
104 && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
105 "TLS1.1 and TLS1.0 in supported versions extension only");
107 #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
109 $testtype = WITH_TLS1_4;
111 $record = pop @{$proxy->record_list};
112 ok(TLSProxy::Message->success()
113 && $record->version() == TLSProxy::Record::VERS_TLS_1_2
114 && TLSProxy::Proxy->is_tls13(),
115 "TLS1.4 in supported versions extension");
117 #Test 8: Set the legacy version to SSLv3 with supported versions. Should fail
119 $testtype = BAD_LEGACY_VERSION;
121 ok(TLSProxy::Message->fail(), "Legacy version is SSLv3 with supported versions");
123 sub modify_supported_versions_filter
127 if ($proxy->flight == 1) {
128 # Change the ServerRandom so that the downgrade sentinel doesn't cause
129 # the connection to fail
130 my $message = ${$proxy->message_list}[1];
131 return if (!defined $message);
133 $message->random("\0"x32);
138 # We're only interested in the initial ClientHello
139 if ($proxy->flight != 0) {
143 foreach my $message (@{$proxy->message_list}) {
144 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
146 if ($testtype == REVERSE_ORDER_VERSIONS) {
151 } elsif ($testtype == UNRECOGNISED_VERSIONS) {
154 0x04, 0x04, #Some unrecognised version
155 0x04, 0x03; #Another unrecognised version
156 } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
161 } elsif ($testtype == WITH_TLS1_4) {
167 if ($testtype == REVERSE_ORDER_VERSIONS
168 || $testtype == UNRECOGNISED_VERSIONS
169 || $testtype == TLS1_1_AND_1_0_ONLY
170 || $testtype == WITH_TLS1_4) {
171 $message->set_extension(
172 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
173 } elsif ($testtype == EMPTY_EXTENSION) {
174 $message->set_extension(
175 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
176 } elsif ($testtype == NO_EXTENSION) {
177 $message->delete_extension(
178 TLSProxy::Message::EXT_SUPPORTED_VERSIONS);
181 $message->client_version(TLSProxy::Record::VERS_SSL_3_0);