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::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",
61 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
67 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
68 # However, IO::Socket::INET6 is older and is said to be more widely
69 # deployed for the moment, and may have less bugs, so we try the latter
70 # first, then fall back on the code modules. Worst case scenario, we
71 # fall back to IO::Socket::INET, only supports IPv4.
73 require IO::Socket::INET6;
74 my $s = IO::Socket::INET6->new(
83 $IP_factory = sub { IO::Socket::INET6->new(@_); };
87 require IO::Socket::IP;
88 my $s = IO::Socket::IP->new(
97 $IP_factory = sub { IO::Socket::IP->new(@_); };
100 $IP_factory = sub { IO::Socket::INET->new(@_); };
104 return bless $self, $class;
111 $self->{cipherc} = "";
113 $self->{record_list} = [];
114 $self->{message_list} = [];
115 $self->{clientflags} = "";
116 $self->{sessionfile} = undef;
117 $self->{clientpid} = 0;
119 $ciphersuite = undef;
121 TLSProxy::Message->clear();
122 TLSProxy::Record->clear();
130 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
131 $self->{serverflags} = "";
132 $self->{serverconnects} = 1;
133 $self->{serverpid} = 0;
161 open(STDOUT, ">", File::Spec->devnull())
162 or die "Failed to redirect stdout: $!";
163 open(STDERR, ">&STDOUT");
165 my $execcmd = $self->execute
166 ." s_server -no_comp -rev -engine ossltest -accept "
167 .($self->server_port)
168 ." -cert ".$self->cert." -cert2 ".$self->cert
169 ." -naccept ".$self->serverconnects;
170 unless ($self->supports_IPv6) {
173 if ($self->ciphers ne "") {
174 $execcmd .= " -cipher ".$self->ciphers;
176 if ($self->serverflags ne "") {
177 $execcmd .= " ".$self->serverflags;
180 print STDERR "Server command: $execcmd\n";
184 $self->serverpid($pid);
186 return $self->clientstart;
195 open DEVNULL, ">", File::Spec->devnull();
196 $oldstdout = select(DEVNULL);
199 # Create the Proxy socket
200 my $proxaddr = $self->proxy_addr;
201 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
202 my $proxy_sock = $IP_factory->(
203 LocalHost => $proxaddr,
204 LocalPort => $self->proxy_port,
211 print "Proxy started on port ".$self->proxy_port."\n";
213 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
217 if ($self->execute) {
221 open(STDOUT, ">", File::Spec->devnull())
222 or die "Failed to redirect stdout: $!";
223 open(STDERR, ">&STDOUT");
226 if ($self->reneg()) {
231 my $execcmd = "echo ".$echostr." | ".$self->execute
232 ." s_client -engine ossltest -connect "
233 .($self->proxy_addr).":".($self->proxy_port);
234 unless ($self->supports_IPv6) {
237 if ($self->cipherc ne "") {
238 $execcmd .= " -cipher ".$self->cipherc;
240 if ($self->clientflags ne "") {
241 $execcmd .= " ".$self->clientflags;
243 if (defined $self->sessionfile) {
244 $execcmd .= " -ign_eof";
247 print STDERR "Client command: $execcmd\n";
251 $self->clientpid($pid);
254 # Wait for incoming connection from client
256 if(!($client_sock = $proxy_sock->accept())) {
257 warn "Failed accepting incoming connection: $!\n";
261 print "Connection opened\n";
263 # Now connect to the server
266 #We loop over this a few times because sometimes s_server can take a while
269 my $servaddr = $self->server_addr;
270 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
272 $server_sock = $IP_factory->(
273 PeerAddr => $servaddr,
274 PeerPort => $self->server_port,
281 #Some buggy IP factories can return a defined server_sock that hasn't
282 #actually connected, so we check peerport too
283 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
284 $server_sock->close() if defined($server_sock);
287 #Sleep for a short while
288 select(undef, undef, undef, 0.1);
290 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
294 } while (!$server_sock);
296 my $sel = IO::Select->new($server_sock, $client_sock);
298 my @handles = ($server_sock, $client_sock);
300 #Wait for either the server socket or the client socket to become readable
303 local $SIG{PIPE} = "IGNORE";
304 while( (!(TLSProxy::Message->end)
305 || (defined $self->sessionfile()
306 && (-s $self->sessionfile()) == 0))
308 if (!(@ready = $sel->can_read(1))) {
312 foreach my $hand (@ready) {
313 if ($hand == $server_sock) {
314 $server_sock->sysread($indata, 16384) or goto END;
315 $indata = $self->process_packet(1, $indata);
316 $client_sock->syswrite($indata);
318 } elsif ($hand == $client_sock) {
319 $client_sock->sysread($indata, 16384) or goto END;
320 $indata = $self->process_packet(0, $indata);
321 $server_sock->syswrite($indata);
324 die "Unexpected handle";
329 die "No progress made" if $ctr >= 10;
332 print "Connection closed\n";
334 $server_sock->close();
337 #Closing this also kills the child process
338 $client_sock->close();
341 $proxy_sock->close();
346 $self->serverconnects($self->serverconnects - 1);
347 if ($self->serverconnects == 0) {
348 die "serverpid is zero\n" if $self->serverpid == 0;
349 print "Waiting for server process to close: "
350 .$self->serverpid."\n";
351 waitpid( $self->serverpid, 0);
352 die "exit code $? from server process\n" if $? != 0;
354 # Give s_server sufficient time to finish what it was doing
357 die "clientpid is zero\n" if $self->clientpid == 0;
358 print "Waiting for client process to close: ".$self->clientpid."\n";
359 waitpid($self->clientpid, 0);
366 my ($self, $server, $packet) = @_;
373 print "Received server packet\n";
375 print "Received client packet\n";
378 print "Packet length = ".length($packet)."\n";
379 print "Processing flight ".$self->flight."\n";
381 #Return contains the list of record found in the packet followed by the
382 #list of messages in those records
383 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
384 push @{$self->record_list}, @{$ret[0]};
385 push @{$self->{message_list}}, @{$ret[1]};
389 #Finished parsing. Call user provided filter here
390 if(defined $self->filter) {
391 $self->filter->($self);
394 #Reconstruct the packet
396 foreach my $record (@{$self->record_list}) {
397 #We only replay the records for the current flight
398 if ($record->flight != $self->flight) {
401 $packet .= $record->reconstruct_record($server);
404 $self->{flight} = $self->{flight} + 1;
406 print "Forwarded packet length = ".length($packet)."\n\n";
415 return $self->{execute};
420 return $self->{cert};
425 return $self->{debug};
430 return $self->{flight};
435 return $self->{record_list};
440 return $self->{success};
453 #Read/write accessors
458 $self->{proxy_addr} = shift;
460 return $self->{proxy_addr};
466 $self->{proxy_port} = shift;
468 return $self->{proxy_port};
474 $self->{server_addr} = shift;
476 return $self->{server_addr};
482 $self->{server_port} = shift;
484 return $self->{server_port};
490 $self->{filter} = shift;
492 return $self->{filter};
498 $self->{cipherc} = shift;
500 return $self->{cipherc};
506 $self->{ciphers} = shift;
508 return $self->{ciphers};
514 $self->{serverflags} = shift;
516 return $self->{serverflags};
522 $self->{clientflags} = shift;
524 return $self->{clientflags};
530 $self->{serverconnects} = shift;
532 return $self->{serverconnects};
534 # This is a bit ugly because the caller is responsible for keeping the records
535 # in sync with the updated message list; simply updating the message list isn't
536 # sufficient to get the proxy to forward the new message.
537 # But it does the trick for the one test (test_sslsessiontick) that needs it.
542 $self->{message_list} = shift;
544 return $self->{message_list};
550 $self->{serverpid} = shift;
552 return $self->{serverpid};
558 $self->{clientpid} = shift;
560 return $self->{clientpid};
567 for (my $i = 0; $i < $length; $i++) {
586 $self->{reneg} = shift;
588 return $self->{reneg};
591 #Setting a sessionfile means that the client will not close until the given
592 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
593 #immediately at the end of the handshake, but before the session has been
594 #received from the server. A side effect of this is that s_client never sends
595 #a close_notify, so instead we consider success to be when it sends application
596 #data over the connection.
601 $self->{sessionfile} = shift;
602 TLSProxy::Message->successondata(1);
604 return $self->{sessionfile};
611 $ciphersuite = shift;