Use NON_EMPTY_TRANSLATION_UNIT, consistently.
[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_STATUS_REQUEST => 5,
62     EXT_ENCRYPT_THEN_MAC => 22,
63     EXT_EXTENDED_MASTER_SECRET => 23,
64     EXT_SESSION_TICKET => 35
65 };
66
67 sub new
68 {
69     my $class = shift;
70     my ($server,
71         $data,
72         $records,
73         $startoffset,
74         $message_frag_lens) = @_;
75
76     my $self = $class->SUPER::new(
77         $server,
78         1,
79         $data,
80         $records,
81         $startoffset,
82         $message_frag_lens);
83
84     $self->{client_version} = 0;
85     $self->{random} = [];
86     $self->{session_id_len} = 0;
87     $self->{session} = "";
88     $self->{ciphersuite_len} = 0;
89     $self->{ciphersuites} = [];
90     $self->{comp_meth_len} = 0;
91     $self->{comp_meths} = [];
92     $self->{extensions_len} = 0;
93     $self->{extensions_data} = "";
94
95     return $self;
96 }
97
98 sub parse
99 {
100     my $self = shift;
101     my $ptr = 2;
102     my ($client_version) = unpack('n', $self->data);
103     my $random = substr($self->data, $ptr, 32);
104     $ptr += 32;
105     my $session_id_len = unpack('C', substr($self->data, $ptr));
106     $ptr++;
107     my $session = substr($self->data, $ptr, $session_id_len);
108     $ptr += $session_id_len;
109     my $ciphersuite_len = unpack('n', substr($self->data, $ptr));
110     $ptr += 2;
111     my @ciphersuites = unpack('n*', substr($self->data, $ptr,
112                                            $ciphersuite_len));
113     $ptr += $ciphersuite_len;
114     my $comp_meth_len = unpack('C', substr($self->data, $ptr));
115     $ptr++;
116     my @comp_meths = unpack('C*', substr($self->data, $ptr, $comp_meth_len));
117     $ptr += $comp_meth_len;
118     my $extensions_len = unpack('n', substr($self->data, $ptr));
119     $ptr += 2;
120     #For now we just deal with this as a block of data. In the future we will
121     #want to parse this
122     my $extension_data = substr($self->data, $ptr);
123
124     if (length($extension_data) != $extensions_len) {
125         die "Invalid extension length\n";
126     }
127     my %extensions = ();
128     while (length($extension_data) >= 4) {
129         my ($type, $size) = unpack("nn", $extension_data);
130         my $extdata = substr($extension_data, 4, $size);
131         $extension_data = substr($extension_data, 4 + $size);
132         $extensions{$type} = $extdata;
133     }
134
135     $self->client_version($client_version);
136     $self->random($random);
137     $self->session_id_len($session_id_len);
138     $self->session($session);
139     $self->ciphersuite_len($ciphersuite_len);
140     $self->ciphersuites(\@ciphersuites);
141     $self->comp_meth_len($comp_meth_len);
142     $self->comp_meths(\@comp_meths);
143     $self->extensions_len($extensions_len);
144     $self->extension_data(\%extensions);
145
146     $self->process_extensions();
147
148     print "    Client Version:".$client_version."\n";
149     print "    Session ID Len:".$session_id_len."\n";
150     print "    Ciphersuite len:".$ciphersuite_len."\n";
151     print "    Compression Method Len:".$comp_meth_len."\n";
152     print "    Extensions Len:".$extensions_len."\n";
153 }
154
155 #Perform any actions necessary based on the extensions we've seen
156 sub process_extensions
157 {
158     my $self = shift;
159     my %extensions = %{$self->extension_data};
160
161     #Clear any state from a previous run
162     TLSProxy::Record->etm(0);
163
164     if (exists $extensions{&EXT_ENCRYPT_THEN_MAC}) {
165         TLSProxy::Record->etm(1);
166     }
167 }
168
169 #Reconstruct the on-the-wire message data following changes
170 sub set_message_contents
171 {
172     my $self = shift;
173     my $data;
174     my $extensions = "";
175
176     $data = pack('n', $self->client_version);
177     $data .= $self->random;
178     $data .= pack('C', $self->session_id_len);
179     $data .= $self->session;
180     $data .= pack('n', $self->ciphersuite_len);
181     $data .= pack("n*", @{$self->ciphersuites});
182     $data .= pack('C', $self->comp_meth_len);
183     $data .= pack("C*", @{$self->comp_meths});
184
185     foreach my $key (keys %{$self->extension_data}) {
186         my $extdata = ${$self->extension_data}{$key};
187         $extensions .= pack("n", $key);
188         $extensions .= pack("n", length($extdata));
189         $extensions .= $extdata;
190     }
191
192     $data .= pack('n', length($extensions));
193     $data .= $extensions;
194
195     $self->data($data);
196 }
197
198 #Read/write accessors
199 sub client_version
200 {
201     my $self = shift;
202     if (@_) {
203       $self->{client_version} = shift;
204     }
205     return $self->{client_version};
206 }
207 sub random
208 {
209     my $self = shift;
210     if (@_) {
211       $self->{random} = shift;
212     }
213     return $self->{random};
214 }
215 sub session_id_len
216 {
217     my $self = shift;
218     if (@_) {
219       $self->{session_id_len} = shift;
220     }
221     return $self->{session_id_len};
222 }
223 sub session
224 {
225     my $self = shift;
226     if (@_) {
227       $self->{session} = shift;
228     }
229     return $self->{session};
230 }
231 sub ciphersuite_len
232 {
233     my $self = shift;
234     if (@_) {
235       $self->{ciphersuite_len} = shift;
236     }
237     return $self->{ciphersuite_len};
238 }
239 sub ciphersuites
240 {
241     my $self = shift;
242     if (@_) {
243       $self->{ciphersuites} = shift;
244     }
245     return $self->{ciphersuites};
246 }
247 sub comp_meth_len
248 {
249     my $self = shift;
250     if (@_) {
251       $self->{comp_meth_len} = shift;
252     }
253     return $self->{comp_meth_len};
254 }
255 sub comp_meths
256 {
257     my $self = shift;
258     if (@_) {
259       $self->{comp_meths} = shift;
260     }
261     return $self->{comp_meths};
262 }
263 sub extensions_len
264 {
265     my $self = shift;
266     if (@_) {
267       $self->{extensions_len} = shift;
268     }
269     return $self->{extensions_len};
270 }
271 sub extension_data
272 {
273     my $self = shift;
274     if (@_) {
275       $self->{extension_data} = shift;
276     }
277     return $self->{extension_data};
278 }
279 sub delete_extension
280 {
281     my ($self, $ext_type) = @_;
282     delete $self->{extension_data}{$ext_type};
283 }
284 1;