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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2003 Alan DeKok <aland@freeradius.org>
27 * 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
28 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * |V M r r r r r r| AVP Length |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * Verify that the diameter packet is valid.
42 static int diameter_verify(const uint8_t *data, int data_len)
48 while (data_len > 0) {
49 memcpy(&attr, data, sizeof(attr));
53 DEBUG2(" rlm_eap_ttls: Non-RADIUS attribute in tunneled authentication is not supported");
57 memcpy(&length, data , sizeof(length));
59 length = ntohl(length);
62 * A "vendor" flag, with a vendor ID of zero,
63 * is equivalent to no vendor. This is stupid.
66 if ((length & (1 << 31)) != 0) {
71 memcpy(&vendor, data, sizeof(vendor));
72 vendor = ntohl(vendor);
75 DEBUG2(" rlm_eap_ttls: Vendor codes larger than 65535 are not supported");
79 attribute = (vendor << 16) | attr;
81 da = dict_attrbyvalue(attribute);
84 * SHOULD check ((length & (1 << 30)) != 0)
85 * for the mandatory bit.
88 DEBUG2(" rlm_eap_ttls: Fatal! Vendor %u, Attribute %u was not found in our dictionary. ",
93 data += 4; /* skip the vendor field */
94 offset += 4; /* offset to value field */
98 * Ignore the M bit. We support all RADIUS attributes...
102 * Get the length. If it's too big, die.
104 length &= 0x00ffffff;
107 * Too short or too long is bad.
111 if ((length < offset) ||
112 (length > (MAX_STRING_LEN + 8)) ||
113 (length > data_len)) {
114 DEBUG2(" rlm_eap_ttls: Tunneled attribute %d has invalid length %d", attr, length);
119 * Check for broken implementations, which don't
120 * pad the AVP to a 4-octet boundary.
122 if (data_len == length) break;
125 * The length does NOT include the padding, so
126 * we've got to account for it here by rounding up
127 * to the nearest 4-byte boundary.
133 * If the rest of the diameter packet is larger than
134 * this attribute, continue.
136 * Otherwise, if the attribute over-flows the end
137 * of the packet, die.
139 if (data_len > length) {
141 data += length - offset;
143 } else if (data_len < length) {
144 DEBUG2(" rlm_eap_ttls: ERROR! Diameter attribute overflows packet!");
147 } else { /* equal, end of packet... */
153 * We got this far. It looks OK.
160 * Convert diameter attributes to our VALUE_PAIR's
162 static VALUE_PAIR *diameter2vp(SSL *ssl,
163 const uint8_t *data, unsigned int data_len)
169 VALUE_PAIR *first = NULL;
170 VALUE_PAIR **last = &first;
173 while (data_len > 0) {
174 memcpy(&attr, data, sizeof(attr));
178 memcpy(&length, data, sizeof(length));
180 length = ntohl(length);
183 * Ignore the M bit. We support all RADIUS attributes...
187 * A "vendor" flag, with a vendor ID of zero,
188 * is equivalent to no vendor. This is stupid.
191 if ((length & (1 << 31)) != 0) {
194 memcpy(&vendor, data, sizeof(vendor));
195 vendor = ntohl(vendor);
197 attr |= (vendor << 16);
199 data += 4; /* skip the vendor field, it's zero */
200 offset += 4; /* offset to value field */
206 length &= 0x00ffffff;
209 * diameter code + length, and it must fit in
212 rad_assert(length <= (offset + MAX_STRING_LEN));
215 * Get the size of the value portion of the
218 size = length - offset;
223 vp = paircreate(attr, PW_TYPE_OCTETS);
225 DEBUG2(" rlm_eap_ttls: Failure in creating VP");
231 * If it's a type from our dictionary, then
232 * we need to put the data in a relevant place.
235 case PW_TYPE_INTEGER:
237 if (size != vp->length) {
238 DEBUG2(" rlm_eap_ttls: Invalid length attribute %d",
243 memcpy(&vp->lvalue, data, vp->length);
246 * Stored in host byte order: change it.
248 vp->lvalue = ntohl(vp->lvalue);
252 if (size != vp->length) {
253 DEBUG2(" rlm_eap_ttls: Invalid length attribute %d",
258 memcpy(&vp->lvalue, data, vp->length);
261 * Stored in network byte order: don't change it.
266 * String, octet, etc. Copy the data from the
267 * value field over verbatim.
269 * FIXME: Ipv6 attributes ?
274 memcpy(vp->strvalue, data, vp->length);
279 * User-Password is NUL padded to a multiple
280 * of 16 bytes. Let's chop it to something
283 * NOTE: This means that the User-Password
284 * attribute CANNOT EVER have embedded zeros in it!
286 switch (vp->attribute) {
287 case PW_USER_PASSWORD:
291 for (i = 0; i < vp->length; i++) {
292 if (vp->strvalue[i] == 0) {
301 * Ensure that the client is using the
302 * correct challenge. This weirdness is
303 * to protect against against replay
304 * attacks, where anyone observing the
305 * CHAP exchange could pose as that user,
306 * by simply choosing to use the same
309 * By using a challenge based on
310 * information from the current session,
311 * we can guarantee that the client is
312 * not *choosing* a challenge.
314 * We're a little forgiving in that we
315 * have loose checks on the length, and
316 * we do NOT check the Id (first octet of
317 * the response to the challenge)
319 * But if the client gets the challenge correct,
320 * we're not too worried about the Id.
322 case PW_CHAP_CHALLENGE:
323 case PW_MSCHAP_CHALLENGE:
324 if ((vp->length < 8) ||
326 DEBUG2(" TTLS: Tunneled challenge has invalid length");
332 uint8_t challenge[16];
334 eapttls_gen_challenge(ssl, challenge,
337 for (i = 0; i < vp->length; i++) {
338 if (challenge[i] != vp->strvalue[i]) {
339 DEBUG2(" TTLS: Tunneled challenge is incorrect");
349 } /* switch over checking/re-writing of attributes. */
358 * The length does NOT include the padding, so
359 * we've got to account for it here by rounding up
360 * to the nearest 4-byte boundary.
366 data += length - offset; /* already updated */
370 * We got this far. It looks OK.
376 * Convert VALUE_PAIR's to diameter attributes, and write them
379 * The ONLY VALUE_PAIR's which may be passed to this function
380 * are ones which can go inside of a RADIUS (i.e. diameter)
381 * packet. So no server-configuration attributes, or the like.
383 static int vp2diameter(tls_session_t *tls_session, VALUE_PAIR *first)
386 * RADIUS packets are no more than 4k in size, so if
387 * we've got more than 4k of data to write, it's very
390 uint8_t buffer[4096];
401 for (vp = first; vp != NULL; vp = vp->next) {
403 * Too much data: die.
405 if ((total + vp->length + 12) >= sizeof(buffer)) {
406 DEBUG2(" TTLS output buffer is full!");
411 * Hmm... we don't group multiple EAP-Messages
412 * together. Maybe we should...
416 * Length is no more than 253, due to RADIUS
420 vendor = (vp->attribute >> 16) & 0xffff;
422 attr = vp->attribute & 0xffff;
425 attr = vp->attribute;
429 * Hmm... set the M bit for all attributes?
435 memcpy(p, &attr, sizeof(attr));
439 length += 8; /* includes 8 bytes of attr & length */
442 length += 4; /* include 4 bytes of vendor */
444 length = ntohl(length);
445 memcpy(p, &length, sizeof(length));
449 vendor = ntohl(vendor);
450 memcpy(p, &vendor, sizeof(vendor));
454 length = ntohl(length);
455 memcpy(p, &length, sizeof(length));
461 case PW_TYPE_INTEGER:
463 attr = ntohl(vp->lvalue); /* stored in host order */
464 memcpy(p, &attr, sizeof(attr));
469 attr = vp->lvalue; /* stored in network order */
470 memcpy(p, &attr, sizeof(attr));
477 memcpy(p, vp->strvalue, vp->length);
483 * Skip to the end of the data.
489 * Align the data to a multiple of 4 bytes.
491 if ((total & 0x03) != 0) {
494 length = total & 0x03;
495 for (i = 0; i < length; i++) {
501 } /* loop over the VP's to write. */
504 * Write the data in the buffer to the SSL session.
511 if (debug_flag > 2) {
512 for (i = 0; i < total; i++) {
513 if ((i & 0x0f) == 0) printf(" TTLS tunnel data out %04x: ", i);
515 printf("%02x ", buffer[i]);
517 if ((i & 0x0f) == 0x0f) printf("\n");
519 if ((total & 0x0f) != 0) printf("\n");
523 record_plus(&tls_session->clean_in, buffer, total);
526 * FIXME: Check the return code.
528 tls_handshake_send(tls_session);
538 * Use a reply packet to determine what to do.
540 static int process_reply(EAP_HANDLER *handler, tls_session_t *tls_session,
541 REQUEST *request, RADIUS_PACKET *reply)
543 int rcode = RLM_MODULE_REJECT;
545 ttls_tunnel_t *t = tls_session->opaque;
548 * If the response packet was Access-Accept, then
549 * we're OK. If not, die horribly.
551 * FIXME: Take MS-CHAP2-Success attribute, and
552 * tunnel it back to the client, to authenticate
553 * ourselves to the client.
555 * FIXME: If we have an Access-Challenge, then
556 * the Reply-Message is tunneled back to the client.
558 * FIXME: If we have an EAP-Message, then that message
559 * must be tunneled back to the client.
561 * FIXME: If we have an Access-Challenge with a State
562 * attribute, then do we tunnel that to the client, or
563 * keep track of it ourselves?
565 * FIXME: EAP-Messages can only start with 'identity',
566 * NOT 'eap start', so we should check for that....
568 switch (reply->code) {
569 case PW_AUTHENTICATION_ACK:
570 DEBUG2(" TTLS: Got tunneled Access-Accept");
572 rcode = RLM_MODULE_OK;
575 * MS-CHAP2-Success means that we do NOT return
576 * an Access-Accept, but instead tunnel that
577 * attribute to the client, and keep going with
578 * the TTLS session. Once the client accepts
579 * our identity, it will respond with an empty
580 * packet, and we will send EAP-Success.
583 pairmove2(&vp, &reply->vps, PW_MSCHAP2_SUCCESS);
587 * FIXME: Tunneling MS-CHAP2-Success causes
588 * the only client we have access to, to die.
590 * We don't want that...
594 DEBUG2(" TTLS: Got MS-CHAP2-Success, tunneling it to the client in a challenge.");
595 rcode = RLM_MODULE_HANDLED;
596 t->authenticated = TRUE;
598 } else { /* no MS-CHAP2-Success */
600 * Can only have EAP-Message if there's
601 * no MS-CHAP2-Success. (FIXME: EAP-MSCHAP?)
603 * We also do NOT tunnel the EAP-Success
604 * attribute back to the client, as the client
605 * can figure it out, from the non-tunneled
606 * EAP-Success packet.
608 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE);
612 * If we've been told to use the attributes from
613 * the reply, then do so.
615 * WARNING: This may leak information about the
618 if (t->use_tunneled_reply) {
619 pairadd(&request->reply->vps, reply->vps);
625 * Handle the ACK, by tunneling any necessary reply
626 * VP's back to the client.
629 vp2diameter(tls_session, vp);
635 case PW_AUTHENTICATION_REJECT:
636 DEBUG2(" TTLS: Got tunneled Access-Reject");
637 rcode = RLM_MODULE_REJECT;
641 * Handle Access-Challenge, but only if we
642 * send tunneled reply data. This is because
643 * an Access-Challenge means that we MUST tunnel
644 * a Reply-Message to the client.
646 case PW_ACCESS_CHALLENGE:
647 DEBUG2(" TTLS: Got tunneled Access-Challenge");
650 * Keep the State attribute, if necessary.
652 * Get rid of the old State, too.
655 pairmove2(&t->state, &reply->vps, PW_STATE);
658 * We should really be a bit smarter about this,
659 * and move over only those attributes which
660 * are relevant to the authentication request,
661 * but that's a lot more work, and this "dumb"
662 * method works in 99.9% of the situations.
665 pairmove2(&vp, &reply->vps, PW_EAP_MESSAGE);
668 * There MUST be a Reply-Message in the challenge,
669 * which we tunnel back to the client.
671 * If there isn't one in the reply VP's, then
672 * we MUST create one, with an empty string as
675 pairmove2(&vp, &reply->vps, PW_REPLY_MESSAGE);
678 * Handle the ACK, by tunneling any necessary reply
679 * VP's back to the client.
682 vp2diameter(tls_session, vp);
685 rcode = RLM_MODULE_HANDLED;
689 DEBUG2(" TTLS: Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
690 rcode = RLM_MODULE_REJECT;
699 * Do post-proxy processing,
701 static int eapttls_postproxy(EAP_HANDLER *handler, void *data)
704 tls_session_t *tls_session = (tls_session_t *) data;
706 DEBUG2(" TTLS: Passing reply from proxy back into the tunnel.");
709 * Process the reply from the home server.
711 rcode = process_reply(handler, tls_session, handler->request,
712 handler->request->proxy_reply);
715 * The proxy code uses the reply from the home server as
716 * the basis for the reply to the NAS. We don't want that,
717 * so we toss it, after we've had our way with it.
719 pairfree(&handler->request->proxy_reply->vps);
722 case RLM_MODULE_REJECT:
723 DEBUG2(" TTLS: Reply was rejected");
726 case RLM_MODULE_HANDLED:
727 DEBUG2(" TTLS: Reply was handled");
728 eaptls_request(handler->eap_ds, tls_session);
732 DEBUG2(" TTLS: Reply was OK");
733 eaptls_success(handler->eap_ds, 0);
734 eaptls_gen_mppe_keys(&handler->request->reply->vps,
736 "ttls keying material");
740 DEBUG2(" TTLS: Reply was unknown.");
744 eaptls_fail(handler->eap_ds, 0);
750 * Process the "diameter" contents of the tunneled data.
752 int eapttls_process(EAP_HANDLER *handler, tls_session_t *tls_session)
755 int rcode = PW_AUTHENTICATION_REJECT;
760 unsigned int data_len;
762 REQUEST *request = handler->request;
765 * Grab the dirty data, and copy it to our buffer.
767 * I *really* don't like these 'record_t' things...
769 data_len = record_minus(&tls_session->dirty_in, buffer, sizeof(buffer));
773 * Write the data from the dirty buffer (i.e. packet
774 * data) into the buffer which we will give to SSL for
777 * Some of this code COULD technically go into the TLS
778 * module, in eaptls_process(), where it returns EAPTLS_OK.
780 * Similarly, the writing of data to the SSL context could
783 BIO_write(tls_session->into_ssl, buffer, data_len);
784 record_init(&tls_session->clean_out);
787 * Read (and decrypt) the tunneled data from the SSL session,
788 * and put it into the decrypted data buffer.
790 err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
791 sizeof(tls_session->clean_out.data));
794 * FIXME: Call SSL_get_error() to see what went
797 radlog(L_INFO, "rlm_eap_ttls: SSL_read Error");
798 return PW_AUTHENTICATION_REJECT;
801 t = (ttls_tunnel_t *) tls_session->opaque;
804 * If there's no data, maybe this is an ACK to an
808 if (t->authenticated) {
809 DEBUG2(" TTLS: Got ACK, and the user was already authenticated.");
810 return PW_AUTHENTICATION_ACK;
811 } /* else no session, no data, die. */
814 * FIXME: Call SSL_get_error() to see what went
817 radlog(L_INFO, "rlm_eap_ttls: SSL_read Error");
818 return PW_AUTHENTICATION_REJECT;
821 data_len = tls_session->clean_out.used = err;
822 data = tls_session->clean_out.data;
825 if (debug_flag > 2) {
826 for (i = 0; i < data_len; i++) {
827 if ((i & 0x0f) == 0) printf(" TTLS tunnel data in %04x: ", i);
829 printf("%02x ", data[i]);
831 if ((i & 0x0f) == 0x0f) printf("\n");
833 if ((data_len & 0x0f) != 0) printf("\n");
837 if (!diameter_verify(data, (int) data_len)) {
838 return PW_AUTHENTICATION_REJECT;
842 * Allocate a fake REQUEST structe.
844 fake = request_alloc_fake(request);
846 rad_assert(fake->packet->vps == NULL);
849 * Add the tunneled attributes to the fake request.
851 fake->packet->vps = diameter2vp(tls_session->ssl, data, data_len);
852 if (!fake->packet->vps) {
853 return PW_AUTHENTICATION_REJECT;
857 * Tell the request that it's a fake one.
859 vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
861 pairadd(&fake->packet->vps, vp);
865 if (debug_flag > 0) {
866 printf(" TTLS: Got tunneled request\n");
868 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
869 putchar('\t');vp_print(stdout, vp);putchar('\n');
875 * Update other items in the REQUEST data structure.
877 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
878 fake->password = pairfind(fake->packet->vps, PW_PASSWORD);
881 * No User-Name, try to create one from stored data.
883 if (!fake->username) {
885 * No User-Name in the stored data, look for
886 * an EAP-Identity, and pull it out of there.
889 vp = pairfind(fake->packet->vps, PW_EAP_MESSAGE);
891 (vp->length >= EAP_HEADER_LEN + 2) &&
892 (vp->strvalue[0] == PW_EAP_RESPONSE) &&
893 (vp->strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
894 (vp->strvalue[EAP_HEADER_LEN + 1] != 0)) {
896 * Create & remember a User-Name
898 t->username = pairmake("User-Name", "", T_OP_EQ);
899 rad_assert(t->username != NULL);
901 memcpy(t->username->strvalue, vp->strvalue + 5,
903 t->username->length = vp->length - 5;
904 t->username->strvalue[t->username->length] = 0;
906 DEBUG2(" TTLS: Got tunneled identity of %s",
907 t->username->strvalue);
910 * If there's a default EAP type,
913 if (t->default_eap_type != 0) {
914 DEBUG2(" TTLS: Setting default EAP type for tunneled EAP session.");
915 vp = paircreate(PW_EAP_TYPE,
917 rad_assert(vp != NULL);
918 vp->lvalue = t->default_eap_type;
919 pairadd(&fake->config_items, vp);
924 * Don't reject the request outright,
925 * as it's permitted to do EAP without
928 DEBUG2(" rlm_eap_ttls: WARNING! No EAP-Identity found to start EAP conversation.");
930 } /* else there WAS a t->username */
933 vp = paircopy(t->username);
934 pairadd(&fake->packet->vps, vp);
935 fake->username = pairfind(fake->packet->vps, PW_USER_NAME);
937 } /* else the request ALREADY had a User-Name */
940 * Add the State attribute, too, if it exists.
943 DEBUG2(" TTLS: Adding old state with %02x %02x",
944 t->state->strvalue[0], t->state->strvalue[1]);
945 vp = paircopy(t->state);
946 if (vp) pairadd(&fake->packet->vps, vp);
950 * If this is set, we copy SOME of the request attributes
951 * from outside of the tunnel to inside of the tunnel.
953 * We copy ONLY those attributes which do NOT already
954 * exist in the tunneled request.
956 if (t->copy_request_to_tunnel) {
959 for (vp = request->packet->vps; vp != NULL; vp = vp->next) {
961 * The attribute is a server-side thingy,
964 if ((vp->attribute > 255) &&
965 (((vp->attribute >> 16) & 0xffff) == 0)) {
970 * The outside attribute is already in the
971 * tunnel, don't copy it.
973 * This works for BOTH attributes which
974 * are originally in the tunneled request,
975 * AND attributes which are copied there
978 if (pairfind(fake->packet->vps, vp->attribute)) {
983 * Some attributes are handled specially.
985 switch (vp->attribute) {
987 * NEVER copy Message-Authenticator,
988 * EAP-Message, or State. They're
989 * only for outside of the tunnel.
992 case PW_USER_PASSWORD:
993 case PW_CHAP_PASSWORD:
994 case PW_CHAP_CHALLENGE:
996 case PW_MESSAGE_AUTHENTICATOR:
1003 * By default, copy it over.
1010 * Don't copy from the head, we've already
1013 copy = paircopy2(vp, vp->attribute);
1014 pairadd(&fake->packet->vps, copy);
1019 if (debug_flag > 0) {
1020 printf(" TTLS: Sending tunneled request\n");
1022 for (vp = fake->packet->vps; vp != NULL; vp = vp->next) {
1023 putchar('\t');vp_print(stdout, vp);putchar('\n');
1029 * Call authentication recursively, which will
1030 * do PAP, CHAP, MS-CHAP, etc.
1032 rad_authenticate(fake);
1035 * Note that we don't do *anything* with the reply
1039 if (debug_flag > 0) {
1040 printf(" TTLS: Got tunneled reply RADIUS code %d\n",
1043 for (vp = fake->reply->vps; vp != NULL; vp = vp->next) {
1044 putchar('\t');vp_print(stdout, vp);putchar('\n');
1050 * Decide what to do with the reply.
1052 switch (fake->reply->code) {
1053 case 0: /* No reply code, must be proxied... */
1054 vp = pairfind(fake->config_items, PW_PROXY_TO_REALM);
1056 eap_tunnel_data_t *tunnel;
1057 DEBUG2(" TTLS: Tunneled authentication will be proxied to %s", vp->strvalue);
1060 * Tell the original request that it's going
1063 pairmove2(&(request->config_items),
1064 &(fake->config_items),
1068 * Seed the proxy packet with the
1071 rad_assert(request->proxy == NULL);
1072 request->proxy = fake->packet;
1073 fake->packet = NULL;
1076 * Set up the callbacks for the tunnel
1078 tunnel = rad_malloc(sizeof(*tunnel));
1079 memset(tunnel, 0, sizeof(*tunnel));
1081 tunnel->tls_session = tls_session;
1082 tunnel->callback = eapttls_postproxy;
1085 * Associate the callback with the request.
1087 rcode = request_data_add(request,
1089 REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1091 rad_assert(rcode == 0);
1094 * Didn't authenticate the packet, but
1095 * we're proxying it.
1097 rcode = RLM_MODULE_UPDATED;
1100 DEBUG2(" TTLS: No tunneled reply was found for request %d , and the request was not proxied: rejecting the user.",
1102 rcode = RLM_MODULE_REJECT;
1107 rcode = process_reply(handler, tls_session, request,
1112 request_free(&fake);