eap_pax.c 14 KB

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