1 # Copyright 2016-2018 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
12 package TLSProxy::Record;
14 my $server_encrypting = 0;
15 my $client_encrypting = 0;
18 use constant TLS_RECORD_HEADER_LENGTH => 5;
22 RT_APPLICATION_DATA => 23,
30 RT_APPLICATION_DATA, "APPLICATION DATA",
31 RT_HANDSHAKE, "HANDSHAKE",
38 VERS_TLS_1_4 => 0x0305,
39 VERS_TLS_1_3_DRAFT => 0x7f1a,
40 VERS_TLS_1_3 => 0x0304,
41 VERS_TLS_1_2 => 0x0303,
42 VERS_TLS_1_1 => 0x0302,
43 VERS_TLS_1_0 => 0x0301,
44 VERS_SSL_3_0 => 0x0300,
45 VERS_SSL_LT_3_0 => 0x02ff
49 VERS_TLS_1_3, "TLS1.3",
50 VERS_TLS_1_2, "TLS1.2",
51 VERS_TLS_1_1, "TLS1.1",
52 VERS_TLS_1_0, "TLS1.0",
54 VERS_SSL_LT_3_0, "SSL<3"
57 #Class method to extract records from a packet of data
66 my @message_list = ();
69 while (length ($packet) > 0) {
70 print " Record $recnum ", $server ? "(server -> client)\n"
71 : "(client -> server)\n";
73 #Get the record header (unpack can't fail if $packet is too short)
74 my ($content_type, $version, $len) = unpack('Cnn', $packet);
76 if (length($packet) < TLS_RECORD_HEADER_LENGTH + ($len // 0)) {
77 print "Partial data : ".length($packet)." bytes\n";
82 my $data = substr($packet, TLS_RECORD_HEADER_LENGTH, $len);
84 print " Content type: ".$record_type{$content_type}."\n";
85 print " Version: $tls_version{$version}\n";
86 print " Length: $len\n";
88 my $record = TLSProxy::Record->new(
100 if ($content_type != RT_CCS) {
101 if (($server && $server_encrypting)
102 || (!$server && $client_encrypting)) {
103 if (!TLSProxy::Proxy->is_tls13() && $etm) {
104 $record->decryptETM();
108 $record->encrypted(1);
110 if (TLSProxy::Proxy->is_tls13()) {
111 print " Inner content type: "
112 .$record_type{$record->content_type()}."\n";
117 push @record_list, $record;
119 #Now figure out what messages are contained within this record
120 my @messages = TLSProxy::Message->get_messages($server, $record);
121 push @message_list, @messages;
123 $packet = substr($packet, TLS_RECORD_HEADER_LENGTH + $len);
127 return (\@record_list, \@message_list, $partial);
132 $server_encrypting = 0;
133 $client_encrypting = 0;
136 #Class level accessors
137 sub server_encrypting
141 $server_encrypting = shift;
143 return $server_encrypting;
145 sub client_encrypting
149 $client_encrypting= shift;
151 return $client_encrypting;
153 #Enable/Disable Encrypt-then-MAC
178 content_type => $content_type,
182 len_real => $len_real,
183 decrypt_len => $decrypt_len,
185 decrypt_data => $decrypt_data,
186 orig_decrypt_data => $decrypt_data,
189 outer_content_type => RT_APPLICATION_DATA
192 return bless $self, $class;
195 #Decrypt using encrypt-then-MAC
200 my $data = $self->data;
202 if($self->version >= VERS_TLS_1_1()) {
203 #TLS1.1+ has an explicit IV. Throw it away
204 $data = substr($data, 16);
207 #Throw away the MAC (assumes MAC is 20 bytes for now. FIXME)
208 $data = substr($data, 0, length($data) - 20);
210 #Find out what the padding byte is
211 my $padval = unpack("C", substr($data, length($data) - 1));
213 #Throw away the padding
214 $data = substr($data, 0, length($data) - ($padval + 1));
216 $self->decrypt_data($data);
217 $self->decrypt_len(length($data));
227 my $data = $self->data;
230 if (TLSProxy::Proxy->is_tls13()) {
231 #A TLS1.3 client, when processing the server's initial flight, could
232 #respond with either an encrypted or an unencrypted alert.
233 if ($self->content_type() == RT_ALERT) {
234 #TODO(TLS1.3): Eventually it is sufficient just to check the record
235 #content type. If an alert is encrypted it will have a record
236 #content type of application data. However we haven't done the
237 #record layer changes yet, so it's a bit more complicated. For now
238 #we will additionally check if the data length is 2 (1 byte for
239 #alert level, 1 byte for alert description). If it is, then this is
240 #an unencrypted alert, so don't try to decrypt
241 return $data if (length($data) == 2);
244 } elsif ($self->version >= VERS_TLS_1_1()) {
245 #16 bytes for a standard IV
246 $data = substr($data, 16);
248 #Find out what the padding byte is
249 my $padval = unpack("C", substr($data, length($data) - 1));
251 #Throw away the padding
252 $data = substr($data, 0, length($data) - ($padval + 1));
255 #Throw away the MAC or TAG
256 $data = substr($data, 0, length($data) - $mactaglen);
258 if (TLSProxy::Proxy->is_tls13()) {
259 #Get the content type
260 my $content_type = unpack("C", substr($data, length($data) - 1));
261 $self->content_type($content_type);
262 $data = substr($data, 0, length($data) - 1);
265 $self->decrypt_data($data);
266 $self->decrypt_len(length($data));
271 #Reconstruct the on-the-wire record representation
272 sub reconstruct_record
278 #We only replay the records in the same direction
279 if ($self->{sent} || ($self->flight & 1) != $server) {
285 $data = pack('n', $self->len | 0x8000);
287 if (TLSProxy::Proxy->is_tls13() && $self->encrypted) {
288 $data = pack('Cnn', $self->outer_content_type, $self->version,
291 $data = pack('Cnn', $self->content_type, $self->version,
296 $data .= $self->data;
305 return $self->{flight};
310 return $self->{sslv2};
315 return $self->{len_real};
317 sub orig_decrypt_data
320 return $self->{orig_decrypt_data};
323 #Read/write accessors
328 $self->{decrypt_len} = shift;
330 return $self->{decrypt_len};
336 $self->{data} = shift;
338 return $self->{data};
344 $self->{decrypt_data} = shift;
346 return $self->{decrypt_data};
352 $self->{len} = shift;
360 $self->{version} = shift;
362 return $self->{version};
368 $self->{content_type} = shift;
370 return $self->{content_type};
376 $self->{encrypted} = shift;
378 return $self->{encrypted};
380 sub outer_content_type
384 $self->{outer_content_type} = shift;
386 return $self->{outer_content_type};
393 if (($self->{flight} & 1) == $server
394 && $self->{content_type} == TLSProxy::Record::RT_ALERT) {
395 my ($level, $alert) = unpack('CC', $self->decrypt_data);
396 return $alert if ($level == 2);