Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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