A number of minor fixes
[PHPCredLocker.git] / Resources / info.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/** Entry Point for Crypto key - Utilises browser caching so only have to send to the client once per key session
*
* Originally had to send twice because expiry was tied to the user's CredLocker session, 
* meaning Chrome requested twice (expiry of 0 epoch was sent by the login form). Firefox exhibited an odd behaviour and
* ignored the expiry date, so only requested once.
*
*
* Although not currently implemented, this file will eventually define the following
*
*  - Encryption key for received data
*  - Encryption key for sent data
*  - Delimiter to use for API requests
*  - API terminology to use (allowing us to replace known calls such as retCred with a random string)
* 
*
*
* Copyright (C) 2012 B Tasker
* Released under GNU GPL V2
* See LICENSE
*
*/
 
 
// Using gzhandler broke on a few systems. A lot seem to automatically gzip where possible though, so removed gzhandler call
ob_start();
error_reporting(0);
 
session_start();
define('_CREDLOCK',1);
 
// Change the current working dir
chdir(dirname(__FILE__)."/../");
 
// Load the framework
require_once 'lib/Framework/main.php';
 
$tls = BTMain::getSessVar('tls');
$expiry = BTMain::getSessVar('KeyExpiry');
 
 
 
$apiterms = array(
                        "retCred",
                        "checkSess",
                        "delCred",
                        "delUser",
                        "delCredType",
                        "delCust",
                        "delGroup"
                                    );
 
 
 
 
// We only to do key generation if  we're not on a SSL connection
if (!BTMain::getConnTypeSSL() && !BTMain::getConf()->forceTLS):
 
 
// If the key is still valid and we know the browser has already retrieved it, just tell the browser to use the cache
if  ((time() < $expiry) && ($_COOKIE['PHPCredLockerKeySet'] == 1) && ($expiry) && (!empty($tls))){
header("HTTP/1.1 304 Not Modified");
die;
}
 
// Would actually prefer not to include this in an unauthenticated session, but want to put key generation in the most logical place.
require_once 'lib/crypto.php';
 
 
 
// Set MIME-Header
header("Content-Type: text/javascript");
 
 
 
 
 
if (isset($_COOKIE['PHPCredLocker'])):
 
          foreach ($apiterms as $term){
 
                $x = 0;
                $new = '';
                $termlength = mt_rand(4,15);
 
 
                while ($x <= $termlength){
                      $new .= chr(mt_rand(97,122));
                          if (($x == $termlength) && in_array($new,$usedterms)){
                          // Make sure the termcode isn't already in used, if so, start again
                          $x = 0;
                          $new = '';
                      }
                  $x++;
                }       
 
                $usedterms[] = $new;
                $terms[$new] = $term;
      }
 
 
          $expiry = strtotime('+10 minutes');
          $seconds_to_cache = $expiry - time();
          $gmt = gmdate("D, d M Y H:i:s", $expiry) . " GMT";
 
          // Set caching headers
          header("Expires: $gmt");
          header("Pragma: cache");
          header("Cache-Control: Private, max-age=$seconds_to_cache");
 
          // Add the key and it's expiry to the session
          BTMain::setSessVar('KeyExpiry',$expiry);
          BTMain::setSessVar('tls',Crypto::genxorekey());
          BTMain::setSessVar('apiterms',$terms);
 
          // By setting a cookie, we provide an easy mechanism for allowing the API to force a key refresh
          setcookie("PHPCredLockerKeySet", 1, $expiry, dirname($_SERVER["REQUEST_URI"]), $_SERVER['HTTP_HOST'], BTMain::getConf()->forceSSL);
 
      endif;
 
      // We use a different method to generate Auth keys - in case a pattern does somehow appear in the TLS generation stuff we don't want anyone to be
      // able to view those keys without a valid login (at which point they won't really need to do key analysis!)
 
      $x = 0;
      $str = '';
      while ($x < 40){
 
      $str .= chr(rand(33,126)) .mt_rand(16,45);
      
      $x++;
 
      }
 
 
      BTMain::setSessVar('AuthKey',rtrim(base64_encode($str),"="));
      $enabled = 'true';
 
 
 
else:
// We don't need to generate keys as we're on a SSL connection
 
     foreach ($apiterms as $value){
        $terms[$value] = $value;
      }
 
     BTMain::setSessVar('apiterms',$terms);
     BTMain::setSessVar('tls',' ');
     BTMain::setSessVar('AuthKey',' ');
     $enabled = 'false';
 
 
 
    $expiry = strtotime('+1 day');
    $seconds_to_cache = $expiry - time();
    $gmt = gmdate("D, d M Y H:i:s", $expiry) . " GMT";
 
    // Set caching headers
    header("Expires: $gmt");
    header("Pragma: cache");
    header("Cache-Control: Private, max-age=$seconds_to_cache");
 
 
endif;
 
 
 
ob_start();
?> 
function getKey(){ return '<?php echo base64_encode(BTMain::getSessVar('tls'));?>'; }
 
 
function getDelimiter(){ return "|..|";}
 
 
function getTerminology(a){ 
 
if (a == 'undefined' || a == 'null' || a == ''){
return;}
 
<?php foreach ($terms as $key=>$value){ echo "this.$value='".base64_encode($key) ."';"; }?>
 
return this[a];
 }
 
 
 
function getAuthKey(){
return '<?php echo base64_encode(BTMain::getSessVar('AuthKey')); ?>';
}
 
 
function destroyKeys(){
window.getKey = '';
window.getDelimiter = '';
window.getTerminology = '';
window.getAuthKey = '';
return window.destroyKeys = '';
}
 
function enabledEncryption(){
return <?php echo $enabled;?>;
}
 
 
 
 
 
 
 
new getTerminology();
<?php
 
 
 
echo str_replace("\n","",ob_get_clean());
 
ob_end_flush();