Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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