eap_server_sim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * hostapd / EAP-SIM (RFC 4186)
  3. * Copyright (c) 2005-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 "crypto/random.h"
  17. #include "eap_server/eap_i.h"
  18. #include "eap_common/eap_sim_common.h"
  19. #include "eap_server/eap_sim_db.h"
  20. struct eap_sim_data {
  21. u8 mk[EAP_SIM_MK_LEN];
  22. u8 nonce_mt[EAP_SIM_NONCE_MT_LEN];
  23. u8 nonce_s[EAP_SIM_NONCE_S_LEN];
  24. u8 k_aut[EAP_SIM_K_AUT_LEN];
  25. u8 k_encr[EAP_SIM_K_ENCR_LEN];
  26. u8 msk[EAP_SIM_KEYING_DATA_LEN];
  27. u8 emsk[EAP_EMSK_LEN];
  28. u8 kc[EAP_SIM_MAX_CHAL][EAP_SIM_KC_LEN];
  29. u8 sres[EAP_SIM_MAX_CHAL][EAP_SIM_SRES_LEN];
  30. u8 rand[EAP_SIM_MAX_CHAL][GSM_RAND_LEN];
  31. int num_chal;
  32. enum {
  33. START, CHALLENGE, REAUTH, NOTIFICATION, SUCCESS, FAILURE
  34. } state;
  35. char *next_pseudonym;
  36. char *next_reauth_id;
  37. u16 counter;
  38. struct eap_sim_reauth *reauth;
  39. u16 notification;
  40. int use_result_ind;
  41. };
  42. static const char * eap_sim_state_txt(int state)
  43. {
  44. switch (state) {
  45. case START:
  46. return "START";
  47. case CHALLENGE:
  48. return "CHALLENGE";
  49. case REAUTH:
  50. return "REAUTH";
  51. case SUCCESS:
  52. return "SUCCESS";
  53. case FAILURE:
  54. return "FAILURE";
  55. case NOTIFICATION:
  56. return "NOTIFICATION";
  57. default:
  58. return "Unknown?!";
  59. }
  60. }
  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. if (sm->eap_sim_db_priv == NULL) {
  72. wpa_printf(MSG_WARNING, "EAP-SIM: eap_sim_db not configured");
  73. return NULL;
  74. }
  75. data = os_zalloc(sizeof(*data));
  76. if (data == NULL)
  77. return NULL;
  78. data->state = START;
  79. return data;
  80. }
  81. static void eap_sim_reset(struct eap_sm *sm, void *priv)
  82. {
  83. struct eap_sim_data *data = priv;
  84. os_free(data->next_pseudonym);
  85. os_free(data->next_reauth_id);
  86. os_free(data);
  87. }
  88. static struct wpabuf * eap_sim_build_start(struct eap_sm *sm,
  89. struct eap_sim_data *data, u8 id)
  90. {
  91. struct eap_sim_msg *msg;
  92. u8 ver[2];
  93. wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Start");
  94. msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
  95. EAP_SIM_SUBTYPE_START);
  96. if (eap_sim_db_identity_known(sm->eap_sim_db_priv, sm->identity,
  97. sm->identity_len)) {
  98. wpa_printf(MSG_DEBUG, " AT_PERMANENT_ID_REQ");
  99. eap_sim_msg_add(msg, EAP_SIM_AT_PERMANENT_ID_REQ, 0, NULL, 0);
  100. } else {
  101. /*
  102. * RFC 4186, Chap. 4.2.4 recommends that identity from EAP is
  103. * ignored and the SIM/Start is used to request the identity.
  104. */
  105. wpa_printf(MSG_DEBUG, " AT_ANY_ID_REQ");
  106. eap_sim_msg_add(msg, EAP_SIM_AT_ANY_ID_REQ, 0, NULL, 0);
  107. }
  108. wpa_printf(MSG_DEBUG, " AT_VERSION_LIST");
  109. ver[0] = 0;
  110. ver[1] = EAP_SIM_VERSION;
  111. eap_sim_msg_add(msg, EAP_SIM_AT_VERSION_LIST, sizeof(ver),
  112. ver, sizeof(ver));
  113. return eap_sim_msg_finish(msg, NULL, NULL, 0);
  114. }
  115. static int eap_sim_build_encr(struct eap_sm *sm, struct eap_sim_data *data,
  116. struct eap_sim_msg *msg, u16 counter,
  117. const u8 *nonce_s)
  118. {
  119. os_free(data->next_pseudonym);
  120. data->next_pseudonym =
  121. eap_sim_db_get_next_pseudonym(sm->eap_sim_db_priv, 0);
  122. os_free(data->next_reauth_id);
  123. if (data->counter <= EAP_SIM_MAX_FAST_REAUTHS) {
  124. data->next_reauth_id =
  125. eap_sim_db_get_next_reauth_id(sm->eap_sim_db_priv, 0);
  126. } else {
  127. wpa_printf(MSG_DEBUG, "EAP-SIM: Max fast re-authentication "
  128. "count exceeded - force full authentication");
  129. data->next_reauth_id = NULL;
  130. }
  131. if (data->next_pseudonym == NULL && data->next_reauth_id == NULL &&
  132. counter == 0 && nonce_s == NULL)
  133. return 0;
  134. wpa_printf(MSG_DEBUG, " AT_IV");
  135. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  136. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
  137. if (counter > 0) {
  138. wpa_printf(MSG_DEBUG, " *AT_COUNTER (%u)", counter);
  139. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
  140. }
  141. if (nonce_s) {
  142. wpa_printf(MSG_DEBUG, " *AT_NONCE_S");
  143. eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_S, 0, nonce_s,
  144. EAP_SIM_NONCE_S_LEN);
  145. }
  146. if (data->next_pseudonym) {
  147. wpa_printf(MSG_DEBUG, " *AT_NEXT_PSEUDONYM (%s)",
  148. data->next_pseudonym);
  149. eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_PSEUDONYM,
  150. os_strlen(data->next_pseudonym),
  151. (u8 *) data->next_pseudonym,
  152. os_strlen(data->next_pseudonym));
  153. }
  154. if (data->next_reauth_id) {
  155. wpa_printf(MSG_DEBUG, " *AT_NEXT_REAUTH_ID (%s)",
  156. data->next_reauth_id);
  157. eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_REAUTH_ID,
  158. os_strlen(data->next_reauth_id),
  159. (u8 *) data->next_reauth_id,
  160. os_strlen(data->next_reauth_id));
  161. }
  162. if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
  163. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
  164. "AT_ENCR_DATA");
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. static struct wpabuf * eap_sim_build_challenge(struct eap_sm *sm,
  170. struct eap_sim_data *data,
  171. u8 id)
  172. {
  173. struct eap_sim_msg *msg;
  174. wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Challenge");
  175. msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
  176. EAP_SIM_SUBTYPE_CHALLENGE);
  177. wpa_printf(MSG_DEBUG, " AT_RAND");
  178. eap_sim_msg_add(msg, EAP_SIM_AT_RAND, 0, (u8 *) data->rand,
  179. data->num_chal * GSM_RAND_LEN);
  180. if (eap_sim_build_encr(sm, data, msg, 0, NULL)) {
  181. eap_sim_msg_free(msg);
  182. return NULL;
  183. }
  184. if (sm->eap_sim_aka_result_ind) {
  185. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  186. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  187. }
  188. wpa_printf(MSG_DEBUG, " AT_MAC");
  189. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  190. return eap_sim_msg_finish(msg, data->k_aut, data->nonce_mt,
  191. EAP_SIM_NONCE_MT_LEN);
  192. }
  193. static struct wpabuf * eap_sim_build_reauth(struct eap_sm *sm,
  194. struct eap_sim_data *data, u8 id)
  195. {
  196. struct eap_sim_msg *msg;
  197. wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Re-authentication");
  198. if (random_get_bytes(data->nonce_s, EAP_SIM_NONCE_S_LEN))
  199. return NULL;
  200. wpa_hexdump_key(MSG_MSGDUMP, "EAP-SIM: NONCE_S",
  201. data->nonce_s, EAP_SIM_NONCE_S_LEN);
  202. eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
  203. data->emsk);
  204. eap_sim_derive_keys_reauth(data->counter, sm->identity,
  205. sm->identity_len, data->nonce_s, data->mk,
  206. data->msk, data->emsk);
  207. msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
  208. EAP_SIM_SUBTYPE_REAUTHENTICATION);
  209. if (eap_sim_build_encr(sm, data, msg, data->counter, data->nonce_s)) {
  210. eap_sim_msg_free(msg);
  211. return NULL;
  212. }
  213. if (sm->eap_sim_aka_result_ind) {
  214. wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
  215. eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
  216. }
  217. wpa_printf(MSG_DEBUG, " AT_MAC");
  218. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  219. return eap_sim_msg_finish(msg, data->k_aut, NULL, 0);
  220. }
  221. static struct wpabuf * eap_sim_build_notification(struct eap_sm *sm,
  222. struct eap_sim_data *data,
  223. u8 id)
  224. {
  225. struct eap_sim_msg *msg;
  226. wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Notification");
  227. msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
  228. EAP_SIM_SUBTYPE_NOTIFICATION);
  229. wpa_printf(MSG_DEBUG, " AT_NOTIFICATION (%d)", data->notification);
  230. eap_sim_msg_add(msg, EAP_SIM_AT_NOTIFICATION, data->notification,
  231. NULL, 0);
  232. if (data->use_result_ind) {
  233. if (data->reauth) {
  234. wpa_printf(MSG_DEBUG, " AT_IV");
  235. wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
  236. eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
  237. EAP_SIM_AT_ENCR_DATA);
  238. wpa_printf(MSG_DEBUG, " *AT_COUNTER (%u)",
  239. data->counter);
  240. eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
  241. NULL, 0);
  242. if (eap_sim_msg_add_encr_end(msg, data->k_encr,
  243. EAP_SIM_AT_PADDING)) {
  244. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to "
  245. "encrypt AT_ENCR_DATA");
  246. eap_sim_msg_free(msg);
  247. return NULL;
  248. }
  249. }
  250. wpa_printf(MSG_DEBUG, " AT_MAC");
  251. eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
  252. }
  253. return eap_sim_msg_finish(msg, data->k_aut, NULL, 0);
  254. }
  255. static struct wpabuf * eap_sim_buildReq(struct eap_sm *sm, void *priv, u8 id)
  256. {
  257. struct eap_sim_data *data = priv;
  258. switch (data->state) {
  259. case START:
  260. return eap_sim_build_start(sm, data, id);
  261. case CHALLENGE:
  262. return eap_sim_build_challenge(sm, data, id);
  263. case REAUTH:
  264. return eap_sim_build_reauth(sm, data, id);
  265. case NOTIFICATION:
  266. return eap_sim_build_notification(sm, data, id);
  267. default:
  268. wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown state %d in "
  269. "buildReq", data->state);
  270. break;
  271. }
  272. return NULL;
  273. }
  274. static Boolean eap_sim_check(struct eap_sm *sm, void *priv,
  275. struct wpabuf *respData)
  276. {
  277. struct eap_sim_data *data = priv;
  278. const u8 *pos;
  279. size_t len;
  280. u8 subtype;
  281. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, respData, &len);
  282. if (pos == NULL || len < 3) {
  283. wpa_printf(MSG_INFO, "EAP-SIM: Invalid frame");
  284. return TRUE;
  285. }
  286. subtype = *pos;
  287. if (subtype == EAP_SIM_SUBTYPE_CLIENT_ERROR)
  288. return FALSE;
  289. switch (data->state) {
  290. case START:
  291. if (subtype != EAP_SIM_SUBTYPE_START) {
  292. wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
  293. "subtype %d", subtype);
  294. return TRUE;
  295. }
  296. break;
  297. case CHALLENGE:
  298. if (subtype != EAP_SIM_SUBTYPE_CHALLENGE) {
  299. wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
  300. "subtype %d", subtype);
  301. return TRUE;
  302. }
  303. break;
  304. case REAUTH:
  305. if (subtype != EAP_SIM_SUBTYPE_REAUTHENTICATION) {
  306. wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
  307. "subtype %d", subtype);
  308. return TRUE;
  309. }
  310. break;
  311. case NOTIFICATION:
  312. if (subtype != EAP_SIM_SUBTYPE_NOTIFICATION) {
  313. wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
  314. "subtype %d", subtype);
  315. return TRUE;
  316. }
  317. break;
  318. default:
  319. wpa_printf(MSG_INFO, "EAP-SIM: Unexpected state (%d) for "
  320. "processing a response", data->state);
  321. return TRUE;
  322. }
  323. return FALSE;
  324. }
  325. static int eap_sim_supported_ver(struct eap_sim_data *data, int version)
  326. {
  327. return version == EAP_SIM_VERSION;
  328. }
  329. static void eap_sim_process_start(struct eap_sm *sm,
  330. struct eap_sim_data *data,
  331. struct wpabuf *respData,
  332. struct eap_sim_attrs *attr)
  333. {
  334. const u8 *identity;
  335. size_t identity_len;
  336. u8 ver_list[2];
  337. wpa_printf(MSG_DEBUG, "EAP-SIM: Receive start response");
  338. if (attr->identity) {
  339. os_free(sm->identity);
  340. sm->identity = os_malloc(attr->identity_len);
  341. if (sm->identity) {
  342. os_memcpy(sm->identity, attr->identity,
  343. attr->identity_len);
  344. sm->identity_len = attr->identity_len;
  345. }
  346. }
  347. identity = NULL;
  348. identity_len = 0;
  349. if (sm->identity && sm->identity_len > 0 &&
  350. sm->identity[0] == EAP_SIM_PERMANENT_PREFIX) {
  351. identity = sm->identity;
  352. identity_len = sm->identity_len;
  353. } else {
  354. identity = eap_sim_db_get_permanent(sm->eap_sim_db_priv,
  355. sm->identity,
  356. sm->identity_len,
  357. &identity_len);
  358. if (identity == NULL) {
  359. data->reauth = eap_sim_db_get_reauth_entry(
  360. sm->eap_sim_db_priv, sm->identity,
  361. sm->identity_len);
  362. if (data->reauth) {
  363. wpa_printf(MSG_DEBUG, "EAP-SIM: Using fast "
  364. "re-authentication");
  365. identity = data->reauth->identity;
  366. identity_len = data->reauth->identity_len;
  367. data->counter = data->reauth->counter;
  368. os_memcpy(data->mk, data->reauth->mk,
  369. EAP_SIM_MK_LEN);
  370. }
  371. }
  372. }
  373. if (identity == NULL) {
  374. wpa_printf(MSG_DEBUG, "EAP-SIM: Could not get proper permanent"
  375. " user name");
  376. eap_sim_state(data, FAILURE);
  377. return;
  378. }
  379. wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Identity",
  380. identity, identity_len);
  381. if (data->reauth) {
  382. eap_sim_state(data, REAUTH);
  383. return;
  384. }
  385. if (attr->nonce_mt == NULL || attr->selected_version < 0) {
  386. wpa_printf(MSG_DEBUG, "EAP-SIM: Start/Response missing "
  387. "required attributes");
  388. eap_sim_state(data, FAILURE);
  389. return;
  390. }
  391. if (!eap_sim_supported_ver(data, attr->selected_version)) {
  392. wpa_printf(MSG_DEBUG, "EAP-SIM: Peer selected unsupported "
  393. "version %d", attr->selected_version);
  394. eap_sim_state(data, FAILURE);
  395. return;
  396. }
  397. data->counter = 0; /* reset re-auth counter since this is full auth */
  398. data->reauth = NULL;
  399. data->num_chal = eap_sim_db_get_gsm_triplets(
  400. sm->eap_sim_db_priv, identity, identity_len,
  401. EAP_SIM_MAX_CHAL,
  402. (u8 *) data->rand, (u8 *) data->kc, (u8 *) data->sres, sm);
  403. if (data->num_chal == EAP_SIM_DB_PENDING) {
  404. wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication triplets "
  405. "not yet available - pending request");
  406. sm->method_pending = METHOD_PENDING_WAIT;
  407. return;
  408. }
  409. if (data->num_chal < 2) {
  410. wpa_printf(MSG_INFO, "EAP-SIM: Failed to get GSM "
  411. "authentication triplets for the peer");
  412. eap_sim_state(data, FAILURE);
  413. return;
  414. }
  415. identity_len = sm->identity_len;
  416. while (identity_len > 0 && sm->identity[identity_len - 1] == '\0') {
  417. wpa_printf(MSG_DEBUG, "EAP-SIM: Workaround - drop last null "
  418. "character from identity");
  419. identity_len--;
  420. }
  421. wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Identity for MK derivation",
  422. sm->identity, identity_len);
  423. os_memcpy(data->nonce_mt, attr->nonce_mt, EAP_SIM_NONCE_MT_LEN);
  424. WPA_PUT_BE16(ver_list, EAP_SIM_VERSION);
  425. eap_sim_derive_mk(sm->identity, identity_len, attr->nonce_mt,
  426. attr->selected_version, ver_list, sizeof(ver_list),
  427. data->num_chal, (const u8 *) data->kc, data->mk);
  428. eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
  429. data->emsk);
  430. eap_sim_state(data, CHALLENGE);
  431. }
  432. static void eap_sim_process_challenge(struct eap_sm *sm,
  433. struct eap_sim_data *data,
  434. struct wpabuf *respData,
  435. struct eap_sim_attrs *attr)
  436. {
  437. const u8 *identity;
  438. size_t identity_len;
  439. if (attr->mac == NULL ||
  440. eap_sim_verify_mac(data->k_aut, respData, attr->mac,
  441. (u8 *) data->sres,
  442. data->num_chal * EAP_SIM_SRES_LEN)) {
  443. wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
  444. "did not include valid AT_MAC");
  445. eap_sim_state(data, FAILURE);
  446. return;
  447. }
  448. wpa_printf(MSG_DEBUG, "EAP-SIM: Challenge response includes the "
  449. "correct AT_MAC");
  450. if (sm->eap_sim_aka_result_ind && attr->result_ind) {
  451. data->use_result_ind = 1;
  452. data->notification = EAP_SIM_SUCCESS;
  453. eap_sim_state(data, NOTIFICATION);
  454. } else
  455. eap_sim_state(data, SUCCESS);
  456. identity = eap_sim_db_get_permanent(sm->eap_sim_db_priv, sm->identity,
  457. sm->identity_len, &identity_len);
  458. if (identity == NULL) {
  459. identity = sm->identity;
  460. identity_len = sm->identity_len;
  461. }
  462. if (data->next_pseudonym) {
  463. eap_sim_db_add_pseudonym(sm->eap_sim_db_priv, identity,
  464. identity_len,
  465. data->next_pseudonym);
  466. data->next_pseudonym = NULL;
  467. }
  468. if (data->next_reauth_id) {
  469. eap_sim_db_add_reauth(sm->eap_sim_db_priv, identity,
  470. identity_len,
  471. data->next_reauth_id, data->counter + 1,
  472. data->mk);
  473. data->next_reauth_id = NULL;
  474. }
  475. }
  476. static void eap_sim_process_reauth(struct eap_sm *sm,
  477. struct eap_sim_data *data,
  478. struct wpabuf *respData,
  479. struct eap_sim_attrs *attr)
  480. {
  481. struct eap_sim_attrs eattr;
  482. u8 *decrypted = NULL;
  483. const u8 *identity, *id2;
  484. size_t identity_len, id2_len;
  485. if (attr->mac == NULL ||
  486. eap_sim_verify_mac(data->k_aut, respData, attr->mac, data->nonce_s,
  487. EAP_SIM_NONCE_S_LEN)) {
  488. wpa_printf(MSG_WARNING, "EAP-SIM: Re-authentication message "
  489. "did not include valid AT_MAC");
  490. goto fail;
  491. }
  492. if (attr->encr_data == NULL || attr->iv == NULL) {
  493. wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
  494. "message did not include encrypted data");
  495. goto fail;
  496. }
  497. decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
  498. attr->encr_data_len, attr->iv, &eattr,
  499. 0);
  500. if (decrypted == NULL) {
  501. wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
  502. "data from reauthentication message");
  503. goto fail;
  504. }
  505. if (eattr.counter != data->counter) {
  506. wpa_printf(MSG_WARNING, "EAP-SIM: Re-authentication message "
  507. "used incorrect counter %u, expected %u",
  508. eattr.counter, data->counter);
  509. goto fail;
  510. }
  511. os_free(decrypted);
  512. decrypted = NULL;
  513. wpa_printf(MSG_DEBUG, "EAP-SIM: Re-authentication response includes "
  514. "the correct AT_MAC");
  515. if (sm->eap_sim_aka_result_ind && attr->result_ind) {
  516. data->use_result_ind = 1;
  517. data->notification = EAP_SIM_SUCCESS;
  518. eap_sim_state(data, NOTIFICATION);
  519. } else
  520. eap_sim_state(data, SUCCESS);
  521. if (data->reauth) {
  522. identity = data->reauth->identity;
  523. identity_len = data->reauth->identity_len;
  524. } else {
  525. identity = sm->identity;
  526. identity_len = sm->identity_len;
  527. }
  528. id2 = eap_sim_db_get_permanent(sm->eap_sim_db_priv, identity,
  529. identity_len, &id2_len);
  530. if (id2) {
  531. identity = id2;
  532. identity_len = id2_len;
  533. }
  534. if (data->next_pseudonym) {
  535. eap_sim_db_add_pseudonym(sm->eap_sim_db_priv, identity,
  536. identity_len, data->next_pseudonym);
  537. data->next_pseudonym = NULL;
  538. }
  539. if (data->next_reauth_id) {
  540. eap_sim_db_add_reauth(sm->eap_sim_db_priv, identity,
  541. identity_len, data->next_reauth_id,
  542. data->counter + 1, data->mk);
  543. data->next_reauth_id = NULL;
  544. } else {
  545. eap_sim_db_remove_reauth(sm->eap_sim_db_priv, data->reauth);
  546. data->reauth = NULL;
  547. }
  548. return;
  549. fail:
  550. eap_sim_state(data, FAILURE);
  551. eap_sim_db_remove_reauth(sm->eap_sim_db_priv, data->reauth);
  552. data->reauth = NULL;
  553. os_free(decrypted);
  554. }
  555. static void eap_sim_process_client_error(struct eap_sm *sm,
  556. struct eap_sim_data *data,
  557. struct wpabuf *respData,
  558. struct eap_sim_attrs *attr)
  559. {
  560. wpa_printf(MSG_DEBUG, "EAP-SIM: Client reported error %d",
  561. attr->client_error_code);
  562. if (data->notification == EAP_SIM_SUCCESS && data->use_result_ind)
  563. eap_sim_state(data, SUCCESS);
  564. else
  565. eap_sim_state(data, FAILURE);
  566. }
  567. static void eap_sim_process_notification(struct eap_sm *sm,
  568. struct eap_sim_data *data,
  569. struct wpabuf *respData,
  570. struct eap_sim_attrs *attr)
  571. {
  572. wpa_printf(MSG_DEBUG, "EAP-SIM: Client replied to notification");
  573. if (data->notification == EAP_SIM_SUCCESS && data->use_result_ind)
  574. eap_sim_state(data, SUCCESS);
  575. else
  576. eap_sim_state(data, FAILURE);
  577. }
  578. static void eap_sim_process(struct eap_sm *sm, void *priv,
  579. struct wpabuf *respData)
  580. {
  581. struct eap_sim_data *data = priv;
  582. const u8 *pos, *end;
  583. u8 subtype;
  584. size_t len;
  585. struct eap_sim_attrs attr;
  586. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, respData, &len);
  587. if (pos == NULL || len < 3)
  588. return;
  589. end = pos + len;
  590. subtype = *pos;
  591. pos += 3;
  592. if (eap_sim_parse_attr(pos, end, &attr, 0, 0)) {
  593. wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to parse attributes");
  594. eap_sim_state(data, FAILURE);
  595. return;
  596. }
  597. if (subtype == EAP_SIM_SUBTYPE_CLIENT_ERROR) {
  598. eap_sim_process_client_error(sm, data, respData, &attr);
  599. return;
  600. }
  601. switch (data->state) {
  602. case START:
  603. eap_sim_process_start(sm, data, respData, &attr);
  604. break;
  605. case CHALLENGE:
  606. eap_sim_process_challenge(sm, data, respData, &attr);
  607. break;
  608. case REAUTH:
  609. eap_sim_process_reauth(sm, data, respData, &attr);
  610. break;
  611. case NOTIFICATION:
  612. eap_sim_process_notification(sm, data, respData, &attr);
  613. break;
  614. default:
  615. wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown state %d in "
  616. "process", data->state);
  617. break;
  618. }
  619. }
  620. static Boolean eap_sim_isDone(struct eap_sm *sm, void *priv)
  621. {
  622. struct eap_sim_data *data = priv;
  623. return data->state == SUCCESS || data->state == FAILURE;
  624. }
  625. static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
  626. {
  627. struct eap_sim_data *data = priv;
  628. u8 *key;
  629. if (data->state != SUCCESS)
  630. return NULL;
  631. key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
  632. if (key == NULL)
  633. return NULL;
  634. os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
  635. *len = EAP_SIM_KEYING_DATA_LEN;
  636. return key;
  637. }
  638. static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  639. {
  640. struct eap_sim_data *data = priv;
  641. u8 *key;
  642. if (data->state != SUCCESS)
  643. return NULL;
  644. key = os_malloc(EAP_EMSK_LEN);
  645. if (key == NULL)
  646. return NULL;
  647. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  648. *len = EAP_EMSK_LEN;
  649. return key;
  650. }
  651. static Boolean eap_sim_isSuccess(struct eap_sm *sm, void *priv)
  652. {
  653. struct eap_sim_data *data = priv;
  654. return data->state == SUCCESS;
  655. }
  656. int eap_server_sim_register(void)
  657. {
  658. struct eap_method *eap;
  659. int ret;
  660. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  661. EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
  662. if (eap == NULL)
  663. return -1;
  664. eap->init = eap_sim_init;
  665. eap->reset = eap_sim_reset;
  666. eap->buildReq = eap_sim_buildReq;
  667. eap->check = eap_sim_check;
  668. eap->process = eap_sim_process;
  669. eap->isDone = eap_sim_isDone;
  670. eap->getKey = eap_sim_getKey;
  671. eap->isSuccess = eap_sim_isSuccess;
  672. eap->get_emsk = eap_sim_get_emsk;
  673. ret = eap_server_method_register(eap);
  674. if (ret)
  675. eap_server_method_free(eap);
  676. return ret;
  677. }