eap_server_gpsk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * hostapd / EAP-GPSK (RFC 5433) server
  3. * Copyright (c) 2006-2007, 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 "crypto/random.h"
  17. #include "eap_server/eap_i.h"
  18. #include "eap_common/eap_gpsk_common.h"
  19. struct eap_gpsk_data {
  20. enum { GPSK_1, GPSK_3, SUCCESS, FAILURE } state;
  21. u8 rand_server[EAP_GPSK_RAND_LEN];
  22. u8 rand_peer[EAP_GPSK_RAND_LEN];
  23. u8 msk[EAP_MSK_LEN];
  24. u8 emsk[EAP_EMSK_LEN];
  25. u8 sk[EAP_GPSK_MAX_SK_LEN];
  26. size_t sk_len;
  27. u8 pk[EAP_GPSK_MAX_PK_LEN];
  28. size_t pk_len;
  29. u8 *id_peer;
  30. size_t id_peer_len;
  31. u8 *id_server;
  32. size_t id_server_len;
  33. #define MAX_NUM_CSUITES 2
  34. struct eap_gpsk_csuite csuite_list[MAX_NUM_CSUITES];
  35. size_t csuite_count;
  36. int vendor; /* CSuite/Vendor */
  37. int specifier; /* CSuite/Specifier */
  38. };
  39. static const char * eap_gpsk_state_txt(int state)
  40. {
  41. switch (state) {
  42. case GPSK_1:
  43. return "GPSK-1";
  44. case GPSK_3:
  45. return "GPSK-3";
  46. case SUCCESS:
  47. return "SUCCESS";
  48. case FAILURE:
  49. return "FAILURE";
  50. default:
  51. return "?";
  52. }
  53. }
  54. static void eap_gpsk_state(struct eap_gpsk_data *data, int state)
  55. {
  56. wpa_printf(MSG_DEBUG, "EAP-GPSK: %s -> %s",
  57. eap_gpsk_state_txt(data->state),
  58. eap_gpsk_state_txt(state));
  59. data->state = state;
  60. }
  61. static void * eap_gpsk_init(struct eap_sm *sm)
  62. {
  63. struct eap_gpsk_data *data;
  64. data = os_zalloc(sizeof(*data));
  65. if (data == NULL)
  66. return NULL;
  67. data->state = GPSK_1;
  68. /* TODO: add support for configuring ID_Server */
  69. data->id_server = (u8 *) os_strdup("hostapd");
  70. if (data->id_server)
  71. data->id_server_len = os_strlen((char *) data->id_server);
  72. data->csuite_count = 0;
  73. if (eap_gpsk_supported_ciphersuite(EAP_GPSK_VENDOR_IETF,
  74. EAP_GPSK_CIPHER_AES)) {
  75. WPA_PUT_BE32(data->csuite_list[data->csuite_count].vendor,
  76. EAP_GPSK_VENDOR_IETF);
  77. WPA_PUT_BE16(data->csuite_list[data->csuite_count].specifier,
  78. EAP_GPSK_CIPHER_AES);
  79. data->csuite_count++;
  80. }
  81. if (eap_gpsk_supported_ciphersuite(EAP_GPSK_VENDOR_IETF,
  82. EAP_GPSK_CIPHER_SHA256)) {
  83. WPA_PUT_BE32(data->csuite_list[data->csuite_count].vendor,
  84. EAP_GPSK_VENDOR_IETF);
  85. WPA_PUT_BE16(data->csuite_list[data->csuite_count].specifier,
  86. EAP_GPSK_CIPHER_SHA256);
  87. data->csuite_count++;
  88. }
  89. return data;
  90. }
  91. static void eap_gpsk_reset(struct eap_sm *sm, void *priv)
  92. {
  93. struct eap_gpsk_data *data = priv;
  94. os_free(data->id_server);
  95. os_free(data->id_peer);
  96. os_free(data);
  97. }
  98. static struct wpabuf * eap_gpsk_build_gpsk_1(struct eap_sm *sm,
  99. struct eap_gpsk_data *data, u8 id)
  100. {
  101. size_t len;
  102. struct wpabuf *req;
  103. wpa_printf(MSG_DEBUG, "EAP-GPSK: Request/GPSK-1");
  104. if (random_get_bytes(data->rand_server, EAP_GPSK_RAND_LEN)) {
  105. wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to get random data");
  106. eap_gpsk_state(data, FAILURE);
  107. return NULL;
  108. }
  109. wpa_hexdump(MSG_MSGDUMP, "EAP-GPSK: RAND_Server",
  110. data->rand_server, EAP_GPSK_RAND_LEN);
  111. len = 1 + 2 + data->id_server_len + EAP_GPSK_RAND_LEN + 2 +
  112. data->csuite_count * sizeof(struct eap_gpsk_csuite);
  113. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
  114. EAP_CODE_REQUEST, id);
  115. if (req == NULL) {
  116. wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to allocate memory "
  117. "for request/GPSK-1");
  118. eap_gpsk_state(data, FAILURE);
  119. return NULL;
  120. }
  121. wpabuf_put_u8(req, EAP_GPSK_OPCODE_GPSK_1);
  122. wpabuf_put_be16(req, data->id_server_len);
  123. wpabuf_put_data(req, data->id_server, data->id_server_len);
  124. wpabuf_put_data(req, data->rand_server, EAP_GPSK_RAND_LEN);
  125. wpabuf_put_be16(req,
  126. data->csuite_count * sizeof(struct eap_gpsk_csuite));
  127. wpabuf_put_data(req, data->csuite_list,
  128. data->csuite_count * sizeof(struct eap_gpsk_csuite));
  129. return req;
  130. }
  131. static struct wpabuf * eap_gpsk_build_gpsk_3(struct eap_sm *sm,
  132. struct eap_gpsk_data *data, u8 id)
  133. {
  134. u8 *pos, *start;
  135. size_t len, miclen;
  136. struct eap_gpsk_csuite *csuite;
  137. struct wpabuf *req;
  138. wpa_printf(MSG_DEBUG, "EAP-GPSK: Request/GPSK-3");
  139. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  140. len = 1 + 2 * EAP_GPSK_RAND_LEN + 2 + data->id_server_len +
  141. sizeof(struct eap_gpsk_csuite) + 2 + miclen;
  142. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
  143. EAP_CODE_REQUEST, id);
  144. if (req == NULL) {
  145. wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to allocate memory "
  146. "for request/GPSK-3");
  147. eap_gpsk_state(data, FAILURE);
  148. return NULL;
  149. }
  150. wpabuf_put_u8(req, EAP_GPSK_OPCODE_GPSK_3);
  151. start = wpabuf_put(req, 0);
  152. wpabuf_put_data(req, data->rand_peer, EAP_GPSK_RAND_LEN);
  153. wpabuf_put_data(req, data->rand_server, EAP_GPSK_RAND_LEN);
  154. wpabuf_put_be16(req, data->id_server_len);
  155. wpabuf_put_data(req, data->id_server, data->id_server_len);
  156. csuite = wpabuf_put(req, sizeof(*csuite));
  157. WPA_PUT_BE32(csuite->vendor, data->vendor);
  158. WPA_PUT_BE16(csuite->specifier, data->specifier);
  159. /* no PD_Payload_2 */
  160. wpabuf_put_be16(req, 0);
  161. pos = wpabuf_put(req, miclen);
  162. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  163. data->specifier, start, pos - start, pos) < 0)
  164. {
  165. os_free(req);
  166. eap_gpsk_state(data, FAILURE);
  167. return NULL;
  168. }
  169. return req;
  170. }
  171. static struct wpabuf * eap_gpsk_buildReq(struct eap_sm *sm, void *priv, u8 id)
  172. {
  173. struct eap_gpsk_data *data = priv;
  174. switch (data->state) {
  175. case GPSK_1:
  176. return eap_gpsk_build_gpsk_1(sm, data, id);
  177. case GPSK_3:
  178. return eap_gpsk_build_gpsk_3(sm, data, id);
  179. default:
  180. wpa_printf(MSG_DEBUG, "EAP-GPSK: Unknown state %d in buildReq",
  181. data->state);
  182. break;
  183. }
  184. return NULL;
  185. }
  186. static Boolean eap_gpsk_check(struct eap_sm *sm, void *priv,
  187. struct wpabuf *respData)
  188. {
  189. struct eap_gpsk_data *data = priv;
  190. const u8 *pos;
  191. size_t len;
  192. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, respData, &len);
  193. if (pos == NULL || len < 1) {
  194. wpa_printf(MSG_INFO, "EAP-GPSK: Invalid frame");
  195. return TRUE;
  196. }
  197. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode=%d", *pos);
  198. if (data->state == GPSK_1 && *pos == EAP_GPSK_OPCODE_GPSK_2)
  199. return FALSE;
  200. if (data->state == GPSK_3 && *pos == EAP_GPSK_OPCODE_GPSK_4)
  201. return FALSE;
  202. wpa_printf(MSG_INFO, "EAP-GPSK: Unexpected opcode=%d in state=%d",
  203. *pos, data->state);
  204. return TRUE;
  205. }
  206. static void eap_gpsk_process_gpsk_2(struct eap_sm *sm,
  207. struct eap_gpsk_data *data,
  208. const u8 *payload, size_t payloadlen)
  209. {
  210. const u8 *pos, *end;
  211. u16 alen;
  212. const struct eap_gpsk_csuite *csuite;
  213. size_t i, miclen;
  214. u8 mic[EAP_GPSK_MAX_MIC_LEN];
  215. if (data->state != GPSK_1)
  216. return;
  217. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Response/GPSK-2");
  218. pos = payload;
  219. end = payload + payloadlen;
  220. if (end - pos < 2) {
  221. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  222. "ID_Peer length");
  223. eap_gpsk_state(data, FAILURE);
  224. return;
  225. }
  226. alen = WPA_GET_BE16(pos);
  227. pos += 2;
  228. if (end - pos < alen) {
  229. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  230. "ID_Peer");
  231. eap_gpsk_state(data, FAILURE);
  232. return;
  233. }
  234. os_free(data->id_peer);
  235. data->id_peer = os_malloc(alen);
  236. if (data->id_peer == NULL) {
  237. wpa_printf(MSG_DEBUG, "EAP-GPSK: Not enough memory to store "
  238. "%d-octet ID_Peer", alen);
  239. return;
  240. }
  241. os_memcpy(data->id_peer, pos, alen);
  242. data->id_peer_len = alen;
  243. wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
  244. data->id_peer, data->id_peer_len);
  245. pos += alen;
  246. if (end - pos < 2) {
  247. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  248. "ID_Server length");
  249. eap_gpsk_state(data, FAILURE);
  250. return;
  251. }
  252. alen = WPA_GET_BE16(pos);
  253. pos += 2;
  254. if (end - pos < alen) {
  255. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  256. "ID_Server");
  257. eap_gpsk_state(data, FAILURE);
  258. return;
  259. }
  260. if (alen != data->id_server_len ||
  261. os_memcmp(pos, data->id_server, alen) != 0) {
  262. wpa_printf(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1 and "
  263. "GPSK-2 did not match");
  264. eap_gpsk_state(data, FAILURE);
  265. return;
  266. }
  267. pos += alen;
  268. if (end - pos < EAP_GPSK_RAND_LEN) {
  269. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  270. "RAND_Peer");
  271. eap_gpsk_state(data, FAILURE);
  272. return;
  273. }
  274. os_memcpy(data->rand_peer, pos, EAP_GPSK_RAND_LEN);
  275. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
  276. data->rand_peer, EAP_GPSK_RAND_LEN);
  277. pos += EAP_GPSK_RAND_LEN;
  278. if (end - pos < EAP_GPSK_RAND_LEN) {
  279. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  280. "RAND_Server");
  281. eap_gpsk_state(data, FAILURE);
  282. return;
  283. }
  284. if (os_memcmp(data->rand_server, pos, EAP_GPSK_RAND_LEN) != 0) {
  285. wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
  286. "GPSK-2 did not match");
  287. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
  288. data->rand_server, EAP_GPSK_RAND_LEN);
  289. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-2",
  290. pos, EAP_GPSK_RAND_LEN);
  291. eap_gpsk_state(data, FAILURE);
  292. return;
  293. }
  294. pos += EAP_GPSK_RAND_LEN;
  295. if (end - pos < 2) {
  296. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  297. "CSuite_List length");
  298. eap_gpsk_state(data, FAILURE);
  299. return;
  300. }
  301. alen = WPA_GET_BE16(pos);
  302. pos += 2;
  303. if (end - pos < alen) {
  304. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  305. "CSuite_List");
  306. eap_gpsk_state(data, FAILURE);
  307. return;
  308. }
  309. if (alen != data->csuite_count * sizeof(struct eap_gpsk_csuite) ||
  310. os_memcmp(pos, data->csuite_list, alen) != 0) {
  311. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List in GPSK-1 and "
  312. "GPSK-2 did not match");
  313. eap_gpsk_state(data, FAILURE);
  314. return;
  315. }
  316. pos += alen;
  317. if (end - pos < (int) sizeof(*csuite)) {
  318. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  319. "CSuite_Sel");
  320. eap_gpsk_state(data, FAILURE);
  321. return;
  322. }
  323. csuite = (const struct eap_gpsk_csuite *) pos;
  324. for (i = 0; i < data->csuite_count; i++) {
  325. if (os_memcmp(csuite, &data->csuite_list[i], sizeof(*csuite))
  326. == 0)
  327. break;
  328. }
  329. if (i == data->csuite_count) {
  330. wpa_printf(MSG_DEBUG, "EAP-GPSK: Peer selected unsupported "
  331. "ciphersuite %d:%d",
  332. WPA_GET_BE32(csuite->vendor),
  333. WPA_GET_BE16(csuite->specifier));
  334. eap_gpsk_state(data, FAILURE);
  335. return;
  336. }
  337. data->vendor = WPA_GET_BE32(csuite->vendor);
  338. data->specifier = WPA_GET_BE16(csuite->specifier);
  339. wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel %d:%d",
  340. data->vendor, data->specifier);
  341. pos += sizeof(*csuite);
  342. if (end - pos < 2) {
  343. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  344. "PD_Payload_1 length");
  345. eap_gpsk_state(data, FAILURE);
  346. return;
  347. }
  348. alen = WPA_GET_BE16(pos);
  349. pos += 2;
  350. if (end - pos < alen) {
  351. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  352. "PD_Payload_1");
  353. eap_gpsk_state(data, FAILURE);
  354. return;
  355. }
  356. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_1", pos, alen);
  357. pos += alen;
  358. if (sm->user == NULL || sm->user->password == NULL) {
  359. wpa_printf(MSG_INFO, "EAP-GPSK: No PSK/password configured "
  360. "for the user");
  361. eap_gpsk_state(data, FAILURE);
  362. return;
  363. }
  364. if (eap_gpsk_derive_keys(sm->user->password, sm->user->password_len,
  365. data->vendor, data->specifier,
  366. data->rand_peer, data->rand_server,
  367. data->id_peer, data->id_peer_len,
  368. data->id_server, data->id_server_len,
  369. data->msk, data->emsk,
  370. data->sk, &data->sk_len,
  371. data->pk, &data->pk_len) < 0) {
  372. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
  373. eap_gpsk_state(data, FAILURE);
  374. return;
  375. }
  376. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  377. if (end - pos < (int) miclen) {
  378. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
  379. "(left=%lu miclen=%lu)",
  380. (unsigned long) (end - pos),
  381. (unsigned long) miclen);
  382. eap_gpsk_state(data, FAILURE);
  383. return;
  384. }
  385. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  386. data->specifier, payload, pos - payload, mic)
  387. < 0) {
  388. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
  389. eap_gpsk_state(data, FAILURE);
  390. return;
  391. }
  392. if (os_memcmp(mic, pos, miclen) != 0) {
  393. wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-2");
  394. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
  395. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
  396. eap_gpsk_state(data, FAILURE);
  397. return;
  398. }
  399. pos += miclen;
  400. if (pos != end) {
  401. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
  402. "data in the end of GPSK-2",
  403. (unsigned long) (end - pos));
  404. }
  405. eap_gpsk_state(data, GPSK_3);
  406. }
  407. static void eap_gpsk_process_gpsk_4(struct eap_sm *sm,
  408. struct eap_gpsk_data *data,
  409. const u8 *payload, size_t payloadlen)
  410. {
  411. const u8 *pos, *end;
  412. u16 alen;
  413. size_t miclen;
  414. u8 mic[EAP_GPSK_MAX_MIC_LEN];
  415. if (data->state != GPSK_3)
  416. return;
  417. wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Response/GPSK-4");
  418. pos = payload;
  419. end = payload + payloadlen;
  420. if (end - pos < 2) {
  421. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  422. "PD_Payload_1 length");
  423. eap_gpsk_state(data, FAILURE);
  424. return;
  425. }
  426. alen = WPA_GET_BE16(pos);
  427. pos += 2;
  428. if (end - pos < alen) {
  429. wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
  430. "PD_Payload_1");
  431. eap_gpsk_state(data, FAILURE);
  432. return;
  433. }
  434. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_1", pos, alen);
  435. pos += alen;
  436. miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
  437. if (end - pos < (int) miclen) {
  438. wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
  439. "(left=%lu miclen=%lu)",
  440. (unsigned long) (end - pos),
  441. (unsigned long) miclen);
  442. eap_gpsk_state(data, FAILURE);
  443. return;
  444. }
  445. if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
  446. data->specifier, payload, pos - payload, mic)
  447. < 0) {
  448. wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
  449. eap_gpsk_state(data, FAILURE);
  450. return;
  451. }
  452. if (os_memcmp(mic, pos, miclen) != 0) {
  453. wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-4");
  454. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
  455. wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
  456. eap_gpsk_state(data, FAILURE);
  457. return;
  458. }
  459. pos += miclen;
  460. if (pos != end) {
  461. wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
  462. "data in the end of GPSK-4",
  463. (unsigned long) (end - pos));
  464. }
  465. eap_gpsk_state(data, SUCCESS);
  466. }
  467. static void eap_gpsk_process(struct eap_sm *sm, void *priv,
  468. struct wpabuf *respData)
  469. {
  470. struct eap_gpsk_data *data = priv;
  471. const u8 *pos;
  472. size_t len;
  473. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, respData, &len);
  474. if (pos == NULL || len < 1)
  475. return;
  476. switch (*pos) {
  477. case EAP_GPSK_OPCODE_GPSK_2:
  478. eap_gpsk_process_gpsk_2(sm, data, pos + 1, len - 1);
  479. break;
  480. case EAP_GPSK_OPCODE_GPSK_4:
  481. eap_gpsk_process_gpsk_4(sm, data, pos + 1, len - 1);
  482. break;
  483. }
  484. }
  485. static Boolean eap_gpsk_isDone(struct eap_sm *sm, void *priv)
  486. {
  487. struct eap_gpsk_data *data = priv;
  488. return data->state == SUCCESS || data->state == FAILURE;
  489. }
  490. static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
  491. {
  492. struct eap_gpsk_data *data = priv;
  493. u8 *key;
  494. if (data->state != SUCCESS)
  495. return NULL;
  496. key = os_malloc(EAP_MSK_LEN);
  497. if (key == NULL)
  498. return NULL;
  499. os_memcpy(key, data->msk, EAP_MSK_LEN);
  500. *len = EAP_MSK_LEN;
  501. return key;
  502. }
  503. static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  504. {
  505. struct eap_gpsk_data *data = priv;
  506. u8 *key;
  507. if (data->state != SUCCESS)
  508. return NULL;
  509. key = os_malloc(EAP_EMSK_LEN);
  510. if (key == NULL)
  511. return NULL;
  512. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  513. *len = EAP_EMSK_LEN;
  514. return key;
  515. }
  516. static Boolean eap_gpsk_isSuccess(struct eap_sm *sm, void *priv)
  517. {
  518. struct eap_gpsk_data *data = priv;
  519. return data->state == SUCCESS;
  520. }
  521. int eap_server_gpsk_register(void)
  522. {
  523. struct eap_method *eap;
  524. int ret;
  525. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  526. EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
  527. if (eap == NULL)
  528. return -1;
  529. eap->init = eap_gpsk_init;
  530. eap->reset = eap_gpsk_reset;
  531. eap->buildReq = eap_gpsk_buildReq;
  532. eap->check = eap_gpsk_check;
  533. eap->process = eap_gpsk_process;
  534. eap->isDone = eap_gpsk_isDone;
  535. eap->getKey = eap_gpsk_getKey;
  536. eap->isSuccess = eap_gpsk_isSuccess;
  537. eap->get_emsk = eap_gpsk_get_emsk;
  538. ret = eap_server_method_register(eap);
  539. if (ret)
  540. eap_server_method_free(eap);
  541. return ret;
  542. }