2018-01-19 09:49:22 +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;
|
2016-03-15 16:44:26 +00:00
|
|
|
use POSIX ":sys_wait_h";
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
package TLSProxy::Proxy;
|
|
|
|
|
|
|
|
use File::Spec;
|
|
|
|
use IO::Socket;
|
|
|
|
use IO::Select;
|
|
|
|
use TLSProxy::Record;
|
|
|
|
use TLSProxy::Message;
|
|
|
|
use TLSProxy::ClientHello;
|
2015-08-07 13:38:21 +00:00
|
|
|
use TLSProxy::ServerHello;
|
2016-11-28 22:39:23 +00:00
|
|
|
use TLSProxy::EncryptedExtensions;
|
2016-12-02 09:14:15 +00:00
|
|
|
use TLSProxy::Certificate;
|
2017-01-05 12:28:40 +00:00
|
|
|
use TLSProxy::CertificateVerify;
|
2015-08-07 13:38:21 +00:00
|
|
|
use TLSProxy::ServerKeyExchange;
|
2015-09-15 10:06:12 +00:00
|
|
|
use TLSProxy::NewSessionTicket;
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2016-02-02 23:57:30 +00:00
|
|
|
my $have_IPv6 = 0;
|
|
|
|
my $IP_factory;
|
|
|
|
|
2016-11-15 11:09:25 +00:00
|
|
|
my $is_tls13 = 0;
|
2016-12-28 15:01:57 +00:00
|
|
|
my $ciphersuite = undef;
|
2016-11-15 11:09:25 +00:00
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
my ($filter,
|
|
|
|
$execute,
|
|
|
|
$cert,
|
|
|
|
$debug) = @_;
|
|
|
|
|
|
|
|
my $self = {
|
|
|
|
#Public read/write
|
|
|
|
proxy_addr => "localhost",
|
|
|
|
server_addr => "localhost",
|
|
|
|
filter => $filter,
|
2015-08-13 14:17:14 +00:00
|
|
|
serverflags => "",
|
|
|
|
clientflags => "",
|
|
|
|
serverconnects => 1,
|
2016-11-22 13:43:50 +00:00
|
|
|
reneg => 0,
|
2017-01-20 12:08:51 +00:00
|
|
|
sessionfile => undef,
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
#Public read
|
2018-04-02 21:26:25 +00:00
|
|
|
proxy_port => 0,
|
|
|
|
server_port => 0,
|
|
|
|
serverpid => 0,
|
|
|
|
clientpid => 0,
|
2015-06-16 12:06:41 +00:00
|
|
|
execute => $execute,
|
|
|
|
cert => $cert,
|
|
|
|
debug => $debug,
|
2015-08-13 14:17:14 +00:00
|
|
|
cipherc => "",
|
2018-02-16 11:26:02 +00:00
|
|
|
ciphersuitesc => "",
|
|
|
|
ciphers => "AES128-SHA",
|
|
|
|
ciphersuitess => "TLS_AES_128_GCM_SHA256",
|
2018-03-22 16:28:39 +00:00
|
|
|
flight => -1,
|
|
|
|
direction => -1,
|
|
|
|
partial => ["", ""],
|
2015-06-16 12:06:41 +00:00
|
|
|
record_list => [],
|
|
|
|
message_list => [],
|
|
|
|
};
|
|
|
|
|
2016-02-14 06:10:38 +00:00
|
|
|
# IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
|
|
|
|
# However, IO::Socket::INET6 is older and is said to be more widely
|
|
|
|
# deployed for the moment, and may have less bugs, so we try the latter
|
|
|
|
# first, then fall back on the code modules. Worst case scenario, we
|
|
|
|
# fall back to IO::Socket::INET, only supports IPv4.
|
2016-02-02 23:57:30 +00:00
|
|
|
eval {
|
2016-02-14 06:10:38 +00:00
|
|
|
require IO::Socket::INET6;
|
|
|
|
my $s = IO::Socket::INET6->new(
|
2016-02-02 23:57:30 +00:00
|
|
|
LocalAddr => "::1",
|
|
|
|
LocalPort => 0,
|
|
|
|
Listen=>1,
|
|
|
|
);
|
|
|
|
$s or die "\n";
|
|
|
|
$s->close();
|
|
|
|
};
|
|
|
|
if ($@ eq "") {
|
2016-02-14 06:10:38 +00:00
|
|
|
$IP_factory = sub { IO::Socket::INET6->new(@_); };
|
2016-02-02 23:57:30 +00:00
|
|
|
$have_IPv6 = 1;
|
|
|
|
} else {
|
|
|
|
eval {
|
2016-02-14 06:10:38 +00:00
|
|
|
require IO::Socket::IP;
|
|
|
|
my $s = IO::Socket::IP->new(
|
2016-02-02 23:57:30 +00:00
|
|
|
LocalAddr => "::1",
|
|
|
|
LocalPort => 0,
|
|
|
|
Listen=>1,
|
|
|
|
);
|
|
|
|
$s or die "\n";
|
|
|
|
$s->close();
|
|
|
|
};
|
|
|
|
if ($@ eq "") {
|
2016-02-14 06:10:38 +00:00
|
|
|
$IP_factory = sub { IO::Socket::IP->new(@_); };
|
2016-02-02 23:57:30 +00:00
|
|
|
$have_IPv6 = 1;
|
|
|
|
} else {
|
|
|
|
$IP_factory = sub { IO::Socket::INET->new(@_); };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:22:47 +00:00
|
|
|
# Create the Proxy socket
|
|
|
|
my $proxaddr = $self->{proxy_addr};
|
|
|
|
$proxaddr =~ s/[\[\]]//g; # Remove [ and ]
|
|
|
|
my @proxyargs = (
|
|
|
|
LocalHost => $proxaddr,
|
2018-04-02 21:26:25 +00:00
|
|
|
LocalPort => 0,
|
2018-01-17 10:22:47 +00:00
|
|
|
Proto => "tcp",
|
|
|
|
Listen => SOMAXCONN,
|
|
|
|
);
|
|
|
|
$self->{proxy_sock} = $IP_factory->(@proxyargs);
|
|
|
|
|
|
|
|
if ($self->{proxy_sock}) {
|
2018-04-02 21:26:25 +00:00
|
|
|
$self->{proxy_port} = $self->{proxy_sock}->sockport();
|
2018-01-17 10:22:47 +00:00
|
|
|
print "Proxy started on port ".$self->{proxy_port}."\n";
|
|
|
|
} else {
|
2018-04-02 21:26:25 +00:00
|
|
|
warn "Failed creating proxy socket (".$proxaddr.",0): $!\n";
|
2018-01-17 10:22:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
return bless $self, $class;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:22:47 +00:00
|
|
|
sub DESTROY
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->{proxy_sock}->close() if $self->{proxy_sock};
|
|
|
|
}
|
|
|
|
|
2016-03-15 16:44:26 +00:00
|
|
|
sub clearClient
|
2015-06-16 12:06:41 +00:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-08-13 14:17:14 +00:00
|
|
|
$self->{cipherc} = "";
|
2018-02-16 11:26:02 +00:00
|
|
|
$self->{ciphersuitec} = "";
|
2018-03-22 16:28:39 +00:00
|
|
|
$self->{flight} = -1;
|
|
|
|
$self->{direction} = -1;
|
|
|
|
$self->{partial} = ["", ""];
|
2015-06-16 12:06:41 +00:00
|
|
|
$self->{record_list} = [];
|
|
|
|
$self->{message_list} = [];
|
2015-08-13 14:17:14 +00:00
|
|
|
$self->{clientflags} = "";
|
2017-01-20 12:08:51 +00:00
|
|
|
$self->{sessionfile} = undef;
|
2017-05-26 12:06:08 +00:00
|
|
|
$self->{clientpid} = 0;
|
2016-11-15 11:09:25 +00:00
|
|
|
$is_tls13 = 0;
|
2016-12-28 15:01:57 +00:00
|
|
|
$ciphersuite = undef;
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
TLSProxy::Message->clear();
|
|
|
|
TLSProxy::Record->clear();
|
|
|
|
}
|
|
|
|
|
2016-03-15 16:44:26 +00:00
|
|
|
sub clear
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->clearClient;
|
2018-02-16 11:26:02 +00:00
|
|
|
$self->{ciphers} = "AES128-SHA";
|
|
|
|
$self->{ciphersuitess} = "TLS_AES_128_GCM_SHA256";
|
2016-03-15 16:44:26 +00:00
|
|
|
$self->{serverflags} = "";
|
|
|
|
$self->{serverconnects} = 1;
|
|
|
|
$self->{serverpid} = 0;
|
2016-11-22 13:43:50 +00:00
|
|
|
$self->{reneg} = 0;
|
2016-03-15 16:44:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
sub restart
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->clear;
|
|
|
|
$self->start;
|
|
|
|
}
|
|
|
|
|
2015-08-13 14:17:14 +00:00
|
|
|
sub clientrestart
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
$self->clear;
|
|
|
|
$self->clientstart;
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:26:25 +00:00
|
|
|
sub connect_to_server
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my $servaddr = $self->{server_addr};
|
|
|
|
|
|
|
|
$servaddr =~ s/[\[\]]//g; # Remove [ and ]
|
|
|
|
|
|
|
|
$self->{server_sock} = $IP_factory->(PeerAddr => $servaddr,
|
|
|
|
PeerPort => $self->{server_port},
|
|
|
|
Proto => 'tcp')
|
|
|
|
or die "unable to connect: $!\n";
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
sub start
|
|
|
|
{
|
|
|
|
my ($self) = shift;
|
|
|
|
my $pid;
|
|
|
|
|
2018-01-17 10:22:47 +00:00
|
|
|
if ($self->{proxy_sock} == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:26:25 +00:00
|
|
|
my $execcmd = $self->execute
|
|
|
|
." s_server -max_protocol TLSv1.3 -no_comp -rev -engine ossltest"
|
|
|
|
." -accept 0 -cert ".$self->cert." -cert2 ".$self->cert
|
|
|
|
." -naccept ".$self->serverconnects;
|
|
|
|
unless ($self->supports_IPv6) {
|
|
|
|
$execcmd .= " -4";
|
|
|
|
}
|
|
|
|
if ($self->ciphers ne "") {
|
|
|
|
$execcmd .= " -cipher ".$self->ciphers;
|
|
|
|
}
|
|
|
|
if ($self->ciphersuitess ne "") {
|
|
|
|
$execcmd .= " -ciphersuites ".$self->ciphersuitess;
|
|
|
|
}
|
|
|
|
if ($self->serverflags ne "") {
|
|
|
|
$execcmd .= " ".$self->serverflags;
|
|
|
|
}
|
|
|
|
if ($self->debug) {
|
|
|
|
print STDERR "Server command: $execcmd\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
open(my $savedin, "<&STDIN");
|
|
|
|
|
|
|
|
# Temporarily replace STDIN so that sink process can inherit it...
|
|
|
|
$pid = open(STDIN, "$execcmd |") or die "Failed to $execcmd: $!\n";
|
|
|
|
$self->{real_serverpid} = $pid;
|
|
|
|
|
|
|
|
# Process the output from s_server until we find the ACCEPT line, which
|
|
|
|
# tells us what the accepting address and port are.
|
|
|
|
while (<>) {
|
|
|
|
print;
|
|
|
|
s/\R$//; # Better chomp
|
|
|
|
next unless (/^ACCEPT\s.*:(\d+)$/);
|
|
|
|
$self->{server_port} = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{server_port} == 0) {
|
|
|
|
# This actually means that s_server exited, because otherwise
|
|
|
|
# we would still searching for ACCEPT...
|
|
|
|
die "no ACCEPT detected in '$execcmd' output\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Just make sure everything else is simply printed [as separate lines].
|
|
|
|
# The sub process simply inherits our STD* and will keep consuming
|
|
|
|
# server's output and printing it as long as there is anything there,
|
|
|
|
# out of our way.
|
|
|
|
my $error;
|
|
|
|
$pid = undef;
|
|
|
|
if (eval { require Win32::Process; 1; }) {
|
|
|
|
if (Win32::Process::Create(my $h, $^X, "perl -ne print", 0, 0, ".")) {
|
|
|
|
$pid = $h->GetProcessID();
|
|
|
|
} else {
|
|
|
|
$error = Win32::FormatMessage(Win32::GetLastError());
|
2015-08-13 14:17:14 +00:00
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
} else {
|
|
|
|
if (defined($pid = fork)) {
|
|
|
|
$pid or exec("$^X -ne print") or exit($!);
|
|
|
|
} else {
|
|
|
|
$error = $!;
|
2017-04-28 07:20:05 +00:00
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
|
|
|
|
# Change back to original stdin
|
|
|
|
open(STDIN, "<&", $savedin);
|
|
|
|
close($savedin);
|
|
|
|
|
|
|
|
if (!defined($pid)) {
|
|
|
|
kill(3, $self->{real_serverpid});
|
|
|
|
die "Failed to capture s_server's output: $error\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{serverpid} = $pid;
|
|
|
|
|
|
|
|
print STDERR "Server responds on ",
|
|
|
|
$self->{server_addr}, ":", $self->{server_port}, "\n";
|
|
|
|
|
|
|
|
# Connect right away...
|
|
|
|
$self->connect_to_server();
|
2015-06-16 12:06:41 +00:00
|
|
|
|
2016-06-14 13:35:26 +00:00
|
|
|
return $self->clientstart;
|
2015-08-13 14:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub clientstart
|
|
|
|
{
|
|
|
|
my ($self) = shift;
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
if ($self->execute) {
|
2018-04-02 21:26:25 +00:00
|
|
|
my $pid;
|
|
|
|
my $execcmd = $self->execute
|
|
|
|
." s_client -max_protocol TLSv1.3 -engine ossltest -connect "
|
|
|
|
.($self->proxy_addr).":".($self->proxy_port);
|
|
|
|
unless ($self->supports_IPv6) {
|
|
|
|
$execcmd .= " -4";
|
|
|
|
}
|
|
|
|
if ($self->cipherc ne "") {
|
|
|
|
$execcmd .= " -cipher ".$self->cipherc;
|
|
|
|
}
|
|
|
|
if ($self->ciphersuitesc ne "") {
|
|
|
|
$execcmd .= " -ciphersuites ".$self->ciphersuitesc;
|
|
|
|
}
|
|
|
|
if ($self->clientflags ne "") {
|
|
|
|
$execcmd .= " ".$self->clientflags;
|
|
|
|
}
|
|
|
|
if (defined $self->sessionfile) {
|
|
|
|
$execcmd .= " -ign_eof";
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
if ($self->debug) {
|
|
|
|
print STDERR "Client command: $execcmd\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
open(my $savedout, ">&STDOUT");
|
|
|
|
# If we open pipe with new descriptor, attempt to close it,
|
|
|
|
# explicitly or implicitly, would incur waitpid and effectively
|
|
|
|
# dead-lock...
|
|
|
|
if (!($pid = open(STDOUT, "| $execcmd"))) {
|
|
|
|
my $err = $!;
|
|
|
|
kill(3, $self->{real_serverpid});
|
|
|
|
die "Failed to $execcmd: $err\n";
|
|
|
|
}
|
|
|
|
$self->{clientpid} = $pid;
|
|
|
|
|
|
|
|
# queue [magic] input
|
|
|
|
print $self->reneg ? "R" : "test";
|
|
|
|
|
|
|
|
# this closes client's stdin without waiting for its pid
|
|
|
|
open(STDOUT, ">&", $savedout);
|
|
|
|
close($savedout);
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Wait for incoming connection from client
|
2018-04-02 21:26:25 +00:00
|
|
|
my $fdset = IO::Select->new($self->{proxy_sock});
|
|
|
|
if (!$fdset->can_read(1)) {
|
|
|
|
kill(3, $self->{real_serverpid});
|
|
|
|
die "s_client didn't try to connect\n";
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:35:26 +00:00
|
|
|
my $client_sock;
|
2018-01-17 10:22:47 +00:00
|
|
|
if(!($client_sock = $self->{proxy_sock}->accept())) {
|
2016-06-14 13:35:26 +00:00
|
|
|
warn "Failed accepting incoming connection: $!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
print "Connection opened\n";
|
|
|
|
|
2018-04-02 21:26:25 +00:00
|
|
|
my $server_sock = $self->{server_sock};
|
2015-06-16 12:06:41 +00:00
|
|
|
my $indata;
|
|
|
|
|
|
|
|
#Wait for either the server socket or the client socket to become readable
|
2018-04-02 21:26:25 +00:00
|
|
|
$fdset = IO::Select->new($server_sock, $client_sock);
|
2015-06-16 12:06:41 +00:00
|
|
|
my @ready;
|
2017-03-15 00:54:04 +00:00
|
|
|
my $ctr = 0;
|
2018-01-13 09:24:45 +00:00
|
|
|
local $SIG{PIPE} = "IGNORE";
|
2017-03-15 00:54:04 +00:00
|
|
|
while( (!(TLSProxy::Message->end)
|
|
|
|
|| (defined $self->sessionfile()
|
|
|
|
&& (-s $self->sessionfile()) == 0))
|
2017-08-31 08:39:26 +00:00
|
|
|
&& $ctr < 10) {
|
2018-04-02 21:26:25 +00:00
|
|
|
if (!(@ready = $fdset->can_read(1))) {
|
2017-08-31 08:39:26 +00:00
|
|
|
$ctr++;
|
|
|
|
next;
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
foreach my $hand (@ready) {
|
|
|
|
if ($hand == $server_sock) {
|
|
|
|
$server_sock->sysread($indata, 16384) or goto END;
|
|
|
|
$indata = $self->process_packet(1, $indata);
|
|
|
|
$client_sock->syswrite($indata);
|
2017-03-15 00:54:04 +00:00
|
|
|
$ctr = 0;
|
2015-06-16 12:06:41 +00:00
|
|
|
} elsif ($hand == $client_sock) {
|
|
|
|
$client_sock->sysread($indata, 16384) or goto END;
|
|
|
|
$indata = $self->process_packet(0, $indata);
|
|
|
|
$server_sock->syswrite($indata);
|
2017-03-15 00:54:04 +00:00
|
|
|
$ctr = 0;
|
2015-06-16 12:06:41 +00:00
|
|
|
} else {
|
2018-04-02 21:26:25 +00:00
|
|
|
kill(3, $self->{real_serverpid});
|
2017-08-31 08:39:26 +00:00
|
|
|
die "Unexpected handle";
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:26:25 +00:00
|
|
|
if ($ctr >= 10) {
|
|
|
|
kill(3, $self->{real_serverpid});
|
|
|
|
die "No progress made";
|
|
|
|
}
|
2017-01-20 12:08:51 +00:00
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
END:
|
|
|
|
print "Connection closed\n";
|
|
|
|
if($server_sock) {
|
|
|
|
$server_sock->close();
|
2018-04-02 21:26:25 +00:00
|
|
|
$self->{server_sock} = undef;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
if($client_sock) {
|
|
|
|
#Closing this also kills the child process
|
|
|
|
$client_sock->close();
|
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
|
|
|
|
my $pid;
|
|
|
|
if (--$self->{serverconnects} == 0) {
|
|
|
|
$pid = $self->{serverpid};
|
|
|
|
die "serverpid is zero\n" if $pid == 0;
|
|
|
|
print "Waiting for server process to close: $pid...\n";
|
|
|
|
# recall that we wait on process that buffers server's output
|
|
|
|
waitpid($pid, 0);
|
2017-01-10 08:22:05 +00:00
|
|
|
die "exit code $? from server process\n" if $? != 0;
|
2017-11-03 10:43:06 +00:00
|
|
|
} else {
|
2018-04-02 21:26:25 +00:00
|
|
|
# It's a bit counter-intuitive spot to make next connection to
|
|
|
|
# the s_server. Rationale is that established connection works
|
|
|
|
# as syncronization point, in sense that this way we know that
|
|
|
|
# s_server is actually done with current session...
|
|
|
|
$self->connect_to_server();
|
2016-03-15 16:44:26 +00:00
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
$pid = $self->{clientpid};
|
|
|
|
die "clientpid is zero\n" if $pid == 0;
|
|
|
|
print "Waiting for client process to close: $pid...\n";
|
|
|
|
waitpid($pid, 0);
|
2017-05-26 12:06:08 +00:00
|
|
|
|
2016-06-14 13:35:26 +00:00
|
|
|
return 1;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_packet
|
|
|
|
{
|
|
|
|
my ($self, $server, $packet) = @_;
|
|
|
|
my $len_real;
|
|
|
|
my $decrypt_len;
|
|
|
|
my $data;
|
|
|
|
my $recnum;
|
|
|
|
|
|
|
|
if ($server) {
|
|
|
|
print "Received server packet\n";
|
|
|
|
} else {
|
|
|
|
print "Received client packet\n";
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:28:39 +00:00
|
|
|
if ($self->{direction} != $server) {
|
|
|
|
$self->{flight} = $self->{flight} + 1;
|
|
|
|
$self->{direction} = $server;
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
print "Packet length = ".length($packet)."\n";
|
|
|
|
print "Processing flight ".$self->flight."\n";
|
|
|
|
|
|
|
|
#Return contains the list of record found in the packet followed by the
|
2018-03-22 16:28:39 +00:00
|
|
|
#list of messages in those records and any partial message
|
|
|
|
my @ret = TLSProxy::Record->get_records($server, $self->flight, $self->{partial}[$server].$packet);
|
|
|
|
$self->{partial}[$server] = $ret[2];
|
2018-04-02 21:26:25 +00:00
|
|
|
push @{$self->{record_list}}, @{$ret[0]};
|
2015-06-16 12:06:41 +00:00
|
|
|
push @{$self->{message_list}}, @{$ret[1]};
|
|
|
|
|
|
|
|
print "\n";
|
|
|
|
|
2018-03-22 16:28:39 +00:00
|
|
|
if (scalar(@{$ret[0]}) == 0 or length($ret[2]) != 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
#Finished parsing. Call user provided filter here
|
2018-03-22 16:28:39 +00:00
|
|
|
if (defined $self->filter) {
|
2015-08-13 14:17:14 +00:00
|
|
|
$self->filter->($self);
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
|
|
|
|
#Reconstruct the packet
|
|
|
|
$packet = "";
|
|
|
|
foreach my $record (@{$self->record_list}) {
|
2016-11-18 23:44:09 +00:00
|
|
|
$packet .= $record->reconstruct_record($server);
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print "Forwarded packet length = ".length($packet)."\n\n";
|
|
|
|
|
|
|
|
return $packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
#Read accessors
|
|
|
|
sub execute
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{execute};
|
|
|
|
}
|
|
|
|
sub cert
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{cert};
|
|
|
|
}
|
|
|
|
sub debug
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{debug};
|
|
|
|
}
|
|
|
|
sub flight
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{flight};
|
|
|
|
}
|
|
|
|
sub record_list
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{record_list};
|
|
|
|
}
|
|
|
|
sub success
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{success};
|
|
|
|
}
|
|
|
|
sub end
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{end};
|
|
|
|
}
|
2016-02-02 23:57:30 +00:00
|
|
|
sub supports_IPv6
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $have_IPv6;
|
|
|
|
}
|
2015-06-16 12:06:41 +00:00
|
|
|
sub proxy_addr
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{proxy_addr};
|
|
|
|
}
|
|
|
|
sub proxy_port
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{proxy_port};
|
|
|
|
}
|
|
|
|
sub server_addr
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{server_addr};
|
|
|
|
}
|
|
|
|
sub server_port
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{server_port};
|
|
|
|
}
|
2018-04-02 21:26:25 +00:00
|
|
|
sub serverpid
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{serverpid};
|
|
|
|
}
|
|
|
|
sub clientpid
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{clientpid};
|
|
|
|
}
|
|
|
|
|
|
|
|
#Read/write accessors
|
2015-06-16 12:06:41 +00:00
|
|
|
sub filter
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{filter} = shift;
|
2015-06-16 12:06:41 +00:00
|
|
|
}
|
|
|
|
return $self->{filter};
|
|
|
|
}
|
2015-08-07 13:38:21 +00:00
|
|
|
sub cipherc
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{cipherc} = shift;
|
2015-08-07 13:38:21 +00:00
|
|
|
}
|
|
|
|
return $self->{cipherc};
|
|
|
|
}
|
2018-02-16 11:26:02 +00:00
|
|
|
sub ciphersuitesc
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{ciphersuitesc} = shift;
|
|
|
|
}
|
|
|
|
return $self->{ciphersuitesc};
|
|
|
|
}
|
2015-08-07 13:38:21 +00:00
|
|
|
sub ciphers
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{ciphers} = shift;
|
2015-08-07 13:38:21 +00:00
|
|
|
}
|
|
|
|
return $self->{ciphers};
|
|
|
|
}
|
2018-02-16 11:26:02 +00:00
|
|
|
sub ciphersuitess
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{ciphersuitess} = shift;
|
|
|
|
}
|
|
|
|
return $self->{ciphersuitess};
|
|
|
|
}
|
2015-08-13 14:17:14 +00:00
|
|
|
sub serverflags
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{serverflags} = shift;
|
2015-08-13 14:17:14 +00:00
|
|
|
}
|
|
|
|
return $self->{serverflags};
|
|
|
|
}
|
|
|
|
sub clientflags
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{clientflags} = shift;
|
2015-08-13 14:17:14 +00:00
|
|
|
}
|
|
|
|
return $self->{clientflags};
|
|
|
|
}
|
|
|
|
sub serverconnects
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{serverconnects} = shift;
|
2015-08-13 14:17:14 +00:00
|
|
|
}
|
|
|
|
return $self->{serverconnects};
|
|
|
|
}
|
2015-09-16 15:47:55 +00:00
|
|
|
# This is a bit ugly because the caller is responsible for keeping the records
|
|
|
|
# in sync with the updated message list; simply updating the message list isn't
|
|
|
|
# sufficient to get the proxy to forward the new message.
|
|
|
|
# But it does the trick for the one test (test_sslsessiontick) that needs it.
|
|
|
|
sub message_list
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{message_list} = shift;
|
|
|
|
}
|
|
|
|
return $self->{message_list};
|
|
|
|
}
|
2016-08-10 04:45:51 +00:00
|
|
|
|
|
|
|
sub fill_known_data
|
|
|
|
{
|
|
|
|
my $length = shift;
|
|
|
|
my $ret = "";
|
|
|
|
for (my $i = 0; $i < $length; $i++) {
|
|
|
|
$ret .= chr($i);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
2016-11-22 13:43:50 +00:00
|
|
|
|
2016-11-15 11:09:25 +00:00
|
|
|
sub is_tls13
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$is_tls13 = shift;
|
2016-11-15 11:09:25 +00:00
|
|
|
}
|
|
|
|
return $is_tls13;
|
|
|
|
}
|
2016-11-22 13:43:50 +00:00
|
|
|
|
|
|
|
sub reneg
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
2016-12-08 09:44:06 +00:00
|
|
|
$self->{reneg} = shift;
|
2016-11-22 13:43:50 +00:00
|
|
|
}
|
|
|
|
return $self->{reneg};
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:08:51 +00:00
|
|
|
#Setting a sessionfile means that the client will not close until the given
|
|
|
|
#file exists. This is useful in TLSv1.3 where otherwise s_client will close
|
|
|
|
#immediately at the end of the handshake, but before the session has been
|
|
|
|
#received from the server. A side effect of this is that s_client never sends
|
|
|
|
#a close_notify, so instead we consider success to be when it sends application
|
|
|
|
#data over the connection.
|
|
|
|
sub sessionfile
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
if (@_) {
|
|
|
|
$self->{sessionfile} = shift;
|
|
|
|
TLSProxy::Message->successondata(1);
|
|
|
|
}
|
|
|
|
return $self->{sessionfile};
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:01:57 +00:00
|
|
|
sub ciphersuite
|
|
|
|
{
|
|
|
|
my $class = shift;
|
|
|
|
if (@_) {
|
|
|
|
$ciphersuite = shift;
|
|
|
|
}
|
|
|
|
return $ciphersuite;
|
|
|
|
}
|
|
|
|
|
2015-06-16 12:06:41 +00:00
|
|
|
1;
|