]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/ethernet/lwIP_130/src/netif/ppp/chpms.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / Common / ethernet / lwIP_130 / src / netif / ppp / chpms.c
1 /*** WARNING - THIS CODE HAS NOT BEEN FINISHED! ***/\r
2 /*****************************************************************************\r
3 * chpms.c - Network MicroSoft Challenge Handshake Authentication Protocol program file.\r
4 *\r
5 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.\r
6 * Copyright (c) 1997 by Global Election Systems Inc.  All rights reserved.\r
7 *\r
8 * The authors hereby grant permission to use, copy, modify, distribute,\r
9 * and license this software and its documentation for any purpose, provided\r
10 * that existing copyright notices are retained in all copies and that this\r
11 * notice and the following disclaimer are included verbatim in any \r
12 * distributions. No written agreement, license, or royalty fee is required\r
13 * for any of the authorized uses.\r
14 *\r
15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR\r
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
18 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
25 *\r
26 ******************************************************************************\r
27 * REVISION HISTORY\r
28 *\r
29 * 03-01-01 Marc Boucher <marc@mbsi.ca>\r
30 *   Ported to lwIP.\r
31 * 97-12-08 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.\r
32 *   Original based on BSD chap_ms.c.\r
33 *****************************************************************************/\r
34 /*\r
35  * chap_ms.c - Microsoft MS-CHAP compatible implementation.\r
36  *\r
37  * Copyright (c) 1995 Eric Rosenquist, Strata Software Limited.\r
38  * http://www.strataware.com/\r
39  *\r
40  * All rights reserved.\r
41  *\r
42  * Redistribution and use in source and binary forms are permitted\r
43  * provided that the above copyright notice and this paragraph are\r
44  * duplicated in all such forms and that any documentation,\r
45  * advertising materials, and other materials related to such\r
46  * distribution and use acknowledge that the software was developed\r
47  * by Eric Rosenquist.  The name of the author may not be used to\r
48  * endorse or promote products derived from this software without\r
49  * specific prior written permission.\r
50  *\r
51  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR\r
52  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED\r
53  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.\r
54  */\r
55 \r
56 /*\r
57  * Modifications by Lauri Pesonen / lpesonen@clinet.fi, april 1997\r
58  *\r
59  *   Implemented LANManager type password response to MS-CHAP challenges.\r
60  *   Now pppd provides both NT style and LANMan style blocks, and the\r
61  *   prefered is set by option "ms-lanman". Default is to use NT.\r
62  *   The hash text (StdText) was taken from Win95 RASAPI32.DLL.\r
63  *\r
64  *   You should also use DOMAIN\\USERNAME as described in README.MSCHAP80\r
65  */\r
66 \r
67 #define USE_CRYPT\r
68 \r
69 #include "lwip/opt.h"\r
70 \r
71 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */\r
72 \r
73 #if MSCHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */\r
74 \r
75 #include "ppp.h"\r
76 #include "pppdebug.h"\r
77 \r
78 #include "md4.h"\r
79 #ifndef USE_CRYPT\r
80 #include "des.h"\r
81 #endif\r
82 #include "chap.h"\r
83 #include "chpms.h"\r
84 \r
85 \r
86 /*************************/\r
87 /*** LOCAL DEFINITIONS ***/\r
88 /*************************/\r
89 \r
90 \r
91 /************************/\r
92 /*** LOCAL DATA TYPES ***/\r
93 /************************/\r
94 typedef struct {\r
95     u_char LANManResp[24];\r
96     u_char NTResp[24];\r
97     u_char UseNT; /* If 1, ignore the LANMan response field */\r
98 } MS_ChapResponse;\r
99 /* We use MS_CHAP_RESPONSE_LEN, rather than sizeof(MS_ChapResponse),\r
100    in case this struct gets padded. */\r
101 \r
102 \r
103 \r
104 /***********************************/\r
105 /*** LOCAL FUNCTION DECLARATIONS ***/\r
106 /***********************************/\r
107 \r
108 /* XXX Don't know what to do with these. */\r
109 extern void setkey(const char *);\r
110 extern void encrypt(char *, int);\r
111 \r
112 static void DesEncrypt (u_char *, u_char *, u_char *);\r
113 static void MakeKey (u_char *, u_char *);\r
114 \r
115 #ifdef USE_CRYPT\r
116 static void Expand (u_char *, u_char *);\r
117 static void Collapse (u_char *, u_char *);\r
118 #endif\r
119 \r
120 static void ChallengeResponse(\r
121   u_char *challenge, /* IN   8 octets */\r
122   u_char *pwHash,    /* IN  16 octets */\r
123   u_char *response   /* OUT 24 octets */\r
124 );\r
125 static void ChapMS_NT(\r
126   char *rchallenge,\r
127   int rchallenge_len,\r
128   char *secret,\r
129   int secret_len,\r
130   MS_ChapResponse *response\r
131 );\r
132 static u_char Get7Bits(\r
133   u_char *input,\r
134   int startBit\r
135 );\r
136 \r
137 \r
138 /***********************************/\r
139 /*** PUBLIC FUNCTION DEFINITIONS ***/\r
140 /***********************************/\r
141 void\r
142 ChapMS( chap_state *cstate, char *rchallenge, int rchallenge_len, char *secret, int secret_len)\r
143 {\r
144   MS_ChapResponse response;\r
145 #ifdef MSLANMAN\r
146   extern int ms_lanman;\r
147 #endif\r
148 \r
149 #if 0\r
150   CHAPDEBUG((LOG_INFO, "ChapMS: secret is '%.*s'\n", secret_len, secret));\r
151 #endif\r
152   BZERO(&response, sizeof(response));\r
153 \r
154   /* Calculate both always */\r
155   ChapMS_NT(rchallenge, rchallenge_len, secret, secret_len, &response);\r
156 \r
157 #ifdef MSLANMAN\r
158   ChapMS_LANMan(rchallenge, rchallenge_len, secret, secret_len, &response);\r
159 \r
160   /* prefered method is set by option  */\r
161   response.UseNT = !ms_lanman;\r
162 #else\r
163   response.UseNT = 1;\r
164 #endif\r
165 \r
166   BCOPY(&response, cstate->response, MS_CHAP_RESPONSE_LEN);\r
167   cstate->resp_length = MS_CHAP_RESPONSE_LEN;\r
168 }\r
169 \r
170 \r
171 /**********************************/\r
172 /*** LOCAL FUNCTION DEFINITIONS ***/\r
173 /**********************************/\r
174 static void\r
175 ChallengeResponse( u_char *challenge, /* IN   8 octets */\r
176                    u_char *pwHash,    /* IN  16 octets */\r
177                    u_char *response   /* OUT 24 octets */)\r
178 {\r
179   char    ZPasswordHash[21];\r
180 \r
181   BZERO(ZPasswordHash, sizeof(ZPasswordHash));\r
182   BCOPY(pwHash, ZPasswordHash, 16);\r
183 \r
184 #if 0\r
185   log_packet(ZPasswordHash, sizeof(ZPasswordHash), "ChallengeResponse - ZPasswordHash", LOG_DEBUG);\r
186 #endif\r
187 \r
188   DesEncrypt(challenge, ZPasswordHash +  0, response + 0);\r
189   DesEncrypt(challenge, ZPasswordHash +  7, response + 8);\r
190   DesEncrypt(challenge, ZPasswordHash + 14, response + 16);\r
191 \r
192 #if 0\r
193   log_packet(response, 24, "ChallengeResponse - response", LOG_DEBUG);\r
194 #endif\r
195 }\r
196 \r
197 \r
198 #ifdef USE_CRYPT\r
199 static void\r
200 DesEncrypt( u_char *clear, /* IN  8 octets */\r
201             u_char *key,   /* IN  7 octets */\r
202             u_char *cipher /* OUT 8 octets */)\r
203 {\r
204   u_char des_key[8];\r
205   u_char crypt_key[66];\r
206   u_char des_input[66];\r
207 \r
208   MakeKey(key, des_key);\r
209 \r
210   Expand(des_key, crypt_key);\r
211   setkey(crypt_key);\r
212 \r
213 #if 0\r
214   CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X\n",\r
215              clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));\r
216 #endif\r
217 \r
218   Expand(clear, des_input);\r
219   encrypt(des_input, 0);\r
220   Collapse(des_input, cipher);\r
221 \r
222 #if 0\r
223   CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X\n",\r
224              cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));\r
225 #endif\r
226 }\r
227 \r
228 #else /* USE_CRYPT */\r
229 \r
230 static void\r
231 DesEncrypt( u_char *clear, /* IN  8 octets */\r
232             u_char *key,   /* IN  7 octets */\r
233             u_char *cipher /* OUT 8 octets */)\r
234 {\r
235   des_cblock    des_key;\r
236   des_key_schedule  key_schedule;\r
237 \r
238   MakeKey(key, des_key);\r
239 \r
240   des_set_key(&des_key, key_schedule);\r
241 \r
242 #if 0\r
243   CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet input : %02X%02X%02X%02X%02X%02X%02X%02X\n",\r
244              clear[0], clear[1], clear[2], clear[3], clear[4], clear[5], clear[6], clear[7]));\r
245 #endif\r
246 \r
247   des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);\r
248 \r
249 #if 0\r
250   CHAPDEBUG((LOG_INFO, "DesEncrypt: 8 octet output: %02X%02X%02X%02X%02X%02X%02X%02X\n",\r
251              cipher[0], cipher[1], cipher[2], cipher[3], cipher[4], cipher[5], cipher[6], cipher[7]));\r
252 #endif\r
253 }\r
254 \r
255 #endif /* USE_CRYPT */\r
256 \r
257 \r
258 static u_char\r
259 Get7Bits( u_char *input, int startBit)\r
260 {\r
261   register unsigned int  word;\r
262 \r
263   word  = (unsigned)input[startBit / 8] << 8;\r
264   word |= (unsigned)input[startBit / 8 + 1];\r
265 \r
266   word >>= 15 - (startBit % 8 + 7);\r
267 \r
268   return word & 0xFE;\r
269 }\r
270 \r
271 #ifdef USE_CRYPT\r
272 \r
273 /* in == 8-byte string (expanded version of the 56-bit key)\r
274  * out == 64-byte string where each byte is either 1 or 0\r
275  * Note that the low-order "bit" is always ignored by by setkey()\r
276  */\r
277 static void\r
278 Expand(u_char *in, u_char *out)\r
279 {\r
280   int j, c;\r
281   int i;\r
282 \r
283   for(i = 0; i < 64; in++){\r
284     c = *in;\r
285     for(j = 7; j >= 0; j--) {\r
286       *out++ = (c >> j) & 01;\r
287     }\r
288     i += 8;\r
289   }\r
290 }\r
291 \r
292 /* The inverse of Expand\r
293  */\r
294 static void\r
295 Collapse(u_char *in, u_char *out)\r
296 {\r
297   int j;\r
298   int i;\r
299   unsigned int c;\r
300 \r
301   for (i = 0; i < 64; i += 8, out++) {\r
302     c = 0;\r
303     for (j = 7; j >= 0; j--, in++) {\r
304       c |= *in << j;\r
305     }\r
306     *out = c & 0xff;\r
307   }\r
308 }\r
309 #endif\r
310 \r
311 static void\r
312 MakeKey( u_char *key,    /* IN  56 bit DES key missing parity bits */\r
313          u_char *des_key /* OUT 64 bit DES key with parity bits added */)\r
314 {\r
315   des_key[0] = Get7Bits(key,  0);\r
316   des_key[1] = Get7Bits(key,  7);\r
317   des_key[2] = Get7Bits(key, 14);\r
318   des_key[3] = Get7Bits(key, 21);\r
319   des_key[4] = Get7Bits(key, 28);\r
320   des_key[5] = Get7Bits(key, 35);\r
321   des_key[6] = Get7Bits(key, 42);\r
322   des_key[7] = Get7Bits(key, 49);\r
323   \r
324 #ifndef USE_CRYPT\r
325   des_set_odd_parity((des_cblock *)des_key);\r
326 #endif\r
327   \r
328 #if 0\r
329   CHAPDEBUG((LOG_INFO, "MakeKey: 56-bit input : %02X%02X%02X%02X%02X%02X%02X\n",\r
330              key[0], key[1], key[2], key[3], key[4], key[5], key[6]));\r
331   CHAPDEBUG((LOG_INFO, "MakeKey: 64-bit output: %02X%02X%02X%02X%02X%02X%02X%02X\n",\r
332              des_key[0], des_key[1], des_key[2], des_key[3], des_key[4], des_key[5], des_key[6], des_key[7]));\r
333 #endif\r
334 }\r
335 \r
336 static void\r
337 ChapMS_NT( char *rchallenge,\r
338            int rchallenge_len,\r
339            char *secret,\r
340            int secret_len,\r
341            MS_ChapResponse *response)\r
342 {\r
343   int      i;\r
344   MDstruct  md4Context;\r
345   u_char    unicodePassword[MAX_NT_PASSWORD * 2];\r
346   static int  low_byte_first = -1;\r
347 \r
348   /* Initialize the Unicode version of the secret (== password). */\r
349   /* This implicitly supports 8-bit ISO8859/1 characters. */\r
350   BZERO(unicodePassword, sizeof(unicodePassword));\r
351   for (i = 0; i < secret_len; i++) {\r
352     unicodePassword[i * 2] = (u_char)secret[i];\r
353   }\r
354   MDbegin(&md4Context);\r
355   MDupdate(&md4Context, unicodePassword, secret_len * 2 * 8);  /* Unicode is 2 bytes/char, *8 for bit count */\r
356 \r
357   if (low_byte_first == -1) {\r
358     low_byte_first = (htons((unsigned short int)1) != 1);\r
359   }\r
360   if (low_byte_first == 0) {\r
361     MDreverse((u_long *)&md4Context);  /*  sfb 961105 */\r
362   }\r
363 \r
364   MDupdate(&md4Context, NULL, 0);  /* Tell MD4 we're done */\r
365 \r
366   ChallengeResponse(rchallenge, (char *)md4Context.buffer, response->NTResp);\r
367 }\r
368 \r
369 #ifdef MSLANMAN\r
370 static u_char *StdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */\r
371 \r
372 static void\r
373 ChapMS_LANMan( char *rchallenge,\r
374                int rchallenge_len,\r
375                char *secret,\r
376                int secret_len,\r
377                MS_ChapResponse  *response)\r
378 {\r
379   int      i;\r
380   u_char    UcasePassword[MAX_NT_PASSWORD]; /* max is actually 14 */\r
381   u_char    PasswordHash[16];\r
382   \r
383   /* LANMan password is case insensitive */\r
384   BZERO(UcasePassword, sizeof(UcasePassword));\r
385   for (i = 0; i < secret_len; i++) {\r
386     UcasePassword[i] = (u_char)toupper(secret[i]);\r
387   }\r
388   DesEncrypt( StdText, UcasePassword + 0, PasswordHash + 0 );\r
389   DesEncrypt( StdText, UcasePassword + 7, PasswordHash + 8 );\r
390   ChallengeResponse(rchallenge, PasswordHash, response->LANManResp);\r
391 }\r
392 #endif\r
393 \r
394 #endif /* MSCHAP_SUPPORT */\r
395 \r
396 #endif /* PPP_SUPPORT */\r