This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Wed Feb 09 13:57:49 2011 +0000
Revision:
0:f30e2135b0db
V0.0.0.1

Who changed what in which revision?

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