]> git.ozlabs.org Git - ppp.git/blob - pppd/chap_ms.c
take out superfluous check
[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.12 1998/11/24 19:38:05 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 #ifdef MSLANMAN
85 bool    ms_lanman = 0;          /* Use LanMan password instead of NT */
86                                 /* Has meaning only with MS-CHAP challenges */
87 #endif
88
89 static void
90 ChallengeResponse(challenge, pwHash, response)
91     u_char *challenge;  /* IN   8 octets */
92     u_char *pwHash;     /* IN  16 octets */
93     u_char *response;   /* OUT 24 octets */
94 {
95     char    ZPasswordHash[21];
96
97     BZERO(ZPasswordHash, sizeof(ZPasswordHash));
98     BCOPY(pwHash, ZPasswordHash, MD4_SIGNATURE_SIZE);
99
100 #if 0
101     log_packet(ZPasswordHash, sizeof(ZPasswordHash), "ChallengeResponse - ZPasswordHash", LOG_DEBUG);
102 #endif
103
104     DesEncrypt(challenge, ZPasswordHash +  0, response + 0);
105     DesEncrypt(challenge, ZPasswordHash +  7, response + 8);
106     DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
107
108 #if 0
109     log_packet(response, 24, "ChallengeResponse - response", LOG_DEBUG);
110 #endif
111 }
112
113
114 #ifdef USE_CRYPT
115 static void
116 DesEncrypt(clear, key, cipher)
117     u_char *clear;      /* IN  8 octets */
118     u_char *key;        /* IN  7 octets */
119     u_char *cipher;     /* OUT 8 octets */
120 {
121     u_char des_key[8];
122     u_char crypt_key[66];
123     u_char des_input[66];
124
125     MakeKey(key, des_key);
126
127     Expand(des_key, crypt_key);
128     setkey(crypt_key);
129
130 #if 0
131     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
132                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
133 #endif
134
135     Expand(clear, des_input);
136     encrypt(des_input, 0);
137     Collapse(des_input, cipher);
138
139 #if 0
140     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
141                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
142 #endif
143 }
144
145 #else /* USE_CRYPT */
146
147 static void
148 DesEncrypt(clear, key, cipher)
149     u_char *clear;      /* IN  8 octets */
150     u_char *key;        /* IN  7 octets */
151     u_char *cipher;     /* OUT 8 octets */
152 {
153     des_cblock          des_key;
154     des_key_schedule    key_schedule;
155
156     MakeKey(key, des_key);
157
158     des_set_key(&des_key, key_schedule);
159
160 #if 0
161     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
162                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
163 #endif
164
165     des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
166
167 #if 0
168     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
169                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
170 #endif
171 }
172
173 #endif /* USE_CRYPT */
174
175
176 static u_char Get7Bits(input, startBit)
177     u_char *input;
178     int startBit;
179 {
180     register unsigned int       word;
181
182     word  = (unsigned)input[startBit / 8] << 8;
183     word |= (unsigned)input[startBit / 8 + 1];
184
185     word >>= 15 - (startBit % 8 + 7);
186
187     return word & 0xFE;
188 }
189
190 #ifdef USE_CRYPT
191
192 /* in == 8-byte string (expanded version of the 56-bit key)
193  * out == 64-byte string where each byte is either 1 or 0
194  * Note that the low-order "bit" is always ignored by by setkey()
195  */
196 static void Expand(in, out)
197     u_char *in;
198     u_char *out;
199 {
200         int j, c;
201         int i;
202
203         for(i = 0; i < 64; in++){
204                 c = *in;
205                 for(j = 7; j >= 0; j--)
206                         *out++ = (c >> j) & 01;
207                 i += 8;
208         }
209 }
210
211 /* The inverse of Expand
212  */
213 static void Collapse(in, out)
214     u_char *in;
215     u_char *out;
216 {
217         int j;
218         int i;
219         unsigned int c;
220
221         for (i = 0; i < 64; i += 8, out++) {
222             c = 0;
223             for (j = 7; j >= 0; j--, in++)
224                 c |= *in << j;
225             *out = c & 0xff;
226         }
227 }
228 #endif
229
230 static void MakeKey(key, des_key)
231     u_char *key;        /* IN  56 bit DES key missing parity bits */
232     u_char *des_key;    /* OUT 64 bit DES key with parity bits added */
233 {
234     des_key[0] = Get7Bits(key,  0);
235     des_key[1] = Get7Bits(key,  7);
236     des_key[2] = Get7Bits(key, 14);
237     des_key[3] = Get7Bits(key, 21);
238     des_key[4] = Get7Bits(key, 28);
239     des_key[5] = Get7Bits(key, 35);
240     des_key[6] = Get7Bits(key, 42);
241     des_key[7] = Get7Bits(key, 49);
242
243 #ifndef USE_CRYPT
244     des_set_odd_parity((des_cblock *)des_key);
245 #endif
246
247 #if 0
248     CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %02X%02X%02X%02X%02X%02X%02X",
249                key[0], key[1], key[2], key[3], key[4], key[5], key[6]));
250     CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X",
251                des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));
252 #endif
253 }
254
255 static void
256 ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, response)
257     char *rchallenge;
258     int rchallenge_len;
259     char *secret;
260     int secret_len;
261     MS_ChapResponse    *response;
262 {
263     int                 i;
264 #ifdef __NetBSD__
265     /* NetBSD uses the libc md4 routines which take bytes instead of bits */
266     int                 mdlen = secret_len * 2;
267 #else
268     int                 mdlen = secret_len * 2 * 8;
269 #endif
270     MD4_CTX             md4Context;
271     u_char              hash[MD4_SIGNATURE_SIZE];
272     u_char              unicodePassword[MAX_NT_PASSWORD * 2];
273
274     /* Initialize the Unicode version of the secret (== password). */
275     /* This implicitly supports 8-bit ISO8859/1 characters. */
276     BZERO(unicodePassword, sizeof(unicodePassword));
277     for (i = 0; i < secret_len; i++)
278         unicodePassword[i * 2] = (u_char)secret[i];
279
280     MD4Init(&md4Context);
281     MD4Update(&md4Context, unicodePassword, mdlen);
282
283     MD4Final(hash, &md4Context);        /* Tell MD4 we're done */
284
285     ChallengeResponse(rchallenge, hash, response->NTResp);
286 }
287
288 #ifdef MSLANMAN
289 static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
290
291 static void
292 ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, response)
293     char *rchallenge;
294     int rchallenge_len;
295     char *secret;
296     int secret_len;
297     MS_ChapResponse     *response;
298 {
299     int                 i;
300     u_char              UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
301     u_char              PasswordHash[MD4_SIGNATURE_SIZE];
302
303     /* LANMan password is case insensitive */
304     BZERO(UcasePassword, sizeof(UcasePassword));
305     for (i = 0; i < secret_len; i++)
306        UcasePassword[i] = (u_char)toupper(secret[i]);
307     DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
308     DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
309     ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
310 }
311 #endif
312
313 void
314 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len)
315     chap_state *cstate;
316     char *rchallenge;
317     int rchallenge_len;
318     char *secret;
319     int secret_len;
320 {
321     MS_ChapResponse     response;
322
323 #if 0
324     CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'", secret_len, secret));
325 #endif
326     BZERO(&response, sizeof(response));
327
328     /* Calculate both always */
329     ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
330
331 #ifdef MSLANMAN
332     ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
333
334     /* prefered method is set by option  */
335     response.UseNT = !ms_lanman;
336 #else
337     response.UseNT = 1;
338 #endif
339
340     BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);
341     cstate->resp_length = MS_CHAP_RESPONSE_LEN;
342 }
343
344 #endif /* CHAPMS */