]> git.ozlabs.org Git - ppp.git/blob - pppd/chap_ms.c
don't set CLOCAL when running connector
[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.4 1997/05/22 06:46:19 paulus Exp $";
36 #endif
37
38 #ifdef CHAPMS
39
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <syslog.h>
44
45 #include "pppd.h"
46 #include "chap.h"
47 #include "chap_ms.h"
48 #include "md4.h"
49
50 #ifndef USE_CRYPT
51 #include <des.h>
52 #endif
53
54 typedef struct {
55     u_char LANManResp[24];
56     u_char NTResp[24];
57     u_char UseNT;               /* If 1, ignore the LANMan response field */
58 } MS_ChapResponse;
59 /* We use MS_CHAP_RESPONSE_LEN, rather than sizeof(MS_ChapResponse),
60    in case this struct gets padded. */
61
62
63 static void     DesEncrypt __P((u_char *, u_char *, u_char *));
64 static void     MakeKey __P((u_char *, u_char *));
65
66 #ifdef USE_CRYPT
67 static void     Expand __P((u_char *, u_char *));
68 static void     Collapse __P((u_char *, u_char *));
69 #endif
70
71 static void
72 ChallengeResponse(challenge, pwHash, response)
73     u_char *challenge;  /* IN   8 octets */
74     u_char *pwHash;     /* IN  16 octets */
75     u_char *response;   /* OUT 24 octets */
76 {
77     char    ZPasswordHash[21];
78
79     BZERO(ZPasswordHash, sizeof(ZPasswordHash));
80     BCOPY(pwHash, ZPasswordHash, 16);
81
82 #if 0
83     log_packet(ZPasswordHash, sizeof(ZPasswordHash), "ChallengeResponse - ZPasswordHash", LOG_DEBUG);
84 #endif
85
86     DesEncrypt(challenge, ZPasswordHash +  0, response + 0);
87     DesEncrypt(challenge, ZPasswordHash +  7, response + 8);
88     DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
89
90 #if 0
91     log_packet(response, 24, "ChallengeResponse - response", LOG_DEBUG);
92 #endif
93 }
94
95
96 #ifdef USE_CRYPT
97 static void
98 DesEncrypt(clear, key, cipher)
99     u_char *clear;      /* IN  8 octets */
100     u_char *key;        /* IN  7 octets */
101     u_char *cipher;     /* OUT 8 octets */
102 {
103     u_char des_key[8];
104     u_char crypt_key[66];
105     u_char des_input[66];
106
107     MakeKey(key, des_key);
108
109     Expand(des_key, crypt_key);
110     setkey(crypt_key);
111
112 #if 0
113     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
114                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
115 #endif
116
117     Expand(clear, des_input);
118     encrypt(des_input, 0);
119     Collapse(des_input, cipher);
120
121 #if 0
122     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
123                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
124 #endif
125 }
126
127 #else /* USE_CRYPT */
128
129 static void
130 DesEncrypt(clear, key, cipher)
131     u_char *clear;      /* IN  8 octets */
132     u_char *key;        /* IN  7 octets */
133     u_char *cipher;     /* OUT 8 octets */
134 {
135     des_cblock          des_key;
136     des_key_schedule    key_schedule;
137
138     MakeKey(key, des_key);
139
140     des_set_key(&des_key, key_schedule);
141
142 #if 0
143     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
144                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
145 #endif
146
147     des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
148
149 #if 0
150     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
151                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
152 #endif
153 }
154
155 #endif /* USE_CRYPT */
156
157
158 static u_char Get7Bits(input, startBit)
159     u_char *input;
160     int startBit;
161 {
162     register unsigned int       word;
163
164     word  = (unsigned)input[startBit / 8] << 8;
165     word |= (unsigned)input[startBit / 8 + 1];
166
167     word >>= 15 - (startBit % 8 + 7);
168
169     return word & 0xFE;
170 }
171
172 #ifdef USE_CRYPT
173
174 /* in == 8-byte string (expanded version of the 56-bit key)
175  * out == 64-byte string where each byte is either 1 or 0
176  * Note that the low-order "bit" is always ignored by by setkey()
177  */
178 static void Expand(in, out)
179     u_char *in;
180     u_char *out;
181 {
182         int j, c;
183         int i;
184
185         for(i = 0; i < 64; in++){
186                 c = *in;
187                 for(j = 7; j >= 0; j--)
188                         *out++ = (c >> j) & 01;
189                 i += 8;
190         }
191 }
192
193 /* The inverse of Expand
194  */
195 static void Collapse(in, out)
196     u_char *in;
197     u_char *out;
198 {
199         int j;
200         int i;
201         unsigned int c;
202
203         for (i = 0; i < 64; i += 8, out++) {
204             c = 0;
205             for (j = 7; j >= 0; j--, in++)
206                 c |= *in << j;
207             *out = c & 0xff;
208         }
209 }
210 #endif
211
212 static void MakeKey(key, des_key)
213     u_char *key;        /* IN  56 bit DES key missing parity bits */
214     u_char *des_key;    /* OUT 64 bit DES key with parity bits added */
215 {
216     des_key[0] = Get7Bits(key,  0);
217     des_key[1] = Get7Bits(key,  7);
218     des_key[2] = Get7Bits(key, 14);
219     des_key[3] = Get7Bits(key, 21);
220     des_key[4] = Get7Bits(key, 28);
221     des_key[5] = Get7Bits(key, 35);
222     des_key[6] = Get7Bits(key, 42);
223     des_key[7] = Get7Bits(key, 49);
224
225 #ifndef USE_CRYPT
226     des_set_odd_parity((des_cblock *)des_key);
227 #endif
228
229 #if 0
230     CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %02X%02X%02X%02X%02X%02X%02X",
231                key[0], key[1], key[2], key[3], key[4], key[5], key[6]));
232     CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X",
233                des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));
234 #endif
235 }
236
237 static void
238 ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, response)
239     char *rchallenge;
240     int rchallenge_len;
241     char *secret;
242     int secret_len;
243     MS_ChapResponse    *response;
244 {
245     int                 i;
246     MDstruct            md4Context;
247     u_char              unicodePassword[MAX_NT_PASSWORD * 2];
248     static int          low_byte_first = -1;
249
250     /* Initialize the Unicode version of the secret (== password). */
251     /* This implicitly supports 8-bit ISO8859/1 characters. */
252     BZERO(unicodePassword, sizeof(unicodePassword));
253     for (i = 0; i < secret_len; i++)
254         unicodePassword[i * 2] = (u_char)secret[i];
255
256     MDbegin(&md4Context);
257     MDupdate(&md4Context, unicodePassword, secret_len * 2 * 8); /* Unicode is 2 bytes/char, *8 for bit count */
258
259     if (low_byte_first == -1)
260         low_byte_first = (htons((unsigned short int)1) != 1);
261     if (low_byte_first == 0)
262         MDreverse(&md4Context);  /*  sfb 961105 */
263
264     MDupdate(&md4Context, NULL, 0);     /* Tell MD4 we're done */
265
266     ChallengeResponse(rchallenge, (char *)md4Context.buffer, response->NTResp);
267 }
268
269 #ifdef MSLANMAN
270 static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
271
272 static ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, response)
273     char *rchallenge;
274     int rchallenge_len;
275     char *secret;
276     int secret_len;
277     MS_ChapResponse     *response;
278 {
279     int                 i;
280     u_char              UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
281     u_char              PasswordHash[16];
282
283     /* LANMan password is case insensitive */
284     BZERO(UcasePassword, sizeof(UcasePassword));
285     for (i = 0; i < secret_len; i++)
286        UcasePassword[i] = (u_char)toupper(secret[i]);
287     DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
288     DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
289     ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
290 }
291 #endif
292
293 void
294 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len)
295     chap_state *cstate;
296     char *rchallenge;
297     int rchallenge_len;
298     char *secret;
299     int secret_len;
300 {
301     MS_ChapResponse     response;
302 #ifdef MSLANMAN
303     extern int ms_lanman;
304 #endif
305
306 #if 0
307     CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'", secret_len, secret));
308 #endif
309     BZERO(&response, sizeof(response));
310
311     /* Calculate both always */
312     ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
313
314 #ifdef MSLANMAN
315     ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
316
317     /* prefered method is set by option  */
318     response.UseNT = !ms_lanman;
319 #else
320     response.UseNT = 1;
321 #endif
322
323     BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);
324     cstate->resp_length = MS_CHAP_RESPONSE_LEN;
325 }
326
327 #endif /* CHAPMS */