1 # Written by Matt Caswell for the OpenSSL project.
2 # ====================================================================
3 # Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 # 6. Redistributions of any form whatsoever must retain the following
33 # "This product includes software developed by the OpenSSL Project
34 # for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 # ====================================================================
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).
56 package TLSProxy::Message;
58 use constant TLS_MESSAGE_HEADER_LENGTH => 4;
62 MT_HELLO_REQUEST => 0,
65 MT_NEW_SESSION_TICKET => 4,
67 MT_SERVER_KEY_EXCHANGE => 12,
68 MT_CERTIFICATE_REQUEST => 13,
69 MT_SERVER_HELLO_DONE => 14,
70 MT_CERTIFICATE_VERIFY => 15,
71 MT_CLIENT_KEY_EXCHANGE => 16,
73 MT_CERTIFICATE_STATUS => 22,
85 AL_DESC_CLOSE_NOTIFY => 0
89 MT_HELLO_REQUEST, "HelloRequest",
90 MT_CLIENT_HELLO, "ClientHello",
91 MT_SERVER_HELLO, "ServerHello",
92 MT_NEW_SESSION_TICKET, "NewSessionTicket",
93 MT_CERTIFICATE, "Certificate",
94 MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange",
95 MT_CERTIFICATE_REQUEST, "CertificateRequest",
96 MT_SERVER_HELLO_DONE, "ServerHelloDone",
97 MT_CERTIFICATE_VERIFY, "CertificateVerify",
98 MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange",
99 MT_FINISHED, "Finished",
100 MT_CERTIFICATE_STATUS, "CertificateStatus",
101 MT_NEXT_PROTO, "NextProto"
105 EXT_STATUS_REQUEST => 5,
106 EXT_ENCRYPT_THEN_MAC => 22,
107 EXT_EXTENDED_MASTER_SECRET => 23,
108 EXT_SESSION_TICKET => 35,
109 # This extension does not exist and isn't recognised by OpenSSL.
110 # We use it to test handling of duplicate extensions.
111 EXT_DUPLICATE_EXTENSION => 1234
117 my $startoffset = -1;
121 my @message_rec_list = ();
122 my @message_frag_lens = ();
133 @message_rec_list = ();
134 @message_frag_lens = ();
137 #Class method to extract messages from a record
141 my $serverin = shift;
146 @message_frag_lens = ();
148 if ($serverin != $server && length($payload) != 0) {
149 die "Changed peer, but we still have fragment data\n";
153 if ($record->content_type == TLSProxy::Record::RT_CCS) {
154 if ($payload ne "") {
155 #We can't handle this yet
156 die "CCS received before message data complete\n";
159 TLSProxy::Record->server_ccs_seen(1);
161 TLSProxy::Record->client_ccs_seen(1);
163 } elsif ($record->content_type == TLSProxy::Record::RT_HANDSHAKE) {
164 if ($record->len == 0 || $record->len_real == 0) {
165 print " Message truncated\n";
169 if (length $payload > 0) {
170 #We are continuing processing a message started in a previous
171 #record. Add this record to the list associated with this
173 push @message_rec_list, $record;
175 if ($messlen <= length($payload)) {
177 die "Internal error: invalid messlen: ".$messlen
178 ." payload length:".length($payload)."\n";
180 if (length($payload) + $record->decrypt_len >= $messlen) {
181 #We can complete the message with this record
182 $recoffset = $messlen - length($payload);
183 $payload .= substr($record->decrypt_data, 0, $recoffset);
184 push @message_frag_lens, $recoffset;
185 $message = create_message($server, $mt, $payload,
187 push @messages, $message;
191 #This is just part of the total message
192 $payload .= $record->decrypt_data;
193 $recoffset = $record->decrypt_len;
194 push @message_frag_lens, $record->decrypt_len;
196 print " Partial message data read: ".$recoffset." bytes\n";
199 while ($record->decrypt_len > $recoffset) {
200 #We are at the start of a new message
201 if ($record->decrypt_len - $recoffset < 4) {
202 #Whilst technically probably valid we can't cope with this
203 die "End of record in the middle of a message header\n";
205 @message_rec_list = ($record);
208 ($mt, $lenhi, $lenlo) = unpack('CnC',
209 substr($record->decrypt_data,
211 $messlen = ($lenhi << 8) | $lenlo;
212 print " Message type: $message_type{$mt}\n";
213 print " Message Length: $messlen\n";
214 $startoffset = $recoffset;
218 if ($recoffset < $record->decrypt_len) {
219 #Some payload data is present in this record
220 if ($record->decrypt_len - $recoffset >= $messlen) {
221 #We can complete the message with this record
222 $payload .= substr($record->decrypt_data, $recoffset,
224 $recoffset += $messlen;
225 push @message_frag_lens, $messlen;
226 $message = create_message($server, $mt, $payload,
228 push @messages, $message;
232 #This is just part of the total message
233 $payload .= substr($record->decrypt_data, $recoffset,
234 $record->decrypt_len - $recoffset);
235 $recoffset = $record->decrypt_len;
236 push @message_frag_lens, $recoffset;
241 } elsif ($record->content_type == TLSProxy::Record::RT_APPLICATION_DATA) {
242 print " [ENCRYPTED APPLICATION DATA]\n";
243 print " [".$record->decrypt_data."]\n";
244 } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
245 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
246 #All alerts end the test
248 #A CloseNotify from the client indicates we have finished successfully
250 if (!$server && $alertlev == AL_LEVEL_WARN
251 && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
259 #Function to work out which sub-class we need to create and then
263 my ($server, $mt, $data, $startoffset) = @_;
266 #We only support ClientHello in this version...needs to be extended for
268 if ($mt == MT_CLIENT_HELLO) {
269 $message = TLSProxy::ClientHello->new(
277 } elsif ($mt == MT_SERVER_HELLO) {
278 $message = TLSProxy::ServerHello->new(
286 } elsif ($mt == MT_SERVER_KEY_EXCHANGE) {
287 $message = TLSProxy::ServerKeyExchange->new(
295 } elsif ($mt == MT_NEW_SESSION_TICKET) {
296 $message = TLSProxy::NewSessionTicket->new(
305 #Unknown message type
306 $message = TLSProxy::Message->new(
332 return !$success && $end;
342 $message_frag_lens) = @_;
349 startoffset => $startoffset,
350 message_frag_lens => $message_frag_lens
353 return bless $self, $class;
360 $ciphersuite = shift;
365 #Update all the underlying records with the modified data from this message
366 #Note: Does not currently support re-encrypting
372 my $numrecs = $#{$self->records};
374 $self->set_message_contents();
379 $lenlo = length($self->data) & 0xff;
380 $lenhi = length($self->data) >> 8;
381 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
384 #The message is fully contained within one record
385 my ($rec) = @{$self->records};
386 my $recdata = $rec->decrypt_data;
390 # We use empty message_frag_lens to indicates that pre-repacking,
391 # the message wasn't present. The first fragment length doesn't include
392 # the TLS header, so we need to check and compute the right length.
393 if (@{$self->message_frag_lens}) {
394 $old_length = ${$self->message_frag_lens}[0] +
395 TLS_MESSAGE_HEADER_LENGTH;
400 my $prefix = substr($recdata, 0, $self->startoffset);
401 my $suffix = substr($recdata, $self->startoffset + $old_length);
403 $rec->decrypt_data($prefix.($msgdata).($suffix));
404 # TODO(openssl-team): don't keep explicit lengths.
405 # (If a length override is ever needed to construct invalid packets,
406 # use an explicit override field instead.)
407 $rec->decrypt_len(length($rec->decrypt_data));
408 $rec->len($rec->len + length($msgdata) - $old_length);
409 # Don't support re-encryption.
410 $rec->data($rec->decrypt_data);
412 #Update the fragment len in case we changed it above
413 ${$self->message_frag_lens}[0] = length($msgdata)
414 - TLS_MESSAGE_HEADER_LENGTH;
418 #Note we don't currently support changing a fragmented message length
421 foreach my $rec (@{$self->records}) {
422 my $recdata = $rec->decrypt_data;
424 #This is the first record
425 my $remainlen = length($recdata) - $self->startoffset;
426 $rec->data(substr($recdata, 0, $self->startoffset)
427 .substr(($msgdata), 0, $remainlen));
428 $datadone += $remainlen;
429 } elsif ($recctr + 1 == $numrecs) {
430 #This is the last record
431 $rec->data(substr($msgdata, $datadone));
433 #This is a middle record
434 $rec->data(substr($msgdata, $datadone, length($rec->data)));
435 $datadone += length($rec->data);
441 #To be overridden by sub-classes
442 sub set_message_contents
450 return $self->{server};
453 #Read/write accessors
466 $self->{data} = shift;
468 return $self->{data};
474 $self->{records} = shift;
476 return $self->{records};
482 $self->{startoffset} = shift;
484 return $self->{startoffset};
486 sub message_frag_lens
490 $self->{message_frag_lens} = shift;
492 return $self->{message_frag_lens};
497 return TLS_MESSAGE_HEADER_LENGTH + length($self->data);