2 # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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,
26 my $test_name = "test_sslversions";
29 plan skip_all => "TLSProxy isn't usable on $^O"
30 if $^O =~ /^(VMS|MSWin32)$/;
32 plan skip_all => "$test_name needs the dynamic engine feature enabled"
33 if disabled("engine") || disabled("dynamic-engine");
35 plan skip_all => "$test_name needs the sock feature enabled"
38 plan skip_all => "$test_name needs TLS1.3, TLS1.2 and TLS1.1 enabled"
39 if disabled("tls1_3") || disabled("tls1_2") || disabled("tls1_1");
41 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
43 my $proxy = TLSProxy::Proxy->new(
45 cmdstr(app(["openssl"]), display => 1),
46 srctop_file("apps", "server.pem"),
47 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
50 #We're just testing various negative and unusual scenarios here. ssltest with
51 #02-protocol-version.conf should check all the various combinations of normal
54 #Test 1: An empty supported_versions extension should not succeed
55 $testtype = EMPTY_EXTENSION;
56 $proxy->filter(\&modify_supported_versions_filter);
57 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
59 ok(TLSProxy::Message->fail(), "Empty supported versions");
61 #Test 2: supported_versions extension with no recognised versions should not
64 $testtype = UNRECOGNISED_VERSIONS;
66 ok(TLSProxy::Message->fail(), "No recognised versions");
68 #Test 3: No supported versions extensions should succeed and select TLSv1.2
70 $testtype = NO_EXTENSION;
72 my $record = pop @{$proxy->record_list};
73 ok(TLSProxy::Message->success()
74 && $record->version() == TLSProxy::Record::VERS_TLS_1_2,
75 "No supported versions extension");
77 #Test 4: No supported versions extensions should fail if only TLS1.3 available
79 $proxy->serverflags("-tls1_3");
81 ok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)");
83 #Test 5: supported versions extension with best version last should succeed
86 $testtype = REVERSE_ORDER_VERSIONS;
88 $record = pop @{$proxy->record_list};
89 ok(TLSProxy::Message->success()
90 && $record->version() == TLSProxy::Record::VERS_TLS_1_3,
91 "Reverse order versions");
93 #Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but
94 #TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed
96 $testtype = TLS1_1_AND_1_0_ONLY;
98 $record = pop @{$proxy->record_list};
99 ok(TLSProxy::Message->success()
100 && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
101 "TLS1.1 and TLS1.0 in supported versions extension only");
103 #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
105 $testtype = WITH_TLS1_4;
107 $record = pop @{$proxy->record_list};
108 ok(TLSProxy::Message->success()
109 && $record->version() == TLSProxy::Record::VERS_TLS_1_3,
110 "TLS1.4 in supported versions extension");
112 sub modify_supported_versions_filter
116 # We're only interested in the initial ClientHello
117 if ($proxy->flight != 0) {
121 foreach my $message (@{$proxy->message_list}) {
122 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
124 if ($testtype == REVERSE_ORDER_VERSIONS) {
129 } elsif ($testtype == UNRECOGNISED_VERSIONS) {
132 0x04, 0x04, #Some unrecognised version
133 0x04, 0x03; #Another unrecognised version
134 } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
139 } elsif ($testtype == WITH_TLS1_4) {
145 if ($testtype == REVERSE_ORDER_VERSIONS
146 || $testtype == UNRECOGNISED_VERSIONS
147 || $testtype == TLS1_1_AND_1_0_ONLY
148 || $testtype == WITH_TLS1_4) {
149 $message->set_extension(
150 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
151 } elsif ($testtype == EMPTY_EXTENSION) {
152 $message->set_extension(
153 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
155 $message->delete_extension(
156 TLSProxy::Message::EXT_SUPPORTED_VERSIONS);