]> git.ozlabs.org Git - ppp.git/commitdiff
New CHAP implementation, rewritten from scratch to avoid the code
authorPaul Mackerras <paulus@samba.org>
Wed, 11 Jun 2003 23:56:26 +0000 (23:56 +0000)
committerPaul Mackerras <paulus@samba.org>
Wed, 11 Jun 2003 23:56:26 +0000 (23:56 +0000)
copyrighted by the uncontactable Gregory Christy.  The new code is
much cleaner and splits out all the digest-specific code to separate
files.  Thus the CHAP-MD5 stuff is now in chap-md5.c and all the
CHAP-MS and CHAP-MSv2 stuff has moved into chap_ms.c, instead of
having half of it in chap.c.

There are a few minor differences in this implementation; we don't
retransmit responses, but instead just wait for a new challenge.
The success/failure messages are more boring as well.  In fact the
digest code now sets the success/failure message.

CHAP_DIGEST_MD5 has been renamed to CHAP_MD5 for consistency.
There is a new function random_bytes() in magic.c, which generates
a string of random bytes.

14 files changed:
pppd/Makefile.linux
pppd/auth.c
pppd/chap-md5.c [new file with mode: 0644]
pppd/chap-md5.h [new file with mode: 0644]
pppd/chap-new.c [new file with mode: 0644]
pppd/chap-new.h [new file with mode: 0644]
pppd/chap_ms.c
pppd/chap_ms.h
pppd/eap.c
pppd/eap.h
pppd/lcp.c
pppd/magic.c
pppd/magic.h
pppd/main.c

index 0ab8c760f87e71aedc4085982a1792b9e05dd254..85113d070c92a36e6540df0deeb9dcfc77597ca5 100644 (file)
@@ -1,6 +1,6 @@
 #
 # pppd makefile for Linux
-# $Id: Makefile.linux,v 1.58 2003/05/12 08:18:54 fcusack Exp $
+# $Id: Makefile.linux,v 1.59 2003/06/11 23:56:26 paulus Exp $
 #
 
 # Default installation locations
@@ -10,17 +10,18 @@ INCDIR = $(DESTDIR)/usr/include
 
 TARGETS = pppd
 
-PPPDSRCS = main.c magic.c fsm.c lcp.c ipcp.c upap.c chap.c md5.c ccp.c ecp.c \
-          ipxcp.c auth.c options.c sys-linux.c md4.c chap_ms.c \
-          demand.c utils.c tty.c eap.c
+PPPDSRCS = main.c magic.c fsm.c lcp.c ipcp.c upap.c chap-new.c md5.c ccp.c \
+          ecp.c ipxcp.c auth.c options.c sys-linux.c md4.c chap_ms.c \
+          demand.c utils.c tty.c eap.c chap-md5.c
 
-HEADERS = ccp.h chap.h ecp.h fsm.h ipcp.h \
+HEADERS = ccp.h chap-new.h ecp.h fsm.h ipcp.h \
        ipxcp.h lcp.h magic.h md5.h patchlevel.h pathnames.h pppd.h \
        upap.h eap.h
 
 MANPAGES = pppd.8
-PPPDOBJS = main.o magic.o fsm.o lcp.o ipcp.o upap.o chap.o md5.o ccp.o ecp.o \
-          auth.o options.o demand.o utils.o sys-linux.o ipxcp.o tty.o eap.o
+PPPDOBJS = main.o magic.o fsm.o lcp.o ipcp.o upap.o chap-new.o md5.o ccp.o \
+          ecp.o auth.o options.o demand.o utils.o sys-linux.o ipxcp.o tty.o \
+          eap.o chap-md5.o
 
 #
 # include dependencies if present
index b0bcdcf4a9d59349dd2872f344a4450c7bb0fdae..002ec859f880abb2363982e62220902385272fa6 100644 (file)
@@ -73,7 +73,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: auth.c,v 1.94 2003/03/09 09:52:19 etbe Exp $"
+#define RCSID  "$Id: auth.c,v 1.95 2003/06/11 23:56:26 paulus Exp $"
 
 #include <stdio.h>
 #include <stddef.h>
 #include "ecp.h"
 #include "ipcp.h"
 #include "upap.h"
-#include "chap.h"
+#include "chap-new.h"
 #include "eap.h"
 #ifdef CBCP_SUPPORT
 #include "cbcp.h"
@@ -186,6 +186,12 @@ void (*pap_logout_hook) __P((void)) = NULL;
 /* Hook for a plugin to get the PAP password for authenticating us */
 int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
 
+/* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
+int (*chap_check_hook) __P((void)) = NULL;
+
+/* Hook for a plugin to get the CHAP password for authenticating us */
+int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
+
 /* Hook for a plugin to say whether it is OK if the peer
    refuses to authenticate. */
 int (*null_auth_hook) __P((struct wordlist **paddrs,
@@ -634,7 +640,7 @@ link_established(unit)
        eap_authpeer(unit, our_name);
        auth |= EAP_PEER;
     } else if (go->neg_chap) {
-       ChapAuthPeer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
+       chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
        auth |= CHAP_PEER;
     } else if (go->neg_upap) {
        upap_authpeer(unit);
@@ -644,7 +650,7 @@ link_established(unit)
        eap_authwithpeer(unit, user);
        auth |= EAP_WITHPEER;
     } else if (ho->neg_chap) {
-       ChapAuthWithPeer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
+       chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
        auth |= CHAP_WITHPEER;
     } else if (ho->neg_upap) {
        if (passwd[0] == 0) {
@@ -800,7 +806,7 @@ auth_peer_success(unit, protocol, prot_flavor, name, namelen)
     case PPP_CHAP:
        bit = CHAP_PEER;
        switch (prot_flavor) {
-       case CHAP_DIGEST_MD5:
+       case CHAP_MD5:
            bit |= CHAP_MD5_PEER;
            break;
 #ifdef CHAPMS
@@ -876,7 +882,7 @@ auth_withpeer_success(unit, protocol, prot_flavor)
     case PPP_CHAP:
        bit = CHAP_WITHPEER;
        switch (prot_flavor) {
-       case CHAP_DIGEST_MD5:
+       case CHAP_MD5:
            bit |= CHAP_MD5_WITHPEER;
            break;
 #ifdef CHAPMS
diff --git a/pppd/chap-md5.c b/pppd/chap-md5.c
new file mode 100644 (file)
index 0000000..2529f4e
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * chap-md5.c - New CHAP/MD5 implementation.
+ *
+ * Copyright (c) 2003 Paul Mackerras. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name(s) of the authors of this software must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission.
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Paul Mackerras
+ *     <paulus@samba.org>".
+ *
+ * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define RCSID  "$Id"
+
+#include <stdlib.h>
+#include <string.h>
+#include "pppd.h"
+#include "chap-new.h"
+#include "chap-md5.h"
+#include "magic.h"
+#include "md5.h"
+
+#define MD5_HASH_SIZE          16
+#define MD5_MIN_CHALLENGE      16
+#define MD5_MAX_CHALLENGE      24
+
+static void
+chap_md5_generate_challenge(unsigned char *cp)
+{
+       int clen;
+
+       clen = (int)(drand48() * (MD5_MAX_CHALLENGE - MD5_MIN_CHALLENGE))
+               + MD5_MIN_CHALLENGE;
+       *cp++ = clen;
+       random_bytes(cp, clen);
+}
+
+static int
+chap_md5_verify_response(int id, char *name,
+                        unsigned char *secret, int secret_len,
+                        unsigned char *challenge, unsigned char *response,
+                        char *message, int message_space)
+{
+       MD5_CTX ctx;
+       unsigned char idbyte = id;
+       unsigned char hash[MD5_HASH_SIZE];
+       int challenge_len, response_len;
+
+       challenge_len = *challenge++;
+       response_len = *response++;
+       if (response_len == MD5_HASH_SIZE) {
+               /* Generate hash of ID, secret, challenge */
+               MD5Init(&ctx);
+               MD5Update(&ctx, &idbyte, 1);
+               MD5Update(&ctx, secret, secret_len);
+               MD5Update(&ctx, challenge, challenge_len);
+               MD5Final(hash, &ctx);
+
+               /* Test if our hash matches the peer's response */
+               if (memcmp(hash, response, MD5_HASH_SIZE) == 0) {
+                       slprintf(message, message_space, "Access granted");
+                       return 1;
+               }
+       }
+       slprintf(message, message_space, "Access denied");
+       return 0;
+}
+
+static void
+chap_md5_make_response(unsigned char *response, int id, char *our_name,
+                      unsigned char *challenge, char *secret, int secret_len,
+                      unsigned char *private)
+{
+       MD5_CTX ctx;
+       unsigned char idbyte = id;
+       int challenge_len = *challenge++;
+
+       MD5Init(&ctx);
+       MD5Update(&ctx, &idbyte, 1);
+       MD5Update(&ctx, secret, secret_len);
+       MD5Update(&ctx, challenge, challenge_len);
+       MD5Final(&response[1], &ctx);
+       response[0] = MD5_HASH_SIZE;
+}
+
+static struct chap_digest_type md5_digest = {
+       CHAP_MD5,               /* code */
+       chap_md5_generate_challenge,
+       chap_md5_verify_response,
+       chap_md5_make_response,
+       NULL,                   /* check_success */
+       NULL,                   /* handle_failure */
+};
+
+void
+chap_md5_init(void)
+{
+       chap_register_digest(&md5_digest);
+}
diff --git a/pppd/chap-md5.h b/pppd/chap-md5.h
new file mode 100644 (file)
index 0000000..d63a43f
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * chap-md5.h - New CHAP/MD5 implementation.
+ *
+ * Copyright (c) 2003 Paul Mackerras. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name(s) of the authors of this software must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission.
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Paul Mackerras
+ *     <paulus@samba.org>".
+ *
+ * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+extern void chap_md5_init(void);
diff --git a/pppd/chap-new.c b/pppd/chap-new.c
new file mode 100644 (file)
index 0000000..ef002d1
--- /dev/null
@@ -0,0 +1,636 @@
+/*
+ * chap-new.c - New CHAP implementation.
+ *
+ * Copyright (c) 2003 Paul Mackerras. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name(s) of the authors of this software must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission.
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Paul Mackerras
+ *     <paulus@samba.org>".
+ *
+ * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define RCSID  "$Id"
+
+#include <stdlib.h>
+#include <string.h>
+#include "pppd.h"
+#include "chap-new.h"
+#include "chap-md5.h"
+
+#ifdef CHAPMS
+#include "chap_ms.h"
+#endif
+
+/* Hook for a plugin to validate CHAP challenge */
+int (*chap_verify_hook)(char *name, char *ourname, int id,
+                       struct chap_digest_type *digest,
+                       unsigned char *challenge, unsigned char *response,
+                       unsigned char *message, int message_space) = NULL;
+
+/*
+ * Option variables.
+ */
+int chap_timeout_time = 3;
+int chap_max_transmits = 10;
+int chap_rechallenge_time = 0;
+
+/*
+ * Command-line options.
+ */
+static option_t chap_option_list[] = {
+       { "chap-restart", o_int, &chap_timeout_time,
+         "Set timeout for CHAP", OPT_PRIO },
+       { "chap-max-challenge", o_int, &chap_max_transmits,
+         "Set max #xmits for challenge", OPT_PRIO },
+       { "chap-interval", o_int, &chap_rechallenge_time,
+         "Set interval for rechallenge", OPT_PRIO },
+       { NULL }
+};
+
+/*
+ * Internal state.
+ */
+static struct chap_client_state {
+       int flags;
+       char *name;
+       struct chap_digest_type *digest;
+       unsigned char priv[64];         /* private area for digest's use */
+} client;
+
+/*
+ * These limits apply to challenge and response packets we send.
+ * The +4 is the +1 that we actually need rounded up.
+ */
+#define CHAL_MAX_PKTLEN        (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
+#define RESP_MAX_PKTLEN        (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
+
+static struct chap_server_state {
+       int flags;
+       int id;
+       char *name;
+       struct chap_digest_type *digest;
+       int challenge_xmits;
+       int challenge_pktlen;
+       unsigned char challenge[CHAL_MAX_PKTLEN];
+} server;
+
+/* Values for flags in chap_client_state and chap_server_state */
+#define LOWERUP                        1
+#define AUTH_STARTED           2
+#define AUTH_DONE              4
+#define AUTH_FAILED            8
+#define TIMEOUT_PENDING                0x10
+#define CHALLENGE_VALID                0x20
+
+/*
+ * Prototypes.
+ */
+static void chap_init(int unit);
+static void chap_lowerup(int unit);
+static void chap_lowerdown(int unit);
+static void chap_timeout(void *arg);
+static void chap_generate_challenge(struct chap_server_state *ss);
+static void chap_handle_response(struct chap_server_state *ss, int code,
+               unsigned char *pkt, int len);
+static int chap_verify_response(char *name, char *ourname, int id,
+               struct chap_digest_type *digest,
+               unsigned char *challenge, unsigned char *response,
+               unsigned char *message, int message_space);
+static void chap_respond(struct chap_client_state *cs, int id,
+               unsigned char *pkt, int len);
+static void chap_handle_status(struct chap_client_state *cs, int code, int id,
+               unsigned char *pkt, int len);
+static void chap_protrej(int unit);
+static void chap_input(int unit, unsigned char *pkt, int pktlen);
+static int chap_print_pkt(unsigned char *p, int plen,
+               void (*printer) __P((void *, char *, ...)), void *arg);
+
+/* List of digest types that we know about */
+static struct chap_digest_type *chap_digests;
+
+/*
+ * chap_init - reset to initial state.
+ */
+static void
+chap_init(int unit)
+{
+       memset(&client, 0, sizeof(client));
+       memset(&server, 0, sizeof(server));
+
+       chap_md5_init();
+#ifdef CHAPMS
+       chapms_init();
+#endif
+}
+
+/*
+ * Add a new digest type to the list.
+ */
+void
+chap_register_digest(struct chap_digest_type *dp)
+{
+       dp->next = chap_digests;
+       chap_digests = dp;
+}
+
+/*
+ * chap_lowerup - we can start doing stuff now.
+ */
+static void
+chap_lowerup(int unit)
+{
+       struct chap_client_state *cs = &client;
+       struct chap_server_state *ss = &server;
+
+       cs->flags |= LOWERUP;
+       ss->flags |= LOWERUP;
+       if (ss->flags & AUTH_STARTED)
+               chap_timeout(ss);
+}
+
+static void
+chap_lowerdown(int unit)
+{
+       struct chap_client_state *cs = &client;
+       struct chap_server_state *ss = &server;
+
+       cs->flags = 0;
+       if (ss->flags & TIMEOUT_PENDING)
+               UNTIMEOUT(chap_timeout, ss);
+       ss->flags = 0;
+}
+
+/*
+ * chap_auth_peer - Start authenticating the peer.
+ * If the lower layer is already up, we start sending challenges,
+ * otherwise we wait for the lower layer to come up.
+ */
+void
+chap_auth_peer(int unit, char *our_name, int digest_code)
+{
+       struct chap_server_state *ss = &server;
+       struct chap_digest_type *dp;
+
+       if (ss->flags & AUTH_STARTED) {
+               error("CHAP: peer authentication already started!");
+               return;
+       }
+       for (dp = chap_digests; dp != NULL; dp = dp->next)
+               if (dp->code == digest_code)
+                       break;
+       if (dp == NULL)
+               fatal("CHAP digest 0x%x requested but not available",
+                     digest_code);
+
+       ss->digest = dp;
+       ss->name = our_name;
+       /* Start with a random ID value */
+       ss->id = (unsigned char)(drand48() * 256);
+       ss->flags |= AUTH_STARTED;
+       if (ss->flags & LOWERUP)
+               chap_timeout(ss);
+}
+
+/*
+ * chap_auth_peer - Prepare to authenticate ourselves to the peer.
+ * There isn't much to do until we receive a challenge.
+ */
+void
+chap_auth_with_peer(int unit, char *our_name, int digest_code)
+{
+       struct chap_client_state *cs = &client;
+       struct chap_digest_type *dp;
+
+       if (cs->flags & AUTH_STARTED) {
+               error("CHAP: authentication with peer already started!");
+               return;
+       }
+       for (dp = chap_digests; dp != NULL; dp = dp->next)
+               if (dp->code == digest_code)
+                       break;
+       if (dp == NULL)
+               fatal("CHAP digest 0x%x requested but not available",
+                     digest_code);
+
+       cs->digest = dp;
+       cs->name = our_name;
+       cs->flags |= AUTH_STARTED;
+}
+
+/*
+ * chap_timeout - It's time to send another challenge to the peer.
+ * This could be either a retransmission of a previous challenge,
+ * or a new challenge to start re-authentication.
+ */
+static void
+chap_timeout(void *arg)
+{
+       struct chap_server_state *ss = arg;
+
+       ss->flags &= ~TIMEOUT_PENDING;
+       if ((ss->flags & CHALLENGE_VALID) == 0) {
+               ss->challenge_xmits = 0;
+               chap_generate_challenge(ss);
+               ss->flags |= CHALLENGE_VALID;
+       } else if (ss->challenge_xmits >= chap_max_transmits) {
+               ss->flags &= ~CHALLENGE_VALID;
+               ss->flags |= AUTH_DONE | AUTH_FAILED;
+               auth_peer_fail(0, PPP_CHAP);
+               return;
+       }
+
+       output(0, ss->challenge, ss->challenge_pktlen);
+       ++ss->challenge_xmits;
+       ss->flags |= TIMEOUT_PENDING;
+       TIMEOUT(chap_timeout, arg, chap_timeout_time);
+}
+
+/*
+ * chap_generate_challenge - generate a challenge string and format
+ * the challenge packet in ss->challenge_pkt.
+ */
+static void
+chap_generate_challenge(struct chap_server_state *ss)
+{
+       int clen = 1, nlen, len;
+       unsigned char *p;
+
+       p = ss->challenge;
+       MAKEHEADER(p, PPP_CHAP);
+       p += CHAP_HDRLEN;
+       ss->digest->generate_challenge(p);
+       clen = *p;
+       nlen = strlen(ss->name);
+       memcpy(p + 1 + clen, ss->name, nlen);
+
+       len = CHAP_HDRLEN + 1 + clen + nlen;
+       ss->challenge_pktlen = PPP_HDRLEN + len;
+
+       p = ss->challenge + PPP_HDRLEN;
+       p[0] = CHAP_CHALLENGE;
+       p[1] = ++ss->id;
+       p[2] = len >> 8;
+       p[3] = len;
+}
+
+/*
+ * chap_handle_response - check the response to our challenge.
+ */
+static void
+chap_handle_response(struct chap_server_state *ss, int id,
+                    unsigned char *pkt, int len)
+{
+       int response_len, ok, mlen;
+       unsigned char *response, *p;
+       unsigned char *name = NULL;     /* initialized to shut gcc up */
+       int (*verifier)(char *, char *, int, struct chap_digest_type *,
+               unsigned char *, unsigned char *, unsigned char *, int);
+       char rname[MAXNAMELEN+1];
+       unsigned char message[256];
+
+       if ((ss->flags & LOWERUP) == 0)
+               return;
+       if (id != ss->challenge[PPP_HDRLEN+1] || len < 2)
+               return;
+       if ((ss->flags & AUTH_DONE) == 0) {
+               if ((ss->flags & CHALLENGE_VALID) == 0)
+                       return;
+               response = pkt;
+               GETCHAR(response_len, pkt);
+               len -= response_len + 1;        /* length of name */
+               name = pkt + response_len;
+               if (len < 0)
+                       return;
+
+               ss->flags &= ~CHALLENGE_VALID;
+               if (ss->flags & TIMEOUT_PENDING) {
+                       ss->flags &= ~TIMEOUT_PENDING;
+                       UNTIMEOUT(chap_timeout, ss);
+               }
+
+               if (explicit_remote) {
+                       name = remote_name;
+               } else {
+                       /* Null terminate and clean remote name. */
+                       slprintf(rname, sizeof(rname), "%.*v", len, name);
+                       name = rname;
+               }
+
+               if (chap_verify_hook)
+                       verifier = chap_verify_hook;
+               else
+                       verifier = chap_verify_response;
+               ok = (*verifier)(name, ss->name, id, ss->digest,
+                                ss->challenge + PPP_HDRLEN + CHAP_HDRLEN,
+                                response, message, sizeof(message));
+               if (!ok || !auth_number()) {
+                       ss->flags |= AUTH_FAILED;
+                       warn("Peer %q failed CHAP authentication", name);
+               }
+       }
+
+       /* send the response */
+       p = outpacket_buf;
+       MAKEHEADER(p, PPP_CHAP);
+       mlen = strlen(message);
+       len = CHAP_HDRLEN + mlen;
+       p[0] = (ss->flags & AUTH_FAILED)? CHAP_FAILURE: CHAP_SUCCESS;
+       p[1] = id;
+       p[2] = len >> 8;
+       p[3] = len;
+       if (mlen > 0)
+               memcpy(p + CHAP_HDRLEN, message, mlen);
+       output(0, outpacket_buf, PPP_HDRLEN + len);
+
+       if ((ss->flags & AUTH_DONE) == 0) {
+               ss->flags |= AUTH_DONE;
+               if (ss->flags & AUTH_FAILED) {
+                       auth_peer_fail(0, PPP_CHAP);
+               } else {
+                       auth_peer_success(0, PPP_CHAP, ss->digest->code,
+                                         name, strlen(name));
+                       if (chap_rechallenge_time) {
+                               ss->flags |= TIMEOUT_PENDING;
+                               TIMEOUT(chap_timeout, ss,
+                                       chap_rechallenge_time);
+                       }
+               }
+       }
+}
+
+/*
+ * chap_verify_response - check whether the peer's response matches
+ * what we think it should be.  Returns 1 if it does (authentication
+ * succeeded), or 0 if it doesn't.
+ */
+static int
+chap_verify_response(char *name, char *ourname, int id,
+                    struct chap_digest_type *digest,
+                    unsigned char *challenge, unsigned char *response,
+                    unsigned char *message, int message_space)
+{
+       int ok;
+       char secret[MAXSECRETLEN];
+       int secret_len;
+
+       /* Get the secret that the peer is supposed to know */
+       if (!get_secret(0, name, ourname, secret, &secret_len, 1)) {
+               error("No CHAP secret found for authenticating %q", name);
+               return 0;
+       }
+
+       ok = digest->verify_response(id, name, secret, secret_len, challenge,
+                                    response, message, message_space);
+       memset(secret, 0, sizeof(secret));
+
+       return ok;
+}
+
+/*
+ * chap_respond - Generate and send a response to a challenge.
+ */
+static void
+chap_respond(struct chap_client_state *cs, int id,
+            unsigned char *pkt, int len)
+{
+       int clen, nlen;
+       int secret_len;
+       unsigned char *p;
+       unsigned char response[RESP_MAX_PKTLEN];
+       char rname[MAXNAMELEN+1];
+       char secret[MAXSECRETLEN+1];
+
+       if ((cs->flags & (LOWERUP | AUTH_STARTED)) != (LOWERUP | AUTH_STARTED))
+               return;         /* not ready */
+       if (len < 2 || len < pkt[0] + 1)
+               return;         /* too short */
+       clen = pkt[0];
+       nlen = len - (clen + 1);
+
+       /* Null terminate and clean remote name. */
+       slprintf(rname, sizeof(rname), "%.*v", nlen, pkt + clen + 1);
+
+       /* Microsoft doesn't send their name back in the PPP packet */
+       if (explicit_remote || (remote_name[0] != 0 && rname[0] == 0))
+               strlcpy(rname, remote_name, sizeof(rname));
+
+       /* get secret for authenticating ourselves with the specified host */
+       if (!get_secret(0, cs->name, rname, secret, &secret_len, 0)) {
+               secret_len = 0; /* assume null secret if can't find one */
+               warn("No CHAP secret found for authenticating us to %q", rname);
+       }
+
+       p = response;
+       MAKEHEADER(p, PPP_CHAP);
+       p += CHAP_HDRLEN;
+
+       cs->digest->make_response(p, id, cs->name, pkt,
+                                 secret, secret_len, cs->priv);
+       memset(secret, 0, secret_len);
+
+       clen = *p;
+       nlen = strlen(cs->name);
+       memcpy(p + clen + 1, cs->name, nlen);
+
+       p = response + PPP_HDRLEN;
+       len = CHAP_HDRLEN + clen + 1 + nlen;
+       p[0] = CHAP_RESPONSE;
+       p[1] = id;
+       p[2] = len >> 8;
+       p[3] = len;
+
+       output(0, response, PPP_HDRLEN + len);
+}
+
+static void
+chap_handle_status(struct chap_client_state *cs, int code, int id,
+                  unsigned char *pkt, int len)
+{
+       const char *msg = NULL;
+
+       if ((cs->flags & (AUTH_DONE|AUTH_STARTED|LOWERUP))
+           != (AUTH_STARTED|LOWERUP))
+               return;
+       cs->flags |= AUTH_DONE;
+
+       if (code == CHAP_SUCCESS) {
+               /* used for MS-CHAP v2 mutual auth, yuck */
+               if (cs->digest->check_success != NULL) {
+                       if (!(*cs->digest->check_success)(pkt, len, cs->priv))
+                               code = CHAP_FAILURE;
+               } else
+                       msg = "CHAP authentication succeeded";
+       } else {
+               if (cs->digest->handle_failure != NULL)
+                       (*cs->digest->handle_failure)(pkt, len);
+               else
+                       msg = "CHAP authentication failed";
+       }
+       if (msg) {
+               if (len > 0)
+                       info("%s: %.*v", msg, len, pkt);
+               else
+                       info("%s", msg);
+       }
+       if (code == CHAP_SUCCESS)
+               auth_withpeer_success(0, PPP_CHAP, cs->digest->code);
+       else {
+               cs->flags |= AUTH_FAILED;
+               auth_withpeer_fail(0, PPP_CHAP);
+       }
+}
+
+static void
+chap_input(int unit, unsigned char *pkt, int pktlen)
+{
+       struct chap_client_state *cs = &client;
+       struct chap_server_state *ss = &server;
+       unsigned char code, id;
+       int len;
+
+       if (pktlen < CHAP_HDRLEN)
+               return;
+       GETCHAR(code, pkt);
+       GETCHAR(id, pkt);
+       GETSHORT(len, pkt);
+       if (len < CHAP_HDRLEN || len > pktlen)
+               return;
+       len -= CHAP_HDRLEN;
+
+       switch (code) {
+       case CHAP_CHALLENGE:
+               chap_respond(cs, id, pkt, len);
+               break;
+       case CHAP_RESPONSE:
+               chap_handle_response(ss, id, pkt, len);
+               break;
+       case CHAP_FAILURE:
+       case CHAP_SUCCESS:
+               chap_handle_status(cs, code, id, pkt, len);
+               break;
+       }
+}
+
+static void
+chap_protrej(int unit)
+{
+       struct chap_client_state *cs = &client;
+       struct chap_server_state *ss = &server;
+
+       if (ss->flags & TIMEOUT_PENDING) {
+               ss->flags &= ~TIMEOUT_PENDING;
+               UNTIMEOUT(chap_timeout, ss);
+       }
+       if (ss->flags & AUTH_STARTED) {
+               ss->flags = 0;
+               auth_peer_fail(0, PPP_CHAP);
+       }
+       if ((cs->flags & (AUTH_STARTED|AUTH_DONE)) == AUTH_STARTED) {
+               cs->flags &= ~AUTH_STARTED;
+               auth_withpeer_fail(0, PPP_CHAP);
+       }
+}
+
+/*
+ * chap_print_pkt - print the contents of a CHAP packet.
+ */
+static char *chap_code_names[] = {
+       "Challenge", "Response", "Success", "Failure"
+};
+
+static int
+chap_print_pkt(unsigned char *p, int plen,
+              void (*printer) __P((void *, char *, ...)), void *arg)
+{
+       int code, id, len;
+       int clen, nlen;
+       unsigned char x;
+
+       if (plen < CHAP_HDRLEN)
+               return 0;
+       GETCHAR(code, p);
+       GETCHAR(id, p);
+       GETSHORT(len, p);
+       if (len < CHAP_HDRLEN || len > plen)
+               return 0;
+
+       if (code >= 1 && code <= sizeof(chap_code_names) / sizeof(char *))
+               printer(arg, " %s", chap_code_names[code-1]);
+       else
+               printer(arg, " code=0x%x", code);
+       printer(arg, " id=0x%x", id);
+       len -= CHAP_HDRLEN;
+       switch (code) {
+       case CHAP_CHALLENGE:
+       case CHAP_RESPONSE:
+               if (len < 1)
+                       break;
+               clen = p[0];
+               if (len < clen + 1)
+                       break;
+               ++p;
+               nlen = len - clen - 1;
+               printer(arg, " <");
+               for (; clen > 0; --clen) {
+                       GETCHAR(x, p);
+                       printer(arg, "%.2x", x);
+               }
+               printer(arg, ">, name = ");
+               print_string((char *)p, nlen, printer, arg);
+               break;
+       case CHAP_FAILURE:
+       case CHAP_SUCCESS:
+               printer(arg, " ");
+               print_string((char *)p, len, printer, arg);
+               break;
+       default:
+               for (clen = len; clen > 0; --clen) {
+                       GETCHAR(x, p);
+                       printer(arg, " %.2x", x);
+               }
+       }
+
+       return len + CHAP_HDRLEN;
+}
+
+struct protent chap_protent = {
+       PPP_CHAP,
+       chap_init,
+       chap_input,
+       chap_protrej,
+       chap_lowerup,
+       chap_lowerdown,
+       NULL,           /* open */
+       NULL,           /* close */
+       chap_print_pkt,
+       NULL,           /* datainput */
+       1,              /* enabled_flag */
+       "CHAP",         /* name */
+       NULL,           /* data_name */
+       chap_option_list,
+       NULL,           /* check_options */
+};
diff --git a/pppd/chap-new.h b/pppd/chap-new.h
new file mode 100644 (file)
index 0000000..141667b
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * chap-new.c - New CHAP implementation.
+ *
+ * Copyright (c) 2003 Paul Mackerras. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name(s) of the authors of this software must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission.
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Paul Mackerras
+ *     <paulus@samba.org>".
+ *
+ * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * CHAP packets begin with a standard header with code, id, len (2 bytes).
+ */
+#define CHAP_HDRLEN    4
+
+/*
+ * Values for the code field.
+ */
+#define CHAP_CHALLENGE 1
+#define CHAP_RESPONSE  2
+#define CHAP_SUCCESS   3
+#define CHAP_FAILURE   4
+
+/*
+ * CHAP digest codes.
+ */
+#define CHAP_MD5               5
+#define CHAP_MICROSOFT         0x80
+#define CHAP_MICROSOFT_V2      0x81
+
+/*
+ * Semi-arbitrary limits on challenge and response fields.
+ */
+#define MAX_CHALLENGE_LEN      64
+#define MAX_RESPONSE_LEN       64
+
+/* bitmask of supported algorithms */
+#define MDTYPE_MICROSOFT_V2    0x1
+#define MDTYPE_MICROSOFT       0x2
+#define MDTYPE_MD5             0x4
+
+#ifdef CHAPMS
+#define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
+#else
+#define MDTYPE_ALL (MDTYPE_MD5)
+#endif
+#define MDTYPE_NONE 0
+
+/* Return the digest alg. ID for the most preferred digest type. */
+#define CHAP_DIGEST(mdtype) \
+    ((mdtype) & MDTYPE_MICROSOFT_V2)? CHAP_MICROSOFT_V2: \
+    ((mdtype) & MDTYPE_MICROSOFT)? CHAP_MICROSOFT: \
+    ((mdtype) & MDTYPE_MD5)? CHAP_MD5: \
+    0
+
+/* Return the bit flag (lsb set) for our most preferred digest type. */
+#define CHAP_MDTYPE(mdtype) ((mdtype) ^ ((mdtype) - 1)) & (mdtype)
+
+/* Return the bit flag for a given digest algorithm ID. */
+#define CHAP_MDTYPE_D(digest) \
+    ((digest) == CHAP_MICROSOFT_V2)? MDTYPE_MICROSOFT_V2: \
+    ((digest) == CHAP_MICROSOFT)? MDTYPE_MICROSOFT: \
+    ((digest) == CHAP_MD5)? MDTYPE_MD5: \
+    0
+
+/* Can we do the requested digest? */
+#define CHAP_CANDIGEST(mdtype, digest) \
+    ((digest) == CHAP_MICROSOFT_V2)? (mdtype) & MDTYPE_MICROSOFT_V2: \
+    ((digest) == CHAP_MICROSOFT)? (mdtype) & MDTYPE_MICROSOFT: \
+    ((digest) == CHAP_MD5)? (mdtype) & MDTYPE_MD5: \
+    0
+
+/*
+ * The code for each digest type has to supply one of these.
+ */
+struct chap_digest_type {
+       int code;
+
+       /*
+        * Note: challenge and response arguments below are formatted as
+        * a length byte followed by the actual challenge/response data.
+        */
+       void (*generate_challenge)(unsigned char *challenge);
+       int (*verify_response)(int id, char *name,
+               unsigned char *secret, int secret_len,
+               unsigned char *challenge, unsigned char *response,
+               char *message, int message_space);
+       void (*make_response)(unsigned char *response, int id, char *our_name,
+               unsigned char *challenge, char *secret, int secret_len,
+               unsigned char *priv);
+       int (*check_success)(unsigned char *pkt, int len, unsigned char *priv);
+       void (*handle_failure)(unsigned char *pkt, int len);
+
+       struct chap_digest_type *next;
+};
+
+/* Hook for a plugin to validate CHAP challenge */
+extern int (*chap_verify_hook)(char *name, char *ourname, int id,
+                       struct chap_digest_type *digest,
+                       unsigned char *challenge, unsigned char *response,
+                       unsigned char *message, int message_space);
+
+/* Called by digest code to register a digest type */
+extern void chap_register_digest(struct chap_digest_type *);
+
+/* Called by authentication code to start authenticating the peer. */
+extern void chap_auth_peer(int unit, char *our_name, int digest_code);
+
+/* Called by auth. code to start authenticating us to the peer. */
+extern void chap_auth_with_peer(int unit, char *our_name, int digest_code);
+
+/* Represents the CHAP protocol to the main pppd code */
+extern struct protent chap_protent;
index 69b2982412d1b54a507ff981e5c534d6a996bcb6..a489cc0d111aae8d61af1aed4432b06ddd5ce574 100644 (file)
@@ -74,7 +74,7 @@
  *
  */
 
-#define RCSID  "$Id: chap_ms.c,v 1.28 2003/01/10 07:12:36 fcusack Exp $"
+#define RCSID  "$Id: chap_ms.c,v 1.29 2003/06/11 23:56:26 paulus Exp $"
 
 #ifdef CHAPMS
 
 #include <unistd.h>
 
 #include "pppd.h"
-#include "chap.h"
+#include "chap-new.h"
 #include "chap_ms.h"
 #include "md4.h"
 #include "sha1.h"
 #include "pppcrypt.h"
+#include "magic.h"
 
 static const char rcsid[] = RCSID;
 
@@ -115,8 +116,6 @@ static void Set_Start_Key __P((u_char *, char *, int));
 static void    SetMasterKeys __P((char *, int, u_char[24], int));
 #endif
 
-extern double drand48 __P((void));
-
 #ifdef MSLANMAN
 bool   ms_lanman = 0;          /* Use LanMan password instead of NT */
                                /* Has meaning only with MS-CHAP challenges */
@@ -127,11 +126,309 @@ u_char mppe_send_key[MPPE_MAX_KEY_LEN];
 u_char mppe_recv_key[MPPE_MAX_KEY_LEN];
 int mppe_keys_set = 0;         /* Have the MPPE keys been set? */
 
+/* For MPPE debug */
+/* Use "[]|}{?/><,`!2&&(" (sans quotes) for RFC 3079 MS-CHAPv2 test value */
+static char *mschap_challenge = NULL;
+/* Use "!@\#$%^&*()_+:3|~" (sans quotes, backslash is to escape #) for ... */
+static char *mschap2_peer_challenge = NULL;
+
 #include "fsm.h"               /* Need to poke MPPE options */
 #include "ccp.h"
 #include <net/ppp-comp.h>
 #endif
 
+/*
+ * Command-line options.
+ */
+static option_t chapms_option_list[] = {
+#ifdef MSLANMAN
+       { "ms-lanman", o_bool, &ms_lanman,
+         "Use LanMan passwd when using MS-CHAP", 1 },
+#endif
+#ifdef DEBUGMPPEKEY
+       { "mschap-challenge", o_string, &mschap_challenge,
+         "specify CHAP challenge" },
+       { "mschap2-peer-challenge", o_string, &mschap2_peer_challenge,
+         "specify CHAP peer challenge" },
+#endif
+       { NULL }
+};
+
+/*
+ * chapms_generate_challenge - generate a challenge for MS-CHAP.
+ * For MS-CHAP the challenge length is fixed at 8 bytes.
+ * The length goes in challenge[0] and the actual challenge starts
+ * at challenge[1].
+ */
+static void
+chapms_generate_challenge(unsigned char *challenge)
+{
+       *challenge++ = 8;
+       if (mschap_challenge && strlen(mschap_challenge) == 8)
+               memcpy(challenge, mschap_challenge, 8);
+       else
+               random_bytes(challenge, 8);
+}
+
+static void
+chapms2_generate_challenge(unsigned char *challenge)
+{
+       *challenge++ = 16;
+       if (mschap_challenge && strlen(mschap_challenge) == 16)
+               memcpy(challenge, mschap_challenge, 16);
+       else
+               random_bytes(challenge, 16);
+}
+
+static int
+chapms_verify_response(int id, char *name,
+                      unsigned char *secret, int secret_len,
+                      unsigned char *challenge, unsigned char *response,
+                      char *message, int message_space)
+{
+       MS_ChapResponse *rmd = (MS_ChapResponse *) response;
+       MS_ChapResponse md;
+       int diff;
+       int challenge_len, response_len;
+
+       challenge_len = *challenge++;   /* skip length, is 8 */
+
+       response_len = *response++;
+       if (response_len != MS_CHAP_RESPONSE_LEN)
+               goto bad;
+
+#ifndef MSLANMAN
+       if (!rmd->UseNT[0]) {
+               /* Should really propagate this into the error packet. */
+               notice("Peer request for LANMAN auth not supported");
+               goto bad;
+       }
+#endif
+
+       /* Generate the expected response. */
+       ChapMS(challenge, secret, secret_len, &md);
+
+#ifdef MSLANMAN
+       /* Determine which part of response to verify against */
+       if (!rmd->UseNT[0])
+               diff = memcmp(&rmd->LANManResp, &md.LANManResp,
+                             sizeof(md.LANManResp));
+       else
+#endif
+               diff = memcmp(&rmd->NTResp, &md.NTResp, sizeof(md.NTResp));
+
+       if (diff == 0) {
+               slprintf(message, message_space, "Access granted");
+               return 1;
+       }
+
+ bad:
+       /* See comments below for MS-CHAP V2 */
+       slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0",
+                challenge_len, challenge);
+       return 0;
+}
+
+static int
+chapms2_verify_response(int id, char *name,
+                       unsigned char *secret, int secret_len,
+                       unsigned char *challenge, unsigned char *response,
+                       char *message, int message_space)
+{
+       MS_Chap2Response *rmd = (MS_Chap2Response *) response;
+       MS_Chap2Response md;
+       char saresponse[MS_AUTH_RESPONSE_LENGTH+1];
+       int challenge_len, response_len;
+
+       challenge_len = *challenge++;   /* skip length, is 16 */
+       response_len = *response++;
+       if (response_len != MS_CHAP2_RESPONSE_LEN)
+               goto bad;       /* not even the right length */
+
+       /* Generate the expected response and our mutual auth. */
+       ChapMS2(challenge, rmd->PeerChallenge, name,
+               secret, secret_len, &md,
+               saresponse, MS_CHAP2_AUTHENTICATOR);
+
+       /* compare MDs and send the appropriate status */
+       /*
+        * Per RFC 2759, success message must be formatted as
+        *     "S=<auth_string> M=<message>"
+        * where
+        *     <auth_string> is the Authenticator Response (mutual auth)
+        *     <message> is a text message
+        *
+        * However, some versions of Windows (win98 tested) do not know
+        * about the M=<message> part (required per RFC 2759) and flag
+        * it as an error (reported incorrectly as an encryption error
+        * to the user).  Since the RFC requires it, and it can be
+        * useful information, we supply it if the peer is a conforming
+        * system.  Luckily (?), win98 sets the Flags field to 0x04
+        * (contrary to RFC requirements) so we can use that to
+        * distinguish between conforming and non-conforming systems.
+        *
+        * Special thanks to Alex Swiridov <say@real.kharkov.ua> for
+        * help debugging this.
+        */
+       if (memcmp(md.NTResp, rmd->NTResp, sizeof(md.NTResp)) == 0) {
+               if (rmd->Flags[0])
+                       slprintf(message, message_space, "S=%s", saresponse);
+               else
+                       slprintf(message, message_space, "S=%s M=%s",
+                                saresponse, "Access granted");
+               return 1;
+       }
+
+ bad:
+       /*
+        * Failure message must be formatted as
+        *     "E=e R=r C=c V=v M=m"
+        * where
+        *     e = error code (we use 691, ERROR_AUTHENTICATION_FAILURE)
+        *     r = retry (we use 1, ok to retry)
+        *     c = challenge to use for next response, we reuse previous
+        *     v = Change Password version supported, we use 0
+        *     m = text message
+        *
+        * The M=m part is only for MS-CHAPv2.  Neither win2k nor
+        * win98 (others untested) display the message to the user anyway.
+        * They also both ignore the E=e code.
+        *
+        * Note that it's safe to reuse the same challenge as we don't
+        * actually accept another response based on the error message
+        * (and no clients try to resend a response anyway).
+        *
+        * Basically, this whole bit is useless code, even the small
+        * implementation here is only because of overspecification.
+        */
+       slprintf(message, message_space, "E=691 R=1 C=%0.*B V=0 M=%s",
+                challenge_len, challenge, "Access denied");
+       return 0;
+}
+
+static void
+chapms_make_response(unsigned char *response, int id, char *our_name,
+                    unsigned char *challenge, char *secret, int secret_len,
+                    unsigned char *private)
+{
+       challenge++;    /* skip length, should be 8 */
+       *response++ = MS_CHAP_RESPONSE_LEN;
+       ChapMS(challenge, secret, secret_len, (MS_ChapResponse *) response);
+}
+
+static void
+chapms2_make_response(unsigned char *response, int id, char *our_name,
+                     unsigned char *challenge, char *secret, int secret_len,
+                     unsigned char *private)
+{
+       challenge++;    /* skip length, should be 16 */
+       *response++ = MS_CHAP2_RESPONSE_LEN;
+       ChapMS2(challenge, mschap2_peer_challenge, our_name,
+               secret, secret_len,
+               (MS_Chap2Response *) response, private,
+               MS_CHAP2_AUTHENTICATEE);
+}
+
+static int
+chapms2_check_success(unsigned char *msg, int len, unsigned char *private)
+{
+       if ((len < MS_AUTH_RESPONSE_LENGTH + 2) || strncmp(msg, "S=", 2)) {
+               /* Packet does not start with "S=" */
+               error("MS-CHAPv2 Success packet is badly formed.");
+               return 0;
+       }
+       msg += 2;
+       len -= 2;
+       if (len < MS_AUTH_RESPONSE_LENGTH
+           || memcmp(msg, private, MS_AUTH_RESPONSE_LENGTH)) {
+               /* Authenticator Response did not match expected. */
+               error("MS-CHAPv2 mutual authentication failed.");
+               return 0;
+       }
+       /* Authenticator Response matches. */
+       msg += MS_AUTH_RESPONSE_LENGTH; /* Eat it */
+       len -= MS_AUTH_RESPONSE_LENGTH;
+       if ((len >= 3) && !strncmp(msg, " M=", 3)) {
+               msg += 3; /* Eat the delimiter */
+       } else if (len) {
+               /* Packet has extra text which does not begin " M=" */
+               error("MS-CHAPv2 Success packet is badly formed.");
+               return 0;
+       }
+       return 1;
+}
+
+static void
+chapms_handle_failure(unsigned char *inp, int len)
+{
+       int err;
+       char *p, *msg;
+
+       /* We want a null-terminated string for strxxx(). */
+       msg = malloc(len + 1);
+       if (!msg) {
+               notice("Out of memory in chapms_handle_failure");
+               return;
+       }
+       BCOPY(inp, msg, len);
+       msg[len] = 0;
+       p = msg;
+
+       /*
+        * Deal with MS-CHAP formatted failure messages; just print the
+        * M=<message> part (if any).  For MS-CHAP we're not really supposed
+        * to use M=<message>, but it shouldn't hurt.  See
+        * chapms[2]_verify_response.
+        */
+       if (!strncmp(p, "E=", 2))
+               err = strtol(p, NULL, 10); /* Remember the error code. */
+       else
+               goto print_msg; /* Message is badly formatted. */
+
+       if (len && ((p = strstr(p, " M=")) != NULL)) {
+               /* M=<message> field found. */
+               p += 3;
+       } else {
+               /* No M=<message>; use the error code. */
+               switch (err) {
+               case MS_CHAP_ERROR_RESTRICTED_LOGON_HOURS:
+                       p = "E=646 Restricted logon hours";
+                       break;
+
+               case MS_CHAP_ERROR_ACCT_DISABLED:
+                       p = "E=647 Account disabled";
+                       break;
+
+               case MS_CHAP_ERROR_PASSWD_EXPIRED:
+                       p = "E=648 Password expired";
+                       break;
+
+               case MS_CHAP_ERROR_NO_DIALIN_PERMISSION:
+                       p = "E=649 No dialin permission";
+                       break;
+
+               case MS_CHAP_ERROR_AUTHENTICATION_FAILURE:
+                       p = "E=691 Authentication failure";
+                       break;
+
+               case MS_CHAP_ERROR_CHANGING_PASSWORD:
+                       /* Should never see this, we don't support Change Password. */
+                       p = "E=709 Error changing password";
+                       break;
+
+               default:
+                       free(msg);
+                       error("Unknown MS-CHAP authentication failure: %.*v",
+                             len, inp);
+                       return;
+               }
+       }
+print_msg:
+       if (p != NULL)
+               error("MS-CHAP authentication failed: %v", p);
+       free(msg);
+}
+
 static void
 ChallengeResponse(u_char *challenge,
                  u_char PasswordHash[MD4_SIGNATURE_SIZE],
@@ -469,7 +766,7 @@ SetMasterKeys(char *secret, int secret_len, u_char NTResponse[24], int IsServer)
 
 
 void
-ChapMS(chap_state *cstate, u_char *rchallenge, char *secret, int secret_len,
+ChapMS(u_char *rchallenge, char *secret, int secret_len,
        MS_ChapResponse *response)
 {
 #if 0
@@ -488,8 +785,6 @@ ChapMS(chap_state *cstate, u_char *rchallenge, char *secret, int secret_len,
     response->UseNT[0] = 1;
 #endif
 
-    cstate->resp_length = MS_CHAP_RESPONSE_LEN;
-
 #ifdef MPPE
     Set_Start_Key(rchallenge, secret, secret_len);
     mppe_keys_set = 1;
@@ -508,9 +803,9 @@ ChapMS(chap_state *cstate, u_char *rchallenge, char *secret, int secret_len,
  * Authenticator Response.
  */
 void
-ChapMS2(chap_state *cstate, u_char *rchallenge, u_char *PeerChallenge,
+ChapMS2(u_char *rchallenge, u_char *PeerChallenge,
        char *user, char *secret, int secret_len, MS_Chap2Response *response,
-       u_char authResponse[MS_AUTH_RESPONSE_LENGTH+1], int authenticator)
+       u_char authResponse[], int authenticator)
 {
     /* ARGSUSED */
     u_char *p = response->PeerChallenge;
@@ -535,8 +830,6 @@ ChapMS2(chap_state *cstate, u_char *rchallenge, u_char *PeerChallenge,
                                  response->PeerChallenge, rchallenge,
                                  user, authResponse);
 
-    cstate->resp_length = MS_CHAP2_RESPONSE_LEN;
-
 #ifdef MPPE
     SetMasterKeys(secret, secret_len, response->NTResp, authenticator);
     mppe_keys_set = 1;
@@ -576,4 +869,30 @@ set_mppe_enc_types(int policy, int types)
 }
 #endif /* MPPE */
 
+static struct chap_digest_type chapms_digest = {
+       CHAP_MICROSOFT,         /* code */
+       chapms_generate_challenge,
+       chapms_verify_response,
+       chapms_make_response,
+       NULL,                   /* check_success */
+       chapms_handle_failure,
+};
+
+static struct chap_digest_type chapms2_digest = {
+       CHAP_MICROSOFT_V2,      /* code */
+       chapms2_generate_challenge,
+       chapms2_verify_response,
+       chapms2_make_response,
+       chapms2_check_success,
+       chapms_handle_failure,
+};
+
+void
+chapms_init(void)
+{
+       chap_register_digest(&chapms_digest);
+       chap_register_digest(&chapms2_digest);
+       add_options(chapms_option_list);
+}
+
 #endif /* CHAPMS */
index 251a4ba01de1f78826f4aee98f8978634c906a0c..4e2ac11b67171f84b7eefd58e3c6434152f89145 100644 (file)
@@ -27,7 +27,7 @@
  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $Id: chap_ms.h,v 1.9 2002/12/24 03:43:35 fcusack Exp $
+ * $Id: chap_ms.h,v 1.10 2003/06/11 23:56:26 paulus Exp $
  */
 
 #ifndef __CHAPMS_INCLUDE__
@@ -37,6 +37,8 @@
 
 #define MS_CHAP_RESPONSE_LEN   49      /* Response length for MS-CHAP */
 #define MS_CHAP2_RESPONSE_LEN  49      /* Response length for MS-CHAPv2 */
+#define MS_AUTH_RESPONSE_LENGTH        40      /* MS-CHAPv2 authenticator response, */
+                                       /* as ASCII */
 
 /* E=eeeeeeeeee error codes for MS-CHAP failure messages. */
 #define MS_CHAP_ERROR_RESTRICTED_LOGON_HOURS   646
@@ -87,13 +89,14 @@ extern void set_mppe_enc_types(int, int);
 #define MS_CHAP2_AUTHENTICATEE 0
 #define MS_CHAP2_AUTHENTICATOR 1
 
-#include "chap.h" /* chap_state, et al */
-void ChapMS __P((chap_state *, u_char *, char *, int, MS_ChapResponse *));
-void ChapMS2 __P((chap_state *, u_char *, u_char *, char *, char *, int,
+void ChapMS __P((u_char *, char *, int, MS_ChapResponse *));
+void ChapMS2 __P((u_char *, u_char *, char *, char *, int,
                  MS_Chap2Response *, u_char[MS_AUTH_RESPONSE_LENGTH+1], int));
 #ifdef MPPE
 void mppe_set_keys __P((u_char *, u_char[MD4_SIGNATURE_SIZE]));
 #endif
 
+void chapms_init(void);
+
 #define __CHAPMS_INCLUDE__
 #endif /* __CHAPMS_INCLUDE__ */
index 8c228380c860252c66ab052e6f45e424d910e5e5..6a50a599e45756b3471586ea22fa0a31603ba0fc 100644 (file)
@@ -43,7 +43,7 @@
  * Based on draft-ietf-pppext-eap-srp-03.txt.
  */
 
-#define RCSID  "$Id: eap.c,v 1.2 2002/11/14 21:50:12 fcusack Exp $"
+#define RCSID  "$Id: eap.c,v 1.3 2003/06/11 23:56:26 paulus Exp $"
 
 /*
  * TODO:
@@ -63,8 +63,6 @@
 #include "pppd.h"
 #include "pathnames.h"
 #include "md5.h"
-/* For MD5_SIGNATURE_SIZE and MIN/MAX_CHALLENGE_LENGTH; should fix. */
-#include "chap.h"
 #include "eap.h"
 
 #ifdef USE_SRP
index cf5adace7c1c103ae160fbbcc80b86a522ef913e..199d1849b8261093ebd0f54a1b100081f27d2f3f 100644 (file)
@@ -17,7 +17,7 @@
  *
  * Original version by James Carlson
  *
- * $Id: eap.h,v 1.1 2002/11/02 19:48:12 carlsonj Exp $
+ * $Id: eap.h,v 1.2 2003/06/11 23:56:26 paulus Exp $
  */
 
 #ifndef PPP_EAP_H
@@ -74,6 +74,10 @@ extern "C" {
 #define        SRP_PSEUDO_ID   "pseudo_"
 #define        SRP_PSEUDO_LEN  7
 
+#define MD5_SIGNATURE_SIZE     16
+#define MIN_CHALLENGE_LENGTH   16
+#define MAX_CHALLENGE_LENGTH   24
+
 enum eap_state_code {
        eapInitial = 0, /* No EAP authentication yet requested */
        eapPending,     /* Waiting for LCP (no timer) */
index cf2f46d32c2106599e10b49ba14ba9a829ed072a..88f43db1b66175d2b2cb8b80c56c6a97bb391b01 100644 (file)
@@ -40,7 +40,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: lcp.c,v 1.68 2003/06/11 23:50:53 paulus Exp $"
+#define RCSID  "$Id: lcp.c,v 1.69 2003/06/11 23:56:26 paulus Exp $"
 
 /*
  * TODO:
@@ -53,7 +53,7 @@
 #include "pppd.h"
 #include "fsm.h"
 #include "lcp.h"
-#include "chap.h"
+#include "chap-new.h"
 #include "magic.h"
 
 static const char rcsid[] = RCSID;
@@ -2038,7 +2038,7 @@ lcp_printpkt(p, plen, printer, arg)
                        printer(arg, "chap");
                        if (p < optend) {
                            switch (*p) {
-                           case CHAP_DIGEST_MD5:
+                           case CHAP_MD5:
                                printer(arg, " MD5");
                                ++p;
                                break;
index 2e0426c5c6af9839469c7568206541a6977a1138..2fb23ff4adf48b68e6da309eb90373cd6bcc1d0f 100644 (file)
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: magic.c,v 1.10 2002/12/04 23:03:32 paulus Exp $"
+#define RCSID  "$Id: magic.c,v 1.11 2003/06/11 23:56:26 paulus Exp $"
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/time.h>
@@ -82,6 +83,18 @@ magic()
     return (u_int32_t) mrand48();
 }
 
+/*
+ * random_bytes - Fill a buffer with random bytes.
+ */
+void
+random_bytes(unsigned char *buf, int len)
+{
+       int i;
+
+       for (i = 0; i < len; ++i)
+               buf[i] = mrand48() >> 24;
+}
+
 #ifdef NO_DRAND48
 /*
  * Substitute procedures for those systems which don't have
index 38718fb5b955662e0fd47c12ee4fbb360754c3fe..c81213b40545bd204818a4f1053c6aeba5151a53 100644 (file)
  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * $Id: magic.h,v 1.4 2002/12/04 23:03:32 paulus Exp $
+ * $Id: magic.h,v 1.5 2003/06/11 23:56:26 paulus Exp $
  */
 
 void magic_init __P((void));   /* Initialize the magic number generator */
 u_int32_t magic __P((void));   /* Returns the next magic number */
+
+/* Fill buffer with random bytes */
+void random_bytes __P((unsigned char *buf, int len));
index 8511503f78aa8296ac839df7728bb161542d82c7..640782ad6f35d03b02f08d583733669734943acd 100644 (file)
@@ -40,7 +40,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: main.c,v 1.127 2003/05/12 07:31:36 fcusack Exp $"
+#define RCSID  "$Id: main.c,v 1.128 2003/06/11 23:56:26 paulus Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -74,7 +74,7 @@
 #include "ipv6cp.h"
 #endif
 #include "upap.h"
-#include "chap.h"
+#include "chap-new.h"
 #include "eap.h"
 #include "ccp.h"
 #include "ecp.h"