Proxy.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. # Written by Matt Caswell for the OpenSSL project.
  2. # ====================================================================
  3. # Copyright (c) 1998-2015 The OpenSSL Project. All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #
  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
  15. # distribution.
  16. #
  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/)"
  21. #
  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.
  26. #
  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.
  30. #
  31. # 6. Redistributions of any form whatsoever must retain the following
  32. # acknowledgment:
  33. # "This product includes software developed by the OpenSSL Project
  34. # for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. #
  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. # ====================================================================
  49. #
  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).
  53. use strict;
  54. package TLSProxy::Proxy;
  55. use File::Spec;
  56. use IO::Socket;
  57. use IO::Select;
  58. use TLSProxy::Record;
  59. use TLSProxy::Message;
  60. use TLSProxy::ClientHello;
  61. use TLSProxy::ServerHello;
  62. use TLSProxy::ServerKeyExchange;
  63. use TLSProxy::NewSessionTicket;
  64. my $have_IPv6 = 0;
  65. my $IP_factory;
  66. sub new
  67. {
  68. my $class = shift;
  69. my ($filter,
  70. $execute,
  71. $cert,
  72. $debug) = @_;
  73. my $self = {
  74. #Public read/write
  75. proxy_addr => "localhost",
  76. proxy_port => 4453,
  77. server_addr => "localhost",
  78. server_port => 4443,
  79. filter => $filter,
  80. serverflags => "",
  81. clientflags => "",
  82. serverconnects => 1,
  83. #Public read
  84. execute => $execute,
  85. cert => $cert,
  86. debug => $debug,
  87. cipherc => "",
  88. ciphers => "AES128-SHA",
  89. flight => 0,
  90. record_list => [],
  91. message_list => [],
  92. };
  93. eval {
  94. require IO::Socket::IP;
  95. my $s = IO::Socket::IP->new(
  96. LocalAddr => "::1",
  97. LocalPort => 0,
  98. Listen=>1,
  99. );
  100. $s or die "\n";
  101. $s->close();
  102. };
  103. if ($@ eq "") {
  104. # IO::Socket::IP supports IPv6 and is in the core modules list
  105. $IP_factory = sub { IO::Socket::IP->new(@_); };
  106. $have_IPv6 = 1;
  107. } else {
  108. eval {
  109. require IO::Socket::INET6;
  110. my $s = IO::Socket::INET6->new(
  111. LocalAddr => "::1",
  112. LocalPort => 0,
  113. Listen=>1,
  114. );
  115. $s or die "\n";
  116. $s->close();
  117. };
  118. if ($@ eq "") {
  119. # IO::Socket::INET6 supports IPv6 but isn't on the core modules list
  120. # However, it's a bit older and said to be more widely deployed
  121. # at the time of writing this comment.
  122. $IP_factory = sub { IO::Socket::INET6->new(@_); };
  123. $have_IPv6 = 1;
  124. } else {
  125. # IO::Socket::INET doesn't support IPv6 but is a fallback in case
  126. # we have no other.
  127. $IP_factory = sub { IO::Socket::INET->new(@_); };
  128. }
  129. }
  130. return bless $self, $class;
  131. }
  132. sub clear
  133. {
  134. my $self = shift;
  135. $self->{cipherc} = "";
  136. $self->{ciphers} = "AES128-SHA";
  137. $self->{flight} = 0;
  138. $self->{record_list} = [];
  139. $self->{message_list} = [];
  140. $self->{serverflags} = "";
  141. $self->{clientflags} = "";
  142. $self->{serverconnects} = 1;
  143. TLSProxy::Message->clear();
  144. TLSProxy::Record->clear();
  145. }
  146. sub restart
  147. {
  148. my $self = shift;
  149. $self->clear;
  150. $self->start;
  151. }
  152. sub clientrestart
  153. {
  154. my $self = shift;
  155. $self->clear;
  156. $self->clientstart;
  157. }
  158. sub start
  159. {
  160. my ($self) = shift;
  161. my $pid;
  162. $pid = fork();
  163. if ($pid == 0) {
  164. open(STDOUT, ">", File::Spec->devnull())
  165. or die "Failed to redirect stdout: $!";
  166. open(STDERR, ">&STDOUT");
  167. my $execcmd = $self->execute
  168. ." s_server -no_comp -rev -engine ossltest -accept "
  169. .($self->server_port)
  170. ." -cert ".$self->cert." -naccept ".$self->serverconnects;
  171. if ($self->ciphers ne "") {
  172. $execcmd .= " -cipher ".$self->ciphers;
  173. }
  174. if ($self->serverflags ne "") {
  175. $execcmd .= " ".$self->serverflags;
  176. }
  177. exec($execcmd);
  178. }
  179. $self->clientstart;
  180. }
  181. sub clientstart
  182. {
  183. my ($self) = shift;
  184. my $oldstdout;
  185. if(!$self->debug) {
  186. open DEVNULL, ">", File::Spec->devnull();
  187. $oldstdout = select(DEVNULL);
  188. }
  189. # Create the Proxy socket
  190. my $proxaddr = $self->proxy_addr;
  191. $proxaddr =~ s/[\[\]]//g; # Remove [ and ]
  192. my $proxy_sock = $IP_factory->(
  193. LocalHost => $proxaddr,
  194. LocalPort => $self->proxy_port,
  195. Proto => "tcp",
  196. Listen => SOMAXCONN,
  197. ReuseAddr => 1
  198. );
  199. if ($proxy_sock) {
  200. print "Proxy started on port ".$self->proxy_port."\n";
  201. } else {
  202. die "Failed creating proxy socket (".$proxaddr.",".$self->proxy_port."): $!\n";
  203. }
  204. if ($self->execute) {
  205. my $pid = fork();
  206. if ($pid == 0) {
  207. open(STDOUT, ">", File::Spec->devnull())
  208. or die "Failed to redirect stdout: $!";
  209. open(STDERR, ">&STDOUT");
  210. my $execcmd = "echo test | ".$self->execute
  211. ." s_client -engine ossltest -connect "
  212. .($self->proxy_addr).":".($self->proxy_port);
  213. if ($self->cipherc ne "") {
  214. $execcmd .= " -cipher ".$self->cipherc;
  215. }
  216. if ($self->clientflags ne "") {
  217. $execcmd .= " ".$self->clientflags;
  218. }
  219. exec($execcmd);
  220. }
  221. }
  222. # Wait for incoming connection from client
  223. my $client_sock = $proxy_sock->accept()
  224. or die "Failed accepting incoming connection: $!\n";
  225. print "Connection opened\n";
  226. # Now connect to the server
  227. my $retry = 3;
  228. my $server_sock;
  229. #We loop over this a few times because sometimes s_server can take a while
  230. #to start up
  231. do {
  232. my $servaddr = $self->server_addr;
  233. $servaddr =~ s/[\[\]]//g; # Remove [ and ]
  234. $server_sock = $IP_factory->(
  235. PeerAddr => $servaddr,
  236. PeerPort => $self->server_port,
  237. MultiHomed => 1,
  238. Proto => 'tcp'
  239. );
  240. $retry--;
  241. if (!$server_sock) {
  242. if ($retry) {
  243. #Sleep for a short while
  244. select(undef, undef, undef, 0.1);
  245. } else {
  246. die "Failed to start up server (".$servaddr.",".$self->server_port."): $!\n";
  247. }
  248. }
  249. } while (!$server_sock);
  250. my $sel = IO::Select->new($server_sock, $client_sock);
  251. my $indata;
  252. my @handles = ($server_sock, $client_sock);
  253. #Wait for either the server socket or the client socket to become readable
  254. my @ready;
  255. while(!(TLSProxy::Message->end) && (@ready = $sel->can_read)) {
  256. foreach my $hand (@ready) {
  257. if ($hand == $server_sock) {
  258. $server_sock->sysread($indata, 16384) or goto END;
  259. $indata = $self->process_packet(1, $indata);
  260. $client_sock->syswrite($indata);
  261. } elsif ($hand == $client_sock) {
  262. $client_sock->sysread($indata, 16384) or goto END;
  263. $indata = $self->process_packet(0, $indata);
  264. $server_sock->syswrite($indata);
  265. } else {
  266. print "Err\n";
  267. goto END;
  268. }
  269. }
  270. }
  271. END:
  272. print "Connection closed\n";
  273. if($server_sock) {
  274. $server_sock->close();
  275. }
  276. if($client_sock) {
  277. #Closing this also kills the child process
  278. $client_sock->close();
  279. }
  280. if($proxy_sock) {
  281. $proxy_sock->close();
  282. }
  283. if(!$self->debug) {
  284. select($oldstdout);
  285. }
  286. }
  287. sub process_packet
  288. {
  289. my ($self, $server, $packet) = @_;
  290. my $len_real;
  291. my $decrypt_len;
  292. my $data;
  293. my $recnum;
  294. if ($server) {
  295. print "Received server packet\n";
  296. } else {
  297. print "Received client packet\n";
  298. }
  299. print "Packet length = ".length($packet)."\n";
  300. print "Processing flight ".$self->flight."\n";
  301. #Return contains the list of record found in the packet followed by the
  302. #list of messages in those records
  303. my @ret = TLSProxy::Record->get_records($server, $self->flight, $packet);
  304. push @{$self->record_list}, @{$ret[0]};
  305. push @{$self->{message_list}}, @{$ret[1]};
  306. print "\n";
  307. #Finished parsing. Call user provided filter here
  308. if(defined $self->filter) {
  309. $self->filter->($self);
  310. }
  311. #Reconstruct the packet
  312. $packet = "";
  313. foreach my $record (@{$self->record_list}) {
  314. #We only replay the records for the current flight
  315. if ($record->flight != $self->flight) {
  316. next;
  317. }
  318. $packet .= $record->reconstruct_record();
  319. }
  320. $self->{flight} = $self->{flight} + 1;
  321. print "Forwarded packet length = ".length($packet)."\n\n";
  322. return $packet;
  323. }
  324. #Read accessors
  325. sub execute
  326. {
  327. my $self = shift;
  328. return $self->{execute};
  329. }
  330. sub cert
  331. {
  332. my $self = shift;
  333. return $self->{cert};
  334. }
  335. sub debug
  336. {
  337. my $self = shift;
  338. return $self->{debug};
  339. }
  340. sub flight
  341. {
  342. my $self = shift;
  343. return $self->{flight};
  344. }
  345. sub record_list
  346. {
  347. my $self = shift;
  348. return $self->{record_list};
  349. }
  350. sub success
  351. {
  352. my $self = shift;
  353. return $self->{success};
  354. }
  355. sub end
  356. {
  357. my $self = shift;
  358. return $self->{end};
  359. }
  360. sub supports_IPv6
  361. {
  362. my $self = shift;
  363. return $have_IPv6;
  364. }
  365. #Read/write accessors
  366. sub proxy_addr
  367. {
  368. my $self = shift;
  369. if (@_) {
  370. $self->{proxy_addr} = shift;
  371. }
  372. return $self->{proxy_addr};
  373. }
  374. sub proxy_port
  375. {
  376. my $self = shift;
  377. if (@_) {
  378. $self->{proxy_port} = shift;
  379. }
  380. return $self->{proxy_port};
  381. }
  382. sub server_addr
  383. {
  384. my $self = shift;
  385. if (@_) {
  386. $self->{server_addr} = shift;
  387. }
  388. return $self->{server_addr};
  389. }
  390. sub server_port
  391. {
  392. my $self = shift;
  393. if (@_) {
  394. $self->{server_port} = shift;
  395. }
  396. return $self->{server_port};
  397. }
  398. sub filter
  399. {
  400. my $self = shift;
  401. if (@_) {
  402. $self->{filter} = shift;
  403. }
  404. return $self->{filter};
  405. }
  406. sub cipherc
  407. {
  408. my $self = shift;
  409. if (@_) {
  410. $self->{cipherc} = shift;
  411. }
  412. return $self->{cipherc};
  413. }
  414. sub ciphers
  415. {
  416. my $self = shift;
  417. if (@_) {
  418. $self->{ciphers} = shift;
  419. }
  420. return $self->{ciphers};
  421. }
  422. sub serverflags
  423. {
  424. my $self = shift;
  425. if (@_) {
  426. $self->{serverflags} = shift;
  427. }
  428. return $self->{serverflags};
  429. }
  430. sub clientflags
  431. {
  432. my $self = shift;
  433. if (@_) {
  434. $self->{clientflags} = shift;
  435. }
  436. return $self->{clientflags};
  437. }
  438. sub serverconnects
  439. {
  440. my $self = shift;
  441. if (@_) {
  442. $self->{serverconnects} = shift;
  443. }
  444. return $self->{serverconnects};
  445. }
  446. # This is a bit ugly because the caller is responsible for keeping the records
  447. # in sync with the updated message list; simply updating the message list isn't
  448. # sufficient to get the proxy to forward the new message.
  449. # But it does the trick for the one test (test_sslsessiontick) that needs it.
  450. sub message_list
  451. {
  452. my $self = shift;
  453. if (@_) {
  454. $self->{message_list} = shift;
  455. }
  456. return $self->{message_list};
  457. }
  458. 1;