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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2003 Alan DeKok <aland@freeradius.org>
25 * Send protected EAP-Failure
27 * Result-TLV = Failure
29 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
31 uint8_t tlv_packet[11];
33 DEBUG2(" rlm_eap_peap: FAILURE");
35 tlv_packet[0] = PW_EAP_REQUEST;
36 tlv_packet[1] = handler->eap_ds->response->id +1;
38 tlv_packet[3] = 11; /* length of this packet */
39 tlv_packet[4] = PW_EAP_TLV;
41 tlv_packet[6] = EAP_TLV_ACK_RESULT;
43 tlv_packet[8] = 2; /* length of the data portion */
45 tlv_packet[10] = EAP_TLV_FAILURE;
47 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
50 * FIXME: Check the return code.
52 tls_handshake_send(tls_session);
59 * Send protected EAP-Success
61 * Result-TLV = Success
63 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
65 uint8_t tlv_packet[11];
67 DEBUG2(" rlm_eap_peap: SUCCESS");
69 tlv_packet[0] = PW_EAP_REQUEST;
70 tlv_packet[1] = handler->eap_ds->response->id +1;
72 tlv_packet[3] = 11; /* length of this packet */
73 tlv_packet[4] = PW_EAP_TLV;
74 tlv_packet[5] = 0x80; /* mandatory AVP */
75 tlv_packet[6] = EAP_TLV_ACK_RESULT;
77 tlv_packet[8] = 2; /* length of the data portion */
79 tlv_packet[10] = EAP_TLV_SUCCESS;
81 (tls_session->record_plus)(&tls_session->clean_in, tlv_packet, 11);
84 * FIXME: Check the return code.
86 tls_handshake_send(tls_session);
93 * Verify the tunneled EAP message.
95 static int eapmessage_verify(const uint8_t *data, unsigned int data_len)
97 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
101 if (!data || (data_len <= 1)) {
107 case PW_EAP_IDENTITY:
108 memcpy(identity, data + 1, data_len - 1);
109 identity[data_len - 1] = '\0';
110 DEBUG2(" rlm_eap_peap: Identity - %s", identity);
115 * If the first byte of the packet is
116 * EAP-Response, and the EAP data is a TLV,
117 * then it looks OK...
119 case PW_EAP_RESPONSE:
120 if (eap_packet->data[0] == PW_EAP_TLV) {
121 DEBUG2(" rlm_eap_peap: Received EAP-TLV response.");
124 DEBUG2(" rlm_eap_peap: Got something weird.");
129 * We normally do Microsoft MS-CHAPv2 (26), versus
130 * Cisco MS-CHAPv2 (29).
132 case PW_EAP_MSCHAPV2:
134 DEBUG2(" rlm_eap_peap: EAP type %s",
135 eaptype_type2name(eap_type,
136 identity, sizeof(identity)));
145 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
147 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
148 const uint8_t *data, unsigned int data_len)
150 VALUE_PAIR *vp = NULL;
153 * Sanity check this...
155 if (data_len + EAP_HEADER_LEN > MAX_STRING_LEN) {
156 radlog(L_ERR, "rlm_eap_peap: EAP Response packet is too large. Code must be fixed to handle this.");
160 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
162 DEBUG2(" rlm_eap_peap: Failure in creating VP");
167 * Hand-build an EAP packet from the crap in PEAP version 0.
169 vp->strvalue[0] = PW_EAP_RESPONSE;
170 vp->strvalue[1] = eap_ds->response->id;
172 vp->strvalue[3] = EAP_HEADER_LEN + data_len;
174 memcpy(vp->strvalue + EAP_HEADER_LEN, data, data_len);
175 vp->length = EAP_HEADER_LEN + data_len;
182 * Convert a list of VALUE_PAIR's to an EAP packet, through the
183 * simple expedient of dumping the EAP message
185 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
187 if (vp->next != NULL) {
188 radlog(L_ERR, "rlm_eap_peap: EAP Request packet is too large. Code must be fixed to handle this.");
193 * Skip the id, code, and length. Just write the EAP
194 * type & data to the client.
197 if (debug_flag > 2) {
199 int total = vp->length - 4;
201 if (debug_flag > 0) for (i = 0; i < total; i++) {
202 if ((i & 0x0f) == 0) printf(" PEAP tunnel data out %04x: ", i);
204 printf("%02x ", vp->strvalue[i + 4]);
206 if ((i & 0x0f) == 0x0f) printf("\n");
208 if ((total & 0x0f) != 0) printf("\n");
213 * Send the EAP data, WITHOUT the header.
216 (tls_session->record_plus)(&tls_session->clean_in, vp->strvalue + EAP_HEADER_LEN,
217 vp->length - EAP_HEADER_LEN);
219 (tls_session->record_plus)(&tls_session->clean_in, vp->strvalue, vp->length);
221 tls_handshake_send(tls_session);
228 * See if there's a TLV in the response.
230 static int eappeap_check_tlv(const uint8_t *data)
232 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
235 * Look for success or failure.
237 if ((eap_packet->code == PW_EAP_RESPONSE) &&
238 (eap_packet->data[0] == PW_EAP_TLV)) {
239 if (data[10] == EAP_TLV_SUCCESS) {
243 if (data[10] == EAP_TLV_FAILURE) {
244 DEBUG2(" rlm_eap_peap: Client rejected our response. The password is probably incorrect.");
254 * Use a reply packet to determine what to do.
256 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
257 UNUSED REQUEST *request, RADIUS_PACKET *reply)
259 int rcode = RLM_MODULE_REJECT;
261 peap_tunnel_t *t = tls_session->opaque;
264 if (debug_flag > 0) {
265 printf(" PEAP: Processing from tunneled session code %p %d\n",
268 for (vp = reply->vps; vp != NULL; vp = vp->next) {
269 putchar('\t');vp_print(stdout, vp);putchar('\n');
274 switch (reply->code) {
275 case PW_AUTHENTICATION_ACK:
276 DEBUG2(" PEAP: Tunneled authentication was successful.");
277 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
278 eappeap_success(handler, tls_session);
279 rcode = RLM_MODULE_HANDLED;
282 * If we've been told to use the attributes from
283 * the reply, then do so.
285 * WARNING: This may leak information about the
288 if (t->use_tunneled_reply) {
289 DEBUG2(" Saving tunneled attributes for later");
292 * Clean up the tunneled reply.
294 pairdelete(&reply->vps, PW_PROXY_STATE);
295 pairdelete(&reply->vps, PW_EAP_MESSAGE);
296 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
298 t->accept_vps = reply->vps;
303 case PW_AUTHENTICATION_REJECT:
304 DEBUG2(" PEAP: Tunneled authentication was rejected.");
305 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
306 eappeap_failure(handler, tls_session);
307 rcode = RLM_MODULE_HANDLED;
310 case PW_ACCESS_CHALLENGE:
311 DEBUG2(" PEAP: Got tunneled Access-Challenge");
314 * Keep the State attribute, if necessary.
316 * Get rid of the old State, too.
319 pairmove2(&t->state, &(reply->vps), PW_STATE);
322 * PEAP takes only EAP-Message attributes inside
323 * of the tunnel. Any Reply-Message in the
324 * Access-Challenge is ignored.
327 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
330 * Handle EAP-MSCHAP-V2, where Access-Accept's
331 * from the home server may contain MS-CHAP-Success,
332 * which the module turns into challenges, so that
333 * the client may respond to the challenge with
336 if (t->home_access_accept && t->use_tunneled_reply) {
337 DEBUG2(" Saving tunneled attributes for later");
340 * Clean up the tunneled reply.
342 pairdelete(&reply->vps, PW_PROXY_STATE);
343 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
345 t->accept_vps = reply->vps;
350 * Handle the ACK, by tunneling any necessary reply
351 * VP's back to the client.
354 vp2eap(tls_session, vp);
358 rcode = RLM_MODULE_HANDLED;
362 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
363 rcode = RLM_MODULE_REJECT;
372 * Do post-proxy processing,
374 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
377 tls_session_t *tls_session = (tls_session_t *) data;
380 DEBUG2(" PEAP: Passing reply from proxy back into the tunnel.");
383 * If there was a fake request associated with the proxied
384 * request, do more processing of it.
386 fake = (REQUEST *) request_data_get(handler->request,
387 handler->request->proxy,
388 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
391 * Do the callback, if it exists, and if it was a success.
393 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
395 REQUEST *request = handler->request;
396 peap_tunnel_t *t = tls_session->opaque;
398 t->home_access_accept = TRUE;
403 rad_assert(fake->packet == NULL);
404 fake->packet = request->proxy;
405 request->proxy = NULL;
407 rad_assert(fake->reply == NULL);
408 fake->reply = request->proxy_reply;
409 request->proxy_reply = NULL;
412 * Perform a post-auth stage, which will get the EAP
415 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
416 DEBUG2(" PEAP: Passing reply back for EAP-MS-CHAP-V2 %p %d",
417 fake, fake->reply->code);
418 rcode = module_post_proxy(0, fake);
421 * FIXME: If rcode returns fail, do something
424 DEBUG2(" POST-PROXY %d", rcode);
425 rcode = rad_postauth(fake);
426 DEBUG2(" POST-AUTH %d", rcode);
429 if (debug_flag > 0) {
430 printf(" PEAP: Final reply from tunneled session code %d\n",
433 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
434 putchar('\t');vp_print(stdout, vp);putchar('\n');
442 request->proxy = fake->packet;
444 request->proxy_reply = fake->reply;
448 * And we're done with this request.
452 case RLM_MODULE_FAIL:
454 eaptls_fail(handler->eap_ds, 0);
458 default: /* Don't Do Anything */
459 DEBUG2(" PEAP: Got reply %d",
460 request->proxy_reply->code);
464 request_free(&fake); /* robust if fake == NULL */
467 * If there was no EAP-Message in the reply packet, then
468 * we know that we're supposed to re-run the "authenticate"
469 * stage, in order to get the right kind of handling...
473 * Process the reply from the home server.
476 rcode = process_reply(handler, tls_session, handler->request,
477 handler->request->proxy_reply);
480 * The proxy code uses the reply from the home server as
481 * the basis for the reply to the NAS. We don't want that,
482 * so we toss it, after we've had our way with it.
484 pairfree(&handler->request->proxy_reply->vps);
487 case RLM_MODULE_REJECT:
488 DEBUG2(" PEAP: Reply was rejected");
489 eaptls_fail(handler->eap_ds, 0);
492 case RLM_MODULE_HANDLED:
493 DEBUG2(" PEAP: Reply was handled");
494 eaptls_request(handler->eap_ds, tls_session);
498 DEBUG2(" PEAP: Reply was OK");
499 eaptls_success(handler->eap_ds, 0);
500 eaptls_gen_mppe_keys(&handler->request->reply->vps,
502 "client EAP encryption");
506 DEBUG2(" PEAP: Reply was unknown.");
510 eaptls_fail(handler->eap_ds, 0);
517 static void my_request_free(void *data)
519 REQUEST *request = (REQUEST *)data;
521 request_free(&request);
526 * Process the pseudo-EAP contents of the tunneled data.
528 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
531 peap_tunnel_t *t = tls_session->opaque;
534 int rcode = RLM_MODULE_REJECT;
536 unsigned int data_len;
537 unsigned char buffer[1024];
542 REQUEST *request = handler->request;
543 EAP_DS *eap_ds = handler->eap_ds;
546 * Grab the dirty data, and copy it to our buffer.
548 * I *really* don't like these 'record_t' things...
550 data_len = (tls_session->record_minus)(&tls_session->dirty_in, buffer, sizeof(buffer));
554 * Write the data from the dirty buffer (i.e. packet
555 * data) into the buffer which we will give to SSL for
558 * Some of this code COULD technically go into the TLS
559 * module, in eaptls_process(), where it returns EAPTLS_OK.
561 * Similarly, the writing of data to the SSL context could
564 BIO_write(tls_session->into_ssl, buffer, data_len);
565 (tls_session->record_init)(&tls_session->clean_out);
568 * Read (and decrypt) the tunneled data from the SSL session,
569 * and put it into the decrypted data buffer.
571 err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
572 sizeof(tls_session->clean_out.data));
575 * FIXME: Call SSL_get_error() to see what went
578 radlog(L_INFO, "rlm_eap_peap: SSL_read Error");
579 return RLM_MODULE_REJECT;
583 * If there's no data, maybe this is an ACK to an
588 * FIXME: Call SSL_get_error() to see what went
591 radlog(L_INFO, "rlm_eap_peap: No data inside of the tunnel.");
592 return RLM_MODULE_REJECT;
595 data_len = tls_session->clean_out.used = err;
596 data = tls_session->clean_out.data;
599 if (debug_flag > 2) {
600 for (i = 0; i < data_len; i++) {
601 if ((i & 0x0f) == 0) printf(" PEAP tunnel data in %04x: ", i);
603 printf("%02x ", data[i]);
605 if ((i & 0x0f) == 0x0f) printf("\n");
607 if ((data_len & 0x0f) != 0) printf("\n");
611 if (!eapmessage_verify(data, data_len)) {
612 return RLM_MODULE_REJECT;
615 DEBUG2(" rlm_eap_peap: Tunneled data is valid.");
618 * If we authenticated the user, then it's OK.
620 if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
621 if (eappeap_check_tlv(data)) {
622 DEBUG2(" rlm_eap_peap: Success");
623 return RLM_MODULE_OK;
626 return RLM_MODULE_REJECT;
628 } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
629 DEBUG2(" rlm_eap_peap: Had sent TLV failure. User was rejcted rejected earlier in this session.");
630 return RLM_MODULE_REJECT;
633 fake = request_alloc_fake(request);
635 rad_assert(fake->packet->vps == NULL);
637 fake->packet->vps = eap2vp(eap_ds, data, data_len);
638 if (!fake->packet->vps) {
639 DEBUG2(" rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
640 return PW_AUTHENTICATION_REJECT;
644 if (debug_flag > 0) {
645 printf(" PEAP: Got tunneled EAP-Message\n");
647 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
648 putchar('\t');vp_print(stdout, vp);putchar('\n');
654 * Tell the request that it's a fake one.
656 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
658 pairadd(&fake->packet->vps, vp);
662 * Update other items in the REQUEST data structure.
666 * There's no User-Name in the tunneled session,
667 * so we add one here, by pulling it out of the
668 * EAP-Identity packet.
670 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
671 t->username = pairmake("User-Name", "", T_OP_EQ);
672 rad_assert(t->username != NULL);
674 memcpy(t->username->strvalue, data + 1, data_len - 1);
675 t->username->length = data_len - 1;
676 t->username->strvalue[t->username->length] = 0;
677 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->strvalue);
680 * If there's a default EAP type,
683 if (t->default_eap_type != 0) {
684 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
685 vp = pairmake("EAP-Type", "0", T_OP_EQ);
686 vp->lvalue = t->default_eap_type;
687 pairadd(&fake->config_items, vp);
690 } /* else there WAS a t->username */
693 vp = paircopy(t->username);
694 pairadd(&fake->packet->vps, vp);
695 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
696 DEBUG2(" PEAP: Setting User-Name to %s",
697 fake->username->strvalue);
701 * Add the State attribute, too, if it exists.
704 DEBUG2(" PEAP: Adding old state with %02x %02x",
705 t->state->strvalue[0], t->state->strvalue[1]);
706 vp = paircopy(t->state);
707 if (vp) pairadd(&fake->packet->vps, vp);
711 * If this is set, we copy SOME of the request attributes
712 * from outside of the tunnel to inside of the tunnel.
714 * We copy ONLY those attributes which do NOT already
715 * exist in the tunneled request.
717 * This code is copied from ../rlm_eap_ttls/ttls.c
719 if (t->copy_request_to_tunnel) {
722 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
724 * The attribute is a server-side thingy,
727 if ((vp->attribute > 255) &&
728 (((vp->attribute >> 16) & 0xffff) == 0)) {
733 * The outside attribute is already in the
734 * tunnel, don't copy it.
736 * This works for BOTH attributes which
737 * are originally in the tunneled request,
738 * AND attributes which are copied there
741 if (pairfind(fake->packet->vps, vp->attribute)) {
746 * Some attributes are handled specially.
748 switch (vp->attribute) {
750 * NEVER copy Message-Authenticator,
751 * EAP-Message, or State. They're
752 * only for outside of the tunnel.
755 case PW_USER_PASSWORD:
756 case PW_CHAP_PASSWORD:
757 case PW_CHAP_CHALLENGE:
759 case PW_MESSAGE_AUTHENTICATOR:
766 * By default, copy it over.
773 * Don't copy from the head, we've already
776 copy = paircopy2(vp, vp->attribute);
777 pairadd(&fake->packet->vps, copy);
782 if (debug_flag > 0) {
783 printf(" PEAP: Sending tunneled request\n");
785 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
786 putchar('\t');vp_print(stdout, vp);putchar('\n');
792 * Call authentication recursively, which will
793 * do PAP, CHAP, MS-CHAP, etc.
795 rad_authenticate(fake);
798 * Note that we don't do *anything* with the reply
802 if (debug_flag > 0) {
803 printf(" PEAP: Got tunneled reply RADIUS code %d\n",
806 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
807 putchar('\t');vp_print(stdout, vp);putchar('\n');
813 * Decide what to do with the reply.
815 switch (fake->reply->code) {
816 case 0: /* No reply code, must be proxied... */
817 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
820 eap_tunnel_data_t *tunnel;
823 * The tunneled request was NOT handled,
824 * it has to be proxied. This means that
825 * the "authenticate" stage was never
828 * If we are told to NOT proxy the
829 * tunneled request as EAP, then this
830 * means that we've got to decode it,
831 * which means that we MUST run the
832 * "authenticate" portion by hand, here.
834 * Once the tunneled EAP session is ALMOST
835 * done, THEN we proxy it...
837 if (!t->proxy_tunneled_request_as_eap) {
838 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
841 * Hmm... should we check for
842 * Auth-Type & EAP-Message here?
847 * Run the EAP authentication.
849 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
850 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
851 if (rcode == RLM_MODULE_OK) {
853 * Authentication succeeded! Rah!
855 fake->reply->code = PW_AUTHENTICATION_ACK;
859 if (rcode != RLM_MODULE_HANDLED) {
860 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
861 rcode = RLM_MODULE_REJECT;
866 * The module decided it wasn't
867 * done. Handle it like normal.
869 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
870 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->strvalue);
875 * The module has decoded the
876 * EAP-Message into another set
879 pairdelete(&fake->packet->vps,
883 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->strvalue);
886 * Tell the original request that it's going
889 pairmove2(&(request->config_items),
890 &(fake->config_items),
894 * Seed the proxy packet with the
897 rad_assert(request->proxy == NULL);
898 request->proxy = fake->packet;
900 rad_free(&fake->reply);
904 * Set up the callbacks for the tunnel
906 tunnel = rad_malloc(sizeof(*tunnel));
907 memset(tunnel, 0, sizeof(*tunnel));
909 tunnel->tls_session = tls_session;
910 tunnel->callback = eappeap_postproxy;
913 * Associate the callback with the request.
915 rcode = request_data_add(request,
917 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
919 rad_assert(rcode == 0);
922 * We're not proxying it as EAP, so we've got
923 * to do the callback later.
925 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
926 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
929 * rlm_eap.c has taken care of associating
930 * the handler with the fake request.
932 * So we associate the fake request with
935 rcode = request_data_add(request,
937 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
938 fake, my_request_free);
939 rad_assert(rcode == 0);
942 * Do NOT free the fake request!
944 return RLM_MODULE_UPDATED;
948 * Didn't authenticate the packet, but
951 rcode = RLM_MODULE_UPDATED;
954 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
955 rcode = RLM_MODULE_REJECT;
961 rcode = process_reply(handler, tls_session, request,