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];
38 DEBUG2(" rlm_eap_peap: FAILURE");
40 tlv_packet[0] = PW_EAP_REQUEST;
41 tlv_packet[1] = handler->eap_ds->response->id +1;
43 tlv_packet[3] = 11; /* length of this packet */
44 tlv_packet[4] = PW_EAP_TLV;
46 tlv_packet[6] = EAP_TLV_ACK_RESULT;
48 tlv_packet[8] = 2; /* length of the data portion */
50 tlv_packet[10] = EAP_TLV_FAILURE;
52 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
55 * FIXME: Check the return code.
57 tls_handshake_send(tls_session);
64 * Send protected EAP-Success
66 * Result-TLV = Success
68 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
70 uint8_t tlv_packet[11];
72 DEBUG2(" rlm_eap_peap: SUCCESS");
74 tlv_packet[0] = PW_EAP_REQUEST;
75 tlv_packet[1] = handler->eap_ds->response->id +1;
77 tlv_packet[3] = 11; /* length of this packet */
78 tlv_packet[4] = PW_EAP_TLV;
79 tlv_packet[5] = 0x80; /* mandatory AVP */
80 tlv_packet[6] = EAP_TLV_ACK_RESULT;
82 tlv_packet[8] = 2; /* length of the data portion */
84 tlv_packet[10] = EAP_TLV_SUCCESS;
86 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
89 * FIXME: Check the return code.
91 tls_handshake_send(tls_session);
98 * Verify the tunneled EAP message.
100 static int eapmessage_verify(const uint8_t *data, unsigned int data_len)
102 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
106 if (!data || (data_len <= 1)) {
112 case PW_EAP_IDENTITY:
113 DEBUG2(" rlm_eap_peap: Identity - %*s",
114 data_len - 1, data + 1);
119 * If the first byte of the packet is
120 * EAP-Response, and the EAP data is a TLV,
121 * then it looks OK...
123 case PW_EAP_RESPONSE:
124 if (eap_packet->data[0] == PW_EAP_TLV) {
125 DEBUG2(" rlm_eap_peap: Received EAP-TLV response.");
128 DEBUG2(" rlm_eap_peap: Got something weird.");
133 * We normally do Microsoft MS-CHAPv2 (26), versus
134 * Cisco MS-CHAPv2 (29).
136 case PW_EAP_MSCHAPV2:
138 DEBUG2(" rlm_eap_peap: EAP type %s",
139 eaptype_type2name(eap_type,
140 buffer, sizeof(buffer)));
149 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
151 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
152 const uint8_t *data, size_t data_len)
155 VALUE_PAIR *vp = NULL, *head, **tail;
157 if (data_len > 65535) return NULL; /* paranoia */
159 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
161 DEBUG2(" rlm_eap_peap: Failure in creating VP");
166 if (total > 249) total = 249;
169 * Hand-build an EAP packet from the crap in PEAP version 0.
171 vp->vp_octets[0] = PW_EAP_RESPONSE;
172 vp->vp_octets[1] = eap_ds->response->id;
173 vp->vp_octets[2] = (data_len + EAP_HEADER_LEN) >> 8;
174 vp->vp_octets[3] = (data_len + EAP_HEADER_LEN) & 0xff;
176 memcpy(vp->vp_octets + EAP_HEADER_LEN, data, total);
177 vp->length = EAP_HEADER_LEN + total;
181 while (total < data_len) {
185 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
187 DEBUG2(" rlm_eap_peap: Failure in creating VP");
191 vp_len = (data_len - total);
192 if (vp_len > 253) vp_len = 253;
194 memcpy(vp->vp_octets, data + total, vp_len);
207 * Convert a list of VALUE_PAIR's to an EAP packet, through the
208 * simple expedient of dumping the EAP message
210 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
213 * Skip the id, code, and length. Just write the EAP
214 * type & data to the client.
217 if ((debug_flag > 2) && fr_log_fp) {
223 for (this = vp; this != NULL; this = this->next) {
226 if (this == vp) start = EAP_HEADER_LEN;
228 for (i = start; i < vp->length; i++) {
229 if ((total & 0x0f) == 0) fprintf(fr_log_fp, " PEAP tunnel data out %04x: ", total);
231 fprintf(fr_log_fp, "%02x ", vp->vp_octets[i]);
233 if ((total & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
237 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
242 * Send the EAP data, WITHOUT the header.
244 (tls_session->record_plus)(&tls_session->clean_in,
245 vp->vp_octets + EAP_HEADER_LEN,
246 vp->length - EAP_HEADER_LEN);
249 * Send the rest of the EAP data.
251 for (vp = vp->next; vp != NULL; vp = vp->next) {
252 (tls_session->record_plus)(&tls_session->clean_in,
253 vp->vp_octets, vp->length);
256 tls_handshake_send(tls_session);
263 * See if there's a TLV in the response.
265 static int eappeap_check_tlv(const uint8_t *data)
267 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
270 * Look for success or failure.
272 if ((eap_packet->code == PW_EAP_RESPONSE) &&
273 (eap_packet->data[0] == PW_EAP_TLV)) {
274 if (data[10] == EAP_TLV_SUCCESS) {
278 if (data[10] == EAP_TLV_FAILURE) {
279 DEBUG2(" rlm_eap_peap: Client rejected our response. The password is probably incorrect.");
289 * Use a reply packet to determine what to do.
291 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
292 UNUSED REQUEST *request, RADIUS_PACKET *reply)
294 int rcode = RLM_MODULE_REJECT;
296 peap_tunnel_t *t = tls_session->opaque;
298 if ((debug_flag > 0) && fr_log_fp) {
299 RDEBUG("Got tunneled reply RADIUS code %d",
302 debug_pair_list(reply->vps);
305 switch (reply->code) {
306 case PW_AUTHENTICATION_ACK:
307 DEBUG2(" PEAP: Tunneled authentication was successful.");
308 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
309 eappeap_success(handler, tls_session);
310 rcode = RLM_MODULE_HANDLED;
313 * If we've been told to use the attributes from
314 * the reply, then do so.
316 * WARNING: This may leak information about the
319 if (t->use_tunneled_reply) {
320 DEBUG2(" Saving tunneled attributes for later");
323 * Clean up the tunneled reply.
325 pairdelete(&reply->vps, PW_PROXY_STATE);
326 pairdelete(&reply->vps, PW_EAP_MESSAGE);
327 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
329 t->accept_vps = reply->vps;
334 case PW_AUTHENTICATION_REJECT:
335 DEBUG2(" PEAP: Tunneled authentication was rejected.");
336 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
337 eappeap_failure(handler, tls_session);
338 rcode = RLM_MODULE_HANDLED;
341 case PW_ACCESS_CHALLENGE:
342 DEBUG2(" PEAP: Got tunneled Access-Challenge");
345 * Keep the State attribute, if necessary.
347 * Get rid of the old State, too.
350 pairmove2(&t->state, &(reply->vps), PW_STATE);
353 * PEAP takes only EAP-Message attributes inside
354 * of the tunnel. Any Reply-Message in the
355 * Access-Challenge is ignored.
358 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
361 * Handle EAP-MSCHAP-V2, where Access-Accept's
362 * from the home server may contain MS-CHAP-Success,
363 * which the module turns into challenges, so that
364 * the client may respond to the challenge with
367 if (t->home_access_accept && t->use_tunneled_reply) {
368 DEBUG2(" Saving tunneled attributes for later");
371 * Clean up the tunneled reply.
373 pairdelete(&reply->vps, PW_PROXY_STATE);
374 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
376 t->accept_vps = reply->vps;
381 * Handle the ACK, by tunneling any necessary reply
382 * VP's back to the client.
385 vp2eap(tls_session, vp);
389 rcode = RLM_MODULE_HANDLED;
393 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
394 rcode = RLM_MODULE_REJECT;
403 * Do post-proxy processing,
405 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
408 tls_session_t *tls_session = (tls_session_t *) data;
411 DEBUG2(" PEAP: Passing reply from proxy back into the tunnel.");
414 * If there was a fake request associated with the proxied
415 * request, do more processing of it.
417 fake = (REQUEST *) request_data_get(handler->request,
418 handler->request->proxy,
419 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
422 * Do the callback, if it exists, and if it was a success.
424 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
425 REQUEST *request = handler->request;
426 peap_tunnel_t *t = tls_session->opaque;
428 t->home_access_accept = TRUE;
433 rad_assert(fake->packet == NULL);
434 fake->packet = request->proxy;
435 fake->packet->src_ipaddr = request->packet->src_ipaddr;
436 request->proxy = NULL;
438 rad_assert(fake->reply == NULL);
439 fake->reply = request->proxy_reply;
440 request->proxy_reply = NULL;
442 if ((debug_flag > 0) && fr_log_fp) {
443 fprintf(fr_log_fp, "server %s {\n", fake->server);
447 * Perform a post-auth stage, which will get the EAP
450 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
451 DEBUG2(" PEAP: Passing reply back for EAP-MS-CHAP-V2");
452 rcode = module_post_proxy(0, fake);
455 * FIXME: If rcode returns fail, do something
458 rcode = rad_postauth(fake);
460 if ((debug_flag > 0) && fr_log_fp) {
461 fprintf(fr_log_fp, "} # server %s\n", fake->server);
463 RDEBUG("Final reply from tunneled session code %d",
466 debug_pair_list(fake->reply->vps);
472 request->proxy = fake->packet;
474 request->proxy_reply = fake->reply;
478 * And we're done with this request.
482 case RLM_MODULE_FAIL:
484 eaptls_fail(handler->eap_ds, 0);
488 default: /* Don't Do Anything */
489 DEBUG2(" PEAP: Got reply %d",
490 request->proxy_reply->code);
494 request_free(&fake); /* robust if fake == NULL */
497 * If there was no EAP-Message in the reply packet, then
498 * we know that we're supposed to re-run the "authenticate"
499 * stage, in order to get the right kind of handling...
503 * Process the reply from the home server.
506 rcode = process_reply(handler, tls_session, handler->request,
507 handler->request->proxy_reply);
510 * The proxy code uses the reply from the home server as
511 * the basis for the reply to the NAS. We don't want that,
512 * so we toss it, after we've had our way with it.
514 pairfree(&handler->request->proxy_reply->vps);
517 case RLM_MODULE_REJECT:
518 DEBUG2(" PEAP: Reply was rejected");
519 eaptls_fail(handler->eap_ds, 0);
522 case RLM_MODULE_HANDLED:
523 DEBUG2(" PEAP: Reply was handled");
524 eaptls_request(handler->eap_ds, tls_session);
528 DEBUG2(" PEAP: Reply was OK");
529 eaptls_success(handler->eap_ds, 0);
530 eaptls_gen_mppe_keys(&handler->request->reply->vps,
532 "client EAP encryption");
536 DEBUG2(" PEAP: Reply was unknown.");
540 eaptls_fail(handler->eap_ds, 0);
547 static void my_request_free(void *data)
549 REQUEST *request = (REQUEST *)data;
551 request_free(&request);
556 * Process the pseudo-EAP contents of the tunneled data.
558 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(data, data_len)) {
596 DEBUG2(" rlm_eap_peap: Tunneled data is invalid.");
597 return RLM_MODULE_REJECT;
602 * If we authenticated the user, then it's OK.
604 if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
605 if (eappeap_check_tlv(data)) {
606 DEBUG2(" rlm_eap_peap: Success");
607 return RLM_MODULE_OK;
610 return RLM_MODULE_REJECT;
612 } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
613 DEBUG2(" rlm_eap_peap: Had sent TLV failure. User was rejected earlier in this session.");
614 return RLM_MODULE_REJECT;
617 fake = request_alloc_fake(request);
619 rad_assert(fake->packet->vps == NULL);
621 fake->packet->vps = eap2vp(eap_ds, data, data_len);
622 if (!fake->packet->vps) {
624 DEBUG2(" rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
625 return PW_AUTHENTICATION_REJECT;
628 if ((debug_flag > 0) && fr_log_fp) {
629 RDEBUG("Got tunnled request");
631 debug_pair_list(fake->packet->vps);
633 fprintf(fr_log_fp, "server %s {\n", fake->server);
637 * Tell the request that it's a fake one.
639 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
641 pairadd(&fake->packet->vps, vp);
645 * Update other items in the REQUEST data structure.
649 * There's no User-Name in the tunneled session,
650 * so we add one here, by pulling it out of the
651 * EAP-Identity packet.
653 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
654 t->username = pairmake("User-Name", "", T_OP_EQ);
655 rad_assert(t->username != NULL);
657 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
658 t->username->length = data_len - 1;
659 t->username->vp_strvalue[t->username->length] = 0;
660 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
663 * If there's a default EAP type,
666 if (t->default_eap_type != 0) {
667 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
668 vp = pairmake("EAP-Type", "0", T_OP_EQ);
669 vp->vp_integer = t->default_eap_type;
670 pairadd(&fake->config_items, vp);
673 } /* else there WAS a t->username */
676 vp = paircopy(t->username);
677 pairadd(&fake->packet->vps, vp);
678 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
679 DEBUG2(" PEAP: Setting User-Name to %s",
680 fake->username->vp_strvalue);
684 * Add the State attribute, too, if it exists.
687 vp = paircopy(t->state);
688 if (vp) pairadd(&fake->packet->vps, vp);
692 * If this is set, we copy SOME of the request attributes
693 * from outside of the tunnel to inside of the tunnel.
695 * We copy ONLY those attributes which do NOT already
696 * exist in the tunneled request.
698 * This code is copied from ../rlm_eap_ttls/ttls.c
700 if (t->copy_request_to_tunnel) {
703 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
705 * The attribute is a server-side thingy,
708 if ((vp->attribute > 255) &&
709 (((vp->attribute >> 16) & 0xffff) == 0)) {
714 * The outside attribute is already in the
715 * tunnel, don't copy it.
717 * This works for BOTH attributes which
718 * are originally in the tunneled request,
719 * AND attributes which are copied there
722 if (pairfind(fake->packet->vps, vp->attribute)) {
727 * Some attributes are handled specially.
729 switch (vp->attribute) {
731 * NEVER copy Message-Authenticator,
732 * EAP-Message, or State. They're
733 * only for outside of the tunnel.
736 case PW_USER_PASSWORD:
737 case PW_CHAP_PASSWORD:
738 case PW_CHAP_CHALLENGE:
740 case PW_MESSAGE_AUTHENTICATOR:
747 * By default, copy it over.
754 * Don't copy from the head, we've already
757 copy = paircopy2(vp, vp->attribute);
758 pairadd(&fake->packet->vps, copy);
762 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER)) != NULL) {
763 fake->server = vp->vp_strvalue;
765 } else if (t->virtual_server) {
766 fake->server = t->virtual_server;
768 } /* else fake->server == request->server */
770 if ((debug_flag > 0) && fr_log_fp) {
771 fprintf(fr_log_fp, "Sending tunneled request\n");
773 debug_pair_list(fake->packet->vps);
775 fprintf(fr_log_fp, "server %s {\n", fake->server);
779 * Call authentication recursively, which will
780 * do PAP, CHAP, MS-CHAP, etc.
782 rad_authenticate(fake);
785 * Note that we don't do *anything* with the reply
788 if ((debug_flag > 0) && fr_log_fp) {
789 fprintf(fr_log_fp, "} # server %s\n", fake->server);
791 RDEBUG("Got tunneled reply code %d", fake->reply->code);
793 debug_pair_list(fake->reply->vps);
797 * Decide what to do with the reply.
799 switch (fake->reply->code) {
800 case 0: /* No reply code, must be proxied... */
801 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
804 eap_tunnel_data_t *tunnel;
807 * The tunneled request was NOT handled,
808 * it has to be proxied. This means that
809 * the "authenticate" stage was never
812 * If we are told to NOT proxy the
813 * tunneled request as EAP, then this
814 * means that we've got to decode it,
815 * which means that we MUST run the
816 * "authenticate" portion by hand, here.
818 * Once the tunneled EAP session is ALMOST
819 * done, THEN we proxy it...
821 if (!t->proxy_tunneled_request_as_eap) {
822 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
825 * Hmm... should we check for
826 * Auth-Type & EAP-Message here?
831 * Run the EAP authentication.
833 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
834 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
835 if (rcode == RLM_MODULE_OK) {
837 * Authentication succeeded! Rah!
839 fake->reply->code = PW_AUTHENTICATION_ACK;
843 if (rcode != RLM_MODULE_HANDLED) {
844 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
845 rcode = RLM_MODULE_REJECT;
850 * The module decided it wasn't
851 * done. Handle it like normal.
853 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
854 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
859 * The module has decoded the
860 * EAP-Message into another set
863 pairdelete(&fake->packet->vps,
867 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
870 * Tell the original request that it's going
873 pairmove2(&(request->config_items),
874 &(fake->config_items),
878 * Seed the proxy packet with the
881 rad_assert(request->proxy == NULL);
882 request->proxy = fake->packet;
884 rad_free(&fake->reply);
888 * Set up the callbacks for the tunnel
890 tunnel = rad_malloc(sizeof(*tunnel));
891 memset(tunnel, 0, sizeof(*tunnel));
893 tunnel->tls_session = tls_session;
894 tunnel->callback = eappeap_postproxy;
897 * Associate the callback with the request.
899 rcode = request_data_add(request,
901 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
903 rad_assert(rcode == 0);
906 * We're not proxying it as EAP, so we've got
907 * to do the callback later.
909 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
910 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
913 * rlm_eap.c has taken care of associating
914 * the handler with the fake request.
916 * So we associate the fake request with
919 rcode = request_data_add(request,
921 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
922 fake, my_request_free);
923 rad_assert(rcode == 0);
926 * Do NOT free the fake request!
928 return RLM_MODULE_UPDATED;
932 * Didn't authenticate the packet, but
935 rcode = RLM_MODULE_UPDATED;
938 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
939 rcode = RLM_MODULE_REJECT;
945 rcode = process_reply(handler, tls_session, request,