]> git.ozlabs.org Git - ppp.git/blob - pppd/chap_ms.c
763693753e3a1d00a358088c7f86779e082534f4
[ppp.git] / pppd / chap_ms.c
1 /*
2  * chap_ms.c - Microsoft MS-CHAP compatible implementation.
3  *
4  * Copyright (c) 1995 Eric Rosenquist, Strata Software Limited.
5  * http://www.strataware.com/
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by Eric Rosenquist.  The name of the author may not be used to
15  * endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22
23 /*
24  * Modifications by Lauri Pesonen / lpesonen@clinet.fi, april 1997
25  *
26  *   Implemented LANManager type password response to MS-CHAP challenges.
27  *   Now pppd provides both NT style and LANMan style blocks, and the
28  *   prefered is set by option "ms-lanman". Default is to use NT.
29  *   The hash text (StdText) was taken from Win95 RASAPI32.DLL.
30  *
31  *   You should also use DOMAIN\\USERNAME as described in README.MSCHAP80
32  */
33
34 #ifndef lint
35 static char rcsid[] = "$Id: chap_ms.c,v 1.9 1998/09/04 18:48:12 christos Exp $";
36 #endif
37
38 #ifdef CHAPMS
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <syslog.h>
47 #include <unistd.h>
48 #ifdef HAVE_CRYPT_H
49 #include <crypt.h>
50 #endif
51
52 #include "pppd.h"
53 #include "chap.h"
54 #include "chap_ms.h"
55 #include "md4.h"
56
57 #ifndef USE_CRYPT
58 #include <des.h>
59 #endif
60
61 typedef struct {
62     u_char LANManResp[24];
63     u_char NTResp[24];
64     u_char UseNT;               /* If 1, ignore the LANMan response field */
65 } MS_ChapResponse;
66 /* We use MS_CHAP_RESPONSE_LEN, rather than sizeof(MS_ChapResponse),
67    in case this struct gets padded. */
68
69
70 static void     ChallengeResponse __P((u_char *, u_char *, u_char *));
71 static void     DesEncrypt __P((u_char *, u_char *, u_char *));
72 static void     MakeKey __P((u_char *, u_char *));
73 static u_char   Get7Bits __P((u_char *, int));
74 static void     ChapMS_NT __P((char *, int, char *, int, MS_ChapResponse *));
75 #ifdef MSLANMAN
76 static void     ChapMS_LANMan __P((char *, int, char *, int, MS_ChapResponse *));
77 #endif
78
79 #ifdef USE_CRYPT
80 static void     Expand __P((u_char *, u_char *));
81 static void     Collapse __P((u_char *, u_char *));
82 #endif
83
84 static void
85 ChallengeResponse(challenge, pwHash, response)
86     u_char *challenge;  /* IN   8 octets */
87     u_char *pwHash;     /* IN  16 octets */
88     u_char *response;   /* OUT 24 octets */
89 {
90     char    ZPasswordHash[21];
91
92     BZERO(ZPasswordHash, sizeof(ZPasswordHash));
93     BCOPY(pwHash, ZPasswordHash, MD4_SIGNATURE_SIZE);
94
95 #if 0
96     log_packet(ZPasswordHash, sizeof(ZPasswordHash), "ChallengeResponse - ZPasswordHash", LOG_DEBUG);
97 #endif
98
99     DesEncrypt(challenge, ZPasswordHash +  0, response + 0);
100     DesEncrypt(challenge, ZPasswordHash +  7, response + 8);
101     DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
102
103 #if 0
104     log_packet(response, 24, "ChallengeResponse - response", LOG_DEBUG);
105 #endif
106 }
107
108
109 #ifdef USE_CRYPT
110 static void
111 DesEncrypt(clear, key, cipher)
112     u_char *clear;      /* IN  8 octets */
113     u_char *key;        /* IN  7 octets */
114     u_char *cipher;     /* OUT 8 octets */
115 {
116     u_char des_key[8];
117     u_char crypt_key[66];
118     u_char des_input[66];
119
120     MakeKey(key, des_key);
121
122     Expand(des_key, crypt_key);
123     setkey(crypt_key);
124
125 #if 0
126     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
127                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
128 #endif
129
130     Expand(clear, des_input);
131     encrypt(des_input, 0);
132     Collapse(des_input, cipher);
133
134 #if 0
135     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
136                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
137 #endif
138 }
139
140 #else /* USE_CRYPT */
141
142 static void
143 DesEncrypt(clear, key, cipher)
144     u_char *clear;      /* IN  8 octets */
145     u_char *key;        /* IN  7 octets */
146     u_char *cipher;     /* OUT 8 octets */
147 {
148     des_cblock          des_key;
149     des_key_schedule    key_schedule;
150
151     MakeKey(key, des_key);
152
153     des_set_key(&des_key, key_schedule);
154
155 #if 0
156     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
157                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
158 #endif
159
160     des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
161
162 #if 0
163     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
164                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
165 #endif
166 }
167
168 #endif /* USE_CRYPT */
169
170
171 static u_char Get7Bits(input, startBit)
172     u_char *input;
173     int startBit;
174 {
175     register unsigned int       word;
176
177     word  = (unsigned)input[startBit / 8] << 8;
178     word |= (unsigned)input[startBit / 8 + 1];
179
180     word >>= 15 - (startBit % 8 + 7);
181
182     return word & 0xFE;
183 }
184
185 #ifdef USE_CRYPT
186
187 /* in == 8-byte string (expanded version of the 56-bit key)
188  * out == 64-byte string where each byte is either 1 or 0
189  * Note that the low-order "bit" is always ignored by by setkey()
190  */
191 static void Expand(in, out)
192     u_char *in;
193     u_char *out;
194 {
195         int j, c;
196         int i;
197
198         for(i = 0; i < 64; in++){
199                 c = *in;
200                 for(j = 7; j >= 0; j--)
201                         *out++ = (c >> j) & 01;
202                 i += 8;
203         }
204 }
205
206 /* The inverse of Expand
207  */
208 static void Collapse(in, out)
209     u_char *in;
210     u_char *out;
211 {
212         int j;
213         int i;
214         unsigned int c;
215
216         for (i = 0; i < 64; i += 8, out++) {
217             c = 0;
218             for (j = 7; j >= 0; j--, in++)
219                 c |= *in << j;
220             *out = c & 0xff;
221         }
222 }
223 #endif
224
225 static void MakeKey(key, des_key)
226     u_char *key;        /* IN  56 bit DES key missing parity bits */
227     u_char *des_key;    /* OUT 64 bit DES key with parity bits added */
228 {
229     des_key[0] = Get7Bits(key,  0);
230     des_key[1] = Get7Bits(key,  7);
231     des_key[2] = Get7Bits(key, 14);
232     des_key[3] = Get7Bits(key, 21);
233     des_key[4] = Get7Bits(key, 28);
234     des_key[5] = Get7Bits(key, 35);
235     des_key[6] = Get7Bits(key, 42);
236     des_key[7] = Get7Bits(key, 49);
237
238 #ifndef USE_CRYPT
239     des_set_odd_parity((des_cblock *)des_key);
240 #endif
241
242 #if 0
243     CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %02X%02X%02X%02X%02X%02X%02X",
244                key[0], key[1], key[2], key[3], key[4], key[5], key[6]));
245     CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X",
246                des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));
247 #endif
248 }
249
250 static void
251 ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, response)
252     char *rchallenge;
253     int rchallenge_len;
254     char *secret;
255     int secret_len;
256     MS_ChapResponse    *response;
257 {
258     int                 i;
259     MD4_CTX             md4Context;
260     u_char              hash[MD4_SIGNATURE_SIZE];
261     u_char              unicodePassword[MAX_NT_PASSWORD * 2];
262
263     /* Initialize the Unicode version of the secret (== password). */
264     /* This implicitly supports 8-bit ISO8859/1 characters. */
265     BZERO(unicodePassword, sizeof(unicodePassword));
266     for (i = 0; i < secret_len; i++)
267         unicodePassword[i * 2] = (u_char)secret[i];
268
269     MD4Init(&md4Context);
270     MD4Update(&md4Context, unicodePassword, secret_len * 2 * 8);        /* Unicode is 2 bytes/char, *8 for bit count */
271
272     MD4Final(hash, &md4Context);        /* Tell MD4 we're done */
273
274     ChallengeResponse(rchallenge, hash, response->NTResp);
275 }
276
277 #ifdef MSLANMAN
278 static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
279
280 static void
281 ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, response)
282     char *rchallenge;
283     int rchallenge_len;
284     char *secret;
285     int secret_len;
286     MS_ChapResponse     *response;
287 {
288     int                 i;
289     u_char              UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
290     u_char              PasswordHash[MD4_SIGNATURE_SIZE];
291
292     /* LANMan password is case insensitive */
293     BZERO(UcasePassword, sizeof(UcasePassword));
294     for (i = 0; i < secret_len; i++)
295        UcasePassword[i] = (u_char)toupper(secret[i]);
296     DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
297     DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
298     ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
299 }
300 #endif
301
302 void
303 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len)
304     chap_state *cstate;
305     char *rchallenge;
306     int rchallenge_len;
307     char *secret;
308     int secret_len;
309 {
310     MS_ChapResponse     response;
311 #ifdef MSLANMAN
312     extern int ms_lanman;
313 #endif
314
315 #if 0
316     CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'", secret_len, secret));
317 #endif
318     BZERO(&response, sizeof(response));
319
320     /* Calculate both always */
321     ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
322
323 #ifdef MSLANMAN
324     ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
325
326     /* prefered method is set by option  */
327     response.UseNT = !ms_lanman;
328 #else
329     response.UseNT = 1;
330 #endif
331
332     BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);
333     cstate->resp_length = MS_CHAP_RESPONSE_LEN;
334 }
335
336 #endif /* CHAPMS */