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.
37 static char rcsid[] = "$Id: chap.c,v 1.16 1998/11/07 06:59:26 paulus Exp $";
46 #include <sys/types.h>
58 * Command-line options.
60 static option_t chap_option_list[] = {
61 { "chap-restart", o_int, &chap[0].timeouttime,
62 "Set timeout for CHAP" },
63 { "chap-max-challenge", o_int, &chap[0].max_transmits,
64 "Set max #xmits for challenge" },
65 { "chap-interval", o_int, &chap[0].chal_interval,
66 "Set interval for rechallenge" },
68 { "ms-lanman", o_bool, &ms_lanman,
69 "Use LanMan psswd when using MS-CHAP", 1 },
75 * Protocol entry points.
77 static void ChapInit __P((int));
78 static void ChapLowerUp __P((int));
79 static void ChapLowerDown __P((int));
80 static void ChapInput __P((int, u_char *, int));
81 static void ChapProtocolReject __P((int));
82 static int ChapPrintPkt __P((u_char *, int,
83 void (*) __P((void *, char *, ...)), void *));
85 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 syslog(LOG_ERR, "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((LOG_INFO, "ChapInput: rcvd short header."));
358 if (len < CHAP_HEADERLEN) {
359 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
362 if (len > packet_len) {
363 CHAPDEBUG((LOG_INFO, "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 syslog(LOG_WARNING, "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 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
414 if (cstate->clientstate == CHAPCS_CLOSED ||
415 cstate->clientstate == CHAPCS_PENDING) {
416 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
417 cstate->clientstate));
422 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
426 GETCHAR(rchallenge_len, inp);
427 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
429 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
433 INCPTR(rchallenge_len, inp);
435 if (len >= sizeof(rhostname))
436 len = sizeof(rhostname) - 1;
437 BCOPY(inp, rhostname, len);
438 rhostname[len] = '\000';
440 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field '%s'",
443 /* Microsoft doesn't send their name back in the PPP packet */
444 if (remote_name[0] != 0 && (explicit_remote || rhostname[0] == 0)) {
445 strncpy(rhostname, remote_name, sizeof(rhostname));
446 rhostname[sizeof(rhostname) - 1] = 0;
447 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: using '%s' as remote name",
451 /* get secret for authenticating ourselves with the specified host */
452 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
453 secret, &secret_len, 0)) {
454 secret_len = 0; /* assume null secret if can't find one */
455 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
459 /* cancel response send timeout if necessary */
460 if (cstate->clientstate == CHAPCS_RESPONSE)
461 UNTIMEOUT(ChapResponseTimeout, cstate);
463 cstate->resp_id = id;
464 cstate->resp_transmits = 0;
466 /* generate MD based on negotiated type */
467 switch (cstate->resp_type) {
469 case CHAP_DIGEST_MD5:
471 MD5Update(&mdContext, &cstate->resp_id, 1);
472 MD5Update(&mdContext, secret, secret_len);
473 MD5Update(&mdContext, rchallenge, rchallenge_len);
474 MD5Final(hash, &mdContext);
475 BCOPY(hash, cstate->response, MD5_SIGNATURE_SIZE);
476 cstate->resp_length = MD5_SIGNATURE_SIZE;
481 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
486 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
490 BZERO(secret, sizeof(secret));
491 ChapSendResponse(cstate);
496 * ChapReceiveResponse - Receive and process response.
499 ChapReceiveResponse(cstate, inp, id, len)
505 u_char *remmd, remmd_len;
506 int secret_len, old_state;
510 char secret[MAXSECRETLEN];
511 u_char hash[MD5_SIGNATURE_SIZE];
513 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
515 if (cstate->serverstate == CHAPSS_CLOSED ||
516 cstate->serverstate == CHAPSS_PENDING) {
517 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
518 cstate->serverstate));
522 if (id != cstate->chal_id)
523 return; /* doesn't match ID of last challenge */
526 * If we have received a duplicate or bogus Response,
527 * we have to send the same answer (Success/Failure)
528 * as we did for the first Response we saw.
530 if (cstate->serverstate == CHAPSS_OPEN) {
531 ChapSendStatus(cstate, CHAP_SUCCESS);
534 if (cstate->serverstate == CHAPSS_BADAUTH) {
535 ChapSendStatus(cstate, CHAP_FAILURE);
540 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
543 GETCHAR(remmd_len, inp); /* get length of MD */
544 remmd = inp; /* get pointer to MD */
545 INCPTR(remmd_len, inp);
547 len -= sizeof (u_char) + remmd_len;
549 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
553 UNTIMEOUT(ChapChallengeTimeout, cstate);
555 if (len >= sizeof(rhostname))
556 len = sizeof(rhostname) - 1;
557 BCOPY(inp, rhostname, len);
558 rhostname[len] = '\000';
560 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
564 * Get secret for authenticating them with us,
565 * do the hash ourselves, and compare the result.
568 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
569 secret, &secret_len, 1)) {
570 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
574 /* generate MD based on negotiated type */
575 switch (cstate->chal_type) {
577 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
578 if (remmd_len != MD5_SIGNATURE_SIZE)
579 break; /* it's not even the right length */
581 MD5Update(&mdContext, &cstate->chal_id, 1);
582 MD5Update(&mdContext, secret, secret_len);
583 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
584 MD5Final(hash, &mdContext);
586 /* compare local and remote MDs and send the appropriate status */
587 if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
588 code = CHAP_SUCCESS; /* they are the same! */
592 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
596 BZERO(secret, sizeof(secret));
597 ChapSendStatus(cstate, code);
599 if (code == CHAP_SUCCESS) {
600 old_state = cstate->serverstate;
601 cstate->serverstate = CHAPSS_OPEN;
602 if (old_state == CHAPSS_INITIAL_CHAL) {
603 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
605 if (cstate->chal_interval != 0)
606 TIMEOUT(ChapRechallenge, cstate, cstate->chal_interval);
607 syslog(LOG_NOTICE, "CHAP peer authentication succeeded for %s",
611 syslog(LOG_ERR, "CHAP peer authentication failed for remote host %s",
613 cstate->serverstate = CHAPSS_BADAUTH;
614 auth_peer_fail(cstate->unit, PPP_CHAP);
619 * ChapReceiveSuccess - Receive Success
622 ChapReceiveSuccess(cstate, inp, id, len)
629 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
631 if (cstate->clientstate == CHAPCS_OPEN)
632 /* presumably an answer to a duplicate response */
635 if (cstate->clientstate != CHAPCS_RESPONSE) {
636 /* don't know what this is */
637 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
638 cstate->clientstate));
642 UNTIMEOUT(ChapResponseTimeout, cstate);
650 cstate->clientstate = CHAPCS_OPEN;
652 auth_withpeer_success(cstate->unit, PPP_CHAP);
657 * ChapReceiveFailure - Receive failure.
660 ChapReceiveFailure(cstate, inp, id, len)
666 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
668 if (cstate->clientstate != CHAPCS_RESPONSE) {
669 /* don't know what this is */
670 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
671 cstate->clientstate));
675 UNTIMEOUT(ChapResponseTimeout, cstate);
683 syslog(LOG_ERR, "CHAP authentication failed");
684 auth_withpeer_fail(cstate->unit, PPP_CHAP);
689 * ChapSendChallenge - Send an Authenticate challenge.
692 ChapSendChallenge(cstate)
696 int chal_len, name_len;
699 chal_len = cstate->chal_len;
700 name_len = strlen(cstate->chal_name);
701 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
702 outp = outpacket_buf;
704 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
706 PUTCHAR(CHAP_CHALLENGE, outp);
707 PUTCHAR(cstate->chal_id, outp);
708 PUTSHORT(outlen, outp);
710 PUTCHAR(chal_len, outp); /* put length of challenge */
711 BCOPY(cstate->challenge, outp, chal_len);
712 INCPTR(chal_len, outp);
714 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
716 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
718 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
720 TIMEOUT(ChapChallengeTimeout, cstate, cstate->timeouttime);
721 ++cstate->chal_transmits;
726 * ChapSendStatus - Send a status response (ack or nak).
729 ChapSendStatus(cstate, code)
737 if (code == CHAP_SUCCESS)
738 sprintf(msg, "Welcome to %s.", hostname);
740 sprintf(msg, "I don't like you. Go 'way.");
741 msglen = strlen(msg);
743 outlen = CHAP_HEADERLEN + msglen;
744 outp = outpacket_buf;
746 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
749 PUTCHAR(cstate->chal_id, outp);
750 PUTSHORT(outlen, outp);
751 BCOPY(msg, outp, msglen);
752 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
754 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
759 * ChapGenChallenge is used to generate a pseudo-random challenge string of
760 * a pseudo-random length between min_len and max_len. The challenge
761 * string and its length are stored in *cstate, and various other fields of
762 * *cstate are initialized.
766 ChapGenChallenge(cstate)
770 u_char *ptr = cstate->challenge;
773 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
774 MAX_CHALLENGE_LENGTH */
775 chal_len = (unsigned) ((drand48() *
776 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
777 MIN_CHALLENGE_LENGTH);
778 cstate->chal_len = chal_len;
779 cstate->chal_id = ++cstate->id;
780 cstate->chal_transmits = 0;
782 /* generate a random string */
783 for (i = 0; i < chal_len; i++ )
784 *ptr++ = (char) (drand48() * 0xff);
788 * ChapSendResponse - send a response packet with values as specified
793 ChapSendResponse(cstate)
797 int outlen, md_len, name_len;
799 md_len = cstate->resp_length;
800 name_len = strlen(cstate->resp_name);
801 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
802 outp = outpacket_buf;
804 MAKEHEADER(outp, PPP_CHAP);
806 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
807 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
808 PUTSHORT(outlen, outp); /* packet length */
810 PUTCHAR(md_len, outp); /* length of MD */
811 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
812 INCPTR(md_len, outp);
814 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
816 /* send the packet */
817 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
819 cstate->clientstate = CHAPCS_RESPONSE;
820 TIMEOUT(ChapResponseTimeout, cstate, cstate->timeouttime);
821 ++cstate->resp_transmits;
825 * ChapPrintPkt - print the contents of a CHAP packet.
827 static char *ChapCodenames[] = {
828 "Challenge", "Response", "Success", "Failure"
832 ChapPrintPkt(p, plen, printer, arg)
835 void (*printer) __P((void *, char *, ...));
842 if (plen < CHAP_HEADERLEN)
847 if (len < CHAP_HEADERLEN || len > plen)
850 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
851 printer(arg, " %s", ChapCodenames[code-1]);
853 printer(arg, " code=0x%x", code);
854 printer(arg, " id=0x%x", id);
855 len -= CHAP_HEADERLEN;
865 nlen = len - clen - 1;
867 for (; clen > 0; --clen) {
869 printer(arg, "%.2x", x);
871 printer(arg, ">, name = ");
872 print_string((char *)p, nlen, printer, arg);
877 print_string((char *)p, len, printer, arg);
880 for (clen = len; clen > 0; --clen) {
882 printer(arg, " %.2x", x);
886 return len + CHAP_HEADERLEN;