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