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.13 1996/07/01 01:12:09 paulus Exp $";
46 #include <sys/types.h>
58 * Protocol entry points.
60 static void ChapInit __P((int));
61 static void ChapLowerUp __P((int));
62 static void ChapLowerDown __P((int));
63 static void ChapInput __P((int, u_char *, int));
64 static void ChapProtocolReject __P((int));
65 static int ChapPrintPkt __P((u_char *, int,
66 void (*) __P((void *, char *, ...)), void *));
68 struct protent chap_protent = {
86 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
88 static void ChapChallengeTimeout __P((caddr_t));
89 static void ChapResponseTimeout __P((caddr_t));
90 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
91 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
92 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
93 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
94 static void ChapSendStatus __P((chap_state *, int));
95 static void ChapSendChallenge __P((chap_state *));
96 static void ChapSendResponse __P((chap_state *));
97 static void ChapGenChallenge __P((chap_state *));
99 extern double drand48 __P((void));
100 extern void srand48 __P((long));
103 * ChapInit - Initialize a CHAP unit.
109 chap_state *cstate = &chap[unit];
111 BZERO(cstate, sizeof(*cstate));
113 cstate->clientstate = CHAPCS_INITIAL;
114 cstate->serverstate = CHAPSS_INITIAL;
115 cstate->timeouttime = CHAP_DEFTIMEOUT;
116 cstate->max_transmits = CHAP_DEFTRANSMITS;
117 /* random number generator is initialized in magic_init */
122 * ChapAuthWithPeer - Authenticate us with our peer (start client).
126 ChapAuthWithPeer(unit, our_name, digest)
131 chap_state *cstate = &chap[unit];
133 cstate->resp_name = our_name;
134 cstate->resp_type = digest;
136 if (cstate->clientstate == CHAPCS_INITIAL ||
137 cstate->clientstate == CHAPCS_PENDING) {
138 /* lower layer isn't up - wait until later */
139 cstate->clientstate = CHAPCS_PENDING;
144 * We get here as a result of LCP coming up.
145 * So even if CHAP was open before, we will
146 * have to re-authenticate ourselves.
148 cstate->clientstate = CHAPCS_LISTEN;
153 * ChapAuthPeer - Authenticate our peer (start server).
156 ChapAuthPeer(unit, our_name, digest)
161 chap_state *cstate = &chap[unit];
163 cstate->chal_name = our_name;
164 cstate->chal_type = digest;
166 if (cstate->serverstate == CHAPSS_INITIAL ||
167 cstate->serverstate == CHAPSS_PENDING) {
168 /* lower layer isn't up - wait until later */
169 cstate->serverstate = CHAPSS_PENDING;
173 ChapGenChallenge(cstate);
174 ChapSendChallenge(cstate); /* crank it up dude! */
175 cstate->serverstate = CHAPSS_INITIAL_CHAL;
180 * ChapChallengeTimeout - Timeout expired on sending challenge.
183 ChapChallengeTimeout(arg)
186 chap_state *cstate = (chap_state *) arg;
188 /* if we aren't sending challenges, don't worry. then again we */
189 /* probably shouldn't be here either */
190 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
191 cstate->serverstate != CHAPSS_RECHALLENGE)
194 if (cstate->chal_transmits >= cstate->max_transmits) {
195 /* give up on peer */
196 syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
197 cstate->serverstate = CHAPSS_BADAUTH;
198 auth_peer_fail(cstate->unit, PPP_CHAP);
202 ChapSendChallenge(cstate); /* Re-send challenge */
207 * ChapResponseTimeout - Timeout expired on sending response.
210 ChapResponseTimeout(arg)
213 chap_state *cstate = (chap_state *) arg;
215 /* if we aren't sending a response, don't worry. */
216 if (cstate->clientstate != CHAPCS_RESPONSE)
219 ChapSendResponse(cstate); /* re-send response */
224 * ChapRechallenge - Time to challenge the peer again.
230 chap_state *cstate = (chap_state *) arg;
232 /* if we aren't sending a response, don't worry. */
233 if (cstate->serverstate != CHAPSS_OPEN)
236 ChapGenChallenge(cstate);
237 ChapSendChallenge(cstate);
238 cstate->serverstate = CHAPSS_RECHALLENGE;
243 * ChapLowerUp - The lower layer is up.
245 * Start up if we have pending requests.
251 chap_state *cstate = &chap[unit];
253 if (cstate->clientstate == CHAPCS_INITIAL)
254 cstate->clientstate = CHAPCS_CLOSED;
255 else if (cstate->clientstate == CHAPCS_PENDING)
256 cstate->clientstate = CHAPCS_LISTEN;
258 if (cstate->serverstate == CHAPSS_INITIAL)
259 cstate->serverstate = CHAPSS_CLOSED;
260 else if (cstate->serverstate == CHAPSS_PENDING) {
261 ChapGenChallenge(cstate);
262 ChapSendChallenge(cstate);
263 cstate->serverstate = CHAPSS_INITIAL_CHAL;
269 * ChapLowerDown - The lower layer is down.
271 * Cancel all timeouts.
277 chap_state *cstate = &chap[unit];
279 /* Timeout(s) pending? Cancel if so. */
280 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
281 cstate->serverstate == CHAPSS_RECHALLENGE)
282 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
283 else if (cstate->serverstate == CHAPSS_OPEN
284 && cstate->chal_interval != 0)
285 UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
286 if (cstate->clientstate == CHAPCS_RESPONSE)
287 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
289 cstate->clientstate = CHAPCS_INITIAL;
290 cstate->serverstate = CHAPSS_INITIAL;
295 * ChapProtocolReject - Peer doesn't grok CHAP.
298 ChapProtocolReject(unit)
301 chap_state *cstate = &chap[unit];
303 if (cstate->serverstate != CHAPSS_INITIAL &&
304 cstate->serverstate != CHAPSS_CLOSED)
305 auth_peer_fail(unit, PPP_CHAP);
306 if (cstate->clientstate != CHAPCS_INITIAL &&
307 cstate->clientstate != CHAPCS_CLOSED)
308 auth_withpeer_fail(unit, PPP_CHAP);
309 ChapLowerDown(unit); /* shutdown chap */
314 * ChapInput - Input CHAP packet.
317 ChapInput(unit, inpacket, packet_len)
322 chap_state *cstate = &chap[unit];
328 * Parse header (code, id and length).
329 * If packet too short, drop it.
332 if (packet_len < CHAP_HEADERLEN) {
333 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
339 if (len < CHAP_HEADERLEN) {
340 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
343 if (len > packet_len) {
344 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
347 len -= CHAP_HEADERLEN;
350 * Action depends on code (as in fact it usually does :-).
354 ChapReceiveChallenge(cstate, inp, id, len);
358 ChapReceiveResponse(cstate, inp, id, len);
362 ChapReceiveFailure(cstate, inp, id, len);
366 ChapReceiveSuccess(cstate, inp, id, len);
369 default: /* Need code reject? */
370 syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
377 * ChapReceiveChallenge - Receive Challenge and send Response.
380 ChapReceiveChallenge(cstate, inp, id, len)
389 char secret[MAXSECRETLEN];
393 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
394 if (cstate->clientstate == CHAPCS_CLOSED ||
395 cstate->clientstate == CHAPCS_PENDING) {
396 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
397 cstate->clientstate));
402 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
406 GETCHAR(rchallenge_len, inp);
407 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
409 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
413 INCPTR(rchallenge_len, inp);
415 if (len >= sizeof(rhostname))
416 len = sizeof(rhostname) - 1;
417 BCOPY(inp, rhostname, len);
418 rhostname[len] = '\000';
420 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field '%s'",
423 /* Microsoft doesn't send their name back in the PPP packet */
424 if (rhostname[0] == 0 && cstate->resp_type == CHAP_MICROSOFT) {
425 strcpy(rhostname, remote_name);
426 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: using '%s' as remote name",
430 /* get secret for authenticating ourselves with the specified host */
431 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
432 secret, &secret_len, 0)) {
433 secret_len = 0; /* assume null secret if can't find one */
434 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
438 /* cancel response send timeout if necessary */
439 if (cstate->clientstate == CHAPCS_RESPONSE)
440 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
442 cstate->resp_id = id;
443 cstate->resp_transmits = 0;
445 /* generate MD based on negotiated type */
446 switch (cstate->resp_type) {
448 case CHAP_DIGEST_MD5:
450 MD5Update(&mdContext, &cstate->resp_id, 1);
451 MD5Update(&mdContext, secret, secret_len);
452 MD5Update(&mdContext, rchallenge, rchallenge_len);
453 MD5Final(&mdContext);
454 BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
455 cstate->resp_length = MD5_SIGNATURE_SIZE;
460 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
465 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
469 BZERO(secret, sizeof(secret));
470 ChapSendResponse(cstate);
475 * ChapReceiveResponse - Receive and process response.
478 ChapReceiveResponse(cstate, inp, id, len)
484 u_char *remmd, remmd_len;
485 int secret_len, old_state;
489 char secret[MAXSECRETLEN];
491 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
493 if (cstate->serverstate == CHAPSS_CLOSED ||
494 cstate->serverstate == CHAPSS_PENDING) {
495 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
496 cstate->serverstate));
500 if (id != cstate->chal_id)
501 return; /* doesn't match ID of last challenge */
504 * If we have received a duplicate or bogus Response,
505 * we have to send the same answer (Success/Failure)
506 * as we did for the first Response we saw.
508 if (cstate->serverstate == CHAPSS_OPEN) {
509 ChapSendStatus(cstate, CHAP_SUCCESS);
512 if (cstate->serverstate == CHAPSS_BADAUTH) {
513 ChapSendStatus(cstate, CHAP_FAILURE);
518 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
521 GETCHAR(remmd_len, inp); /* get length of MD */
522 remmd = inp; /* get pointer to MD */
523 INCPTR(remmd_len, inp);
525 len -= sizeof (u_char) + remmd_len;
527 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
531 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
533 if (len >= sizeof(rhostname))
534 len = sizeof(rhostname) - 1;
535 BCOPY(inp, rhostname, len);
536 rhostname[len] = '\000';
538 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
542 * Get secret for authenticating them with us,
543 * do the hash ourselves, and compare the result.
546 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
547 secret, &secret_len, 1)) {
548 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
552 /* generate MD based on negotiated type */
553 switch (cstate->chal_type) {
555 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
556 if (remmd_len != MD5_SIGNATURE_SIZE)
557 break; /* it's not even the right length */
559 MD5Update(&mdContext, &cstate->chal_id, 1);
560 MD5Update(&mdContext, secret, secret_len);
561 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
562 MD5Final(&mdContext);
564 /* compare local and remote MDs and send the appropriate status */
565 if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
566 code = CHAP_SUCCESS; /* they are the same! */
570 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
574 BZERO(secret, sizeof(secret));
575 ChapSendStatus(cstate, code);
577 if (code == CHAP_SUCCESS) {
578 old_state = cstate->serverstate;
579 cstate->serverstate = CHAPSS_OPEN;
580 if (old_state == CHAPSS_INITIAL_CHAL) {
581 auth_peer_success(cstate->unit, PPP_CHAP, rhostname, len);
583 if (cstate->chal_interval != 0)
584 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
587 syslog(LOG_ERR, "CHAP peer authentication failed");
588 cstate->serverstate = CHAPSS_BADAUTH;
589 auth_peer_fail(cstate->unit, PPP_CHAP);
594 * ChapReceiveSuccess - Receive Success
597 ChapReceiveSuccess(cstate, inp, id, len)
604 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
606 if (cstate->clientstate == CHAPCS_OPEN)
607 /* presumably an answer to a duplicate response */
610 if (cstate->clientstate != CHAPCS_RESPONSE) {
611 /* don't know what this is */
612 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
613 cstate->clientstate));
617 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
625 cstate->clientstate = CHAPCS_OPEN;
627 auth_withpeer_success(cstate->unit, PPP_CHAP);
632 * ChapReceiveFailure - Receive failure.
635 ChapReceiveFailure(cstate, inp, id, len)
641 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
643 if (cstate->clientstate != CHAPCS_RESPONSE) {
644 /* don't know what this is */
645 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
646 cstate->clientstate));
650 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
658 syslog(LOG_ERR, "CHAP authentication failed");
659 auth_withpeer_fail(cstate->unit, PPP_CHAP);
664 * ChapSendChallenge - Send an Authenticate challenge.
667 ChapSendChallenge(cstate)
671 int chal_len, name_len;
674 chal_len = cstate->chal_len;
675 name_len = strlen(cstate->chal_name);
676 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
677 outp = outpacket_buf;
679 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
681 PUTCHAR(CHAP_CHALLENGE, outp);
682 PUTCHAR(cstate->chal_id, outp);
683 PUTSHORT(outlen, outp);
685 PUTCHAR(chal_len, outp); /* put length of challenge */
686 BCOPY(cstate->challenge, outp, chal_len);
687 INCPTR(chal_len, outp);
689 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
691 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
693 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
695 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
696 ++cstate->chal_transmits;
701 * ChapSendStatus - Send a status response (ack or nak).
704 ChapSendStatus(cstate, code)
712 if (code == CHAP_SUCCESS)
713 sprintf(msg, "Welcome to %s.", hostname);
715 sprintf(msg, "I don't like you. Go 'way.");
716 msglen = strlen(msg);
718 outlen = CHAP_HEADERLEN + msglen;
719 outp = outpacket_buf;
721 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
724 PUTCHAR(cstate->chal_id, outp);
725 PUTSHORT(outlen, outp);
726 BCOPY(msg, outp, msglen);
727 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
729 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
734 * ChapGenChallenge is used to generate a pseudo-random challenge string of
735 * a pseudo-random length between min_len and max_len. The challenge
736 * string and its length are stored in *cstate, and various other fields of
737 * *cstate are initialized.
741 ChapGenChallenge(cstate)
745 u_char *ptr = cstate->challenge;
748 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
749 MAX_CHALLENGE_LENGTH */
750 chal_len = (unsigned) ((drand48() *
751 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
752 MIN_CHALLENGE_LENGTH);
753 cstate->chal_len = chal_len;
754 cstate->chal_id = ++cstate->id;
755 cstate->chal_transmits = 0;
757 /* generate a random string */
758 for (i = 0; i < chal_len; i++ )
759 *ptr++ = (char) (drand48() * 0xff);
763 * ChapSendResponse - send a response packet with values as specified
768 ChapSendResponse(cstate)
772 int outlen, md_len, name_len;
774 md_len = cstate->resp_length;
775 name_len = strlen(cstate->resp_name);
776 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
777 outp = outpacket_buf;
779 MAKEHEADER(outp, PPP_CHAP);
781 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
782 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
783 PUTSHORT(outlen, outp); /* packet length */
785 PUTCHAR(md_len, outp); /* length of MD */
786 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
787 INCPTR(md_len, outp);
789 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
791 /* send the packet */
792 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
794 cstate->clientstate = CHAPCS_RESPONSE;
795 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
796 ++cstate->resp_transmits;
800 * ChapPrintPkt - print the contents of a CHAP packet.
802 static char *ChapCodenames[] = {
803 "Challenge", "Response", "Success", "Failure"
807 ChapPrintPkt(p, plen, printer, arg)
810 void (*printer) __P((void *, char *, ...));
817 if (plen < CHAP_HEADERLEN)
822 if (len < CHAP_HEADERLEN || len > plen)
825 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
826 printer(arg, " %s", ChapCodenames[code-1]);
828 printer(arg, " code=0x%x", code);
829 printer(arg, " id=0x%x", id);
830 len -= CHAP_HEADERLEN;
840 nlen = len - clen - 1;
842 for (; clen > 0; --clen) {
844 printer(arg, "%.2x", x);
846 printer(arg, ">, name = ");
847 print_string((char *)p, nlen, printer, arg);
852 print_string((char *)p, len, printer, arg);
855 for (clen = len; clen > 0; --clen) {
857 printer(arg, " %.2x", x);
861 return len + CHAP_HEADERLEN;