eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

erp.c (11715B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdint.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 
     32 #include <ell/ell.h>
     33 
     34 #include "ell/useful.h"
     35 #include "src/missing.h"
     36 #include "src/iwd.h"
     37 #include "src/module.h"
     38 #include "src/eap-private.h"
     39 #include "src/erp.h"
     40 #include "src/crypto.h"
     41 #include "src/util.h"
     42 
     43 #define ERP_DEFAULT_KEY_LIFETIME_US 86400000000
     44 
     45 struct erp_cache_entry {
     46 	char *id;
     47 	void *emsk;
     48 	size_t emsk_len;
     49 	void *session_id;
     50 	size_t session_len;
     51 	char *ssid;
     52 	uint64_t expire_time;
     53 	uint32_t ref;
     54 	bool invalid : 1;
     55 };
     56 
     57 struct erp_state {
     58 	erp_tx_packet_func_t tx_packet;
     59 	void *user_data;
     60 
     61 	struct erp_cache_entry *cache;
     62 
     63 	uint8_t rmsk[64];
     64 	uint8_t r_rk[64];
     65 	uint8_t r_ik[64];
     66 	char keyname_nai[254];
     67 	uint16_t seq;
     68 };
     69 
     70 enum eap_erp_type {
     71 	ERP_TYPE_REAUTH_START	= 1,
     72 	ERP_TYPE_REAUTH		= 2,
     73 };
     74 
     75 enum eap_erp_tlv {
     76 	ERP_TLV_KEYNAME_NAI = 1,
     77 	ERP_TV_RRK_LIFETIME = 2,
     78 	ERP_TV_RMSK_LIFETIME = 3,
     79 	ERP_TLV_DOMAIN_NAME = 4,
     80 	ERP_TLV_CRYPTOSUITES = 5,
     81 	ERP_TLV_AUTH_INDICATION = 6,
     82 	ERP_TLV_CALLED_STATION_ID = 128,
     83 	ERP_TLV_CALLING_STATION_ID = 129,
     84 	ERP_TLV_NAS_IDENTIFIER = 130,
     85 	ERP_TLV_NAS_IP_ADDRESS = 131,
     86 	ERP_TLV_NAS_IPV6_ADDRESS = 132,
     87 };
     88 
     89 enum eap_erp_cryptosuite {
     90 	ERP_CRYPTOSUITE_SHA256_64 = 1,
     91 	ERP_CRYPTOSUITE_SHA256_128 = 2,
     92 	ERP_CRYPTOSUITE_SHA256_256 = 3,
     93 };
     94 
     95 struct erp_tlv_iter {
     96 	unsigned int max;
     97 	unsigned int pos;
     98 	const unsigned char *tlv;
     99 	unsigned int tag;
    100 	unsigned int len;
    101 	const unsigned char *data;
    102 };
    103 
    104 static struct l_queue *key_cache;
    105 
    106 static void erp_tlv_iter_init(struct erp_tlv_iter *iter,
    107 				const unsigned char *tlv, unsigned int len)
    108 {
    109 	iter->tlv = tlv;
    110 	iter->max = len;
    111 	iter->pos = 0;
    112 }
    113 
    114 static bool erp_tlv_iter_next(struct erp_tlv_iter *iter)
    115 {
    116 	const unsigned char *tlv = iter->tlv + iter->pos;
    117 	const unsigned char *end = iter->tlv + iter->max;
    118 	uint8_t tag;
    119 	uint8_t len;
    120 
    121 	if (end - tlv < 2)
    122 		return false;
    123 
    124 	tag = *tlv++;
    125 
    126 	/*
    127 	 * These two tags are not actually TLVs (they are just type-value).
    128 	 * Both are 32-bit integers.
    129 	 */
    130 	if (tag != ERP_TV_RMSK_LIFETIME && tag != ERP_TV_RRK_LIFETIME)
    131 		len = *tlv++;
    132 	else
    133 		len = 4;
    134 
    135 	if (len > end - tlv)
    136 		return false;
    137 
    138 	iter->tag = tag;
    139 	iter->len = len;
    140 	iter->data = tlv;
    141 
    142 	iter->pos = tlv + len - iter->tlv;
    143 
    144 	return true;
    145 }
    146 
    147 static void erp_cache_entry_destroy(void *data)
    148 {
    149 	struct erp_cache_entry *entry = data;
    150 
    151 	if (entry->ref)
    152 		l_error("ERP entry still has a reference on cleanup!");
    153 
    154 	l_free(entry->id);
    155 	l_free(entry->emsk);
    156 	l_free(entry->session_id);
    157 	l_free(entry->ssid);
    158 
    159 	l_free(entry);
    160 }
    161 
    162 void erp_cache_add(const char *id, const void *session_id,
    163 			size_t session_len, const void *emsk, size_t emsk_len,
    164 			const uint8_t *ssid, size_t ssid_len)
    165 {
    166 	struct erp_cache_entry *entry;
    167 
    168 	if (!unlikely(id || session_id || emsk))
    169 		return;
    170 
    171 	if (!util_ssid_is_utf8(ssid_len, ssid))
    172 		return;
    173 
    174 	if (util_ssid_is_hidden(ssid_len, ssid))
    175 		return;
    176 
    177 	entry = l_new(struct erp_cache_entry, 1);
    178 
    179 	entry->id = l_strdup(id);
    180 	entry->emsk = l_memdup(emsk, emsk_len);
    181 	entry->emsk_len = emsk_len;
    182 	entry->session_id = l_memdup(session_id, session_len);
    183 	entry->session_len = session_len;
    184 	entry->ssid = l_strndup((char *) ssid, ssid_len);
    185 	entry->expire_time = l_time_offset(l_time_now(),
    186 					ERP_DEFAULT_KEY_LIFETIME_US);
    187 
    188 	l_queue_push_head(key_cache, entry);
    189 }
    190 
    191 static struct erp_cache_entry *find_keycache(const char *id, const char *ssid)
    192 {
    193 	const struct l_queue_entry *entry;
    194 
    195 	if (!id && !ssid)
    196 		return NULL;
    197 
    198 	for (entry = l_queue_get_entries(key_cache); entry;
    199 			entry = entry->next) {
    200 		struct erp_cache_entry *cache = entry->data;
    201 
    202 		if (cache->invalid)
    203 			continue;
    204 
    205 		if (l_time_after(l_time_now(), cache->expire_time)) {
    206 			if (!cache->ref) {
    207 				l_queue_remove(key_cache, cache);
    208 				erp_cache_entry_destroy(cache);
    209 			} else
    210 				cache->invalid = true;
    211 
    212 			continue;
    213 		}
    214 
    215 		if (id && !strcmp(cache->id, id))
    216 			return cache;
    217 
    218 		if (ssid && !strcmp(cache->ssid, ssid))
    219 			return cache;
    220 	}
    221 
    222 	return NULL;
    223 }
    224 
    225 void erp_cache_remove(const char *id)
    226 {
    227 	struct erp_cache_entry *entry = find_keycache(id, NULL);
    228 
    229 	if (!entry)
    230 		return;
    231 
    232 	if (entry->ref) {
    233 		entry->invalid = true;
    234 		return;
    235 	}
    236 
    237 	l_queue_remove(key_cache, entry);
    238 
    239 	erp_cache_entry_destroy(entry);
    240 }
    241 
    242 struct erp_cache_entry *erp_cache_get(const char *ssid)
    243 {
    244 	struct erp_cache_entry *cache = find_keycache(NULL, ssid);
    245 
    246 	if (!cache)
    247 		return NULL;
    248 
    249 	cache->ref++;
    250 
    251 	return cache;
    252 }
    253 
    254 void erp_cache_put(struct erp_cache_entry *cache)
    255 {
    256 	cache->ref--;
    257 
    258 	if (cache->ref)
    259 		return;
    260 
    261 	if (!cache->invalid)
    262 		return;
    263 
    264 	/*
    265 	 * Cache entry marked as invalid, either it expired or something
    266 	 * attempted to remove it. Either way, it can now be removed.
    267 	 */
    268 	l_queue_remove(key_cache, cache);
    269 	erp_cache_entry_destroy(cache);
    270 }
    271 
    272 const char *erp_cache_entry_get_identity(struct erp_cache_entry *cache)
    273 {
    274 	return cache->id;
    275 }
    276 
    277 #define ERP_RRK_LABEL	"EAP Re-authentication Root Key@ietf.org"
    278 #define ERP_RIK_LABEL	"Re-authentication Integrity Key@ietf.org"
    279 #define ERP_RMSK_LABEL	"Re-authentication Master Session Key@ietf.org"
    280 
    281 /*
    282  * RFC 5295 - Section 3.2. EMSK and USRK Name Derivation
    283  */
    284 static bool erp_derive_emsk_name(const uint8_t *session_id, size_t session_len,
    285 					char buf[static 17])
    286 {
    287 	uint8_t hex[8];
    288 	uint16_t eight = L_CPU_TO_BE16(8);
    289 	char *ascii;
    290 
    291 	if (!prf_plus(L_CHECKSUM_SHA256, session_id, session_len,
    292 				hex, 8, 2, "EMSK", strlen("EMSK") + 1,
    293 				&eight, sizeof(eight)))
    294 		return false;
    295 
    296 	ascii = l_util_hexstring(hex, 8);
    297 	strcpy(buf, ascii);
    298 	l_free(ascii);
    299 
    300 	return true;
    301 }
    302 
    303 /*
    304  * RFC 6696 - Section 4.1 and 4.3 - rRK and rIK derivation
    305  *
    306  * All reauth keys form a hierarchy, and all ultimately are derived from the
    307  * EMSK. All keys follow the rule:
    308  *
    309  * "The length of the <key> MUST be equal to the length of the parent key used
    310  *  to derive it."
    311  *
    312  * Therefore all keys derived are equal to the EMSK length.
    313  */
    314 static bool erp_derive_reauth_keys(const uint8_t *emsk, size_t emsk_len,
    315 					void *r_rk, void *r_ik)
    316 {
    317 	uint16_t len = L_CPU_TO_BE16(emsk_len);
    318 	uint8_t cryptosuite = ERP_CRYPTOSUITE_SHA256_128;
    319 
    320 	if (!prf_plus(L_CHECKSUM_SHA256, emsk, emsk_len,
    321 				r_rk, emsk_len, 2, ERP_RRK_LABEL,
    322 				strlen(ERP_RRK_LABEL) + 1,
    323 				&len, sizeof(len)))
    324 		return false;
    325 
    326 	if (!prf_plus(L_CHECKSUM_SHA256, r_rk, emsk_len,
    327 				r_ik, emsk_len, 3, ERP_RIK_LABEL,
    328 				strlen(ERP_RIK_LABEL) + 1,
    329 				&cryptosuite, (size_t) 1, &len, sizeof(len)))
    330 		return false;
    331 
    332 	return true;
    333 }
    334 
    335 struct erp_state *erp_new(struct erp_cache_entry *cache,
    336 				erp_tx_packet_func_t tx_packet,
    337 				void *user_data)
    338 {
    339 	struct erp_state *erp;
    340 
    341 	if (!cache)
    342 		return NULL;
    343 
    344 	erp = l_new(struct erp_state, 1);
    345 
    346 	erp->tx_packet = tx_packet;
    347 	erp->user_data = user_data;
    348 	erp->cache = cache;
    349 	cache->ref++;
    350 
    351 	return erp;
    352 }
    353 
    354 void erp_free(struct erp_state *erp)
    355 {
    356 	erp_cache_put(erp->cache);
    357 
    358 	explicit_bzero(erp->rmsk, sizeof(erp->rmsk));
    359 	explicit_bzero(erp->r_ik, sizeof(erp->r_ik));
    360 	explicit_bzero(erp->r_rk, sizeof(erp->r_rk));
    361 
    362 	l_free(erp);
    363 }
    364 
    365 bool erp_start(struct erp_state *erp)
    366 {
    367 	uint8_t buf[512];
    368 	uint8_t *ptr = buf;
    369 	char emsk_name[17];
    370 	size_t nai_len;
    371 
    372 	if (!erp_derive_emsk_name(erp->cache->session_id,
    373 					erp->cache->session_len, emsk_name))
    374 		return false;
    375 
    376 	if (!erp_derive_reauth_keys(erp->cache->emsk, erp->cache->emsk_len,
    377 					erp->r_rk, erp->r_ik))
    378 		return false;
    379 
    380 	nai_len = sprintf(erp->keyname_nai, "%s@%s", emsk_name,
    381 				util_get_domain(erp->cache->id));
    382 
    383 	*ptr++ = EAP_CODE_INITIATE;
    384 	*ptr++ = 0;
    385 	/* Header (8) + TL (2) + NAI (nai_len) + CS (1) + auth tag (16) */
    386 	l_put_be16(27 + nai_len, ptr);
    387 	ptr += 2;
    388 	*ptr++ = ERP_TYPE_REAUTH;
    389 	*ptr++ = 0;
    390 	l_put_be16(erp->seq, ptr);
    391 	ptr += 2;
    392 
    393 	/* keyName-NAI TLV */
    394 	*ptr++ = ERP_TLV_KEYNAME_NAI;
    395 	*ptr++ = nai_len;
    396 	memcpy(ptr, erp->keyname_nai, nai_len);
    397 	ptr += nai_len;
    398 
    399 	*ptr++ = ERP_CRYPTOSUITE_SHA256_128;
    400 
    401 	hmac_sha256(erp->r_ik, erp->cache->emsk_len, buf, ptr - buf, ptr, 16);
    402 	ptr += 16;
    403 
    404 	erp->tx_packet(buf, ptr - buf, erp->user_data);
    405 
    406 	return true;
    407 }
    408 
    409 int erp_rx_packet(struct erp_state *erp, const uint8_t *pkt, size_t len)
    410 {
    411 	struct erp_tlv_iter iter;
    412 	enum eap_erp_cryptosuite cs;
    413 	uint8_t hash[16];
    414 	const uint8_t *nai = NULL;
    415 	uint8_t type;
    416 	uint16_t seq;
    417 	uint16_t length;
    418 	bool r;
    419 
    420 	/*
    421 	 * Not including the TLVs we have:
    422 	 * header (8) + cryptosuite (1) + auth tag (16) = 25 bytes
    423 	 */
    424 	if (len < 25)
    425 		goto eap_failed;
    426 
    427 	/*
    428 	 * We can skip code/id/len, since that was already parsed. We just need
    429 	 * the whole packet so we can verify the Auth tag.
    430 	 */
    431 	type = pkt[4];
    432 
    433 	if (type != ERP_TYPE_REAUTH)
    434 		goto eap_failed;
    435 
    436 	r = test_bit(pkt + 5, 0);
    437 	if (r)
    438 		goto eap_failed;
    439 
    440 	/*
    441 	 * TODO: Parse B and L bits. L bit indicates rRK lifetime, but our ERP
    442 	 * cache does not yet support this.
    443 	 */
    444 
    445 	seq = l_get_be16(pkt + 6);
    446 
    447 	if (seq != erp->seq)
    448 		goto eap_failed;
    449 
    450 	/*
    451 	 * The Cryptosuite byte comes after the TLVs. Because of this we cannot
    452 	 * parse the TLVs yet since we don't actually know where they end. There
    453 	 * is really no good way to do this, but (at least for now) we can just
    454 	 * require the 128 bit cryptosuite. If we limit to only this suite we
    455 	 * can work backwards from the end (17 bytes) to get the cryptosuite. If
    456 	 * it is not the 128 bit suite we just fail. If it is, we now know where
    457 	 * the TLVs end;
    458 	 */
    459 	cs = *(pkt + len - 17);
    460 
    461 	if (cs != ERP_CRYPTOSUITE_SHA256_128)
    462 		goto eap_failed;
    463 
    464 	hmac_sha256(erp->r_ik, erp->cache->emsk_len, pkt, len - 16, hash, 16);
    465 
    466 	if (memcmp(hash, pkt + len - 16, 16) != 0) {
    467 		l_debug("Authentication Tag did not verify");
    468 		goto eap_failed;
    469 	}
    470 
    471 	erp_tlv_iter_init(&iter, pkt + 8, len - 8 - 17);
    472 
    473 	while (erp_tlv_iter_next(&iter)) {
    474 		switch (iter.tag) {
    475 		case ERP_TLV_KEYNAME_NAI:
    476 			if (nai)
    477 				goto eap_failed;
    478 
    479 			nai = iter.data;
    480 			break;
    481 		default:
    482 			break;
    483 		}
    484 	}
    485 
    486 	/*
    487 	 * RFC 6696 Section 5.3.3
    488 	 *
    489 	 * Exactly one instance of the keyName-NAI attribute SHALL be present
    490 	 * in an EAP-Finish/Re-auth message
    491 	 */
    492 	if (!nai) {
    493 		l_error("AP did not include keyName-NAI in EAP-Finish");
    494 		goto eap_failed;
    495 	}
    496 
    497 	if (memcmp(nai, erp->keyname_nai, strlen(erp->keyname_nai))) {
    498 		l_error("keyName-NAI did not match");
    499 		goto eap_failed;
    500 	}
    501 
    502 	/*
    503 	 * RFC 6696 Section 4.6 - rMSK Derivation
    504 	 */
    505 	seq = L_CPU_TO_BE16(erp->seq);
    506 	length = L_CPU_TO_BE16(64);
    507 
    508 	if (!prf_plus(L_CHECKSUM_SHA256, erp->r_rk, erp->cache->emsk_len,
    509 				erp->rmsk, erp->cache->emsk_len, 3,
    510 				ERP_RMSK_LABEL, strlen(ERP_RMSK_LABEL) + 1,
    511 				&seq, sizeof(seq),
    512 				&length, sizeof(length)))
    513 		goto eap_failed;
    514 
    515 	return 0;
    516 
    517 eap_failed:
    518 	return -EINVAL;
    519 }
    520 
    521 const void *erp_get_rmsk(struct erp_state *erp, size_t *rmsk_len)
    522 {
    523 	if (rmsk_len)
    524 		*rmsk_len = erp->cache->emsk_len;
    525 
    526 	return erp->rmsk;
    527 }
    528 
    529 static int erp_init(void)
    530 {
    531 	key_cache = l_queue_new();
    532 
    533 	return 0;
    534 }
    535 
    536 static void erp_exit(void)
    537 {
    538 	l_queue_destroy(key_cache, erp_cache_entry_destroy);
    539 }
    540 
    541 IWD_MODULE(erp, erp_init, erp_exit)