2 * rlm_eap_ttls.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>
28 #include "eap_chbind.h"
32 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * |V M r r r r r r| AVP Length |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * Verify that the diameter packet is valid.
47 static int diameter_verify(REQUEST *request,
48 const uint8_t *data, unsigned int data_len)
53 unsigned int remaining = data_len;
55 while (remaining > 0) {
58 if (remaining < hdr_len) {
59 RDEBUG2(" Diameter attribute is too small (%u) to contain a Diameter header", remaining);
63 memcpy(&attr, data, sizeof(attr));
65 memcpy(&length, data + 4, sizeof(length));
66 length = ntohl(length);
68 if ((data[4] & 0x80) != 0) {
70 RDEBUG2(" Diameter attribute is too small to contain a Diameter header with Vendor-Id");
78 * Get the length. If it's too big, die.
83 * Too short or too long is bad.
85 if (length <= (hdr_len - 4)) {
86 RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr, length, hdr_len);
90 if (length > remaining) {
91 RDEBUG2("Tunneled attribute %u is longer than room remaining in the packet (%u > %u).", attr, length, remaining);
96 * Check for broken implementations, which don't
97 * pad the AVP to a 4-octet boundary.
99 if (remaining == length) break;
102 * The length does NOT include the padding, so
103 * we've got to account for it here by rounding up
104 * to the nearest 4-byte boundary.
110 * If the rest of the diameter packet is larger than
111 * this attribute, continue.
113 * Otherwise, if the attribute over-flows the end
114 * of the packet, die.
116 if (remaining < length) {
117 RDEBUG2E("Diameter attribute overflows packet!");
122 * remaining > length, continue.
129 * We got this far. It looks OK.
136 * Convert diameter attributes to our VALUE_PAIR's
138 static VALUE_PAIR *diameter2vp(REQUEST *request, SSL *ssl,
139 const uint8_t *data, size_t data_len)
146 size_t data_left = data_len;
147 VALUE_PAIR *first = NULL;
148 VALUE_PAIR **last = &first;
151 while (data_left > 0) {
152 rad_assert(data_left <= data_len);
153 memcpy(&attr, data, sizeof(attr));
158 memcpy(&length, data, sizeof(length));
160 length = ntohl(length);
163 * A "vendor" flag, with a vendor ID of zero,
164 * is equivalent to no vendor. This is stupid.
167 if ((length & (1 << 31)) != 0) {
168 memcpy(&vendor, data, sizeof(vendor));
169 vendor = ntohl(vendor);
171 data += 4; /* skip the vendor field, it's zero */
172 offset += 4; /* offset to value field */
174 if (attr > 65535) goto next_attr;
175 if (vendor > FR_MAX_VENDOR) goto next_attr;
179 * FIXME: Handle the M bit. For now, we assume that
180 * some other module takes care of any attribute
181 * with the M bit set.
187 length &= 0x00ffffff;
190 * Get the size of the value portion of the
193 size = length - offset;
196 * Vendor attributes can be larger than 255.
197 * Normal attributes cannot be.
199 if ((attr > 255) && (vendor == 0)) {
200 RDEBUG2W("Skipping Diameter attribute %u",
206 * EAP-Message AVPs can be larger than 253 octets.
208 if ((size > 253) && !((vendor == 0) && (attr == PW_EAP_MESSAGE))) {
209 RDEBUG2W("diameter2vp skipping long attribute %u", attr);
214 * RADIUS VSAs are handled as Diameter attributes
215 * with Vendor-Id == 0, and the VSA data packed
216 * into the "String" field as per normal.
218 * EXCEPT for the MS-CHAP attributes.
220 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
224 buffer[0] = PW_VENDOR_SPECIFIC;
225 buffer[1] = size + 2;
226 memcpy(buffer + 2, data, size);
229 decoded = rad_attr2vp(NULL, NULL, NULL,
230 buffer, size + 2, &vp);
232 RDEBUG2E("diameter2vp failed decoding attr: %s",
237 if ((size_t) decoded != size + 2) {
238 RDEBUG2E("diameter2vp failed to entirely decode VSA");
247 } while (vp != NULL);
253 * Create it. If this fails, it's because we're OOM.
256 vp = paircreate(attr, vendor);
258 RDEBUG2("Failure in creating VP");
262 if (vendor == VENDORPEC_UKERNA) {
263 RDEBUG("Received UKERNA attr %d!", attr);
267 * If it's a type from our dictionary, then
268 * we need to put the data in a relevant place.
270 switch (vp->da->type) {
271 case PW_TYPE_INTEGER:
273 if (size != vp->length) {
277 * Bad format. Create a "raw"
281 if (vp) pairfree(&vp);
282 da = dict_attrunknown(attr, vendor, TRUE);
283 if (!da) return NULL;
284 vp = pairalloc(NULL, da);
285 if (size >= 253) size = 253;
287 memcpy(vp->vp_octets, data, vp->length);
290 memcpy(&vp->vp_integer, data, vp->length);
293 * Stored in host byte order: change it.
295 vp->vp_integer = ntohl(vp->vp_integer);
298 case PW_TYPE_INTEGER64:
299 if (size != vp->length) goto raw;
300 memcpy(&vp->vp_integer64, data, vp->length);
303 * Stored in host byte order: change it.
305 vp->vp_integer64 = ntohll(vp->vp_integer64);
309 if (size != vp->length) {
310 RDEBUG2("Invalid length attribute %d",
316 memcpy(&vp->vp_ipaddr, data, vp->length);
319 * Stored in network byte order: don't change it.
324 if (size != vp->length) goto raw;
325 vp->vp_integer = data[0];
329 if (size != vp->length) goto raw;
330 vp->vp_integer = (data[0] * 256) + data[1];
334 if (size != vp->length) goto raw;
335 memcpy(&vp->vp_signed, data, vp->length);
336 vp->vp_signed = ntohl(vp->vp_signed);
339 case PW_TYPE_IPV6ADDR:
340 if (size != vp->length) goto raw;
341 memcpy(&vp->vp_ipv6addr, data, vp->length);
344 case PW_TYPE_IPV6PREFIX:
345 if (size != vp->length) goto raw;
346 memcpy(&vp->vp_ipv6prefix, data, vp->length);
350 * String, octet, etc. Copy the data from the
351 * value field over verbatim.
354 if (attr == PW_EAP_MESSAGE) {
355 const uint8_t *eap_message = data;
358 * vp exists the first time around.
362 if (vp->length > 253) vp->length = 253;
363 memcpy(vp->vp_octets, eap_message,
367 eap_message += vp->length;
372 if (size == 0) break;
374 vp = paircreate(attr, vendor);
376 RDEBUG2("Failure in creating VP");
383 } /* else it's another kind of attribute */
387 if (size >= 253) size = 253;
389 memcpy(vp->vp_strvalue, data, vp->length);
394 * User-Password is NUL padded to a multiple
395 * of 16 bytes. Let's chop it to something
398 * NOTE: This means that the User-Password
399 * attribute CANNOT EVER have embedded zeros in it!
401 if ((vp->da->vendor == 0) && (vp->da->attr == PW_USER_PASSWORD)) {
403 * If the password is exactly 16 octets,
404 * it won't be zero-terminated.
406 vp->vp_strvalue[vp->length] = '\0';
407 vp->length = strlen(vp->vp_strvalue);
411 * Ensure that the client is using the
412 * correct challenge. This weirdness is
413 * to protect against against replay
414 * attacks, where anyone observing the
415 * CHAP exchange could pose as that user,
416 * by simply choosing to use the same
419 * By using a challenge based on
420 * information from the current session,
421 * we can guarantee that the client is
422 * not *choosing* a challenge.
424 * We're a little forgiving in that we
425 * have loose checks on the length, and
426 * we do NOT check the Id (first octet of
427 * the response to the challenge)
429 * But if the client gets the challenge correct,
430 * we're not too worried about the Id.
432 if (((vp->da->vendor == 0) && (vp->da->attr == PW_CHAP_CHALLENGE)) ||
433 ((vp->da->vendor == VENDORPEC_MICROSOFT) && (vp->da->attr == PW_MSCHAP_CHALLENGE))) {
434 uint8_t challenge[16];
436 if ((vp->length < 8) ||
438 RDEBUG("Tunneled challenge has invalid length");
444 eapttls_gen_challenge(ssl, challenge,
447 if (memcmp(challenge, vp->vp_octets,
449 RDEBUG("Tunneled challenge is incorrect");
464 * Catch non-aligned attributes.
466 if (data_left == length) break;
469 * The length does NOT include the padding, so
470 * we've got to account for it here by rounding up
471 * to the nearest 4-byte boundary.
476 rad_assert(data_left >= length);
478 data += length - offset; /* already updated */
482 * We got this far. It looks OK.
488 * Convert VALUE_PAIR's to diameter attributes, and write them
491 * The ONLY VALUE_PAIR's which may be passed to this function
492 * are ones which can go inside of a RADIUS (i.e. diameter)
493 * packet. So no server-configuration attributes, or the like.
495 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
498 * RADIUS packets are no more than 4k in size, so if
499 * we've got more than 4k of data to write, it's very
502 uint8_t buffer[4096];
514 for (vp = first; vp != NULL; vp = vp->next) {
516 * Too much data: die.
518 if ((total + vp->length + 12) >= sizeof(buffer)) {
519 RDEBUG2("output buffer is full!");
524 * Hmm... we don't group multiple EAP-Messages
525 * together. Maybe we should...
529 * Length is no more than 253, due to RADIUS
533 vendor = vp->da->vendor;
535 attr = vp->da->attr & 0xffff;
542 * Hmm... set the M bit for all attributes?
548 memcpy(p, &attr, sizeof(attr));
552 length += 8; /* includes 8 bytes of attr & length */
555 length += 4; /* include 4 bytes of vendor */
557 length = ntohl(length);
558 memcpy(p, &length, sizeof(length));
562 vendor = ntohl(vendor);
563 memcpy(p, &vendor, sizeof(vendor));
567 length = ntohl(length);
568 memcpy(p, &length, sizeof(length));
573 switch (vp->da->type) {
574 case PW_TYPE_INTEGER:
576 attr = htonl(vp->vp_integer); /* stored in host order */
577 memcpy(p, &attr, sizeof(attr));
581 case PW_TYPE_INTEGER64:
582 attr64 = htonll(vp->vp_integer64); /* stored in host order */
583 memcpy(p, &attr64, sizeof(attr64));
588 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
595 memcpy(p, vp->vp_strvalue, vp->length);
601 * Skip to the end of the data.
607 * Align the data to a multiple of 4 bytes.
609 if ((total & 0x03) != 0) {
612 length = 4 - (total & 0x03);
613 for (i = 0; i < length; i++) {
619 } /* loop over the VP's to write. */
622 * Write the data in the buffer to the SSL session.
628 if ((debug_flag > 2) && fr_log_fp) {
629 for (i = 0; i < total; i++) {
630 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
632 fprintf(fr_log_fp, "%02x ", buffer[i]);
634 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
636 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
640 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
643 * FIXME: Check the return code.
645 tls_handshake_send(request, tls_session);
655 * Use a reply packet to determine what to do.
657 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
658 REQUEST *request, RADIUS_PACKET *reply)
660 int rcode = RLM_MODULE_REJECT;
662 ttls_tunnel_t *t = tls_session->opaque;
664 handler = handler; /* -Wunused */
665 rad_assert(request != NULL);
666 rad_assert(handler->request == request);
669 * If the response packet was Access-Accept, then
670 * we're OK. If not, die horribly.
672 * FIXME: Take MS-CHAP2-Success attribute, and
673 * tunnel it back to the client, to authenticate
674 * ourselves to the client.
676 * FIXME: If we have an Access-Challenge, then
677 * the Reply-Message is tunneled back to the client.
679 * FIXME: If we have an EAP-Message, then that message
680 * must be tunneled back to the client.
682 * FIXME: If we have an Access-Challenge with a State
683 * attribute, then do we tunnel that to the client, or
684 * keep track of it ourselves?
686 * FIXME: EAP-Messages can only start with 'identity',
687 * NOT 'eap start', so we should check for that....
689 switch (reply->code) {
690 case PW_AUTHENTICATION_ACK:
691 RDEBUG("Got tunneled Access-Accept");
693 rcode = RLM_MODULE_OK;
696 * MS-CHAP2-Success means that we do NOT return
697 * an Access-Accept, but instead tunnel that
698 * attribute to the client, and keep going with
699 * the TTLS session. Once the client accepts
700 * our identity, it will respond with an empty
701 * packet, and we will send EAP-Success.
704 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT, TAG_ANY);
706 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
707 rcode = RLM_MODULE_HANDLED;
708 t->authenticated = TRUE;
711 * Delete MPPE keys & encryption policy. We don't
714 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
715 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
716 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
717 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
720 * Use the tunneled reply, but not now.
722 if (t->use_tunneled_reply) {
723 t->accept_vps = reply->vps;
727 } else { /* no MS-CHAP2-Success */
729 * Can only have EAP-Message if there's
730 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
732 * We also do NOT tunnel the EAP-Success
733 * attribute back to the client, as the client
734 * can figure it out, from the non-tunneled
735 * EAP-Success packet.
737 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
741 /* move channel binding responses; we need to send them */
742 pairmove2(&vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA, TAG_ANY);
745 * Handle the ACK, by tunneling any necessary reply
746 * VP's back to the client.
749 RDEBUG("sending tunneled reply attributes");
751 RDEBUG("end tunneled reply attributes");
752 vp2diameter(request, tls_session, vp);
757 * If we've been told to use the attributes from
758 * the reply, then do so.
760 * WARNING: This may leak information about the
763 if (t->use_tunneled_reply) {
764 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
765 pairadd(&request->reply->vps, reply->vps);
771 case PW_AUTHENTICATION_REJECT:
772 RDEBUG("Got tunneled Access-Reject");
773 rcode = RLM_MODULE_REJECT;
777 * Handle Access-Challenge, but only if we
778 * send tunneled reply data. This is because
779 * an Access-Challenge means that we MUST tunnel
780 * a Reply-Message to the client.
782 case PW_ACCESS_CHALLENGE:
783 RDEBUG("Got tunneled Access-Challenge");
786 * Keep the State attribute, if necessary.
788 * Get rid of the old State, too.
791 pairmove2(&t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
794 * We should really be a bit smarter about this,
795 * and move over only those attributes which
796 * are relevant to the authentication request,
797 * but that's a lot more work, and this "dumb"
798 * method works in 99.9% of the situations.
801 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
804 * There MUST be a Reply-Message in the challenge,
805 * which we tunnel back to the client.
807 * If there isn't one in the reply VP's, then
808 * we MUST create one, with an empty string as
811 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
813 /* also move chbind messages, if any */
814 pairmove2(&vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA,
818 * Handle the ACK, by tunneling any necessary reply
819 * VP's back to the client.
822 vp2diameter(request, tls_session, vp);
825 rcode = RLM_MODULE_HANDLED;
829 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
830 rcode = RLM_MODULE_INVALID;
840 * Do post-proxy processing,
842 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
845 tls_session_t *tls_session = (tls_session_t *) data;
846 REQUEST *fake, *request = handler->request;
848 rad_assert(request != NULL);
849 RDEBUG("Passing reply from proxy back into the tunnel.");
852 * If there was a fake request associated with the proxied
853 * request, do more processing of it.
855 fake = (REQUEST *) request_data_get(handler->request,
856 handler->request->proxy,
857 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
860 * Do the callback, if it exists, and if it was a success.
863 handler->request->proxy_reply &&
864 (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
868 rad_assert(fake->packet == NULL);
869 fake->packet = request->proxy;
870 fake->packet->src_ipaddr = request->packet->src_ipaddr;
871 request->proxy = NULL;
873 rad_assert(fake->reply == NULL);
874 fake->reply = request->proxy_reply;
875 request->proxy_reply = NULL;
877 if ((debug_flag > 0) && fr_log_fp) {
878 fprintf(fr_log_fp, "server %s {\n",
879 (fake->server == NULL) ? "" : fake->server);
883 * Perform a post-auth stage for the tunneled
886 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
887 rcode = rad_postauth(fake);
888 RDEBUG2("post-auth returns %d", rcode);
890 if ((debug_flag > 0) && fr_log_fp) {
891 fprintf(fr_log_fp, "} # server %s\n",
892 (fake->server == NULL) ? "" : fake->server);
894 RDEBUG("Final reply from tunneled session code %d",
896 debug_pair_list(fake->reply->vps);
902 request->proxy = fake->packet;
904 request->proxy_reply = fake->reply;
908 * And we're done with this request.
912 case RLM_MODULE_FAIL:
914 eaptls_fail(handler, 0);
918 default: /* Don't Do Anything */
919 RDEBUG2("Got reply %d",
920 request->proxy_reply->code);
924 request_free(&fake); /* robust if fake == NULL */
927 * Process the reply from the home server.
929 rcode = process_reply(handler, tls_session, handler->request,
930 handler->request->proxy_reply);
933 * The proxy code uses the reply from the home server as
934 * the basis for the reply to the NAS. We don't want that,
935 * so we toss it, after we've had our way with it.
937 pairfree(&handler->request->proxy_reply->vps);
940 case RLM_MODULE_REJECT:
941 RDEBUG("Reply was rejected");
944 case RLM_MODULE_HANDLED:
945 RDEBUG("Reply was handled");
946 eaptls_request(handler->eap_ds, tls_session);
950 RDEBUG("Reply was OK");
953 * Success: Automatically return MPPE keys.
955 return eaptls_success(handler, 0);
958 RDEBUG("Reply was unknown.");
962 eaptls_fail(handler, 0);
970 static void my_request_free(void *data)
972 REQUEST *request = (REQUEST *)data;
974 request_free(&request);
976 #endif /* WITH_PROXY */
979 * Process the "diameter" contents of the tunneled data.
981 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
983 int rcode = PW_AUTHENTICATION_REJECT;
989 REQUEST *request = handler->request;
990 eap_chbind_packet_t *chbind_packet;
993 rad_assert(request != NULL);
996 * Just look at the buffer directly, without doing
999 data_len = tls_session->clean_out.used;
1000 tls_session->clean_out.used = 0;
1001 data = tls_session->clean_out.data;
1003 t = (ttls_tunnel_t *) tls_session->opaque;
1006 * If there's no data, maybe this is an ACK to an
1009 if (data_len == 0) {
1010 if (t->authenticated) {
1011 RDEBUG("Got ACK, and the user was already authenticated.");
1012 return PW_AUTHENTICATION_ACK;
1013 } /* else no session, no data, die. */
1016 * FIXME: Call SSL_get_error() to see what went
1019 RDEBUG2("SSL_read Error");
1020 return PW_AUTHENTICATION_REJECT;
1024 if ((debug_flag > 2) && fr_log_fp) {
1027 for (i = 0; i < data_len; i++) {
1028 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
1030 fprintf(fr_log_fp, "%02x ", data[i]);
1032 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
1034 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
1038 if (!diameter_verify(request, data, data_len)) {
1039 return PW_AUTHENTICATION_REJECT;
1043 * Allocate a fake REQUEST structe.
1045 fake = request_alloc_fake(request);
1047 rad_assert(fake->packet->vps == NULL);
1050 * Add the tunneled attributes to the fake request.
1052 fake->packet->vps = diameter2vp(request, tls_session->ssl, data, data_len);
1053 if (!fake->packet->vps) {
1054 request_free(&fake);
1055 return PW_AUTHENTICATION_REJECT;
1059 * Tell the request that it's a fake one.
1061 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1063 pairadd(&fake->packet->vps, vp);
1066 if ((debug_flag > 0) && fr_log_fp) {
1067 RDEBUG("Got tunneled request");
1069 debug_pair_list(fake->packet->vps);
1073 * Update other items in the REQUEST data structure.
1075 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1076 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1079 * No User-Name, try to create one from stored data.
1081 if (!fake->username) {
1083 * No User-Name in the stored data, look for
1084 * an EAP-Identity, and pull it out of there.
1087 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1089 (vp->length >= EAP_HEADER_LEN + 2) &&
1090 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1091 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1092 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1094 * Create & remember a User-Name
1096 t->username = pairmake("User-Name", "", T_OP_EQ);
1097 rad_assert(t->username != NULL);
1099 memcpy(t->username->vp_strvalue, vp->vp_strvalue + 5,
1101 t->username->length = vp->length - 5;
1102 t->username->vp_strvalue[t->username->length] = 0;
1104 RDEBUG("Got tunneled identity of %s",
1105 t->username->vp_strvalue);
1108 * If there's a default EAP type,
1111 if (t->default_eap_type != 0) {
1112 RDEBUG("Setting default EAP type for tunneled EAP session.");
1113 vp = paircreate(PW_EAP_TYPE, 0);
1114 rad_assert(vp != NULL);
1115 vp->vp_integer = t->default_eap_type;
1116 pairadd(&fake->config_items, vp);
1121 * Don't reject the request outright,
1122 * as it's permitted to do EAP without
1125 RDEBUG2W("No EAP-Identity found to start EAP conversation.");
1127 } /* else there WAS a t->username */
1130 vp = paircopy(t->username);
1131 pairadd(&fake->packet->vps, vp);
1132 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1134 } /* else the request ALREADY had a User-Name */
1137 * Add the State attribute, too, if it exists.
1140 vp = paircopy(t->state);
1141 if (vp) pairadd(&fake->packet->vps, vp);
1145 * If this is set, we copy SOME of the request attributes
1146 * from outside of the tunnel to inside of the tunnel.
1148 * We copy ONLY those attributes which do NOT already
1149 * exist in the tunneled request.
1151 if (t->copy_request_to_tunnel) {
1154 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
1156 * The attribute is a server-side thingy,
1159 if ((vp->da->attr > 255) &&
1160 (vp->da->vendor == 0)) {
1165 * The outside attribute is already in the
1166 * tunnel, don't copy it.
1168 * This works for BOTH attributes which
1169 * are originally in the tunneled request,
1170 * AND attributes which are copied there
1173 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1178 * Some attributes are handled specially.
1180 switch (vp->da->attr) {
1182 * NEVER copy Message-Authenticator,
1183 * EAP-Message, or State. They're
1184 * only for outside of the tunnel.
1187 case PW_USER_PASSWORD:
1188 case PW_CHAP_PASSWORD:
1189 case PW_CHAP_CHALLENGE:
1190 case PW_PROXY_STATE:
1191 case PW_MESSAGE_AUTHENTICATOR:
1192 case PW_EAP_MESSAGE:
1198 * By default, copy it over.
1205 * Don't copy from the head, we've already
1208 copy = paircopy2(vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1209 pairadd(&fake->packet->vps, copy);
1213 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1214 fake->server = vp->vp_strvalue;
1216 } else if (t->virtual_server) {
1217 fake->server = t->virtual_server;
1219 } /* else fake->server == request->server */
1222 if ((debug_flag > 0) && fr_log_fp) {
1223 RDEBUG("Sending tunneled request");
1225 debug_pair_list(fake->packet->vps);
1227 fprintf(fr_log_fp, "server %s {\n",
1228 (fake->server == NULL) ? "" : fake->server);
1232 * Process channel binding here.
1234 chbind_len = eap_chbind_vp2packet(fake->packet->vps, &chbind_packet);
1235 if (chbind_len > 0) {
1237 CHBIND_REQ *req = chbind_allocate();
1239 RDEBUG("received chbind request");
1240 req->chbind_req_pkt = (uint8_t *)chbind_packet;
1241 req->chbind_req_len = chbind_len;
1242 if (fake->username) {
1243 req->username = fake->username->vp_octets;
1244 req->username_len = fake->username->length;
1246 req->username = NULL;
1247 req->username_len = 0;
1249 chbind_rcode = chbind_process(request, req);
1251 /* free the chbind packet; we're done with it */
1252 free(chbind_packet);
1254 /* encapsulate response here */
1255 if (req->chbind_resp_len > 0) {
1256 RDEBUG("sending chbind response");
1257 pairadd(&fake->reply->vps,
1258 eap_chbind_packet2vp((eap_chbind_packet_t *)req->chbind_resp,
1259 req->chbind_resp_len));
1261 RDEBUG("no chbind response");
1264 /* clean up chbind req */
1267 if (chbind_rcode != PW_AUTHENTICATION_ACK)
1268 return chbind_rcode;
1272 * Call authentication recursively, which will
1273 * do PAP, CHAP, MS-CHAP, etc.
1275 rad_virtual_server(fake);
1278 * Note that we don't do *anything* with the reply
1281 if ((debug_flag > 0) && fr_log_fp) {
1282 fprintf(fr_log_fp, "} # server %s\n",
1283 (fake->server == NULL) ? "" : fake->server);
1285 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1287 debug_pair_list(fake->reply->vps);
1291 * Decide what to do with the reply.
1293 switch (fake->reply->code) {
1294 case 0: /* No reply code, must be proxied... */
1296 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1298 eap_tunnel_data_t *tunnel;
1299 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1302 * Tell the original request that it's going
1305 pairmove2(&(request->config_items),
1306 &(fake->config_items),
1307 PW_PROXY_TO_REALM, 0, TAG_ANY);
1310 * Seed the proxy packet with the
1313 rad_assert(request->proxy == NULL);
1314 request->proxy = fake->packet;
1315 memset(&request->proxy->src_ipaddr, 0,
1316 sizeof(request->proxy->src_ipaddr));
1317 memset(&request->proxy->src_ipaddr, 0,
1318 sizeof(request->proxy->src_ipaddr));
1319 request->proxy->src_port = 0;
1320 request->proxy->dst_port = 0;
1321 fake->packet = NULL;
1322 rad_free(&fake->reply);
1326 * Set up the callbacks for the tunnel
1328 tunnel = rad_malloc(sizeof(*tunnel));
1329 memset(tunnel, 0, sizeof(*tunnel));
1331 tunnel->tls_session = tls_session;
1332 tunnel->callback = eapttls_postproxy;
1335 * Associate the callback with the request.
1337 rcode = request_data_add(request,
1339 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1341 rad_assert(rcode == 0);
1344 * rlm_eap.c has taken care of associating
1345 * the handler with the fake request.
1347 * So we associate the fake request with
1350 rcode = request_data_add(request,
1352 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1353 fake, my_request_free);
1354 rad_assert(rcode == 0);
1358 * Didn't authenticate the packet, but
1359 * we're proxying it.
1361 rcode = PW_STATUS_CLIENT;
1364 #endif /* WITH_PROXY */
1366 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1368 rcode = PW_AUTHENTICATION_REJECT;
1374 * Returns RLM_MODULE_FOO, and we want to return
1377 rcode = process_reply(handler, tls_session, request,
1380 case RLM_MODULE_REJECT:
1381 rcode = PW_AUTHENTICATION_REJECT;
1384 case RLM_MODULE_HANDLED:
1385 rcode = PW_ACCESS_CHALLENGE;
1389 rcode = PW_AUTHENTICATION_ACK;
1393 rcode = PW_AUTHENTICATION_REJECT;
1399 request_free(&fake);