#define RCSID "$Id: eap.c,v 1.2 2002/03/13 22:44:34 callie Exp $"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include "pppd.h"
#include "eap.h"
#include "md5.h"
#include "fsm.h"
#include "lcp.h"
static const char rcsid[] = RCSID;
static option_t eap_option_list[] = {
{ "eap-restart", o_int, &eap[0].timeouttime,
"Set timeout for EAP" },
{ "eap-max-challenge", o_int, &eap[0].max_transmits,
"Set max #xmits for challenge" },
{ "eap-interval", o_int, &eap[0].chal_interval,
"Set interval for rechallenge" },
{ NULL }
};
static void EapInit __P((int));
static void EapLowerUp __P((int));
static void EapLowerDown __P((int));
static void EapInput __P((int, u_char *, int));
static void EapProtocolReject __P((int));
static int EapPrintPkt __P((u_char *, int,
void (*) __P((void *, char *, ...)), void *));
struct protent eap_protent = {
PPP_EAP,
EapInit,
EapInput,
EapProtocolReject,
EapLowerUp,
EapLowerDown,
NULL,
NULL,
EapPrintPkt,
NULL,
1,
"EAP",
NULL,
eap_option_list,
NULL,
NULL,
NULL
};
eap_state eap[NUM_PPP];
static void EapChallengeTimeout __P((void *));
static void EapReceiveRequest __P((eap_state *, u_char *, int, int));
static void EapRechallenge __P((void *));
static void EapReceiveResponse __P((eap_state *, u_char *, int, int));
static void EapReceiveSuccess __P((eap_state *, u_char *, int, int));
static void EapReceiveFailure __P((eap_state *, u_char *, int, int));
static void EapSendStatus __P((eap_state *, int));
static void EapSendChallenge __P((eap_state *));
static void EapSendResponse __P((eap_state *));
void EapGenChallenge __P((eap_state *));
extern double drand48 __P((void));
extern void srand48 __P((long));
static void
EapInit(unit)
int unit;
{
eap_state *cstate = &eap[unit];
BZERO(cstate, sizeof(*cstate));
cstate->unit = unit;
cstate->clientstate = EAPCS_INITIAL;
cstate->serverstate = EAPSS_INITIAL;
cstate->timeouttime = EAP_DEFTIMEOUT;
cstate->max_transmits = EAP_DEFTRANSMITS;
}
void
EapAuthWithPeer(unit, our_name)
int unit;
char *our_name;
{
eap_state *cstate = &eap[unit];
int digest;
cstate->resp_name = our_name;
cstate->resp_type = digest;
if (cstate->clientstate == EAPCS_INITIAL ||
cstate->clientstate == EAPCS_PENDING) {
cstate->clientstate = EAPCS_PENDING;
return;
}
cstate->clientstate = EAPCS_LISTEN;
}
void
EapAuthPeer(unit, our_name)
int unit;
char *our_name;
{
eap_state *cstate = &eap[unit];
int digest;
cstate->chal_name = our_name;
cstate->chal_type = digest;
if (cstate->serverstate == EAPSS_INITIAL ||
cstate->serverstate == EAPSS_PENDING) {
cstate->serverstate = EAPSS_PENDING;
return;
}
EapGenChallenge(cstate);
EapSendChallenge(cstate);
cstate->serverstate = EAPSS_INITIAL_CHAL;
}
static void
EapChallengeTimeout(arg)
void *arg;
{
eap_state *cstate = (eap_state *) arg;
if (cstate->serverstate != EAPSS_INITIAL_CHAL &&
cstate->serverstate != EAPSS_RECHALLENGE)
return;
if (cstate->chal_transmits >= cstate->max_transmits) {
error("Peer failed to respond to EAP challenge");
cstate->serverstate = EAPSS_BADAUTH;
auth_peer_fail(cstate->unit, PPP_EAP);
return;
}
EapSendChallenge(cstate);
}
static void
EapRechallenge(arg)
void *arg;
{
eap_state *cstate = (eap_state *) arg;
if (cstate->serverstate != EAPSS_OPEN)
return;
EapGenChallenge(cstate);
EapSendChallenge(cstate);
cstate->serverstate = EAPSS_RECHALLENGE;
}
static void
EapLowerUp(unit)
int unit;
{
eap_state *cstate = &eap[unit];
if (cstate->clientstate == EAPCS_INITIAL)
cstate->clientstate = EAPCS_CLOSED;
else if (cstate->clientstate == EAPCS_PENDING)
cstate->clientstate = EAPCS_LISTEN;
if (cstate->serverstate == EAPSS_INITIAL)
cstate->serverstate = EAPSS_CLOSED;
else if (cstate->serverstate == EAPSS_PENDING) {
EapGenChallenge(cstate);
EapSendChallenge(cstate);
cstate->serverstate = EAPSS_INITIAL_CHAL;
}
}
static void
EapLowerDown(unit)
int unit;
{
eap_state *cstate = &eap[unit];
if (cstate->serverstate == EAPSS_INITIAL_CHAL ||
cstate->serverstate == EAPSS_RECHALLENGE)
UNTIMEOUT(EapChallengeTimeout, cstate);
else if (cstate->serverstate == EAPSS_OPEN
&& cstate->chal_interval != 0)
UNTIMEOUT(EapRechallenge, cstate);
cstate->clientstate = EAPCS_INITIAL;
cstate->serverstate = EAPSS_INITIAL;
}
static void
EapProtocolReject(unit)
int unit;
{
eap_state *cstate = &eap[unit];
if (cstate->serverstate != EAPSS_INITIAL &&
cstate->serverstate != EAPSS_CLOSED)
auth_peer_fail(unit, PPP_EAP);
if (cstate->clientstate != EAPCS_INITIAL &&
cstate->clientstate != EAPCS_CLOSED)
auth_withpeer_fail(unit, PPP_EAP);
EapLowerDown(unit);
}
static void
EapInput(unit, inpacket, packet_len)
int unit;
u_char *inpacket;
int packet_len;
{
eap_state *cstate = &eap[unit];
u_char *inp;
u_char code, id;
int len;
inp = inpacket;
if (packet_len < EAP_HEADERLEN) {
EAPDEBUG(("EapInput: rcvd short header."));
return;
}
GETCHAR(code, inp);
GETCHAR(id, inp);
GETSHORT(len, inp);
if (len < EAP_HEADERLEN) {
EAPDEBUG(("EapInput: rcvd illegal length."));
return;
}
if (len > packet_len) {
EAPDEBUG(("EapInput: rcvd short packet."));
return;
}
len -= EAP_HEADERLEN;
switch (code) {
case EAP_REQUEST:
EapReceiveRequest(cstate, inp, id, len);
break;
case EAP_RESPONSE:
EapReceiveResponse(cstate, inp, id, len);
break;
case EAP_FAILURE:
EapReceiveFailure(cstate, inp, id, len);
break;
case EAP_SUCCESS:
EapReceiveSuccess(cstate, inp, id, len);
break;
default:
warn("Unknown EAP code (%d) received.", code);
break;
}
}
static void
EapReceiveRequest(cstate, inp, id, len)
eap_state *cstate;
u_char *inp;
int id;
int len;
{
int req, rchallenge_len, i;
u_char *rchallenge;
int secret_len;
char secret[MAXSECRETLEN];
char rhostname[256];
MD5_CTX mdContext;
u_char hash[MD5_SIGNATURE_SIZE];
if (cstate->clientstate == EAPCS_CLOSED ||
cstate->clientstate == EAPCS_PENDING) {
EAPDEBUG(("EapReceiveRequest: in state %d", cstate->clientstate));
return;
}
if (len < 1) {
EAPDEBUG(("EapReceiveRequest: rcvd short packet."));
return;
}
GETCHAR(req, inp);
len -= 1;
cstate->resp_id = id;
cstate->resp_type = req;
cstate->resp_length = 0;
switch (req) {
case EAP_REQ_IDENTITY:
strcpy(cstate->response, user);
cstate->resp_length = strlen(user);
break;
case EAP_REQ_NOTIFICATION:
break;
case EAP_REQ_NAK:
break;
case EAP_REQ_MD5CHALLENGE:
GETCHAR(rchallenge_len, inp);
len -= sizeof (u_char) + rchallenge_len;
if (len < 0) {
CHAPDEBUG(("EapReceiveRequest: rcvd short packet."));
return;
}
rchallenge = inp;
INCPTR(rchallenge_len, inp);
if (len >= sizeof(rhostname))
len = sizeof(rhostname) - 1;
BCOPY(inp, rhostname, len);
rhostname[len] = '\000';
if (explicit_remote || (remote_name[0] != 0 && rhostname[0] == 0)) {
strlcpy(rhostname, remote_name, sizeof(rhostname));
EAPDEBUG(("EapReceiveRequest: using '%q' as remote name",
rhostname));
}
if (!get_secret(cstate->unit, cstate->resp_name, rhostname,
secret, &secret_len, 0)) {
secret_len = 0;
warn("No CHAP(EAP) secret found for authenticating us to %q", rhostname);
}
MD5Init(&mdContext);
MD5Update(&mdContext, &cstate->resp_id, 1);
EAPDEBUG(("id = %d",cstate->resp_id));
EAPDEBUG(("secret = %s, len = %d", secret, secret_len));
MD5Update(&mdContext, secret, secret_len);
MD5Update(&mdContext, rchallenge, rchallenge_len);
MD5Final(hash, &mdContext);
BCOPY(hash, cstate->response+1, MD5_SIGNATURE_SIZE);
cstate->response[0] = MD5_SIGNATURE_SIZE;
cstate->resp_length = MD5_SIGNATURE_SIZE + 1;
BCOPY(user, cstate->response + MD5_SIGNATURE_SIZE + 1, strlen(user));
cstate->resp_length += strlen(user);
break;
default:
;
}
BZERO(secret, sizeof(secret));
EapSendResponse(cstate);
}
static void
EapReceiveResponse(cstate, inp, id, len)
eap_state *cstate;
u_char *inp;
int id;
int len;
{
u_char *remmd, remmd_len;
int secret_len, old_state;
int code;
char rhostname[256];
MD5_CTX mdContext;
char secret[MAXSECRETLEN];
u_char hash[MD5_SIGNATURE_SIZE];
if (cstate->serverstate == EAPSS_CLOSED ||
cstate->serverstate == EAPSS_PENDING) {
EAPDEBUG(("EapReceiveResponse: in state %d", cstate->serverstate));
return;
}
if (id != cstate->chal_id)
return;
if (cstate->serverstate == EAPSS_OPEN) {
EapSendStatus(cstate, EAP_SUCCESS);
return;
}
if (cstate->serverstate == EAPSS_BADAUTH) {
EapSendStatus(cstate, EAP_FAILURE);
return;
}
if (len < 2) {
EAPDEBUG(("EapReceiveResponse: rcvd short packet."));
return;
}
GETCHAR(remmd_len, inp);
remmd = inp;
INCPTR(remmd_len, inp);
len -= sizeof (u_char) + remmd_len;
if (len < 0) {
EAPDEBUG(("EapReceiveResponse: rcvd short packet."));
return;
}
UNTIMEOUT(EapChallengeTimeout, cstate);
if (len >= sizeof(rhostname))
len = sizeof(rhostname) - 1;
BCOPY(inp, rhostname, len);
rhostname[len] = '\000';
code = EAP_FAILURE;
if (!get_secret(cstate->unit, (explicit_remote? remote_name: rhostname),
cstate->chal_name, secret, &secret_len, 1)) {
warn("No EAP secret found for authenticating %q", rhostname);
} else {
switch (cstate->chal_type) {
case EAP_DIGEST_MD5:
EAPDEBUG(("EapReceiveResponse: rcvd type EAP-DIGEST-MD5"));
if (remmd_len != MD5_SIGNATURE_SIZE)
break;
MD5Init(&mdContext);
MD5Update(&mdContext, &cstate->chal_id, 1);
MD5Update(&mdContext, secret, secret_len);
MD5Update(&mdContext, cstate->challenge, cstate->chal_len);
MD5Final(hash, &mdContext);
if (memcmp (hash, remmd, MD5_SIGNATURE_SIZE) == 0)
code = EAP_SUCCESS;
break;
default:
EAPDEBUG(("unknown digest type %d", cstate->chal_type));
}
}
BZERO(secret, sizeof(secret));
EapSendStatus(cstate, code);
if ((code == EAP_SUCCESS) || (code == EAP_SUCCESS_R)) {
old_state = cstate->serverstate;
cstate->serverstate = EAPSS_OPEN;
if (old_state == EAPSS_INITIAL_CHAL) {
auth_peer_success(cstate->unit, PPP_EAP, rhostname, len);
}
if (cstate->chal_interval != 0)
TIMEOUT(EapRechallenge, cstate, cstate->chal_interval);
switch (cstate->chal_type) {
case EAP_DIGEST_MD5:
notice("EAP peer authentication succeeded for %q", rhostname);
break;
default:
notice("EAP (unknown) peer authentication succeeded for %q",
rhostname);
break;
}
} else {
switch (cstate->chal_type) {
case EAP_DIGEST_MD5:
error("EAP peer authentication failed for remote host %q",
rhostname);
break;
default:
error("EAP (unknown) peer authentication failed for remote host %q", rhostname);
break;
}
cstate->serverstate = EAPSS_BADAUTH;
auth_peer_fail(cstate->unit, PPP_EAP);
}
}
static void
EapReceiveSuccess(cstate, inp, id, len)
eap_state *cstate;
u_char *inp;
u_char id;
int len;
{
if (cstate->clientstate == EAPCS_OPEN)
return;
if (cstate->clientstate != EAPCS_RESPONSE) {
EAPDEBUG(("EapReceiveSuccess: in state %d\n", cstate->clientstate));
return;
}
if (len > 0)
PRINTMSG(inp, len);
cstate->clientstate = EAPCS_OPEN;
auth_withpeer_success(cstate->unit, PPP_EAP);
}
static void
EapReceiveFailure(cstate, inp, id, len)
eap_state *cstate;
u_char *inp;
u_char id;
int len;
{
if (len > 0)
PRINTMSG(inp, len);
error("EAP authentication failed");
auth_withpeer_fail(cstate->unit, PPP_EAP);
}
static void
EapSendChallenge(cstate)
eap_state *cstate;
{
u_char *outp;
int chal_len, name_len;
int outlen;
chal_len = cstate->chal_len;
name_len = strlen(cstate->chal_name);
outlen = EAP_HEADERLEN + sizeof (u_char) + chal_len + name_len;
outp = outpacket_buf;
MAKEHEADER(outp, PPP_EAP);
PUTCHAR(EAP_REQUEST, outp);
PUTCHAR(cstate->chal_id, outp);
PUTSHORT(outlen, outp);
PUTCHAR(chal_len, outp);
BCOPY(cstate->challenge, outp, chal_len);
INCPTR(chal_len, outp);
BCOPY(cstate->chal_name, outp, name_len);
output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
TIMEOUT(EapChallengeTimeout, cstate, cstate->timeouttime);
++cstate->chal_transmits;
}
static void
EapSendStatus(cstate, code)
eap_state *cstate;
int code;
{
u_char *outp;
int outlen, msglen;
char msg[256];
if (code == EAP_SUCCESS)
slprintf(msg, sizeof(msg), "Welcome to %s.", hostname);
else if(code == EAP_SUCCESS_R)
strcpy(msg, cstate->response);
else
slprintf(msg, sizeof(msg), "I don't like you. Go 'way.");
msglen = strlen(msg);
outlen = EAP_HEADERLEN + msglen;
outp = outpacket_buf;
MAKEHEADER(outp, PPP_EAP);
PUTCHAR(code == EAP_SUCCESS_R ? EAP_SUCCESS : code, outp);
PUTCHAR(cstate->chal_id, outp);
PUTSHORT(outlen, outp);
BCOPY(msg, outp, msglen);
output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
}
void
EapGenChallenge(cstate)
eap_state *cstate;
{
int chal_len;
u_char *ptr = cstate->challenge;
int i;
chal_len = (unsigned) ((drand48() *
(MAX_CHALLENGE_LENGTH - MIN_CHALLENGE_LENGTH)) +
MIN_CHALLENGE_LENGTH);
cstate->chal_len = chal_len;
cstate->chal_id = ++cstate->id;
cstate->chal_transmits = 0;
for (i = 0; i < chal_len; i++)
*ptr++ = (char) (drand48() * 0xff);
}
static void
EapSendResponse(cstate)
eap_state *cstate;
{
u_char *outp;
int outlen;
outlen = EAP_HEADERLEN + sizeof (u_char) + cstate->resp_length;
outp = outpacket_buf;
MAKEHEADER(outp, PPP_EAP);
PUTCHAR(EAP_RESPONSE, outp);
PUTCHAR(cstate->resp_id, outp);
PUTSHORT(outlen, outp);
PUTCHAR(cstate->resp_type, outp);
BCOPY(cstate->response, outp, cstate->resp_length);
output(cstate->unit, outpacket_buf, outlen + PPP_HDRLEN);
}
static char *EapCodenames[] = {
"Request", "Response", "Success", "Failure"
};
static char *EapRequestnames[] = {
"Identity", "Notification", "Nak", "MD5-Challenge"
};
static int
EapPrintPkt(p, plen, printer, arg)
u_char *p;
int plen;
void (*printer) __P((void *, char *, ...));
void *arg;
{
int code, id, len, req;
int clen, nlen;
u_char x;
if (plen < EAP_HEADERLEN)
return 0;
GETCHAR(code, p);
GETCHAR(id, p);
GETSHORT(len, p);
if (len < EAP_HEADERLEN || len > plen)
return 0;
if (code >= 1 && code <= sizeof(EapCodenames) / sizeof(char *))
printer(arg, " %s", EapCodenames[code-1]);
else
printer(arg, " code=0x%x", code);
printer(arg, " id=0x%x", id);
len -= EAP_HEADERLEN;
switch (code) {
case EAP_REQUEST:
case EAP_RESPONSE:
if (len < 1)
break;
GETCHAR(req, p);
if (req >= 1 && req <= sizeof(EapRequestnames) / sizeof(char *))
printer(arg, " %s", EapRequestnames[req-1]);
else
printer(arg, " type=0x%x", req);
len -= 1;
if (len == 0)
break;
printer(arg, " <");
switch (req) {
case EAP_REQ_IDENTITY:
case EAP_REQ_NOTIFICATION:
print_string((char *)p, len, printer, arg);
break;
case EAP_REQ_MD5CHALLENGE:
GETCHAR(clen, p);
nlen = clen;
if ((nlen + 1) > len)
break;
for (; clen > 0; clen--) {
GETCHAR(x, p);
printer(arg, "%.2x", x);
}
clen = len - nlen - 1;
if (clen) {
printer(arg, ">");
printer(arg, " <");
print_string((char *)p, clen, printer, arg);
}
break;
default :
clen = len;
for (; clen > 0; clen--) {
GETCHAR(x, p);
printer(arg, "%.2x", x);
}
}
printer(arg, ">");
break;
case EAP_FAILURE:
case EAP_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 + EAP_HEADERLEN + 1;
}