1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ciuse strict; 10e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/; 11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils; 12e1051a39Sopenharmony_ciuse TLSProxy::Proxy; 13e1051a39Sopenharmony_ciuse File::Temp qw(tempfile); 14e1051a39Sopenharmony_ci 15e1051a39Sopenharmony_ciuse constant { 16e1051a39Sopenharmony_ci REVERSE_ORDER_VERSIONS => 1, 17e1051a39Sopenharmony_ci UNRECOGNISED_VERSIONS => 2, 18e1051a39Sopenharmony_ci NO_EXTENSION => 3, 19e1051a39Sopenharmony_ci EMPTY_EXTENSION => 4, 20e1051a39Sopenharmony_ci TLS1_1_AND_1_0_ONLY => 5, 21e1051a39Sopenharmony_ci WITH_TLS1_4 => 6, 22e1051a39Sopenharmony_ci BAD_LEGACY_VERSION => 7 23e1051a39Sopenharmony_ci}; 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_cimy $testtype; 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_cimy $test_name = "test_sslversions"; 28e1051a39Sopenharmony_cisetup($test_name); 29e1051a39Sopenharmony_ci 30e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O" 31e1051a39Sopenharmony_ci if $^O =~ /^(VMS)$/; 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled" 34e1051a39Sopenharmony_ci if disabled("engine") || disabled("dynamic-engine"); 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled" 37e1051a39Sopenharmony_ci if disabled("sock"); 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLS1.3, TLS1.2 and TLS1.1 enabled" 40e1051a39Sopenharmony_ci if disabled("tls1_3") 41e1051a39Sopenharmony_ci || (disabled("ec") && disabled("dh")) 42e1051a39Sopenharmony_ci || disabled("tls1_2") 43e1051a39Sopenharmony_ci || disabled("tls1_1"); 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000'; 46e1051a39Sopenharmony_ci 47e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new( 48e1051a39Sopenharmony_ci undef, 49e1051a39Sopenharmony_ci cmdstr(app(["openssl"]), display => 1), 50e1051a39Sopenharmony_ci srctop_file("apps", "server.pem"), 51e1051a39Sopenharmony_ci (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 52e1051a39Sopenharmony_ci); 53e1051a39Sopenharmony_ci 54e1051a39Sopenharmony_ci#We're just testing various negative and unusual scenarios here. ssltest with 55e1051a39Sopenharmony_ci#02-protocol-version.cnf should check all the various combinations of normal 56e1051a39Sopenharmony_ci#version neg 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_ci#Test 1: An empty supported_versions extension should not succeed 59e1051a39Sopenharmony_ci$testtype = EMPTY_EXTENSION; 60e1051a39Sopenharmony_ci$proxy->filter(\&modify_supported_versions_filter); 61e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 62e1051a39Sopenharmony_ciplan tests => 8; 63e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Empty supported versions"); 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ci#Test 2: supported_versions extension with no recognised versions should not 66e1051a39Sopenharmony_ci#succeed 67e1051a39Sopenharmony_ci$proxy->clear(); 68e1051a39Sopenharmony_ci$testtype = UNRECOGNISED_VERSIONS; 69e1051a39Sopenharmony_ci$proxy->start(); 70e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "No recognised versions"); 71e1051a39Sopenharmony_ci 72e1051a39Sopenharmony_ci#Test 3: No supported versions extensions should succeed and select TLSv1.2 73e1051a39Sopenharmony_ci$proxy->clear(); 74e1051a39Sopenharmony_ci$testtype = NO_EXTENSION; 75e1051a39Sopenharmony_ci$proxy->start(); 76e1051a39Sopenharmony_cimy $record = pop @{$proxy->record_list}; 77e1051a39Sopenharmony_ciok(TLSProxy::Message->success() 78e1051a39Sopenharmony_ci && $record->version() == TLSProxy::Record::VERS_TLS_1_2, 79e1051a39Sopenharmony_ci "No supported versions extension"); 80e1051a39Sopenharmony_ci 81e1051a39Sopenharmony_ci#Test 4: No supported versions extensions should fail if only TLS1.3 available 82e1051a39Sopenharmony_ci$proxy->clear(); 83e1051a39Sopenharmony_ci$proxy->serverflags("-tls1_3"); 84e1051a39Sopenharmony_ci$proxy->start(); 85e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "No supported versions extension (only TLS1.3)"); 86e1051a39Sopenharmony_ci 87e1051a39Sopenharmony_ci#Test 5: supported versions extension with best version last should succeed 88e1051a39Sopenharmony_ci#and select TLSv1.3 89e1051a39Sopenharmony_ci$proxy->clear(); 90e1051a39Sopenharmony_ci$testtype = REVERSE_ORDER_VERSIONS; 91e1051a39Sopenharmony_ci$proxy->start(); 92e1051a39Sopenharmony_ci$record = pop @{$proxy->record_list}; 93e1051a39Sopenharmony_ciok(TLSProxy::Message->success() 94e1051a39Sopenharmony_ci && $record->version() == TLSProxy::Record::VERS_TLS_1_2 95e1051a39Sopenharmony_ci && TLSProxy::Proxy->is_tls13(), 96e1051a39Sopenharmony_ci "Reverse order versions"); 97e1051a39Sopenharmony_ci 98e1051a39Sopenharmony_ci#Test 6: no TLSv1.3 or TLSv1.2 version in supported versions extension, but 99e1051a39Sopenharmony_ci#TLSv1.1 and TLSv1.0 are present. Should just use TLSv1.1 and succeed 100e1051a39Sopenharmony_ci$proxy->clear(); 101e1051a39Sopenharmony_ci$proxy->clientflags("-cipher DEFAULT:\@SECLEVEL=0"); 102e1051a39Sopenharmony_ci$proxy->ciphers("AES128-SHA:\@SECLEVEL=0"); 103e1051a39Sopenharmony_ci$testtype = TLS1_1_AND_1_0_ONLY; 104e1051a39Sopenharmony_ci$proxy->start(); 105e1051a39Sopenharmony_ci$record = pop @{$proxy->record_list}; 106e1051a39Sopenharmony_ciok(TLSProxy::Message->success() 107e1051a39Sopenharmony_ci && $record->version() == TLSProxy::Record::VERS_TLS_1_1, 108e1051a39Sopenharmony_ci "TLS1.1 and TLS1.0 in supported versions extension only"); 109e1051a39Sopenharmony_ci 110e1051a39Sopenharmony_ci#Test 7: TLS1.4 and TLS1.3 in supported versions. Should succeed and use TLS1.3 111e1051a39Sopenharmony_ci$proxy->clear(); 112e1051a39Sopenharmony_ci$testtype = WITH_TLS1_4; 113e1051a39Sopenharmony_ci$proxy->start(); 114e1051a39Sopenharmony_ci$record = pop @{$proxy->record_list}; 115e1051a39Sopenharmony_ciok(TLSProxy::Message->success() 116e1051a39Sopenharmony_ci && $record->version() == TLSProxy::Record::VERS_TLS_1_2 117e1051a39Sopenharmony_ci && TLSProxy::Proxy->is_tls13(), 118e1051a39Sopenharmony_ci "TLS1.4 in supported versions extension"); 119e1051a39Sopenharmony_ci 120e1051a39Sopenharmony_ci#Test 8: Set the legacy version to SSLv3 with supported versions. Should fail 121e1051a39Sopenharmony_ci$proxy->clear(); 122e1051a39Sopenharmony_ci$testtype = BAD_LEGACY_VERSION; 123e1051a39Sopenharmony_ci$proxy->start(); 124e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Legacy version is SSLv3 with supported versions"); 125e1051a39Sopenharmony_ci 126e1051a39Sopenharmony_cisub modify_supported_versions_filter 127e1051a39Sopenharmony_ci{ 128e1051a39Sopenharmony_ci my $proxy = shift; 129e1051a39Sopenharmony_ci 130e1051a39Sopenharmony_ci if ($proxy->flight == 1) { 131e1051a39Sopenharmony_ci # Change the ServerRandom so that the downgrade sentinel doesn't cause 132e1051a39Sopenharmony_ci # the connection to fail 133e1051a39Sopenharmony_ci my $message = ${$proxy->message_list}[1]; 134e1051a39Sopenharmony_ci return if (!defined $message); 135e1051a39Sopenharmony_ci 136e1051a39Sopenharmony_ci $message->random("\0"x32); 137e1051a39Sopenharmony_ci $message->repack(); 138e1051a39Sopenharmony_ci return; 139e1051a39Sopenharmony_ci } 140e1051a39Sopenharmony_ci 141e1051a39Sopenharmony_ci # We're only interested in the initial ClientHello 142e1051a39Sopenharmony_ci if ($proxy->flight != 0) { 143e1051a39Sopenharmony_ci return; 144e1051a39Sopenharmony_ci } 145e1051a39Sopenharmony_ci 146e1051a39Sopenharmony_ci foreach my $message (@{$proxy->message_list}) { 147e1051a39Sopenharmony_ci if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) { 148e1051a39Sopenharmony_ci my $ext; 149e1051a39Sopenharmony_ci if ($testtype == REVERSE_ORDER_VERSIONS) { 150e1051a39Sopenharmony_ci $ext = pack "C5", 151e1051a39Sopenharmony_ci 0x04, # Length 152e1051a39Sopenharmony_ci 0x03, 0x03, #TLSv1.2 153e1051a39Sopenharmony_ci 0x03, 0x04; #TLSv1.3 154e1051a39Sopenharmony_ci } elsif ($testtype == UNRECOGNISED_VERSIONS) { 155e1051a39Sopenharmony_ci $ext = pack "C5", 156e1051a39Sopenharmony_ci 0x04, # Length 157e1051a39Sopenharmony_ci 0x04, 0x04, #Some unrecognised version 158e1051a39Sopenharmony_ci 0x04, 0x03; #Another unrecognised version 159e1051a39Sopenharmony_ci } elsif ($testtype == TLS1_1_AND_1_0_ONLY) { 160e1051a39Sopenharmony_ci $ext = pack "C5", 161e1051a39Sopenharmony_ci 0x04, # Length 162e1051a39Sopenharmony_ci 0x03, 0x02, #TLSv1.1 163e1051a39Sopenharmony_ci 0x03, 0x01; #TLSv1.0 164e1051a39Sopenharmony_ci } elsif ($testtype == WITH_TLS1_4) { 165e1051a39Sopenharmony_ci $ext = pack "C5", 166e1051a39Sopenharmony_ci 0x04, # Length 167e1051a39Sopenharmony_ci 0x03, 0x05, #TLSv1.4 168e1051a39Sopenharmony_ci 0x03, 0x04; #TLSv1.3 169e1051a39Sopenharmony_ci } 170e1051a39Sopenharmony_ci if ($testtype == REVERSE_ORDER_VERSIONS 171e1051a39Sopenharmony_ci || $testtype == UNRECOGNISED_VERSIONS 172e1051a39Sopenharmony_ci || $testtype == TLS1_1_AND_1_0_ONLY 173e1051a39Sopenharmony_ci || $testtype == WITH_TLS1_4) { 174e1051a39Sopenharmony_ci $message->set_extension( 175e1051a39Sopenharmony_ci TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext); 176e1051a39Sopenharmony_ci } elsif ($testtype == EMPTY_EXTENSION) { 177e1051a39Sopenharmony_ci $message->set_extension( 178e1051a39Sopenharmony_ci TLSProxy::Message::EXT_SUPPORTED_VERSIONS, ""); 179e1051a39Sopenharmony_ci } elsif ($testtype == NO_EXTENSION) { 180e1051a39Sopenharmony_ci $message->delete_extension( 181e1051a39Sopenharmony_ci TLSProxy::Message::EXT_SUPPORTED_VERSIONS); 182e1051a39Sopenharmony_ci } else { 183e1051a39Sopenharmony_ci # BAD_LEGACY_VERSION 184e1051a39Sopenharmony_ci $message->client_version(TLSProxy::Record::VERS_SSL_3_0); 185e1051a39Sopenharmony_ci } 186e1051a39Sopenharmony_ci 187e1051a39Sopenharmony_ci $message->repack(); 188e1051a39Sopenharmony_ci } 189e1051a39Sopenharmony_ci } 190e1051a39Sopenharmony_ci} 191