TLS: reject duplicate extensions
[openssl.git] / util / TLSProxy / ClientHello.pm
1 # Written by Matt Caswell for the OpenSSL project.
2 # ====================================================================
3 # Copyright (c) 1998-2015 The OpenSSL Project.  All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in
14 #    the documentation and/or other materials provided with the
15 #    distribution.
16 #
17 # 3. All advertising materials mentioning features or use of this
18 #    software must display the following acknowledgment:
19 #    "This product includes software developed by the OpenSSL Project
20 #    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 #
22 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 #    endorse or promote products derived from this software without
24 #    prior written permission. For written permission, please contact
25 #    openssl-core@openssl.org.
26 #
27 # 5. Products derived from this software may not be called "OpenSSL"
28 #    nor may "OpenSSL" appear in their names without prior written
29 #    permission of the OpenSSL Project.
30 #
31 # 6. Redistributions of any form whatsoever must retain the following
32 #    acknowledgment:
33 #    "This product includes software developed by the OpenSSL Project
34 #    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 #
36 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 # OF THE POSSIBILITY OF SUCH DAMAGE.
48 # ====================================================================
49 #
50 # This product includes cryptographic software written by Eric Young
51 # (eay@cryptsoft.com).  This product includes software written by Tim
52 # Hudson (tjh@cryptsoft.com).
53
54 use strict;
55
56 package TLSProxy::ClientHello;
57
58 use parent 'TLSProxy::Message';
59
60 sub new
61 {
62     my $class = shift;
63     my ($server,
64         $data,
65         $records,
66         $startoffset,
67         $message_frag_lens) = @_;
68
69     my $self = $class->SUPER::new(
70         $server,
71         1,
72         $data,
73         $records,
74         $startoffset,
75         $message_frag_lens);
76
77     $self->{client_version} = 0;
78     $self->{random} = [];
79     $self->{session_id_len} = 0;
80     $self->{session} = "";
81     $self->{ciphersuite_len} = 0;
82     $self->{ciphersuites} = [];
83     $self->{comp_meth_len} = 0;
84     $self->{comp_meths} = [];
85     $self->{extensions_len} = 0;
86     $self->{extension_data} = "";
87
88     return $self;
89 }
90
91 sub parse
92 {
93     my $self = shift;
94     my $ptr = 2;
95     my ($client_version) = unpack('n', $self->data);
96     my $random = substr($self->data, $ptr, 32);
97     $ptr += 32;
98     my $session_id_len = unpack('C', substr($self->data, $ptr));
99     $ptr++;
100     my $session = substr($self->data, $ptr, $session_id_len);
101     $ptr += $session_id_len;
102     my $ciphersuite_len = unpack('n', substr($self->data, $ptr));
103     $ptr += 2;
104     my @ciphersuites = unpack('n*', substr($self->data, $ptr,
105                                            $ciphersuite_len));
106     $ptr += $ciphersuite_len;
107     my $comp_meth_len = unpack('C', substr($self->data, $ptr));
108     $ptr++;
109     my @comp_meths = unpack('C*', substr($self->data, $ptr, $comp_meth_len));
110     $ptr += $comp_meth_len;
111     my $extensions_len = unpack('n', substr($self->data, $ptr));
112     $ptr += 2;
113     #For now we just deal with this as a block of data. In the future we will
114     #want to parse this
115     my $extension_data = substr($self->data, $ptr);
116
117     if (length($extension_data) != $extensions_len) {
118         die "Invalid extension length\n";
119     }
120     my %extensions = ();
121     while (length($extension_data) >= 4) {
122         my ($type, $size) = unpack("nn", $extension_data);
123         my $extdata = substr($extension_data, 4, $size);
124         $extension_data = substr($extension_data, 4 + $size);
125         $extensions{$type} = $extdata;
126     }
127
128     $self->client_version($client_version);
129     $self->random($random);
130     $self->session_id_len($session_id_len);
131     $self->session($session);
132     $self->ciphersuite_len($ciphersuite_len);
133     $self->ciphersuites(\@ciphersuites);
134     $self->comp_meth_len($comp_meth_len);
135     $self->comp_meths(\@comp_meths);
136     $self->extensions_len($extensions_len);
137     $self->extension_data(\%extensions);
138
139     $self->process_extensions();
140
141     print "    Client Version:".$client_version."\n";
142     print "    Session ID Len:".$session_id_len."\n";
143     print "    Ciphersuite len:".$ciphersuite_len."\n";
144     print "    Compression Method Len:".$comp_meth_len."\n";
145     print "    Extensions Len:".$extensions_len."\n";
146 }
147
148 #Perform any actions necessary based on the extensions we've seen
149 sub process_extensions
150 {
151     my $self = shift;
152     my %extensions = %{$self->extension_data};
153
154     #Clear any state from a previous run
155     TLSProxy::Record->etm(0);
156
157     if (exists $extensions{TLSProxy::Message::EXT_ENCRYPT_THEN_MAC}) {
158         TLSProxy::Record->etm(1);
159     }
160 }
161
162 #Reconstruct the on-the-wire message data following changes
163 sub set_message_contents
164 {
165     my $self = shift;
166     my $data;
167     my $extensions = "";
168
169     $data = pack('n', $self->client_version);
170     $data .= $self->random;
171     $data .= pack('C', $self->session_id_len);
172     $data .= $self->session;
173     $data .= pack('n', $self->ciphersuite_len);
174     $data .= pack("n*", @{$self->ciphersuites});
175     $data .= pack('C', $self->comp_meth_len);
176     $data .= pack("C*", @{$self->comp_meths});
177
178     foreach my $key (keys %{$self->extension_data}) {
179         my $extdata = ${$self->extension_data}{$key};
180         $extensions .= pack("n", $key);
181         $extensions .= pack("n", length($extdata));
182         $extensions .= $extdata;
183         if ($key == TLSProxy::Message::EXT_DUPLICATE_EXTENSION) {
184           $extensions .= pack("n", $key);
185           $extensions .= pack("n", length($extdata));
186           $extensions .= $extdata;
187         }
188     }
189
190     $data .= pack('n', length($extensions));
191     $data .= $extensions;
192
193     $self->data($data);
194 }
195
196 #Read/write accessors
197 sub client_version
198 {
199     my $self = shift;
200     if (@_) {
201       $self->{client_version} = shift;
202     }
203     return $self->{client_version};
204 }
205 sub random
206 {
207     my $self = shift;
208     if (@_) {
209       $self->{random} = shift;
210     }
211     return $self->{random};
212 }
213 sub session_id_len
214 {
215     my $self = shift;
216     if (@_) {
217       $self->{session_id_len} = shift;
218     }
219     return $self->{session_id_len};
220 }
221 sub session
222 {
223     my $self = shift;
224     if (@_) {
225       $self->{session} = shift;
226     }
227     return $self->{session};
228 }
229 sub ciphersuite_len
230 {
231     my $self = shift;
232     if (@_) {
233       $self->{ciphersuite_len} = shift;
234     }
235     return $self->{ciphersuite_len};
236 }
237 sub ciphersuites
238 {
239     my $self = shift;
240     if (@_) {
241       $self->{ciphersuites} = shift;
242     }
243     return $self->{ciphersuites};
244 }
245 sub comp_meth_len
246 {
247     my $self = shift;
248     if (@_) {
249       $self->{comp_meth_len} = shift;
250     }
251     return $self->{comp_meth_len};
252 }
253 sub comp_meths
254 {
255     my $self = shift;
256     if (@_) {
257       $self->{comp_meths} = shift;
258     }
259     return $self->{comp_meths};
260 }
261 sub extensions_len
262 {
263     my $self = shift;
264     if (@_) {
265       $self->{extensions_len} = shift;
266     }
267     return $self->{extensions_len};
268 }
269 sub extension_data
270 {
271     my $self = shift;
272     if (@_) {
273       $self->{extension_data} = shift;
274     }
275     return $self->{extension_data};
276 }
277 sub set_extension
278 {
279     my ($self, $ext_type, $ext_data) = @_;
280     $self->{extension_data}{$ext_type} = $ext_data;
281 }
282 sub delete_extension
283 {
284     my ($self, $ext_type) = @_;
285     delete $self->{extension_data}{$ext_type};
286 }
287 1;