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.26 2002/01/22 16:02:58 dfs Exp $"
44 #include <sys/types.h>
54 /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
55 int (*chap_check_hook) __P((void)) = NULL;
57 /* Hook for a plugin to get the CHAP password for authenticating us */
58 int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
60 /* Hook for a plugin to validate CHAP challenge */
61 int (*chap_auth_hook) __P((char *user,
64 chap_state *cstate)) = NULL;
66 static const char rcsid[] = RCSID;
69 * Command-line options.
71 static option_t chap_option_list[] = {
72 { "chap-restart", o_int, &chap[0].timeouttime,
73 "Set timeout for CHAP", OPT_PRIO },
74 { "chap-max-challenge", o_int, &chap[0].max_transmits,
75 "Set max #xmits for challenge", OPT_PRIO },
76 { "chap-interval", o_int, &chap[0].chal_interval,
77 "Set interval for rechallenge", OPT_PRIO },
79 { "ms-lanman", o_bool, &ms_lanman,
80 "Use LanMan passwd when using MS-CHAP", 1 },
86 * Protocol entry points.
88 static void ChapInit __P((int));
89 static void ChapLowerUp __P((int));
90 static void ChapLowerDown __P((int));
91 static void ChapInput __P((int, u_char *, int));
92 static void ChapProtocolReject __P((int));
93 static int ChapPrintPkt __P((u_char *, int,
94 void (*) __P((void *, char *, ...)), void *));
96 struct protent chap_protent = {
116 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
118 static void ChapChallengeTimeout __P((void *));
119 static void ChapResponseTimeout __P((void *));
120 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
121 static void ChapRechallenge __P((void *));
122 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
123 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
124 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
125 static void ChapSendStatus __P((chap_state *, int));
126 static void ChapSendChallenge __P((chap_state *));
127 static void ChapSendResponse __P((chap_state *));
128 static void ChapGenChallenge __P((chap_state *));
130 extern double drand48 __P((void));
131 extern void srand48 __P((long));
134 * ChapInit - Initialize a CHAP unit.
140 chap_state *cstate = &chap[unit];
142 BZERO(cstate, sizeof(*cstate));
144 cstate->clientstate = CHAPCS_INITIAL;
145 cstate->serverstate = CHAPSS_INITIAL;
146 cstate->timeouttime = CHAP_DEFTIMEOUT;
147 cstate->max_transmits = CHAP_DEFTRANSMITS;
148 /* random number generator is initialized in magic_init */
153 * ChapAuthWithPeer - Authenticate us with our peer (start client).
157 ChapAuthWithPeer(unit, our_name, digest)
162 chap_state *cstate = &chap[unit];
164 cstate->resp_name = our_name;
165 cstate->resp_type = digest;
167 if (cstate->clientstate == CHAPCS_INITIAL ||
168 cstate->clientstate == CHAPCS_PENDING) {
169 /* lower layer isn't up - wait until later */
170 cstate->clientstate = CHAPCS_PENDING;
175 * We get here as a result of LCP coming up.
176 * So even if CHAP was open before, we will
177 * have to re-authenticate ourselves.
179 cstate->clientstate = CHAPCS_LISTEN;
184 * ChapAuthPeer - Authenticate our peer (start server).
187 ChapAuthPeer(unit, our_name, digest)
192 chap_state *cstate = &chap[unit];
194 cstate->chal_name = our_name;
195 cstate->chal_type = digest;
197 if (cstate->serverstate == CHAPSS_INITIAL ||
198 cstate->serverstate == CHAPSS_PENDING) {
199 /* lower layer isn't up - wait until later */
200 cstate->serverstate = CHAPSS_PENDING;
204 ChapGenChallenge(cstate);
205 ChapSendChallenge(cstate); /* crank it up dude! */
206 cstate->serverstate = CHAPSS_INITIAL_CHAL;
211 * ChapChallengeTimeout - Timeout expired on sending challenge.
214 ChapChallengeTimeout(arg)
217 chap_state *cstate = (chap_state *) arg;
219 /* if we aren't sending challenges, don't worry. then again we */
220 /* probably shouldn't be here either */
221 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
222 cstate->serverstate != CHAPSS_RECHALLENGE)
225 if (cstate->chal_transmits >= cstate->max_transmits) {
226 /* give up on peer */
227 error("Peer failed to respond to CHAP challenge");
228 cstate->serverstate = CHAPSS_BADAUTH;
229 auth_peer_fail(cstate->unit, PPP_CHAP);
233 ChapSendChallenge(cstate); /* Re-send challenge */
238 * ChapResponseTimeout - Timeout expired on sending response.
241 ChapResponseTimeout(arg)
244 chap_state *cstate = (chap_state *) arg;
246 /* if we aren't sending a response, don't worry. */
247 if (cstate->clientstate != CHAPCS_RESPONSE)
250 ChapSendResponse(cstate); /* re-send response */
255 * ChapRechallenge - Time to challenge the peer again.
261 chap_state *cstate = (chap_state *) arg;
263 /* if we aren't sending a response, don't worry. */
264 if (cstate->serverstate != CHAPSS_OPEN)
267 ChapGenChallenge(cstate);
268 ChapSendChallenge(cstate);
269 cstate->serverstate = CHAPSS_RECHALLENGE;
274 * ChapLowerUp - The lower layer is up.
276 * Start up if we have pending requests.
282 chap_state *cstate = &chap[unit];
284 if (cstate->clientstate == CHAPCS_INITIAL)
285 cstate->clientstate = CHAPCS_CLOSED;
286 else if (cstate->clientstate == CHAPCS_PENDING)
287 cstate->clientstate = CHAPCS_LISTEN;
289 if (cstate->serverstate == CHAPSS_INITIAL)
290 cstate->serverstate = CHAPSS_CLOSED;
291 else if (cstate->serverstate == CHAPSS_PENDING) {
292 ChapGenChallenge(cstate);
293 ChapSendChallenge(cstate);
294 cstate->serverstate = CHAPSS_INITIAL_CHAL;
300 * ChapLowerDown - The lower layer is down.
302 * Cancel all timeouts.
308 chap_state *cstate = &chap[unit];
310 /* Timeout(s) pending? Cancel if so. */
311 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
312 cstate->serverstate == CHAPSS_RECHALLENGE)
313 UNTIMEOUT(ChapChallengeTimeout, cstate);
314 else if (cstate->serverstate == CHAPSS_OPEN
315 && cstate->chal_interval != 0)
316 UNTIMEOUT(ChapRechallenge, cstate);
317 if (cstate->clientstate == CHAPCS_RESPONSE)
318 UNTIMEOUT(ChapResponseTimeout, cstate);
320 cstate->clientstate = CHAPCS_INITIAL;
321 cstate->serverstate = CHAPSS_INITIAL;
326 * ChapProtocolReject - Peer doesn't grok CHAP.
329 ChapProtocolReject(unit)
332 chap_state *cstate = &chap[unit];
334 if (cstate->serverstate != CHAPSS_INITIAL &&
335 cstate->serverstate != CHAPSS_CLOSED)
336 auth_peer_fail(unit, PPP_CHAP);
337 if (cstate->clientstate != CHAPCS_INITIAL &&
338 cstate->clientstate != CHAPCS_CLOSED)
339 auth_withpeer_fail(unit, PPP_CHAP);
340 ChapLowerDown(unit); /* shutdown chap */
345 * ChapInput - Input CHAP packet.
348 ChapInput(unit, inpacket, packet_len)
353 chap_state *cstate = &chap[unit];
359 * Parse header (code, id and length).
360 * If packet too short, drop it.
363 if (packet_len < CHAP_HEADERLEN) {
364 CHAPDEBUG(("ChapInput: rcvd short header."));
370 if (len < CHAP_HEADERLEN) {
371 CHAPDEBUG(("ChapInput: rcvd illegal length."));
374 if (len > packet_len) {
375 CHAPDEBUG(("ChapInput: rcvd short packet."));
378 len -= CHAP_HEADERLEN;
381 * Action depends on code (as in fact it usually does :-).
385 ChapReceiveChallenge(cstate, inp, id, len);
389 ChapReceiveResponse(cstate, inp, id, len);
393 ChapReceiveFailure(cstate, inp, id, len);
397 ChapReceiveSuccess(cstate, inp, id, len);
400 default: /* Need code reject? */
401 warn("Unknown CHAP code (%d) received.", code);
408 * ChapReceiveChallenge - Receive Challenge and send Response.
411 ChapReceiveChallenge(cstate, inp, id, len)
420 char secret[MAXSECRETLEN];
423 u_char hash[MD5_SIGNATURE_SIZE];
425 if (cstate->clientstate == CHAPCS_CLOSED ||
426 cstate->clientstate == CHAPCS_PENDING) {
427 CHAPDEBUG(("ChapReceiveChallenge: in state %d", cstate->clientstate));
432 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
436 GETCHAR(rchallenge_len, inp);
437 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
439 CHAPDEBUG(("ChapReceiveChallenge: rcvd short packet."));
443 INCPTR(rchallenge_len, inp);
445 if (len >= sizeof(rhostname))
446 len = sizeof(rhostname) - 1;
447 BCOPY(inp, rhostname, len);
448 rhostname[len] = '\000';
450 /* Microsoft doesn't send their name back in the PPP packet */
451 if (explicit_remote || (remote_name[0] != 0 && rhostname[0] == 0)) {
452 strlcpy(rhostname, remote_name, sizeof(rhostname));
453 CHAPDEBUG(("ChapReceiveChallenge: using '%q' as remote name",
457 /* get secret for authenticating ourselves with the specified host */
458 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
459 secret, &secret_len, 0)) {
460 secret_len = 0; /* assume null secret if can't find one */
461 warn("No CHAP secret found for authenticating us to %q", rhostname);
464 /* cancel response send timeout if necessary */
465 if (cstate->clientstate == CHAPCS_RESPONSE)
466 UNTIMEOUT(ChapResponseTimeout, cstate);
468 cstate->resp_id = id;
469 cstate->resp_transmits = 0;
471 /* generate MD based on negotiated type */
472 switch (cstate->resp_type) {
474 case CHAP_DIGEST_MD5:
476 MD5Update(&mdContext, &cstate->resp_id, 1);
477 MD5Update(&mdContext, secret, secret_len);
478 MD5Update(&mdContext, rchallenge, rchallenge_len);
479 MD5Final(hash, &mdContext);
480 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
481 cstate->resp_length = MD5_SIGNATURE_SIZE;
486 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
491 CHAPDEBUG(("unknown digest type %d", cstate->resp_type));
495 BZERO(secret, sizeof(secret));
496 ChapSendResponse(cstate);
501 * ChapReceiveResponse - Receive and process response.
504 ChapReceiveResponse(cstate, inp, id, len)
510 u_char *remmd, remmd_len;
511 int secret_len, old_state;
515 char secret[MAXSECRETLEN];
516 u_char hash[MD5_SIGNATURE_SIZE];
518 if (cstate->serverstate == CHAPSS_CLOSED ||
519 cstate->serverstate == CHAPSS_PENDING) {
520 CHAPDEBUG(("ChapReceiveResponse: in state %d", cstate->serverstate));
524 if (id != cstate->chal_id)
525 return; /* doesn't match ID of last challenge */
528 * If we have received a duplicate or bogus Response,
529 * we have to send the same answer (Success/Failure)
530 * as we did for the first Response we saw.
532 if (cstate->serverstate == CHAPSS_OPEN) {
533 ChapSendStatus(cstate, CHAP_SUCCESS);
536 if (cstate->serverstate == CHAPSS_BADAUTH) {
537 ChapSendStatus(cstate, CHAP_FAILURE);
542 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
545 GETCHAR(remmd_len, inp); /* get length of MD */
546 remmd = inp; /* get pointer to MD */
547 INCPTR(remmd_len, inp);
549 len -= sizeof (u_char) + remmd_len;
551 CHAPDEBUG(("ChapReceiveResponse: rcvd short packet."));
555 UNTIMEOUT(ChapChallengeTimeout, cstate);
557 if (len >= sizeof(rhostname))
558 len = sizeof(rhostname) - 1;
559 BCOPY(inp, rhostname, len);
560 rhostname[len] = '\000';
563 * Get secret for authenticating them with us,
564 * do the hash ourselves, and compare the result.
568 /* If a plugin will verify the response, let the plugin do it. */
569 if (chap_auth_hook) {
570 code = (*chap_auth_hook) ( (explicit_remote ? remote_name : rhostname),
571 remmd, (int) remmd_len,
574 if (!get_secret(cstate->unit, (explicit_remote? remote_name: rhostname),
575 cstate->chal_name, secret, &secret_len, 1)) {
576 warn("No CHAP secret found for authenticating %q", rhostname);
579 /* generate MD based on negotiated type */
580 switch (cstate->chal_type) {
582 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
583 if (remmd_len != MD5_SIGNATURE_SIZE)
584 break; /* it's not even the right length */
586 MD5Update(&mdContext, &cstate->chal_id, 1);
587 MD5Update(&mdContext, secret, secret_len);
588 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
589 MD5Final(hash, &mdContext);
591 /* compare local and remote MDs and send the appropriate status */
592 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
593 code = CHAP_SUCCESS; /* they are the same! */
597 CHAPDEBUG(("unknown digest type %d", cstate->chal_type));
601 BZERO(secret, sizeof(secret));
603 ChapSendStatus(cstate, code);
605 if (code == CHAP_SUCCESS) {
606 old_state = cstate->serverstate;
607 cstate->serverstate = CHAPSS_OPEN;
608 if (old_state == CHAPSS_INITIAL_CHAL) {
609 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
611 if (cstate->chal_interval != 0)
612 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
613 notice("CHAP peer authentication succeeded for %q", rhostname);
616 error("CHAP peer authentication failed for remote host %q", rhostname);
617 cstate->serverstate = CHAPSS_BADAUTH;
618 auth_peer_fail(cstate->unit, PPP_CHAP);
623 * ChapReceiveSuccess - Receive Success
626 ChapReceiveSuccess(cstate, inp, id, len)
633 if (cstate->clientstate == CHAPCS_OPEN)
634 /* presumably an answer to a duplicate response */
637 if (cstate->clientstate != CHAPCS_RESPONSE) {
638 /* don't know what this is */
639 CHAPDEBUG(("ChapReceiveSuccess: in state %d\n", cstate->clientstate));
643 UNTIMEOUT(ChapResponseTimeout, cstate);
651 cstate->clientstate = CHAPCS_OPEN;
653 auth_withpeer_success(cstate->unit, PPP_CHAP);
658 * ChapReceiveFailure - Receive failure.
661 ChapReceiveFailure(cstate, inp, id, len)
667 if (cstate->clientstate != CHAPCS_RESPONSE) {
668 /* don't know what this is */
669 CHAPDEBUG(("ChapReceiveFailure: in state %d\n", cstate->clientstate));
673 UNTIMEOUT(ChapResponseTimeout, cstate);
681 error("CHAP authentication failed");
682 auth_withpeer_fail(cstate->unit, PPP_CHAP);
687 * ChapSendChallenge - Send an Authenticate challenge.
690 ChapSendChallenge(cstate)
694 int chal_len, name_len;
697 chal_len = cstate->chal_len;
698 name_len = strlen(cstate->chal_name);
699 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
700 outp = outpacket_buf;
702 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
704 PUTCHAR(CHAP_CHALLENGE, outp);
705 PUTCHAR(cstate->chal_id, outp);
706 PUTSHORT(outlen, outp);
708 PUTCHAR(chal_len, outp); /* put length of challenge */
709 BCOPY(cstate->challenge, outp, chal_len);
710 INCPTR(chal_len, outp);
712 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
714 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
716 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
717 ++cstate->chal_transmits;
722 * ChapSendStatus - Send a status response (ack or nak).
725 ChapSendStatus(cstate, code)
733 if (code == CHAP_SUCCESS)
734 slprintf(msg, sizeof(msg), "Welcome to %s.", hostname);
736 slprintf(msg, sizeof(msg), "I don't like you. Go 'way.");
737 msglen = strlen(msg);
739 outlen = CHAP_HEADERLEN + msglen;
740 outp = outpacket_buf;
742 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
745 PUTCHAR(cstate->chal_id, outp);
746 PUTSHORT(outlen, outp);
747 BCOPY(msg, outp, msglen);
748 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
752 * ChapGenChallenge is used to generate a pseudo-random challenge string of
753 * a pseudo-random length between min_len and max_len. The challenge
754 * string and its length are stored in *cstate, and various other fields of
755 * *cstate are initialized.
759 ChapGenChallenge(cstate)
763 u_char *ptr = cstate->challenge;
766 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
767 MAX_CHALLENGE_LENGTH */
768 chal_len = (unsigned) ((drand48() *
769 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
770 MIN_CHALLENGE_LENGTH);
771 cstate->chal_len = chal_len;
772 cstate->chal_id = ++cstate->id;
773 cstate->chal_transmits = 0;
775 /* generate a random string */
776 for (i = 0; i < chal_len; i++)
777 *ptr++ = (char) (drand48() * 0xff);
781 * ChapSendResponse - send a response packet with values as specified
786 ChapSendResponse(cstate)
790 int outlen, md_len, name_len;
792 md_len = cstate->resp_length;
793 name_len = strlen(cstate->resp_name);
794 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
795 outp = outpacket_buf;
797 MAKEHEADER(outp, PPP_CHAP);
799 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
800 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
801 PUTSHORT(outlen, outp); /* packet length */
803 PUTCHAR(md_len, outp); /* length of MD */
804 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
805 INCPTR(md_len, outp);
807 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
809 /* send the packet */
810 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
812 cstate->clientstate = CHAPCS_RESPONSE;
813 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
814 ++cstate->resp_transmits;
818 * ChapPrintPkt - print the contents of a CHAP packet.
820 static char *ChapCodenames[] = {
821 "Challenge", "Response", "Success", "Failure"
825 ChapPrintPkt(p, plen, printer, arg)
828 void (*printer) __P((void *, char *, ...));
835 if (plen < CHAP_HEADERLEN)
840 if (len < CHAP_HEADERLEN || len > plen)
843 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
844 printer(arg, " %s", ChapCodenames[code-1]);
846 printer(arg, " code=0x%x", code);
847 printer(arg, " id=0x%x", id);
848 len -= CHAP_HEADERLEN;
858 nlen = len - clen - 1;
860 for (; clen > 0; --clen) {
862 printer(arg, "%.2x", x);
864 printer(arg, ">, name = ");
865 print_string((char *)p, nlen, printer, arg);
870 print_string((char *)p, len, printer, arg);
873 for (clen = len; clen > 0; --clen) {
875 printer(arg, " %.2x", x);
879 return len + CHAP_HEADERLEN;