xref: /freebsd/crypto/openssl/test/recipes/70-test_tls13cookie.t (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1#! /usr/bin/env perl
2# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (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") || (disabled("ec") && disabled("dh"));
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") if !disabled("ec");
50$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
51plan tests => 2;
52SKIP: {
53    skip "EC disabled", 1, if disabled("ec");
54    ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
55}
56
57
58
59#Test 2: Same as test 1 but should also work where a new key_share is also
60#        required
61$testtype = COOKIE_AND_KEY_SHARE;
62$proxy->clear();
63if (disabled("ec")) {
64    $proxy->clientflags("-curves ffdhe3072:ffdhe2048");
65    $proxy->serverflags("-curves ffdhe2048");
66} else {
67    $proxy->clientflags("-curves P-256:X25519");
68    $proxy->serverflags("-curves X25519");
69}
70$proxy->start();
71ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
72
73sub cookie_filter
74{
75    my $proxy = shift;
76
77    # We're only interested in the HRR and both ClientHellos
78    return if ($proxy->flight > 2);
79
80    my $ext = pack "C8",
81        0x00, 0x06, #Cookie Length
82        0x00, 0x01, #Dummy cookie data (6 bytes)
83        0x02, 0x03,
84        0x04, 0x05;
85
86    foreach my $message (@{$proxy->message_list}) {
87        if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
88                && ${$message->records}[0]->flight == 1) {
89            $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE)
90                if ($testtype == COOKIE_ONLY);
91            $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
92            $message->repack();
93        } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
94            if (${$message->records}[0]->flight == 0) {
95                if ($testtype == COOKIE_ONLY) {
96                    my $ext = pack "C7",
97                        0x00, 0x05, #List Length
98                        0x00, 0x17, #P-256
99                        0x00, 0x01, #key_exchange data length
100                        0xff;       #Dummy key_share data
101                    # Trick the server into thinking we got an unacceptable
102                    # key_share
103                    $message->set_extension(
104                        TLSProxy::Message::EXT_KEY_SHARE, $ext);
105                    $message->repack();
106                }
107            } else {
108                #cmp can behave differently dependent on locale
109                no locale;
110                my $cookie =
111                    $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
112
113                return if !defined($cookie);
114
115                return if ($cookie cmp $ext) != 0;
116
117                $cookieseen = 1;
118            }
119        }
120    }
121}
122