Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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