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
287 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
288 foreach my $hand (@ready) {
289 if ($hand == $server_sock) {
290 $server_sock->sysread($indata, 16384) or goto END;
291 $indata = $self->process_packet(1, $indata);
292 $client_sock->syswrite($indata);
293 } elsif ($hand == $client_sock) {
294 $client_sock->sysread($indata, 16384) or goto END;
295 $indata = $self->process_packet(0, $indata);
296 $server_sock->syswrite($indata);
305 defined $self->sessionfile()
306 && (!(-f $self->sessionfile()) || $ctr == 3);
311 die "Session file not created"
312 if (defined $self->sessionfile() && !(-f $self->sessionfile()));
315 print "Connection closed\n";
317 $server_sock->close();
320 #Closing this also kills the child process
321 $client_sock->close();
324 $proxy_sock->close();
329 $self->serverconnects($self->serverconnects - 1);
330 if ($self->serverconnects == 0) {
331 die "serverpid is zero\n" if $self->serverpid == 0;
332 print "Waiting for server process to close: "
333 .$self->serverpid."\n";
334 waitpid( $self->serverpid, 0);
335 die "exit code $? from server process\n" if $? != 0;
342 my ($self, $server, $packet) = @_;
349 print "Received server packet\n";
351 print "Received client packet\n";
354 print "Packet length = ".length($packet)."\n";
355 print "Processing flight ".$self->flight."\n";
357 #Return contains the list of record found in the packet followed by the
358 #list of messages in those records
359 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
360 push @{$self->record_list}, @{$ret[0]};
361 push @{$self->{message_list}}, @{$ret[1]};
365 #Finished parsing. Call user provided filter here
366 if(defined $self->filter) {
367 $self->filter->($self);
370 #Reconstruct the packet
372 foreach my $record (@{$self->record_list}) {
373 #We only replay the records for the current flight
374 if ($record->flight != $self->flight) {
377 $packet .= $record->reconstruct_record($server);
380 $self->{flight} = $self->{flight} + 1;
382 print "Forwarded packet length = ".length($packet)."\n\n";
391 return $self->{execute};
396 return $self->{cert};
401 return $self->{debug};
406 return $self->{flight};
411 return $self->{record_list};
416 return $self->{success};
429 #Read/write accessors
434 $self->{proxy_addr} = shift;
436 return $self->{proxy_addr};
442 $self->{proxy_port} = shift;
444 return $self->{proxy_port};
450 $self->{server_addr} = shift;
452 return $self->{server_addr};
458 $self->{server_port} = shift;
460 return $self->{server_port};
466 $self->{filter} = shift;
468 return $self->{filter};
474 $self->{cipherc} = shift;
476 return $self->{cipherc};
482 $self->{ciphers} = shift;
484 return $self->{ciphers};
490 $self->{serverflags} = shift;
492 return $self->{serverflags};
498 $self->{clientflags} = shift;
500 return $self->{clientflags};
506 $self->{serverconnects} = shift;
508 return $self->{serverconnects};
510 # This is a bit ugly because the caller is responsible for keeping the records
511 # in sync with the updated message list; simply updating the message list isn't
512 # sufficient to get the proxy to forward the new message.
513 # But it does the trick for the one test (test_sslsessiontick) that needs it.
518 $self->{message_list} = shift;
520 return $self->{message_list};
526 $self->{serverpid} = shift;
528 return $self->{serverpid};
535 for (my $i = 0; $i < $length; $i++) {
554 $self->{reneg} = shift;
556 return $self->{reneg};
559 #Setting a sessionfile means that the client will not close until the given
560 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
561 #immediately at the end of the handshake, but before the session has been
562 #received from the server. A side effect of this is that s_client never sends
563 #a close_notify, so instead we consider success to be when it sends application
564 #data over the connection.
569 $self->{sessionfile} = shift;
570 TLSProxy::Message->successondata(1);
572 return $self->{sessionfile};
579 $ciphersuite = shift;