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
9 use POSIX ":sys_wait_h";
11 package TLSProxy::Proxy;
17 use TLSProxy::Message;
18 use TLSProxy::ClientHello;
19 use TLSProxy::ServerHello;
20 use TLSProxy::EncryptedExtensions;
21 use TLSProxy::Certificate;
22 use TLSProxy::CertificateVerify;
23 use TLSProxy::ServerKeyExchange;
24 use TLSProxy::NewSessionTicket;
25 use Time::HiRes qw/usleep/;
31 my $ciphersuite = undef;
43 proxy_addr => "localhost",
45 server_addr => "localhost",
62 ciphers => "AES128-SHA",
63 ciphersuitess => "TLS_AES_128_GCM_SHA256",
71 ### TODO vvvv REMOVE THIS AGAIN
72 warn "Proxy tests temporarily disabled!\n";
73 $self->{proxy_sock} = 0;
74 return bless $self, $class;
75 ### TODO ^^^^ REMOVE THIS AGAIN
76 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
77 # However, IO::Socket::INET6 is older and is said to be more widely
78 # deployed for the moment, and may have less bugs, so we try the latter
79 # first, then fall back on the code modules. Worst case scenario, we
80 # fall back to IO::Socket::INET, only supports IPv4.
82 require IO::Socket::INET6;
83 my $s = IO::Socket::INET6->new(
92 $IP_factory = sub { IO::Socket::INET6->new(@_); };
96 require IO::Socket::IP;
97 my $s = IO::Socket::IP->new(
106 $IP_factory = sub { IO::Socket::IP->new(@_); };
109 $IP_factory = sub { IO::Socket::INET->new(@_); };
113 # Create the Proxy socket
114 my $proxaddr = $self->{proxy_addr};
115 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
117 LocalHost => $proxaddr,
118 LocalPort => $self->{proxy_port},
122 push @proxyargs, ReuseAddr => 1
123 unless $^O eq "MSWin32";
124 $self->{proxy_sock} = $IP_factory->(@proxyargs);
126 if ($self->{proxy_sock}) {
127 print "Proxy started on port ".$self->{proxy_port}."\n";
129 warn "Failed creating proxy socket (".$proxaddr.",".$self->{proxy_port}."): $!\n";
132 return bless $self, $class;
139 $self->{proxy_sock}->close() if $self->{proxy_sock};
146 $self->{cipherc} = "";
147 $self->{ciphersuitec} = "";
148 $self->{flight} = -1;
149 $self->{direction} = -1;
150 $self->{partial} = ["", ""];
151 $self->{record_list} = [];
152 $self->{message_list} = [];
153 $self->{clientflags} = "";
154 $self->{sessionfile} = undef;
155 $self->{clientpid} = 0;
157 $ciphersuite = undef;
159 TLSProxy::Message->clear();
160 TLSProxy::Record->clear();
168 $self->{ciphers} = "AES128-SHA";
169 $self->{ciphersuitess} = "TLS_AES_128_GCM_SHA256";
170 $self->{serverflags} = "";
171 $self->{serverconnects} = 1;
172 $self->{serverpid} = 0;
197 if ($self->{proxy_sock} == 0) {
203 my $execcmd = $self->execute
204 ." s_server -max_protocol TLSv1.3 -no_comp -rev -engine ossltest -accept "
205 .($self->server_port)
206 ." -cert ".$self->cert." -cert2 ".$self->cert
207 ." -naccept ".$self->serverconnects;
208 unless ($self->supports_IPv6) {
211 if ($self->ciphers ne "") {
212 $execcmd .= " -cipher ".$self->ciphers;
214 if ($self->ciphersuitess ne "") {
215 $execcmd .= " -ciphersuites ".$self->ciphersuitess;
217 if ($self->serverflags ne "") {
218 $execcmd .= " ".$self->serverflags;
221 print STDERR "Server command: $execcmd\n";
225 $self->serverpid($pid);
227 return $self->clientstart;
235 if ($self->execute) {
239 if ($self->reneg()) {
244 my $execcmd = "echo ".$echostr." | ".$self->execute
245 ." s_client -max_protocol TLSv1.3 -engine ossltest -connect "
246 .($self->proxy_addr).":".($self->proxy_port);
247 unless ($self->supports_IPv6) {
250 if ($self->cipherc ne "") {
251 $execcmd .= " -cipher ".$self->cipherc;
253 if ($self->ciphersuitesc ne "") {
254 $execcmd .= " -ciphersuites ".$self->ciphersuitesc;
256 if ($self->clientflags ne "") {
257 $execcmd .= " ".$self->clientflags;
259 if (defined $self->sessionfile) {
260 $execcmd .= " -ign_eof";
263 print STDERR "Client command: $execcmd\n";
267 $self->clientpid($pid);
270 # Wait for incoming connection from client
272 if(!($client_sock = $self->{proxy_sock}->accept())) {
273 warn "Failed accepting incoming connection: $!\n";
277 print "Connection opened\n";
279 # Now connect to the server
282 #We loop over this a few times because sometimes s_server can take a while
285 my $servaddr = $self->server_addr;
286 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
288 $server_sock = $IP_factory->(
289 PeerAddr => $servaddr,
290 PeerPort => $self->server_port,
297 #Some buggy IP factories can return a defined server_sock that hasn't
298 #actually connected, so we check peerport too
299 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
300 $server_sock->close() if defined($server_sock);
303 #Sleep for a short while
304 select(undef, undef, undef, 0.1);
306 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
310 } while (!$server_sock);
312 my $sel = IO::Select->new($server_sock, $client_sock);
314 my @handles = ($server_sock, $client_sock);
316 #Wait for either the server socket or the client socket to become readable
319 local $SIG{PIPE} = "IGNORE";
320 while( (!(TLSProxy::Message->end)
321 || (defined $self->sessionfile()
322 && (-s $self->sessionfile()) == 0))
324 if (!(@ready = $sel->can_read(1))) {
328 foreach my $hand (@ready) {
329 if ($hand == $server_sock) {
330 $server_sock->sysread($indata, 16384) or goto END;
331 $indata = $self->process_packet(1, $indata);
332 $client_sock->syswrite($indata);
334 } elsif ($hand == $client_sock) {
335 $client_sock->sysread($indata, 16384) or goto END;
336 $indata = $self->process_packet(0, $indata);
337 $server_sock->syswrite($indata);
340 die "Unexpected handle";
345 die "No progress made" if $ctr >= 10;
348 print "Connection closed\n";
350 $server_sock->close();
353 #Closing this also kills the child process
354 $client_sock->close();
359 $self->serverconnects($self->serverconnects - 1);
360 if ($self->serverconnects == 0) {
361 die "serverpid is zero\n" if $self->serverpid == 0;
362 print "Waiting for server process to close: "
363 .$self->serverpid."\n";
364 waitpid( $self->serverpid, 0);
365 die "exit code $? from server process\n" if $? != 0;
367 # Give s_server sufficient time to finish what it was doing
370 die "clientpid is zero\n" if $self->clientpid == 0;
371 print "Waiting for client process to close: ".$self->clientpid."\n";
372 waitpid($self->clientpid, 0);
379 my ($self, $server, $packet) = @_;
386 print "Received server packet\n";
388 print "Received client packet\n";
391 if ($self->{direction} != $server) {
392 $self->{flight} = $self->{flight} + 1;
393 $self->{direction} = $server;
396 print "Packet length = ".length($packet)."\n";
397 print "Processing flight ".$self->flight."\n";
399 #Return contains the list of record found in the packet followed by the
400 #list of messages in those records and any partial message
401 my @ret = TLSProxy::Record->get_records($server, $self->flight, $self->{partial}[$server].$packet);
402 $self->{partial}[$server] = $ret[2];
403 push @{$self->record_list}, @{$ret[0]};
404 push @{$self->{message_list}}, @{$ret[1]};
408 if (scalar(@{$ret[0]}) == 0 or length($ret[2]) != 0) {
412 #Finished parsing. Call user provided filter here
413 if (defined $self->filter) {
414 $self->filter->($self);
417 #Reconstruct the packet
419 foreach my $record (@{$self->record_list}) {
420 $packet .= $record->reconstruct_record($server);
423 print "Forwarded packet length = ".length($packet)."\n\n";
432 return $self->{execute};
437 return $self->{cert};
442 return $self->{debug};
447 return $self->{flight};
452 return $self->{record_list};
457 return $self->{success};
472 return $self->{proxy_addr};
477 return $self->{proxy_port};
480 #Read/write accessors
485 $self->{server_addr} = shift;
487 return $self->{server_addr};
493 $self->{server_port} = shift;
495 return $self->{server_port};
501 $self->{filter} = shift;
503 return $self->{filter};
509 $self->{cipherc} = shift;
511 return $self->{cipherc};
517 $self->{ciphersuitesc} = shift;
519 return $self->{ciphersuitesc};
525 $self->{ciphers} = shift;
527 return $self->{ciphers};
533 $self->{ciphersuitess} = shift;
535 return $self->{ciphersuitess};
541 $self->{serverflags} = shift;
543 return $self->{serverflags};
549 $self->{clientflags} = shift;
551 return $self->{clientflags};
557 $self->{serverconnects} = shift;
559 return $self->{serverconnects};
561 # This is a bit ugly because the caller is responsible for keeping the records
562 # in sync with the updated message list; simply updating the message list isn't
563 # sufficient to get the proxy to forward the new message.
564 # But it does the trick for the one test (test_sslsessiontick) that needs it.
569 $self->{message_list} = shift;
571 return $self->{message_list};
577 $self->{serverpid} = shift;
579 return $self->{serverpid};
585 $self->{clientpid} = shift;
587 return $self->{clientpid};
594 for (my $i = 0; $i < $length; $i++) {
613 $self->{reneg} = shift;
615 return $self->{reneg};
618 #Setting a sessionfile means that the client will not close until the given
619 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
620 #immediately at the end of the handshake, but before the session has been
621 #received from the server. A side effect of this is that s_client never sends
622 #a close_notify, so instead we consider success to be when it sends application
623 #data over the connection.
628 $self->{sessionfile} = shift;
629 TLSProxy::Message->successondata(1);
631 return $self->{sessionfile};
638 $ciphersuite = shift;