1#! /usr/bin/env perl
2# Copyright 2017-2018 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
9use strict;
10use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11use OpenSSL::Test::Utils;
12use TLSProxy::Proxy;
13
14my $test_name = "test_tls13cookie";
15setup($test_name);
16
17plan skip_all => "TLSProxy isn't usable on $^O"
18    if $^O =~ /^(VMS)$/;
19
20plan skip_all => "$test_name needs the dynamic engine feature enabled"
21    if disabled("engine") || disabled("dynamic-engine");
22
23plan skip_all => "$test_name needs the sock feature enabled"
24    if disabled("sock");
25
26plan skip_all => "$test_name needs TLS1.3 enabled"
27    if disabled("tls1_3");
28
29$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30
31use constant {
32    COOKIE_ONLY => 0,
33    COOKIE_AND_KEY_SHARE => 1
34};
35
36my $proxy = TLSProxy::Proxy->new(
37    undef,
38    cmdstr(app(["openssl"]), display => 1),
39    srctop_file("apps", "server.pem"),
40    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
41);
42
43my $cookieseen = 0;
44my $testtype;
45
46#Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello
47$testtype = COOKIE_ONLY;
48$proxy->filter(\&cookie_filter);
49$proxy->serverflags("-curves X25519");
50$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
51plan tests => 2;
52ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
53
54#Test 2: Same as test 1 but should also work where a new key_share is also
55#        required
56$testtype = COOKIE_AND_KEY_SHARE;
57$proxy->clear();
58$proxy->clientflags("-curves P-256:X25519");
59$proxy->serverflags("-curves X25519");
60$proxy->start();
61ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
62
63sub cookie_filter
64{
65    my $proxy = shift;
66
67    # We're only interested in the HRR and both ClientHellos
68    return if ($proxy->flight > 2);
69
70    my $ext = pack "C8",
71        0x00, 0x06, #Cookie Length
72        0x00, 0x01, #Dummy cookie data (6 bytes)
73        0x02, 0x03,
74        0x04, 0x05;
75
76    foreach my $message (@{$proxy->message_list}) {
77        if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
78                && ${$message->records}[0]->flight == 1) {
79            $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE)
80                if ($testtype == COOKIE_ONLY);
81            $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
82            $message->repack();
83        } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
84            if (${$message->records}[0]->flight == 0) {
85                if ($testtype == COOKIE_ONLY) {
86                    my $ext = pack "C7",
87                        0x00, 0x05, #List Length
88                        0x00, 0x17, #P-256
89                        0x00, 0x01, #key_exchange data length
90                        0xff;       #Dummy key_share data
91                    # Trick the server into thinking we got an unacceptable
92                    # key_share
93                    $message->set_extension(
94                        TLSProxy::Message::EXT_KEY_SHARE, $ext);
95                    $message->repack();
96                }
97            } else {
98                #cmp can behave differently dependent on locale
99                no locale;
100                my $cookie =
101                    $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
102
103                return if !defined($cookie);
104
105                return if ($cookie cmp $ext) != 0;
106
107                $cookieseen = 1;
108            }
109        }
110    }
111}
112