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.12 1996/05/28 00:42:27 paulus Exp $";
46 #include <sys/types.h>
57 struct protent chap_protent = {
58 PPP_CHAP, ChapInit, ChapInput, ChapProtocolReject,
59 ChapLowerUp, ChapLowerDown, NULL, NULL,
60 ChapPrintPkt, NULL, 1, "CHAP", NULL, NULL
63 chap_state chap[NUM_PPP]; /* CHAP state; one for each unit */
65 static void ChapChallengeTimeout __P((caddr_t));
66 static void ChapResponseTimeout __P((caddr_t));
67 static void ChapReceiveChallenge __P((chap_state *, u_char *, int, int));
68 static void ChapReceiveResponse __P((chap_state *, u_char *, int, int));
69 static void ChapReceiveSuccess __P((chap_state *, u_char *, int, int));
70 static void ChapReceiveFailure __P((chap_state *, u_char *, int, int));
71 static void ChapSendStatus __P((chap_state *, int));
72 static void ChapSendChallenge __P((chap_state *));
73 static void ChapSendResponse __P((chap_state *));
74 static void ChapGenChallenge __P((chap_state *));
76 extern double drand48 __P((void));
77 extern void srand48 __P((long));
80 * ChapInit - Initialize a CHAP unit.
86 chap_state *cstate = &chap[unit];
88 BZERO(cstate, sizeof(*cstate));
90 cstate->clientstate = CHAPCS_INITIAL;
91 cstate->serverstate = CHAPSS_INITIAL;
92 cstate->timeouttime = CHAP_DEFTIMEOUT;
93 cstate->max_transmits = CHAP_DEFTRANSMITS;
94 /* random number generator is initialized in magic_init */
99 * ChapAuthWithPeer - Authenticate us with our peer (start client).
103 ChapAuthWithPeer(unit, our_name, digest)
108 chap_state *cstate = &chap[unit];
110 cstate->resp_name = our_name;
111 cstate->resp_type = digest;
113 if (cstate->clientstate == CHAPCS_INITIAL ||
114 cstate->clientstate == CHAPCS_PENDING) {
115 /* lower layer isn't up - wait until later */
116 cstate->clientstate = CHAPCS_PENDING;
121 * We get here as a result of LCP coming up.
122 * So even if CHAP was open before, we will
123 * have to re-authenticate ourselves.
125 cstate->clientstate = CHAPCS_LISTEN;
130 * ChapAuthPeer - Authenticate our peer (start server).
133 ChapAuthPeer(unit, our_name, digest)
138 chap_state *cstate = &chap[unit];
140 cstate->chal_name = our_name;
141 cstate->chal_type = digest;
143 if (cstate->serverstate == CHAPSS_INITIAL ||
144 cstate->serverstate == CHAPSS_PENDING) {
145 /* lower layer isn't up - wait until later */
146 cstate->serverstate = CHAPSS_PENDING;
150 ChapGenChallenge(cstate);
151 ChapSendChallenge(cstate); /* crank it up dude! */
152 cstate->serverstate = CHAPSS_INITIAL_CHAL;
157 * ChapChallengeTimeout - Timeout expired on sending challenge.
160 ChapChallengeTimeout(arg)
163 chap_state *cstate = (chap_state *) arg;
165 /* if we aren't sending challenges, don't worry. then again we */
166 /* probably shouldn't be here either */
167 if (cstate->serverstate != CHAPSS_INITIAL_CHAL &&
168 cstate->serverstate != CHAPSS_RECHALLENGE)
171 if (cstate->chal_transmits >= cstate->max_transmits) {
172 /* give up on peer */
173 syslog(LOG_ERR, "Peer failed to respond to CHAP challenge");
174 cstate->serverstate = CHAPSS_BADAUTH;
175 auth_peer_fail(cstate->unit, PPP_CHAP);
179 ChapSendChallenge(cstate); /* Re-send challenge */
184 * ChapResponseTimeout - Timeout expired on sending response.
187 ChapResponseTimeout(arg)
190 chap_state *cstate = (chap_state *) arg;
192 /* if we aren't sending a response, don't worry. */
193 if (cstate->clientstate != CHAPCS_RESPONSE)
196 ChapSendResponse(cstate); /* re-send response */
201 * ChapRechallenge - Time to challenge the peer again.
207 chap_state *cstate = (chap_state *) arg;
209 /* if we aren't sending a response, don't worry. */
210 if (cstate->serverstate != CHAPSS_OPEN)
213 ChapGenChallenge(cstate);
214 ChapSendChallenge(cstate);
215 cstate->serverstate = CHAPSS_RECHALLENGE;
220 * ChapLowerUp - The lower layer is up.
222 * Start up if we have pending requests.
228 chap_state *cstate = &chap[unit];
230 if (cstate->clientstate == CHAPCS_INITIAL)
231 cstate->clientstate = CHAPCS_CLOSED;
232 else if (cstate->clientstate == CHAPCS_PENDING)
233 cstate->clientstate = CHAPCS_LISTEN;
235 if (cstate->serverstate == CHAPSS_INITIAL)
236 cstate->serverstate = CHAPSS_CLOSED;
237 else if (cstate->serverstate == CHAPSS_PENDING) {
238 ChapGenChallenge(cstate);
239 ChapSendChallenge(cstate);
240 cstate->serverstate = CHAPSS_INITIAL_CHAL;
246 * ChapLowerDown - The lower layer is down.
248 * Cancel all timeouts.
254 chap_state *cstate = &chap[unit];
256 /* Timeout(s) pending? Cancel if so. */
257 if (cstate->serverstate == CHAPSS_INITIAL_CHAL ||
258 cstate->serverstate == CHAPSS_RECHALLENGE)
259 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
260 else if (cstate->serverstate == CHAPSS_OPEN
261 && cstate->chal_interval != 0)
262 UNTIMEOUT(ChapRechallenge, (caddr_t) cstate);
263 if (cstate->clientstate == CHAPCS_RESPONSE)
264 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
266 cstate->clientstate = CHAPCS_INITIAL;
267 cstate->serverstate = CHAPSS_INITIAL;
272 * ChapProtocolReject - Peer doesn't grok CHAP.
275 ChapProtocolReject(unit)
278 chap_state *cstate = &chap[unit];
280 if (cstate->serverstate != CHAPSS_INITIAL &&
281 cstate->serverstate != CHAPSS_CLOSED)
282 auth_peer_fail(unit, PPP_CHAP);
283 if (cstate->clientstate != CHAPCS_INITIAL &&
284 cstate->clientstate != CHAPCS_CLOSED)
285 auth_withpeer_fail(unit, PPP_CHAP);
286 ChapLowerDown(unit); /* shutdown chap */
291 * ChapInput - Input CHAP packet.
294 ChapInput(unit, inpacket, packet_len)
299 chap_state *cstate = &chap[unit];
305 * Parse header (code, id and length).
306 * If packet too short, drop it.
309 if (packet_len < CHAP_HEADERLEN) {
310 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short header."));
316 if (len < CHAP_HEADERLEN) {
317 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd illegal length."));
320 if (len > packet_len) {
321 CHAPDEBUG((LOG_INFO, "ChapInput: rcvd short packet."));
324 len -= CHAP_HEADERLEN;
327 * Action depends on code (as in fact it usually does :-).
331 ChapReceiveChallenge(cstate, inp, id, len);
335 ChapReceiveResponse(cstate, inp, id, len);
339 ChapReceiveFailure(cstate, inp, id, len);
343 ChapReceiveSuccess(cstate, inp, id, len);
346 default: /* Need code reject? */
347 syslog(LOG_WARNING, "Unknown CHAP code (%d) received.", code);
354 * ChapReceiveChallenge - Receive Challenge and send Response.
357 ChapReceiveChallenge(cstate, inp, id, len)
366 char secret[MAXSECRETLEN];
370 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: Rcvd id %d.", id));
371 if (cstate->clientstate == CHAPCS_CLOSED ||
372 cstate->clientstate == CHAPCS_PENDING) {
373 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: in state %d",
374 cstate->clientstate));
379 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
383 GETCHAR(rchallenge_len, inp);
384 len -= sizeof (u_char) + rchallenge_len; /* now name field length */
386 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: rcvd short packet."));
390 INCPTR(rchallenge_len, inp);
392 if (len >= sizeof(rhostname))
393 len = sizeof(rhostname) - 1;
394 BCOPY(inp, rhostname, len);
395 rhostname[len] = '\000';
397 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: received name field '%s'",
400 /* Microsoft doesn't send their name back in the PPP packet */
401 if (rhostname[0] == 0 && cstate->resp_type == CHAP_MICROSOFT) {
402 strcpy(rhostname, remote_name);
403 CHAPDEBUG((LOG_INFO, "ChapReceiveChallenge: using '%s' as remote name",
407 /* get secret for authenticating ourselves with the specified host */
408 if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
409 secret, &secret_len, 0)) {
410 secret_len = 0; /* assume null secret if can't find one */
411 syslog(LOG_WARNING, "No CHAP secret found for authenticating us to %s",
415 /* cancel response send timeout if necessary */
416 if (cstate->clientstate == CHAPCS_RESPONSE)
417 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
419 cstate->resp_id = id;
420 cstate->resp_transmits = 0;
422 /* generate MD based on negotiated type */
423 switch (cstate->resp_type) {
425 case CHAP_DIGEST_MD5:
427 MD5Update(&mdContext, &cstate->resp_id, 1);
428 MD5Update(&mdContext, secret, secret_len);
429 MD5Update(&mdContext, rchallenge, rchallenge_len);
430 MD5Final(&mdContext);
431 BCOPY(mdContext.digest, cstate->response, MD5_SIGNATURE_SIZE);
432 cstate->resp_length = MD5_SIGNATURE_SIZE;
437 ChapMS(cstate, rchallenge, rchallenge_len, secret, secret_len);
442 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->resp_type));
446 ChapSendResponse(cstate);
451 * ChapReceiveResponse - Receive and process response.
454 ChapReceiveResponse(cstate, inp, id, len)
460 u_char *remmd, remmd_len;
461 int secret_len, old_state;
465 char secret[MAXSECRETLEN];
467 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: Rcvd id %d.", id));
469 if (cstate->serverstate == CHAPSS_CLOSED ||
470 cstate->serverstate == CHAPSS_PENDING) {
471 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: in state %d",
472 cstate->serverstate));
476 if (id != cstate->chal_id)
477 return; /* doesn't match ID of last challenge */
480 * If we have received a duplicate or bogus Response,
481 * we have to send the same answer (Success/Failure)
482 * as we did for the first Response we saw.
484 if (cstate->serverstate == CHAPSS_OPEN) {
485 ChapSendStatus(cstate, CHAP_SUCCESS);
488 if (cstate->serverstate == CHAPSS_BADAUTH) {
489 ChapSendStatus(cstate, CHAP_FAILURE);
494 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
497 GETCHAR(remmd_len, inp); /* get length of MD */
498 remmd = inp; /* get pointer to MD */
499 INCPTR(remmd_len, inp);
501 len -= sizeof (u_char) + remmd_len;
503 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: rcvd short packet."));
507 UNTIMEOUT(ChapChallengeTimeout, (caddr_t) cstate);
509 if (len >= sizeof(rhostname))
510 len = sizeof(rhostname) - 1;
511 BCOPY(inp, rhostname, len);
512 rhostname[len] = '\000';
514 CHAPDEBUG((LOG_INFO, "ChapReceiveResponse: received name field: %s",
518 * Get secret for authenticating them with us,
519 * do the hash ourselves, and compare the result.
522 if (!get_secret(cstate->unit, rhostname, cstate->chal_name,
523 secret, &secret_len, 1)) {
524 syslog(LOG_WARNING, "No CHAP secret found for authenticating %s",
528 /* generate MD based on negotiated type */
529 switch (cstate->chal_type) {
531 case CHAP_DIGEST_MD5: /* only MD5 is defined for now */
532 if (remmd_len != MD5_SIGNATURE_SIZE)
533 break; /* it's not even the right length */
535 MD5Update(&mdContext, &cstate->chal_id, 1);
536 MD5Update(&mdContext, secret, secret_len);
537 MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
538 MD5Final(&mdContext);
540 /* compare local and remote MDs and send the appropriate status */
541 if (memcmp (mdContext.digest, remmd, MD5_SIGNATURE_SIZE) == 0)
542 code = CHAP_SUCCESS; /* they are the same! */
546 CHAPDEBUG((LOG_INFO, "unknown digest type %d", cstate->chal_type));
550 ChapSendStatus(cstate, code);
552 if (code == CHAP_SUCCESS) {
553 old_state = cstate->serverstate;
554 cstate->serverstate = CHAPSS_OPEN;
555 if (old_state == CHAPSS_INITIAL_CHAL) {
556 auth_peer_success(cstate->unit, PPP_CHAP);
558 if (cstate->chal_interval != 0)
559 TIMEOUT(ChapRechallenge, (caddr_t) cstate, cstate->chal_interval);
562 syslog(LOG_ERR, "CHAP peer authentication failed");
563 cstate->serverstate = CHAPSS_BADAUTH;
564 auth_peer_fail(cstate->unit, PPP_CHAP);
569 * ChapReceiveSuccess - Receive Success
572 ChapReceiveSuccess(cstate, inp, id, len)
579 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: Rcvd id %d.", id));
581 if (cstate->clientstate == CHAPCS_OPEN)
582 /* presumably an answer to a duplicate response */
585 if (cstate->clientstate != CHAPCS_RESPONSE) {
586 /* don't know what this is */
587 CHAPDEBUG((LOG_INFO, "ChapReceiveSuccess: in state %d\n",
588 cstate->clientstate));
592 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
600 cstate->clientstate = CHAPCS_OPEN;
602 auth_withpeer_success(cstate->unit, PPP_CHAP);
607 * ChapReceiveFailure - Receive failure.
610 ChapReceiveFailure(cstate, inp, id, len)
616 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: Rcvd id %d.", id));
618 if (cstate->clientstate != CHAPCS_RESPONSE) {
619 /* don't know what this is */
620 CHAPDEBUG((LOG_INFO, "ChapReceiveFailure: in state %d\n",
621 cstate->clientstate));
625 UNTIMEOUT(ChapResponseTimeout, (caddr_t) cstate);
633 syslog(LOG_ERR, "CHAP authentication failed");
634 auth_withpeer_fail(cstate->unit, PPP_CHAP);
639 * ChapSendChallenge - Send an Authenticate challenge.
642 ChapSendChallenge(cstate)
646 int chal_len, name_len;
649 chal_len = cstate->chal_len;
650 name_len = strlen(cstate->chal_name);
651 outlen = CHAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
652 outp = outpacket_buf;
654 MAKEHEADER(outp, PPP_CHAP); /* paste in a CHAP header */
656 PUTCHAR(CHAP_CHALLENGE, outp);
657 PUTCHAR(cstate->chal_id, outp);
658 PUTSHORT(outlen, outp);
660 PUTCHAR(chal_len, outp); /* put length of challenge */
661 BCOPY(cstate->challenge, outp, chal_len);
662 INCPTR(chal_len, outp);
664 BCOPY(cstate->chal_name, outp, name_len); /* append hostname */
666 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
668 CHAPDEBUG((LOG_INFO, "ChapSendChallenge: Sent id %d.", cstate->chal_id));
670 TIMEOUT(ChapChallengeTimeout, (caddr_t) cstate, cstate->timeouttime);
671 ++cstate->chal_transmits;
676 * ChapSendStatus - Send a status response (ack or nak).
679 ChapSendStatus(cstate, code)
687 if (code == CHAP_SUCCESS)
688 sprintf(msg, "Welcome to %s.", hostname);
690 sprintf(msg, "I don't like you. Go 'way.");
691 msglen = strlen(msg);
693 outlen = CHAP_HEADERLEN + msglen;
694 outp = outpacket_buf;
696 MAKEHEADER(outp, PPP_CHAP); /* paste in a header */
699 PUTCHAR(cstate->chal_id, outp);
700 PUTSHORT(outlen, outp);
701 BCOPY(msg, outp, msglen);
702 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
704 CHAPDEBUG((LOG_INFO, "ChapSendStatus: Sent code %d, id %d.", code,
709 * ChapGenChallenge is used to generate a pseudo-random challenge string of
710 * a pseudo-random length between min_len and max_len. The challenge
711 * string and its length are stored in *cstate, and various other fields of
712 * *cstate are initialized.
716 ChapGenChallenge(cstate)
720 u_char *ptr = cstate->challenge;
723 /* pick a random challenge length between MIN_CHALLENGE_LENGTH and
724 MAX_CHALLENGE_LENGTH */
725 chal_len = (unsigned) ((drand48() *
726 (MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
727 MIN_CHALLENGE_LENGTH);
728 cstate->chal_len = chal_len;
729 cstate->chal_id = ++cstate->id;
730 cstate->chal_transmits = 0;
732 /* generate a random string */
733 for (i = 0; i < chal_len; i++ )
734 *ptr++ = (char) (drand48() * 0xff);
738 * ChapSendResponse - send a response packet with values as specified
743 ChapSendResponse(cstate)
747 int outlen, md_len, name_len;
749 md_len = cstate->resp_length;
750 name_len = strlen(cstate->resp_name);
751 outlen = CHAP_HEADERLEN + sizeof (u_char) + md_len + name_len;
752 outp = outpacket_buf;
754 MAKEHEADER(outp, PPP_CHAP);
756 PUTCHAR(CHAP_RESPONSE, outp); /* we are a response */
757 PUTCHAR(cstate->resp_id, outp); /* copy id from challenge packet */
758 PUTSHORT(outlen, outp); /* packet length */
760 PUTCHAR(md_len, outp); /* length of MD */
761 BCOPY(cstate->response, outp, md_len); /* copy MD to buffer */
762 INCPTR(md_len, outp);
764 BCOPY(cstate->resp_name, outp, name_len); /* append our name */
766 /* send the packet */
767 output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
769 cstate->clientstate = CHAPCS_RESPONSE;
770 TIMEOUT(ChapResponseTimeout, (caddr_t) cstate, cstate->timeouttime);
771 ++cstate->resp_transmits;
775 * ChapPrintPkt - print the contents of a CHAP packet.
777 char *ChapCodenames[] = {
778 "Challenge", "Response", "Success", "Failure"
782 ChapPrintPkt(p, plen, printer, arg)
785 void (*printer) __P((void *, char *, ...));
792 if (plen < CHAP_HEADERLEN)
797 if (len < CHAP_HEADERLEN || len > plen)
800 if (code >= 1 && code <= sizeof(ChapCodenames) / sizeof(char *))
801 printer(arg, " %s", ChapCodenames[code-1]);
803 printer(arg, " code=0x%x", code);
804 printer(arg, " id=0x%x", id);
805 len -= CHAP_HEADERLEN;
815 nlen = len - clen - 1;
817 for (; clen > 0; --clen) {
819 printer(arg, "%.2x", x);
821 printer(arg, ">, name = ");
822 print_string((char *)p, nlen, printer, arg);
827 print_string((char *)p, len, printer, arg);
830 for (clen = len; clen > 0; --clen) {
832 printer(arg, " %.2x", x);
836 return len + CHAP_HEADERLEN;