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 # Create the Proxy socket
105 my $proxaddr = $self->{proxy_addr};
106 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
108 LocalHost => $proxaddr,
109 LocalPort => $self->{proxy_port},
113 push @proxyargs, ReuseAddr => 1
114 unless $^O eq "MSWin32";
115 $self->{proxy_sock} = $IP_factory->(@proxyargs);
117 if ($self->{proxy_sock}) {
118 print "Proxy started on port ".$self->{proxy_port}."\n";
120 warn "Failed creating proxy socket (".$proxaddr.",".$self->{proxy_port}."): $!\n";
123 return bless $self, $class;
130 $self->{proxy_sock}->close() if $self->{proxy_sock};
137 $self->{cipherc} = "";
139 $self->{record_list} = [];
140 $self->{message_list} = [];
141 $self->{clientflags} = "";
142 $self->{sessionfile} = undef;
143 $self->{clientpid} = 0;
145 $ciphersuite = undef;
147 TLSProxy::Message->clear();
148 TLSProxy::Record->clear();
156 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
157 $self->{serverflags} = "";
158 $self->{serverconnects} = 1;
159 $self->{serverpid} = 0;
184 if ($self->{proxy_sock} == 0) {
190 my $execcmd = $self->execute
191 ." s_server -no_comp -rev -engine ossltest -accept "
192 .($self->server_port)
193 ." -cert ".$self->cert." -cert2 ".$self->cert
194 ." -naccept ".$self->serverconnects;
195 unless ($self->supports_IPv6) {
198 if ($self->ciphers ne "") {
199 $execcmd .= " -cipher ".$self->ciphers;
201 if ($self->serverflags ne "") {
202 $execcmd .= " ".$self->serverflags;
205 print STDERR "Server command: $execcmd\n";
209 $self->serverpid($pid);
211 return $self->clientstart;
219 if ($self->execute) {
223 if ($self->reneg()) {
228 my $execcmd = "echo ".$echostr." | ".$self->execute
229 ." s_client -engine ossltest -connect "
230 .($self->proxy_addr).":".($self->proxy_port);
231 unless ($self->supports_IPv6) {
234 if ($self->cipherc ne "") {
235 $execcmd .= " -cipher ".$self->cipherc;
237 if ($self->clientflags ne "") {
238 $execcmd .= " ".$self->clientflags;
240 if (defined $self->sessionfile) {
241 $execcmd .= " -ign_eof";
244 print STDERR "Client command: $execcmd\n";
248 $self->clientpid($pid);
251 # Wait for incoming connection from client
253 if(!($client_sock = $self->{proxy_sock}->accept())) {
254 warn "Failed accepting incoming connection: $!\n";
258 print "Connection opened\n";
260 # Now connect to the server
263 #We loop over this a few times because sometimes s_server can take a while
266 my $servaddr = $self->server_addr;
267 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
269 $server_sock = $IP_factory->(
270 PeerAddr => $servaddr,
271 PeerPort => $self->server_port,
278 #Some buggy IP factories can return a defined server_sock that hasn't
279 #actually connected, so we check peerport too
280 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
281 $server_sock->close() if defined($server_sock);
284 #Sleep for a short while
285 select(undef, undef, undef, 0.1);
287 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
291 } while (!$server_sock);
293 my $sel = IO::Select->new($server_sock, $client_sock);
295 my @handles = ($server_sock, $client_sock);
297 #Wait for either the server socket or the client socket to become readable
300 local $SIG{PIPE} = "IGNORE";
301 while( (!(TLSProxy::Message->end)
302 || (defined $self->sessionfile()
303 && (-s $self->sessionfile()) == 0))
305 if (!(@ready = $sel->can_read(1))) {
309 foreach my $hand (@ready) {
310 if ($hand == $server_sock) {
311 $server_sock->sysread($indata, 16384) or goto END;
312 $indata = $self->process_packet(1, $indata);
313 $client_sock->syswrite($indata);
315 } elsif ($hand == $client_sock) {
316 $client_sock->sysread($indata, 16384) or goto END;
317 $indata = $self->process_packet(0, $indata);
318 $server_sock->syswrite($indata);
321 die "Unexpected handle";
326 die "No progress made" if $ctr >= 10;
329 print "Connection closed\n";
331 $server_sock->close();
334 #Closing this also kills the child process
335 $client_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};
449 return $self->{proxy_addr};
454 return $self->{proxy_port};
457 #Read/write accessors
462 $self->{server_addr} = shift;
464 return $self->{server_addr};
470 $self->{server_port} = shift;
472 return $self->{server_port};
478 $self->{filter} = shift;
480 return $self->{filter};
486 $self->{cipherc} = shift;
488 return $self->{cipherc};
494 $self->{ciphers} = shift;
496 return $self->{ciphers};
502 $self->{serverflags} = shift;
504 return $self->{serverflags};
510 $self->{clientflags} = shift;
512 return $self->{clientflags};
518 $self->{serverconnects} = shift;
520 return $self->{serverconnects};
522 # This is a bit ugly because the caller is responsible for keeping the records
523 # in sync with the updated message list; simply updating the message list isn't
524 # sufficient to get the proxy to forward the new message.
525 # But it does the trick for the one test (test_sslsessiontick) that needs it.
530 $self->{message_list} = shift;
532 return $self->{message_list};
538 $self->{serverpid} = shift;
540 return $self->{serverpid};
546 $self->{clientpid} = shift;
548 return $self->{clientpid};
555 for (my $i = 0; $i < $length; $i++) {
574 $self->{reneg} = shift;
576 return $self->{reneg};
579 #Setting a sessionfile means that the client will not close until the given
580 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
581 #immediately at the end of the handshake, but before the session has been
582 #received from the server. A side effect of this is that s_client never sends
583 #a close_notify, so instead we consider success to be when it sends application
584 #data over the connection.
589 $self->{sessionfile} = shift;
590 TLSProxy::Message->successondata(1);
592 return $self->{sessionfile};
599 $ciphersuite = shift;