eap_gpsk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. static 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. static 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. static const u8 * eap_gpsk_process_csuite_list(struct eap_sm *sm,
  189. struct eap_gpsk_data *data,
  190. const u8 **list,
  191. size_t *list_len,
  192. const u8 *pos, const u8 *end)
  193. {
  194. if (pos == NULL)
  195. return NULL;
  196. if (end - pos < 2) {
  197. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
  198. return NULL;
  199. }
  200. *list_len = WPA_GET_BE16(pos);
  201. pos += 2;
  202. if (end - pos < (int) *list_len) {
  203. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List overflow");
  204. return NULL;
  205. }
  206. if (*list_len == 0 || (*list_len % sizeof(struct eap_gpsk_csuite))) {
  207. wpa_printf(MSG_DEBUG, "EAP-GPSK: Invalid CSuite_List len %lu",
  208. (unsigned long) *list_len);
  209. return NULL;
  210. }
  211. *list = pos;
  212. pos += *list_len;
  213. if (eap_gpsk_select_csuite(sm, data, *list, *list_len) < 0)
  214. return NULL;
  215. return pos;
  216. }
  217. static struct wpabuf * eap_gpsk_process_gpsk_1(struct eap_sm *sm,
  218. struct eap_gpsk_data *data,
  219. struct eap_method_ret *ret,
  220. const struct wpabuf *reqData,
  221. const u8 *payload,
  222. size_t payload_len)
  223. {
  224. size_t csuite_list_len;
  225. const u8 *csuite_list, *pos, *end;
  226. struct wpabuf *resp;
  227. if (data->state != GPSK_1) {
  228. ret->ignore = TRUE;
  229. return NULL;
  230. }
  231. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-1");
  232. end = payload + payload_len;
  233. pos = eap_gpsk_process_id_server(data, payload, end);
  234. pos = eap_gpsk_process_rand_server(data, pos, end);
  235. pos = eap_gpsk_process_csuite_list(sm, data, &csuite_list,
  236. &csuite_list_len, pos, end);
  237. if (pos == NULL) {
  238. eap_gpsk_state(data, FAILURE);
  239. return NULL;
  240. }
  241. resp = eap_gpsk_send_gpsk_2(data, eap_get_id(reqData),
  242. csuite_list, csuite_list_len);
  243. if (resp == NULL)
  244. return NULL;
  245. eap_gpsk_state(data, GPSK_3);
  246. return resp;
  247. }
  248. static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
  249. u8 identifier,
  250. const u8 *csuite_list,
  251. size_t csuite_list_len)
  252. {
  253. struct wpabuf *resp;
  254. size_t len, miclen;
  255. u8 *rpos, *start;
  256. struct eap_gpsk_csuite *csuite;
  257. wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-2");
  258. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  259. len = 1 + 2 + data->id_peer_len + 2 + data->id_server_len +
  260. 2 * EAP_GPSK_RAND_LEN + 2 + csuite_list_len +
  261. sizeof(struct eap_gpsk_csuite) + 2 + miclen;
  262. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
  263. EAP_CODE_RESPONSE, identifier);
  264. if (resp == NULL)
  265. return NULL;
  266. wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_2);
  267. start = wpabuf_put(resp, 0);
  268. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
  269. data->id_peer, data->id_peer_len);
  270. wpabuf_put_be16(resp, data->id_peer_len);
  271. wpabuf_put_data(resp, data->id_peer, data->id_peer_len);
  272. wpabuf_put_be16(resp, data->id_server_len);
  273. wpabuf_put_data(resp, data->id_server, data->id_server_len);
  274. if (os_get_random(data->rand_peer, EAP_GPSK_RAND_LEN)) {
  275. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to get random data "
  276. "for RAND_Peer");
  277. eap_gpsk_state(data, FAILURE);
  278. wpabuf_free(resp);
  279. return NULL;
  280. }
  281. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
  282. data->rand_peer, EAP_GPSK_RAND_LEN);
  283. wpabuf_put_data(resp, data->rand_peer, EAP_GPSK_RAND_LEN);
  284. wpabuf_put_data(resp, data->rand_server, EAP_GPSK_RAND_LEN);
  285. wpabuf_put_be16(resp, csuite_list_len);
  286. wpabuf_put_data(resp, csuite_list, csuite_list_len);
  287. csuite = wpabuf_put(resp, sizeof(*csuite));
  288. WPA_PUT_BE32(csuite->vendor, data->vendor);
  289. WPA_PUT_BE16(csuite->specifier, data->specifier);
  290. if (eap_gpsk_derive_keys(data->psk, data->psk_len,
  291. data->vendor, data->specifier,
  292. data->rand_peer, data->rand_server,
  293. data->id_peer, data->id_peer_len,
  294. data->id_server, data->id_server_len,
  295. data->msk, data->emsk,
  296. data->sk, &data->sk_len,
  297. data->pk, &data->pk_len) < 0) {
  298. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
  299. eap_gpsk_state(data, FAILURE);
  300. wpabuf_free(resp);
  301. return NULL;
  302. }
  303. /* No PD_Payload_1 */
  304. wpabuf_put_be16(resp, 0);
  305. rpos = wpabuf_put(resp, miclen);
  306. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  307. data->specifier, start, rpos - start, rpos) <
  308. 0) {
  309. eap_gpsk_state(data, FAILURE);
  310. wpabuf_free(resp);
  311. return NULL;
  312. }
  313. return resp;
  314. }
  315. static const u8 * eap_gpsk_validate_rand(struct eap_gpsk_data *data,
  316. const u8 *pos, const u8 *end)
  317. {
  318. if (end - pos < EAP_GPSK_RAND_LEN) {
  319. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  320. "RAND_Peer");
  321. return NULL;
  322. }
  323. if (os_memcmp(pos, data->rand_peer, EAP_GPSK_RAND_LEN) != 0) {
  324. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2 and "
  325. "GPSK-3 did not match");
  326. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2",
  327. data->rand_peer, EAP_GPSK_RAND_LEN);
  328. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-3",
  329. pos, EAP_GPSK_RAND_LEN);
  330. return NULL;
  331. }
  332. pos += EAP_GPSK_RAND_LEN;
  333. if (end - pos < EAP_GPSK_RAND_LEN) {
  334. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  335. "RAND_Server");
  336. return NULL;
  337. }
  338. if (os_memcmp(pos, data->rand_server, EAP_GPSK_RAND_LEN) != 0) {
  339. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
  340. "GPSK-3 did not match");
  341. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
  342. data->rand_server, EAP_GPSK_RAND_LEN);
  343. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-3",
  344. pos, EAP_GPSK_RAND_LEN);
  345. return NULL;
  346. }
  347. pos += EAP_GPSK_RAND_LEN;
  348. return pos;
  349. }
  350. static const u8 * eap_gpsk_validate_id_server(struct eap_gpsk_data *data,
  351. const u8 *pos, const u8 *end)
  352. {
  353. size_t len;
  354. if (pos == NULL)
  355. return NULL;
  356. if (end - pos < (int) 2) {
  357. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  358. "length(ID_Server)");
  359. return NULL;
  360. }
  361. len = WPA_GET_BE16(pos);
  362. pos += 2;
  363. if (end - pos < (int) len) {
  364. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  365. "ID_Server");
  366. return NULL;
  367. }
  368. if (len != data->id_server_len ||
  369. os_memcmp(pos, data->id_server, len) != 0) {
  370. wpa_printf(MSG_INFO, "EAP-GPSK: ID_Server did not match with "
  371. "the one used in GPSK-1");
  372. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1",
  373. data->id_server, data->id_server_len);
  374. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-3",
  375. pos, len);
  376. return NULL;
  377. }
  378. pos += len;
  379. return pos;
  380. }
  381. static const u8 * eap_gpsk_validate_csuite(struct eap_gpsk_data *data,
  382. const u8 *pos, const u8 *end)
  383. {
  384. int vendor, specifier;
  385. const struct eap_gpsk_csuite *csuite;
  386. if (pos == NULL)
  387. return NULL;
  388. if (end - pos < (int) sizeof(*csuite)) {
  389. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  390. "CSuite_Sel");
  391. return NULL;
  392. }
  393. csuite = (const struct eap_gpsk_csuite *) pos;
  394. vendor = WPA_GET_BE32(csuite->vendor);
  395. specifier = WPA_GET_BE16(csuite->specifier);
  396. pos += sizeof(*csuite);
  397. if (vendor != data->vendor || specifier != data->specifier) {
  398. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel (%d:%d) does not "
  399. "match with the one sent in GPSK-2 (%d:%d)",
  400. vendor, specifier, data->vendor, data->specifier);
  401. return NULL;
  402. }
  403. return pos;
  404. }
  405. static const u8 * eap_gpsk_validate_pd_payload_2(struct eap_gpsk_data *data,
  406. const u8 *pos, const u8 *end)
  407. {
  408. u16 alen;
  409. if (pos == NULL)
  410. return NULL;
  411. if (end - pos < 2) {
  412. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  413. "PD_Payload_2 length");
  414. return NULL;
  415. }
  416. alen = WPA_GET_BE16(pos);
  417. pos += 2;
  418. if (end - pos < alen) {
  419. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
  420. "%d-octet PD_Payload_2", alen);
  421. return NULL;
  422. }
  423. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_2", pos, alen);
  424. pos += alen;
  425. return pos;
  426. }
  427. static const u8 * eap_gpsk_validate_gpsk_3_mic(struct eap_gpsk_data *data,
  428. const u8 *payload,
  429. const u8 *pos, const u8 *end)
  430. {
  431. size_t miclen;
  432. u8 mic[EAP_GPSK_MAX_MIC_LEN];
  433. if (pos == NULL)
  434. return NULL;
  435. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  436. if (end - pos < (int) miclen) {
  437. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
  438. "(left=%lu miclen=%lu)",
  439. (unsigned long) (end - pos),
  440. (unsigned long) miclen);
  441. return NULL;
  442. }
  443. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  444. data->specifier, payload, pos - payload, mic)
  445. < 0) {
  446. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
  447. return NULL;
  448. }
  449. if (os_memcmp(mic, pos, miclen) != 0) {
  450. wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-3");
  451. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
  452. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
  453. return NULL;
  454. }
  455. pos += miclen;
  456. return pos;
  457. }
  458. static struct wpabuf * eap_gpsk_process_gpsk_3(struct eap_sm *sm,
  459. struct eap_gpsk_data *data,
  460. struct eap_method_ret *ret,
  461. const struct wpabuf *reqData,
  462. const u8 *payload,
  463. size_t payload_len)
  464. {
  465. struct wpabuf *resp;
  466. const u8 *pos, *end;
  467. if (data->state != GPSK_3) {
  468. ret->ignore = TRUE;
  469. return NULL;
  470. }
  471. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-3");
  472. end = payload + payload_len;
  473. pos = eap_gpsk_validate_rand(data, payload, end);
  474. pos = eap_gpsk_validate_id_server(data, pos, end);
  475. pos = eap_gpsk_validate_csuite(data, pos, end);
  476. pos = eap_gpsk_validate_pd_payload_2(data, pos, end);
  477. pos = eap_gpsk_validate_gpsk_3_mic(data, payload, pos, end);
  478. if (pos == NULL) {
  479. eap_gpsk_state(data, FAILURE);
  480. return NULL;
  481. }
  482. if (pos != end) {
  483. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
  484. "data in the end of GPSK-2",
  485. (unsigned long) (end - pos));
  486. }
  487. resp = eap_gpsk_send_gpsk_4(data, eap_get_id(reqData));
  488. if (resp == NULL)
  489. return NULL;
  490. eap_gpsk_state(data, SUCCESS);
  491. ret->methodState = METHOD_DONE;
  492. ret->decision = DECISION_UNCOND_SUCC;
  493. return resp;
  494. }
  495. static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
  496. u8 identifier)
  497. {
  498. struct wpabuf *resp;
  499. u8 *rpos, *start;
  500. size_t mlen;
  501. wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-4");
  502. mlen = eap_gpsk_mic_len(data->vendor, data->specifier);
  503. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, 1 + 2 + mlen,
  504. EAP_CODE_RESPONSE, identifier);
  505. if (resp == NULL)
  506. return NULL;
  507. wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_4);
  508. start = wpabuf_put(resp, 0);
  509. /* No PD_Payload_3 */
  510. wpabuf_put_be16(resp, 0);
  511. rpos = wpabuf_put(resp, mlen);
  512. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  513. data->specifier, start, rpos - start, rpos) <
  514. 0) {
  515. eap_gpsk_state(data, FAILURE);
  516. wpabuf_free(resp);
  517. return NULL;
  518. }
  519. return resp;
  520. }
  521. static struct wpabuf * eap_gpsk_process(struct eap_sm *sm, void *priv,
  522. struct eap_method_ret *ret,
  523. const struct wpabuf *reqData)
  524. {
  525. struct eap_gpsk_data *data = priv;
  526. struct wpabuf *resp;
  527. const u8 *pos;
  528. size_t len;
  529. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqData, &len);
  530. if (pos == NULL || len < 1) {
  531. ret->ignore = TRUE;
  532. return NULL;
  533. }
  534. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode %d", *pos);
  535. ret->ignore = FALSE;
  536. ret->methodState = METHOD_MAY_CONT;
  537. ret->decision = DECISION_FAIL;
  538. ret->allowNotifications = FALSE;
  539. switch (*pos) {
  540. case EAP_GPSK_OPCODE_GPSK_1:
  541. resp = eap_gpsk_process_gpsk_1(sm, data, ret, reqData,
  542. pos + 1, len - 1);
  543. break;
  544. case EAP_GPSK_OPCODE_GPSK_3:
  545. resp = eap_gpsk_process_gpsk_3(sm, data, ret, reqData,
  546. pos + 1, len - 1);
  547. break;
  548. default:
  549. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignoring message with "
  550. "unknown opcode %d", *pos);
  551. ret->ignore = TRUE;
  552. return NULL;
  553. }
  554. return resp;
  555. }
  556. static Boolean eap_gpsk_isKeyAvailable(struct eap_sm *sm, void *priv)
  557. {
  558. struct eap_gpsk_data *data = priv;
  559. return data->state == SUCCESS;
  560. }
  561. static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
  562. {
  563. struct eap_gpsk_data *data = priv;
  564. u8 *key;
  565. if (data->state != SUCCESS)
  566. return NULL;
  567. key = os_malloc(EAP_MSK_LEN);
  568. if (key == NULL)
  569. return NULL;
  570. os_memcpy(key, data->msk, EAP_MSK_LEN);
  571. *len = EAP_MSK_LEN;
  572. return key;
  573. }
  574. static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  575. {
  576. struct eap_gpsk_data *data = priv;
  577. u8 *key;
  578. if (data->state != SUCCESS)
  579. return NULL;
  580. key = os_malloc(EAP_EMSK_LEN);
  581. if (key == NULL)
  582. return NULL;
  583. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  584. *len = EAP_EMSK_LEN;
  585. return key;
  586. }
  587. int eap_peer_gpsk_register(void)
  588. {
  589. struct eap_method *eap;
  590. int ret;
  591. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  592. EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
  593. if (eap == NULL)
  594. return -1;
  595. eap->init = eap_gpsk_init;
  596. eap->deinit = eap_gpsk_deinit;
  597. eap->process = eap_gpsk_process;
  598. eap->isKeyAvailable = eap_gpsk_isKeyAvailable;
  599. eap->getKey = eap_gpsk_getKey;
  600. eap->get_emsk = eap_gpsk_get_emsk;
  601. ret = eap_peer_method_register(eap);
  602. if (ret)
  603. eap_peer_method_free(eap);
  604. return ret;
  605. }