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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Copyright 2000-2003 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 "libradius.h"
61 #include "eap_types.h"
63 static const char rcsid[] = "$Id$";
65 static const char *eap_types[] = {
84 "sim", /* 18 GSM-SIM authentication */
95 "cisco_mschapv2" /* 29 */
97 #define MAX_EAP_TYPE_NAME 29
100 * Return an EAP-Type for a particular name.
102 int eaptype_name2type(const char *name)
106 for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
107 if (strcmp(name, eap_types[i]) == 0) {
116 * Returns a text string containing the name of the EAP type.
118 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
122 if (type > MAX_EAP_TYPE_NAME) {
124 * Prefer the dictionary name over a number,
127 dval = dict_valbyattr(PW_EAP_TYPE, type);
129 snprintf(buffer, buflen, "%s", dval->name);
132 snprintf(buffer, buflen, "%d", type);
134 } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
136 * Prefer the dictionary name, if it exists.
138 dval = dict_valbyattr(PW_EAP_TYPE, type);
140 snprintf(buffer, buflen, "%s", dval->name);
142 } /* else it wasn't in the dictionary */
143 } /* else the name in the array was non-numeric */
146 * Return the name, whatever it is.
148 return eap_types[type];
152 * EAP packet format to be sent over the wire
154 * i.e. code+id+length+data where data = null/type+typedata
157 * INPUT to function is reply->code
159 * reply->type - setup with data
161 * OUTPUT reply->packet is setup with wire format, and will
162 * be malloc()'ed to the right size.
165 static int eap_wireformat(EAP_PACKET *reply)
169 uint16_t total_length = 0;
171 if (reply == NULL) return EAP_INVALID;
174 * if reply->packet is set, then the wire format
175 * has already been calculated, just succeed!
177 if(reply->packet != NULL)
182 total_length = EAP_HEADER_LEN;
183 if (reply->code < 3) {
184 total_length += 1/*EAPtype*/;
185 if (reply->type.data && reply->type.length > 0) {
186 total_length += reply->type.length;
190 reply->packet = (unsigned char *)malloc(total_length);
191 hdr = (eap_packet_t *)reply->packet;
193 radlog(L_ERR, "rlm_eap: out of memory");
197 hdr->code = (reply->code & 0xFF);
198 hdr->id = (reply->id & 0xFF);
199 total_length = htons(total_length);
200 memcpy(hdr->length, &total_length, sizeof(uint16_t));
203 * Request and Response packets are special.
205 if ((reply->code == PW_EAP_REQUEST) ||
206 (reply->code == PW_EAP_RESPONSE)) {
207 hdr->data[0] = (reply->type.type & 0xFF);
210 * Here since we cannot know the typedata format and length
212 * Type_data is expected to be wired by each EAP-Type
214 * Zero length/No typedata is supported as long as
217 if (reply->type.data && reply->type.length > 0) {
218 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
219 free(reply->type.data);
220 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
228 * compose EAP reply packet in EAP-Message attr of RADIUS. If
229 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
231 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
233 uint16_t eap_len, len;
236 eap_packet_t *eap_packet;
240 if (eap_wireformat(reply) == EAP_INVALID) {
241 return RLM_MODULE_INVALID;
243 eap_packet = (eap_packet_t *)reply->packet;
245 memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
246 len = eap_len = ntohs(eap_len);
247 ptr = (unsigned char *)eap_packet;
249 pairdelete(&(packet->vps), PW_EAP_MESSAGE);
261 * create a value pair & append it to the packet list
262 * This memory gets freed up when packet is freed up
264 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
265 memcpy(eap_msg->strvalue, ptr, len);
266 eap_msg->length = len;
267 pairadd(&(packet->vps), eap_msg);
273 * EAP-Message is always associated with
274 * Message-Authenticator but not vice-versa.
276 * Don't add a Message-Authenticator if it's already
279 vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR);
281 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
282 memset(vp->strvalue, 0, AUTH_VECTOR_LEN);
283 vp->length = AUTH_VECTOR_LEN;
284 pairadd(&(packet->vps), vp);
287 /* Set request reply code, but only if it's not already set. */
288 rcode = RLM_MODULE_OK;
289 if (!packet->code) switch(reply->code) {
290 case PW_EAP_RESPONSE:
292 packet->code = PW_AUTHENTICATION_ACK;
293 rcode = RLM_MODULE_HANDLED;
296 packet->code = PW_AUTHENTICATION_REJECT;
297 rcode = RLM_MODULE_REJECT;
300 packet->code = PW_ACCESS_CHALLENGE;
301 rcode = RLM_MODULE_HANDLED;
304 /* Should never enter here */
305 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
306 packet->code = PW_AUTHENTICATION_REJECT;
314 * given a radius request with some attributes in the EAP range, build
315 * them all into a single EAP-Message body.
317 * Note that this function will build multiple EAP-Message bodies
318 * if there are multiple eligible EAP-types. This is incorrect, as the
319 * recipient will in fact concatenate them.
321 * XXX - we could break the loop once we process one type. Maybe this
322 * just deserves an assert?
325 void map_eap_types(RADIUS_PACKET *req)
327 VALUE_PAIR *vp, *vpnext;
332 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
334 id = ((int)getpid() & 0xff);
339 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
341 eapcode = PW_EAP_REQUEST;
343 eapcode = vp->lvalue;
347 for(vp = req->vps; vp != NULL; vp = vpnext) {
348 /* save it in case it changes! */
351 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
352 vp->attribute < ATTRIBUTE_EAP_BASE+256) {
361 eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
364 case PW_EAP_IDENTITY:
365 case PW_EAP_NOTIFICATION:
376 * no known special handling, it is just encoded as an
377 * EAP-message with the given type.
380 /* nuke any existing EAP-Messages */
381 pairdelete(&req->vps, PW_EAP_MESSAGE);
383 memset(&ep, 0, sizeof(ep));
386 ep.type.type = eap_type;
387 ep.type.length = vp->length;
388 ep.type.data = malloc(vp->length);
389 memcpy(ep.type.data,vp->strvalue, vp->length);
390 eap_basic_compose(req, &ep);
395 * Handles multiple EAP-Message attrs
396 * ie concatenates all to get the complete EAP packet.
398 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
399 * refer fragmentation in rfc2869.
401 eap_packet_t *eap_attribute(VALUE_PAIR *vps)
403 VALUE_PAIR *first, *vp;
404 eap_packet_t *eap_packet;
410 * Get only EAP-Message attribute list
412 first = pairfind(vps, PW_EAP_MESSAGE);
414 radlog(L_ERR, "rlm_eap: EAP-Message not found");
419 * Sanity check the length before doing anything.
421 if (first->length < 4) {
422 radlog(L_ERR, "rlm_eap: EAP packet is too short.");
427 * Get the Actual length from the EAP packet
428 * First EAP-Message contains the EAP packet header
430 memcpy(&len, first->strvalue + 2, sizeof(len));
434 * Take out even more weird things.
437 radlog(L_ERR, "rlm_eap: EAP packet has invalid length.");
442 * Sanity check the length, BEFORE malloc'ing memory.
445 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
446 total_len += vp->length;
448 if (total_len > len) {
449 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
455 * If the length is SMALLER, die, too.
457 if (total_len < len) {
458 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
463 * Now that we know the lengths are OK, allocate memory.
465 eap_packet = (eap_packet_t *) malloc(len);
466 if (eap_packet == NULL) {
467 radlog(L_ERR, "rlm_eap: out of memory");
472 * Copy the data from EAP-Message's over to out EAP packet.
474 ptr = (unsigned char *)eap_packet;
476 /* RADIUS ensures order of attrs, so just concatenate all */
477 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
478 memcpy(ptr, vp->strvalue, vp->length);
486 * given a radius request with an EAP-Message body, decode it specific
489 void unmap_eap_types(RADIUS_PACKET *rep)
496 /* find eap message */
497 e = eap_attribute(rep->vps);
500 if(e == NULL) return;
502 /* create EAP-ID and EAP-CODE attributes to start */
503 eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
504 eap1->lvalue = e->id;
505 pairadd(&(rep->vps), eap1);
507 eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
508 eap1->lvalue = e->code;
509 pairadd(&(rep->vps), eap1);
520 case PW_EAP_RESPONSE:
521 /* there is a type field, which we use to create
524 /* the length was decode already into the attribute
525 * length, and was checked already. Network byte
526 * order, just pull it out using math.
528 len = e->length[0]*256 + e->length[1];
530 /* verify the length is big enough to hold type */
538 type += ATTRIBUTE_EAP_BASE;
541 if(len > MAX_STRING_LEN) {
542 len = MAX_STRING_LEN;
545 eap1 = paircreate(type, PW_TYPE_OCTETS);
546 memcpy(eap1->strvalue, &e->data[1], len);
548 pairadd(&(rep->vps), eap1);