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>
26 * Send protected EAP-Failure
28 * Result-TLV = Failure
30 static int eappeap_failure(EAP_HANDLER *handler, tls_session_t *tls_session)
32 uint8_t tlv_packet[11];
34 DEBUG2(" rlm_eap_peap: FAILURE");
36 tlv_packet[0] = PW_EAP_REQUEST;
37 tlv_packet[1] = handler->eap_ds->response->id +1;
39 tlv_packet[3] = 11; /* length of this packet */
40 tlv_packet[4] = PW_EAP_TLV;
42 tlv_packet[6] = EAP_TLV_ACK_RESULT;
44 tlv_packet[8] = 2; /* length of the data portion */
46 tlv_packet[10] = EAP_TLV_FAILURE;
48 record_plus(&tls_session->clean_in, tlv_packet, 11);
51 * FIXME: Check the return code.
53 tls_handshake_send(tls_session);
54 record_init(&tls_session->clean_in);
61 * Send protected EAP-Success
63 * Result-TLV = Success
65 static int eappeap_success(EAP_HANDLER *handler, tls_session_t *tls_session)
67 uint8_t tlv_packet[11];
69 DEBUG2(" rlm_eap_peap: SUCCESS");
71 tlv_packet[0] = PW_EAP_REQUEST;
72 tlv_packet[1] = handler->eap_ds->response->id +1;
74 tlv_packet[3] = 11; /* length of this packet */
75 tlv_packet[4] = PW_EAP_TLV;
76 tlv_packet[5] = 0x80; /* mandatory AVP */
77 tlv_packet[6] = EAP_TLV_ACK_RESULT;
79 tlv_packet[8] = 2; /* length of the data portion */
81 tlv_packet[10] = EAP_TLV_SUCCESS;
83 record_plus(&tls_session->clean_in, tlv_packet, 11);
86 * FIXME: Check the return code.
88 tls_handshake_send(tls_session);
89 record_init(&tls_session->clean_in);
96 * Verify the tunneled EAP message.
98 static int eapmessage_verify(const uint8_t *data, unsigned int data_len)
100 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
104 if (!data || (data_len <= 1)) {
110 case PW_EAP_IDENTITY:
111 memcpy(identity, data + 1, data_len - 1);
112 identity[data_len - 1] = '\0';
113 DEBUG2(" rlm_eap_peap: Identity - %s", identity);
118 * If the first byte of the packet is
119 * EAP-Response, and the EAP data is a TLV,
120 * then it looks OK...
122 case PW_EAP_RESPONSE:
123 if (eap_packet->data[0] == PW_EAP_TLV) {
124 DEBUG2(" rlm_eap_peap: Received EAP-TLV response.");
127 DEBUG2(" rlm_eap_peap: Got something weird.");
132 * We normally do Microsoft MS-CHAPv2 (26), versus
133 * Cisco MS-CHAPv2 (29).
135 case PW_EAP_MSCHAPV2:
137 DEBUG2(" rlm_eap_peap: EAP type %s",
138 eaptype_type2name(eap_type,
139 identity, sizeof(identity)));
148 * Convert a pseudo-EAP packet to a list of VALUE_PAIR's.
150 static VALUE_PAIR *eap2vp(EAP_DS *eap_ds,
151 const uint8_t *data, unsigned int data_len)
153 VALUE_PAIR *vp = NULL;
156 * Sanity check this...
158 if (data_len + EAP_HEADER_LEN > MAX_STRING_LEN) {
159 radlog(L_ERR, "rlm_eap_peap: EAP Response packet is too large. Code must be fixed to handle this.");
163 vp = paircreate(PW_EAP_MESSAGE, PW_TYPE_OCTETS);
165 DEBUG2(" rlm_eap_peap: Failure in creating VP");
170 * Hand-build an EAP packet from the crap in PEAP version 0.
172 vp->strvalue[0] = PW_EAP_RESPONSE;
173 vp->strvalue[1] = eap_ds->response->id;
175 vp->strvalue[3] = EAP_HEADER_LEN + data_len;
177 memcpy(vp->strvalue + EAP_HEADER_LEN, data, data_len);
178 vp->length = EAP_HEADER_LEN + data_len;
185 * Convert a list of VALUE_PAIR's to an EAP packet, through the
186 * simple expedient of dumping the EAP message
188 static int vp2eap(tls_session_t *tls_session, VALUE_PAIR *vp)
190 if (vp->next != NULL) {
191 radlog(L_ERR, "rlm_eap_peap: EAP Request packet is too large. Code must be fixed to handle this.");
196 * Skip the id, code, and length. Just write the EAP
197 * type & data to the client.
200 if (debug_flag > 2) {
202 int total = vp->length - 4;
204 if (debug_flag > 0) for (i = 0; i < total; i++) {
205 if ((i & 0x0f) == 0) printf(" PEAP tunnel data out %04x: ", i);
207 printf("%02x ", vp->strvalue[i + 4]);
209 if ((i & 0x0f) == 0x0f) printf("\n");
211 if ((total & 0x0f) != 0) printf("\n");
216 * Send the EAP data, WITHOUT the header.
219 record_plus(&tls_session->clean_in, vp->strvalue + EAP_HEADER_LEN,
220 vp->length - EAP_HEADER_LEN);
222 record_plus(&tls_session->clean_in, vp->strvalue, vp->length);
224 tls_handshake_send(tls_session);
225 record_init(&tls_session->clean_in);
232 * See if there's a TLV in the response.
234 static int eappeap_check_tlv(const uint8_t *data)
236 const eap_packet_t *eap_packet = (const eap_packet_t *) data;
239 * Look for success or failure.
241 if ((eap_packet->code == PW_EAP_RESPONSE) &&
242 (eap_packet->data[0] == PW_EAP_TLV)) {
243 if (data[10] == EAP_TLV_SUCCESS) {
247 if (data[10] == EAP_TLV_FAILURE) {
248 DEBUG2(" rlm_eap_peap: Client rejected our response. The password is probably incorrect.");
258 * Use a reply packet to determine what to do.
260 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
261 REQUEST *request, RADIUS_PACKET *reply)
263 int rcode = RLM_MODULE_REJECT;
265 peap_tunnel_t *t = tls_session->opaque;
268 if (debug_flag > 0) {
269 printf(" PEAP: Processing from tunneled session code %p %d\n",
272 for (vp = reply->vps; vp != NULL; vp = vp->next) {
273 putchar('\t');vp_print(stdout, vp);putchar('\n');
278 switch (reply->code) {
279 case PW_AUTHENTICATION_ACK:
280 DEBUG2(" PEAP: Tunneled authentication was successful.");
281 t->status = PEAP_STATUS_SENT_TLV_SUCCESS;
282 eappeap_success(handler, tls_session);
283 rcode = RLM_MODULE_HANDLED;
286 * If we've been told to use the attributes from
287 * the reply, then do so.
289 * WARNING: This may leak information about the
292 if (t->use_tunneled_reply) {
293 DEBUG2(" Saving tunneled attributes for later");
296 * Clean up the tunneled reply.
298 pairdelete(&reply->vps, PW_PROXY_STATE);
299 pairdelete(&reply->vps, PW_EAP_MESSAGE);
300 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
302 t->accept_vps = reply->vps;
307 case PW_AUTHENTICATION_REJECT:
308 DEBUG2(" PEAP: Tunneled authentication was rejected.");
309 t->status = PEAP_STATUS_SENT_TLV_FAILURE;
310 eappeap_failure(handler, tls_session);
311 rcode = RLM_MODULE_HANDLED;
314 case PW_ACCESS_CHALLENGE:
315 DEBUG2(" PEAP: Got tunneled Access-Challenge");
318 * Keep the State attribute, if necessary.
320 * Get rid of the old State, too.
323 pairmove2(&t->state, &(reply->vps), PW_STATE);
326 * PEAP takes only EAP-Message attributes inside
327 * of the tunnel. Any Reply-Message in the
328 * Access-Challenge is ignored.
331 pairmove2(&vp, &(reply->vps), PW_EAP_MESSAGE);
334 * Handle EAP-MSCHAP-V2, where Access-Accept's
335 * from the home server may contain MS-CHAP-Success,
336 * which the module turns into challenges, so that
337 * the client may respond to the challenge with
340 if (t->home_access_accept && t->use_tunneled_reply) {
341 DEBUG2(" Saving tunneled attributes for later");
344 * Clean up the tunneled reply.
346 pairdelete(&reply->vps, PW_PROXY_STATE);
347 pairdelete(&reply->vps, PW_MESSAGE_AUTHENTICATOR);
349 t->accept_vps = reply->vps;
354 * Handle the ACK, by tunneling any necessary reply
355 * VP's back to the client.
358 vp2eap(tls_session, vp);
362 rcode = RLM_MODULE_HANDLED;
366 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
367 rcode = RLM_MODULE_REJECT;
376 * Do post-proxy processing,
378 static int eappeap_postproxy(EAP_HANDLER *handler, void *data)
381 tls_session_t *tls_session = (tls_session_t *) data;
384 DEBUG2(" PEAP: Passing reply from proxy back into the tunnel.");
387 * If there was a fake request associated with the proxied
388 * request, do more processing of it.
390 fake = (REQUEST *) request_data_get(handler->request,
391 handler->request->proxy,
392 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
395 * Do the callback, if it exists, and if it was a success.
397 if (fake && (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
399 REQUEST *request = handler->request;
400 peap_tunnel_t *t = tls_session->opaque;
402 t->home_access_accept = TRUE;
407 rad_assert(fake->packet == NULL);
408 fake->packet = request->proxy;
409 request->proxy = NULL;
411 rad_assert(fake->reply == NULL);
412 fake->reply = request->proxy_reply;
413 request->proxy_reply = NULL;
416 * Perform a post-auth stage, which will get the EAP
419 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
420 DEBUG2(" PEAP: Passing reply back for EAP-MS-CHAP-V2 %p %d",
421 fake, fake->reply->code);
422 rcode = module_post_proxy(0, fake);
425 * FIXME: If rcode returns fail, do something
428 DEBUG2(" POST-PROXY %d", rcode);
429 rcode = rad_postauth(fake);
430 DEBUG2(" POST-AUTH %d", rcode);
433 if (debug_flag > 0) {
434 printf(" PEAP: Final reply from tunneled session code %d\n",
437 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
438 putchar('\t');vp_print(stdout, vp);putchar('\n');
446 request->proxy = fake->packet;
448 request->proxy_reply = fake->reply;
452 * And we're done with this request.
456 case RLM_MODULE_FAIL:
458 eaptls_fail(handler->eap_ds, 0);
462 default: /* Don't Do Anything */
463 DEBUG2(" PEAP: Got reply %d",
464 request->proxy_reply->code);
468 request_free(&fake); /* robust if fake == NULL */
471 * If there was no EAP-Message in the reply packet, then
472 * we know that we're supposed to re-run the "authenticate"
473 * stage, in order to get the right kind of handling...
477 * Process the reply from the home server.
480 rcode = process_reply(handler, tls_session, handler->request,
481 handler->request->proxy_reply);
484 * The proxy code uses the reply from the home server as
485 * the basis for the reply to the NAS. We don't want that,
486 * so we toss it, after we've had our way with it.
488 pairfree(&handler->request->proxy_reply->vps);
491 case RLM_MODULE_REJECT:
492 DEBUG2(" PEAP: Reply was rejected");
493 eaptls_fail(handler->eap_ds, 0);
496 case RLM_MODULE_HANDLED:
497 DEBUG2(" PEAP: Reply was handled");
498 eaptls_request(handler->eap_ds, tls_session);
502 DEBUG2(" PEAP: Reply was OK");
503 eaptls_success(handler->eap_ds, 0);
504 eaptls_gen_mppe_keys(&handler->request->reply->vps,
506 "client EAP encryption");
510 DEBUG2(" PEAP: Reply was unknown.");
514 eaptls_fail(handler->eap_ds, 0);
521 static void my_request_free(void *data)
523 REQUEST *request = (REQUEST *)data;
525 request_free(&request);
530 * Process the pseudo-EAP contents of the tunneled data.
532 int eappeap_process(EAP_HANDLER *handler, tls_session_t *tls_session)
535 peap_tunnel_t *t = tls_session->opaque;
538 int rcode = RLM_MODULE_REJECT;
540 unsigned int data_len;
541 unsigned char buffer[1024];
546 REQUEST *request = handler->request;
547 EAP_DS *eap_ds = handler->eap_ds;
550 * Grab the dirty data, and copy it to our buffer.
552 * I *really* don't like these 'record_t' things...
554 data_len = record_minus(&tls_session->dirty_in, buffer, sizeof(buffer));
558 * Write the data from the dirty buffer (i.e. packet
559 * data) into the buffer which we will give to SSL for
562 * Some of this code COULD technically go into the TLS
563 * module, in eaptls_process(), where it returns EAPTLS_OK.
565 * Similarly, the writing of data to the SSL context could
568 BIO_write(tls_session->into_ssl, buffer, data_len);
569 record_init(&tls_session->clean_out);
572 * Read (and decrypt) the tunneled data from the SSL session,
573 * and put it into the decrypted data buffer.
575 err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
576 sizeof(tls_session->clean_out.data));
579 * FIXME: Call SSL_get_error() to see what went
582 radlog(L_INFO, "rlm_eap_peap: SSL_read Error");
583 return RLM_MODULE_REJECT;
587 * If there's no data, maybe this is an ACK to an
592 * FIXME: Call SSL_get_error() to see what went
595 radlog(L_INFO, "rlm_eap_peap: No data inside of the tunnel.");
596 return RLM_MODULE_REJECT;
599 data_len = tls_session->clean_out.used = err;
600 data = tls_session->clean_out.data;
603 if (debug_flag > 2) {
604 for (i = 0; i < data_len; i++) {
605 if ((i & 0x0f) == 0) printf(" PEAP tunnel data in %04x: ", i);
607 printf("%02x ", data[i]);
609 if ((i & 0x0f) == 0x0f) printf("\n");
611 if ((data_len & 0x0f) != 0) printf("\n");
615 if (!eapmessage_verify(data, data_len)) {
616 return RLM_MODULE_REJECT;
619 DEBUG2(" rlm_eap_peap: Tunneled data is valid.");
622 * If we authenticated the user, then it's OK.
624 if (t->status == PEAP_STATUS_SENT_TLV_SUCCESS) {
625 if (eappeap_check_tlv(data)) {
626 DEBUG2(" rlm_eap_peap: Success");
627 return RLM_MODULE_OK;
630 return RLM_MODULE_REJECT;
632 } else if (t->status == PEAP_STATUS_SENT_TLV_FAILURE) {
633 DEBUG2(" rlm_eap_peap: Had sent TLV failure. User was rejcted rejected earlier in this session.");
634 return RLM_MODULE_REJECT;
637 fake = request_alloc_fake(request);
639 rad_assert(fake->packet->vps == NULL);
641 fake->packet->vps = eap2vp(eap_ds, data, data_len);
642 if (!fake->packet->vps) {
643 DEBUG2(" rlm_eap_peap: Unable to convert tunneled EAP packet to internal server data structures");
644 return PW_AUTHENTICATION_REJECT;
648 if (debug_flag > 0) {
649 printf(" PEAP: Got tunneled EAP-Message\n");
651 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
652 putchar('\t');vp_print(stdout, vp);putchar('\n');
658 * Tell the request that it's a fake one.
660 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
662 pairadd(&fake->packet->vps, vp);
666 * Update other items in the REQUEST data structure.
670 * There's no User-Name in the tunneled session,
671 * so we add one here, by pulling it out of the
672 * EAP-Identity packet.
674 if ((data[0] == PW_EAP_IDENTITY) && (data_len > 1)) {
675 t->username = pairmake("User-Name", "", T_OP_EQ);
676 rad_assert(t->username != NULL);
678 memcpy(t->username->strvalue, data + 1, data_len - 1);
679 t->username->length = data_len - 1;
680 t->username->strvalue[t->username->length] = 0;
681 DEBUG2(" PEAP: Got tunneled identity of %s", t->username->strvalue);
684 * If there's a default EAP type,
687 if (t->default_eap_type != 0) {
688 DEBUG2(" PEAP: Setting default EAP type for tunneled EAP session.");
689 vp = pairmake("EAP-Type", "0", T_OP_EQ);
690 vp->lvalue = t->default_eap_type;
691 pairadd(&fake->config_items, vp);
694 } /* else there WAS a t->username */
697 vp = paircopy(t->username);
698 pairadd(&fake->packet->vps, vp);
699 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
700 DEBUG2(" PEAP: Setting User-Name to %s",
701 fake->username->strvalue);
705 * Add the State attribute, too, if it exists.
708 DEBUG2(" PEAP: Adding old state with %02x %02x",
709 t->state->strvalue[0], t->state->strvalue[1]);
710 vp = paircopy(t->state);
711 if (vp) pairadd(&fake->packet->vps, vp);
715 * If this is set, we copy SOME of the request attributes
716 * from outside of the tunnel to inside of the tunnel.
718 * We copy ONLY those attributes which do NOT already
719 * exist in the tunneled request.
721 * This code is copied from ../rlm_eap_ttls/ttls.c
723 if (t->copy_request_to_tunnel) {
726 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
728 * The attribute is a server-side thingy,
731 if ((vp->attribute > 255) &&
732 (((vp->attribute >> 16) & 0xffff) == 0)) {
737 * The outside attribute is already in the
738 * tunnel, don't copy it.
740 * This works for BOTH attributes which
741 * are originally in the tunneled request,
742 * AND attributes which are copied there
745 if (pairfind(fake->packet->vps, vp->attribute)) {
750 * Some attributes are handled specially.
752 switch (vp->attribute) {
754 * NEVER copy Message-Authenticator,
755 * EAP-Message, or State. They're
756 * only for outside of the tunnel.
759 case PW_USER_PASSWORD:
760 case PW_CHAP_PASSWORD:
761 case PW_CHAP_CHALLENGE:
763 case PW_MESSAGE_AUTHENTICATOR:
770 * By default, copy it over.
777 * Don't copy from the head, we've already
780 copy = paircopy2(vp, vp->attribute);
781 pairadd(&fake->packet->vps, copy);
786 if (debug_flag > 0) {
787 printf(" PEAP: Sending tunneled request\n");
789 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
790 putchar('\t');vp_print(stdout, vp);putchar('\n');
796 * Call authentication recursively, which will
797 * do PAP, CHAP, MS-CHAP, etc.
799 rad_authenticate(fake);
802 * Note that we don't do *anything* with the reply
806 if (debug_flag > 0) {
807 printf(" PEAP: Got tunneled reply RADIUS code %d\n",
810 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
811 putchar('\t');vp_print(stdout, vp);putchar('\n');
817 * Decide what to do with the reply.
819 switch (fake->reply->code) {
820 case 0: /* No reply code, must be proxied... */
821 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
824 eap_tunnel_data_t *tunnel;
827 * The tunneled request was NOT handled,
828 * it has to be proxied. This means that
829 * the "authenticate" stage was never
832 * If we are told to NOT proxy the
833 * tunneled request as EAP, then this
834 * means that we've got to decode it,
835 * which means that we MUST run the
836 * "authenticate" portion by hand, here.
838 * Once the tunneled EAP session is ALMOST
839 * done, THEN we proxy it...
841 if (!t->proxy_tunneled_request_as_eap) {
842 fake->options |= RAD_REQUEST_OPTION_PROXY_EAP;
845 * Hmm... should we check for
846 * Auth-Type & EAP-Message here?
851 * Run the EAP authentication.
853 DEBUG2(" PEAP: Calling authenticate in order to initiate tunneled EAP session.");
854 rcode = module_authenticate(PW_AUTHTYPE_EAP, fake);
855 if (rcode == RLM_MODULE_OK) {
857 * Authentication succeeded! Rah!
859 fake->reply->code = PW_AUTHENTICATION_ACK;
863 if (rcode != RLM_MODULE_HANDLED) {
864 DEBUG2(" PEAP: Can't handle the return code %d", rcode);
865 rcode = RLM_MODULE_REJECT;
870 * The module decided it wasn't
871 * done. Handle it like normal.
873 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) == 0) {
874 DEBUG2(" PEAP: Cancelling proxy to realm %s until the tunneled EAP session has been established", vp->strvalue);
879 * The module has decoded the
880 * EAP-Message into another set
883 pairdelete(&fake->packet->vps,
887 DEBUG2(" PEAP: Tunneled authentication will be proxied to %s", vp->strvalue);
890 * Tell the original request that it's going
893 pairmove2(&(request->config_items),
894 &(fake->config_items),
898 * Seed the proxy packet with the
901 rad_assert(request->proxy == NULL);
902 request->proxy = fake->packet;
904 rad_free(&fake->reply);
908 * Set up the callbacks for the tunnel
910 tunnel = rad_malloc(sizeof(*tunnel));
911 memset(tunnel, 0, sizeof(*tunnel));
913 tunnel->tls_session = tls_session;
914 tunnel->callback = eappeap_postproxy;
917 * Associate the callback with the request.
919 rcode = request_data_add(request,
921 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
923 rad_assert(rcode == 0);
926 * We're not proxying it as EAP, so we've got
927 * to do the callback later.
929 if ((fake->options & RAD_REQUEST_OPTION_PROXY_EAP) != 0) {
930 DEBUG2(" PEAP: Remembering to do EAP-MS-CHAP-V2 post-proxy.");
933 * rlm_eap.c has taken care of associating
934 * the handler with the fake request.
936 * So we associate the fake request with
939 rcode = request_data_add(request,
941 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
942 fake, my_request_free);
943 rad_assert(rcode == 0);
946 * Do NOT free the fake request!
948 return RLM_MODULE_UPDATED;
952 * Didn't authenticate the packet, but
955 rcode = RLM_MODULE_UPDATED;
958 DEBUG2(" PEAP: Unknown RADIUS packet type %d: rejecting tunneled user", fake->reply->code);
959 rcode = RLM_MODULE_REJECT;
965 rcode = process_reply(handler, tls_session, request,