-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.php
187 lines (177 loc) · 6.15 KB
/
keys.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
if (!defined('CORRECT_ENTRY')) {
http_response_code(500);
echo 'Internal server error';
exit();
}
if($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
echo 'Method not allowed';
exit();
}
if(!isset($_GET['reader'])) {
http_response_code(400);
echo 'Missing reader';
exit();
}
$reader = explode( ',', $_GET['reader'] );
if(count($reader) == 0) {
http_response_code(400);
echo 'Missing reader';
exit();
}
if(isset($_GET['request'])) {
$requested = explode( ',', $_GET['request'] );
}
$collectedKeys = array();
if(!isset($requested) || count($requested) == 0) {
foreach($reader as $readerKey) {
$groupResult = enumerate_latest_rounds_iterative($readerKey);
if($groupResult) {
$collectedKeys = array_merge($collectedKeys, $groupResult);
}
}
} else {
foreach($requested as $requestedKey) {
$requestedKeyParts = explode('.', $requestedKey);
if(count($requestedKeyParts) != 2 || !is_base64urlsafe($requestedKeyParts[0]) || !is_base64urlsafe($requestedKeyParts[1])) {
continue;
}
$requestedKeyGroup = $requestedKeyParts[0];
$requestedKeyRound = $requestedKeyParts[1];
foreach($reader as $readerKey) {
$groupResult = find_key_path_iterative($readerKey, $requestedKeyGroup, $requestedKeyRound);
if($groupResult) {
$collectedKeys = array_merge($collectedKeys, $groupResult['keys']);
continue;
}
}
}
}
$response = array();
foreach($collectedKeys as $key) {
$audience = $key['enc_with_grp'];
$group = $key['grp'];
$round = $key['rnd'];
$data = $key['data'];
if(!isset($response[$audience])) {
$response[$audience] = array();
}
if(!isset($response[$audience][$group])) {
$response[$audience][$group] = array();
}
$response[$audience][$group][$round] = $data;
}
header('Content-Type: application/json');
echo json_encode($response, JSON_FORCE_OBJECT);
function find_key_path_iterative($knownKeyGroup, $requestedKeyGroup, $requestedKeyRound) {
$allAccessibleGroups = get_reachable_groups($knownKeyGroup);
if(in_array($requestedKeyGroup, $allAccessibleGroups)) {
$key = get_single_key($requestedKeyGroup, $requestedKeyRound, $knownKeyGroup);
if($key) {
return array(
'keys' => array($key),
'required_rnd' => $key['enc_with_rnd']
);
}
}
foreach($allAccessibleGroups as $accessibleGroup) {
$groupResult = find_key_path_iterative($accessibleGroup, $requestedKeyGroup, $requestedKeyRound);
if($groupResult) {
$key = get_single_key($accessibleGroup, $groupResult['required_rnd'], $knownKeyGroup);
if($key) {
$keys = $groupResult['keys'];
$keys[] = $key;
return array(
'keys' => $keys,
'required_rnd' => $key['enc_with_rnd']
);
}
}
}
return false;
}
function enumerate_latest_rounds_iterative($knownKeyGroup) {
$allAccessibleGroups = get_reachable_groups($knownKeyGroup);
if(count($allAccessibleGroups) == 0) {
return false;
}
$result = array();
foreach($allAccessibleGroups as $accessibleGroup) {
$groupResult = enumerate_latest_rounds_iterative($accessibleGroup);
if(!$groupResult) {
$key = get_latest_key($accessibleGroup, $knownKeyGroup);
$result[] = $key;
} else {
foreach($groupResult as $encKey) {
if($encKey['enc_with_grp'] == $accessibleGroup) {
$key = get_single_key($encKey['enc_with_grp'], $encKey['enc_with_rnd'], $knownKeyGroup);
if($key) {
$result[] = $encKey;
$result[] = $key;
}
} else {
$result[] = $encKey;
}
}
}
}
return $result;
}
function get_reachable_groups($group) {
global $profile;
global $pdo;
$reachableKeyGroupsStatement = $pdo->prepare('SELECT DISTINCT grp FROM profile_keys WHERE profile=? and enc_with_grp=?');
$reachableKeyGroupsStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$reachableKeyGroupsStatement->bindValue(2, $group, PDO::PARAM_STR);
try {
$reachableKeyGroupsStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$result = array();
while($row = $reachableKeyGroupsStatement->fetch()) {
$result[] = $row['grp'];
}
$reachableKeyGroupsStatement->closeCursor();
return $result;
}
function get_single_key($grp, $rnd, $enc_with_grp) {
global $profile;
global $pdo;
$keyRoundStatement = $pdo->prepare('SELECT * FROM profile_keys WHERE profile=? and grp=? and rnd=? and enc_with_grp=?');
$keyRoundStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$keyRoundStatement->bindValue(2, $grp, PDO::PARAM_STR);
$keyRoundStatement->bindValue(3, $rnd, PDO::PARAM_STR);
$keyRoundStatement->bindValue(4, $enc_with_grp, PDO::PARAM_STR);
try {
$keyRoundStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$key = $keyRoundStatement->fetch();
$keyRoundStatement->closeCursor();
return $key;
}
function get_latest_key($grp, $enc_with_grp) {
global $profile;
global $pdo;
$keyRoundStatement = $pdo->prepare('SELECT * FROM profile_keys WHERE profile=? and grp=? and enc_with_grp=? ORDER BY published desc LIMIT 1');
$keyRoundStatement->bindValue(1, $profile['name'], PDO::PARAM_STR);
$keyRoundStatement->bindValue(2, $grp, PDO::PARAM_STR);
$keyRoundStatement->bindValue(3, $enc_with_grp, PDO::PARAM_STR);
try {
$keyRoundStatement->execute();
} catch (PDOException $e) {
http_response_code(500);
echo 'Internal server error';
exit();
}
$key = $keyRoundStatement->fetch();
$keyRoundStatement->closeCursor();
return $key;
}