1 1.1 christos #! /usr/bin/env perl 2 1.1 christos # Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos # 4 1.1 christos # Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos # this file except in compliance with the License. You can obtain a copy 6 1.1 christos # in the file LICENSE in the source distribution or at 7 1.1 christos # https://www.openssl.org/source/license.html 8 1.1 christos 9 1.1 christos use strict; 10 1.1 christos use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/; 11 1.1 christos use OpenSSL::Test::Utils; 12 1.1 christos use TLSProxy::Proxy; 13 1.1 christos use File::Temp qw(tempfile); 14 1.1 christos 15 1.1 christos my $test_name = "test_tlsextms"; 16 1.1 christos setup($test_name); 17 1.1 christos 18 1.1 christos plan skip_all => "TLSProxy isn't usable on $^O" 19 1.1 christos if $^O =~ /^(VMS)$/; 20 1.1 christos 21 1.1 christos plan skip_all => "$test_name needs the dynamic engine feature enabled" 22 1.1 christos if disabled("engine") || disabled("dynamic-engine"); 23 1.1 christos 24 1.1 christos plan skip_all => "$test_name needs the sock feature enabled" 25 1.1 christos if disabled("sock"); 26 1.1 christos 27 1.1 christos plan skip_all => "$test_name needs TLSv1.0, TLSv1.1 or TLSv1.2 enabled" 28 1.1 christos if disabled("tls1") && disabled("tls1_1") && disabled("tls1_2"); 29 1.1 christos 30 1.1 christos sub checkmessages($$$$$); 31 1.1 christos sub setrmextms($$); 32 1.1 christos sub clearall(); 33 1.1 christos 34 1.1 christos my $crmextms = 0; 35 1.1 christos my $srmextms = 0; 36 1.1 christos my $cextms = 0; 37 1.1 christos my $sextms = 0; 38 1.1 christos my $fullhand = 0; 39 1.1 christos 40 1.1 christos my $proxy = TLSProxy::Proxy->new( 41 1.1 christos \&extms_filter, 42 1.1 christos cmdstr(app(["openssl"]), display => 1), 43 1.1 christos srctop_file("apps", "server.pem"), 44 1.1 christos (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 45 1.1 christos ); 46 1.1 christos 47 1.1 christos #Note that EXTMS is only relevant for <TLS1.3 48 1.1 christos 49 1.1 christos #Test 1: By default server and client should send extended master secret 50 1.1 christos # extension. 51 1.1 christos #Expected result: ClientHello extension seen; ServerHello extension seen 52 1.1 christos # Full handshake 53 1.1 christos 54 1.1 christos setrmextms(0, 0); 55 1.1 christos $proxy->clientflags("-no_tls1_3"); 56 1.1 christos $proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 57 1.1 christos plan tests => 10; 58 1.1 christos checkmessages(1, "Default extended master secret test", 1, 1, 1); 59 1.1 christos 60 1.1 christos #Test 2: If client omits extended master secret extension, server should too. 61 1.1 christos #Expected result: ClientHello extension not seen; ServerHello extension not seen 62 1.1 christos # Full handshake 63 1.1 christos 64 1.1 christos clearall(); 65 1.1 christos setrmextms(1, 0); 66 1.1 christos $proxy->clientflags("-no_tls1_3"); 67 1.1 christos $proxy->start(); 68 1.1 christos checkmessages(2, "No client extension extended master secret test", 0, 0, 1); 69 1.1 christos 70 1.1 christos # Test 3: same as 1 but with session tickets disabled. 71 1.1 christos # Expected result: same as test 1. 72 1.1 christos 73 1.1 christos clearall(); 74 1.1 christos $proxy->clientflags("-no_ticket -no_tls1_3"); 75 1.1 christos setrmextms(0, 0); 76 1.1 christos $proxy->start(); 77 1.1 christos checkmessages(3, "No ticket extended master secret test", 1, 1, 1); 78 1.1 christos 79 1.1 christos # Test 4: same as 2 but with session tickets disabled. 80 1.1 christos # Expected result: same as test 2. 81 1.1 christos 82 1.1 christos clearall(); 83 1.1 christos $proxy->clientflags("-no_ticket -no_tls1_3"); 84 1.1 christos setrmextms(1, 0); 85 1.1 christos $proxy->start(); 86 1.1 christos checkmessages(4, "No ticket, no client extension extended master secret test", 0, 0, 1); 87 1.1 christos 88 1.1 christos #Test 5: Session resumption extended master secret test 89 1.1 christos # 90 1.1 christos #Expected result: ClientHello extension seen; ServerHello extension seen 91 1.1 christos # Abbreviated handshake 92 1.1 christos 93 1.1 christos clearall(); 94 1.1 christos setrmextms(0, 0); 95 1.1 christos (undef, my $session) = tempfile(); 96 1.1 christos $proxy->serverconnects(2); 97 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_out ".$session); 98 1.1 christos $proxy->start(); 99 1.1 christos $proxy->clearClient(); 100 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_in ".$session); 101 1.1 christos $proxy->clientstart(); 102 1.1 christos checkmessages(5, "Session resumption extended master secret test", 1, 1, 0); 103 1.1 christos unlink $session; 104 1.1 christos 105 1.1 christos #Test 6: Session resumption extended master secret test original session 106 1.1 christos # omits extension. Server must not resume session. 107 1.1 christos #Expected result: ClientHello extension seen; ServerHello extension seen 108 1.1 christos # Full handshake 109 1.1 christos 110 1.1 christos clearall(); 111 1.1 christos setrmextms(1, 0); 112 1.1 christos (undef, $session) = tempfile(); 113 1.1 christos $proxy->serverconnects(2); 114 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_out ".$session); 115 1.1 christos $proxy->start(); 116 1.1 christos $proxy->clearClient(); 117 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_in ".$session); 118 1.1 christos setrmextms(0, 0); 119 1.1 christos $proxy->clientstart(); 120 1.1 christos checkmessages(6, "Session resumption extended master secret test", 1, 1, 1); 121 1.1 christos unlink $session; 122 1.1 christos 123 1.1 christos #Test 7: Session resumption extended master secret test resumed session 124 1.1 christos # omits client extension. Server must abort connection. 125 1.1 christos #Expected result: aborted connection. 126 1.1 christos 127 1.1 christos clearall(); 128 1.1 christos setrmextms(0, 0); 129 1.1 christos (undef, $session) = tempfile(); 130 1.1 christos $proxy->serverconnects(2); 131 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_out ".$session); 132 1.1 christos $proxy->start(); 133 1.1 christos $proxy->clearClient(); 134 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_in ".$session); 135 1.1 christos setrmextms(1, 0); 136 1.1 christos $proxy->clientstart(); 137 1.1 christos ok(TLSProxy::Message->fail(), "Client inconsistent session resumption"); 138 1.1 christos unlink $session; 139 1.1 christos 140 1.1 christos #Test 8: Session resumption extended master secret test resumed session 141 1.1 christos # omits server extension. Client must abort connection. 142 1.1 christos #Expected result: aborted connection. 143 1.1 christos 144 1.1 christos clearall(); 145 1.1 christos setrmextms(0, 0); 146 1.1 christos (undef, $session) = tempfile(); 147 1.1 christos $proxy->serverconnects(2); 148 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_out ".$session); 149 1.1 christos $proxy->start(); 150 1.1 christos $proxy->clearClient(); 151 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_in ".$session); 152 1.1 christos setrmextms(0, 1); 153 1.1 christos $proxy->clientstart(); 154 1.1 christos ok(TLSProxy::Message->fail(), "Server inconsistent session resumption 1"); 155 1.1 christos unlink $session; 156 1.1 christos 157 1.1 christos #Test 9: Session resumption extended master secret test initial session 158 1.1 christos # omits server extension. Client must abort connection. 159 1.1 christos #Expected result: aborted connection. 160 1.1 christos 161 1.1 christos clearall(); 162 1.1 christos setrmextms(0, 1); 163 1.1 christos (undef, $session) = tempfile(); 164 1.1 christos $proxy->serverconnects(2); 165 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_out ".$session); 166 1.1 christos $proxy->start(); 167 1.1 christos $proxy->clearClient(); 168 1.1 christos $proxy->clientflags("-no_tls1_3 -sess_in ".$session); 169 1.1 christos setrmextms(0, 0); 170 1.1 christos $proxy->clientstart(); 171 1.1 christos ok(TLSProxy::Message->fail(), "Server inconsistent session resumption 2"); 172 1.1 christos unlink $session; 173 1.1 christos 174 1.1 christos SKIP: { 175 1.1 christos skip "TLS 1.3 disabled", 1 176 1.1 christos if disabled("tls1_3") || (disabled("ec") && disabled("dh")); 177 1.1 christos 178 1.1 christos #Test 10: In TLS1.3 we should not negotiate extended master secret 179 1.1 christos #Expected result: ClientHello extension seen; ServerHello extension not seen 180 1.1 christos # TLS1.3 handshake (will appear as abbreviated handshake 181 1.1 christos # because of no CKE message) 182 1.1 christos clearall(); 183 1.1 christos setrmextms(0, 0); 184 1.1 christos $proxy->start(); 185 1.1 christos checkmessages(10, "TLS1.3 extended master secret test", 1, 0, 0); 186 1.1 christos } 187 1.1 christos 188 1.1 christos 189 1.1 christos sub extms_filter 190 1.1 christos { 191 1.1 christos my $proxy = shift; 192 1.1 christos 193 1.1 christos foreach my $message (@{$proxy->message_list}) { 194 1.1 christos if ($crmextms && $message->mt == TLSProxy::Message::MT_CLIENT_HELLO) { 195 1.1 christos $message->delete_extension(TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET); 196 1.1 christos $message->repack(); 197 1.1 christos } 198 1.1 christos if ($srmextms && $message->mt == TLSProxy::Message::MT_SERVER_HELLO) { 199 1.1 christos $message->delete_extension(TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET); 200 1.1 christos $message->repack(); 201 1.1 christos } 202 1.1 christos } 203 1.1 christos } 204 1.1 christos 205 1.1 christos sub checkmessages($$$$$) 206 1.1 christos { 207 1.1 christos my ($testno, $testname, $testcextms, $testsextms, $testhand) = @_; 208 1.1 christos 209 1.1 christos subtest $testname => sub { 210 1.1 christos 211 1.1 christos foreach my $message (@{$proxy->message_list}) { 212 1.1 christos if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO 213 1.1 christos || $message->mt == TLSProxy::Message::MT_SERVER_HELLO) { 214 1.1 christos #Get the extensions data 215 1.1 christos my %extensions = %{$message->extension_data}; 216 1.1 christos if (defined 217 1.1 christos $extensions{TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET}) { 218 1.1 christos if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) { 219 1.1 christos $cextms = 1; 220 1.1 christos } else { 221 1.1 christos $sextms = 1; 222 1.1 christos } 223 1.1 christos } 224 1.1 christos } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_KEY_EXCHANGE) { 225 1.1 christos #Must be doing a full handshake 226 1.1 christos $fullhand = 1; 227 1.1 christos } 228 1.1 christos } 229 1.1 christos 230 1.1 christos plan tests => 4; 231 1.1 christos 232 1.1 christos ok(TLSProxy::Message->success, "Handshake"); 233 1.1 christos 234 1.1 christos ok($testcextms == $cextms, 235 1.1 christos "ClientHello extension extended master secret check"); 236 1.1 christos ok($testsextms == $sextms, 237 1.1 christos "ServerHello extension extended master secret check"); 238 1.1 christos ok($testhand == $fullhand, 239 1.1 christos "Extended master secret full handshake check"); 240 1.1 christos 241 1.1 christos } 242 1.1 christos } 243 1.1 christos 244 1.1 christos sub setrmextms($$) 245 1.1 christos { 246 1.1 christos ($crmextms, $srmextms) = @_; 247 1.1 christos } 248 1.1 christos 249 1.1 christos sub clearall() 250 1.1 christos { 251 1.1 christos $cextms = 0; 252 1.1 christos $sextms = 0; 253 1.1 christos $fullhand = 0; 254 1.1 christos $proxy->clear(); 255 1.1 christos } 256