1 # Written by Matt Caswell for the OpenSSL project.
2 # ====================================================================
3 # Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
17 # 3. All advertising materials mentioning features or use of this
18 # software must display the following acknowledgment:
19 # "This product includes software developed by the OpenSSL Project
20 # for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 # endorse or promote products derived from this software without
24 # prior written permission. For written permission, please contact
25 # openssl-core@openssl.org.
27 # 5. Products derived from this software may not be called "OpenSSL"
28 # nor may "OpenSSL" appear in their names without prior written
29 # permission of the OpenSSL Project.
31 # 6. Redistributions of any form whatsoever must retain the following
33 # "This product includes software developed by the OpenSSL Project
34 # for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 # OF THE POSSIBILITY OF SUCH DAMAGE.
48 # ====================================================================
50 # This product includes cryptographic software written by Eric Young
51 # (eay@cryptsoft.com). This product includes software written by Tim
52 # Hudson (tjh@cryptsoft.com).
56 package TLSProxy::Proxy;
62 use TLSProxy::Message;
63 use TLSProxy::ClientHello;
64 use TLSProxy::ServerHello;
65 use TLSProxy::ServerKeyExchange;
66 use TLSProxy::NewSessionTicket;
81 proxy_addr => "localhost",
83 server_addr => "localhost",
95 ciphers => "AES128-SHA",
101 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
102 # However, IO::Socket::INET6 is older and is said to be more widely
103 # deployed for the moment, and may have less bugs, so we try the latter
104 # first, then fall back on the code modules. Worst case scenario, we
105 # fall back to IO::Socket::INET, only supports IPv4.
107 require IO::Socket::INET6;
108 my $s = IO::Socket::INET6->new(
117 $IP_factory = sub { IO::Socket::INET6->new(@_); };
121 require IO::Socket::IP;
122 my $s = IO::Socket::IP->new(
131 $IP_factory = sub { IO::Socket::IP->new(@_); };
134 $IP_factory = sub { IO::Socket::INET->new(@_); };
138 return bless $self, $class;
145 $self->{cipherc} = "";
146 $self->{ciphers} = "AES128-SHA";
148 $self->{record_list} = [];
149 $self->{message_list} = [];
150 $self->{serverflags} = "";
151 $self->{clientflags} = "";
152 $self->{serverconnects} = 1;
154 TLSProxy::Message->clear();
155 TLSProxy::Record->clear();
181 open(STDOUT, ">", File::Spec->devnull())
182 or die "Failed to redirect stdout: $!";
183 open(STDERR, ">&STDOUT");
184 my $execcmd = $self->execute
185 ." s_server -no_comp -rev -engine ossltest -accept "
186 .($self->server_port)
187 ." -cert ".$self->cert." -naccept ".$self->serverconnects;
188 if ($self->ciphers ne "") {
189 $execcmd .= " -cipher ".$self->ciphers;
191 if ($self->serverflags ne "") {
192 $execcmd .= " ".$self->serverflags;
206 open DEVNULL, ">", File::Spec->devnull();
207 $oldstdout = select(DEVNULL);
210 # Create the Proxy socket
211 my $proxaddr = $self->proxy_addr;
212 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
213 my $proxy_sock = $IP_factory->(
214 LocalHost => $proxaddr,
215 LocalPort => $self->proxy_port,
222 print "Proxy started on port ".$self->proxy_port."\n";
224 die "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
227 if ($self->execute) {
230 open(STDOUT, ">", File::Spec->devnull())
231 or die "Failed to redirect stdout: $!";
232 open(STDERR, ">&STDOUT");
233 my $execcmd = "echo test | ".$self->execute
234 ." s_client -engine ossltest -connect "
235 .($self->proxy_addr).":".($self->proxy_port);
236 if ($self->cipherc ne "") {
237 $execcmd .= " -cipher ".$self->cipherc;
239 if ($self->clientflags ne "") {
240 $execcmd .= " ".$self->clientflags;
246 # Wait for incoming connection from client
247 my $client_sock = $proxy_sock->accept()
248 or die "Failed accepting incoming connection: $!\n";
250 print "Connection opened\n";
252 # Now connect to the server
255 #We loop over this a few times because sometimes s_server can take a while
258 my $servaddr = $self->server_addr;
259 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
260 $server_sock = $IP_factory->(
261 PeerAddr => $servaddr,
262 PeerPort => $self->server_port,
270 #Sleep for a short while
271 select(undef, undef, undef, 0.1);
273 die "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
276 } while (!$server_sock);
278 my $sel = IO::Select->new($server_sock, $client_sock);
280 my @handles = ($server_sock, $client_sock);
282 #Wait for either the server socket or the client socket to become readable
284 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
285 foreach my $hand (@ready) {
286 if ($hand == $server_sock) {
287 $server_sock->sysread($indata, 16384) or goto END;
288 $indata = $self->process_packet(1, $indata);
289 $client_sock->syswrite($indata);
290 } elsif ($hand == $client_sock) {
291 $client_sock->sysread($indata, 16384) or goto END;
292 $indata = $self->process_packet(0, $indata);
293 $server_sock->syswrite($indata);
302 print "Connection closed\n";
304 $server_sock->close();
307 #Closing this also kills the child process
308 $client_sock->close();
311 $proxy_sock->close();
320 my ($self, $server, $packet) = @_;
327 print "Received server packet\n";
329 print "Received client packet\n";
332 print "Packet length = ".length($packet)."\n";
333 print "Processing flight ".$self->flight."\n";
335 #Return contains the list of record found in the packet followed by the
336 #list of messages in those records
337 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
338 push @{$self->record_list}, @{$ret[0]};
339 push @{$self->{message_list}}, @{$ret[1]};
343 #Finished parsing. Call user provided filter here
344 if(defined $self->filter) {
345 $self->filter->($self);
348 #Reconstruct the packet
350 foreach my $record (@{$self->record_list}) {
351 #We only replay the records for the current flight
352 if ($record->flight != $self->flight) {
355 $packet .= $record->reconstruct_record();
358 $self->{flight} = $self->{flight} + 1;
360 print "Forwarded packet length = ".length($packet)."\n\n";
369 return $self->{execute};
374 return $self->{cert};
379 return $self->{debug};
384 return $self->{flight};
389 return $self->{record_list};
394 return $self->{success};
407 #Read/write accessors
412 $self->{proxy_addr} = shift;
414 return $self->{proxy_addr};
420 $self->{proxy_port} = shift;
422 return $self->{proxy_port};
428 $self->{server_addr} = shift;
430 return $self->{server_addr};
436 $self->{server_port} = shift;
438 return $self->{server_port};
444 $self->{filter} = shift;
446 return $self->{filter};
452 $self->{cipherc} = shift;
454 return $self->{cipherc};
460 $self->{ciphers} = shift;
462 return $self->{ciphers};
468 $self->{serverflags} = shift;
470 return $self->{serverflags};
476 $self->{clientflags} = shift;
478 return $self->{clientflags};
484 $self->{serverconnects} = shift;
486 return $self->{serverconnects};
488 # This is a bit ugly because the caller is responsible for keeping the records
489 # in sync with the updated message list; simply updating the message list isn't
490 # sufficient to get the proxy to forward the new message.
491 # But it does the trick for the one test (test_sslsessiontick) that needs it.
496 $self->{message_list} = shift;
498 return $self->{message_list};