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