eap_aka_prime.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * EAP peer method: EAP-AKA' (draft-arkko-eap-aka-kdf-10.txt)
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eap_peer/eap_i.h"
  17. #include "pcsc_funcs.h"
  18. #include "eap_common/eap_sim_common.h"
  19. #include "sha1.h"
  20. #include "sha256.h"
  21. #include "crypto.h"
  22. #include "eap_peer/eap_config.h"
  23. #ifdef CONFIG_USIM_SIMULATOR
  24. #include "hlr_auc_gw/milenage.h"
  25. #endif /* CONFIG_USIM_SIMULATOR */
  26. struct eap_aka_data {
  27. u8 ik[EAP_AKA_IK_LEN], ck[EAP_AKA_CK_LEN], res[EAP_AKA_RES_MAX_LEN];
  28. size_t res_len;
  29. u8 nonce_s[EAP_SIM_NONCE_S_LEN];
  30. u8 mk[EAP_SIM_MK_LEN];
  31. u8 k_aut[EAP_AKA_PRIME_K_AUT_LEN];
  32. u8 k_encr[EAP_SIM_K_ENCR_LEN];
  33. u8 k_re[EAP_AKA_PRIME_K_RE_LEN];
  34. u8 msk[EAP_SIM_KEYING_DATA_LEN];
  35. u8 emsk[EAP_EMSK_LEN];
  36. u8 rand[EAP_AKA_RAND_LEN], autn[EAP_AKA_AUTN_LEN];
  37. u8 auts[EAP_AKA_AUTS_LEN];
  38. int num_id_req, num_notification;
  39. u8 *pseudonym;
  40. size_t pseudonym_len;
  41. u8 *reauth_id;
  42. size_t reauth_id_len;
  43. int reauth;
  44. unsigned int counter, counter_too_small;
  45. u8 *last_eap_identity;
  46. size_t last_eap_identity_len;
  47. enum {
  48. CONTINUE, RESULT_SUCCESS, RESULT_FAILURE, SUCCESS, FAILURE
  49. } state;
  50. struct wpabuf *id_msgs;
  51. int prev_id;
  52. int result_ind, use_result_ind;
  53. u8 eap_method;
  54. };
  55. #ifndef CONFIG_NO_STDOUT_DEBUG
  56. static const char * eap_aka_state_txt(int state)
  57. {
  58. switch (state) {
  59. case CONTINUE:
  60. return "CONTINUE";
  61. case RESULT_SUCCESS:
  62. return "RESULT_SUCCESS";
  63. case RESULT_FAILURE:
  64. return "RESULT_FAILURE";
  65. case SUCCESS:
  66. return "SUCCESS";
  67. case FAILURE:
  68. return "FAILURE";
  69. default:
  70. return "?";
  71. }
  72. }
  73. #endif /* CONFIG_NO_STDOUT_DEBUG */
  74. static void eap_aka_state(struct eap_aka_data *data, int state)
  75. {
  76. wpa_printf(MSG_DEBUG, "EAP-AKA%s: %s -> %s",
  77. data->eap_method == EAP_TYPE_AKA_PRIME ? "'" : "",
  78. eap_aka_state_txt(data->state),
  79. eap_aka_state_txt(state));
  80. data->state = state;
  81. }
  82. static void * eap_aka_init(struct eap_sm *sm)
  83. {
  84. struct eap_aka_data *data;
  85. const char *phase1 = eap_get_config_phase1(sm);
  86. data = os_zalloc(sizeof(*data));
  87. if (data == NULL)
  88. return NULL;
  89. if (1)
  90. data->eap_method = EAP_TYPE_AKA_PRIME;
  91. else
  92. data->eap_method = EAP_TYPE_AKA;
  93. eap_aka_state(data, CONTINUE);
  94. data->prev_id = -1;
  95. data->result_ind = phase1 && os_strstr(phase1, "result_ind=1") != NULL;
  96. return data;
  97. }
  98. static void eap_aka_deinit(struct eap_sm *sm, void *priv)
  99. {
  100. struct eap_aka_data *data = priv;
  101. if (data) {
  102. os_free(data->pseudonym);
  103. os_free(data->reauth_id);
  104. os_free(data->last_eap_identity);
  105. wpabuf_free(data->id_msgs);
  106. os_free(data);
  107. }
  108. }
  109. static int eap_aka_umts_auth(struct eap_sm *sm, struct eap_aka_data *data)
  110. {
  111. struct eap_peer_config *conf;
  112. wpa_printf(MSG_DEBUG, "EAP-AKA: UMTS authentication algorithm");
  113. conf = eap_get_config(sm);
  114. if (conf == NULL)
  115. return -1;
  116. if (conf->pcsc) {
  117. return scard_umts_auth(sm->scard_ctx, data->rand,
  118. data->autn, data->res, &data->res_len,
  119. data->ik, data->ck, data->auts);
  120. }
  121. #ifdef CONFIG_USIM_SIMULATOR
  122. if (conf->password) {
  123. u8 opc[16], k[16], sqn[6];
  124. const char *pos;
  125. wpa_printf(MSG_DEBUG, "EAP-AKA: Use internal Milenage "
  126. "implementation for UMTS authentication");
  127. if (conf->password_len < 78) {
  128. wpa_printf(MSG_DEBUG, "EAP-AKA: invalid Milenage "
  129. "password");
  130. return -1;
  131. }
  132. pos = (const char *) conf->password;
  133. if (hexstr2bin(pos, k, 16))
  134. return -1;
  135. pos += 32;
  136. if (*pos != ':')
  137. return -1;
  138. pos++;
  139. if (hexstr2bin(pos, opc, 16))
  140. return -1;
  141. pos += 32;
  142. if (*pos != ':')
  143. return -1;
  144. pos++;
  145. if (hexstr2bin(pos, sqn, 6))
  146. return -1;
  147. return milenage_check(opc, k, sqn, data->rand, data->autn,
  148. data->ik, data->ck,
  149. data->res, &data->res_len, data->auts);
  150. }
  151. #endif /* CONFIG_USIM_SIMULATOR */
  152. #ifdef CONFIG_USIM_HARDCODED
  153. wpa_printf(MSG_DEBUG, "EAP-AKA: Use hardcoded Kc and SRES values for "
  154. "testing");
  155. /* These hardcoded Kc and SRES values are used for testing.
  156. * Could consider making them configurable. */
  157. os_memset(data->res, '2', EAP_AKA_RES_MAX_LEN);
  158. data->res_len = EAP_AKA_RES_MAX_LEN;
  159. os_memset(data->ik, '3', EAP_AKA_IK_LEN);
  160. os_memset(data->ck, '4', EAP_AKA_CK_LEN);
  161. {
  162. u8 autn[EAP_AKA_AUTN_LEN];
  163. os_memset(autn, '1', EAP_AKA_AUTN_LEN);
  164. if (os_memcmp(autn, data->autn, EAP_AKA_AUTN_LEN) != 0) {
  165. wpa_printf(MSG_WARNING, "EAP-AKA: AUTN did not match "
  166. "with expected value");
  167. return -1;
  168. }
  169. }
  170. #if 0
  171. {
  172. static int test_resync = 1;
  173. if (test_resync) {
  174. /* Test Resynchronization */
  175. test_resync = 0;
  176. return -2;
  177. }
  178. }
  179. #endif
  180. return 0;
  181. #else /* CONFIG_USIM_HARDCODED */
  182. wpa_printf(MSG_DEBUG, "EAP-AKA: No UMTS authentication algorith "
  183. "enabled");
  184. return -1;
  185. #endif /* CONFIG_USIM_HARDCODED */
  186. }
  187. #define CLEAR_PSEUDONYM 0x01
  188. #define CLEAR_REAUTH_ID 0x02
  189. #define CLEAR_EAP_ID 0x04
  190. static void eap_aka_clear_identities(struct eap_aka_data *data, int id)
  191. {
  192. wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old%s%s%s",
  193. id & CLEAR_PSEUDONYM ? " pseudonym" : "",
  194. id & CLEAR_REAUTH_ID ? " reauth_id" : "",
  195. id & CLEAR_EAP_ID ? " eap_id" : "");
  196. if (id & CLEAR_PSEUDONYM) {
  197. os_free(data->pseudonym);
  198. data->pseudonym = NULL;
  199. data->pseudonym_len = 0;
  200. }
  201. if (id & CLEAR_REAUTH_ID) {
  202. os_free(data->reauth_id);
  203. data->reauth_id = NULL;
  204. data->reauth_id_len = 0;
  205. }
  206. if (id & CLEAR_EAP_ID) {
  207. os_free(data->last_eap_identity);
  208. data->last_eap_identity = NULL;
  209. data->last_eap_identity_len = 0;
  210. }
  211. }
  212. static int eap_aka_learn_ids(struct eap_aka_data *data,
  213. struct eap_sim_attrs *attr)
  214. {
  215. if (attr->next_pseudonym) {
  216. os_free(data->pseudonym);
  217. data->pseudonym = os_malloc(attr->next_pseudonym_len);
  218. if (data->pseudonym == NULL) {
  219. wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
  220. "next pseudonym");
  221. return -1;
  222. }
  223. os_memcpy(data->pseudonym, attr->next_pseudonym,
  224. attr->next_pseudonym_len);
  225. data->pseudonym_len = attr->next_pseudonym_len;
  226. wpa_hexdump_ascii(MSG_DEBUG,
  227. "EAP-AKA: (encr) AT_NEXT_PSEUDONYM",
  228. data->pseudonym,
  229. data->pseudonym_len);
  230. }
  231. if (attr->next_reauth_id) {
  232. os_free(data->reauth_id);
  233. data->reauth_id = os_malloc(attr->next_reauth_id_len);
  234. if (data->reauth_id == NULL) {
  235. wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
  236. "next reauth_id");
  237. return -1;
  238. }
  239. os_memcpy(data->reauth_id, attr->next_reauth_id,
  240. attr->next_reauth_id_len);
  241. data->reauth_id_len = attr->next_reauth_id_len;
  242. wpa_hexdump_ascii(MSG_DEBUG,
  243. "EAP-AKA: (encr) AT_NEXT_REAUTH_ID",
  244. data->reauth_id,
  245. data->reauth_id_len);
  246. }
  247. return 0;
  248. }
  249. static int eap_aka_add_id_msg(struct eap_aka_data *data,
  250. const struct wpabuf *msg)
  251. {
  252. if (msg == NULL)
  253. return -1;
  254. if (data->id_msgs == NULL) {
  255. data->id_msgs = wpabuf_dup(msg);
  256. return data->id_msgs == NULL ? -1 : 0;
  257. }
  258. if (wpabuf_resize(&data->id_msgs, wpabuf_len(msg)) < 0)
  259. return -1;
  260. wpabuf_put_buf(data->id_msgs, msg);
  261. return 0;
  262. }
  263. static void eap_aka_add_checkcode(struct eap_aka_data *data,
  264. struct eap_sim_msg *msg)
  265. {
  266. const u8 *addr;
  267. size_t len;
  268. u8 hash[SHA256_MAC_LEN];
  269. wpa_printf(MSG_DEBUG, " AT_CHECKCODE");
  270. if (data->id_msgs == NULL) {
  271. /*
  272. * No EAP-AKA/Identity packets were exchanged - send empty
  273. * checkcode.
  274. */
  275. eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, NULL, 0);
  276. return;
  277. }
  278. /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
  279. addr = wpabuf_head(data->id_msgs);
  280. len = wpabuf_len(data->id_msgs);
  281. wpa_hexdump(MSG_MSGDUMP, "EAP-AKA: AT_CHECKCODE data", addr, len);
  282. if (data->eap_method == EAP_TYPE_AKA_PRIME)
  283. sha256_vector(1, &addr, &len, hash);
  284. else
  285. sha1_vector(1, &addr, &len, hash);
  286. eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, hash,
  287. data->eap_method == EAP_TYPE_AKA_PRIME ?
  288. EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN);
  289. }
  290. static int eap_aka_verify_checkcode(struct eap_aka_data *data,
  291. const u8 *checkcode, size_t checkcode_len)
  292. {
  293. const u8 *addr;
  294. size_t len;
  295. u8 hash[SHA256_MAC_LEN];
  296. size_t hash_len;
  297. if (checkcode == NULL)
  298. return -1;
  299. if (data->id_msgs == NULL) {
  300. if (checkcode_len != 0) {
  301. wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
  302. "indicates that AKA/Identity messages were "
  303. "used, but they were not");
  304. return -1;
  305. }
  306. return 0;
  307. }
  308. hash_len = data->eap_method == EAP_TYPE_AKA_PRIME ?
  309. EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN;
  310. if (checkcode_len != hash_len) {
  311. wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
  312. "indicates that AKA/Identity message were not "
  313. "used, but they were");
  314. return -1;
  315. }
  316. /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
  317. addr = wpabuf_head(data->id_msgs);
  318. len = wpabuf_len(data->id_msgs);
  319. if (data->eap_method == EAP_TYPE_AKA_PRIME)
  320. sha256_vector(1, &addr, &len, hash);
  321. else
  322. sha1_vector(1, &addr, &len, hash);
  323. if (os_memcmp(hash, checkcode, hash_len) != 0) {
  324. wpa_printf(MSG_DEBUG, "EAP-AKA: Mismatch in AT_CHECKCODE");
  325. return -1;
  326. }
  327. return 0;
  328. }
  329. static struct wpabuf * eap_aka_client_error(struct eap_aka_data *data, u8 id,
  330. int err)
  331. {
  332. struct eap_sim_msg *msg;
  333. eap_aka_state(data, FAILURE);
  334. data->num_id_req = 0;
  335. data->num_notification = 0;
  336. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  337. EAP_AKA_SUBTYPE_CLIENT_ERROR);
  338. eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
  339. return eap_sim_msg_finish(msg, NULL, NULL, 0);
  340. }
  341. static struct wpabuf * eap_aka_authentication_reject(struct eap_aka_data *data,
  342. u8 id)
  343. {
  344. struct eap_sim_msg *msg;
  345. eap_aka_state(data, FAILURE);
  346. data->num_id_req = 0;
  347. data->num_notification = 0;
  348. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Authentication-Reject "
  349. "(id=%d)", id);
  350. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  351. EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT);
  352. return eap_sim_msg_finish(msg, NULL, NULL, 0);
  353. }
  354. static struct wpabuf * eap_aka_synchronization_failure(
  355. struct eap_aka_data *data, u8 id)
  356. {
  357. struct eap_sim_msg *msg;
  358. data->num_id_req = 0;
  359. data->num_notification = 0;
  360. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Synchronization-Failure "
  361. "(id=%d)", id);
  362. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  363. EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE);
  364. wpa_printf(MSG_DEBUG, " AT_AUTS");
  365. eap_sim_msg_add_full(msg, EAP_SIM_AT_AUTS, data->auts,
  366. EAP_AKA_AUTS_LEN);
  367. return eap_sim_msg_finish(msg, NULL, NULL, 0);
  368. }
  369. static struct wpabuf * eap_aka_response_identity(struct eap_sm *sm,
  370. struct eap_aka_data *data,
  371. u8 id,
  372. enum eap_sim_id_req id_req)
  373. {
  374. const u8 *identity = NULL;
  375. size_t identity_len = 0;
  376. struct eap_sim_msg *msg;
  377. data->reauth = 0;
  378. if (id_req == ANY_ID && data->reauth_id) {
  379. identity = data->reauth_id;
  380. identity_len = data->reauth_id_len;
  381. data->reauth = 1;
  382. } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
  383. data->pseudonym) {
  384. identity = data->pseudonym;
  385. identity_len = data->pseudonym_len;
  386. eap_aka_clear_identities(data, CLEAR_REAUTH_ID);
  387. } else if (id_req != NO_ID_REQ) {
  388. identity = eap_get_config_identity(sm, &identity_len);
  389. if (identity) {
  390. eap_aka_clear_identities(data, CLEAR_PSEUDONYM |
  391. CLEAR_REAUTH_ID);
  392. }
  393. }
  394. if (id_req != NO_ID_REQ)
  395. eap_aka_clear_identities(data, CLEAR_EAP_ID);
  396. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Identity (id=%d)", id);
  397. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  398. EAP_AKA_SUBTYPE_IDENTITY);
  399. if (identity) {
  400. wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
  401. identity, identity_len);
  402. eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
  403. identity, identity_len);
  404. }
  405. return eap_sim_msg_finish(msg, NULL, NULL, 0);
  406. }
  407. static struct wpabuf * eap_aka_response_challenge(struct eap_aka_data *data,
  408. u8 id)
  409. {
  410. struct eap_sim_msg *msg;
  411. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d)", id);
  412. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  413. EAP_AKA_SUBTYPE_CHALLENGE);
  414. wpa_printf(MSG_DEBUG, " AT_RES");
  415. eap_sim_msg_add(msg, EAP_SIM_AT_RES, data->res_len * 8,
  416. data->res, data->res_len);
  417. eap_aka_add_checkcode(data, msg);
  418. if (data->use_result_ind) {
  419. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  420. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  421. }
  422. wpa_printf(MSG_DEBUG, " AT_MAC");
  423. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  424. return eap_sim_msg_finish(msg, data->k_aut, (u8 *) "", 0);
  425. }
  426. static struct wpabuf * eap_aka_response_reauth(struct eap_aka_data *data,
  427. u8 id, int counter_too_small,
  428. const u8 *nonce_s)
  429. {
  430. struct eap_sim_msg *msg;
  431. unsigned int counter;
  432. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Reauthentication (id=%d)",
  433. id);
  434. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  435. EAP_AKA_SUBTYPE_REAUTHENTICATION);
  436. wpa_printf(MSG_DEBUG, " AT_IV");
  437. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  438. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
  439. if (counter_too_small) {
  440. wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
  441. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
  442. counter = data->counter_too_small;
  443. } else
  444. counter = data->counter;
  445. wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
  446. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
  447. if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
  448. wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
  449. "AT_ENCR_DATA");
  450. eap_sim_msg_free(msg);
  451. return NULL;
  452. }
  453. eap_aka_add_checkcode(data, msg);
  454. if (data->use_result_ind) {
  455. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  456. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  457. }
  458. wpa_printf(MSG_DEBUG, " AT_MAC");
  459. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  460. return eap_sim_msg_finish(msg, data->k_aut, nonce_s,
  461. EAP_SIM_NONCE_S_LEN);
  462. }
  463. static struct wpabuf * eap_aka_response_notification(struct eap_aka_data *data,
  464. u8 id, u16 notification)
  465. {
  466. struct eap_sim_msg *msg;
  467. u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
  468. wpa_printf(MSG_DEBUG, "Generating EAP-AKA Notification (id=%d)", id);
  469. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
  470. EAP_AKA_SUBTYPE_NOTIFICATION);
  471. if (k_aut && data->reauth) {
  472. wpa_printf(MSG_DEBUG, " AT_IV");
  473. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  474. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
  475. EAP_SIM_AT_ENCR_DATA);
  476. wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
  477. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
  478. NULL, 0);
  479. if (eap_sim_msg_add_encr_end(msg, data->k_encr,
  480. EAP_SIM_AT_PADDING)) {
  481. wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
  482. "AT_ENCR_DATA");
  483. eap_sim_msg_free(msg);
  484. return NULL;
  485. }
  486. }
  487. if (k_aut) {
  488. wpa_printf(MSG_DEBUG, " AT_MAC");
  489. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  490. }
  491. return eap_sim_msg_finish(msg, k_aut, (u8 *) "", 0);
  492. }
  493. static struct wpabuf * eap_aka_process_identity(struct eap_sm *sm,
  494. struct eap_aka_data *data,
  495. u8 id,
  496. const struct wpabuf *reqData,
  497. struct eap_sim_attrs *attr)
  498. {
  499. int id_error;
  500. struct wpabuf *buf;
  501. wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Identity");
  502. id_error = 0;
  503. switch (attr->id_req) {
  504. case NO_ID_REQ:
  505. break;
  506. case ANY_ID:
  507. if (data->num_id_req > 0)
  508. id_error++;
  509. data->num_id_req++;
  510. break;
  511. case FULLAUTH_ID:
  512. if (data->num_id_req > 1)
  513. id_error++;
  514. data->num_id_req++;
  515. break;
  516. case PERMANENT_ID:
  517. if (data->num_id_req > 2)
  518. id_error++;
  519. data->num_id_req++;
  520. break;
  521. }
  522. if (id_error) {
  523. wpa_printf(MSG_INFO, "EAP-AKA: Too many ID requests "
  524. "used within one authentication");
  525. return eap_aka_client_error(data, id,
  526. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  527. }
  528. buf = eap_aka_response_identity(sm, data, id, attr->id_req);
  529. if (data->prev_id != id) {
  530. eap_aka_add_id_msg(data, reqData);
  531. eap_aka_add_id_msg(data, buf);
  532. data->prev_id = id;
  533. }
  534. return buf;
  535. }
  536. static int eap_aka_verify_mac(struct eap_aka_data *data,
  537. const struct wpabuf *req,
  538. const u8 *mac, const u8 *extra,
  539. size_t extra_len)
  540. {
  541. if (data->eap_method == EAP_TYPE_AKA_PRIME)
  542. return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra,
  543. extra_len);
  544. return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len);
  545. }
  546. static struct wpabuf * eap_aka_process_challenge(struct eap_sm *sm,
  547. struct eap_aka_data *data,
  548. u8 id,
  549. const struct wpabuf *reqData,
  550. struct eap_sim_attrs *attr)
  551. {
  552. const u8 *identity;
  553. size_t identity_len;
  554. int res;
  555. struct eap_sim_attrs eattr;
  556. wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Challenge");
  557. if (attr->checkcode &&
  558. eap_aka_verify_checkcode(data, attr->checkcode,
  559. attr->checkcode_len)) {
  560. wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
  561. "message");
  562. return eap_aka_client_error(data, id,
  563. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  564. }
  565. data->reauth = 0;
  566. if (!attr->mac || !attr->rand || !attr->autn) {
  567. wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
  568. "did not include%s%s%s",
  569. !attr->mac ? " AT_MAC" : "",
  570. !attr->rand ? " AT_RAND" : "",
  571. !attr->autn ? " AT_AUTN" : "");
  572. return eap_aka_client_error(data, id,
  573. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  574. }
  575. os_memcpy(data->rand, attr->rand, EAP_AKA_RAND_LEN);
  576. os_memcpy(data->autn, attr->autn, EAP_AKA_AUTN_LEN);
  577. res = eap_aka_umts_auth(sm, data);
  578. if (res == -1) {
  579. wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
  580. "failed (AUTN)");
  581. return eap_aka_authentication_reject(data, id);
  582. } else if (res == -2) {
  583. wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
  584. "failed (AUTN seq# -> AUTS)");
  585. return eap_aka_synchronization_failure(data, id);
  586. } else if (res) {
  587. wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication failed");
  588. return eap_aka_client_error(data, id,
  589. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  590. }
  591. if (data->last_eap_identity) {
  592. identity = data->last_eap_identity;
  593. identity_len = data->last_eap_identity_len;
  594. } else if (data->pseudonym) {
  595. identity = data->pseudonym;
  596. identity_len = data->pseudonym_len;
  597. } else
  598. identity = eap_get_config_identity(sm, &identity_len);
  599. wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Selected identity for MK "
  600. "derivation", identity, identity_len);
  601. if (data->eap_method == EAP_TYPE_AKA_PRIME) {
  602. eap_aka_prime_derive_keys(identity, identity_len, data->ik,
  603. data->ck, data->k_encr, data->k_aut,
  604. data->k_re, data->msk, data->emsk);
  605. } else {
  606. eap_aka_derive_mk(identity, identity_len, data->ik, data->ck,
  607. data->mk);
  608. eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut,
  609. data->msk, data->emsk);
  610. }
  611. if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
  612. wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
  613. "used invalid AT_MAC");
  614. return eap_aka_client_error(data, id,
  615. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  616. }
  617. /* Old reauthentication and pseudonym identities must not be used
  618. * anymore. In other words, if no new identities are received, full
  619. * authentication will be used on next reauthentication. */
  620. eap_aka_clear_identities(data, CLEAR_PSEUDONYM | CLEAR_REAUTH_ID |
  621. CLEAR_EAP_ID);
  622. if (attr->encr_data) {
  623. u8 *decrypted;
  624. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  625. attr->encr_data_len, attr->iv,
  626. &eattr, 0);
  627. if (decrypted == NULL) {
  628. return eap_aka_client_error(
  629. data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  630. }
  631. eap_aka_learn_ids(data, &eattr);
  632. os_free(decrypted);
  633. }
  634. if (data->result_ind && attr->result_ind)
  635. data->use_result_ind = 1;
  636. if (data->state != FAILURE && data->state != RESULT_FAILURE) {
  637. eap_aka_state(data, data->use_result_ind ?
  638. RESULT_SUCCESS : SUCCESS);
  639. }
  640. data->num_id_req = 0;
  641. data->num_notification = 0;
  642. /* RFC 4187 specifies that counter is initialized to one after
  643. * fullauth, but initializing it to zero makes it easier to implement
  644. * reauth verification. */
  645. data->counter = 0;
  646. return eap_aka_response_challenge(data, id);
  647. }
  648. static int eap_aka_process_notification_reauth(struct eap_aka_data *data,
  649. struct eap_sim_attrs *attr)
  650. {
  651. struct eap_sim_attrs eattr;
  652. u8 *decrypted;
  653. if (attr->encr_data == NULL || attr->iv == NULL) {
  654. wpa_printf(MSG_WARNING, "EAP-AKA: Notification message after "
  655. "reauth did not include encrypted data");
  656. return -1;
  657. }
  658. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  659. attr->encr_data_len, attr->iv, &eattr,
  660. 0);
  661. if (decrypted == NULL) {
  662. wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
  663. "data from notification message");
  664. return -1;
  665. }
  666. if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
  667. wpa_printf(MSG_WARNING, "EAP-AKA: Counter in notification "
  668. "message does not match with counter in reauth "
  669. "message");
  670. os_free(decrypted);
  671. return -1;
  672. }
  673. os_free(decrypted);
  674. return 0;
  675. }
  676. static int eap_aka_process_notification_auth(struct eap_aka_data *data,
  677. const struct wpabuf *reqData,
  678. struct eap_sim_attrs *attr)
  679. {
  680. if (attr->mac == NULL) {
  681. wpa_printf(MSG_INFO, "EAP-AKA: no AT_MAC in after_auth "
  682. "Notification message");
  683. return -1;
  684. }
  685. if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
  686. wpa_printf(MSG_WARNING, "EAP-AKA: Notification message "
  687. "used invalid AT_MAC");
  688. return -1;
  689. }
  690. if (data->reauth &&
  691. eap_aka_process_notification_reauth(data, attr)) {
  692. wpa_printf(MSG_WARNING, "EAP-AKA: Invalid notification "
  693. "message after reauth");
  694. return -1;
  695. }
  696. return 0;
  697. }
  698. static struct wpabuf * eap_aka_process_notification(
  699. struct eap_sm *sm, struct eap_aka_data *data, u8 id,
  700. const struct wpabuf *reqData, struct eap_sim_attrs *attr)
  701. {
  702. wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Notification");
  703. if (data->num_notification > 0) {
  704. wpa_printf(MSG_INFO, "EAP-AKA: too many notification "
  705. "rounds (only one allowed)");
  706. return eap_aka_client_error(data, id,
  707. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  708. }
  709. data->num_notification++;
  710. if (attr->notification == -1) {
  711. wpa_printf(MSG_INFO, "EAP-AKA: no AT_NOTIFICATION in "
  712. "Notification message");
  713. return eap_aka_client_error(data, id,
  714. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  715. }
  716. if ((attr->notification & 0x4000) == 0 &&
  717. eap_aka_process_notification_auth(data, reqData, attr)) {
  718. return eap_aka_client_error(data, id,
  719. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  720. }
  721. eap_sim_report_notification(sm->msg_ctx, attr->notification, 1);
  722. if (attr->notification >= 0 && attr->notification < 32768) {
  723. eap_aka_state(data, FAILURE);
  724. } else if (attr->notification == EAP_SIM_SUCCESS &&
  725. data->state == RESULT_SUCCESS)
  726. eap_aka_state(data, SUCCESS);
  727. return eap_aka_response_notification(data, id, attr->notification);
  728. }
  729. static struct wpabuf * eap_aka_process_reauthentication(
  730. struct eap_sm *sm, struct eap_aka_data *data, u8 id,
  731. const struct wpabuf *reqData, struct eap_sim_attrs *attr)
  732. {
  733. struct eap_sim_attrs eattr;
  734. u8 *decrypted;
  735. wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Reauthentication");
  736. if (attr->checkcode &&
  737. eap_aka_verify_checkcode(data, attr->checkcode,
  738. attr->checkcode_len)) {
  739. wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
  740. "message");
  741. return eap_aka_client_error(data, id,
  742. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  743. }
  744. if (data->reauth_id == NULL) {
  745. wpa_printf(MSG_WARNING, "EAP-AKA: Server is trying "
  746. "reauthentication, but no reauth_id available");
  747. return eap_aka_client_error(data, id,
  748. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  749. }
  750. data->reauth = 1;
  751. if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
  752. wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
  753. "did not have valid AT_MAC");
  754. return eap_aka_client_error(data, id,
  755. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  756. }
  757. if (attr->encr_data == NULL || attr->iv == NULL) {
  758. wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
  759. "message did not include encrypted data");
  760. return eap_aka_client_error(data, id,
  761. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  762. }
  763. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  764. attr->encr_data_len, attr->iv, &eattr,
  765. 0);
  766. if (decrypted == NULL) {
  767. wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
  768. "data from reauthentication message");
  769. return eap_aka_client_error(data, id,
  770. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  771. }
  772. if (eattr.nonce_s == NULL || eattr.counter < 0) {
  773. wpa_printf(MSG_INFO, "EAP-AKA: (encr) No%s%s in reauth packet",
  774. !eattr.nonce_s ? " AT_NONCE_S" : "",
  775. eattr.counter < 0 ? " AT_COUNTER" : "");
  776. os_free(decrypted);
  777. return eap_aka_client_error(data, id,
  778. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  779. }
  780. if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
  781. struct wpabuf *res;
  782. wpa_printf(MSG_INFO, "EAP-AKA: (encr) Invalid counter "
  783. "(%d <= %d)", eattr.counter, data->counter);
  784. data->counter_too_small = eattr.counter;
  785. /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
  786. * reauth_id must not be used to start a new reauthentication.
  787. * However, since it was used in the last EAP-Response-Identity
  788. * packet, it has to saved for the following fullauth to be
  789. * used in MK derivation. */
  790. os_free(data->last_eap_identity);
  791. data->last_eap_identity = data->reauth_id;
  792. data->last_eap_identity_len = data->reauth_id_len;
  793. data->reauth_id = NULL;
  794. data->reauth_id_len = 0;
  795. res = eap_aka_response_reauth(data, id, 1, eattr.nonce_s);
  796. os_free(decrypted);
  797. return res;
  798. }
  799. data->counter = eattr.counter;
  800. os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
  801. wpa_hexdump(MSG_DEBUG, "EAP-AKA: (encr) AT_NONCE_S",
  802. data->nonce_s, EAP_SIM_NONCE_S_LEN);
  803. if (data->eap_method == EAP_TYPE_AKA_PRIME) {
  804. eap_aka_prime_derive_keys_reauth(data->k_re, data->counter,
  805. data->reauth_id,
  806. data->reauth_id_len,
  807. data->nonce_s,
  808. data->msk, data->emsk);
  809. } else {
  810. eap_sim_derive_keys_reauth(data->counter, data->reauth_id,
  811. data->reauth_id_len,
  812. data->nonce_s, data->mk,
  813. data->msk, data->emsk);
  814. }
  815. eap_aka_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
  816. eap_aka_learn_ids(data, &eattr);
  817. if (data->result_ind && attr->result_ind)
  818. data->use_result_ind = 1;
  819. if (data->state != FAILURE && data->state != RESULT_FAILURE) {
  820. eap_aka_state(data, data->use_result_ind ?
  821. RESULT_SUCCESS : SUCCESS);
  822. }
  823. data->num_id_req = 0;
  824. data->num_notification = 0;
  825. if (data->counter > EAP_AKA_MAX_FAST_REAUTHS) {
  826. wpa_printf(MSG_DEBUG, "EAP-AKA: Maximum number of "
  827. "fast reauths performed - force fullauth");
  828. eap_aka_clear_identities(data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
  829. }
  830. os_free(decrypted);
  831. return eap_aka_response_reauth(data, id, 0, data->nonce_s);
  832. }
  833. static struct wpabuf * eap_aka_process(struct eap_sm *sm, void *priv,
  834. struct eap_method_ret *ret,
  835. const struct wpabuf *reqData)
  836. {
  837. struct eap_aka_data *data = priv;
  838. const struct eap_hdr *req;
  839. u8 subtype, id;
  840. struct wpabuf *res;
  841. const u8 *pos;
  842. struct eap_sim_attrs attr;
  843. size_t len;
  844. wpa_hexdump_buf(MSG_DEBUG, "EAP-AKA: EAP data", reqData);
  845. if (eap_get_config_identity(sm, &len) == NULL) {
  846. wpa_printf(MSG_INFO, "EAP-AKA: Identity not configured");
  847. eap_sm_request_identity(sm);
  848. ret->ignore = TRUE;
  849. return NULL;
  850. }
  851. pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_method, reqData,
  852. &len);
  853. if (pos == NULL || len < 1) {
  854. ret->ignore = TRUE;
  855. return NULL;
  856. }
  857. req = wpabuf_head(reqData);
  858. id = req->identifier;
  859. len = be_to_host16(req->length);
  860. ret->ignore = FALSE;
  861. ret->methodState = METHOD_MAY_CONT;
  862. ret->decision = DECISION_FAIL;
  863. ret->allowNotifications = TRUE;
  864. subtype = *pos++;
  865. wpa_printf(MSG_DEBUG, "EAP-AKA: Subtype=%d", subtype);
  866. pos += 2; /* Reserved */
  867. if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 1,
  868. 0)) {
  869. res = eap_aka_client_error(data, id,
  870. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  871. goto done;
  872. }
  873. switch (subtype) {
  874. case EAP_AKA_SUBTYPE_IDENTITY:
  875. res = eap_aka_process_identity(sm, data, id, reqData, &attr);
  876. break;
  877. case EAP_AKA_SUBTYPE_CHALLENGE:
  878. res = eap_aka_process_challenge(sm, data, id, reqData, &attr);
  879. break;
  880. case EAP_AKA_SUBTYPE_NOTIFICATION:
  881. res = eap_aka_process_notification(sm, data, id, reqData,
  882. &attr);
  883. break;
  884. case EAP_AKA_SUBTYPE_REAUTHENTICATION:
  885. res = eap_aka_process_reauthentication(sm, data, id, reqData,
  886. &attr);
  887. break;
  888. case EAP_AKA_SUBTYPE_CLIENT_ERROR:
  889. wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Client-Error");
  890. res = eap_aka_client_error(data, id,
  891. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  892. break;
  893. default:
  894. wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown subtype=%d", subtype);
  895. res = eap_aka_client_error(data, id,
  896. EAP_AKA_UNABLE_TO_PROCESS_PACKET);
  897. break;
  898. }
  899. done:
  900. if (data->state == FAILURE) {
  901. ret->decision = DECISION_FAIL;
  902. ret->methodState = METHOD_DONE;
  903. } else if (data->state == SUCCESS) {
  904. ret->decision = data->use_result_ind ?
  905. DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
  906. /*
  907. * It is possible for the server to reply with AKA
  908. * Notification, so we must allow the method to continue and
  909. * not only accept EAP-Success at this point.
  910. */
  911. ret->methodState = data->use_result_ind ?
  912. METHOD_DONE : METHOD_MAY_CONT;
  913. } else if (data->state == RESULT_FAILURE)
  914. ret->methodState = METHOD_CONT;
  915. else if (data->state == RESULT_SUCCESS)
  916. ret->methodState = METHOD_CONT;
  917. if (ret->methodState == METHOD_DONE) {
  918. ret->allowNotifications = FALSE;
  919. }
  920. return res;
  921. }
  922. static Boolean eap_aka_has_reauth_data(struct eap_sm *sm, void *priv)
  923. {
  924. struct eap_aka_data *data = priv;
  925. return data->pseudonym || data->reauth_id;
  926. }
  927. static void eap_aka_deinit_for_reauth(struct eap_sm *sm, void *priv)
  928. {
  929. struct eap_aka_data *data = priv;
  930. eap_aka_clear_identities(data, CLEAR_EAP_ID);
  931. data->prev_id = -1;
  932. wpabuf_free(data->id_msgs);
  933. data->id_msgs = NULL;
  934. data->use_result_ind = 0;
  935. }
  936. static void * eap_aka_init_for_reauth(struct eap_sm *sm, void *priv)
  937. {
  938. struct eap_aka_data *data = priv;
  939. data->num_id_req = 0;
  940. data->num_notification = 0;
  941. eap_aka_state(data, CONTINUE);
  942. return priv;
  943. }
  944. static const u8 * eap_aka_get_identity(struct eap_sm *sm, void *priv,
  945. size_t *len)
  946. {
  947. struct eap_aka_data *data = priv;
  948. if (data->reauth_id) {
  949. *len = data->reauth_id_len;
  950. return data->reauth_id;
  951. }
  952. if (data->pseudonym) {
  953. *len = data->pseudonym_len;
  954. return data->pseudonym;
  955. }
  956. return NULL;
  957. }
  958. static Boolean eap_aka_isKeyAvailable(struct eap_sm *sm, void *priv)
  959. {
  960. struct eap_aka_data *data = priv;
  961. return data->state == SUCCESS;
  962. }
  963. static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len)
  964. {
  965. struct eap_aka_data *data = priv;
  966. u8 *key;
  967. if (data->state != SUCCESS)
  968. return NULL;
  969. key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
  970. if (key == NULL)
  971. return NULL;
  972. *len = EAP_SIM_KEYING_DATA_LEN;
  973. os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
  974. return key;
  975. }
  976. static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  977. {
  978. struct eap_aka_data *data = priv;
  979. u8 *key;
  980. if (data->state != SUCCESS)
  981. return NULL;
  982. key = os_malloc(EAP_EMSK_LEN);
  983. if (key == NULL)
  984. return NULL;
  985. *len = EAP_EMSK_LEN;
  986. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  987. return key;
  988. }
  989. int eap_peer_aka_prime_register(void)
  990. {
  991. struct eap_method *eap;
  992. int ret;
  993. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  994. EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME,
  995. "AKA'");
  996. if (eap == NULL)
  997. return -1;
  998. eap->init = eap_aka_init;
  999. eap->deinit = eap_aka_deinit;
  1000. eap->process = eap_aka_process;
  1001. eap->isKeyAvailable = eap_aka_isKeyAvailable;
  1002. eap->getKey = eap_aka_getKey;
  1003. eap->has_reauth_data = eap_aka_has_reauth_data;
  1004. eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
  1005. eap->init_for_reauth = eap_aka_init_for_reauth;
  1006. eap->get_identity = eap_aka_get_identity;
  1007. eap->get_emsk = eap_aka_get_emsk;
  1008. ret = eap_peer_method_register(eap);
  1009. if (ret)
  1010. eap_peer_method_free(eap);
  1011. return ret;
  1012. }