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 => 0x0304,
40 VERS_TLS_1_2 => 0x0303,
41 VERS_TLS_1_1 => 0x0302,
42 VERS_TLS_1_0 => 0x0301,
43 VERS_SSL_3_0 => 0x0300,
44 VERS_SSL_LT_3_0 => 0x02ff
48 VERS_TLS_1_3, "TLS1.3",
49 VERS_TLS_1_2, "TLS1.2",
50 VERS_TLS_1_1, "TLS1.1",
51 VERS_TLS_1_0, "TLS1.0",
53 VERS_SSL_LT_3_0, "SSL<3"
56 #Class method to extract records from a packet of data
65 my @message_list = ();
68 while (length ($packet) > 0) {
69 print " Record $recnum ", $server ? "(server -> client)\n"
70 : "(client -> server)\n";
72 #Get the record header (unpack can't fail if $packet is too short)
73 my ($content_type, $version, $len) = unpack('Cnn', $packet);
75 if (length($packet) < TLS_RECORD_HEADER_LENGTH + ($len // 0)) {
76 print "Partial data : ".length($packet)." bytes\n";
81 my $data = substr($packet, TLS_RECORD_HEADER_LENGTH, $len);
83 print " Content type: ".$record_type{$content_type}."\n";
84 print " Version: $tls_version{$version}\n";
85 print " Length: $len\n";
87 my $record = TLSProxy::Record->new(
99 if ($content_type != RT_CCS
100 && (!TLSProxy::Proxy->is_tls13()
101 || $content_type != RT_ALERT)) {
102 if (($server && $server_encrypting)
103 || (!$server && $client_encrypting)) {
104 if (!TLSProxy::Proxy->is_tls13() && $etm) {
105 $record->decryptETM();
109 $record->encrypted(1);
111 if (TLSProxy::Proxy->is_tls13()) {
112 print " Inner content type: "
113 .$record_type{$record->content_type()}."\n";
118 push @record_list, $record;
120 #Now figure out what messages are contained within this record
121 my @messages = TLSProxy::Message->get_messages($server, $record);
122 push @message_list, @messages;
124 $packet = substr($packet, TLS_RECORD_HEADER_LENGTH + $len);
128 return (\@record_list, \@message_list, $partial);
133 $server_encrypting = 0;
134 $client_encrypting = 0;
137 #Class level accessors
138 sub server_encrypting
142 $server_encrypting = shift;
144 return $server_encrypting;
146 sub client_encrypting
150 $client_encrypting= shift;
152 return $client_encrypting;
154 #Enable/Disable Encrypt-then-MAC
179 content_type => $content_type,
183 len_real => $len_real,
184 decrypt_len => $decrypt_len,
186 decrypt_data => $decrypt_data,
187 orig_decrypt_data => $decrypt_data,
190 outer_content_type => RT_APPLICATION_DATA
193 return bless $self, $class;
196 #Decrypt using encrypt-then-MAC
201 my $data = $self->data;
203 if($self->version >= VERS_TLS_1_1()) {
204 #TLS1.1+ has an explicit IV. Throw it away
205 $data = substr($data, 16);
208 #Throw away the MAC (assumes MAC is 20 bytes for now. FIXME)
209 $data = substr($data, 0, length($data) - 20);
211 #Find out what the padding byte is
212 my $padval = unpack("C", substr($data, length($data) - 1));
214 #Throw away the padding
215 $data = substr($data, 0, length($data) - ($padval + 1));
217 $self->decrypt_data($data);
218 $self->decrypt_len(length($data));
228 my $data = $self->data;
231 if (TLSProxy::Proxy->is_tls13()) {
232 #A TLS1.3 client, when processing the server's initial flight, could
233 #respond with either an encrypted or an unencrypted alert.
234 if ($self->content_type() == RT_ALERT) {
235 #TODO(TLS1.3): Eventually it is sufficient just to check the record
236 #content type. If an alert is encrypted it will have a record
237 #content type of application data. However we haven't done the
238 #record layer changes yet, so it's a bit more complicated. For now
239 #we will additionally check if the data length is 2 (1 byte for
240 #alert level, 1 byte for alert description). If it is, then this is
241 #an unencrypted alert, so don't try to decrypt
242 return $data if (length($data) == 2);
245 } elsif ($self->version >= VERS_TLS_1_1()) {
246 #16 bytes for a standard IV
247 $data = substr($data, 16);
249 #Find out what the padding byte is
250 my $padval = unpack("C", substr($data, length($data) - 1));
252 #Throw away the padding
253 $data = substr($data, 0, length($data) - ($padval + 1));
256 #Throw away the MAC or TAG
257 $data = substr($data, 0, length($data) - $mactaglen);
259 if (TLSProxy::Proxy->is_tls13()) {
260 #Get the content type
261 my $content_type = unpack("C", substr($data, length($data) - 1));
262 $self->content_type($content_type);
263 $data = substr($data, 0, length($data) - 1);
266 $self->decrypt_data($data);
267 $self->decrypt_len(length($data));
272 #Reconstruct the on-the-wire record representation
273 sub reconstruct_record
279 #We only replay the records in the same direction
280 if ($self->{sent} || ($self->flight & 1) != $server) {
286 $data = pack('n', $self->len | 0x8000);
288 if (TLSProxy::Proxy->is_tls13() && $self->encrypted) {
289 $data = pack('Cnn', $self->outer_content_type, $self->version,
292 $data = pack('Cnn', $self->content_type, $self->version,
297 $data .= $self->data;
306 return $self->{flight};
311 return $self->{sslv2};
316 return $self->{len_real};
318 sub orig_decrypt_data
321 return $self->{orig_decrypt_data};
324 #Read/write accessors
329 $self->{decrypt_len} = shift;
331 return $self->{decrypt_len};
337 $self->{data} = shift;
339 return $self->{data};
345 $self->{decrypt_data} = shift;
347 return $self->{decrypt_data};
353 $self->{len} = shift;
361 $self->{version} = shift;
363 return $self->{version};
369 $self->{content_type} = shift;
371 return $self->{content_type};
377 $self->{encrypted} = shift;
379 return $self->{encrypted};
381 sub outer_content_type
385 $self->{outer_content_type} = shift;
387 return $self->{outer_content_type};
394 if (($self->{flight} & 1) == $server
395 && $self->{content_type} == TLSProxy::Record::RT_ALERT) {
396 my ($level, $alert) = unpack('CC', $self->decrypt_data);
397 return $alert if ($level == 2);