[763] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Crypto * |
---|
| 4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
| 5 | * Handles encrypting strings based on various encryption schemes * |
---|
| 6 | * Copyright (C) 2000, 2001 Dan Kuykendall * |
---|
| 7 | * -------------------------------------------------------------------------* |
---|
| 8 | * This library is part of the eGroupWare API * |
---|
| 9 | * http://www.egroupware.org/api * |
---|
| 10 | * -------------------------------------------------------------------------* |
---|
| 11 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 12 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 13 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 14 | * or any later version. * |
---|
| 15 | * This library is distributed in the hope that it will be useful, but * |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 18 | * See the GNU Lesser General Public License for more details. * |
---|
| 19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 21 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 22 | \**************************************************************************/ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | class crypto |
---|
| 26 | { |
---|
| 27 | var $enabled = False; |
---|
| 28 | var $debug = False; |
---|
| 29 | |
---|
| 30 | var $mcrypt_version = ''; |
---|
| 31 | var $algo = MCRYPT_TRIPLEDES; |
---|
| 32 | var $mode = MCRYPT_MODE_CBC; |
---|
| 33 | var $td = False; /* Handle for mcrypt */ |
---|
| 34 | var $iv = ''; |
---|
| 35 | var $key = ''; |
---|
| 36 | |
---|
| 37 | function crypto($vars='') |
---|
| 38 | { |
---|
| 39 | if($GLOBALS['phpgw_info']['flags']['currentapp'] == 'login' || |
---|
| 40 | $GLOBALS['phpgw_info']['flags']['currentapp'] == 'logout' || |
---|
| 41 | $GLOBALS['phpgw_info']['flags']['currentapp'] == 'home' |
---|
| 42 | ) |
---|
| 43 | { |
---|
| 44 | $this->debug = False; |
---|
| 45 | } |
---|
| 46 | if(is_array($vars)) |
---|
| 47 | { |
---|
| 48 | $this->init($vars); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | function init($vars) |
---|
| 53 | { |
---|
| 54 | /* _debug_array(mcrypt_list_algorithms()); */ |
---|
| 55 | $key = $vars[0]; |
---|
| 56 | $iv = $vars[1]; |
---|
| 57 | |
---|
| 58 | if($GLOBALS['phpgw_info']['server']['mcrypt_enabled'] && extension_loaded('mcrypt')) |
---|
| 59 | { |
---|
| 60 | if($GLOBALS['phpgw_info']['server']['mcrypt_algo']) |
---|
| 61 | { |
---|
| 62 | $this->algo = $GLOBALS['phpgw_info']['server']['mcrypt_algo']; |
---|
| 63 | } |
---|
| 64 | if($GLOBALS['phpgw_info']['server']['mcrypt_mode']) |
---|
| 65 | { |
---|
| 66 | $this->mode = $GLOBALS['phpgw_info']['server']['mcrypt_mode']; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if($this->debug) |
---|
| 70 | { |
---|
| 71 | echo '<br>crypto: algorithm=' . $this->algo; |
---|
| 72 | echo '<br>crypto: mode =' . $this->mode; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | $this->enabled = True; |
---|
| 76 | $this->mcrypt_version = $GLOBALS['phpgw_info']['server']['versions']['mcrypt']; |
---|
| 77 | if($this->mcrypt_version == 'old') |
---|
| 78 | { |
---|
| 79 | $this->td = False; |
---|
| 80 | if(phpversion() > '4.0.2pl1') |
---|
| 81 | { |
---|
| 82 | $keysize = mcrypt_get_key_size($this->algo); |
---|
| 83 | $ivsize = mcrypt_get_iv_size($this->algo,$this->mode); |
---|
| 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
| 87 | $keysize = 8; |
---|
| 88 | $ivsize = 8; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | /* Start up mcrypt */ |
---|
| 94 | $this->td = mcrypt_module_open($this->algo, '', $this->mode, ''); |
---|
| 95 | |
---|
| 96 | $ivsize = mcrypt_enc_get_iv_size($this->td); |
---|
| 97 | $keysize = mcrypt_enc_get_key_size($this->td); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /* Hack IV to be the correct size */ |
---|
| 101 | $x = strlen($iv); |
---|
| 102 | $this->iv = ''; |
---|
| 103 | for($i = 0; $i < $ivsize; $i++) |
---|
| 104 | { |
---|
| 105 | $this->iv .= $iv[$i % $x]; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /* Hack Key to be the correct size */ |
---|
| 109 | $x = strlen($key); |
---|
| 110 | $this->key = ''; |
---|
| 111 | for($i = 0; $i < $keysize; $i++) |
---|
| 112 | { |
---|
| 113 | $this->key .= $key[$i % $x]; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | /* If mcrypt isn't loaded, key and iv are not needed. */ |
---|
| 119 | if($this->debug) |
---|
| 120 | { |
---|
| 121 | echo '<br>crypto: mycrypt unavailable or disabled'; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | function cleanup() |
---|
| 127 | { |
---|
| 128 | if($this->enabled) |
---|
| 129 | { |
---|
| 130 | if($this->mcrypt_version != 'old') |
---|
| 131 | { |
---|
| 132 | if(function_exists('mcrypt_generic_deinit')) |
---|
| 133 | { |
---|
| 134 | mcrypt_generic_deinit($this->td); |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | mcrypt_generic_end($this->td); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | function hex2bin($data) |
---|
| 145 | { |
---|
| 146 | $len = strlen($data); |
---|
| 147 | return pack('H'.$len, $data); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | function encrypt($data) |
---|
| 151 | { |
---|
| 152 | if($this->debug) |
---|
| 153 | { |
---|
| 154 | echo '<br>' . time() . ' crypto->encrypt() unencrypted data: ---->>>>' . $data . "\n"; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | if(is_array($data) || is_object($data)) |
---|
| 158 | { |
---|
| 159 | if($this->debug) |
---|
| 160 | { |
---|
| 161 | echo '<br>' . time() . ' crypto->encrypt() found an "' . gettype($data) . '". Serializing...' . "\n"; |
---|
| 162 | } |
---|
| 163 | $data = serialize($data); |
---|
| 164 | $_obj = True; |
---|
| 165 | } |
---|
| 166 | else |
---|
| 167 | { |
---|
| 168 | if($this->debug) |
---|
| 169 | { |
---|
| 170 | echo '<br>' . time() . ' crypto->encrypt() found "' . gettype($data) . '". No serialization...' . "\n"; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* Disable all encryption if the admin didn't set it up */ |
---|
| 175 | if($this->enabled) |
---|
| 176 | { |
---|
| 177 | if($_obj) |
---|
| 178 | { |
---|
| 179 | if($this->debug) |
---|
| 180 | { |
---|
| 181 | echo '<br>' . time() . ' crypto->encrypt() adding slashes' . "\n"; |
---|
| 182 | } |
---|
| 183 | $data = addslashes($data); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | if($this->debug) |
---|
| 187 | { |
---|
| 188 | echo '<br>' . time() . ' crypto->encrypt() data: ---->>>>' . $data; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | switch($this->mcrypt_version) |
---|
| 192 | { |
---|
| 193 | case 'old': |
---|
| 194 | /* The old code, only works with mcrypt <= 2.2.x */ |
---|
| 195 | $encrypteddata = mcrypt_cbc($this->algo, $this->key, $data, MCRYPT_ENCRYPT); |
---|
| 196 | break; |
---|
| 197 | default: |
---|
| 198 | /* Handle 2.4 and newer API */ |
---|
| 199 | mcrypt_generic_init($this->td, $this->key, $this->iv); |
---|
| 200 | $encrypteddata = mcrypt_generic($this->td, $data); |
---|
| 201 | break; |
---|
| 202 | } |
---|
| 203 | $encrypteddata = bin2hex($encrypteddata); |
---|
| 204 | if($this->debug) |
---|
| 205 | { |
---|
| 206 | echo '<br>' . time() . ' crypto->encrypt() crypted data: ---->>>>' . $encrypteddata; |
---|
| 207 | } |
---|
| 208 | return $encrypteddata; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | /* No mcrypt == insecure ! */ |
---|
| 213 | if($this->debug) |
---|
| 214 | { |
---|
| 215 | echo '<br>' . time() . ' crypto->encrypt() crypted data: ---->>>>' . $data; |
---|
| 216 | } |
---|
| 217 | return $data; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | function decrypt($encrypteddata) |
---|
| 222 | { |
---|
| 223 | if($this->debug) |
---|
| 224 | { |
---|
| 225 | echo '<br>' . time() . ' crypto->decrypt() crypted data: ---->>>>' . $encrypteddata; |
---|
| 226 | } |
---|
| 227 | /* Disable all encryption if the admin didn't set it up */ |
---|
| 228 | if($this->enabled) |
---|
| 229 | { |
---|
| 230 | $data = $this->hex2bin($encrypteddata); |
---|
| 231 | switch($this->mcrypt_version) |
---|
| 232 | { |
---|
| 233 | case 'old': |
---|
| 234 | /* The old code, only works with mcrypt <= 2.2.x */ |
---|
| 235 | $data = mcrypt_cbc($this->algo, $this->key, $data, MCRYPT_DECRYPT); |
---|
| 236 | break; |
---|
| 237 | default: |
---|
| 238 | /* Handle 2.4 and newer API */ |
---|
| 239 | mcrypt_generic_init($this->td, $this->key, $this->iv); |
---|
| 240 | $data = mdecrypt_generic($this->td, $data); |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | if($this->debug) |
---|
| 245 | { |
---|
| 246 | echo '<br>' . time() . ' crypto->decrypt() decrypted data: ---->>>>' . $data; |
---|
| 247 | } |
---|
| 248 | $test = stripslashes($data); |
---|
| 249 | if(@unserialize($test)) |
---|
| 250 | { |
---|
| 251 | if($this->debug) |
---|
| 252 | { |
---|
| 253 | echo '<br>' . time() . ' crypto->decrypt() stripping slashes' . "\n"; |
---|
| 254 | } |
---|
| 255 | $data = $test; |
---|
| 256 | } |
---|
| 257 | unset($test); |
---|
| 258 | |
---|
| 259 | if($this->debug) |
---|
| 260 | { |
---|
| 261 | echo '<br>' . time() . ' crypto->decrypt() data: ---->>>>' . $data . "\n"; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | else |
---|
| 265 | { |
---|
| 266 | /* No mcrypt == insecure ! */ |
---|
| 267 | $data = $encrypteddata; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // Fix strange bug |
---|
| 271 | // Without this, somes ^@^@^@^@ appears in data |
---|
| 272 | $data = chop($data); |
---|
| 273 | |
---|
| 274 | $newdata = @unserialize($data); |
---|
| 275 | if($newdata) |
---|
| 276 | { |
---|
| 277 | if($this->debug) |
---|
| 278 | { |
---|
| 279 | echo '<br>' . time() . ' crypto->decrypt() found serialized "' . gettype($newdata) . '". Unserializing...' . "\n"; |
---|
| 280 | echo '<br>' . time() . ' crypto->decrypt() returning: '; _debug_array($newdata); |
---|
| 281 | } |
---|
| 282 | return $newdata; |
---|
| 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | if($this->debug) |
---|
| 287 | { |
---|
| 288 | echo '<br>' . time() . ' crypto->decrypt() found UNserialized "' . gettype($data) . '". No unserialization...' . "\n"; |
---|
| 289 | echo '<br>' . time() . ' crypto->decrypt() returning: ' . $data; |
---|
| 290 | } |
---|
| 291 | return $data; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | } // class crypto |
---|
| 295 | ?> |
---|