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.11 1996/04/04 03:35:58 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", NULL, NULL
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;
434 char secret[MAXSECRETLEN];
436 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
438 if (cstate->serverstate == CHAPSS_CLOSED ||
439 cstate->serverstate == CHAPSS_PENDING) {
440 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
441 cstate->serverstate));
445 if (id != cstate->chal_id)
446 return; /* doesn't match ID of last challenge */
449 * If we have received a duplicate or bogus Response,
450 * we have to send the same answer (Success/Failure)
451 * as we did for the first Response we saw.
453 if (cstate->serverstate == CHAPSS_OPEN) {
454 ChapSendStatus(cstate, CHAP_SUCCESS);
457 if (cstate->serverstate == CHAPSS_BADAUTH) {
458 ChapSendStatus(cstate, CHAP_FAILURE);
463 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
466 GETCHAR(remmd_len, inp); /* get length of MD */
467 remmd = inp; /* get pointer to MD */
468 INCPTR(remmd_len, inp);
470 len -= sizeof (u_char) + remmd_len;
472 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
476 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
478 if (len >= sizeof(rhostname))
479 len = sizeof(rhostname) - 1;
480 BCOPY(inp, rhostname, len);
481 rhostname[len] = '\000';
483 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
487 * Get secret for authenticating them with us,
488 * do the hash ourselves, and compare the result.
491 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
492 secret, &secret_len, 1)) {
493 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
497 /* generate MD based on negotiated type */
498 switch (cstate->chal_type) {
500 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
501 if (remmd_len != MD5_SIGNATURE_SIZE)
502 break; /* it's not even the right length */
504 MD5Update(&mdContext, &cstate->chal_id, 1);
505 MD5Update(&mdContext, secret, secret_len);
506 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
507 MD5Final(&mdContext);
509 /* compare local and remote MDs and send the appropriate status */
510 if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
511 code = CHAP_SUCCESS; /* they are the same! */
515 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
519 ChapSendStatus(cstate, code);
521 if (code == CHAP_SUCCESS) {
522 old_state = cstate->serverstate;
523 cstate->serverstate = CHAPSS_OPEN;
524 if (old_state == CHAPSS_INITIAL_CHAL) {
525 auth_peer_success(cstate->unit, PPP_CHAP);
527 if (cstate->chal_interval != 0)
528 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
531 syslog(LOG_ERR, "CHAP peer authentication failed");
532 cstate->serverstate = CHAPSS_BADAUTH;
533 auth_peer_fail(cstate->unit, PPP_CHAP);
538 * ChapReceiveSuccess - Receive Success
541 ChapReceiveSuccess(cstate, inp, id, len)
548 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
550 if (cstate->clientstate == CHAPCS_OPEN)
551 /* presumably an answer to a duplicate response */
554 if (cstate->clientstate != CHAPCS_RESPONSE) {
555 /* don't know what this is */
556 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
557 cstate->clientstate));
561 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
569 cstate->clientstate = CHAPCS_OPEN;
571 auth_withpeer_success(cstate->unit, PPP_CHAP);
576 * ChapReceiveFailure - Receive failure.
579 ChapReceiveFailure(cstate, inp, id, len)
585 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
587 if (cstate->clientstate != CHAPCS_RESPONSE) {
588 /* don't know what this is */
589 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
590 cstate->clientstate));
594 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
602 syslog(LOG_ERR, "CHAP authentication failed");
603 auth_withpeer_fail(cstate->unit, PPP_CHAP);
608 * ChapSendChallenge - Send an Authenticate challenge.
611 ChapSendChallenge(cstate)
615 int chal_len, name_len;
618 chal_len = cstate->chal_len;
619 name_len = strlen(cstate->chal_name);
620 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
621 outp = outpacket_buf;
623 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
625 PUTCHAR(CHAP_CHALLENGE, outp);
626 PUTCHAR(cstate->chal_id, outp);
627 PUTSHORT(outlen, outp);
629 PUTCHAR(chal_len, outp); /* put length of challenge */
630 BCOPY(cstate->challenge, outp, chal_len);
631 INCPTR(chal_len, outp);
633 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
635 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
637 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
639 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
640 ++cstate->chal_transmits;
645 * ChapSendStatus - Send a status response (ack or nak).
648 ChapSendStatus(cstate, code)
656 if (code == CHAP_SUCCESS)
657 sprintf(msg, "Welcome to %s.", hostname);
659 sprintf(msg, "I don't like you. Go 'way.");
660 msglen = strlen(msg);
662 outlen = CHAP_HEADERLEN + msglen;
663 outp = outpacket_buf;
665 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
668 PUTCHAR(cstate->chal_id, outp);
669 PUTSHORT(outlen, outp);
670 BCOPY(msg, outp, msglen);
671 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
673 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
678 * ChapGenChallenge is used to generate a pseudo-random challenge string of
679 * a pseudo-random length between min_len and max_len. The challenge
680 * string and its length are stored in *cstate, and various other fields of
681 * *cstate are initialized.
685 ChapGenChallenge(cstate)
689 u_char *ptr = cstate->challenge;
692 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
693 MAX_CHALLENGE_LENGTH */
694 chal_len = (unsigned) ((drand48() *
695 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
696 MIN_CHALLENGE_LENGTH);
697 cstate->chal_len = chal_len;
698 cstate->chal_id = ++cstate->id;
699 cstate->chal_transmits = 0;
701 /* generate a random string */
702 for (i = 0; i < chal_len; i++ )
703 *ptr++ = (char) (drand48() * 0xff);
707 * ChapSendResponse - send a response packet with values as specified
712 ChapSendResponse(cstate)
716 int outlen, md_len, name_len;
718 md_len = cstate->resp_length;
719 name_len = strlen(cstate->resp_name);
720 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
721 outp = outpacket_buf;
723 MAKEHEADER(outp, PPP_CHAP);
725 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
726 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
727 PUTSHORT(outlen, outp); /* packet length */
729 PUTCHAR(md_len, outp); /* length of MD */
730 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
731 INCPTR(md_len, outp);
733 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
735 /* send the packet */
736 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
738 cstate->clientstate = CHAPCS_RESPONSE;
739 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
740 ++cstate->resp_transmits;
744 * ChapPrintPkt - print the contents of a CHAP packet.
746 char *ChapCodenames[] = {
747 "Challenge", "Response", "Success", "Failure"
751 ChapPrintPkt(p, plen, printer, arg)
754 void (*printer) __P((void *, char *, ...));
761 if (plen < CHAP_HEADERLEN)
766 if (len < CHAP_HEADERLEN || len > plen)
769 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
770 printer(arg, " %s", ChapCodenames[code-1]);
772 printer(arg, " code=0x%x", code);
773 printer(arg, " id=0x%x", id);
774 len -= CHAP_HEADERLEN;
784 nlen = len - clen - 1;
786 for (; clen > 0; --clen) {
788 printer(arg, "%.2x", x);
790 printer(arg, ">, name = ");
791 print_string((char *)p, nlen, printer, arg);
796 print_string((char *)p, len, printer, arg);
799 for (clen = len; clen > 0; --clen) {
801 printer(arg, " %.2x", x);
805 return len + CHAP_HEADERLEN;