2 * chap.c - Challenge Handshake Authentication Protocol.
4 * Copyright (c) 1993 The Australian National University.
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 the Australian National University. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * Copyright (c) 1991 Gregory M. Christy.
20 * All rights reserved.
22 * Redistribution and use in source and binary forms are permitted
23 * provided that the above copyright notice and this paragraph are
24 * duplicated in all such forms and that any documentation,
25 * advertising materials, and other materials related to such
26 * distribution and use acknowledge that the software was developed
27 * by Gregory M. Christy. The name of the author may not be used to
28 * endorse or promote products derived from this software without
29 * specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
33 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36 #define RCSID "$Id: chap.c,v 1.25 2001/03/08 05:11:11 paulus Exp $"
44 #include <sys/types.h>
54 static const char rcsid[] = RCSID;
57 * Command-line options.
59 static option_t chap_option_list[] = {
60 { "chap-restart", o_int, &chap[0].timeouttime,
61 "Set timeout for CHAP", OPT_PRIO },
62 { "chap-max-challenge", o_int, &chap[0].max_transmits,
63 "Set max #xmits for challenge", OPT_PRIO },
64 { "chap-interval", o_int, &chap[0].chal_interval,
65 "Set interval for rechallenge", OPT_PRIO },
67 { "ms-lanman", o_bool, &ms_lanman,
68 "Use LanMan passwd when using MS-CHAP", 1 },
74 * Protocol entry points.
76 static void ChapInit __P((int));
77 static void ChapLowerUp __P((int));
78 static void ChapLowerDown __P((int));
79 static void ChapInput __P((int, u_char *, int));
80 static void ChapProtocolReject __P((int));
81 static int ChapPrintPkt __P((u_char *, int,
82 void (*) __P((void *, char *, ...)), void *));
84 struct protent chap_protent = {
104 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
106 static void ChapChallengeTimeout __P((void *));
107 static void ChapResponseTimeout __P((void *));
108 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
109 static void ChapRechallenge __P((void *));
110 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
111 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
112 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
113 static void ChapSendStatus __P((chap_state *, int));
114 static void ChapSendChallenge __P((chap_state *));
115 static void ChapSendResponse __P((chap_state *));
116 static void ChapGenChallenge __P((chap_state *));
118 extern double drand48 __P((void));
119 extern void srand48 __P((long));
122 * ChapInit - Initialize a CHAP unit.
128 chap_state *cstate = &chap[unit];
130 BZERO(cstate, sizeof(*cstate));
132 cstate->clientstate = CHAPCS_INITIAL;
133 cstate->serverstate = CHAPSS_INITIAL;
134 cstate->timeouttime = CHAP_DEFTIMEOUT;
135 cstate->max_transmits = CHAP_DEFTRANSMITS;
136 /* random number generator is initialized in magic_init */
141 * ChapAuthWithPeer - Authenticate us with our peer (start client).
145 ChapAuthWithPeer(unit, our_name, digest)
150 chap_state *cstate = &chap[unit];
152 cstate->resp_name = our_name;
153 cstate->resp_type = digest;
155 if (cstate->clientstate == CHAPCS_INITIAL ||
156 cstate->clientstate == CHAPCS_PENDING) {
157 /* lower layer isn't up - wait until later */
158 cstate->clientstate = CHAPCS_PENDING;
163 * We get here as a result of LCP coming up.
164 * So even if CHAP was open before, we will
165 * have to re-authenticate ourselves.
167 cstate->clientstate = CHAPCS_LISTEN;
172 * ChapAuthPeer - Authenticate our peer (start server).
175 ChapAuthPeer(unit, our_name, digest)
180 chap_state *cstate = &chap[unit];
182 cstate->chal_name = our_name;
183 cstate->chal_type = digest;
185 if (cstate->serverstate == CHAPSS_INITIAL ||
186 cstate->serverstate == CHAPSS_PENDING) {
187 /* lower layer isn't up - wait until later */
188 cstate->serverstate = CHAPSS_PENDING;
192 ChapGenChallenge(cstate);
193 ChapSendChallenge(cstate); /* crank it up dude! */
194 cstate->serverstate = CHAPSS_INITIAL_CHAL;
199 * ChapChallengeTimeout - Timeout expired on sending challenge.
202 ChapChallengeTimeout(arg)
205 chap_state *cstate = (chap_state *) arg;
207 /* if we aren't sending challenges, don't worry. then again we */
208 /* probably shouldn't be here either */
209 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
210 cstate->serverstate != CHAPSS_RECHALLENGE)
213 if (cstate->chal_transmits >= cstate->max_transmits) {
214 /* give up on peer */
215 error("Peer failed to respond to CHAP challenge");
216 cstate->serverstate = CHAPSS_BADAUTH;
217 auth_peer_fail(cstate->unit, PPP_CHAP);
221 ChapSendChallenge(cstate); /* Re-send challenge */
226 * ChapResponseTimeout - Timeout expired on sending response.
229 ChapResponseTimeout(arg)
232 chap_state *cstate = (chap_state *) arg;
234 /* if we aren't sending a response, don't worry. */
235 if (cstate->clientstate != CHAPCS_RESPONSE)
238 ChapSendResponse(cstate); /* re-send response */
243 * ChapRechallenge - Time to challenge the peer again.
249 chap_state *cstate = (chap_state *) arg;
251 /* if we aren't sending a response, don't worry. */
252 if (cstate->serverstate != CHAPSS_OPEN)
255 ChapGenChallenge(cstate);
256 ChapSendChallenge(cstate);
257 cstate->serverstate = CHAPSS_RECHALLENGE;
262 * ChapLowerUp - The lower layer is up.
264 * Start up if we have pending requests.
270 chap_state *cstate = &chap[unit];
272 if (cstate->clientstate == CHAPCS_INITIAL)
273 cstate->clientstate = CHAPCS_CLOSED;
274 else if (cstate->clientstate == CHAPCS_PENDING)
275 cstate->clientstate = CHAPCS_LISTEN;
277 if (cstate->serverstate == CHAPSS_INITIAL)
278 cstate->serverstate = CHAPSS_CLOSED;
279 else if (cstate->serverstate == CHAPSS_PENDING) {
280 ChapGenChallenge(cstate);
281 ChapSendChallenge(cstate);
282 cstate->serverstate = CHAPSS_INITIAL_CHAL;
288 * ChapLowerDown - The lower layer is down.
290 * Cancel all timeouts.
296 chap_state *cstate = &chap[unit];
298 /* Timeout(s) pending? Cancel if so. */
299 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
300 cstate->serverstate == CHAPSS_RECHALLENGE)
301 UNTIMEOUT(ChapChallengeTimeout, cstate);
302 else if (cstate->serverstate == CHAPSS_OPEN
303 && cstate->chal_interval != 0)
304 UNTIMEOUT(ChapRechallenge, cstate);
305 if (cstate->clientstate == CHAPCS_RESPONSE)
306 UNTIMEOUT(ChapResponseTimeout, cstate);
308 cstate->clientstate = CHAPCS_INITIAL;
309 cstate->serverstate = CHAPSS_INITIAL;
314 * ChapProtocolReject - Peer doesn't grok CHAP.
317 ChapProtocolReject(unit)
320 chap_state *cstate = &chap[unit];
322 if (cstate->serverstate != CHAPSS_INITIAL &&
323 cstate->serverstate != CHAPSS_CLOSED)
324 auth_peer_fail(unit, PPP_CHAP);
325 if (cstate->clientstate != CHAPCS_INITIAL &&
326 cstate->clientstate != CHAPCS_CLOSED)
327 auth_withpeer_fail(unit, PPP_CHAP);
328 ChapLowerDown(unit); /* shutdown chap */
333 * ChapInput - Input CHAP packet.
336 ChapInput(unit, inpacket, packet_len)
341 chap_state *cstate = &chap[unit];
347 * Parse header (code, id and length).
348 * If packet too short, drop it.
351 if (packet_len < CHAP_HEADERLEN) {
352 CHAPDEBUG(("ChapInput: rcvd short header."));
358 if (len < CHAP_HEADERLEN) {
359 CHAPDEBUG(("ChapInput: rcvd illegal length."));
362 if (len > packet_len) {
363 CHAPDEBUG(("ChapInput: rcvd short packet."));
366 len -= CHAP_HEADERLEN;
369 * Action depends on code (as in fact it usually does :-).
373 ChapReceiveChallenge(cstate, inp, id, len);
377 ChapReceiveResponse(cstate, inp, id, len);
381 ChapReceiveFailure(cstate, inp, id, len);
385 ChapReceiveSuccess(cstate, inp, id, len);
388 default: /* Need code reject? */
389 warn("Unknown CHAP code (%d) received.", code);
396 * ChapReceiveChallenge - Receive Challenge and send Response.
399 ChapReceiveChallenge(cstate, inp, id, len)
408 char secret[MAXSECRETLEN];
411 u_char hash[MD5_SIGNATURE_SIZE];
413 if (cstate->clientstate == CHAPCS_CLOSED ||
414 cstate->clientstate == CHAPCS_PENDING) {
415 CHAPDEBUG(("ChapReceiveChallenge: in state %d", cstate->clientstate));
420 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
424 GETCHAR(rchallenge_len, inp);
425 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
427 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
431 INCPTR(rchallenge_len, inp);
433 if (len >= sizeof(rhostname))
434 len = sizeof(rhostname) - 1;
435 BCOPY(inp, rhostname, len);
436 rhostname[len] = '\000';
438 /* Microsoft doesn't send their name back in the PPP packet */
439 if (explicit_remote || (remote_name[0] != 0 && rhostname[0] == 0)) {
440 strlcpy(rhostname, remote_name, sizeof(rhostname));
441 CHAPDEBUG(("ChapReceiveChallenge: using '%q' as remote name",
445 /* get secret for authenticating ourselves with the specified host */
446 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
447 secret, &secret_len, 0)) {
448 secret_len = 0; /* assume null secret if can't find one */
449 warn("No CHAP secret found for authenticating us to %q", rhostname);
452 /* cancel response send timeout if necessary */
453 if (cstate->clientstate == CHAPCS_RESPONSE)
454 UNTIMEOUT(ChapResponseTimeout, cstate);
456 cstate->resp_id = id;
457 cstate->resp_transmits = 0;
459 /* generate MD based on negotiated type */
460 switch (cstate->resp_type) {
462 case CHAP_DIGEST_MD5:
464 MD5Update(&mdContext, &cstate->resp_id, 1);
465 MD5Update(&mdContext, secret, secret_len);
466 MD5Update(&mdContext, rchallenge, rchallenge_len);
467 MD5Final(hash, &mdContext);
468 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
469 cstate->resp_length = MD5_SIGNATURE_SIZE;
474 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
479 CHAPDEBUG(("unknown digest type %d", cstate->resp_type));
483 BZERO(secret, sizeof(secret));
484 ChapSendResponse(cstate);
489 * ChapReceiveResponse - Receive and process response.
492 ChapReceiveResponse(cstate, inp, id, len)
498 u_char *remmd, remmd_len;
499 int secret_len, old_state;
503 char secret[MAXSECRETLEN];
504 u_char hash[MD5_SIGNATURE_SIZE];
506 if (cstate->serverstate == CHAPSS_CLOSED ||
507 cstate->serverstate == CHAPSS_PENDING) {
508 CHAPDEBUG(("ChapReceiveResponse: in state %d", cstate->serverstate));
512 if (id != cstate->chal_id)
513 return; /* doesn't match ID of last challenge */
516 * If we have received a duplicate or bogus Response,
517 * we have to send the same answer (Success/Failure)
518 * as we did for the first Response we saw.
520 if (cstate->serverstate == CHAPSS_OPEN) {
521 ChapSendStatus(cstate, CHAP_SUCCESS);
524 if (cstate->serverstate == CHAPSS_BADAUTH) {
525 ChapSendStatus(cstate, CHAP_FAILURE);
530 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
533 GETCHAR(remmd_len, inp); /* get length of MD */
534 remmd = inp; /* get pointer to MD */
535 INCPTR(remmd_len, inp);
537 len -= sizeof (u_char) + remmd_len;
539 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
543 UNTIMEOUT(ChapChallengeTimeout, cstate);
545 if (len >= sizeof(rhostname))
546 len = sizeof(rhostname) - 1;
547 BCOPY(inp, rhostname, len);
548 rhostname[len] = '\000';
551 * Get secret for authenticating them with us,
552 * do the hash ourselves, and compare the result.
555 if (!get_secret(cstate->unit, (explicit_remote? remote_name: rhostname),
556 cstate->chal_name, secret, &secret_len, 1)) {
557 warn("No CHAP secret found for authenticating %q", rhostname);
560 /* generate MD based on negotiated type */
561 switch (cstate->chal_type) {
563 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
564 if (remmd_len != MD5_SIGNATURE_SIZE)
565 break; /* it's not even the right length */
567 MD5Update(&mdContext, &cstate->chal_id, 1);
568 MD5Update(&mdContext, secret, secret_len);
569 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
570 MD5Final(hash, &mdContext);
572 /* compare local and remote MDs and send the appropriate status */
573 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
574 code = CHAP_SUCCESS; /* they are the same! */
578 CHAPDEBUG(("unknown digest type %d", cstate->chal_type));
582 BZERO(secret, sizeof(secret));
583 ChapSendStatus(cstate, code);
585 if (code == CHAP_SUCCESS) {
586 old_state = cstate->serverstate;
587 cstate->serverstate = CHAPSS_OPEN;
588 if (old_state == CHAPSS_INITIAL_CHAL) {
589 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
591 if (cstate->chal_interval != 0)
592 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
593 notice("CHAP peer authentication succeeded for %q", rhostname);
596 error("CHAP peer authentication failed for remote host %q", rhostname);
597 cstate->serverstate = CHAPSS_BADAUTH;
598 auth_peer_fail(cstate->unit, PPP_CHAP);
603 * ChapReceiveSuccess - Receive Success
606 ChapReceiveSuccess(cstate, inp, id, len)
613 if (cstate->clientstate == CHAPCS_OPEN)
614 /* presumably an answer to a duplicate response */
617 if (cstate->clientstate != CHAPCS_RESPONSE) {
618 /* don't know what this is */
619 CHAPDEBUG(("ChapReceiveSuccess: in state %d\n", cstate->clientstate));
623 UNTIMEOUT(ChapResponseTimeout, cstate);
631 cstate->clientstate = CHAPCS_OPEN;
633 auth_withpeer_success(cstate->unit, PPP_CHAP);
638 * ChapReceiveFailure - Receive failure.
641 ChapReceiveFailure(cstate, inp, id, len)
647 if (cstate->clientstate != CHAPCS_RESPONSE) {
648 /* don't know what this is */
649 CHAPDEBUG(("ChapReceiveFailure: in state %d\n", cstate->clientstate));
653 UNTIMEOUT(ChapResponseTimeout, cstate);
661 error("CHAP authentication failed");
662 auth_withpeer_fail(cstate->unit, PPP_CHAP);
667 * ChapSendChallenge - Send an Authenticate challenge.
670 ChapSendChallenge(cstate)
674 int chal_len, name_len;
677 chal_len = cstate->chal_len;
678 name_len = strlen(cstate->chal_name);
679 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
680 outp = outpacket_buf;
682 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
684 PUTCHAR(CHAP_CHALLENGE, outp);
685 PUTCHAR(cstate->chal_id, outp);
686 PUTSHORT(outlen, outp);
688 PUTCHAR(chal_len, outp); /* put length of challenge */
689 BCOPY(cstate->challenge, outp, chal_len);
690 INCPTR(chal_len, outp);
692 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
694 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
696 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
697 ++cstate->chal_transmits;
702 * ChapSendStatus - Send a status response (ack or nak).
705 ChapSendStatus(cstate, code)
713 if (code == CHAP_SUCCESS)
714 slprintf(msg, sizeof(msg), "Welcome to %s.", hostname);
716 slprintf(msg, sizeof(msg), "I don't like you. Go 'way.");
717 msglen = strlen(msg);
719 outlen = CHAP_HEADERLEN + msglen;
720 outp = outpacket_buf;
722 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
725 PUTCHAR(cstate->chal_id, outp);
726 PUTSHORT(outlen, outp);
727 BCOPY(msg, outp, msglen);
728 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
732 * ChapGenChallenge is used to generate a pseudo-random challenge string of
733 * a pseudo-random length between min_len and max_len. The challenge
734 * string and its length are stored in *cstate, and various other fields of
735 * *cstate are initialized.
739 ChapGenChallenge(cstate)
743 u_char *ptr = cstate->challenge;
746 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
747 MAX_CHALLENGE_LENGTH */
748 chal_len = (unsigned) ((drand48() *
749 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
750 MIN_CHALLENGE_LENGTH);
751 cstate->chal_len = chal_len;
752 cstate->chal_id = ++cstate->id;
753 cstate->chal_transmits = 0;
755 /* generate a random string */
756 for (i = 0; i < chal_len; i++)
757 *ptr++ = (char) (drand48() * 0xff);
761 * ChapSendResponse - send a response packet with values as specified
766 ChapSendResponse(cstate)
770 int outlen, md_len, name_len;
772 md_len = cstate->resp_length;
773 name_len = strlen(cstate->resp_name);
774 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
775 outp = outpacket_buf;
777 MAKEHEADER(outp, PPP_CHAP);
779 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
780 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
781 PUTSHORT(outlen, outp); /* packet length */
783 PUTCHAR(md_len, outp); /* length of MD */
784 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
785 INCPTR(md_len, outp);
787 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
789 /* send the packet */
790 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
792 cstate->clientstate = CHAPCS_RESPONSE;
793 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
794 ++cstate->resp_transmits;
798 * ChapPrintPkt - print the contents of a CHAP packet.
800 static char *ChapCodenames[] = {
801 "Challenge", "Response", "Success", "Failure"
805 ChapPrintPkt(p, plen, printer, arg)
808 void (*printer) __P((void *, char *, ...));
815 if (plen < CHAP_HEADERLEN)
820 if (len < CHAP_HEADERLEN || len > plen)
823 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
824 printer(arg, " %s", ChapCodenames[code-1]);
826 printer(arg, " code=0x%x", code);
827 printer(arg, " id=0x%x", id);
828 len -= CHAP_HEADERLEN;
838 nlen = len - clen - 1;
840 for (; clen > 0; --clen) {
842 printer(arg, "%.2x", x);
844 printer(arg, ">, name = ");
845 print_string((char *)p, nlen, printer, arg);
850 print_string((char *)p, len, printer, arg);
853 for (clen = len; clen > 0; --clen) {
855 printer(arg, " %.2x", x);
859 return len + CHAP_HEADERLEN;