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";
72 print " (server -> client)\n";
74 print " (client -> server)\n";
77 #Get the record header (unpack can't fail if $packet is too short)
78 my ($content_type, $version, $len) = unpack('Cnn', $packet);
80 if (length($packet) < TLS_RECORD_HEADER_LENGTH + $len) {
81 print "Partial data : ".length($packet)." bytes\n";
86 my $data = substr($packet, TLS_RECORD_HEADER_LENGTH, $len);
88 print " Content type: ".$record_type{$content_type}."\n";
89 print " Version: $tls_version{$version}\n";
90 print " Length: $len\n";
92 my $record = TLSProxy::Record->new(
104 if ($content_type != RT_CCS) {
105 if (($server && $server_encrypting)
106 || (!$server && $client_encrypting)) {
107 if (!TLSProxy::Proxy->is_tls13() && $etm) {
108 $record->decryptETM();
112 $record->encrypted(1);
114 if (TLSProxy::Proxy->is_tls13()) {
115 print " Inner content type: "
116 .$record_type{$record->content_type()}."\n";
121 push @record_list, $record;
123 #Now figure out what messages are contained within this record
124 my @messages = TLSProxy::Message->get_messages($server, $record);
125 push @message_list, @messages;
127 $packet = substr($packet, TLS_RECORD_HEADER_LENGTH + $len);
131 return (\@record_list, \@message_list, $partial);
136 $server_encrypting = 0;
137 $client_encrypting = 0;
140 #Class level accessors
141 sub server_encrypting
145 $server_encrypting = shift;
147 return $server_encrypting;
149 sub client_encrypting
153 $client_encrypting= shift;
155 return $client_encrypting;
157 #Enable/Disable Encrypt-then-MAC
182 content_type => $content_type,
186 len_real => $len_real,
187 decrypt_len => $decrypt_len,
189 decrypt_data => $decrypt_data,
190 orig_decrypt_data => $decrypt_data,
193 outer_content_type => RT_APPLICATION_DATA
196 return bless $self, $class;
199 #Decrypt using encrypt-then-MAC
204 my $data = $self->data;
206 if($self->version >= VERS_TLS_1_1()) {
207 #TLS1.1+ has an explicit IV. Throw it away
208 $data = substr($data, 16);
211 #Throw away the MAC (assumes MAC is 20 bytes for now. FIXME)
212 $data = substr($data, 0, length($data) - 20);
214 #Find out what the padding byte is
215 my $padval = unpack("C", substr($data, length($data) - 1));
217 #Throw away the padding
218 $data = substr($data, 0, length($data) - ($padval + 1));
220 $self->decrypt_data($data);
221 $self->decrypt_len(length($data));
231 my $data = $self->data;
234 if (TLSProxy::Proxy->is_tls13()) {
235 #A TLS1.3 client, when processing the server's initial flight, could
236 #respond with either an encrypted or an unencrypted alert.
237 if ($self->content_type() == RT_ALERT) {
238 #TODO(TLS1.3): Eventually it is sufficient just to check the record
239 #content type. If an alert is encrypted it will have a record
240 #content type of application data. However we haven't done the
241 #record layer changes yet, so it's a bit more complicated. For now
242 #we will additionally check if the data length is 2 (1 byte for
243 #alert level, 1 byte for alert description). If it is, then this is
244 #an unencrypted alert, so don't try to decrypt
245 return $data if (length($data) == 2);
248 } elsif ($self->version >= VERS_TLS_1_1()) {
249 #16 bytes for a standard IV
250 $data = substr($data, 16);
252 #Find out what the padding byte is
253 my $padval = unpack("C", substr($data, length($data) - 1));
255 #Throw away the padding
256 $data = substr($data, 0, length($data) - ($padval + 1));
259 #Throw away the MAC or TAG
260 $data = substr($data, 0, length($data) - $mactaglen);
262 if (TLSProxy::Proxy->is_tls13()) {
263 #Get the content type
264 my $content_type = unpack("C", substr($data, length($data) - 1));
265 $self->content_type($content_type);
266 $data = substr($data, 0, length($data) - 1);
269 $self->decrypt_data($data);
270 $self->decrypt_len(length($data));
275 #Reconstruct the on-the-wire record representation
276 sub reconstruct_record
288 $data = pack('n', $self->len | 0x8000);
290 if (TLSProxy::Proxy->is_tls13() && $self->encrypted) {
291 $data = pack('Cnn', $self->outer_content_type, $self->version,
294 $data = pack('Cnn', $self->content_type, $self->version,
299 $data .= $self->data;
308 return $self->{flight};
313 return $self->{sslv2};
318 return $self->{len_real};
320 sub orig_decrypt_data
323 return $self->{orig_decrypt_data};
326 #Read/write accessors
331 $self->{decrypt_len} = shift;
333 return $self->{decrypt_len};
339 $self->{data} = shift;
341 return $self->{data};
347 $self->{decrypt_data} = shift;
349 return $self->{decrypt_data};
355 $self->{len} = shift;
363 $self->{version} = shift;
365 return $self->{version};
371 $self->{content_type} = shift;
373 return $self->{content_type};
379 $self->{encrypted} = shift;
381 return $self->{encrypted};
383 sub outer_content_type
387 $self->{outer_content_type} = shift;
389 return $self->{outer_content_type};