Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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