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.8 1995/07/04 12:32:26 paulus Exp $";
31 #include <sys/types.h>
39 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
41 static void ChapChallengeTimeout __P((caddr_t));
42 static void ChapResponseTimeout __P((caddr_t));
43 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
44 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
45 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
46 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
47 static void ChapSendStatus __P((chap_state *, int));
48 static void ChapSendChallenge __P((chap_state *));
49 static void ChapSendResponse __P((chap_state *));
50 static void ChapGenChallenge __P((chap_state *));
52 extern double drand48 __P((void));
53 extern void srand48 __P((long));
56 * ChapInit - Initialize a CHAP unit.
62 chap_state *cstate = &chap[unit];
64 BZERO(cstate, sizeof(*cstate));
66 cstate->clientstate = CHAPCS_INITIAL;
67 cstate->serverstate = CHAPSS_INITIAL;
68 cstate->timeouttime = CHAP_DEFTIMEOUT;
69 cstate->max_transmits = CHAP_DEFTRANSMITS;
70 /* random number generator is initialized in magic_init */
75 * ChapAuthWithPeer - Authenticate us with our peer (start client).
79 ChapAuthWithPeer(unit, our_name, digest)
84 chap_state *cstate = &chap[unit];
86 cstate->resp_name = our_name;
87 cstate->resp_type = digest;
89 if (cstate->clientstate == CHAPCS_INITIAL ||
90 cstate->clientstate == CHAPCS_PENDING) {
91 /* lower layer isn't up - wait until later */
92 cstate->clientstate = CHAPCS_PENDING;
97 * We get here as a result of LCP coming up.
98 * So even if CHAP was open before, we will
99 * have to re-authenticate ourselves.
101 cstate->clientstate = CHAPCS_LISTEN;
106 * ChapAuthPeer - Authenticate our peer (start server).
109 ChapAuthPeer(unit, our_name, digest)
114 chap_state *cstate = &chap[unit];
116 cstate->chal_name = our_name;
117 cstate->chal_type = digest;
119 if (cstate->serverstate == CHAPSS_INITIAL ||
120 cstate->serverstate == CHAPSS_PENDING) {
121 /* lower layer isn't up - wait until later */
122 cstate->serverstate = CHAPSS_PENDING;
126 ChapGenChallenge(cstate);
127 ChapSendChallenge(cstate); /* crank it up dude! */
128 cstate->serverstate = CHAPSS_INITIAL_CHAL;
133 * ChapChallengeTimeout - Timeout expired on sending challenge.
136 ChapChallengeTimeout(arg)
139 chap_state *cstate = (chap_state *) arg;
141 /* if we aren't sending challenges, don't worry. then again we */
142 /* probably shouldn't be here either */
143 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
144 cstate->serverstate != CHAPSS_RECHALLENGE)
147 if (cstate->chal_transmits >= cstate->max_transmits) {
148 /* give up on peer */
149 syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
150 cstate->serverstate = CHAPSS_BADAUTH;
151 auth_peer_fail(cstate->unit, PPP_CHAP);
155 ChapSendChallenge(cstate); /* Re-send challenge */
160 * ChapResponseTimeout - Timeout expired on sending response.
163 ChapResponseTimeout(arg)
166 chap_state *cstate = (chap_state *) arg;
168 /* if we aren't sending a response, don't worry. */
169 if (cstate->clientstate != CHAPCS_RESPONSE)
172 ChapSendResponse(cstate); /* re-send response */
177 * ChapRechallenge - Time to challenge the peer again.
183 chap_state *cstate = (chap_state *) arg;
185 /* if we aren't sending a response, don't worry. */
186 if (cstate->serverstate != CHAPSS_OPEN)
189 ChapGenChallenge(cstate);
190 ChapSendChallenge(cstate);
191 cstate->serverstate = CHAPSS_RECHALLENGE;
196 * ChapLowerUp - The lower layer is up.
198 * Start up if we have pending requests.
204 chap_state *cstate = &chap[unit];
206 if (cstate->clientstate == CHAPCS_INITIAL)
207 cstate->clientstate = CHAPCS_CLOSED;
208 else if (cstate->clientstate == CHAPCS_PENDING)
209 cstate->clientstate = CHAPCS_LISTEN;
211 if (cstate->serverstate == CHAPSS_INITIAL)
212 cstate->serverstate = CHAPSS_CLOSED;
213 else if (cstate->serverstate == CHAPSS_PENDING) {
214 ChapGenChallenge(cstate);
215 ChapSendChallenge(cstate);
216 cstate->serverstate = CHAPSS_INITIAL_CHAL;
222 * ChapLowerDown - The lower layer is down.
224 * Cancel all timeouts.
230 chap_state *cstate = &chap[unit];
232 /* Timeout(s) pending? Cancel if so. */
233 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
234 cstate->serverstate == CHAPSS_RECHALLENGE)
235 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
236 else if (cstate->serverstate == CHAPSS_OPEN
237 && cstate->chal_interval != 0)
238 UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
239 if (cstate->clientstate == CHAPCS_RESPONSE)
240 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
242 cstate->clientstate = CHAPCS_INITIAL;
243 cstate->serverstate = CHAPSS_INITIAL;
248 * ChapProtocolReject - Peer doesn't grok CHAP.
251 ChapProtocolReject(unit)
254 chap_state *cstate = &chap[unit];
256 if (cstate->serverstate != CHAPSS_INITIAL &&
257 cstate->serverstate != CHAPSS_CLOSED)
258 auth_peer_fail(unit, PPP_CHAP);
259 if (cstate->clientstate != CHAPCS_INITIAL &&
260 cstate->clientstate != CHAPCS_CLOSED)
261 auth_withpeer_fail(unit, PPP_CHAP);
262 ChapLowerDown(unit); /* shutdown chap */
267 * ChapInput - Input CHAP packet.
270 ChapInput(unit, inpacket, packet_len)
275 chap_state *cstate = &chap[unit];
281 * Parse header (code, id and length).
282 * If packet too short, drop it.
285 if (packet_len < CHAP_HEADERLEN) {
286 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
292 if (len < CHAP_HEADERLEN) {
293 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
296 if (len > packet_len) {
297 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
300 len -= CHAP_HEADERLEN;
303 * Action depends on code (as in fact it usually does :-).
307 ChapReceiveChallenge(cstate, inp, id, len);
311 ChapReceiveResponse(cstate, inp, id, len);
315 ChapReceiveFailure(cstate, inp, id, len);
319 ChapReceiveSuccess(cstate, inp, id, len);
322 default: /* Need code reject? */
323 syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
330 * ChapReceiveChallenge - Receive Challenge and send Response.
333 ChapReceiveChallenge(cstate, inp, id, len)
342 char secret[MAXSECRETLEN];
346 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
347 if (cstate->clientstate == CHAPCS_CLOSED ||
348 cstate->clientstate == CHAPCS_PENDING) {
349 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
350 cstate->clientstate));
355 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
359 GETCHAR(rchallenge_len, inp);
360 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
362 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
366 INCPTR(rchallenge_len, inp);
368 if (len >= sizeof(rhostname))
369 len = sizeof(rhostname) - 1;
370 BCOPY(inp, rhostname, len);
371 rhostname[len] = '\000';
373 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field: %s",
376 /* get secret for authenticating ourselves with the specified host */
377 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
378 secret, &secret_len, 0)) {
379 secret_len = 0; /* assume null secret if can't find one */
380 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
384 /* cancel response send timeout if necessary */
385 if (cstate->clientstate == CHAPCS_RESPONSE)
386 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
388 cstate->resp_id = id;
389 cstate->resp_transmits = 0;
391 /* generate MD based on negotiated type */
392 switch (cstate->resp_type) {
394 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
396 MD5Update(&mdContext, &cstate->resp_id, 1);
397 MD5Update(&mdContext, secret, secret_len);
398 MD5Update(&mdContext, rchallenge, rchallenge_len);
399 MD5Final(&mdContext);
400 BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
401 cstate->resp_length = MD5_SIGNATURE_SIZE;
405 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
409 ChapSendResponse(cstate);
414 * ChapReceiveResponse - Receive and process response.
417 ChapReceiveResponse(cstate, inp, id, len)
423 u_char *remmd, remmd_len;
424 int secret_len, old_state;
430 char secret[MAXSECRETLEN];
432 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
434 if (cstate->serverstate == CHAPSS_CLOSED ||
435 cstate->serverstate == CHAPSS_PENDING) {
436 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
437 cstate->serverstate));
441 if (id != cstate->chal_id)
442 return; /* doesn't match ID of last challenge */
445 * If we have received a duplicate or bogus Response,
446 * we have to send the same answer (Success/Failure)
447 * as we did for the first Response we saw.
449 if (cstate->serverstate == CHAPSS_OPEN) {
450 ChapSendStatus(cstate, CHAP_SUCCESS);
453 if (cstate->serverstate == CHAPSS_BADAUTH) {
454 ChapSendStatus(cstate, CHAP_FAILURE);
459 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
462 GETCHAR(remmd_len, inp); /* get length of MD */
463 remmd = inp; /* get pointer to MD */
464 INCPTR(remmd_len, inp);
466 len -= sizeof (u_char) + remmd_len;
468 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
472 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
474 if (len >= sizeof(rhostname))
475 len = sizeof(rhostname) - 1;
476 BCOPY(inp, rhostname, len);
477 rhostname[len] = '\000';
479 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
483 * Get secret for authenticating them with us,
484 * do the hash ourselves, and compare the result.
487 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
488 secret, &secret_len, 1)) {
489 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
493 /* generate MD based on negotiated type */
494 switch (cstate->chal_type) {
496 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
497 if (remmd_len != MD5_SIGNATURE_SIZE)
498 break; /* it's not even the right length */
500 MD5Update(&mdContext, &cstate->chal_id, 1);
501 MD5Update(&mdContext, secret, secret_len);
502 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
503 MD5Final(&mdContext);
505 /* compare local and remote MDs and send the appropriate status */
506 if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
507 code = CHAP_SUCCESS; /* they are the same! */
511 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
515 ChapSendStatus(cstate, code);
517 if (code == CHAP_SUCCESS) {
518 old_state = cstate->serverstate;
519 cstate->serverstate = CHAPSS_OPEN;
520 if (old_state == CHAPSS_INITIAL_CHAL) {
521 auth_peer_success(cstate->unit, PPP_CHAP);
523 if (cstate->chal_interval != 0)
524 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
527 syslog(LOG_ERR, "CHAP peer authentication failed");
528 cstate->serverstate = CHAPSS_BADAUTH;
529 auth_peer_fail(cstate->unit, PPP_CHAP);
534 * ChapReceiveSuccess - Receive Success
537 ChapReceiveSuccess(cstate, inp, id, len)
544 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
546 if (cstate->clientstate == CHAPCS_OPEN)
547 /* presumably an answer to a duplicate response */
550 if (cstate->clientstate != CHAPCS_RESPONSE) {
551 /* don't know what this is */
552 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
553 cstate->clientstate));
557 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
565 cstate->clientstate = CHAPCS_OPEN;
567 auth_withpeer_success(cstate->unit, PPP_CHAP);
572 * ChapReceiveFailure - Receive failure.
575 ChapReceiveFailure(cstate, inp, id, len)
584 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
586 if (cstate->clientstate != CHAPCS_RESPONSE) {
587 /* don't know what this is */
588 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
589 cstate->clientstate));
593 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
601 syslog(LOG_ERR, "CHAP authentication failed");
602 auth_withpeer_fail(cstate->unit, PPP_CHAP);
607 * ChapSendChallenge - Send an Authenticate challenge.
610 ChapSendChallenge(cstate)
614 int chal_len, name_len;
617 chal_len = cstate->chal_len;
618 name_len = strlen(cstate->chal_name);
619 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
620 outp = outpacket_buf;
622 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
624 PUTCHAR(CHAP_CHALLENGE, outp);
625 PUTCHAR(cstate->chal_id, outp);
626 PUTSHORT(outlen, outp);
628 PUTCHAR(chal_len, outp); /* put length of challenge */
629 BCOPY(cstate->challenge, outp, chal_len);
630 INCPTR(chal_len, outp);
632 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
634 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
636 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
638 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
639 ++cstate->chal_transmits;
644 * ChapSendStatus - Send a status response (ack or nak).
647 ChapSendStatus(cstate, code)
655 if (code == CHAP_SUCCESS)
656 sprintf(msg, "Welcome to %s.", hostname);
658 sprintf(msg, "I don't like you. Go 'way.");
659 msglen = strlen(msg);
661 outlen = CHAP_HEADERLEN + msglen;
662 outp = outpacket_buf;
664 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
667 PUTCHAR(cstate->chal_id, outp);
668 PUTSHORT(outlen, outp);
669 BCOPY(msg, outp, msglen);
670 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
672 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
677 * ChapGenChallenge is used to generate a pseudo-random challenge string of
678 * a pseudo-random length between min_len and max_len. The challenge
679 * string and its length are stored in *cstate, and various other fields of
680 * *cstate are initialized.
684 ChapGenChallenge(cstate)
688 u_char *ptr = cstate->challenge;
691 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
692 MAX_CHALLENGE_LENGTH */
693 chal_len = (unsigned) ((drand48() *
694 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
695 MIN_CHALLENGE_LENGTH);
696 cstate->chal_len = chal_len;
697 cstate->chal_id = ++cstate->id;
698 cstate->chal_transmits = 0;
700 /* generate a random string */
701 for (i = 0; i < chal_len; i++ )
702 *ptr++ = (char) (drand48() * 0xff);
706 * ChapSendResponse - send a response packet with values as specified
711 ChapSendResponse(cstate)
715 int outlen, md_len, name_len;
717 md_len = cstate->resp_length;
718 name_len = strlen(cstate->resp_name);
719 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
720 outp = outpacket_buf;
722 MAKEHEADER(outp, PPP_CHAP);
724 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
725 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
726 PUTSHORT(outlen, outp); /* packet length */
728 PUTCHAR(md_len, outp); /* length of MD */
729 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
730 INCPTR(md_len, outp);
732 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
734 /* send the packet */
735 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
737 cstate->clientstate = CHAPCS_RESPONSE;
738 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
739 ++cstate->resp_transmits;
743 * ChapPrintPkt - print the contents of a CHAP packet.
745 char *ChapCodenames[] = {
746 "Challenge", "Response", "Success", "Failure"
750 ChapPrintPkt(p, plen, printer, arg)
753 void (*printer) __P((void *, char *, ...));
760 if (plen < CHAP_HEADERLEN)
765 if (len < CHAP_HEADERLEN || len > plen)
768 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
769 printer(arg, " %s", ChapCodenames[code-1]);
771 printer(arg, " code=0x%x", code);
772 printer(arg, " id=0x%x", id);
773 len -= CHAP_HEADERLEN;
783 nlen = len - clen - 1;
785 for (; clen > 0; --clen) {
787 printer(arg, "%.2x", x);
789 printer(arg, ">, name = ");
790 print_string((char *)p, nlen, printer, arg);
795 print_string((char *)p, len, printer, arg);
798 for (clen = len; clen > 0; --clen) {
800 printer(arg, " %.2x", x);
804 return len + CHAP_HEADERLEN;