]> git.ozlabs.org Git - ppp.git/blob - pppd/chap_ms.c
9a7002ced4201e572762951ec376371bedcd4c56
[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 #define RCSID   "$Id: chap_ms.c,v 1.16 2002/03/01 14:39:18 dfs Exp $"
35
36 #ifdef CHAPMS
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45 #ifdef HAVE_CRYPT_H
46 #include <crypt.h>
47 #endif
48
49 #include "pppd.h"
50 #include "chap.h"
51 #include "chap_ms.h"
52 #include "md4.h"
53
54 #ifndef USE_CRYPT
55 #include <des.h>
56 #endif
57
58 static const char rcsid[] = RCSID;
59
60
61 static void     ChallengeResponse __P((u_char *, u_char *, u_char *));
62 static void     DesEncrypt __P((u_char *, u_char *, u_char *));
63 static void     MakeKey __P((u_char *, u_char *));
64 static u_char   Get7Bits __P((u_char *, int));
65 static void     ChapMS_NT __P((char *, int, char *, int, MS_ChapResponse *));
66 #ifdef MSLANMAN
67 static void     ChapMS_LANMan __P((char *, int, char *, int, MS_ChapResponse *));
68 #endif
69
70 #ifdef USE_CRYPT
71 static void     Expand __P((u_char *, u_char *));
72 static void     Collapse __P((u_char *, u_char *));
73 #endif
74
75 #ifdef MSLANMAN
76 bool    ms_lanman = 0;          /* Use LanMan password instead of NT */
77                                 /* Has meaning only with MS-CHAP challenges */
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     dbglog("ChallengeResponse - ZPasswordHash %.*B",
93            sizeof(ZPasswordHash), ZPasswordHash);
94 #endif
95
96     DesEncrypt(challenge, ZPasswordHash +  0, response + 0);
97     DesEncrypt(challenge, ZPasswordHash +  7, response + 8);
98     DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
99
100 #if 0
101     dbglog("ChallengeResponse - response %.24B", response);
102 #endif
103 }
104
105
106 #ifdef USE_CRYPT
107 static void
108 DesEncrypt(clear, key, cipher)
109     u_char *clear;      /* IN  8 octets */
110     u_char *key;        /* IN  7 octets */
111     u_char *cipher;     /* OUT 8 octets */
112 {
113     u_char des_key[8];
114     u_char crypt_key[66];
115     u_char des_input[66];
116
117     MakeKey(key, des_key);
118
119     Expand(des_key, crypt_key);
120     setkey(crypt_key);
121
122 #if 0
123     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %.8B", clear));
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: %.8B", cipher));
132 #endif
133 }
134
135 #else /* USE_CRYPT */
136
137 static void
138 DesEncrypt(clear, key, cipher)
139     u_char *clear;      /* IN  8 octets */
140     u_char *key;        /* IN  7 octets */
141     u_char *cipher;     /* OUT 8 octets */
142 {
143     des_cblock          des_key;
144     des_key_schedule    key_schedule;
145
146     MakeKey(key, des_key);
147
148     des_set_key(&des_key, key_schedule);
149
150 #if 0
151     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %.8B", clear));
152 #endif
153
154     des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
155
156 #if 0
157     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %.8B", cipher));
158 #endif
159 }
160
161 #endif /* USE_CRYPT */
162
163
164 static u_char Get7Bits(input, startBit)
165     u_char *input;
166     int startBit;
167 {
168     register unsigned int       word;
169
170     word  = (unsigned)input[startBit / 8] << 8;
171     word |= (unsigned)input[startBit / 8 + 1];
172
173     word >>= 15 - (startBit % 8 + 7);
174
175     return word & 0xFE;
176 }
177
178 #ifdef USE_CRYPT
179
180 /* in == 8-byte string (expanded version of the 56-bit key)
181  * out == 64-byte string where each byte is either 1 or 0
182  * Note that the low-order "bit" is always ignored by by setkey()
183  */
184 static void Expand(in, out)
185     u_char *in;
186     u_char *out;
187 {
188         int j, c;
189         int i;
190
191         for(i = 0; i < 64; in++){
192                 c = *in;
193                 for(j = 7; j >= 0; j--)
194                         *out++ = (c >> j) & 01;
195                 i += 8;
196         }
197 }
198
199 /* The inverse of Expand
200  */
201 static void Collapse(in, out)
202     u_char *in;
203     u_char *out;
204 {
205         int j;
206         int i;
207         unsigned int c;
208
209         for (i = 0; i < 64; i += 8, out++) {
210             c = 0;
211             for (j = 7; j >= 0; j--, in++)
212                 c |= *in << j;
213             *out = c & 0xff;
214         }
215 }
216 #endif
217
218 static void MakeKey(key, des_key)
219     u_char *key;        /* IN  56 bit DES key missing parity bits */
220     u_char *des_key;    /* OUT 64 bit DES key with parity bits added */
221 {
222     des_key[0] = Get7Bits(key,  0);
223     des_key[1] = Get7Bits(key,  7);
224     des_key[2] = Get7Bits(key, 14);
225     des_key[3] = Get7Bits(key, 21);
226     des_key[4] = Get7Bits(key, 28);
227     des_key[5] = Get7Bits(key, 35);
228     des_key[6] = Get7Bits(key, 42);
229     des_key[7] = Get7Bits(key, 49);
230
231 #ifndef USE_CRYPT
232     des_set_odd_parity((des_cblock *)des_key);
233 #endif
234
235 #if 0
236     CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %.7B", key));
237     CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %.8B", des_key));
238 #endif
239 }
240
241 static void
242 ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, response)
243     char *rchallenge;
244     int rchallenge_len;
245     char *secret;
246     int secret_len;
247     MS_ChapResponse    *response;
248 {
249     int                 i;
250 #ifdef __NetBSD__
251     /* NetBSD uses the libc md4 routines which take bytes instead of bits */
252     int                 mdlen = secret_len * 2;
253 #else
254     int                 mdlen = secret_len * 2 * 8;
255 #endif
256     MD4_CTX             md4Context;
257     u_char              hash[MD4_SIGNATURE_SIZE];
258     u_char              unicodePassword[MAX_NT_PASSWORD * 2];
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, mdlen);
268
269     MD4Final(hash, &md4Context);        /* Tell MD4 we're done */
270
271     ChallengeResponse(rchallenge, hash, response->NTResp);
272 }
273
274 #ifdef MSLANMAN
275 static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
276
277 static void
278 ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, response)
279     char *rchallenge;
280     int rchallenge_len;
281     char *secret;
282     int secret_len;
283     MS_ChapResponse     *response;
284 {
285     int                 i;
286     u_char              UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
287     u_char              PasswordHash[MD4_SIGNATURE_SIZE];
288
289     /* LANMan password is case insensitive */
290     BZERO(UcasePassword, sizeof(UcasePassword));
291     for (i = 0; i < secret_len; i++)
292        UcasePassword[i] = (u_char)toupper(secret[i]);
293     DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
294     DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
295     ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
296 }
297 #endif
298
299 void
300 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len)
301     chap_state *cstate;
302     char *rchallenge;
303     int rchallenge_len;
304     char *secret;
305     int secret_len;
306 {
307     MS_ChapResponse     response;
308
309 #if 0
310     CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'", secret_len, secret));
311 #endif
312     BZERO(&response, sizeof(response));
313
314     /* Calculate both always */
315     ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
316
317 #ifdef MSLANMAN
318     ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
319
320     /* prefered method is set by option  */
321     response.UseNT = !ms_lanman;
322 #else
323     response.UseNT = 1;
324 #endif
325
326     BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);
327     cstate->resp_length = MS_CHAP_RESPONSE_LEN;
328 }
329
330 #endif /* CHAPMS */