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 */
122 }; /* MUST have PW_EAP_MAX_TYPES */
125 * Return an EAP-Type for a particular name.
127 int eaptype_name2type(const char *name)
131 for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
132 if (strcmp(name, eap_types[i]) == 0) {
141 * Returns a text string containing the name of the EAP type.
143 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
147 if (type > PW_EAP_MAX_TYPES) {
149 * Prefer the dictionary name over a number,
152 dval = dict_valbyattr(PW_EAP_TYPE, 0, type);
154 snprintf(buffer, buflen, "%s", dval->name);
157 snprintf(buffer, buflen, "%d", type);
159 } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
161 * Prefer the dictionary name, if it exists.
163 dval = dict_valbyattr(PW_EAP_TYPE, 0, type);
165 snprintf(buffer, buflen, "%s", dval->name);
167 } /* else it wasn't in the dictionary */
168 } /* else the name in the array was non-numeric */
171 * Return the name, whatever it is.
173 return eap_types[type];
177 * EAP packet format to be sent over the wire
179 * i.e. code+id+length+data where data = null/type+typedata
182 * INPUT to function is reply->code
184 * reply->type - setup with data
186 * OUTPUT reply->packet is setup with wire format, and will
187 * be malloc()'ed to the right size.
190 int eap_wireformat(EAP_PACKET *reply)
193 uint16_t total_length = 0;
195 if (reply == NULL) return EAP_INVALID;
198 * If reply->packet is set, then the wire format
199 * has already been calculated, just succeed.
201 if(reply->packet != NULL) return EAP_VALID;
203 total_length = EAP_HEADER_LEN;
204 if (reply->code < 3) {
205 total_length += 1/*EAPtype*/;
206 if (reply->type.data && reply->type.length > 0) {
207 total_length += reply->type.length;
211 reply->packet = (unsigned char *)malloc(total_length);
212 hdr = (eap_packet_t *)reply->packet;
214 radlog(L_ERR, "rlm_eap: out of memory");
218 hdr->code = (reply->code & 0xFF);
219 hdr->id = (reply->id & 0xFF);
220 total_length = htons(total_length);
221 memcpy(hdr->length, &total_length, sizeof(total_length));
224 * Request and Response packets are special.
226 if ((reply->code == PW_EAP_REQUEST) ||
227 (reply->code == PW_EAP_RESPONSE)) {
228 hdr->data[0] = (reply->type.type & 0xFF);
231 * Here since we cannot know the typedata format and length
233 * Type_data is expected to be wired by each EAP-Type
235 * Zero length/No typedata is supported as long as
238 if (reply->type.data && reply->type.length > 0) {
239 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
240 free(reply->type.data);
241 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
250 * compose EAP reply packet in EAP-Message attr of RADIUS. If
251 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
253 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
256 eap_packet_t *eap_packet;
259 if (eap_wireformat(reply) == EAP_INVALID) {
260 return RLM_MODULE_INVALID;
262 eap_packet = (eap_packet_t *)reply->packet;
264 pairdelete(&(packet->vps), PW_EAP_MESSAGE, 0);
266 vp = eap_packet2vp(eap_packet);
267 if (!vp) return RLM_MODULE_INVALID;
268 pairadd(&(packet->vps), vp);
271 * EAP-Message is always associated with
272 * Message-Authenticator but not vice-versa.
274 * Don't add a Message-Authenticator if it's already
277 vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR, 0);
279 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, 0, PW_TYPE_OCTETS);
280 memset(vp->vp_strvalue, 0, AUTH_VECTOR_LEN);
281 vp->length = AUTH_VECTOR_LEN;
282 pairadd(&(packet->vps), vp);
285 /* Set request reply code, but only if it's not already set. */
286 rcode = RLM_MODULE_OK;
287 if (!packet->code) switch(reply->code) {
288 case PW_EAP_RESPONSE:
290 packet->code = PW_AUTHENTICATION_ACK;
291 rcode = RLM_MODULE_HANDLED;
294 packet->code = PW_AUTHENTICATION_REJECT;
295 rcode = RLM_MODULE_REJECT;
298 packet->code = PW_ACCESS_CHALLENGE;
299 rcode = RLM_MODULE_HANDLED;
302 /* Should never enter here */
303 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
304 packet->code = PW_AUTHENTICATION_REJECT;
312 VALUE_PAIR *eap_packet2vp(const eap_packet_t *packet)
316 VALUE_PAIR *head = NULL;
317 VALUE_PAIR **tail = &head;
320 total = packet->length[0] * 256 + packet->length[1];
322 ptr = (const uint8_t *) packet;
326 if (size > 253) size = 253;
328 vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
333 memcpy(vp->vp_octets, ptr, size);
348 * Handles multiple EAP-Message attrs
349 * ie concatenates all to get the complete EAP packet.
351 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
352 * refer fragmentation in rfc2869.
354 eap_packet_t *eap_vp2packet(VALUE_PAIR *vps)
356 VALUE_PAIR *first, *vp;
357 eap_packet_t *eap_packet;
363 * Get only EAP-Message attribute list
365 first = pairfind(vps, PW_EAP_MESSAGE, 0);
367 DEBUG("rlm_eap: EAP-Message not found");
372 * Sanity check the length before doing anything.
374 if (first->length < 4) {
375 DEBUG("rlm_eap: EAP packet is too short.");
380 * Get the Actual length from the EAP packet
381 * First EAP-Message contains the EAP packet header
383 memcpy(&len, first->vp_strvalue + 2, sizeof(len));
387 * Take out even more weird things.
390 DEBUG("rlm_eap: EAP packet has invalid length.");
395 * Sanity check the length, BEFORE malloc'ing memory.
398 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE, 0)) {
399 total_len += vp->length;
401 if (total_len > len) {
402 DEBUG("rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
408 * If the length is SMALLER, die, too.
410 if (total_len < len) {
411 DEBUG("rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
416 * Now that we know the lengths are OK, allocate memory.
418 eap_packet = (eap_packet_t *) malloc(len);
419 if (eap_packet == NULL) {
420 radlog(L_ERR, "rlm_eap: out of memory");
425 * Copy the data from EAP-Message's over to our EAP packet.
427 ptr = (unsigned char *)eap_packet;
429 /* RADIUS ensures order of attrs, so just concatenate all */
430 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE, 0)) {
431 memcpy(ptr, vp->vp_strvalue, vp->length);