eiwd

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

eap-sim.c (18161B)


      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 <alloca.h>
     32 #include <string.h>
     33 #include <ell/ell.h>
     34 
     35 #include "src/missing.h"
     36 #include "src/eap.h"
     37 #include "src/eap-private.h"
     38 #include "src/crypto.h"
     39 #include "src/simutil.h"
     40 #include "src/simauth.h"
     41 #include "src/util.h"
     42 
     43 /*
     44  * EAP-SIM authentication protocol.
     45  *
     46  * Open Items:
     47  *    - Fast Re-authentication. In order to implement this, the higher level
     48  *      EAP code would need to know/retrieve a fast re-authentication identity
     49  *      that it would send in the EAP-Start packet. This ID is provided by
     50  *      the server during the challenge in full authentication. EAP-SIM does
     51  *      save this ID, but there is no mechanism to provide it to the upper
     52  *      level EAP system. Once this is done the server will recognize the
     53  *      ID and send a SIM/Re-authentication request.
     54  *
     55  *    - Version validation. Perhaps a real SIM card will provide a version
     56  *      of EAP-SIM that it supports? Currently we accept any version the
     57  *      server provides.
     58  *
     59  *    - Real SIM authentication. Right now Kc/SRES/Identity values are loaded
     60  *      from a settings file. If a real SIM is used they would need to be
     61  *      obtained there. This would require providing the SIM with a RAND, to
     62  *      have it run its GSM algorithm. Kc/SRES can then be derived from that.
     63  */
     64 
     65 /* RFC 4187, Section 11 */
     66 #define EAP_SIM_ST_START		0x0a
     67 #define EAP_SIM_ST_CHALLENGE		0x0b
     68 #define EAP_SIM_ST_NOTIFICATION		0x0c
     69 #define EAP_SIM_ST_CLIENT_ERROR		0x0e
     70 
     71 /* EAP-SIM value lengths */
     72 #define EAP_SIM_NONCE_LEN	16
     73 
     74 /*
     75  * Internal client state, tracked to ensure that we are receiving the right
     76  * messages at the right time.
     77  */
     78 enum eap_sim_state {
     79 	EAP_SIM_STATE_UNCONNECTED = 0,
     80 	EAP_SIM_STATE_START,
     81 	EAP_SIM_STATE_CHALLENGE,
     82 	EAP_SIM_STATE_SUCCESS,
     83 	EAP_SIM_STATE_ERROR
     84 };
     85 
     86 struct eap_sim_handle {
     87 	enum eap_sim_state state;
     88 	/* Identity from SIM */
     89 	char *identity;
     90 
     91 	/* EAP-SIM supported version list */
     92 	uint16_t *vlist;
     93 	uint16_t vlist_len;
     94 
     95 	/* Negotiated EAP-SIM version */
     96 	uint16_t selected_version;
     97 
     98 	/* Random generated nonce */
     99 	uint8_t nonce[EAP_SIM_NONCE_LEN];
    100 
    101 	/* Derived master key */
    102 	uint8_t mk[EAP_SIM_MK_LEN];
    103 
    104 	/* Derived K_encr key from PRNG */
    105 	uint8_t k_encr[EAP_SIM_K_ENCR_LEN];
    106 
    107 	/* Derived K_aut key from PRNG */
    108 	uint8_t k_aut[EAP_SIM_K_AUT_LEN];
    109 
    110 	/* Derived MSK from PRNG */
    111 	uint8_t msk[EAP_SIM_MSK_LEN];
    112 
    113 	/* Derived EMSK from PRNG */
    114 	uint8_t emsk[EAP_SIM_EMSK_LEN];
    115 
    116 	/* Save RANDS from AT_RAND attribute for session ID derivation */
    117 	uint8_t rands[EAP_SIM_RAND_LEN * 3];
    118 
    119 	/* Flag set if AT_ANY_ID_REQ was present */
    120 	bool any_id_req : 1;
    121 
    122 	/* Flag to indicate protected status indications */
    123 	bool protected : 1;
    124 
    125 	uint8_t *chal_pkt;
    126 	uint32_t pkt_len;
    127 
    128 	struct iwd_sim_auth *auth;
    129 	unsigned int auth_watch;
    130 };
    131 
    132 static void eap_sim_clear_secrets(struct eap_sim_handle *sim)
    133 {
    134 	explicit_bzero(sim->mk, sizeof(sim->mk));
    135 	explicit_bzero(sim->k_encr, sizeof(sim->k_encr));
    136 	explicit_bzero(sim->k_aut, sizeof(sim->k_aut));
    137 	explicit_bzero(sim->msk, sizeof(sim->msk));
    138 	explicit_bzero(sim->emsk, sizeof(sim->emsk));
    139 }
    140 
    141 static void eap_sim_free(struct eap_state *eap)
    142 {
    143 	struct eap_sim_handle *sim = eap_get_data(eap);
    144 
    145 	if (sim->auth)
    146 		sim_auth_unregistered_watch_remove(sim->auth, sim->auth_watch);
    147 
    148 	eap_sim_clear_secrets(sim);
    149 
    150 	l_free(sim->identity);
    151 	l_free(sim->vlist);
    152 	l_free(sim);
    153 
    154 	eap_set_data(eap, NULL);
    155 }
    156 
    157 /*
    158  * Derive the master key (MK):
    159  *  SHA1(identity | kc | nonce | version list | selected version)
    160  */
    161 static bool derive_master_key(const char *identity, const void *kc,
    162 		const void *nonce, const void *vlist, uint16_t vlist_len,
    163 		uint16_t selected_version, uint8_t *mk)
    164 {
    165 	int ret;
    166 	struct iovec iov[5];
    167 	struct l_checksum *checksum = l_checksum_new(L_CHECKSUM_SHA1);
    168 
    169 	if (!checksum) {
    170 		l_error("could not create SHA1 checksum");
    171 		return false;
    172 	}
    173 
    174 	iov[0].iov_base = (void *)identity;
    175 	iov[0].iov_len = strlen(identity);
    176 	iov[1].iov_base = (void *)kc;
    177 	iov[1].iov_len = EAP_SIM_KC_LEN * 3;
    178 	iov[2].iov_base = (void *)nonce;
    179 	iov[2].iov_len = EAP_SIM_NONCE_LEN;
    180 	iov[3].iov_base = (void *)vlist;
    181 	iov[3].iov_len = vlist_len;
    182 	iov[4].iov_base = &selected_version;
    183 	iov[4].iov_len = 2;
    184 
    185 	if (!l_checksum_updatev(checksum, iov, 5))
    186 		goto mk_error;
    187 
    188 	ret = l_checksum_get_digest(checksum, mk, EAP_SIM_MK_LEN);
    189 	l_checksum_free(checksum);
    190 
    191 	return (ret == EAP_SIM_MK_LEN);
    192 
    193 mk_error:
    194 	l_checksum_free(checksum);
    195 	l_error("error deriving master key");
    196 	return false;
    197 }
    198 
    199 /*
    200  * Handles EAP-SIM Start subtype
    201  */
    202 static void handle_start(struct eap_state *eap, const uint8_t *pkt,
    203 		size_t len)
    204 {
    205 	struct eap_sim_handle *sim = eap_get_data(eap);
    206 	struct eap_sim_tlv_iter iter;
    207 	uint16_t resp_len;
    208 	uint8_t *response;
    209 	uint8_t *pos;
    210 
    211 	if (len < 3) {
    212 		l_error("packet is too small");
    213 		goto start_error;
    214 	}
    215 
    216 	if (sim->state != EAP_SIM_STATE_UNCONNECTED) {
    217 		l_error("invalid packet for EAP-SIM state");
    218 		goto start_error;
    219 	}
    220 
    221 	eap_sim_tlv_iter_init(&iter, pkt + 3, len - 3);
    222 
    223 	while (eap_sim_tlv_iter_next(&iter)) {
    224 		const uint8_t *contents = eap_sim_tlv_iter_get_data(&iter);
    225 		uint16_t length = eap_sim_tlv_iter_get_length(&iter);
    226 
    227 		switch (eap_sim_tlv_iter_get_type(&iter)) {
    228 		case EAP_SIM_AT_VERSION_LIST:
    229 			/* Actual len (2) + version 1 (2) + padding (2) */
    230 			if (length < 6) {
    231 				l_error("AT_VERSION_LIST was malformed");
    232 				goto start_error;
    233 			}
    234 
    235 			sim->vlist_len = l_get_be16(contents);
    236 
    237 			/* check that attribute was properly padded */
    238 			if (length < 2 + align_len(sim->vlist_len, 4)) {
    239 				l_error("AT_VERSION_LIST was malformed");
    240 				goto start_error;
    241 			}
    242 
    243 			sim->vlist = l_memdup(contents + 2, sim->vlist_len);
    244 
    245 			sim->selected_version = sim->vlist[0];
    246 
    247 			break;
    248 
    249 		case EAP_SIM_AT_ANY_ID_REQ:
    250 			sim->any_id_req = true;
    251 
    252 			break;
    253 
    254 		case EAP_SIM_AT_PERMANENT_ID_REQ:
    255 		case EAP_SIM_AT_FULLAUTH_ID_REQ:
    256 			/*
    257 			 * TODO: Server requesting permanent ID/pseudonym
    258 			 */
    259 			break;
    260 
    261 		default:
    262 			l_error("attribute %u was found in Start",
    263 					eap_sim_tlv_iter_get_type(&iter));
    264 			goto start_error;
    265 		}
    266 	}
    267 
    268 	sim->state = EAP_SIM_STATE_START;
    269 
    270 	/* header + AT_NONCE + AT_SELECTED_VERSION */
    271 	resp_len = (8) + (20) + (4);
    272 	if (sim->any_id_req) {
    273 		/* + AT_IDENTITY */
    274 		resp_len += EAP_SIM_ROUND(strlen(sim->identity) + 4);
    275 	}
    276 
    277 	l_getrandom(sim->nonce, EAP_SIM_NONCE_LEN);
    278 
    279 	response = alloca(resp_len);
    280 	pos = response;
    281 
    282 	pos += eap_sim_build_header(eap, EAP_TYPE_SIM, EAP_SIM_ST_START, pos,
    283 			resp_len);
    284 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_NONCE, EAP_SIM_PAD_ZERO,
    285 			sim->nonce, EAP_SIM_NONCE_LEN);
    286 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_SELECTED_VERSION,
    287 			EAP_SIM_PAD_NONE, (uint8_t *)&sim->selected_version,
    288 			2);
    289 
    290 	if (sim->any_id_req)
    291 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_IDENTITY,
    292 				EAP_SIM_PAD_LENGTH, (uint8_t *)sim->identity,
    293 				strlen(sim->identity));
    294 
    295 	eap_method_respond(eap, response, resp_len);
    296 
    297 	return;
    298 
    299 start_error:
    300 	eap_sim_client_error(eap, EAP_TYPE_SIM, EAP_SIM_ERROR_PROCESS);
    301 }
    302 
    303 static void eap_sim_finish(struct eap_state *eap)
    304 {
    305 	struct eap_sim_handle *sim = eap_get_data(eap);
    306 	uint8_t session_id[1 + sizeof(sim->rands) + EAP_SIM_NONCE_LEN];
    307 
    308 	session_id[0] = EAP_TYPE_SIM;
    309 
    310 	memcpy(session_id + 1, sim->rands, sizeof(sim->rands));
    311 	memcpy(session_id + 1 + sizeof(sim->rands), sim->nonce,
    312 				EAP_SIM_NONCE_LEN);
    313 
    314 	eap_method_success(eap);
    315 	eap_set_key_material(eap, sim->msk, 32, sim->emsk, 32, NULL, 0,
    316 					session_id, sizeof(session_id));
    317 }
    318 
    319 static void gsm_callback(const uint8_t *sres, const uint8_t *kc,
    320 		void *user_data)
    321 {
    322 	struct eap_state *eap = user_data;
    323 	struct eap_sim_handle *sim = eap_get_data(eap);
    324 	uint16_t resp_len = 8 + 20;
    325 	uint8_t response[resp_len + 4 + (EAP_SIM_SRES_LEN * 3)];
    326 	uint8_t *pos = response;
    327 	uint8_t prng_buf[160];
    328 	uint8_t *mac_pos;
    329 	bool r;
    330 
    331 	if (!sres || !kc)
    332 		goto chal_error;
    333 
    334 	if (sim->protected)
    335 		resp_len += 4;
    336 
    337 	if (!derive_master_key(sim->identity, kc, sim->nonce, sim->vlist,
    338 			sim->vlist_len, sim->selected_version, sim->mk)) {
    339 		l_error("error deriving master key");
    340 		goto chal_fatal;
    341 	}
    342 
    343 	eap_sim_fips_prf(sim->mk, 20, prng_buf, 160);
    344 
    345 	r = eap_sim_get_encryption_keys(prng_buf, sim->k_encr, sim->k_aut,
    346 					sim->msk, sim->emsk);
    347 	explicit_bzero(prng_buf, sizeof(prng_buf));
    348 
    349 	if (!r) {
    350 		l_error("could not derive encryption keys");
    351 		goto chal_fatal;
    352 	}
    353 
    354 	if (!eap_sim_verify_mac(eap, EAP_TYPE_SIM, sim->chal_pkt,
    355 			sim->pkt_len, sim->k_aut, sim->nonce,
    356 			EAP_SIM_NONCE_LEN)) {
    357 		l_error("server MAC was invalid");
    358 		goto chal_error;
    359 	}
    360 
    361 	sim->state = EAP_SIM_STATE_CHALLENGE;
    362 
    363 	/*
    364 	 * TODO: When/If fast re-authentication is supported, the AT_ENCR_DATA
    365 	 *       attribute would be decrypted here. Currently there is no need
    366 	 *       or reason to do this without support for fast
    367 	 *       re-authentication.
    368 	 */
    369 
    370 	/* build response packet */
    371 	pos += eap_sim_build_header(eap, EAP_TYPE_SIM, EAP_SIM_ST_CHALLENGE,
    372 			pos, resp_len);
    373 
    374 	if (sim->protected)
    375 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_RESULT_IND,
    376 				EAP_SIM_PAD_NONE, NULL, 2);
    377 
    378 	/* save MAC position to know where to write it to */
    379 	mac_pos = pos;
    380 	pos += eap_sim_add_attribute(pos, EAP_SIM_AT_MAC, EAP_SIM_PAD_NONE,
    381 			NULL, EAP_SIM_MAC_LEN);
    382 
    383 	/* append SRES for MAC derivation */
    384 	memcpy(pos, sres, EAP_SIM_SRES_LEN * 3);
    385 	pos += EAP_SIM_SRES_LEN * 3;
    386 
    387 	if (!eap_sim_derive_mac(EAP_TYPE_SIM, response, pos - response,
    388 			sim->k_aut, mac_pos + 4)) {
    389 		l_error("could not derive MAC");
    390 		goto chal_fatal;
    391 	}
    392 
    393 	l_free(sim->chal_pkt);
    394 	sim->chal_pkt = NULL;
    395 
    396 	eap_method_respond(eap, response, resp_len);
    397 
    398 	if (!sim->protected) {
    399 		/*
    400 		 * Result indication not required, we must accept success.
    401 		 */
    402 		eap_sim_finish(eap);
    403 
    404 		sim->state = EAP_SIM_STATE_SUCCESS;
    405 	}
    406 
    407 	return;
    408 
    409 	/*
    410 	 * fatal, unrecoverable error
    411 	 */
    412 chal_fatal:
    413 	eap_method_error(eap);
    414 	sim->state = EAP_SIM_STATE_ERROR;
    415 	return;
    416 
    417 chal_error:
    418 	l_free(sim->chal_pkt);
    419 	sim->chal_pkt = NULL;
    420 	eap_sim_client_error(eap, EAP_TYPE_SIM, EAP_SIM_ERROR_PROCESS);
    421 }
    422 
    423 /*
    424  * Handles EAP-SIM Challenge subtype
    425  */
    426 static void handle_challenge(struct eap_state *eap, const uint8_t *pkt,
    427 		size_t len)
    428 {
    429 	struct eap_sim_handle *sim = eap_get_data(eap);
    430 	struct eap_sim_tlv_iter iter;
    431 	enum eap_sim_error code = EAP_SIM_ERROR_PROCESS;
    432 
    433 	if (sim->state != EAP_SIM_STATE_START) {
    434 		l_error("invalid packet for EAP-SIM state");
    435 		goto chal_error;
    436 	}
    437 
    438 	if (len < 3) {
    439 		l_error("packet is too small");
    440 		goto chal_error;
    441 	}
    442 
    443 	eap_sim_tlv_iter_init(&iter, pkt + 3, len - 3);
    444 
    445 	while (eap_sim_tlv_iter_next(&iter)) {
    446 		const uint8_t *contents = eap_sim_tlv_iter_get_data(&iter);
    447 		uint16_t length = eap_sim_tlv_iter_get_length(&iter);
    448 
    449 		switch (eap_sim_tlv_iter_get_type(&iter)) {
    450 		case EAP_SIM_AT_RAND:
    451 			if ((length - 2) / 16 != 3) {
    452 				l_error("insufficient RAND's %u",
    453 					(length - 2) / 16);
    454 				code = EAP_SIM_ERROR_CHALLENGE;
    455 				goto chal_error;
    456 			}
    457 			/*
    458 			 * TODO: check that RAND's are fresh. Existing RAND's
    459 			 * should only exist if we are re-authenticating to the
    460 			 * server, which is currently not implemented.
    461 			 */
    462 			memcpy(sim->rands, contents + 2, EAP_SIM_RAND_LEN * 3);
    463 			break;
    464 
    465 		case EAP_SIM_AT_RESULT_IND:
    466 			sim->protected = true;
    467 			break;
    468 
    469 		case EAP_SIM_AT_IV:
    470 		case EAP_SIM_AT_ENCR_DATA:
    471 		case EAP_SIM_AT_MAC:
    472 			/* need a case for these so the default won't get hit */
    473 			break;
    474 
    475 		default:
    476 			l_error("attribute type %u not allowed in Challenge",
    477 					eap_sim_tlv_iter_get_type(&iter));
    478 			goto chal_error;
    479 		}
    480 	}
    481 
    482 	sim->chal_pkt = l_memdup(pkt, len);
    483 	sim->pkt_len = len;
    484 
    485 	if (sim_auth_run_gsm(sim->auth, sim->rands, 3, gsm_callback, eap) < 0) {
    486 		l_free(sim->chal_pkt);
    487 		sim->chal_pkt = NULL;
    488 		goto chal_error;
    489 	}
    490 
    491 	return;
    492 
    493 chal_error:
    494 	eap_sim_client_error(eap, EAP_TYPE_SIM, code);
    495 }
    496 
    497 /*
    498  * Handles EAP-SIM Notification subtype
    499  */
    500 static void handle_notification(struct eap_state *eap, const uint8_t *pkt,
    501 		size_t len)
    502 {
    503 	struct eap_sim_handle *sim = eap_get_data(eap);
    504 	struct eap_sim_tlv_iter iter;
    505 	int32_t value = -1;
    506 
    507 	if (len < 3) {
    508 		l_error("packet is too small");
    509 		goto notif_error;
    510 	}
    511 
    512 	eap_sim_tlv_iter_init(&iter, pkt + 3, len - 3);
    513 
    514 	while (eap_sim_tlv_iter_next(&iter)) {
    515 		const uint8_t *contents = eap_sim_tlv_iter_get_data(&iter);
    516 		uint16_t length = eap_sim_tlv_iter_get_length(&iter);
    517 
    518 		switch (eap_sim_tlv_iter_get_type(&iter)) {
    519 		case EAP_SIM_AT_NOTIFICATION:
    520 			if (length < 2) {
    521 				l_error("malformed AT_NOTIFICATION");
    522 				goto notif_error;
    523 			}
    524 
    525 			value = l_get_be16(contents);
    526 
    527 			break;
    528 
    529 		case EAP_SIM_AT_IV:
    530 		case EAP_SIM_AT_ENCR_DATA:
    531 		case EAP_SIM_AT_PADDING:
    532 		case EAP_SIM_AT_MAC:
    533 			/* RFC 4186, Section 10.1 */
    534 			break;
    535 
    536 		default:
    537 			l_error("attribute type %u not allowed in Notification",
    538 					eap_sim_tlv_iter_get_type(&iter));
    539 			goto notif_error;
    540 		}
    541 	}
    542 
    543 	if (value == EAP_SIM_SUCCESS && sim->protected &&
    544 			sim->state == EAP_SIM_STATE_CHALLENGE) {
    545 		/* header + MAC + MAC header */
    546 		uint8_t response[8 + EAP_SIM_MAC_LEN + 4];
    547 		uint8_t *pos = response;
    548 
    549 		/*
    550 		 * Server sent successful result indication
    551 		 */
    552 		eap_sim_finish(eap);
    553 
    554 		/*
    555 		 * Build response packet
    556 		 */
    557 		pos += eap_sim_build_header(eap, EAP_TYPE_SIM,
    558 				EAP_SIM_ST_NOTIFICATION, pos, 20);
    559 		pos += eap_sim_add_attribute(pos, EAP_SIM_AT_MAC,
    560 				EAP_SIM_PAD_NONE, NULL, EAP_SIM_MAC_LEN);
    561 
    562 		if (!eap_sim_derive_mac(EAP_TYPE_SIM, response, pos - response,
    563 				sim->k_aut, response + 12)) {
    564 			l_error("could not derive MAC");
    565 			eap_method_error(eap);
    566 			sim->state = EAP_SIM_STATE_ERROR;
    567 			return;
    568 		}
    569 
    570 		eap_method_respond(eap, response, pos - response);
    571 
    572 		sim->state = EAP_SIM_STATE_SUCCESS;
    573 		return;
    574 	} else if (value == EAP_SIM_SUCCESS) {
    575 		/*
    576 		 * Unexpected success notification, what should
    577 		 * be done here?
    578 		 */
    579 		l_error("Unexpected success notification");
    580 	} else {
    581 		/*
    582 		 * All other values are error conditions.
    583 		 * Nothing unique can be done for any error so
    584 		 * print the code and signal EAP failure.
    585 		 */
    586 		l_error("Error authenticating: code=%u", value);
    587 	}
    588 
    589 notif_error:
    590 	eap_sim_client_error(eap, EAP_TYPE_SIM, EAP_SIM_ERROR_PROCESS);
    591 }
    592 
    593 static void eap_sim_handle_request(struct eap_state *eap,
    594 					const uint8_t *pkt, size_t len)
    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_SIM_ST_START:
    603 		handle_start(eap, pkt, len);
    604 		break;
    605 	case EAP_SIM_ST_CHALLENGE:
    606 		handle_challenge(eap, pkt, len);
    607 		break;
    608 	case EAP_SIM_ST_NOTIFICATION:
    609 		handle_notification(eap, pkt, len);
    610 		break;
    611 	default:
    612 		l_error("unknown EAP-SIM subtype: %u", pkt[0]);
    613 		goto req_error;
    614 	}
    615 
    616 	return;
    617 
    618 req_error:
    619 	eap_sim_client_error(eap, EAP_TYPE_SIM, EAP_SIM_ERROR_PROCESS);
    620 }
    621 
    622 static const char *eap_sim_get_identity(struct eap_state *eap)
    623 {
    624 	struct eap_sim_handle *sim = eap_get_data(eap);
    625 
    626 	return sim->identity;
    627 }
    628 
    629 static void auth_destroyed(void *data)
    630 {
    631 	struct eap_state *eap = data;
    632 	struct eap_sim_handle *sim = eap_get_data(eap);
    633 
    634 	/*
    635 	 * If AKA was already successful we can return. Also if the state
    636 	 * has been set to ERROR, then eap_method_error has already been called,
    637 	 * so we can return.
    638 	 */
    639 	if (sim->state == EAP_SIM_STATE_SUCCESS ||
    640 			sim->state == EAP_SIM_STATE_ERROR)
    641 		return;
    642 
    643 	l_error("auth provider destroyed before SIM could finish");
    644 
    645 	sim->state = EAP_SIM_STATE_ERROR;
    646 	eap_method_error(eap);
    647 }
    648 
    649 static int eap_sim_check_settings(struct l_settings *settings,
    650 					struct l_queue *secrets,
    651 					const char *prefix,
    652 					struct l_queue **out_missing)
    653 {
    654 	struct iwd_sim_auth *auth;
    655 
    656 	auth = iwd_sim_auth_find(true, false);
    657 	if (!auth) {
    658 		l_debug("No SIM driver available for EAP-SIM");
    659 		return -EUNATCH;
    660 	}
    661 
    662 	if (!iwd_sim_auth_get_nai(auth)) {
    663 		l_error("SIM driver didn't provide NAI");
    664 		return -ENOENT;
    665 	}
    666 
    667 	return 0;
    668 }
    669 
    670 static bool eap_sim_reset_state(struct eap_state *eap)
    671 {
    672 	struct eap_sim_handle *sim = eap_get_data(eap);
    673 
    674 	sim->state = EAP_SIM_STATE_UNCONNECTED;
    675 
    676 	l_free(sim->vlist);
    677 	sim->vlist = NULL;
    678 	l_free(sim->chal_pkt);
    679 	sim->chal_pkt = NULL;
    680 
    681 	memset(sim->nonce, 0, sizeof(sim->nonce));
    682 	eap_sim_clear_secrets(sim);
    683 
    684 	return true;
    685 }
    686 
    687 static bool eap_sim_load_settings(struct eap_state *eap,
    688 					struct l_settings *settings,
    689 					const char *prefix)
    690 {
    691 	struct eap_sim_handle *sim;
    692 
    693 	/*
    694 	 * No specific settings for EAP-SIM, the auth provider will have all
    695 	 * required data.
    696 	 */
    697 	sim = l_new(struct eap_sim_handle, 1);
    698 	eap_set_data(eap, sim);
    699 
    700 	sim->auth = iwd_sim_auth_find(true, false);
    701 	if (!sim->auth)
    702 		return false;
    703 
    704 	sim->auth_watch = sim_auth_unregistered_watch_add(sim->auth,
    705 			auth_destroyed, eap);
    706 	/*
    707 	 * RFC 4186 Section 4.2.1.6
    708 	 * EAP-SIM identity prefix is '1'
    709 	 */
    710 	sim->identity = l_strdup_printf("%c%s", '1',
    711 			iwd_sim_auth_get_nai(sim->auth));
    712 
    713 	return true;
    714 }
    715 
    716 static struct eap_method eap_sim = {
    717 	.request_type = EAP_TYPE_SIM,
    718 	.exports_msk = true,
    719 	.name = "SIM",
    720 	.free = eap_sim_free,
    721 	.handle_request = eap_sim_handle_request,
    722 	.check_settings = eap_sim_check_settings,
    723 	.load_settings = eap_sim_load_settings,
    724 	.get_identity = eap_sim_get_identity,
    725 	.reset_state = eap_sim_reset_state
    726 };
    727 
    728 static int eap_sim_init(void)
    729 {
    730 	l_debug("");
    731 	return eap_register_method(&eap_sim);
    732 }
    733 
    734 static void eap_sim_exit(void)
    735 {
    736 	l_debug("");
    737 	eap_unregister_method(&eap_sim);
    738 }
    739 
    740 EAP_METHOD_BUILTIN(eap_sim, eap_sim_init, eap_sim_exit)