2 * eapcommon.c rfc2284 & rfc2869 implementation
4 * code common to clients and to servers.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Copyright 2000-2003,2006 The FreeRADIUS server project
23 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
24 * Copyright 2003 Alan DeKok <aland@freeradius.org>
25 * Copyright 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
31 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Code | Identifier | Length |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * EAP Request and Response Packet Format
40 * --- ------- --- -------- ------ ------
42 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Code | Identifier | Length |
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * | Type | Type-Data ...
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
50 * EAP Success and Failure Packet Format
51 * --- ------- --- ------- ------ ------
53 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | Code | Identifier | Length |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 #include <freeradius-devel/ident.h>
63 #include <freeradius-devel/autoconf.h>
64 #include <freeradius-devel/missing.h>
65 #include <freeradius-devel/libradius.h>
66 #include "eap_types.h"
68 static const char *eap_types[] = {
87 "sim", /* 18 GSM-SIM authentication */
98 "cisco_mschapv2", /* 29 */
110 #define MAX_EAP_TYPE_NAME 39
113 * Return an EAP-Type for a particular name.
115 int eaptype_name2type(const char *name)
119 for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
120 if (strcmp(name, eap_types[i]) == 0) {
129 * Returns a text string containing the name of the EAP type.
131 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
135 if (type > MAX_EAP_TYPE_NAME) {
137 * Prefer the dictionary name over a number,
140 dval = dict_valbyattr(PW_EAP_TYPE, type);
142 snprintf(buffer, buflen, "%s", dval->name);
145 snprintf(buffer, buflen, "%d", type);
147 } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
149 * Prefer the dictionary name, if it exists.
151 dval = dict_valbyattr(PW_EAP_TYPE, type);
153 snprintf(buffer, buflen, "%s", dval->name);
155 } /* else it wasn't in the dictionary */
156 } /* else the name in the array was non-numeric */
159 * Return the name, whatever it is.
161 return eap_types[type];
165 * EAP packet format to be sent over the wire
167 * i.e. code+id+length+data where data = null/type+typedata
170 * INPUT to function is reply->code
172 * reply->type - setup with data
174 * OUTPUT reply->packet is setup with wire format, and will
175 * be malloc()'ed to the right size.
178 int eap_wireformat(EAP_PACKET *reply)
182 uint16_t total_length = 0;
184 if (reply == NULL) return EAP_INVALID;
187 * If reply->packet is set, then the wire format
188 * has already been calculated, just succeed.
190 if(reply->packet != NULL) return EAP_VALID;
192 total_length = EAP_HEADER_LEN;
193 if (reply->code < 3) {
194 total_length += 1/*EAPtype*/;
195 if (reply->type.data && reply->type.length > 0) {
196 total_length += reply->type.length;
200 reply->packet = (unsigned char *)malloc(total_length);
201 hdr = (eap_packet_t *)reply->packet;
203 radlog(L_ERR, "rlm_eap: out of memory");
207 hdr->code = (reply->code & 0xFF);
208 hdr->id = (reply->id & 0xFF);
209 total_length = htons(total_length);
210 memcpy(hdr->length, &total_length, sizeof(uint16_t));
213 * Request and Response packets are special.
215 if ((reply->code == PW_EAP_REQUEST) ||
216 (reply->code == PW_EAP_RESPONSE)) {
217 hdr->data[0] = (reply->type.type & 0xFF);
220 * Here since we cannot know the typedata format and length
222 * Type_data is expected to be wired by each EAP-Type
224 * Zero length/No typedata is supported as long as
227 if (reply->type.data && reply->type.length > 0) {
228 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
229 free(reply->type.data);
230 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
238 * compose EAP reply packet in EAP-Message attr of RADIUS. If
239 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
241 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
243 uint16_t eap_len, len;
246 eap_packet_t *eap_packet;
250 if (eap_wireformat(reply) == EAP_INVALID) {
251 return RLM_MODULE_INVALID;
253 eap_packet = (eap_packet_t *)reply->packet;
255 memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
256 len = eap_len = ntohs(eap_len);
257 ptr = (unsigned char *)eap_packet;
259 pairdelete(&(packet->vps), PW_EAP_MESSAGE);
271 * create a value pair & append it to the packet list
272 * This memory gets freed up when packet is freed up
274 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
275 memcpy(eap_msg->vp_strvalue, ptr, len);
276 eap_msg->length = len;
277 pairadd(&(packet->vps), eap_msg);
283 * EAP-Message is always associated with
284 * Message-Authenticator but not vice-versa.
286 * Don't add a Message-Authenticator if it's already
289 vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR);
291 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
292 memset(vp->vp_strvalue, 0, AUTH_VECTOR_LEN);
293 vp->length = AUTH_VECTOR_LEN;
294 pairadd(&(packet->vps), vp);
297 /* Set request reply code, but only if it's not already set. */
298 rcode = RLM_MODULE_OK;
299 if (!packet->code) switch(reply->code) {
300 case PW_EAP_RESPONSE:
302 packet->code = PW_AUTHENTICATION_ACK;
303 rcode = RLM_MODULE_HANDLED;
306 packet->code = PW_AUTHENTICATION_REJECT;
307 rcode = RLM_MODULE_REJECT;
310 packet->code = PW_ACCESS_CHALLENGE;
311 rcode = RLM_MODULE_HANDLED;
314 /* Should never enter here */
315 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
316 packet->code = PW_AUTHENTICATION_REJECT;
324 * given a radius request with some attributes in the EAP range, build
325 * them all into a single EAP-Message body.
327 * Note that this function will build multiple EAP-Message bodies
328 * if there are multiple eligible EAP-types. This is incorrect, as the
329 * recipient will in fact concatenate them.
331 * XXX - we could break the loop once we process one type. Maybe this
332 * just deserves an assert?
335 void map_eap_types(RADIUS_PACKET *req)
337 VALUE_PAIR *vp, *vpnext;
342 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
344 id = ((int)getpid() & 0xff);
349 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
351 eapcode = PW_EAP_REQUEST;
353 eapcode = vp->vp_integer;
357 for(vp = req->vps; vp != NULL; vp = vpnext) {
358 /* save it in case it changes! */
361 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
362 vp->attribute < ATTRIBUTE_EAP_BASE+256) {
371 eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
374 case PW_EAP_IDENTITY:
375 case PW_EAP_NOTIFICATION:
386 * no known special handling, it is just encoded as an
387 * EAP-message with the given type.
390 /* nuke any existing EAP-Messages */
391 pairdelete(&req->vps, PW_EAP_MESSAGE);
393 memset(&ep, 0, sizeof(ep));
396 ep.type.type = eap_type;
397 ep.type.length = vp->length;
398 ep.type.data = malloc(vp->length);
399 memcpy(ep.type.data,vp->vp_octets, vp->length);
400 eap_basic_compose(req, &ep);
405 * Handles multiple EAP-Message attrs
406 * ie concatenates all to get the complete EAP packet.
408 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
409 * refer fragmentation in rfc2869.
411 eap_packet_t *eap_attribute(VALUE_PAIR *vps)
413 VALUE_PAIR *first, *vp;
414 eap_packet_t *eap_packet;
420 * Get only EAP-Message attribute list
422 first = pairfind(vps, PW_EAP_MESSAGE);
424 radlog(L_ERR, "rlm_eap: EAP-Message not found");
429 * Sanity check the length before doing anything.
431 if (first->length < 4) {
432 radlog(L_ERR, "rlm_eap: EAP packet is too short.");
437 * Get the Actual length from the EAP packet
438 * First EAP-Message contains the EAP packet header
440 memcpy(&len, first->vp_strvalue + 2, sizeof(len));
444 * Take out even more weird things.
447 radlog(L_ERR, "rlm_eap: EAP packet has invalid length.");
452 * Sanity check the length, BEFORE malloc'ing memory.
455 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
456 total_len += vp->length;
458 if (total_len > len) {
459 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
465 * If the length is SMALLER, die, too.
467 if (total_len < len) {
468 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
473 * Now that we know the lengths are OK, allocate memory.
475 eap_packet = (eap_packet_t *) malloc(len);
476 if (eap_packet == NULL) {
477 radlog(L_ERR, "rlm_eap: out of memory");
482 * Copy the data from EAP-Message's over to out EAP packet.
484 ptr = (unsigned char *)eap_packet;
486 /* RADIUS ensures order of attrs, so just concatenate all */
487 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
488 memcpy(ptr, vp->vp_strvalue, vp->length);
496 * given a radius request with an EAP-Message body, decode it specific
499 void unmap_eap_types(RADIUS_PACKET *rep)
506 /* find eap message */
507 e = eap_attribute(rep->vps);
510 if(e == NULL) return;
512 /* create EAP-ID and EAP-CODE attributes to start */
513 eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
514 eap1->vp_integer = e->id;
515 pairadd(&(rep->vps), eap1);
517 eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
518 eap1->vp_integer = e->code;
519 pairadd(&(rep->vps), eap1);
530 case PW_EAP_RESPONSE:
531 /* there is a type field, which we use to create
534 /* the length was decode already into the attribute
535 * length, and was checked already. Network byte
536 * order, just pull it out using math.
538 len = e->length[0]*256 + e->length[1];
540 /* verify the length is big enough to hold type */
548 type += ATTRIBUTE_EAP_BASE;
551 if(len > MAX_STRING_LEN) {
552 len = MAX_STRING_LEN;
555 eap1 = paircreate(type, PW_TYPE_OCTETS);
556 memcpy(eap1->vp_strvalue, &e->data[1], len);
558 pairadd(&(rep->vps), eap1);