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 <freeradius-devel/autoconf.h>
61 #include <freeradius-devel/missing.h>
62 #include <freeradius-devel/libradius.h>
63 #include "eap_types.h"
65 static const char rcsid[] = "$Id$";
67 static const char *eap_types[] = {
86 "sim", /* 18 GSM-SIM authentication */
97 "cisco_mschapv2" /* 29 */
99 #define MAX_EAP_TYPE_NAME 29
102 * Return an EAP-Type for a particular name.
104 int eaptype_name2type(const char *name)
108 for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
109 if (strcmp(name, eap_types[i]) == 0) {
118 * Returns a text string containing the name of the EAP type.
120 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
124 if (type > MAX_EAP_TYPE_NAME) {
126 * Prefer the dictionary name over a number,
129 dval = dict_valbyattr(PW_EAP_TYPE, type);
131 snprintf(buffer, buflen, "%s", dval->name);
134 snprintf(buffer, buflen, "%d", type);
136 } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
138 * Prefer the dictionary name, if it exists.
140 dval = dict_valbyattr(PW_EAP_TYPE, type);
142 snprintf(buffer, buflen, "%s", dval->name);
144 } /* else it wasn't in the dictionary */
145 } /* else the name in the array was non-numeric */
148 * Return the name, whatever it is.
150 return eap_types[type];
154 * EAP packet format to be sent over the wire
156 * i.e. code+id+length+data where data = null/type+typedata
159 * INPUT to function is reply->code
161 * reply->type - setup with data
163 * OUTPUT reply->packet is setup with wire format, and will
164 * be malloc()'ed to the right size.
167 static int eap_wireformat(EAP_PACKET *reply)
171 uint16_t total_length = 0;
173 if (reply == NULL) return EAP_INVALID;
176 * if reply->packet is set, then the wire format
177 * has already been calculated, just succeed!
179 if(reply->packet != NULL)
184 total_length = EAP_HEADER_LEN;
185 if (reply->code < 3) {
186 total_length += 1/*EAPtype*/;
187 if (reply->type.data && reply->type.length > 0) {
188 total_length += reply->type.length;
192 reply->packet = (unsigned char *)malloc(total_length);
193 hdr = (eap_packet_t *)reply->packet;
195 radlog(L_ERR, "rlm_eap: out of memory");
199 hdr->code = (reply->code & 0xFF);
200 hdr->id = (reply->id & 0xFF);
201 total_length = htons(total_length);
202 memcpy(hdr->length, &total_length, sizeof(uint16_t));
205 * Request and Response packets are special.
207 if ((reply->code == PW_EAP_REQUEST) ||
208 (reply->code == PW_EAP_RESPONSE)) {
209 hdr->data[0] = (reply->type.type & 0xFF);
212 * Here since we cannot know the typedata format and length
214 * Type_data is expected to be wired by each EAP-Type
216 * Zero length/No typedata is supported as long as
219 if (reply->type.data && reply->type.length > 0) {
220 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
221 free(reply->type.data);
222 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
230 * compose EAP reply packet in EAP-Message attr of RADIUS. If
231 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
233 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
235 uint16_t eap_len, len;
238 eap_packet_t *eap_packet;
242 if (eap_wireformat(reply) == EAP_INVALID) {
243 return RLM_MODULE_INVALID;
245 eap_packet = (eap_packet_t *)reply->packet;
247 memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
248 len = eap_len = ntohs(eap_len);
249 ptr = (unsigned char *)eap_packet;
251 pairdelete(&(packet->vps), PW_EAP_MESSAGE);
263 * create a value pair & append it to the packet list
264 * This memory gets freed up when packet is freed up
266 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
267 memcpy(eap_msg->vp_strvalue, ptr, len);
268 eap_msg->length = len;
269 pairadd(&(packet->vps), eap_msg);
275 * EAP-Message is always associated with
276 * Message-Authenticator but not vice-versa.
278 * Don't add a Message-Authenticator if it's already
281 vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR);
283 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
284 memset(vp->vp_strvalue, 0, AUTH_VECTOR_LEN);
285 vp->length = AUTH_VECTOR_LEN;
286 pairadd(&(packet->vps), vp);
289 /* Set request reply code, but only if it's not already set. */
290 rcode = RLM_MODULE_OK;
291 if (!packet->code) switch(reply->code) {
292 case PW_EAP_RESPONSE:
294 packet->code = PW_AUTHENTICATION_ACK;
295 rcode = RLM_MODULE_HANDLED;
298 packet->code = PW_AUTHENTICATION_REJECT;
299 rcode = RLM_MODULE_REJECT;
302 packet->code = PW_ACCESS_CHALLENGE;
303 rcode = RLM_MODULE_HANDLED;
306 /* Should never enter here */
307 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
308 packet->code = PW_AUTHENTICATION_REJECT;
316 * given a radius request with some attributes in the EAP range, build
317 * them all into a single EAP-Message body.
319 * Note that this function will build multiple EAP-Message bodies
320 * if there are multiple eligible EAP-types. This is incorrect, as the
321 * recipient will in fact concatenate them.
323 * XXX - we could break the loop once we process one type. Maybe this
324 * just deserves an assert?
327 void map_eap_types(RADIUS_PACKET *req)
329 VALUE_PAIR *vp, *vpnext;
334 vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
336 id = ((int)getpid() & 0xff);
341 vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
343 eapcode = PW_EAP_REQUEST;
345 eapcode = vp->lvalue;
349 for(vp = req->vps; vp != NULL; vp = vpnext) {
350 /* save it in case it changes! */
353 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
354 vp->attribute < ATTRIBUTE_EAP_BASE+256) {
363 eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
366 case PW_EAP_IDENTITY:
367 case PW_EAP_NOTIFICATION:
378 * no known special handling, it is just encoded as an
379 * EAP-message with the given type.
382 /* nuke any existing EAP-Messages */
383 pairdelete(&req->vps, PW_EAP_MESSAGE);
385 memset(&ep, 0, sizeof(ep));
388 ep.type.type = eap_type;
389 ep.type.length = vp->length;
390 ep.type.data = vp->vp_strvalue;
391 eap_basic_compose(req, &ep);
396 * Handles multiple EAP-Message attrs
397 * ie concatenates all to get the complete EAP packet.
399 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
400 * refer fragmentation in rfc2869.
402 eap_packet_t *eap_attribute(VALUE_PAIR *vps)
404 VALUE_PAIR *first, *vp;
405 eap_packet_t *eap_packet;
411 * Get only EAP-Message attribute list
413 first = pairfind(vps, PW_EAP_MESSAGE);
415 radlog(L_ERR, "rlm_eap: EAP-Message not found");
420 * Sanity check the length before doing anything.
422 if (first->length < 4) {
423 radlog(L_ERR, "rlm_eap: EAP packet is too short.");
428 * Get the Actual length from the EAP packet
429 * First EAP-Message contains the EAP packet header
431 memcpy(&len, first->vp_strvalue + 2, sizeof(len));
435 * Take out even more weird things.
438 radlog(L_ERR, "rlm_eap: EAP packet has invalid length.");
443 * Sanity check the length, BEFORE malloc'ing memory.
446 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
447 total_len += vp->length;
449 if (total_len > len) {
450 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
456 * If the length is SMALLER, die, too.
458 if (total_len < len) {
459 radlog(L_ERR, "rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
464 * Now that we know the lengths are OK, allocate memory.
466 eap_packet = (eap_packet_t *) malloc(len);
467 if (eap_packet == NULL) {
468 radlog(L_ERR, "rlm_eap: out of memory");
473 * Copy the data from EAP-Message's over to out EAP packet.
475 ptr = (unsigned char *)eap_packet;
477 /* RADIUS ensures order of attrs, so just concatenate all */
478 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
479 memcpy(ptr, vp->vp_strvalue, vp->length);
487 * given a radius request with an EAP-Message body, decode it specific
490 void unmap_eap_types(RADIUS_PACKET *rep)
497 /* find eap message */
498 e = eap_attribute(rep->vps);
501 if(e == NULL) return;
503 /* create EAP-ID and EAP-CODE attributes to start */
504 eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
505 eap1->lvalue = e->id;
506 pairadd(&(rep->vps), eap1);
508 eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
509 eap1->lvalue = e->code;
510 pairadd(&(rep->vps), eap1);
521 case PW_EAP_RESPONSE:
522 /* there is a type field, which we use to create
525 /* the length was decode already into the attribute
526 * length, and was checked already. Network byte
527 * order, just pull it out using math.
529 len = e->length[0]*256 + e->length[1];
531 /* verify the length is big enough to hold type */
539 type += ATTRIBUTE_EAP_BASE;
542 if(len > MAX_STRING_LEN) {
543 len = MAX_STRING_LEN;
546 eap1 = paircreate(type, PW_TYPE_OCTETS);
547 memcpy(eap1->vp_strvalue, &e->data[1], len);
549 pairadd(&(rep->vps), eap1);