772055ce21400d28897f7003df035ad210971410
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_ttls / ttls.c
1 /*
2  * rlm_eap_ttls.c  contains the interfaces that are called from eap
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  *   Copyright 2003 Alan DeKok <aland@freeradius.org>
21  *   Copyright 2006 The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include "eap_ttls.h"
28
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  *   |                           AVP Code                            |
34  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35  *   |V M r r r r r r|                  AVP Length                   |
36  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37  *   |                        Vendor-ID (opt)                        |
38  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  *   |    Data ...
40  *   +-+-+-+-+-+-+-+-+
41  */
42
43 /*
44  *      Verify that the diameter packet is valid.
45  */
46 static int diameter_verify(REQUEST *request,
47                            const uint8_t *data, unsigned int data_len)
48 {
49         uint32_t attr;
50         uint32_t length;
51         unsigned int hdr_len;
52         unsigned int data_left = data_len;
53
54         while (data_left > 0) {
55                 hdr_len = 12;
56
57                 if (data_len < 12) {
58                         RDEBUG2(" Diameter attribute is too small to contain a Diameter header");
59                         return 0;
60                 }
61
62                 memcpy(&attr, data, 4);
63                 attr = htonl(attr);
64                 memcpy(&length, data + 4, 4);
65                 length = ntohl(length);
66
67                 if ((data[4] & 0x80) != 0) {
68                         if (data_len < 16) {
69                                 RDEBUG2(" Diameter attribute is too small to contain a Diameter header with Vendor-Id");
70                                 return 0;
71                         }
72
73                         hdr_len = 16;
74                 }
75
76                 length &= 0x00ffffff;
77
78                 /*
79                  *      Too short or too long is bad.
80                  */
81                 if (length <= (hdr_len - 4)) {
82                         RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr, length, hdr_len);
83                         return 0;
84                 }
85
86                 if (length > data_left) {
87                         RDEBUG2("Tunneled attribute %u is longer than room left in the packet (%u > %u).", attr, length, data_left);
88                         return 0;
89                 }
90
91                 /*
92                  *      Check for broken implementations, which don't
93                  *      pad the AVP to a 4-octet boundary.
94                  */
95                 if (data_left == length) break;
96
97                 /*
98                  *      The length does NOT include the padding, so
99                  *      we've got to account for it here by rounding up
100                  *      to the nearest 4-byte boundary.
101                  */
102                 length += 0x03;
103                 length &= ~0x03;
104
105                 /*
106                  *      If the rest of the diameter packet is larger than
107                  *      this attribute, continue.
108                  *
109                  *      Otherwise, if the attribute over-flows the end
110                  *      of the packet, die.
111                  */
112                 if (data_left < length) {
113                         RDEBUG2("ERROR! Diameter attribute overflows packet!");
114                         return 0;
115                 }
116
117                 /*
118                  *      data_left > length, continue.
119                  */
120                 data_left -= length;
121                 data += length;
122         }
123
124         /*
125          *      We got this far.  It looks OK.
126          */
127         return 1;
128 }
129
130
131 /*
132  *      Convert diameter attributes to our VALUE_PAIR's
133  */
134 static VALUE_PAIR *diameter2vp(REQUEST *request, SSL *ssl,
135                                const uint8_t *data, size_t data_len)
136 {
137         uint32_t        attr;
138         uint32_t        length;
139         size_t          offset;
140         size_t          size;
141         size_t          data_left = data_len;
142         VALUE_PAIR      *first = NULL;
143         VALUE_PAIR      **last = &first;
144         VALUE_PAIR      *vp;
145
146         while (data_left > 0) {
147                 rad_assert(data_left <= data_len);
148                 memcpy(&attr, data, sizeof(attr));
149                 data += 4;
150                 attr = ntohl(attr);
151
152                 memcpy(&length, data, sizeof(length));
153                 data += 4;
154                 length = ntohl(length);
155
156                 /*
157                  *      A "vendor" flag, with a vendor ID of zero,
158                  *      is equivalent to no vendor.  This is stupid.
159                  */
160                 offset = 8;
161                 if ((length & (1 << 31)) != 0) {
162                         uint32_t vendor;
163
164                         memcpy(&vendor, data, sizeof(vendor));
165                         vendor = ntohl(vendor);
166
167                         data += 4; /* skip the vendor field, it's zero */
168                         offset += 4; /* offset to value field */
169                         length &= 0x00ffffff;
170
171                         if (attr > 65535) goto next_attr;
172                         if (vendor > 65535) goto next_attr;
173
174                         attr |= (vendor << 16);
175                 }
176
177                 /*
178                  *      Get the length.
179                  */
180                 length &= 0x00ffffff;
181
182                 /*
183                  *      FIXME: Handle the M bit.  For now, we assume that
184                  *      some other module takes care of any attribute
185                  *      with the M bit set.
186                  */
187                 
188                 /*
189                  *      Get the size of the value portion of the
190                  *      attribute.
191                  */
192                 size = length - offset;
193
194                 /*
195                  *      Vendor attributes can be larger than 255.
196                  *      Normal attributes cannot be.
197                  */
198                 if ((attr > 255) && (VENDOR(attr) == 0)) {
199                         RDEBUG2("WARNING: Skipping Diameter attribute %u",
200                                 attr);
201                         goto next_attr;
202                 }
203
204                 if (size > 253) {
205                         RDEBUG2("WARNING: diameter2vp skipping long attribute %u, attr");
206                         goto next_attr;
207                 }
208
209                 /*
210                  *      Create it.  If this fails, it's because we're OOM.
211                  */
212                 vp = paircreate(attr, PW_TYPE_OCTETS);
213                 if (!vp) {
214                         RDEBUG2("diameter2vp: Failed creating attribute %u", attr);
215                         pairfree(&first);
216                         return NULL;
217                 }
218
219                 /*
220                  *      If it's a type from our dictionary, then
221                  *      we need to put the data in a relevant place.
222                  */
223                 switch (vp->type) {
224                 case PW_TYPE_INTEGER:
225                 case PW_TYPE_DATE:
226                         if (size != vp->length) {
227                                 /*
228                                  *      Bad format.  Create a "raw"
229                                  *      attribute.
230                                  */
231                 raw:
232                                 vp = paircreate_raw(vp->attribute,
233                                                     PW_TYPE_OCTETS, vp);
234                                 vp->length = size;
235                                 memcpy(vp->vp_octets, data, vp->length);
236                                 break;
237                         }
238                         memcpy(&vp->vp_integer, data, vp->length);
239
240                         /*
241                          *      Stored in host byte order: change it.
242                          */
243                         vp->vp_integer = ntohl(vp->vp_integer);
244                         break;
245
246                 case PW_TYPE_IPADDR:
247                         if (size != vp->length) goto raw;
248                         memcpy(&vp->vp_ipaddr, data, vp->length);
249
250                         /*
251                          *      Stored in network byte order: don't change it.
252                          */
253                         break;
254
255                 case PW_TYPE_BYTE:
256                         if (size != vp->length) goto raw;
257                         vp->vp_integer = data[0];
258                         break;
259
260                 case PW_TYPE_SHORT:
261                         if (size != vp->length) goto raw;
262                         vp->vp_integer = (data[0] * 256) + data[1];
263                         break;
264
265                 case PW_TYPE_SIGNED:
266                         if (size != vp->length) goto raw;
267                         memcpy(&vp->vp_signed, data, vp->length);
268                         vp->vp_signed = ntohl(vp->vp_signed);
269                         break;
270
271                 case PW_TYPE_IPV6ADDR:
272                         if (size != vp->length) goto raw;
273                         memcpy(&vp->vp_ipv6addr, data, vp->length);
274                         break;
275
276                 case PW_TYPE_IPV6PREFIX:
277                         if (size != vp->length) goto raw;
278                         memcpy(&vp->vp_ipv6prefix, data, vp->length);
279                         break;
280
281                         /*
282                          *      String, octet, etc.  Copy the data from the
283                          *      value field over verbatim.
284                          *
285                          *      FIXME: Ipv6 attributes ?
286                          *
287                          */
288                 case PW_TYPE_OCTETS:
289                         if (attr == PW_EAP_MESSAGE) {
290                                 const uint8_t *eap_message = data;
291
292                                 /*
293                                  *      vp exists the first time around.
294                                  */
295                                 while (1) {
296                                         vp->length = size;
297                                         if (vp->length > 253) vp->length = 253;
298                                         memcpy(vp->vp_octets, eap_message,
299                                                vp->length);
300
301                                         size -= vp->length;
302                                         eap_message += vp->length;
303
304                                         *last = vp;
305                                         last = &(vp->next);
306
307                                         if (size == 0) break;
308
309                                         vp = paircreate(attr, PW_TYPE_OCTETS);
310                                         if (!vp) {
311                                                 RDEBUG2("Failed creating EAP-Message");
312                                                 pairfree(&first);
313                                                 return NULL;
314                                         }
315                                 }
316
317                                 goto next_attr;
318                         } /* else it's another kind of attribute */
319                         /* FALL-THROUGH */
320
321                 default:
322                         vp->length = size;
323                         memcpy(vp->vp_octets, data, vp->length);
324                         break;
325                 }
326
327                 /*
328                  *      User-Password is NUL padded to a multiple
329                  *      of 16 bytes.  Let's chop it to something
330                  *      more reasonable.
331                  *
332                  *      NOTE: This means that the User-Password
333                  *      attribute CANNOT EVER have embedded zeros in it!
334                  */
335                 switch (vp->attribute) {
336                 case PW_USER_PASSWORD:
337                         /*
338                          *      If the password is exactly 16 octets,
339                          *      it won't be zero-terminated.
340                          */
341                         vp->vp_strvalue[vp->length] = '\0';
342                         vp->length = strlen(vp->vp_strvalue);
343                         break;
344
345                         /*
346                          *      Ensure that the client is using the
347                          *      correct challenge.  This weirdness is
348                          *      to protect against against replay
349                          *      attacks, where anyone observing the
350                          *      CHAP exchange could pose as that user,
351                          *      by simply choosing to use the same
352                          *      challenge.
353                          *
354                          *      By using a challenge based on
355                          *      information from the current session,
356                          *      we can guarantee that the client is
357                          *      not *choosing* a challenge.
358                          *
359                          *      We're a little forgiving in that we
360                          *      have loose checks on the length, and
361                          *      we do NOT check the Id (first octet of
362                          *      the response to the challenge)
363                          *
364                          *      But if the client gets the challenge correct,
365                          *      we're not too worried about the Id.
366                          */
367                 case PW_CHAP_CHALLENGE:
368                 case PW_MSCHAP_CHALLENGE:
369                         if ((vp->length < 8) ||
370                             (vp->length > 16)) {
371                                 RDEBUG("Tunneled challenge has invalid length");
372                                 pairfree(&first);
373                                 pairfree(&vp);
374                                 return NULL;
375
376                         } else {
377                                 uint8_t challenge[16];
378
379                                 eapttls_gen_challenge(ssl, challenge,
380                                                       sizeof(challenge));
381
382                                 if (memcmp(challenge, vp->vp_octets,
383                                            vp->length) != 0) {
384                                         RDEBUG("Tunneled challenge is incorrect");
385                                         pairfree(&first);
386                                         pairfree(&vp);
387                                         return NULL;
388                                 }
389                         }
390                         break;
391
392                 default:
393                         break;
394                 } /* switch over checking/re-writing of attributes. */
395
396                 /*
397                  *      Update the list.
398                  */
399                 *last = vp;
400                 last = &(vp->next);
401
402         next_attr:
403                 /*
404                  *      Catch non-aligned attributes.
405                  */
406                 if (data_left == length) break;
407
408                 /*
409                  *      The length does NOT include the padding, so
410                  *      we've got to account for it here by rounding up
411                  *      to the nearest 4-byte boundary.
412                  */
413                 length += 0x03;
414                 length &= ~0x03;
415
416                 rad_assert(data_left >= length);
417                 data_left -= length;
418                 data += length - offset; /* already updated */
419         }
420
421         /*
422          *      We got this far.  It looks OK.
423          */
424         return first;
425 }
426
427 /*
428  *      Convert VALUE_PAIR's to diameter attributes, and write them
429  *      to an SSL session.
430  *
431  *      The ONLY VALUE_PAIR's which may be passed to this function
432  *      are ones which can go inside of a RADIUS (i.e. diameter)
433  *      packet.  So no server-configuration attributes, or the like.
434  */
435 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
436 {
437         /*
438          *      RADIUS packets are no more than 4k in size, so if
439          *      we've got more than 4k of data to write, it's very
440          *      bad.
441          */
442         uint8_t         buffer[4096];
443         uint8_t         *p;
444         uint32_t        attr;
445         uint32_t        length;
446         uint32_t        vendor;
447         size_t          total;
448         VALUE_PAIR      *vp;
449
450         p = buffer;
451         total = 0;
452
453         for (vp = first; vp != NULL; vp = vp->next) {
454                 /*
455                  *      Too much data: die.
456                  */
457                 if ((total + vp->length + 12) >= sizeof(buffer)) {
458                         RDEBUG2("output buffer is full!");
459                         return 0;
460                 }
461
462                 /*
463                  *      Hmm... we don't group multiple EAP-Messages
464                  *      together.  Maybe we should...
465                  */
466
467                 /*
468                  *      Length is no more than 253, due to RADIUS
469                  *      issues.
470                  */
471                 length = vp->length;
472                 vendor = (vp->attribute >> 16) & 0xffff;
473                 if (vendor != 0) {
474                         attr = vp->attribute & 0xffff;
475                         length |= (1 << 31);
476                 } else {
477                         attr = vp->attribute;
478                 }
479
480                 /*
481                  *      Hmm... set the M bit for all attributes?
482                  */
483                 length |= (1 << 30);
484
485                 attr = ntohl(attr);
486
487                 memcpy(p, &attr, sizeof(attr));
488                 p += 4;
489                 total += 4;
490
491                 length += 8;    /* includes 8 bytes of attr & length */
492
493                 if (vendor != 0) {
494                         length += 4; /* include 4 bytes of vendor */
495
496                         length = ntohl(length);
497                         memcpy(p, &length, sizeof(length));
498                         p += 4;
499                         total += 4;
500
501                         vendor = ntohl(vendor);
502                         memcpy(p, &vendor, sizeof(vendor));
503                         p += 4;
504                         total += 4;
505                 } else {
506                         length = ntohl(length);
507                         memcpy(p, &length, sizeof(length));
508                         p += 4;
509                         total += 4;
510                 }
511
512                 switch (vp->type) {
513                 case PW_TYPE_INTEGER:
514                 case PW_TYPE_DATE:
515                         attr = ntohl(vp->vp_integer); /* stored in host order */
516                         memcpy(p, &attr, sizeof(attr));
517                         length = 4;
518                         break;
519
520                 case PW_TYPE_IPADDR:
521                         memcpy(p, &vp->vp_ipaddr, 4); /* network order */
522                         length = 4;
523                         break;
524
525                 case PW_TYPE_STRING:
526                 case PW_TYPE_OCTETS:
527                 default:
528                         memcpy(p, vp->vp_strvalue, vp->length);
529                         length = vp->length;
530                         break;
531                 }
532
533                 /*
534                  *      Skip to the end of the data.
535                  */
536                 p += length;
537                 total += length;
538
539                 /*
540                  *      Align the data to a multiple of 4 bytes.
541                  */
542                 if ((total & 0x03) != 0) {
543                         size_t i;
544
545                         length = 4 - (total & 0x03);
546                         for (i = 0; i < length; i++) {
547                                 *p = '\0';
548                                 p++;
549                                 total++;
550                         }
551                 }
552         } /* loop over the VP's to write. */
553
554         /*
555          *      Write the data in the buffer to the SSL session.
556          */
557         if (total > 0) {
558 #ifndef NDEBUG
559                 size_t i;
560
561                 if ((debug_flag > 2) && fr_log_fp) {
562                         for (i = 0; i < total; i++) {
563                           if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  TTLS tunnel data out %04x: ", (int) i);
564
565                                 fprintf(fr_log_fp, "%02x ", buffer[i]);
566
567                                 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
568                         }
569                         if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
570                 }
571 #endif
572
573                 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
574
575                 /*
576                  *      FIXME: Check the return code.
577                  */
578                 tls_handshake_send(request, tls_session);
579         }
580
581         /*
582          *      Everything's OK.
583          */
584         return 1;
585 }
586
587 /*
588  *      Use a reply packet to determine what to do.
589  */
590 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
591                          REQUEST *request, RADIUS_PACKET *reply)
592 {
593         int rcode = RLM_MODULE_REJECT;
594         VALUE_PAIR *vp;
595         ttls_tunnel_t *t = tls_session->opaque;
596
597         handler = handler;      /* -Wunused */
598
599         /*
600          *      If the response packet was Access-Accept, then
601          *      we're OK.  If not, die horribly.
602          *
603          *      FIXME: Take MS-CHAP2-Success attribute, and
604          *      tunnel it back to the client, to authenticate
605          *      ourselves to the client.
606          *
607          *      FIXME: If we have an Access-Challenge, then
608          *      the Reply-Message is tunneled back to the client.
609          *
610          *      FIXME: If we have an EAP-Message, then that message
611          *      must be tunneled back to the client.
612          *
613          *      FIXME: If we have an Access-Challenge with a State
614          *      attribute, then do we tunnel that to the client, or
615          *      keep track of it ourselves?
616          *
617          *      FIXME: EAP-Messages can only start with 'identity',
618          *      NOT 'eap start', so we should check for that....
619          */
620         switch (reply->code) {
621         case PW_AUTHENTICATION_ACK:
622                 RDEBUG("Got tunneled Access-Accept");
623
624                 rcode = RLM_MODULE_OK;
625
626                 /*
627                  *      MS-CHAP2-Success means that we do NOT return
628                  *      an Access-Accept, but instead tunnel that
629                  *      attribute to the client, and keep going with
630                  *      the TTLS session.  Once the client accepts
631                  *      our identity, it will respond with an empty
632                  *      packet, and we will send EAP-Success.
633                  */
634                 vp = NULL;
635                 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS);
636                 if (vp) {
637                         RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
638                         rcode = RLM_MODULE_HANDLED;
639                         t->authenticated = TRUE;
640
641                         /*
642                          *      Delete MPPE keys & encryption policy.  We don't
643                          *      want these here.
644                          */
645                         pairdelete(&reply->vps, ((311 << 16) | 7));
646                         pairdelete(&reply->vps, ((311 << 16) | 8));
647                         pairdelete(&reply->vps, ((311 << 16) | 16));
648                         pairdelete(&reply->vps, ((311 << 16) | 17));
649
650                         /*
651                          *      Use the tunneled reply, but not now.
652                          */
653                         if (t->use_tunneled_reply) {
654                                 t->accept_vps = reply->vps;
655                                 reply->vps = NULL;
656                         }
657
658                 } else { /* no MS-CHAP2-Success */
659                         /*
660                          *      Can only have EAP-Message if there's
661                          *      no MS-CHAP2-Success.  (FIXME: EAP-MSCHAP?)
662                          *
663                          *      We also do NOT tunnel the EAP-Success
664                          *      attribute back to the client, as the client
665                          *      can figure it out, from the non-tunneled
666                          *      EAP-Success packet.
667                          */
668                         pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE);
669                         pairfree(&vp);
670                 }
671
672                 /*
673                  *      Handle the ACK, by tunneling any necessary reply
674                  *      VP's back to the client.
675                  */
676                 if (vp) {
677                         vp2diameter(request, tls_session, vp);
678                         pairfree(&vp);
679                 }
680
681                 /*
682                  *      If we've been told to use the attributes from
683                  *      the reply, then do so.
684                  *
685                  *      WARNING: This may leak information about the
686                  *      tunneled user!
687                  */
688                 if (t->use_tunneled_reply) {
689                         pairdelete(&reply->vps, PW_PROXY_STATE);
690                         pairadd(&request->reply->vps, reply->vps);
691                         reply->vps = NULL;
692                 }
693                 break;
694
695
696         case PW_AUTHENTICATION_REJECT:
697                 RDEBUG("Got tunneled Access-Reject");
698                 rcode = RLM_MODULE_REJECT;
699                 break;
700
701                 /*
702                  *      Handle Access-Challenge, but only if we
703                  *      send tunneled reply data.  This is because
704                  *      an Access-Challenge means that we MUST tunnel
705                  *      a Reply-Message to the client.
706                  */
707         case PW_ACCESS_CHALLENGE:
708                 RDEBUG("Got tunneled Access-Challenge");
709
710                 /*
711                  *      Keep the State attribute, if necessary.
712                  *
713                  *      Get rid of the old State, too.
714                  */
715                 pairfree(&t->state);
716                 pairmove2(&t->state, &reply->vps, PW_STATE);
717
718                 /*
719                  *      We should really be a bit smarter about this,
720                  *      and move over only those attributes which
721                  *      are relevant to the authentication request,
722                  *      but that's a lot more work, and this "dumb"
723                  *      method works in 99.9% of the situations.
724                  */
725                 vp = NULL;
726                 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE);
727
728                 /*
729                  *      There MUST be a Reply-Message in the challenge,
730                  *      which we tunnel back to the client.
731                  *
732                  *      If there isn't one in the reply VP's, then
733                  *      we MUST create one, with an empty string as
734                  *      it's value.
735                  */
736                 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE);
737
738                 /*
739                  *      Handle the ACK, by tunneling any necessary reply
740                  *      VP's back to the client.
741                  */
742                 if (vp) {
743                         vp2diameter(request, tls_session, vp);
744                         pairfree(&vp);
745                 }
746                 rcode = RLM_MODULE_HANDLED;
747                 break;
748
749         default:
750                 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
751                 rcode = RLM_MODULE_INVALID;
752                 break;
753         }
754
755         return rcode;
756 }
757
758
759 #ifdef WITH_PROXY
760 /*
761  *      Do post-proxy processing,
762  */
763 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
764 {
765         int rcode;
766         tls_session_t *tls_session = (tls_session_t *) data;
767         REQUEST *fake, *request = handler->request;
768
769         RDEBUG("Passing reply from proxy back into the tunnel.");
770
771         /*
772          *      If there was a fake request associated with the proxied
773          *      request, do more processing of it.
774          */
775         fake = (REQUEST *) request_data_get(handler->request,
776                                             handler->request->proxy,
777                                             REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
778
779         /*
780          *      Do the callback, if it exists, and if it was a success.
781          */
782         if (fake &&
783             handler->request->proxy_reply &&
784             (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
785                 /*
786                  *      Terrible hacks.
787                  */
788                 rad_assert(fake->packet == NULL);
789                 fake->packet = request->proxy;
790                 fake->packet->src_ipaddr = request->packet->src_ipaddr;
791                 request->proxy = NULL;
792
793                 rad_assert(fake->reply == NULL);
794                 fake->reply = request->proxy_reply;
795                 request->proxy_reply = NULL;
796
797                 if ((debug_flag > 0) && fr_log_fp) {
798                         fprintf(fr_log_fp, "server %s {\n",
799                                 (fake->server == NULL) ? "" : fake->server);
800                 }
801
802                 /*
803                  *      Perform a post-auth stage for the tunneled
804                  *      session.
805                  */
806                 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
807                 rcode = rad_postauth(fake);
808                 RDEBUG2("post-auth returns %d", rcode);
809
810                 if ((debug_flag > 0) && fr_log_fp) {
811                         fprintf(fr_log_fp, "} # server %s\n",
812                                 (fake->server == NULL) ? "" : fake->server);
813                         
814                         RDEBUG("Final reply from tunneled session code %d",
815                                fake->reply->code);
816                         debug_pair_list(fake->reply->vps);
817                 }
818
819                 /*
820                  *      Terrible hacks.
821                  */
822                 request->proxy = fake->packet;
823                 fake->packet = NULL;
824                 request->proxy_reply = fake->reply;
825                 fake->reply = NULL;
826
827                 /*
828                  *      And we're done with this request.
829                  */
830
831                 switch (rcode) {
832                 case RLM_MODULE_FAIL:
833                         request_free(&fake);
834                         eaptls_fail(handler, 0);
835                         return 0;
836                         break;
837
838                 default:  /* Don't Do Anything */
839                         RDEBUG2("Got reply %d",
840                                request->proxy_reply->code);
841                         break;
842                 }
843         }
844         request_free(&fake);    /* robust if fake == NULL */
845
846         /*
847          *      Process the reply from the home server.
848          */
849         rcode = process_reply(handler, tls_session, handler->request,
850                               handler->request->proxy_reply);
851
852         /*
853          *      The proxy code uses the reply from the home server as
854          *      the basis for the reply to the NAS.  We don't want that,
855          *      so we toss it, after we've had our way with it.
856          */
857         pairfree(&handler->request->proxy_reply->vps);
858
859         switch (rcode) {
860         case RLM_MODULE_REJECT:
861                 RDEBUG("Reply was rejected");
862                 break;
863
864         case RLM_MODULE_HANDLED:
865                 RDEBUG("Reply was handled");
866                 eaptls_request(handler->eap_ds, tls_session);
867                 return 1;
868
869         case RLM_MODULE_OK:
870                 RDEBUG("Reply was OK");
871
872                 /*
873                  *      Success: Automatically return MPPE keys.
874                  */
875                 return eaptls_success(handler, 0);
876
877         default:
878                 RDEBUG("Reply was unknown.");
879                 break;
880         }
881
882         eaptls_fail(handler, 0);
883         return 0;
884 }
885
886
887 /*
888  *      Free a request.
889  */
890 static void my_request_free(void *data)
891 {
892         REQUEST *request = (REQUEST *)data;
893
894         request_free(&request);
895 }
896 #endif  /* WITH_PROXY */
897
898 /*
899  *      Process the "diameter" contents of the tunneled data.
900  */
901 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
902 {
903         int rcode = PW_AUTHENTICATION_REJECT;
904         REQUEST *fake;
905         VALUE_PAIR *vp;
906         ttls_tunnel_t *t;
907         const uint8_t *data;
908         size_t data_len;
909         REQUEST *request = handler->request;
910
911         /*
912          *      Just look at the buffer directly, without doing
913          *      record_minus.
914          */
915         data_len = tls_session->clean_out.used;
916         tls_session->clean_out.used = 0;
917         data = tls_session->clean_out.data;
918
919         t = (ttls_tunnel_t *) tls_session->opaque;
920
921         /*
922          *      If there's no data, maybe this is an ACK to an
923          *      MS-CHAP2-Success.
924          */
925         if (data_len == 0) {
926                 if (t->authenticated) {
927                         RDEBUG("Got ACK, and the user was already authenticated.");
928                         return PW_AUTHENTICATION_ACK;
929                 } /* else no session, no data, die. */
930
931                 /*
932                  *      FIXME: Call SSL_get_error() to see what went
933                  *      wrong.
934                  */
935                 RDEBUG2("SSL_read Error");
936                 return PW_AUTHENTICATION_REJECT;
937         }
938
939 #ifndef NDEBUG
940         if ((debug_flag > 2) && fr_log_fp) {
941                 size_t i;
942
943                 for (i = 0; i < data_len; i++) {
944                   if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  TTLS tunnel data in %04x: ", (int) i);
945
946                         fprintf(fr_log_fp, "%02x ", data[i]);
947
948                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
949                 }
950                 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
951         }
952 #endif
953
954         if (!diameter_verify(request, data, data_len)) {
955                 return PW_AUTHENTICATION_REJECT;
956         }
957
958         /*
959          *      Allocate a fake REQUEST structe.
960          */
961         fake = request_alloc_fake(request);
962
963         rad_assert(fake->packet->vps == NULL);
964
965         /*
966          *      Add the tunneled attributes to the fake request.
967          */
968         fake->packet->vps = diameter2vp(request, tls_session->ssl, data, data_len);
969         if (!fake->packet->vps) {
970                 request_free(&fake);
971                 return PW_AUTHENTICATION_REJECT;
972         }
973
974         /*
975          *      Tell the request that it's a fake one.
976          */
977         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
978         if (vp) {
979                 pairadd(&fake->packet->vps, vp);
980         }
981
982         if ((debug_flag > 0) && fr_log_fp) {
983                 RDEBUG("Got tunneled request");
984
985                 debug_pair_list(fake->packet->vps);
986         }
987
988         /*
989          *      Update other items in the REQUEST data structure.
990          */
991         fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
992         fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD);
993
994         /*
995          *      No User-Name, try to create one from stored data.
996          */
997         if (!fake->username) {
998                 /*
999                  *      No User-Name in the stored data, look for
1000                  *      an EAP-Identity, and pull it out of there.
1001                  */
1002                 if (!t->username) {
1003                         vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE);
1004                         if (vp &&
1005                             (vp->length >= EAP_HEADER_LEN + 2) &&
1006                             (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1007                             (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1008                             (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1009                                 /*
1010                                  *      Create & remember a User-Name
1011                                  */
1012                                 t->username = pairmake("User-Name", "", T_OP_EQ);
1013                                 rad_assert(t->username != NULL);
1014
1015                                 memcpy(t->username->vp_strvalue, vp->vp_strvalue + 5,
1016                                        vp->length - 5);
1017                                 t->username->length = vp->length - 5;
1018                                 t->username->vp_strvalue[t->username->length] = 0;
1019
1020                                 RDEBUG("Got tunneled identity of %s",
1021                                        t->username->vp_strvalue);
1022
1023                                 /*
1024                                  *      If there's a default EAP type,
1025                                  *      set it here.
1026                                  */
1027                                 if (t->default_eap_type != 0) {
1028                                         RDEBUG("Setting default EAP type for tunneled EAP session.");
1029                                         vp = paircreate(PW_EAP_TYPE,
1030                                                         PW_TYPE_INTEGER);
1031                                         rad_assert(vp != NULL);
1032                                         vp->vp_integer = t->default_eap_type;
1033                                         pairadd(&fake->config_items, vp);
1034                                 }
1035
1036                         } else {
1037                                 /*
1038                                  *      Don't reject the request outright,
1039                                  *      as it's permitted to do EAP without
1040                                  *      user-name.
1041                                  */
1042                                 RDEBUG2("WARNING! No EAP-Identity found to start EAP conversation.");
1043                         }
1044                 } /* else there WAS a t->username */
1045
1046                 if (t->username) {
1047                         vp = paircopy(t->username);
1048                         pairadd(&fake->packet->vps, vp);
1049                         fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
1050                 }
1051         } /* else the request ALREADY had a User-Name */
1052
1053         /*
1054          *      Add the State attribute, too, if it exists.
1055          */
1056         if (t->state) {
1057                 vp = paircopy(t->state);
1058                 if (vp) pairadd(&fake->packet->vps, vp);
1059         }
1060
1061         /*
1062          *      If this is set, we copy SOME of the request attributes
1063          *      from outside of the tunnel to inside of the tunnel.
1064          *
1065          *      We copy ONLY those attributes which do NOT already
1066          *      exist in the tunneled request.
1067          */
1068         if (t->copy_request_to_tunnel) {
1069                 VALUE_PAIR *copy;
1070
1071                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1072                         /*
1073                          *      The attribute is a server-side thingy,
1074                          *      don't copy it.
1075                          */
1076                         if ((vp->attribute > 255) &&
1077                             (((vp->attribute >> 16) & 0xffff) == 0)) {
1078                                 continue;
1079                         }
1080
1081                         /*
1082                          *      The outside attribute is already in the
1083                          *      tunnel, don't copy it.
1084                          *
1085                          *      This works for BOTH attributes which
1086                          *      are originally in the tunneled request,
1087                          *      AND attributes which are copied there
1088                          *      from below.
1089                          */
1090                         if (pairfind(fake->packet->vps, vp->attribute)) {
1091                                 continue;
1092                         }
1093
1094                         /*
1095                          *      Some attributes are handled specially.
1096                          */
1097                         switch (vp->attribute) {
1098                                 /*
1099                                  *      NEVER copy Message-Authenticator,
1100                                  *      EAP-Message, or State.  They're
1101                                  *      only for outside of the tunnel.
1102                                  */
1103                         case PW_USER_NAME:
1104                         case PW_USER_PASSWORD:
1105                         case PW_CHAP_PASSWORD:
1106                         case PW_CHAP_CHALLENGE:
1107                         case PW_PROXY_STATE:
1108                         case PW_MESSAGE_AUTHENTICATOR:
1109                         case PW_EAP_MESSAGE:
1110                         case PW_STATE:
1111                                 continue;
1112                                 break;
1113
1114                                 /*
1115                                  *      By default, copy it over.
1116                                  */
1117                         default:
1118                                 break;
1119                         }
1120
1121                         /*
1122                          *      Don't copy from the head, we've already
1123                          *      checked it.
1124                          */
1125                         copy = paircopy2(vp, vp->attribute);
1126                         pairadd(&fake->packet->vps, copy);
1127                 }
1128         }
1129
1130         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER)) != NULL) {
1131                 fake->server = vp->vp_strvalue;
1132
1133         } else if (t->virtual_server) {
1134                 fake->server = t->virtual_server;
1135
1136         } /* else fake->server == request->server */
1137
1138
1139         if ((debug_flag > 0) && fr_log_fp) {
1140                 RDEBUG("Sending tunneled request");
1141
1142                 debug_pair_list(fake->packet->vps);
1143
1144                 fprintf(fr_log_fp, "server %s {\n",
1145                         (fake->server == NULL) ? "" : fake->server);
1146         }
1147
1148         /*
1149          *      Call authentication recursively, which will
1150          *      do PAP, CHAP, MS-CHAP, etc.
1151          */
1152         rad_authenticate(fake);
1153
1154         /*
1155          *      Note that we don't do *anything* with the reply
1156          *      attributes.
1157          */
1158         if ((debug_flag > 0) && fr_log_fp) {
1159                 fprintf(fr_log_fp, "} # server %s\n",
1160                         (fake->server == NULL) ? "" : fake->server);
1161
1162                 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1163                 
1164                 debug_pair_list(fake->reply->vps);
1165         }
1166
1167         /*
1168          *      Decide what to do with the reply.
1169          */
1170         switch (fake->reply->code) {
1171         case 0:                 /* No reply code, must be proxied... */
1172 #ifdef WITH_PROXY
1173                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
1174                 if (vp) {
1175                         eap_tunnel_data_t *tunnel;
1176                         RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1177
1178                         /*
1179                          *      Tell the original request that it's going
1180                          *      to be proxied.
1181                          */
1182                         pairmove2(&(request->config_items),
1183                                   &(fake->config_items),
1184                                   PW_PROXY_TO_REALM);
1185
1186                         /*
1187                          *      Seed the proxy packet with the
1188                          *      tunneled request.
1189                          */
1190                         rad_assert(request->proxy == NULL);
1191                         request->proxy = fake->packet;
1192                         memset(&request->proxy->src_ipaddr, 0,
1193                                sizeof(request->proxy->src_ipaddr));
1194                         memset(&request->proxy->src_ipaddr, 0,
1195                                sizeof(request->proxy->src_ipaddr));
1196                         request->proxy->src_port = 0;
1197                         request->proxy->dst_port = 0;
1198                         fake->packet = NULL;
1199                         rad_free(&fake->reply);
1200                         fake->reply = NULL;
1201
1202                         /*
1203                          *      Set up the callbacks for the tunnel
1204                          */
1205                         tunnel = rad_malloc(sizeof(*tunnel));
1206                         memset(tunnel, 0, sizeof(*tunnel));
1207
1208                         tunnel->tls_session = tls_session;
1209                         tunnel->callback = eapttls_postproxy;
1210
1211                         /*
1212                          *      Associate the callback with the request.
1213                          */
1214                         rcode = request_data_add(request,
1215                                                  request->proxy,
1216                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1217                                                  tunnel, free);
1218                         rad_assert(rcode == 0);
1219
1220                         /*
1221                          *      rlm_eap.c has taken care of associating
1222                          *      the handler with the fake request.
1223                          *
1224                          *      So we associate the fake request with
1225                          *      this request.
1226                          */
1227                         rcode = request_data_add(request,
1228                                                  request->proxy,
1229                                                  REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1230                                                  fake, my_request_free);
1231                         rad_assert(rcode == 0);
1232                         fake = NULL;
1233
1234                         /*
1235                          *      Didn't authenticate the packet, but
1236                          *      we're proxying it.
1237                          */
1238                         rcode = PW_STATUS_CLIENT;
1239
1240                 } else
1241 #endif  /* WITH_PROXY */
1242                   {
1243                         RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1244                                request->number);
1245                         rcode = PW_AUTHENTICATION_REJECT;
1246                 }
1247                 break;
1248
1249         default:
1250                 /*
1251                  *      Returns RLM_MODULE_FOO, and we want to return
1252                  *      PW_FOO
1253                  */
1254                 rcode = process_reply(handler, tls_session, request,
1255                                       fake->reply);
1256                 switch (rcode) {
1257                 case RLM_MODULE_REJECT:
1258                         rcode = PW_AUTHENTICATION_REJECT;
1259                         break;
1260
1261                 case RLM_MODULE_HANDLED:
1262                         rcode = PW_ACCESS_CHALLENGE;
1263                         break;
1264
1265                 case RLM_MODULE_OK:
1266                         rcode = PW_AUTHENTICATION_ACK;
1267                         break;
1268
1269                 default:
1270                         rcode = PW_AUTHENTICATION_REJECT;
1271                         break;
1272                 }
1273                 break;
1274         }
1275
1276         request_free(&fake);
1277
1278         return rcode;
1279 }