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