Fix incorrect function name in BN_bn2bin manpage
[openssl.git] / test / recipes / 70-test_tls13kexmodes.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 use checkhandshake qw(checkhandshake @handmessages @extensions);
15
16 my $test_name = "test_tls13kexmodes";
17 setup($test_name);
18
19 plan skip_all => "TLSProxy isn't usable on $^O"
20     if $^O =~ /^(VMS|MSWin32)$/;
21
22 plan skip_all => "$test_name needs the dynamic engine feature enabled"
23     if disabled("engine") || disabled("dynamic-engine");
24
25 plan skip_all => "$test_name needs the sock feature enabled"
26     if disabled("sock");
27
28 plan skip_all => "$test_name needs TLSv1.3 enabled"
29     if disabled("tls1_3");
30
31 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
32 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
33
34
35 @handmessages = (
36     [TLSProxy::Message::MT_CLIENT_HELLO,
37         checkhandshake::ALL_HANDSHAKES],
38     [TLSProxy::Message::MT_HELLO_RETRY_REQUEST,
39         checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
40     [TLSProxy::Message::MT_CLIENT_HELLO,
41         checkhandshake::HRR_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE],
42     [TLSProxy::Message::MT_SERVER_HELLO,
43         checkhandshake::ALL_HANDSHAKES],
44     [TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
45         checkhandshake::ALL_HANDSHAKES],
46     [TLSProxy::Message::MT_CERTIFICATE_REQUEST,
47         checkhandshake::CLIENT_AUTH_HANDSHAKE],
48     [TLSProxy::Message::MT_CERTIFICATE,
49         checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
50     [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
51         checkhandshake::ALL_HANDSHAKES & ~(checkhandshake::RESUME_HANDSHAKE | checkhandshake::HRR_RESUME_HANDSHAKE)],
52     [TLSProxy::Message::MT_FINISHED,
53         checkhandshake::ALL_HANDSHAKES],
54     [TLSProxy::Message::MT_CERTIFICATE,
55         checkhandshake::CLIENT_AUTH_HANDSHAKE],
56     [TLSProxy::Message::MT_CERTIFICATE_VERIFY,
57         checkhandshake::CLIENT_AUTH_HANDSHAKE],
58     [TLSProxy::Message::MT_FINISHED,
59         checkhandshake::ALL_HANDSHAKES],
60     [0, 0]
61 );
62
63 @extensions = (
64     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
65         checkhandshake::SERVER_NAME_CLI_EXTENSION],
66     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
67         checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
68     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
69         checkhandshake::DEFAULT_EXTENSIONS],
70     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EC_POINT_FORMATS,
71         checkhandshake::DEFAULT_EXTENSIONS],
72     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
73         checkhandshake::DEFAULT_EXTENSIONS],
74     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
75         checkhandshake::ALPN_CLI_EXTENSION],
76     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
77         checkhandshake::SCT_CLI_EXTENSION],
78     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ENCRYPT_THEN_MAC,
79         checkhandshake::DEFAULT_EXTENSIONS],
80     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_EXTENDED_MASTER_SECRET,
81         checkhandshake::DEFAULT_EXTENSIONS],
82     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SESSION_TICKET,
83         checkhandshake::DEFAULT_EXTENSIONS],
84     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
85         checkhandshake::DEFAULT_EXTENSIONS],
86     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
87         checkhandshake::DEFAULT_EXTENSIONS],
88     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
89         checkhandshake::PSK_KEX_MODES_EXTENSION],
90     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
91         checkhandshake::PSK_CLI_EXTENSION],
92
93     [TLSProxy::Message::MT_HELLO_RETRY_REQUEST, TLSProxy::Message::EXT_KEY_SHARE,
94         checkhandshake::KEY_SHARE_HRR_EXTENSION],
95
96     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SERVER_NAME,
97         checkhandshake::SERVER_NAME_CLI_EXTENSION],
98     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_STATUS_REQUEST,
99         checkhandshake::STATUS_REQUEST_CLI_EXTENSION],
100     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_GROUPS,
101         checkhandshake::DEFAULT_EXTENSIONS],
102     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SIG_ALGS,
103         checkhandshake::DEFAULT_EXTENSIONS],
104     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_ALPN,
105         checkhandshake::ALPN_CLI_EXTENSION],
106     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SCT,
107         checkhandshake::SCT_CLI_EXTENSION],
108     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
109         checkhandshake::DEFAULT_EXTENSIONS],
110     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_SUPPORTED_VERSIONS,
111         checkhandshake::DEFAULT_EXTENSIONS],
112     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK_KEX_MODES,
113         checkhandshake::PSK_KEX_MODES_EXTENSION],
114     [TLSProxy::Message::MT_CLIENT_HELLO, TLSProxy::Message::EXT_PSK,
115         checkhandshake::PSK_CLI_EXTENSION],
116
117     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_KEY_SHARE,
118         checkhandshake::KEY_SHARE_SRV_EXTENSION],
119     [TLSProxy::Message::MT_SERVER_HELLO, TLSProxy::Message::EXT_PSK,
120         checkhandshake::PSK_SRV_EXTENSION],
121
122     [TLSProxy::Message::MT_CERTIFICATE, TLSProxy::Message::EXT_STATUS_REQUEST,
123         checkhandshake::STATUS_REQUEST_SRV_EXTENSION],
124     [0,0,0]
125 );
126
127 use constant {
128     DELETE_EXTENSION => 0,
129     EMPTY_EXTENSION => 1,
130     NON_DHE_KEX_MODE_ONLY => 2,
131     DHE_KEX_MODE_ONLY => 3,
132     UNKNONW_KEX_MODES => 4,
133     BOTH_KEX_MODES => 5
134 };
135
136 my $proxy = TLSProxy::Proxy->new(
137     undef,
138     cmdstr(app(["openssl"]), display => 1),
139     srctop_file("apps", "server.pem"),
140     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
141 );
142
143 #Test 1: First get a session
144 (undef, my $session) = tempfile();
145 $proxy->clientflags("-sess_out ".$session);
146 $proxy->serverflags("-servername localhost");
147 $proxy->sessionfile($session);
148 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
149 plan tests => 11;
150 ok(TLSProxy::Message->success(), "Initial connection");
151
152 #Test 2: Attempt a resume with no kex modes extension. Should not resume
153 $proxy->clear();
154 $proxy->clientflags("-sess_in ".$session);
155 my $testtype = DELETE_EXTENSION;
156 $proxy->filter(\&modify_kex_modes_filter);
157 $proxy->start();
158 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
159                checkhandshake::DEFAULT_EXTENSIONS
160                | checkhandshake::KEY_SHARE_SRV_EXTENSION
161                | checkhandshake::PSK_CLI_EXTENSION,
162                "Resume with no kex modes");
163
164 #Test 3: Attempt a resume with empty kex modes extension. Should fail (empty
165 #        extension is invalid)
166 $proxy->clear();
167 $proxy->clientflags("-sess_in ".$session);
168 $testtype = EMPTY_EXTENSION;
169 $proxy->start();
170 ok(TLSProxy::Message->fail(), "Resume with empty kex modes");
171
172 #Test 4: Attempt a resume with non-dhe kex mode only. Should resume without a
173 #        key_share
174 $proxy->clear();
175 $proxy->clientflags("-allow_no_dhe_kex -sess_in ".$session);
176 $proxy->serverflags("-allow_no_dhe_kex");
177 $testtype = NON_DHE_KEX_MODE_ONLY;
178 $proxy->start();
179 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
180                checkhandshake::DEFAULT_EXTENSIONS
181                | checkhandshake::PSK_KEX_MODES_EXTENSION
182                | checkhandshake::PSK_CLI_EXTENSION
183                | checkhandshake::PSK_SRV_EXTENSION,
184                "Resume with non-dhe kex mode");
185
186 #Test 5: Attempt a resume with dhe kex mode only. Should resume with a key_share
187 $proxy->clear();
188 $proxy->clientflags("-sess_in ".$session);
189 $testtype = DHE_KEX_MODE_ONLY;
190 $proxy->start();
191 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
192                checkhandshake::DEFAULT_EXTENSIONS
193                | checkhandshake::PSK_KEX_MODES_EXTENSION
194                | checkhandshake::KEY_SHARE_SRV_EXTENSION
195                | checkhandshake::PSK_CLI_EXTENSION
196                | checkhandshake::PSK_SRV_EXTENSION,
197                "Resume with non-dhe kex mode");
198
199 #Test 6: Attempt a resume with only unrecognised kex modes. Should not resume
200 $proxy->clear();
201 $proxy->clientflags("-sess_in ".$session);
202 $testtype = UNKNONW_KEX_MODES;
203 $proxy->start();
204 checkhandshake($proxy, checkhandshake::DEFAULT_HANDSHAKE,
205                checkhandshake::DEFAULT_EXTENSIONS
206                | checkhandshake::PSK_KEX_MODES_EXTENSION
207                | checkhandshake::KEY_SHARE_SRV_EXTENSION
208                | checkhandshake::PSK_CLI_EXTENSION,
209                "Resume with empty kex modes");
210
211 #Test 7: Attempt a resume with both non-dhe and dhe kex mode. Should resume with
212 #        a key_share
213 $proxy->clear();
214 $proxy->clientflags("-sess_in ".$session);
215 $testtype = BOTH_KEX_MODES;
216 $proxy->start();
217 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
218                checkhandshake::DEFAULT_EXTENSIONS
219                | checkhandshake::PSK_KEX_MODES_EXTENSION
220                | checkhandshake::KEY_SHARE_SRV_EXTENSION
221                | checkhandshake::PSK_CLI_EXTENSION
222                | checkhandshake::PSK_SRV_EXTENSION,
223                "Resume with non-dhe kex mode");
224
225 #Test 8: Attempt a resume with both non-dhe and dhe kex mode, but unacceptable
226 #        initial key_share. Should resume with a key_share following an HRR
227 $proxy->clear();
228 $proxy->clientflags("-sess_in ".$session);
229 $proxy->serverflags("-curves P-256");
230 $testtype = BOTH_KEX_MODES;
231 $proxy->start();
232 checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
233                checkhandshake::DEFAULT_EXTENSIONS
234                | checkhandshake::PSK_KEX_MODES_EXTENSION
235                | checkhandshake::KEY_SHARE_SRV_EXTENSION
236                | checkhandshake::KEY_SHARE_HRR_EXTENSION
237                | checkhandshake::PSK_CLI_EXTENSION
238                | checkhandshake::PSK_SRV_EXTENSION,
239                "Resume with both kex modes and HRR");
240
241 #Test 9: Attempt a resume with dhe kex mode only and an unnacceptable initial
242 #        key_share. Should resume with a key_share following an HRR
243 $proxy->clear();
244 $proxy->clientflags("-sess_in ".$session);
245 $proxy->serverflags("-curves P-256");
246 $testtype = DHE_KEX_MODE_ONLY;
247 $proxy->start();
248 checkhandshake($proxy, checkhandshake::HRR_RESUME_HANDSHAKE,
249                checkhandshake::DEFAULT_EXTENSIONS
250                | checkhandshake::PSK_KEX_MODES_EXTENSION
251                | checkhandshake::KEY_SHARE_SRV_EXTENSION
252                | checkhandshake::KEY_SHARE_HRR_EXTENSION
253                | checkhandshake::PSK_CLI_EXTENSION
254                | checkhandshake::PSK_SRV_EXTENSION,
255                "Resume with dhe kex mode and HRR");
256
257 #Test 10: Attempt a resume with both non-dhe and dhe kex mode, unacceptable
258 #         initial key_share and no overlapping groups. Should resume without a
259 #         key_share
260 $proxy->clear();
261 $proxy->clientflags("-allow_no_dhe_kex -curves P-384 -sess_in ".$session);
262 $proxy->serverflags("-allow_no_dhe_kex -curves P-256");
263 $testtype = BOTH_KEX_MODES;
264 $proxy->start();
265 checkhandshake($proxy, checkhandshake::RESUME_HANDSHAKE,
266                checkhandshake::DEFAULT_EXTENSIONS
267                | checkhandshake::PSK_KEX_MODES_EXTENSION
268                | checkhandshake::PSK_CLI_EXTENSION
269                | checkhandshake::PSK_SRV_EXTENSION,
270                "Resume with both kex modes, no overlapping groups");
271
272 #Test 11: Attempt a resume with dhe kex mode only, unacceptable
273 #         initial key_share and no overlapping groups. Should fail
274 $proxy->clear();
275 $proxy->clientflags("-curves P-384 -sess_in ".$session);
276 $proxy->serverflags("-curves P-256");
277 $testtype = DHE_KEX_MODE_ONLY;
278 $proxy->start();
279 ok(TLSProxy::Message->fail(), "Resume with dhe kex mode, no overlapping groups");
280
281 unlink $session;
282
283 sub modify_kex_modes_filter
284 {
285     my $proxy = shift;
286
287     # We're only interested in the initial ClientHello
288     return if ($proxy->flight != 0);
289
290     foreach my $message (@{$proxy->message_list}) {
291         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
292             my $ext;
293
294             if ($testtype == EMPTY_EXTENSION) {
295                 $ext = pack "C",
296                     0x00;       #List length
297             } elsif ($testtype == NON_DHE_KEX_MODE_ONLY) {
298                 $ext = pack "C2",
299                     0x01,       #List length
300                     0x00;       #psk_ke
301             } elsif ($testtype == DHE_KEX_MODE_ONLY) {
302                 $ext = pack "C2",
303                     0x01,       #List length
304                     0x01;       #psk_dhe_ke
305             } elsif ($testtype == UNKNONW_KEX_MODES) {
306                 $ext = pack "C3",
307                     0x02,       #List length
308                     0xfe,       #unknown
309                     0xff;       #unknown
310             } elsif ($testtype == BOTH_KEX_MODES) {
311                 #We deliberately list psk_ke first...should still use psk_dhe_ke
312                 $ext = pack "C3",
313                     0x02,       #List length
314                     0x00,       #psk_ke
315                     0x01;       #psk_dhe_ke
316             }
317
318             if ($testtype == DELETE_EXTENSION) {
319                 $message->delete_extension(
320                     TLSProxy::Message::EXT_PSK_KEX_MODES);
321             } else {
322                 $message->set_extension(
323                     TLSProxy::Message::EXT_PSK_KEX_MODES, $ext);
324             }
325
326             $message->repack();
327         }
328     }
329 }