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