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 memcpy(identity, data + 1, data_len - 1);
114 identity[data_len - 1] = '\0';
115 DEBUG2(" rlm_eap_peap: Identity - %s", identity);
120 * If the first byte of the packet is
121 * EAP-Response, and the EAP data is a TLV,
122 * then it looks OK...
124 case PW_EAP_RESPONSE:
125 if (eap_packet->data[0] == PW_EAP_TLV) {
126 DEBUG2(" rlm_eap_peap: Received EAP-TLV response.");
129 DEBUG2(" rlm_eap_peap: Got something weird.");
134 * We normally do Microsoft MS-CHAPv2 (26), versus
135 * Cisco MS-CHAPv2 (29).
137 case PW_EAP_MSCHAPV2:
139 DEBUG2(" rlm_eap_peap: EAP type %s",
140 eaptype_type2name(eap_type,
141 identity, sizeof(identity)));
150 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
152 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
153 const uint8_t *data, unsigned int data_len)
155 VALUE_PAIR *vp = NULL;
158 * Sanity check this...
160 if (data_len + EAP_HEADER_LEN > MAX_STRING_LEN) {
161 radlog(L_ERR, "rlm_eap_peap: EAP Response packet is too large. Code must be fixed to handle this.");
165 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
167 DEBUG2(" rlm_eap_peap: Failure in creating VP");
172 * Hand-build an EAP packet from the crap in PEAP version 0.
174 vp->vp_strvalue[0] = PW_EAP_RESPONSE;
175 vp->vp_strvalue[1] = eap_ds->response->id;
176 vp->vp_strvalue[2] = 0;
177 vp->vp_strvalue[3] = EAP_HEADER_LEN + data_len;
179 memcpy(vp->vp_strvalue + EAP_HEADER_LEN, data, data_len);
180 vp->length = EAP_HEADER_LEN + data_len;
187 * Convert a list of VALUE_PAIR's to an EAP packet, through the
188 * simple expedient of dumping the EAP message
190 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
192 if (vp->next != NULL) {
193 radlog(L_ERR, "rlm_eap_peap: EAP Request packet is too large. Code must be fixed to handle this.");
198 * Skip the id, code, and length. Just write the EAP
199 * type & data to the client.
202 if (debug_flag > 2) {
204 int total = vp->length - 4;
206 if (debug_flag > 0) for (i = 0; i < total; i++) {
207 if ((i & 0x0f) == 0) printf(" PEAP tunnel data out %04x: ", i);
209 printf("%02x ", vp->vp_strvalue[i + 4]);
211 if ((i & 0x0f) == 0x0f) printf("\n");
213 if ((total & 0x0f) != 0) printf("\n");
218 * Send the EAP data, WITHOUT the header.
221 (tls_session->record_plus)(&tls_session->clean_in, vp->vp_strvalue + EAP_HEADER_LEN,
222 vp->length - EAP_HEADER_LEN);
224 (tls_session->record_plus)(&tls_session->clean_in, vp->vp_strvalue, vp->length);
226 tls_handshake_send(tls_session);
233 * See if there's a TLV in the response.
235 static int eappeap_check_tlv(const uint8_t *data)
237 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
240 * Look for success or failure.
242 if ((eap_packet->code == PW_EAP_RESPONSE) &&
243 (eap_packet->data[0] == PW_EAP_TLV)) {
244 if (data[10] == EAP_TLV_SUCCESS) {
248 if (data[10] == EAP_TLV_FAILURE) {
249 DEBUG2(" rlm_eap_peap: Client rejected our response. The password is probably incorrect.");
259 * Use a reply packet to determine what to do.
261 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
262 UNUSED REQUEST *request, RADIUS_PACKET *reply)
264 int rcode = RLM_MODULE_REJECT;
266 peap_tunnel_t *t = tls_session->opaque;
269 if (debug_flag > 0) {
270 printf(" PEAP: Processing from tunneled session code %p %d\n",
273 for (vp = reply->vps; vp != NULL; vp = vp->next) {
274 putchar('\t');vp_print(stdout, vp);putchar('\n');
279 switch (reply->code) {
280 case PW_AUTHENTICATION_ACK:
281 DEBUG2(" PEAP: Tunneled authentication was successful.");
282 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
283 eappeap_success(handler, tls_session);
284 rcode = RLM_MODULE_HANDLED;
287 * If we've been told to use the attributes from
288 * the reply, then do so.
290 * WARNING: This may leak information about the
293 if (t->use_tunneled_reply) {
294 DEBUG2(" Saving tunneled attributes for later");
297 * Clean up the tunneled reply.
299 pairdelete(&reply->vps, PW_PROXY_STATE);
300 pairdelete(&reply->vps, PW_EAP_MESSAGE);
301 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
303 t->accept_vps = reply->vps;
308 case PW_AUTHENTICATION_REJECT:
309 DEBUG2(" PEAP: Tunneled authentication was rejected.");
310 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
311 eappeap_failure(handler, tls_session);
312 rcode = RLM_MODULE_HANDLED;
315 case PW_ACCESS_CHALLENGE:
316 DEBUG2(" PEAP: Got tunneled Access-Challenge");
319 * Keep the State attribute, if necessary.
321 * Get rid of the old State, too.
324 pairmove2(&t->state, &(reply->vps), PW_STATE);
327 * PEAP takes only EAP-Message attributes inside
328 * of the tunnel. Any Reply-Message in the
329 * Access-Challenge is ignored.
332 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
335 * Handle EAP-MSCHAP-V2, where Access-Accept's
336 * from the home server may contain MS-CHAP-Success,
337 * which the module turns into challenges, so that
338 * the client may respond to the challenge with
341 if (t->home_access_accept && t->use_tunneled_reply) {
342 DEBUG2(" Saving tunneled attributes for later");
345 * Clean up the tunneled reply.
347 pairdelete(&reply->vps, PW_PROXY_STATE);
348 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
350 t->accept_vps = reply->vps;
355 * Handle the ACK, by tunneling any necessary reply
356 * VP's back to the client.
359 vp2eap(tls_session, vp);
363 rcode = RLM_MODULE_HANDLED;
367 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
368 rcode = RLM_MODULE_REJECT;
377 * Do post-proxy processing,
379 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
382 tls_session_t *tls_session = (tls_session_t *) data;
385 DEBUG2(" PEAP: Passing reply from proxy back into the tunnel.");
388 * If there was a fake request associated with the proxied
389 * request, do more processing of it.
391 fake = (REQUEST *) request_data_get(handler->request,
392 handler->request->proxy,
393 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
396 * Do the callback, if it exists, and if it was a success.
398 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
400 REQUEST *request = handler->request;
401 peap_tunnel_t *t = tls_session->opaque;
403 t->home_access_accept = TRUE;
408 rad_assert(fake->packet == NULL);
409 fake->packet = request->proxy;
410 request->proxy = NULL;
412 rad_assert(fake->reply == NULL);
413 fake->reply = request->proxy_reply;
414 request->proxy_reply = NULL;
417 * Perform a post-auth stage, which will get the EAP
420 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
421 DEBUG2(" PEAP: Passing reply back for EAP-MS-CHAP-V2 %p %d",
422 fake, fake->reply->code);
423 rcode = module_post_proxy(0, fake);
426 * FIXME: If rcode returns fail, do something
429 DEBUG2(" POST-PROXY %d", rcode);
430 rcode = rad_postauth(fake);
431 DEBUG2(" POST-AUTH %d", rcode);
434 if (debug_flag > 0) {
435 printf(" PEAP: Final reply from tunneled session code %d\n",
438 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
439 putchar('\t');vp_print(stdout, vp);putchar('\n');
447 request->proxy = fake->packet;
449 request->proxy_reply = fake->reply;
453 * And we're done with this request.
457 case RLM_MODULE_FAIL:
459 eaptls_fail(handler->eap_ds, 0);
463 default: /* Don't Do Anything */
464 DEBUG2(" PEAP: Got reply %d",
465 request->proxy_reply->code);
469 request_free(&fake); /* robust if fake == NULL */
472 * If there was no EAP-Message in the reply packet, then
473 * we know that we're supposed to re-run the "authenticate"
474 * stage, in order to get the right kind of handling...
478 * Process the reply from the home server.
481 rcode = process_reply(handler, tls_session, handler->request,
482 handler->request->proxy_reply);
485 * The proxy code uses the reply from the home server as
486 * the basis for the reply to the NAS. We don't want that,
487 * so we toss it, after we've had our way with it.
489 pairfree(&handler->request->proxy_reply->vps);
492 case RLM_MODULE_REJECT:
493 DEBUG2(" PEAP: Reply was rejected");
494 eaptls_fail(handler->eap_ds, 0);
497 case RLM_MODULE_HANDLED:
498 DEBUG2(" PEAP: Reply was handled");
499 eaptls_request(handler->eap_ds, tls_session);
503 DEBUG2(" PEAP: Reply was OK");
504 eaptls_success(handler->eap_ds, 0);
505 eaptls_gen_mppe_keys(&handler->request->reply->vps,
507 "client EAP encryption");
511 DEBUG2(" PEAP: Reply was unknown.");
515 eaptls_fail(handler->eap_ds, 0);
522 static void my_request_free(void *data)
524 REQUEST *request = (REQUEST *)data;
526 request_free(&request);
531 * Process the pseudo-EAP contents of the tunneled data.
533 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
536 peap_tunnel_t *t = tls_session->opaque;
539 int rcode = RLM_MODULE_REJECT;
541 unsigned int data_len;
542 unsigned char buffer[1024];
547 REQUEST *request = handler->request;
548 EAP_DS *eap_ds = handler->eap_ds;
551 * Grab the dirty data, and copy it to our buffer.
553 * I *really* don't like these 'record_t' things...
555 data_len = (tls_session->record_minus)(&tls_session->dirty_in, buffer, sizeof(buffer));
559 * Write the data from the dirty buffer (i.e. packet
560 * data) into the buffer which we will give to SSL for
563 * Some of this code COULD technically go into the TLS
564 * module, in eaptls_process(), where it returns EAPTLS_OK.
566 * Similarly, the writing of data to the SSL context could
569 BIO_write(tls_session->into_ssl, buffer, data_len);
570 (tls_session->record_init)(&tls_session->clean_out);
573 * Read (and decrypt) the tunneled data from the SSL session,
574 * and put it into the decrypted data buffer.
576 err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
577 sizeof(tls_session->clean_out.data));
580 * FIXME: Call SSL_get_error() to see what went
583 radlog(L_INFO, "rlm_eap_peap: SSL_read Error");
584 return RLM_MODULE_REJECT;
588 * If there's no data, maybe this is an ACK to an
593 * FIXME: Call SSL_get_error() to see what went
596 radlog(L_INFO, "rlm_eap_peap: No data inside of the tunnel.");
597 return RLM_MODULE_REJECT;
600 data_len = tls_session->clean_out.used = err;
601 data = tls_session->clean_out.data;
604 if (debug_flag > 2) {
605 for (i = 0; i < data_len; i++) {
606 if ((i & 0x0f) == 0) printf(" PEAP tunnel data in %04x: ", i);
608 printf("%02x ", data[i]);
610 if ((i & 0x0f) == 0x0f) printf("\n");
612 if ((data_len & 0x0f) != 0) printf("\n");
616 if (!eapmessage_verify(data, data_len)) {
617 return RLM_MODULE_REJECT;
620 DEBUG2(" rlm_eap_peap: Tunneled data is valid.");
623 * If we authenticated the user, then it's OK.
625 if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
626 if (eappeap_check_tlv(data)) {
627 DEBUG2(" rlm_eap_peap: Success");
628 return RLM_MODULE_OK;
631 return RLM_MODULE_REJECT;
633 } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
634 DEBUG2(" rlm_eap_peap: Had sent TLV failure. User was rejected earlier in this session.");
635 return RLM_MODULE_REJECT;
638 fake = request_alloc_fake(request);
640 rad_assert(fake->packet->vps == NULL);
642 fake->packet->vps = eap2vp(eap_ds, data, data_len);
643 if (!fake->packet->vps) {
645 DEBUG2(" rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
646 return PW_AUTHENTICATION_REJECT;
650 if (debug_flag > 0) {
651 printf(" PEAP: Got tunneled EAP-Message\n");
653 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
654 putchar('\t');vp_print(stdout, vp);putchar('\n');
660 * Tell the request that it's a fake one.
662 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
664 pairadd(&fake->packet->vps, vp);
668 * Update other items in the REQUEST data structure.
672 * There's no User-Name in the tunneled session,
673 * so we add one here, by pulling it out of the
674 * EAP-Identity packet.
676 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
677 t->username = pairmake("User-Name", "", T_OP_EQ);
678 rad_assert(t->username != NULL);
680 memcpy(t->username->vp_strvalue, data + 1, data_len - 1);
681 t->username->length = data_len - 1;
682 t->username->vp_strvalue[t->username->length] = 0;
683 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->vp_strvalue);
686 * If there's a default EAP type,
689 if (t->default_eap_type != 0) {
690 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
691 vp = pairmake("EAP-Type", "0", T_OP_EQ);
692 vp->vp_integer = t->default_eap_type;
693 pairadd(&fake->config_items, vp);
696 } /* else there WAS a t->username */
699 vp = paircopy(t->username);
700 pairadd(&fake->packet->vps, vp);
701 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
702 DEBUG2(" PEAP: Setting User-Name to %s",
703 fake->username->vp_strvalue);
707 * Add the State attribute, too, if it exists.
710 DEBUG2(" PEAP: Adding old state with %02x %02x",
711 t->state->vp_strvalue[0], t->state->vp_strvalue[1]);
712 vp = paircopy(t->state);
713 if (vp) pairadd(&fake->packet->vps, vp);
717 * If this is set, we copy SOME of the request attributes
718 * from outside of the tunnel to inside of the tunnel.
720 * We copy ONLY those attributes which do NOT already
721 * exist in the tunneled request.
723 * This code is copied from ../rlm_eap_ttls/ttls.c
725 if (t->copy_request_to_tunnel) {
728 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
730 * The attribute is a server-side thingy,
733 if ((vp->attribute > 255) &&
734 (((vp->attribute >> 16) & 0xffff) == 0)) {
739 * The outside attribute is already in the
740 * tunnel, don't copy it.
742 * This works for BOTH attributes which
743 * are originally in the tunneled request,
744 * AND attributes which are copied there
747 if (pairfind(fake->packet->vps, vp->attribute)) {
752 * Some attributes are handled specially.
754 switch (vp->attribute) {
756 * NEVER copy Message-Authenticator,
757 * EAP-Message, or State. They're
758 * only for outside of the tunnel.
761 case PW_USER_PASSWORD:
762 case PW_CHAP_PASSWORD:
763 case PW_CHAP_CHALLENGE:
765 case PW_MESSAGE_AUTHENTICATOR:
772 * By default, copy it over.
779 * Don't copy from the head, we've already
782 copy = paircopy2(vp, vp->attribute);
783 pairadd(&fake->packet->vps, copy);
787 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER)) != NULL) {
788 fake->server = vp->vp_strvalue;
790 } else if (t->virtual_server) {
791 fake->server = t->virtual_server;
793 } /* else fake->server == request->server */
796 if (debug_flag > 0) {
797 printf(" PEAP: Sending tunneled request\n");
799 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
800 putchar('\t');vp_print(stdout, vp);putchar('\n');
803 printf("server %s {\n", fake->server);
808 * Call authentication recursively, which will
809 * do PAP, CHAP, MS-CHAP, etc.
811 rad_authenticate(fake);
814 * Note that we don't do *anything* with the reply
818 if (debug_flag > 0) {
819 printf("} # server %s\n", fake->server);
821 printf(" PEAP: Got tunneled reply RADIUS code %d\n",
824 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
825 putchar('\t');vp_print(stdout, vp);putchar('\n');
831 * Decide what to do with the reply.
833 switch (fake->reply->code) {
834 case 0: /* No reply code, must be proxied... */
835 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
838 eap_tunnel_data_t *tunnel;
841 * The tunneled request was NOT handled,
842 * it has to be proxied. This means that
843 * the "authenticate" stage was never
846 * If we are told to NOT proxy the
847 * tunneled request as EAP, then this
848 * means that we've got to decode it,
849 * which means that we MUST run the
850 * "authenticate" portion by hand, here.
852 * Once the tunneled EAP session is ALMOST
853 * done, THEN we proxy it...
855 if (!t->proxy_tunneled_request_as_eap) {
856 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
859 * Hmm... should we check for
860 * Auth-Type & EAP-Message here?
865 * Run the EAP authentication.
867 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
868 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
869 if (rcode == RLM_MODULE_OK) {
871 * Authentication succeeded! Rah!
873 fake->reply->code = PW_AUTHENTICATION_ACK;
877 if (rcode != RLM_MODULE_HANDLED) {
878 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
879 rcode = RLM_MODULE_REJECT;
884 * The module decided it wasn't
885 * done. Handle it like normal.
887 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
888 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->vp_strvalue);
893 * The module has decoded the
894 * EAP-Message into another set
897 pairdelete(&fake->packet->vps,
901 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->vp_strvalue);
904 * Tell the original request that it's going
907 pairmove2(&(request->config_items),
908 &(fake->config_items),
912 * Seed the proxy packet with the
915 rad_assert(request->proxy == NULL);
916 request->proxy = fake->packet;
918 rad_free(&fake->reply);
922 * Set up the callbacks for the tunnel
924 tunnel = rad_malloc(sizeof(*tunnel));
925 memset(tunnel, 0, sizeof(*tunnel));
927 tunnel->tls_session = tls_session;
928 tunnel->callback = eappeap_postproxy;
931 * Associate the callback with the request.
933 rcode = request_data_add(request,
935 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
937 rad_assert(rcode == 0);
940 * We're not proxying it as EAP, so we've got
941 * to do the callback later.
943 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
944 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
947 * rlm_eap.c has taken care of associating
948 * the handler with the fake request.
950 * So we associate the fake request with
953 rcode = request_data_add(request,
955 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
956 fake, my_request_free);
957 rad_assert(rcode == 0);
960 * Do NOT free the fake request!
962 return RLM_MODULE_UPDATED;
966 * Didn't authenticate the packet, but
969 rcode = RLM_MODULE_UPDATED;
972 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
973 rcode = RLM_MODULE_REJECT;
979 rcode = process_reply(handler, tls_session, request,