2 * chap.c - Crytographic Handshake Authentication Protocol.
4 * Copyright (c) 1991 Gregory M. Christy.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Gregory M. Christy. The name of the author may not be used to
13 * endorse or promote products derived from this software without
14 * specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: chap.c,v 1.9 1995/12/18 03:46:19 paulus Exp $";
31 #include <sys/types.h>
39 struct protent chap_protent = {
40 PPP_CHAP, ChapInit, ChapInput, ChapProtocolReject,
41 ChapLowerUp, ChapLowerDown, NULL, NULL,
42 ChapPrintPkt, NULL, 1, "CHAP"
45 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
47 static void ChapChallengeTimeout __P((caddr_t));
48 static void ChapResponseTimeout __P((caddr_t));
49 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
50 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
51 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
52 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
53 static void ChapSendStatus __P((chap_state *, int));
54 static void ChapSendChallenge __P((chap_state *));
55 static void ChapSendResponse __P((chap_state *));
56 static void ChapGenChallenge __P((chap_state *));
58 extern double drand48 __P((void));
59 extern void srand48 __P((long));
62 * ChapInit - Initialize a CHAP unit.
68 chap_state *cstate = &chap[unit];
70 BZERO(cstate, sizeof(*cstate));
72 cstate->clientstate = CHAPCS_INITIAL;
73 cstate->serverstate = CHAPSS_INITIAL;
74 cstate->timeouttime = CHAP_DEFTIMEOUT;
75 cstate->max_transmits = CHAP_DEFTRANSMITS;
76 /* random number generator is initialized in magic_init */
81 * ChapAuthWithPeer - Authenticate us with our peer (start client).
85 ChapAuthWithPeer(unit, our_name, digest)
90 chap_state *cstate = &chap[unit];
92 cstate->resp_name = our_name;
93 cstate->resp_type = digest;
95 if (cstate->clientstate == CHAPCS_INITIAL ||
96 cstate->clientstate == CHAPCS_PENDING) {
97 /* lower layer isn't up - wait until later */
98 cstate->clientstate = CHAPCS_PENDING;
103 * We get here as a result of LCP coming up.
104 * So even if CHAP was open before, we will
105 * have to re-authenticate ourselves.
107 cstate->clientstate = CHAPCS_LISTEN;
112 * ChapAuthPeer - Authenticate our peer (start server).
115 ChapAuthPeer(unit, our_name, digest)
120 chap_state *cstate = &chap[unit];
122 cstate->chal_name = our_name;
123 cstate->chal_type = digest;
125 if (cstate->serverstate == CHAPSS_INITIAL ||
126 cstate->serverstate == CHAPSS_PENDING) {
127 /* lower layer isn't up - wait until later */
128 cstate->serverstate = CHAPSS_PENDING;
132 ChapGenChallenge(cstate);
133 ChapSendChallenge(cstate); /* crank it up dude! */
134 cstate->serverstate = CHAPSS_INITIAL_CHAL;
139 * ChapChallengeTimeout - Timeout expired on sending challenge.
142 ChapChallengeTimeout(arg)
145 chap_state *cstate = (chap_state *) arg;
147 /* if we aren't sending challenges, don't worry. then again we */
148 /* probably shouldn't be here either */
149 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
150 cstate->serverstate != CHAPSS_RECHALLENGE)
153 if (cstate->chal_transmits >= cstate->max_transmits) {
154 /* give up on peer */
155 syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
156 cstate->serverstate = CHAPSS_BADAUTH;
157 auth_peer_fail(cstate->unit, PPP_CHAP);
161 ChapSendChallenge(cstate); /* Re-send challenge */
166 * ChapResponseTimeout - Timeout expired on sending response.
169 ChapResponseTimeout(arg)
172 chap_state *cstate = (chap_state *) arg;
174 /* if we aren't sending a response, don't worry. */
175 if (cstate->clientstate != CHAPCS_RESPONSE)
178 ChapSendResponse(cstate); /* re-send response */
183 * ChapRechallenge - Time to challenge the peer again.
189 chap_state *cstate = (chap_state *) arg;
191 /* if we aren't sending a response, don't worry. */
192 if (cstate->serverstate != CHAPSS_OPEN)
195 ChapGenChallenge(cstate);
196 ChapSendChallenge(cstate);
197 cstate->serverstate = CHAPSS_RECHALLENGE;
202 * ChapLowerUp - The lower layer is up.
204 * Start up if we have pending requests.
210 chap_state *cstate = &chap[unit];
212 if (cstate->clientstate == CHAPCS_INITIAL)
213 cstate->clientstate = CHAPCS_CLOSED;
214 else if (cstate->clientstate == CHAPCS_PENDING)
215 cstate->clientstate = CHAPCS_LISTEN;
217 if (cstate->serverstate == CHAPSS_INITIAL)
218 cstate->serverstate = CHAPSS_CLOSED;
219 else if (cstate->serverstate == CHAPSS_PENDING) {
220 ChapGenChallenge(cstate);
221 ChapSendChallenge(cstate);
222 cstate->serverstate = CHAPSS_INITIAL_CHAL;
228 * ChapLowerDown - The lower layer is down.
230 * Cancel all timeouts.
236 chap_state *cstate = &chap[unit];
238 /* Timeout(s) pending? Cancel if so. */
239 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
240 cstate->serverstate == CHAPSS_RECHALLENGE)
241 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
242 else if (cstate->serverstate == CHAPSS_OPEN
243 && cstate->chal_interval != 0)
244 UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
245 if (cstate->clientstate == CHAPCS_RESPONSE)
246 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
248 cstate->clientstate = CHAPCS_INITIAL;
249 cstate->serverstate = CHAPSS_INITIAL;
254 * ChapProtocolReject - Peer doesn't grok CHAP.
257 ChapProtocolReject(unit)
260 chap_state *cstate = &chap[unit];
262 if (cstate->serverstate != CHAPSS_INITIAL &&
263 cstate->serverstate != CHAPSS_CLOSED)
264 auth_peer_fail(unit, PPP_CHAP);
265 if (cstate->clientstate != CHAPCS_INITIAL &&
266 cstate->clientstate != CHAPCS_CLOSED)
267 auth_withpeer_fail(unit, PPP_CHAP);
268 ChapLowerDown(unit); /* shutdown chap */
273 * ChapInput - Input CHAP packet.
276 ChapInput(unit, inpacket, packet_len)
281 chap_state *cstate = &chap[unit];
287 * Parse header (code, id and length).
288 * If packet too short, drop it.
291 if (packet_len < CHAP_HEADERLEN) {
292 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
298 if (len < CHAP_HEADERLEN) {
299 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
302 if (len > packet_len) {
303 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
306 len -= CHAP_HEADERLEN;
309 * Action depends on code (as in fact it usually does :-).
313 ChapReceiveChallenge(cstate, inp, id, len);
317 ChapReceiveResponse(cstate, inp, id, len);
321 ChapReceiveFailure(cstate, inp, id, len);
325 ChapReceiveSuccess(cstate, inp, id, len);
328 default: /* Need code reject? */
329 syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
336 * ChapReceiveChallenge - Receive Challenge and send Response.
339 ChapReceiveChallenge(cstate, inp, id, len)
348 char secret[MAXSECRETLEN];
352 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
353 if (cstate->clientstate == CHAPCS_CLOSED ||
354 cstate->clientstate == CHAPCS_PENDING) {
355 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
356 cstate->clientstate));
361 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
365 GETCHAR(rchallenge_len, inp);
366 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
368 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
372 INCPTR(rchallenge_len, inp);
374 if (len >= sizeof(rhostname))
375 len = sizeof(rhostname) - 1;
376 BCOPY(inp, rhostname, len);
377 rhostname[len] = '\000';
379 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field: %s",
382 /* get secret for authenticating ourselves with the specified host */
383 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
384 secret, &secret_len, 0)) {
385 secret_len = 0; /* assume null secret if can't find one */
386 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
390 /* cancel response send timeout if necessary */
391 if (cstate->clientstate == CHAPCS_RESPONSE)
392 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
394 cstate->resp_id = id;
395 cstate->resp_transmits = 0;
397 /* generate MD based on negotiated type */
398 switch (cstate->resp_type) {
400 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
402 MD5Update(&mdContext, &cstate->resp_id, 1);
403 MD5Update(&mdContext, secret, secret_len);
404 MD5Update(&mdContext, rchallenge, rchallenge_len);
405 MD5Final(&mdContext);
406 BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
407 cstate->resp_length = MD5_SIGNATURE_SIZE;
411 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
415 ChapSendResponse(cstate);
420 * ChapReceiveResponse - Receive and process response.
423 ChapReceiveResponse(cstate, inp, id, len)
429 u_char *remmd, remmd_len;
430 int secret_len, old_state;
436 char secret[MAXSECRETLEN];
438 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
440 if (cstate->serverstate == CHAPSS_CLOSED ||
441 cstate->serverstate == CHAPSS_PENDING) {
442 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
443 cstate->serverstate));
447 if (id != cstate->chal_id)
448 return; /* doesn't match ID of last challenge */
451 * If we have received a duplicate or bogus Response,
452 * we have to send the same answer (Success/Failure)
453 * as we did for the first Response we saw.
455 if (cstate->serverstate == CHAPSS_OPEN) {
456 ChapSendStatus(cstate, CHAP_SUCCESS);
459 if (cstate->serverstate == CHAPSS_BADAUTH) {
460 ChapSendStatus(cstate, CHAP_FAILURE);
465 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
468 GETCHAR(remmd_len, inp); /* get length of MD */
469 remmd = inp; /* get pointer to MD */
470 INCPTR(remmd_len, inp);
472 len -= sizeof (u_char) + remmd_len;
474 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
478 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
480 if (len >= sizeof(rhostname))
481 len = sizeof(rhostname) - 1;
482 BCOPY(inp, rhostname, len);
483 rhostname[len] = '\000';
485 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
489 * Get secret for authenticating them with us,
490 * do the hash ourselves, and compare the result.
493 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
494 secret, &secret_len, 1)) {
495 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
499 /* generate MD based on negotiated type */
500 switch (cstate->chal_type) {
502 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
503 if (remmd_len != MD5_SIGNATURE_SIZE)
504 break; /* it's not even the right length */
506 MD5Update(&mdContext, &cstate->chal_id, 1);
507 MD5Update(&mdContext, secret, secret_len);
508 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
509 MD5Final(&mdContext);
511 /* compare local and remote MDs and send the appropriate status */
512 if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
513 code = CHAP_SUCCESS; /* they are the same! */
517 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
521 ChapSendStatus(cstate, code);
523 if (code == CHAP_SUCCESS) {
524 old_state = cstate->serverstate;
525 cstate->serverstate = CHAPSS_OPEN;
526 if (old_state == CHAPSS_INITIAL_CHAL) {
527 auth_peer_success(cstate->unit, PPP_CHAP);
529 if (cstate->chal_interval != 0)
530 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
533 syslog(LOG_ERR, "CHAP peer authentication failed");
534 cstate->serverstate = CHAPSS_BADAUTH;
535 auth_peer_fail(cstate->unit, PPP_CHAP);
540 * ChapReceiveSuccess - Receive Success
543 ChapReceiveSuccess(cstate, inp, id, len)
550 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
552 if (cstate->clientstate == CHAPCS_OPEN)
553 /* presumably an answer to a duplicate response */
556 if (cstate->clientstate != CHAPCS_RESPONSE) {
557 /* don't know what this is */
558 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
559 cstate->clientstate));
563 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
571 cstate->clientstate = CHAPCS_OPEN;
573 auth_withpeer_success(cstate->unit, PPP_CHAP);
578 * ChapReceiveFailure - Receive failure.
581 ChapReceiveFailure(cstate, inp, id, len)
590 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
592 if (cstate->clientstate != CHAPCS_RESPONSE) {
593 /* don't know what this is */
594 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
595 cstate->clientstate));
599 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
607 syslog(LOG_ERR, "CHAP authentication failed");
608 auth_withpeer_fail(cstate->unit, PPP_CHAP);
613 * ChapSendChallenge - Send an Authenticate challenge.
616 ChapSendChallenge(cstate)
620 int chal_len, name_len;
623 chal_len = cstate->chal_len;
624 name_len = strlen(cstate->chal_name);
625 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
626 outp = outpacket_buf;
628 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
630 PUTCHAR(CHAP_CHALLENGE, outp);
631 PUTCHAR(cstate->chal_id, outp);
632 PUTSHORT(outlen, outp);
634 PUTCHAR(chal_len, outp); /* put length of challenge */
635 BCOPY(cstate->challenge, outp, chal_len);
636 INCPTR(chal_len, outp);
638 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
640 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
642 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
644 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
645 ++cstate->chal_transmits;
650 * ChapSendStatus - Send a status response (ack or nak).
653 ChapSendStatus(cstate, code)
661 if (code == CHAP_SUCCESS)
662 sprintf(msg, "Welcome to %s.", hostname);
664 sprintf(msg, "I don't like you. Go 'way.");
665 msglen = strlen(msg);
667 outlen = CHAP_HEADERLEN + msglen;
668 outp = outpacket_buf;
670 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
673 PUTCHAR(cstate->chal_id, outp);
674 PUTSHORT(outlen, outp);
675 BCOPY(msg, outp, msglen);
676 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
678 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
683 * ChapGenChallenge is used to generate a pseudo-random challenge string of
684 * a pseudo-random length between min_len and max_len. The challenge
685 * string and its length are stored in *cstate, and various other fields of
686 * *cstate are initialized.
690 ChapGenChallenge(cstate)
694 u_char *ptr = cstate->challenge;
697 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
698 MAX_CHALLENGE_LENGTH */
699 chal_len = (unsigned) ((drand48() *
700 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
701 MIN_CHALLENGE_LENGTH);
702 cstate->chal_len = chal_len;
703 cstate->chal_id = ++cstate->id;
704 cstate->chal_transmits = 0;
706 /* generate a random string */
707 for (i = 0; i < chal_len; i++ )
708 *ptr++ = (char) (drand48() * 0xff);
712 * ChapSendResponse - send a response packet with values as specified
717 ChapSendResponse(cstate)
721 int outlen, md_len, name_len;
723 md_len = cstate->resp_length;
724 name_len = strlen(cstate->resp_name);
725 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
726 outp = outpacket_buf;
728 MAKEHEADER(outp, PPP_CHAP);
730 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
731 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
732 PUTSHORT(outlen, outp); /* packet length */
734 PUTCHAR(md_len, outp); /* length of MD */
735 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
736 INCPTR(md_len, outp);
738 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
740 /* send the packet */
741 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
743 cstate->clientstate = CHAPCS_RESPONSE;
744 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
745 ++cstate->resp_transmits;
749 * ChapPrintPkt - print the contents of a CHAP packet.
751 char *ChapCodenames[] = {
752 "Challenge", "Response", "Success", "Failure"
756 ChapPrintPkt(p, plen, printer, arg)
759 void (*printer) __P((void *, char *, ...));
766 if (plen < CHAP_HEADERLEN)
771 if (len < CHAP_HEADERLEN || len > plen)
774 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
775 printer(arg, " %s", ChapCodenames[code-1]);
777 printer(arg, " code=0x%x", code);
778 printer(arg, " id=0x%x", id);
779 len -= CHAP_HEADERLEN;
789 nlen = len - clen - 1;
791 for (; clen > 0; --clen) {
793 printer(arg, "%.2x", x);
795 printer(arg, ">, name = ");
796 print_string((char *)p, nlen, printer, arg);
801 print_string((char *)p, len, printer, arg);
804 for (clen = len; clen > 0; --clen) {
806 printer(arg, " %.2x", x);
810 return len + CHAP_HEADERLEN;