Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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