1#! /usr/bin/env perl 2# Copyright 2017-2023 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 srctop_dir bldtop_dir/; 11use OpenSSL::Test::Utils; 12use File::Temp qw(tempfile); 13use TLSProxy::Proxy; 14 15my $test_name = "test_tls13psk"; 16setup($test_name); 17 18plan skip_all => "TLSProxy isn't usable on $^O" 19 if $^O =~ /^(VMS)$/; 20 21plan skip_all => "$test_name needs the dynamic engine feature enabled" 22 if disabled("engine") || disabled("dynamic-engine"); 23 24plan skip_all => "$test_name needs the sock feature enabled" 25 if disabled("sock"); 26 27plan skip_all => "$test_name needs TLSv1.3 enabled" 28 if disabled("tls1_3") || (disabled("ec") && disabled("dh")); 29 30my $proxy = TLSProxy::Proxy->new( 31 undef, 32 cmdstr(app(["openssl"]), display => 1), 33 srctop_file("apps", "server.pem"), 34 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE}) 35); 36 37use constant { 38 PSK_LAST_FIRST_CH => 0, 39 ILLEGAL_EXT_SECOND_CH => 1 40}; 41 42#Most PSK tests are done in test_ssl_new. This tests various failure scenarios 43#around PSK 44 45#Test 1: First get a session 46(undef, my $session) = tempfile(); 47$proxy->clientflags("-sess_out ".$session); 48$proxy->serverflags("-servername localhost"); 49$proxy->sessionfile($session); 50$proxy->start() or plan skip_all => "Unable to start up Proxy for tests"; 51plan tests => 5; 52ok(TLSProxy::Message->success(), "Initial connection"); 53 54#Test 2: Attempt a resume with PSK not in last place. Should fail 55$proxy->clear(); 56$proxy->clientflags("-sess_in ".$session); 57$proxy->filter(\&modify_psk_filter); 58my $testtype = PSK_LAST_FIRST_CH; 59$proxy->start(); 60ok(TLSProxy::Message->fail(), "PSK not last"); 61 62#Test 3: Attempt a resume after an HRR where PSK hash matches selected 63# ciphersuite. Should see PSK on second ClientHello 64$proxy->clear(); 65$proxy->clientflags("-sess_in ".$session); 66if (disabled("ec")) { 67 $proxy->serverflags("-curves ffdhe3072"); 68} else { 69 $proxy->serverflags("-curves P-384"); 70} 71$proxy->filter(undef); 72$proxy->start(); 73#Check if the PSK is present in the second ClientHello 74my $ch2 = ${$proxy->message_list}[2]; 75my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO; 76my $pskseen = $ch2seen 77 && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK}; 78ok($pskseen, "PSK hash matches"); 79 80#Test 4: Attempt a resume after an HRR where PSK hash does not match selected 81# ciphersuite. Should not see PSK on second ClientHello 82$proxy->clear(); 83$proxy->clientflags("-sess_in ".$session); 84$proxy->filter(\&modify_psk_filter); 85if (disabled("ec")) { 86 $proxy->serverflags("-curves ffdhe3072"); 87} else { 88 $proxy->serverflags("-curves P-384"); 89} 90$proxy->ciphersuitesc("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"); 91$proxy->ciphersuitess("TLS_AES_256_GCM_SHA384"); 92#We force an early failure because TLS Proxy doesn't actually support 93#TLS_AES_256_GCM_SHA384. That doesn't matter for this test though. 94$testtype = ILLEGAL_EXT_SECOND_CH; 95$proxy->start(); 96#Check if the PSK is present in the second ClientHello 97$ch2 = ${$proxy->message_list}[2]; 98$ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO; 99$pskseen = $ch2seen 100 && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK}; 101ok($ch2seen && !$pskseen, "PSK hash does not match"); 102 103#Test 5: Attempt a resume without a sig agls extension. Should succeed because 104# sig algs is not needed in a resumption. 105$proxy->clear(); 106$proxy->clientflags("-sess_in ".$session); 107$proxy->filter(\&remove_sig_algs_filter); 108$proxy->start(); 109ok(TLSProxy::Message->success(), "Remove sig algs"); 110 111unlink $session; 112 113sub modify_psk_filter 114{ 115 my $proxy = shift; 116 my $flight; 117 my $message; 118 119 if ($testtype == PSK_LAST_FIRST_CH) { 120 $flight = 0; 121 } else { 122 $flight = 2; 123 } 124 125 # Only look at the first or second ClientHello 126 return if $proxy->flight != $flight; 127 128 if ($testtype == PSK_LAST_FIRST_CH) { 129 $message = ${$proxy->message_list}[0]; 130 } else { 131 $message = ${$proxy->message_list}[2]; 132 } 133 134 return if (!defined $message 135 || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO); 136 137 if ($testtype == PSK_LAST_FIRST_CH) { 138 $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, ""); 139 } else { 140 #Deliberately break the connection 141 $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, ""); 142 } 143 $message->repack(); 144} 145 146sub remove_sig_algs_filter 147{ 148 my $proxy = shift; 149 my $message; 150 151 # Only look at the first ClientHello 152 return if $proxy->flight != 0; 153 154 $message = ${$proxy->message_list}[0]; 155 $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS); 156 $message->repack(); 157} 158