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>
30 * Send protected EAP-Failure
32 * Result-TLV = Failure
34 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
36 uint8_t tlv_packet[11];
37 REQUEST *request = handler->request;
41 tlv_packet[0] = PW_EAP_REQUEST;
42 tlv_packet[1] = handler->eap_ds->response->id +1;
44 tlv_packet[3] = 11; /* length of this packet */
45 tlv_packet[4] = PW_EAP_TLV;
47 tlv_packet[6] = EAP_TLV_ACK_RESULT;
49 tlv_packet[8] = 2; /* length of the data portion */
51 tlv_packet[10] = EAP_TLV_FAILURE;
53 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
56 * FIXME: Check the return code.
58 tls_handshake_send(tls_session);
65 * Send protected EAP-Success
67 * Result-TLV = Success
69 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
71 uint8_t tlv_packet[11];
72 REQUEST *request = handler->request;
76 tlv_packet[0] = PW_EAP_REQUEST;
77 tlv_packet[1] = handler->eap_ds->response->id +1;
79 tlv_packet[3] = 11; /* length of this packet */
80 tlv_packet[4] = PW_EAP_TLV;
81 tlv_packet[5] = 0x80; /* mandatory AVP */
82 tlv_packet[6] = EAP_TLV_ACK_RESULT;
84 tlv_packet[8] = 2; /* length of the data portion */
86 tlv_packet[10] = EAP_TLV_SUCCESS;
88 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
91 * FIXME: Check the return code.
93 tls_handshake_send(tls_session);
100 * Verify the tunneled EAP message.
102 static int eapmessage_verify(REQUEST *request,
103 const uint8_t *data, unsigned int data_len)
105 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
109 if (!data || (data_len <= 1)) {
115 case PW_EAP_IDENTITY:
116 RDEBUG2("Identity - %*s",
117 data_len - 1, data + 1);
122 * If the first byte of the packet is
123 * EAP-Response, and the EAP data is a TLV,
124 * then it looks OK...
126 case PW_EAP_RESPONSE:
127 if (eap_packet->data[0] == PW_EAP_TLV) {
128 RDEBUG2("Received EAP-TLV response.");
131 RDEBUG2("Got something weird.");
136 * We normally do Microsoft MS-CHAPv2 (26), versus
137 * Cisco MS-CHAPv2 (29).
139 case PW_EAP_MSCHAPV2:
141 RDEBUG2("EAP type %s",
142 eaptype_type2name(eap_type,
143 buffer, sizeof(buffer)));
152 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
154 static VALUE_PAIR *eap2vp(REQUEST *request, EAP_DS *eap_ds,
155 const uint8_t *data, size_t data_len)
158 VALUE_PAIR *vp = NULL, *head, **tail;
160 if (data_len > 65535) return NULL; /* paranoia */
162 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
164 RDEBUG2("Failure in creating VP");
169 if (total > 249) total = 249;
172 * Hand-build an EAP packet from the crap in PEAP version 0.
174 vp->vp_octets[0] = PW_EAP_RESPONSE;
175 vp->vp_octets[1] = eap_ds->response->id;
176 vp->vp_octets[2] = (data_len + EAP_HEADER_LEN) >> 8;
177 vp->vp_octets[3] = (data_len + EAP_HEADER_LEN) & 0xff;
179 memcpy(vp->vp_octets + EAP_HEADER_LEN, data, total);
180 vp->length = EAP_HEADER_LEN + total;
184 while (total < data_len) {
188 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
190 RDEBUG2("Failure in creating VP");
194 vp_len = (data_len - total);
195 if (vp_len > 253) vp_len = 253;
197 memcpy(vp->vp_octets, data + total, vp_len);
210 * Convert a list of VALUE_PAIR's to an EAP packet, through the
211 * simple expedient of dumping the EAP message
213 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
216 * Skip the id, code, and length. Just write the EAP
217 * type & data to the client.
220 if ((debug_flag > 2) && fr_log_fp) {
226 for (this = vp; this != NULL; this = this->next) {
229 if (this == vp) start = EAP_HEADER_LEN;
231 for (i = start; i < vp->length; i++) {
232 if ((total & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data out %04x: ", total);
234 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
236 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
240 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
245 * Send the EAP data, WITHOUT the header.
247 (tls_session->record_plus)(&tls_session->clean_in,
248 vp->vp_octets + EAP_HEADER_LEN,
249 vp->length - EAP_HEADER_LEN);
252 * Send the rest of the EAP data.
254 for (vp = vp->next; vp != NULL; vp = vp->next) {
255 (tls_session->record_plus)(&tls_session->clean_in,
256 vp->vp_octets, vp->length);
259 tls_handshake_send(tls_session);
266 * See if there's a TLV in the response.
268 static int eappeap_check_tlv(REQUEST *request, const uint8_t *data)
270 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
273 * Look for success or failure.
275 if ((eap_packet->code == PW_EAP_RESPONSE) &&
276 (eap_packet->data[0] == PW_EAP_TLV)) {
277 if (data[10] == EAP_TLV_SUCCESS) {
281 if (data[10] == EAP_TLV_FAILURE) {
282 RDEBUG2("Client rejected our response. The password is probably incorrect.");
292 * Use a reply packet to determine what to do.
294 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
295 REQUEST *request, RADIUS_PACKET *reply)
297 int rcode = RLM_MODULE_REJECT;
299 peap_tunnel_t *t = tls_session->opaque;
301 if ((debug_flag > 0) && fr_log_fp) {
302 RDEBUG("Got tunneled reply RADIUS code %d",
305 debug_pair_list(reply->vps);
308 switch (reply->code) {
309 case PW_AUTHENTICATION_ACK:
310 RDEBUG2("Tunneled authentication was successful.");
311 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
312 eappeap_success(handler, tls_session);
313 rcode = RLM_MODULE_HANDLED;
316 * If we've been told to use the attributes from
317 * the reply, then do so.
319 * WARNING: This may leak information about the
322 if (t->use_tunneled_reply) {
323 RDEBUG2("Saving tunneled attributes for later");
326 * Clean up the tunneled reply.
328 pairdelete(&reply->vps, PW_PROXY_STATE);
329 pairdelete(&reply->vps, PW_EAP_MESSAGE);
330 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
332 t->accept_vps = reply->vps;
337 case PW_AUTHENTICATION_REJECT:
338 RDEBUG2("Tunneled authentication was rejected.");
339 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
340 eappeap_failure(handler, tls_session);
341 rcode = RLM_MODULE_HANDLED;
344 case PW_ACCESS_CHALLENGE:
345 RDEBUG2("Got tunneled Access-Challenge");
348 * Keep the State attribute, if necessary.
350 * Get rid of the old State, too.
353 pairmove2(&t->state, &(reply->vps), PW_STATE);
356 * PEAP takes only EAP-Message attributes inside
357 * of the tunnel. Any Reply-Message in the
358 * Access-Challenge is ignored.
361 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
364 * Handle EAP-MSCHAP-V2, where Access-Accept's
365 * from the home server may contain MS-CHAP-Success,
366 * which the module turns into challenges, so that
367 * the client may respond to the challenge with
370 if (t->home_access_accept && t->use_tunneled_reply) {
371 RDEBUG2("Saving tunneled attributes for later");
374 * Clean up the tunneled reply.
376 pairdelete(&reply->vps, PW_PROXY_STATE);
377 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
379 t->accept_vps = reply->vps;
384 * Handle the ACK, by tunneling any necessary reply
385 * VP's back to the client.
388 vp2eap(tls_session, vp);
392 rcode = RLM_MODULE_HANDLED;
396 RDEBUG2("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
397 rcode = RLM_MODULE_REJECT;
406 * Do post-proxy processing,
408 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
411 tls_session_t *tls_session = (tls_session_t *) data;
412 REQUEST *fake, *request = handler->request;
414 RDEBUG2("Passing reply from proxy back into the tunnel.");
417 * If there was a fake request associated with the proxied
418 * request, do more processing of it.
420 fake = (REQUEST *) request_data_get(handler->request,
421 handler->request->proxy,
422 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
425 * Do the callback, if it exists, and if it was a success.
427 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
428 peap_tunnel_t *t = tls_session->opaque;
430 t->home_access_accept = TRUE;
435 rad_assert(fake->packet == NULL);
436 fake->packet = request->proxy;
437 fake->packet->src_ipaddr = request->packet->src_ipaddr;
438 request->proxy = NULL;
440 rad_assert(fake->reply == NULL);
441 fake->reply = request->proxy_reply;
442 request->proxy_reply = NULL;
444 if ((debug_flag > 0) && fr_log_fp) {
445 fprintf(fr_log_fp, "server %s {\n", fake->server);
449 * Perform a post-auth stage, which will get the EAP
452 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
453 RDEBUG2("Passing reply back for EAP-MS-CHAP-V2");
454 rcode = module_post_proxy(0, fake);
457 * FIXME: If rcode returns fail, do something
460 rcode = rad_postauth(fake);
462 if ((debug_flag > 0) && fr_log_fp) {
463 fprintf(fr_log_fp, "} # server %s\n", fake->server);
465 RDEBUG("Final reply from tunneled session code %d",
468 debug_pair_list(fake->reply->vps);
474 request->proxy = fake->packet;
476 request->proxy_reply = fake->reply;
480 * And we're done with this request.
484 case RLM_MODULE_FAIL:
486 eaptls_fail(handler, 0);
490 default: /* Don't Do Anything */
491 RDEBUG2("Got reply %d", request->proxy_reply->code);
495 request_free(&fake); /* robust if fake == NULL */
498 * If there was no EAP-Message in the reply packet, then
499 * we know that we're supposed to re-run the "authenticate"
500 * stage, in order to get the right kind of handling...
504 * Process the reply from the home server.
507 rcode = process_reply(handler, tls_session, handler->request,
508 handler->request->proxy_reply);
511 * The proxy code uses the reply from the home server as
512 * the basis for the reply to the NAS. We don't want that,
513 * so we toss it, after we've had our way with it.
515 pairfree(&handler->request->proxy_reply->vps);
518 case RLM_MODULE_REJECT:
519 RDEBUG2("Reply was rejected");
520 eaptls_fail(handler, 0);
523 case RLM_MODULE_HANDLED:
524 RDEBUG2("Reply was handled");
525 eaptls_request(handler->eap_ds, tls_session);
529 RDEBUG2("Reply was OK");
532 * Success: Automatically return MPPE keys.
534 return eaptls_success(handler, 0);
537 RDEBUG2("Reply was unknown.");
541 eaptls_fail(handler, 0);
548 static void my_request_free(void *data)
550 REQUEST *request = (REQUEST *)data;
552 request_free(&request);
557 * Process the pseudo-EAP contents of the tunneled data.
559 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
561 peap_tunnel_t *t = tls_session->opaque;
564 int rcode = RLM_MODULE_REJECT;
566 unsigned int data_len;
571 REQUEST *request = handler->request;
572 EAP_DS *eap_ds = handler->eap_ds;
575 * Just look at the buffer directly, without doing
576 * record_minus. This lets us avoid another data copy.
578 data_len = tls_session->clean_out.used;
579 tls_session->clean_out.used = 0;
580 data = tls_session->clean_out.data;
583 if ((debug_flag > 2) && fr_log_fp) {
584 for (i = 0; i < data_len; i++) {
585 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data in %04x: ", i);
587 fprintf(fr_log_fp, "%02x ", data[i]);
589 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
591 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
595 if (!eapmessage_verify(request, data, data_len)) {
596 RDEBUG2("Tunneled data is invalid.");
597 return RLM_MODULE_REJECT;
601 * If we authenticated the user, then it's OK.
603 if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
604 if (eappeap_check_tlv(request, data)) {
606 return RLM_MODULE_OK;
610 * Otherwise, the client rejected the session
611 * resumption. If the session is being re-used,
612 * we need to do a full authentication.
614 * We do this by sending an EAP-Identity request
615 * inside of the PEAP tunnel.
617 if ((t->session_resumption_state != PEAP_RESUMPTION_NO) &&
618 SSL_session_reused(tls_session->ssl)) {
619 eap_packet_t eap_packet;
621 RDEBUG2("Client rejected session resumption. Re-starting full authentication");
622 eap_packet.code = PW_EAP_REQUEST;
623 eap_packet.id = handler->eap_ds->response->id + 1;
624 eap_packet.length[0] = 0;
625 eap_packet.length[1] = EAP_HEADER_LEN + 1;
626 eap_packet.data[0] = PW_EAP_IDENTITY;
629 * Mark session resumption status.
632 t->session_resumption_state = PEAP_RESUMPTION_NO;
634 (tls_session->record_plus)(&tls_session->clean_in,
637 tls_handshake_send(tls_session);
638 return RLM_MODULE_HANDLED;
641 return RLM_MODULE_REJECT;
645 if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
646 RDEBUG2(" Had sent TLV failure. User was rejected earlier in this session.");
647 return RLM_MODULE_REJECT;
650 fake = request_alloc_fake(request);
652 rad_assert(fake->packet->vps == NULL);
654 fake->packet->vps = eap2vp(request, eap_ds, data, data_len);
655 if (!fake->packet->vps) {
657 RDEBUG2("Unable to convert tunneled EAP packet to internal server data structures");
658 return PW_AUTHENTICATION_REJECT;
661 if ((debug_flag > 0) && fr_log_fp) {
662 RDEBUG("Got tunneled request");
664 debug_pair_list(fake->packet->vps);
666 fprintf(fr_log_fp, "server %s {\n",
667 (fake->server == NULL) ? "" : fake->server);
671 * Tell the request that it's a fake one.
673 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
675 pairadd(&fake->packet->vps, vp);
679 * Update other items in the REQUEST data structure.
683 * There's no User-Name in the tunneled session,
684 * so we add one here, by pulling it out of the
685 * EAP-Identity packet.
687 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
688 t->username = pairmake("User-Name", "", T_OP_EQ);
689 rad_assert(t->username != NULL);
691 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
692 t->username->length = data_len - 1;
693 t->username->vp_strvalue[t->username->length] = 0;
694 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
697 * If there's a default EAP type,
700 if (t->default_eap_type != 0) {
701 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
702 vp = pairmake("EAP-Type", "0", T_OP_EQ);
703 vp->vp_integer = t->default_eap_type;
704 pairadd(&fake->config_items, vp);
707 } /* else there WAS a t->username */
710 vp = paircopy(t->username);
711 pairadd(&fake->packet->vps, vp);
712 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
713 DEBUG2(" PEAP: Setting User-Name to %s",
714 fake->username->vp_strvalue);
718 * Add the State attribute, too, if it exists.
721 vp = paircopy(t->state);
722 if (vp) pairadd(&fake->packet->vps, vp);
726 * If this is set, we copy SOME of the request attributes
727 * from outside of the tunnel to inside of the tunnel.
729 * We copy ONLY those attributes which do NOT already
730 * exist in the tunneled request.
732 * This code is copied from ../rlm_eap_ttls/ttls.c
734 if (t->copy_request_to_tunnel) {
737 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
739 * The attribute is a server-side thingy,
742 if ((vp->attribute > 255) &&
743 (((vp->attribute >> 16) & 0xffff) == 0)) {
748 * The outside attribute is already in the
749 * tunnel, don't copy it.
751 * This works for BOTH attributes which
752 * are originally in the tunneled request,
753 * AND attributes which are copied there
756 if (pairfind(fake->packet->vps, vp->attribute)) {
761 * Some attributes are handled specially.
763 switch (vp->attribute) {
765 * NEVER copy Message-Authenticator,
766 * EAP-Message, or State. They're
767 * only for outside of the tunnel.
770 case PW_USER_PASSWORD:
771 case PW_CHAP_PASSWORD:
772 case PW_CHAP_CHALLENGE:
774 case PW_MESSAGE_AUTHENTICATOR:
781 * By default, copy it over.
788 * Don't copy from the head, we've already
791 copy = paircopy2(vp, vp->attribute);
792 pairadd(&fake->packet->vps, copy);
796 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER)) != NULL) {
797 fake->server = vp->vp_strvalue;
799 } else if (t->virtual_server) {
800 fake->server = t->virtual_server;
802 } /* else fake->server == request->server */
804 if ((debug_flag > 0) && fr_log_fp) {
805 fprintf(fr_log_fp, "Sending tunneled request\n");
807 debug_pair_list(fake->packet->vps);
809 fprintf(fr_log_fp, "server %s {\n",
810 (fake->server == NULL) ? "" : fake->server);
814 * Call authentication recursively, which will
815 * do PAP, CHAP, MS-CHAP, etc.
817 request->process(fake);
820 * Note that we don't do *anything* with the reply
823 if ((debug_flag > 0) && fr_log_fp) {
824 fprintf(fr_log_fp, "} # server %s\n",
825 (fake->server == NULL) ? "" : fake->server);
827 RDEBUG("Got tunneled reply code %d", fake->reply->code);
829 debug_pair_list(fake->reply->vps);
833 * Decide what to do with the reply.
835 switch (fake->reply->code) {
836 case 0: /* No reply code, must be proxied... */
837 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
840 eap_tunnel_data_t *tunnel;
843 * The tunneled request was NOT handled,
844 * it has to be proxied. This means that
845 * the "authenticate" stage was never
848 * If we are told to NOT proxy the
849 * tunneled request as EAP, then this
850 * means that we've got to decode it,
851 * which means that we MUST run the
852 * "authenticate" portion by hand, here.
854 * Once the tunneled EAP session is ALMOST
855 * done, THEN we proxy it...
857 if (!t->proxy_tunneled_request_as_eap) {
858 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
861 * Hmm... should we check for
862 * Auth-Type & EAP-Message here?
867 * Run the EAP authentication.
869 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
870 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
871 if (rcode == RLM_MODULE_OK) {
873 * Authentication succeeded! Rah!
875 fake->reply->code = PW_AUTHENTICATION_ACK;
879 if (rcode != RLM_MODULE_HANDLED) {
880 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
881 rcode = RLM_MODULE_REJECT;
886 * The module decided it wasn't
887 * done. Handle it like normal.
889 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
890 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
895 * The module has decoded the
896 * EAP-Message into another set
899 pairdelete(&fake->packet->vps,
903 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
906 * Tell the original request that it's going
909 pairmove2(&(request->config_items),
910 &(fake->config_items),
914 * Seed the proxy packet with the
917 rad_assert(request->proxy == NULL);
918 request->proxy = fake->packet;
920 rad_free(&fake->reply);
924 * Set up the callbacks for the tunnel
926 tunnel = rad_malloc(sizeof(*tunnel));
927 memset(tunnel, 0, sizeof(*tunnel));
929 tunnel->tls_session = tls_session;
930 tunnel->callback = eappeap_postproxy;
933 * Associate the callback with the request.
935 rcode = request_data_add(request,
937 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
939 rad_assert(rcode == 0);
942 * We're not proxying it as EAP, so we've got
943 * to do the callback later.
945 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
946 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
949 * rlm_eap.c has taken care of associating
950 * the handler with the fake request.
952 * So we associate the fake request with
955 rcode = request_data_add(request,
957 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
958 fake, my_request_free);
959 rad_assert(rcode == 0);
962 * Do NOT free the fake request!
964 return RLM_MODULE_UPDATED;
968 * Didn't authenticate the packet, but
971 rcode = RLM_MODULE_UPDATED;
974 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
975 rcode = RLM_MODULE_REJECT;
981 rcode = process_reply(handler, tls_session, request,