1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2017-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_ci 14e1051a39Sopenharmony_cimy $test_name = "test_tls13downgrade"; 15e1051a39Sopenharmony_cisetup($test_name); 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O" 18e1051a39Sopenharmony_ci if $^O =~ /^(VMS)$/; 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled" 21e1051a39Sopenharmony_ci if disabled("engine") || disabled("dynamic-engine"); 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled" 24e1051a39Sopenharmony_ci if disabled("sock"); 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLS1.3 and TLS1.2 enabled" 27e1051a39Sopenharmony_ci if disabled("tls1_3") 28e1051a39Sopenharmony_ci || (disabled("ec") && disabled("dh")) 29e1051a39Sopenharmony_ci || disabled("tls1_2"); 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000'; 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new( 34e1051a39Sopenharmony_ci undef, 35e1051a39Sopenharmony_ci cmdstr(app(["openssl"]), display => 1), 36e1051a39Sopenharmony_ci srctop_file("apps", "server.pem"), 37e1051a39Sopenharmony_ci (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 38e1051a39Sopenharmony_ci); 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_ciuse constant { 41e1051a39Sopenharmony_ci DOWNGRADE_TO_TLS_1_2 => 0, 42e1051a39Sopenharmony_ci DOWNGRADE_TO_TLS_1_1 => 1, 43e1051a39Sopenharmony_ci FALLBACK_FROM_TLS_1_3 => 2, 44e1051a39Sopenharmony_ci}; 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_ci#Test 1: Downgrade from TLSv1.3 to TLSv1.2 47e1051a39Sopenharmony_ci$proxy->filter(\&downgrade_filter); 48e1051a39Sopenharmony_cimy $testtype = DOWNGRADE_TO_TLS_1_2; 49e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 50e1051a39Sopenharmony_ciplan tests => 6; 51e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.2"); 52e1051a39Sopenharmony_ci 53e1051a39Sopenharmony_ci#Test 2: Downgrade from TLSv1.3 to TLSv1.1 54e1051a39Sopenharmony_ci$proxy->clear(); 55e1051a39Sopenharmony_ci$testtype = DOWNGRADE_TO_TLS_1_1; 56e1051a39Sopenharmony_ci$proxy->start(); 57e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Downgrade TLSv1.3 to TLSv1.1"); 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ci#Test 3: Downgrade from TLSv1.2 to TLSv1.1 60e1051a39Sopenharmony_ci$proxy->clear(); 61e1051a39Sopenharmony_ci$proxy->clientflags("-no_tls1_3"); 62e1051a39Sopenharmony_ci$proxy->serverflags("-no_tls1_3"); 63e1051a39Sopenharmony_ci$proxy->start(); 64e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Downgrade TLSv1.2 to TLSv1.1"); 65e1051a39Sopenharmony_ci 66e1051a39Sopenharmony_ci#Test 4: Client falls back from TLSv1.3 (server does not support the fallback 67e1051a39Sopenharmony_ci# SCSV) 68e1051a39Sopenharmony_ci$proxy->clear(); 69e1051a39Sopenharmony_ci$testtype = FALLBACK_FROM_TLS_1_3; 70e1051a39Sopenharmony_ci$proxy->clientflags("-fallback_scsv -no_tls1_3"); 71e1051a39Sopenharmony_ci$proxy->start(); 72e1051a39Sopenharmony_cimy $alert = TLSProxy::Message->alert(); 73e1051a39Sopenharmony_ciok(TLSProxy::Message->fail() 74e1051a39Sopenharmony_ci && !$alert->server() 75e1051a39Sopenharmony_ci && $alert->description() == TLSProxy::Message::AL_DESC_ILLEGAL_PARAMETER, 76e1051a39Sopenharmony_ci "Fallback from TLSv1.3"); 77e1051a39Sopenharmony_ci 78e1051a39Sopenharmony_ciSKIP: { 79e1051a39Sopenharmony_ci skip "TLSv1.1 disabled", 2 if disabled("tls1_1"); 80e1051a39Sopenharmony_ci #Test 5: A client side protocol "hole" should not be detected as a downgrade 81e1051a39Sopenharmony_ci $proxy->clear(); 82e1051a39Sopenharmony_ci $proxy->filter(undef); 83e1051a39Sopenharmony_ci $proxy->clientflags("-no_tls1_2"); 84e1051a39Sopenharmony_ci $proxy->ciphers("AES128-SHA:\@SECLEVEL=0"); 85e1051a39Sopenharmony_ci $proxy->start(); 86e1051a39Sopenharmony_ci ok(TLSProxy::Message->success(), "TLSv1.2 client-side protocol hole"); 87e1051a39Sopenharmony_ci 88e1051a39Sopenharmony_ci #Test 6: A server side protocol "hole" should not be detected as a downgrade 89e1051a39Sopenharmony_ci $proxy->clear(); 90e1051a39Sopenharmony_ci $proxy->filter(undef); 91e1051a39Sopenharmony_ci $proxy->serverflags("-no_tls1_2"); 92e1051a39Sopenharmony_ci $proxy->start(); 93e1051a39Sopenharmony_ci ok(TLSProxy::Message->success(), "TLSv1.2 server-side protocol hole"); 94e1051a39Sopenharmony_ci} 95e1051a39Sopenharmony_ci 96e1051a39Sopenharmony_cisub downgrade_filter 97e1051a39Sopenharmony_ci{ 98e1051a39Sopenharmony_ci my $proxy = shift; 99e1051a39Sopenharmony_ci 100e1051a39Sopenharmony_ci # We're only interested in the initial ClientHello 101e1051a39Sopenharmony_ci if ($proxy->flight != 0) { 102e1051a39Sopenharmony_ci return; 103e1051a39Sopenharmony_ci } 104e1051a39Sopenharmony_ci 105e1051a39Sopenharmony_ci my $message = ${$proxy->message_list}[0]; 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_ci my $ext; 108e1051a39Sopenharmony_ci if ($testtype == FALLBACK_FROM_TLS_1_3) { 109e1051a39Sopenharmony_ci #The default ciphersuite we use for TLSv1.2 without any SCSV 110e1051a39Sopenharmony_ci my @ciphersuites = (TLSProxy::Message::CIPHER_RSA_WITH_AES_128_CBC_SHA); 111e1051a39Sopenharmony_ci $message->ciphersuite_len(2 * scalar @ciphersuites); 112e1051a39Sopenharmony_ci $message->ciphersuites(\@ciphersuites); 113e1051a39Sopenharmony_ci } else { 114e1051a39Sopenharmony_ci if ($testtype == DOWNGRADE_TO_TLS_1_2) { 115e1051a39Sopenharmony_ci $ext = pack "C3", 116e1051a39Sopenharmony_ci 0x02, # Length 117e1051a39Sopenharmony_ci 0x03, 0x03; #TLSv1.2 118e1051a39Sopenharmony_ci } else { 119e1051a39Sopenharmony_ci $ext = pack "C3", 120e1051a39Sopenharmony_ci 0x02, # Length 121e1051a39Sopenharmony_ci 0x03, 0x02; #TLSv1.1 122e1051a39Sopenharmony_ci } 123e1051a39Sopenharmony_ci 124e1051a39Sopenharmony_ci $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_VERSIONS, $ext); 125e1051a39Sopenharmony_ci } 126e1051a39Sopenharmony_ci 127e1051a39Sopenharmony_ci $message->repack(); 128e1051a39Sopenharmony_ci} 129e1051a39Sopenharmony_ci 130