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",
69 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
70 # However, IO::Socket::INET6 is older and is said to be more widely
71 # deployed for the moment, and may have less bugs, so we try the latter
72 # first, then fall back on the code modules. Worst case scenario, we
73 # fall back to IO::Socket::INET, only supports IPv4.
75 require IO::Socket::INET6;
76 my $s = IO::Socket::INET6->new(
85 $IP_factory = sub { IO::Socket::INET6->new(@_); };
89 require IO::Socket::IP;
90 my $s = IO::Socket::IP->new(
99 $IP_factory = sub { IO::Socket::IP->new(@_); };
102 $IP_factory = sub { IO::Socket::INET->new(@_); };
106 # Create the Proxy socket
107 my $proxaddr = $self->{proxy_addr};
108 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
110 LocalHost => $proxaddr,
111 LocalPort => $self->{proxy_port},
115 push @proxyargs, ReuseAddr => 1
116 unless $^O eq "MSWin32";
117 $self->{proxy_sock} = $IP_factory->(@proxyargs);
119 if ($self->{proxy_sock}) {
120 print "Proxy started on port ".$self->{proxy_port}."\n";
122 warn "Failed creating proxy socket (".$proxaddr.",".$self->{proxy_port}."): $!\n";
125 return bless $self, $class;
132 $self->{proxy_sock}->close() if $self->{proxy_sock};
139 $self->{cipherc} = "";
140 $self->{ciphersuitec} = "";
142 $self->{record_list} = [];
143 $self->{message_list} = [];
144 $self->{clientflags} = "";
145 $self->{sessionfile} = undef;
146 $self->{clientpid} = 0;
148 $ciphersuite = undef;
150 TLSProxy::Message->clear();
151 TLSProxy::Record->clear();
159 $self->{ciphers} = "AES128-SHA";
160 $self->{ciphersuitess} = "TLS_AES_128_GCM_SHA256";
161 $self->{serverflags} = "";
162 $self->{serverconnects} = 1;
163 $self->{serverpid} = 0;
188 if ($self->{proxy_sock} == 0) {
194 my $execcmd = $self->execute
195 ." s_server -no_comp -rev -engine ossltest -accept "
196 .($self->server_port)
197 ." -cert ".$self->cert." -cert2 ".$self->cert
198 ." -naccept ".$self->serverconnects;
199 unless ($self->supports_IPv6) {
202 if ($self->ciphers ne "") {
203 $execcmd .= " -cipher ".$self->ciphers;
205 if ($self->ciphersuitess ne "") {
206 $execcmd .= " -ciphersuites ".$self->ciphersuitess;
208 if ($self->serverflags ne "") {
209 $execcmd .= " ".$self->serverflags;
212 print STDERR "Server command: $execcmd\n";
216 $self->serverpid($pid);
218 return $self->clientstart;
226 if ($self->execute) {
230 if ($self->reneg()) {
235 my $execcmd = "echo ".$echostr." | ".$self->execute
236 ." s_client -engine ossltest -connect "
237 .($self->proxy_addr).":".($self->proxy_port);
238 unless ($self->supports_IPv6) {
241 if ($self->cipherc ne "") {
242 $execcmd .= " -cipher ".$self->cipherc;
244 if ($self->ciphersuitesc ne "") {
245 $execcmd .= " -ciphersuites ".$self->ciphersuitesc;
247 if ($self->clientflags ne "") {
248 $execcmd .= " ".$self->clientflags;
250 if (defined $self->sessionfile) {
251 $execcmd .= " -ign_eof";
254 print STDERR "Client command: $execcmd\n";
258 $self->clientpid($pid);
261 # Wait for incoming connection from client
263 if(!($client_sock = $self->{proxy_sock}->accept())) {
264 warn "Failed accepting incoming connection: $!\n";
268 print "Connection opened\n";
270 # Now connect to the server
273 #We loop over this a few times because sometimes s_server can take a while
276 my $servaddr = $self->server_addr;
277 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
279 $server_sock = $IP_factory->(
280 PeerAddr => $servaddr,
281 PeerPort => $self->server_port,
288 #Some buggy IP factories can return a defined server_sock that hasn't
289 #actually connected, so we check peerport too
290 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
291 $server_sock->close() if defined($server_sock);
294 #Sleep for a short while
295 select(undef, undef, undef, 0.1);
297 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
301 } while (!$server_sock);
303 my $sel = IO::Select->new($server_sock, $client_sock);
305 my @handles = ($server_sock, $client_sock);
307 #Wait for either the server socket or the client socket to become readable
310 local $SIG{PIPE} = "IGNORE";
311 while( (!(TLSProxy::Message->end)
312 || (defined $self->sessionfile()
313 && (-s $self->sessionfile()) == 0))
315 if (!(@ready = $sel->can_read(1))) {
319 foreach my $hand (@ready) {
320 if ($hand == $server_sock) {
321 $server_sock->sysread($indata, 16384) or goto END;
322 $indata = $self->process_packet(1, $indata);
323 $client_sock->syswrite($indata);
325 } elsif ($hand == $client_sock) {
326 $client_sock->sysread($indata, 16384) or goto END;
327 $indata = $self->process_packet(0, $indata);
328 $server_sock->syswrite($indata);
331 die "Unexpected handle";
336 die "No progress made" if $ctr >= 10;
339 print "Connection closed\n";
341 $server_sock->close();
344 #Closing this also kills the child process
345 $client_sock->close();
350 $self->serverconnects($self->serverconnects - 1);
351 if ($self->serverconnects == 0) {
352 die "serverpid is zero\n" if $self->serverpid == 0;
353 print "Waiting for server process to close: "
354 .$self->serverpid."\n";
355 waitpid( $self->serverpid, 0);
356 die "exit code $? from server process\n" if $? != 0;
358 # Give s_server sufficient time to finish what it was doing
361 die "clientpid is zero\n" if $self->clientpid == 0;
362 print "Waiting for client process to close: ".$self->clientpid."\n";
363 waitpid($self->clientpid, 0);
370 my ($self, $server, $packet) = @_;
377 print "Received server packet\n";
379 print "Received client packet\n";
382 print "Packet length = ".length($packet)."\n";
383 print "Processing flight ".$self->flight."\n";
385 #Return contains the list of record found in the packet followed by the
386 #list of messages in those records
387 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
388 push @{$self->record_list}, @{$ret[0]};
389 push @{$self->{message_list}}, @{$ret[1]};
393 #Finished parsing. Call user provided filter here
394 if(defined $self->filter) {
395 $self->filter->($self);
398 #Reconstruct the packet
400 foreach my $record (@{$self->record_list}) {
401 #We only replay the records for the current flight
402 if ($record->flight != $self->flight) {
405 $packet .= $record->reconstruct_record($server);
408 $self->{flight} = $self->{flight} + 1;
410 print "Forwarded packet length = ".length($packet)."\n\n";
419 return $self->{execute};
424 return $self->{cert};
429 return $self->{debug};
434 return $self->{flight};
439 return $self->{record_list};
444 return $self->{success};
459 return $self->{proxy_addr};
464 return $self->{proxy_port};
467 #Read/write accessors
472 $self->{server_addr} = shift;
474 return $self->{server_addr};
480 $self->{server_port} = shift;
482 return $self->{server_port};
488 $self->{filter} = shift;
490 return $self->{filter};
496 $self->{cipherc} = shift;
498 return $self->{cipherc};
504 $self->{ciphersuitesc} = shift;
506 return $self->{ciphersuitesc};
512 $self->{ciphers} = shift;
514 return $self->{ciphers};
520 $self->{ciphersuitess} = shift;
522 return $self->{ciphersuitess};
528 $self->{serverflags} = shift;
530 return $self->{serverflags};
536 $self->{clientflags} = shift;
538 return $self->{clientflags};
544 $self->{serverconnects} = shift;
546 return $self->{serverconnects};
548 # This is a bit ugly because the caller is responsible for keeping the records
549 # in sync with the updated message list; simply updating the message list isn't
550 # sufficient to get the proxy to forward the new message.
551 # But it does the trick for the one test (test_sslsessiontick) that needs it.
556 $self->{message_list} = shift;
558 return $self->{message_list};
564 $self->{serverpid} = shift;
566 return $self->{serverpid};
572 $self->{clientpid} = shift;
574 return $self->{clientpid};
581 for (my $i = 0; $i < $length; $i++) {
600 $self->{reneg} = shift;
602 return $self->{reneg};
605 #Setting a sessionfile means that the client will not close until the given
606 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
607 #immediately at the end of the handshake, but before the session has been
608 #received from the server. A side effect of this is that s_client never sends
609 #a close_notify, so instead we consider success to be when it sends application
610 #data over the connection.
615 $self->{sessionfile} = shift;
616 TLSProxy::Message->successondata(1);
618 return $self->{sessionfile};
625 $ciphersuite = shift;