-
Notifications
You must be signed in to change notification settings - Fork 4
/
mschap.c
145 lines (127 loc) · 4.11 KB
/
mschap.c
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
/*
* mschap.c
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2001,2006,2010 The FreeRADIUS server project
*/
/*
* This implements MS-CHAP, as described in RFC 2548
*
* http://www.freeradius.org/rfc/rfc2548.txt
*
*/
#include "ident.h"
#include "md5.h"
#include "sha1.h"
#include "md4.h"
#include <ctype.h>
#include <stdint.h>
#include "smbdes.h"
/*
* ntpwdhash converts Unicode password to 16-byte NT hash
* with MD4
*/
void mschap_ntpwdhash (uint8_t *szHash, const char *szPassword,size_t nPasswordLen)
{
char szUnicodePass[513] = {0};
int i;
/*
* NT passwords are unicode. Convert plain text password
* to unicode by inserting a zero every other byte
*/
for (i = 0; i < nPasswordLen; i++) {
szUnicodePass[i << 1] = szPassword[i];
szUnicodePass[(i << 1) + 1] = 0;
}
/* Encrypt Unicode password to a 16-byte MD4 hash */
fr_md4_calc(szHash, (uint8_t *) szUnicodePass, (nPasswordLen<<1) );
}
/*
* challenge_hash() is used by mschap2() and auth_response()
* implements RFC2759 ChallengeHash()
* generates 64 bit challenge
*/
void mschap_challenge_hash( const uint8_t *peer_challenge,
const uint8_t *auth_challenge,
const char *user_name,size_t len,uint8_t *challenge )
{
fr_SHA1_CTX Context;
uint8_t hash[20] = {0};
fr_SHA1Init(&Context);
fr_SHA1Update(&Context, peer_challenge, 16);
fr_SHA1Update(&Context, auth_challenge, 16);
fr_SHA1Update(&Context, (const uint8_t *) user_name,
len);
fr_SHA1Final(hash, &Context);
memcpy(challenge, hash, 8);
}
/*
* auth_response() generates MS-CHAP v2 SUCCESS response
* according to RFC 2759 GenerateAuthenticatorResponse()
* returns 42-octet response string
*/
void mschap_auth_response(const char *username,size_t len,
const uint8_t *nt_hash_hash,
uint8_t *ntresponse,
uint8_t *peer_challenge, uint8_t *auth_challenge,
char *response)
{
fr_SHA1_CTX Context;
static const uint8_t magic1[39] =
{0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74};
static const uint8_t magic2[41] =
{0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
0x6E};
static const char hex[16] = "0123456789ABCDEF";
size_t i;
uint8_t challenge[8];
uint8_t digest[20];
fr_SHA1Init(&Context);
fr_SHA1Update(&Context, nt_hash_hash, 16);
fr_SHA1Update(&Context, ntresponse, 24);
fr_SHA1Update(&Context, magic1, 39);
fr_SHA1Final(digest, &Context);
mschap_challenge_hash(peer_challenge, auth_challenge,username,len,challenge);
fr_SHA1Init(&Context);
fr_SHA1Update(&Context, digest, 20);
fr_SHA1Update(&Context, challenge, 8);
fr_SHA1Update(&Context, magic2, 41);
fr_SHA1Final(digest, &Context);
/*
* Encode the value of 'Digest' as "S=" followed by
* 40 ASCII hexadecimal digits and return it in
* AuthenticatorResponse.
* For example,
* "S=0123456789ABCDEF0123456789ABCDEF01234567"
*/
response[0] = 'S';
response[1] = '=';
/*
* The hexadecimal digits [A-F] MUST be uppercase.
*/
for (i = 0; i < sizeof(digest); i++) {
response[2 + (i * 2)] = hex[(digest[i] >> 4) & 0x0f];
response[3 + (i * 2)] = hex[digest[i] & 0x0f];
}
}