eap_gpsk.c 19 KB

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