Add some badly formatted compression methods tests
[oweals/openssl.git] / test / recipes / 70-test_comp.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
15 my $test_name = "test_comp";
16 setup($test_name);
17
18 plan skip_all => "TLSProxy isn't usable on $^O"
19     if $^O =~ /^(VMS|MSWin32)$/;
20
21 plan skip_all => "$test_name needs the dynamic engine feature enabled"
22     if disabled("engine") || disabled("dynamic-engine");
23
24 plan skip_all => "$test_name needs the sock feature enabled"
25     if disabled("sock");
26
27 plan skip_all => "$test_name needs TLSv1.3 enabled"
28     if disabled("tls1_3");
29
30 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
31 $ENV{CTLOG_FILE} = srctop_file("test", "ct", "log_list.conf");
32
33 use constant {
34     MULTIPLE_COMPRESSIONS => 0,
35     NON_NULL_COMPRESSION => 1
36 };
37 my $testtype;
38
39 my $proxy = TLSProxy::Proxy->new(
40     undef,
41     cmdstr(app(["openssl"]), display => 1),
42     srctop_file("apps", "server.pem"),
43     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
44 );
45
46 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
47 plan tests => 4;
48
49 SKIP: {
50     skip "TLSv1.2 disabled", 2 if disabled("tls1_2");
51     #Test 1: Check that sending multiple compression methods in a TLSv1.2
52     #        ClientHello succeeds
53     $proxy->clear();
54     $proxy->filter(\&add_comp_filter);
55     $proxy->clientflags("-no_tls1_3");
56     $testtype = MULTIPLE_COMPRESSIONS;
57     $proxy->start();
58     ok(TLSProxy::Message->success(), "Non null compression");
59
60     #Test 2: NULL compression method must be present in TLSv1.2
61     $proxy->clear();
62     $proxy->filter(\&add_comp_filter);
63     $proxy->clientflags("-no_tls1_3");
64     $testtype = NON_NULL_COMPRESSION;
65     $proxy->start();
66     ok(TLSProxy::Message->fail(), "NULL compression missing");
67 }
68
69 SKIP: {
70     skip "TLSv1.3 disabled", 2 if disabled("tls1_3");
71     #Test 3: Check that sending multiple compression methods in a TLSv1.3
72     #        ClientHello fails
73     $proxy->clear();
74     $proxy->filter(\&add_comp_filter);
75     $testtype = MULTIPLE_COMPRESSIONS;
76     $proxy->start();
77     ok(TLSProxy::Message->fail(), "Non null compression (TLSv1.3)");
78
79     #Test 4: NULL compression method must be present in TLSv1.2
80     $proxy->clear();
81     $proxy->filter(\&add_comp_filter);
82     $proxy->clientflags("-no_tls1_3");
83     $testtype = NON_NULL_COMPRESSION;
84     $proxy->start();
85     ok(TLSProxy::Message->fail(), "NULL compression missing (TLSv1.3)");
86 }
87
88 sub add_comp_filter
89 {
90     my $proxy = shift;
91     my $flight;
92     my $message;
93     my @comp;
94
95     # Only look at the ClientHello
96     return if $proxy->flight != 0;
97
98     $message = ${$proxy->message_list}[0];
99
100     return if (!defined $message
101                || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
102
103     if ($testtype == MULTIPLE_COMPRESSIONS) {
104         @comp = (
105             0x00, #Null compression method
106             0xff); #Unknown compression
107     } else {
108         @comp = (0xff); #Unknown compression
109     }
110     $message->comp_meths(\@comp);
111     $message->comp_meth_len(scalar @comp);
112     $message->repack();
113 }