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