TLSProxy update
[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 use constant {
61     EXT_ENCRYPT_THEN_MAC => 22,
62     EXT_SESSION_TICKET => 35
63 };
64
65 sub new
66 {
67     my $class = shift;
68     my ($server,
69         $data,
70         $records,
71         $startoffset,
72         $message_frag_lens) = @_;
73
74     my $self = $class->SUPER::new(
75         $server,
76         1,
77         $data,
78         $records,
79         $startoffset,
80         $message_frag_lens);
81
82     $self->{client_version} = 0;
83     $self->{random} = [];
84     $self->{session_id_len} = 0;
85     $self->{session} = "";
86     $self->{ciphersuite_len} = 0;
87     $self->{ciphersuites} = [];
88     $self->{comp_meth_len} = 0;
89     $self->{comp_meths} = [];
90     $self->{extensions_len} = 0;
91     $self->{extensions_data} = "";
92
93     return $self;
94 }
95
96 sub parse
97 {
98     my $self = shift;
99     my $ptr = 2;
100     my ($client_version) = unpack('n', $self->data);
101     my $random = substr($self->data, $ptr, 32);
102     $ptr += 32;
103     my $session_id_len = unpack('C', substr($self->data, $ptr));
104     $ptr++;
105     my $session = substr($self->data, $ptr, $session_id_len);
106     $ptr += $session_id_len;
107     my $ciphersuite_len = unpack('n', substr($self->data, $ptr));
108     $ptr += 2;
109     my @ciphersuites = unpack('n*', substr($self->data, $ptr,
110                                            $ciphersuite_len));
111     $ptr += $ciphersuite_len;
112     my $comp_meth_len = unpack('C', substr($self->data, $ptr));
113     $ptr++;
114     my @comp_meths = unpack('C*', substr($self->data, $ptr, $comp_meth_len));
115     $ptr += $comp_meth_len;
116     my $extensions_len = unpack('n', substr($self->data, $ptr));
117     $ptr += 2;
118     #For now we just deal with this as a block of data. In the future we will
119     #want to parse this
120     my $extension_data = substr($self->data, $ptr);
121
122     if (length($extension_data) != $extensions_len) {
123         die "Invalid extension length\n";
124     }
125     my %extensions = ();
126     while (length($extension_data) >= 4) {
127         my ($type, $size) = unpack("nn", $extension_data);
128         my $extdata = substr($extension_data, 4, $size);
129         $extension_data = substr($extension_data, 4 + $size);
130         $extensions{$type} = $extdata;
131     }
132
133     $self->client_version($client_version);
134     $self->random($random);
135     $self->session_id_len($session_id_len);
136     $self->session($session);
137     $self->ciphersuite_len($ciphersuite_len);
138     $self->ciphersuites(\@ciphersuites);
139     $self->comp_meth_len($comp_meth_len);
140     $self->comp_meths(\@comp_meths);
141     $self->extensions_len($extensions_len);
142     $self->extension_data(\%extensions);
143
144     $self->process_extensions();
145
146     print "    Client Version:".$client_version."\n";
147     print "    Session ID Len:".$session_id_len."\n";
148     print "    Ciphersuite len:".$ciphersuite_len."\n";
149     print "    Compression Method Len:".$comp_meth_len."\n";
150     print "    Extensions Len:".$extensions_len."\n";
151 }
152
153 #Perform any actions necessary based on the extensions we've seen
154 sub process_extensions
155 {
156     my $self = shift;
157     my %extensions = %{$self->extension_data};
158
159     #Clear any state from a previous run
160     TLSProxy::Record->etm(0);
161
162     if (exists $extensions{&EXT_ENCRYPT_THEN_MAC}) {
163         TLSProxy::Record->etm(1);
164     }
165 }
166
167 #Reconstruct the on-the-wire message data following changes
168 sub set_message_contents
169 {
170     my $self = shift;
171     my $data;
172     my $extensions = "";
173
174     $data = pack('n', $self->client_version);
175     $data .= $self->random;
176     $data .= pack('C', $self->session_id_len);
177     $data .= $self->session;
178     $data .= pack('n', $self->ciphersuite_len);
179     $data .= pack("n*", @{$self->ciphersuites});
180     $data .= pack('C', $self->comp_meth_len);
181     $data .= pack("C*", @{$self->comp_meths});
182
183     foreach my $key (keys %{$self->extension_data}) {
184         my $extdata = ${$self->extension_data}{$key};
185         $extensions .= pack("n", $key);
186         $extensions .= pack("n", length($extdata));
187         $extensions .= $extdata;
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 delete_extension
278 {
279     my ($self, $ext_type) = @_;
280     delete $self->{extension_data}{$ext_type};
281 }
282 1;