1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ciuse strict;
10e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT cmdstr srctop_file srctop_dir bldtop_dir/;
11e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
12e1051a39Sopenharmony_ciuse File::Temp qw(tempfile);
13e1051a39Sopenharmony_ciuse TLSProxy::Proxy;
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cimy $test_name = "test_comp";
16e1051a39Sopenharmony_cisetup($test_name);
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ciplan skip_all => "TLSProxy isn't usable on $^O"
19e1051a39Sopenharmony_ci    if $^O =~ /^(VMS)$/;
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the dynamic engine feature enabled"
22e1051a39Sopenharmony_ci    if disabled("engine") || disabled("dynamic-engine");
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ciplan skip_all => "$test_name needs the sock feature enabled"
25e1051a39Sopenharmony_ci    if disabled("sock");
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciplan skip_all => "$test_name needs TLSv1.3 or TLSv1.2 enabled"
28e1051a39Sopenharmony_ci    if disabled("tls1_3") && disabled("tls1_2");
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ci$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciuse constant {
33e1051a39Sopenharmony_ci    MULTIPLE_COMPRESSIONS => 0,
34e1051a39Sopenharmony_ci    NON_NULL_COMPRESSION => 1
35e1051a39Sopenharmony_ci};
36e1051a39Sopenharmony_cimy $testtype;
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_cimy $proxy = TLSProxy::Proxy->new(
39e1051a39Sopenharmony_ci    undef,
40e1051a39Sopenharmony_ci    cmdstr(app(["openssl"]), display => 1),
41e1051a39Sopenharmony_ci    srctop_file("apps", "server.pem"),
42e1051a39Sopenharmony_ci    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
43e1051a39Sopenharmony_ci);
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_ci$proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
46e1051a39Sopenharmony_ciplan tests => 4;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ciSKIP: {
49e1051a39Sopenharmony_ci    skip "TLSv1.2 disabled", 2 if disabled("tls1_2");
50e1051a39Sopenharmony_ci    #Test 1: Check that sending multiple compression methods in a TLSv1.2
51e1051a39Sopenharmony_ci    #        ClientHello succeeds
52e1051a39Sopenharmony_ci    $proxy->clear();
53e1051a39Sopenharmony_ci    $proxy->filter(\&add_comp_filter);
54e1051a39Sopenharmony_ci    $proxy->clientflags("-no_tls1_3");
55e1051a39Sopenharmony_ci    $testtype = MULTIPLE_COMPRESSIONS;
56e1051a39Sopenharmony_ci    $proxy->start();
57e1051a39Sopenharmony_ci    ok(TLSProxy::Message->success(), "Non null compression");
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ci    #Test 2: NULL compression method must be present in TLSv1.2
60e1051a39Sopenharmony_ci    $proxy->clear();
61e1051a39Sopenharmony_ci    $proxy->clientflags("-no_tls1_3");
62e1051a39Sopenharmony_ci    $testtype = NON_NULL_COMPRESSION;
63e1051a39Sopenharmony_ci    $proxy->start();
64e1051a39Sopenharmony_ci    ok(TLSProxy::Message->fail(), "NULL compression missing");
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ciSKIP: {
68e1051a39Sopenharmony_ci    skip "TLSv1.3 disabled", 2
69e1051a39Sopenharmony_ci        if disabled("tls1_3") || (disabled("ec") && disabled("dh"));
70e1051a39Sopenharmony_ci    #Test 3: Check that sending multiple compression methods in a TLSv1.3
71e1051a39Sopenharmony_ci    #        ClientHello fails
72e1051a39Sopenharmony_ci    $proxy->clear();
73e1051a39Sopenharmony_ci    $proxy->filter(\&add_comp_filter);
74e1051a39Sopenharmony_ci    $testtype = MULTIPLE_COMPRESSIONS;
75e1051a39Sopenharmony_ci    $proxy->start();
76e1051a39Sopenharmony_ci    ok(TLSProxy::Message->fail(), "Non null compression (TLSv1.3)");
77e1051a39Sopenharmony_ci
78e1051a39Sopenharmony_ci    #Test 4: NULL compression method must be present in TLSv1.3
79e1051a39Sopenharmony_ci    $proxy->clear();
80e1051a39Sopenharmony_ci    $testtype = NON_NULL_COMPRESSION;
81e1051a39Sopenharmony_ci    $proxy->start();
82e1051a39Sopenharmony_ci    ok(TLSProxy::Message->fail(), "NULL compression missing (TLSv1.3)");
83e1051a39Sopenharmony_ci}
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_cisub add_comp_filter
86e1051a39Sopenharmony_ci{
87e1051a39Sopenharmony_ci    my $proxy = shift;
88e1051a39Sopenharmony_ci    my $flight;
89e1051a39Sopenharmony_ci    my $message;
90e1051a39Sopenharmony_ci    my @comp;
91e1051a39Sopenharmony_ci
92e1051a39Sopenharmony_ci    # Only look at the ClientHello
93e1051a39Sopenharmony_ci    return if $proxy->flight != 0;
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci    $message = ${$proxy->message_list}[0];
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_ci    return if (!defined $message
98e1051a39Sopenharmony_ci               || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci    if ($testtype == MULTIPLE_COMPRESSIONS) {
101e1051a39Sopenharmony_ci        @comp = (
102e1051a39Sopenharmony_ci            0x00, #Null compression method
103e1051a39Sopenharmony_ci            0xff); #Unknown compression
104e1051a39Sopenharmony_ci    } elsif ($testtype == NON_NULL_COMPRESSION) {
105e1051a39Sopenharmony_ci        @comp = (0xff); #Unknown compression
106e1051a39Sopenharmony_ci    }
107e1051a39Sopenharmony_ci    $message->comp_meths(\@comp);
108e1051a39Sopenharmony_ci    $message->comp_meth_len(scalar @comp);
109e1051a39Sopenharmony_ci    $message->repack();
110e1051a39Sopenharmony_ci}
111