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
24 #include <freeradius-devel/ident.h>
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 const uint8_t *data, unsigned int data_len) {
163 uint8_t eap_method_base;
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");
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 const uint8_t *data, unsigned int data_len)
212 const eap_packet_raw_t *eap_packet = (const eap_packet_raw_t *) data;
213 eap_type_t eap_method;
216 * No data, OR only 1 byte of EAP type.
218 if (!data || (data_len == 0) ||
219 ((data_len <= 1) && (data[0] != PW_EAP_IDENTITY))) {
224 switch (eap_method) {
225 case PW_EAP_IDENTITY:
227 RDEBUG2("Identity - ");
230 RDEBUG2("Identity - %*s",
231 data_len - 1, data + 1);
236 * If the first byte of the packet is
237 * EAP-Response, and the EAP data is a TLV,
238 * then it looks OK...
240 case PW_EAP_RESPONSE:
241 if (eap_packet->data[0] == PW_EAP_TLV) {
242 RDEBUG2("Received EAP-TLV response.");
245 RDEBUG2("Got something weird.");
250 * We normally do Microsoft MS-CHAPv2 (26), versus
251 * Cisco MS-CHAPv2 (29).
253 case PW_EAP_MSCHAPV2:
255 RDEBUG2("EAP type %s (%d)", eap_type2name(eap_method),
265 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
267 static VALUE_PAIR *eap2vp(REQUEST *request, RADIUS_PACKET *packet,
269 const uint8_t *data, size_t data_len)
272 VALUE_PAIR *vp = NULL, *head, **tail;
274 if (data_len > 65535) return NULL; /* paranoia */
276 vp = paircreate(packet, PW_EAP_MESSAGE, 0);
278 RDEBUG2("Failure in creating VP");
283 if (total > 249) total = 249;
286 * Hand-build an EAP packet from the crap in PEAP version 0.
288 vp->vp_octets[0] = PW_EAP_RESPONSE;
289 vp->vp_octets[1] = eap_ds->response->id;
290 vp->vp_octets[2] = (data_len + EAP_HEADER_LEN) >> 8;
291 vp->vp_octets[3] = (data_len + EAP_HEADER_LEN) & 0xff;
293 memcpy(vp->vp_octets + EAP_HEADER_LEN, data, total);
294 vp->length = EAP_HEADER_LEN + total;
298 while (total < data_len) {
302 vp = paircreate(packet, PW_EAP_MESSAGE, 0);
304 RDEBUG2("Failure in creating VP");
308 vp_len = (data_len - total);
309 if (vp_len > 253) vp_len = 253;
311 memcpy(vp->vp_octets, data + total, vp_len);
324 * Convert a list of VALUE_PAIR's to an EAP packet, through the
325 * simple expedient of dumping the EAP message
327 static int vp2eap(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *vp)
329 rad_assert(vp != NULL);
332 * Skip the id, code, and length. Just write the EAP
333 * type & data to the client.
336 if ((debug_flag > 2) && fr_log_fp) {
342 for (this = vp; this != NULL; this = this->next) {
345 if (this == vp) start = EAP_HEADER_LEN;
347 for (i = start; i < vp->length; i++) {
348 if ((total & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data out %04x: ", (int) total);
350 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
352 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
356 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
361 * Send the EAP data, WITHOUT the header.
363 (tls_session->record_plus)(&tls_session->clean_in,
364 vp->vp_octets + EAP_HEADER_LEN,
365 vp->length - EAP_HEADER_LEN);
368 * Send the rest of the EAP data.
370 for (vp = vp->next; vp != NULL; vp = vp->next) {
371 (tls_session->record_plus)(&tls_session->clean_in,
372 vp->vp_octets, vp->length);
375 tls_handshake_send(request, tls_session);
382 * See if there's a TLV in the response.
384 static int eappeap_check_tlv(REQUEST *request, const uint8_t *data,
387 const eap_packet_raw_t *eap_packet = (const eap_packet_raw_t *) data;
389 if (data_len < 11) return 0;
392 * Look for success or failure.
394 if ((eap_packet->code == PW_EAP_RESPONSE) &&
395 (eap_packet->data[0] == PW_EAP_TLV)) {
396 if (data[10] == EAP_TLV_SUCCESS) {
400 if (data[10] == EAP_TLV_FAILURE) {
401 RDEBUG2("Client rejected our response. The password is probably incorrect.");
406 RDEBUG("Unknown TLV %02x", data[10]);
413 * Use a reply packet to determine what to do.
415 static int process_reply(eap_handler_t *handler, tls_session_t *tls_session,
416 REQUEST *request, RADIUS_PACKET *reply)
418 int rcode = RLM_MODULE_REJECT;
420 peap_tunnel_t *t = tls_session->opaque;
422 if ((debug_flag > 0) && fr_log_fp) {
423 RDEBUG("Got tunneled reply RADIUS code %d",
426 debug_pair_list(reply->vps);
429 switch (reply->code) {
430 case PW_AUTHENTICATION_ACK:
431 RDEBUG2("Tunneled authentication was successful.");
432 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
433 eappeap_success(handler, tls_session);
434 rcode = RLM_MODULE_HANDLED;
437 * If we've been told to use the attributes from
438 * the reply, then do so.
440 * WARNING: This may leak information about the
443 if (t->use_tunneled_reply) {
444 RDEBUG2("Saving tunneled attributes for later");
447 * Clean up the tunneled reply.
449 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
450 pairdelete(&reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
451 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
454 * Delete MPPE keys & encryption policy. We don't
457 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
458 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
459 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
460 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
462 t->accept_vps = reply->vps;
467 case PW_AUTHENTICATION_REJECT:
468 RDEBUG2("Tunneled authentication was rejected.");
469 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
470 eappeap_failure(handler, tls_session);
471 rcode = RLM_MODULE_HANDLED;
474 case PW_ACCESS_CHALLENGE:
475 RDEBUG2("Got tunneled Access-Challenge");
478 * Keep the State attribute, if necessary.
480 * Get rid of the old State, too.
483 pairmove2(t, &t->state, &(reply->vps), PW_STATE, 0, TAG_ANY);
486 * PEAP takes only EAP-Message attributes inside
487 * of the tunnel. Any Reply-Message in the
488 * Access-Challenge is ignored.
491 pairmove2(t, &vp, &(reply->vps), PW_EAP_MESSAGE, 0, TAG_ANY);
494 * Handle EAP-MSCHAP-V2, where Access-Accept's
495 * from the home server may contain MS-CHAP-Success,
496 * which the module turns into challenges, so that
497 * the client may respond to the challenge with
500 if (t->home_access_accept && t->use_tunneled_reply) {
501 RDEBUG2("Saving tunneled attributes for later");
504 * Clean up the tunneled reply.
506 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
507 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
509 t->accept_vps = reply->vps;
514 * Handle the ACK, by tunneling any necessary reply
515 * VP's back to the client.
518 vp2eap(request, tls_session, vp);
522 rcode = RLM_MODULE_HANDLED;
526 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
527 rcode = RLM_MODULE_REJECT;
536 * Do post-proxy processing,
538 static int eappeap_postproxy(eap_handler_t *handler, void *data)
541 tls_session_t *tls_session = (tls_session_t *) data;
542 REQUEST *fake, *request = handler->request;
544 rad_assert(request != NULL);
545 RDEBUG2("Passing reply from proxy back into the tunnel.");
548 * If there was a fake request associated with the proxied
549 * request, do more processing of it.
551 fake = (REQUEST *) request_data_get(handler->request,
552 handler->request->proxy,
553 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
556 * Do the callback, if it exists, and if it was a success.
558 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
559 peap_tunnel_t *t = tls_session->opaque;
561 t->home_access_accept = TRUE;
566 rad_assert(fake->packet == NULL);
567 fake->packet = request->proxy;
568 fake->packet->src_ipaddr = request->packet->src_ipaddr;
569 request->proxy = NULL;
571 rad_assert(fake->reply == NULL);
572 fake->reply = request->proxy_reply;
573 request->proxy_reply = NULL;
575 if ((debug_flag > 0) && fr_log_fp) {
576 fprintf(fr_log_fp, "server %s {\n", fake->server);
580 * Perform a post-auth stage, which will get the EAP
583 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
584 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
585 process_post_proxy(0, fake);
588 * FIXME: If rcode returns fail, do something
591 rcode = rad_postauth(fake);
593 if ((debug_flag > 0) && fr_log_fp) {
594 fprintf(fr_log_fp, "} # server %s\n", fake->server);
596 RDEBUG("Final reply from tunneled session code %d",
599 debug_pair_list(fake->reply->vps);
605 request->proxy = fake->packet;
607 request->proxy_reply = fake->reply;
611 * And we're done with this request.
615 case RLM_MODULE_FAIL:
617 eaptls_fail(handler, 0);
621 default: /* Don't Do Anything */
622 RDEBUG2("Got reply %d", request->proxy_reply->code);
626 request_free(&fake); /* robust if fake == NULL */
629 * If there was no EAP-Message in the reply packet, then
630 * we know that we're supposed to re-run the "authenticate"
631 * stage, in order to get the right kind of handling...
635 * Process the reply from the home server.
638 rcode = process_reply(handler, tls_session, handler->request,
639 handler->request->proxy_reply);
642 * The proxy code uses the reply from the home server as
643 * the basis for the reply to the NAS. We don't want that,
644 * so we toss it, after we've had our way with it.
646 pairfree(&handler->request->proxy_reply->vps);
649 case RLM_MODULE_REJECT:
650 RDEBUG2("Reply was rejected");
651 eaptls_fail(handler, 0);
654 case RLM_MODULE_HANDLED:
655 RDEBUG2("Reply was handled");
656 eaptls_request(handler->eap_ds, tls_session);
660 RDEBUG2("Reply was OK");
663 * Success: Automatically return MPPE keys.
665 return eaptls_success(handler, 0);
668 RDEBUG2("Reply was unknown.");
672 eaptls_fail(handler, 0);
679 static void my_request_free(void *data)
681 REQUEST *request = (REQUEST *)data;
683 request_free(&request);
688 static const char *peap_state(peap_tunnel_t *t)
691 case PEAP_STATUS_TUNNEL_ESTABLISHED:
692 return "TUNNEL ESTABLISHED";
693 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
694 return "WAITING FOR SOH RESPONSE";
695 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
696 return "WAITING FOR INNER IDENTITY";
697 case PEAP_STATUS_SENT_TLV_SUCCESS:
698 return "send tlv success";
699 case PEAP_STATUS_SENT_TLV_FAILURE:
700 return "send tlv failure";
701 case PEAP_STATUS_PHASE2_INIT:
702 return "phase2_init";
703 case PEAP_STATUS_PHASE2:
711 static void print_tunneled_data(const uint8_t *data, size_t data_len)
715 if ((debug_flag > 2) && fr_log_fp) {
716 for (i = 0; i < data_len; i++) {
717 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data in %02x: ", (int) i);
719 fprintf(fr_log_fp, "%02x ", data[i]);
721 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
723 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
729 * Process the pseudo-EAP contents of the tunneled data.
731 int eappeap_process(eap_handler_t *handler, tls_session_t *tls_session)
733 peap_tunnel_t *t = tls_session->opaque;
736 int rcode = RLM_MODULE_REJECT;
738 unsigned int data_len;
740 REQUEST *request = handler->request;
741 EAP_DS *eap_ds = handler->eap_ds;
743 rad_assert(request != NULL);
746 * Just look at the buffer directly, without doing
747 * record_minus. This lets us avoid another data copy.
749 data_len = tls_session->clean_out.used;
750 tls_session->clean_out.used = 0;
751 data = tls_session->clean_out.data;
753 RDEBUG2("Peap state %s", peap_state(t));
755 if ((t->status != PEAP_STATUS_TUNNEL_ESTABLISHED) &&
756 !eapmessage_verify(request, data, data_len)) {
757 RDEBUG2("FAILED processing PEAP: Tunneled data is invalid.");
758 if (debug_flag > 2) print_tunneled_data(data, data_len);
759 return RLM_MODULE_REJECT;
763 case PEAP_STATUS_TUNNEL_ESTABLISHED:
764 /* FIXME: should be no data in the buffer here, check & assert? */
766 if (SSL_session_reused(tls_session->ssl)) {
767 RDEBUG2("Skipping Phase2 because of session resumption");
768 t->session_resumption_state = PEAP_RESUMPTION_YES;
770 t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
771 RDEBUG2("Requesting SoH from client");
772 eappeap_soh(handler, tls_session);
773 return RLM_MODULE_HANDLED;
775 /* we're good, send success TLV */
776 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
777 eappeap_success(handler, tls_session);
780 /* send an identity request */
781 t->session_resumption_state = PEAP_RESUMPTION_NO;
782 t->status = PEAP_STATUS_INNER_IDENTITY_REQ_SENT;
783 eappeap_identity(handler, tls_session);
785 return RLM_MODULE_HANDLED;
787 case PEAP_STATUS_INNER_IDENTITY_REQ_SENT:
788 /* we're expecting an identity response */
789 if (data[0] != PW_EAP_IDENTITY) {
790 RDEBUG("Expected EAP-Identity, got something else.");
791 return RLM_MODULE_REJECT;
794 if (data_len >= sizeof(t->username->vp_strvalue)) {
795 RDEBUG("EAP-Identity is too long");
796 return RLM_MODULE_REJECT;
802 t->username = pairmake(t, NULL, "User-Name", "", T_OP_EQ);
803 rad_assert(t->username != NULL);
805 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
806 t->username->length = data_len - 1;
807 t->username->vp_strvalue[t->username->length] = 0;
808 RDEBUG("Got inner identity '%s'", t->username->vp_strvalue);
810 t->status = PEAP_STATUS_WAIT_FOR_SOH_RESPONSE;
811 RDEBUG2("Requesting SoH from client");
812 eappeap_soh(handler, tls_session);
813 return RLM_MODULE_HANDLED;
815 t->status = PEAP_STATUS_PHASE2_INIT;
818 case PEAP_STATUS_WAIT_FOR_SOH_RESPONSE:
819 fake = request_alloc_fake(request);
820 rad_assert(fake->packet->vps == NULL);
821 eapsoh_verify(request, fake->packet, data, data_len);
822 setup_fake_request(request, fake, t);
824 if (t->soh_virtual_server) {
825 fake->server = t->soh_virtual_server;
827 RDEBUG("Sending SoH request to server %s", fake->server ? fake->server : "NULL");
828 debug_pair_list(fake->packet->vps);
829 RDEBUG("server %s {", fake->server);
830 rad_virtual_server(fake);
831 RDEBUG("} # server %s", fake->server);
832 RDEBUG("Got SoH reply");
833 debug_pair_list(fake->reply->vps);
835 if (fake->reply->code != PW_AUTHENTICATION_ACK) {
836 RDEBUG2("SoH was rejected");
838 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
839 eappeap_failure(handler, tls_session);
840 return RLM_MODULE_HANDLED;
843 /* save the SoH VPs */
844 t->soh_reply_vps = fake->reply->vps;
845 fake->reply->vps = NULL;
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, data_len)) {
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 RDEBUG2("We sent a success, but received something weird in return.");
890 return RLM_MODULE_REJECT;
893 * Damned if I know why the clients continue sending EAP
894 * packets after we told them to f*ck off.
896 case PEAP_STATUS_SENT_TLV_FAILURE:
897 RDEBUG(" The users session was previously rejected: returning reject (again.)");
898 RDEBUG(" *** This means you need to read the PREVIOUS messages in the debug output");
899 RDEBUG(" *** to find out the reason why the user was rejected.");
900 RDEBUG(" *** Look for \"reject\" or \"fail\". Those earlier messages will tell you.");
901 RDEBUG(" *** what went wrong, and how to fix the problem.");
902 return RLM_MODULE_REJECT;
904 case PEAP_STATUS_PHASE2_INIT:
905 RDEBUG("In state machine in phase2 init?");
907 case PEAP_STATUS_PHASE2:
911 RDEBUG2("Unhandled state in peap");
912 return RLM_MODULE_REJECT;
915 fake = request_alloc_fake(request);
917 rad_assert(fake->packet->vps == NULL);
921 * If we're in PHASE2_INIT, the phase2 method hasn't been
922 * sent an Identity packet yet; do so from the stored
923 * username and this will kick off the phase2 eap method
926 case PEAP_STATUS_PHASE2_INIT: {
927 size_t len = t->username->length + EAP_HEADER_LEN + 1;
929 t->status = PEAP_STATUS_PHASE2;
931 vp = paircreate(fake->packet, PW_EAP_MESSAGE, 0);
933 vp->vp_octets[0] = PW_EAP_RESPONSE;
934 vp->vp_octets[1] = eap_ds->response->id;
935 vp->vp_octets[2] = (len >> 8) & 0xff;
936 vp->vp_octets[3] = len & 0xff;
937 vp->vp_octets[4] = PW_EAP_IDENTITY;
939 if (len > sizeof(vp->vp_octets)) len = sizeof(vp->vp_octets);
940 memcpy(vp->vp_octets + EAP_HEADER_LEN + 1,
941 t->username->vp_strvalue, len - EAP_HEADER_LEN - 1);
944 pairadd(&fake->packet->vps, vp);
946 if (t->default_method != 0) {
947 RDEBUG2("Setting default EAP type for tunneled EAP session.");
948 vp = pairmake(fake, &fake->config_items, "EAP-Type", "0", T_OP_EQ);
949 vp->vp_integer = t->default_method;
953 case PEAP_STATUS_PHASE2:
954 fake->packet->vps = eap2vp(request, fake->packet,
955 eap_ds, data, data_len);
956 if (!fake->packet->vps) {
958 RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
959 return PW_AUTHENTICATION_REJECT;
964 RDEBUG("Invalid state change in PEAP.");
965 return PW_AUTHENTICATION_REJECT;
968 if ((debug_flag > 0) && fr_log_fp) {
969 RDEBUG("Got tunneled request");
971 debug_pair_list(fake->packet->vps);
973 fprintf(fr_log_fp, "server %s {\n",
974 (fake->server == NULL) ? "" : fake->server);
978 * Update other items in the REQUEST data structure.
982 * There's no User-Name in the tunneled session,
983 * so we add one here, by pulling it out of the
984 * EAP-Identity packet.
986 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
987 t->username = pairmake(t, NULL, "User-Name", "", T_OP_EQ);
988 rad_assert(t->username != NULL);
990 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
991 t->username->length = data_len - 1;
992 t->username->vp_strvalue[t->username->length] = 0;
993 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
996 * If there's a default EAP type,
999 if (t->default_method != 0) {
1000 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
1001 vp = pairmake(fake, &fake->config_items, "EAP-Type", "0", T_OP_EQ);
1002 vp->vp_integer = t->default_method;
1005 } /* else there WAS a t->username */
1007 setup_fake_request(request, fake, t);
1009 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1010 fake->server = vp->vp_strvalue;
1012 } else if (t->virtual_server) {
1013 fake->server = t->virtual_server;
1015 } /* else fake->server == request->server */
1017 if ((debug_flag > 0) && fr_log_fp) {
1018 fprintf(fr_log_fp, "Sending tunneled request\n");
1020 debug_pair_list(fake->packet->vps);
1022 fprintf(fr_log_fp, "server %s {\n",
1023 (fake->server == NULL) ? "" : fake->server);
1027 * Call authentication recursively, which will
1028 * do PAP, CHAP, MS-CHAP, etc.
1030 rad_virtual_server(fake);
1033 * Note that we don't do *anything* with the reply
1036 if ((debug_flag > 0) && fr_log_fp) {
1037 fprintf(fr_log_fp, "} # server %s\n",
1038 (fake->server == NULL) ? "" : fake->server);
1040 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1042 debug_pair_list(fake->reply->vps);
1046 * Decide what to do with the reply.
1048 switch (fake->reply->code) {
1049 case 0: /* No reply code, must be proxied... */
1051 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1054 eap_tunnel_data_t *tunnel;
1057 * The tunneled request was NOT handled,
1058 * it has to be proxied. This means that
1059 * the "authenticate" stage was never
1062 * If we are told to NOT proxy the
1063 * tunneled request as EAP, then this
1064 * means that we've got to decode it,
1065 * which means that we MUST run the
1066 * "authenticate" portion by hand, here.
1068 * Once the tunneled EAP session is ALMOST
1069 * done, THEN we proxy it...
1071 if (!t->proxy_tunneled_request_as_eap) {
1072 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
1075 * Hmm... should we check for
1076 * Auth-Type & EAP-Message here?
1081 * Run the EAP authentication.
1083 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
1084 rcode = process_authenticate(PW_AUTHTYPE_EAP, fake);
1085 if (rcode == RLM_MODULE_OK) {
1087 * Authentication succeeded! Rah!
1089 fake->reply->code = PW_AUTHENTICATION_ACK;
1093 if (rcode != RLM_MODULE_HANDLED) {
1094 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
1095 rcode = RLM_MODULE_REJECT;
1100 * The module decided it wasn't
1101 * done. Handle it like normal.
1103 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
1104 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
1109 * The module has decoded the
1110 * EAP-Message into another set
1113 pairdelete(&fake->packet->vps,
1114 PW_EAP_MESSAGE, 0, TAG_ANY);
1117 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1120 * Tell the original request that it's going
1123 pairmove2(request, &(request->config_items),
1124 &(fake->config_items),
1125 PW_PROXY_TO_REALM, 0, TAG_ANY);
1128 * Seed the proxy packet with the
1131 rad_assert(request->proxy == NULL);
1132 request->proxy = fake->packet;
1133 memset(&request->proxy->src_ipaddr, 0,
1134 sizeof(request->proxy->src_ipaddr));
1135 memset(&request->proxy->dst_ipaddr, 0,
1136 sizeof(request->proxy->dst_ipaddr));
1137 request->proxy->src_port = 0;
1138 request->proxy->dst_port = 0;
1139 fake->packet = NULL;
1140 rad_free(&fake->reply);
1144 * Set up the callbacks for the tunnel
1146 tunnel = talloc_zero(request, eap_tunnel_data_t);
1147 tunnel->tls_session = tls_session;
1148 tunnel->callback = eappeap_postproxy;
1151 * Associate the callback with the request.
1153 rcode = request_data_add(request,
1155 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1157 rad_assert(rcode == 0);
1160 * We're not proxying it as EAP, so we've got
1161 * to do the callback later.
1163 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
1164 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
1167 * rlm_eap.c has taken care of associating
1168 * the handler with the fake request.
1170 * So we associate the fake request with
1173 rcode = request_data_add(request,
1175 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1176 fake, my_request_free);
1177 rad_assert(rcode == 0);
1180 * Do NOT free the fake request!
1182 return RLM_MODULE_UPDATED;
1186 * Didn't authenticate the packet, but
1187 * we're proxying it.
1189 rcode = RLM_MODULE_UPDATED;
1192 #endif /* WITH_PROXY */
1194 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
1195 rcode = RLM_MODULE_REJECT;
1201 rcode = process_reply(handler, tls_session, request,
1207 request_free(&fake);
1212 static int setup_fake_request(REQUEST *request, REQUEST *fake, peap_tunnel_t *t) {
1216 * Tell the request that it's a fake one.
1218 pairmake(fake->packet, &fake->packet->vps,
1219 "Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1222 vp = paircopy(fake->packet, t->username);
1223 pairadd(&fake->packet->vps, vp);
1224 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
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 = paircopy(fake->packet, t->state);
1236 if (vp) pairadd(&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) {
1251 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1253 * The attribute is a server-side thingy,
1256 if ((vp->da->attr > 255) &&
1257 (((vp->da->attr >> 16) & 0xffff) == 0)) {
1262 * The outside attribute is already in the
1263 * tunnel, don't copy it.
1265 * This works for BOTH attributes which
1266 * are originally in the tunneled request,
1267 * AND attributes which are copied there
1270 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1275 * Some attributes are handled specially.
1277 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:
1295 * By default, copy it over.
1302 * Don't copy from the head, we've already
1305 copy = paircopy2(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1306 pairadd(&fake->packet->vps, copy);