eap_pax.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * EAP peer method: EAP-PAX (RFC 4746)
  3. * Copyright (c) 2005-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_pax_common.h"
  18. #include "sha1.h"
  19. #include "crypto.h"
  20. /*
  21. * Note: only PAX_STD subprotocol is currently supported
  22. *
  23. * TODO: Add support with PAX_SEC with the mandatory to implement ciphersuite
  24. * (HMAC_SHA1_128, IANA DH Group 14 (2048 bits), RSA-PKCS1-V1_5) and
  25. * recommended ciphersuite (HMAC_SHA256_128, IANA DH Group 15 (3072 bits),
  26. * RSAES-OAEP).
  27. */
  28. struct eap_pax_data {
  29. enum { PAX_INIT, PAX_STD_2_SENT, PAX_DONE } state;
  30. u8 mac_id, dh_group_id, public_key_id;
  31. union {
  32. u8 e[2 * EAP_PAX_RAND_LEN];
  33. struct {
  34. u8 x[EAP_PAX_RAND_LEN]; /* server rand */
  35. u8 y[EAP_PAX_RAND_LEN]; /* client rand */
  36. } r;
  37. } rand;
  38. char *cid;
  39. size_t cid_len;
  40. u8 ak[EAP_PAX_AK_LEN];
  41. u8 mk[EAP_PAX_MK_LEN];
  42. u8 ck[EAP_PAX_CK_LEN];
  43. u8 ick[EAP_PAX_ICK_LEN];
  44. };
  45. static void eap_pax_deinit(struct eap_sm *sm, void *priv);
  46. static void * eap_pax_init(struct eap_sm *sm)
  47. {
  48. struct eap_pax_data *data;
  49. const u8 *identity, *password;
  50. size_t identity_len, password_len;
  51. identity = eap_get_config_identity(sm, &identity_len);
  52. password = eap_get_config_password(sm, &password_len);
  53. if (!identity || !password) {
  54. wpa_printf(MSG_INFO, "EAP-PAX: CID (nai) or key (password) "
  55. "not configured");
  56. return NULL;
  57. }
  58. if (password_len != EAP_PAX_AK_LEN) {
  59. wpa_printf(MSG_INFO, "EAP-PAX: Invalid PSK length");
  60. return NULL;
  61. }
  62. data = os_zalloc(sizeof(*data));
  63. if (data == NULL)
  64. return NULL;
  65. data->state = PAX_INIT;
  66. data->cid = os_malloc(identity_len);
  67. if (data->cid == NULL) {
  68. eap_pax_deinit(sm, data);
  69. return NULL;
  70. }
  71. os_memcpy(data->cid, identity, identity_len);
  72. data->cid_len = identity_len;
  73. os_memcpy(data->ak, password, EAP_PAX_AK_LEN);
  74. return data;
  75. }
  76. static void eap_pax_deinit(struct eap_sm *sm, void *priv)
  77. {
  78. struct eap_pax_data *data = priv;
  79. os_free(data->cid);
  80. os_free(data);
  81. }
  82. static struct wpabuf * eap_pax_alloc_resp(const struct eap_pax_hdr *req,
  83. u8 id, u8 op_code, size_t plen)
  84. {
  85. struct wpabuf *resp;
  86. struct eap_pax_hdr *pax;
  87. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PAX,
  88. sizeof(*pax) + plen, EAP_CODE_RESPONSE, id);
  89. if (resp == NULL)
  90. return NULL;
  91. pax = wpabuf_put(resp, sizeof(*pax));
  92. pax->op_code = op_code;
  93. pax->flags = 0;
  94. pax->mac_id = req->mac_id;
  95. pax->dh_group_id = req->dh_group_id;
  96. pax->public_key_id = req->public_key_id;
  97. return resp;
  98. }
  99. static struct wpabuf * eap_pax_process_std_1(struct eap_pax_data *data,
  100. struct eap_method_ret *ret, u8 id,
  101. const struct eap_pax_hdr *req,
  102. size_t req_plen)
  103. {
  104. struct wpabuf *resp;
  105. const u8 *pos;
  106. u8 *rpos;
  107. size_t left, plen;
  108. wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-1 (received)");
  109. if (data->state != PAX_INIT) {
  110. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 received in "
  111. "unexpected state (%d) - ignored", data->state);
  112. ret->ignore = TRUE;
  113. return NULL;
  114. }
  115. if (req->flags & EAP_PAX_FLAGS_CE) {
  116. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with CE flag set - "
  117. "ignored");
  118. ret->ignore = TRUE;
  119. return NULL;
  120. }
  121. left = req_plen - sizeof(*req);
  122. if (left < 2 + EAP_PAX_RAND_LEN) {
  123. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with too short "
  124. "payload");
  125. ret->ignore = TRUE;
  126. return NULL;
  127. }
  128. pos = (const u8 *) (req + 1);
  129. if (WPA_GET_BE16(pos) != EAP_PAX_RAND_LEN) {
  130. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with incorrect A "
  131. "length %d (expected %d)",
  132. WPA_GET_BE16(pos), EAP_PAX_RAND_LEN);
  133. ret->ignore = TRUE;
  134. return NULL;
  135. }
  136. pos += 2;
  137. left -= 2;
  138. os_memcpy(data->rand.r.x, pos, EAP_PAX_RAND_LEN);
  139. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: X (server rand)",
  140. data->rand.r.x, EAP_PAX_RAND_LEN);
  141. pos += EAP_PAX_RAND_LEN;
  142. left -= EAP_PAX_RAND_LEN;
  143. if (left > 0) {
  144. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ignored extra payload",
  145. pos, left);
  146. }
  147. if (os_get_random(data->rand.r.y, EAP_PAX_RAND_LEN)) {
  148. wpa_printf(MSG_ERROR, "EAP-PAX: Failed to get random data");
  149. ret->ignore = TRUE;
  150. return NULL;
  151. }
  152. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Y (client rand)",
  153. data->rand.r.y, EAP_PAX_RAND_LEN);
  154. if (eap_pax_initial_key_derivation(req->mac_id, data->ak, data->rand.e,
  155. data->mk, data->ck, data->ick) < 0)
  156. {
  157. ret->ignore = TRUE;
  158. return NULL;
  159. }
  160. wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-2 (sending)");
  161. plen = 2 + EAP_PAX_RAND_LEN + 2 + data->cid_len + 2 + EAP_PAX_MAC_LEN +
  162. EAP_PAX_ICV_LEN;
  163. resp = eap_pax_alloc_resp(req, id, EAP_PAX_OP_STD_2, plen);
  164. if (resp == NULL)
  165. return NULL;
  166. wpabuf_put_be16(resp, EAP_PAX_RAND_LEN);
  167. wpabuf_put_data(resp, data->rand.r.y, EAP_PAX_RAND_LEN);
  168. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: B = Y (client rand)",
  169. data->rand.r.y, EAP_PAX_RAND_LEN);
  170. wpabuf_put_be16(resp, data->cid_len);
  171. wpabuf_put_data(resp, data->cid, data->cid_len);
  172. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PAX: CID",
  173. (u8 *) data->cid, data->cid_len);
  174. wpabuf_put_be16(resp, EAP_PAX_MAC_LEN);
  175. rpos = wpabuf_put(resp, EAP_PAX_MAC_LEN);
  176. eap_pax_mac(req->mac_id, data->ck, EAP_PAX_CK_LEN,
  177. data->rand.r.x, EAP_PAX_RAND_LEN,
  178. data->rand.r.y, EAP_PAX_RAND_LEN,
  179. (u8 *) data->cid, data->cid_len, rpos);
  180. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(A, B, CID)",
  181. rpos, EAP_PAX_MAC_LEN);
  182. /* Optional ADE could be added here, if needed */
  183. rpos = wpabuf_put(resp, EAP_PAX_ICV_LEN);
  184. eap_pax_mac(req->mac_id, data->ick, EAP_PAX_ICK_LEN,
  185. wpabuf_head(resp), wpabuf_len(resp) - EAP_PAX_ICV_LEN,
  186. NULL, 0, NULL, 0, rpos);
  187. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", rpos, EAP_PAX_ICV_LEN);
  188. data->state = PAX_STD_2_SENT;
  189. data->mac_id = req->mac_id;
  190. data->dh_group_id = req->dh_group_id;
  191. data->public_key_id = req->public_key_id;
  192. return resp;
  193. }
  194. static struct wpabuf * eap_pax_process_std_3(struct eap_pax_data *data,
  195. struct eap_method_ret *ret, u8 id,
  196. const struct eap_pax_hdr *req,
  197. size_t req_plen)
  198. {
  199. struct wpabuf *resp;
  200. u8 *rpos, mac[EAP_PAX_MAC_LEN];
  201. const u8 *pos;
  202. size_t left;
  203. wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-3 (received)");
  204. if (data->state != PAX_STD_2_SENT) {
  205. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 received in "
  206. "unexpected state (%d) - ignored", data->state);
  207. ret->ignore = TRUE;
  208. return NULL;
  209. }
  210. if (req->flags & EAP_PAX_FLAGS_CE) {
  211. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with CE flag set - "
  212. "ignored");
  213. ret->ignore = TRUE;
  214. return NULL;
  215. }
  216. left = req_plen - sizeof(*req);
  217. if (left < 2 + EAP_PAX_MAC_LEN) {
  218. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with too short "
  219. "payload");
  220. ret->ignore = TRUE;
  221. return NULL;
  222. }
  223. pos = (const u8 *) (req + 1);
  224. if (WPA_GET_BE16(pos) != EAP_PAX_MAC_LEN) {
  225. wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with incorrect "
  226. "MAC_CK length %d (expected %d)",
  227. WPA_GET_BE16(pos), EAP_PAX_MAC_LEN);
  228. ret->ignore = TRUE;
  229. return NULL;
  230. }
  231. pos += 2;
  232. left -= 2;
  233. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(B, CID)",
  234. pos, EAP_PAX_MAC_LEN);
  235. eap_pax_mac(data->mac_id, data->ck, EAP_PAX_CK_LEN,
  236. data->rand.r.y, EAP_PAX_RAND_LEN,
  237. (u8 *) data->cid, data->cid_len, NULL, 0, mac);
  238. if (os_memcmp(pos, mac, EAP_PAX_MAC_LEN) != 0) {
  239. wpa_printf(MSG_INFO, "EAP-PAX: Invalid MAC_CK(B, CID) "
  240. "received");
  241. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: expected MAC_CK(B, CID)",
  242. mac, EAP_PAX_MAC_LEN);
  243. ret->methodState = METHOD_DONE;
  244. ret->decision = DECISION_FAIL;
  245. return NULL;
  246. }
  247. pos += EAP_PAX_MAC_LEN;
  248. left -= EAP_PAX_MAC_LEN;
  249. if (left > 0) {
  250. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ignored extra payload",
  251. pos, left);
  252. }
  253. wpa_printf(MSG_DEBUG, "EAP-PAX: PAX-ACK (sending)");
  254. resp = eap_pax_alloc_resp(req, id, EAP_PAX_OP_ACK, EAP_PAX_ICV_LEN);
  255. if (resp == NULL)
  256. return NULL;
  257. /* Optional ADE could be added here, if needed */
  258. rpos = wpabuf_put(resp, EAP_PAX_ICV_LEN);
  259. eap_pax_mac(data->mac_id, data->ick, EAP_PAX_ICK_LEN,
  260. wpabuf_head(resp), wpabuf_len(resp) - EAP_PAX_ICV_LEN,
  261. NULL, 0, NULL, 0, rpos);
  262. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", rpos, EAP_PAX_ICV_LEN);
  263. data->state = PAX_DONE;
  264. ret->methodState = METHOD_DONE;
  265. ret->decision = DECISION_UNCOND_SUCC;
  266. ret->allowNotifications = FALSE;
  267. return resp;
  268. }
  269. static struct wpabuf * eap_pax_process(struct eap_sm *sm, void *priv,
  270. struct eap_method_ret *ret,
  271. const struct wpabuf *reqData)
  272. {
  273. struct eap_pax_data *data = priv;
  274. const struct eap_pax_hdr *req;
  275. struct wpabuf *resp;
  276. u8 icvbuf[EAP_PAX_ICV_LEN], id;
  277. const u8 *icv, *pos;
  278. size_t len;
  279. u16 flen, mlen;
  280. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PAX, reqData, &len);
  281. if (pos == NULL || len < EAP_PAX_ICV_LEN) {
  282. ret->ignore = TRUE;
  283. return NULL;
  284. }
  285. id = eap_get_id(reqData);
  286. req = (const struct eap_pax_hdr *) pos;
  287. flen = len - EAP_PAX_ICV_LEN;
  288. mlen = wpabuf_len(reqData) - EAP_PAX_ICV_LEN;
  289. wpa_printf(MSG_DEBUG, "EAP-PAX: received frame: op_code 0x%x "
  290. "flags 0x%x mac_id 0x%x dh_group_id 0x%x "
  291. "public_key_id 0x%x",
  292. req->op_code, req->flags, req->mac_id, req->dh_group_id,
  293. req->public_key_id);
  294. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: received payload",
  295. pos, len - EAP_PAX_ICV_LEN);
  296. if (data->state != PAX_INIT && data->mac_id != req->mac_id) {
  297. wpa_printf(MSG_INFO, "EAP-PAX: MAC ID changed during "
  298. "authentication (was 0x%d, is 0x%d)",
  299. data->mac_id, req->mac_id);
  300. ret->ignore = TRUE;
  301. return NULL;
  302. }
  303. if (data->state != PAX_INIT && data->dh_group_id != req->dh_group_id) {
  304. wpa_printf(MSG_INFO, "EAP-PAX: DH Group ID changed during "
  305. "authentication (was 0x%d, is 0x%d)",
  306. data->dh_group_id, req->dh_group_id);
  307. ret->ignore = TRUE;
  308. return NULL;
  309. }
  310. if (data->state != PAX_INIT &&
  311. data->public_key_id != req->public_key_id) {
  312. wpa_printf(MSG_INFO, "EAP-PAX: Public Key ID changed during "
  313. "authentication (was 0x%d, is 0x%d)",
  314. data->public_key_id, req->public_key_id);
  315. ret->ignore = TRUE;
  316. return NULL;
  317. }
  318. /* TODO: add support EAP_PAX_HMAC_SHA256_128 */
  319. if (req->mac_id != EAP_PAX_MAC_HMAC_SHA1_128) {
  320. wpa_printf(MSG_INFO, "EAP-PAX: Unsupported MAC ID 0x%x",
  321. req->mac_id);
  322. ret->ignore = TRUE;
  323. return NULL;
  324. }
  325. if (req->dh_group_id != EAP_PAX_DH_GROUP_NONE) {
  326. wpa_printf(MSG_INFO, "EAP-PAX: Unsupported DH Group ID 0x%x",
  327. req->dh_group_id);
  328. ret->ignore = TRUE;
  329. return NULL;
  330. }
  331. if (req->public_key_id != EAP_PAX_PUBLIC_KEY_NONE) {
  332. wpa_printf(MSG_INFO, "EAP-PAX: Unsupported Public Key ID 0x%x",
  333. req->public_key_id);
  334. ret->ignore = TRUE;
  335. return NULL;
  336. }
  337. if (req->flags & EAP_PAX_FLAGS_MF) {
  338. /* TODO: add support for reassembling fragments */
  339. wpa_printf(MSG_INFO, "EAP-PAX: fragmentation not supported - "
  340. "ignored packet");
  341. ret->ignore = TRUE;
  342. return NULL;
  343. }
  344. icv = pos + len - EAP_PAX_ICV_LEN;
  345. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", icv, EAP_PAX_ICV_LEN);
  346. if (req->op_code == EAP_PAX_OP_STD_1) {
  347. eap_pax_mac(req->mac_id, (u8 *) "", 0,
  348. wpabuf_head(reqData), mlen, NULL, 0, NULL, 0,
  349. icvbuf);
  350. } else {
  351. eap_pax_mac(req->mac_id, data->ick, EAP_PAX_ICK_LEN,
  352. wpabuf_head(reqData), mlen, NULL, 0, NULL, 0,
  353. icvbuf);
  354. }
  355. if (os_memcmp(icv, icvbuf, EAP_PAX_ICV_LEN) != 0) {
  356. wpa_printf(MSG_DEBUG, "EAP-PAX: invalid ICV - ignoring the "
  357. "message");
  358. wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: expected ICV",
  359. icvbuf, EAP_PAX_ICV_LEN);
  360. ret->ignore = TRUE;
  361. return NULL;
  362. }
  363. ret->ignore = FALSE;
  364. ret->methodState = METHOD_MAY_CONT;
  365. ret->decision = DECISION_FAIL;
  366. ret->allowNotifications = TRUE;
  367. switch (req->op_code) {
  368. case EAP_PAX_OP_STD_1:
  369. resp = eap_pax_process_std_1(data, ret, id, req, flen);
  370. break;
  371. case EAP_PAX_OP_STD_3:
  372. resp = eap_pax_process_std_3(data, ret, id, req, flen);
  373. break;
  374. default:
  375. wpa_printf(MSG_DEBUG, "EAP-PAX: ignoring message with unknown "
  376. "op_code %d", req->op_code);
  377. ret->ignore = TRUE;
  378. return NULL;
  379. }
  380. if (ret->methodState == METHOD_DONE) {
  381. ret->allowNotifications = FALSE;
  382. }
  383. return resp;
  384. }
  385. static Boolean eap_pax_isKeyAvailable(struct eap_sm *sm, void *priv)
  386. {
  387. struct eap_pax_data *data = priv;
  388. return data->state == PAX_DONE;
  389. }
  390. static u8 * eap_pax_getKey(struct eap_sm *sm, void *priv, size_t *len)
  391. {
  392. struct eap_pax_data *data = priv;
  393. u8 *key;
  394. if (data->state != PAX_DONE)
  395. return NULL;
  396. key = os_malloc(EAP_MSK_LEN);
  397. if (key == NULL)
  398. return NULL;
  399. *len = EAP_MSK_LEN;
  400. eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN,
  401. "Master Session Key", data->rand.e, 2 * EAP_PAX_RAND_LEN,
  402. EAP_MSK_LEN, key);
  403. return key;
  404. }
  405. static u8 * eap_pax_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  406. {
  407. struct eap_pax_data *data = priv;
  408. u8 *key;
  409. if (data->state != PAX_DONE)
  410. return NULL;
  411. key = os_malloc(EAP_EMSK_LEN);
  412. if (key == NULL)
  413. return NULL;
  414. *len = EAP_EMSK_LEN;
  415. eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN,
  416. "Extended Master Session Key",
  417. data->rand.e, 2 * EAP_PAX_RAND_LEN,
  418. EAP_EMSK_LEN, key);
  419. return key;
  420. }
  421. int eap_peer_pax_register(void)
  422. {
  423. struct eap_method *eap;
  424. int ret;
  425. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  426. EAP_VENDOR_IETF, EAP_TYPE_PAX, "PAX");
  427. if (eap == NULL)
  428. return -1;
  429. eap->init = eap_pax_init;
  430. eap->deinit = eap_pax_deinit;
  431. eap->process = eap_pax_process;
  432. eap->isKeyAvailable = eap_pax_isKeyAvailable;
  433. eap->getKey = eap_pax_getKey;
  434. eap->get_emsk = eap_pax_get_emsk;
  435. ret = eap_peer_method_register(eap);
  436. if (ret)
  437. eap_peer_method_free(eap);
  438. return ret;
  439. }