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