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/libradius.h>
64 #include "eap_types.h"
66 static const char *eap_types[] = {
85 "sim", /* 18 GSM-SIM authentication */
96 "cisco_mschapv2", /* 29 */
120 }; /* MUST have PW_EAP_MAX_TYPES */
123 * Return an EAP-Type for a particular name.
125 int eaptype_name2type(const char *name)
129 for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
130 if (strcmp(name, eap_types[i]) == 0) {
139 * Returns a text string containing the name of the EAP type.
141 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
145 if (type > PW_EAP_MAX_TYPES) {
147 * Prefer the dictionary name over a number,
150 dval = dict_valbyattr(PW_EAP_TYPE, 0, type);
152 snprintf(buffer, buflen, "%s", dval->name);
155 snprintf(buffer, buflen, "%d", type);
157 } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
159 * Prefer the dictionary name, if it exists.
161 dval = dict_valbyattr(PW_EAP_TYPE, 0, type);
163 snprintf(buffer, buflen, "%s", dval->name);
165 } /* else it wasn't in the dictionary */
166 } /* else the name in the array was non-numeric */
169 * Return the name, whatever it is.
171 return eap_types[type];
175 * EAP packet format to be sent over the wire
177 * i.e. code+id+length+data where data = null/type+typedata
180 * INPUT to function is reply->code
182 * reply->type - setup with data
184 * OUTPUT reply->packet is setup with wire format, and will
185 * be malloc()'ed to the right size.
188 int eap_wireformat(EAP_PACKET *reply)
191 uint16_t total_length = 0;
193 if (reply == NULL) return EAP_INVALID;
196 * If reply->packet is set, then the wire format
197 * has already been calculated, just succeed.
199 if(reply->packet != NULL) return EAP_VALID;
201 total_length = EAP_HEADER_LEN;
202 if (reply->code < 3) {
203 total_length += 1/*EAPtype*/;
204 if (reply->type.data && reply->type.length > 0) {
205 total_length += reply->type.length;
209 reply->packet = (unsigned char *)malloc(total_length);
210 hdr = (eap_packet_t *)reply->packet;
212 radlog(L_ERR, "rlm_eap: out of memory");
216 hdr->code = (reply->code & 0xFF);
217 hdr->id = (reply->id & 0xFF);
218 total_length = htons(total_length);
219 memcpy(hdr->length, &total_length, sizeof(total_length));
222 * Request and Response packets are special.
224 if ((reply->code == PW_EAP_REQUEST) ||
225 (reply->code == PW_EAP_RESPONSE)) {
226 hdr->data[0] = (reply->type.type & 0xFF);
229 * Here since we cannot know the typedata format and length
231 * Type_data is expected to be wired by each EAP-Type
233 * Zero length/No typedata is supported as long as
236 if (reply->type.data && reply->type.length > 0) {
237 memcpy(&hdr->data[1], reply->type.data, reply->type.length);
238 free(reply->type.data);
239 reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
248 * compose EAP reply packet in EAP-Message attr of RADIUS. If
249 * EAP exceeds 253, frame it in multiple EAP-Message attrs.
251 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
254 eap_packet_t *eap_packet;
257 if (eap_wireformat(reply) == EAP_INVALID) {
258 return RLM_MODULE_INVALID;
260 eap_packet = (eap_packet_t *)reply->packet;
262 pairdelete(&(packet->vps), PW_EAP_MESSAGE, 0, TAG_ANY);
264 vp = eap_packet2vp(eap_packet);
265 if (!vp) return RLM_MODULE_INVALID;
266 pairadd(&(packet->vps), vp);
269 * EAP-Message is always associated with
270 * Message-Authenticator but not vice-versa.
272 * Don't add a Message-Authenticator if it's already
275 vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
277 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, 0);
278 memset(vp->vp_strvalue, 0, AUTH_VECTOR_LEN);
279 vp->length = AUTH_VECTOR_LEN;
280 pairadd(&(packet->vps), vp);
283 /* Set request reply code, but only if it's not already set. */
284 rcode = RLM_MODULE_OK;
285 if (!packet->code) switch(reply->code) {
286 case PW_EAP_RESPONSE:
288 packet->code = PW_AUTHENTICATION_ACK;
289 rcode = RLM_MODULE_HANDLED;
292 packet->code = PW_AUTHENTICATION_REJECT;
293 rcode = RLM_MODULE_REJECT;
296 packet->code = PW_ACCESS_CHALLENGE;
297 rcode = RLM_MODULE_HANDLED;
300 /* Should never enter here */
301 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
302 packet->code = PW_AUTHENTICATION_REJECT;
310 VALUE_PAIR *eap_packet2vp(const eap_packet_t *packet)
314 VALUE_PAIR *head = NULL;
315 VALUE_PAIR **tail = &head;
318 total = packet->length[0] * 256 + packet->length[1];
320 ptr = (const uint8_t *) packet;
324 if (size > 253) size = 253;
326 vp = paircreate(PW_EAP_MESSAGE, 0);
331 memcpy(vp->vp_octets, ptr, size);
346 * Handles multiple EAP-Message attrs
347 * ie concatenates all to get the complete EAP packet.
349 * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
350 * refer fragmentation in rfc2869.
352 eap_packet_t *eap_vp2packet(VALUE_PAIR *vps)
354 VALUE_PAIR *first, *vp;
355 eap_packet_t *eap_packet;
361 * Get only EAP-Message attribute list
363 first = pairfind(vps, PW_EAP_MESSAGE, 0, TAG_ANY);
365 DEBUG("rlm_eap: EAP-Message not found");
370 * Sanity check the length before doing anything.
372 if (first->length < 4) {
373 DEBUG("rlm_eap: EAP packet is too short.");
378 * Get the Actual length from the EAP packet
379 * First EAP-Message contains the EAP packet header
381 memcpy(&len, first->vp_strvalue + 2, sizeof(len));
385 * Take out even more weird things.
388 DEBUG("rlm_eap: EAP packet has invalid length.");
393 * Sanity check the length, BEFORE malloc'ing memory.
396 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE, 0, TAG_ANY)) {
397 total_len += vp->length;
399 if (total_len > len) {
400 DEBUG("rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
406 * If the length is SMALLER, die, too.
408 if (total_len < len) {
409 DEBUG("rlm_eap: Malformed EAP packet. Length in packet header does not match actual length");
414 * Now that we know the lengths are OK, allocate memory.
416 eap_packet = (eap_packet_t *) malloc(len);
417 if (eap_packet == NULL) {
418 radlog(L_ERR, "rlm_eap: out of memory");
423 * Copy the data from EAP-Message's over to our EAP packet.
425 ptr = (unsigned char *)eap_packet;
427 /* RADIUS ensures order of attrs, so just concatenate all */
428 for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE, 0, TAG_ANY)) {
429 memcpy(ptr, vp->vp_strvalue, vp->length);
436 VALUE_PAIR *eap_chbind_packet2vp(const eap_chbind_packet_t *packet, size_t len)
440 VALUE_PAIR *head = NULL;
441 VALUE_PAIR **tail = &head;
444 ptr = (const uint8_t *) packet;
448 if (size > 247) size = 247;
450 vp = paircreate(PW_UKERNA_CHBIND, VENDORPEC_UKERNA,
456 memcpy(vp->vp_octets, ptr, size);
471 * Handles multiple EAP-channel-binding Message attrs
472 * ie concatenates all to get the complete EAP-channel-binding packet.
474 size_t eap_chbind_vp2packet(VALUE_PAIR *vps, eap_chbind_packet_t **result)
476 VALUE_PAIR *first, *vp;
477 eap_chbind_packet_t *eap_chbind_packet;
481 first = pairfind(vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA);
484 * Compute total length
488 vp = pairfind(vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA)) {
493 * Now that we know the length, allocate memory.
495 eap_chbind_packet = (eap_chbind_packet_t *) malloc(len);
496 if (eap_chbind_packet == NULL) {
497 radlog(L_ERR, "rlm_eap: out of memory");
502 * Copy the data from EAP-Message's over to our EAP packet.
504 ptr = (unsigned char *)eap_chbind_packet;
506 /* RADIUS ensures order of attrs, so just concatenate all */
508 vp = pairfind(vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA)) {
509 memcpy(ptr, vp->vp_octets, vp->length);
513 *result = eap_chbind_packet;