summaryrefslogtreecommitdiff
path: root/deps/openssl/openssl/test/recipes/70-test_tls13psk.t
blob: fedc527892ee9d5f120dbc0d44e698eacf99ee40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#! /usr/bin/env perl
# Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html

use strict;
use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
use OpenSSL::Test::Utils;
use File::Temp qw(tempfile);
use TLSProxy::Proxy;

my $test_name = "test_tls13psk";
setup($test_name);

plan skip_all => "TLSProxy isn't usable on $^O"
    if $^O =~ /^(VMS)$/;

plan skip_all => "$test_name needs the dynamic engine feature enabled"
    if disabled("engine") || disabled("dynamic-engine");

plan skip_all => "$test_name needs the sock feature enabled"
    if disabled("sock");

plan skip_all => "$test_name needs TLSv1.3 enabled"
    if disabled("tls1_3");

$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
$ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");

my $proxy = TLSProxy::Proxy->new(
    undef,
    cmdstr(app(["openssl"]), display => 1),
    srctop_file("apps", "server.pem"),
    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
);

use constant {
    PSK_LAST_FIRST_CH => 0,
    ILLEGAL_EXT_SECOND_CH => 1
};

#Most PSK tests are done in test_ssl_new. This tests various failure scenarios
#around PSK

#Test 1: First get a session
(undef, my $session) = tempfile();
$proxy->clientflags("-sess_out ".$session);
$proxy->serverflags("-servername localhost");
$proxy->sessionfile($session);
$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
plan tests => 5;
ok(TLSProxy::Message->success(), "Initial connection");

#Test 2: Attempt a resume with PSK not in last place. Should fail
$proxy->clear();
$proxy->clientflags("-sess_in ".$session);
$proxy->filter(\&modify_psk_filter);
my $testtype = PSK_LAST_FIRST_CH;
$proxy->start();
ok(TLSProxy::Message->fail(), "PSK not last");

#Test 3: Attempt a resume after an HRR where PSK hash matches selected
#        ciphersuite. Should see PSK on second ClientHello
$proxy->clear();
$proxy->clientflags("-sess_in ".$session);
$proxy->serverflags("-curves P-256");
$proxy->filter(undef);
$proxy->start();
#Check if the PSK is present in the second ClientHello
my $ch2 = ${$proxy->message_list}[2];
my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
my $pskseen = $ch2seen
              && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK};
ok($pskseen, "PSK hash matches");

#Test 4: Attempt a resume after an HRR where PSK hash does not match selected
#        ciphersuite. Should not see PSK on second ClientHello
$proxy->clear();
$proxy->clientflags("-sess_in ".$session);
$proxy->filter(\&modify_psk_filter);
$proxy->serverflags("-curves P-256");
$proxy->ciphersuitesc("TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384");
$proxy->ciphersuitess("TLS_AES_256_GCM_SHA384");
#We force an early failure because TLS Proxy doesn't actually support
#TLS_AES_256_GCM_SHA384. That doesn't matter for this test though.
$testtype = ILLEGAL_EXT_SECOND_CH;
$proxy->start();
#Check if the PSK is present in the second ClientHello
$ch2 = ${$proxy->message_list}[2];
$ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
$pskseen = $ch2seen
           && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK};
ok($ch2seen && !$pskseen, "PSK hash does not match");

#Test 5: Attempt a resume without a sig agls extension. Should succeed because
#        sig algs is not needed in a resumption.
$proxy->clear();
$proxy->clientflags("-sess_in ".$session);
$proxy->filter(\&remove_sig_algs_filter);
$proxy->start();
ok(TLSProxy::Message->success(), "Remove sig algs");

unlink $session;

sub modify_psk_filter
{
    my $proxy = shift;
    my $flight;
    my $message;

    if ($testtype == PSK_LAST_FIRST_CH) {
        $flight = 0;
    } else {
        $flight = 2;
    }

    # Only look at the first or second ClientHello
    return if $proxy->flight != $flight;

    if ($testtype == PSK_LAST_FIRST_CH) {
        $message = ${$proxy->message_list}[0];
    } else {
        $message = ${$proxy->message_list}[2];
    }

    return if (!defined $message
               || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);

    if ($testtype == PSK_LAST_FIRST_CH) {
        $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
    } else {
        #Deliberately break the connection
        $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, "");
    }
    $message->repack();
}

sub remove_sig_algs_filter
{
    my $proxy = shift;
    my $message;

    # Only look at the first ClientHello
    return if $proxy->flight != 0;

    $message = ${$proxy->message_list}[0];
    $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
    $message->repack();
}