1 # Copyright 2016 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::HelloRetryRequest;
20 use TLSProxy::ServerHello;
21 use TLSProxy::EncryptedExtensions;
22 use TLSProxy::Certificate;
23 use TLSProxy::CertificateVerify;
24 use TLSProxy::ServerKeyExchange;
25 use TLSProxy::NewSessionTicket;
26 use Time::HiRes qw/usleep/;
32 my $ciphersuite = undef;
44 proxy_addr => "localhost",
46 server_addr => "localhost",
62 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
68 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
69 # However, IO::Socket::INET6 is older and is said to be more widely
70 # deployed for the moment, and may have less bugs, so we try the latter
71 # first, then fall back on the code modules. Worst case scenario, we
72 # fall back to IO::Socket::INET, only supports IPv4.
74 require IO::Socket::INET6;
75 my $s = IO::Socket::INET6->new(
84 $IP_factory = sub { IO::Socket::INET6->new(@_); };
88 require IO::Socket::IP;
89 my $s = IO::Socket::IP->new(
98 $IP_factory = sub { IO::Socket::IP->new(@_); };
101 $IP_factory = sub { IO::Socket::INET->new(@_); };
105 return bless $self, $class;
112 $self->{cipherc} = "";
114 $self->{record_list} = [];
115 $self->{message_list} = [];
116 $self->{clientflags} = "";
117 $self->{sessionfile} = undef;
118 $self->{clientpid} = 0;
120 $ciphersuite = undef;
122 TLSProxy::Message->clear();
123 TLSProxy::Record->clear();
131 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
132 $self->{serverflags} = "";
133 $self->{serverconnects} = 1;
134 $self->{serverpid} = 0;
162 open(STDOUT, ">", File::Spec->devnull())
163 or die "Failed to redirect stdout: $!";
164 open(STDERR, ">&STDOUT");
166 my $execcmd = $self->execute
167 ." s_server -no_comp -rev -engine ossltest -accept "
168 .($self->server_port)
169 ." -cert ".$self->cert." -cert2 ".$self->cert
170 ." -naccept ".$self->serverconnects;
171 if ($self->ciphers ne "") {
172 $execcmd .= " -cipher ".$self->ciphers;
174 if ($self->serverflags ne "") {
175 $execcmd .= " ".$self->serverflags;
178 print STDERR "Server command: $execcmd\n";
182 $self->serverpid($pid);
184 return $self->clientstart;
193 open DEVNULL, ">", File::Spec->devnull();
194 $oldstdout = select(DEVNULL);
197 # Create the Proxy socket
198 my $proxaddr = $self->proxy_addr;
199 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
200 my $proxy_sock = $IP_factory->(
201 LocalHost => $proxaddr,
202 LocalPort => $self->proxy_port,
209 print "Proxy started on port ".$self->proxy_port."\n";
211 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
215 if ($self->execute) {
219 open(STDOUT, ">", File::Spec->devnull())
220 or die "Failed to redirect stdout: $!";
221 open(STDERR, ">&STDOUT");
224 if ($self->reneg()) {
229 my $execcmd = "echo ".$echostr." | ".$self->execute
230 ." s_client -engine ossltest -connect "
231 .($self->proxy_addr).":".($self->proxy_port);
232 if ($self->cipherc ne "") {
233 $execcmd .= " -cipher ".$self->cipherc;
235 if ($self->clientflags ne "") {
236 $execcmd .= " ".$self->clientflags;
238 if (defined $self->sessionfile) {
239 $execcmd .= " -ign_eof";
242 print STDERR "Client command: $execcmd\n";
246 $self->clientpid($pid);
249 # Wait for incoming connection from client
251 if(!($client_sock = $proxy_sock->accept())) {
252 warn "Failed accepting incoming connection: $!\n";
256 print "Connection opened\n";
258 # Now connect to the server
261 #We loop over this a few times because sometimes s_server can take a while
264 my $servaddr = $self->server_addr;
265 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
267 $server_sock = $IP_factory->(
268 PeerAddr => $servaddr,
269 PeerPort => $self->server_port,
276 #Some buggy IP factories can return a defined server_sock that hasn't
277 #actually connected, so we check peerport too
278 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
279 $server_sock->close() if defined($server_sock);
282 #Sleep for a short while
283 select(undef, undef, undef, 0.1);
285 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
289 } while (!$server_sock);
291 my $sel = IO::Select->new($server_sock, $client_sock);
293 my @handles = ($server_sock, $client_sock);
295 #Wait for either the server socket or the client socket to become readable
298 while( (!(TLSProxy::Message->end)
299 || (defined $self->sessionfile()
300 && (-s $self->sessionfile()) == 0))
302 if (!(@ready = $sel->can_read(1))) {
306 foreach my $hand (@ready) {
307 if ($hand == $server_sock) {
308 $server_sock->sysread($indata, 16384) or goto END;
309 $indata = $self->process_packet(1, $indata);
310 $client_sock->syswrite($indata);
312 } elsif ($hand == $client_sock) {
313 $client_sock->sysread($indata, 16384) or goto END;
314 $indata = $self->process_packet(0, $indata);
315 $server_sock->syswrite($indata);
318 die "Unexpected handle";
323 die "No progress made" if $ctr >= 10;
326 print "Connection closed\n";
328 $server_sock->close();
331 #Closing this also kills the child process
332 $client_sock->close();
335 $proxy_sock->close();
340 $self->serverconnects($self->serverconnects - 1);
341 if ($self->serverconnects == 0) {
342 die "serverpid is zero\n" if $self->serverpid == 0;
343 print "Waiting for server process to close: "
344 .$self->serverpid."\n";
345 waitpid( $self->serverpid, 0);
346 die "exit code $? from server process\n" if $? != 0;
348 # Give s_server sufficient time to finish what it was doing
351 die "clientpid is zero\n" if $self->clientpid == 0;
352 print "Waiting for client process to close: ".$self->clientpid."\n";
353 waitpid($self->clientpid, 0);
360 my ($self, $server, $packet) = @_;
367 print "Received server packet\n";
369 print "Received client packet\n";
372 print "Packet length = ".length($packet)."\n";
373 print "Processing flight ".$self->flight."\n";
375 #Return contains the list of record found in the packet followed by the
376 #list of messages in those records
377 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
378 push @{$self->record_list}, @{$ret[0]};
379 push @{$self->{message_list}}, @{$ret[1]};
383 #Finished parsing. Call user provided filter here
384 if(defined $self->filter) {
385 $self->filter->($self);
388 #Reconstruct the packet
390 foreach my $record (@{$self->record_list}) {
391 #We only replay the records for the current flight
392 if ($record->flight != $self->flight) {
395 $packet .= $record->reconstruct_record($server);
398 $self->{flight} = $self->{flight} + 1;
400 print "Forwarded packet length = ".length($packet)."\n\n";
409 return $self->{execute};
414 return $self->{cert};
419 return $self->{debug};
424 return $self->{flight};
429 return $self->{record_list};
434 return $self->{success};
447 #Read/write accessors
452 $self->{proxy_addr} = shift;
454 return $self->{proxy_addr};
460 $self->{proxy_port} = shift;
462 return $self->{proxy_port};
468 $self->{server_addr} = shift;
470 return $self->{server_addr};
476 $self->{server_port} = shift;
478 return $self->{server_port};
484 $self->{filter} = shift;
486 return $self->{filter};
492 $self->{cipherc} = shift;
494 return $self->{cipherc};
500 $self->{ciphers} = shift;
502 return $self->{ciphers};
508 $self->{serverflags} = shift;
510 return $self->{serverflags};
516 $self->{clientflags} = shift;
518 return $self->{clientflags};
524 $self->{serverconnects} = shift;
526 return $self->{serverconnects};
528 # This is a bit ugly because the caller is responsible for keeping the records
529 # in sync with the updated message list; simply updating the message list isn't
530 # sufficient to get the proxy to forward the new message.
531 # But it does the trick for the one test (test_sslsessiontick) that needs it.
536 $self->{message_list} = shift;
538 return $self->{message_list};
544 $self->{serverpid} = shift;
546 return $self->{serverpid};
552 $self->{clientpid} = shift;
554 return $self->{clientpid};
561 for (my $i = 0; $i < $length; $i++) {
580 $self->{reneg} = shift;
582 return $self->{reneg};
585 #Setting a sessionfile means that the client will not close until the given
586 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
587 #immediately at the end of the handshake, but before the session has been
588 #received from the server. A side effect of this is that s_client never sends
589 #a close_notify, so instead we consider success to be when it sends application
590 #data over the connection.
595 $self->{sessionfile} = shift;
596 TLSProxy::Message->successondata(1);
598 return $self->{sessionfile};
605 $ciphersuite = shift;