]> git.ipfire.org Git - thirdparty/openssl.git/blob - util/TLSProxy/Proxy.pm
95599e50ebecd99bbbab69117f9d9efc7b89848e
[thirdparty/openssl.git] / util / TLSProxy / Proxy.pm
1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
2 #
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
7
8 use strict;
9 use POSIX ":sys_wait_h";
10
11 package TLSProxy::Proxy;
12
13 use File::Spec;
14 use IO::Socket;
15 use IO::Select;
16 use TLSProxy::Record;
17 use TLSProxy::Message;
18 use TLSProxy::ClientHello;
19 use TLSProxy::ServerHello;
20 use TLSProxy::EncryptedExtensions;
21 use TLSProxy::ServerKeyExchange;
22 use TLSProxy::NewSessionTicket;
23
24 my $have_IPv6 = 0;
25 my $IP_factory;
26
27 my $is_tls13 = 0;
28
29 sub new
30 {
31 my $class = shift;
32 my ($filter,
33 $execute,
34 $cert,
35 $debug) = @_;
36
37 my $self = {
38 #Public read/write
39 proxy_addr => "localhost",
40 proxy_port => 4453,
41 server_addr => "localhost",
42 server_port => 4443,
43 filter => $filter,
44 serverflags => "",
45 clientflags => "",
46 serverconnects => 1,
47 serverpid => 0,
48
49 #Public read
50 execute => $execute,
51 cert => $cert,
52 debug => $debug,
53 cipherc => "",
54 ciphers => "AES128-SHA:TLS13-AES-128-GCM-SHA256",
55 flight => 0,
56 record_list => [],
57 message_list => [],
58 };
59
60 # IO::Socket::IP is on the core module list, IO::Socket::INET6 isn't.
61 # However, IO::Socket::INET6 is older and is said to be more widely
62 # deployed for the moment, and may have less bugs, so we try the latter
63 # first, then fall back on the code modules. Worst case scenario, we
64 # fall back to IO::Socket::INET, only supports IPv4.
65 eval {
66 require IO::Socket::INET6;
67 my $s = IO::Socket::INET6->new(
68 LocalAddr => "::1",
69 LocalPort => 0,
70 Listen=>1,
71 );
72 $s or die "\n";
73 $s->close();
74 };
75 if ($@ eq "") {
76 $IP_factory = sub { IO::Socket::INET6->new(@_); };
77 $have_IPv6 = 1;
78 } else {
79 eval {
80 require IO::Socket::IP;
81 my $s = IO::Socket::IP->new(
82 LocalAddr => "::1",
83 LocalPort => 0,
84 Listen=>1,
85 );
86 $s or die "\n";
87 $s->close();
88 };
89 if ($@ eq "") {
90 $IP_factory = sub { IO::Socket::IP->new(@_); };
91 $have_IPv6 = 1;
92 } else {
93 $IP_factory = sub { IO::Socket::INET->new(@_); };
94 }
95 }
96
97 return bless $self, $class;
98 }
99
100 sub clearClient
101 {
102 my $self = shift;
103
104 $self->{cipherc} = "";
105 $self->{flight} = 0;
106 $self->{record_list} = [];
107 $self->{message_list} = [];
108 $self->{clientflags} = "";
109 $is_tls13 = 0;
110
111 TLSProxy::Message->clear();
112 TLSProxy::Record->clear();
113 }
114
115 sub clear
116 {
117 my $self = shift;
118
119 $self->clearClient;
120 $self->{ciphers} = "AES128-SHA:TLS13-AES-128-GCM-SHA256";
121 $self->{serverflags} = "";
122 $self->{serverconnects} = 1;
123 $self->{serverpid} = 0;
124 }
125
126 sub restart
127 {
128 my $self = shift;
129
130 $self->clear;
131 $self->start;
132 }
133
134 sub clientrestart
135 {
136 my $self = shift;
137
138 $self->clear;
139 $self->clientstart;
140 }
141
142 sub start
143 {
144 my ($self) = shift;
145 my $pid;
146
147 $pid = fork();
148 if ($pid == 0) {
149 if (!$self->debug) {
150 open(STDOUT, ">", File::Spec->devnull())
151 or die "Failed to redirect stdout: $!";
152 open(STDERR, ">&STDOUT");
153 }
154 my $execcmd = $self->execute
155 ." s_server -no_comp -rev -engine ossltest -accept "
156 .($self->server_port)
157 ." -cert ".$self->cert." -cert2 ".$self->cert
158 ." -naccept ".$self->serverconnects;
159 if ($self->ciphers ne "") {
160 $execcmd .= " -cipher ".$self->ciphers;
161 }
162 if ($self->serverflags ne "") {
163 $execcmd .= " ".$self->serverflags;
164 }
165 exec($execcmd);
166 }
167 $self->serverpid($pid);
168
169 return $self->clientstart;
170 }
171
172 sub clientstart
173 {
174 my ($self) = shift;
175 my $oldstdout;
176
177 if(!$self->debug) {
178 open DEVNULL, ">", File::Spec->devnull();
179 $oldstdout = select(DEVNULL);
180 }
181
182 # Create the Proxy socket
183 my $proxaddr = $self->proxy_addr;
184 $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
185 my $proxy_sock = $IP_factory->(
186 LocalHost => $proxaddr,
187 LocalPort => $self->proxy_port,
188 Proto => "tcp",
189 Listen => SOMAXCONN,
190 ReuseAddr => 1
191 );
192
193 if ($proxy_sock) {
194 print "Proxy started on port ".$self->proxy_port."\n";
195 } else {
196 warn "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
197 return 0;
198 }
199
200 if ($self->execute) {
201 my $pid = fork();
202 if ($pid == 0) {
203 if (!$self->debug) {
204 open(STDOUT, ">", File::Spec->devnull())
205 or die "Failed to redirect stdout: $!";
206 open(STDERR, ">&STDOUT");
207 }
208 my $execcmd = "echo test | ".$self->execute
209 ." s_client -engine ossltest -connect "
210 .($self->proxy_addr).":".($self->proxy_port);
211 if ($self->cipherc ne "") {
212 $execcmd .= " -cipher ".$self->cipherc;
213 }
214 if ($self->clientflags ne "") {
215 $execcmd .= " ".$self->clientflags;
216 }
217 exec($execcmd);
218 }
219 }
220
221 # Wait for incoming connection from client
222 my $client_sock;
223 if(!($client_sock = $proxy_sock->accept())) {
224 warn "Failed accepting incoming connection: $!\n";
225 return 0;
226 }
227
228 print "Connection opened\n";
229
230 # Now connect to the server
231 my $retry = 3;
232 my $server_sock;
233 #We loop over this a few times because sometimes s_server can take a while
234 #to start up
235 do {
236 my $servaddr = $self->server_addr;
237 $servaddr =~ s/[\[\]]//g; # Remove [ and ]
238 eval {
239 $server_sock = $IP_factory->(
240 PeerAddr => $servaddr,
241 PeerPort => $self->server_port,
242 MultiHomed => 1,
243 Proto => 'tcp'
244 );
245 };
246
247 $retry--;
248 #Some buggy IP factories can return a defined server_sock that hasn't
249 #actually connected, so we check peerport too
250 if ($@ || !defined($server_sock) || !defined($server_sock->peerport)) {
251 $server_sock->close() if defined($server_sock);
252 undef $server_sock;
253 if ($retry) {
254 #Sleep for a short while
255 select(undef, undef, undef, 0.1);
256 } else {
257 warn "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
258 return 0;
259 }
260 }
261 } while (!$server_sock);
262
263 my $sel = IO::Select->new($server_sock, $client_sock);
264 my $indata;
265 my @handles = ($server_sock, $client_sock);
266
267 #Wait for either the server socket or the client socket to become readable
268 my @ready;
269 while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
270 foreach my $hand (@ready) {
271 if ($hand == $server_sock) {
272 $server_sock->sysread($indata, 16384) or goto END;
273 $indata = $self->process_packet(1, $indata);
274 $client_sock->syswrite($indata);
275 } elsif ($hand == $client_sock) {
276 $client_sock->sysread($indata, 16384) or goto END;
277 $indata = $self->process_packet(0, $indata);
278 $server_sock->syswrite($indata);
279 } else {
280 print "Err\n";
281 goto END;
282 }
283 }
284 }
285
286 END:
287 print "Connection closed\n";
288 if($server_sock) {
289 $server_sock->close();
290 }
291 if($client_sock) {
292 #Closing this also kills the child process
293 $client_sock->close();
294 }
295 if($proxy_sock) {
296 $proxy_sock->close();
297 }
298 if(!$self->debug) {
299 select($oldstdout);
300 }
301 $self->serverconnects($self->serverconnects - 1);
302 if ($self->serverconnects == 0) {
303 die "serverpid is zero\n" if $self->serverpid == 0;
304 print "Waiting for server process to close: "
305 .$self->serverpid."\n";
306 waitpid( $self->serverpid, 0);
307 }
308 return 1;
309 }
310
311 sub process_packet
312 {
313 my ($self, $server, $packet) = @_;
314 my $len_real;
315 my $decrypt_len;
316 my $data;
317 my $recnum;
318
319 if ($server) {
320 print "Received server packet\n";
321 } else {
322 print "Received client packet\n";
323 }
324
325 print "Packet length = ".length($packet)."\n";
326 print "Processing flight ".$self->flight."\n";
327
328 #Return contains the list of record found in the packet followed by the
329 #list of messages in those records
330 my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
331 push @{$self->record_list}, @{$ret[0]};
332 push @{$self->{message_list}}, @{$ret[1]};
333
334 print "\n";
335
336 #Finished parsing. Call user provided filter here
337 if(defined $self->filter) {
338 $self->filter->($self);
339 }
340
341 #Reconstruct the packet
342 $packet = "";
343 foreach my $record (@{$self->record_list}) {
344 #We only replay the records for the current flight
345 if ($record->flight != $self->flight) {
346 next;
347 }
348 $packet .= $record->reconstruct_record($server);
349 }
350
351 $self->{flight} = $self->{flight} + 1;
352
353 print "Forwarded packet length = ".length($packet)."\n\n";
354
355 return $packet;
356 }
357
358 #Read accessors
359 sub execute
360 {
361 my $self = shift;
362 return $self->{execute};
363 }
364 sub cert
365 {
366 my $self = shift;
367 return $self->{cert};
368 }
369 sub debug
370 {
371 my $self = shift;
372 return $self->{debug};
373 }
374 sub flight
375 {
376 my $self = shift;
377 return $self->{flight};
378 }
379 sub record_list
380 {
381 my $self = shift;
382 return $self->{record_list};
383 }
384 sub success
385 {
386 my $self = shift;
387 return $self->{success};
388 }
389 sub end
390 {
391 my $self = shift;
392 return $self->{end};
393 }
394 sub supports_IPv6
395 {
396 my $self = shift;
397 return $have_IPv6;
398 }
399
400 #Read/write accessors
401 sub proxy_addr
402 {
403 my $self = shift;
404 if (@_) {
405 $self->{proxy_addr} = shift;
406 }
407 return $self->{proxy_addr};
408 }
409 sub proxy_port
410 {
411 my $self = shift;
412 if (@_) {
413 $self->{proxy_port} = shift;
414 }
415 return $self->{proxy_port};
416 }
417 sub server_addr
418 {
419 my $self = shift;
420 if (@_) {
421 $self->{server_addr} = shift;
422 }
423 return $self->{server_addr};
424 }
425 sub server_port
426 {
427 my $self = shift;
428 if (@_) {
429 $self->{server_port} = shift;
430 }
431 return $self->{server_port};
432 }
433 sub filter
434 {
435 my $self = shift;
436 if (@_) {
437 $self->{filter} = shift;
438 }
439 return $self->{filter};
440 }
441 sub cipherc
442 {
443 my $self = shift;
444 if (@_) {
445 $self->{cipherc} = shift;
446 }
447 return $self->{cipherc};
448 }
449 sub ciphers
450 {
451 my $self = shift;
452 if (@_) {
453 $self->{ciphers} = shift;
454 }
455 return $self->{ciphers};
456 }
457 sub serverflags
458 {
459 my $self = shift;
460 if (@_) {
461 $self->{serverflags} = shift;
462 }
463 return $self->{serverflags};
464 }
465 sub clientflags
466 {
467 my $self = shift;
468 if (@_) {
469 $self->{clientflags} = shift;
470 }
471 return $self->{clientflags};
472 }
473 sub serverconnects
474 {
475 my $self = shift;
476 if (@_) {
477 $self->{serverconnects} = shift;
478 }
479 return $self->{serverconnects};
480 }
481 # This is a bit ugly because the caller is responsible for keeping the records
482 # in sync with the updated message list; simply updating the message list isn't
483 # sufficient to get the proxy to forward the new message.
484 # But it does the trick for the one test (test_sslsessiontick) that needs it.
485 sub message_list
486 {
487 my $self = shift;
488 if (@_) {
489 $self->{message_list} = shift;
490 }
491 return $self->{message_list};
492 }
493 sub serverpid
494 {
495 my $self = shift;
496 if (@_) {
497 $self->{serverpid} = shift;
498 }
499 return $self->{serverpid};
500 }
501
502 sub fill_known_data
503 {
504 my $length = shift;
505 my $ret = "";
506 for (my $i = 0; $i < $length; $i++) {
507 $ret .= chr($i);
508 }
509 return $ret;
510 }
511 sub is_tls13
512 {
513 my $class = shift;
514 if (@_) {
515 $is_tls13 = shift;
516 }
517 return $is_tls13;
518 }
519 1;