0
|
1 |
<?php
|
|
2 |
|
|
3 |
/*
|
|
4 |
Copyright (c) 2003, Michael Bretterklieber <michael@bretterklieber.com>
|
|
5 |
All rights reserved.
|
|
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without
|
|
8 |
modification, are permitted provided that the following conditions
|
|
9 |
are met:
|
|
10 |
|
|
11 |
1. Redistributions of source code must retain the above copyright
|
|
12 |
notice, this list of conditions and the following disclaimer.
|
|
13 |
2. Redistributions in binary form must reproduce the above copyright
|
|
14 |
notice, this list of conditions and the following disclaimer in the
|
|
15 |
documentation and/or other materials provided with the distribution.
|
|
16 |
3. The names of the authors may not be used to endorse or promote products
|
|
17 |
derived from this software without specific prior written permission.
|
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29 |
|
|
30 |
The author of this file respectfully requests that you refrain from
|
|
31 |
relicensing it under the GPL, although the BSD license permits you to do so.
|
|
32 |
|
|
33 |
$Id: mschap.php,v 1.5 2003/01/26 20:31:11 mbretter Exp $
|
|
34 |
*/
|
|
35 |
|
|
36 |
function des_encrypt_ecb($key, $clearText)
|
|
37 |
{
|
|
38 |
return mcrypt_ecb (MCRYPT_DES, $key, $clearText, MCRYPT_ENCRYPT, str_pad("", 8, chr(0x00)));
|
|
39 |
}
|
|
40 |
|
|
41 |
function NtPasswordHash($plain)
|
|
42 |
{
|
|
43 |
return mhash (MHASH_MD4, str2unicode($plain));
|
|
44 |
}
|
|
45 |
|
|
46 |
function str2unicode($str)
|
|
47 |
{
|
|
48 |
$uni = '';
|
|
49 |
for ($i=0;$i<strlen($str);$i++) {
|
|
50 |
$a = ord($str{$i}) << 8;
|
|
51 |
$uni .= sprintf("%X",$a);
|
|
52 |
}
|
|
53 |
return pack('H*', $uni);
|
|
54 |
}
|
|
55 |
|
|
56 |
function GenerateChallenge($size = 8)
|
|
57 |
{
|
|
58 |
$chall = '';
|
|
59 |
mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
|
|
60 |
for($i = 0; $i < $size; $i++) {
|
|
61 |
$chall .= pack('C', 1 + mt_rand() % 255);
|
|
62 |
}
|
|
63 |
return $chall;
|
|
64 |
}
|
|
65 |
|
|
66 |
function ChallengeResponse($challenge, $nthash)
|
|
67 |
{
|
|
68 |
while (strlen($nthash) < 21)
|
|
69 |
$nthash .= "\0";
|
|
70 |
|
|
71 |
$resp1 = des_encrypt_ecb(substr($nthash, 0, 7), $challenge);
|
|
72 |
$resp2 = des_encrypt_ecb(substr($nthash, 7, 7), $challenge);
|
|
73 |
$resp3 = des_encrypt_ecb(substr($nthash, 14, 7), $challenge);
|
|
74 |
|
|
75 |
return $resp1 . $resp2 . $resp3;
|
|
76 |
}
|
|
77 |
|
|
78 |
// MS-CHAPv2
|
|
79 |
|
|
80 |
function GeneratePeerChallenge()
|
|
81 |
{
|
|
82 |
return GenerateChallenge(16);
|
|
83 |
}
|
|
84 |
|
|
85 |
function NtPasswordHashHash($hash)
|
|
86 |
{
|
|
87 |
return mhash (MHASH_MD4, $hash);
|
|
88 |
}
|
|
89 |
|
|
90 |
function ChallengeHash($challenge, $peerChallenge, $username)
|
|
91 |
{
|
|
92 |
return substr(mhash (MHASH_SHA1, $peerChallenge . $challenge . $username), 0, 8);
|
|
93 |
}
|
|
94 |
|
|
95 |
function GenerateNTResponse($challenge, $peerChallenge, $username, $password)
|
|
96 |
{
|
|
97 |
$challengeHash = ChallengeHash($challenge, $peerChallenge, $username);
|
|
98 |
$pwhash = NtPasswordHash($password);
|
|
99 |
return ChallengeResponse($challengeHash, $pwhash);
|
|
100 |
}
|
|
101 |
|