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;
30 my $ciphersuite = undef;
42 proxy_addr => "localhost",
44 server_addr => "localhost",
59 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
65 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
66 # However, IO::Socket::INET6 is older and is said to be more widely
67 # deployed for the moment, and may have less bugs, so we try the latter
68 # first, then fall back on the code modules. Worst case scenario, we
69 # fall back to IO::Socket::INET, only supports IPv4.
71 require IO::Socket::INET6;
72 my $s = IO::Socket::INET6->new(
81 $IP_factory = sub { IO::Socket::INET6->new(@_); };
85 require IO::Socket::IP;
86 my $s = IO::Socket::IP->new(
95 $IP_factory = sub { IO::Socket::IP->new(@_); };
98 $IP_factory = sub { IO::Socket::INET->new(@_); };
102 return bless $self, $class;
109 $self->{cipherc} = "";
111 $self->{record_list} = [];
112 $self->{message_list} = [];
113 $self->{clientflags} = "";
114 $self->{sessionfile} = undef;
116 $ciphersuite = undef;
118 TLSProxy::Message->clear();
119 TLSProxy::Record->clear();
127 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
128 $self->{serverflags} = "";
129 $self->{serverconnects} = 1;
130 $self->{serverpid} = 0;
158 open(STDOUT, ">", File::Spec->devnull())
159 or die "Failed to redirect stdout: $!";
160 open(STDERR, ">&STDOUT");
162 my $execcmd = $self->execute
163 ." s_server -no_comp -rev -engine ossltest -accept "
164 .($self->server_port)
165 ." -cert ".$self->cert." -cert2 ".$self->cert
166 ." -naccept ".$self->serverconnects;
167 if ($self->ciphers ne "") {
168 $execcmd .= " -cipher ".$self->ciphers;
170 if ($self->serverflags ne "") {
171 $execcmd .= " ".$self->serverflags;
175 $self->serverpid($pid);
177 return $self->clientstart;
186 open DEVNULL, ">", File::Spec->devnull();
187 $oldstdout = select(DEVNULL);
190 # Create the Proxy socket
191 my $proxaddr = $self->proxy_addr;
192 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
193 my $proxy_sock = $IP_factory->(
194 LocalHost => $proxaddr,
195 LocalPort => $self->proxy_port,
202 print "Proxy started on port ".$self->proxy_port."\n";
204 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
208 if ($self->execute) {
212 open(STDOUT, ">", File::Spec->devnull())
213 or die "Failed to redirect stdout: $!";
214 open(STDERR, ">&STDOUT");
217 if ($self->reneg()) {
222 my $execcmd = "echo ".$echostr." | ".$self->execute
223 ." s_client -engine ossltest -connect "
224 .($self->proxy_addr).":".($self->proxy_port);
225 if ($self->cipherc ne "") {
226 $execcmd .= " -cipher ".$self->cipherc;
228 if ($self->clientflags ne "") {
229 $execcmd .= " ".$self->clientflags;
231 if (defined $self->sessionfile) {
232 $execcmd .= " -ign_eof";
238 # Wait for incoming connection from client
240 if(!($client_sock = $proxy_sock->accept())) {
241 warn "Failed accepting incoming connection: $!\n";
245 print "Connection opened\n";
247 # Now connect to the server
250 #We loop over this a few times because sometimes s_server can take a while
253 my $servaddr = $self->server_addr;
254 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
256 $server_sock = $IP_factory->(
257 PeerAddr => $servaddr,
258 PeerPort => $self->server_port,
265 #Some buggy IP factories can return a defined server_sock that hasn't
266 #actually connected, so we check peerport too
267 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
268 $server_sock->close() if defined($server_sock);
271 #Sleep for a short while
272 select(undef, undef, undef, 0.1);
274 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
278 } while (!$server_sock);
280 my $sel = IO::Select->new($server_sock, $client_sock);
282 my @handles = ($server_sock, $client_sock);
284 #Wait for either the server socket or the client socket to become readable
286 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
287 foreach my $hand (@ready) {
288 if ($hand == $server_sock) {
289 $server_sock->sysread($indata, 16384) or goto END;
290 $indata = $self->process_packet(1, $indata);
291 $client_sock->syswrite($indata);
292 } elsif ($hand == $client_sock) {
293 $client_sock->sysread($indata, 16384) or goto END;
294 $indata = $self->process_packet(0, $indata);
295 $server_sock->syswrite($indata);
304 defined $self->sessionfile()
305 && (!(-f $self->sessionfile()) || $ctr == 3);
310 die "Session file not created"
311 if (defined $self->sessionfile() && !(-f $self->sessionfile()));
314 print "Connection closed\n";
316 $server_sock->close();
319 #Closing this also kills the child process
320 $client_sock->close();
323 $proxy_sock->close();
328 $self->serverconnects($self->serverconnects - 1);
329 if ($self->serverconnects == 0) {
330 die "serverpid is zero\n" if $self->serverpid == 0;
331 print "Waiting for server process to close: "
332 .$self->serverpid."\n";
333 waitpid( $self->serverpid, 0);
334 die "exit code $? from server process\n" if $? != 0;
341 my ($self, $server, $packet) = @_;
348 print "Received server packet\n";
350 print "Received client packet\n";
353 print "Packet length = ".length($packet)."\n";
354 print "Processing flight ".$self->flight."\n";
356 #Return contains the list of record found in the packet followed by the
357 #list of messages in those records
358 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
359 push @{$self->record_list}, @{$ret[0]};
360 push @{$self->{message_list}}, @{$ret[1]};
364 #Finished parsing. Call user provided filter here
365 if(defined $self->filter) {
366 $self->filter->($self);
369 #Reconstruct the packet
371 foreach my $record (@{$self->record_list}) {
372 #We only replay the records for the current flight
373 if ($record->flight != $self->flight) {
376 $packet .= $record->reconstruct_record($server);
379 $self->{flight} = $self->{flight} + 1;
381 print "Forwarded packet length = ".length($packet)."\n\n";
390 return $self->{execute};
395 return $self->{cert};
400 return $self->{debug};
405 return $self->{flight};
410 return $self->{record_list};
415 return $self->{success};
428 #Read/write accessors
433 $self->{proxy_addr} = shift;
435 return $self->{proxy_addr};
441 $self->{proxy_port} = shift;
443 return $self->{proxy_port};
449 $self->{server_addr} = shift;
451 return $self->{server_addr};
457 $self->{server_port} = shift;
459 return $self->{server_port};
465 $self->{filter} = shift;
467 return $self->{filter};
473 $self->{cipherc} = shift;
475 return $self->{cipherc};
481 $self->{ciphers} = shift;
483 return $self->{ciphers};
489 $self->{serverflags} = shift;
491 return $self->{serverflags};
497 $self->{clientflags} = shift;
499 return $self->{clientflags};
505 $self->{serverconnects} = shift;
507 return $self->{serverconnects};
509 # This is a bit ugly because the caller is responsible for keeping the records
510 # in sync with the updated message list; simply updating the message list isn't
511 # sufficient to get the proxy to forward the new message.
512 # But it does the trick for the one test (test_sslsessiontick) that needs it.
517 $self->{message_list} = shift;
519 return $self->{message_list};
525 $self->{serverpid} = shift;
527 return $self->{serverpid};
534 for (my $i = 0; $i < $length; $i++) {
553 $self->{reneg} = shift;
555 return $self->{reneg};
558 #Setting a sessionfile means that the client will not close until the given
559 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
560 #immediately at the end of the handshake, but before the session has been
561 #received from the server. A side effect of this is that s_client never sends
562 #a close_notify, so instead we consider success to be when it sends application
563 #data over the connection.
568 $self->{sessionfile} = shift;
569 TLSProxy::Message->successondata(1);
571 return $self->{sessionfile};
578 $ciphersuite = shift;