|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 Copyright (c) 2002-2003, Michael Bretterklieber <michael@bretterklieber.com> 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 00010 1. Redistributions of source code must retain the above copyright 00011 notice, this list of conditions and the following disclaimer. 00012 2. Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 3. The names of the authors may not be used to endorse or promote products 00016 derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00019 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00020 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00021 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00022 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00023 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00024 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00025 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 00029 This code cannot simply be copied and put under the GNU Public License or 00030 any other GPL-like (LGPL, GPL2) License. 00031 00032 $Id: CHAP.php,v 1.2 2010/12/14 17:36:04 moodlerobot Exp $ 00033 */ 00034 00035 require_once 'PEAR.php'; 00036 00056 class Crypt_CHAP extends PEAR 00057 { 00062 var $challenge = null; 00063 00068 var $response = null; 00069 00074 var $password = null; 00075 00080 var $chapid = 1; 00081 00088 function Crypt_CHAP() 00089 { 00090 $this->PEAR(); 00091 $this->generateChallenge(); 00092 } 00093 00101 function generateChallenge($varname = 'challenge', $size = 8) 00102 { 00103 $this->$varname = ''; 00104 mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff); 00105 for ($i = 0; $i < $size; $i++) { 00106 $this->$varname .= pack('C', 1 + mt_rand() % 255); 00107 } 00108 return $this->$varname; 00109 } 00110 00116 function challengeResponse() 00117 { 00118 } 00119 00120 } 00121 00129 class Crypt_CHAP_MD5 extends Crypt_CHAP 00130 { 00131 00140 function challengeResponse() 00141 { 00142 return pack('H*', md5(pack('C', $this->chapid) . $this->password . $this->challenge)); 00143 } 00144 } 00145 00156 class Crypt_CHAP_MSv1 extends Crypt_CHAP 00157 { 00163 var $flags = 1; 00164 00171 function Crypt_CHAP_MSv1() 00172 { 00173 $this->Crypt_CHAP(); 00174 $this->loadExtension('mhash'); 00175 } 00176 00183 function ntPasswordHash($password = null) 00184 { 00185 if (isset($password)) { 00186 return mhash(MHASH_MD4, $this->str2unicode($password)); 00187 } else { 00188 return mhash(MHASH_MD4, $this->str2unicode($this->password)); 00189 } 00190 } 00191 00198 function str2unicode($str) 00199 { 00200 $uni = ''; 00201 $str = (string) $str; 00202 for ($i = 0; $i < strlen($str); $i++) { 00203 $a = ord($str{$i}) << 8; 00204 $uni .= sprintf("%X", $a); 00205 } 00206 return pack('H*', $uni); 00207 } 00208 00215 function challengeResponse() 00216 { 00217 return $this->_challengeResponse(); 00218 } 00219 00226 function ntChallengeResponse() 00227 { 00228 return $this->_challengeResponse(false); 00229 } 00230 00237 function lmChallengeResponse() 00238 { 00239 return $this->_challengeResponse(true); 00240 } 00241 00251 function _challengeResponse($lm = false) 00252 { 00253 if ($lm) { 00254 $hash = $this->lmPasswordHash(); 00255 } else { 00256 $hash = $this->ntPasswordHash(); 00257 } 00258 00259 while (strlen($hash) < 21) { 00260 $hash .= "\0"; 00261 } 00262 00263 $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); 00264 $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 00265 $key = $this->_desAddParity(substr($hash, 0, 7)); 00266 mcrypt_generic_init($td, $key, $iv); 00267 $resp1 = mcrypt_generic($td, $this->challenge); 00268 mcrypt_generic_deinit($td); 00269 00270 $key = $this->_desAddParity(substr($hash, 7, 7)); 00271 mcrypt_generic_init($td, $key, $iv); 00272 $resp2 = mcrypt_generic($td, $this->challenge); 00273 mcrypt_generic_deinit($td); 00274 00275 $key = $this->_desAddParity(substr($hash, 14, 7)); 00276 mcrypt_generic_init($td, $key, $iv); 00277 $resp3 = mcrypt_generic($td, $this->challenge); 00278 mcrypt_generic_deinit($td); 00279 mcrypt_module_close($td); 00280 00281 return $resp1 . $resp2 . $resp3; 00282 } 00283 00290 function lmPasswordHash($password = null) 00291 { 00292 $plain = isset($password) ? $password : $this->password; 00293 00294 $plain = substr(strtoupper($plain), 0, 14); 00295 while (strlen($plain) < 14) { 00296 $plain .= "\0"; 00297 } 00298 00299 return $this->_desHash(substr($plain, 0, 7)) . $this->_desHash(substr($plain, 7, 7)); 00300 } 00301 00308 function _desHash($plain) 00309 { 00310 $key = $this->_desAddParity($plain); 00311 $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, ''); 00312 $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 00313 mcrypt_generic_init($td, $key, $iv); 00314 $hash = mcrypt_generic($td, 'KGS!@#$%'); 00315 mcrypt_generic_deinit($td); 00316 mcrypt_module_close($td); 00317 return $hash; 00318 } 00319 00327 function _desAddParity($key) 00328 { 00329 static $odd_parity = array( 00330 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14, 00331 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31, 00332 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47, 00333 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62, 00334 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79, 00335 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94, 00336 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110, 00337 112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127, 00338 128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143, 00339 145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158, 00340 161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174, 00341 176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191, 00342 193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206, 00343 208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223, 00344 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239, 00345 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254); 00346 00347 $bin = ''; 00348 for ($i = 0; $i < strlen($key); $i++) { 00349 $bin .= sprintf('%08s', decbin(ord($key{$i}))); 00350 } 00351 00352 $str1 = explode('-', substr(chunk_split($bin, 7, '-'), 0, -1)); 00353 $x = ''; 00354 foreach($str1 as $s) { 00355 $x .= sprintf('%02s', dechex($odd_parity[bindec($s . '0')])); 00356 } 00357 00358 return pack('H*', $x); 00359 00360 } 00361 00369 function response($lm = false) 00370 { 00371 $ntresp = $this->ntChallengeResponse(); 00372 if ($lm) { 00373 $lmresp = $this->lmChallengeResponse(); 00374 } else { 00375 $lmresp = str_repeat ("\0", 24); 00376 } 00377 00378 // Response: LM Response, NT Response, flags (0 = use LM Response, 1 = use NT Response) 00379 return $lmresp . $ntresp . pack('C', !$lm); 00380 } 00381 } 00382 00393 class Crypt_CHAP_MSv2 extends Crypt_CHAP_MSv1 00394 { 00399 var $username = null; 00400 00405 var $peerChallenge = null; 00406 00411 var $authChallenge = null; 00412 00419 function Crypt_CHAP_MSv2() 00420 { 00421 $this->Crypt_CHAP_MSv1(); 00422 $this->generateChallenge('peerChallenge', 16); 00423 $this->generateChallenge('authChallenge', 16); 00424 } 00425 00433 function ntPasswordHashHash($nthash) 00434 { 00435 return mhash(MHASH_MD4, $nthash); 00436 } 00437 00445 function challengeHash() 00446 { 00447 return substr(mhash(MHASH_SHA1, $this->peerChallenge . $this->authChallenge . $this->username), 0, 8); 00448 } 00449 00456 function challengeResponse() 00457 { 00458 $this->challenge = $this->challengeHash(); 00459 return $this->_challengeResponse(); 00460 } 00461 } 00462 00463 00464 ?>