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;
31 my $ciphersuite = undef;
43 proxy_addr => "localhost",
45 server_addr => "localhost",
60 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
66 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
67 # However, IO::Socket::INET6 is older and is said to be more widely
68 # deployed for the moment, and may have less bugs, so we try the latter
69 # first, then fall back on the code modules. Worst case scenario, we
70 # fall back to IO::Socket::INET, only supports IPv4.
72 require IO::Socket::INET6;
73 my $s = IO::Socket::INET6->new(
82 $IP_factory = sub { IO::Socket::INET6->new(@_); };
86 require IO::Socket::IP;
87 my $s = IO::Socket::IP->new(
96 $IP_factory = sub { IO::Socket::IP->new(@_); };
99 $IP_factory = sub { IO::Socket::INET->new(@_); };
103 return bless $self, $class;
110 $self->{cipherc} = "";
112 $self->{record_list} = [];
113 $self->{message_list} = [];
114 $self->{clientflags} = "";
115 $self->{sessionfile} = undef;
117 $ciphersuite = undef;
119 TLSProxy::Message->clear();
120 TLSProxy::Record->clear();
128 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
129 $self->{serverflags} = "";
130 $self->{serverconnects} = 1;
131 $self->{serverpid} = 0;
159 open(STDOUT, ">", File::Spec->devnull())
160 or die "Failed to redirect stdout: $!";
161 open(STDERR, ">&STDOUT");
163 my $execcmd = $self->execute
164 ." s_server -no_comp -rev -engine ossltest -accept "
165 .($self->server_port)
166 ." -cert ".$self->cert." -cert2 ".$self->cert
167 ." -naccept ".$self->serverconnects;
168 if ($self->ciphers ne "") {
169 $execcmd .= " -cipher ".$self->ciphers;
171 if ($self->serverflags ne "") {
172 $execcmd .= " ".$self->serverflags;
175 print STDERR "Server command: $execcmd\n";
179 $self->serverpid($pid);
181 return $self->clientstart;
190 open DEVNULL, ">", File::Spec->devnull();
191 $oldstdout = select(DEVNULL);
194 # Create the Proxy socket
195 my $proxaddr = $self->proxy_addr;
196 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
197 my $proxy_sock = $IP_factory->(
198 LocalHost => $proxaddr,
199 LocalPort => $self->proxy_port,
206 print "Proxy started on port ".$self->proxy_port."\n";
208 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
212 if ($self->execute) {
216 open(STDOUT, ">", File::Spec->devnull())
217 or die "Failed to redirect stdout: $!";
218 open(STDERR, ">&STDOUT");
221 if ($self->reneg()) {
226 my $execcmd = "echo ".$echostr." | ".$self->execute
227 ." s_client -engine ossltest -connect "
228 .($self->proxy_addr).":".($self->proxy_port);
229 if ($self->cipherc ne "") {
230 $execcmd .= " -cipher ".$self->cipherc;
232 if ($self->clientflags ne "") {
233 $execcmd .= " ".$self->clientflags;
235 if (defined $self->sessionfile) {
236 $execcmd .= " -ign_eof";
239 print STDERR "Client command: $execcmd\n";
245 # Wait for incoming connection from client
247 if(!($client_sock = $proxy_sock->accept())) {
248 warn "Failed accepting incoming connection: $!\n";
252 print "Connection opened\n";
254 # Now connect to the server
257 #We loop over this a few times because sometimes s_server can take a while
260 my $servaddr = $self->server_addr;
261 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
263 $server_sock = $IP_factory->(
264 PeerAddr => $servaddr,
265 PeerPort => $self->server_port,
272 #Some buggy IP factories can return a defined server_sock that hasn't
273 #actually connected, so we check peerport too
274 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
275 $server_sock->close() if defined($server_sock);
278 #Sleep for a short while
279 select(undef, undef, undef, 0.1);
281 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
285 } while (!$server_sock);
287 my $sel = IO::Select->new($server_sock, $client_sock);
289 my @handles = ($server_sock, $client_sock);
291 #Wait for either the server socket or the client socket to become readable
294 while( (!(TLSProxy::Message->end)
295 || (defined $self->sessionfile()
296 && (-s $self->sessionfile()) == 0))
298 && (@ready = $sel->can_read(1))) {
299 foreach my $hand (@ready) {
300 if ($hand == $server_sock) {
301 $server_sock->sysread($indata, 16384) or goto END;
302 $indata = $self->process_packet(1, $indata);
303 $client_sock->syswrite($indata);
305 } elsif ($hand == $client_sock) {
306 $client_sock->sysread($indata, 16384) or goto END;
307 $indata = $self->process_packet(0, $indata);
308 $server_sock->syswrite($indata);
316 die "No progress made" if $ctr >= 10;
319 print "Connection closed\n";
321 $server_sock->close();
324 #Closing this also kills the child process
325 $client_sock->close();
328 $proxy_sock->close();
333 $self->serverconnects($self->serverconnects - 1);
334 if ($self->serverconnects == 0) {
335 die "serverpid is zero\n" if $self->serverpid == 0;
336 print "Waiting for server process to close: "
337 .$self->serverpid."\n";
338 waitpid( $self->serverpid, 0);
339 die "exit code $? from server process\n" if $? != 0;
346 my ($self, $server, $packet) = @_;
353 print "Received server packet\n";
355 print "Received client packet\n";
358 print "Packet length = ".length($packet)."\n";
359 print "Processing flight ".$self->flight."\n";
361 #Return contains the list of record found in the packet followed by the
362 #list of messages in those records
363 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
364 push @{$self->record_list}, @{$ret[0]};
365 push @{$self->{message_list}}, @{$ret[1]};
369 #Finished parsing. Call user provided filter here
370 if(defined $self->filter) {
371 $self->filter->($self);
374 #Reconstruct the packet
376 foreach my $record (@{$self->record_list}) {
377 #We only replay the records for the current flight
378 if ($record->flight != $self->flight) {
381 $packet .= $record->reconstruct_record($server);
384 $self->{flight} = $self->{flight} + 1;
386 print "Forwarded packet length = ".length($packet)."\n\n";
395 return $self->{execute};
400 return $self->{cert};
405 return $self->{debug};
410 return $self->{flight};
415 return $self->{record_list};
420 return $self->{success};
433 #Read/write accessors
438 $self->{proxy_addr} = shift;
440 return $self->{proxy_addr};
446 $self->{proxy_port} = shift;
448 return $self->{proxy_port};
454 $self->{server_addr} = shift;
456 return $self->{server_addr};
462 $self->{server_port} = shift;
464 return $self->{server_port};
470 $self->{filter} = shift;
472 return $self->{filter};
478 $self->{cipherc} = shift;
480 return $self->{cipherc};
486 $self->{ciphers} = shift;
488 return $self->{ciphers};
494 $self->{serverflags} = shift;
496 return $self->{serverflags};
502 $self->{clientflags} = shift;
504 return $self->{clientflags};
510 $self->{serverconnects} = shift;
512 return $self->{serverconnects};
514 # This is a bit ugly because the caller is responsible for keeping the records
515 # in sync with the updated message list; simply updating the message list isn't
516 # sufficient to get the proxy to forward the new message.
517 # But it does the trick for the one test (test_sslsessiontick) that needs it.
522 $self->{message_list} = shift;
524 return $self->{message_list};
530 $self->{serverpid} = shift;
532 return $self->{serverpid};
539 for (my $i = 0; $i < $length; $i++) {
558 $self->{reneg} = shift;
560 return $self->{reneg};
563 #Setting a sessionfile means that the client will not close until the given
564 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
565 #immediately at the end of the handshake, but before the session has been
566 #received from the server. A side effect of this is that s_client never sends
567 #a close_notify, so instead we consider success to be when it sends application
568 #data over the connection.
573 $self->{sessionfile} = shift;
574 TLSProxy::Message->successondata(1);
576 return $self->{sessionfile};
583 $ciphersuite = shift;