eiwd

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

eap-aka.c (18620B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2017-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 <ctype.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 #include <string.h>
     32 #include <ell/ell.h>
     33 
     34 #include "src/missing.h"
     35 #include "src/eap.h"
     36 #include "src/eap-private.h"
     37 #include "src/crypto.h"
     38 #include "src/simutil.h"
     39 #include "src/simauth.h"
     40 
     41 /*
     42  * EAP-AKA specific values
     43  */
     44 #define EAP_AKA_AUTN_LEN	16
     45 #define EAP_AKA_AUTS_LEN	14
     46 #define EAP_AKA_RES_LEN		8
     47 #define EAP_AKA_K_RE_LEN	32
     48 
     49 #define EAP_AKA_KDF_DEFAULT	0x0001
     50 
     51 #define EAP_AKA_ST_CHALLENGE	0x01
     52 #define EAP_AKA_ST_AUTH_REJECT	0x02
     53 #define EAP_AKA_ST_SYNC_FAILURE	0x04
     54 #define EAP_AKA_ST_IDENTITY	0x05
     55 #define EAP_AKA_ST_NOTIFICATION	0x0c
     56 #define EAP_AKA_ST_CLIENT_ERROR	0x0e
     57 
     58 /*
     59  * Internal client state, tracked to ensure that we are receiving the right
     60  * messages at the right time.
     61  */
     62 enum eap_aka_state {
     63 	EAP_AKA_STATE_UNCONNECTED = 0,
     64 	EAP_AKA_STATE_IDENTITY,
     65 	EAP_AKA_STATE_CHALLENGE,
     66 	EAP_AKA_STATE_SUCCESS,
     67 	EAP_AKA_STATE_ERROR
     68 };
     69 
     70 struct eap_aka_handle {
     71 	enum eap_aka_state state;
     72 	enum eap_type type;
     73 	/* Identity from SIM */
     74 	char *identity;
     75 
     76 	/* Derived master key */
     77 	uint8_t mk[EAP_SIM_MK_LEN];
     78 
     79 	/* Derived K_encr key from PRNG */
     80 	uint8_t k_encr[EAP_SIM_K_ENCR_LEN];
     81 
     82 	/* Derived K_aut key from PRNG, extended for AKA' */
     83 	uint8_t k_aut[EAP_AKA_PRIME_K_AUT_LEN];
     84 
     85 	/* Derived MSK from PRNG */
     86 	uint8_t msk[EAP_SIM_MSK_LEN];
     87 
     88 	/* Derived EMSK from PRNG */
     89 	uint8_t emsk[EAP_SIM_EMSK_LEN];
     90 
     91 	/* Flag set if AT_ANY_ID_REQ was present */
     92 	bool any_id_req : 1;
     93 
     94 	/* Flag to indicate protected status indications */
     95 	bool protected : 1;
     96 
     97 	/* Authentication value from AuC */
     98 	uint8_t autn[EAP_AKA_AUTN_LEN];
     99 
    100 	/* re-auth key */
    101 	uint8_t k_re[EAP_AKA_K_RE_LEN];
    102 
    103 	uint8_t rand[EAP_SIM_RAND_LEN];
    104 
    105 	char *kdf_in;
    106 
    107 	uint8_t *chal_pkt;
    108 	uint32_t pkt_len;
    109 
    110 	struct iwd_sim_auth *auth;
    111 	unsigned int auth_watch;
    112 };
    113 
    114 static void eap_aka_clear_secrets(struct eap_aka_handle *aka)
    115 {
    116 	explicit_bzero(aka->mk, sizeof(aka->mk));
    117 	explicit_bzero(aka->k_encr, sizeof(aka->k_encr));
    118 	explicit_bzero(aka->k_aut, sizeof(aka->k_aut));
    119 	explicit_bzero(aka->k_re, sizeof(aka->k_re));
    120 	explicit_bzero(aka->msk, sizeof(aka->msk));
    121 	explicit_bzero(aka->emsk, sizeof(aka->emsk));
    122 }
    123 
    124 static void eap_aka_free(struct eap_state *eap)
    125 {
    126 	struct eap_aka_handle *aka = eap_get_data(eap);
    127 
    128 	if (aka->auth)
    129 		sim_auth_unregistered_watch_remove(aka->auth, aka->auth_watch);
    130 
    131 	eap_aka_clear_secrets(aka);
    132 
    133 	l_free(aka->identity);
    134 	l_free(aka->kdf_in);
    135 	l_free(aka);
    136 
    137 	eap_set_data(eap, NULL);
    138 }
    139 
    140 static bool derive_aka_mk(const char *identity, const uint8_t *ik,
    141 		const uint8_t *ck, uint8_t *mk)
    142 {
    143 	int ret;
    144 	struct iovec iov[5];
    145 	struct l_checksum *checksum = l_checksum_new(L_CHECKSUM_SHA1);
    146 
    147 	if (!checksum) {
    148 		l_error("could not create SHA1 checksum");
    149 		return false;
    150 	}
    151 
    152 	iov[0].iov_base = (void *)identity;
    153 	iov[0].iov_len = strlen(identity);
    154 	iov[1].iov_base = (void *)ik;
    155 	iov[1].iov_len = EAP_AKA_IK_LEN;
    156 	iov[2].iov_base = (void *)ck;
    157 	iov[2].iov_len = EAP_AKA_CK_LEN;
    158 
    159 	if (!l_checksum_updatev(checksum, iov, 3))
    160 		goto mk_error;
    161 
    162 	ret = l_checksum_get_digest(checksum, mk, EAP_SIM_MK_LEN);
    163 	l_checksum_free(checksum);
    164 
    165 	return (ret == EAP_SIM_MK_LEN);
    166 
    167 mk_error:
    168 	l_checksum_free(checksum);
    169 	l_error("error deriving master key");
    170 	return false;
    171 }
    172 
    173 static void eap_aka_finish(struct eap_state *eap)
    174 {
    175 	struct eap_aka_handle *aka = eap_get_data(eap);
    176 	uint8_t session_id[1 + EAP_SIM_RAND_LEN + EAP_AKA_AUTN_LEN];
    177 
    178 	session_id[0] = EAP_TYPE_AKA;
    179 	memcpy(session_id + 1, aka->rand, EAP_SIM_RAND_LEN);
    180 	memcpy(session_id + 1 + EAP_SIM_RAND_LEN, aka->autn, EAP_AKA_AUTN_LEN);
    181 
    182 	eap_method_success(eap);
    183 	eap_set_key_material(eap, aka->msk, 32, aka->emsk, 32, NULL, 0,
    184 					session_id, sizeof(session_id));
    185 }
    186 
    187 static void check_milenage_cb(const uint8_t *res, const uint8_t *ck,
    188 		const uint8_t *ik, const uint8_t *auts, void *data)
    189 {
    190 	struct eap_state *eap = data;
    191 	struct eap_aka_handle *aka = eap_get_data(eap);
    192 
    193 	size_t resp_len = aka->protected ? 44 : 40;
    194 	uint8_t response[EAP_SIM_ROUND(resp_len + 4)];
    195 	uint8_t *pos = response;
    196 
    197 	if (auts) {
    198 		/*
    199 		 * If AUTS is non NULL then the SQN was not correct, send AUTS
    200 		 * to server which will update the SQN and send another
    201 		 * challenge packet.
    202 		 */
    203 		l_free(aka->chal_pkt);
    204 		aka->chal_pkt = NULL;
    205 
    206 		pos += eap_sim_build_header(eap, aka->type,
    207 				EAP_AKA_ST_SYNC_FAILURE, pos, 24);
    208 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_AUTS,
    209 				EAP_SIM_PAD_NONE, auts, EAP_AKA_AUTS_LEN);
    210 
    211 		eap_method_respond(eap, response, 24);
    212 
    213 		return;
    214 	}
    215 
    216 	if (!res || !ck || !ik)
    217 		goto chal_error;
    218 
    219 	if (aka->type == EAP_TYPE_AKA_PRIME) {
    220 		bool r;
    221 		uint8_t ik_p[EAP_AKA_IK_LEN];
    222 		uint8_t ck_p[EAP_AKA_CK_LEN];
    223 
    224 		if (!eap_aka_derive_primes(ck, ik, aka->autn,
    225 				(uint8_t *)aka->kdf_in, strlen(aka->kdf_in),
    226 				ck_p, ik_p)) {
    227 			l_error("could not derive primes");
    228 			goto chal_fatal;
    229 		}
    230 
    231 		r = eap_aka_prf_prime(ik_p, ck_p, aka->identity, aka->k_encr,
    232 				aka->k_aut, aka->k_re, aka->msk, aka->emsk);
    233 		explicit_bzero(ik_p, sizeof(ik_p));
    234 		explicit_bzero(ck_p, sizeof(ck_p));
    235 
    236 		if (!r) {
    237 			l_error("could not derive encryption keys");
    238 			goto chal_fatal;
    239 		}
    240 	} else {
    241 		uint8_t prng_buf[160];
    242 		bool r;
    243 
    244 		if (!derive_aka_mk(aka->identity, ik, ck, aka->mk)) {
    245 			l_error("error deriving MK");
    246 			goto chal_fatal;
    247 		}
    248 
    249 		eap_sim_fips_prf(aka->mk, 20, prng_buf, 160);
    250 
    251 		r = eap_sim_get_encryption_keys(prng_buf, aka->k_encr,
    252 					aka->k_aut, aka->msk, aka->emsk);
    253 		explicit_bzero(prng_buf, sizeof(prng_buf));
    254 
    255 		if (!r) {
    256 			l_error("could not derive encryption keys");
    257 			goto chal_fatal;
    258 		}
    259 	}
    260 
    261 	if (!eap_sim_verify_mac(eap, aka->type, aka->chal_pkt, aka->pkt_len,
    262 			aka->k_aut, NULL, 0)) {
    263 		l_error("MAC was not valid");
    264 		goto chal_error;
    265 	}
    266 
    267 	aka->state = EAP_AKA_STATE_CHALLENGE;
    268 
    269 	pos += eap_sim_build_header(eap, aka->type, EAP_AKA_ST_CHALLENGE,
    270 			pos, resp_len);
    271 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_RES,
    272 			EAP_SIM_PAD_LENGTH_BITS, res, EAP_AKA_RES_LEN);
    273 
    274 	if (aka->protected)
    275 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_RESULT_IND,
    276 				EAP_SIM_PAD_NONE, NULL, 2);
    277 
    278 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_MAC, EAP_SIM_PAD_NONE,
    279 			NULL, EAP_SIM_MAC_LEN);
    280 
    281 	if (!eap_sim_derive_mac(aka->type, response, resp_len, aka->k_aut,
    282 			pos - EAP_SIM_MAC_LEN)) {
    283 		l_error("error deriving MAC");
    284 		goto chal_fatal;
    285 	}
    286 
    287 	l_free(aka->chal_pkt);
    288 	aka->chal_pkt = NULL;
    289 
    290 	eap_method_respond(eap, response, resp_len);
    291 
    292 	if (!aka->protected) {
    293 		eap_aka_finish(eap);
    294 
    295 		aka->state = EAP_AKA_STATE_SUCCESS;
    296 	}
    297 
    298 	return;
    299 
    300 chal_fatal:
    301 	eap_method_error(eap);
    302 	aka->state = EAP_AKA_STATE_ERROR;
    303 
    304 	return;
    305 
    306 chal_error:
    307 	l_free(aka->chal_pkt);
    308 	aka->chal_pkt = NULL;
    309 	eap_sim_client_error(eap, aka->type, EAP_SIM_ERROR_PROCESS);
    310 }
    311 
    312 /*
    313  * Handles EAP-AKA Challenge subtype
    314  */
    315 static void handle_challenge(struct eap_state *eap, const uint8_t *pkt,
    316 		size_t len)
    317 {
    318 	struct eap_aka_handle *aka = eap_get_data(eap);
    319 	struct eap_sim_tlv_iter iter;
    320 	const uint8_t *rand = NULL;
    321 	const uint8_t *autn = NULL;
    322 	bool kdf_func = false;
    323 	uint16_t kdf_in_len = 0;
    324 
    325 	if (len < 3) {
    326 		l_error("packet is too small");
    327 		goto chal_error;
    328 	}
    329 
    330 	if (aka->state != EAP_AKA_STATE_IDENTITY) {
    331 		l_error("invalid packet for EAP-AKA state");
    332 		goto chal_error;
    333 	}
    334 
    335 	eap_sim_tlv_iter_init(&iter, pkt + 3, len - 3);
    336 
    337 	while (eap_sim_tlv_iter_next(&iter)) {
    338 		const uint8_t *contents = eap_sim_tlv_iter_get_data(&iter);
    339 		uint16_t length = eap_sim_tlv_iter_get_length(&iter);
    340 
    341 		switch (eap_sim_tlv_iter_get_type(&iter)) {
    342 		case EAP_SIM_AT_AUTN:
    343 			if (length < EAP_AKA_AUTN_LEN + 2) {
    344 				l_error("malformed AT_AUTN");
    345 				goto chal_error;
    346 			}
    347 
    348 			autn = contents + 2;
    349 
    350 			break;
    351 
    352 		case EAP_SIM_AT_RAND:
    353 			if (length < EAP_SIM_RAND_LEN + 2) {
    354 				l_error("malformed AT_RAND");
    355 				goto chal_error;
    356 			}
    357 
    358 			rand = contents + 2;
    359 
    360 			break;
    361 
    362 		case EAP_SIM_AT_RESULT_IND:
    363 			if (length < 2) {
    364 				l_error("malformed AT_RESULT_IND");
    365 				goto chal_error;
    366 			}
    367 
    368 			aka->protected = 1;
    369 
    370 			break;
    371 
    372 		case EAP_SIM_AT_KDF:
    373 			if (aka->type != EAP_TYPE_AKA_PRIME) {
    374 				l_error("invalid attribute found for EAP-AKA");
    375 				goto chal_error;
    376 			}
    377 
    378 			if (length < 2) {
    379 				l_error("malformed AT_KDF");
    380 				goto chal_error;
    381 			}
    382 
    383 			if (l_get_be16(contents) != EAP_AKA_KDF_DEFAULT) {
    384 				l_error("KDF requested is not supported");
    385 				goto chal_error;
    386 			}
    387 
    388 			kdf_func = true;
    389 
    390 			break;
    391 
    392 		case EAP_SIM_AT_KDF_INPUT:
    393 			if (aka->type != EAP_TYPE_AKA_PRIME) {
    394 				l_error("invalid attribute found for EAP-AKA");
    395 				goto chal_error;
    396 			}
    397 
    398 			if (length < 3) {
    399 				l_error("malformed AT_KDF_INPUT");
    400 				goto chal_error;
    401 			}
    402 
    403 			kdf_in_len = l_get_be16(contents);
    404 
    405 			if (length < kdf_in_len + 2) {
    406 				l_error("malformed AT_KDF_INPUT");
    407 				goto chal_error;
    408 			}
    409 
    410 			aka->kdf_in = l_strndup((const char *)(contents + 2),
    411 					kdf_in_len);
    412 
    413 			break;
    414 
    415 		case EAP_SIM_AT_NEXT_PSEUDONYM:
    416 		case EAP_SIM_AT_NEXT_REAUTH_ID:
    417 		case EAP_SIM_AT_IV:
    418 		case EAP_SIM_AT_ENCR_DATA:
    419 		case EAP_SIM_AT_PADDING:
    420 		case EAP_SIM_AT_CHECKCODE:
    421 		case EAP_SIM_AT_MAC:
    422 		/*
    423 		 * AT_BIDDING is defined in RFC 5448 (AKA'). It is used to
    424 		 * communicate support for AKA', if supported.
    425 		 */
    426 		case EAP_SIM_AT_BIDDING:
    427 			/* RFC 4187, Section 10.1 */
    428 			break;
    429 
    430 		default:
    431 			l_error("attribute %u was found in Challenge",
    432 					eap_sim_tlv_iter_get_type(&iter));
    433 			goto chal_error;
    434 		}
    435 	}
    436 
    437 	/* check that the right attributes were found */
    438 	if (!rand || !autn) {
    439 		l_error("AT_RAND or AT_AUTN were not found");
    440 		goto chal_error;
    441 	}
    442 
    443 	if (aka->type == EAP_TYPE_AKA_PRIME && (!aka->kdf_in || !kdf_func)) {
    444 		l_error("AT_KDF or AT_KDF_INPUT were not found");
    445 		goto chal_error;
    446 	}
    447 
    448 	aka->chal_pkt = l_memdup(pkt, len);
    449 	aka->pkt_len = len;
    450 
    451 	/* AKA' needs AUTN for prime derivation */
    452 	memcpy(aka->autn, autn, EAP_AKA_AUTN_LEN);
    453 	/* Keep RAND for session ID derivation */
    454 	memcpy(aka->rand, rand, EAP_SIM_RAND_LEN);
    455 
    456 	if (sim_auth_check_milenage(aka->auth, rand, autn, check_milenage_cb,
    457 			eap) < 0) {
    458 		l_free(aka->chal_pkt);
    459 		aka->chal_pkt = NULL;
    460 		goto chal_error;
    461 	}
    462 
    463 	return;
    464 
    465 chal_error:
    466 	eap_sim_client_error(eap, aka->type, EAP_SIM_ERROR_PROCESS);
    467 }
    468 
    469 /*
    470  * Handles Notification subtype
    471  */
    472 static void handle_notification(struct eap_state *eap, const uint8_t *pkt,
    473 		size_t len)
    474 {
    475 	struct eap_aka_handle *aka = eap_get_data(eap);
    476 	struct eap_sim_tlv_iter iter;
    477 	int32_t value = -1;
    478 
    479 	if (len < 3) {
    480 		l_error("packet is too small");
    481 		goto notif_error;
    482 	}
    483 
    484 	eap_sim_tlv_iter_init(&iter, pkt + 3, len - 3);
    485 
    486 	while (eap_sim_tlv_iter_next(&iter)) {
    487 		const uint8_t *contents = eap_sim_tlv_iter_get_data(&iter);
    488 		uint16_t length = eap_sim_tlv_iter_get_length(&iter);
    489 
    490 		switch (eap_sim_tlv_iter_get_type(&iter)) {
    491 		case EAP_SIM_AT_NOTIFICATION:
    492 			if (length < 2) {
    493 				l_error("malformed AT_NOTIFICATION");
    494 				goto notif_error;
    495 			}
    496 
    497 			value = l_get_be16(contents);
    498 			break;
    499 
    500 		case EAP_SIM_AT_IV:
    501 		case EAP_SIM_AT_ENCR_DATA:
    502 		case EAP_SIM_AT_PADDING:
    503 		case EAP_SIM_AT_MAC:
    504 			/* RFC 4186, Section 10.1 */
    505 			break;
    506 
    507 		default:
    508 			l_error("attribute type %u not allowed in Notification",
    509 					eap_sim_tlv_iter_get_type(&iter));
    510 			goto notif_error;
    511 		}
    512 	}
    513 
    514 	if (value == EAP_SIM_SUCCESS && aka->protected &&
    515 			aka->state == EAP_AKA_STATE_CHALLENGE) {
    516 		/* header + MAC + MAC header */
    517 		uint8_t response[EAP_SIM_ROUND(8 + EAP_SIM_MAC_LEN + 4)];
    518 		uint8_t *pos = response;
    519 
    520 		/*
    521 		 * Server sent successful result indication
    522 		 */
    523 		eap_aka_finish(eap);
    524 
    525 		/*
    526 		 * Build response packet
    527 		 */
    528 		pos += eap_sim_build_header(eap, aka->type,
    529 				EAP_AKA_ST_NOTIFICATION, pos, 20);
    530 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_MAC,
    531 				EAP_SIM_PAD_NONE, NULL, EAP_SIM_MAC_LEN);
    532 
    533 		if (!eap_sim_derive_mac(aka->type, response, pos - response,
    534 				aka->k_aut, response + 12)) {
    535 			l_error("could not derive MAC");
    536 			eap_method_error(eap);
    537 			aka->state = EAP_AKA_STATE_ERROR;
    538 			return;
    539 		}
    540 
    541 		eap_method_respond(eap, response, pos - response);
    542 
    543 		aka->state = EAP_AKA_STATE_SUCCESS;
    544 
    545 		return;
    546 	} else if (value == EAP_SIM_SUCCESS) {
    547 		/*
    548 		 * Unexpected success notification, what should
    549 		 * be done here?
    550 		 */
    551 		l_error("Unexpected success notification");
    552 	} else {
    553 		/*
    554 		 * All other values are error conditions.
    555 		 * Nothing unique can be done for any error so
    556 		 * print the code and signal EAP failure.
    557 		 */
    558 		l_error("Error authenticating: code=%u", value);
    559 	}
    560 
    561 notif_error:
    562 	eap_sim_client_error(eap, aka->type, EAP_SIM_ERROR_PROCESS);
    563 }
    564 
    565 static void handle_identity(struct eap_state *eap, const uint8_t *pkt,
    566 		size_t len)
    567 {
    568 	struct eap_aka_handle *aka = eap_get_data(eap);
    569 	uint8_t response[EAP_SIM_ROUND(8 + strlen(aka->identity) + 4)];
    570 	uint8_t *pos = response;
    571 
    572 	if (aka->state != EAP_AKA_STATE_UNCONNECTED) {
    573 		l_error("invalid packet for EAP-AKA state");
    574 		eap_sim_client_error(eap, aka->type, EAP_SIM_ERROR_PROCESS);
    575 		return;
    576 	}
    577 
    578 	aka->state = EAP_AKA_STATE_IDENTITY;
    579 	/*
    580 	 * Build response packet
    581 	 */
    582 	pos += eap_sim_build_header(eap, aka->type, EAP_AKA_ST_IDENTITY, pos,
    583 			20);
    584 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_IDENTITY,
    585 			EAP_SIM_PAD_LENGTH, (uint8_t *)aka->identity,
    586 			strlen(aka->identity));
    587 
    588 	eap_method_respond(eap, response, pos - response);
    589 }
    590 
    591 static void eap_aka_handle_request(struct eap_state *eap,
    592 					const uint8_t *pkt, size_t len)
    593 {
    594 	struct eap_aka_handle *aka = eap_get_data(eap);
    595 
    596 	if (len < 1) {
    597 		l_error("packet is too small");
    598 		goto req_error;
    599 	}
    600 
    601 	switch (pkt[0]) {
    602 	case EAP_AKA_ST_IDENTITY:
    603 		handle_identity(eap, pkt, len);
    604 		break;
    605 
    606 	case EAP_AKA_ST_CHALLENGE:
    607 		handle_challenge(eap, pkt, len);
    608 		break;
    609 
    610 	case EAP_AKA_ST_NOTIFICATION:
    611 		handle_notification(eap, pkt, len);
    612 		break;
    613 
    614 	default:
    615 		l_error("unknown EAP-SIM subtype: %u", pkt[0]);
    616 		goto req_error;
    617 	}
    618 
    619 	return;
    620 
    621 req_error:
    622 	eap_sim_client_error(eap, aka->type, EAP_SIM_ERROR_PROCESS);
    623 }
    624 
    625 static const char *eap_aka_get_identity(struct eap_state *eap)
    626 {
    627 	struct eap_aka_handle *aka = eap_get_data(eap);
    628 
    629 	return aka->identity;
    630 }
    631 
    632 static void auth_destroyed(void *data)
    633 {
    634 	struct eap_state *eap = data;
    635 	struct eap_aka_handle *aka = eap_get_data(eap);
    636 
    637 	/*
    638 	 * If AKA was already successful we can return. Also if the state
    639 	 * has been set to ERROR, then eap_method_error has already been called,
    640 	 * so we can return.
    641 	 */
    642 	if (aka->state == EAP_AKA_STATE_SUCCESS ||
    643 			aka->state == EAP_AKA_STATE_ERROR)
    644 		return;
    645 
    646 	l_error("auth provider destroyed before AKA could finish");
    647 
    648 	aka->state = EAP_AKA_STATE_ERROR;
    649 	eap_method_error(eap);
    650 }
    651 
    652 static int eap_aka_check_settings(struct l_settings *settings,
    653 					struct l_queue *secrets,
    654 					const char *prefix,
    655 					struct l_queue **out_missing)
    656 {
    657 	struct iwd_sim_auth *auth;
    658 
    659 	auth = iwd_sim_auth_find(false, true);
    660 	if (!auth) {
    661 		l_debug("No SIM driver available for EAP-AKA");
    662 		return -EUNATCH;
    663 	}
    664 
    665 	if (!iwd_sim_auth_get_nai(auth)) {
    666 		l_error("SIM driver didn't provide NAI");
    667 		return -ENOENT;
    668 	}
    669 
    670 	return 0;
    671 }
    672 
    673 static bool eap_aka_common_load_settings(struct eap_state *eap,
    674 						struct l_settings *settings,
    675 						const char *prefix)
    676 {
    677 	struct eap_aka_handle *aka = eap_get_data(eap);
    678 	/*
    679 	 * RFC 4187 Section 4.1.1.6
    680 	 * For AKA, the permanent username prefix is '0'
    681 	 *
    682 	 * RFC 5448 Section 3
    683 	 * For AKA', the permanent username prefix is '6'
    684 	 */
    685 	char id_prefix = (aka->type == EAP_TYPE_AKA) ? '0' : '6';
    686 
    687 	/*
    688 	 * No specific settings for EAP-SIM, the auth provider will have all
    689 	 * required data.
    690 	 */
    691 
    692 	aka->auth = iwd_sim_auth_find(false, true);
    693 	if (!aka->auth)
    694 		return false;
    695 
    696 	aka->auth_watch = sim_auth_unregistered_watch_add(aka->auth,
    697 			auth_destroyed, eap);
    698 	aka->identity = l_strdup_printf("%c%s", id_prefix,
    699 			iwd_sim_auth_get_nai(aka->auth));
    700 
    701 	return true;
    702 }
    703 
    704 static bool eap_aka_load_settings(struct eap_state *eap,
    705 					struct l_settings *settings,
    706 					const char *prefix)
    707 {
    708 	struct eap_aka_handle *aka = l_new(struct eap_aka_handle, 1);
    709 
    710 	aka->type = EAP_TYPE_AKA;
    711 	eap_set_data(eap, aka);
    712 
    713 	return eap_aka_common_load_settings(eap, settings, prefix);
    714 }
    715 
    716 static bool eap_aka_prime_load_settings(struct eap_state *eap,
    717 					struct l_settings *settings,
    718 					const char *prefix)
    719 {
    720 	struct eap_aka_handle *aka = l_new(struct eap_aka_handle, 1);
    721 
    722 	aka->type = EAP_TYPE_AKA_PRIME;
    723 	eap_set_data(eap, aka);
    724 
    725 	return eap_aka_common_load_settings(eap, settings, prefix);
    726 }
    727 
    728 static bool eap_aka_reset_state(struct eap_state *eap)
    729 {
    730 	struct eap_aka_handle *aka = eap_get_data(eap);
    731 
    732 	aka->state = EAP_AKA_STATE_UNCONNECTED;
    733 
    734 	l_free(aka->kdf_in);
    735 	aka->kdf_in = NULL;
    736 	l_free(aka->chal_pkt);
    737 	aka->chal_pkt = NULL;
    738 
    739 	eap_aka_clear_secrets(aka);
    740 	memset(aka->autn, 0, sizeof(aka->autn));
    741 
    742 	return true;
    743 }
    744 
    745 static struct eap_method eap_aka = {
    746 	.request_type = EAP_TYPE_AKA,
    747 	.exports_msk = true,
    748 	.name = "AKA",
    749 	.free = eap_aka_free,
    750 	.handle_request = eap_aka_handle_request,
    751 	.check_settings = eap_aka_check_settings,
    752 	.load_settings = eap_aka_load_settings,
    753 	.get_identity = eap_aka_get_identity,
    754 	.reset_state = eap_aka_reset_state
    755 };
    756 
    757 static struct eap_method eap_aka_prime = {
    758 	.request_type = EAP_TYPE_AKA_PRIME,
    759 	.exports_msk = true,
    760 	.name = "AKA'",
    761 	.free = eap_aka_free,
    762 	.handle_request = eap_aka_handle_request,
    763 	.check_settings = eap_aka_check_settings,
    764 	.load_settings = eap_aka_prime_load_settings,
    765 	.get_identity = eap_aka_get_identity,
    766 	.reset_state = eap_aka_reset_state
    767 };
    768 
    769 static int eap_aka_init(void)
    770 {
    771 	l_debug("");
    772 	return eap_register_method(&eap_aka);
    773 }
    774 
    775 static void eap_aka_exit(void)
    776 {
    777 	l_debug("");
    778 	eap_unregister_method(&eap_aka);
    779 }
    780 
    781 static int eap_aka_prime_init(void)
    782 {
    783 	l_debug("");
    784 	return eap_register_method(&eap_aka_prime);
    785 }
    786 
    787 static void eap_aka_prime_exit(void)
    788 {
    789 	l_debug("");
    790 	eap_unregister_method(&eap_aka_prime);
    791 }
    792 
    793 EAP_METHOD_BUILTIN(eap_aka, eap_aka_init, eap_aka_exit);
    794 EAP_METHOD_BUILTIN(eap_aka_prime, eap_aka_prime_init, eap_aka_prime_exit);