Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1 /*** WARNING - THIS HAS NEVER BEEN FINISHED ***/
simon 0:350011bf8be7 2 /*****************************************************************************
simon 0:350011bf8be7 3 * chap.c - Network Challenge Handshake Authentication Protocol program file.
simon 0:350011bf8be7 4 *
simon 0:350011bf8be7 5 * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
simon 0:350011bf8be7 6 * portions Copyright (c) 1997 by Global Election Systems Inc.
simon 0:350011bf8be7 7 *
simon 0:350011bf8be7 8 * The authors hereby grant permission to use, copy, modify, distribute,
simon 0:350011bf8be7 9 * and license this software and its documentation for any purpose, provided
simon 0:350011bf8be7 10 * that existing copyright notices are retained in all copies and that this
simon 0:350011bf8be7 11 * notice and the following disclaimer are included verbatim in any
simon 0:350011bf8be7 12 * distributions. No written agreement, license, or royalty fee is required
simon 0:350011bf8be7 13 * for any of the authorized uses.
simon 0:350011bf8be7 14 *
simon 0:350011bf8be7 15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
simon 0:350011bf8be7 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
simon 0:350011bf8be7 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
simon 0:350011bf8be7 18 * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
simon 0:350011bf8be7 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
simon 0:350011bf8be7 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
simon 0:350011bf8be7 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
simon 0:350011bf8be7 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
simon 0:350011bf8be7 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
simon 0:350011bf8be7 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
simon 0:350011bf8be7 25 *
simon 0:350011bf8be7 26 ******************************************************************************
simon 0:350011bf8be7 27 * REVISION HISTORY
simon 0:350011bf8be7 28 *
simon 0:350011bf8be7 29 * 03-01-01 Marc Boucher <marc@mbsi.ca>
simon 0:350011bf8be7 30 * Ported to lwIP.
simon 0:350011bf8be7 31 * 97-12-04 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
simon 0:350011bf8be7 32 * Original based on BSD chap.c.
simon 0:350011bf8be7 33 *****************************************************************************/
simon 0:350011bf8be7 34 /*
simon 0:350011bf8be7 35 * chap.c - Challenge Handshake Authentication Protocol.
simon 0:350011bf8be7 36 *
simon 0:350011bf8be7 37 * Copyright (c) 1993 The Australian National University.
simon 0:350011bf8be7 38 * All rights reserved.
simon 0:350011bf8be7 39 *
simon 0:350011bf8be7 40 * Redistribution and use in source and binary forms are permitted
simon 0:350011bf8be7 41 * provided that the above copyright notice and this paragraph are
simon 0:350011bf8be7 42 * duplicated in all such forms and that any documentation,
simon 0:350011bf8be7 43 * advertising materials, and other materials related to such
simon 0:350011bf8be7 44 * distribution and use acknowledge that the software was developed
simon 0:350011bf8be7 45 * by the Australian National University. The name of the University
simon 0:350011bf8be7 46 * may not be used to endorse or promote products derived from this
simon 0:350011bf8be7 47 * software without specific prior written permission.
simon 0:350011bf8be7 48 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
simon 0:350011bf8be7 49 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
simon 0:350011bf8be7 50 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
simon 0:350011bf8be7 51 *
simon 0:350011bf8be7 52 * Copyright (c) 1991 Gregory M. Christy.
simon 0:350011bf8be7 53 * All rights reserved.
simon 0:350011bf8be7 54 *
simon 0:350011bf8be7 55 * Redistribution and use in source and binary forms are permitted
simon 0:350011bf8be7 56 * provided that the above copyright notice and this paragraph are
simon 0:350011bf8be7 57 * duplicated in all such forms and that any documentation,
simon 0:350011bf8be7 58 * advertising materials, and other materials related to such
simon 0:350011bf8be7 59 * distribution and use acknowledge that the software was developed
simon 0:350011bf8be7 60 * by Gregory M. Christy. The name of the author may not be used to
simon 0:350011bf8be7 61 * endorse or promote products derived from this software without
simon 0:350011bf8be7 62 * specific prior written permission.
simon 0:350011bf8be7 63 *
simon 0:350011bf8be7 64 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
simon 0:350011bf8be7 65 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
simon 0:350011bf8be7 66 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
simon 0:350011bf8be7 67 */
simon 0:350011bf8be7 68
simon 0:350011bf8be7 69 #include "lwip/opt.h"
simon 0:350011bf8be7 70
simon 0:350011bf8be7 71 #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 72
simon 0:350011bf8be7 73 #if CHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 74
simon 0:350011bf8be7 75 #include "ppp.h"
simon 0:350011bf8be7 76 #include "pppdebug.h"
simon 0:350011bf8be7 77
simon 0:350011bf8be7 78 #include "magic.h"
simon 0:350011bf8be7 79 #include "randm.h"
simon 0:350011bf8be7 80 #include "auth.h"
simon 0:350011bf8be7 81 #include "md5.h"
simon 0:350011bf8be7 82 #include "chap.h"
simon 0:350011bf8be7 83 #include "chpms.h"
simon 0:350011bf8be7 84
simon 0:350011bf8be7 85 #include <string.h>
simon 0:350011bf8be7 86
simon 0:350011bf8be7 87 #if 0 /* UNUSED */
simon 0:350011bf8be7 88 /*
simon 0:350011bf8be7 89 * Command-line options.
simon 0:350011bf8be7 90 */
simon 0:350011bf8be7 91 static option_t chap_option_list[] = {
simon 0:350011bf8be7 92 { "chap-restart", o_int, &chap[0].timeouttime,
simon 0:350011bf8be7 93 "Set timeout for CHAP" },
simon 0:350011bf8be7 94 { "chap-max-challenge", o_int, &chap[0].max_transmits,
simon 0:350011bf8be7 95 "Set max #xmits for challenge" },
simon 0:350011bf8be7 96 { "chap-interval", o_int, &chap[0].chal_interval,
simon 0:350011bf8be7 97 "Set interval for rechallenge" },
simon 0:350011bf8be7 98 #ifdef MSLANMAN
simon 0:350011bf8be7 99 { "ms-lanman", o_bool, &ms_lanman,
simon 0:350011bf8be7 100 "Use LanMan passwd when using MS-CHAP", 1 },
simon 0:350011bf8be7 101 #endif
simon 0:350011bf8be7 102 { NULL }
simon 0:350011bf8be7 103 };
simon 0:350011bf8be7 104 #endif /* UNUSED */
simon 0:350011bf8be7 105
simon 0:350011bf8be7 106 /*
simon 0:350011bf8be7 107 * Protocol entry points.
simon 0:350011bf8be7 108 */
simon 0:350011bf8be7 109 static void ChapInit (int);
simon 0:350011bf8be7 110 static void ChapLowerUp (int);
simon 0:350011bf8be7 111 static void ChapLowerDown (int);
simon 0:350011bf8be7 112 static void ChapInput (int, u_char *, int);
simon 0:350011bf8be7 113 static void ChapProtocolReject (int);
simon 0:350011bf8be7 114 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 115 static int ChapPrintPkt (u_char *, int, void (*) (void *, char *, ...), void *);
simon 0:350011bf8be7 116 #endif
simon 0:350011bf8be7 117
simon 0:350011bf8be7 118 struct protent chap_protent = {
simon 0:350011bf8be7 119 PPP_CHAP,
simon 0:350011bf8be7 120 ChapInit,
simon 0:350011bf8be7 121 ChapInput,
simon 0:350011bf8be7 122 ChapProtocolReject,
simon 0:350011bf8be7 123 ChapLowerUp,
simon 0:350011bf8be7 124 ChapLowerDown,
simon 0:350011bf8be7 125 NULL,
simon 0:350011bf8be7 126 NULL,
simon 0:350011bf8be7 127 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 128 ChapPrintPkt,
simon 0:350011bf8be7 129 NULL,
simon 0:350011bf8be7 130 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 131 1,
simon 0:350011bf8be7 132 "CHAP",
simon 0:350011bf8be7 133 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 134 NULL,
simon 0:350011bf8be7 135 NULL,
simon 0:350011bf8be7 136 NULL
simon 0:350011bf8be7 137 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 138 };
simon 0:350011bf8be7 139
simon 0:350011bf8be7 140 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
simon 0:350011bf8be7 141
simon 0:350011bf8be7 142 static void ChapChallengeTimeout (void *);
simon 0:350011bf8be7 143 static void ChapResponseTimeout (void *);
simon 0:350011bf8be7 144 static void ChapReceiveChallenge (chap_state *, u_char *, u_char, int);
simon 0:350011bf8be7 145 static void ChapRechallenge (void *);
simon 0:350011bf8be7 146 static void ChapReceiveResponse (chap_state *, u_char *, int, int);
simon 0:350011bf8be7 147 static void ChapReceiveSuccess(chap_state *cstate, u_char *inp, u_char id, int len);
simon 0:350011bf8be7 148 static void ChapReceiveFailure(chap_state *cstate, u_char *inp, u_char id, int len);
simon 0:350011bf8be7 149 static void ChapSendStatus (chap_state *, int);
simon 0:350011bf8be7 150 static void ChapSendChallenge (chap_state *);
simon 0:350011bf8be7 151 static void ChapSendResponse (chap_state *);
simon 0:350011bf8be7 152 static void ChapGenChallenge (chap_state *);
simon 0:350011bf8be7 153
simon 0:350011bf8be7 154 /*
simon 0:350011bf8be7 155 * ChapInit - Initialize a CHAP unit.
simon 0:350011bf8be7 156 */
simon 0:350011bf8be7 157 static void
simon 0:350011bf8be7 158 ChapInit(int unit)
simon 0:350011bf8be7 159 {
simon 0:350011bf8be7 160 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 161
simon 0:350011bf8be7 162 BZERO(cstate, sizeof(*cstate));
simon 0:350011bf8be7 163 cstate->unit = unit;
simon 0:350011bf8be7 164 cstate->clientstate = CHAPCS_INITIAL;
simon 0:350011bf8be7 165 cstate->serverstate = CHAPSS_INITIAL;
simon 0:350011bf8be7 166 cstate->timeouttime = CHAP_DEFTIMEOUT;
simon 0:350011bf8be7 167 cstate->max_transmits = CHAP_DEFTRANSMITS;
simon 0:350011bf8be7 168 /* random number generator is initialized in magic_init */
simon 0:350011bf8be7 169 }
simon 0:350011bf8be7 170
simon 0:350011bf8be7 171
simon 0:350011bf8be7 172 /*
simon 0:350011bf8be7 173 * ChapAuthWithPeer - Authenticate us with our peer (start client).
simon 0:350011bf8be7 174 *
simon 0:350011bf8be7 175 */
simon 0:350011bf8be7 176 void
simon 0:350011bf8be7 177 ChapAuthWithPeer(int unit, char *our_name, u_char digest)
simon 0:350011bf8be7 178 {
simon 0:350011bf8be7 179 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 180
simon 0:350011bf8be7 181 cstate->resp_name = our_name;
simon 0:350011bf8be7 182 cstate->resp_type = digest;
simon 0:350011bf8be7 183
simon 0:350011bf8be7 184 if (cstate->clientstate == CHAPCS_INITIAL ||
simon 0:350011bf8be7 185 cstate->clientstate == CHAPCS_PENDING) {
simon 0:350011bf8be7 186 /* lower layer isn't up - wait until later */
simon 0:350011bf8be7 187 cstate->clientstate = CHAPCS_PENDING;
simon 0:350011bf8be7 188 return;
simon 0:350011bf8be7 189 }
simon 0:350011bf8be7 190
simon 0:350011bf8be7 191 /*
simon 0:350011bf8be7 192 * We get here as a result of LCP coming up.
simon 0:350011bf8be7 193 * So even if CHAP was open before, we will
simon 0:350011bf8be7 194 * have to re-authenticate ourselves.
simon 0:350011bf8be7 195 */
simon 0:350011bf8be7 196 cstate->clientstate = CHAPCS_LISTEN;
simon 0:350011bf8be7 197 }
simon 0:350011bf8be7 198
simon 0:350011bf8be7 199
simon 0:350011bf8be7 200 /*
simon 0:350011bf8be7 201 * ChapAuthPeer - Authenticate our peer (start server).
simon 0:350011bf8be7 202 */
simon 0:350011bf8be7 203 void
simon 0:350011bf8be7 204 ChapAuthPeer(int unit, char *our_name, u_char digest)
simon 0:350011bf8be7 205 {
simon 0:350011bf8be7 206 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 207
simon 0:350011bf8be7 208 cstate->chal_name = our_name;
simon 0:350011bf8be7 209 cstate->chal_type = digest;
simon 0:350011bf8be7 210
simon 0:350011bf8be7 211 if (cstate->serverstate == CHAPSS_INITIAL ||
simon 0:350011bf8be7 212 cstate->serverstate == CHAPSS_PENDING) {
simon 0:350011bf8be7 213 /* lower layer isn't up - wait until later */
simon 0:350011bf8be7 214 cstate->serverstate = CHAPSS_PENDING;
simon 0:350011bf8be7 215 return;
simon 0:350011bf8be7 216 }
simon 0:350011bf8be7 217
simon 0:350011bf8be7 218 ChapGenChallenge(cstate);
simon 0:350011bf8be7 219 ChapSendChallenge(cstate); /* crank it up dude! */
simon 0:350011bf8be7 220 cstate->serverstate = CHAPSS_INITIAL_CHAL;
simon 0:350011bf8be7 221 }
simon 0:350011bf8be7 222
simon 0:350011bf8be7 223
simon 0:350011bf8be7 224 /*
simon 0:350011bf8be7 225 * ChapChallengeTimeout - Timeout expired on sending challenge.
simon 0:350011bf8be7 226 */
simon 0:350011bf8be7 227 static void
simon 0:350011bf8be7 228 ChapChallengeTimeout(void *arg)
simon 0:350011bf8be7 229 {
simon 0:350011bf8be7 230 chap_state *cstate = (chap_state *) arg;
simon 0:350011bf8be7 231
simon 0:350011bf8be7 232 /* if we aren't sending challenges, don't worry. then again we */
simon 0:350011bf8be7 233 /* probably shouldn't be here either */
simon 0:350011bf8be7 234 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
simon 0:350011bf8be7 235 cstate->serverstate != CHAPSS_RECHALLENGE) {
simon 0:350011bf8be7 236 return;
simon 0:350011bf8be7 237 }
simon 0:350011bf8be7 238
simon 0:350011bf8be7 239 if (cstate->chal_transmits >= cstate->max_transmits) {
simon 0:350011bf8be7 240 /* give up on peer */
simon 0:350011bf8be7 241 CHAPDEBUG(LOG_ERR, ("Peer failed to respond to CHAP challenge\n"));
simon 0:350011bf8be7 242 cstate->serverstate = CHAPSS_BADAUTH;
simon 0:350011bf8be7 243 auth_peer_fail(cstate->unit, PPP_CHAP);
simon 0:350011bf8be7 244 return;
simon 0:350011bf8be7 245 }
simon 0:350011bf8be7 246
simon 0:350011bf8be7 247 ChapSendChallenge(cstate); /* Re-send challenge */
simon 0:350011bf8be7 248 }
simon 0:350011bf8be7 249
simon 0:350011bf8be7 250
simon 0:350011bf8be7 251 /*
simon 0:350011bf8be7 252 * ChapResponseTimeout - Timeout expired on sending response.
simon 0:350011bf8be7 253 */
simon 0:350011bf8be7 254 static void
simon 0:350011bf8be7 255 ChapResponseTimeout(void *arg)
simon 0:350011bf8be7 256 {
simon 0:350011bf8be7 257 chap_state *cstate = (chap_state *) arg;
simon 0:350011bf8be7 258
simon 0:350011bf8be7 259 /* if we aren't sending a response, don't worry. */
simon 0:350011bf8be7 260 if (cstate->clientstate != CHAPCS_RESPONSE) {
simon 0:350011bf8be7 261 return;
simon 0:350011bf8be7 262 }
simon 0:350011bf8be7 263
simon 0:350011bf8be7 264 ChapSendResponse(cstate); /* re-send response */
simon 0:350011bf8be7 265 }
simon 0:350011bf8be7 266
simon 0:350011bf8be7 267
simon 0:350011bf8be7 268 /*
simon 0:350011bf8be7 269 * ChapRechallenge - Time to challenge the peer again.
simon 0:350011bf8be7 270 */
simon 0:350011bf8be7 271 static void
simon 0:350011bf8be7 272 ChapRechallenge(void *arg)
simon 0:350011bf8be7 273 {
simon 0:350011bf8be7 274 chap_state *cstate = (chap_state *) arg;
simon 0:350011bf8be7 275
simon 0:350011bf8be7 276 /* if we aren't sending a response, don't worry. */
simon 0:350011bf8be7 277 if (cstate->serverstate != CHAPSS_OPEN) {
simon 0:350011bf8be7 278 return;
simon 0:350011bf8be7 279 }
simon 0:350011bf8be7 280
simon 0:350011bf8be7 281 ChapGenChallenge(cstate);
simon 0:350011bf8be7 282 ChapSendChallenge(cstate);
simon 0:350011bf8be7 283 cstate->serverstate = CHAPSS_RECHALLENGE;
simon 0:350011bf8be7 284 }
simon 0:350011bf8be7 285
simon 0:350011bf8be7 286
simon 0:350011bf8be7 287 /*
simon 0:350011bf8be7 288 * ChapLowerUp - The lower layer is up.
simon 0:350011bf8be7 289 *
simon 0:350011bf8be7 290 * Start up if we have pending requests.
simon 0:350011bf8be7 291 */
simon 0:350011bf8be7 292 static void
simon 0:350011bf8be7 293 ChapLowerUp(int unit)
simon 0:350011bf8be7 294 {
simon 0:350011bf8be7 295 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 296
simon 0:350011bf8be7 297 if (cstate->clientstate == CHAPCS_INITIAL) {
simon 0:350011bf8be7 298 cstate->clientstate = CHAPCS_CLOSED;
simon 0:350011bf8be7 299 } else if (cstate->clientstate == CHAPCS_PENDING) {
simon 0:350011bf8be7 300 cstate->clientstate = CHAPCS_LISTEN;
simon 0:350011bf8be7 301 }
simon 0:350011bf8be7 302
simon 0:350011bf8be7 303 if (cstate->serverstate == CHAPSS_INITIAL) {
simon 0:350011bf8be7 304 cstate->serverstate = CHAPSS_CLOSED;
simon 0:350011bf8be7 305 } else if (cstate->serverstate == CHAPSS_PENDING) {
simon 0:350011bf8be7 306 ChapGenChallenge(cstate);
simon 0:350011bf8be7 307 ChapSendChallenge(cstate);
simon 0:350011bf8be7 308 cstate->serverstate = CHAPSS_INITIAL_CHAL;
simon 0:350011bf8be7 309 }
simon 0:350011bf8be7 310 }
simon 0:350011bf8be7 311
simon 0:350011bf8be7 312
simon 0:350011bf8be7 313 /*
simon 0:350011bf8be7 314 * ChapLowerDown - The lower layer is down.
simon 0:350011bf8be7 315 *
simon 0:350011bf8be7 316 * Cancel all timeouts.
simon 0:350011bf8be7 317 */
simon 0:350011bf8be7 318 static void
simon 0:350011bf8be7 319 ChapLowerDown(int unit)
simon 0:350011bf8be7 320 {
simon 0:350011bf8be7 321 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 322
simon 0:350011bf8be7 323 /* Timeout(s) pending? Cancel if so. */
simon 0:350011bf8be7 324 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
simon 0:350011bf8be7 325 cstate->serverstate == CHAPSS_RECHALLENGE) {
simon 0:350011bf8be7 326 UNTIMEOUT(ChapChallengeTimeout, cstate);
simon 0:350011bf8be7 327 } else if (cstate->serverstate == CHAPSS_OPEN
simon 0:350011bf8be7 328 && cstate->chal_interval != 0) {
simon 0:350011bf8be7 329 UNTIMEOUT(ChapRechallenge, cstate);
simon 0:350011bf8be7 330 }
simon 0:350011bf8be7 331 if (cstate->clientstate == CHAPCS_RESPONSE) {
simon 0:350011bf8be7 332 UNTIMEOUT(ChapResponseTimeout, cstate);
simon 0:350011bf8be7 333 }
simon 0:350011bf8be7 334 cstate->clientstate = CHAPCS_INITIAL;
simon 0:350011bf8be7 335 cstate->serverstate = CHAPSS_INITIAL;
simon 0:350011bf8be7 336 }
simon 0:350011bf8be7 337
simon 0:350011bf8be7 338
simon 0:350011bf8be7 339 /*
simon 0:350011bf8be7 340 * ChapProtocolReject - Peer doesn't grok CHAP.
simon 0:350011bf8be7 341 */
simon 0:350011bf8be7 342 static void
simon 0:350011bf8be7 343 ChapProtocolReject(int unit)
simon 0:350011bf8be7 344 {
simon 0:350011bf8be7 345 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 346
simon 0:350011bf8be7 347 if (cstate->serverstate != CHAPSS_INITIAL &&
simon 0:350011bf8be7 348 cstate->serverstate != CHAPSS_CLOSED) {
simon 0:350011bf8be7 349 auth_peer_fail(unit, PPP_CHAP);
simon 0:350011bf8be7 350 }
simon 0:350011bf8be7 351 if (cstate->clientstate != CHAPCS_INITIAL &&
simon 0:350011bf8be7 352 cstate->clientstate != CHAPCS_CLOSED) {
simon 0:350011bf8be7 353 auth_withpeer_fail(unit, PPP_CHAP); /* lwip: just sets the PPP error code on this unit to PPPERR_AUTHFAIL */
simon 0:350011bf8be7 354 }
simon 0:350011bf8be7 355 ChapLowerDown(unit); /* shutdown chap */
simon 0:350011bf8be7 356 }
simon 0:350011bf8be7 357
simon 0:350011bf8be7 358
simon 0:350011bf8be7 359 /*
simon 0:350011bf8be7 360 * ChapInput - Input CHAP packet.
simon 0:350011bf8be7 361 */
simon 0:350011bf8be7 362 static void
simon 0:350011bf8be7 363 ChapInput(int unit, u_char *inpacket, int packet_len)
simon 0:350011bf8be7 364 {
simon 0:350011bf8be7 365 chap_state *cstate = &chap[unit];
simon 0:350011bf8be7 366 u_char *inp;
simon 0:350011bf8be7 367 u_char code, id;
simon 0:350011bf8be7 368 int len;
simon 0:350011bf8be7 369
simon 0:350011bf8be7 370 /*
simon 0:350011bf8be7 371 * Parse header (code, id and length).
simon 0:350011bf8be7 372 * If packet too short, drop it.
simon 0:350011bf8be7 373 */
simon 0:350011bf8be7 374 inp = inpacket;
simon 0:350011bf8be7 375 if (packet_len < CHAP_HEADERLEN) {
simon 0:350011bf8be7 376 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd short header.\n"));
simon 0:350011bf8be7 377 return;
simon 0:350011bf8be7 378 }
simon 0:350011bf8be7 379 GETCHAR(code, inp);
simon 0:350011bf8be7 380 GETCHAR(id, inp);
simon 0:350011bf8be7 381 GETSHORT(len, inp);
simon 0:350011bf8be7 382 if (len < CHAP_HEADERLEN) {
simon 0:350011bf8be7 383 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd illegal length.\n"));
simon 0:350011bf8be7 384 return;
simon 0:350011bf8be7 385 }
simon 0:350011bf8be7 386 if (len > packet_len) {
simon 0:350011bf8be7 387 CHAPDEBUG(LOG_INFO, ("ChapInput: rcvd short packet.\n"));
simon 0:350011bf8be7 388 return;
simon 0:350011bf8be7 389 }
simon 0:350011bf8be7 390 len -= CHAP_HEADERLEN;
simon 0:350011bf8be7 391
simon 0:350011bf8be7 392 /*
simon 0:350011bf8be7 393 * Action depends on code (as in fact it usually does :-).
simon 0:350011bf8be7 394 */
simon 0:350011bf8be7 395 switch (code) {
simon 0:350011bf8be7 396 case CHAP_CHALLENGE:
simon 0:350011bf8be7 397 ChapReceiveChallenge(cstate, inp, id, len);
simon 0:350011bf8be7 398 break;
simon 0:350011bf8be7 399
simon 0:350011bf8be7 400 case CHAP_RESPONSE:
simon 0:350011bf8be7 401 ChapReceiveResponse(cstate, inp, id, len);
simon 0:350011bf8be7 402 break;
simon 0:350011bf8be7 403
simon 0:350011bf8be7 404 case CHAP_FAILURE:
simon 0:350011bf8be7 405 ChapReceiveFailure(cstate, inp, id, len);
simon 0:350011bf8be7 406 break;
simon 0:350011bf8be7 407
simon 0:350011bf8be7 408 case CHAP_SUCCESS:
simon 0:350011bf8be7 409 ChapReceiveSuccess(cstate, inp, id, len);
simon 0:350011bf8be7 410 break;
simon 0:350011bf8be7 411
simon 0:350011bf8be7 412 default: /* Need code reject? */
simon 0:350011bf8be7 413 CHAPDEBUG(LOG_WARNING, ("Unknown CHAP code (%d) received.\n", code));
simon 0:350011bf8be7 414 break;
simon 0:350011bf8be7 415 }
simon 0:350011bf8be7 416 }
simon 0:350011bf8be7 417
simon 0:350011bf8be7 418
simon 0:350011bf8be7 419 /*
simon 0:350011bf8be7 420 * ChapReceiveChallenge - Receive Challenge and send Response.
simon 0:350011bf8be7 421 */
simon 0:350011bf8be7 422 static void
simon 0:350011bf8be7 423 ChapReceiveChallenge(chap_state *cstate, u_char *inp, u_char id, int len)
simon 0:350011bf8be7 424 {
simon 0:350011bf8be7 425 int rchallenge_len;
simon 0:350011bf8be7 426 u_char *rchallenge;
simon 0:350011bf8be7 427 int secret_len;
simon 0:350011bf8be7 428 char secret[MAXSECRETLEN];
simon 0:350011bf8be7 429 char rhostname[256];
simon 0:350011bf8be7 430 MD5_CTX mdContext;
simon 0:350011bf8be7 431 u_char hash[MD5_SIGNATURE_SIZE];
simon 0:350011bf8be7 432
simon 0:350011bf8be7 433 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: Rcvd id %d.\n", id));
simon 0:350011bf8be7 434 if (cstate->clientstate == CHAPCS_CLOSED ||
simon 0:350011bf8be7 435 cstate->clientstate == CHAPCS_PENDING) {
simon 0:350011bf8be7 436 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: in state %d\n",
simon 0:350011bf8be7 437 cstate->clientstate));
simon 0:350011bf8be7 438 return;
simon 0:350011bf8be7 439 }
simon 0:350011bf8be7 440
simon 0:350011bf8be7 441 if (len < 2) {
simon 0:350011bf8be7 442 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: rcvd short packet.\n"));
simon 0:350011bf8be7 443 return;
simon 0:350011bf8be7 444 }
simon 0:350011bf8be7 445
simon 0:350011bf8be7 446 GETCHAR(rchallenge_len, inp);
simon 0:350011bf8be7 447 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
simon 0:350011bf8be7 448 if (len < 0) {
simon 0:350011bf8be7 449 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: rcvd short packet.\n"));
simon 0:350011bf8be7 450 return;
simon 0:350011bf8be7 451 }
simon 0:350011bf8be7 452 rchallenge = inp;
simon 0:350011bf8be7 453 INCPTR(rchallenge_len, inp);
simon 0:350011bf8be7 454
simon 0:350011bf8be7 455 if (len >= (int)sizeof(rhostname)) {
simon 0:350011bf8be7 456 len = sizeof(rhostname) - 1;
simon 0:350011bf8be7 457 }
simon 0:350011bf8be7 458 BCOPY(inp, rhostname, len);
simon 0:350011bf8be7 459 rhostname[len] = '\000';
simon 0:350011bf8be7 460
simon 0:350011bf8be7 461 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: received name field '%s'\n",
simon 0:350011bf8be7 462 rhostname));
simon 0:350011bf8be7 463
simon 0:350011bf8be7 464 /* Microsoft doesn't send their name back in the PPP packet */
simon 0:350011bf8be7 465 if (ppp_settings.remote_name[0] != 0 && (ppp_settings.explicit_remote || rhostname[0] == 0)) {
simon 0:350011bf8be7 466 strncpy(rhostname, ppp_settings.remote_name, sizeof(rhostname));
simon 0:350011bf8be7 467 rhostname[sizeof(rhostname) - 1] = 0;
simon 0:350011bf8be7 468 CHAPDEBUG(LOG_INFO, ("ChapReceiveChallenge: using '%s' as remote name\n",
simon 0:350011bf8be7 469 rhostname));
simon 0:350011bf8be7 470 }
simon 0:350011bf8be7 471
simon 0:350011bf8be7 472 /* get secret for authenticating ourselves with the specified host */
simon 0:350011bf8be7 473 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
simon 0:350011bf8be7 474 secret, &secret_len, 0)) {
simon 0:350011bf8be7 475 secret_len = 0; /* assume null secret if can't find one */
simon 0:350011bf8be7 476 CHAPDEBUG(LOG_WARNING, ("No CHAP secret found for authenticating us to %s\n",
simon 0:350011bf8be7 477 rhostname));
simon 0:350011bf8be7 478 }
simon 0:350011bf8be7 479
simon 0:350011bf8be7 480 /* cancel response send timeout if necessary */
simon 0:350011bf8be7 481 if (cstate->clientstate == CHAPCS_RESPONSE) {
simon 0:350011bf8be7 482 UNTIMEOUT(ChapResponseTimeout, cstate);
simon 0:350011bf8be7 483 }
simon 0:350011bf8be7 484
simon 0:350011bf8be7 485 cstate->resp_id = id;
simon 0:350011bf8be7 486 cstate->resp_transmits = 0;
simon 0:350011bf8be7 487
simon 0:350011bf8be7 488 /* generate MD based on negotiated type */
simon 0:350011bf8be7 489 switch (cstate->resp_type) {
simon 0:350011bf8be7 490
simon 0:350011bf8be7 491 case CHAP_DIGEST_MD5:
simon 0:350011bf8be7 492 MD5Init(&mdContext);
simon 0:350011bf8be7 493 MD5Update(&mdContext, &cstate->resp_id, 1);
simon 0:350011bf8be7 494 MD5Update(&mdContext, (u_char*)secret, secret_len);
simon 0:350011bf8be7 495 MD5Update(&mdContext, rchallenge, rchallenge_len);
simon 0:350011bf8be7 496 MD5Final(hash, &mdContext);
simon 0:350011bf8be7 497 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
simon 0:350011bf8be7 498 cstate->resp_length = MD5_SIGNATURE_SIZE;
simon 0:350011bf8be7 499 break;
simon 0:350011bf8be7 500
simon 0:350011bf8be7 501 #if MSCHAP_SUPPORT
simon 0:350011bf8be7 502 case CHAP_MICROSOFT:
simon 0:350011bf8be7 503 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
simon 0:350011bf8be7 504 break;
simon 0:350011bf8be7 505 #endif
simon 0:350011bf8be7 506
simon 0:350011bf8be7 507 default:
simon 0:350011bf8be7 508 CHAPDEBUG(LOG_INFO, ("unknown digest type %d\n", cstate->resp_type));
simon 0:350011bf8be7 509 return;
simon 0:350011bf8be7 510 }
simon 0:350011bf8be7 511
simon 0:350011bf8be7 512 BZERO(secret, sizeof(secret));
simon 0:350011bf8be7 513 ChapSendResponse(cstate);
simon 0:350011bf8be7 514 }
simon 0:350011bf8be7 515
simon 0:350011bf8be7 516
simon 0:350011bf8be7 517 /*
simon 0:350011bf8be7 518 * ChapReceiveResponse - Receive and process response.
simon 0:350011bf8be7 519 */
simon 0:350011bf8be7 520 static void
simon 0:350011bf8be7 521 ChapReceiveResponse(chap_state *cstate, u_char *inp, int id, int len)
simon 0:350011bf8be7 522 {
simon 0:350011bf8be7 523 u_char *remmd, remmd_len;
simon 0:350011bf8be7 524 int secret_len, old_state;
simon 0:350011bf8be7 525 int code;
simon 0:350011bf8be7 526 char rhostname[256];
simon 0:350011bf8be7 527 MD5_CTX mdContext;
simon 0:350011bf8be7 528 char secret[MAXSECRETLEN];
simon 0:350011bf8be7 529 u_char hash[MD5_SIGNATURE_SIZE];
simon 0:350011bf8be7 530
simon 0:350011bf8be7 531 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: Rcvd id %d.\n", id));
simon 0:350011bf8be7 532
simon 0:350011bf8be7 533 if (cstate->serverstate == CHAPSS_CLOSED ||
simon 0:350011bf8be7 534 cstate->serverstate == CHAPSS_PENDING) {
simon 0:350011bf8be7 535 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: in state %d\n",
simon 0:350011bf8be7 536 cstate->serverstate));
simon 0:350011bf8be7 537 return;
simon 0:350011bf8be7 538 }
simon 0:350011bf8be7 539
simon 0:350011bf8be7 540 if (id != cstate->chal_id) {
simon 0:350011bf8be7 541 return; /* doesn't match ID of last challenge */
simon 0:350011bf8be7 542 }
simon 0:350011bf8be7 543
simon 0:350011bf8be7 544 /*
simon 0:350011bf8be7 545 * If we have received a duplicate or bogus Response,
simon 0:350011bf8be7 546 * we have to send the same answer (Success/Failure)
simon 0:350011bf8be7 547 * as we did for the first Response we saw.
simon 0:350011bf8be7 548 */
simon 0:350011bf8be7 549 if (cstate->serverstate == CHAPSS_OPEN) {
simon 0:350011bf8be7 550 ChapSendStatus(cstate, CHAP_SUCCESS);
simon 0:350011bf8be7 551 return;
simon 0:350011bf8be7 552 }
simon 0:350011bf8be7 553 if (cstate->serverstate == CHAPSS_BADAUTH) {
simon 0:350011bf8be7 554 ChapSendStatus(cstate, CHAP_FAILURE);
simon 0:350011bf8be7 555 return;
simon 0:350011bf8be7 556 }
simon 0:350011bf8be7 557
simon 0:350011bf8be7 558 if (len < 2) {
simon 0:350011bf8be7 559 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: rcvd short packet.\n"));
simon 0:350011bf8be7 560 return;
simon 0:350011bf8be7 561 }
simon 0:350011bf8be7 562 GETCHAR(remmd_len, inp); /* get length of MD */
simon 0:350011bf8be7 563 remmd = inp; /* get pointer to MD */
simon 0:350011bf8be7 564 INCPTR(remmd_len, inp);
simon 0:350011bf8be7 565
simon 0:350011bf8be7 566 len -= sizeof (u_char) + remmd_len;
simon 0:350011bf8be7 567 if (len < 0) {
simon 0:350011bf8be7 568 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: rcvd short packet.\n"));
simon 0:350011bf8be7 569 return;
simon 0:350011bf8be7 570 }
simon 0:350011bf8be7 571
simon 0:350011bf8be7 572 UNTIMEOUT(ChapChallengeTimeout, cstate);
simon 0:350011bf8be7 573
simon 0:350011bf8be7 574 if (len >= (int)sizeof(rhostname)) {
simon 0:350011bf8be7 575 len = sizeof(rhostname) - 1;
simon 0:350011bf8be7 576 }
simon 0:350011bf8be7 577 BCOPY(inp, rhostname, len);
simon 0:350011bf8be7 578 rhostname[len] = '\000';
simon 0:350011bf8be7 579
simon 0:350011bf8be7 580 CHAPDEBUG(LOG_INFO, ("ChapReceiveResponse: received name field: %s\n",
simon 0:350011bf8be7 581 rhostname));
simon 0:350011bf8be7 582
simon 0:350011bf8be7 583 /*
simon 0:350011bf8be7 584 * Get secret for authenticating them with us,
simon 0:350011bf8be7 585 * do the hash ourselves, and compare the result.
simon 0:350011bf8be7 586 */
simon 0:350011bf8be7 587 code = CHAP_FAILURE;
simon 0:350011bf8be7 588 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
simon 0:350011bf8be7 589 secret, &secret_len, 1)) {
simon 0:350011bf8be7 590 CHAPDEBUG(LOG_WARNING, ("No CHAP secret found for authenticating %s\n",
simon 0:350011bf8be7 591 rhostname));
simon 0:350011bf8be7 592 } else {
simon 0:350011bf8be7 593 /* generate MD based on negotiated type */
simon 0:350011bf8be7 594 switch (cstate->chal_type) {
simon 0:350011bf8be7 595
simon 0:350011bf8be7 596 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
simon 0:350011bf8be7 597 if (remmd_len != MD5_SIGNATURE_SIZE) {
simon 0:350011bf8be7 598 break; /* it's not even the right length */
simon 0:350011bf8be7 599 }
simon 0:350011bf8be7 600 MD5Init(&mdContext);
simon 0:350011bf8be7 601 MD5Update(&mdContext, &cstate->chal_id, 1);
simon 0:350011bf8be7 602 MD5Update(&mdContext, (u_char*)secret, secret_len);
simon 0:350011bf8be7 603 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
simon 0:350011bf8be7 604 MD5Final(hash, &mdContext);
simon 0:350011bf8be7 605
simon 0:350011bf8be7 606 /* compare local and remote MDs and send the appropriate status */
simon 0:350011bf8be7 607 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0) {
simon 0:350011bf8be7 608 code = CHAP_SUCCESS; /* they are the same! */
simon 0:350011bf8be7 609 }
simon 0:350011bf8be7 610 break;
simon 0:350011bf8be7 611
simon 0:350011bf8be7 612 default:
simon 0:350011bf8be7 613 CHAPDEBUG(LOG_INFO, ("unknown digest type %d\n", cstate->chal_type));
simon 0:350011bf8be7 614 }
simon 0:350011bf8be7 615 }
simon 0:350011bf8be7 616
simon 0:350011bf8be7 617 BZERO(secret, sizeof(secret));
simon 0:350011bf8be7 618 ChapSendStatus(cstate, code);
simon 0:350011bf8be7 619
simon 0:350011bf8be7 620 if (code == CHAP_SUCCESS) {
simon 0:350011bf8be7 621 old_state = cstate->serverstate;
simon 0:350011bf8be7 622 cstate->serverstate = CHAPSS_OPEN;
simon 0:350011bf8be7 623 if (old_state == CHAPSS_INITIAL_CHAL) {
simon 0:350011bf8be7 624 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
simon 0:350011bf8be7 625 }
simon 0:350011bf8be7 626 if (cstate->chal_interval != 0) {
simon 0:350011bf8be7 627 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
simon 0:350011bf8be7 628 }
simon 0:350011bf8be7 629 } else {
simon 0:350011bf8be7 630 CHAPDEBUG(LOG_ERR, ("CHAP peer authentication failed\n"));
simon 0:350011bf8be7 631 cstate->serverstate = CHAPSS_BADAUTH;
simon 0:350011bf8be7 632 auth_peer_fail(cstate->unit, PPP_CHAP);
simon 0:350011bf8be7 633 }
simon 0:350011bf8be7 634 }
simon 0:350011bf8be7 635
simon 0:350011bf8be7 636 /*
simon 0:350011bf8be7 637 * ChapReceiveSuccess - Receive Success
simon 0:350011bf8be7 638 */
simon 0:350011bf8be7 639 static void
simon 0:350011bf8be7 640 ChapReceiveSuccess(chap_state *cstate, u_char *inp, u_char id, int len)
simon 0:350011bf8be7 641 {
simon 0:350011bf8be7 642 LWIP_UNUSED_ARG(id);
simon 0:350011bf8be7 643 LWIP_UNUSED_ARG(inp);
simon 0:350011bf8be7 644
simon 0:350011bf8be7 645 CHAPDEBUG(LOG_INFO, ("ChapReceiveSuccess: Rcvd id %d.\n", id));
simon 0:350011bf8be7 646
simon 0:350011bf8be7 647 if (cstate->clientstate == CHAPCS_OPEN) {
simon 0:350011bf8be7 648 /* presumably an answer to a duplicate response */
simon 0:350011bf8be7 649 return;
simon 0:350011bf8be7 650 }
simon 0:350011bf8be7 651
simon 0:350011bf8be7 652 if (cstate->clientstate != CHAPCS_RESPONSE) {
simon 0:350011bf8be7 653 /* don't know what this is */
simon 0:350011bf8be7 654 CHAPDEBUG(LOG_INFO, ("ChapReceiveSuccess: in state %d\n",
simon 0:350011bf8be7 655 cstate->clientstate));
simon 0:350011bf8be7 656 return;
simon 0:350011bf8be7 657 }
simon 0:350011bf8be7 658
simon 0:350011bf8be7 659 UNTIMEOUT(ChapResponseTimeout, cstate);
simon 0:350011bf8be7 660
simon 0:350011bf8be7 661 /*
simon 0:350011bf8be7 662 * Print message.
simon 0:350011bf8be7 663 */
simon 0:350011bf8be7 664 if (len > 0) {
simon 0:350011bf8be7 665 PRINTMSG(inp, len);
simon 0:350011bf8be7 666 }
simon 0:350011bf8be7 667
simon 0:350011bf8be7 668 cstate->clientstate = CHAPCS_OPEN;
simon 0:350011bf8be7 669
simon 0:350011bf8be7 670 auth_withpeer_success(cstate->unit, PPP_CHAP);
simon 0:350011bf8be7 671 }
simon 0:350011bf8be7 672
simon 0:350011bf8be7 673
simon 0:350011bf8be7 674 /*
simon 0:350011bf8be7 675 * ChapReceiveFailure - Receive failure.
simon 0:350011bf8be7 676 */
simon 0:350011bf8be7 677 static void
simon 0:350011bf8be7 678 ChapReceiveFailure(chap_state *cstate, u_char *inp, u_char id, int len)
simon 0:350011bf8be7 679 {
simon 0:350011bf8be7 680 LWIP_UNUSED_ARG(id);
simon 0:350011bf8be7 681 LWIP_UNUSED_ARG(inp);
simon 0:350011bf8be7 682
simon 0:350011bf8be7 683 CHAPDEBUG(LOG_INFO, ("ChapReceiveFailure: Rcvd id %d.\n", id));
simon 0:350011bf8be7 684
simon 0:350011bf8be7 685 if (cstate->clientstate != CHAPCS_RESPONSE) {
simon 0:350011bf8be7 686 /* don't know what this is */
simon 0:350011bf8be7 687 CHAPDEBUG(LOG_INFO, ("ChapReceiveFailure: in state %d\n",
simon 0:350011bf8be7 688 cstate->clientstate));
simon 0:350011bf8be7 689 return;
simon 0:350011bf8be7 690 }
simon 0:350011bf8be7 691
simon 0:350011bf8be7 692 UNTIMEOUT(ChapResponseTimeout, cstate);
simon 0:350011bf8be7 693
simon 0:350011bf8be7 694 /*
simon 0:350011bf8be7 695 * Print message.
simon 0:350011bf8be7 696 */
simon 0:350011bf8be7 697 if (len > 0) {
simon 0:350011bf8be7 698 PRINTMSG(inp, len);
simon 0:350011bf8be7 699 }
simon 0:350011bf8be7 700
simon 0:350011bf8be7 701 CHAPDEBUG(LOG_ERR, ("CHAP authentication failed\n"));
simon 0:350011bf8be7 702 auth_withpeer_fail(cstate->unit, PPP_CHAP); /* lwip: just sets the PPP error code on this unit to PPPERR_AUTHFAIL */
simon 0:350011bf8be7 703 }
simon 0:350011bf8be7 704
simon 0:350011bf8be7 705
simon 0:350011bf8be7 706 /*
simon 0:350011bf8be7 707 * ChapSendChallenge - Send an Authenticate challenge.
simon 0:350011bf8be7 708 */
simon 0:350011bf8be7 709 static void
simon 0:350011bf8be7 710 ChapSendChallenge(chap_state *cstate)
simon 0:350011bf8be7 711 {
simon 0:350011bf8be7 712 u_char *outp;
simon 0:350011bf8be7 713 int chal_len, name_len;
simon 0:350011bf8be7 714 int outlen;
simon 0:350011bf8be7 715
simon 0:350011bf8be7 716 chal_len = cstate->chal_len;
simon 0:350011bf8be7 717 name_len = (int)strlen(cstate->chal_name);
simon 0:350011bf8be7 718 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
simon 0:350011bf8be7 719 outp = outpacket_buf[cstate->unit];
simon 0:350011bf8be7 720
simon 0:350011bf8be7 721 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
simon 0:350011bf8be7 722
simon 0:350011bf8be7 723 PUTCHAR(CHAP_CHALLENGE, outp);
simon 0:350011bf8be7 724 PUTCHAR(cstate->chal_id, outp);
simon 0:350011bf8be7 725 PUTSHORT(outlen, outp);
simon 0:350011bf8be7 726
simon 0:350011bf8be7 727 PUTCHAR(chal_len, outp); /* put length of challenge */
simon 0:350011bf8be7 728 BCOPY(cstate->challenge, outp, chal_len);
simon 0:350011bf8be7 729 INCPTR(chal_len, outp);
simon 0:350011bf8be7 730
simon 0:350011bf8be7 731 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
simon 0:350011bf8be7 732
simon 0:350011bf8be7 733 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
simon 0:350011bf8be7 734
simon 0:350011bf8be7 735 CHAPDEBUG(LOG_INFO, ("ChapSendChallenge: Sent id %d.\n", cstate->chal_id));
simon 0:350011bf8be7 736
simon 0:350011bf8be7 737 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
simon 0:350011bf8be7 738 ++cstate->chal_transmits;
simon 0:350011bf8be7 739 }
simon 0:350011bf8be7 740
simon 0:350011bf8be7 741
simon 0:350011bf8be7 742 /*
simon 0:350011bf8be7 743 * ChapSendStatus - Send a status response (ack or nak).
simon 0:350011bf8be7 744 */
simon 0:350011bf8be7 745 static void
simon 0:350011bf8be7 746 ChapSendStatus(chap_state *cstate, int code)
simon 0:350011bf8be7 747 {
simon 0:350011bf8be7 748 u_char *outp;
simon 0:350011bf8be7 749 int outlen, msglen;
simon 0:350011bf8be7 750 char msg[256]; /* @todo: this can be a char*, no strcpy needed */
simon 0:350011bf8be7 751
simon 0:350011bf8be7 752 if (code == CHAP_SUCCESS) {
simon 0:350011bf8be7 753 strcpy(msg, "Welcome!");
simon 0:350011bf8be7 754 } else {
simon 0:350011bf8be7 755 strcpy(msg, "I don't like you. Go 'way.");
simon 0:350011bf8be7 756 }
simon 0:350011bf8be7 757 msglen = (int)strlen(msg);
simon 0:350011bf8be7 758
simon 0:350011bf8be7 759 outlen = CHAP_HEADERLEN + msglen;
simon 0:350011bf8be7 760 outp = outpacket_buf[cstate->unit];
simon 0:350011bf8be7 761
simon 0:350011bf8be7 762 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
simon 0:350011bf8be7 763
simon 0:350011bf8be7 764 PUTCHAR(code, outp);
simon 0:350011bf8be7 765 PUTCHAR(cstate->chal_id, outp);
simon 0:350011bf8be7 766 PUTSHORT(outlen, outp);
simon 0:350011bf8be7 767 BCOPY(msg, outp, msglen);
simon 0:350011bf8be7 768 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
simon 0:350011bf8be7 769
simon 0:350011bf8be7 770 CHAPDEBUG(LOG_INFO, ("ChapSendStatus: Sent code %d, id %d.\n", code,
simon 0:350011bf8be7 771 cstate->chal_id));
simon 0:350011bf8be7 772 }
simon 0:350011bf8be7 773
simon 0:350011bf8be7 774 /*
simon 0:350011bf8be7 775 * ChapGenChallenge is used to generate a pseudo-random challenge string of
simon 0:350011bf8be7 776 * a pseudo-random length between min_len and max_len. The challenge
simon 0:350011bf8be7 777 * string and its length are stored in *cstate, and various other fields of
simon 0:350011bf8be7 778 * *cstate are initialized.
simon 0:350011bf8be7 779 */
simon 0:350011bf8be7 780
simon 0:350011bf8be7 781 static void
simon 0:350011bf8be7 782 ChapGenChallenge(chap_state *cstate)
simon 0:350011bf8be7 783 {
simon 0:350011bf8be7 784 int chal_len;
simon 0:350011bf8be7 785 u_char *ptr = cstate->challenge;
simon 0:350011bf8be7 786 int i;
simon 0:350011bf8be7 787
simon 0:350011bf8be7 788 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
simon 0:350011bf8be7 789 MAX_CHALLENGE_LENGTH */
simon 0:350011bf8be7 790 chal_len = (unsigned)
simon 0:350011bf8be7 791 ((((magic() >> 16) *
simon 0:350011bf8be7 792 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) >> 16)
simon 0:350011bf8be7 793 + MIN_CHALLENGE_LENGTH);
simon 0:350011bf8be7 794 LWIP_ASSERT("chal_len <= 0xff", chal_len <= 0xffff);
simon 0:350011bf8be7 795 cstate->chal_len = (u_char)chal_len;
simon 0:350011bf8be7 796 cstate->chal_id = ++cstate->id;
simon 0:350011bf8be7 797 cstate->chal_transmits = 0;
simon 0:350011bf8be7 798
simon 0:350011bf8be7 799 /* generate a random string */
simon 0:350011bf8be7 800 for (i = 0; i < chal_len; i++ ) {
simon 0:350011bf8be7 801 *ptr++ = (char) (magic() & 0xff);
simon 0:350011bf8be7 802 }
simon 0:350011bf8be7 803 }
simon 0:350011bf8be7 804
simon 0:350011bf8be7 805 /*
simon 0:350011bf8be7 806 * ChapSendResponse - send a response packet with values as specified
simon 0:350011bf8be7 807 * in *cstate.
simon 0:350011bf8be7 808 */
simon 0:350011bf8be7 809 /* ARGSUSED */
simon 0:350011bf8be7 810 static void
simon 0:350011bf8be7 811 ChapSendResponse(chap_state *cstate)
simon 0:350011bf8be7 812 {
simon 0:350011bf8be7 813 u_char *outp;
simon 0:350011bf8be7 814 int outlen, md_len, name_len;
simon 0:350011bf8be7 815
simon 0:350011bf8be7 816 md_len = cstate->resp_length;
simon 0:350011bf8be7 817 name_len = (int)strlen(cstate->resp_name);
simon 0:350011bf8be7 818 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
simon 0:350011bf8be7 819 outp = outpacket_buf[cstate->unit];
simon 0:350011bf8be7 820
simon 0:350011bf8be7 821 MAKEHEADER(outp, PPP_CHAP);
simon 0:350011bf8be7 822
simon 0:350011bf8be7 823 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
simon 0:350011bf8be7 824 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
simon 0:350011bf8be7 825 PUTSHORT(outlen, outp); /* packet length */
simon 0:350011bf8be7 826
simon 0:350011bf8be7 827 PUTCHAR(md_len, outp); /* length of MD */
simon 0:350011bf8be7 828 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
simon 0:350011bf8be7 829 INCPTR(md_len, outp);
simon 0:350011bf8be7 830
simon 0:350011bf8be7 831 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
simon 0:350011bf8be7 832
simon 0:350011bf8be7 833 /* send the packet */
simon 0:350011bf8be7 834 pppWrite(cstate->unit, outpacket_buf[cstate->unit], outlen + PPP_HDRLEN);
simon 0:350011bf8be7 835
simon 0:350011bf8be7 836 cstate->clientstate = CHAPCS_RESPONSE;
simon 0:350011bf8be7 837 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
simon 0:350011bf8be7 838 ++cstate->resp_transmits;
simon 0:350011bf8be7 839 }
simon 0:350011bf8be7 840
simon 0:350011bf8be7 841 #if PPP_ADDITIONAL_CALLBACKS
simon 0:350011bf8be7 842 static char *ChapCodenames[] = {
simon 0:350011bf8be7 843 "Challenge", "Response", "Success", "Failure"
simon 0:350011bf8be7 844 };
simon 0:350011bf8be7 845 /*
simon 0:350011bf8be7 846 * ChapPrintPkt - print the contents of a CHAP packet.
simon 0:350011bf8be7 847 */
simon 0:350011bf8be7 848 static int
simon 0:350011bf8be7 849 ChapPrintPkt( u_char *p, int plen, void (*printer) (void *, char *, ...), void *arg)
simon 0:350011bf8be7 850 {
simon 0:350011bf8be7 851 int code, id, len;
simon 0:350011bf8be7 852 int clen, nlen;
simon 0:350011bf8be7 853 u_char x;
simon 0:350011bf8be7 854
simon 0:350011bf8be7 855 if (plen < CHAP_HEADERLEN) {
simon 0:350011bf8be7 856 return 0;
simon 0:350011bf8be7 857 }
simon 0:350011bf8be7 858 GETCHAR(code, p);
simon 0:350011bf8be7 859 GETCHAR(id, p);
simon 0:350011bf8be7 860 GETSHORT(len, p);
simon 0:350011bf8be7 861 if (len < CHAP_HEADERLEN || len > plen) {
simon 0:350011bf8be7 862 return 0;
simon 0:350011bf8be7 863 }
simon 0:350011bf8be7 864
simon 0:350011bf8be7 865 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *)) {
simon 0:350011bf8be7 866 printer(arg, " %s", ChapCodenames[code-1]);
simon 0:350011bf8be7 867 } else {
simon 0:350011bf8be7 868 printer(arg, " code=0x%x", code);
simon 0:350011bf8be7 869 }
simon 0:350011bf8be7 870 printer(arg, " id=0x%x", id);
simon 0:350011bf8be7 871 len -= CHAP_HEADERLEN;
simon 0:350011bf8be7 872 switch (code) {
simon 0:350011bf8be7 873 case CHAP_CHALLENGE:
simon 0:350011bf8be7 874 case CHAP_RESPONSE:
simon 0:350011bf8be7 875 if (len < 1) {
simon 0:350011bf8be7 876 break;
simon 0:350011bf8be7 877 }
simon 0:350011bf8be7 878 clen = p[0];
simon 0:350011bf8be7 879 if (len < clen + 1) {
simon 0:350011bf8be7 880 break;
simon 0:350011bf8be7 881 }
simon 0:350011bf8be7 882 ++p;
simon 0:350011bf8be7 883 nlen = len - clen - 1;
simon 0:350011bf8be7 884 printer(arg, " <");
simon 0:350011bf8be7 885 for (; clen > 0; --clen) {
simon 0:350011bf8be7 886 GETCHAR(x, p);
simon 0:350011bf8be7 887 printer(arg, "%.2x", x);
simon 0:350011bf8be7 888 }
simon 0:350011bf8be7 889 printer(arg, ">, name = %.*Z", nlen, p);
simon 0:350011bf8be7 890 break;
simon 0:350011bf8be7 891 case CHAP_FAILURE:
simon 0:350011bf8be7 892 case CHAP_SUCCESS:
simon 0:350011bf8be7 893 printer(arg, " %.*Z", len, p);
simon 0:350011bf8be7 894 break;
simon 0:350011bf8be7 895 default:
simon 0:350011bf8be7 896 for (clen = len; clen > 0; --clen) {
simon 0:350011bf8be7 897 GETCHAR(x, p);
simon 0:350011bf8be7 898 printer(arg, " %.2x", x);
simon 0:350011bf8be7 899 }
simon 0:350011bf8be7 900 }
simon 0:350011bf8be7 901
simon 0:350011bf8be7 902 return len + CHAP_HEADERLEN;
simon 0:350011bf8be7 903 }
simon 0:350011bf8be7 904 #endif /* PPP_ADDITIONAL_CALLBACKS */
simon 0:350011bf8be7 905
simon 0:350011bf8be7 906 #endif /* CHAP_SUPPORT */
simon 0:350011bf8be7 907
simon 0:350011bf8be7 908 #endif /* PPP_SUPPORT */