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
27 #include "eap_chbind.h"
31 * 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
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * |V M r r r r r r| AVP Length |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * Verify that the diameter packet is valid.
46 static int diameter_verify(REQUEST *request, uint8_t const *data, unsigned int data_len)
51 unsigned int remaining = data_len;
53 while (remaining > 0) {
56 if (remaining < hdr_len) {
57 RDEBUG2(" Diameter attribute is too small (%u) to contain a Diameter header", remaining);
61 memcpy(&attr, data, sizeof(attr));
63 memcpy(&length, data + 4, sizeof(length));
64 length = ntohl(length);
66 if ((data[4] & 0x80) != 0) {
68 RDEBUG2(" Diameter attribute is too small to contain a Diameter header with Vendor-Id");
76 * Get the length. If it's too big, die.
81 * Too short or too long is bad.
83 if (length <= (hdr_len - 4)) {
84 RDEBUG2("Tunneled attribute %u is too short (%u < %u) to contain anything useful.", attr,
89 if (length > remaining) {
90 RDEBUG2("Tunneled attribute %u is longer than room remaining in the packet (%u > %u).", attr,
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 REDEBUG2("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, REQUEST *fake, SSL *ssl,
139 uint8_t const *data, size_t data_len)
146 size_t data_left = data_len;
148 VALUE_PAIR *first = NULL;
150 RADIUS_PACKET *packet = fake->packet; /* FIXME: api issues */
153 paircursor(&out, &first);
155 while (data_left > 0) {
156 rad_assert(data_left <= data_len);
157 memcpy(&attr, data, sizeof(attr));
162 memcpy(&length, data, sizeof(length));
164 length = ntohl(length);
167 * A "vendor" flag, with a vendor ID of zero,
168 * is equivalent to no vendor. This is stupid.
171 if ((length & (1 << 31)) != 0) {
172 memcpy(&vendor, data, sizeof(vendor));
173 vendor = ntohl(vendor);
175 data += 4; /* skip the vendor field, it's zero */
176 offset += 4; /* offset to value field */
178 if (attr > 65535) goto next_attr;
179 if (vendor > FR_MAX_VENDOR) goto next_attr;
183 * FIXME: Handle the M bit. For now, we assume that
184 * some other module takes care of any attribute
185 * with the M bit set.
191 length &= 0x00ffffff;
194 * Get the size of the value portion of the
197 size = length - offset;
200 * Vendor attributes can be larger than 255.
201 * Normal attributes cannot be.
203 if ((attr > 255) && (vendor == 0)) {
204 RWDEBUG2("Skipping Diameter attribute %u", attr);
209 * EAP-Message AVPs can be larger than 253 octets.
211 * For now, we rely on the main decoder in
212 * src/lib/radius to decode data into VPs. This
213 * means putting the data into a RADIUS attribute
214 * format. It also means that we can't handle
215 * "extended" attributes in the Diameter space. Oh well...
217 if ((size > 253) && !((vendor == 0) && (attr == PW_EAP_MESSAGE))) {
218 RWDEBUG2("diameter2vp skipping long attribute %u", attr);
223 * RADIUS VSAs are handled as Diameter attributes
224 * with Vendor-Id == 0, and the VSA data packed
225 * into the "String" field as per normal.
227 * EXCEPT for the MS-CHAP attributes.
229 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
233 buffer[0] = PW_VENDOR_SPECIFIC;
234 buffer[1] = size + 2;
235 memcpy(buffer + 2, data, size);
238 decoded = rad_attr2vp(NULL, NULL, NULL,
239 buffer, size + 2, &vp);
241 REDEBUG2("diameter2vp failed decoding attr: %s",
246 if ((size_t) decoded != size + 2) {
247 REDEBUG2("diameter2vp failed to entirely decode VSA");
252 pairinsert(&out, vp);
258 * Create it. If this fails, it's because we're OOM.
261 vp = paircreate(packet, attr, vendor);
263 RDEBUG2("Failure in creating VP");
267 if (vendor == VENDORPEC_UKERNA) {
268 RDEBUG("Received UKERNA attr %d!", attr);
272 * If it's a type from our dictionary, then
273 * we need to put the data in a relevant place.
275 * @todo: Export the lib/radius.c decoder, and use it here!
277 switch (vp->da->type) {
278 case PW_TYPE_INTEGER:
280 if (size != vp->length) {
284 * Bad format. Create a "raw"
288 if (vp) pairfree(&vp);
289 da = dict_attrunknown(attr, vendor, true);
290 if (!da) return NULL;
291 vp = pairalloc(NULL, da);
292 pairmemcpy(vp, data, size);
295 memcpy(&vp->vp_integer, data, vp->length);
298 * Stored in host byte order: change it.
300 vp->vp_integer = ntohl(vp->vp_integer);
303 case PW_TYPE_INTEGER64:
304 if (size != vp->length) goto raw;
305 memcpy(&vp->vp_integer64, data, vp->length);
308 * Stored in host byte order: change it.
310 vp->vp_integer64 = ntohll(vp->vp_integer64);
314 if (size != vp->length) {
315 RDEBUG2("Invalid length attribute %d",
321 memcpy(&vp->vp_ipaddr, data, vp->length);
324 * Stored in network byte order: don't change it.
329 if (size != vp->length) goto raw;
330 vp->vp_integer = data[0];
334 if (size != vp->length) goto raw;
335 vp->vp_integer = (data[0] * 256) + data[1];
339 if (size != vp->length) goto raw;
340 memcpy(&vp->vp_signed, data, vp->length);
341 vp->vp_signed = ntohl(vp->vp_signed);
344 case PW_TYPE_IPV6ADDR:
345 if (size != vp->length) goto raw;
346 memcpy(&vp->vp_ipv6addr, data, vp->length);
349 case PW_TYPE_IPV6PREFIX:
350 if (size != vp->length) goto raw;
351 memcpy(&vp->vp_ipv6prefix, data, vp->length);
355 * Ensure it's NUL terminated.
358 vp->vp_strvalue = p = talloc_array(vp, char, size + 1);
360 memcpy(p, data, size);
362 vp->length = strlen(p);
366 * Copy it over verbatim.
370 pairmemcpy(vp, data, size);
375 * Ensure that the client is using the
376 * correct challenge. This weirdness is
377 * to protect against against replay
378 * attacks, where anyone observing the
379 * CHAP exchange could pose as that user,
380 * by simply choosing to use the same
383 * By using a challenge based on
384 * information from the current session,
385 * we can guarantee that the client is
386 * not *choosing* a challenge.
388 * We're a little forgiving in that we
389 * have loose checks on the length, and
390 * we do NOT check the Id (first octet of
391 * the response to the challenge)
393 * But if the client gets the challenge correct,
394 * we're not too worried about the Id.
396 if (((vp->da->vendor == 0) && (vp->da->attr == PW_CHAP_CHALLENGE)) ||
397 ((vp->da->vendor == VENDORPEC_MICROSOFT) && (vp->da->attr == PW_MSCHAP_CHALLENGE))) {
398 uint8_t challenge[16];
400 if ((vp->length < 8) ||
402 RDEBUG("Tunneled challenge has invalid length");
408 eapttls_gen_challenge(ssl, challenge,
411 if (memcmp(challenge, vp->vp_octets,
413 RDEBUG("Tunneled challenge is incorrect");
423 pairinsert(&out, vp);
427 * Catch non-aligned attributes.
429 if (data_left == length) break;
432 * The length does NOT include the padding, so
433 * we've got to account for it here by rounding up
434 * to the nearest 4-byte boundary.
439 rad_assert(data_left >= length);
441 data += length - offset; /* already updated */
445 * We got this far. It looks OK.
451 * Convert VALUE_PAIR's to diameter attributes, and write them
454 * The ONLY VALUE_PAIR's which may be passed to this function
455 * are ones which can go inside of a RADIUS (i.e. diameter)
456 * packet. So no server-configuration attributes, or the like.
458 static int vp2diameter(REQUEST *request, tls_session_t *tls_session, VALUE_PAIR *first)
461 * RADIUS packets are no more than 4k in size, so if
462 * we've got more than 4k of data to write, it's very
465 uint8_t buffer[4096];
478 for (vp = paircursor(&cursor, &first); vp; vp = pairnext(&cursor)) {
480 * Too much data: die.
482 if ((total + vp->length + 12) >= sizeof(buffer)) {
483 RDEBUG2("output buffer is full!");
488 * Hmm... we don't group multiple EAP-Messages
489 * together. Maybe we should...
493 vendor = vp->da->vendor;
495 attr = vp->da->attr & 0xffff;
502 * Hmm... set the M bit for all attributes?
508 memcpy(p, &attr, sizeof(attr));
512 length += 8; /* includes 8 bytes of attr & length */
515 length += 4; /* include 4 bytes of vendor */
517 length = ntohl(length);
518 memcpy(p, &length, sizeof(length));
522 vendor = ntohl(vendor);
523 memcpy(p, &vendor, sizeof(vendor));
527 length = ntohl(length);
528 memcpy(p, &length, sizeof(length));
533 switch (vp->da->type) {
534 case PW_TYPE_INTEGER:
536 attr = htonl(vp->vp_integer); /* stored in host order */
537 memcpy(p, &attr, sizeof(attr));
541 case PW_TYPE_INTEGER64:
542 attr64 = htonll(vp->vp_integer64); /* stored in host order */
543 memcpy(p, &attr64, sizeof(attr64));
548 memcpy(p, &vp->vp_ipaddr, 4); /* network order */
555 memcpy(p, vp->vp_strvalue, vp->length);
561 * Skip to the end of the data.
567 * Align the data to a multiple of 4 bytes.
569 if ((total & 0x03) != 0) {
572 length = 4 - (total & 0x03);
573 for (i = 0; i < length; i++) {
579 } /* loop over the VP's to write. */
582 * Write the data in the buffer to the SSL session.
588 if ((debug_flag > 2) && fr_log_fp) {
589 for (i = 0; i < total; i++) {
590 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data out %04x: ", (int) i);
592 fprintf(fr_log_fp, "%02x ", buffer[i]);
594 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
596 if ((total & 0x0f) != 0) fprintf(fr_log_fp, "\n");
600 (tls_session->record_plus)(&tls_session->clean_in, buffer, total);
603 * FIXME: Check the return code.
605 tls_handshake_send(request, tls_session);
615 * Use a reply packet to determine what to do.
617 static rlm_rcode_t process_reply(UNUSED eap_handler_t *handler, tls_session_t *tls_session,
618 REQUEST *request, RADIUS_PACKET *reply)
620 rlm_rcode_t rcode = RLM_MODULE_REJECT;
622 ttls_tunnel_t *t = tls_session->opaque;
624 rad_assert(request != NULL);
625 rad_assert(handler->request == request);
628 * If the response packet was Access-Accept, then
629 * we're OK. If not, die horribly.
631 * FIXME: Take MS-CHAP2-Success attribute, and
632 * tunnel it back to the client, to authenticate
633 * ourselves to the client.
635 * FIXME: If we have an Access-Challenge, then
636 * the Reply-Message is tunneled back to the client.
638 * FIXME: If we have an EAP-Message, then that message
639 * must be tunneled back to the client.
641 * FIXME: If we have an Access-Challenge with a State
642 * attribute, then do we tunnel that to the client, or
643 * keep track of it ourselves?
645 * FIXME: EAP-Messages can only start with 'identity',
646 * NOT 'eap start', so we should check for that....
648 switch (reply->code) {
649 case PW_AUTHENTICATION_ACK:
650 RDEBUG("Got tunneled Access-Accept");
652 rcode = RLM_MODULE_OK;
655 * MS-CHAP2-Success means that we do NOT return
656 * an Access-Accept, but instead tunnel that
657 * attribute to the client, and keep going with
658 * the TTLS session. Once the client accepts
659 * our identity, it will respond with an empty
660 * packet, and we will send EAP-Success.
663 pairfilter(tls_session, &vp, &reply->vps, PW_MSCHAP2_SUCCESS, VENDORPEC_MICROSOFT, TAG_ANY);
665 RDEBUG("Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
666 rcode = RLM_MODULE_HANDLED;
667 t->authenticated = true;
670 * Delete MPPE keys & encryption policy. We don't
673 pairdelete(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
674 pairdelete(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
675 pairdelete(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
676 pairdelete(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
679 * Use the tunneled reply, but not now.
681 if (t->use_tunneled_reply) {
682 rad_assert(!t->accept_vps);
683 pairfilter(t, &t->accept_vps, &reply->vps,
685 rad_assert(!reply->vps);
688 } else { /* no MS-CHAP2-Success */
690 * Can only have EAP-Message if there's
691 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
693 * We also do NOT tunnel the EAP-Success
694 * attribute back to the client, as the client
695 * can figure it out, from the non-tunneled
696 * EAP-Success packet.
698 pairfilter(tls_session, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
702 /* move channel binding responses; we need to send them */
703 pairfilter(tls_session, &vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA, TAG_ANY);
704 if (pairfind(vp, PW_UKERNA_CHBIND, VENDORPEC_UKERNA, TAG_ANY) != NULL) {
705 t->authenticated = true;
707 * Use the tunneled reply, but not now.
709 if (t->use_tunneled_reply) {
710 rad_assert(!t->accept_vps);
711 pairfilter(t, &t->accept_vps, &reply->vps,
713 rad_assert(!reply->vps);
715 rcode = RLM_MODULE_HANDLED;
719 * Handle the ACK, by tunneling any necessary reply
720 * VP's back to the client.
723 RDEBUG("sending tunneled reply attributes");
725 RDEBUG("end tunneled reply attributes");
726 vp2diameter(request, tls_session, vp);
731 * If we've been told to use the attributes from
732 * the reply, then do so.
734 * WARNING: This may leak information about the
737 if (t->use_tunneled_reply) {
738 pairdelete(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
739 pairfilter(request->reply, &request->reply->vps,
740 &reply->vps, 0, 0, TAG_ANY);
745 case PW_AUTHENTICATION_REJECT:
746 RDEBUG("Got tunneled Access-Reject");
747 rcode = RLM_MODULE_REJECT;
751 * Handle Access-Challenge, but only if we
752 * send tunneled reply data. This is because
753 * an Access-Challenge means that we MUST tunnel
754 * a Reply-Message to the client.
756 case PW_ACCESS_CHALLENGE:
757 RDEBUG("Got tunneled Access-Challenge");
760 * Keep the State attribute, if necessary.
762 * Get rid of the old State, too.
765 pairfilter(t, &t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
768 * We should really be a bit smarter about this,
769 * and move over only those attributes which
770 * are relevant to the authentication request,
771 * but that's a lot more work, and this "dumb"
772 * method works in 99.9% of the situations.
775 pairfilter(t, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
778 * There MUST be a Reply-Message in the challenge,
779 * which we tunnel back to the client.
781 * If there isn't one in the reply VP's, then
782 * we MUST create one, with an empty string as
785 pairfilter(t, &vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
787 /* also move chbind messages, if any */
788 pairfilter(t, &vp, &reply->vps, PW_UKERNA_CHBIND, VENDORPEC_UKERNA,
792 * Handle the ACK, by tunneling any necessary reply
793 * VP's back to the client.
796 vp2diameter(request, tls_session, vp);
799 rcode = RLM_MODULE_HANDLED;
803 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
804 rcode = RLM_MODULE_INVALID;
814 * Do post-proxy processing,
816 static int eapttls_postproxy(eap_handler_t *handler, void *data)
819 tls_session_t *tls_session = (tls_session_t *) data;
820 REQUEST *fake, *request = handler->request;
822 rad_assert(request != NULL);
823 RDEBUG("Passing reply from proxy back into the tunnel.");
826 * If there was a fake request associated with the proxied
827 * request, do more processing of it.
829 fake = (REQUEST *) request_data_get(handler->request,
830 handler->request->proxy,
831 REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK);
834 * Do the callback, if it exists, and if it was a success.
837 handler->request->proxy_reply &&
838 (handler->request->proxy_reply->code == PW_AUTHENTICATION_ACK)) {
842 rad_assert(!fake->packet);
843 fake->packet = request->proxy;
844 fake->packet->src_ipaddr = request->packet->src_ipaddr;
845 request->proxy = NULL;
847 rad_assert(!fake->reply);
848 fake->reply = request->proxy_reply;
849 request->proxy_reply = NULL;
851 if ((debug_flag > 0) && fr_log_fp) {
852 fprintf(fr_log_fp, "server %s {\n",
853 (!fake->server) ? "" : fake->server);
857 * Perform a post-auth stage for the tunneled
860 fake->options &= ~RAD_REQUEST_OPTION_PROXY_EAP;
861 rcode = rad_postauth(fake);
862 RDEBUG2("post-auth returns %d", rcode);
864 if ((debug_flag > 0) && fr_log_fp) {
865 fprintf(fr_log_fp, "} # server %s\n",
866 (!fake->server) ? "" : fake->server);
868 RDEBUG("Final reply from tunneled session code %d",
870 debug_pair_list(fake->reply->vps);
876 request->proxy = fake->packet;
878 request->proxy_reply = fake->reply;
882 * And we're done with this request.
886 case RLM_MODULE_FAIL:
888 eaptls_fail(handler, 0);
892 default: /* Don't Do Anything */
893 RDEBUG2("Got reply %d",
894 request->proxy_reply->code);
898 request_free(&fake); /* robust if !fake */
901 * Process the reply from the home server.
903 rcode = process_reply(handler, tls_session, handler->request,
904 handler->request->proxy_reply);
907 * The proxy code uses the reply from the home server as
908 * the basis for the reply to the NAS. We don't want that,
909 * so we toss it, after we've had our way with it.
911 pairfree(&handler->request->proxy_reply->vps);
914 case RLM_MODULE_REJECT:
915 RDEBUG("Reply was rejected");
918 case RLM_MODULE_HANDLED:
919 RDEBUG("Reply was handled");
920 eaptls_request(handler->eap_ds, tls_session);
924 RDEBUG("Reply was OK");
927 * Success: Automatically return MPPE keys.
929 return eaptls_success(handler, 0);
932 RDEBUG("Reply was unknown.");
936 eaptls_fail(handler, 0);
940 #endif /* WITH_PROXY */
943 * Process the "diameter" contents of the tunneled data.
945 int eapttls_process(eap_handler_t *handler, tls_session_t *tls_session)
947 int code = PW_AUTHENTICATION_REJECT;
954 REQUEST *request = handler->request;
955 eap_chbind_packet_t *chbind_packet;
958 rad_assert(request != NULL);
961 * Just look at the buffer directly, without doing
964 data_len = tls_session->clean_out.used;
965 tls_session->clean_out.used = 0;
966 data = tls_session->clean_out.data;
968 t = (ttls_tunnel_t *) tls_session->opaque;
971 * If there's no data, maybe this is an ACK to an
975 if (t->authenticated) {
976 RDEBUG("Got ACK, and the user was already authenticated.");
977 return PW_AUTHENTICATION_ACK;
978 } /* else no session, no data, die. */
981 * FIXME: Call SSL_get_error() to see what went
984 RDEBUG2("SSL_read Error");
985 return PW_AUTHENTICATION_REJECT;
989 if ((debug_flag > 2) && fr_log_fp) {
992 for (i = 0; i < data_len; i++) {
993 if ((i & 0x0f) == 0) fprintf(fr_log_fp, " TTLS tunnel data in %04x: ", (int) i);
995 fprintf(fr_log_fp, "%02x ", data[i]);
997 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
999 if ((data_len & 0x0f) != 0) fprintf(fr_log_fp, "\n");
1003 if (!diameter_verify(request, data, data_len)) {
1004 return PW_AUTHENTICATION_REJECT;
1008 * Allocate a fake REQUEST structe.
1010 fake = request_alloc_fake(request);
1012 rad_assert(!fake->packet->vps);
1015 * Add the tunneled attributes to the fake request.
1017 fake->packet->vps = diameter2vp(request, fake, tls_session->ssl, data, data_len);
1018 if (!fake->packet->vps) {
1019 request_free(&fake);
1020 return PW_AUTHENTICATION_REJECT;
1024 * Tell the request that it's a fake one.
1026 pairmake_packet("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
1028 if ((debug_flag > 0) && fr_log_fp) {
1029 RDEBUG("Got tunneled request");
1031 debug_pair_list(fake->packet->vps);
1035 * Update other items in the REQUEST data structure.
1037 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1038 fake->password = pairfind(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
1041 * No User-Name, try to create one from stored data.
1043 if (!fake->username) {
1045 * No User-Name in the stored data, look for
1046 * an EAP-Identity, and pull it out of there.
1049 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
1051 (vp->length >= EAP_HEADER_LEN + 2) &&
1052 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
1053 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
1054 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
1058 * Create & remember a User-Name
1060 t->username = pairmake(t, NULL, "User-Name", NULL, T_OP_EQ);
1061 rad_assert(t->username != NULL);
1062 t->username->length = vp->length - 5;
1064 t->username->vp_strvalue = p = talloc_array(t->username, char,
1065 t->username->length + 1);
1066 memcpy(p, vp->vp_octets + 5, t->username->length);
1067 p[t->username->length] = 0;
1069 RDEBUG("Got tunneled identity of %s",
1070 t->username->vp_strvalue);
1073 * If there's a default EAP type,
1076 if (t->default_method != 0) {
1077 RDEBUG("Setting default EAP type for tunneled EAP session.");
1078 vp = paircreate(fake, PW_EAP_TYPE, 0);
1079 rad_assert(vp != NULL);
1080 vp->vp_integer = t->default_method;
1081 pairadd(&fake->config_items, vp);
1086 * Don't reject the request outright,
1087 * as it's permitted to do EAP without
1090 RWDEBUG2("No EAP-Identity found to start EAP conversation.");
1092 } /* else there WAS a t->username */
1095 vp = paircopy(fake->packet, t->username);
1096 pairadd(&fake->packet->vps, vp);
1097 fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
1099 } /* else the request ALREADY had a User-Name */
1102 * Add the State attribute, too, if it exists.
1105 vp = paircopy(fake->packet, t->state);
1106 if (vp) pairadd(&fake->packet->vps, vp);
1110 * If this is set, we copy SOME of the request attributes
1111 * from outside of the tunnel to inside of the tunnel.
1113 * We copy ONLY those attributes which do NOT already
1114 * exist in the tunneled request.
1116 if (t->copy_request_to_tunnel) {
1120 for (vp = paircursor(&cursor, &request->packet->vps); vp; vp = pairnext(&cursor)) {
1122 * The attribute is a server-side thingy,
1125 if ((vp->da->attr > 255) &&
1126 (vp->da->vendor == 0)) {
1131 * The outside attribute is already in the
1132 * tunnel, don't copy it.
1134 * This works for BOTH attributes which
1135 * are originally in the tunneled request,
1136 * AND attributes which are copied there
1139 if (pairfind(fake->packet->vps, vp->da->attr, vp->da->vendor, TAG_ANY)) {
1144 * Some attributes are handled specially.
1146 switch (vp->da->attr) {
1148 * NEVER copy Message-Authenticator,
1149 * EAP-Message, or State. They're
1150 * only for outside of the tunnel.
1153 case PW_USER_PASSWORD:
1154 case PW_CHAP_PASSWORD:
1155 case PW_CHAP_CHALLENGE:
1156 case PW_PROXY_STATE:
1157 case PW_MESSAGE_AUTHENTICATOR:
1158 case PW_EAP_MESSAGE:
1164 * By default, copy it over.
1171 * Don't copy from the head, we've already
1174 copy = paircopy2(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
1175 pairadd(&fake->packet->vps, copy);
1179 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1180 fake->server = vp->vp_strvalue;
1182 } else if (t->virtual_server) {
1183 fake->server = t->virtual_server;
1185 } /* else fake->server == request->server */
1188 if ((debug_flag > 0) && fr_log_fp) {
1189 RDEBUG("Sending tunneled request");
1191 debug_pair_list(fake->packet->vps);
1193 fprintf(fr_log_fp, "server %s {\n",
1194 (!fake->server) ? "" : fake->server);
1198 * Process channel binding here.
1200 chbind_len = eap_chbind_vp2packet(fake->packet->vps, &chbind_packet);
1201 if (chbind_len > 0) {
1203 CHBIND_REQ *req = chbind_allocate();
1205 RDEBUG("received chbind request");
1206 req->chbind_req_pkt = (uint8_t *)chbind_packet;
1207 req->chbind_req_len = chbind_len;
1208 if (fake->username) {
1209 req->username = fake->username->vp_octets;
1210 req->username_len = fake->username->length;
1212 req->username = NULL;
1213 req->username_len = 0;
1215 chbind_rcode = chbind_process(request, req);
1217 /* free the chbind packet; we're done with it */
1218 free(chbind_packet);
1220 /* encapsulate response here */
1221 if (req->chbind_resp_len > 0) {
1222 RDEBUG("sending chbind response");
1223 pairadd(&fake->reply->vps,
1224 eap_chbind_packet2vp(fake, (eap_chbind_packet_t *)req->chbind_resp,
1225 req->chbind_resp_len));
1227 RDEBUG("no chbind response");
1230 /* clean up chbind req */
1233 if (chbind_rcode != PW_AUTHENTICATION_ACK)
1234 return chbind_rcode;
1238 * Call authentication recursively, which will
1239 * do PAP, CHAP, MS-CHAP, etc.
1241 rad_virtual_server(fake);
1244 * Note that we don't do *anything* with the reply
1247 if ((debug_flag > 0) && fr_log_fp) {
1248 fprintf(fr_log_fp, "} # server %s\n",
1249 (!fake->server) ? "" : fake->server);
1251 RDEBUG("Got tunneled reply code %d", fake->reply->code);
1253 debug_pair_list(fake->reply->vps);
1257 * Decide what to do with the reply.
1259 switch (fake->reply->code) {
1260 case 0: /* No reply code, must be proxied... */
1262 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM, 0, TAG_ANY);
1264 eap_tunnel_data_t *tunnel;
1265 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1268 * Tell the original request that it's going
1271 pairfilter(request, &(request->config_items),
1272 &(fake->config_items),
1273 PW_PROXY_TO_REALM, 0, TAG_ANY);
1276 * Seed the proxy packet with the
1279 rad_assert(!request->proxy);
1280 request->proxy = fake->packet;
1281 memset(&request->proxy->src_ipaddr, 0,
1282 sizeof(request->proxy->src_ipaddr));
1283 memset(&request->proxy->src_ipaddr, 0,
1284 sizeof(request->proxy->src_ipaddr));
1285 request->proxy->src_port = 0;
1286 request->proxy->dst_port = 0;
1287 fake->packet = NULL;
1288 rad_free(&fake->reply);
1292 * Set up the callbacks for the tunnel
1294 tunnel = talloc_zero(request, eap_tunnel_data_t);
1295 tunnel->tls_session = tls_session;
1296 tunnel->callback = eapttls_postproxy;
1299 * Associate the callback with the request.
1301 code = request_data_add(request, request->proxy, REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1303 rad_assert(code == 0);
1306 * rlm_eap.c has taken care of associating
1307 * the handler with the fake request.
1309 * So we associate the fake request with
1312 talloc_set_destructor(fake, request_opaque_free);
1313 code = request_data_add(request, request->proxy, REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1315 rad_assert(code == 0);
1319 * Didn't authenticate the packet, but
1320 * we're proxying it.
1322 code = PW_STATUS_CLIENT;
1325 #endif /* WITH_PROXY */
1327 RDEBUG("No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1329 code = PW_AUTHENTICATION_REJECT;
1335 * Returns RLM_MODULE_FOO, and we want to return PW_FOO
1337 rcode = process_reply(handler, tls_session, request, fake->reply);
1339 case RLM_MODULE_REJECT:
1340 code = PW_AUTHENTICATION_REJECT;
1343 case RLM_MODULE_HANDLED:
1344 code = PW_ACCESS_CHALLENGE;
1348 code = PW_AUTHENTICATION_ACK;
1352 code = PW_AUTHENTICATION_REJECT;
1358 request_free(&fake);