spelling fixes, just comments and readme.
[openssl.git] / test / recipes / 70-test_sslrecords.t
1 #! /usr/bin/env perl
2 # Copyright 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_sslrecords";
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 TLSv1.2 enabled"
27     if disabled("tls1_2");
28
29 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
30 my $proxy = TLSProxy::Proxy->new(
31     \&add_empty_recs_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: Injecting out of context empty records should fail
38 my $content_type = TLSProxy::Record::RT_APPLICATION_DATA;
39 my $inject_recs_num = 1;
40 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
41 plan tests => 4;
42 ok(TLSProxy::Message->fail(), "Out of context empty records test");
43
44 #Test 2: Injecting in context empty records should succeed
45 $proxy->clear();
46 $content_type = TLSProxy::Record::RT_HANDSHAKE;
47 $proxy->start();
48 ok(TLSProxy::Message->success(), "In context empty records test");
49
50 #Test 3: Injecting too many in context empty records should fail
51 $proxy->clear();
52 #We allow 32 consecutive in context empty records
53 $inject_recs_num = 33;
54 $proxy->start();
55 ok(TLSProxy::Message->fail(), "Too many in context empty records test");
56
57 #Test 4: Injecting a fragmented fatal alert should fail. We actually expect no
58 #        alerts to be sent from either side because *we* injected the fatal
59 #        alert, i.e. this will look like a disorderly close
60 $proxy->clear();
61 $proxy->filter(\&add_frag_alert_filter);
62 $proxy->start();
63 ok(!TLSProxy::Message->end(), "Fragmented alert records test");
64
65 sub add_empty_recs_filter
66 {
67     my $proxy = shift;
68
69     # We're only interested in the initial ClientHello
70     if ($proxy->flight != 0) {
71         return;
72     }
73
74     for (my $i = 0; $i < $inject_recs_num; $i++) {
75         my $record = TLSProxy::Record->new(
76             0,
77             $content_type,
78             TLSProxy::Record::VERS_TLS_1_2,
79             0,
80             0,
81             0,
82             "",
83             ""
84         );
85
86         push @{$proxy->record_list}, $record;
87     }
88 }
89
90 sub add_frag_alert_filter
91 {
92     my $proxy = shift;
93     my $byte;
94
95     # We're only interested in the initial ClientHello
96     if ($proxy->flight != 0) {
97         return;
98     }
99
100     # Add a zero length fragment first
101     #my $record = TLSProxy::Record->new(
102     #    0,
103     #    TLSProxy::Record::RT_ALERT,
104     #    TLSProxy::Record::VERS_TLS_1_2,
105     #    0,
106     #    0,
107     #    0,
108     #    "",
109     #    ""
110     #);
111     #push @{$proxy->record_list}, $record;
112
113     # Now add the alert level (Fatal) as a separate record
114     $byte = pack('C', TLSProxy::Message::AL_LEVEL_FATAL);
115     my $record = TLSProxy::Record->new(
116         0,
117         TLSProxy::Record::RT_ALERT,
118         TLSProxy::Record::VERS_TLS_1_2,
119         1,
120         1,
121         1,
122         $byte,
123         $byte
124     );
125     push @{$proxy->record_list}, $record;
126
127     # And finally the description (Unexpected message) in a third record
128     $byte = pack('C', TLSProxy::Message::AL_DESC_UNEXPECTED_MESSAGE);
129     $record = TLSProxy::Record->new(
130         0,
131         TLSProxy::Record::RT_ALERT,
132         TLSProxy::Record::VERS_TLS_1_2,
133         1,
134         1,
135         1,
136         $byte,
137         $byte
138     );
139     push @{$proxy->record_list}, $record;
140 }