15e89dd1d86d74dba51bb38480141cd3bf4a0d2b
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_peap / peap.c
1 /*
2  * peap.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_peap.h"
28
29 static int setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t);
30
31 /*
32  *      Send protected EAP-Failure
33  *
34  *       Result-TLV = Failure
35  */
36 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
37 {
38         uint8_t tlv_packet[11];
39         REQUEST *request = handler->request;
40
41         RDEBUG2("FAILURE");
42
43         tlv_packet[0] = PW_EAP_REQUEST;
44         tlv_packet[1] = handler->eap_ds->response->id +1;
45         tlv_packet[2] = 0;
46         tlv_packet[3] = 11;     /* length of this packet */
47         tlv_packet[4] = PW_EAP_TLV;
48         tlv_packet[5] = 0x80;
49         tlv_packet[6] = EAP_TLV_ACK_RESULT;
50         tlv_packet[7] = 0;
51         tlv_packet[8] = 2;      /* length of the data portion */
52         tlv_packet[9] = 0;
53         tlv_packet[10] = EAP_TLV_FAILURE;
54
55         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
56
57         /*
58          *      FIXME: Check the return code.
59          */
60         tls_handshake_send(request, tls_session);
61
62         return 1;
63 }
64
65
66 /*
67  *      Send protected EAP-Success
68  *
69  *       Result-TLV = Success
70  */
71 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
72 {
73         uint8_t tlv_packet[11];
74         REQUEST *request = handler->request;
75
76         RDEBUG2("SUCCESS");
77
78         tlv_packet[0] = PW_EAP_REQUEST;
79         tlv_packet[1] = handler->eap_ds->response->id +1;
80         tlv_packet[2] = 0;
81         tlv_packet[3] = 11;     /* length of this packet */
82         tlv_packet[4] = PW_EAP_TLV;
83         tlv_packet[5] = 0x80;   /* mandatory AVP */
84         tlv_packet[6] = EAP_TLV_ACK_RESULT;
85         tlv_packet[7] = 0;
86         tlv_packet[8] = 2;      /* length of the data portion */
87         tlv_packet[9] = 0;
88         tlv_packet[10] = EAP_TLV_SUCCESS;
89
90         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
91
92         /*
93          *      FIXME: Check the return code.
94          */
95         tls_handshake_send(request, tls_session);
96
97         return 1;
98 }
99
100
101 static int eappeap_identity(EAP_HANDLER *handler, tls_session_t *tls_session)
102 {
103         eap_packet_t eap_packet;
104
105         eap_packet.code = PW_EAP_REQUEST;
106         eap_packet.id = handler->eap_ds->response->id + 1;
107         eap_packet.length[0] = 0;
108         eap_packet.length[1] = EAP_HEADER_LEN + 1;
109         eap_packet.data[0] = PW_EAP_IDENTITY;
110
111         (tls_session->record_plus)(&tls_session->clean_in,
112                                   &eap_packet, sizeof(eap_packet));
113
114         tls_handshake_send(handler->request, tls_session);
115         (tls_session->record_init)(&tls_session->clean_in);
116
117         return 1;
118 }
119
120 /*
121  * Send an MS SoH request
122  */
123 static int eappeap_soh(EAP_HANDLER *handler, tls_session_t *tls_session)
124 {
125         uint8_t tlv_packet[20];
126
127         tlv_packet[0] = 254;    /* extended type */
128
129         tlv_packet[1] = 0;
130         tlv_packet[2] = 0x01;   /* ms vendor */
131         tlv_packet[3] = 0x37;
132
133         tlv_packet[4] = 0;      /* ms soh eap */
134         tlv_packet[5] = 0;
135         tlv_packet[6] = 0;
136         tlv_packet[7] = 0x21;
137
138         tlv_packet[8] = 0;      /* vendor-spec tlv */
139         tlv_packet[9] = 7;
140
141         tlv_packet[10] = 0;
142         tlv_packet[11] = 8;     /* payload len */
143
144         tlv_packet[12] = 0;     /* ms vendor */
145         tlv_packet[13] = 0;
146         tlv_packet[14] = 0x01;
147         tlv_packet[15] = 0x37;
148
149         tlv_packet[16] = 0;
150         tlv_packet[17] = 2;
151         tlv_packet[18] = 0;
152         tlv_packet[19] = 0;
153         
154         (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 20);
155         tls_handshake_send(handler->request, tls_session);
156         return 1;
157 }
158
159 static VALUE_PAIR* eapsoh_verify(REQUEST *request, const uint8_t *data, unsigned int data_len) {
160
161         VALUE_PAIR *vp;
162         uint8_t eap_type_base;
163         uint32_t eap_vendor;
164         uint32_t eap_type;
165         int rv;
166
167         vp = pairmake("SoH-Supported", "no", T_OP_EQ);
168         if (data && data[0] == PW_EAP_NAK) {
169                 RDEBUG("SoH - client NAKed");
170                 goto done;
171         }
172
173         if (!data || data_len < 8) {
174                 RDEBUG("SoH - eap payload too short");
175                 goto done;
176         }
177
178         eap_type_base = *data++;
179         if (eap_type_base != 254) {
180                 RDEBUG("SoH - response is not extended EAP: %i", eap_type_base);
181                 goto done;
182         }
183
184         eap_vendor = soh_pull_be_24(data); data += 3;
185         if (eap_vendor != 0x137) {
186                 RDEBUG("SoH - extended eap vendor %08x is not Microsoft", eap_vendor);
187                 goto done;
188         }
189
190         eap_type = soh_pull_be_32(data); data += 4;
191         if (eap_type != 0x21) {
192                 RDEBUG("SoH - response eap type %08x is not EAP-SoH", eap_type);
193                 goto done;
194         }
195
196
197         rv = soh_verify(request, vp, data, data_len - 8);
198         if (rv<0) {
199                 RDEBUG("SoH - error decoding payload: %s", fr_strerror());
200         } else {
201                 vp->vp_integer = 1;
202         }
203 done:
204         return vp;
205 }
206
207 /*
208  *      Verify the tunneled EAP message.
209  */
210 static int eapmessage_verify(REQUEST *request,
211                              const uint8_t *data, unsigned int data_len)
212 {
213         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
214         uint8_t eap_type;
215         char buffer[256];
216
217         /*
218          *      No data, OR only 1 byte of EAP type.
219          */
220         if (!data || (data_len == 0) ||
221             ((data_len <= 1) && (data[0] != PW_EAP_IDENTITY))) {
222                 return 0;
223         }
224
225         eap_type = *data;
226         switch (eap_type) {
227         case PW_EAP_IDENTITY:
228                 if (data_len == 1) {
229                         RDEBUG2("Identity - ");
230                         return 1;
231                 }
232                 RDEBUG2("Identity - %*s",
233                        data_len - 1, data + 1);
234                 return 1;
235                 break;
236
237                 /*
238                  *      If the first byte of the packet is
239                  *      EAP-Response, and the EAP data is a TLV,
240                  *      then it looks OK...
241                  */
242         case PW_EAP_RESPONSE:
243                 if (eap_packet->data[0] == PW_EAP_TLV) {
244                         RDEBUG2("Received EAP-TLV response.");
245                         return 1;
246                 }
247                 RDEBUG2("Got something weird.");
248                 break;
249
250
251                 /*
252                  *      We normally do Microsoft MS-CHAPv2 (26), versus
253                  *      Cisco MS-CHAPv2 (29).
254                  */
255         case PW_EAP_MSCHAPV2:
256         default:
257                 RDEBUG2("EAP type %s",
258                        eaptype_type2name(eap_type,
259                                          buffer, sizeof(buffer)));
260                 return 1;
261                 break;
262         }
263
264         return 0;
265 }
266
267 /*
268  *      Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
269  */
270 static VALUE_PAIR *eap2vp(REQUEST *request, EAP_DS *eap_ds,
271                           const uint8_t *data, size_t data_len)
272 {
273         size_t total;
274         VALUE_PAIR *vp = NULL, *head, **tail;
275
276         if (data_len > 65535) return NULL; /* paranoia */
277
278         vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
279         if (!vp) {
280                 RDEBUG2("Failure in creating VP");
281                 return NULL;
282         }
283
284         total = data_len;
285         if (total > 249) total = 249;
286
287         /*
288          *      Hand-build an EAP packet from the crap in PEAP version 0.
289          */
290         vp->vp_octets[0] = PW_EAP_RESPONSE;
291         vp->vp_octets[1] = eap_ds->response->id;
292         vp->vp_octets[2] = (data_len + EAP_HEADER_LEN) >> 8;
293         vp->vp_octets[3] = (data_len + EAP_HEADER_LEN) & 0xff;
294
295         memcpy(vp->vp_octets + EAP_HEADER_LEN, data, total);
296         vp->length = EAP_HEADER_LEN + total;
297
298         head = vp;
299         tail = &(vp->next);
300         while (total < data_len) {
301                 int vp_len;
302
303
304                 vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
305                 if (!vp) {
306                         RDEBUG2("Failure in creating VP");
307                         pairfree(&head);
308                         return NULL;
309                 }
310                 vp_len = (data_len - total);
311                 if (vp_len > 253) vp_len = 253;
312
313                 memcpy(vp->vp_octets, data + total, vp_len);
314                 vp->length = vp_len;
315                 
316                 total += vp_len;
317                 *tail = vp;
318                 tail = &(vp->next);
319         }
320
321         return head;
322 }
323
324
325 /*
326  *      Convert a list of VALUE_PAIR's to an EAP packet, through the
327  *      simple expedient of dumping the EAP message
328  */
329 static int vp2eap(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *vp)
330 {
331         rad_assert(vp != NULL);
332
333         /*
334          *      Skip the id, code, and length.  Just write the EAP
335          *      type & data to the client.
336          */
337 #ifndef NDEBUG
338         if ((debug_flag > 2) && fr_log_fp) {
339                 size_t i, total;
340                 VALUE_PAIR *this;
341
342                 total = 0;
343
344                 for (this = vp; this != NULL; this = this->next) {
345                         int start = 0;
346
347                         if (this == vp) start = EAP_HEADER_LEN;
348                         
349                         for (i = start; i < vp->length; i++) {
350                           if ((total & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data out %04x: ", (int) total);
351
352                                 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
353                                 
354                                 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
355                                 total++;
356                         }
357                 }
358                 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
359         }
360 #endif
361
362         /*
363          *      Send the EAP data, WITHOUT the header.
364          */
365         (tls_session->record_plus)(&tls_session->clean_in,
366                                    vp->vp_octets + EAP_HEADER_LEN,
367                                    vp->length - EAP_HEADER_LEN);
368         
369         /*
370          *      Send the rest of the EAP data.
371          */
372         for (vp = vp->next; vp != NULL; vp = vp->next) {
373                 (tls_session->record_plus)(&tls_session->clean_in,
374                                            vp->vp_octets, vp->length);
375         }
376
377         tls_handshake_send(request, tls_session);
378
379         return 1;
380 }
381
382
383 /*
384  *      See if there's a TLV in the response.
385  */
386 static int eappeap_check_tlv(REQUEST *request, const uint8_t *data)
387 {
388         const eap_packet_t *eap_packet = (const eap_packet_t *) data;
389
390         /*
391          *      Look for success or failure.
392          */
393         if ((eap_packet->code == PW_EAP_RESPONSE) &&
394             (eap_packet->data[0] == PW_EAP_TLV)) {
395                 if (data[10] == EAP_TLV_SUCCESS) {
396                         return 1;
397                 }
398
399                 if (data[10] == EAP_TLV_FAILURE) {
400                         RDEBUG2("Client rejected our response.  The password is probably incorrect.");
401                         return 0;
402                 }
403         }
404
405         return 0;
406 }
407
408
409 /*
410  *      Use a reply packet to determine what to do.
411  */
412 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
413                          REQUEST *request, RADIUS_PACKET *reply)
414 {
415         int rcode = RLM_MODULE_REJECT;
416         VALUE_PAIR *vp;
417         peap_tunnel_t *t = tls_session->opaque;
418
419         if ((debug_flag > 0) && fr_log_fp) {
420                 RDEBUG("Got tunneled reply RADIUS code %d",
421                        reply->code);
422
423                 debug_pair_list(reply->vps);
424         }
425
426         switch (reply->code) {
427         case PW_AUTHENTICATION_ACK:
428                 RDEBUG2("Tunneled authentication was successful.");
429                 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
430                 eappeap_success(handler, tls_session);
431                 rcode = RLM_MODULE_HANDLED;
432
433                 /*
434                  *      If we've been told to use the attributes from
435                  *      the reply, then do so.
436                  *
437                  *      WARNING: This may leak information about the
438                  *      tunneled user!
439                  */
440                 if (t->use_tunneled_reply) {
441                         RDEBUG2("Saving tunneled attributes for later");
442
443                         /*
444                          *      Clean up the tunneled reply.
445                          */
446                         pairdelete(&reply->vps, PW_PROXY_STATE, 0);
447                         pairdelete(&reply->vps, PW_EAP_MESSAGE, 0);
448                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0);
449
450                         /*
451                          *      Delete MPPE keys & encryption policy.  We don't
452                          *      want these here.
453                          */
454                         pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT);
455                         pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT);
456                         pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT);
457                         pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT);
458
459                         t->accept_vps = reply->vps;
460                         reply->vps = NULL;
461                 }
462                 break;
463
464         case PW_AUTHENTICATION_REJECT:
465                 RDEBUG2("Tunneled authentication was rejected.");
466                 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
467                 eappeap_failure(handler, tls_session);
468                 rcode = RLM_MODULE_HANDLED;
469                 break;
470
471         case PW_ACCESS_CHALLENGE:
472                 RDEBUG2("Got tunneled Access-Challenge");
473
474                 /*
475                  *      Keep the State attribute, if necessary.
476                  *
477                  *      Get rid of the old State, too.
478                  */
479                 pairfree(&t->state);
480                 pairmove2(&t->state, &(reply->vps), PW_STATE, 0);
481
482                 /*
483                  *      PEAP takes only EAP-Message attributes inside
484                  *      of the tunnel.  Any Reply-Message in the
485                  *      Access-Challenge is ignored.
486                  */
487                 vp = NULL;
488                 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE, 0);
489
490                 /*
491                  *      Handle EAP-MSCHAP-V2, where Access-Accept's
492                  *      from the home server may contain MS-CHAP-Success,
493                  *      which the module turns into challenges, so that
494                  *      the client may respond to the challenge with
495                  *      an "ack" packet.
496                  */
497                 if (t->home_access_accept && t->use_tunneled_reply) {
498                         RDEBUG2("Saving tunneled attributes for later");
499
500                         /*
501                          *      Clean up the tunneled reply.
502                          */
503                         pairdelete(&reply->vps, PW_PROXY_STATE, 0);
504                         pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0);
505
506                         t->accept_vps = reply->vps;
507                         reply->vps = NULL;
508                 }
509
510                 /*
511                  *      Handle the ACK, by tunneling any necessary reply
512                  *      VP's back to the client.
513                  */
514                 if (vp) {
515                         vp2eap(request, tls_session, vp);
516                         pairfree(&vp);
517                 }
518
519                 rcode = RLM_MODULE_HANDLED;
520                 break;
521
522         default:
523                 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
524                 rcode = RLM_MODULE_REJECT;
525                 break;
526         }
527
528         return rcode;
529 }
530
531 #ifdef WITH_PROXY
532 /*
533  *      Do post-proxy processing,
534  */
535 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
536 {
537         int rcode;
538         tls_session_t *tls_session = (tls_session_t *) data;
539         REQUEST *fake, *request = handler->request;
540
541         rad_assert(request != NULL);
542         RDEBUG2("Passing reply from proxy back into the tunnel.");
543
544         /*
545          *      If there was a fake request associated with the proxied
546          *      request, do more processing of it.
547          */
548         fake = (REQUEST *) request_data_get(handler->request,
549                                             handler->request->proxy,
550                                             REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
551
552         /*
553          *      Do the callback, if it exists, and if it was a success.
554          */
555         if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
556                 peap_tunnel_t *t = tls_session->opaque;
557
558                 t->home_access_accept = TRUE;
559
560                 /*
561                  *      Terrible hacks.
562                  */
563                 rad_assert(fake->packet == NULL);
564                 fake->packet = request->proxy;
565                 fake->packet->src_ipaddr = request->packet->src_ipaddr;
566                 request->proxy = NULL;
567
568                 rad_assert(fake->reply == NULL);
569                 fake->reply = request->proxy_reply;
570                 request->proxy_reply = NULL;
571
572                 if ((debug_flag > 0) && fr_log_fp) {
573                         fprintf(fr_log_fp, "server %s {\n", fake->server);
574                 }
575
576                 /*
577                  *      Perform a post-auth stage, which will get the EAP
578                  *      handler, too...
579                  */
580                 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
581                 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
582                 module_post_proxy(0, fake);
583
584                 /*
585                  *      FIXME: If rcode returns fail, do something
586                  *      intelligent...
587                  */
588                 rcode = rad_postauth(fake);
589
590                 if ((debug_flag > 0) && fr_log_fp) {
591                         fprintf(fr_log_fp, "} # server %s\n", fake->server);
592                         
593                         RDEBUG("Final reply from tunneled session code %d",
594                                fake->reply->code);
595                 
596                         debug_pair_list(fake->reply->vps);
597                 }
598
599                 /*
600                  *      Terrible hacks.
601                  */
602                 request->proxy = fake->packet;
603                 fake->packet = NULL;
604                 request->proxy_reply = fake->reply;
605                 fake->reply = NULL;
606
607                 /*
608                  *      And we're done with this request.
609                  */
610
611                 switch (rcode) {
612                 case RLM_MODULE_FAIL:
613                         request_free(&fake);
614                         eaptls_fail(handler, 0);
615                         return 0;
616                         break;
617
618                 default:  /* Don't Do Anything */
619                         RDEBUG2("Got reply %d", request->proxy_reply->code);
620                         break;
621                 }
622         }
623         request_free(&fake);    /* robust if fake == NULL */
624
625         /*
626          *      If there was no EAP-Message in the reply packet, then
627          *      we know that we're supposed to re-run the "authenticate"
628          *      stage, in order to get the right kind of handling...
629          */
630
631         /*
632          *      Process the reply from the home server.
633          */
634
635         rcode = process_reply(handler, tls_session, handler->request,
636                               handler->request->proxy_reply);
637
638         /*
639          *      The proxy code uses the reply from the home server as
640          *      the basis for the reply to the NAS.  We don't want that,
641          *      so we toss it, after we've had our way with it.
642          */
643         pairfree(&handler->request->proxy_reply->vps);
644
645         switch (rcode) {
646         case RLM_MODULE_REJECT:
647                 RDEBUG2("Reply was rejected");
648                 eaptls_fail(handler, 0);
649                 return 0;
650
651         case RLM_MODULE_HANDLED:
652                 RDEBUG2("Reply was handled");
653                 eaptls_request(handler->eap_ds, tls_session);
654                 return 1;
655
656         case RLM_MODULE_OK:
657                 RDEBUG2("Reply was OK");
658
659                 /*
660                  *      Success: Automatically return MPPE keys.
661                  */
662                 return eaptls_success(handler, 0);
663
664         default:
665                 RDEBUG2("Reply was unknown.");
666                 break;
667         }
668
669         eaptls_fail(handler, 0);
670         return 0;
671 }
672
673 /*
674  *      Free a request.
675  */
676 static void my_request_free(void *data)
677 {
678         REQUEST *request = (REQUEST *)data;
679
680         request_free(&request);
681 }
682 #endif
683
684
685 static const char *peap_state(peap_tunnel_t *t)
686 {
687         switch (t->status) {
688                 case PEAP_STATUS_TUNNEL_ESTABLISHED:
689                         return "TUNNEL ESTABLISHED";
690                 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
691                         return "WAITING FOR SOH RESPONSE";
692                 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
693                         return "WAITING FOR INNER IDENTITY";
694                 case PEAP_STATUS_SENT_TLV_SUCCESS:
695                         return "send tlv success";
696                 case PEAP_STATUS_SENT_TLV_FAILURE:
697                         return "send tlv failure";
698                 case PEAP_STATUS_PHASE2_INIT:
699                         return "phase2_init";
700                 case PEAP_STATUS_PHASE2:
701                         return "phase2";
702                 default:
703                         break;
704         }
705         return "?";
706 }
707
708 static void print_tunneled_data(const uint8_t *data, size_t data_len)
709 {
710         size_t i;
711
712         if ((debug_flag > 2) && fr_log_fp) {
713                 for (i = 0; i < data_len; i++) {
714                   if ((i & 0x0f) == 0) fprintf(fr_log_fp, "  PEAP tunnel data in %02x: ", (int) i);
715                         
716                         fprintf(fr_log_fp, "%02x ", data[i]);
717                         
718                         if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
719                 }
720                 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
721         }
722 }
723
724
725 /*
726  *      Process the pseudo-EAP contents of the tunneled data.
727  */
728 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
729 {
730         peap_tunnel_t *t = tls_session->opaque;
731         REQUEST *fake;
732         VALUE_PAIR *vp;
733         int rcode = RLM_MODULE_REJECT;
734         const uint8_t   *data;
735         unsigned int data_len;
736
737         REQUEST *request = handler->request;
738         EAP_DS *eap_ds = handler->eap_ds;
739
740         rad_assert(request != NULL);
741
742         /*
743          *      Just look at the buffer directly, without doing
744          *      record_minus.  This lets us avoid another data copy.
745          */
746         data_len = tls_session->clean_out.used;
747         tls_session->clean_out.used = 0;
748         data = tls_session->clean_out.data;
749
750         RDEBUG2("Peap state %s", peap_state(t));
751
752         if ((t->status != PEAP_STATUS_TUNNEL_ESTABLISHED) &&
753             !eapmessage_verify(request, data, data_len)) {
754                 RDEBUG2("FAILED processing PEAP: Tunneled data is invalid.");
755                 if (debug_flag > 2) print_tunneled_data(data, data_len);
756                 return RLM_MODULE_REJECT;
757         }
758
759         switch (t->status) {
760         case PEAP_STATUS_TUNNEL_ESTABLISHED:
761                 /* FIXME: should be no data in the buffer here, check & assert? */
762                 
763                 if (SSL_session_reused(tls_session->ssl)) {
764                         RDEBUG2("Skipping Phase2 because of session resumption");
765                         t->session_resumption_state = PEAP_RESUMPTION_YES;
766                         if (t->soh) {
767                                 t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
768                                 RDEBUG2("Requesting SoH from client");
769                                 eappeap_soh(handler, tls_session);
770                                 return RLM_MODULE_HANDLED;
771                         }
772                         /* we're good, send success TLV */
773                         t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
774                         eappeap_success(handler, tls_session);
775                         
776                 } else {
777                         /* send an identity request */
778                         t->session_resumption_state = PEAP_RESUMPTION_NO;
779                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
780                         eappeap_identity(handler, tls_session);
781                 }
782                 return RLM_MODULE_HANDLED;
783
784         case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
785                 /* we're expecting an identity response */
786                 if (data[0] != PW_EAP_IDENTITY) {
787                         RDEBUG("Expected EAP-Identity, got something else.");
788                         return RLM_MODULE_REJECT;
789                 }
790
791                 if (data_len >= sizeof(t->username->vp_strvalue)) {
792                         RDEBUG("EAP-Identity is too long");
793                         return RLM_MODULE_REJECT;
794                 }
795                 
796                 /*
797                  *      Save it for later.
798                  */
799                 t->username = pairmake("User-Name", "", T_OP_EQ);
800                 rad_assert(t->username != NULL);
801                 
802                 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
803                 t->username->length = data_len - 1;
804                 t->username->vp_strvalue[t->username->length] = 0;
805                 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
806                 if (t->soh) {
807                         t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
808                         RDEBUG2("Requesting SoH from client");
809                         eappeap_soh(handler, tls_session);
810                         return RLM_MODULE_HANDLED;
811                 }
812                 t->status = PEAP_STATUS_PHASE2_INIT;
813                 break;
814
815         case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
816                 fake = request_alloc_fake(request);
817                 rad_assert(fake->packet->vps == NULL);
818                 fake->packet->vps = eapsoh_verify(request, data, data_len);
819                 setup_fake_request(request, fake, t);
820
821                 if (t->soh_virtual_server) {
822                         fake->server = t->soh_virtual_server;
823                 }
824                 RDEBUG("Sending SoH request to server %s", fake->server ? fake->server : "NULL");
825                 debug_pair_list(fake->packet->vps);
826                 RDEBUG("server %s {", fake->server);
827                 rad_authenticate(fake);
828                 RDEBUG("} # server %s", fake->server);
829                 RDEBUG("Got SoH reply");
830                 debug_pair_list(fake->reply->vps);
831
832                 if (fake->reply->code != PW_AUTHENTICATION_ACK) {
833                         RDEBUG2("SoH was rejected");
834                         request_free(&fake);
835                         t->status = PEAP_STATUS_SENT_TLV_FAILURE;
836                         eappeap_failure(handler, tls_session);
837                         return RLM_MODULE_HANDLED;
838                 }
839
840                 /* save the SoH VPs */
841                 t->soh_reply_vps = fake->reply->vps;
842                 fake->reply->vps = NULL;
843                 request_free(&fake);
844
845                 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
846                         /* we're good, send success TLV */
847                         t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
848                         eappeap_success(handler, tls_session);
849                         return RLM_MODULE_HANDLED;
850                 }
851
852                 t->status = PEAP_STATUS_PHASE2_INIT;
853                 break;
854
855
856         /*
857          *      If we authenticated the user, then it's OK.
858          */
859         case PEAP_STATUS_SENT_TLV_SUCCESS:
860                 if (eappeap_check_tlv(request, data)) {
861                         RDEBUG2("Success");
862                         return RLM_MODULE_OK;
863                 }
864
865                 /*
866                  *      Otherwise, the client rejected the session
867                  *      resumption.  If the session is being re-used,
868                  *      we need to do a full authentication.
869                  *
870                  *      We do this by sending an EAP-Identity request
871                  *      inside of the PEAP tunnel.
872                  */
873                 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
874                         RDEBUG2("Client rejected session resumption.  Re-starting full authentication");
875                         
876                         /*
877                          *      Mark session resumption status.
878                          */
879                         t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
880                         t->session_resumption_state = PEAP_RESUMPTION_NO;
881                         
882                         eappeap_identity(handler, tls_session);
883                         return RLM_MODULE_HANDLED;
884                 }
885
886                 RDEBUG2("We sent a success, but received something weird in return.");
887                 return RLM_MODULE_REJECT;
888
889         /*
890          *      Damned if I know why the clients continue sending EAP
891          *      packets after we told them to f*ck off.
892          */
893         case PEAP_STATUS_SENT_TLV_FAILURE:
894                 RDEBUG(" The users session was previously rejected: returning reject (again.)");
895                 RDEBUG(" *** This means you need to read the PREVIOUS messages in the debug output");
896                 RDEBUG(" *** to find out the reason why the user was rejected.");
897                 RDEBUG(" *** Look for \"reject\" or \"fail\".  Those earlier messages will tell you.");
898                 RDEBUG(" *** what went wrong, and how to fix the problem.");
899                 return RLM_MODULE_REJECT;
900
901                 case PEAP_STATUS_PHASE2_INIT:
902                         RDEBUG("In state machine in phase2 init?");
903
904                 case PEAP_STATUS_PHASE2:
905                         break;
906
907                 default:
908                         RDEBUG2("Unhandled state in peap");
909                         return RLM_MODULE_REJECT;
910         }
911
912         fake = request_alloc_fake(request);
913
914         rad_assert(fake->packet->vps == NULL);
915
916         switch (t->status) {
917                 /*
918                  *      If we're in PHASE2_INIT, the phase2 method hasn't been
919                  *      sent an Identity packet yet; do so from the stored
920                  *      username and this will kick off the phase2 eap method
921                  */
922                 
923         case PEAP_STATUS_PHASE2_INIT: {
924                 int len = t->username->length + EAP_HEADER_LEN + 1;
925
926                 t->status = PEAP_STATUS_PHASE2;
927
928                 vp = paircreate(PW_EAP_MESSAGE, 0, PW_TYPE_OCTETS);
929
930                 vp->vp_octets[0] = PW_EAP_RESPONSE;
931                 vp->vp_octets[1] = eap_ds->response->id;
932                 vp->vp_octets[2] = (len >> 8) & 0xff;
933                 vp->vp_octets[3] = len & 0xff;
934                 vp->vp_octets[4] = PW_EAP_IDENTITY;
935
936                 memcpy(vp->vp_octets + EAP_HEADER_LEN + 1, t->username->vp_strvalue, t->username->length);
937                 vp->length = len;
938
939                 pairadd(&fake->packet->vps, vp);
940
941                 if (t->default_eap_type != 0) {
942                         RDEBUG2("Setting default EAP type for tunneled EAP session.");
943                         vp = pairmake("EAP-Type", "0", T_OP_EQ);
944                         vp->vp_integer = t->default_eap_type;
945                         pairadd(&fake->config_items, vp);
946                 }
947                 break; }
948
949         case PEAP_STATUS_PHASE2:
950                 fake->packet->vps = eap2vp(request, eap_ds, data, data_len);
951                 if (!fake->packet->vps) {
952                         request_free(&fake);
953                         RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
954                         return PW_AUTHENTICATION_REJECT;
955                 }
956                 break;
957
958         default:
959                 RDEBUG("Invalid state change in PEAP.");
960                 return PW_AUTHENTICATION_REJECT;
961         }
962
963         if ((debug_flag > 0) && fr_log_fp) {
964                 RDEBUG("Got tunneled request");
965                 
966                 debug_pair_list(fake->packet->vps);
967
968                 fprintf(fr_log_fp, "server %s {\n",
969                         (fake->server == NULL) ? "" : fake->server);
970         }
971
972         /*
973          *      Update other items in the REQUEST data structure.
974          */
975         if (!t->username) {
976                 /*
977                  *      There's no User-Name in the tunneled session,
978                  *      so we add one here, by pulling it out of the
979                  *      EAP-Identity packet.
980                  */
981                 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
982                         t->username = pairmake("User-Name", "", T_OP_EQ);
983                         rad_assert(t->username != NULL);
984
985                         memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
986                         t->username->length = data_len - 1;
987                         t->username->vp_strvalue[t->username->length] = 0;
988                         DEBUG2("  PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
989
990                         /*
991                          *      If there's a default EAP type,
992                          *      set it here.
993                          */
994                         if (t->default_eap_type != 0) {
995                                 DEBUG2("  PEAP: Setting default EAP type for tunneled EAP session.");
996                                 vp = pairmake("EAP-Type", "0", T_OP_EQ);
997                                 vp->vp_integer = t->default_eap_type;
998                                 pairadd(&fake->config_items, vp);
999                         }
1000                 }
1001         } /* else there WAS a t->username */
1002
1003         setup_fake_request(request, fake, t);
1004
1005         if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0)) != NULL) {
1006                 fake->server = vp->vp_strvalue;
1007
1008         } else if (t->virtual_server) {
1009                 fake->server = t->virtual_server;
1010
1011         } /* else fake->server == request->server */
1012
1013         if ((debug_flag > 0) && fr_log_fp) {
1014                 fprintf(fr_log_fp, "Sending tunneled request\n");
1015
1016                 debug_pair_list(fake->packet->vps);
1017
1018                 fprintf(fr_log_fp, "server %s {\n",
1019                         (fake->server == NULL) ? "" : fake->server);
1020         }
1021
1022         /*
1023          *      Call authentication recursively, which will
1024          *      do PAP, CHAP, MS-CHAP, etc.
1025          */
1026         rad_authenticate(fake);
1027
1028         /*
1029          *      Note that we don't do *anything* with the reply
1030          *      attributes.
1031          */
1032         if ((debug_flag > 0) && fr_log_fp) {
1033                 fprintf(fr_log_fp, "} # server %s\n",
1034                         (fake->server == NULL) ? "" : fake->server);
1035
1036                 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1037                 
1038                 debug_pair_list(fake->reply->vps);
1039         }
1040
1041         /*
1042          *      Decide what to do with the reply.
1043          */
1044         switch (fake->reply->code) {
1045         case 0:                 /* No reply code, must be proxied... */
1046 #ifdef WITH_PROXY
1047                 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0);
1048
1049                 if (vp) {
1050                         eap_tunnel_data_t *tunnel;
1051
1052                         /*
1053                          *      The tunneled request was NOT handled,
1054                          *      it has to be proxied.  This means that
1055                          *      the "authenticate" stage was never
1056                          *      performed.
1057                          *
1058                          *      If we are told to NOT proxy the
1059                          *      tunneled request as EAP, then this
1060                          *      means that we've got to decode it,
1061                          *      which means that we MUST run the
1062                          *      "authenticate" portion by hand, here.
1063                          *
1064                          *      Once the tunneled EAP session is ALMOST
1065                          *      done, THEN we proxy it...
1066                          */
1067                         if (!t->proxy_tunneled_request_as_eap) {
1068                                 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1069
1070                                 /*
1071                                  *      Hmm... should we check for
1072                                  *      Auth-Type & EAP-Message here?
1073                                  */
1074
1075
1076                                 /*
1077                                  *      Run the EAP authentication.
1078                                  */
1079                                 DEBUG2("  PEAP: Calling authenticate in order to initiate tunneled EAP session.");
1080                                 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
1081                                 if (rcode == RLM_MODULE_OK) {
1082                                         /*
1083                                          *      Authentication succeeded! Rah!
1084                                          */
1085                                         fake->reply->code = PW_AUTHENTICATION_ACK;
1086                                         goto do_process;
1087                                 }
1088
1089                                 if (rcode != RLM_MODULE_HANDLED) {
1090                                         DEBUG2("  PEAP: Can't handle the return code %d", rcode);
1091                                         rcode = RLM_MODULE_REJECT;
1092                                         goto done;
1093                                 }
1094
1095                                 /*
1096                                  *      The module decided it wasn't
1097                                  *      done.  Handle it like normal.
1098                                  */
1099                                 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1100                                         DEBUG2("    PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
1101                                         goto do_process;
1102                                 }
1103
1104                                 /*
1105                                  *      The module has decoded the
1106                                  *      EAP-Message into another set
1107                                  *      of attributes.
1108                                  */
1109                                 pairdelete(&fake->packet->vps,
1110                                            PW_EAP_MESSAGE, 0);
1111                         }
1112
1113                         DEBUG2("  PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1114
1115                         /*
1116                          *      Tell the original request that it's going
1117                          *      to be proxied.
1118                          */
1119                         pairmove2(&(request->config_items),
1120                                   &(fake->config_items),
1121                                   PW_PROXY_TO_REALM, 0);
1122
1123                         /*
1124                          *      Seed the proxy packet with the
1125                          *      tunneled request.
1126                          */
1127                         rad_assert(request->proxy == NULL);
1128                         request->proxy = fake->packet;
1129                         memset(&request->proxy->src_ipaddr, 0,
1130                                sizeof(request->proxy->src_ipaddr));
1131                         memset(&request->proxy->src_ipaddr, 0,
1132                                sizeof(request->proxy->src_ipaddr));
1133                         request->proxy->src_port = 0;
1134                         request->proxy->dst_port = 0;
1135                         fake->packet = NULL;
1136                         rad_free(&fake->reply);
1137                         fake->reply = NULL;
1138
1139                         /*
1140                          *      Set up the callbacks for the tunnel
1141                          */
1142                         tunnel = rad_malloc(sizeof(*tunnel));
1143                         memset(tunnel, 0, sizeof(*tunnel));
1144
1145                         tunnel->tls_session = tls_session;
1146                         tunnel->callback = eappeap_postproxy;
1147
1148                         /*
1149                          *      Associate the callback with the request.
1150                          */
1151                         rcode = request_data_add(request,
1152                                                  request->proxy,
1153                                                  REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1154                                                  tunnel, free);
1155                         rad_assert(rcode == 0);
1156
1157                         /*
1158                          *      We're not proxying it as EAP, so we've got
1159                          *      to do the callback later.
1160                          */
1161                         if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1162                                 DEBUG2("  PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
1163
1164                                 /*
1165                                  *      rlm_eap.c has taken care of associating
1166                                  *      the handler with the fake request.
1167                                  *
1168                                  *      So we associate the fake request with
1169                                  *      this request.
1170                                  */
1171                                 rcode = request_data_add(request,
1172                                                          request->proxy,
1173                                                          REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1174                                                          fake, my_request_free);
1175                                 rad_assert(rcode == 0);
1176
1177                                 /*
1178                                  *      Do NOT free the fake request!
1179                                  */
1180                                 return RLM_MODULE_UPDATED;
1181                         }
1182
1183                         /*
1184                          *      Didn't authenticate the packet, but
1185                          *      we're proxying it.
1186                          */
1187                         rcode = RLM_MODULE_UPDATED;
1188
1189                 } else
1190 #endif  /* WITH_PROXY */
1191                   {
1192                         DEBUG2("  PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1193                         rcode = RLM_MODULE_REJECT;
1194                   }
1195                 break;
1196
1197         default:
1198         do_process:
1199                 rcode = process_reply(handler, tls_session, request,
1200                                       fake->reply);
1201                 break;
1202         }
1203
1204  done:
1205         request_free(&fake);
1206
1207         return rcode;
1208 }
1209
1210 static int setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t) {
1211
1212         VALUE_PAIR *vp;
1213         /*
1214          *      Tell the request that it's a fake one.
1215          */
1216         vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1217         if (vp) {
1218                 pairadd(&fake->packet->vps, vp);
1219         }
1220
1221         if (t->username) {
1222                 vp = paircopy(t->username);
1223                 pairadd(&fake->packet->vps, vp);
1224                 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0);
1225                 RDEBUG2("Setting User-Name to %s", fake->username->vp_strvalue);
1226         } else {
1227                 RDEBUG2("No tunnel username (SSL resumption?)");
1228         }
1229
1230
1231         /*
1232          *      Add the State attribute, too, if it exists.
1233          */
1234         if (t->state) {
1235                 vp = paircopy(t->state);
1236                 if (vp) pairadd(&fake->packet->vps, vp);
1237         }
1238
1239         /*
1240          *      If this is set, we copy SOME of the request attributes
1241          *      from outside of the tunnel to inside of the tunnel.
1242          *
1243          *      We copy ONLY those attributes which do NOT already
1244          *      exist in the tunneled request.
1245          *
1246          *      This code is copied from ../rlm_eap_ttls/ttls.c
1247          */
1248         if (t->copy_request_to_tunnel) {
1249                 VALUE_PAIR *copy;
1250
1251                 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1252                         /*
1253                          *      The attribute is a server-side thingy,
1254                          *      don't copy it.
1255                          */
1256                         if ((vp->attribute > 255) &&
1257                             (((vp->attribute >> 16) & 0xffff) == 0)) {
1258                                 continue;
1259                         }
1260
1261                         /*
1262                          *      The outside attribute is already in the
1263                          *      tunnel, don't copy it.
1264                          *
1265                          *      This works for BOTH attributes which
1266                          *      are originally in the tunneled request,
1267                          *      AND attributes which are copied there
1268                          *      from below.
1269                          */
1270                         if (pairfind(fake->packet->vps, vp->attribute, vp->vendor)) {
1271                                 continue;
1272                         }
1273
1274                         /*
1275                          *      Some attributes are handled specially.
1276                          */
1277                         switch (vp->attribute) {
1278                                 /*
1279                                  *      NEVER copy Message-Authenticator,
1280                                  *      EAP-Message, or State.  They're
1281                                  *      only for outside of the tunnel.
1282                                  */
1283                         case PW_USER_NAME:
1284                         case PW_USER_PASSWORD:
1285                         case PW_CHAP_PASSWORD:
1286                         case PW_CHAP_CHALLENGE:
1287                         case PW_PROXY_STATE:
1288                         case PW_MESSAGE_AUTHENTICATOR:
1289                         case PW_EAP_MESSAGE:
1290                         case PW_STATE:
1291                                 continue;
1292                                 break;
1293
1294                                 /*
1295                                  *      By default, copy it over.
1296                                  */
1297                         default:
1298                                 break;
1299                         }
1300
1301                         /*
1302                          *      Don't copy from the head, we've already
1303                          *      checked it.
1304                          */
1305                         copy = paircopy2(vp, vp->attribute, vp->vendor);
1306                         pairadd(&fake->packet->vps, copy);
1307                 }
1308         }
1309
1310         return 0;
1311 }