2 # Copyright 2015-2021 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"
41 || (disabled("ec") && disabled("dh"))
43 || disabled("tls1_1");
45 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
47 my $proxy = TLSProxy::Proxy->new(
49 cmdstr(app(["openssl"]), display => 1),
50 srctop_file("apps", "server.pem"),
51 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
54 #We're just testing various negative and unusual scenarios here. ssltest with
55 #02-protocol-version.cnf should check all the various combinations of normal
58 #Test 1: An empty supported_versions extension should not succeed
59 $testtype = EMPTY_EXTENSION;
60 $proxy->filter(\&modify_supported_versions_filter);
61 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
63 ok(TLSProxy::Message->fail(), "Empty supported versions");
65 #Test 2: supported_versions extension with no recognised versions should not
68 $testtype = UNRECOGNISED_VERSIONS;
70 ok(TLSProxy::Message->fail(), "No recognised versions");
72 #Test 3: No supported versions extensions should succeed and select TLSv1.2
74 $testtype = NO_EXTENSION;
76 my $record = pop @{$proxy->record_list};
77 ok(TLSProxy::Message->success()
78 && $record->version() == TLSProxy::Record::VERS_TLS_1_2,
79 "No supported versions extension");
81 #Test 4: No supported versions extensions should fail if only TLS1.3 available
83 $proxy->serverflags("-tls1_3");
85 ok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)");
87 #Test 5: supported versions extension with best version last should succeed
90 $testtype = REVERSE_ORDER_VERSIONS;
92 $record = pop @{$proxy->record_list};
93 ok(TLSProxy::Message->success()
94 && $record->version() == TLSProxy::Record::VERS_TLS_1_2
95 && TLSProxy::Proxy->is_tls13(),
96 "Reverse order versions");
98 #Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but
99 #TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed
101 $proxy->clientflags("-cipher DEFAULT:\@SECLEVEL=0");
102 $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
103 $testtype = TLS1_1_AND_1_0_ONLY;
105 $record = pop @{$proxy->record_list};
106 ok(TLSProxy::Message->success()
107 && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
108 "TLS1.1 and TLS1.0 in supported versions extension only");
110 #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
112 $testtype = WITH_TLS1_4;
114 $record = pop @{$proxy->record_list};
115 ok(TLSProxy::Message->success()
116 && $record->version() == TLSProxy::Record::VERS_TLS_1_2
117 && TLSProxy::Proxy->is_tls13(),
118 "TLS1.4 in supported versions extension");
120 #Test 8: Set the legacy version to SSLv3 with supported versions. Should fail
122 $testtype = BAD_LEGACY_VERSION;
124 ok(TLSProxy::Message->fail(), "Legacy version is SSLv3 with supported versions");
126 sub modify_supported_versions_filter
130 if ($proxy->flight == 1) {
131 # Change the ServerRandom so that the downgrade sentinel doesn't cause
132 # the connection to fail
133 my $message = ${$proxy->message_list}[1];
134 return if (!defined $message);
136 $message->random("\0"x32);
141 # We're only interested in the initial ClientHello
142 if ($proxy->flight != 0) {
146 foreach my $message (@{$proxy->message_list}) {
147 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
149 if ($testtype == REVERSE_ORDER_VERSIONS) {
154 } elsif ($testtype == UNRECOGNISED_VERSIONS) {
157 0x04, 0x04, #Some unrecognised version
158 0x04, 0x03; #Another unrecognised version
159 } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
164 } elsif ($testtype == WITH_TLS1_4) {
170 if ($testtype == REVERSE_ORDER_VERSIONS
171 || $testtype == UNRECOGNISED_VERSIONS
172 || $testtype == TLS1_1_AND_1_0_ONLY
173 || $testtype == WITH_TLS1_4) {
174 $message->set_extension(
175 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
176 } elsif ($testtype == EMPTY_EXTENSION) {
177 $message->set_extension(
178 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
179 } elsif ($testtype == NO_EXTENSION) {
180 $message->delete_extension(
181 TLSProxy::Message::EXT_SUPPORTED_VERSIONS);
184 $message->client_version(TLSProxy::Record::VERS_SSL_3_0);