]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/infrastructure/php/d0_blind_id.inc
update PHP script to also support signed GET
[xonotic/xonotic.git] / misc / infrastructure / php / d0_blind_id.inc
1 <?php
2
3 // you may want to override these after including this file
4 $d0_blind_id_keygen = "crypto-keygen-standalone";
5 $d0_blind_id_d0pk = "key_0.d0pk";
6
7 // usage:
8 //   list($status, $idfp) = get_d0_blind_id();
9 // return values:
10 //   null, null = signature failed
11 //   "", 0      = not signed
12 //   idfp, 0    = signed, ID was not signed by CA
13 //   idfp, 1    = signed, ID was signed by CA
14 function d0_blind_id_verify()
15 {
16         global $d0_blind_id_keygen;
17         global $d0_blind_id_d0pk;
18
19         if($_SERVER["REQUEST_METHOD"] == "POST")
20                 $data = file_get_contents("php://input") . "\0" . $_SERVER["QUERY_STRING"];
21         else
22                 $data = $_SERVER["QUERY_STRING"];
23         $sig = $_SERVER["HTTP_X_D0_BLIND_ID_DETACHED_SIGNATURE"];
24         if($sig)
25         {
26                 // check signature
27                 putenv("KEYGEN=$d0_blind_id_keygen");
28                 $checker = proc_open(
29                                 "\"\$KEYGEN\" -p /dev/fd/3 -d /dev/fd/4 -s /dev/fd/5",
30                                 array(
31                                         1 => array("pipe", "w"),
32                                         3 => array("file", $d0_blind_id_d0pk, "r"),
33                                         4 => array("pipe", "r"),
34                                         5 => array("pipe", "r")
35                                      ),
36                                 $pipes,
37                                 null,
38                                 null,
39                                 array("binary_pipes")
40                                 );
41                 if(!$checker)
42                         die("Cannot start process");
43                 $outfh = $pipes[1];
44                 $buffers = array(
45                                 4 => $data,
46                                 5 => base64_decode($sig)
47                                 );
48                 $rpipes = array(
49                                 4 => $pipes[4],
50                                 5 => $pipes[5]
51                                );
52                 foreach($rpipes as $p)
53                         stream_set_blocking($p, 0);
54                 while(!empty($rpipes))
55                 {
56                         $readers = null;
57                         $writers = $rpipes;
58                         $errorers = $rpipes;
59                         $n = stream_select($readers, $writers, $errorers, 1, 0);
60                         if($n == 0)
61                                 break;
62                         $n = 0;
63                         foreach($errorers as $e)
64                         {
65                                 $i = array_search($e, $rpipes);
66                                 if($i === false)
67                                         continue;
68                                 fclose($pipes[$i]);
69                                 unset($buffers[$i]);
70                                 unset($rpipes[$i]);
71                                 ++$n;
72                         }
73                         foreach($writers as $w)
74                         {
75                                 $i = array_search($w, $rpipes);
76                                 if($i === false)
77                                         continue;
78                                 $written = fwrite($w, $buffers[$i], strlen($buffers[$i]));
79                                 if($written)
80                                         $buffers[$i] = substr($buffers[$i], $written);
81                                 if($buffers[$i] == "")
82                                 {
83                                         fclose($pipes[$i]);
84                                         unset($buffers[$i]);
85                                         unset($rpipes[$i]);
86                                 }
87                                 ++$n;
88                         }
89                         if(!$n)
90                                 break;
91                 }
92                 if($buffers)
93                         die("could not write data to process");
94                 $status = stream_get_line($outfh, 8192, "\n");
95                 $idfp = stream_get_line($outfh, 8192, "\n");
96                 $ret = proc_close($checker);
97                 if($ret != 0)
98                         return array(null, null);
99                 return array($idfp, $status);
100         }
101         else
102                 return array("", 0);
103 }
104 ?>