Fix a few internals tests
[openssl.git] / test / recipes / 70-test_tls13cookie.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 bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use TLSProxy::Proxy;
13
14 my $test_name = "test_tls13cookie";
15 setup($test_name);
16
17 plan skip_all => "TLSProxy isn't usable on $^O"
18     if $^O =~ /^(VMS|MSWin32)$/;
19
20 plan skip_all => "$test_name needs the dynamic engine feature enabled"
21     if disabled("engine") || disabled("dynamic-engine");
22
23 plan skip_all => "$test_name needs the sock feature enabled"
24     if disabled("sock");
25
26 plan skip_all => "$test_name needs TLS1.3 enabled"
27     if disabled("tls1_3");
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30
31 my $proxy = TLSProxy::Proxy->new(
32     undef,
33     cmdstr(app(["openssl"]), display => 1),
34     srctop_file("apps", "server.pem"),
35     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
36 );
37
38 my $cookieseen = 0;
39
40 #Test 1: Inserting a cookie into an HRR should see it echoed in the ClientHello
41 $proxy->filter(\&cookie_filter);
42 $proxy->serverflags("-curves P-256");
43 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
44 plan tests => 1;
45 ok(TLSProxy::Message->success() && $cookieseen == 1, "Cookie seen");
46
47 sub cookie_filter
48 {
49     my $proxy = shift;
50
51     # We're only interested in the HRR and subsequent ClientHello
52     if ($proxy->flight != 1 && $proxy->flight != 2) {
53         return;
54     }
55
56     my $ext = pack "C8",
57         0x00, 0x06, #Cookie Length
58         0x00, 0x01, #Dummy cookie data (6 bytes)
59         0x02, 0x03,
60         0x04, 0x05;
61
62     foreach my $message (@{$proxy->message_list}) {
63         if ($message->mt == TLSProxy::Message::MT_HELLO_RETRY_REQUEST) {
64
65             $message->set_extension(TLSProxy::Message::EXT_COOKIE, $ext);
66             $message->repack();
67         } elsif ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO
68                     && ${$message->records}[0]->flight == 2) {
69             #cmp can behave differently dependent on locale
70             no locale;
71             my $cookie =
72                 $message->extension_data->{TLSProxy::Message::EXT_COOKIE};
73
74             return if !defined($cookie);
75
76             return if ($cookie cmp $ext) != 0;
77
78             $cookieseen = 1;
79         }
80     }
81 }