eap_server_sim.c 22 KB

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