Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
0:ac1725ba162c

        

Who changed what in which revision?

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