ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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