Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Classes for generating packets for CHAP-MD5, MS-CHAPv1, MS-CHAPv2.


git-svn-id: http://svn.php.net/repository/pear/packages/Crypt_CHAP/trunk@114314 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Michael Bretterklieber committed Feb 1, 2003
1 parent 754f6ce commit 23e4bcd
Show file tree
Hide file tree
Showing 3 changed files with 685 additions and 0 deletions.
317 changes: 317 additions & 0 deletions CHAP.php
@@ -0,0 +1,317 @@
<?php
/*
Copyright (c) 2002-2003, 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$
*/

require_once 'PEAR.php';

/**
* Classed 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
* @version $Revision$
*/

/**
* class Crypt_CHAP
*
* Abstract base class for CHAP
*
* @package Crypt_CHAP
*/
class Crypt_CHAP extends PEAR
{
/**
* Random binary challenge
* @var string
*/
var $challenge = null;

/**
* Binary response
* @var string
*/
var $response = null;

/**
* User password
* @var string
*/
var $password = null;

/**
* Id of the authentication request. Should incremented after every request.
* @var integer
*/
var $chapid = 1;

/**
* Constructor
*
* Generates a random challenge
* @return void
*/
function Crypt_CHAP()
{
$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)
{
$this->$varname = '';
mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
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()
{
}

}

/**
* 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()
{
return pack('H*', md5(pack('C', $this->chapid) . $this->password . $this->challenge));
}
}

/**
* class Crypt_MSCHAPv1
*
* 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 mhash extension for this class.
*
* @package Crypt_CHAP
*/
class Crypt_MSCHAPv1 extends Crypt_CHAP
{
/**
* Constructor
*
* Loads the mhash extension
* @return void
*/
function Crypt_MSCHAPv1()
{
$this->Crypt_CHAP();
$this->loadExtension('mhash');
}

/**
* Generates the NT-HASH from the given plaintext password.
*
* @access public
* @return string
*/
function ntPasswordHash($password = null)
{
if (isset($password)) {
return mhash(MHASH_MD4, $this->str2unicode($password));
} else {
return mhash(MHASH_MD4, $this->str2unicode($this->password));
}
}

/**
* Converts ascii to unicode.
*
* @access public
* @return string
*/
function str2unicode($str)
{
for ($i = 0; $i < strlen($str); $i++) {
$a = ord($str{$i}) << 8;
$uni .= sprintf("%X", $a);
}
return pack('H*', $uni);
}

/**
* Generates the response.
*
* @access public
* @return string
*/
function challengeResponse()
{
return $this->_challengeResponse();
}

/**
* Generates the response.
*
* This method inludes a file where des-encryption functions are implemented.
* This is needed to be independent of the mcrypt extension.
*
* @access private
* @return string
*/
function _challengeResponse()
{
require_once 'Crypt_CHAP/_des.php';

$nthash = $this->ntPasswordHash();
while (strlen($nthash) < 21) {
$nthash .= "\0";
}
$resp1 = des_encrypt_ecb(substr($nthash, 0, 7), $this->challenge);
$resp2 = des_encrypt_ecb(substr($nthash, 7, 7), $this->challenge);
$resp3 = des_encrypt_ecb(substr($nthash, 14, 7), $this->challenge);

return $resp1 . $resp2 . $resp3;
}
}

/**
* class Crypt_MSCHAPv2
*
* 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_MSCHAPv2 extends Crypt_MSCHAPv1
{
/**
* The username
* @var string
*/
var $username = null;

/**
* The 16 Bytes random binary peer challenge
* @var string
*/
var $peerChallenge = null;

/**
* The 16 Bytes random binary authenticator challenge
* @var string
*/
var $authChallenge = null;

/**
* Constructor
*
* Generates the 16 Bytes peer and authentication challenge
* @return void
*/
function Crypt_MSCHAPv2()
{
$this->Crypt_MSCHAPv1();
$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)
{
return mhash(MHASH_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()
{
return substr(mhash(MHASH_SHA1, $this->peerChallenge . $this->authChallenge . $this->username), 0, 8);
}

/**
* Generates the response.
*
* @access public
* @return string
*/
function challengeResponse()
{
$this->challenge = $this->challengeHash();
return $this->_challengeResponse();
}
}


?>

0 comments on commit 23e4bcd

Please sign in to comment.