pull fixes from branch_1_1
[freeradius.git] / src / modules / rlm_eap / libeap / eapcommon.c
1 /*
2  * eapcommon.c    rfc2284 & rfc2869 implementation
3  *
4  * code common to clients and to servers.
5  *
6  * Version:     $Id$
7  *
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.
12  *
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.
17  *
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
21  *
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>
26  */
27 /*
28  *  EAP PACKET FORMAT
29  *  --- ------ ------
30  *  0                   1                   2                   3
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  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35  * |    Data ...
36  * +-+-+-+-+
37  *
38  *
39  * EAP Request and Response Packet Format
40  * --- ------- --- -------- ------ ------
41  *  0                   1                   2                   3
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  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
48  *
49  *
50  * EAP Success and Failure Packet Format
51  * --- ------- --- ------- ------ ------
52  *  0                   1                   2                   3
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  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57  *
58  */
59
60 #include <freeradius-devel/ident.h>
61 RCSID("$Id$")
62
63 #include <freeradius-devel/autoconf.h>
64 #include <freeradius-devel/missing.h>
65 #include <freeradius-devel/libradius.h>
66 #include "eap_types.h"
67
68 static const char *eap_types[] = {
69   "",
70   "identity",
71   "notification",
72   "nak",                        /* NAK */
73   "md5",
74   "otp",
75   "gtc",
76   "7",
77   "8",
78   "9",
79   "10",
80   "11",
81   "12",
82   "tls",                        /* 13 */
83   "14",
84   "15",
85   "16",
86   "leap",                       /* 17 */
87   "sim",                        /* 18 GSM-SIM authentication */
88   "19",
89   "20",
90   "ttls",                       /* 21 */
91   "22",
92   "23",
93   "24",
94   "peap",                       /* 25 */
95   "mschapv2",                   /* 26 */
96   "27",
97   "28",
98   "cisco_mschapv2"              /* 29 */
99 };
100 #define MAX_EAP_TYPE_NAME 29
101
102 /*
103  *      Return an EAP-Type for a particular name.
104  */
105 int eaptype_name2type(const char *name)
106 {
107         int i;
108
109         for (i = 0; i <= PW_EAP_MAX_TYPES; i++) {
110                 if (strcmp(name, eap_types[i]) == 0) {
111                         return i;
112                 }
113         }
114
115         return -1;
116 }
117
118 /*
119  *      Returns a text string containing the name of the EAP type.
120  */
121 const char *eaptype_type2name(unsigned int type, char *buffer, size_t buflen)
122 {
123         DICT_VALUE      *dval;
124
125         if (type > MAX_EAP_TYPE_NAME) {
126                 /*
127                  *      Prefer the dictionary name over a number,
128                  *      if it exists.
129                  */
130                 dval = dict_valbyattr(PW_EAP_TYPE, type);
131                 if (dval) {
132                         snprintf(buffer, buflen, "%s", dval->name);
133                 }
134
135                 snprintf(buffer, buflen, "%d", type);
136                 return buffer;
137         } else if ((*eap_types[type] >= '0') && (*eap_types[type] <= '9')) {
138                 /*
139                  *      Prefer the dictionary name, if it exists.
140                  */
141                 dval = dict_valbyattr(PW_EAP_TYPE, type);
142                 if (dval) {
143                         snprintf(buffer, buflen, "%s", dval->name);
144                         return buffer;
145                 } /* else it wasn't in the dictionary */
146         } /* else the name in the array was non-numeric */
147
148         /*
149          *      Return the name, whatever it is.
150          */
151         return eap_types[type];
152 }
153
154 /*
155  *      EAP packet format to be sent over the wire
156  *
157  *      i.e. code+id+length+data where data = null/type+typedata
158  *      based on code.
159  *
160  * INPUT to function is reply->code
161  *                      reply->id
162  *                      reply->type   - setup with data
163  *
164  * OUTPUT reply->packet is setup with wire format, and will
165  *                      be malloc()'ed to the right size.
166  *
167  */
168 static int eap_wireformat(EAP_PACKET *reply)
169 {
170
171         eap_packet_t    *hdr;
172         uint16_t total_length = 0;
173
174         if (reply == NULL) return EAP_INVALID;
175
176         /*
177          * if reply->packet is set, then the wire format
178          * has already been calculated, just succeed!
179          */
180         if(reply->packet != NULL)
181         {
182                 return EAP_VALID;
183         }
184
185         total_length = EAP_HEADER_LEN;
186         if (reply->code < 3) {
187                 total_length += 1/*EAPtype*/;
188                 if (reply->type.data && reply->type.length > 0) {
189                         total_length += reply->type.length;
190                 }
191         }
192
193         reply->packet = (unsigned char *)malloc(total_length);
194         hdr = (eap_packet_t *)reply->packet;
195         if (!hdr) {
196                 radlog(L_ERR, "rlm_eap: out of memory");
197                 return EAP_INVALID;
198         }
199
200         hdr->code = (reply->code & 0xFF);
201         hdr->id = (reply->id & 0xFF);
202         total_length = htons(total_length);
203         memcpy(hdr->length, &total_length, sizeof(uint16_t));
204
205         /*
206          *      Request and Response packets are special.
207          */
208         if ((reply->code == PW_EAP_REQUEST) ||
209             (reply->code == PW_EAP_RESPONSE)) {
210                 hdr->data[0] = (reply->type.type & 0xFF);
211
212                 /*
213                  * Here since we cannot know the typedata format and length
214                  *
215                  * Type_data is expected to be wired by each EAP-Type
216                  *
217                  * Zero length/No typedata is supported as long as
218                  * type is defined
219                  */
220                 if (reply->type.data && reply->type.length > 0) {
221                         memcpy(&hdr->data[1], reply->type.data, reply->type.length);
222                         free(reply->type.data);
223                         reply->type.data = reply->packet + EAP_HEADER_LEN + 1/*EAPtype*/;
224                 }
225         }
226
227         return EAP_VALID;
228 }
229
230 /*
231  *      compose EAP reply packet in EAP-Message attr of RADIUS.  If
232  *      EAP exceeds 253, frame it in multiple EAP-Message attrs.
233  */
234 int eap_basic_compose(RADIUS_PACKET *packet, EAP_PACKET *reply)
235 {
236         uint16_t eap_len, len;
237         VALUE_PAIR *eap_msg;
238         VALUE_PAIR *vp;
239         eap_packet_t *eap_packet;
240         unsigned char   *ptr;
241         int rcode;
242
243         if (eap_wireformat(reply) == EAP_INVALID) {
244                 return RLM_MODULE_INVALID;
245         }
246         eap_packet = (eap_packet_t *)reply->packet;
247
248         memcpy(&eap_len, &(eap_packet->length), sizeof(uint16_t));
249         len = eap_len = ntohs(eap_len);
250         ptr = (unsigned char *)eap_packet;
251
252         pairdelete(&(packet->vps), PW_EAP_MESSAGE);
253
254         do {
255                 if (eap_len > 253) {
256                         len = 253;
257                         eap_len -= 253;
258                 } else {
259                         len = eap_len;
260                         eap_len = 0;
261                 }
262
263                 /*
264                  * create a value pair & append it to the packet list
265                  * This memory gets freed up when packet is freed up
266                  */
267                 eap_msg = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
268                 memcpy(eap_msg->vp_strvalue, ptr, len);
269                 eap_msg->length = len;
270                 pairadd(&(packet->vps), eap_msg);
271                 ptr += len;
272                 eap_msg = NULL;
273         } while (eap_len);
274
275         /*
276          *      EAP-Message is always associated with
277          *      Message-Authenticator but not vice-versa.
278          *
279          *      Don't add a Message-Authenticator if it's already
280          *      there.
281          */
282         vp = pairfind(packet->vps, PW_MESSAGE_AUTHENTICATOR);
283         if (!vp) {
284                 vp = paircreate(PW_MESSAGE_AUTHENTICATOR, PW_TYPE_OCTETS);
285                 memset(vp->vp_strvalue, 0, AUTH_VECTOR_LEN);
286                 vp->length = AUTH_VECTOR_LEN;
287                 pairadd(&(packet->vps), vp);
288         }
289
290         /* Set request reply code, but only if it's not already set. */
291         rcode = RLM_MODULE_OK;
292         if (!packet->code) switch(reply->code) {
293         case PW_EAP_RESPONSE:
294         case PW_EAP_SUCCESS:
295                 packet->code = PW_AUTHENTICATION_ACK;
296                 rcode = RLM_MODULE_HANDLED;
297                 break;
298         case PW_EAP_FAILURE:
299                 packet->code = PW_AUTHENTICATION_REJECT;
300                 rcode = RLM_MODULE_REJECT;
301                 break;
302         case PW_EAP_REQUEST:
303                 packet->code = PW_ACCESS_CHALLENGE;
304                 rcode = RLM_MODULE_HANDLED;
305                 break;
306         default:
307                 /* Should never enter here */
308                 radlog(L_ERR, "rlm_eap: reply code %d is unknown, Rejecting the request.", reply->code);
309                 packet->code = PW_AUTHENTICATION_REJECT;
310                 break;
311         }
312
313         return rcode;
314 }
315
316 /*
317  * given a radius request with some attributes in the EAP range, build
318  * them all into a single EAP-Message body.
319  *
320  * Note that this function will build multiple EAP-Message bodies
321  * if there are multiple eligible EAP-types. This is incorrect, as the
322  * recipient will in fact concatenate them.
323  *
324  * XXX - we could break the loop once we process one type. Maybe this
325  *       just deserves an assert?
326  *
327  */
328 void map_eap_types(RADIUS_PACKET *req)
329 {
330         VALUE_PAIR *vp, *vpnext;
331         int id, eapcode;
332         EAP_PACKET ep;
333         int eap_type;
334
335         vp = pairfind(req->vps, ATTRIBUTE_EAP_ID);
336         if(vp == NULL) {
337                 id = ((int)getpid() & 0xff);
338         } else {
339                 id = vp->lvalue;
340         }
341
342         vp = pairfind(req->vps, ATTRIBUTE_EAP_CODE);
343         if(vp == NULL) {
344                 eapcode = PW_EAP_REQUEST;
345         } else {
346                 eapcode = vp->lvalue;
347         }
348
349
350         for(vp = req->vps; vp != NULL; vp = vpnext) {
351                 /* save it in case it changes! */
352                 vpnext = vp->next;
353
354                 if(vp->attribute >= ATTRIBUTE_EAP_BASE &&
355                    vp->attribute < ATTRIBUTE_EAP_BASE+256) {
356                         break;
357                 }
358         }
359
360         if(vp == NULL) {
361                 return;
362         }
363
364         eap_type = vp->attribute - ATTRIBUTE_EAP_BASE;
365
366         switch(eap_type) {
367         case PW_EAP_IDENTITY:
368         case PW_EAP_NOTIFICATION:
369         case PW_EAP_NAK:
370         case PW_EAP_MD5:
371         case PW_EAP_OTP:
372         case PW_EAP_GTC:
373         case PW_EAP_TLS:
374         case PW_EAP_LEAP:
375         case PW_EAP_TTLS:
376         case PW_EAP_PEAP:
377         default:
378                 /*
379                  * no known special handling, it is just encoded as an
380                  * EAP-message with the given type.
381                  */
382
383                 /* nuke any existing EAP-Messages */
384                 pairdelete(&req->vps, PW_EAP_MESSAGE);
385
386                 memset(&ep, 0, sizeof(ep));
387                 ep.code = eapcode;
388                 ep.id   = id;
389                 ep.type.type = eap_type;
390                 ep.type.length = vp->length;
391                 ep.type.data = vp->vp_octets;
392                 eap_basic_compose(req, &ep);
393         }
394 }
395
396 /*
397  * Handles multiple EAP-Message attrs
398  * ie concatenates all to get the complete EAP packet.
399  *
400  * NOTE: Sometimes Framed-MTU might contain the length of EAP-Message,
401  *      refer fragmentation in rfc2869.
402  */
403 eap_packet_t *eap_attribute(VALUE_PAIR *vps)
404 {
405         VALUE_PAIR *first, *vp;
406         eap_packet_t *eap_packet;
407         unsigned char *ptr;
408         uint16_t len;
409         int total_len;
410
411         /*
412          *      Get only EAP-Message attribute list
413          */
414         first = pairfind(vps, PW_EAP_MESSAGE);
415         if (first == NULL) {
416                 radlog(L_ERR, "rlm_eap: EAP-Message not found");
417                 return NULL;
418         }
419
420         /*
421          *      Sanity check the length before doing anything.
422          */
423         if (first->length < 4) {
424                 radlog(L_ERR, "rlm_eap: EAP packet is too short.");
425                 return NULL;
426         }
427
428         /*
429          *      Get the Actual length from the EAP packet
430          *      First EAP-Message contains the EAP packet header
431          */
432         memcpy(&len, first->vp_strvalue + 2, sizeof(len));
433         len = ntohs(len);
434
435         /*
436          *      Take out even more weird things.
437          */
438         if (len < 4) {
439                 radlog(L_ERR, "rlm_eap: EAP packet has invalid length.");
440                 return NULL;
441         }
442
443         /*
444          *      Sanity check the length, BEFORE malloc'ing memory.
445          */
446         total_len = 0;
447         for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
448                 total_len += vp->length;
449
450                 if (total_len > len) {
451                         radlog(L_ERR, "rlm_eap: Malformed EAP packet.  Length in packet header does not match actual length");
452                         return NULL;
453                 }
454         }
455
456         /*
457          *      If the length is SMALLER, die, too.
458          */
459         if (total_len < len) {
460                 radlog(L_ERR, "rlm_eap: Malformed EAP packet.  Length in packet header does not match actual length");
461                 return NULL;
462         }
463
464         /*
465          *      Now that we know the lengths are OK, allocate memory.
466          */
467         eap_packet = (eap_packet_t *) malloc(len);
468         if (eap_packet == NULL) {
469                 radlog(L_ERR, "rlm_eap: out of memory");
470                 return NULL;
471         }
472
473         /*
474          *      Copy the data from EAP-Message's over to out EAP packet.
475          */
476         ptr = (unsigned char *)eap_packet;
477
478         /* RADIUS ensures order of attrs, so just concatenate all */
479         for (vp = first; vp; vp = pairfind(vp->next, PW_EAP_MESSAGE)) {
480                 memcpy(ptr, vp->vp_strvalue, vp->length);
481                 ptr += vp->length;
482         }
483
484         return eap_packet;
485 }
486
487 /*
488  * given a radius request with an EAP-Message body, decode it specific
489  * attributes.
490  */
491 void unmap_eap_types(RADIUS_PACKET *rep)
492 {
493         VALUE_PAIR *eap1;
494         eap_packet_t *e;
495         int len;
496         int type;
497
498         /* find eap message */
499         e = eap_attribute(rep->vps);
500
501         /* nothing to do! */
502         if(e == NULL) return;
503
504         /* create EAP-ID and EAP-CODE attributes to start */
505         eap1 = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
506         eap1->lvalue = e->id;
507         pairadd(&(rep->vps), eap1);
508
509         eap1 = paircreate(ATTRIBUTE_EAP_CODE, PW_TYPE_INTEGER);
510         eap1->lvalue = e->code;
511         pairadd(&(rep->vps), eap1);
512
513         switch(e->code)
514         {
515         default:
516         case PW_EAP_SUCCESS:
517         case PW_EAP_FAILURE:
518                 /* no data */
519                 break;
520
521         case PW_EAP_REQUEST:
522         case PW_EAP_RESPONSE:
523                 /* there is a type field, which we use to create
524                  * a new attribute */
525
526                 /* the length was decode already into the attribute
527                  * length, and was checked already. Network byte
528                  * order, just pull it out using math.
529                  */
530                 len = e->length[0]*256 + e->length[1];
531
532                 /* verify the length is big enough to hold type */
533                 if(len < 5)
534                 {
535                         return;
536                 }
537
538                 type = e->data[0];
539
540                 type += ATTRIBUTE_EAP_BASE;
541                 len -= 5;
542
543                 if(len > MAX_STRING_LEN) {
544                         len = MAX_STRING_LEN;
545                 }
546
547                 eap1 = paircreate(type, PW_TYPE_OCTETS);
548                 memcpy(eap1->vp_strvalue, &e->data[1], len);
549                 eap1->length = len;
550                 pairadd(&(rep->vps), eap1);
551                 break;
552         }
553
554         return;
555 }
556