eap_sim.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * EAP peer method: EAP-SIM (RFC 4186)
  3. * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "pcsc_funcs.h"
  11. #include "crypto/milenage.h"
  12. #include "crypto/random.h"
  13. #include "eap_peer/eap_i.h"
  14. #include "eap_config.h"
  15. #include "eap_common/eap_sim_common.h"
  16. struct eap_sim_data {
  17. u8 *ver_list;
  18. size_t ver_list_len;
  19. int selected_version;
  20. size_t min_num_chal, num_chal;
  21. u8 kc[3][EAP_SIM_KC_LEN];
  22. u8 sres[3][EAP_SIM_SRES_LEN];
  23. u8 nonce_mt[EAP_SIM_NONCE_MT_LEN], nonce_s[EAP_SIM_NONCE_S_LEN];
  24. u8 mk[EAP_SIM_MK_LEN];
  25. u8 k_aut[EAP_SIM_K_AUT_LEN];
  26. u8 k_encr[EAP_SIM_K_ENCR_LEN];
  27. u8 msk[EAP_SIM_KEYING_DATA_LEN];
  28. u8 emsk[EAP_EMSK_LEN];
  29. u8 rand[3][GSM_RAND_LEN];
  30. int num_id_req, num_notification;
  31. u8 *pseudonym;
  32. size_t pseudonym_len;
  33. u8 *reauth_id;
  34. size_t reauth_id_len;
  35. int reauth;
  36. unsigned int counter, counter_too_small;
  37. u8 *last_eap_identity;
  38. size_t last_eap_identity_len;
  39. enum {
  40. CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
  41. } state;
  42. int result_ind, use_result_ind;
  43. };
  44. #ifndef CONFIG_NO_STDOUT_DEBUG
  45. static const char * eap_sim_state_txt(int state)
  46. {
  47. switch (state) {
  48. case CONTINUE:
  49. return "CONTINUE";
  50. case RESULT_SUCCESS:
  51. return "RESULT_SUCCESS";
  52. case SUCCESS:
  53. return "SUCCESS";
  54. case FAILURE:
  55. return "FAILURE";
  56. default:
  57. return "?";
  58. }
  59. }
  60. #endif /* CONFIG_NO_STDOUT_DEBUG */
  61. static void eap_sim_state(struct eap_sim_data *data, int state)
  62. {
  63. wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
  64. eap_sim_state_txt(data->state),
  65. eap_sim_state_txt(state));
  66. data->state = state;
  67. }
  68. static void * eap_sim_init(struct eap_sm *sm)
  69. {
  70. struct eap_sim_data *data;
  71. struct eap_peer_config *config = eap_get_config(sm);
  72. data = os_zalloc(sizeof(*data));
  73. if (data == NULL)
  74. return NULL;
  75. if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
  76. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
  77. "for NONCE_MT");
  78. os_free(data);
  79. return NULL;
  80. }
  81. data->min_num_chal = 2;
  82. if (config && config->phase1) {
  83. char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
  84. if (pos) {
  85. data->min_num_chal = atoi(pos + 17);
  86. if (data->min_num_chal < 2 || data->min_num_chal > 3) {
  87. wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
  88. "sim_min_num_chal configuration "
  89. "(%lu, expected 2 or 3)",
  90. (unsigned long) data->min_num_chal);
  91. os_free(data);
  92. return NULL;
  93. }
  94. wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
  95. "challenges to %lu",
  96. (unsigned long) data->min_num_chal);
  97. }
  98. data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
  99. NULL;
  100. }
  101. if (config && config->anonymous_identity) {
  102. data->pseudonym = os_malloc(config->anonymous_identity_len);
  103. if (data->pseudonym) {
  104. os_memcpy(data->pseudonym, config->anonymous_identity,
  105. config->anonymous_identity_len);
  106. data->pseudonym_len = config->anonymous_identity_len;
  107. }
  108. }
  109. eap_sim_state(data, CONTINUE);
  110. return data;
  111. }
  112. static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth)
  113. {
  114. if (!reauth) {
  115. os_memset(data->mk, 0, EAP_SIM_MK_LEN);
  116. os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN);
  117. os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
  118. }
  119. os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN);
  120. os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN);
  121. os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
  122. os_memset(data->emsk, 0, EAP_EMSK_LEN);
  123. }
  124. static void eap_sim_deinit(struct eap_sm *sm, void *priv)
  125. {
  126. struct eap_sim_data *data = priv;
  127. if (data) {
  128. os_free(data->ver_list);
  129. os_free(data->pseudonym);
  130. os_free(data->reauth_id);
  131. os_free(data->last_eap_identity);
  132. eap_sim_clear_keys(data, 0);
  133. os_free(data);
  134. }
  135. }
  136. static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
  137. {
  138. char req[200], *pos, *end;
  139. size_t i;
  140. wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
  141. pos = req;
  142. end = pos + sizeof(req);
  143. pos += os_snprintf(pos, end - pos, "GSM-AUTH");
  144. for (i = 0; i < data->num_chal; i++) {
  145. pos += os_snprintf(pos, end - pos, ":");
  146. pos += wpa_snprintf_hex(pos, end - pos, data->rand[i],
  147. GSM_RAND_LEN);
  148. }
  149. eap_sm_request_sim(sm, req);
  150. return 1;
  151. }
  152. static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
  153. struct eap_peer_config *conf)
  154. {
  155. char *resp, *pos;
  156. size_t i;
  157. wpa_printf(MSG_DEBUG,
  158. "EAP-SIM: Use result from external SIM processing");
  159. resp = conf->external_sim_resp;
  160. conf->external_sim_resp = NULL;
  161. if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
  162. wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
  163. os_free(resp);
  164. return -1;
  165. }
  166. pos = resp + 9;
  167. for (i = 0; i < data->num_chal; i++) {
  168. wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
  169. data->rand[i], GSM_RAND_LEN);
  170. if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
  171. goto invalid;
  172. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
  173. data->kc[i], EAP_SIM_KC_LEN);
  174. pos += EAP_SIM_KC_LEN * 2;
  175. if (*pos != ':')
  176. goto invalid;
  177. pos++;
  178. if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
  179. goto invalid;
  180. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
  181. data->sres[i], EAP_SIM_SRES_LEN);
  182. pos += EAP_SIM_SRES_LEN * 2;
  183. if (i + 1 < data->num_chal) {
  184. if (*pos != ':')
  185. goto invalid;
  186. pos++;
  187. }
  188. }
  189. os_free(resp);
  190. return 0;
  191. invalid:
  192. wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
  193. os_free(resp);
  194. return -1;
  195. }
  196. static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
  197. {
  198. struct eap_peer_config *conf;
  199. wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
  200. conf = eap_get_config(sm);
  201. if (conf == NULL)
  202. return -1;
  203. if (sm->external_sim) {
  204. if (conf->external_sim_resp)
  205. return eap_sim_ext_sim_result(sm, data, conf);
  206. else
  207. return eap_sim_ext_sim_req(sm, data);
  208. }
  209. if (conf->pcsc) {
  210. if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
  211. data->sres[0], data->kc[0]) ||
  212. scard_gsm_auth(sm->scard_ctx, data->rand[1],
  213. data->sres[1], data->kc[1]) ||
  214. (data->num_chal > 2 &&
  215. scard_gsm_auth(sm->scard_ctx, data->rand[2],
  216. data->sres[2], data->kc[2]))) {
  217. wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM "
  218. "authentication could not be completed");
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. #ifdef CONFIG_SIM_SIMULATOR
  224. if (conf->password) {
  225. u8 opc[16], k[16];
  226. const char *pos;
  227. size_t i;
  228. wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage "
  229. "implementation for authentication");
  230. if (conf->password_len < 65) {
  231. wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage "
  232. "password");
  233. return -1;
  234. }
  235. pos = (const char *) conf->password;
  236. if (hexstr2bin(pos, k, 16))
  237. return -1;
  238. pos += 32;
  239. if (*pos != ':')
  240. return -1;
  241. pos++;
  242. if (hexstr2bin(pos, opc, 16))
  243. return -1;
  244. for (i = 0; i < data->num_chal; i++) {
  245. if (gsm_milenage(opc, k, data->rand[i],
  246. data->sres[i], data->kc[i])) {
  247. wpa_printf(MSG_DEBUG, "EAP-SIM: "
  248. "GSM-Milenage authentication "
  249. "could not be completed");
  250. return -1;
  251. }
  252. wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
  253. data->rand[i], GSM_RAND_LEN);
  254. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
  255. data->sres[i], EAP_SIM_SRES_LEN);
  256. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
  257. data->kc[i], EAP_SIM_KC_LEN);
  258. }
  259. return 0;
  260. }
  261. #endif /* CONFIG_SIM_SIMULATOR */
  262. #ifdef CONFIG_SIM_HARDCODED
  263. /* These hardcoded Kc and SRES values are used for testing. RAND to
  264. * KC/SREC mapping is very bogus as far as real authentication is
  265. * concerned, but it is quite useful for cases where the AS is rotating
  266. * the order of pre-configured values. */
  267. {
  268. size_t i;
  269. wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
  270. "values for testing");
  271. for (i = 0; i < data->num_chal; i++) {
  272. if (data->rand[i][0] == 0xaa) {
  273. os_memcpy(data->kc[i],
  274. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
  275. EAP_SIM_KC_LEN);
  276. os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
  277. EAP_SIM_SRES_LEN);
  278. } else if (data->rand[i][0] == 0xbb) {
  279. os_memcpy(data->kc[i],
  280. "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
  281. EAP_SIM_KC_LEN);
  282. os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
  283. EAP_SIM_SRES_LEN);
  284. } else {
  285. os_memcpy(data->kc[i],
  286. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
  287. EAP_SIM_KC_LEN);
  288. os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
  289. EAP_SIM_SRES_LEN);
  290. }
  291. }
  292. }
  293. return 0;
  294. #else /* CONFIG_SIM_HARDCODED */
  295. wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
  296. "enabled");
  297. return -1;
  298. #endif /* CONFIG_SIM_HARDCODED */
  299. }
  300. static int eap_sim_supported_ver(int version)
  301. {
  302. return version == EAP_SIM_VERSION;
  303. }
  304. #define CLEAR_PSEUDONYM 0x01
  305. #define CLEAR_REAUTH_ID 0x02
  306. #define CLEAR_EAP_ID 0x04
  307. static void eap_sim_clear_identities(struct eap_sm *sm,
  308. struct eap_sim_data *data, int id)
  309. {
  310. if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
  311. wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
  312. os_free(data->pseudonym);
  313. data->pseudonym = NULL;
  314. data->pseudonym_len = 0;
  315. eap_set_anon_id(sm, NULL, 0);
  316. }
  317. if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
  318. wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
  319. os_free(data->reauth_id);
  320. data->reauth_id = NULL;
  321. data->reauth_id_len = 0;
  322. }
  323. if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
  324. wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
  325. os_free(data->last_eap_identity);
  326. data->last_eap_identity = NULL;
  327. data->last_eap_identity_len = 0;
  328. }
  329. }
  330. static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
  331. struct eap_sim_attrs *attr)
  332. {
  333. if (attr->next_pseudonym) {
  334. const u8 *identity = NULL;
  335. size_t identity_len = 0;
  336. const u8 *realm = NULL;
  337. size_t realm_len = 0;
  338. wpa_hexdump_ascii(MSG_DEBUG,
  339. "EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
  340. attr->next_pseudonym,
  341. attr->next_pseudonym_len);
  342. os_free(data->pseudonym);
  343. /* Look for the realm of the permanent identity */
  344. identity = eap_get_config_identity(sm, &identity_len);
  345. if (identity) {
  346. for (realm = identity, realm_len = identity_len;
  347. realm_len > 0; realm_len--, realm++) {
  348. if (*realm == '@')
  349. break;
  350. }
  351. }
  352. data->pseudonym = os_malloc(attr->next_pseudonym_len +
  353. realm_len);
  354. if (data->pseudonym == NULL) {
  355. wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
  356. "next pseudonym");
  357. data->pseudonym_len = 0;
  358. return -1;
  359. }
  360. os_memcpy(data->pseudonym, attr->next_pseudonym,
  361. attr->next_pseudonym_len);
  362. if (realm_len) {
  363. os_memcpy(data->pseudonym + attr->next_pseudonym_len,
  364. realm, realm_len);
  365. }
  366. data->pseudonym_len = attr->next_pseudonym_len + realm_len;
  367. eap_set_anon_id(sm, data->pseudonym, data->pseudonym_len);
  368. }
  369. if (attr->next_reauth_id) {
  370. os_free(data->reauth_id);
  371. data->reauth_id = os_malloc(attr->next_reauth_id_len);
  372. if (data->reauth_id == NULL) {
  373. wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
  374. "next reauth_id");
  375. data->reauth_id_len = 0;
  376. return -1;
  377. }
  378. os_memcpy(data->reauth_id, attr->next_reauth_id,
  379. attr->next_reauth_id_len);
  380. data->reauth_id_len = attr->next_reauth_id_len;
  381. wpa_hexdump_ascii(MSG_DEBUG,
  382. "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
  383. data->reauth_id,
  384. data->reauth_id_len);
  385. }
  386. return 0;
  387. }
  388. static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
  389. int err)
  390. {
  391. struct eap_sim_msg *msg;
  392. eap_sim_state(data, FAILURE);
  393. data->num_id_req = 0;
  394. data->num_notification = 0;
  395. wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
  396. err);
  397. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
  398. EAP_SIM_SUBTYPE_CLIENT_ERROR);
  399. eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
  400. return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
  401. }
  402. static struct wpabuf * eap_sim_response_start(struct eap_sm *sm,
  403. struct eap_sim_data *data, u8 id,
  404. enum eap_sim_id_req id_req)
  405. {
  406. const u8 *identity = NULL;
  407. size_t identity_len = 0;
  408. struct eap_sim_msg *msg;
  409. data->reauth = 0;
  410. if (id_req == ANY_ID && data->reauth_id) {
  411. identity = data->reauth_id;
  412. identity_len = data->reauth_id_len;
  413. data->reauth = 1;
  414. } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
  415. data->pseudonym) {
  416. identity = data->pseudonym;
  417. identity_len = data->pseudonym_len;
  418. eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
  419. } else if (id_req != NO_ID_REQ) {
  420. identity = eap_get_config_identity(sm, &identity_len);
  421. if (identity) {
  422. eap_sim_clear_identities(sm, data, CLEAR_PSEUDONYM |
  423. CLEAR_REAUTH_ID);
  424. }
  425. }
  426. if (id_req != NO_ID_REQ)
  427. eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
  428. wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
  429. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
  430. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
  431. if (!data->reauth) {
  432. wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
  433. data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
  434. eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
  435. data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
  436. wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
  437. data->selected_version);
  438. eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
  439. data->selected_version, NULL, 0);
  440. }
  441. if (identity) {
  442. wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
  443. identity, identity_len);
  444. eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
  445. identity, identity_len);
  446. }
  447. return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
  448. }
  449. static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
  450. u8 id)
  451. {
  452. struct eap_sim_msg *msg;
  453. wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
  454. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
  455. EAP_SIM_SUBTYPE_CHALLENGE);
  456. if (data->use_result_ind) {
  457. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  458. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  459. }
  460. wpa_printf(MSG_DEBUG, " AT_MAC");
  461. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  462. return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
  463. (u8 *) data->sres,
  464. data->num_chal * EAP_SIM_SRES_LEN);
  465. }
  466. static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
  467. u8 id, int counter_too_small,
  468. const u8 *nonce_s)
  469. {
  470. struct eap_sim_msg *msg;
  471. unsigned int counter;
  472. wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
  473. id);
  474. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
  475. EAP_SIM_SUBTYPE_REAUTHENTICATION);
  476. wpa_printf(MSG_DEBUG, " AT_IV");
  477. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  478. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
  479. if (counter_too_small) {
  480. wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
  481. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
  482. counter = data->counter_too_small;
  483. } else
  484. counter = data->counter;
  485. wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
  486. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
  487. if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
  488. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
  489. "AT_ENCR_DATA");
  490. eap_sim_msg_free(msg);
  491. return NULL;
  492. }
  493. if (data->use_result_ind) {
  494. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  495. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  496. }
  497. wpa_printf(MSG_DEBUG, " AT_MAC");
  498. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  499. return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
  500. EAP_SIM_NONCE_S_LEN);
  501. }
  502. static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
  503. u8 id, u16 notification)
  504. {
  505. struct eap_sim_msg *msg;
  506. u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
  507. wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
  508. msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
  509. EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
  510. if (k_aut && data->reauth) {
  511. wpa_printf(MSG_DEBUG, " AT_IV");
  512. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  513. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
  514. EAP_SIM_AT_ENCR_DATA);
  515. wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
  516. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
  517. NULL, 0);
  518. if (eap_sim_msg_add_encr_end(msg, data->k_encr,
  519. EAP_SIM_AT_PADDING)) {
  520. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
  521. "AT_ENCR_DATA");
  522. eap_sim_msg_free(msg);
  523. return NULL;
  524. }
  525. }
  526. if (k_aut) {
  527. wpa_printf(MSG_DEBUG, " AT_MAC");
  528. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  529. }
  530. return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
  531. }
  532. static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
  533. struct eap_sim_data *data, u8 id,
  534. struct eap_sim_attrs *attr)
  535. {
  536. int selected_version = -1, id_error;
  537. size_t i;
  538. u8 *pos;
  539. wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
  540. if (attr->version_list == NULL) {
  541. wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
  542. "SIM/Start");
  543. return eap_sim_client_error(data, id,
  544. EAP_SIM_UNSUPPORTED_VERSION);
  545. }
  546. os_free(data->ver_list);
  547. data->ver_list = os_malloc(attr->version_list_len);
  548. if (data->ver_list == NULL) {
  549. wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
  550. "memory for version list");
  551. return eap_sim_client_error(data, id,
  552. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  553. }
  554. os_memcpy(data->ver_list, attr->version_list, attr->version_list_len);
  555. data->ver_list_len = attr->version_list_len;
  556. pos = data->ver_list;
  557. for (i = 0; i < data->ver_list_len / 2; i++) {
  558. int ver = pos[0] * 256 + pos[1];
  559. pos += 2;
  560. if (eap_sim_supported_ver(ver)) {
  561. selected_version = ver;
  562. break;
  563. }
  564. }
  565. if (selected_version < 0) {
  566. wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
  567. "version");
  568. return eap_sim_client_error(data, id,
  569. EAP_SIM_UNSUPPORTED_VERSION);
  570. }
  571. wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
  572. selected_version);
  573. data->selected_version = selected_version;
  574. id_error = 0;
  575. switch (attr->id_req) {
  576. case NO_ID_REQ:
  577. break;
  578. case ANY_ID:
  579. if (data->num_id_req > 0)
  580. id_error++;
  581. data->num_id_req++;
  582. break;
  583. case FULLAUTH_ID:
  584. if (data->num_id_req > 1)
  585. id_error++;
  586. data->num_id_req++;
  587. break;
  588. case PERMANENT_ID:
  589. if (data->num_id_req > 2)
  590. id_error++;
  591. data->num_id_req++;
  592. break;
  593. }
  594. if (id_error) {
  595. wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
  596. "used within one authentication");
  597. return eap_sim_client_error(data, id,
  598. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  599. }
  600. return eap_sim_response_start(sm, data, id, attr->id_req);
  601. }
  602. static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
  603. struct eap_sim_data *data,
  604. u8 id,
  605. const struct wpabuf *reqData,
  606. struct eap_sim_attrs *attr)
  607. {
  608. const u8 *identity;
  609. size_t identity_len;
  610. struct eap_sim_attrs eattr;
  611. int res;
  612. wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
  613. data->reauth = 0;
  614. if (!attr->mac || !attr->rand) {
  615. wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
  616. "did not include%s%s",
  617. !attr->mac ? " AT_MAC" : "",
  618. !attr->rand ? " AT_RAND" : "");
  619. return eap_sim_client_error(data, id,
  620. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  621. }
  622. wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
  623. (unsigned long) attr->num_chal);
  624. if (attr->num_chal < data->min_num_chal) {
  625. wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
  626. "challenges (%lu)", (unsigned long) attr->num_chal);
  627. return eap_sim_client_error(data, id,
  628. EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
  629. }
  630. if (attr->num_chal > 3) {
  631. wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
  632. "(%lu)", (unsigned long) attr->num_chal);
  633. return eap_sim_client_error(data, id,
  634. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  635. }
  636. /* Verify that RANDs are different */
  637. if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
  638. GSM_RAND_LEN) == 0 ||
  639. (attr->num_chal > 2 &&
  640. (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
  641. GSM_RAND_LEN) == 0 ||
  642. os_memcmp(attr->rand + GSM_RAND_LEN,
  643. attr->rand + 2 * GSM_RAND_LEN,
  644. GSM_RAND_LEN) == 0))) {
  645. wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
  646. return eap_sim_client_error(data, id,
  647. EAP_SIM_RAND_NOT_FRESH);
  648. }
  649. os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
  650. data->num_chal = attr->num_chal;
  651. res = eap_sim_gsm_auth(sm, data);
  652. if (res > 0) {
  653. wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
  654. return NULL;
  655. }
  656. if (res) {
  657. wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
  658. return eap_sim_client_error(data, id,
  659. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  660. }
  661. if (data->last_eap_identity) {
  662. identity = data->last_eap_identity;
  663. identity_len = data->last_eap_identity_len;
  664. } else if (data->pseudonym) {
  665. identity = data->pseudonym;
  666. identity_len = data->pseudonym_len;
  667. } else
  668. identity = eap_get_config_identity(sm, &identity_len);
  669. wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
  670. "derivation", identity, identity_len);
  671. eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
  672. data->selected_version, data->ver_list,
  673. data->ver_list_len, data->num_chal,
  674. (const u8 *) data->kc, data->mk);
  675. eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
  676. data->emsk);
  677. if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
  678. EAP_SIM_NONCE_MT_LEN)) {
  679. wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
  680. "used invalid AT_MAC");
  681. return eap_sim_client_error(data, id,
  682. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  683. }
  684. /* Old reauthentication identity must not be used anymore. In
  685. * other words, if no new reauth identity is received, full
  686. * authentication will be used on next reauthentication (using
  687. * pseudonym identity or permanent identity). */
  688. eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
  689. if (attr->encr_data) {
  690. u8 *decrypted;
  691. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  692. attr->encr_data_len, attr->iv,
  693. &eattr, 0);
  694. if (decrypted == NULL) {
  695. return eap_sim_client_error(
  696. data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  697. }
  698. eap_sim_learn_ids(sm, data, &eattr);
  699. os_free(decrypted);
  700. }
  701. if (data->result_ind && attr->result_ind)
  702. data->use_result_ind = 1;
  703. if (data->state != FAILURE) {
  704. eap_sim_state(data, data->use_result_ind ?
  705. RESULT_SUCCESS : SUCCESS);
  706. }
  707. data->num_id_req = 0;
  708. data->num_notification = 0;
  709. /* RFC 4186 specifies that counter is initialized to one after
  710. * fullauth, but initializing it to zero makes it easier to implement
  711. * reauth verification. */
  712. data->counter = 0;
  713. return eap_sim_response_challenge(data, id);
  714. }
  715. static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
  716. struct eap_sim_attrs *attr)
  717. {
  718. struct eap_sim_attrs eattr;
  719. u8 *decrypted;
  720. if (attr->encr_data == NULL || attr->iv == NULL) {
  721. wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
  722. "reauth did not include encrypted data");
  723. return -1;
  724. }
  725. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  726. attr->encr_data_len, attr->iv, &eattr,
  727. 0);
  728. if (decrypted == NULL) {
  729. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
  730. "data from notification message");
  731. return -1;
  732. }
  733. if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
  734. wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
  735. "message does not match with counter in reauth "
  736. "message");
  737. os_free(decrypted);
  738. return -1;
  739. }
  740. os_free(decrypted);
  741. return 0;
  742. }
  743. static int eap_sim_process_notification_auth(struct eap_sim_data *data,
  744. const struct wpabuf *reqData,
  745. struct eap_sim_attrs *attr)
  746. {
  747. if (attr->mac == NULL) {
  748. wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
  749. "Notification message");
  750. return -1;
  751. }
  752. if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
  753. {
  754. wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
  755. "used invalid AT_MAC");
  756. return -1;
  757. }
  758. if (data->reauth &&
  759. eap_sim_process_notification_reauth(data, attr)) {
  760. wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
  761. "message after reauth");
  762. return -1;
  763. }
  764. return 0;
  765. }
  766. static struct wpabuf * eap_sim_process_notification(
  767. struct eap_sm *sm, struct eap_sim_data *data, u8 id,
  768. const struct wpabuf *reqData, struct eap_sim_attrs *attr)
  769. {
  770. wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
  771. if (data->num_notification > 0) {
  772. wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
  773. "rounds (only one allowed)");
  774. return eap_sim_client_error(data, id,
  775. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  776. }
  777. data->num_notification++;
  778. if (attr->notification == -1) {
  779. wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
  780. "Notification message");
  781. return eap_sim_client_error(data, id,
  782. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  783. }
  784. if ((attr->notification & 0x4000) == 0 &&
  785. eap_sim_process_notification_auth(data, reqData, attr)) {
  786. return eap_sim_client_error(data, id,
  787. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  788. }
  789. eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
  790. if (attr->notification >= 0 && attr->notification < 32768) {
  791. eap_sim_state(data, FAILURE);
  792. } else if (attr->notification == EAP_SIM_SUCCESS &&
  793. data->state == RESULT_SUCCESS)
  794. eap_sim_state(data, SUCCESS);
  795. return eap_sim_response_notification(data, id, attr->notification);
  796. }
  797. static struct wpabuf * eap_sim_process_reauthentication(
  798. struct eap_sm *sm, struct eap_sim_data *data, u8 id,
  799. const struct wpabuf *reqData, struct eap_sim_attrs *attr)
  800. {
  801. struct eap_sim_attrs eattr;
  802. u8 *decrypted;
  803. wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
  804. if (data->reauth_id == NULL) {
  805. wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
  806. "reauthentication, but no reauth_id available");
  807. return eap_sim_client_error(data, id,
  808. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  809. }
  810. data->reauth = 1;
  811. if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
  812. {
  813. wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
  814. "did not have valid AT_MAC");
  815. return eap_sim_client_error(data, id,
  816. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  817. }
  818. if (attr->encr_data == NULL || attr->iv == NULL) {
  819. wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
  820. "message did not include encrypted data");
  821. return eap_sim_client_error(data, id,
  822. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  823. }
  824. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  825. attr->encr_data_len, attr->iv, &eattr,
  826. 0);
  827. if (decrypted == NULL) {
  828. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
  829. "data from reauthentication message");
  830. return eap_sim_client_error(data, id,
  831. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  832. }
  833. if (eattr.nonce_s == NULL || eattr.counter < 0) {
  834. wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
  835. !eattr.nonce_s ? " AT_NONCE_S" : "",
  836. eattr.counter < 0 ? " AT_COUNTER" : "");
  837. os_free(decrypted);
  838. return eap_sim_client_error(data, id,
  839. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  840. }
  841. if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
  842. struct wpabuf *res;
  843. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
  844. "(%d <= %d)", eattr.counter, data->counter);
  845. data->counter_too_small = eattr.counter;
  846. /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
  847. * reauth_id must not be used to start a new reauthentication.
  848. * However, since it was used in the last EAP-Response-Identity
  849. * packet, it has to saved for the following fullauth to be
  850. * used in MK derivation. */
  851. os_free(data->last_eap_identity);
  852. data->last_eap_identity = data->reauth_id;
  853. data->last_eap_identity_len = data->reauth_id_len;
  854. data->reauth_id = NULL;
  855. data->reauth_id_len = 0;
  856. res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
  857. os_free(decrypted);
  858. return res;
  859. }
  860. data->counter = eattr.counter;
  861. os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
  862. wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
  863. data->nonce_s, EAP_SIM_NONCE_S_LEN);
  864. eap_sim_derive_keys_reauth(data->counter,
  865. data->reauth_id, data->reauth_id_len,
  866. data->nonce_s, data->mk, data->msk,
  867. data->emsk);
  868. eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
  869. eap_sim_learn_ids(sm, data, &eattr);
  870. if (data->result_ind && attr->result_ind)
  871. data->use_result_ind = 1;
  872. if (data->state != FAILURE) {
  873. eap_sim_state(data, data->use_result_ind ?
  874. RESULT_SUCCESS : SUCCESS);
  875. }
  876. data->num_id_req = 0;
  877. data->num_notification = 0;
  878. if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
  879. wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
  880. "fast reauths performed - force fullauth");
  881. eap_sim_clear_identities(sm, data,
  882. CLEAR_REAUTH_ID | CLEAR_EAP_ID);
  883. }
  884. os_free(decrypted);
  885. return eap_sim_response_reauth(data, id, 0, data->nonce_s);
  886. }
  887. static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
  888. struct eap_method_ret *ret,
  889. const struct wpabuf *reqData)
  890. {
  891. struct eap_sim_data *data = priv;
  892. const struct eap_hdr *req;
  893. u8 subtype, id;
  894. struct wpabuf *res;
  895. const u8 *pos;
  896. struct eap_sim_attrs attr;
  897. size_t len;
  898. wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
  899. if (eap_get_config_identity(sm, &len) == NULL) {
  900. wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
  901. eap_sm_request_identity(sm);
  902. ret->ignore = TRUE;
  903. return NULL;
  904. }
  905. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
  906. if (pos == NULL || len < 3) {
  907. ret->ignore = TRUE;
  908. return NULL;
  909. }
  910. req = wpabuf_head(reqData);
  911. id = req->identifier;
  912. len = be_to_host16(req->length);
  913. ret->ignore = FALSE;
  914. ret->methodState = METHOD_MAY_CONT;
  915. ret->decision = DECISION_FAIL;
  916. ret->allowNotifications = TRUE;
  917. subtype = *pos++;
  918. wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
  919. pos += 2; /* Reserved */
  920. if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
  921. 0)) {
  922. res = eap_sim_client_error(data, id,
  923. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  924. goto done;
  925. }
  926. switch (subtype) {
  927. case EAP_SIM_SUBTYPE_START:
  928. res = eap_sim_process_start(sm, data, id, &attr);
  929. break;
  930. case EAP_SIM_SUBTYPE_CHALLENGE:
  931. res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
  932. break;
  933. case EAP_SIM_SUBTYPE_NOTIFICATION:
  934. res = eap_sim_process_notification(sm, data, id, reqData,
  935. &attr);
  936. break;
  937. case EAP_SIM_SUBTYPE_REAUTHENTICATION:
  938. res = eap_sim_process_reauthentication(sm, data, id, reqData,
  939. &attr);
  940. break;
  941. case EAP_SIM_SUBTYPE_CLIENT_ERROR:
  942. wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
  943. res = eap_sim_client_error(data, id,
  944. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  945. break;
  946. default:
  947. wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
  948. res = eap_sim_client_error(data, id,
  949. EAP_SIM_UNABLE_TO_PROCESS_PACKET);
  950. break;
  951. }
  952. done:
  953. if (data->state == FAILURE) {
  954. ret->decision = DECISION_FAIL;
  955. ret->methodState = METHOD_DONE;
  956. } else if (data->state == SUCCESS) {
  957. ret->decision = data->use_result_ind ?
  958. DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
  959. ret->methodState = data->use_result_ind ?
  960. METHOD_DONE : METHOD_MAY_CONT;
  961. } else if (data->state == RESULT_SUCCESS)
  962. ret->methodState = METHOD_CONT;
  963. if (ret->methodState == METHOD_DONE) {
  964. ret->allowNotifications = FALSE;
  965. }
  966. return res;
  967. }
  968. static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
  969. {
  970. struct eap_sim_data *data = priv;
  971. return data->pseudonym || data->reauth_id;
  972. }
  973. static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
  974. {
  975. struct eap_sim_data *data = priv;
  976. eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
  977. data->use_result_ind = 0;
  978. eap_sim_clear_keys(data, 1);
  979. }
  980. static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
  981. {
  982. struct eap_sim_data *data = priv;
  983. if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
  984. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
  985. "for NONCE_MT");
  986. os_free(data);
  987. return NULL;
  988. }
  989. data->num_id_req = 0;
  990. data->num_notification = 0;
  991. eap_sim_state(data, CONTINUE);
  992. return priv;
  993. }
  994. static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
  995. size_t *len)
  996. {
  997. struct eap_sim_data *data = priv;
  998. if (data->reauth_id) {
  999. *len = data->reauth_id_len;
  1000. return data->reauth_id;
  1001. }
  1002. if (data->pseudonym) {
  1003. *len = data->pseudonym_len;
  1004. return data->pseudonym;
  1005. }
  1006. return NULL;
  1007. }
  1008. static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
  1009. {
  1010. struct eap_sim_data *data = priv;
  1011. return data->state == SUCCESS;
  1012. }
  1013. static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1014. {
  1015. struct eap_sim_data *data = priv;
  1016. u8 *key;
  1017. if (data->state != SUCCESS)
  1018. return NULL;
  1019. key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
  1020. if (key == NULL)
  1021. return NULL;
  1022. *len = EAP_SIM_KEYING_DATA_LEN;
  1023. os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
  1024. return key;
  1025. }
  1026. static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  1027. {
  1028. struct eap_sim_data *data = priv;
  1029. u8 *id;
  1030. if (data->state != SUCCESS)
  1031. return NULL;
  1032. *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
  1033. id = os_malloc(*len);
  1034. if (id == NULL)
  1035. return NULL;
  1036. id[0] = EAP_TYPE_SIM;
  1037. os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
  1038. os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN, data->nonce_mt,
  1039. EAP_SIM_NONCE_MT_LEN);
  1040. wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
  1041. return id;
  1042. }
  1043. static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1044. {
  1045. struct eap_sim_data *data = priv;
  1046. u8 *key;
  1047. if (data->state != SUCCESS)
  1048. return NULL;
  1049. key = os_malloc(EAP_EMSK_LEN);
  1050. if (key == NULL)
  1051. return NULL;
  1052. *len = EAP_EMSK_LEN;
  1053. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  1054. return key;
  1055. }
  1056. int eap_peer_sim_register(void)
  1057. {
  1058. struct eap_method *eap;
  1059. int ret;
  1060. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1061. EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
  1062. if (eap == NULL)
  1063. return -1;
  1064. eap->init = eap_sim_init;
  1065. eap->deinit = eap_sim_deinit;
  1066. eap->process = eap_sim_process;
  1067. eap->isKeyAvailable = eap_sim_isKeyAvailable;
  1068. eap->getKey = eap_sim_getKey;
  1069. eap->getSessionId = eap_sim_get_session_id;
  1070. eap->has_reauth_data = eap_sim_has_reauth_data;
  1071. eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
  1072. eap->init_for_reauth = eap_sim_init_for_reauth;
  1073. eap->get_identity = eap_sim_get_identity;
  1074. eap->get_emsk = eap_sim_get_emsk;
  1075. ret = eap_peer_method_register(eap);
  1076. if (ret)
  1077. eap_peer_method_free(eap);
  1078. return ret;
  1079. }