NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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