Reset executable bits on files where not needed.
[openssl.git] / test / recipes / 70-test_sslextension.t
1 #! /usr/bin/env perl
2 # Copyright 2015-2016 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_sslextension";
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 TLS enabled"
27     if alldisabled(available_protocols("tls"));
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     \&extension_filter,
32     cmdstr(app(["openssl"]), display => 1),
33     srctop_file("apps", "server.pem"),
34     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
35 );
36
37 # Test 1: Sending a zero length extension block should pass
38 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
39 plan tests => 3;
40 ok(TLSProxy::Message->success, "Zero extension length test");
41
42 sub extension_filter
43 {
44     my $proxy = shift;
45
46     # We're only interested in the initial ClientHello
47     if ($proxy->flight != 0) {
48         return;
49     }
50
51     foreach my $message (@{$proxy->message_list}) {
52         if ($message->mt == TLSProxy::Message::MT_CLIENT_HELLO) {
53             # Remove all extensions and set the extension len to zero
54             $message->extension_data({});
55             $message->extensions_len(0);
56             # Extensions have been removed so make sure we don't try to use them
57             $message->process_extensions();
58
59             $message->repack();
60         }
61     }
62 }
63
64 # Test 2-3: Sending a duplicate extension should fail.
65 sub inject_duplicate_extension
66 {
67   my ($proxy, $message_type) = @_;
68
69     foreach my $message (@{$proxy->message_list}) {
70         if ($message->mt == $message_type) {
71           my %extensions = %{$message->extension_data};
72             # Add a duplicate (unknown) extension.
73             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
74             $message->set_extension(TLSProxy::Message::EXT_DUPLICATE_EXTENSION, "");
75             $message->repack();
76         }
77     }
78 }
79
80 sub inject_duplicate_extension_clienthello
81 {
82     my $proxy = shift;
83
84     # We're only interested in the initial ClientHello
85     if ($proxy->flight != 0) {
86         return;
87     }
88
89     inject_duplicate_extension($proxy, TLSProxy::Message::MT_CLIENT_HELLO);
90 }
91
92 sub inject_duplicate_extension_serverhello
93 {
94     my $proxy = shift;
95
96     # We're only interested in the initial ServerHello
97     if ($proxy->flight != 1) {
98         return;
99     }
100
101     inject_duplicate_extension($proxy, TLSProxy::Message::MT_SERVER_HELLO);
102 }
103
104 $proxy->clear();
105 $proxy->filter(\&inject_duplicate_extension_clienthello);
106 $proxy->start();
107 ok(TLSProxy::Message->fail(), "Duplicate ClientHello extension");
108
109 $proxy->clear();
110 $proxy->filter(\&inject_duplicate_extension_serverhello);
111 $proxy->start();
112 ok(TLSProxy::Message->fail(), "Duplicate ServerHello extension");