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_0
91 && TLSProxy::Proxy->is_tls13(),
92 "Reverse order versions");
94 #Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but
95 #TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed
97 $testtype = TLS1_1_AND_1_0_ONLY;
99 $record = pop @{$proxy->record_list};
100 ok(TLSProxy::Message->success()
101 && $record->version() == TLSProxy::Record::VERS_TLS_1_1,
102 "TLS1.1 and TLS1.0 in supported versions extension only");
104 #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
106 $testtype = WITH_TLS1_4;
108 $record = pop @{$proxy->record_list};
109 ok(TLSProxy::Message->success()
110 && $record->version() == TLSProxy::Record::VERS_TLS_1_0
111 && TLSProxy::Proxy->is_tls13(),
112 "TLS1.4 in supported versions extension");
114 sub modify_supported_versions_filter
118 # We're only interested in the initial ClientHello
119 if ($proxy->flight != 0) {
123 foreach my $message (@{$proxy->message_list}) {
124 if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
126 if ($testtype == REVERSE_ORDER_VERSIONS) {
131 } elsif ($testtype == UNRECOGNISED_VERSIONS) {
134 0x04, 0x04, #Some unrecognised version
135 0x04, 0x03; #Another unrecognised version
136 } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
141 } elsif ($testtype == WITH_TLS1_4) {
147 if ($testtype == REVERSE_ORDER_VERSIONS
148 || $testtype == UNRECOGNISED_VERSIONS
149 || $testtype == TLS1_1_AND_1_0_ONLY
150 || $testtype == WITH_TLS1_4) {
151 $message->set_extension(
152 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext);
153 } elsif ($testtype == EMPTY_EXTENSION) {
154 $message->set_extension(
155 TLSProxy::Message::EXT_SUPPORTED_VERSIONS, "");
157 $message->delete_extension(
158 TLSProxy::Message::EXT_SUPPORTED_VERSIONS);