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;
78 proxy_addr => "localhost",
80 server_addr => "localhost",
92 ciphers => "AES128-SHA",
98 message_rec_list => []
101 return bless $self, $class;
108 $self->{cipherc} = "";
109 $self->{ciphers} = "AES128-SHA";
111 $self->{record_list} = [];
112 $self->{message_list} = [];
113 $self->{message_rec_list} = [];
114 $self->{serverflags} = "";
115 $self->{clientflags} = "";
116 $self->{serverconnects} = 1;
118 TLSProxy::Message->clear();
119 TLSProxy::Record->clear();
145 open(STDOUT, ">", File::Spec->devnull())
146 or die "Failed to redirect stdout";
147 open(STDERR, ">&STDOUT");
148 my $execcmd = $self->execute
149 ." s_server -no_comp -rev -engine ossltest -accept "
150 .($self->server_port)
151 ." -cert ".$self->cert." -naccept ".$self->serverconnects;
152 if ($self->ciphers ne "") {
153 $execcmd .= " -cipher ".$self->ciphers;
155 if ($self->serverflags ne "") {
156 $execcmd .= " ".$self->serverflags;
170 open DEVNULL, ">", File::Spec->devnull();
171 $oldstdout = select(DEVNULL);
174 # Create the Proxy socket
175 my $proxy_sock = new IO::Socket::INET(
176 LocalHost => $self->proxy_addr,
177 LocalPort => $self->proxy_port,
184 print "Proxy started on port ".$self->proxy_port."\n";
186 die "Failed creating proxy socket\n";
189 if ($self->execute) {
192 open(STDOUT, ">", File::Spec->devnull())
193 or die "Failed to redirect stdout";
194 open(STDERR, ">&STDOUT");
195 my $execcmd = "echo test | ".$self->execute
196 ." s_client -engine ossltest -connect "
197 .($self->proxy_addr).":".($self->proxy_port);
198 if ($self->cipherc ne "") {
199 $execcmd .= " -cipher ".$self->cipherc;
201 if ($self->clientflags ne "") {
202 $execcmd .= " ".$self->clientflags;
208 # Wait for incoming connection from client
209 my $client_sock = $proxy_sock->accept()
210 or die "Failed accepting incoming connection\n";
212 print "Connection opened\n";
214 # Now connect to the server
217 #We loop over this a few times because sometimes s_server can take a while
220 $server_sock = new IO::Socket::INET(
221 PeerAddr => $self->server_addr,
222 PeerPort => $self->server_port,
229 #Sleep for a short while
230 select(undef, undef, undef, 0.1);
232 die "Failed to start up server\n";
235 } while (!$server_sock);
237 my $sel = IO::Select->new($server_sock, $client_sock);
239 my @handles = ($server_sock, $client_sock);
241 #Wait for either the server socket or the client socket to become readable
243 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
244 foreach my $hand (@ready) {
245 if ($hand == $server_sock) {
246 $server_sock->sysread($indata, 16384) or goto END;
247 $indata = $self->process_packet(1, $indata);
248 $client_sock->syswrite($indata);
249 } elsif ($hand == $client_sock) {
250 $client_sock->sysread($indata, 16384) or goto END;
251 $indata = $self->process_packet(0, $indata);
252 $server_sock->syswrite($indata);
261 print "Connection closed\n";
263 $server_sock->close();
266 #Closing this also kills the child process
267 $client_sock->close();
270 $proxy_sock->close();
280 my ($self, $server, $packet) = @_;
287 print "Received server packet\n";
289 print "Received client packet\n";
292 print "Packet length = ".length($packet)."\n";
293 print "Processing flight ".$self->flight."\n";
295 #Return contains the list of record found in the packet followed by the
296 #list of messages in those records
297 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
298 push @{$self->record_list}, @{$ret[0]};
299 $self->{message_rec_list} = $ret[0];
300 push @{$self->{message_list}}, @{$ret[1]};
304 #Finished parsing. Call user provided filter here
305 if(defined $self->filter) {
306 $self->filter->($self);
309 #Reconstruct the packet
311 foreach my $record (@{$self->record_list}) {
312 #We only replay the records for the current flight
313 if ($record->flight != $self->flight) {
316 $packet .= $record->reconstruct_record();
319 $self->{flight} = $self->{flight} + 1;
321 print "Forwarded packet length = ".length($packet)."\n\n";
330 return $self->{execute};
335 return $self->{cert};
340 return $self->{debug};
345 return $self->{flight};
350 return $self->{record_list};
355 return $self->{message_list};
360 return $self->{success};
368 #Read/write accessors
373 $self->{proxy_addr} = shift;
375 return $self->{proxy_addr};
381 $self->{proxy_port} = shift;
383 return $self->{proxy_port};
389 $self->{server_addr} = shift;
391 return $self->{server_addr};
397 $self->{server_port} = shift;
399 return $self->{server_port};
405 $self->{filter} = shift;
407 return $self->{filter};
413 $self->{cipherc} = shift;
415 return $self->{cipherc};
421 $self->{ciphers} = shift;
423 return $self->{ciphers};
429 $self->{serverflags} = shift;
431 return $self->{serverflags};
437 $self->{clientflags} = shift;
439 return $self->{clientflags};
445 $self->{serverconnects} = shift;
447 return $self->{serverconnects};