eap_sim_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * EAP peer/server: EAP-SIM/AKA shared routines
  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_common/eap_defs.h"
  17. #include "sha1.h"
  18. #include "crypto.h"
  19. #include "aes_wrap.h"
  20. #include "wpabuf.h"
  21. #include "eap_common/eap_sim_common.h"
  22. static int eap_sim_prf(const u8 *key, u8 *x, size_t xlen)
  23. {
  24. return fips186_2_prf(key, EAP_SIM_MK_LEN, x, xlen);
  25. }
  26. void eap_sim_derive_mk(const u8 *identity, size_t identity_len,
  27. const u8 *nonce_mt, u16 selected_version,
  28. const u8 *ver_list, size_t ver_list_len,
  29. int num_chal, const u8 *kc, u8 *mk)
  30. {
  31. u8 sel_ver[2];
  32. const unsigned char *addr[5];
  33. size_t len[5];
  34. addr[0] = identity;
  35. len[0] = identity_len;
  36. addr[1] = kc;
  37. len[1] = num_chal * EAP_SIM_KC_LEN;
  38. addr[2] = nonce_mt;
  39. len[2] = EAP_SIM_NONCE_MT_LEN;
  40. addr[3] = ver_list;
  41. len[3] = ver_list_len;
  42. addr[4] = sel_ver;
  43. len[4] = 2;
  44. WPA_PUT_BE16(sel_ver, selected_version);
  45. /* MK = SHA1(Identity|n*Kc|NONCE_MT|Version List|Selected Version) */
  46. sha1_vector(5, addr, len, mk);
  47. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: MK", mk, EAP_SIM_MK_LEN);
  48. }
  49. void eap_aka_derive_mk(const u8 *identity, size_t identity_len,
  50. const u8 *ik, const u8 *ck, u8 *mk)
  51. {
  52. const u8 *addr[3];
  53. size_t len[3];
  54. addr[0] = identity;
  55. len[0] = identity_len;
  56. addr[1] = ik;
  57. len[1] = EAP_AKA_IK_LEN;
  58. addr[2] = ck;
  59. len[2] = EAP_AKA_CK_LEN;
  60. /* MK = SHA1(Identity|IK|CK) */
  61. sha1_vector(3, addr, len, mk);
  62. wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: IK", ik, EAP_AKA_IK_LEN);
  63. wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: CK", ck, EAP_AKA_CK_LEN);
  64. wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: MK", mk, EAP_SIM_MK_LEN);
  65. }
  66. int eap_sim_derive_keys(const u8 *mk, u8 *k_encr, u8 *k_aut, u8 *msk, u8 *emsk)
  67. {
  68. u8 buf[EAP_SIM_K_ENCR_LEN + EAP_SIM_K_AUT_LEN +
  69. EAP_SIM_KEYING_DATA_LEN + EAP_EMSK_LEN], *pos;
  70. if (eap_sim_prf(mk, buf, sizeof(buf)) < 0) {
  71. wpa_printf(MSG_ERROR, "EAP-SIM: Failed to derive keys");
  72. return -1;
  73. }
  74. pos = buf;
  75. os_memcpy(k_encr, pos, EAP_SIM_K_ENCR_LEN);
  76. pos += EAP_SIM_K_ENCR_LEN;
  77. os_memcpy(k_aut, pos, EAP_SIM_K_AUT_LEN);
  78. pos += EAP_SIM_K_AUT_LEN;
  79. os_memcpy(msk, pos, EAP_SIM_KEYING_DATA_LEN);
  80. pos += EAP_SIM_KEYING_DATA_LEN;
  81. os_memcpy(emsk, pos, EAP_EMSK_LEN);
  82. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: K_encr",
  83. k_encr, EAP_SIM_K_ENCR_LEN);
  84. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: K_aut",
  85. k_aut, EAP_SIM_K_AUT_LEN);
  86. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: keying material (MSK)",
  87. msk, EAP_SIM_KEYING_DATA_LEN);
  88. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: EMSK", emsk, EAP_EMSK_LEN);
  89. os_memset(buf, 0, sizeof(buf));
  90. return 0;
  91. }
  92. int eap_sim_derive_keys_reauth(u16 _counter,
  93. const u8 *identity, size_t identity_len,
  94. const u8 *nonce_s, const u8 *mk, u8 *msk,
  95. u8 *emsk)
  96. {
  97. u8 xkey[SHA1_MAC_LEN];
  98. u8 buf[EAP_SIM_KEYING_DATA_LEN + EAP_EMSK_LEN + 32];
  99. u8 counter[2];
  100. const u8 *addr[4];
  101. size_t len[4];
  102. while (identity_len > 0 && identity[identity_len - 1] == 0) {
  103. wpa_printf(MSG_DEBUG, "EAP-SIM: Workaround - drop null "
  104. "character from the end of identity");
  105. identity_len--;
  106. }
  107. addr[0] = identity;
  108. len[0] = identity_len;
  109. addr[1] = counter;
  110. len[1] = 2;
  111. addr[2] = nonce_s;
  112. len[2] = EAP_SIM_NONCE_S_LEN;
  113. addr[3] = mk;
  114. len[3] = EAP_SIM_MK_LEN;
  115. WPA_PUT_BE16(counter, _counter);
  116. wpa_printf(MSG_DEBUG, "EAP-SIM: Deriving keying data from reauth");
  117. wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Identity",
  118. identity, identity_len);
  119. wpa_hexdump(MSG_DEBUG, "EAP-SIM: counter", counter, 2);
  120. wpa_hexdump(MSG_DEBUG, "EAP-SIM: NONCE_S", nonce_s,
  121. EAP_SIM_NONCE_S_LEN);
  122. wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: MK", mk, EAP_SIM_MK_LEN);
  123. /* XKEY' = SHA1(Identity|counter|NONCE_S|MK) */
  124. sha1_vector(4, addr, len, xkey);
  125. wpa_hexdump(MSG_DEBUG, "EAP-SIM: XKEY'", xkey, SHA1_MAC_LEN);
  126. if (eap_sim_prf(xkey, buf, sizeof(buf)) < 0) {
  127. wpa_printf(MSG_ERROR, "EAP-SIM: Failed to derive keys");
  128. return -1;
  129. }
  130. if (msk) {
  131. os_memcpy(msk, buf, EAP_SIM_KEYING_DATA_LEN);
  132. wpa_hexdump(MSG_DEBUG, "EAP-SIM: keying material (MSK)",
  133. msk, EAP_SIM_KEYING_DATA_LEN);
  134. }
  135. if (emsk) {
  136. os_memcpy(emsk, buf + EAP_SIM_KEYING_DATA_LEN, EAP_EMSK_LEN);
  137. wpa_hexdump(MSG_DEBUG, "EAP-SIM: EMSK", emsk, EAP_EMSK_LEN);
  138. }
  139. os_memset(buf, 0, sizeof(buf));
  140. return 0;
  141. }
  142. int eap_sim_verify_mac(const u8 *k_aut, const struct wpabuf *req,
  143. const u8 *mac, const u8 *extra, size_t extra_len)
  144. {
  145. unsigned char hmac[SHA1_MAC_LEN];
  146. const u8 *addr[2];
  147. size_t len[2];
  148. u8 *tmp;
  149. if (mac == NULL || wpabuf_len(req) < EAP_SIM_MAC_LEN ||
  150. mac < wpabuf_head_u8(req) ||
  151. mac > wpabuf_head_u8(req) + wpabuf_len(req) - EAP_SIM_MAC_LEN)
  152. return -1;
  153. tmp = os_malloc(wpabuf_len(req));
  154. if (tmp == NULL)
  155. return -1;
  156. addr[0] = tmp;
  157. len[0] = wpabuf_len(req);
  158. addr[1] = extra;
  159. len[1] = extra_len;
  160. /* HMAC-SHA1-128 */
  161. os_memcpy(tmp, wpabuf_head(req), wpabuf_len(req));
  162. os_memset(tmp + (mac - wpabuf_head_u8(req)), 0, EAP_SIM_MAC_LEN);
  163. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Verify MAC - msg",
  164. tmp, wpabuf_len(req));
  165. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Verify MAC - extra data",
  166. extra, extra_len);
  167. wpa_hexdump_key(MSG_MSGDUMP, "EAP-SIM: Verify MAC - K_aut",
  168. k_aut, EAP_SIM_K_AUT_LEN);
  169. hmac_sha1_vector(k_aut, EAP_SIM_K_AUT_LEN, 2, addr, len, hmac);
  170. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Verify MAC: MAC",
  171. hmac, EAP_SIM_MAC_LEN);
  172. os_free(tmp);
  173. return (os_memcmp(hmac, mac, EAP_SIM_MAC_LEN) == 0) ? 0 : 1;
  174. }
  175. void eap_sim_add_mac(const u8 *k_aut, const u8 *msg, size_t msg_len, u8 *mac,
  176. const u8 *extra, size_t extra_len)
  177. {
  178. unsigned char hmac[SHA1_MAC_LEN];
  179. const u8 *addr[2];
  180. size_t len[2];
  181. addr[0] = msg;
  182. len[0] = msg_len;
  183. addr[1] = extra;
  184. len[1] = extra_len;
  185. /* HMAC-SHA1-128 */
  186. os_memset(mac, 0, EAP_SIM_MAC_LEN);
  187. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Add MAC - msg", msg, msg_len);
  188. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Add MAC - extra data",
  189. extra, extra_len);
  190. wpa_hexdump_key(MSG_MSGDUMP, "EAP-SIM: Add MAC - K_aut",
  191. k_aut, EAP_SIM_K_AUT_LEN);
  192. hmac_sha1_vector(k_aut, EAP_SIM_K_AUT_LEN, 2, addr, len, hmac);
  193. os_memcpy(mac, hmac, EAP_SIM_MAC_LEN);
  194. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Add MAC: MAC",
  195. mac, EAP_SIM_MAC_LEN);
  196. }
  197. int eap_sim_parse_attr(const u8 *start, const u8 *end,
  198. struct eap_sim_attrs *attr, int aka, int encr)
  199. {
  200. const u8 *pos = start, *apos;
  201. size_t alen, plen, i, list_len;
  202. os_memset(attr, 0, sizeof(*attr));
  203. attr->id_req = NO_ID_REQ;
  204. attr->notification = -1;
  205. attr->counter = -1;
  206. attr->selected_version = -1;
  207. attr->client_error_code = -1;
  208. while (pos < end) {
  209. if (pos + 2 > end) {
  210. wpa_printf(MSG_INFO, "EAP-SIM: Attribute overflow(1)");
  211. return -1;
  212. }
  213. wpa_printf(MSG_MSGDUMP, "EAP-SIM: Attribute: Type=%d Len=%d",
  214. pos[0], pos[1] * 4);
  215. if (pos + pos[1] * 4 > end) {
  216. wpa_printf(MSG_INFO, "EAP-SIM: Attribute overflow "
  217. "(pos=%p len=%d end=%p)",
  218. pos, pos[1] * 4, end);
  219. return -1;
  220. }
  221. if (pos[1] == 0) {
  222. wpa_printf(MSG_INFO, "EAP-SIM: Attribute underflow");
  223. return -1;
  224. }
  225. apos = pos + 2;
  226. alen = pos[1] * 4 - 2;
  227. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Attribute data",
  228. apos, alen);
  229. switch (pos[0]) {
  230. case EAP_SIM_AT_RAND:
  231. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_RAND");
  232. apos += 2;
  233. alen -= 2;
  234. if ((!aka && (alen % GSM_RAND_LEN)) ||
  235. (aka && alen != EAP_AKA_RAND_LEN)) {
  236. wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_RAND"
  237. " (len %lu)",
  238. (unsigned long) alen);
  239. return -1;
  240. }
  241. attr->rand = apos;
  242. attr->num_chal = alen / GSM_RAND_LEN;
  243. break;
  244. case EAP_SIM_AT_AUTN:
  245. wpa_printf(MSG_DEBUG, "EAP-AKA: AT_AUTN");
  246. if (!aka) {
  247. wpa_printf(MSG_DEBUG, "EAP-SIM: "
  248. "Unexpected AT_AUTN");
  249. return -1;
  250. }
  251. apos += 2;
  252. alen -= 2;
  253. if (alen != EAP_AKA_AUTN_LEN) {
  254. wpa_printf(MSG_INFO, "EAP-AKA: Invalid AT_AUTN"
  255. " (len %lu)",
  256. (unsigned long) alen);
  257. return -1;
  258. }
  259. attr->autn = apos;
  260. break;
  261. case EAP_SIM_AT_PADDING:
  262. if (!encr) {
  263. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  264. "AT_PADDING");
  265. return -1;
  266. }
  267. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) AT_PADDING");
  268. for (i = 2; i < alen; i++) {
  269. if (apos[i] != 0) {
  270. wpa_printf(MSG_INFO, "EAP-SIM: (encr) "
  271. "AT_PADDING used a non-zero"
  272. " padding byte");
  273. wpa_hexdump(MSG_DEBUG, "EAP-SIM: "
  274. "(encr) padding bytes",
  275. apos + 2, alen - 2);
  276. return -1;
  277. }
  278. }
  279. break;
  280. case EAP_SIM_AT_NONCE_MT:
  281. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_NONCE_MT");
  282. if (alen != 2 + EAP_SIM_NONCE_MT_LEN) {
  283. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  284. "AT_NONCE_MT length");
  285. return -1;
  286. }
  287. attr->nonce_mt = apos + 2;
  288. break;
  289. case EAP_SIM_AT_PERMANENT_ID_REQ:
  290. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_PERMANENT_ID_REQ");
  291. attr->id_req = PERMANENT_ID;
  292. break;
  293. case EAP_SIM_AT_MAC:
  294. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_MAC");
  295. if (alen != 2 + EAP_SIM_MAC_LEN) {
  296. wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_MAC "
  297. "length");
  298. return -1;
  299. }
  300. attr->mac = apos + 2;
  301. break;
  302. case EAP_SIM_AT_NOTIFICATION:
  303. if (alen != 2) {
  304. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  305. "AT_NOTIFICATION length %lu",
  306. (unsigned long) alen);
  307. return -1;
  308. }
  309. attr->notification = apos[0] * 256 + apos[1];
  310. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_NOTIFICATION %d",
  311. attr->notification);
  312. break;
  313. case EAP_SIM_AT_ANY_ID_REQ:
  314. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_ANY_ID_REQ");
  315. attr->id_req = ANY_ID;
  316. break;
  317. case EAP_SIM_AT_IDENTITY:
  318. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_IDENTITY");
  319. attr->identity = apos + 2;
  320. attr->identity_len = alen - 2;
  321. break;
  322. case EAP_SIM_AT_VERSION_LIST:
  323. if (aka) {
  324. wpa_printf(MSG_DEBUG, "EAP-AKA: "
  325. "Unexpected AT_VERSION_LIST");
  326. return -1;
  327. }
  328. list_len = apos[0] * 256 + apos[1];
  329. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_VERSION_LIST");
  330. if (list_len < 2 || list_len > alen - 2) {
  331. wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
  332. "AT_VERSION_LIST (list_len=%lu "
  333. "attr_len=%lu)",
  334. (unsigned long) list_len,
  335. (unsigned long) alen);
  336. return -1;
  337. }
  338. attr->version_list = apos + 2;
  339. attr->version_list_len = list_len;
  340. break;
  341. case EAP_SIM_AT_SELECTED_VERSION:
  342. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_SELECTED_VERSION");
  343. if (alen != 2) {
  344. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  345. "AT_SELECTED_VERSION length %lu",
  346. (unsigned long) alen);
  347. return -1;
  348. }
  349. attr->selected_version = apos[0] * 256 + apos[1];
  350. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_SELECTED_VERSION "
  351. "%d", attr->selected_version);
  352. break;
  353. case EAP_SIM_AT_FULLAUTH_ID_REQ:
  354. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_FULLAUTH_ID_REQ");
  355. attr->id_req = FULLAUTH_ID;
  356. break;
  357. case EAP_SIM_AT_COUNTER:
  358. if (!encr) {
  359. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  360. "AT_COUNTER");
  361. return -1;
  362. }
  363. if (alen != 2) {
  364. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid "
  365. "AT_COUNTER (alen=%lu)",
  366. (unsigned long) alen);
  367. return -1;
  368. }
  369. attr->counter = apos[0] * 256 + apos[1];
  370. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) AT_COUNTER %d",
  371. attr->counter);
  372. break;
  373. case EAP_SIM_AT_COUNTER_TOO_SMALL:
  374. if (!encr) {
  375. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  376. "AT_COUNTER_TOO_SMALL");
  377. return -1;
  378. }
  379. if (alen != 2) {
  380. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid "
  381. "AT_COUNTER_TOO_SMALL (alen=%lu)",
  382. (unsigned long) alen);
  383. return -1;
  384. }
  385. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "
  386. "AT_COUNTER_TOO_SMALL");
  387. attr->counter_too_small = 1;
  388. break;
  389. case EAP_SIM_AT_NONCE_S:
  390. if (!encr) {
  391. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  392. "AT_NONCE_S");
  393. return -1;
  394. }
  395. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "
  396. "AT_NONCE_S");
  397. if (alen != 2 + EAP_SIM_NONCE_S_LEN) {
  398. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid "
  399. "AT_NONCE_S (alen=%lu)",
  400. (unsigned long) alen);
  401. return -1;
  402. }
  403. attr->nonce_s = apos + 2;
  404. break;
  405. case EAP_SIM_AT_CLIENT_ERROR_CODE:
  406. if (alen != 2) {
  407. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  408. "AT_CLIENT_ERROR_CODE length %lu",
  409. (unsigned long) alen);
  410. return -1;
  411. }
  412. attr->client_error_code = apos[0] * 256 + apos[1];
  413. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_CLIENT_ERROR_CODE "
  414. "%d", attr->client_error_code);
  415. break;
  416. case EAP_SIM_AT_IV:
  417. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_IV");
  418. if (alen != 2 + EAP_SIM_MAC_LEN) {
  419. wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_IV "
  420. "length %lu", (unsigned long) alen);
  421. return -1;
  422. }
  423. attr->iv = apos + 2;
  424. break;
  425. case EAP_SIM_AT_ENCR_DATA:
  426. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_ENCR_DATA");
  427. attr->encr_data = apos + 2;
  428. attr->encr_data_len = alen - 2;
  429. if (attr->encr_data_len % 16) {
  430. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  431. "AT_ENCR_DATA length %lu",
  432. (unsigned long)
  433. attr->encr_data_len);
  434. return -1;
  435. }
  436. break;
  437. case EAP_SIM_AT_NEXT_PSEUDONYM:
  438. if (!encr) {
  439. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  440. "AT_NEXT_PSEUDONYM");
  441. return -1;
  442. }
  443. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "
  444. "AT_NEXT_PSEUDONYM");
  445. plen = apos[0] * 256 + apos[1];
  446. if (plen > alen - 2) {
  447. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid"
  448. " AT_NEXT_PSEUDONYM (actual"
  449. " len %lu, attr len %lu)",
  450. (unsigned long) plen,
  451. (unsigned long) alen);
  452. return -1;
  453. }
  454. attr->next_pseudonym = pos + 4;
  455. attr->next_pseudonym_len = plen;
  456. break;
  457. case EAP_SIM_AT_NEXT_REAUTH_ID:
  458. if (!encr) {
  459. wpa_printf(MSG_ERROR, "EAP-SIM: Unencrypted "
  460. "AT_NEXT_REAUTH_ID");
  461. return -1;
  462. }
  463. wpa_printf(MSG_DEBUG, "EAP-SIM: (encr) "
  464. "AT_NEXT_REAUTH_ID");
  465. plen = apos[0] * 256 + apos[1];
  466. if (plen > alen - 2) {
  467. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid"
  468. " AT_NEXT_REAUTH_ID (actual"
  469. " len %lu, attr len %lu)",
  470. (unsigned long) plen,
  471. (unsigned long) alen);
  472. return -1;
  473. }
  474. attr->next_reauth_id = pos + 4;
  475. attr->next_reauth_id_len = plen;
  476. break;
  477. case EAP_SIM_AT_RES:
  478. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_RES");
  479. attr->res_len_bits = WPA_GET_BE16(apos);
  480. apos += 2;
  481. alen -= 2;
  482. if (!aka || alen < EAP_AKA_MIN_RES_LEN ||
  483. alen > EAP_AKA_MAX_RES_LEN) {
  484. wpa_printf(MSG_INFO, "EAP-SIM: Invalid AT_RES "
  485. "(len %lu)",
  486. (unsigned long) alen);
  487. return -1;
  488. }
  489. attr->res = apos;
  490. attr->res_len = alen;
  491. break;
  492. case EAP_SIM_AT_AUTS:
  493. wpa_printf(MSG_DEBUG, "EAP-AKA: AT_AUTS");
  494. if (!aka) {
  495. wpa_printf(MSG_DEBUG, "EAP-SIM: "
  496. "Unexpected AT_AUTS");
  497. return -1;
  498. }
  499. if (alen != EAP_AKA_AUTS_LEN) {
  500. wpa_printf(MSG_INFO, "EAP-AKA: Invalid AT_AUTS"
  501. " (len %lu)",
  502. (unsigned long) alen);
  503. return -1;
  504. }
  505. attr->auts = apos;
  506. break;
  507. case EAP_SIM_AT_CHECKCODE:
  508. wpa_printf(MSG_DEBUG, "EAP-AKA: AT_CHECKCODE");
  509. if (!aka) {
  510. wpa_printf(MSG_DEBUG, "EAP-SIM: "
  511. "Unexpected AT_CHECKCODE");
  512. return -1;
  513. }
  514. apos += 2;
  515. alen -= 2;
  516. if (alen != 0 && alen != EAP_AKA_CHECKCODE_LEN) {
  517. wpa_printf(MSG_INFO, "EAP-AKA: Invalid "
  518. "AT_CHECKCODE (len %lu)",
  519. (unsigned long) alen);
  520. return -1;
  521. }
  522. attr->checkcode = apos;
  523. attr->checkcode_len = alen;
  524. break;
  525. case EAP_SIM_AT_RESULT_IND:
  526. if (encr) {
  527. wpa_printf(MSG_ERROR, "EAP-SIM: Encrypted "
  528. "AT_RESULT_IND");
  529. return -1;
  530. }
  531. if (alen != 2) {
  532. wpa_printf(MSG_INFO, "EAP-SIM: Invalid "
  533. "AT_RESULT_IND (alen=%lu)",
  534. (unsigned long) alen);
  535. return -1;
  536. }
  537. wpa_printf(MSG_DEBUG, "EAP-SIM: AT_RESULT_IND");
  538. attr->result_ind = 1;
  539. break;
  540. default:
  541. if (pos[0] < 128) {
  542. wpa_printf(MSG_INFO, "EAP-SIM: Unrecognized "
  543. "non-skippable attribute %d",
  544. pos[0]);
  545. return -1;
  546. }
  547. wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized skippable"
  548. " attribute %d ignored", pos[0]);
  549. break;
  550. }
  551. pos += pos[1] * 4;
  552. }
  553. wpa_printf(MSG_DEBUG, "EAP-SIM: Attributes parsed successfully "
  554. "(aka=%d encr=%d)", aka, encr);
  555. return 0;
  556. }
  557. u8 * eap_sim_parse_encr(const u8 *k_encr, const u8 *encr_data,
  558. size_t encr_data_len, const u8 *iv,
  559. struct eap_sim_attrs *attr, int aka)
  560. {
  561. u8 *decrypted;
  562. if (!iv) {
  563. wpa_printf(MSG_INFO, "EAP-SIM: Encrypted data, but no IV");
  564. return NULL;
  565. }
  566. decrypted = os_malloc(encr_data_len);
  567. if (decrypted == NULL)
  568. return NULL;
  569. os_memcpy(decrypted, encr_data, encr_data_len);
  570. if (aes_128_cbc_decrypt(k_encr, iv, decrypted, encr_data_len)) {
  571. os_free(decrypted);
  572. return NULL;
  573. }
  574. wpa_hexdump(MSG_MSGDUMP, "EAP-SIM: Decrypted AT_ENCR_DATA",
  575. decrypted, encr_data_len);
  576. if (eap_sim_parse_attr(decrypted, decrypted + encr_data_len, attr,
  577. aka, 1)) {
  578. wpa_printf(MSG_INFO, "EAP-SIM: (encr) Failed to parse "
  579. "decrypted AT_ENCR_DATA");
  580. os_free(decrypted);
  581. return NULL;
  582. }
  583. return decrypted;
  584. }
  585. #define EAP_SIM_INIT_LEN 128
  586. struct eap_sim_msg {
  587. struct wpabuf *buf;
  588. size_t mac, iv, encr; /* index from buf */
  589. };
  590. struct eap_sim_msg * eap_sim_msg_init(int code, int id, int type, int subtype)
  591. {
  592. struct eap_sim_msg *msg;
  593. struct eap_hdr *eap;
  594. u8 *pos;
  595. msg = os_zalloc(sizeof(*msg));
  596. if (msg == NULL)
  597. return NULL;
  598. msg->buf = wpabuf_alloc(EAP_SIM_INIT_LEN);
  599. if (msg->buf == NULL) {
  600. os_free(msg);
  601. return NULL;
  602. }
  603. eap = wpabuf_put(msg->buf, sizeof(*eap));
  604. eap->code = code;
  605. eap->identifier = id;
  606. pos = wpabuf_put(msg->buf, 4);
  607. *pos++ = type;
  608. *pos++ = subtype;
  609. *pos++ = 0; /* Reserved */
  610. *pos++ = 0; /* Reserved */
  611. return msg;
  612. }
  613. struct wpabuf * eap_sim_msg_finish(struct eap_sim_msg *msg, const u8 *k_aut,
  614. const u8 *extra, size_t extra_len)
  615. {
  616. struct eap_hdr *eap;
  617. struct wpabuf *buf;
  618. if (msg == NULL)
  619. return NULL;
  620. eap = wpabuf_mhead(msg->buf);
  621. eap->length = host_to_be16(wpabuf_len(msg->buf));
  622. if (k_aut && msg->mac) {
  623. eap_sim_add_mac(k_aut, (u8 *) wpabuf_head(msg->buf),
  624. wpabuf_len(msg->buf),
  625. (u8 *) wpabuf_mhead(msg->buf) + msg->mac,
  626. extra, extra_len);
  627. }
  628. buf = msg->buf;
  629. os_free(msg);
  630. return buf;
  631. }
  632. void eap_sim_msg_free(struct eap_sim_msg *msg)
  633. {
  634. if (msg) {
  635. wpabuf_free(msg->buf);
  636. os_free(msg);
  637. }
  638. }
  639. u8 * eap_sim_msg_add_full(struct eap_sim_msg *msg, u8 attr,
  640. const u8 *data, size_t len)
  641. {
  642. int attr_len = 2 + len;
  643. int pad_len;
  644. u8 *start;
  645. if (msg == NULL)
  646. return NULL;
  647. pad_len = (4 - attr_len % 4) % 4;
  648. attr_len += pad_len;
  649. if (wpabuf_resize(&msg->buf, attr_len))
  650. return NULL;
  651. start = wpabuf_put(msg->buf, 0);
  652. wpabuf_put_u8(msg->buf, attr);
  653. wpabuf_put_u8(msg->buf, attr_len / 4);
  654. wpabuf_put_data(msg->buf, data, len);
  655. if (pad_len)
  656. os_memset(wpabuf_put(msg->buf, pad_len), 0, pad_len);
  657. return start;
  658. }
  659. u8 * eap_sim_msg_add(struct eap_sim_msg *msg, u8 attr, u16 value,
  660. const u8 *data, size_t len)
  661. {
  662. int attr_len = 4 + len;
  663. int pad_len;
  664. u8 *start;
  665. if (msg == NULL)
  666. return NULL;
  667. pad_len = (4 - attr_len % 4) % 4;
  668. attr_len += pad_len;
  669. if (wpabuf_resize(&msg->buf, attr_len))
  670. return NULL;
  671. start = wpabuf_put(msg->buf, 0);
  672. wpabuf_put_u8(msg->buf, attr);
  673. wpabuf_put_u8(msg->buf, attr_len / 4);
  674. wpabuf_put_be16(msg->buf, value);
  675. if (data)
  676. wpabuf_put_data(msg->buf, data, len);
  677. else
  678. wpabuf_put(msg->buf, len);
  679. if (pad_len)
  680. os_memset(wpabuf_put(msg->buf, pad_len), 0, pad_len);
  681. return start;
  682. }
  683. u8 * eap_sim_msg_add_mac(struct eap_sim_msg *msg, u8 attr)
  684. {
  685. u8 *pos = eap_sim_msg_add(msg, attr, 0, NULL, EAP_SIM_MAC_LEN);
  686. if (pos)
  687. msg->mac = (pos - wpabuf_head_u8(msg->buf)) + 4;
  688. return pos;
  689. }
  690. int eap_sim_msg_add_encr_start(struct eap_sim_msg *msg, u8 attr_iv,
  691. u8 attr_encr)
  692. {
  693. u8 *pos = eap_sim_msg_add(msg, attr_iv, 0, NULL, EAP_SIM_IV_LEN);
  694. if (pos == NULL)
  695. return -1;
  696. msg->iv = (pos - wpabuf_head_u8(msg->buf)) + 4;
  697. if (os_get_random(wpabuf_mhead_u8(msg->buf) + msg->iv,
  698. EAP_SIM_IV_LEN)) {
  699. msg->iv = 0;
  700. return -1;
  701. }
  702. pos = eap_sim_msg_add(msg, attr_encr, 0, NULL, 0);
  703. if (pos == NULL) {
  704. msg->iv = 0;
  705. return -1;
  706. }
  707. msg->encr = pos - wpabuf_head_u8(msg->buf);
  708. return 0;
  709. }
  710. int eap_sim_msg_add_encr_end(struct eap_sim_msg *msg, u8 *k_encr, int attr_pad)
  711. {
  712. size_t encr_len;
  713. if (msg == NULL || k_encr == NULL || msg->iv == 0 || msg->encr == 0)
  714. return -1;
  715. encr_len = wpabuf_len(msg->buf) - msg->encr - 4;
  716. if (encr_len % 16) {
  717. u8 *pos;
  718. int pad_len = 16 - (encr_len % 16);
  719. if (pad_len < 4) {
  720. wpa_printf(MSG_WARNING, "EAP-SIM: "
  721. "eap_sim_msg_add_encr_end - invalid pad_len"
  722. " %d", pad_len);
  723. return -1;
  724. }
  725. wpa_printf(MSG_DEBUG, " *AT_PADDING");
  726. pos = eap_sim_msg_add(msg, attr_pad, 0, NULL, pad_len - 4);
  727. if (pos == NULL)
  728. return -1;
  729. os_memset(pos + 4, 0, pad_len - 4);
  730. encr_len += pad_len;
  731. }
  732. wpa_printf(MSG_DEBUG, " (AT_ENCR_DATA data len %lu)",
  733. (unsigned long) encr_len);
  734. wpabuf_mhead_u8(msg->buf)[msg->encr + 1] = encr_len / 4 + 1;
  735. return aes_128_cbc_encrypt(k_encr, wpabuf_head_u8(msg->buf) + msg->iv,
  736. wpabuf_mhead_u8(msg->buf) + msg->encr + 4,
  737. encr_len);
  738. }
  739. void eap_sim_report_notification(void *msg_ctx, int notification, int aka)
  740. {
  741. #ifndef CONFIG_NO_STDOUT_DEBUG
  742. const char *type = aka ? "AKA" : "SIM";
  743. #endif /* CONFIG_NO_STDOUT_DEBUG */
  744. switch (notification) {
  745. case EAP_SIM_GENERAL_FAILURE_AFTER_AUTH:
  746. wpa_printf(MSG_WARNING, "EAP-%s: General failure "
  747. "notification (after authentication)", type);
  748. break;
  749. case EAP_SIM_TEMPORARILY_DENIED:
  750. wpa_printf(MSG_WARNING, "EAP-%s: Failure notification: "
  751. "User has been temporarily denied access to the "
  752. "requested service", type);
  753. break;
  754. case EAP_SIM_NOT_SUBSCRIBED:
  755. wpa_printf(MSG_WARNING, "EAP-%s: Failure notification: "
  756. "User has not subscribed to the requested service",
  757. type);
  758. break;
  759. case EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH:
  760. wpa_printf(MSG_WARNING, "EAP-%s: General failure "
  761. "notification (before authentication)", type);
  762. break;
  763. case EAP_SIM_SUCCESS:
  764. wpa_printf(MSG_INFO, "EAP-%s: Successful authentication "
  765. "notification", type);
  766. break;
  767. default:
  768. if (notification >= 32768) {
  769. wpa_printf(MSG_INFO, "EAP-%s: Unrecognized "
  770. "non-failure notification %d",
  771. type, notification);
  772. } else {
  773. wpa_printf(MSG_WARNING, "EAP-%s: Unrecognized "
  774. "failure notification %d",
  775. type, notification);
  776. }
  777. }
  778. }