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