1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2017-2022 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_tls13hrr"; 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 enabled" 27e1051a39Sopenharmony_ci if disabled("tls1_3") || (disabled("ec") && disabled("dh")); 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000'; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new( 32e1051a39Sopenharmony_ci undef, 33e1051a39Sopenharmony_ci cmdstr(app(["openssl"]), display => 1), 34e1051a39Sopenharmony_ci srctop_file("apps", "server.pem"), 35e1051a39Sopenharmony_ci (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 36e1051a39Sopenharmony_ci); 37e1051a39Sopenharmony_ci 38e1051a39Sopenharmony_ciuse constant { 39e1051a39Sopenharmony_ci CHANGE_HRR_CIPHERSUITE => 0, 40e1051a39Sopenharmony_ci CHANGE_CH1_CIPHERSUITE => 1, 41e1051a39Sopenharmony_ci DUPLICATE_HRR => 2 42e1051a39Sopenharmony_ci}; 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_ci#Test 1: A client should fail if the server changes the ciphersuite between the 45e1051a39Sopenharmony_ci# HRR and the SH 46e1051a39Sopenharmony_ci$proxy->filter(\&hrr_filter); 47e1051a39Sopenharmony_ciif (disabled("ec")) { 48e1051a39Sopenharmony_ci $proxy->serverflags("-curves ffdhe3072"); 49e1051a39Sopenharmony_ci} else { 50e1051a39Sopenharmony_ci $proxy->serverflags("-curves P-256"); 51e1051a39Sopenharmony_ci} 52e1051a39Sopenharmony_cimy $testtype = CHANGE_HRR_CIPHERSUITE; 53e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 54e1051a39Sopenharmony_ciplan tests => 3; 55e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Server ciphersuite changes"); 56e1051a39Sopenharmony_ci 57e1051a39Sopenharmony_ci#Test 2: It is an error if the client changes the offered ciphersuites so that 58e1051a39Sopenharmony_ci# we end up selecting a different ciphersuite between HRR and the SH 59e1051a39Sopenharmony_ci$proxy->clear(); 60e1051a39Sopenharmony_ciif (disabled("ec")) { 61e1051a39Sopenharmony_ci $proxy->serverflags("-curves ffdhe3072"); 62e1051a39Sopenharmony_ci} else { 63e1051a39Sopenharmony_ci $proxy->serverflags("-curves P-256"); 64e1051a39Sopenharmony_ci} 65e1051a39Sopenharmony_ci$proxy->ciphersuitess("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"); 66e1051a39Sopenharmony_ci$testtype = CHANGE_CH1_CIPHERSUITE; 67e1051a39Sopenharmony_ci$proxy->start(); 68e1051a39Sopenharmony_ciok(TLSProxy::Message->fail(), "Client ciphersuite changes"); 69e1051a39Sopenharmony_ci 70e1051a39Sopenharmony_ci#Test 3: A client should fail with unexpected_message alert if the server 71e1051a39Sopenharmony_ci# sends more than 1 HRR 72e1051a39Sopenharmony_cimy $fatal_alert = 0; 73e1051a39Sopenharmony_ci$proxy->clear(); 74e1051a39Sopenharmony_ciif (disabled("ec")) { 75e1051a39Sopenharmony_ci $proxy->serverflags("-curves ffdhe3072"); 76e1051a39Sopenharmony_ci} else { 77e1051a39Sopenharmony_ci $proxy->serverflags("-curves P-256"); 78e1051a39Sopenharmony_ci} 79e1051a39Sopenharmony_ci$testtype = DUPLICATE_HRR; 80e1051a39Sopenharmony_ci$proxy->start(); 81e1051a39Sopenharmony_ciok($fatal_alert, "Server duplicated HRR"); 82e1051a39Sopenharmony_ci 83e1051a39Sopenharmony_cisub hrr_filter 84e1051a39Sopenharmony_ci{ 85e1051a39Sopenharmony_ci my $proxy = shift; 86e1051a39Sopenharmony_ci 87e1051a39Sopenharmony_ci if ($testtype == CHANGE_HRR_CIPHERSUITE) { 88e1051a39Sopenharmony_ci # We're only interested in the HRR 89e1051a39Sopenharmony_ci if ($proxy->flight != 1) { 90e1051a39Sopenharmony_ci return; 91e1051a39Sopenharmony_ci } 92e1051a39Sopenharmony_ci 93e1051a39Sopenharmony_ci my $hrr = ${$proxy->message_list}[1]; 94e1051a39Sopenharmony_ci 95e1051a39Sopenharmony_ci # We will normally only ever select CIPHER_TLS13_AES_128_GCM_SHA256 96e1051a39Sopenharmony_ci # because that's what Proxy tells s_server to do. Setting as below means 97e1051a39Sopenharmony_ci # the ciphersuite will change will we get the ServerHello 98e1051a39Sopenharmony_ci $hrr->ciphersuite(TLSProxy::Message::CIPHER_TLS13_AES_256_GCM_SHA384); 99e1051a39Sopenharmony_ci $hrr->repack(); 100e1051a39Sopenharmony_ci return; 101e1051a39Sopenharmony_ci } 102e1051a39Sopenharmony_ci 103e1051a39Sopenharmony_ci if ($testtype == DUPLICATE_HRR) { 104e1051a39Sopenharmony_ci # We're only interested in the HRR 105e1051a39Sopenharmony_ci # and the unexpected_message alert from client 106e1051a39Sopenharmony_ci if ($proxy->flight == 4) { 107e1051a39Sopenharmony_ci $fatal_alert = 1 108e1051a39Sopenharmony_ci if @{$proxy->record_list}[-1]->is_fatal_alert(0) == 10; 109e1051a39Sopenharmony_ci return; 110e1051a39Sopenharmony_ci } 111e1051a39Sopenharmony_ci if ($proxy->flight != 3) { 112e1051a39Sopenharmony_ci return; 113e1051a39Sopenharmony_ci } 114e1051a39Sopenharmony_ci 115e1051a39Sopenharmony_ci # Find ServerHello record (HRR actually) and insert after that 116e1051a39Sopenharmony_ci my $i; 117e1051a39Sopenharmony_ci for ($i = 0; ${$proxy->record_list}[$i]->flight() < 1; $i++) { 118e1051a39Sopenharmony_ci next; 119e1051a39Sopenharmony_ci } 120e1051a39Sopenharmony_ci my $hrr_record = ${$proxy->record_list}[$i]; 121e1051a39Sopenharmony_ci my $dup_hrr = TLSProxy::Record->new(3, 122e1051a39Sopenharmony_ci $hrr_record->content_type(), 123e1051a39Sopenharmony_ci $hrr_record->version(), 124e1051a39Sopenharmony_ci $hrr_record->len(), 125e1051a39Sopenharmony_ci $hrr_record->sslv2(), 126e1051a39Sopenharmony_ci $hrr_record->len_real(), 127e1051a39Sopenharmony_ci $hrr_record->decrypt_len(), 128e1051a39Sopenharmony_ci $hrr_record->data(), 129e1051a39Sopenharmony_ci $hrr_record->decrypt_data()); 130e1051a39Sopenharmony_ci 131e1051a39Sopenharmony_ci $i++; 132e1051a39Sopenharmony_ci splice @{$proxy->record_list}, $i, 0, $dup_hrr; 133e1051a39Sopenharmony_ci return; 134e1051a39Sopenharmony_ci } 135e1051a39Sopenharmony_ci 136e1051a39Sopenharmony_ci # CHANGE_CH1_CIPHERSUITE 137e1051a39Sopenharmony_ci if ($proxy->flight != 0) { 138e1051a39Sopenharmony_ci return; 139e1051a39Sopenharmony_ci } 140e1051a39Sopenharmony_ci 141e1051a39Sopenharmony_ci my $ch1 = ${$proxy->message_list}[0]; 142e1051a39Sopenharmony_ci 143e1051a39Sopenharmony_ci # The server will always pick TLS_AES_256_GCM_SHA384 144e1051a39Sopenharmony_ci my @ciphersuites = (TLSProxy::Message::CIPHER_TLS13_AES_128_GCM_SHA256); 145e1051a39Sopenharmony_ci $ch1->ciphersuite_len(2 * scalar @ciphersuites); 146e1051a39Sopenharmony_ci $ch1->ciphersuites(\@ciphersuites); 147e1051a39Sopenharmony_ci $ch1->repack(); 148e1051a39Sopenharmony_ci} 149