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