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;
25 use Time::HiRes qw/usleep/;
31 my $ciphersuite = undef;
43 proxy_addr => "localhost",
45 server_addr => "localhost",
61 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
67 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
68 # However, IO::Socket::INET6 is older and is said to be more widely
69 # deployed for the moment, and may have less bugs, so we try the latter
70 # first, then fall back on the code modules. Worst case scenario, we
71 # fall back to IO::Socket::INET, only supports IPv4.
73 require IO::Socket::INET6;
74 my $s = IO::Socket::INET6->new(
83 $IP_factory = sub { IO::Socket::INET6->new(@_); };
87 require IO::Socket::IP;
88 my $s = IO::Socket::IP->new(
97 $IP_factory = sub { IO::Socket::IP->new(@_); };
100 $IP_factory = sub { IO::Socket::INET->new(@_); };
104 return bless $self, $class;
111 $self->{cipherc} = "";
113 $self->{record_list} = [];
114 $self->{message_list} = [];
115 $self->{clientflags} = "";
116 $self->{sessionfile} = undef;
117 $self->{clientpid} = 0;
119 $ciphersuite = undef;
121 TLSProxy::Message->clear();
122 TLSProxy::Record->clear();
130 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
131 $self->{serverflags} = "";
132 $self->{serverconnects} = 1;
133 $self->{serverpid} = 0;
160 my $execcmd = $self->execute
161 ." s_server -no_comp -rev -engine ossltest -accept "
162 .($self->server_port)
163 ." -cert ".$self->cert." -cert2 ".$self->cert
164 ." -naccept ".$self->serverconnects;
165 unless ($self->supports_IPv6) {
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;
189 # Create the Proxy socket
190 my $proxaddr = $self->proxy_addr;
191 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
192 my $proxy_sock = $IP_factory->(
193 LocalHost => $proxaddr,
194 LocalPort => $self->proxy_port,
201 print "Proxy started on port ".$self->proxy_port."\n";
203 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
207 if ($self->execute) {
211 if ($self->reneg()) {
216 my $execcmd = "echo ".$echostr." | ".$self->execute
217 ." s_client -engine ossltest -connect "
218 .($self->proxy_addr).":".($self->proxy_port);
219 unless ($self->supports_IPv6) {
222 if ($self->cipherc ne "") {
223 $execcmd .= " -cipher ".$self->cipherc;
225 if ($self->clientflags ne "") {
226 $execcmd .= " ".$self->clientflags;
228 if (defined $self->sessionfile) {
229 $execcmd .= " -ign_eof";
232 print STDERR "Client command: $execcmd\n";
236 $self->clientpid($pid);
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 local $SIG{PIPE} = "IGNORE";
289 while( (!(TLSProxy::Message->end)
290 || (defined $self->sessionfile()
291 && (-s $self->sessionfile()) == 0))
293 if (!(@ready = $sel->can_read(1))) {
297 foreach my $hand (@ready) {
298 if ($hand == $server_sock) {
299 $server_sock->sysread($indata, 16384) or goto END;
300 $indata = $self->process_packet(1, $indata);
301 $client_sock->syswrite($indata);
303 } elsif ($hand == $client_sock) {
304 $client_sock->sysread($indata, 16384) or goto END;
305 $indata = $self->process_packet(0, $indata);
306 $server_sock->syswrite($indata);
309 die "Unexpected handle";
314 die "No progress made" if $ctr >= 10;
317 print "Connection closed\n";
319 $server_sock->close();
322 #Closing this also kills the child process
323 $client_sock->close();
326 $proxy_sock->close();
331 $self->serverconnects($self->serverconnects - 1);
332 if ($self->serverconnects == 0) {
333 die "serverpid is zero\n" if $self->serverpid == 0;
334 print "Waiting for server process to close: "
335 .$self->serverpid."\n";
336 waitpid( $self->serverpid, 0);
337 die "exit code $? from server process\n" if $? != 0;
339 # Give s_server sufficient time to finish what it was doing
342 die "clientpid is zero\n" if $self->clientpid == 0;
343 print "Waiting for client process to close: ".$self->clientpid."\n";
344 waitpid($self->clientpid, 0);
351 my ($self, $server, $packet) = @_;
358 print "Received server packet\n";
360 print "Received client packet\n";
363 print "Packet length = ".length($packet)."\n";
364 print "Processing flight ".$self->flight."\n";
366 #Return contains the list of record found in the packet followed by the
367 #list of messages in those records
368 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
369 push @{$self->record_list}, @{$ret[0]};
370 push @{$self->{message_list}}, @{$ret[1]};
374 #Finished parsing. Call user provided filter here
375 if(defined $self->filter) {
376 $self->filter->($self);
379 #Reconstruct the packet
381 foreach my $record (@{$self->record_list}) {
382 #We only replay the records for the current flight
383 if ($record->flight != $self->flight) {
386 $packet .= $record->reconstruct_record($server);
389 $self->{flight} = $self->{flight} + 1;
391 print "Forwarded packet length = ".length($packet)."\n\n";
400 return $self->{execute};
405 return $self->{cert};
410 return $self->{debug};
415 return $self->{flight};
420 return $self->{record_list};
425 return $self->{success};
438 #Read/write accessors
443 $self->{proxy_addr} = shift;
445 return $self->{proxy_addr};
451 $self->{proxy_port} = shift;
453 return $self->{proxy_port};
459 $self->{server_addr} = shift;
461 return $self->{server_addr};
467 $self->{server_port} = shift;
469 return $self->{server_port};
475 $self->{filter} = shift;
477 return $self->{filter};
483 $self->{cipherc} = shift;
485 return $self->{cipherc};
491 $self->{ciphers} = shift;
493 return $self->{ciphers};
499 $self->{serverflags} = shift;
501 return $self->{serverflags};
507 $self->{clientflags} = shift;
509 return $self->{clientflags};
515 $self->{serverconnects} = shift;
517 return $self->{serverconnects};
519 # This is a bit ugly because the caller is responsible for keeping the records
520 # in sync with the updated message list; simply updating the message list isn't
521 # sufficient to get the proxy to forward the new message.
522 # But it does the trick for the one test (test_sslsessiontick) that needs it.
527 $self->{message_list} = shift;
529 return $self->{message_list};
535 $self->{serverpid} = shift;
537 return $self->{serverpid};
543 $self->{clientpid} = shift;
545 return $self->{clientpid};
552 for (my $i = 0; $i < $length; $i++) {
571 $self->{reneg} = shift;
573 return $self->{reneg};
576 #Setting a sessionfile means that the client will not close until the given
577 #file exists. This is useful in TLSv1.3 where otherwise s_client will close
578 #immediately at the end of the handshake, but before the session has been
579 #received from the server. A side effect of this is that s_client never sends
580 #a close_notify, so instead we consider success to be when it sends application
581 #data over the connection.
586 $self->{sessionfile} = shift;
587 TLSProxy::Message->successondata(1);
589 return $self->{sessionfile};
596 $ciphersuite = shift;