eap_server_sim.c 20 KB

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