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