1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the OpenSSL license (the "License"). You may not use
4 # this file except in compliance with the License. You can obtain a copy
5 # in the file LICENSE in the source distribution or at
6 # https://www.openssl.org/source/license.html
10 package TLSProxy::Message;
12 use constant TLS_MESSAGE_HEADER_LENGTH => 4;
16 MT_HELLO_REQUEST => 0,
19 MT_NEW_SESSION_TICKET => 4,
20 MT_ENCRYPTED_EXTENSIONS => 8,
22 MT_SERVER_KEY_EXCHANGE => 12,
23 MT_CERTIFICATE_REQUEST => 13,
24 MT_SERVER_HELLO_DONE => 14,
25 MT_CERTIFICATE_VERIFY => 15,
26 MT_CLIENT_KEY_EXCHANGE => 16,
28 MT_CERTIFICATE_STATUS => 22,
40 AL_DESC_CLOSE_NOTIFY => 0,
41 AL_DESC_UNEXPECTED_MESSAGE => 10,
42 AL_DESC_NO_RENEGOTIATION => 100
46 MT_HELLO_REQUEST, "HelloRequest",
47 MT_CLIENT_HELLO, "ClientHello",
48 MT_SERVER_HELLO, "ServerHello",
49 MT_NEW_SESSION_TICKET, "NewSessionTicket",
50 MT_ENCRYPTED_EXTENSIONS, "EncryptedExtensions",
51 MT_CERTIFICATE, "Certificate",
52 MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange",
53 MT_CERTIFICATE_REQUEST, "CertificateRequest",
54 MT_SERVER_HELLO_DONE, "ServerHelloDone",
55 MT_CERTIFICATE_VERIFY, "CertificateVerify",
56 MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange",
57 MT_FINISHED, "Finished",
58 MT_CERTIFICATE_STATUS, "CertificateStatus",
59 MT_NEXT_PROTO, "NextProto"
63 EXT_STATUS_REQUEST => 5,
64 EXT_SUPPORTED_GROUPS => 10,
65 EXT_ENCRYPT_THEN_MAC => 22,
66 EXT_EXTENDED_MASTER_SECRET => 23,
67 EXT_SESSION_TICKET => 35,
68 EXT_SUPPORTED_VERSIONS => 43,
70 # This extension is an unofficial extension only ever written by OpenSSL
71 # (i.e. not read), and even then only when enabled. We use it to test
72 # handling of duplicate extensions.
73 EXT_DUPLICATE_EXTENSION => 0xfde8
83 my @message_rec_list = ();
84 my @message_frag_lens = ();
95 @message_rec_list = ();
96 @message_frag_lens = ();
99 #Class method to extract messages from a record
103 my $serverin = shift;
108 @message_frag_lens = ();
110 if ($serverin != $server && length($payload) != 0) {
111 die "Changed peer, but we still have fragment data\n";
115 if ($record->content_type == TLSProxy::Record::RT_CCS) {
116 if ($payload ne "") {
117 #We can't handle this yet
118 die "CCS received before message data complete\n";
121 TLSProxy::Record->server_encrypting(1);
123 TLSProxy::Record->client_encrypting(1);
125 } elsif ($record->content_type == TLSProxy::Record::RT_HANDSHAKE) {
126 if ($record->len == 0 || $record->len_real == 0) {
127 print " Message truncated\n";
131 if (length $payload > 0) {
132 #We are continuing processing a message started in a previous
133 #record. Add this record to the list associated with this
135 push @message_rec_list, $record;
137 if ($messlen <= length($payload)) {
139 die "Internal error: invalid messlen: ".$messlen
140 ." payload length:".length($payload)."\n";
142 if (length($payload) + $record->decrypt_len >= $messlen) {
143 #We can complete the message with this record
144 $recoffset = $messlen - length($payload);
145 $payload .= substr($record->decrypt_data, 0, $recoffset);
146 push @message_frag_lens, $recoffset;
147 $message = create_message($server, $mt, $payload,
149 push @messages, $message;
153 #This is just part of the total message
154 $payload .= $record->decrypt_data;
155 $recoffset = $record->decrypt_len;
156 push @message_frag_lens, $record->decrypt_len;
158 print " Partial message data read: ".$recoffset." bytes\n";
161 while ($record->decrypt_len > $recoffset) {
162 #We are at the start of a new message
163 if ($record->decrypt_len - $recoffset < 4) {
164 #Whilst technically probably valid we can't cope with this
165 die "End of record in the middle of a message header\n";
167 @message_rec_list = ($record);
170 ($mt, $lenhi, $lenlo) = unpack('CnC',
171 substr($record->decrypt_data,
173 $messlen = ($lenhi << 8) | $lenlo;
174 print " Message type: $message_type{$mt}\n";
175 print " Message Length: $messlen\n";
176 $startoffset = $recoffset;
180 if ($recoffset < $record->decrypt_len) {
181 #Some payload data is present in this record
182 if ($record->decrypt_len - $recoffset >= $messlen) {
183 #We can complete the message with this record
184 $payload .= substr($record->decrypt_data, $recoffset,
186 $recoffset += $messlen;
187 push @message_frag_lens, $messlen;
188 $message = create_message($server, $mt, $payload,
190 push @messages, $message;
194 #This is just part of the total message
195 $payload .= substr($record->decrypt_data, $recoffset,
196 $record->decrypt_len - $recoffset);
197 $recoffset = $record->decrypt_len;
198 push @message_frag_lens, $recoffset;
203 } elsif ($record->content_type == TLSProxy::Record::RT_APPLICATION_DATA) {
204 print " [ENCRYPTED APPLICATION DATA]\n";
205 print " [".$record->decrypt_data."]\n";
206 } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
207 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
208 #A CloseNotify from the client indicates we have finished successfully
210 if (!$end && !$server && $alertlev == AL_LEVEL_WARN
211 && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
214 #All alerts end the test
221 #Function to work out which sub-class we need to create and then
225 my ($server, $mt, $data, $startoffset) = @_;
228 #We only support ClientHello in this version...needs to be extended for
230 if ($mt == MT_CLIENT_HELLO) {
231 $message = TLSProxy::ClientHello->new(
239 } elsif ($mt == MT_SERVER_HELLO) {
240 $message = TLSProxy::ServerHello->new(
248 } elsif ($mt == MT_SERVER_KEY_EXCHANGE) {
249 $message = TLSProxy::ServerKeyExchange->new(
257 } elsif ($mt == MT_NEW_SESSION_TICKET) {
258 $message = TLSProxy::NewSessionTicket->new(
267 #Unknown message type
268 $message = TLSProxy::Message->new(
294 return !$success && $end;
304 $message_frag_lens) = @_;
311 startoffset => $startoffset,
312 message_frag_lens => $message_frag_lens
315 return bless $self, $class;
322 $ciphersuite = shift;
327 #Update all the underlying records with the modified data from this message
328 #Note: Does not currently support re-encrypting
334 my $numrecs = $#{$self->records};
336 $self->set_message_contents();
341 $lenlo = length($self->data) & 0xff;
342 $lenhi = length($self->data) >> 8;
343 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
346 #The message is fully contained within one record
347 my ($rec) = @{$self->records};
348 my $recdata = $rec->decrypt_data;
352 # We use empty message_frag_lens to indicates that pre-repacking,
353 # the message wasn't present. The first fragment length doesn't include
354 # the TLS header, so we need to check and compute the right length.
355 if (@{$self->message_frag_lens}) {
356 $old_length = ${$self->message_frag_lens}[0] +
357 TLS_MESSAGE_HEADER_LENGTH;
362 my $prefix = substr($recdata, 0, $self->startoffset);
363 my $suffix = substr($recdata, $self->startoffset + $old_length);
365 $rec->decrypt_data($prefix.($msgdata).($suffix));
366 # TODO(openssl-team): don't keep explicit lengths.
367 # (If a length override is ever needed to construct invalid packets,
368 # use an explicit override field instead.)
369 $rec->decrypt_len(length($rec->decrypt_data));
370 $rec->len($rec->len + length($msgdata) - $old_length);
371 # Don't support re-encryption.
372 $rec->data($rec->decrypt_data);
374 #Update the fragment len in case we changed it above
375 ${$self->message_frag_lens}[0] = length($msgdata)
376 - TLS_MESSAGE_HEADER_LENGTH;
380 #Note we don't currently support changing a fragmented message length
383 foreach my $rec (@{$self->records}) {
384 my $recdata = $rec->decrypt_data;
386 #This is the first record
387 my $remainlen = length($recdata) - $self->startoffset;
388 $rec->data(substr($recdata, 0, $self->startoffset)
389 .substr(($msgdata), 0, $remainlen));
390 $datadone += $remainlen;
391 } elsif ($recctr + 1 == $numrecs) {
392 #This is the last record
393 $rec->data(substr($msgdata, $datadone));
395 #This is a middle record
396 $rec->data(substr($msgdata, $datadone, length($rec->data)));
397 $datadone += length($rec->data);
403 #To be overridden by sub-classes
404 sub set_message_contents
412 return $self->{server};
415 #Read/write accessors
428 $self->{data} = shift;
430 return $self->{data};
436 $self->{records} = shift;
438 return $self->{records};
444 $self->{startoffset} = shift;
446 return $self->{startoffset};
448 sub message_frag_lens
452 $self->{message_frag_lens} = shift;
454 return $self->{message_frag_lens};
459 return TLS_MESSAGE_HEADER_LENGTH + length($self->data);