eap_gpsk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * EAP peer method: EAP-GPSK (draft-ietf-emu-eap-gpsk-08.txt)
  3. * Copyright (c) 2006-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eap_peer/eap_i.h"
  17. #include "eap_common/eap_gpsk_common.h"
  18. struct eap_gpsk_data {
  19. enum { GPSK_1, GPSK_3, SUCCESS, FAILURE } state;
  20. u8 rand_server[EAP_GPSK_RAND_LEN];
  21. u8 rand_peer[EAP_GPSK_RAND_LEN];
  22. u8 msk[EAP_MSK_LEN];
  23. u8 emsk[EAP_EMSK_LEN];
  24. u8 sk[EAP_GPSK_MAX_SK_LEN];
  25. size_t sk_len;
  26. u8 pk[EAP_GPSK_MAX_PK_LEN];
  27. size_t pk_len;
  28. u8 session_id;
  29. int session_id_set;
  30. u8 *id_peer;
  31. size_t id_peer_len;
  32. u8 *id_server;
  33. size_t id_server_len;
  34. int vendor; /* CSuite/Specifier */
  35. int specifier; /* CSuite/Specifier */
  36. u8 *psk;
  37. size_t psk_len;
  38. };
  39. static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
  40. u8 identifier,
  41. const u8 *csuite_list,
  42. size_t csuite_list_len);
  43. static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
  44. u8 identifier);
  45. #ifndef CONFIG_NO_STDOUT_DEBUG
  46. static const char * eap_gpsk_state_txt(int state)
  47. {
  48. switch (state) {
  49. case GPSK_1:
  50. return "GPSK-1";
  51. case GPSK_3:
  52. return "GPSK-3";
  53. case SUCCESS:
  54. return "SUCCESS";
  55. case FAILURE:
  56. return "FAILURE";
  57. default:
  58. return "?";
  59. }
  60. }
  61. #endif /* CONFIG_NO_STDOUT_DEBUG */
  62. static void eap_gpsk_state(struct eap_gpsk_data *data, int state)
  63. {
  64. wpa_printf(MSG_DEBUG, "EAP-GPSK: %s -> %s",
  65. eap_gpsk_state_txt(data->state),
  66. eap_gpsk_state_txt(state));
  67. data->state = state;
  68. }
  69. static void eap_gpsk_deinit(struct eap_sm *sm, void *priv);
  70. static void * eap_gpsk_init(struct eap_sm *sm)
  71. {
  72. struct eap_gpsk_data *data;
  73. const u8 *identity, *password;
  74. size_t identity_len, password_len;
  75. password = eap_get_config_password(sm, &password_len);
  76. if (password == NULL) {
  77. wpa_printf(MSG_INFO, "EAP-GPSK: No key (password) configured");
  78. return NULL;
  79. }
  80. data = os_zalloc(sizeof(*data));
  81. if (data == NULL)
  82. return NULL;
  83. data->state = GPSK_1;
  84. identity = eap_get_config_identity(sm, &identity_len);
  85. if (identity) {
  86. data->id_peer = os_malloc(identity_len);
  87. if (data->id_peer == NULL) {
  88. eap_gpsk_deinit(sm, data);
  89. return NULL;
  90. }
  91. os_memcpy(data->id_peer, identity, identity_len);
  92. data->id_peer_len = identity_len;
  93. }
  94. data->psk = os_malloc(password_len);
  95. if (data->psk == NULL) {
  96. eap_gpsk_deinit(sm, data);
  97. return NULL;
  98. }
  99. os_memcpy(data->psk, password, password_len);
  100. data->psk_len = password_len;
  101. return data;
  102. }
  103. static void eap_gpsk_deinit(struct eap_sm *sm, void *priv)
  104. {
  105. struct eap_gpsk_data *data = priv;
  106. os_free(data->id_server);
  107. os_free(data->id_peer);
  108. os_free(data->psk);
  109. os_free(data);
  110. }
  111. const u8 * eap_gpsk_process_id_server(struct eap_gpsk_data *data,
  112. const u8 *pos, const u8 *end)
  113. {
  114. u16 alen;
  115. if (end - pos < 2) {
  116. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
  117. return NULL;
  118. }
  119. alen = WPA_GET_BE16(pos);
  120. pos += 2;
  121. if (end - pos < alen) {
  122. wpa_printf(MSG_DEBUG, "EAP-GPSK: ID_Server overflow");
  123. return NULL;
  124. }
  125. os_free(data->id_server);
  126. data->id_server = os_malloc(alen);
  127. if (data->id_server == NULL) {
  128. wpa_printf(MSG_DEBUG, "EAP-GPSK: No memory for ID_Server");
  129. return NULL;
  130. }
  131. os_memcpy(data->id_server, pos, alen);
  132. data->id_server_len = alen;
  133. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server",
  134. data->id_server, data->id_server_len);
  135. pos += alen;
  136. return pos;
  137. }
  138. const u8 * eap_gpsk_process_rand_server(struct eap_gpsk_data *data,
  139. const u8 *pos, const u8 *end)
  140. {
  141. if (pos == NULL)
  142. return NULL;
  143. if (end - pos < EAP_GPSK_RAND_LEN) {
  144. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server overflow");
  145. return NULL;
  146. }
  147. os_memcpy(data->rand_server, pos, EAP_GPSK_RAND_LEN);
  148. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server",
  149. data->rand_server, EAP_GPSK_RAND_LEN);
  150. pos += EAP_GPSK_RAND_LEN;
  151. return pos;
  152. }
  153. static int eap_gpsk_select_csuite(struct eap_sm *sm,
  154. struct eap_gpsk_data *data,
  155. const u8 *csuite_list,
  156. size_t csuite_list_len)
  157. {
  158. struct eap_gpsk_csuite *csuite;
  159. int i, count;
  160. count = csuite_list_len / sizeof(struct eap_gpsk_csuite);
  161. data->vendor = EAP_GPSK_VENDOR_IETF;
  162. data->specifier = EAP_GPSK_CIPHER_RESERVED;
  163. csuite = (struct eap_gpsk_csuite *) csuite_list;
  164. for (i = 0; i < count; i++) {
  165. int vendor, specifier;
  166. vendor = WPA_GET_BE32(csuite->vendor);
  167. specifier = WPA_GET_BE16(csuite->specifier);
  168. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite[%d]: %d:%d",
  169. i, vendor, specifier);
  170. if (data->vendor == EAP_GPSK_VENDOR_IETF &&
  171. data->specifier == EAP_GPSK_CIPHER_RESERVED &&
  172. eap_gpsk_supported_ciphersuite(vendor, specifier)) {
  173. data->vendor = vendor;
  174. data->specifier = specifier;
  175. }
  176. csuite++;
  177. }
  178. if (data->vendor == EAP_GPSK_VENDOR_IETF &&
  179. data->specifier == EAP_GPSK_CIPHER_RESERVED) {
  180. wpa_msg(sm->msg_ctx, MSG_INFO, "EAP-GPSK: No supported "
  181. "ciphersuite found");
  182. return -1;
  183. }
  184. wpa_printf(MSG_DEBUG, "EAP-GPSK: Selected ciphersuite %d:%d",
  185. data->vendor, data->specifier);
  186. return 0;
  187. }
  188. const u8 * eap_gpsk_process_csuite_list(struct eap_sm *sm,
  189. struct eap_gpsk_data *data,
  190. const u8 **list, size_t *list_len,
  191. const u8 *pos, const u8 *end)
  192. {
  193. if (pos == NULL)
  194. return NULL;
  195. if (end - pos < 2) {
  196. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
  197. return NULL;
  198. }
  199. *list_len = WPA_GET_BE16(pos);
  200. pos += 2;
  201. if (end - pos < (int) *list_len) {
  202. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List overflow");
  203. return NULL;
  204. }
  205. if (*list_len == 0 || (*list_len % sizeof(struct eap_gpsk_csuite))) {
  206. wpa_printf(MSG_DEBUG, "EAP-GPSK: Invalid CSuite_List len %lu",
  207. (unsigned long) *list_len);
  208. return NULL;
  209. }
  210. *list = pos;
  211. pos += *list_len;
  212. if (eap_gpsk_select_csuite(sm, data, *list, *list_len) < 0)
  213. return NULL;
  214. return pos;
  215. }
  216. static struct wpabuf * eap_gpsk_process_gpsk_1(struct eap_sm *sm,
  217. struct eap_gpsk_data *data,
  218. struct eap_method_ret *ret,
  219. const struct wpabuf *reqData,
  220. const u8 *payload,
  221. size_t payload_len)
  222. {
  223. size_t csuite_list_len;
  224. const u8 *csuite_list, *pos, *end;
  225. struct wpabuf *resp;
  226. if (data->state != GPSK_1) {
  227. ret->ignore = TRUE;
  228. return NULL;
  229. }
  230. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-1");
  231. end = payload + payload_len;
  232. pos = eap_gpsk_process_id_server(data, payload, end);
  233. pos = eap_gpsk_process_rand_server(data, pos, end);
  234. pos = eap_gpsk_process_csuite_list(sm, data, &csuite_list,
  235. &csuite_list_len, pos, end);
  236. if (pos == NULL) {
  237. eap_gpsk_state(data, FAILURE);
  238. return NULL;
  239. }
  240. resp = eap_gpsk_send_gpsk_2(data, eap_get_id(reqData),
  241. csuite_list, csuite_list_len);
  242. if (resp == NULL)
  243. return NULL;
  244. eap_gpsk_state(data, GPSK_3);
  245. return resp;
  246. }
  247. static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
  248. u8 identifier,
  249. const u8 *csuite_list,
  250. size_t csuite_list_len)
  251. {
  252. struct wpabuf *resp;
  253. size_t len, miclen;
  254. u8 *rpos, *start;
  255. struct eap_gpsk_csuite *csuite;
  256. wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-2");
  257. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  258. len = 1 + 2 + data->id_peer_len + 2 + data->id_server_len +
  259. 2 * EAP_GPSK_RAND_LEN + 2 + csuite_list_len +
  260. sizeof(struct eap_gpsk_csuite) + 2 + miclen;
  261. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
  262. EAP_CODE_RESPONSE, identifier);
  263. if (resp == NULL)
  264. return NULL;
  265. wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_2);
  266. start = wpabuf_put(resp, 0);
  267. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
  268. data->id_peer, data->id_peer_len);
  269. wpabuf_put_be16(resp, data->id_peer_len);
  270. wpabuf_put_data(resp, data->id_peer, data->id_peer_len);
  271. wpabuf_put_be16(resp, data->id_server_len);
  272. wpabuf_put_data(resp, data->id_server, data->id_server_len);
  273. if (os_get_random(data->rand_peer, EAP_GPSK_RAND_LEN)) {
  274. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to get random data "
  275. "for RAND_Peer");
  276. eap_gpsk_state(data, FAILURE);
  277. wpabuf_free(resp);
  278. return NULL;
  279. }
  280. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
  281. data->rand_peer, EAP_GPSK_RAND_LEN);
  282. wpabuf_put_data(resp, data->rand_peer, EAP_GPSK_RAND_LEN);
  283. wpabuf_put_data(resp, data->rand_server, EAP_GPSK_RAND_LEN);
  284. wpabuf_put_be16(resp, csuite_list_len);
  285. wpabuf_put_data(resp, csuite_list, csuite_list_len);
  286. csuite = wpabuf_put(resp, sizeof(*csuite));
  287. WPA_PUT_BE32(csuite->vendor, data->vendor);
  288. WPA_PUT_BE16(csuite->specifier, data->specifier);
  289. if (eap_gpsk_derive_keys(data->psk, data->psk_len,
  290. data->vendor, data->specifier,
  291. data->rand_peer, data->rand_server,
  292. data->id_peer, data->id_peer_len,
  293. data->id_server, data->id_server_len,
  294. data->msk, data->emsk,
  295. data->sk, &data->sk_len,
  296. data->pk, &data->pk_len) < 0) {
  297. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
  298. eap_gpsk_state(data, FAILURE);
  299. wpabuf_free(resp);
  300. return NULL;
  301. }
  302. /* No PD_Payload_1 */
  303. wpabuf_put_be16(resp, 0);
  304. rpos = wpabuf_put(resp, miclen);
  305. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  306. data->specifier, start, rpos - start, rpos) <
  307. 0) {
  308. eap_gpsk_state(data, FAILURE);
  309. wpabuf_free(resp);
  310. return NULL;
  311. }
  312. return resp;
  313. }
  314. const u8 * eap_gpsk_validate_rand(struct eap_gpsk_data *data, const u8 *pos,
  315. const u8 *end)
  316. {
  317. if (end - pos < EAP_GPSK_RAND_LEN) {
  318. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  319. "RAND_Peer");
  320. return NULL;
  321. }
  322. if (os_memcmp(pos, data->rand_peer, EAP_GPSK_RAND_LEN) != 0) {
  323. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2 and "
  324. "GPSK-3 did not match");
  325. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2",
  326. data->rand_peer, EAP_GPSK_RAND_LEN);
  327. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-3",
  328. pos, EAP_GPSK_RAND_LEN);
  329. return NULL;
  330. }
  331. pos += EAP_GPSK_RAND_LEN;
  332. if (end - pos < EAP_GPSK_RAND_LEN) {
  333. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  334. "RAND_Server");
  335. return NULL;
  336. }
  337. if (os_memcmp(pos, data->rand_server, EAP_GPSK_RAND_LEN) != 0) {
  338. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
  339. "GPSK-3 did not match");
  340. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
  341. data->rand_server, EAP_GPSK_RAND_LEN);
  342. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-3",
  343. pos, EAP_GPSK_RAND_LEN);
  344. return NULL;
  345. }
  346. pos += EAP_GPSK_RAND_LEN;
  347. return pos;
  348. }
  349. const u8 * eap_gpsk_validate_id_server(struct eap_gpsk_data *data,
  350. const u8 *pos, const u8 *end)
  351. {
  352. size_t len;
  353. if (pos == NULL)
  354. return NULL;
  355. if (end - pos < (int) 2) {
  356. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  357. "length(ID_Server)");
  358. return NULL;
  359. }
  360. len = WPA_GET_BE16(pos);
  361. pos += 2;
  362. if (end - pos < (int) len) {
  363. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  364. "ID_Server");
  365. return NULL;
  366. }
  367. if (len != data->id_server_len ||
  368. os_memcmp(pos, data->id_server, len) != 0) {
  369. wpa_printf(MSG_INFO, "EAP-GPSK: ID_Server did not match with "
  370. "the one used in GPSK-1");
  371. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1",
  372. data->id_server, data->id_server_len);
  373. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-3",
  374. pos, len);
  375. return NULL;
  376. }
  377. pos += len;
  378. return pos;
  379. }
  380. const u8 * eap_gpsk_validate_csuite(struct eap_gpsk_data *data, const u8 *pos,
  381. const u8 *end)
  382. {
  383. int vendor, specifier;
  384. const struct eap_gpsk_csuite *csuite;
  385. if (pos == NULL)
  386. return NULL;
  387. if (end - pos < (int) sizeof(*csuite)) {
  388. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  389. "CSuite_Sel");
  390. return NULL;
  391. }
  392. csuite = (const struct eap_gpsk_csuite *) pos;
  393. vendor = WPA_GET_BE32(csuite->vendor);
  394. specifier = WPA_GET_BE16(csuite->specifier);
  395. pos += sizeof(*csuite);
  396. if (vendor != data->vendor || specifier != data->specifier) {
  397. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel (%d:%d) does not "
  398. "match with the one sent in GPSK-2 (%d:%d)",
  399. vendor, specifier, data->vendor, data->specifier);
  400. return NULL;
  401. }
  402. return pos;
  403. }
  404. const u8 * eap_gpsk_validate_pd_payload_2(struct eap_gpsk_data *data,
  405. const u8 *pos, const u8 *end)
  406. {
  407. u16 alen;
  408. if (pos == NULL)
  409. return NULL;
  410. if (end - pos < 2) {
  411. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  412. "PD_Payload_2 length");
  413. return NULL;
  414. }
  415. alen = WPA_GET_BE16(pos);
  416. pos += 2;
  417. if (end - pos < alen) {
  418. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  419. "%d-octet PD_Payload_2", alen);
  420. return NULL;
  421. }
  422. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_2", pos, alen);
  423. pos += alen;
  424. return pos;
  425. }
  426. const u8 * eap_gpsk_validate_gpsk_3_mic(struct eap_gpsk_data *data,
  427. const u8 *payload,
  428. const u8 *pos, const u8 *end)
  429. {
  430. size_t miclen;
  431. u8 mic[EAP_GPSK_MAX_MIC_LEN];
  432. if (pos == NULL)
  433. return NULL;
  434. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  435. if (end - pos < (int) miclen) {
  436. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
  437. "(left=%lu miclen=%lu)",
  438. (unsigned long) (end - pos),
  439. (unsigned long) miclen);
  440. return NULL;
  441. }
  442. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  443. data->specifier, payload, pos - payload, mic)
  444. < 0) {
  445. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
  446. return NULL;
  447. }
  448. if (os_memcmp(mic, pos, miclen) != 0) {
  449. wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-3");
  450. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
  451. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
  452. return NULL;
  453. }
  454. pos += miclen;
  455. return pos;
  456. }
  457. static struct wpabuf * eap_gpsk_process_gpsk_3(struct eap_sm *sm,
  458. struct eap_gpsk_data *data,
  459. struct eap_method_ret *ret,
  460. const struct wpabuf *reqData,
  461. const u8 *payload,
  462. size_t payload_len)
  463. {
  464. struct wpabuf *resp;
  465. const u8 *pos, *end;
  466. if (data->state != GPSK_3) {
  467. ret->ignore = TRUE;
  468. return NULL;
  469. }
  470. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-3");
  471. end = payload + payload_len;
  472. pos = eap_gpsk_validate_rand(data, payload, end);
  473. pos = eap_gpsk_validate_id_server(data, pos, end);
  474. pos = eap_gpsk_validate_csuite(data, pos, end);
  475. pos = eap_gpsk_validate_pd_payload_2(data, pos, end);
  476. pos = eap_gpsk_validate_gpsk_3_mic(data, payload, pos, end);
  477. if (pos == NULL) {
  478. eap_gpsk_state(data, FAILURE);
  479. return NULL;
  480. }
  481. if (pos != end) {
  482. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
  483. "data in the end of GPSK-2",
  484. (unsigned long) (end - pos));
  485. }
  486. resp = eap_gpsk_send_gpsk_4(data, eap_get_id(reqData));
  487. if (resp == NULL)
  488. return NULL;
  489. eap_gpsk_state(data, SUCCESS);
  490. ret->methodState = METHOD_DONE;
  491. ret->decision = DECISION_UNCOND_SUCC;
  492. return resp;
  493. }
  494. static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
  495. u8 identifier)
  496. {
  497. struct wpabuf *resp;
  498. u8 *rpos, *start;
  499. size_t mlen;
  500. wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-4");
  501. mlen = eap_gpsk_mic_len(data->vendor, data->specifier);
  502. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, 1 + 2 + mlen,
  503. EAP_CODE_RESPONSE, identifier);
  504. if (resp == NULL)
  505. return NULL;
  506. wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_4);
  507. start = wpabuf_put(resp, 0);
  508. /* No PD_Payload_3 */
  509. wpabuf_put_be16(resp, 0);
  510. rpos = wpabuf_put(resp, mlen);
  511. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  512. data->specifier, start, rpos - start, rpos) <
  513. 0) {
  514. eap_gpsk_state(data, FAILURE);
  515. wpabuf_free(resp);
  516. return NULL;
  517. }
  518. return resp;
  519. }
  520. static struct wpabuf * eap_gpsk_process(struct eap_sm *sm, void *priv,
  521. struct eap_method_ret *ret,
  522. const struct wpabuf *reqData)
  523. {
  524. struct eap_gpsk_data *data = priv;
  525. struct wpabuf *resp;
  526. const u8 *pos;
  527. size_t len;
  528. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqData, &len);
  529. if (pos == NULL || len < 1) {
  530. ret->ignore = TRUE;
  531. return NULL;
  532. }
  533. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode %d", *pos);
  534. ret->ignore = FALSE;
  535. ret->methodState = METHOD_MAY_CONT;
  536. ret->decision = DECISION_FAIL;
  537. ret->allowNotifications = FALSE;
  538. switch (*pos) {
  539. case EAP_GPSK_OPCODE_GPSK_1:
  540. resp = eap_gpsk_process_gpsk_1(sm, data, ret, reqData,
  541. pos + 1, len - 1);
  542. break;
  543. case EAP_GPSK_OPCODE_GPSK_3:
  544. resp = eap_gpsk_process_gpsk_3(sm, data, ret, reqData,
  545. pos + 1, len - 1);
  546. break;
  547. default:
  548. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignoring message with "
  549. "unknown opcode %d", *pos);
  550. ret->ignore = TRUE;
  551. return NULL;
  552. }
  553. return resp;
  554. }
  555. static Boolean eap_gpsk_isKeyAvailable(struct eap_sm *sm, void *priv)
  556. {
  557. struct eap_gpsk_data *data = priv;
  558. return data->state == SUCCESS;
  559. }
  560. static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
  561. {
  562. struct eap_gpsk_data *data = priv;
  563. u8 *key;
  564. if (data->state != SUCCESS)
  565. return NULL;
  566. key = os_malloc(EAP_MSK_LEN);
  567. if (key == NULL)
  568. return NULL;
  569. os_memcpy(key, data->msk, EAP_MSK_LEN);
  570. *len = EAP_MSK_LEN;
  571. return key;
  572. }
  573. static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  574. {
  575. struct eap_gpsk_data *data = priv;
  576. u8 *key;
  577. if (data->state != SUCCESS)
  578. return NULL;
  579. key = os_malloc(EAP_EMSK_LEN);
  580. if (key == NULL)
  581. return NULL;
  582. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  583. *len = EAP_EMSK_LEN;
  584. return key;
  585. }
  586. int eap_peer_gpsk_register(void)
  587. {
  588. struct eap_method *eap;
  589. int ret;
  590. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  591. EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
  592. if (eap == NULL)
  593. return -1;
  594. eap->init = eap_gpsk_init;
  595. eap->deinit = eap_gpsk_deinit;
  596. eap->process = eap_gpsk_process;
  597. eap->isKeyAvailable = eap_gpsk_isKeyAvailable;
  598. eap->getKey = eap_gpsk_getKey;
  599. eap->get_emsk = eap_gpsk_get_emsk;
  600. ret = eap_peer_method_register(eap);
  601. if (ret)
  602. eap_peer_method_free(eap);
  603. return ret;
  604. }