2 * eap_fast.c contains the interfaces that are called from the main handler
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 2016 Alan DeKok <aland@freeradius.org>
21 * Copyright 2016 The FreeRADIUS server project
27 #include "eap_fast_crypto.h"
28 #include <freeradius-devel/sha1.h>
29 #include <openssl/ssl.h>
30 #include <openssl/rand.h>
32 #define RANDFILL(x) do { rad_assert(sizeof(x) % sizeof(uint32_t) == 0); for (size_t i = 0; i < sizeof(x); i += sizeof(uint32_t)) *((uint32_t *)&x[i]) = fr_rand(); } while(0)
35 * Copyright (c) 2002-2016, Jouni Malinen <j@w1.fi> and contributors
36 * All Rights Reserved.
38 * These programs are licensed under the BSD license (the one with
39 * advertisement clause removed).
41 * this function shamelessly stolen from from hostap:src/crypto/tls_openssl.c
43 static int openssl_get_keyblock_size(REQUEST *request, SSL *ssl)
47 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
50 if (ssl->enc_read_ctx == NULL || ssl->enc_read_ctx->cipher == NULL ||
51 ssl->read_hash == NULL)
54 c = ssl->enc_read_ctx->cipher;
55 h = EVP_MD_CTX_md(ssl->read_hash);
57 md_size = EVP_MD_size(h);
59 md_size = ssl->s3->tmp.new_mac_secret_size;
63 RDEBUG2("OpenSSL: keyblock size: key_len=%d MD_size=%d "
64 "IV_len=%d", EVP_CIPHER_key_length(c), md_size,
65 EVP_CIPHER_iv_length(c));
66 return 2 * (EVP_CIPHER_key_length(c) +
68 EVP_CIPHER_iv_length(c));
70 const SSL_CIPHER *ssl_cipher;
73 ssl_cipher = SSL_get_current_cipher(ssl);
76 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
77 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
78 RDEBUG2("OpenSSL: cipher nid %d digest nid %d", cipher, digest);
79 if (cipher < 0 || digest < 0)
81 c = EVP_get_cipherbynid(cipher);
82 h = EVP_get_digestbynid(digest);
86 RDEBUG2("OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
87 EVP_CIPHER_key_length(c), EVP_MD_size(h),
88 EVP_CIPHER_iv_length(c));
89 return 2 * (EVP_CIPHER_key_length(c) + EVP_MD_size(h) +
90 EVP_CIPHER_iv_length(c));
95 * RFC 4851 section 5.1 - EAP-FAST Authentication Phase 1: Key Derivations
97 static void eap_fast_init_keys(REQUEST *request, tls_session_t *tls_session)
99 eap_fast_tunnel_t *t = tls_session->opaque;
104 RDEBUG2("Deriving EAP-FAST keys");
106 rad_assert(t->simck == NULL);
108 ksize = openssl_get_keyblock_size(request, tls_session->ssl);
109 rad_assert(ksize > 0);
110 buf = talloc_size(request, ksize + sizeof(*t->keyblock));
111 scratch = talloc_size(request, ksize + sizeof(*t->keyblock));
113 t->keyblock = talloc(request, eap_fast_keyblock_t);
115 eap_fast_tls_gen_challenge(tls_session->ssl, buf, scratch, ksize + sizeof(*t->keyblock), "key expansion");
116 memcpy(t->keyblock, &buf[ksize], sizeof(*t->keyblock));
117 memset(buf, 0, ksize + sizeof(*t->keyblock));
119 t->simck = talloc_size(request, EAP_FAST_SIMCK_LEN);
120 memcpy(t->simck, t->keyblock, EAP_FAST_SKS_LEN); /* S-IMCK[0] = session_key_seed */
122 t->cmk = talloc_size(request, EAP_FAST_CMK_LEN); /* note that CMK[0] is not defined */
126 talloc_free(scratch);
130 * RFC 4851 section 5.2 - Intermediate Compound Key Derivations
132 static void eap_fast_update_icmk(REQUEST *request, tls_session_t *tls_session, uint8_t *msk)
134 eap_fast_tunnel_t *t = tls_session->opaque;
135 uint8_t imck[EAP_FAST_SIMCK_LEN + EAP_FAST_CMK_LEN];
137 RDEBUG2("Updating ICMK");
139 T_PRF(t->simck, EAP_FAST_SIMCK_LEN, "Inner Methods Compound Keys", msk, 32, imck, sizeof(imck));
141 memcpy(t->simck, imck, EAP_FAST_SIMCK_LEN);
142 memcpy(t->cmk, &imck[EAP_FAST_SIMCK_LEN], EAP_FAST_CMK_LEN);
146 * Calculate MSK/EMSK at the same time as they are coupled to ICMK
148 * RFC 4851 section 5.4 - EAP Master Session Key Generation
150 t->msk = talloc_size(request, EAP_FAST_KEY_LEN);
151 T_PRF(t->simck, EAP_FAST_SIMCK_LEN, "Session Key Generating Function", NULL, 0, t->msk, EAP_FAST_KEY_LEN);
153 t->emsk = talloc_size(request, EAP_EMSK_LEN);
154 T_PRF(t->simck, EAP_FAST_SIMCK_LEN, "Extended Session Key Generating Function", NULL, 0, t->emsk, EAP_EMSK_LEN);
157 void eap_fast_tlv_append(tls_session_t *tls_session, int tlv, bool mandatory, int length, const void *data)
161 hdr[0] = (mandatory) ? htons(tlv | EAP_FAST_TLV_MANDATORY) : htons(tlv);
162 hdr[1] = htons(length);
164 tls_session->record_plus(&tls_session->clean_in, &hdr, 4);
165 tls_session->record_plus(&tls_session->clean_in, data, length);
168 static void eap_fast_send_error(tls_session_t *tls_session, int error)
171 value = htonl(error);
173 eap_fast_tlv_append(tls_session, EAP_FAST_TLV_ERROR, true, sizeof(value), &value);
176 static void eap_fast_append_result(tls_session_t *tls_session, PW_CODE code)
178 eap_fast_tunnel_t *t = (eap_fast_tunnel_t *) tls_session->opaque;
180 int type = (t->result_final)
181 ? EAP_FAST_TLV_RESULT
182 : EAP_FAST_TLV_INTERMED_RESULT;
184 uint16_t state = (code == PW_CODE_ACCESS_REJECT)
185 ? EAP_FAST_TLV_RESULT_FAILURE
186 : EAP_FAST_TLV_RESULT_SUCCESS;
187 state = htons(state);
189 eap_fast_tlv_append(tls_session, type, true, sizeof(state), &state);
192 static void eap_fast_send_identity_request(REQUEST *request, tls_session_t *tls_session, eap_handler_t *eap_session)
194 eap_packet_raw_t eap_packet;
196 RDEBUG("Sending EAP-Identity");
198 eap_packet.code = PW_EAP_REQUEST;
199 eap_packet.id = eap_session->eap_ds->response->id + 1;
200 eap_packet.length[0] = 0;
201 eap_packet.length[1] = EAP_HEADER_LEN + 1;
202 eap_packet.data[0] = PW_EAP_IDENTITY;
204 eap_fast_tlv_append(tls_session, EAP_FAST_TLV_EAP_PAYLOAD, true, sizeof(eap_packet), &eap_packet);
207 static void eap_fast_send_pac_tunnel(REQUEST *request, tls_session_t *tls_session)
209 eap_fast_tunnel_t *t = tls_session->opaque;
211 eap_fast_attr_pac_opaque_plaintext_t opaque_plaintext;
214 memset(&pac, 0, sizeof(pac));
215 memset(&opaque_plaintext, 0, sizeof(opaque_plaintext));
217 RDEBUG("Sending Tunnel PAC");
219 pac.key.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_PAC_KEY);
220 pac.key.hdr.length = htons(sizeof(pac.key.data));
221 rad_assert(sizeof(pac.key.data) % sizeof(uint32_t) == 0);
222 RANDFILL(pac.key.data);
224 pac.info.lifetime.hdr.type = htons(PAC_INFO_PAC_LIFETIME);
225 pac.info.lifetime.hdr.length = htons(sizeof(pac.info.lifetime.data));
226 pac.info.lifetime.data = htonl(time(NULL) + t->pac_lifetime);
228 pac.info.a_id.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_A_ID);
229 pac.info.a_id.hdr.length = htons(sizeof(pac.info.a_id.data));
230 memcpy(pac.info.a_id.data, t->a_id, sizeof(pac.info.a_id.data));
232 pac.info.a_id_info.hdr.type = htons(PAC_INFO_A_ID_INFO);
233 pac.info.a_id_info.hdr.length = htons(sizeof(pac.info.a_id_info.data));
234 #define MIN(a,b) (((a)>(b)) ? (b) : (a))
235 alen = MIN(talloc_array_length(t->authority_identity) - 1, sizeof(pac.info.a_id_info.data));
236 memcpy(pac.info.a_id_info.data, t->authority_identity, alen);
238 pac.info.type.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_PAC_TYPE);
239 pac.info.type.hdr.length = htons(sizeof(pac.info.type.data));
240 pac.info.type.data = htons(PAC_TYPE_TUNNEL);
242 pac.info.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_PAC_INFO);
243 pac.info.hdr.length = htons(sizeof(pac.info.lifetime)
244 + sizeof(pac.info.a_id)
245 + sizeof(pac.info.a_id_info)
246 + sizeof(pac.info.type));
248 memcpy(&opaque_plaintext.type, &pac.info.type, sizeof(opaque_plaintext.type));
249 memcpy(&opaque_plaintext.lifetime, &pac.info.lifetime, sizeof(opaque_plaintext.lifetime));
250 memcpy(&opaque_plaintext.key, &pac.key, sizeof(opaque_plaintext.key));
253 rad_assert(PAC_A_ID_LENGTH <= EVP_GCM_TLS_TAG_LEN);
254 memcpy(pac.opaque.aad, t->a_id, PAC_A_ID_LENGTH);
255 rad_assert(RAND_bytes(pac.opaque.iv, sizeof(pac.opaque.iv)) != 0);
256 dlen = eap_fast_encrypt((unsigned const char *)&opaque_plaintext, sizeof(opaque_plaintext),
257 t->a_id, PAC_A_ID_LENGTH, t->pac_opaque_key, pac.opaque.iv,
258 pac.opaque.data, pac.opaque.tag);
260 pac.opaque.hdr.type = htons(EAP_FAST_TLV_MANDATORY | PAC_INFO_PAC_OPAQUE);
261 pac.opaque.hdr.length = htons(sizeof(pac.opaque) - sizeof(pac.opaque.hdr) - sizeof(pac.opaque.data) + dlen);
263 eap_fast_tlv_append(tls_session, EAP_FAST_TLV_MANDATORY | EAP_FAST_TLV_PAC, true,
264 sizeof(pac) - sizeof(pac.opaque.data) + dlen, &pac);
267 static void eap_fast_append_crypto_binding(REQUEST *request, tls_session_t *tls_session)
269 eap_fast_tunnel_t *t = tls_session->opaque;
270 eap_tlv_crypto_binding_tlv_t binding;
271 memset(&binding, 0, sizeof(eap_tlv_crypto_binding_tlv_t));
272 const int len = sizeof(binding) - (&binding.reserved - (uint8_t *)&binding);
274 RDEBUG("Sending Cryptobinding");
276 binding.tlv_type = htons(EAP_FAST_TLV_MANDATORY | EAP_FAST_TLV_CRYPTO_BINDING);
277 binding.length = htons(len);
278 binding.version = EAP_FAST_VERSION;
279 binding.received_version = EAP_FAST_VERSION; /* FIXME use the clients value */
280 binding.subtype = EAP_FAST_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
282 rad_assert(sizeof(binding.nonce) % sizeof(uint32_t) == 0);
283 RANDFILL(binding.nonce);
284 binding.nonce[sizeof(binding.nonce) - 1] &= ~0x01; /* RFC 4851 section 4.2.8 */
287 fr_hmac_sha1(binding.compound_mac, (uint8_t *)&binding, sizeof(binding), t->cmk, EAP_FAST_CMK_LEN);
289 eap_fast_tlv_append(tls_session, EAP_FAST_TLV_CRYPTO_BINDING, true, len, &binding.reserved);
292 static int eap_fast_verify(REQUEST *request, tls_session_t *tls_session, uint8_t const *data, unsigned int data_len)
296 unsigned int remaining = data_len;
298 int num[EAP_FAST_TLV_MAX] = {0};
299 eap_fast_tunnel_t *t = (eap_fast_tunnel_t *) tls_session->opaque;
300 uint32_t present = 0;
302 rad_assert(sizeof(present) * 8 > EAP_FAST_TLV_MAX);
304 while (remaining > 0) {
306 RDEBUG2("EAP-FAST TLV is too small (%u) to contain a EAP-FAST TLV header", remaining);
310 memcpy(&attr, data, sizeof(attr));
311 attr = ntohs(attr) & EAP_FAST_TLV_TYPE;
314 case EAP_FAST_TLV_RESULT:
315 case EAP_FAST_TLV_NAK:
316 case EAP_FAST_TLV_ERROR:
317 case EAP_FAST_TLV_VENDOR_SPECIFIC:
318 case EAP_FAST_TLV_EAP_PAYLOAD:
319 case EAP_FAST_TLV_INTERMED_RESULT:
320 case EAP_FAST_TLV_PAC:
321 case EAP_FAST_TLV_CRYPTO_BINDING:
323 present |= 1 << attr;
325 if (num[EAP_FAST_TLV_EAP_PAYLOAD] > 1) {
326 RDEBUG("Too many EAP-Payload TLVs");
328 for (int i = 0; i < EAP_FAST_TLV_MAX; i++)
329 if (present & (1 << i))
330 RDEBUG(" - attribute %d is present", i);
331 eap_fast_send_error(tls_session, EAP_FAST_ERR_UNEXPECTED_TLV);
335 if (num[EAP_FAST_TLV_INTERMED_RESULT] > 1) {
336 RDEBUG("Too many Intermediate-Result TLVs");
341 if ((data[0] & 0x80) != 0) {
342 RDEBUG("Unknown mandatory TLV %02x", attr);
351 memcpy(&length, data + 2, sizeof(length));
352 length = ntohs(length);
357 if (length > remaining) {
358 RDEBUG2("EAP-FAST TLV %u is longer than room remaining in the packet (%u > %u).", attr,
364 * If the rest of the TLVs are larger than
365 * this attribute, continue.
367 * Otherwise, if the attribute over-flows the end
370 if (remaining < length) {
371 RDEBUG2("EAP-FAST TLV overflows packet!");
376 * If there's an error, we bail out of the
377 * authentication process before allocating
380 if ((attr == EAP_FAST_TLV_INTERMED_RESULT) || (attr == EAP_FAST_TLV_RESULT)) {
384 RDEBUG("EAP-FAST TLV %u is too short. Expected 2, got %d.", attr, length);
388 memcpy(&status, data, 2);
389 status = ntohs(status);
391 if (status == EAP_FAST_TLV_RESULT_FAILURE) {
392 RDEBUG("EAP-FAST TLV %u indicates failure. Rejecting request.", attr);
396 if (status != EAP_FAST_TLV_RESULT_SUCCESS) {
397 RDEBUG("EAP-FAST TLV %u contains unknown value. Rejecting request.", attr);
403 * remaining > length, continue.
410 * Check if the peer mixed & matched TLVs.
412 if ((num[EAP_FAST_TLV_NAK] > 0) && (num[EAP_FAST_TLV_NAK] != total)) {
413 RDEBUG("NAK TLV sent with non-NAK TLVs. Rejecting request.");
417 if (num[EAP_FAST_TLV_INTERMED_RESULT] > 0 && num[EAP_FAST_TLV_RESULT]) {
418 RDEBUG("NAK TLV sent with non-NAK TLVs. Rejecting request.");
423 * Check mandatory or not mandatory TLVs.
426 case TLS_SESSION_HANDSHAKE:
428 RDEBUG("Unexpected TLVs in TLS Session Handshake stage");
433 if (present != 1 << EAP_FAST_TLV_EAP_PAYLOAD) {
434 RDEBUG("Unexpected TLVs in authentication stage");
438 case CRYPTOBIND_CHECK:
440 uint32_t bits = (t->result_final)
441 ? 1 << EAP_FAST_TLV_RESULT
442 : 1 << EAP_FAST_TLV_INTERMED_RESULT;
443 if (present & ~(bits | (1 << EAP_FAST_TLV_CRYPTO_BINDING) | (1 << EAP_FAST_TLV_PAC))) {
444 RDEBUG("Unexpected TLVs in cryptobind checking stage");
450 if (present & ~((1 << EAP_FAST_TLV_PAC) | (1 << EAP_FAST_TLV_RESULT))) {
451 RDEBUG("Unexpected TLVs in provisioning stage");
457 RDEBUG("Unexpected TLVs in complete stage");
462 RDEBUG("Unexpected stage %d", t->stage);
467 * We got this far. It looks OK.
472 static ssize_t eap_fast_decode_vp(TALLOC_CTX *request, DICT_ATTR const *parent,
473 uint8_t const *data, size_t const attr_len, VALUE_PAIR **out)
475 int8_t tag = TAG_NONE;
477 uint8_t const *p = data;
480 * FIXME: Attrlen can be larger than 253 for extended attrs!
482 if (!parent || !out ) {
483 RERROR("eap_fast_decode_vp: Invalid arguments");
488 * Silently ignore zero-length attributes.
490 if (attr_len == 0) return 0;
493 * And now that we've verified the basic type
494 * information, decode the actual p.
496 vp = fr_pair_afrom_da(request, parent);
499 vp->vp_length = attr_len;
502 switch (parent->type) {
504 fr_pair_value_bstrncpy(vp, p, attr_len);
508 fr_pair_value_memcpy(vp, p, attr_len);
511 case PW_TYPE_ABINARY:
512 if (vp->vp_length > sizeof(vp->vp_filter)) {
513 vp->vp_length = sizeof(vp->vp_filter);
515 memcpy(vp->vp_filter, p, vp->vp_length);
523 vp->vp_short = (p[0] << 8) | p[1];
526 case PW_TYPE_INTEGER:
527 memcpy(&vp->vp_integer, p, 4);
528 vp->vp_integer = ntohl(vp->vp_integer);
531 case PW_TYPE_INTEGER64:
532 memcpy(&vp->vp_integer64, p, 8);
533 vp->vp_integer64 = ntohll(vp->vp_integer64);
537 memcpy(&vp->vp_date, p, 4);
538 vp->vp_date = ntohl(vp->vp_date);
541 case PW_TYPE_ETHERNET:
542 memcpy(vp->vp_ether, p, 6);
545 case PW_TYPE_IPV4_ADDR:
546 memcpy(&vp->vp_ipaddr, p, 4);
550 memcpy(vp->vp_ifid, p, 8);
553 case PW_TYPE_IPV6_ADDR:
554 memcpy(&vp->vp_ipv6addr, p, 16);
557 case PW_TYPE_IPV6_PREFIX:
559 * FIXME: double-check that
560 * (vp->vp_octets[1] >> 3) matches vp->vp_length + 2
562 memcpy(vp->vp_ipv6prefix, p, vp->vp_length);
563 if (vp->vp_length < 18) {
564 memset(((uint8_t *)vp->vp_ipv6prefix) + vp->vp_length, 0,
569 case PW_TYPE_IPV4_PREFIX:
570 /* FIXME: do the same double-check as for IPv6Prefix */
571 memcpy(vp->vp_ipv4prefix, p, vp->vp_length);
574 * /32 means "keep all bits". Otherwise, mask
577 if ((p[1] & 0x3f) > 32) {
580 memcpy(&addr, vp->vp_octets + 2, sizeof(addr));
582 mask <<= (32 - (p[1] & 0x3f));
587 memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
591 case PW_TYPE_SIGNED: /* overloaded with vp_integer */
592 memcpy(&vp->vp_integer, p, 4);
593 vp->vp_integer = ntohl(vp->vp_integer);
597 RERROR("eap_fast_decode_vp: type %d Internal sanity check %d ", parent->type, __LINE__);
598 fr_pair_list_free(&vp);
607 VALUE_PAIR *eap_fast_fast2vp(REQUEST *request, SSL *ssl, uint8_t const *data, size_t data_len,
608 DICT_ATTR const *fast_da, vp_cursor_t *out)
612 size_t data_left = data_len;
613 VALUE_PAIR *first = NULL;
614 VALUE_PAIR *vp = NULL;
618 fast_da = dict_attrbyvalue(PW_FREERADIUS_EAP_FAST_TLV, VENDORPEC_FREERADIUS);
619 rad_assert(fast_da != NULL);
622 out = talloc(request, vp_cursor_t);
623 rad_assert(out != NULL);
624 fr_cursor_init(out, &first);
630 while (data_left > 0) {
633 /* FIXME do something with mandatory */
635 memcpy(&attr, data, sizeof(attr));
636 attr = ntohs(attr) & EAP_FAST_TLV_TYPE;
638 memcpy(&length, data + 2, sizeof(length));
639 length = ntohs(length);
647 * For now, if it doesn't exist, ignore it.
649 da = dict_attrbyparent(fast_da, attr, fast_da->vendor);
651 RDEBUG("eap_fast_fast2vp: no sub attribute found %s attr: %u vendor: %u",
652 fast_da->name, attr, fast_da->vendor);
655 if (da->type == PW_TYPE_TLV) {
656 eap_fast_fast2vp(request, ssl, data, length, da, out);
659 decoded = eap_fast_decode_vp(request, da, data, length, &vp);
661 RERROR("Failed decoding %s: %s", da->name, fr_strerror());
665 fr_cursor_merge(out, vp);
668 while (fr_cursor_next(out)) {
677 * We got this far. It looks OK.
683 static void eap_vp2fast(tls_session_t *tls_session, VALUE_PAIR *first)
688 (void) fr_cursor_init(&cursor, &first);
690 while ((vp = fr_cursor_next_by_num(&cursor, PW_EAP_MESSAGE, 0, TAG_ANY)) != NULL) {
691 eap_fast_tlv_append(tls_session, EAP_FAST_TLV_EAP_PAYLOAD, true, vp->vp_length, vp->vp_octets);
695 static void eapfast_copy_request_to_tunnel(REQUEST *request, REQUEST *fake) {
696 VALUE_PAIR *copy, *vp;
699 for (vp = fr_cursor_init(&cursor, &request->packet->vps);
701 vp = fr_cursor_next(&cursor)) {
703 * The attribute is a server-side thingy,
706 if ((vp->da->attr > 255) && (((vp->da->attr >> 16) & 0xffff) == 0)) {
711 * The outside attribute is already in the
712 * tunnel, don't copy it.
714 * This works for BOTH attributes which
715 * are originally in the tunneled request,
716 * AND attributes which are copied there
719 if (fr_pair_find_by_da(fake->packet->vps, vp->da, TAG_ANY)) continue;
722 * Some attributes are handled specially.
724 if (!vp->da->vendor) switch (vp->da->attr) {
726 * NEVER copy Message-Authenticator,
727 * EAP-Message, or State. They're
728 * only for outside of the tunnel.
731 case PW_USER_PASSWORD:
732 case PW_CHAP_PASSWORD:
733 case PW_CHAP_CHALLENGE:
735 case PW_MESSAGE_AUTHENTICATOR:
741 * By default, copy it over.
748 * Don't copy from the head, we've already
751 copy = fr_pair_list_copy_by_num(fake->packet, vp, vp->da->attr, vp->da->vendor, TAG_ANY);
752 fr_pair_add(&fake->packet->vps, copy);
757 * Use a reply packet to determine what to do.
759 static rlm_rcode_t CC_HINT(nonnull) process_reply( eap_handler_t *eap_session,
760 tls_session_t *tls_session,
761 REQUEST *request, RADIUS_PACKET *reply)
763 rlm_rcode_t rcode = RLM_MODULE_REJECT;
764 VALUE_PAIR *vp, *tunnel_vps = NULL;
766 vp_cursor_t to_tunnel;
768 eap_fast_tunnel_t *t = tls_session->opaque;
770 rad_assert(eap_session->request == request);
773 * If the response packet was Access-Accept, then
774 * we're OK. If not, die horribly.
776 * FIXME: Take MS-CHAP2-Success attribute, and
777 * tunnel it back to the client, to authenticate
778 * ourselves to the client.
780 * FIXME: If we have an Access-Challenge, then
781 * the Reply-Message is tunneled back to the client.
783 * FIXME: If we have an EAP-Message, then that message
784 * must be tunneled back to the client.
786 * FIXME: If we have an Access-Challenge with a State
787 * attribute, then do we tunnel that to the client, or
788 * keep track of it ourselves?
790 * FIXME: EAP-Messages can only start with 'identity',
791 * NOT 'eap start', so we should check for that....
793 switch (reply->code) {
794 case PW_CODE_ACCESS_ACCEPT:
795 RDEBUG("Got tunneled Access-Accept");
796 fr_cursor_init(&to_tunnel, &tunnel_vps);
797 rcode = RLM_MODULE_OK;
799 for (vp = fr_cursor_init(&cursor, &reply->vps); vp; vp = fr_cursor_next(&cursor)) {
800 if (vp->da->vendor != VENDORPEC_MICROSOFT) continue;
802 /* FIXME must be a better way to capture/re-derive this later for ISK */
803 switch (vp->da->attr) {
804 case PW_MSCHAP_MPPE_SEND_KEY:
805 memcpy(t->isk.mppe_send, vp->vp_octets, CHAP_VALUE_LENGTH);
808 case PW_MSCHAP_MPPE_RECV_KEY:
809 memcpy(t->isk.mppe_recv, vp->vp_octets, CHAP_VALUE_LENGTH);
812 case PW_MSCHAP2_SUCCESS:
813 RDEBUG("Got %s, tunneling it to the client in a challenge", vp->da->name);
814 rcode = RLM_MODULE_HANDLED;
815 if (t->use_tunneled_reply) {
816 t->authenticated = true;
818 * Clean up the tunneled reply.
820 fr_pair_delete_by_num(&reply->vps, PW_PROXY_STATE, 0, TAG_ANY);
821 fr_pair_delete_by_num(&reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
822 fr_pair_delete_by_num(&reply->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY);
825 * Delete MPPE keys & encryption policy. We don't
828 fr_pair_delete_by_num(&reply->vps, 7, VENDORPEC_MICROSOFT, TAG_ANY);
829 fr_pair_delete_by_num(&reply->vps, 8, VENDORPEC_MICROSOFT, TAG_ANY);
830 fr_pair_delete_by_num(&reply->vps, 16, VENDORPEC_MICROSOFT, TAG_ANY);
831 fr_pair_delete_by_num(&reply->vps, 17, VENDORPEC_MICROSOFT, TAG_ANY);
833 fr_pair_list_free(&t->accept_vps); /* for proxying MS-CHAP2 */
834 fr_pair_list_mcopy_by_num(t, &t->accept_vps, &reply->vps, 0, 0, TAG_ANY);
835 rad_assert(!reply->vps);
845 case PW_CODE_ACCESS_REJECT:
846 RDEBUG("Got tunneled Access-Reject");
847 rcode = RLM_MODULE_REJECT;
851 * Handle Access-Challenge, but only if we
852 * send tunneled reply data. This is because
853 * an Access-Challenge means that we MUST tunnel
854 * a Reply-Message to the client.
856 case PW_CODE_ACCESS_CHALLENGE:
857 RDEBUG("Got tunneled Access-Challenge");
860 * Keep the State attribute, if necessary.
862 * Get rid of the old State, too.
864 fr_pair_list_free(&t->state);
865 fr_pair_list_mcopy_by_num(t, &t->state, &reply->vps, PW_STATE, 0, TAG_ANY);
868 * We should really be a bit smarter about this,
869 * and move over only those attributes which
870 * are relevant to the authentication request,
871 * but that's a lot more work, and this "dumb"
872 * method works in 99.9% of the situations.
875 fr_pair_list_mcopy_by_num(t, &vp, &reply->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
878 * There MUST be a Reply-Message in the challenge,
879 * which we tunnel back to the client.
881 * If there isn't one in the reply VP's, then
882 * we MUST create one, with an empty string as
885 fr_pair_list_mcopy_by_num(t, &vp, &reply->vps, PW_REPLY_MESSAGE, 0, TAG_ANY);
888 RDEBUG("Sending tunneled reply attributes");
889 eap_vp2fast(tls_session, vp);
890 fr_pair_list_free(&vp);
893 rcode = RLM_MODULE_HANDLED;
897 RDEBUG("Unknown RADIUS packet type %d: rejecting tunneled user", reply->code);
898 rcode = RLM_MODULE_INVALID;
904 * Pack any tunnelled VPs and send them back
908 RDEBUG("Sending tunneled reply attributes");
909 rdebug_pair_list(L_DBG_LVL_2, request, tunnel_vps, NULL);
911 eap_vp2fast(tls_session, tunnel_vps);
912 fr_pair_list_free(&tunnel_vps);
918 static PW_CODE eap_fast_eap_payload(REQUEST *request, eap_handler_t *eap_session,
919 tls_session_t *tls_session, VALUE_PAIR *tlv_eap_payload)
921 PW_CODE code = PW_CODE_ACCESS_REJECT;
924 eap_fast_tunnel_t *t;
927 RDEBUG("Processing received EAP Payload");
930 * Allocate a fake REQUEST structure.
932 fake = request_alloc_fake(request);
933 rad_assert(!fake->packet->vps);
935 t = (eap_fast_tunnel_t *) tls_session->opaque;
938 * Add the tunneled attributes to the fake request.
941 fake->packet->vps = fr_pair_afrom_num(fake->packet, PW_EAP_MESSAGE, 0);
942 fr_pair_value_memcpy(fake->packet->vps, tlv_eap_payload->vp_octets, tlv_eap_payload->vp_length);
944 RDEBUG("Got tunneled request");
945 rdebug_pair_list(L_DBG_LVL_1, request, fake->packet->vps, NULL);
948 * Tell the request that it's a fake one.
950 fr_pair_make(fake->packet, &fake->packet->vps, "Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
953 * Update other items in the REQUEST data structure.
955 fake->username = fr_pair_find_by_num(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
956 fake->password = fr_pair_find_by_num(fake->packet->vps, PW_USER_PASSWORD, 0, TAG_ANY);
959 * No User-Name, try to create one from stored data.
961 if (!fake->username) {
963 * No User-Name in the stored data, look for
964 * an EAP-Identity, and pull it out of there.
967 vp = fr_pair_find_by_num(fake->packet->vps, PW_EAP_MESSAGE, 0, TAG_ANY);
969 (vp->vp_length >= EAP_HEADER_LEN + 2) &&
970 (vp->vp_strvalue[0] == PW_EAP_RESPONSE) &&
971 (vp->vp_strvalue[EAP_HEADER_LEN] == PW_EAP_IDENTITY) &&
972 (vp->vp_strvalue[EAP_HEADER_LEN + 1] != 0)) {
974 * Create & remember a User-Name
976 t->username = fr_pair_make(t, NULL, "User-Name", NULL, T_OP_EQ);
977 rad_assert(t->username != NULL);
979 fr_pair_value_bstrncpy(t->username, vp->vp_octets + 5, vp->vp_length - 5);
981 RDEBUG("Got tunneled identity of %s", t->username->vp_strvalue);
984 * Don't reject the request outright,
985 * as it's permitted to do EAP without
988 RWDEBUG2("No EAP-Identity found to start EAP conversation");
990 } /* else there WAS a t->username */
993 vp = fr_pair_list_copy(fake->packet, t->username);
994 fr_pair_add(&fake->packet->vps, vp);
997 } /* else the request ALREADY had a User-Name */
1000 * Add the State attribute, too, if it exists.
1003 vp = fr_pair_list_copy(fake->packet, t->state);
1004 if (vp) fr_pair_add(&fake->packet->vps, vp);
1008 if (t->stage == AUTHENTICATION) { /* FIXME do this only for MSCHAPv2 */
1011 RWDEBUG2("AUTHENTICATION");
1012 vp = fr_pair_make(fake, &fake->config, "EAP-Type", "0", T_OP_EQ);
1013 vp->vp_integer = t->default_method;
1014 RWDEBUG2("AUTHENTICATION");
1017 * RFC 5422 section 3.2.3 - Authenticating Using EAP-FAST-MSCHAPv2
1019 if (t->mode == EAP_FAST_PROVISIONING_ANON) {
1020 tvp = fr_pair_afrom_num(fake->packet, PW_MSCHAP_CHALLENGE, VENDORPEC_MICROSOFT);
1021 fr_pair_value_memcpy(tvp, t->keyblock->server_challenge, CHAP_VALUE_LENGTH);
1022 fr_pair_add(&fake->config, tvp);
1024 tvp = fr_pair_afrom_num(fake->packet, PW_MS_CHAP_PEER_CHALLENGE, 0);
1025 fr_pair_value_memcpy(tvp, t->keyblock->client_challenge, CHAP_VALUE_LENGTH);
1026 fr_pair_add(&fake->config, tvp);
1030 if (t->copy_request_to_tunnel) {
1031 eapfast_copy_request_to_tunnel(request, fake);
1034 if ((vp = fr_pair_find_by_num(request->config, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
1035 fake->server = vp->vp_strvalue;
1037 } else if (t->virtual_server) {
1038 fake->server = t->virtual_server;
1040 } /* else fake->server == request->server */
1043 * Call authentication recursively, which will
1044 * do PAP, CHAP, MS-CHAP, etc.
1046 rad_virtual_server(fake);
1049 * Decide what to do with the reply.
1051 switch (fake->reply->code) {
1052 case 0: /* No reply code, must be proxied... */
1054 vp = fr_pair_find_by_num(fake->config, PW_PROXY_TO_REALM, 0, TAG_ANY);
1057 eap_tunnel_data_t *tunnel;
1059 RDEBUG("Tunneled authentication will be proxied to %s", vp->vp_strvalue);
1062 * Tell the original request that it's going
1065 fr_pair_list_mcopy_by_num(request, &request->config, &fake->config, PW_PROXY_TO_REALM, 0,
1069 * Seed the proxy packet with the
1072 rad_assert(!request->proxy);
1074 request->proxy = talloc_steal(request, fake->packet);
1076 memset(&request->proxy->src_ipaddr, 0,
1077 sizeof(request->proxy->src_ipaddr));
1078 memset(&request->proxy->src_ipaddr, 0,
1079 sizeof(request->proxy->src_ipaddr));
1080 request->proxy->src_port = 0;
1081 request->proxy->dst_port = 0;
1082 fake->packet = NULL;
1083 rad_free(&fake->reply);
1087 * Set up the callbacks for the tunnel
1089 tunnel = talloc_zero(request, eap_tunnel_data_t);
1090 tunnel->tls_session = tls_session;
1093 * Associate the callback with the request.
1095 ret = request_data_add(request, request->proxy, REQUEST_DATA_EAP_TUNNEL_CALLBACK,
1097 rad_assert(ret == 0);
1100 * rlm_eap.c has taken care of associating
1101 * the eap_session with the fake request.
1103 * So we associate the fake request with
1106 ret = request_data_add(request, request->proxy, REQUEST_DATA_EAP_MSCHAP_TUNNEL_CALLBACK,
1108 rad_assert(ret == 0);
1113 * Didn't authenticate the packet, but
1114 * we're proxying it.
1116 code = PW_CODE_STATUS_CLIENT;
1119 #endif /* WITH_PROXY */
1121 RDEBUG("No tunneled reply was found, and the request was not proxied: rejecting the user.");
1122 code = PW_CODE_ACCESS_REJECT;
1128 * Returns RLM_MODULE_FOO, and we want to return PW_FOO
1130 rcode = process_reply(eap_session, tls_session, request, fake->reply);
1132 case RLM_MODULE_REJECT:
1133 code = PW_CODE_ACCESS_REJECT;
1136 case RLM_MODULE_HANDLED:
1137 code = PW_CODE_ACCESS_CHALLENGE;
1141 code = PW_CODE_ACCESS_ACCEPT;
1145 code = PW_CODE_ACCESS_REJECT;
1156 static PW_CODE eap_fast_crypto_binding(REQUEST *request, UNUSED eap_handler_t *eap_session,
1157 tls_session_t *tls_session, eap_tlv_crypto_binding_tlv_t *binding)
1159 uint8_t cmac[sizeof(binding->compound_mac)];
1160 eap_fast_tunnel_t *t = tls_session->opaque;
1162 memcpy(cmac, binding->compound_mac, sizeof(cmac));
1163 memset(binding->compound_mac, 0, sizeof(binding->compound_mac));
1166 fr_hmac_sha1(binding->compound_mac, (uint8_t *)binding, sizeof(*binding), t->cmk, EAP_FAST_CMK_LEN);
1167 if (memcmp(binding->compound_mac, cmac, sizeof(cmac))) {
1168 RDEBUG2("Crypto-Binding TLV mis-match");
1169 return PW_CODE_ACCESS_REJECT;
1172 return PW_CODE_ACCESS_ACCEPT;
1176 #define PW_EAP_FAST_TLV_PAC (PW_FREERADIUS_EAP_FAST_TLV | (EAP_FAST_TLV_PAC << 8))
1180 static PW_CODE eap_fast_process_tlvs(REQUEST *request, eap_handler_t *eap_session,
1181 tls_session_t *tls_session, VALUE_PAIR *fast_vps)
1183 eap_fast_tunnel_t *t = (eap_fast_tunnel_t *) tls_session->opaque;
1186 eap_tlv_crypto_binding_tlv_t *binding = NULL;
1188 for (vp = fr_cursor_init(&cursor, &fast_vps); vp; vp = fr_cursor_next(&cursor)) {
1189 PW_CODE code = PW_CODE_ACCESS_REJECT;
1191 DICT_ATTR const *parent_da = NULL;
1192 parent_da = dict_parent(vp->da->attr, vp->da->vendor);
1193 if (parent_da == NULL || vp->da->vendor != VENDORPEC_FREERADIUS ||
1194 ((vp->da->attr & 0xff) != PW_FREERADIUS_EAP_FAST_TLV)) {
1195 value = vp_aprints(request->packet, vp, '"');
1196 RDEBUG2("ignoring non-EAP-FAST TLV %s", value);
1201 switch (parent_da->attr) {
1202 case PW_FREERADIUS_EAP_FAST_TLV:
1203 switch (vp->da->attr >> 8) {
1204 case EAP_FAST_TLV_EAP_PAYLOAD:
1205 code = eap_fast_eap_payload(request, eap_session, tls_session, vp);
1206 if (code == PW_CODE_ACCESS_ACCEPT)
1207 t->stage = CRYPTOBIND_CHECK;
1209 case EAP_FAST_TLV_RESULT:
1210 case EAP_FAST_TLV_INTERMED_RESULT:
1211 code = PW_CODE_ACCESS_ACCEPT;
1212 t->stage = PROVISIONING;
1214 case EAP_FAST_TLV_CRYPTO_BINDING:
1216 binding = talloc_zero(request->packet, eap_tlv_crypto_binding_tlv_t);
1217 memcpy(binding, vp->vp_octets, sizeof(*binding));
1218 binding->tlv_type = htons(EAP_FAST_TLV_MANDATORY | EAP_FAST_TLV_CRYPTO_BINDING);
1219 binding->length = htons(sizeof(*binding) - 2 * sizeof(uint16_t));
1223 value = vp_aprints_value(request->packet, vp, '"');
1224 RDEBUG2("ignoring unknown %s", value);
1229 case PW_EAP_FAST_TLV_PAC:
1230 switch ( ( vp->da->attr >> 16 )) {
1231 case PAC_INFO_PAC_ACK:
1232 if (vp->vp_integer == EAP_FAST_TLV_RESULT_SUCCESS) {
1233 code = PW_CODE_ACCESS_ACCEPT;
1234 t->pac.expires = UINT32_MAX;
1235 t->pac.expired = false;
1236 t->stage = COMPLETE;
1239 case PAC_INFO_PAC_TYPE:
1240 if (vp->vp_integer != PAC_TYPE_TUNNEL) {
1241 RDEBUG("only able to serve Tunnel PAC's, ignoring request");
1247 value = vp_aprints(request->packet, vp, '"');
1248 RDEBUG2("ignoring unknown EAP-FAST-PAC-TLV %s", value);
1254 value = vp_aprints(request->packet, vp, '"');
1255 RDEBUG2("ignoring EAP-FAST TLV %s", value);
1260 if (code == PW_CODE_ACCESS_REJECT)
1261 return PW_CODE_ACCESS_REJECT;
1265 PW_CODE code = eap_fast_crypto_binding(request, eap_session, tls_session, binding);
1266 if (code == PW_CODE_ACCESS_ACCEPT)
1267 t->stage = PROVISIONING;
1270 return PW_CODE_ACCESS_ACCEPT;
1275 * Process the inner tunnel data
1277 PW_CODE eap_fast_process(eap_handler_t *eap_session, tls_session_t *tls_session)
1280 VALUE_PAIR *fast_vps;
1281 uint8_t const *data;
1283 eap_fast_tunnel_t *t;
1284 REQUEST *request = eap_session->request;
1287 * Just look at the buffer directly, without doing
1290 data_len = tls_session->clean_out.used;
1291 tls_session->clean_out.used = 0;
1292 data = tls_session->clean_out.data;
1294 t = (eap_fast_tunnel_t *) tls_session->opaque;
1297 * See if the tunneled data is well formed.
1299 if (!eap_fast_verify(request, tls_session, data, data_len)) return PW_CODE_ACCESS_REJECT;
1301 if (t->stage == TLS_SESSION_HANDSHAKE) {
1302 rad_assert(t->mode == EAP_FAST_UNKNOWN);
1305 if (strstr(SSL_CIPHER_description(SSL_get_current_cipher(tls_session->ssl),
1306 buf, sizeof(buf)), "Au=None")) {
1307 /* FIXME enforce MSCHAPv2 - RFC 5422 section 3.2.2 */
1308 RDEBUG2("Using anonymous provisioning");
1309 t->mode = EAP_FAST_PROVISIONING_ANON;
1312 if (SSL_session_reused(tls_session->ssl)) {
1313 RDEBUG("Session Resumed from PAC");
1314 t->mode = EAP_FAST_NORMAL_AUTH;
1316 RDEBUG2("Using authenticated provisioning");
1317 t->mode = EAP_FAST_PROVISIONING_AUTH;
1320 if (!t->pac.expires || t->pac.expired || t->pac.expires - time(NULL) < t->pac_lifetime * 0.6)
1324 eap_fast_init_keys(request, tls_session);
1326 eap_fast_send_identity_request(request, tls_session, eap_session);
1328 t->stage = AUTHENTICATION;
1329 return PW_CODE_ACCESS_CHALLENGE;
1332 fast_vps = eap_fast_fast2vp(request, tls_session->ssl, data, data_len, NULL, NULL);
1334 RDEBUG("Got Tunneled FAST TLVs");
1335 rdebug_pair_list(L_DBG_LVL_1, request, fast_vps, NULL);
1337 code = eap_fast_process_tlvs(request, eap_session, tls_session, fast_vps);
1339 fr_pair_list_free(&fast_vps);
1341 if (code == PW_CODE_ACCESS_REJECT) return PW_CODE_ACCESS_REJECT;
1344 case AUTHENTICATION:
1345 code = PW_CODE_ACCESS_CHALLENGE;
1347 case CRYPTOBIND_CHECK:
1349 if (t->mode != EAP_FAST_PROVISIONING_ANON && !t->pac.send)
1350 t->result_final = true;
1352 eap_fast_append_result(tls_session, code);
1354 eap_fast_update_icmk(request, tls_session, (uint8_t *)&t->isk);
1355 eap_fast_append_crypto_binding(request, tls_session);
1357 code = PW_CODE_ACCESS_CHALLENGE;
1361 t->result_final = true;
1363 eap_fast_append_result(tls_session, code);
1365 if (code == PW_CODE_ACCESS_REJECT)
1369 RDEBUG("Peer requires new PAC");
1370 eap_fast_send_pac_tunnel(request, tls_session);
1371 code = PW_CODE_ACCESS_CHALLENGE;
1375 t->stage = COMPLETE;
1379 * RFC 5422 section 3.5 - Network Access after EAP-FAST Provisioning
1381 if (t->pac.type && t->pac.expired) {
1382 REDEBUG("Rejecting expired PAC.");
1383 code = PW_CODE_ACCESS_REJECT;
1387 if (t->mode == EAP_FAST_PROVISIONING_ANON) {
1388 REDEBUG("Rejecting unauthenticated provisioning");
1389 code = PW_CODE_ACCESS_REJECT;
1394 * eap_tls_gen_mppe_keys() is unsuitable for EAP-FAST as Cisco decided
1395 * it would be a great idea to flip the recv/send keys around
1397 #define EAPTLS_MPPE_KEY_LEN 32
1398 eap_add_reply(request, "MS-MPPE-Recv-Key", t->msk, EAPTLS_MPPE_KEY_LEN);
1399 eap_add_reply(request, "MS-MPPE-Send-Key", &t->msk[EAPTLS_MPPE_KEY_LEN], EAPTLS_MPPE_KEY_LEN);
1400 eap_add_reply(request, "EAP-MSK", t->msk, EAP_FAST_KEY_LEN);
1401 eap_add_reply(request, "EAP-EMSK", t->emsk, EAP_EMSK_LEN);
1406 RERROR("Internal sanity check failed in EAP-FAST at %d", t->stage);
1407 code = PW_CODE_ACCESS_REJECT;