Fix some style issues with TLSv1.3 state machine PR
[openssl.git] / test / recipes / 70-test_sslversions.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 use File::Temp qw(tempfile);
14
15 use constant {
16     REVERSE_ORDER_VERSIONS => 1,
17     UNRECOGNISED_VERSIONS => 2,
18     NO_EXTENSION => 3,
19     EMPTY_EXTENSION => 4,
20     TLS1_1_AND_1_0_ONLY => 5,
21     WITH_TLS1_4 => 6
22 };
23
24 my $testtype;
25
26 my $test_name = "test_sslversions";
27 setup($test_name);
28
29 plan skip_all => "TLSProxy isn't usable on $^O"
30     if $^O =~ /^(VMS|MSWin32)$/;
31
32 plan skip_all => "$test_name needs the dynamic engine feature enabled"
33     if disabled("engine") || disabled("dynamic-engine");
34
35 plan skip_all => "$test_name needs the sock feature enabled"
36     if disabled("sock");
37
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");
40
41 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
42
43 my $proxy = TLSProxy::Proxy->new(
44     undef,
45     cmdstr(app(["openssl"]), display => 1),
46     srctop_file("apps", "server.pem"),
47     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
48 );
49
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
52 #version neg
53
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";
58 plan tests => 7;
59 ok(TLSProxy::Message->fail(), "Empty supported versions");
60
61 #Test 2: supported_versions extension with no recognised versions should not
62 #succeed
63 $proxy->clear();
64 $testtype = UNRECOGNISED_VERSIONS;
65 $proxy->start();
66 ok(TLSProxy::Message->fail(), "No recognised versions");
67
68 #Test 3: No supported versions extensions should succeed and select TLSv1.2
69 $proxy->clear();
70 $testtype = NO_EXTENSION;
71 $proxy->start();
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");
76
77 #Test 4: No supported versions extensions should fail if only TLS1.3 available
78 $proxy->clear();
79 $proxy->serverflags("-tls1_3");
80 $proxy->start();
81 ok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)");
82
83 #Test 5: supported versions extension with best version last should succeed
84 #and select TLSv1.3
85 $proxy->clear();
86 $testtype = REVERSE_ORDER_VERSIONS;
87 $proxy->start();
88 $record = pop @{$proxy->record_list};
89 ok(TLSProxy::Message->success()
90    && $record->version() == TLSProxy::Record::VERS_TLS_1_3,
91    "Reverse order versions");
92
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
95 $proxy->clear();
96 $testtype = TLS1_1_AND_1_0_ONLY;
97 $proxy->start();
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");
102
103 #Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3
104 $proxy->clear();
105 $testtype = WITH_TLS1_4;
106 $proxy->start();
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");
111
112 sub modify_supported_versions_filter
113 {
114     my $proxy = shift;
115
116     # We're only interested in the initial ClientHello
117     if ($proxy->flight != 0) {
118         return;
119     }
120
121     foreach my $message (@{$proxy->message_list}) {
122         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
123             my $ext;
124             if ($testtype == REVERSE_ORDER_VERSIONS) {
125                 $ext = pack "C5",
126                     0x04, # Length
127                     0x03, 0x03, #TLSv1.2
128                     0x03, 0x04; #TLSv1.3
129             } elsif ($testtype == UNRECOGNISED_VERSIONS) {
130                 $ext = pack "C5",
131                     0x04, # Length
132                     0x04, 0x04, #Some unrecognised version
133                     0x04, 0x03; #Another unrecognised version
134             } elsif ($testtype == TLS1_1_AND_1_0_ONLY) {
135                 $ext = pack "C5",
136                     0x04, # Length
137                     0x03, 0x02, #TLSv1.1
138                     0x03, 0x01; #TLSv1.0
139             } elsif ($testtype == WITH_TLS1_4) {
140                     $ext = pack "C5",
141                         0x04, # Length
142                         0x03, 0x05, #TLSv1.4
143                         0x03, 0x04; #TLSv1.3
144             }
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, "");
154             } else {
155                 $message->delete_extension(
156                     TLSProxy::Message::EXT_SUPPORTED_VERSIONS);
157             }
158
159             $message->repack();
160         }
161     }
162 }
163
164