Removes SCT_LIST_set_source and SCT_LIST_set0_logs
[openssl.git] / util / TLSProxy / ServerHello.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::ServerHello;
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         TLSProxy::Message::MT_SERVER_HELLO,
73         $data,
74         $records,
75         $startoffset,
76         $message_frag_lens);
77
78     $self->{server_version} = 0;
79     $self->{random} = [];
80     $self->{session_id_len} = 0;
81     $self->{session} = "";
82     $self->{ciphersuite} = 0;
83     $self->{comp_meth} = 0;
84     $self->{extension_data} = "";
85
86     return $self;
87 }
88
89 sub parse
90 {
91     my $self = shift;
92     my $ptr = 2;
93     my ($server_version) = unpack('n', $self->data);
94     my $random = substr($self->data, $ptr, 32);
95     $ptr += 32;
96     my $session_id_len = unpack('C', substr($self->data, $ptr));
97     $ptr++;
98     my $session = substr($self->data, $ptr, $session_id_len);
99     $ptr += $session_id_len;
100     my $ciphersuite = unpack('n', substr($self->data, $ptr));
101     $ptr += 2;
102     my $comp_meth = unpack('C', substr($self->data, $ptr));
103     $ptr++;
104     my $extensions_len = unpack('n', substr($self->data, $ptr));
105     $ptr += 2;
106     #For now we just deal with this as a block of data. In the future we will
107     #want to parse this
108     my $extension_data = substr($self->data, $ptr);
109     
110     if (length($extension_data) != $extensions_len) {
111         die "Invalid extension length\n";
112     }
113     my %extensions = ();
114     while (length($extension_data) >= 4) {
115         my ($type, $size) = unpack("nn", $extension_data);
116         my $extdata = substr($extension_data, 4, $size);
117         $extension_data = substr($extension_data, 4 + $size);
118         $extensions{$type} = $extdata;
119     }
120
121     $self->server_version($server_version);
122     $self->random($random);
123     $self->session_id_len($session_id_len);
124     $self->session($session);
125     $self->ciphersuite($ciphersuite);
126     $self->comp_meth($comp_meth);
127     $self->extension_data(\%extensions);
128
129     $self->process_data();
130
131     print "    Server Version:".$server_version."\n";
132     print "    Session ID Len:".$session_id_len."\n";
133     print "    Ciphersuite:".$ciphersuite."\n";
134     print "    Compression Method:".$comp_meth."\n";
135     print "    Extensions Len:".$extensions_len."\n";
136 }
137
138 #Perform any actions necessary based on the data we've seen
139 sub process_data
140 {
141     my $self = shift;
142
143     TLSProxy::Message->ciphersuite($self->ciphersuite);
144 }
145
146 #Reconstruct the on-the-wire message data following changes
147 sub set_message_contents
148 {
149     my $self = shift;
150     my $data;
151     my $extensions = "";
152
153     $data = pack('n', $self->server_version);
154     $data .= $self->random;
155     $data .= pack('C', $self->session_id_len);
156     $data .= $self->session;
157     $data .= pack('n', $self->ciphersuite);
158     $data .= pack('C', $self->comp_meth);
159
160     foreach my $key (keys %{$self->extension_data}) {
161         my $extdata = ${$self->extension_data}{$key};
162         $extensions .= pack("n", $key);
163         $extensions .= pack("n", length($extdata));
164         $extensions .= $extdata;
165         if ($key == TLSProxy::Message::EXT_DUPLICATE_EXTENSION) {
166           $extensions .= pack("n", $key);
167           $extensions .= pack("n", length($extdata));
168           $extensions .= $extdata;
169         }
170     }
171
172     $data .= pack('n', length($extensions));
173     $data .= $extensions;
174     $self->data($data);
175 }
176
177 #Read/write accessors
178 sub server_version
179 {
180     my $self = shift;
181     if (@_) {
182       $self->{client_version} = shift;
183     }
184     return $self->{client_version};
185 }
186 sub random
187 {
188     my $self = shift;
189     if (@_) {
190       $self->{random} = shift;
191     }
192     return $self->{random};
193 }
194 sub session_id_len
195 {
196     my $self = shift;
197     if (@_) {
198       $self->{session_id_len} = shift;
199     }
200     return $self->{session_id_len};
201 }
202 sub session
203 {
204     my $self = shift;
205     if (@_) {
206       $self->{session} = shift;
207     }
208     return $self->{session};
209 }
210 sub ciphersuite
211 {
212     my $self = shift;
213     if (@_) {
214       $self->{ciphersuite} = shift;
215     }
216     return $self->{ciphersuite};
217 }
218 sub comp_meth
219 {
220     my $self = shift;
221     if (@_) {
222       $self->{comp_meth} = shift;
223     }
224     return $self->{comp_meth};
225 }
226 sub extension_data
227 {
228     my $self = shift;
229     if (@_) {
230       $self->{extension_data} = shift;
231     }
232     return $self->{extension_data};
233 }
234 sub set_extension
235 {
236     my ($self, $ext_type, $ext_data) = @_;
237     $self->{extension_data}{$ext_type} = $ext_data;
238 }
239 sub delete_extension
240 {
241     my ($self, $ext_type) = @_;
242     delete $self->{extension_data}{$ext_type};
243 }
244 1;