2 # Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use File::Temp qw(tempfile);
15 my $test_name = "test_tls13psk";
18 plan skip_all => "TLSProxy isn't usable on $^O"
21 plan skip_all => "$test_name needs the dynamic engine feature enabled"
22 if disabled("engine") || disabled("dynamic-engine");
24 plan skip_all => "$test_name needs the sock feature enabled"
27 plan skip_all => "$test_name needs TLSv1.3 enabled"
28 if disabled("tls1_3");
30 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32 my $proxy = TLSProxy::Proxy->new(
34 cmdstr(app(["openssl"]), display => 1),
35 srctop_file("apps", "server.pem"),
36 (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
40 PSK_LAST_FIRST_CH => 0,
41 ILLEGAL_EXT_SECOND_CH => 1
44 #Most PSK tests are done in test_ssl_new. This tests various failure scenarios
47 #Test 1: First get a session
48 (undef, my $session) = tempfile();
49 $proxy->clientflags("-sess_out ".$session);
50 $proxy->serverflags("-servername localhost");
51 $proxy->sessionfile($session);
52 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
54 ok(TLSProxy::Message->success(), "Initial connection");
56 #Test 2: Attempt a resume with PSK not in last place. Should fail
58 $proxy->clientflags("-sess_in ".$session);
59 $proxy->filter(\&modify_psk_filter);
60 my $testtype = PSK_LAST_FIRST_CH;
62 ok(TLSProxy::Message->fail(), "PSK not last");
64 #Test 3: Attempt a resume after an HRR where PSK hash matches selected
65 # ciphersuite. Should see PSK on second ClientHello
67 $proxy->clientflags("-sess_in ".$session);
69 $proxy->serverflags("-curves ffdhe3072");
71 $proxy->serverflags("-curves P-256");
73 $proxy->filter(undef);
75 #Check if the PSK is present in the second ClientHello
76 my $ch2 = ${$proxy->message_list}[2];
77 my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
78 my $pskseen = $ch2seen
79 && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK};
80 ok($pskseen, "PSK hash matches");
82 #Test 4: Attempt a resume after an HRR where PSK hash does not match selected
83 # ciphersuite. Should not see PSK on second ClientHello
85 $proxy->clientflags("-sess_in ".$session);
86 $proxy->filter(\&modify_psk_filter);
88 $proxy->serverflags("-curves ffdhe3072");
90 $proxy->serverflags("-curves P-256");
92 $proxy->ciphersuitesc("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
93 $proxy->ciphersuitess("TLS_AES_256_GCM_SHA384");
94 #We force an early failure because TLS Proxy doesn't actually support
95 #TLS_AES_256_GCM_SHA384. That doesn't matter for this test though.
96 $testtype = ILLEGAL_EXT_SECOND_CH;
98 #Check if the PSK is present in the second ClientHello
99 $ch2 = ${$proxy->message_list}[2];
100 $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
102 && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK};
103 ok($ch2seen && !$pskseen, "PSK hash does not match");
105 #Test 5: Attempt a resume without a sig agls extension. Should succeed because
106 # sig algs is not needed in a resumption.
108 $proxy->clientflags("-sess_in ".$session);
109 $proxy->filter(\&remove_sig_algs_filter);
111 ok(TLSProxy::Message->success(), "Remove sig algs");
115 sub modify_psk_filter
121 if ($testtype == PSK_LAST_FIRST_CH) {
127 # Only look at the first or second ClientHello
128 return if $proxy->flight != $flight;
130 if ($testtype == PSK_LAST_FIRST_CH) {
131 $message = ${$proxy->message_list}[0];
133 $message = ${$proxy->message_list}[2];
136 return if (!defined $message
137 || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
139 if ($testtype == PSK_LAST_FIRST_CH) {
140 $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
142 #Deliberately break the connection
143 $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, "");
148 sub remove_sig_algs_filter
153 # Only look at the first ClientHello
154 return if $proxy->flight != 0;
156 $message = ${$proxy->message_list}[0];
157 $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);