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;
176 $self->serverpid($pid);
178 return $self->clientstart;
187 open DEVNULL, ">", File::Spec->devnull();
188 $oldstdout = select(DEVNULL);
191 # Create the Proxy socket
192 my $proxaddr = $self->proxy_addr;
193 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
194 my $proxy_sock = $IP_factory->(
195 LocalHost => $proxaddr,
196 LocalPort => $self->proxy_port,
203 print "Proxy started on port ".$self->proxy_port."\n";
205 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
209 if ($self->execute) {
213 open(STDOUT, ">", File::Spec->devnull())
214 or die "Failed to redirect stdout: $!";
215 open(STDERR, ">&STDOUT");
218 if ($self->reneg()) {
223 my $execcmd = "echo ".$echostr." | ".$self->execute
224 ." s_client -engine ossltest -connect "
225 .($self->proxy_addr).":".($self->proxy_port);
226 if ($self->cipherc ne "") {
227 $execcmd .= " -cipher ".$self->cipherc;
229 if ($self->clientflags ne "") {
230 $execcmd .= " ".$self->clientflags;
232 if (defined $self->sessionfile) {
233 $execcmd .= " -ign_eof";
239 # Wait for incoming connection from client
241 if(!($client_sock = $proxy_sock->accept())) {
242 warn "Failed accepting incoming connection: $!\n";
246 print "Connection opened\n";
248 # Now connect to the server
251 #We loop over this a few times because sometimes s_server can take a while
254 my $servaddr = $self->server_addr;
255 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
257 $server_sock = $IP_factory->(
258 PeerAddr => $servaddr,
259 PeerPort => $self->server_port,
266 #Some buggy IP factories can return a defined server_sock that hasn't
267 #actually connected, so we check peerport too
268 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
269 $server_sock->close() if defined($server_sock);
272 #Sleep for a short while
273 select(undef, undef, undef, 0.1);
275 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
279 } while (!$server_sock);
281 my $sel = IO::Select->new($server_sock, $client_sock);
283 my @handles = ($server_sock, $client_sock);
285 #Wait for either the server socket or the client socket to become readable
288 while( (!(TLSProxy::Message->end)
289 || (defined $self->sessionfile()
290 && (-s $self->sessionfile()) == 0))
292 && (@ready = $sel->can_read(1))) {
293 foreach my $hand (@ready) {
294 if ($hand == $server_sock) {
295 $server_sock->sysread($indata, 16384) or goto END;
296 $indata = $self->process_packet(1, $indata);
297 $client_sock->syswrite($indata);
299 } elsif ($hand == $client_sock) {
300 $client_sock->sysread($indata, 16384) or goto END;
301 $indata = $self->process_packet(0, $indata);
302 $server_sock->syswrite($indata);
310 die "No progress made" if $ctr >= 10;
313 print "Connection closed\n";
315 $server_sock->close();
318 #Closing this also kills the child process
319 $client_sock->close();
322 $proxy_sock->close();
327 $self->serverconnects($self->serverconnects - 1);
328 if ($self->serverconnects == 0) {
329 die "serverpid is zero\n" if $self->serverpid == 0;
330 print "Waiting for server process to close: "
331 .$self->serverpid."\n";
332 waitpid( $self->serverpid, 0);
333 die "exit code $? from server process\n" if $? != 0;
340 my ($self, $server, $packet) = @_;
347 print "Received server packet\n";
349 print "Received client packet\n";
352 print "Packet length = ".length($packet)."\n";
353 print "Processing flight ".$self->flight."\n";
355 #Return contains the list of record found in the packet followed by the
356 #list of messages in those records
357 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
358 push @{$self->record_list}, @{$ret[0]};
359 push @{$self->{message_list}}, @{$ret[1]};
363 #Finished parsing. Call user provided filter here
364 if(defined $self->filter) {
365 $self->filter->($self);
368 #Reconstruct the packet
370 foreach my $record (@{$self->record_list}) {
371 #We only replay the records for the current flight
372 if ($record->flight != $self->flight) {
375 $packet .= $record->reconstruct_record($server);
378 $self->{flight} = $self->{flight} + 1;
380 print "Forwarded packet length = ".length($packet)."\n\n";
389 return $self->{execute};
394 return $self->{cert};
399 return $self->{debug};
404 return $self->{flight};
409 return $self->{record_list};
414 return $self->{success};
427 #Read/write accessors
432 $self->{proxy_addr} = shift;
434 return $self->{proxy_addr};
440 $self->{proxy_port} = shift;
442 return $self->{proxy_port};
448 $self->{server_addr} = shift;
450 return $self->{server_addr};
456 $self->{server_port} = shift;
458 return $self->{server_port};
464 $self->{filter} = shift;
466 return $self->{filter};
472 $self->{cipherc} = shift;
474 return $self->{cipherc};
480 $self->{ciphers} = shift;
482 return $self->{ciphers};
488 $self->{serverflags} = shift;
490 return $self->{serverflags};
496 $self->{clientflags} = shift;
498 return $self->{clientflags};
504 $self->{serverconnects} = shift;
506 return $self->{serverconnects};
508 # This is a bit ugly because the caller is responsible for keeping the records
509 # in sync with the updated message list; simply updating the message list isn't
510 # sufficient to get the proxy to forward the new message.
511 # But it does the trick for the one test (test_sslsessiontick) that needs it.
516 $self->{message_list} = shift;
518 return $self->{message_list};
524 $self->{serverpid} = shift;
526 return $self->{serverpid};
533 for (my $i = 0; $i < $length; $i++) {
552 $self->{reneg} = shift;
554 return $self->{reneg};
557 #Setting a sessionfile means that the client will not close until the given
558 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
559 #immediately at the end of the handshake, but before the session has been
560 #received from the server. A side effect of this is that s_client never sends
561 #a close_notify, so instead we consider success to be when it sends application
562 #data over the connection.
567 $self->{sessionfile} = shift;
568 TLSProxy::Message->successondata(1);
570 return $self->{sessionfile};
577 $ciphersuite = shift;