eap_sim.c 34 KB

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