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