Add a test for a missing sig algs extension
[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 use constant {
41     PSK_LAST_FIRST_CH => 0,
42     ILLEGAL_EXT_SECOND_CH => 1
43 };
44
45 #Most PSK tests are done in test_ssl_new. This tests various failure scenarios
46 #around PSK
47
48 #Test 1: First get a session
49 (undef, my $session) = tempfile();
50 $proxy->clientflags("-sess_out ".$session);
51 $proxy->sessionfile($session);
52 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
53 plan tests => 5;
54 ok(TLSProxy::Message->success(), "Initial connection");
55
56 #Test 2: Attempt a resume with PSK not in last place. Should fail
57 $proxy->clear();
58 $proxy->clientflags("-sess_in ".$session);
59 $proxy->filter(\&modify_psk_filter);
60 my $testtype = PSK_LAST_FIRST_CH;
61 $proxy->start();
62 ok(TLSProxy::Message->fail(), "PSK not last");
63
64 #Test 3: Attempt a resume after an HRR where PSK hash matches selected
65 #        ciperhsuite. Should see PSK on second ClientHello
66 $proxy->clear();
67 $proxy->clientflags("-sess_in ".$session);
68 $proxy->serverflags("-curves P-256");
69 $proxy->filter(undef);
70 $proxy->start();
71 #Check if the PSK is present in the second ClientHello
72 my $ch2 = ${$proxy->message_list}[2];
73 my $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
74 my $pskseen = $ch2seen
75               && defined ${$ch2->{extension_data}}{TLSProxy::Message::EXT_PSK};
76 ok($pskseen, "PSK hash matches");
77
78 #Test 4: Attempt a resume after an HRR where PSK hash does not match selected
79 #        ciphersuite. Should not see PSK on second ClientHello
80 $proxy->clear();
81 $proxy->clientflags("-sess_in ".$session);
82 $proxy->filter(\&modify_psk_filter);
83 $proxy->serverflags("-curves P-256");
84 $proxy->cipherc("TLS13-AES-128-GCM-SHA256:TLS13-AES-256-GCM-SHA384");
85 $proxy->ciphers("TLS13-AES-256-GCM-SHA384");
86 #We force an early failure because TLS Proxy doesn't actually support
87 #TLS13-AES-256-GCM-SHA384. That doesn't matter for this test though.
88 $testtype = ILLEGAL_EXT_SECOND_CH;
89 $proxy->start();
90 #Check if the PSK is present in the second ClientHello
91 $ch2 = ${$proxy->message_list}[2];
92 $ch2seen = defined $ch2 && $ch2->mt() == TLSProxy::Message::MT_CLIENT_HELLO;
93 $pskseen = $ch2seen
94            && defined ${$ch2->extension_data}{TLSProxy::Message::EXT_PSK};
95 ok($ch2seen && !$pskseen, "PSK hash does not match");
96
97 #Test 5: Attempt a resume without a sig agls extension. Should succeed because
98 #        sig algs is not needed in a resumption.
99 $proxy->clear();
100 $proxy->clientflags("-sess_in ".$session);
101 $proxy->filter(\&remove_sig_algs_filter);
102 $proxy->start();
103 ok(TLSProxy::Message->success(), "Remove sig algs");
104
105 unlink $session;
106
107 sub modify_psk_filter
108 {
109     my $proxy = shift;
110     my $flight;
111     my $message;
112
113     if ($testtype == PSK_LAST_FIRST_CH) {
114         $flight = 0;
115     } else {
116         $flight = 2;
117     }
118
119     # Only look at the first or second ClientHello
120     return if $proxy->flight != $flight;
121
122     if ($testtype == PSK_LAST_FIRST_CH) {
123         $message = ${$proxy->message_list}[0];
124     } else {
125         $message = ${$proxy->message_list}[2];
126     }
127
128     return if (!defined $message
129                || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
130
131     if ($testtype == PSK_LAST_FIRST_CH) {
132         $message->set_extension(TLSProxy::Message::EXT_FORCE_LAST, "");
133     } else {
134         #Deliberately break the connection
135         $message->set_extension(TLSProxy::Message::EXT_SUPPORTED_GROUPS, "");
136     }
137     $message->repack();
138 }
139
140 sub remove_sig_algs_filter
141 {
142     my $proxy = shift;
143     my $message;
144
145     # Only look at the first ClientHello
146     return if $proxy->flight != 0;
147
148     $message = ${$proxy->message_list}[0];
149     $message->delete_extension(TLSProxy::Message::EXT_SIG_ALGS);
150     $message->repack();
151 }