eap_sim.c 30 KB

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