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",
102 require IO::Socket::IP;
103 my $s = IO::Socket::IP->new(
112 # IO::Socket::IP supports IPv6 and is in the core modules list
113 $IP_factory = sub { IO::Socket::IP->new(@_); };
117 require IO::Socket::INET6;
118 my $s = IO::Socket::INET6->new(
127 # IO::Socket::INET6 supports IPv6 but isn't on the core modules list
128 # However, it's a bit older and said to be more widely deployed
129 # at the time of writing this comment.
130 $IP_factory = sub { IO::Socket::INET6->new(@_); };
133 # IO::Socket::INET doesn't support IPv6 but is a fallback in case
135 $IP_factory = sub { IO::Socket::INET->new(@_); };
139 return bless $self, $class;
146 $self->{cipherc} = "";
147 $self->{ciphers} = "AES128-SHA";
149 $self->{record_list} = [];
150 $self->{message_list} = [];
151 $self->{serverflags} = "";
152 $self->{clientflags} = "";
153 $self->{serverconnects} = 1;
155 TLSProxy::Message->clear();
156 TLSProxy::Record->clear();
182 open(STDOUT, ">", File::Spec->devnull())
183 or die "Failed to redirect stdout: $!";
184 open(STDERR, ">&STDOUT");
185 my $execcmd = $self->execute
186 ." s_server -rev -engine ossltest -accept "
187 .($self->server_port)
188 ." -cert ".$self->cert." -naccept ".$self->serverconnects;
189 if ($self->ciphers ne "") {
190 $execcmd .= " -cipher ".$self->ciphers;
192 if ($self->serverflags ne "") {
193 $execcmd .= " ".$self->serverflags;
207 open DEVNULL, ">", File::Spec->devnull();
208 $oldstdout = select(DEVNULL);
211 # Create the Proxy socket
212 my $proxaddr = $self->proxy_addr;
213 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
214 my $proxy_sock = $IP_factory->(
215 LocalHost => $proxaddr,
216 LocalPort => $self->proxy_port,
223 print "Proxy started on port ".$self->proxy_port."\n";
225 die "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
228 if ($self->execute) {
231 open(STDOUT, ">", File::Spec->devnull())
232 or die "Failed to redirect stdout: $!";
233 open(STDERR, ">&STDOUT");
234 my $execcmd = "echo test | ".$self->execute
235 ." s_client -engine ossltest -connect "
236 .($self->proxy_addr).":".($self->proxy_port);
237 if ($self->cipherc ne "") {
238 $execcmd .= " -cipher ".$self->cipherc;
240 if ($self->clientflags ne "") {
241 $execcmd .= " ".$self->clientflags;
247 # Wait for incoming connection from client
248 my $client_sock = $proxy_sock->accept()
249 or die "Failed accepting incoming connection: $!\n";
251 print "Connection opened\n";
253 # Now connect to the server
256 #We loop over this a few times because sometimes s_server can take a while
259 my $servaddr = $self->server_addr;
260 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
261 $server_sock = $IP_factory->(
262 PeerAddr => $servaddr,
263 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};