2018-02-13 12:51:29 +00:00
|
|
|
# Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2015-06-16 12:06:41 +00:00
|
|
|
#
|
2016-04-20 14:20:59 +00:00
|
|
|
# Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
# this file except in compliance with the License. You can obtain a copy
|
|
|
|
# in the file LICENSE in the source distribution or at
|
|
|
|
# https://www.openssl.org/source/license.html
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use TLSProxy::Proxy;
|
|
|
|
|
|
|
|
package TLSProxy::Record;
|
|
|
|
|
2016-11-11 16:22:19 +00:00
|
|
|
my $server_encrypting = 0;
|
|
|
|
my $client_encrypting = 0;
|
2015-06-16 12:06:41 +00:00
|
|
|
my $etm = 0;
|
|
|
|
|
|
|
|
use constant TLS_RECORD_HEADER_LENGTH => 5;
|
|
|
|
|
|
|
|
#Record types
|
|
|
|
use constant {
|
|
|
|
RT_APPLICATION_DATA => 23,
|
|
|
|
RT_HANDSHAKE => 22,
|
|
|
|
RT_ALERT => 21,
|
2016-11-02 09:41:37 +00:00
|
|
|
RT_CCS => 20,
|
|
|
|
RT_UNKNOWN => 100
|
2015-06-16 12:06:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
my %record_type = (
|
|
|
|
RT_APPLICATION_DATA, "APPLICATION DATA",
|
|
|
|
RT_HANDSHAKE, "HANDSHAKE",
|
|
|
|
RT_ALERT, "ALERT",
|
2016-11-02 09:41:37 +00:00
|
|
|
RT_CCS, "CCS",
|
|
|
|
RT_UNKNOWN, "UNKNOWN"
|
2015-06-16 12:06:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
use constant {
|
2017-03-08 11:48:43 +00:00
|
|
|
VERS_TLS_1_4 => 0x0305,
|
|
|
|
VERS_TLS_1_3 => 0x0304,
|
|
|
|
VERS_TLS_1_2 => 0x0303,
|
|
|
|
VERS_TLS_1_1 => 0x0302,
|
|
|
|
VERS_TLS_1_0 => 0x0301,
|
|
|
|
VERS_SSL_3_0 => 0x0300,
|
|
|
|
VERS_SSL_LT_3_0 => 0x02ff
|
2015-06-16 12:06:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
my %tls_version = (
|
|
|
|
VERS_TLS_1_3, "TLS1.3",
|
|
|
|
VERS_TLS_1_2, "TLS1.2",
|
|
|
|
VERS_TLS_1_1, "TLS1.1",
|
|
|
|
VERS_TLS_1_0, "TLS1.0",
|
2015-08-13 17:15:45 +00:00
|
|
|
VERS_SSL_3_0, "SSL3",
|
|
|
|
VERS_SSL_LT_3_0, "SSL<3"
|
2015-06-16 12:06:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
#Class method to extract records from a packet of data
|
|
|
|
sub get_records
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
my $server = shift;
|
|
|
|
my $flight = shift;
|
|
|
|
my $packet = shift;
|
2018-03-22 16:28:39 +00:00
|
|
|
my $partial = "";
|
2015-06-16 12:06:41 +00:00
|
|
|
my @record_list = ();
|
|
|
|
my @message_list = ();
|
|
|
|
|
|
|
|
my $recnum = 1;
|
|
|
|
while (length ($packet) > 0) {
|
2018-04-16 12:08:35 +00:00
|
|
|
print " Record $recnum ", $server ? "(server -> client)\n"
|
|
|
|
: "(client -> server)\n";
|
2018-04-06 09:44:38 +00:00
|
|
|
|
|
|
|
#Get the record header (unpack can't fail if $packet is too short)
|
|
|
|
my ($content_type, $version, $len) = unpack('Cnn', $packet);
|
|
|
|
|
2018-04-16 12:08:35 +00:00
|
|
|
if (length($packet) < TLS_RECORD_HEADER_LENGTH + ($len // 0)) {
|
2015-06-16 12:06:41 +00:00
|
|
|
print "Partial data : ".length($packet)." bytes\n";
|
2018-03-22 16:28:39 +00:00
|
|
|
$partial = $packet;
|
2018-04-06 09:44:38 +00:00
|
|
|
last;
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2018-04-06 09:44:38 +00:00
|
|
|
my $data = substr($packet, TLS_RECORD_HEADER_LENGTH, $len);
|
|
|
|
|
|
|
|
print " Content type: ".$record_type{$content_type}."\n";
|
|
|
|
print " Version: $tls_version{$version}\n";
|
|
|
|
print " Length: $len\n";
|
|
|
|
|
|
|
|
my $record = TLSProxy::Record->new(
|
|
|
|
$flight,
|
|
|
|
$content_type,
|
|
|
|
$version,
|
|
|
|
$len,
|
|
|
|
0,
|
|
|
|
$len, # len_real
|
|
|
|
$len, # decrypt_len
|
|
|
|
$data, # data
|
|
|
|
$data # decrypt_data
|
|
|
|
);
|
|
|
|
|
2018-08-07 15:22:31 +00:00
|
|
|
if ($content_type != RT_CCS
|
|
|
|
&& (!TLSProxy::Proxy->is_tls13()
|
|
|
|
|| $content_type != RT_ALERT)) {
|
2018-04-06 09:44:38 +00:00
|
|
|
if (($server && $server_encrypting)
|
|
|
|
|| (!$server && $client_encrypting)) {
|
|
|
|
if (!TLSProxy::Proxy->is_tls13() && $etm) {
|
|
|
|
$record->decryptETM();
|
|
|
|
} else {
|
|
|
|
$record->decrypt();
|
|
|
|
}
|
|
|
|
$record->encrypted(1);
|
|
|
|
|
|
|
|
if (TLSProxy::Proxy->is_tls13()) {
|
|
|
|
print " Inner content type: "
|
|
|
|
.$record_type{$record->content_type()}."\n";
|
2017-11-08 11:37:12 +00:00
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2018-04-06 09:44:38 +00:00
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2018-04-06 09:44:38 +00:00
|
|
|
push @record_list, $record;
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2018-04-06 09:44:38 +00:00
|
|
|
#Now figure out what messages are contained within this record
|
|
|
|
my @messages = TLSProxy::Message->get_messages($server, $record);
|
|
|
|
push @message_list, @messages;
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2018-04-06 09:44:38 +00:00
|
|
|
$packet = substr($packet, TLS_RECORD_HEADER_LENGTH + $len);
|
|
|
|
$recnum++;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:28:39 +00:00
|
|
|
return (\@record_list, \@message_list, $partial);
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub clear
|
|
|
|
{
|
2016-11-11 16:22:19 +00:00
|
|
|
$server_encrypting = 0;
|
|
|
|
$client_encrypting = 0;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#Class level accessors
|
2016-11-11 16:22:19 +00:00
|
|
|
sub server_encrypting
|
2015-06-16 12:06:41 +00:00
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
if (@_) {
|
2016-11-11 16:22:19 +00:00
|
|
|
$server_encrypting = shift;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2016-11-11 16:22:19 +00:00
|
|
|
return $server_encrypting;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2016-11-11 16:22:19 +00:00
|
|
|
sub client_encrypting
|
2015-06-16 12:06:41 +00:00
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
if (@_) {
|
2016-11-11 16:22:19 +00:00
|
|
|
$client_encrypting= shift;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2016-11-11 16:22:19 +00:00
|
|
|
return $client_encrypting;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
#Enable/Disable Encrypt-then-MAC
|
|
|
|
sub etm
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
if (@_) {
|
|
|
|
$etm = shift;
|
|
|
|
}
|
|
|
|
return $etm;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
my ($flight,
|
|
|
|
$content_type,
|
|
|
|
$version,
|
|
|
|
$len,
|
2016-08-02 16:24:54 +00:00
|
|
|
$sslv2,
|
2015-06-16 12:06:41 +00:00
|
|
|
$len_real,
|
|
|
|
$decrypt_len,
|
|
|
|
$data,
|
|
|
|
$decrypt_data) = @_;
|
2019-01-31 18:31:35 +00:00
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
my $self = {
|
|
|
|
flight => $flight,
|
|
|
|
content_type => $content_type,
|
|
|
|
version => $version,
|
|
|
|
len => $len,
|
2016-08-02 16:24:54 +00:00
|
|
|
sslv2 => $sslv2,
|
2015-06-16 12:06:41 +00:00
|
|
|
len_real => $len_real,
|
|
|
|
decrypt_len => $decrypt_len,
|
|
|
|
data => $data,
|
|
|
|
decrypt_data => $decrypt_data,
|
2016-11-18 23:44:09 +00:00
|
|
|
orig_decrypt_data => $decrypt_data,
|
2018-03-22 16:28:39 +00:00
|
|
|
sent => 0,
|
2016-11-21 16:22:00 +00:00
|
|
|
encrypted => 0,
|
|
|
|
outer_content_type => RT_APPLICATION_DATA
|
2015-06-16 12:06:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bless $self, $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
#Decrypt using encrypt-then-MAC
|
|
|
|
sub decryptETM
|
|
|
|
{
|
|
|
|
my ($self) = shift;
|
|
|
|
|
|
|
|
my $data = $self->data;
|
|
|
|
|
|
|
|
if($self->version >= VERS_TLS_1_1()) {
|
|
|
|
#TLS1.1+ has an explicit IV. Throw it away
|
|
|
|
$data = substr($data, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#Throw away the MAC (assumes MAC is 20 bytes for now. FIXME)
|
|
|
|
$data = substr($data, 0, length($data) - 20);
|
|
|
|
|
|
|
|
#Find out what the padding byte is
|
|
|
|
my $padval = unpack("C", substr($data, length($data) - 1));
|
|
|
|
|
|
|
|
#Throw away the padding
|
|
|
|
$data = substr($data, 0, length($data) - ($padval + 1));
|
|
|
|
|
|
|
|
$self->decrypt_data($data);
|
|
|
|
$self->decrypt_len(length($data));
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#Standard decrypt
|
|
|
|
sub decrypt()
|
|
|
|
{
|
|
|
|
my ($self) = shift;
|
2016-10-28 14:57:12 +00:00
|
|
|
my $mactaglen = 20;
|
2015-06-16 12:06:41 +00:00
|
|
|
my $data = $self->data;
|
|
|
|
|
2016-10-28 14:57:12 +00:00
|
|
|
#Throw away any IVs
|
2016-11-15 11:09:25 +00:00
|
|
|
if (TLSProxy::Proxy->is_tls13()) {
|
|
|
|
#A TLS1.3 client, when processing the server's initial flight, could
|
|
|
|
#respond with either an encrypted or an unencrypted alert.
|
|
|
|
if ($self->content_type() == RT_ALERT) {
|
|
|
|
#TODO(TLS1.3): Eventually it is sufficient just to check the record
|
|
|
|
#content type. If an alert is encrypted it will have a record
|
|
|
|
#content type of application data. However we haven't done the
|
|
|
|
#record layer changes yet, so it's a bit more complicated. For now
|
|
|
|
#we will additionally check if the data length is 2 (1 byte for
|
|
|
|
#alert level, 1 byte for alert description). If it is, then this is
|
2017-03-28 21:57:28 +00:00
|
|
|
#an unencrypted alert, so don't try to decrypt
|
2016-11-15 11:09:25 +00:00
|
|
|
return $data if (length($data) == 2);
|
|
|
|
}
|
2016-10-28 14:57:12 +00:00
|
|
|
$mactaglen = 16;
|
|
|
|
} elsif ($self->version >= VERS_TLS_1_1()) {
|
|
|
|
#16 bytes for a standard IV
|
2015-06-16 12:06:41 +00:00
|
|
|
$data = substr($data, 16);
|
|
|
|
|
2016-10-28 14:57:12 +00:00
|
|
|
#Find out what the padding byte is
|
|
|
|
my $padval = unpack("C", substr($data, length($data) - 1));
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2016-10-28 14:57:12 +00:00
|
|
|
#Throw away the padding
|
|
|
|
$data = substr($data, 0, length($data) - ($padval + 1));
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2016-10-28 14:57:12 +00:00
|
|
|
#Throw away the MAC or TAG
|
|
|
|
$data = substr($data, 0, length($data) - $mactaglen);
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2016-11-18 23:44:09 +00:00
|
|
|
if (TLSProxy::Proxy->is_tls13()) {
|
|
|
|
#Get the content type
|
|
|
|
my $content_type = unpack("C", substr($data, length($data) - 1));
|
|
|
|
$self->content_type($content_type);
|
|
|
|
$data = substr($data, 0, length($data) - 1);
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
$self->decrypt_data($data);
|
|
|
|
$self->decrypt_len(length($data));
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#Reconstruct the on-the-wire record representation
|
|
|
|
sub reconstruct_record
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2016-11-18 23:44:09 +00:00
|
|
|
my $server = shift;
|
2015-06-16 12:06:41 +00:00
|
|
|
my $data;
|
|
|
|
|
2018-04-13 16:48:06 +00:00
|
|
|
#We only replay the records in the same direction
|
|
|
|
if ($self->{sent} || ($self->flight & 1) != $server) {
|
2018-03-22 16:28:39 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
$self->{sent} = 1;
|
|
|
|
|
2016-08-02 16:24:54 +00:00
|
|
|
if ($self->sslv2) {
|
|
|
|
$data = pack('n', $self->len | 0x8000);
|
|
|
|
} else {
|
2016-11-18 23:44:09 +00:00
|
|
|
if (TLSProxy::Proxy->is_tls13() && $self->encrypted) {
|
2016-11-21 16:22:00 +00:00
|
|
|
$data = pack('Cnn', $self->outer_content_type, $self->version,
|
2018-02-14 17:29:32 +00:00
|
|
|
$self->len);
|
2016-11-18 23:44:09 +00:00
|
|
|
} else {
|
|
|
|
$data = pack('Cnn', $self->content_type, $self->version,
|
|
|
|
$self->len);
|
|
|
|
}
|
|
|
|
|
2016-08-02 16:24:54 +00:00
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
$data .= $self->data;
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#Read only accessors
|
|
|
|
sub flight
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{flight};
|
|
|
|
}
|
2016-08-02 16:24:54 +00:00
|
|
|
sub sslv2
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{sslv2};
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
sub len_real
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{len_real};
|
|
|
|
}
|
|
|
|
sub orig_decrypt_data
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{orig_decrypt_data};
|
|
|
|
}
|
|
|
|
|
|
|
|
#Read/write accessors
|
|
|
|
sub decrypt_len
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{decrypt_len} = shift;
|
|
|
|
}
|
|
|
|
return $self->{decrypt_len};
|
|
|
|
}
|
|
|
|
sub data
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{data} = shift;
|
|
|
|
}
|
|
|
|
return $self->{data};
|
|
|
|
}
|
|
|
|
sub decrypt_data
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{decrypt_data} = shift;
|
|
|
|
}
|
|
|
|
return $self->{decrypt_data};
|
|
|
|
}
|
|
|
|
sub len
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{len} = shift;
|
|
|
|
}
|
|
|
|
return $self->{len};
|
|
|
|
}
|
2016-11-07 14:26:41 +00:00
|
|
|
sub version
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{version} = shift;
|
|
|
|
}
|
|
|
|
return $self->{version};
|
|
|
|
}
|
2016-11-18 23:44:09 +00:00
|
|
|
sub content_type
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{content_type} = shift;
|
|
|
|
}
|
|
|
|
return $self->{content_type};
|
|
|
|
}
|
|
|
|
sub encrypted
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{encrypted} = shift;
|
|
|
|
}
|
|
|
|
return $self->{encrypted};
|
|
|
|
}
|
2016-11-21 16:22:00 +00:00
|
|
|
sub outer_content_type
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{outer_content_type} = shift;
|
|
|
|
}
|
|
|
|
return $self->{outer_content_type};
|
|
|
|
}
|
2018-04-16 12:08:35 +00:00
|
|
|
sub is_fatal_alert
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $server = shift;
|
|
|
|
|
|
|
|
if (($self->{flight} & 1) == $server
|
|
|
|
&& $self->{content_type} == TLSProxy::Record::RT_ALERT) {
|
|
|
|
my ($level, $alert) = unpack('CC', $self->decrypt_data);
|
|
|
|
return $alert if ($level == 2);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
1;
|