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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
<?php
/*
Copyright (c) 2002-2010, Michael Bretterklieber <michael@bretterklieber.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This code cannot simply be copied and put under the GNU Public License or
any other GPL-like (LGPL, GPL2) License.
$Id: CHAP.php 302857 2010-08-28 21:12:59Z mbretter $
This version of CHAP.php has been modified by Drew Phillips for dapphp/radius.
Modifications remove the PEAR dependency, change from PHP4 OOP to PHP5, and
mcrypt functions have been replaced with openssl_* functions.
Changes are all commented inline throughout the source.
$Id: Pear_CHAP.php 2.5.2 2018-01-25 03:30:29Z dapphp $
*/
// require_once 'PEAR.php'; // removed for dapphp/radius
/**
* Classes for generating packets for various CHAP Protocols:
* CHAP-MD5: RFC1994
* MS-CHAPv1: RFC2433
* MS-CHAPv2: RFC2759
*
* @package Crypt_CHAP
* @author Michael Bretterklieber <michael@bretterklieber.com>
* @access public
*/
/**
* class Crypt_CHAP
*
* Abstract base class for CHAP
*
* @package Crypt_CHAP
*/
class Crypt_CHAP /*extends PEAR // removed for dapphp/radius */
{
/**
* Random binary challenge
* @var string
*/
public $challenge = null;
//var $challenge = null; // removed for dapphp/radius
/**
* Binary response
* @var string
*/
public $response = null;
//var $response = null; // removed for dapphp/radius
/**
* User password
* @var string
*/
public $password = null;
//var $password = null; // removed for dapphp/radius
/**
* Id of the authentication request. Should incremented after every request.
* @var integer
*/
public $chapid = 1;
//var $chapid = 1; // removed for dapphp/radius
/**
* Constructor
*
* Generates a random challenge
* @return void
*/
//function Crypt_CHAP() // removed for dapphp/radius
public function __construct()
{
//$this->PEAR();
$this->generateChallenge();
}
/**
* Generates a random binary challenge
*
* @param string $varname Name of the property
* @param integer $size Size of the challenge in Bytes
* @return void
*/
//function generateChallenge($varname = 'challenge', $size = 8) // removed for dapphp/radius
public function generateChallenge($varname = 'challenge', $size = 8)
{
$this->$varname = '';
for ($i = 0; $i < $size; $i++) {
$this->$varname .= pack('C', 1 + mt_rand() % 255);
}
return $this->$varname;
}
/**
* Generates the response. Overwrite this.
*
* @return void
*/
//function challengeResponse() // removed for dapphp/radius
public function challengeResponse()
{
}
}
/**
* class Crypt_CHAP_MD5
*
* Generate CHAP-MD5 Packets
*
* @package Crypt_CHAP
*/
class Crypt_CHAP_MD5 extends Crypt_CHAP
{
/**
* Generates the response.
*
* CHAP-MD5 uses MD5-Hash for generating the response. The Hash consists
* of the chapid, the plaintext password and the challenge.
*
* @return string
*/
//function challengeResponse() // removed for dapphp/radius
public function challengeResponse()
{
return pack('H*', md5(pack('C', $this->chapid) . $this->password . $this->challenge));
}
}
/**
* class Crypt_CHAP_MSv1
*
* Generate MS-CHAPv1 Packets. MS-CHAP doesen't use the plaintext password, it uses the
* NT-HASH wich is stored in the SAM-Database or in the smbpasswd, if you are using samba.
* The NT-HASH is MD4(str2unicode(plaintextpass)).
* You need the hash extension for this class.
*
* @package Crypt_CHAP
*/
class Crypt_CHAP_MSv1 extends Crypt_CHAP
{
/**
* Wether using deprecated LM-Responses or not.
* 0 = use LM-Response, 1 = use NT-Response
* @var bool
*/
protected $flags = 1;
//var $flags = 1; // removed for dapphp/radius
protected $useMcrypt = false; // added for dapphp/radius (php 5.3 must use mcrypt)
/**
* Constructor
*
* Loads the hash extension
* @return void
*/
//function Crypt_CHAP_MSv1() // removed for dapphp/radius
public function __construct()
{
parent::__construct();
// removed for dapphp/radius
//$this->Crypt_CHAP();
//$this->loadExtension('hash');
// added openssl & mcrypt check for dapphp/radius
if (!extension_loaded('openssl') && !extension_loaded('mcrypt')) {
throw new \Exception("openssl and mcrypt are not installed; cannot use Radius MSCHAP functions");
}
// Added mcrypt check for PHP 5.3 for dapphp/radius
// OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING are required but not
// supported by ext/openssl until PHP 5.4.
if (version_compare(PHP_VERSION, '5.4') < 0) {
if (!extension_loaded('mcrypt')) {
throw new \Exception("Radius MSCHAP functions require mcrypt extension for PHP 5.3");
}
$this->useMcrypt = true;
}
}
/**
* Generates the NT-HASH from the given plaintext password.
*
* @access public
* @return string
*/
//function ntPasswordHash($password = null) // removed for dapphp/radius
public function ntPasswordHash($password = null)
{
//if (isset($password)) {
if (!is_null($password)) {
return pack('H*',hash('md4', $this->str2unicode($password)));
} else {
return pack('H*',hash('md4', $this->str2unicode($this->password)));
}
}
/**
* Converts ascii to unicode.
*
* @access public
* @return string
*/
//function str2unicode($str) // removed for dapphp/radius
public function str2unicode($str)
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($str, 'UTF-16LE');
} else {
$uni = '';
$str = (string) $str;
for ($i = 0; $i < strlen($str); $i++) {
$a = ord($str[$i]) << 8;
$uni .= sprintf("%X", $a);
}
return pack('H*', $uni);
}
}
/**
* Generates the NT-Response.
*
* @access public
* @return string
*/
//function challengeResponse() // removed for dapphp/radius
public function challengeResponse()
{
return $this->_challengeResponse();
}
/**
* Generates the NT-Response.
*
* @access public
* @return string
*/
//function ntChallengeResponse() // removed for dapphp/radius
public function ntChallengeResponse()
{
return $this->_challengeResponse(false);
}
/**
* Generates the LAN-Manager-Response.
*
* @access public
* @return string
*/
//function lmChallengeResponse() // removed for dapphp/radius
public function lmChallengeResponse()
{
return $this->_challengeResponse(true);
}
/**
* Generates the response.
*
* Generates the response using DES.
*
* @param bool $lm wether generating LAN-Manager-Response
* @access private
* @return string
*/
//function _challengeResponse($lm = false) // removed for dapphp/radius
protected function _challengeResponse($lm = false)
{
if ($lm) {
$hash = $this->lmPasswordHash();
} else {
$hash = $this->ntPasswordHash();
}
$hash = str_pad($hash, 21, "\0");
if (extension_loaded('openssl') && $this->useMcrypt === false) {
// added openssl routines for dapphp/radius
$key = $this->_desAddParity(substr($hash, 0, 7));
$resp1 = openssl_encrypt($this->challenge, 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$key = $this->_desAddParity(substr($hash, 7, 7));
$resp2 = openssl_encrypt($this->challenge, 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$key = $this->_desAddParity(substr($hash, 14, 7));
$resp3 = openssl_encrypt($this->challenge, 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
} else {
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$key = $this->_desAddParity(substr($hash, 0, 7));
mcrypt_generic_init($td, $key, $iv);
$resp1 = mcrypt_generic($td, $this->challenge);
mcrypt_generic_deinit($td);
$key = $this->_desAddParity(substr($hash, 7, 7));
mcrypt_generic_init($td, $key, $iv);
$resp2 = mcrypt_generic($td, $this->challenge);
mcrypt_generic_deinit($td);
$key = $this->_desAddParity(substr($hash, 14, 7));
mcrypt_generic_init($td, $key, $iv);
$resp3 = mcrypt_generic($td, $this->challenge);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
return $resp1 . $resp2 . $resp3;
}
/**
* Generates the LAN-Manager-HASH from the given plaintext password.
*
* @access public
* @return string
*/
//function lmPasswordHash($password = null) // removed for dapphp/radius
public function lmPasswordHash($password = null)
{
$plain = isset($password) ? $password : $this->password;
$plain = substr(strtoupper($plain), 0, 14);
while (strlen($plain) < 14) {
$plain .= "\0";
}
return $this->_desHash(substr($plain, 0, 7)) . $this->_desHash(substr($plain, 7, 7));
}
/**
* Generates an irreversible HASH.
*
* @access private
* @return string
*/
//function _desHash($plain) // removed for dapphp/radius
private function _desHash($plain)
{
if (extension_loaded('openssl') && $this->useMcrypt === false) {
// added openssl routines for dapphp/radius
$key = $this->_desAddParity($plain);
$hash = openssl_encrypt('KGS!@#$%', 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
return $hash;
} else {
$key = $this->_desAddParity($plain);
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$hash = mcrypt_generic($td, 'KGS!@#$%');
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $hash;
}
}
/**
* Adds the parity bit to the given DES key.
*
* @access private
* @param string $key 7-Bytes Key without parity
* @return string
*/
//function _desAddParity($key) // removed for dapphp/radius
protected function _desAddParity($key)
{
static $odd_parity = array(
1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254);
$bin = '';
for ($i = 0; $i < strlen($key); $i++) {
$bin .= sprintf('%08s', decbin(ord($key[$i])));
}
$str1 = explode('-', substr(chunk_split($bin, 7, '-'), 0, -1));
$x = '';
foreach($str1 as $s) {
$x .= sprintf('%02s', dechex($odd_parity[bindec($s . '0')]));
}
return pack('H*', $x);
}
/**
* Generates the response-packet.
*
* @param bool $lm wether including LAN-Manager-Response
* @access private
* @return string
*/
//function response($lm = false) // removed for dapphp/radius
public function response($lm = false)
{
$ntresp = $this->ntChallengeResponse();
if ($lm) {
$lmresp = $this->lmChallengeResponse();
} else {
$lmresp = str_repeat ("\0", 24);
}
// Response: LM Response, NT Response, flags (0 = use LM Response, 1 = use NT Response)
return $lmresp . $ntresp . pack('C', !$lm);
}
}
/**
* class Crypt_CHAP_MSv2
*
* Generate MS-CHAPv2 Packets. This version of MS-CHAP uses a 16 Bytes authenticator
* challenge and a 16 Bytes peer Challenge. LAN-Manager responses no longer exists
* in this version. The challenge is already a SHA1 challenge hash of both challenges
* and of the username.
*
* @package Crypt_CHAP
*/
class Crypt_CHAP_MSv2 extends Crypt_CHAP_MSv1
{
/**
* The username
* @var string
*/
public $username = null;
//var $username = null; // removed for dapphp/radius
/**
* The 16 Bytes random binary peer challenge
* @var string
*/
public $peerChallenge = null;
//var $peerChallenge = null; // removed for dapphp/radius
/**
* The 16 Bytes random binary authenticator challenge
* @var string
*/
public $authChallenge = null;
//var $authChallenge = null; // removed for dapphp/radius
/**
* Constructor
*
* Generates the 16 Bytes peer and authentication challenge
* @return void
*/
//function Crypt_CHAP_MSv2() // removed for dapphp/radius
public function __construct()
{
//$this->Crypt_CHAP_MSv1(); // removed for dapphp/radius
parent::__construct();
$this->generateChallenge('peerChallenge', 16);
$this->generateChallenge('authChallenge', 16);
}
/**
* Generates a hash from the NT-HASH.
*
* @access public
* @param string $nthash The NT-HASH
* @return string
*/
//function ntPasswordHashHash($nthash) // removed for dapphp/radius
public function ntPasswordHashHash($nthash)
{
return pack('H*',hash('md4', $nthash));
}
/**
* Generates the challenge hash from the peer and the authenticator challenge and
* the username. SHA1 is used for this, but only the first 8 Bytes are used.
*
* @access public
* @return string
*/
//function challengeHash() // removed for dapphp/radius
public function challengeHash()
{
return substr(pack('H*',hash('sha1', $this->peerChallenge . $this->authChallenge . $this->username)), 0, 8);
}
/**
* Generates the response.
*
* @access public
* @return string
*/
//function challengeResponse() // removed for dapphp/radius
public function challengeResponse()
{
$this->challenge = $this->challengeHash();
return $this->_challengeResponse();
}
/**
* Generates the encrypted new password.
*
* @access public
* @param string $newPassword The new plain text password
* @param string $oldPassword The old plain text password
* @return string EncryptedPwBlock
*/
public function newPasswordEncryptedWithOldNtPasswordHash($newPassword, $oldPassword)
{
$passwordHash = $this->ntPasswordHash($oldPassword);
return $this->encryptPwBlockWithPasswordHash($this->str2unicode($newPassword), $passwordHash);
}
/**
* Generates PwBlock
*
* @access public
* @param string $password New password
* @param string $passwordHash Old password hash
* @return string PwBlock
*/
public function encryptPwBlockWithPasswordHash($password, $passwordHash)
{
// [516=2*256+4] unicode(2) maxpasslength(256) passlength(4)
$clearPwBlock = random_bytes(516);
$pwSize = strlen($password);
$pwOffset = strlen($clearPwBlock) - $pwSize - 4;
$clearPwBlock = substr_replace($clearPwBlock, $password, $pwOffset, $pwSize);
$clearPwBlock = substr_replace($clearPwBlock, pack("V", $pwSize), -4, 4);
return $this->rc4($passwordHash, $clearPwBlock);
}
/**
* RC4 symmetric cipher encryption/decryption
*
* @access public
* @param string key - secret key for encryption/decryption
* @param string str - string to be encrypted/decrypted
* @return string
*/
public function rc4($key, $str)
{
$s = array();
for ($i = 0; $i < 256; $i++) {
$s[$i] = $i;
}
$j = 0;
for ($i = 0; $i < 256; $i++) {
$j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256;
$x = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $x;
}
$i = 0;
$j = 0;
$res = '';
for ($y = 0; $y < strlen($str); $y++) {
$i = ($i + 1) % 256;
$j = ($j + $s[$i]) % 256;
$x = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $x;
$res .= $str[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
}
return $res;
}
/**
* ?
*
* @access public
* @param string $newPassword The new plain text password
* @param string $oldPassword The old plain text password
* @return string EncryptedPasswordHash
*/
public function oldNtPasswordHashEncryptedWithNewNtPasswordHash($newPassword, $oldPassword)
{
$oldPasswordHash = $this->ntPasswordHash($oldPassword);
$newPasswordHash = $this->ntPasswordHash($newPassword);
return $this->ntPasswordHashEncryptedWithBlock($oldPasswordHash, $newPasswordHash);
}
/**
* ?
*
* @access public
* @param string $passwordHash Password hash to encrypt
* @param string $block Key to use for encryption
* @return string
*/
public function ntPasswordHashEncryptedWithBlock($passwordHash, $block)
{
if (extension_loaded('openssl') && $this->useMcrypt === false) {
$key = $this->_desAddParity(substr($block, 0, 7));
$resp1 = openssl_encrypt(substr($passwordHash, 0, 8), 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$key = $this->_desAddParity(substr($block, 7, 7));
$resp2 = openssl_encrypt(substr($passwordHash, 8, 8), 'des-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
} else {
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$key = $this->_desAddParity(substr($block, 0, 7));
mcrypt_generic_init($td, $key, $iv);
$resp1 = mcrypt_generic($td, substr($passwordHash, 0, 8));
mcrypt_generic_deinit($td);
$key = $this->_desAddParity(substr($block, 7, 7));
mcrypt_generic_init($td, $key, $iv);
$resp2 = mcrypt_generic($td, substr($passwordHash, 8, 8));
mcrypt_generic_deinit($td);
}
return $resp1 . $resp2;
}
}
|