Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Sat Feb 25 03:28:05 2012 +0000
Revision:
1:c7958aa34fa1
Parent:
0:616601bde9fb
Use published libraries where possible.

Who changed what in which revision?

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