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 return bless $self, $class;
105 $self->{cipherc} = "";
106 $self->{ciphers} = "AES128-SHA";
108 $self->{record_list} = [];
109 $self->{message_list} = [];
110 $self->{serverflags} = "";
111 $self->{clientflags} = "";
112 $self->{serverconnects} = 1;
114 TLSProxy::Message->clear();
115 TLSProxy::Record->clear();
141 open(STDOUT, ">", File::Spec->devnull())
142 or die "Failed to redirect stdout";
143 open(STDERR, ">&STDOUT");
144 my $execcmd = $self->execute
145 ." s_server -no_comp -rev -engine ossltest -accept "
146 .($self->server_port)
147 ." -cert ".$self->cert." -naccept ".$self->serverconnects;
148 if ($self->ciphers ne "") {
149 $execcmd .= " -cipher ".$self->ciphers;
151 if ($self->serverflags ne "") {
152 $execcmd .= " ".$self->serverflags;
166 open DEVNULL, ">", File::Spec->devnull();
167 $oldstdout = select(DEVNULL);
170 # Create the Proxy socket
171 my $proxy_sock = new IO::Socket::INET(
172 LocalHost => $self->proxy_addr,
173 LocalPort => $self->proxy_port,
180 print "Proxy started on port ".$self->proxy_port."\n";
182 die "Failed creating proxy socket\n";
185 if ($self->execute) {
188 open(STDOUT, ">", File::Spec->devnull())
189 or die "Failed to redirect stdout";
190 open(STDERR, ">&STDOUT");
191 my $execcmd = "echo test | ".$self->execute
192 ." s_client -engine ossltest -connect "
193 .($self->proxy_addr).":".($self->proxy_port);
194 if ($self->cipherc ne "") {
195 $execcmd .= " -cipher ".$self->cipherc;
197 if ($self->clientflags ne "") {
198 $execcmd .= " ".$self->clientflags;
204 # Wait for incoming connection from client
205 my $client_sock = $proxy_sock->accept()
206 or die "Failed accepting incoming connection\n";
208 print "Connection opened\n";
210 # Now connect to the server
213 #We loop over this a few times because sometimes s_server can take a while
216 $server_sock = new IO::Socket::INET(
217 PeerAddr => $self->server_addr,
218 PeerPort => $self->server_port,
225 #Sleep for a short while
226 select(undef, undef, undef, 0.1);
228 die "Failed to start up server\n";
231 } while (!$server_sock);
233 my $sel = IO::Select->new($server_sock, $client_sock);
235 my @handles = ($server_sock, $client_sock);
237 #Wait for either the server socket or the client socket to become readable
239 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
240 foreach my $hand (@ready) {
241 if ($hand == $server_sock) {
242 $server_sock->sysread($indata, 16384) or goto END;
243 $indata = $self->process_packet(1, $indata);
244 $client_sock->syswrite($indata);
245 } elsif ($hand == $client_sock) {
246 $client_sock->sysread($indata, 16384) or goto END;
247 $indata = $self->process_packet(0, $indata);
248 $server_sock->syswrite($indata);
257 print "Connection closed\n";
259 $server_sock->close();
262 #Closing this also kills the child process
263 $client_sock->close();
266 $proxy_sock->close();
275 my ($self, $server, $packet) = @_;
282 print "Received server packet\n";
284 print "Received client packet\n";
287 print "Packet length = ".length($packet)."\n";
288 print "Processing flight ".$self->flight."\n";
290 #Return contains the list of record found in the packet followed by the
291 #list of messages in those records
292 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
293 push @{$self->record_list}, @{$ret[0]};
294 push @{$self->{message_list}}, @{$ret[1]};
298 #Finished parsing. Call user provided filter here
299 if(defined $self->filter) {
300 $self->filter->($self);
303 #Reconstruct the packet
305 foreach my $record (@{$self->record_list}) {
306 #We only replay the records for the current flight
307 if ($record->flight != $self->flight) {
310 $packet .= $record->reconstruct_record();
313 $self->{flight} = $self->{flight} + 1;
315 print "Forwarded packet length = ".length($packet)."\n\n";
324 return $self->{execute};
329 return $self->{cert};
334 return $self->{debug};
339 return $self->{flight};
344 return $self->{record_list};
349 return $self->{success};
357 #Read/write accessors
362 $self->{proxy_addr} = shift;
364 return $self->{proxy_addr};
370 $self->{proxy_port} = shift;
372 return $self->{proxy_port};
378 $self->{server_addr} = shift;
380 return $self->{server_addr};
386 $self->{server_port} = shift;
388 return $self->{server_port};
394 $self->{filter} = shift;
396 return $self->{filter};
402 $self->{cipherc} = shift;
404 return $self->{cipherc};
410 $self->{ciphers} = shift;
412 return $self->{ciphers};
418 $self->{serverflags} = shift;
420 return $self->{serverflags};
426 $self->{clientflags} = shift;
428 return $self->{clientflags};
434 $self->{serverconnects} = shift;
436 return $self->{serverconnects};
438 # This is a bit ugly because the caller is responsible for keeping the records
439 # in sync with the updated message list; simply updating the message list isn't
440 # sufficient to get the proxy to forward the new message.
441 # But it does the trick for the one test (test_sslsessiontick) that needs it.
446 $self->{message_list} = shift;
448 return $self->{message_list};