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.4 1994/09/21 06:47:37 paulus Exp $";
30 #include <sys/types.h>
38 chap_state chap[N_PPP]; /* CHAP state; one for each unit */
40 static void ChapChallengeTimeout __P((caddr_t));
41 static void ChapResponseTimeout __P((caddr_t));
42 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
43 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
44 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
45 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
46 static void ChapSendStatus __P((chap_state *, int));
47 static void ChapSendChallenge __P((chap_state *));
48 static void ChapSendResponse __P((chap_state *));
49 static void ChapGenChallenge __P((chap_state *));
51 extern double drand48 __P((void));
52 extern void srand48 __P((long));
55 * ChapInit - Initialize a CHAP unit.
61 chap_state *cstate = &chap[unit];
63 BZERO(cstate, sizeof(*cstate));
65 cstate->clientstate = CHAPCS_INITIAL;
66 cstate->serverstate = CHAPSS_INITIAL;
67 cstate->timeouttime = CHAP_DEFTIMEOUT;
68 cstate->max_transmits = CHAP_DEFTRANSMITS;
69 srand48((long) time(NULL)); /* joggle random number generator */
74 * ChapAuthWithPeer - Authenticate us with our peer (start client).
78 ChapAuthWithPeer(unit, our_name, digest)
83 chap_state *cstate = &chap[unit];
85 cstate->resp_name = our_name;
86 cstate->resp_type = digest;
88 if (cstate->clientstate == CHAPCS_INITIAL ||
89 cstate->clientstate == CHAPCS_PENDING) {
90 /* lower layer isn't up - wait until later */
91 cstate->clientstate = CHAPCS_PENDING;
96 * We get here as a result of LCP coming up.
97 * So even if CHAP was open before, we will
98 * have to re-authenticate ourselves.
100 cstate->clientstate = CHAPCS_LISTEN;
105 * ChapAuthPeer - Authenticate our peer (start server).
108 ChapAuthPeer(unit, our_name, digest)
113 chap_state *cstate = &chap[unit];
115 cstate->chal_name = our_name;
116 cstate->chal_type = digest;
118 if (cstate->serverstate == CHAPSS_INITIAL ||
119 cstate->serverstate == CHAPSS_PENDING) {
120 /* lower layer isn't up - wait until later */
121 cstate->serverstate = CHAPSS_PENDING;
125 ChapGenChallenge(cstate);
126 ChapSendChallenge(cstate); /* crank it up dude! */
127 cstate->serverstate = CHAPSS_INITIAL_CHAL;
132 * ChapChallengeTimeout - Timeout expired on sending challenge.
135 ChapChallengeTimeout(arg)
138 chap_state *cstate = (chap_state *) arg;
140 /* if we aren't sending challenges, don't worry. then again we */
141 /* probably shouldn't be here either */
142 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
143 cstate->serverstate != CHAPSS_RECHALLENGE)
146 if (cstate->chal_transmits >= cstate->max_transmits) {
147 /* give up on peer */
148 syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
149 cstate->serverstate = CHAPSS_BADAUTH;
150 auth_peer_fail(cstate->unit, PPP_CHAP);
154 ChapSendChallenge(cstate); /* Re-send challenge */
159 * ChapResponseTimeout - Timeout expired on sending response.
162 ChapResponseTimeout(arg)
165 chap_state *cstate = (chap_state *) arg;
167 /* if we aren't sending a response, don't worry. */
168 if (cstate->clientstate != CHAPCS_RESPONSE)
171 ChapSendResponse(cstate); /* re-send response */
176 * ChapRechallenge - Time to challenge the peer again.
182 chap_state *cstate = (chap_state *) arg;
184 /* if we aren't sending a response, don't worry. */
185 if (cstate->serverstate != CHAPSS_OPEN)
188 ChapGenChallenge(cstate);
189 ChapSendChallenge(cstate);
190 cstate->serverstate = CHAPSS_RECHALLENGE;
192 if (cstate->chal_interval != 0)
193 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
198 * ChapLowerUp - The lower layer is up.
200 * Start up if we have pending requests.
206 chap_state *cstate = &chap[unit];
208 if (cstate->clientstate == CHAPCS_INITIAL)
209 cstate->clientstate = CHAPCS_CLOSED;
210 else if (cstate->clientstate == CHAPCS_PENDING)
211 cstate->clientstate = CHAPCS_LISTEN;
213 if (cstate->serverstate == CHAPSS_INITIAL)
214 cstate->serverstate = CHAPSS_CLOSED;
215 else if (cstate->serverstate == CHAPSS_PENDING) {
216 ChapGenChallenge(cstate);
217 ChapSendChallenge(cstate);
218 cstate->serverstate = CHAPSS_INITIAL_CHAL;
224 * ChapLowerDown - The lower layer is down.
226 * Cancel all timeouts.
232 chap_state *cstate = &chap[unit];
234 /* Timeout(s) pending? Cancel if so. */
235 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
236 cstate->serverstate == CHAPSS_RECHALLENGE)
237 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
238 else if (cstate->serverstate == CHAPSS_OPEN
239 && cstate->chal_interval != 0)
240 UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
241 if (cstate->clientstate == CHAPCS_RESPONSE)
242 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
244 cstate->clientstate = CHAPCS_INITIAL;
245 cstate->serverstate = CHAPSS_INITIAL;
250 * ChapProtocolReject - Peer doesn't grok CHAP.
253 ChapProtocolReject(unit)
256 chap_state *cstate = &chap[unit];
258 if (cstate->serverstate != CHAPSS_INITIAL &&
259 cstate->serverstate != CHAPSS_CLOSED)
260 auth_peer_fail(unit, PPP_CHAP);
261 if (cstate->clientstate != CHAPCS_INITIAL &&
262 cstate->clientstate != CHAPCS_CLOSED)
263 auth_withpeer_fail(unit, PPP_CHAP);
264 ChapLowerDown(unit); /* shutdown chap */
269 * ChapInput - Input CHAP packet.
272 ChapInput(unit, inpacket, packet_len)
277 chap_state *cstate = &chap[unit];
283 * Parse header (code, id and length).
284 * If packet too short, drop it.
287 if (packet_len < CHAP_HEADERLEN) {
288 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
294 if (len < CHAP_HEADERLEN) {
295 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
298 if (len > packet_len) {
299 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
302 len -= CHAP_HEADERLEN;
305 * Action depends on code (as in fact it usually does :-).
309 ChapReceiveChallenge(cstate, inp, id, len);
313 ChapReceiveResponse(cstate, inp, id, len);
317 ChapReceiveFailure(cstate, inp, id, len);
321 ChapReceiveSuccess(cstate, inp, id, len);
324 default: /* Need code reject? */
325 syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
332 * ChapReceiveChallenge - Receive Challenge and send Response.
335 ChapReceiveChallenge(cstate, inp, id, len)
344 char secret[MAXSECRETLEN];
348 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
349 if (cstate->clientstate == CHAPCS_CLOSED ||
350 cstate->clientstate == CHAPCS_PENDING) {
351 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
352 cstate->clientstate));
357 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
361 GETCHAR(rchallenge_len, inp);
362 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
364 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
368 INCPTR(rchallenge_len, inp);
370 if (len >= sizeof(rhostname))
371 len = sizeof(rhostname) - 1;
372 BCOPY(inp, rhostname, len);
373 rhostname[len] = '\000';
375 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field: %s",
378 /* get secret for authenticating ourselves with the specified host */
379 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
380 secret, &secret_len, 0)) {
381 secret_len = 0; /* assume null secret if can't find one */
382 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
386 /* cancel response send timeout if necessary */
387 if (cstate->clientstate == CHAPCS_RESPONSE)
388 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
390 cstate->resp_id = id;
391 cstate->resp_transmits = 0;
393 /* generate MD based on negotiated type */
394 switch (cstate->resp_type) {
396 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
398 MD5Update(&mdContext, &cstate->resp_id, 1);
399 MD5Update(&mdContext, secret, secret_len);
400 MD5Update(&mdContext, rchallenge, rchallenge_len);
401 MD5Final(&mdContext);
402 BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
403 cstate->resp_length = MD5_SIGNATURE_SIZE;
407 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
411 ChapSendResponse(cstate);
416 * ChapReceiveResponse - Receive and process response.
419 ChapReceiveResponse(cstate, inp, id, len)
425 u_char *remmd, remmd_len;
426 int secret_len, old_state;
432 char secret[MAXSECRETLEN];
434 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
436 if (cstate->serverstate == CHAPSS_CLOSED ||
437 cstate->serverstate == CHAPSS_PENDING) {
438 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
439 cstate->serverstate));
443 if (id != cstate->chal_id)
444 return; /* doesn't match ID of last challenge */
447 * If we have received a duplicate or bogus Response,
448 * we have to send the same answer (Success/Failure)
449 * as we did for the first Response we saw.
451 if (cstate->serverstate == CHAPSS_OPEN) {
452 ChapSendStatus(cstate, CHAP_SUCCESS);
455 if (cstate->serverstate == CHAPSS_BADAUTH) {
456 ChapSendStatus(cstate, CHAP_FAILURE);
461 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
464 GETCHAR(remmd_len, inp); /* get length of MD */
465 remmd = inp; /* get pointer to MD */
466 INCPTR(remmd_len, inp);
468 len -= sizeof (u_char) + remmd_len;
470 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
474 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
476 if (len >= sizeof(rhostname))
477 len = sizeof(rhostname) - 1;
478 BCOPY(inp, rhostname, len);
479 rhostname[len] = '\000';
481 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
485 * Get secret for authenticating them with us,
486 * do the hash ourselves, and compare the result.
489 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
490 secret, &secret_len, 1)) {
491 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
495 /* generate MD based on negotiated type */
496 switch (cstate->chal_type) {
498 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
499 if (remmd_len != MD5_SIGNATURE_SIZE)
500 break; /* it's not even the right length */
502 MD5Update(&mdContext, &cstate->chal_id, 1);
503 MD5Update(&mdContext, secret, secret_len);
504 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
505 MD5Final(&mdContext);
507 /* compare local and remote MDs and send the appropriate status */
508 if (bcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
509 code = CHAP_SUCCESS; /* they are the same! */
513 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
517 ChapSendStatus(cstate, code);
519 if (code == CHAP_SUCCESS) {
520 old_state = cstate->serverstate;
521 cstate->serverstate = CHAPSS_OPEN;
522 if (old_state == CHAPSS_INITIAL_CHAL) {
523 auth_peer_success(cstate->unit, PPP_CHAP);
525 if (cstate->chal_interval != 0)
526 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
529 syslog(LOG_ERR, "CHAP peer authentication failed");
530 cstate->serverstate = CHAPSS_BADAUTH;
531 auth_peer_fail(cstate->unit, PPP_CHAP);
536 * ChapReceiveSuccess - Receive Success
539 ChapReceiveSuccess(cstate, inp, id, len)
546 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
548 if (cstate->clientstate == CHAPCS_OPEN)
549 /* presumably an answer to a duplicate response */
552 if (cstate->clientstate != CHAPCS_RESPONSE) {
553 /* don't know what this is */
554 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
555 cstate->clientstate));
559 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
567 cstate->clientstate = CHAPCS_OPEN;
569 auth_withpeer_success(cstate->unit, PPP_CHAP);
574 * ChapReceiveFailure - Receive failure.
577 ChapReceiveFailure(cstate, inp, id, len)
586 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
588 if (cstate->clientstate != CHAPCS_RESPONSE) {
589 /* don't know what this is */
590 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
591 cstate->clientstate));
595 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
603 syslog(LOG_ERR, "CHAP authentication failed");
604 auth_withpeer_fail(cstate->unit, PPP_CHAP);
609 * ChapSendChallenge - Send an Authenticate challenge.
612 ChapSendChallenge(cstate)
616 int chal_len, name_len;
619 chal_len = cstate->chal_len;
620 name_len = strlen(cstate->chal_name);
621 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
622 outp = outpacket_buf;
624 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
626 PUTCHAR(CHAP_CHALLENGE, outp);
627 PUTCHAR(cstate->chal_id, outp);
628 PUTSHORT(outlen, outp);
630 PUTCHAR(chal_len, outp); /* put length of challenge */
631 BCOPY(cstate->challenge, outp, chal_len);
632 INCPTR(chal_len, outp);
634 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
636 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
638 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
640 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
641 ++cstate->chal_transmits;
646 * ChapSendStatus - Send a status response (ack or nak).
649 ChapSendStatus(cstate, code)
657 if (code == CHAP_SUCCESS)
658 sprintf(msg, "Welcome to %s.", hostname);
660 sprintf(msg, "I don't like you. Go 'way.");
661 msglen = strlen(msg);
663 outlen = CHAP_HEADERLEN + msglen;
664 outp = outpacket_buf;
666 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
669 PUTCHAR(cstate->chal_id, outp);
670 PUTSHORT(outlen, outp);
671 BCOPY(msg, outp, msglen);
672 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
674 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
679 * ChapGenChallenge is used to generate a pseudo-random challenge string of
680 * a pseudo-random length between min_len and max_len. The challenge
681 * string and its length are stored in *cstate, and various other fields of
682 * *cstate are initialized.
686 ChapGenChallenge(cstate)
690 u_char *ptr = cstate->challenge;
693 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
694 MAX_CHALLENGE_LENGTH */
695 chal_len = (unsigned) ((drand48() *
696 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
697 MIN_CHALLENGE_LENGTH);
698 cstate->chal_len = chal_len;
699 cstate->chal_id = ++cstate->id;
700 cstate->chal_transmits = 0;
702 /* generate a random string */
703 for (i = 0; i < chal_len; i++ )
704 *ptr++ = (char) (drand48() * 0xff);
708 * ChapSendResponse - send a response packet with values as specified
713 ChapSendResponse(cstate)
717 int outlen, md_len, name_len;
719 md_len = cstate->resp_length;
720 name_len = strlen(cstate->resp_name);
721 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
722 outp = outpacket_buf;
724 MAKEHEADER(outp, PPP_CHAP);
726 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
727 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
728 PUTSHORT(outlen, outp); /* packet length */
730 PUTCHAR(md_len, outp); /* length of MD */
731 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
732 INCPTR(md_len, outp);
734 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
736 /* send the packet */
737 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
739 cstate->clientstate = CHAPCS_RESPONSE;
740 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
741 ++cstate->resp_transmits;
745 * ChapPrintPkt - print the contents of a CHAP packet.
747 char *ChapCodenames[] = {
748 "Challenge", "Response", "Success", "Failure"
752 ChapPrintPkt(p, plen, printer, arg)
755 void (*printer) __P((void *, char *, ...));
762 if (plen < CHAP_HEADERLEN)
767 if (len < CHAP_HEADERLEN || len > plen)
770 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
771 printer(arg, " %s", ChapCodenames[code-1]);
773 printer(arg, " code=0x%x", code);
774 printer(arg, " id=0x%x", id);
775 len -= CHAP_HEADERLEN;
785 nlen = len - clen - 1;
787 for (; clen > 0; --clen) {
789 printer(arg, "%.2x", x);
791 printer(arg, ">, name = ");
792 print_string((char *)p, nlen, printer, arg);
797 print_string((char *)p, len, printer, arg);
800 for (clen = len; clen > 0; --clen) {
802 printer(arg, " %.2x", x);
806 return len + CHAP_HEADERLEN;
813 return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
816 void srand48(seedval)