Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.5 2005/11/24 23:42:54 swildner 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 /*
83  * Parameters:
84  *      challenge:      IN   8 octets
85  *      pwHash:         IN  16 octets
86  *      response:       OUT 24 octets
87  */
88 static void
89 ChallengeResponse(u_char *challenge, u_char *pwHash, u_char *response)
90 {
91     char    ZPasswordHash[21];
92
93     BZERO(ZPasswordHash, sizeof(ZPasswordHash));
94     BCOPY(pwHash, ZPasswordHash, MD4_SIGNATURE_SIZE);
95
96 #if 0
97     log_packet(ZPasswordHash, sizeof(ZPasswordHash), "ChallengeResponse - ZPasswordHash", LOG_DEBUG);
98 #endif
99
100     DesEncrypt(challenge, ZPasswordHash +  0, response + 0);
101     DesEncrypt(challenge, ZPasswordHash +  7, response + 8);
102     DesEncrypt(challenge, ZPasswordHash + 14, response + 16);
103
104 #if 0
105     log_packet(response, 24, "ChallengeResponse - response", LOG_DEBUG);
106 #endif
107 }
108
109
110 #ifdef USE_CRYPT
111 /*
112  * Parameters:
113  *      clear:          IN  8 octets
114  *      key:            IN  7 octets
115  *      cipher:         OUT 8 octets
116  */
117 static void
118 DesEncrypt(u_char *clear, u_char *key, u_char *cipher)
119 {
120     u_char des_key[8];
121     u_char crypt_key[66];
122     u_char des_input[66];
123
124     MakeKey(key, des_key);
125
126     Expand(des_key, crypt_key);
127     setkey(crypt_key);
128
129 #if 0
130     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
131                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
132 #endif
133
134     Expand(clear, des_input);
135     encrypt(des_input, 0);
136     Collapse(des_input, cipher);
137
138 #if 0
139     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
140                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
141 #endif
142 }
143
144 #else /* USE_CRYPT */
145
146 /*
147  * Parameters:
148  *      clear:          IN  8 octets
149  *      key:            IN  7 octets
150  *      cipher:         OUT 8 octets
151  */
152 static void
153 DesEncrypt(u_char *clear, u_char *key, u_char *cipher)
154 {
155     des_cblock          des_key;
156     des_key_schedule    key_schedule;
157
158     MakeKey(key, des_key);
159
160     des_set_key(&des_key, key_schedule);
161
162 #if 0
163     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X",
164                clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));
165 #endif
166
167     des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
168
169 #if 0
170     CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X",
171                cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));
172 #endif
173 }
174
175 #endif /* USE_CRYPT */
176
177
178 static u_char
179 Get7Bits(u_char *input, int startBit)
180 {
181     unsigned int word;
182
183     word  = (unsigned)input[startBit / 8] << 8;
184     word |= (unsigned)input[startBit / 8 + 1];
185
186     word >>= 15 - (startBit % 8 + 7);
187
188     return word & 0xFE;
189 }
190
191 #ifdef USE_CRYPT
192
193 /* in == 8-byte string (expanded version of the 56-bit key)
194  * out == 64-byte string where each byte is either 1 or 0
195  * Note that the low-order "bit" is always ignored by by setkey()
196  */
197 static void
198 Expand(u_char *in, u_char *out)
199 {
200         int j, c;
201         int i;
202
203         for(i = 0; i < 64; in++){
204                 c = *in;
205                 for(j = 7; j >= 0; j--)
206                         *out++ = (c >> j) & 01;
207                 i += 8;
208         }
209 }
210
211 /* The inverse of Expand
212  */
213 static void
214 Collapse(u_char *in, u_char *out)
215 {
216         int j;
217         int i;
218         unsigned int c;
219
220         for (i = 0; i < 64; i += 8, out++) {
221             c = 0;
222             for (j = 7; j >= 0; j--, in++)
223                 c |= *in << j;
224             *out = c & 0xff;
225         }
226 }
227 #endif
228
229 /*
230  * Parameters:
231  *      IN  56 bit DES key missing parity bits
232  *      OUT 64 bit DES key with parity bits added
233  */
234 static void
235 MakeKey(u_char *key, u_char *des_key)
236 {
237     des_key[0] = Get7Bits(key,  0);
238     des_key[1] = Get7Bits(key,  7);
239     des_key[2] = Get7Bits(key, 14);
240     des_key[3] = Get7Bits(key, 21);
241     des_key[4] = Get7Bits(key, 28);
242     des_key[5] = Get7Bits(key, 35);
243     des_key[6] = Get7Bits(key, 42);
244     des_key[7] = Get7Bits(key, 49);
245
246 #ifndef USE_CRYPT
247     des_set_odd_parity((des_cblock *)des_key);
248 #endif
249
250 #if 0
251     CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %02X%02X%02X%02X%02X%02X%02X",
252                key[0], key[1], key[2], key[3], key[4], key[5], key[6]));
253     CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X",
254                des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));
255 #endif
256 }
257
258 static void
259 ChapMS_NT(char *rchallenge, int rchallenge_len, char *secret, int secret_len,
260           MS_ChapResponse *response)
261 {
262     int                 i;
263     MD4_CTX             md4Context;
264     u_char              hash[MD4_SIGNATURE_SIZE];
265     u_char              unicodePassword[MAX_NT_PASSWORD * 2];
266
267     /* Initialize the Unicode version of the secret (== password). */
268     /* This implicitly supports 8-bit ISO8859/1 characters. */
269     BZERO(unicodePassword, sizeof(unicodePassword));
270     for (i = 0; i < secret_len; i++)
271         unicodePassword[i * 2] = (u_char)secret[i];
272
273     MD4Init(&md4Context);
274     MD4Update(&md4Context, unicodePassword, secret_len * 2);    /* Unicode is 2 bytes/char */
275
276     MD4Final(hash, &md4Context);        /* Tell MD4 we're done */
277
278     ChallengeResponse(rchallenge, 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(char *rchallenge, int rchallenge_len, char *secret,
286               int secret_len, MS_ChapResponse *response)
287 {
288     int                 i;
289     u_char              UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */
290     u_char              PasswordHash[MD4_SIGNATURE_SIZE];
291
292     /* LANMan password is case insensitive */
293     BZERO(UcasePassword, sizeof(UcasePassword));
294     for (i = 0; i < secret_len; i++)
295        UcasePassword[i] = (u_char)toupper(secret[i]);
296     DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );
297     DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );
298     ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);
299 }
300 #endif
301
302 void
303 ChapMS(chap_state *cstate, char *rchallenge, int rchallenge_len, char *secret,
304        int secret_len)
305 {
306     MS_ChapResponse     response;
307 #ifdef MSLANMAN
308     extern int ms_lanman;
309 #endif
310
311 #if 0
312     CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'", secret_len, secret));
313 #endif
314     BZERO(&response, sizeof(response));
315
316     /* Calculate both always */
317     ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);
318
319 #ifdef MSLANMAN
320     ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);
321
322     /* prefered method is set by option  */
323     response.UseNT = !ms_lanman;
324 #else
325     response.UseNT = 1;
326 #endif
327
328     BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);
329     cstate->resp_length = MS_CHAP_RESPONSE_LEN;
330 }
331
332 #endif /* CHAPMS */