Add a test to check that if a PSK extension is not last then we fail
[openssl.git] / test / recipes / 70-test_tls13psk.t
1 #! /usr/bin/env perl
2 # Copyright 2017 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
9 use strict;
10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use File::Temp qw(tempfile);
13 use TLSProxy::Proxy;
14
15 my $test_name = "test_tls13psk";
16 setup($test_name);
17
18 plan skip_all => "TLSProxy isn't usable on $^O"
19     if $^O =~ /^(VMS|MSWin32)$/;
20
21 plan skip_all => "$test_name needs the dynamic engine feature enabled"
22     if disabled("engine") || disabled("dynamic-engine");
23
24 plan skip_all => "$test_name needs the sock feature enabled"
25     if disabled("sock");
26
27 plan 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
33 my $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
40 #Most PSK tests are done in test_ssl_new. This just checks sending a PSK
41 #extension when it isn't in the last place in a ClientHello
42
43 #Test 1: First get a session
44 (undef, my $session) = tempfile();
45 $proxy->clientflags("-sess_out ".$session);
46 $proxy->sessionfile($session);
47 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
48 plan tests => 2;
49 ok(TLSProxy::Message->success(), "Initial connection");
50
51 #Test 2: Attempt a resume with PSK not in last place. Should fail
52 $proxy->clear();
53 $proxy->clientflags("-sess_in ".$session);
54 $proxy->filter(\&modify_psk_filter);
55 $proxy->start();
56 ok(TLSProxy::Message->fail(), "PSK not last");
57
58 unlink $session;
59
60 sub modify_psk_filter
61 {
62     my $proxy = shift;
63
64     # We're only interested in the initial ClientHello
65     return if ($proxy->flight != 0);
66
67     foreach my $message (@{$proxy->message_list}) {
68         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
69             $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
70             $message->repack();
71         }
72     }
73 }