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