xref: /freebsd/crypto/openssl/test/recipes/70-test_tls13cookie.t (revision 4b15965daa99044daf184221b7c283bf7f2d7e66)
1#! /usr/bin/env perl
2# Copyright 2017-2025 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
29use constant {
30    COOKIE_ONLY => 0,
31    COOKIE_AND_KEY_SHARE => 1
32};
33
34my $proxy = TLSProxy::Proxy->new(
35    undef,
36    cmdstr(app(["openssl"]), display => 1),
37    srctop_file("apps", "server.pem"),
38    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
39);
40
41my $cookieseen = 0;
42my $testtype;
43
44#Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello
45#        (when a key share is required)
46$testtype = COOKIE_AND_KEY_SHARE;
47$proxy->filter(\&cookie_filter);
48if (disabled("ecx")) {
49    $proxy->clientflags("-curves ffdhe3072:ffdhe2048");
50    $proxy->serverflags("-curves ffdhe2048");
51} else {
52    $proxy->clientflags("-curves P-256:X25519");
53    $proxy->serverflags("-curves X25519");
54}
55$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
56plan tests => 2;
57ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
58
59#Test 2: Inserting a cookie into an HRR should see it echoed in the ClientHello
60#        (without a key share required)
61SKIP: {
62    skip "ECX disabled", 1, if (disabled("ecx"));
63    $testtype = COOKIE_ONLY;
64    $proxy->clear();
65    $proxy->serverflags("-curves X25519");
66    $proxy->clientflags("-curves X25519:secp256r1");
67    $proxy->start();
68    ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
69}
70
71sub cookie_filter
72{
73    my $proxy = shift;
74
75    # We're only interested in the HRR and both ClientHellos
76    return if ($proxy->flight > 2);
77
78    my $ext = pack "C8",
79        0x00, 0x06, #Cookie Length
80        0x00, 0x01, #Dummy cookie data (6 bytes)
81        0x02, 0x03,
82        0x04, 0x05;
83
84    foreach my $message (@{$proxy->message_list}) {
85        if ($message->mt == TLSProxy::Message::MT_SERVER_HELLO
86                && ${$message->records}[0]->flight == 1) {
87            $message->delete_extension(TLSProxy::Message::EXT_KEY_SHARE)
88                if ($testtype == COOKIE_ONLY);
89            $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
90            $message->repack();
91        } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
92            if (${$message->records}[0]->flight == 0) {
93                if ($testtype == COOKIE_ONLY) {
94                    my $ext = pack "C7",
95                        0x00, 0x05, #List Length
96                        0x00, 0x17, #P-256
97                        0x00, 0x01, #key_exchange data length
98                        0xff;       #Dummy key_share data
99                    # Trick the server into thinking we got an unacceptable
100                    # key_share
101                    $message->set_extension(
102                        TLSProxy::Message::EXT_KEY_SHARE, $ext);
103                    $message->repack();
104                }
105            } else {
106                #cmp can behave differently dependent on locale
107                no locale;
108                my $cookie =
109                    $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
110
111                return if !defined($cookie);
112
113                return if ($cookie cmp $ext) != 0;
114
115                $cookieseen = 1;
116            }
117        }
118    }
119}
120