2 * chap-new.c - New CHAP implementation.
4 * Copyright (c) 2003 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 #define RCSID "$Id: chap-new.c,v 1.5 2004/10/31 22:23:18 paulus Exp $"
46 #define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
48 #define MDTYPE_ALL (MDTYPE_MD5)
51 int chap_mdtype_all
= MDTYPE_ALL
;
53 /* Hook for a plugin to validate CHAP challenge */
54 int (*chap_verify_hook
)(char *name
, char *ourname
, int id
,
55 struct chap_digest_type
*digest
,
56 unsigned char *challenge
, unsigned char *response
,
57 char *message
, int message_space
) = NULL
;
62 int chap_timeout_time
= 3;
63 int chap_max_transmits
= 10;
64 int chap_rechallenge_time
= 0;
67 * Command-line options.
69 static option_t chap_option_list
[] = {
70 { "chap-restart", o_int
, &chap_timeout_time
,
71 "Set timeout for CHAP", OPT_PRIO
},
72 { "chap-max-challenge", o_int
, &chap_max_transmits
,
73 "Set max #xmits for challenge", OPT_PRIO
},
74 { "chap-interval", o_int
, &chap_rechallenge_time
,
75 "Set interval for rechallenge", OPT_PRIO
},
82 static struct chap_client_state
{
85 struct chap_digest_type
*digest
;
86 unsigned char priv
[64]; /* private area for digest's use */
90 * These limits apply to challenge and response packets we send.
91 * The +4 is the +1 that we actually need rounded up.
93 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
94 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
96 static struct chap_server_state
{
100 struct chap_digest_type
*digest
;
102 int challenge_pktlen
;
103 unsigned char challenge
[CHAL_MAX_PKTLEN
];
106 /* Values for flags in chap_client_state and chap_server_state */
108 #define AUTH_STARTED 2
110 #define AUTH_FAILED 8
111 #define TIMEOUT_PENDING 0x10
112 #define CHALLENGE_VALID 0x20
117 static void chap_init(int unit
);
118 static void chap_lowerup(int unit
);
119 static void chap_lowerdown(int unit
);
120 static void chap_timeout(void *arg
);
121 static void chap_generate_challenge(struct chap_server_state
*ss
);
122 static void chap_handle_response(struct chap_server_state
*ss
, int code
,
123 unsigned char *pkt
, int len
);
124 static int chap_verify_response(char *name
, char *ourname
, int id
,
125 struct chap_digest_type
*digest
,
126 unsigned char *challenge
, unsigned char *response
,
127 char *message
, int message_space
);
128 static void chap_respond(struct chap_client_state
*cs
, int id
,
129 unsigned char *pkt
, int len
);
130 static void chap_handle_status(struct chap_client_state
*cs
, int code
, int id
,
131 unsigned char *pkt
, int len
);
132 static void chap_protrej(int unit
);
133 static void chap_input(int unit
, unsigned char *pkt
, int pktlen
);
134 static int chap_print_pkt(unsigned char *p
, int plen
,
135 void (*printer
) __P((void *, char *, ...)), void *arg
);
137 /* List of digest types that we know about */
138 static struct chap_digest_type
*chap_digests
;
141 * chap_init - reset to initial state.
146 memset(&client
, 0, sizeof(client
));
147 memset(&server
, 0, sizeof(server
));
156 * Add a new digest type to the list.
159 chap_register_digest(struct chap_digest_type
*dp
)
161 dp
->next
= chap_digests
;
166 * chap_lowerup - we can start doing stuff now.
169 chap_lowerup(int unit
)
171 struct chap_client_state
*cs
= &client
;
172 struct chap_server_state
*ss
= &server
;
174 cs
->flags
|= LOWERUP
;
175 ss
->flags
|= LOWERUP
;
176 if (ss
->flags
& AUTH_STARTED
)
181 chap_lowerdown(int unit
)
183 struct chap_client_state
*cs
= &client
;
184 struct chap_server_state
*ss
= &server
;
187 if (ss
->flags
& TIMEOUT_PENDING
)
188 UNTIMEOUT(chap_timeout
, ss
);
193 * chap_auth_peer - Start authenticating the peer.
194 * If the lower layer is already up, we start sending challenges,
195 * otherwise we wait for the lower layer to come up.
198 chap_auth_peer(int unit
, char *our_name
, int digest_code
)
200 struct chap_server_state
*ss
= &server
;
201 struct chap_digest_type
*dp
;
203 if (ss
->flags
& AUTH_STARTED
) {
204 error("CHAP: peer authentication already started!");
207 for (dp
= chap_digests
; dp
!= NULL
; dp
= dp
->next
)
208 if (dp
->code
== digest_code
)
211 fatal("CHAP digest 0x%x requested but not available",
216 /* Start with a random ID value */
217 ss
->id
= (unsigned char)(drand48() * 256);
218 ss
->flags
|= AUTH_STARTED
;
219 if (ss
->flags
& LOWERUP
)
224 * chap_auth_with_peer - Prepare to authenticate ourselves to the peer.
225 * There isn't much to do until we receive a challenge.
228 chap_auth_with_peer(int unit
, char *our_name
, int digest_code
)
230 struct chap_client_state
*cs
= &client
;
231 struct chap_digest_type
*dp
;
233 if (cs
->flags
& AUTH_STARTED
) {
234 error("CHAP: authentication with peer already started!");
237 for (dp
= chap_digests
; dp
!= NULL
; dp
= dp
->next
)
238 if (dp
->code
== digest_code
)
241 fatal("CHAP digest 0x%x requested but not available",
246 cs
->flags
|= AUTH_STARTED
;
250 * chap_timeout - It's time to send another challenge to the peer.
251 * This could be either a retransmission of a previous challenge,
252 * or a new challenge to start re-authentication.
255 chap_timeout(void *arg
)
257 struct chap_server_state
*ss
= arg
;
259 ss
->flags
&= ~TIMEOUT_PENDING
;
260 if ((ss
->flags
& CHALLENGE_VALID
) == 0) {
261 ss
->challenge_xmits
= 0;
262 chap_generate_challenge(ss
);
263 ss
->flags
|= CHALLENGE_VALID
;
264 } else if (ss
->challenge_xmits
>= chap_max_transmits
) {
265 ss
->flags
&= ~CHALLENGE_VALID
;
266 ss
->flags
|= AUTH_DONE
| AUTH_FAILED
;
267 auth_peer_fail(0, PPP_CHAP
);
271 output(0, ss
->challenge
, ss
->challenge_pktlen
);
272 ++ss
->challenge_xmits
;
273 ss
->flags
|= TIMEOUT_PENDING
;
274 TIMEOUT(chap_timeout
, arg
, chap_timeout_time
);
278 * chap_generate_challenge - generate a challenge string and format
279 * the challenge packet in ss->challenge_pkt.
282 chap_generate_challenge(struct chap_server_state
*ss
)
284 int clen
= 1, nlen
, len
;
288 MAKEHEADER(p
, PPP_CHAP
);
290 ss
->digest
->generate_challenge(p
);
292 nlen
= strlen(ss
->name
);
293 memcpy(p
+ 1 + clen
, ss
->name
, nlen
);
295 len
= CHAP_HDRLEN
+ 1 + clen
+ nlen
;
296 ss
->challenge_pktlen
= PPP_HDRLEN
+ len
;
298 p
= ss
->challenge
+ PPP_HDRLEN
;
299 p
[0] = CHAP_CHALLENGE
;
306 * chap_handle_response - check the response to our challenge.
309 chap_handle_response(struct chap_server_state
*ss
, int id
,
310 unsigned char *pkt
, int len
)
312 int response_len
, ok
, mlen
;
313 unsigned char *response
, *p
;
314 char *name
= NULL
; /* initialized to shut gcc up */
315 int (*verifier
)(char *, char *, int, struct chap_digest_type
*,
316 unsigned char *, unsigned char *, char *, int);
317 char rname
[MAXNAMELEN
+1];
320 if ((ss
->flags
& LOWERUP
) == 0)
322 if (id
!= ss
->challenge
[PPP_HDRLEN
+1] || len
< 2)
324 if ((ss
->flags
& AUTH_DONE
) == 0) {
325 if ((ss
->flags
& CHALLENGE_VALID
) == 0)
328 GETCHAR(response_len
, pkt
);
329 len
-= response_len
+ 1; /* length of name */
330 name
= (char *)pkt
+ response_len
;
334 ss
->flags
&= ~CHALLENGE_VALID
;
335 if (ss
->flags
& TIMEOUT_PENDING
) {
336 ss
->flags
&= ~TIMEOUT_PENDING
;
337 UNTIMEOUT(chap_timeout
, ss
);
340 if (explicit_remote
) {
343 /* Null terminate and clean remote name. */
344 slprintf(rname
, sizeof(rname
), "%.*v", len
, name
);
348 if (chap_verify_hook
)
349 verifier
= chap_verify_hook
;
351 verifier
= chap_verify_response
;
352 ok
= (*verifier
)(name
, ss
->name
, id
, ss
->digest
,
353 ss
->challenge
+ PPP_HDRLEN
+ CHAP_HDRLEN
,
354 response
, message
, sizeof(message
));
355 if (!ok
|| !auth_number()) {
356 ss
->flags
|= AUTH_FAILED
;
357 warn("Peer %q failed CHAP authentication", name
);
361 /* send the response */
363 MAKEHEADER(p
, PPP_CHAP
);
364 mlen
= strlen(message
);
365 len
= CHAP_HDRLEN
+ mlen
;
366 p
[0] = (ss
->flags
& AUTH_FAILED
)? CHAP_FAILURE
: CHAP_SUCCESS
;
371 memcpy(p
+ CHAP_HDRLEN
, message
, mlen
);
372 output(0, outpacket_buf
, PPP_HDRLEN
+ len
);
374 if ((ss
->flags
& AUTH_DONE
) == 0) {
375 ss
->flags
|= AUTH_DONE
;
376 if (ss
->flags
& AUTH_FAILED
) {
377 auth_peer_fail(0, PPP_CHAP
);
379 auth_peer_success(0, PPP_CHAP
, ss
->digest
->code
,
381 if (chap_rechallenge_time
) {
382 ss
->flags
|= TIMEOUT_PENDING
;
383 TIMEOUT(chap_timeout
, ss
,
384 chap_rechallenge_time
);
391 * chap_verify_response - check whether the peer's response matches
392 * what we think it should be. Returns 1 if it does (authentication
393 * succeeded), or 0 if it doesn't.
396 chap_verify_response(char *name
, char *ourname
, int id
,
397 struct chap_digest_type
*digest
,
398 unsigned char *challenge
, unsigned char *response
,
399 char *message
, int message_space
)
402 unsigned char secret
[MAXSECRETLEN
];
405 /* Get the secret that the peer is supposed to know */
406 if (!get_secret(0, name
, ourname
, (char *)secret
, &secret_len
, 1)) {
407 error("No CHAP secret found for authenticating %q", name
);
411 ok
= digest
->verify_response(id
, name
, secret
, secret_len
, challenge
,
412 response
, message
, message_space
);
413 memset(secret
, 0, sizeof(secret
));
419 * chap_respond - Generate and send a response to a challenge.
422 chap_respond(struct chap_client_state
*cs
, int id
,
423 unsigned char *pkt
, int len
)
428 unsigned char response
[RESP_MAX_PKTLEN
];
429 char rname
[MAXNAMELEN
+1];
430 char secret
[MAXSECRETLEN
+1];
432 if ((cs
->flags
& (LOWERUP
| AUTH_STARTED
)) != (LOWERUP
| AUTH_STARTED
))
433 return; /* not ready */
434 if (len
< 2 || len
< pkt
[0] + 1)
435 return; /* too short */
437 nlen
= len
- (clen
+ 1);
439 /* Null terminate and clean remote name. */
440 slprintf(rname
, sizeof(rname
), "%.*v", nlen
, pkt
+ clen
+ 1);
442 /* Microsoft doesn't send their name back in the PPP packet */
443 if (explicit_remote
|| (remote_name
[0] != 0 && rname
[0] == 0))
444 strlcpy(rname
, remote_name
, sizeof(rname
));
446 /* get secret for authenticating ourselves with the specified host */
447 if (!get_secret(0, cs
->name
, rname
, secret
, &secret_len
, 0)) {
448 secret_len
= 0; /* assume null secret if can't find one */
449 warn("No CHAP secret found for authenticating us to %q", rname
);
453 MAKEHEADER(p
, PPP_CHAP
);
456 cs
->digest
->make_response(p
, id
, cs
->name
, pkt
,
457 secret
, secret_len
, cs
->priv
);
458 memset(secret
, 0, secret_len
);
461 nlen
= strlen(cs
->name
);
462 memcpy(p
+ clen
+ 1, cs
->name
, nlen
);
464 p
= response
+ PPP_HDRLEN
;
465 len
= CHAP_HDRLEN
+ clen
+ 1 + nlen
;
466 p
[0] = CHAP_RESPONSE
;
471 output(0, response
, PPP_HDRLEN
+ len
);
475 chap_handle_status(struct chap_client_state
*cs
, int code
, int id
,
476 unsigned char *pkt
, int len
)
478 const char *msg
= NULL
;
480 if ((cs
->flags
& (AUTH_DONE
|AUTH_STARTED
|LOWERUP
))
481 != (AUTH_STARTED
|LOWERUP
))
483 cs
->flags
|= AUTH_DONE
;
485 if (code
== CHAP_SUCCESS
) {
486 /* used for MS-CHAP v2 mutual auth, yuck */
487 if (cs
->digest
->check_success
!= NULL
) {
488 if (!(*cs
->digest
->check_success
)(pkt
, len
, cs
->priv
))
491 msg
= "CHAP authentication succeeded";
493 if (cs
->digest
->handle_failure
!= NULL
)
494 (*cs
->digest
->handle_failure
)(pkt
, len
);
496 msg
= "CHAP authentication failed";
500 info("%s: %.*v", msg
, len
, pkt
);
504 if (code
== CHAP_SUCCESS
)
505 auth_withpeer_success(0, PPP_CHAP
, cs
->digest
->code
);
507 cs
->flags
|= AUTH_FAILED
;
508 auth_withpeer_fail(0, PPP_CHAP
);
513 chap_input(int unit
, unsigned char *pkt
, int pktlen
)
515 struct chap_client_state
*cs
= &client
;
516 struct chap_server_state
*ss
= &server
;
517 unsigned char code
, id
;
520 if (pktlen
< CHAP_HDRLEN
)
525 if (len
< CHAP_HDRLEN
|| len
> pktlen
)
531 chap_respond(cs
, id
, pkt
, len
);
534 chap_handle_response(ss
, id
, pkt
, len
);
538 chap_handle_status(cs
, code
, id
, pkt
, len
);
544 chap_protrej(int unit
)
546 struct chap_client_state
*cs
= &client
;
547 struct chap_server_state
*ss
= &server
;
549 if (ss
->flags
& TIMEOUT_PENDING
) {
550 ss
->flags
&= ~TIMEOUT_PENDING
;
551 UNTIMEOUT(chap_timeout
, ss
);
553 if (ss
->flags
& AUTH_STARTED
) {
555 auth_peer_fail(0, PPP_CHAP
);
557 if ((cs
->flags
& (AUTH_STARTED
|AUTH_DONE
)) == AUTH_STARTED
) {
558 cs
->flags
&= ~AUTH_STARTED
;
559 auth_withpeer_fail(0, PPP_CHAP
);
564 * chap_print_pkt - print the contents of a CHAP packet.
566 static char *chap_code_names
[] = {
567 "Challenge", "Response", "Success", "Failure"
571 chap_print_pkt(unsigned char *p
, int plen
,
572 void (*printer
) __P((void *, char *, ...)), void *arg
)
578 if (plen
< CHAP_HDRLEN
)
583 if (len
< CHAP_HDRLEN
|| len
> plen
)
586 if (code
>= 1 && code
<= sizeof(chap_code_names
) / sizeof(char *))
587 printer(arg
, " %s", chap_code_names
[code
-1]);
589 printer(arg
, " code=0x%x", code
);
590 printer(arg
, " id=0x%x", id
);
601 nlen
= len
- clen
- 1;
603 for (; clen
> 0; --clen
) {
605 printer(arg
, "%.2x", x
);
607 printer(arg
, ">, name = ");
608 print_string((char *)p
, nlen
, printer
, arg
);
613 print_string((char *)p
, len
, printer
, arg
);
616 for (clen
= len
; clen
> 0; --clen
) {
618 printer(arg
, " %.2x", x
);
622 return len
+ CHAP_HDRLEN
;
625 struct protent chap_protent
= {
635 NULL
, /* datainput */
636 1, /* enabled_flag */
638 NULL
, /* data_name */
640 NULL
, /* check_options */