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
12 use OpenSSL
::Test qw
/:DEFAULT cmdstr srctop_file bldtop_dir/;
13 use OpenSSL
::Test
::Utils
;
16 my $test_name = "test_sslextension";
19 plan skip_all
=> "TLSProxy isn't usable on $^O"
22 plan skip_all
=> "$test_name needs the dynamic engine feature enabled"
23 if disabled
("engine") || disabled
("dynamic-engine");
25 plan skip_all
=> "$test_name needs the sock feature enabled"
28 plan skip_all
=> "$test_name needs TLS enabled"
29 if alldisabled
(available_protocols
("tls"));
31 my $no_below_tls13 = alldisabled
(("tls1", "tls1_1", "tls1_2"))
32 || (!disabled
("tls1_3") && disabled
("tls1_2"));
35 UNSOLICITED_SERVER_NAME
=> 0,
36 UNSOLICITED_SERVER_NAME_TLS13
=> 1,
38 NONCOMPLIANT_SUPPORTED_GROUPS
=> 3
42 my $fatal_alert = 0; # set by filter on fatal alert
44 my $proxy = TLSProxy
::Proxy
->new(
45 \
&inject_duplicate_extension_clienthello
,
46 cmdstr
(app
(["openssl"]), display
=> 1),
47 srctop_file
("apps", "server.pem"),
48 (!$ENV{HARNESS_ACTIVE
} || $ENV{HARNESS_VERBOSE
})
56 if ($proxy->flight == 1) {
57 # Change the ServerRandom so that the downgrade sentinel doesn't cause
58 # the connection to fail
59 my $message = ${$proxy->message_list}[1];
60 $message->random("\0"x32
);
65 # We're only interested in the initial ClientHello
66 if ($proxy->flight != 0) {
70 foreach my $message (@
{$proxy->message_list}) {
71 if ($message->mt == TLSProxy
::Message
::MT_CLIENT_HELLO
) {
72 # Remove all extensions and set the extension len to zero
73 $message->extension_data({});
74 $message->extensions_len(0);
75 # Extensions have been removed so make sure we don't try to use them
76 $message->process_extensions();
83 sub inject_duplicate_extension
85 my ($proxy, $message_type) = @_;
87 foreach my $message (@
{$proxy->message_list}) {
88 if ($message->mt == $message_type) {
89 my %extensions = %{$message->extension_data};
90 # Add a duplicate extension. We use cryptopro_bug since we never
91 # normally write that one, and it is allowed as unsolicited in the
93 $message->set_extension(TLSProxy
::Message
::EXT_CRYPTOPRO_BUG_EXTENSION
, "");
94 $message->dupext(TLSProxy
::Message
::EXT_CRYPTOPRO_BUG_EXTENSION
);
100 sub inject_duplicate_extension_clienthello
104 # We're only interested in the initial ClientHello
105 if ($proxy->flight == 0) {
106 inject_duplicate_extension
($proxy, TLSProxy
::Message
::MT_CLIENT_HELLO
);
110 my $last_record = @
{$proxy->{record_list
}}[-1];
111 $fatal_alert = 1 if $last_record->is_fatal_alert(1);
114 sub inject_duplicate_extension_serverhello
118 # We're only interested in the initial ServerHello
119 if ($proxy->flight == 0) {
121 } elsif ($proxy->flight == 1) {
122 inject_duplicate_extension
($proxy, TLSProxy
::Message
::MT_SERVER_HELLO
);
126 my $last_record = @
{$proxy->{record_list
}}[-1];
127 $fatal_alert = 1 if $last_record->is_fatal_alert(0);
130 sub inject_unsolicited_extension
134 state $sent_unsolisited_extension;
136 if ($proxy->flight == 0) {
137 $sent_unsolisited_extension = 0;
141 # We're only interested in the initial ServerHello/EncryptedExtensions
142 if ($proxy->flight != 1) {
143 if ($sent_unsolisited_extension) {
144 my $last_record = @
{$proxy->record_list}[-1];
145 $fatal_alert = 1 if $last_record->is_fatal_alert(0);
150 if ($testtype == UNSOLICITED_SERVER_NAME_TLS13
) {
151 return if (!defined($message = ${$proxy->message_list}[2]));
152 die "Expecting EE message ".($message->mt).","
153 .${$proxy->message_list}[1]->mt.", "
154 .${$proxy->message_list}[3]->mt
155 if $message->mt != TLSProxy
::Message
::MT_ENCRYPTED_EXTENSIONS
;
157 $message = ${$proxy->message_list}[1];
161 0x00, 0x00; #Extension length
164 if ($testtype == UNSOLICITED_SERVER_NAME
165 || $testtype == UNSOLICITED_SERVER_NAME_TLS13
) {
166 $type = TLSProxy
::Message
::EXT_SERVER_NAME
;
167 } elsif ($testtype == UNSOLICITED_SCT
) {
168 $type = TLSProxy
::Message
::EXT_SCT
;
169 } elsif ($testtype == NONCOMPLIANT_SUPPORTED_GROUPS
) {
170 $type = TLSProxy
::Message
::EXT_SUPPORTED_GROUPS
;
172 $message->set_extension($type, $ext);
174 $sent_unsolisited_extension = 1;
177 sub inject_cryptopro_extension
181 # We're only interested in the initial ClientHello
182 if ($proxy->flight != 0) {
186 my $message = ${$proxy->message_list}[0];
187 $message->set_extension(TLSProxy
::Message
::EXT_CRYPTOPRO_BUG_EXTENSION
, "");
191 # Test 1-2: Sending a duplicate extension should fail.
192 $proxy->start() or plan skip_all
=> "Unable to start up Proxy for tests";
194 ok
($fatal_alert, "Duplicate ClientHello extension");
197 skip
"TLS <= 1.2 disabled", 4 if $no_below_tls13;
201 $proxy->filter(\
&inject_duplicate_extension_serverhello
);
202 $proxy->clientflags("-no_tls1_3");
204 ok
($fatal_alert, "Duplicate ServerHello extension");
206 #Test 3: Sending a zero length extension block should pass
208 $proxy->filter(\
&extension_filter
);
209 $proxy->cipherc("DEFAULT:\@SECLEVEL=0");
210 $proxy->ciphers("AES128-SHA:\@SECLEVEL=0");
211 $proxy->clientflags("-no_tls1_3");
213 ok
(TLSProxy
::Message
->success, "Zero extension length test");
215 #Test 4: Inject an unsolicited extension (<= TLSv1.2)
218 $proxy->filter(\
&inject_unsolicited_extension
);
219 $testtype = UNSOLICITED_SERVER_NAME
;
220 $proxy->clientflags("-no_tls1_3 -noservername");
222 ok
($fatal_alert, "Unsolicited server name extension");
224 #Test 5: Send the cryptopro extension in a ClientHello. Normally this is an
225 # unsolicited extension only ever seen in the ServerHello. We should
226 # ignore it in a ClientHello
228 $proxy->filter(\
&inject_cryptopro_extension
);
229 $proxy->clientflags("-no_tls1_3");
231 ok
(TLSProxy
::Message
->success(), "Cryptopro extension in ClientHello");
235 skip
"TLS <= 1.2 disabled or EC disabled", 1
236 if $no_below_tls13 || disabled
("ec");
237 #Test 6: Inject a noncompliant supported_groups extension (<= TLSv1.2)
239 $proxy->filter(\
&inject_unsolicited_extension
);
240 $testtype = NONCOMPLIANT_SUPPORTED_GROUPS
;
241 $proxy->clientflags("-no_tls1_3");
243 ok
(TLSProxy
::Message
->success(), "Noncompliant supported_groups extension");
247 skip
"TLS <= 1.2 or CT disabled", 1
248 if $no_below_tls13 || disabled
("ct");
249 #Test 7: Same as above for the SCT extension which has special handling
252 $proxy->filter(\
&inject_unsolicited_extension
);
253 $testtype = UNSOLICITED_SCT
;
254 $proxy->clientflags("-no_tls1_3");
256 ok
($fatal_alert, "Unsolicited sct extension");
260 skip
"TLS 1.3 disabled", 1
261 if disabled
("tls1_3") || (disabled
("ec") && disabled
("dh"));
262 #Test 8: Inject an unsolicited extension (TLSv1.3)
265 $proxy->filter(\
&inject_unsolicited_extension
);
266 $testtype = UNSOLICITED_SERVER_NAME_TLS13
;
267 $proxy->clientflags("-noservername");
269 ok
($fatal_alert, "Unsolicited server name extension (TLSv1.3)");