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