70-test_sslextension.t 3.1 KB
Newer Older
R
Rich Salz 已提交
1 2
#! /usr/bin/env perl
# Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
M
Matt Caswell 已提交
3
#
R
Rich Salz 已提交
4 5 6 7
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
M
Matt Caswell 已提交
8 9

use strict;
10
use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file bldtop_dir/;
11
use OpenSSL::Test::Utils;
M
Matt Caswell 已提交
12 13
use TLSProxy::Proxy;

14 15 16
my $test_name = "test_sslextension";
setup($test_name);

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

M
Matt Caswell 已提交
23 24 25
plan skip_all => "$test_name needs the sock feature enabled"
    if disabled("sock");

26
$ENV{OPENSSL_ia32cap} = '~0x200000200000000';
M
Matt Caswell 已提交
27 28
my $proxy = TLSProxy::Proxy->new(
    \&extension_filter,
29
    cmdstr(app(["openssl"]), display => 1),
30 31
    srctop_file("apps", "server.pem"),
    (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
M
Matt Caswell 已提交
32 33
);

E
Emilia Kasper 已提交
34
plan tests => 3;
35

E
Emilia Kasper 已提交
36
# Test 1: Sending a zero length extension block should pass
M
Matt Caswell 已提交
37
$proxy->start();
38
ok(TLSProxy::Message->success, "Zero extension length test");
M
Matt Caswell 已提交
39 40 41 42 43 44 45 46 47 48 49 50

sub extension_filter
{
    my $proxy = shift;

    # We're only interested in the initial ClientHello
    if ($proxy->flight != 0) {
        return;
    }

    foreach my $message (@{$proxy->message_list}) {
        if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
E
Emilia Kasper 已提交
51
            # Remove all extensions and set the extension len to zero
M
Matt Caswell 已提交
52 53
            $message->extension_data({});
            $message->extensions_len(0);
E
Emilia Kasper 已提交
54
            # Extensions have been removed so make sure we don't try to use them
M
Matt Caswell 已提交
55 56 57 58 59 60
            $message->process_extensions();

            $message->repack();
        }
    }
}
E
Emilia Kasper 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

# Test 2-3: Sending a duplicate extension should fail.
sub inject_duplicate_extension
{
  my ($proxy, $message_type) = @_;

    foreach my $message (@{$proxy->message_list}) {
        if ($message->mt == $message_type) {
          my %extensions = %{$message->extension_data};
            # Add a duplicate (unknown) extension.
            $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
            $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
            $message->repack();
        }
    }
}

sub inject_duplicate_extension_clienthello
{
    my $proxy = shift;

    # We're only interested in the initial ClientHello
    if ($proxy->flight != 0) {
        return;
    }

    inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
}

sub inject_duplicate_extension_serverhello
{
    my $proxy = shift;

    # We're only interested in the initial ServerHello
    if ($proxy->flight != 1) {
        return;
    }

    inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
}

$proxy->clear();
$proxy->filter(\&inject_duplicate_extension_clienthello);
$proxy->start();
ok(TLSProxy::Message->fail(), "Duplicate ClientHello extension");

$proxy->clear();
$proxy->filter(\&inject_duplicate_extension_serverhello);
$proxy->start();
ok(TLSProxy::Message->fail(), "Duplicate ServerHello extension");