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
;
14 my $test_name = "test_sslextension";
17 plan skip_all
=> "TLSProxy isn't usable on $^O"
18 if $^O
=~ /^(VMS|MSWin32)$/;
20 plan skip_all
=> "$test_name needs the dynamic engine feature enabled"
21 if disabled
("engine") || disabled
("dynamic-engine");
23 plan skip_all
=> "$test_name needs the sock feature enabled"
26 plan skip_all
=> "$test_name needs TLS enabled"
27 if alldisabled
(available_protocols
("tls"));
30 UNSOLICITED_SERVER_NAME
=> 0,
31 UNSOLICITED_SERVER_NAME_TLS13
=> 1,
37 $ENV{OPENSSL_ia32cap
} = '~0x200000200000000';
38 my $proxy = TLSProxy
::Proxy
->new(
40 cmdstr
(app
(["openssl"]), display
=> 1),
41 srctop_file
("apps", "server.pem"),
42 (!$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
})
45 # Test 1: Sending a zero length extension block should pass
46 $proxy->start() or plan skip_all
=> "Unable to start up Proxy for tests";
48 ok
(TLSProxy
::Message
->success, "Zero extension length test");
54 if ($proxy->flight == 1) {
55 # Change the ServerRandom so that the downgrade sentinel doesn't cause
56 # the connection to fail
57 my $message = ${$proxy->message_list}[1];
58 $message->random("\0"x32
);
63 # We're only interested in the initial ClientHello
64 if ($proxy->flight != 0) {
68 foreach my $message (@
{$proxy->message_list}) {
69 if ($message->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
) {
70 # Remove all extensions and set the extension len to zero
71 $message->extension_data({});
72 $message->extensions_len(0);
73 # Extensions have been removed so make sure we don't try to use them
74 $message->process_extensions();
81 # Test 2-3: Sending a duplicate extension should fail.
82 sub inject_duplicate_extension
84 my ($proxy, $message_type) = @_;
86 foreach my $message (@
{$proxy->message_list}) {
87 if ($message->mt == $message_type) {
88 my %extensions = %{$message->extension_data};
89 # Add a duplicate (unknown) extension.
90 $message->set_extension(TLSProxy
::Message
::EXT_DUPLICATE_EXTENSION
, "");
91 $message->set_extension(TLSProxy
::Message
::EXT_DUPLICATE_EXTENSION
, "");
97 sub inject_duplicate_extension_clienthello
101 # We're only interested in the initial ClientHello
102 if ($proxy->flight != 0) {
106 inject_duplicate_extension
($proxy, TLSProxy
::Message
::MT_CLIENT_HELLO
);
109 sub inject_duplicate_extension_serverhello
113 # We're only interested in the initial ServerHello
114 if ($proxy->flight != 1) {
118 inject_duplicate_extension
($proxy, TLSProxy
::Message
::MT_SERVER_HELLO
);
122 $proxy->filter(\
&inject_duplicate_extension_clienthello
);
124 ok
(TLSProxy
::Message
->fail(), "Duplicate ClientHello extension");
127 $proxy->filter(\
&inject_duplicate_extension_serverhello
);
129 ok
(TLSProxy
::Message
->fail(), "Duplicate ServerHello extension");
131 sub inject_unsolicited_extension
136 # We're only interested in the initial ServerHello/EncryptedExtensions
137 if ($proxy->flight != 1) {
141 if ($testtype == UNSOLICITED_SERVER_NAME_TLS13
) {
142 $message = ${$proxy->message_list}[2];
143 die "Expecting EE message ".($message->mt).", ".${$proxy->message_list}[1]->mt.", ".${$proxy->message_list}[3]->mt if $message->mt != TLSProxy
::Message
::MT_ENCRYPTED_EXTENSIONS
;
145 $message = ${$proxy->message_list}[1];
149 0x00, 0x00; #Extension length
152 if ($testtype == UNSOLICITED_SERVER_NAME
153 || $testtype == UNSOLICITED_SERVER_NAME_TLS13
) {
154 $type = TLSProxy
::Message
::EXT_SERVER_NAME
;
155 } elsif ($testtype == UNSOLICITED_SCT
) {
156 $type = TLSProxy
::Message
::EXT_SCT
;
158 $message->set_extension($type, $ext);
163 skip
"TLS <= 1.2 disabled", 1 if alldisabled
(("tls1", "tls1_1", "tls1_2"));
164 #Test 4: Inject an unsolicited extension (<= TLSv1.2)
166 $proxy->filter(\
&inject_unsolicited_extension
);
167 $testtype = UNSOLICITED_SERVER_NAME
;
168 $proxy->clientflags("-no_tls1_3 -noservername");
170 ok
(TLSProxy
::Message
->fail(), "Unsolicited server name extension");
174 skip
"TLS <= 1.2 or CT disabled", 1
175 if alldisabled
(("tls1", "tls1_1", "tls1_2")) || disabled
("ct");
176 #Test 5: Same as above for the SCT extension which has special handling
178 $testtype = UNSOLICITED_SCT
;
179 $proxy->clientflags("-no_tls1_3");
181 ok
(TLSProxy
::Message
->fail(), "Unsolicited sct extension");
185 skip
"TLS 1.3 disabled", 1 if disabled
("tls1_3");
186 #Test 6: Inject an unsolicited extension (TLSv1.3)
188 $proxy->filter(\
&inject_unsolicited_extension
);
189 $testtype = UNSOLICITED_SERVER_NAME_TLS13
;
190 $proxy->clientflags("-noservername");
192 ok
(TLSProxy
::Message
->fail(), "Unsolicited server name extension (TLSv1.3)");