2 * Copyright (c) 1997 Gabor Kincses <gabor@acm.org>
3 * 1997 - 2001 Brian Somers <brian@Awfulhak.org>
4 * based on work by Eric Rosenquist
5 * Strata Software Limited.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/usr.sbin/ppp/chap_ms.c,v 1.9.2.6 2002/09/01 02:12:23 brian Exp $
30 * $DragonFly: src/usr.sbin/ppp/chap_ms.c,v 1.4 2004/03/27 01:39:13 cpressey Exp $
35 #include <openssl/des.h>
38 #include <sys/types.h>
41 #include <openssl/des.h>
45 #include <openssl/sha.h>
53 * Documentation & specifications:
55 * MS-CHAP (CHAP80) rfc2433
56 * MS-CHAP-V2 (CHAP81) rfc2759
57 * MPPE key management draft-ietf-pppext-mppe-keys-02.txt
60 static char SHA1_Pad1[40] =
61 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
66 static char SHA1_Pad2[40] =
67 {0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
68 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
69 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
70 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2};
72 /* unused, for documentation only */
73 /* only NTResp is filled in for FreeBSD */
74 struct MS_ChapResponse {
75 u_char LANManResp[24];
77 u_char UseNT; /* If 1, ignore the LANMan response field */
81 Get7Bits(u_char *input, int startBit)
85 word = (unsigned)input[startBit / 8] << 8;
86 word |= (unsigned)input[startBit / 8 + 1];
88 word >>= 15 - (startBit % 8 + 7);
93 /* IN 56 bit DES key missing parity bits
94 OUT 64 bit DES key with parity bits added */
96 MakeKey(u_char *key, u_char *des_key)
98 des_key[0] = Get7Bits(key, 0);
99 des_key[1] = Get7Bits(key, 7);
100 des_key[2] = Get7Bits(key, 14);
101 des_key[3] = Get7Bits(key, 21);
102 des_key[4] = Get7Bits(key, 28);
103 des_key[5] = Get7Bits(key, 35);
104 des_key[6] = Get7Bits(key, 42);
105 des_key[7] = Get7Bits(key, 49);
107 des_set_odd_parity((des_cblock *)des_key);
110 static void /* IN 8 octets IN 7 octest OUT 8 octets */
111 DesEncrypt(u_char *clear, u_char *key, u_char *cipher)
114 des_key_schedule key_schedule;
116 MakeKey(key, des_key);
117 des_set_key(&des_key, key_schedule);
118 des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
121 static void /* IN 8 octets IN 16 octets OUT 24 octets */
122 ChallengeResponse(u_char *challenge, u_char *pwHash, u_char *response)
124 char ZPasswordHash[21];
126 memset(ZPasswordHash, '\0', sizeof ZPasswordHash);
127 memcpy(ZPasswordHash, pwHash, 16);
129 DesEncrypt(challenge, ZPasswordHash + 0, response + 0);
130 DesEncrypt(challenge, ZPasswordHash + 7, response + 8);
131 DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
135 NtPasswordHash(char *key, int keylen, char *hash)
139 MD4Init(&MD4context);
140 MD4Update(&MD4context, key, keylen);
141 MD4Final(hash, &MD4context);
145 HashNtPasswordHash(char *hash, char *hashhash)
149 MD4Init(&MD4context);
150 MD4Update(&MD4context, hash, 16);
151 MD4Final(hashhash, &MD4context);
155 ChallengeHash(char *PeerChallenge, char *AuthenticatorChallenge,
156 char *UserName, char *Challenge)
159 char Digest[SHA_DIGEST_LENGTH];
162 Name = strrchr(UserName, '\\');
170 SHA1_Update(&Context, PeerChallenge, 16);
171 SHA1_Update(&Context, AuthenticatorChallenge, 16);
172 SHA1_Update(&Context, Name, strlen(Name));
174 SHA1_Final(Digest, &Context);
175 memcpy(Challenge, Digest, 8);
179 GenerateNTResponse(char *AuthenticatorChallenge, char *PeerChallenge,
180 char *UserName, char *Password,
181 int PasswordLen, char *Response)
184 char PasswordHash[16];
186 ChallengeHash(PeerChallenge, AuthenticatorChallenge, UserName, Challenge);
187 NtPasswordHash(Password, PasswordLen, PasswordHash);
188 ChallengeResponse(Challenge, PasswordHash, Response);
191 #ifndef __DragonFly__
194 SHA1_End(SHA_CTX *ctx, char *buf)
197 unsigned char digest[LENGTH];
198 static const char hex[]="0123456789abcdef";
201 buf = malloc(2*LENGTH + 1);
204 SHA1_Final(digest, ctx);
205 for (i = 0; i < LENGTH; i++) {
206 buf[i+i] = hex[digest[i] >> 4];
207 buf[i+i+1] = hex[digest[i] & 0x0f];
215 GenerateAuthenticatorResponse(char *Password, int PasswordLen,
216 char *NTResponse, char *PeerChallenge,
217 char *AuthenticatorChallenge, char *UserName,
218 char *AuthenticatorResponse)
221 char PasswordHash[16];
222 char PasswordHashHash[16];
224 u_char Digest[SHA_DIGEST_LENGTH];
228 * "Magic" constants used in response generation
231 {0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
232 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
233 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
234 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74};
238 {0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
239 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
240 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
241 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
244 * Hash the password with MD4
246 NtPasswordHash(Password, PasswordLen, PasswordHash);
250 HashNtPasswordHash(PasswordHash, PasswordHashHash);
253 SHA1_Update(&Context, PasswordHashHash, 16);
254 SHA1_Update(&Context, NTResponse, 24);
255 SHA1_Update(&Context, Magic1, 39);
256 SHA1_Final(Digest, &Context);
257 ChallengeHash(PeerChallenge, AuthenticatorChallenge, UserName, Challenge);
259 SHA1_Update(&Context, Digest, 20);
260 SHA1_Update(&Context, Challenge, 8);
261 SHA1_Update(&Context, Magic2, 41);
264 * Encode the value of 'Digest' as "S=" followed by
265 * 40 ASCII hexadecimal digits and return it in
266 * AuthenticatorResponse.
268 * "S=0123456789ABCDEF0123456789ABCDEF01234567"
270 AuthenticatorResponse[0] = 'S';
271 AuthenticatorResponse[1] = '=';
272 SHA1_End(&Context, AuthenticatorResponse + 2);
274 AuthenticatorResponse[i] = toupper(AuthenticatorResponse[i]);
279 GetMasterKey(char *PasswordHashHash, char *NTResponse, char *MasterKey)
281 char Digest[SHA_DIGEST_LENGTH];
283 static char Magic1[27] =
284 {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74,
285 0x68, 0x65, 0x20, 0x4d, 0x50, 0x50, 0x45, 0x20, 0x4d,
286 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x4b, 0x65, 0x79};
289 SHA1_Update(&Context, PasswordHashHash, 16);
290 SHA1_Update(&Context, NTResponse, 24);
291 SHA1_Update(&Context, Magic1, 27);
292 SHA1_Final(Digest, &Context);
293 memcpy(MasterKey, Digest, 16);
297 GetAsymetricStartKey(char *MasterKey, char *SessionKey, int SessionKeyLength,
298 int IsSend, int IsServer)
300 char Digest[SHA_DIGEST_LENGTH];
304 static char Magic2[84] =
305 {0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
306 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
307 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
308 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x6b, 0x65, 0x79,
309 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
310 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,
311 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
312 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
313 0x6b, 0x65, 0x79, 0x2e};
315 static char Magic3[84] =
316 {0x4f, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x69,
317 0x65, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2c, 0x20,
318 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
319 0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
320 0x6b, 0x65, 0x79, 0x3b, 0x20, 0x6f, 0x6e, 0x20, 0x74, 0x68,
321 0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,
322 0x69, 0x64, 0x65, 0x2c, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,
323 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6e, 0x64, 0x20,
324 0x6b, 0x65, 0x79, 0x2e};
341 SHA1_Update(&Context, MasterKey, 16);
342 SHA1_Update(&Context, SHA1_Pad1, 40);
343 SHA1_Update(&Context, s, 84);
344 SHA1_Update(&Context, SHA1_Pad2, 40);
345 SHA1_Final(Digest, &Context);
347 memcpy(SessionKey, Digest, SessionKeyLength);
351 GetNewKeyFromSHA(char *StartKey, char *SessionKey, long SessionKeyLength,
355 char Digest[SHA_DIGEST_LENGTH];
358 SHA1_Update(&Context, StartKey, SessionKeyLength);
359 SHA1_Update(&Context, SHA1_Pad1, 40);
360 SHA1_Update(&Context, SessionKey, SessionKeyLength);
361 SHA1_Update(&Context, SHA1_Pad2, 40);
362 SHA1_Final(Digest, &Context);
364 memcpy(InterimKey, Digest, SessionKeyLength);
369 Get_Key(char *InitialSessionKey, char *CurrentSessionKey,
370 int LengthOfDesiredKey)
373 char Digest[SHA_DIGEST_LENGTH];
376 SHA1_Update(&Context, InitialSessionKey, LengthOfDesiredKey);
377 SHA1_Update(&Context, SHA1_Pad1, 40);
378 SHA1_Update(&Context, CurrentSessionKey, LengthOfDesiredKey);
379 SHA1_Update(&Context, SHA1_Pad2, 40);
380 SHA1_Final(Digest, &Context);
382 memcpy(CurrentSessionKey, Digest, LengthOfDesiredKey);
386 /* passwordHash 16-bytes MD4 hashed password
387 challenge 8-bytes peer CHAP challenge
388 since passwordHash is in a 24-byte buffer, response is written in there */
390 mschap_NT(char *passwordHash, char *challenge)
394 ChallengeResponse(challenge, passwordHash, response);
395 memcpy(passwordHash, response, 24);
396 passwordHash[24] = 1; /* NT-style response */
400 mschap_LANMan(char *digest, char *challenge, char *secret)
402 static u_char salt[] = "KGS!@#$%"; /* RASAPI32.dll */
403 char SECRET[14], *ptr, *end;
406 end = SECRET + sizeof SECRET;
407 for (ptr = SECRET; *secret && ptr < end; ptr++, secret++)
408 *ptr = toupper(*secret);
410 memset(ptr, '\0', end - ptr);
412 DesEncrypt(salt, SECRET, hash);
413 DesEncrypt(salt, SECRET + 7, hash + 8);
415 ChallengeResponse(challenge, hash, digest);