Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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