2 * peap.c contains the interfaces that are called from eap
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.
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.
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
20 * Copyright 2003 Alan DeKok <aland@freeradius.org>
21 * Copyright 2006 The FreeRADIUS server project
25 USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
29 static int setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t);
32 * Send protected EAP-Failure
34 * Result-TLV = Failure
36 static int eappeap_failure(eap_handler_t *handler, tls_session_t *tls_session)
38 uint8_t tlv_packet[11];
39 REQUEST *request = handler->request;
43 tlv_packet[0] = PW_EAP_REQUEST;
44 tlv_packet[1] = handler->eap_ds->response->id +1;
46 tlv_packet[3] = 11; /* length of this packet */
47 tlv_packet[4] = PW_EAP_TLV;
49 tlv_packet[6] = EAP_TLV_ACK_RESULT;
51 tlv_packet[8] = 2; /* length of the data portion */
53 tlv_packet[10] = EAP_TLV_FAILURE;
55 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
58 * FIXME: Check the return code.
60 tls_handshake_send(request, tls_session);
67 * Send protected EAP-Success
69 * Result-TLV = Success
71 static int eappeap_success(eap_handler_t *handler, tls_session_t *tls_session)
73 uint8_t tlv_packet[11];
74 REQUEST *request = handler->request;
78 tlv_packet[0] = PW_EAP_REQUEST;
79 tlv_packet[1] = handler->eap_ds->response->id +1;
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;
86 tlv_packet[8] = 2; /* length of the data portion */
88 tlv_packet[10] = EAP_TLV_SUCCESS;
90 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
93 * FIXME: Check the return code.
95 tls_handshake_send(request, tls_session);
101 static int eappeap_identity(eap_handler_t *handler, tls_session_t *tls_session)
103 eap_packet_raw_t eap_packet;
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;
111 (tls_session->record_plus)(&tls_session->clean_in,
112 &eap_packet, sizeof(eap_packet));
114 tls_handshake_send(handler->request, tls_session);
115 (tls_session->record_init)(&tls_session->clean_in);
121 * Send an MS SoH request
123 static int eappeap_soh(eap_handler_t *handler, tls_session_t *tls_session)
125 uint8_t tlv_packet[20];
127 tlv_packet[0] = 254; /* extended type */
130 tlv_packet[2] = 0x01; /* ms vendor */
131 tlv_packet[3] = 0x37;
133 tlv_packet[4] = 0; /* ms soh eap */
136 tlv_packet[7] = 0x21;
138 tlv_packet[8] = 0; /* vendor-spec tlv */
142 tlv_packet[11] = 8; /* payload len */
144 tlv_packet[12] = 0; /* ms vendor */
146 tlv_packet[14] = 0x01;
147 tlv_packet[15] = 0x37;
154 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 20);
155 tls_handshake_send(handler->request, tls_session);
159 static void eapsoh_verify(REQUEST *request, RADIUS_PACKET *packet,
160 uint8_t const *data, unsigned int data_len) {
163 uint8_t eap_method_base;
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");
174 if (!data || data_len < 8) {
175 RDEBUG("SoH - eap payload too short");
179 eap_method_base = *data++;
180 if (eap_method_base != 254) {
181 RDEBUG("SoH - response is not extended EAP: %i", eap_method_base);
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);
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);
198 rv = soh_verify(request, data, data_len - 8);
200 RDEBUG("SoH - error decoding payload: %s", fr_strerror());
207 * Verify the tunneled EAP message.
209 static int eapmessage_verify(REQUEST *request,
210 uint8_t const *data, unsigned int data_len, int peap_version)
212 eap_packet_raw_t const *eap_packet = (eap_packet_raw_t const *) data;
213 eap_type_t eap_method;
218 if (peap_version == 1) return 1;
221 * No data, OR only 1 byte of EAP type.
223 if (!data || (data_len == 0) ||
224 ((data_len <= 1) && (data[0] != PW_EAP_IDENTITY))) {
229 switch (eap_method) {
230 case PW_EAP_IDENTITY:
232 RDEBUG2("Identity - ");
235 RDEBUG2("Identity - %*s",
236 data_len - 1, data + 1);
240 * If the first byte of the packet is
241 * EAP-Response, and the EAP data is a TLV,
242 * then it looks OK...
244 case PW_EAP_RESPONSE:
245 if (eap_packet->data[0] == PW_EAP_TLV) {
246 RDEBUG2("Received EAP-TLV response");
249 RDEBUG2("Received unexpected EAP-Response, rejecting the session.");
254 * We normally do Microsoft MS-CHAPv2 (26), versus
255 * Cisco MS-CHAPv2 (29).
257 case PW_EAP_MSCHAPV2:
259 RDEBUG2("EAP method %s (%d)", eap_type2name(eap_method),
268 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
270 static VALUE_PAIR *eap2vp(UNUSED REQUEST *request, RADIUS_PACKET *packet,
272 uint8_t const *data, size_t data_len, int peap_version)
276 VALUE_PAIR *vp = NULL, *head = NULL;
279 if (data_len > 65535) return NULL; /* paranoia */
281 vp = fr_pair_afrom_num(packet, PW_EAP_MESSAGE, 0);
287 if (total > 249) total = 249;
289 if (peap_version == 0) {
291 * Hand-build an EAP packet from the crap in PEAP version 0.
293 vp->vp_length = EAP_HEADER_LEN + total;
294 vp->vp_octets = p = talloc_array(vp, uint8_t, vp->vp_length);
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;
301 memcpy(p + EAP_HEADER_LEN, data, total);
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);
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);
314 fr_pair_list_free(&head);
318 fr_pair_value_memcpy(vp, data + total, (data_len - total));
320 total += vp->vp_length;
322 fr_cursor_insert(&cursor, vp);
330 * Convert a list of VALUE_PAIR's to an EAP packet, through the
331 * simple expedient of dumping the EAP message
333 static int vp2eap(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *vp)
335 rad_assert(vp != NULL);
338 size_t header = EAP_HEADER_LEN;
340 if (tls_session->peap_flag > 0) header = 0;
343 * Skip the id, code, and length. Just write the EAP
344 * type & data to the client.
347 if ((rad_debug_lvl > 2) && fr_log_fp) {
348 size_t i, total, start = header;
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);
356 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
358 if ((total & 0x0f) == 0x0f) {
359 fprintf(fr_log_fp, "\n");
368 if ((total & 0x0f) != 0) {
369 fprintf(fr_log_fp, "\n");
375 * Send the EAP data in the first attribute, WITHOUT the
378 (tls_session->record_plus)(&tls_session->clean_in, vp->vp_octets + header, vp->vp_length - header);
381 * Send the rest of the EAP data, but skipping the first VP.
383 fr_cursor_init(&cursor, &vp);
384 for (this = fr_cursor_next(&cursor);
386 this = fr_cursor_next(&cursor)) {
387 (tls_session->record_plus)(&tls_session->clean_in, this->vp_octets, this->vp_length);
390 tls_handshake_send(request, tls_session);
397 * See if there's a TLV in the response.
399 static int eappeap_check_tlv(REQUEST *request, uint8_t const *data,
402 eap_packet_raw_t const *eap_packet = (eap_packet_raw_t const *) data;
404 if (data_len < 11) return 0;
407 * Look for success or failure.
409 if ((eap_packet->code == PW_EAP_RESPONSE) &&
410 (eap_packet->data[0] == PW_EAP_TLV)) {
411 if (data[10] == EAP_TLV_SUCCESS) {
415 if (data[10] == EAP_TLV_FAILURE) {
416 RDEBUG2("Client rejected our response. The password is probably incorrect");
421 RDEBUG("Unknown TLV %02x", data[10]);
428 * Use a reply packet to determine what to do.
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)
433 rlm_rcode_t rcode = RLM_MODULE_REJECT;
435 peap_tunnel_t *t = tls_session->opaque;
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);
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;
450 * If we've been told to use the attributes from
451 * the reply, then do so.
453 * WARNING: This may leak information about the
456 if (t->use_tunneled_reply) {
457 RDEBUG2("Saving tunneled attributes for later");
460 * Clean up the tunneled reply.
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);
467 * Delete MPPE keys & encryption policy. We don't
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);
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);
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;
488 case PW_CODE_ACCESS_CHALLENGE:
489 RDEBUG2("Got tunneled Access-Challenge");
492 * Keep the State attribute, if necessary.
494 * Get rid of the old State, too.
496 fr_pair_list_free(&t->state);
497 fr_pair_list_mcopy_by_num(t, &t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
500 * PEAP takes only EAP-Message attributes inside
501 * of the tunnel. Any Reply-Message in the
502 * Access-Challenge is ignored.
505 fr_pair_list_mcopy_by_num(t, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
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
514 if (t->home_access_accept && t->use_tunneled_reply) {
515 RDEBUG2("Saving tunneled attributes for later");
518 * Clean up the tunneled reply.
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);
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);
529 * Handle the ACK, by tunneling any necessary reply
530 * VP's back to the client.
533 vp2eap(request, tls_session, vp);
534 fr_pair_list_free(&vp);
537 rcode = RLM_MODULE_HANDLED;
541 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
542 rcode = RLM_MODULE_REJECT;
551 * Do post-proxy processing,
553 static int CC_HINT(nonnull) eappeap_postproxy(eap_handler_t *handler, void *data)
556 tls_session_t *tls_session = (tls_session_t *) data;
557 REQUEST *fake, *request = handler->request;
559 RDEBUG2("Passing reply from proxy back into the tunnel");
562 * If there was a fake request associated with the proxied
563 * request, do more processing of it.
565 fake = (REQUEST *) request_data_get(handler->request,
566 handler->request->proxy,
567 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
570 * Do the callback, if it exists, and if it was a success.
572 if (fake && (handler->request->proxy_reply->code == PW_CODE_ACCESS_ACCEPT)) {
573 peap_tunnel_t *t = tls_session->opaque;
575 t->home_access_accept = true;
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;
585 rad_assert(!fake->reply);
586 fake->reply = talloc_steal(fake, request->proxy_reply);
587 request->proxy_reply = NULL;
589 if ((rad_debug_lvl > 0) && fr_log_fp) {
590 fprintf(fr_log_fp, "server %s {\n", fake->server);
594 * Perform a post-auth stage, which will get the EAP
597 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
598 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
599 process_post_proxy(0, fake);
602 * FIXME: If rcode returns fail, do something
605 rcode = rad_postauth(fake);
607 if ((rad_debug_lvl > 0) && fr_log_fp) {
608 fprintf(fr_log_fp, "} # server %s\n", fake->server);
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);
617 request->proxy = talloc_steal(request, fake->packet);
619 request->proxy_reply = talloc_steal(request, fake->reply);
623 * And we're done with this request.
627 case RLM_MODULE_FAIL:
629 eaptls_fail(handler, 0);
632 default: /* Don't Do Anything */
633 RDEBUG2("Got reply %d", request->proxy_reply->code);
637 talloc_free(fake); /* robust if !fake */
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...
646 * Process the reply from the home server.
649 rcode = process_reply(handler, tls_session, handler->request,
650 handler->request->proxy_reply);
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.
657 fr_pair_list_free(&handler->request->proxy_reply->vps);
660 case RLM_MODULE_REJECT:
661 RDEBUG2("Reply was rejected");
662 eaptls_fail(handler, 0);
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;
672 RDEBUG2("Reply was OK");
675 * Success: Automatically return MPPE keys.
677 return eaptls_success(handler, 0);
680 RDEBUG2("Reply was unknown");
684 eaptls_fail(handler, 0);
690 static char const *peap_state(peap_tunnel_t *t)
693 case PEAP_STATUS_TUNNEL_ESTABLISHED:
694 return "TUNNEL ESTABLISHED";
696 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
697 return "WAITING FOR SOH RESPONSE";
699 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
700 return "WAITING FOR INNER IDENTITY";
702 case PEAP_STATUS_SENT_TLV_SUCCESS:
703 return "send tlv success";
705 case PEAP_STATUS_SENT_TLV_FAILURE:
706 return "send tlv failure";
708 case PEAP_STATUS_PHASE2_INIT:
709 return "phase2_init";
711 case PEAP_STATUS_PHASE2:
720 static void print_tunneled_data(uint8_t const *data, size_t data_len)
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);
728 fprintf(fr_log_fp, "%02x ", data[i]);
730 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
732 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
738 * Process the pseudo-EAP contents of the tunneled data.
740 rlm_rcode_t eappeap_process(eap_handler_t *handler, tls_session_t *tls_session, int auth_type_eap)
742 peap_tunnel_t *t = tls_session->opaque;
745 rlm_rcode_t rcode = RLM_MODULE_REJECT;
747 unsigned int data_len;
750 REQUEST *request = handler->request;
751 EAP_DS *eap_ds = handler->eap_ds;
754 * Just look at the buffer directly, without doing
755 * record_minus. This lets us avoid another data copy.
757 data_len = tls_session->clean_out.used;
758 tls_session->clean_out.used = 0;
759 data = tls_session->clean_out.data;
761 RDEBUG2("PEAP state %s", peap_state(t));
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;
770 if (tls_session->peap_flag > 0) header = EAP_HEADER_LEN;
773 case PEAP_STATUS_TUNNEL_ESTABLISHED:
774 /* FIXME: should be no data in the buffer here, check & assert? */
776 if (SSL_session_reused(tls_session->ssl)) {
777 RDEBUG2("Skipping Phase2 because of session resumption");
778 t->session_resumption_state = PEAP_RESUMPTION_YES;
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;
785 /* we're good, send success TLV */
786 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
787 eappeap_success(handler, tls_session);
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);
795 return RLM_MODULE_HANDLED;
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;
807 t->username = fr_pair_make(t, NULL, "User-Name", NULL, T_OP_EQ);
808 rad_assert(t->username != NULL);
810 fr_pair_value_bstrncpy(t->username, data + header + 1, data_len - header - 1);
812 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
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;
819 t->status = PEAP_STATUS_PHASE2_INIT;
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);
828 if (t->soh_virtual_server) {
829 fake->server = t->soh_virtual_server;
831 RDEBUG("Sending SoH request to server %s", fake->server ? fake->server : "NULL");
832 rad_virtual_server(fake);
834 if (fake->reply->code != PW_CODE_ACCESS_ACCEPT) {
835 RDEBUG2("SoH was rejected");
837 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
838 eappeap_failure(handler, tls_session);
839 return RLM_MODULE_HANDLED;
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);
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;
855 t->status = PEAP_STATUS_PHASE2_INIT;
860 * If we authenticated the user, then it's OK.
862 case PEAP_STATUS_SENT_TLV_SUCCESS:
863 if (eappeap_check_tlv(request, data + header, data_len - header)) {
865 return RLM_MODULE_OK;
869 * Otherwise, the client rejected the session
870 * resumption. If the session is being re-used,
871 * we need to do a full authentication.
873 * We do this by sending an EAP-Identity request
874 * inside of the PEAP tunnel.
876 if (t->session_resumption_state == PEAP_RESUMPTION_YES) {
877 RDEBUG2("Client rejected session resumption. Re-starting full authentication");
880 * Mark session resumption status.
882 t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
883 t->session_resumption_state = PEAP_RESUMPTION_NO;
885 eappeap_identity(handler, tls_session);
886 return RLM_MODULE_HANDLED;
889 REDEBUG("We sent a success, but the client did not agree");
890 return RLM_MODULE_REJECT;
893 * Supplicant ACKs our failure.
895 case PEAP_STATUS_SENT_TLV_FAILURE:
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");
906 return RLM_MODULE_REJECT;
908 case PEAP_STATUS_PHASE2_INIT:
909 RDEBUG("In state machine in phase2 init?");
911 case PEAP_STATUS_PHASE2:
915 REDEBUG("Unhandled state in peap");
916 return RLM_MODULE_REJECT;
919 fake = request_alloc_fake(request);
921 rad_assert(!fake->packet->vps);
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
930 case PEAP_STATUS_PHASE2_INIT: {
931 size_t len = t->username->vp_length + EAP_HEADER_LEN + 1;
934 t->status = PEAP_STATUS_PHASE2;
936 vp = fr_pair_afrom_num(fake->packet, PW_EAP_MESSAGE, 0);
938 vp->vp_octets = q = talloc_array(vp, uint8_t, vp->vp_length);
940 q[0] = PW_EAP_RESPONSE;
941 q[1] = eap_ds->response->id;
942 q[2] = (len >> 8) & 0xff;
944 q[4] = PW_EAP_IDENTITY;
946 memcpy(q + EAP_HEADER_LEN + 1,
947 t->username->vp_strvalue, t->username->vp_length);
949 fr_pair_add(&fake->packet->vps, vp);
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;
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) {
963 RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
964 return RLM_MODULE_REJECT;
969 REDEBUG("Invalid state change in PEAP");
970 return RLM_MODULE_REJECT;
973 RDEBUG2("Got tunneled request");
974 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
977 * Update other items in the REQUEST data structure.
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.
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);
989 fr_pair_value_bstrncpy(t->username, data + header + 1, data_len - header - 1);
991 RDEBUG2("Got tunneled identity of %s", t->username->vp_strvalue);
994 * If there's a default EAP type,
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;
1003 } /* else there WAS a t->username */
1005 setup_fake_request(request, fake, t);
1007 if ((vp = fr_pair_find_by_num(request->config, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1008 fake->server = vp->vp_strvalue;
1010 } else if (t->virtual_server) {
1011 fake->server = t->virtual_server;
1013 } /* else fake->server == request->server */
1016 RDEBUG2("Sending tunneled request to %s", fake->server);
1018 RDEBUG2("Sending tunnelled request");
1020 rdebug_pair_list(L_DBG_LVL_2, request, fake->packet->vps, NULL);
1023 * Call authentication recursively, which will
1024 * do PAP, CHAP, MS-CHAP, etc.
1026 rad_virtual_server(fake);
1029 * Note that we don't do *anything* with the reply
1032 RDEBUG2("Got tunneled reply code %d", fake->reply->code);
1033 rdebug_pair_list(L_DBG_LVL_2, request, fake->reply->vps, NULL);
1036 * Decide what to do with the reply.
1038 switch (fake->reply->code) {
1039 case 0: /* No reply code, must be proxied... */
1041 vp = fr_pair_find_by_num(fake->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
1044 eap_tunnel_data_t *tunnel;
1047 * The tunneled request was NOT handled,
1048 * it has to be proxied. This means that
1049 * the "authenticate" stage was never
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.
1058 * Once the tunneled EAP session is ALMOST
1059 * done, THEN we proxy it...
1061 if (!t->proxy_tunneled_request_as_eap) {
1062 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1065 * Hmm... should we check for
1066 * Auth-Type & EAP-Message here?
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;
1077 * Run the EAP authentication.
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) {
1083 * Authentication succeeded! Rah!
1085 fake->reply->code = PW_CODE_ACCESS_ACCEPT;
1089 if (rcode != RLM_MODULE_HANDLED) {
1090 RDEBUG("Can't handle the return code %d", rcode);
1091 rcode = RLM_MODULE_REJECT;
1096 * The module decided it wasn't
1097 * done. Handle it like normal.
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);
1106 * The module has decoded the
1107 * EAP-Message into another set
1110 fr_pair_delete_by_num(&fake->packet->vps,
1111 PW_EAP_MESSAGE, 0, TAG_ANY);
1114 RDEBUG2("Tunnelled authentication will be proxied to %s", vp->vp_strvalue);
1117 * Tell the original request that it's going
1120 fr_pair_list_mcopy_by_num(request, &request->config,
1122 PW_PROXY_TO_REALM, 0, TAG_ANY);
1125 * Seed the proxy packet with the
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);
1141 * Set up the callbacks for the tunnel
1143 tunnel = talloc_zero(request, eap_tunnel_data_t);
1144 tunnel->tls_session = tls_session;
1145 tunnel->callback = eappeap_postproxy;
1148 * Associate the callback with the request.
1150 rcode = request_data_add(request,
1152 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1154 rad_assert(rcode == 0);
1157 * We're not proxying it as EAP, so we've got
1158 * to do the callback later.
1160 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1161 RDEBUG2("Remembering to do EAP-MS-CHAP-V2 post-proxy");
1164 * rlm_eap.c has taken care of associating
1165 * the handler with the fake request.
1167 * So we associate the fake request with
1170 rcode = request_data_add(request, request->proxy,
1171 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1173 rad_assert(rcode == 0);
1176 * Do NOT free the fake request!
1178 return RLM_MODULE_UPDATED;
1182 * Didn't authenticate the packet, but
1183 * we're proxying it.
1185 rcode = RLM_MODULE_UPDATED;
1188 #endif /* WITH_PROXY */
1190 REDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1191 rcode = RLM_MODULE_REJECT;
1199 rcode = process_reply(handler, tls_session, request,
1212 static int CC_HINT(nonnull) setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t) {
1217 * Tell the request that it's a fake one.
1219 fr_pair_make(fake->packet, &fake->packet->vps, "Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
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);
1227 RDEBUG2("No tunnel username (SSL resumption?)");
1232 * Add the State attribute, too, if it exists.
1235 vp = fr_pair_list_copy(fake->packet, t->state);
1236 if (vp) fr_pair_add(&fake->packet->vps, vp);
1240 * If this is set, we copy SOME of the request attributes
1241 * from outside of the tunnel to inside of the tunnel.
1243 * We copy ONLY those attributes which do NOT already
1244 * exist in the tunneled request.
1246 * This code is copied from ../rlm_eap_ttls/ttls.c
1248 if (t->copy_request_to_tunnel) {
1252 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
1254 vp = fr_cursor_next(&cursor)) {
1256 * The attribute is a server-side thingy,
1259 if ((vp->da->attr > 255) && (((vp->da->attr >> 16) & 0xffff) == 0)) {
1264 * The outside attribute is already in the
1265 * tunnel, don't copy it.
1267 * This works for BOTH attributes which
1268 * are originally in the tunneled request,
1269 * AND attributes which are copied there
1272 if (fr_pair_find_by_da(fake->packet->vps, vp->da, TAG_ANY)) continue;
1275 * Some attributes are handled specially.
1277 if (!vp->da->vendor) switch (vp->da->attr) {
1279 * NEVER copy Message-Authenticator,
1280 * EAP-Message, or State. They're
1281 * only for outside of the tunnel.
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:
1294 * By default, copy it over.
1301 * Don't copy from the head, we've already
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);