eap_pwd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * EAP peer method: EAP-pwd (RFC 5931)
  3. * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org>
  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 "eap_peer/eap_i.h"
  11. #include "eap_common/eap_pwd_common.h"
  12. struct eap_pwd_data {
  13. enum {
  14. PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE
  15. } state;
  16. u8 *id_peer;
  17. size_t id_peer_len;
  18. u8 *id_server;
  19. size_t id_server_len;
  20. u8 *password;
  21. size_t password_len;
  22. u16 group_num;
  23. EAP_PWD_group *grp;
  24. struct wpabuf *inbuf;
  25. size_t in_frag_pos;
  26. struct wpabuf *outbuf;
  27. size_t out_frag_pos;
  28. size_t mtu;
  29. BIGNUM *k;
  30. BIGNUM *private_value;
  31. BIGNUM *server_scalar;
  32. BIGNUM *my_scalar;
  33. EC_POINT *my_element;
  34. EC_POINT *server_element;
  35. u8 msk[EAP_MSK_LEN];
  36. u8 emsk[EAP_EMSK_LEN];
  37. BN_CTX *bnctx;
  38. };
  39. #ifndef CONFIG_NO_STDOUT_DEBUG
  40. static const char * eap_pwd_state_txt(int state)
  41. {
  42. switch (state) {
  43. case PWD_ID_Req:
  44. return "PWD-ID-Req";
  45. case PWD_Commit_Req:
  46. return "PWD-Commit-Req";
  47. case PWD_Confirm_Req:
  48. return "PWD-Confirm-Req";
  49. case SUCCESS:
  50. return "SUCCESS";
  51. case FAILURE:
  52. return "FAILURE";
  53. default:
  54. return "PWD-UNK";
  55. }
  56. }
  57. #endif /* CONFIG_NO_STDOUT_DEBUG */
  58. static void eap_pwd_state(struct eap_pwd_data *data, int state)
  59. {
  60. wpa_printf(MSG_DEBUG, "EAP-PWD: %s -> %s",
  61. eap_pwd_state_txt(data->state), eap_pwd_state_txt(state));
  62. data->state = state;
  63. }
  64. static void * eap_pwd_init(struct eap_sm *sm)
  65. {
  66. struct eap_pwd_data *data;
  67. const u8 *identity, *password;
  68. size_t identity_len, password_len;
  69. password = eap_get_config_password(sm, &password_len);
  70. if (password == NULL) {
  71. wpa_printf(MSG_INFO, "EAP-PWD: No password configured!");
  72. return NULL;
  73. }
  74. identity = eap_get_config_identity(sm, &identity_len);
  75. if (identity == NULL) {
  76. wpa_printf(MSG_INFO, "EAP-PWD: No identity configured!");
  77. return NULL;
  78. }
  79. if ((data = os_zalloc(sizeof(*data))) == NULL) {
  80. wpa_printf(MSG_INFO, "EAP-PWD: memory allocation data fail");
  81. return NULL;
  82. }
  83. if ((data->bnctx = BN_CTX_new()) == NULL) {
  84. wpa_printf(MSG_INFO, "EAP-PWD: bn context allocation fail");
  85. os_free(data);
  86. return NULL;
  87. }
  88. if ((data->id_peer = os_malloc(identity_len)) == NULL) {
  89. wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
  90. BN_CTX_free(data->bnctx);
  91. os_free(data);
  92. return NULL;
  93. }
  94. os_memcpy(data->id_peer, identity, identity_len);
  95. data->id_peer_len = identity_len;
  96. if ((data->password = os_malloc(password_len)) == NULL) {
  97. wpa_printf(MSG_INFO, "EAP-PWD: memory allocation psk fail");
  98. BN_CTX_free(data->bnctx);
  99. os_free(data->id_peer);
  100. os_free(data);
  101. return NULL;
  102. }
  103. os_memcpy(data->password, password, password_len);
  104. data->password_len = password_len;
  105. data->out_frag_pos = data->in_frag_pos = 0;
  106. data->inbuf = data->outbuf = NULL;
  107. data->mtu = 1020; /* default from RFC 5931, make it configurable! */
  108. data->state = PWD_ID_Req;
  109. return data;
  110. }
  111. static void eap_pwd_deinit(struct eap_sm *sm, void *priv)
  112. {
  113. struct eap_pwd_data *data = priv;
  114. BN_free(data->private_value);
  115. BN_free(data->server_scalar);
  116. BN_free(data->my_scalar);
  117. BN_free(data->k);
  118. BN_CTX_free(data->bnctx);
  119. EC_POINT_free(data->my_element);
  120. EC_POINT_free(data->server_element);
  121. os_free(data->id_peer);
  122. os_free(data->id_server);
  123. os_free(data->password);
  124. if (data->grp) {
  125. EC_GROUP_free(data->grp->group);
  126. EC_POINT_free(data->grp->pwe);
  127. BN_free(data->grp->order);
  128. BN_free(data->grp->prime);
  129. os_free(data->grp);
  130. }
  131. os_free(data);
  132. }
  133. static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len)
  134. {
  135. struct eap_pwd_data *data = priv;
  136. u8 *key;
  137. if (data->state != SUCCESS)
  138. return NULL;
  139. key = os_malloc(EAP_MSK_LEN);
  140. if (key == NULL)
  141. return NULL;
  142. os_memcpy(key, data->msk, EAP_MSK_LEN);
  143. *len = EAP_MSK_LEN;
  144. return key;
  145. }
  146. static void
  147. eap_pwd_perform_id_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
  148. struct eap_method_ret *ret,
  149. const struct wpabuf *reqData,
  150. const u8 *payload, size_t payload_len)
  151. {
  152. struct eap_pwd_id *id;
  153. if (data->state != PWD_ID_Req) {
  154. ret->ignore = TRUE;
  155. eap_pwd_state(data, FAILURE);
  156. return;
  157. }
  158. if (payload_len < sizeof(struct eap_pwd_id)) {
  159. ret->ignore = TRUE;
  160. eap_pwd_state(data, FAILURE);
  161. return;
  162. }
  163. id = (struct eap_pwd_id *) payload;
  164. data->group_num = be_to_host16(id->group_num);
  165. if ((id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) ||
  166. (id->prf != EAP_PWD_DEFAULT_PRF)) {
  167. ret->ignore = TRUE;
  168. eap_pwd_state(data, FAILURE);
  169. return;
  170. }
  171. wpa_printf(MSG_DEBUG, "EAP-PWD (peer): using group %d",
  172. data->group_num);
  173. data->id_server = os_malloc(payload_len - sizeof(struct eap_pwd_id));
  174. if (data->id_server == NULL) {
  175. wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
  176. eap_pwd_state(data, FAILURE);
  177. return;
  178. }
  179. data->id_server_len = payload_len - sizeof(struct eap_pwd_id);
  180. os_memcpy(data->id_server, id->identity, data->id_server_len);
  181. wpa_hexdump_ascii(MSG_INFO, "EAP-PWD (peer): server sent id of",
  182. data->id_server, data->id_server_len);
  183. if ((data->grp = (EAP_PWD_group *) os_malloc(sizeof(EAP_PWD_group))) ==
  184. NULL) {
  185. wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for "
  186. "group");
  187. eap_pwd_state(data, FAILURE);
  188. return;
  189. }
  190. /* compute PWE */
  191. if (compute_password_element(data->grp, data->group_num,
  192. data->password, data->password_len,
  193. data->id_server, data->id_server_len,
  194. data->id_peer, data->id_peer_len,
  195. id->token)) {
  196. wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute PWE");
  197. eap_pwd_state(data, FAILURE);
  198. return;
  199. }
  200. wpa_printf(MSG_DEBUG, "EAP-PWD (peer): computed %d bit PWE...",
  201. BN_num_bits(data->grp->prime));
  202. data->outbuf = wpabuf_alloc(sizeof(struct eap_pwd_id) +
  203. data->id_peer_len);
  204. if (data->outbuf == NULL) {
  205. eap_pwd_state(data, FAILURE);
  206. return;
  207. }
  208. wpabuf_put_be16(data->outbuf, data->group_num);
  209. wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_RAND_FUNC);
  210. wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_PRF);
  211. wpabuf_put_data(data->outbuf, id->token, sizeof(id->token));
  212. wpabuf_put_u8(data->outbuf, EAP_PWD_PREP_NONE);
  213. wpabuf_put_data(data->outbuf, data->id_peer, data->id_peer_len);
  214. eap_pwd_state(data, PWD_Commit_Req);
  215. }
  216. static void
  217. eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
  218. struct eap_method_ret *ret,
  219. const struct wpabuf *reqData,
  220. const u8 *payload, size_t payload_len)
  221. {
  222. EC_POINT *K = NULL, *point = NULL;
  223. BIGNUM *mask = NULL, *x = NULL, *y = NULL, *cofactor = NULL;
  224. u16 offset;
  225. u8 *ptr, *scalar = NULL, *element = NULL;
  226. if (((data->private_value = BN_new()) == NULL) ||
  227. ((data->my_element = EC_POINT_new(data->grp->group)) == NULL) ||
  228. ((cofactor = BN_new()) == NULL) ||
  229. ((data->my_scalar = BN_new()) == NULL) ||
  230. ((mask = BN_new()) == NULL)) {
  231. wpa_printf(MSG_INFO, "EAP-PWD (peer): scalar allocation fail");
  232. goto fin;
  233. }
  234. if (!EC_GROUP_get_cofactor(data->grp->group, cofactor, NULL)) {
  235. wpa_printf(MSG_INFO, "EAP-pwd (peer): unable to get cofactor "
  236. "for curve");
  237. goto fin;
  238. }
  239. BN_rand_range(data->private_value, data->grp->order);
  240. BN_rand_range(mask, data->grp->order);
  241. BN_add(data->my_scalar, data->private_value, mask);
  242. BN_mod(data->my_scalar, data->my_scalar, data->grp->order,
  243. data->bnctx);
  244. if (!EC_POINT_mul(data->grp->group, data->my_element, NULL,
  245. data->grp->pwe, mask, data->bnctx)) {
  246. wpa_printf(MSG_INFO, "EAP-PWD (peer): element allocation "
  247. "fail");
  248. eap_pwd_state(data, FAILURE);
  249. goto fin;
  250. }
  251. if (!EC_POINT_invert(data->grp->group, data->my_element, data->bnctx))
  252. {
  253. wpa_printf(MSG_INFO, "EAP-PWD (peer): element inversion fail");
  254. goto fin;
  255. }
  256. BN_free(mask);
  257. if (((x = BN_new()) == NULL) ||
  258. ((y = BN_new()) == NULL)) {
  259. wpa_printf(MSG_INFO, "EAP-PWD (peer): point allocation fail");
  260. goto fin;
  261. }
  262. /* process the request */
  263. if (((data->server_scalar = BN_new()) == NULL) ||
  264. ((data->k = BN_new()) == NULL) ||
  265. ((K = EC_POINT_new(data->grp->group)) == NULL) ||
  266. ((point = EC_POINT_new(data->grp->group)) == NULL) ||
  267. ((data->server_element = EC_POINT_new(data->grp->group)) == NULL))
  268. {
  269. wpa_printf(MSG_INFO, "EAP-PWD (peer): peer data allocation "
  270. "fail");
  271. goto fin;
  272. }
  273. /* element, x then y, followed by scalar */
  274. ptr = (u8 *) payload;
  275. BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), x);
  276. ptr += BN_num_bytes(data->grp->prime);
  277. BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), y);
  278. ptr += BN_num_bytes(data->grp->prime);
  279. BN_bin2bn(ptr, BN_num_bytes(data->grp->order), data->server_scalar);
  280. if (!EC_POINT_set_affine_coordinates_GFp(data->grp->group,
  281. data->server_element, x, y,
  282. data->bnctx)) {
  283. wpa_printf(MSG_INFO, "EAP-PWD (peer): setting peer element "
  284. "fail");
  285. goto fin;
  286. }
  287. /* check to ensure server's element is not in a small sub-group */
  288. if (BN_cmp(cofactor, BN_value_one())) {
  289. if (!EC_POINT_mul(data->grp->group, point, NULL,
  290. data->server_element, cofactor, NULL)) {
  291. wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply "
  292. "server element by order!\n");
  293. goto fin;
  294. }
  295. if (EC_POINT_is_at_infinity(data->grp->group, point)) {
  296. wpa_printf(MSG_INFO, "EAP-PWD (peer): server element "
  297. "is at infinity!\n");
  298. goto fin;
  299. }
  300. }
  301. /* compute the shared key, k */
  302. if ((!EC_POINT_mul(data->grp->group, K, NULL, data->grp->pwe,
  303. data->server_scalar, data->bnctx)) ||
  304. (!EC_POINT_add(data->grp->group, K, K, data->server_element,
  305. data->bnctx)) ||
  306. (!EC_POINT_mul(data->grp->group, K, NULL, K, data->private_value,
  307. data->bnctx))) {
  308. wpa_printf(MSG_INFO, "EAP-PWD (peer): computing shared key "
  309. "fail");
  310. goto fin;
  311. }
  312. /* ensure that the shared key isn't in a small sub-group */
  313. if (BN_cmp(cofactor, BN_value_one())) {
  314. if (!EC_POINT_mul(data->grp->group, K, NULL, K, cofactor,
  315. NULL)) {
  316. wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply "
  317. "shared key point by order");
  318. goto fin;
  319. }
  320. }
  321. /*
  322. * This check is strictly speaking just for the case above where
  323. * co-factor > 1 but it was suggested that even though this is probably
  324. * never going to happen it is a simple and safe check "just to be
  325. * sure" so let's be safe.
  326. */
  327. if (EC_POINT_is_at_infinity(data->grp->group, K)) {
  328. wpa_printf(MSG_INFO, "EAP-PWD (peer): shared key point is at "
  329. "infinity!\n");
  330. goto fin;
  331. }
  332. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, K, data->k,
  333. NULL, data->bnctx)) {
  334. wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to extract "
  335. "shared secret from point");
  336. goto fin;
  337. }
  338. /* now do the response */
  339. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  340. data->my_element, x, y,
  341. data->bnctx)) {
  342. wpa_printf(MSG_INFO, "EAP-PWD (peer): point assignment fail");
  343. goto fin;
  344. }
  345. if (((scalar = os_malloc(BN_num_bytes(data->grp->order))) == NULL) ||
  346. ((element = os_malloc(BN_num_bytes(data->grp->prime) * 2)) ==
  347. NULL)) {
  348. wpa_printf(MSG_INFO, "EAP-PWD (peer): data allocation fail");
  349. goto fin;
  350. }
  351. /*
  352. * bignums occupy as little memory as possible so one that is
  353. * sufficiently smaller than the prime or order might need pre-pending
  354. * with zeros.
  355. */
  356. os_memset(scalar, 0, BN_num_bytes(data->grp->order));
  357. os_memset(element, 0, BN_num_bytes(data->grp->prime) * 2);
  358. offset = BN_num_bytes(data->grp->order) -
  359. BN_num_bytes(data->my_scalar);
  360. BN_bn2bin(data->my_scalar, scalar + offset);
  361. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  362. BN_bn2bin(x, element + offset);
  363. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  364. BN_bn2bin(y, element + BN_num_bytes(data->grp->prime) + offset);
  365. data->outbuf = wpabuf_alloc(BN_num_bytes(data->grp->order) +
  366. 2 * BN_num_bytes(data->grp->prime));
  367. if (data->outbuf == NULL)
  368. goto fin;
  369. /* we send the element as (x,y) follwed by the scalar */
  370. wpabuf_put_data(data->outbuf, element,
  371. 2 * BN_num_bytes(data->grp->prime));
  372. wpabuf_put_data(data->outbuf, scalar, BN_num_bytes(data->grp->order));
  373. fin:
  374. os_free(scalar);
  375. os_free(element);
  376. BN_free(x);
  377. BN_free(y);
  378. BN_free(cofactor);
  379. EC_POINT_free(K);
  380. EC_POINT_free(point);
  381. if (data->outbuf == NULL)
  382. eap_pwd_state(data, FAILURE);
  383. else
  384. eap_pwd_state(data, PWD_Confirm_Req);
  385. }
  386. static void
  387. eap_pwd_perform_confirm_exchange(struct eap_sm *sm, struct eap_pwd_data *data,
  388. struct eap_method_ret *ret,
  389. const struct wpabuf *reqData,
  390. const u8 *payload, size_t payload_len)
  391. {
  392. BIGNUM *x = NULL, *y = NULL;
  393. HMAC_CTX ctx;
  394. u32 cs;
  395. u16 grp;
  396. u8 conf[SHA256_DIGEST_LENGTH], *cruft = NULL, *ptr;
  397. int offset;
  398. /*
  399. * first build up the ciphersuite which is group | random_function |
  400. * prf
  401. */
  402. grp = htons(data->group_num);
  403. ptr = (u8 *) &cs;
  404. os_memcpy(ptr, &grp, sizeof(u16));
  405. ptr += sizeof(u16);
  406. *ptr = EAP_PWD_DEFAULT_RAND_FUNC;
  407. ptr += sizeof(u8);
  408. *ptr = EAP_PWD_DEFAULT_PRF;
  409. /* each component of the cruft will be at most as big as the prime */
  410. if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) ||
  411. ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
  412. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm allocation "
  413. "fail");
  414. goto fin;
  415. }
  416. /*
  417. * server's commit is H(k | server_element | server_scalar |
  418. * peer_element | peer_scalar | ciphersuite)
  419. */
  420. H_Init(&ctx);
  421. /*
  422. * zero the memory each time because this is mod prime math and some
  423. * value may start with a few zeros and the previous one did not.
  424. */
  425. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  426. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
  427. BN_bn2bin(data->k, cruft + offset);
  428. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  429. /* server element: x, y */
  430. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  431. data->server_element, x, y,
  432. data->bnctx)) {
  433. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  434. "assignment fail");
  435. goto fin;
  436. }
  437. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  438. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  439. BN_bn2bin(x, cruft + offset);
  440. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  441. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  442. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  443. BN_bn2bin(y, cruft + offset);
  444. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  445. /* server scalar */
  446. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  447. offset = BN_num_bytes(data->grp->order) -
  448. BN_num_bytes(data->server_scalar);
  449. BN_bn2bin(data->server_scalar, cruft + offset);
  450. H_Update(&ctx, cruft, BN_num_bytes(data->grp->order));
  451. /* my element: x, y */
  452. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  453. data->my_element, x, y,
  454. data->bnctx)) {
  455. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  456. "assignment fail");
  457. goto fin;
  458. }
  459. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  460. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  461. BN_bn2bin(x, cruft + offset);
  462. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  463. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  464. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  465. BN_bn2bin(y, cruft + offset);
  466. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  467. /* my scalar */
  468. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  469. offset = BN_num_bytes(data->grp->order) -
  470. BN_num_bytes(data->my_scalar);
  471. BN_bn2bin(data->my_scalar, cruft + offset);
  472. H_Update(&ctx, cruft, BN_num_bytes(data->grp->order));
  473. /* the ciphersuite */
  474. H_Update(&ctx, (u8 *) &cs, sizeof(u32));
  475. /* random function fin */
  476. H_Final(&ctx, conf);
  477. ptr = (u8 *) payload;
  478. if (os_memcmp(conf, ptr, SHA256_DIGEST_LENGTH)) {
  479. wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm did not verify");
  480. goto fin;
  481. }
  482. wpa_printf(MSG_DEBUG, "EAP-pwd (peer): confirm verified");
  483. /*
  484. * compute confirm:
  485. * H(k | peer_element | peer_scalar | server_element | server_scalar |
  486. * ciphersuite)
  487. */
  488. H_Init(&ctx);
  489. /* k */
  490. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  491. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
  492. BN_bn2bin(data->k, cruft + offset);
  493. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  494. /* my element */
  495. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  496. data->my_element, x, y,
  497. data->bnctx)) {
  498. wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
  499. "assignment fail");
  500. goto fin;
  501. }
  502. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  503. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  504. BN_bn2bin(x, cruft + offset);
  505. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  506. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  507. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  508. BN_bn2bin(y, cruft + offset);
  509. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  510. /* my scalar */
  511. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  512. offset = BN_num_bytes(data->grp->order) -
  513. BN_num_bytes(data->my_scalar);
  514. BN_bn2bin(data->my_scalar, cruft + offset);
  515. H_Update(&ctx, cruft, BN_num_bytes(data->grp->order));
  516. /* server element: x, y */
  517. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  518. data->server_element, x, y,
  519. data->bnctx)) {
  520. wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point "
  521. "assignment fail");
  522. goto fin;
  523. }
  524. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  525. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  526. BN_bn2bin(x, cruft + offset);
  527. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  528. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  529. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  530. BN_bn2bin(y, cruft + offset);
  531. H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime));
  532. /* server scalar */
  533. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  534. offset = BN_num_bytes(data->grp->order) -
  535. BN_num_bytes(data->server_scalar);
  536. BN_bn2bin(data->server_scalar, cruft + offset);
  537. H_Update(&ctx, cruft, BN_num_bytes(data->grp->order));
  538. /* the ciphersuite */
  539. H_Update(&ctx, (u8 *) &cs, sizeof(u32));
  540. /* all done */
  541. H_Final(&ctx, conf);
  542. if (compute_keys(data->grp, data->bnctx, data->k,
  543. data->my_scalar, data->server_scalar, conf, ptr,
  544. &cs, data->msk, data->emsk) < 0) {
  545. wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute MSK | "
  546. "EMSK");
  547. goto fin;
  548. }
  549. data->outbuf = wpabuf_alloc(SHA256_DIGEST_LENGTH);
  550. if (data->outbuf == NULL)
  551. goto fin;
  552. wpabuf_put_data(data->outbuf, conf, SHA256_DIGEST_LENGTH);
  553. fin:
  554. os_free(cruft);
  555. BN_free(x);
  556. BN_free(y);
  557. ret->methodState = METHOD_DONE;
  558. if (data->outbuf == NULL) {
  559. ret->decision = DECISION_FAIL;
  560. eap_pwd_state(data, FAILURE);
  561. } else {
  562. ret->decision = DECISION_UNCOND_SUCC;
  563. eap_pwd_state(data, SUCCESS);
  564. }
  565. }
  566. static struct wpabuf *
  567. eap_pwd_process(struct eap_sm *sm, void *priv, struct eap_method_ret *ret,
  568. const struct wpabuf *reqData)
  569. {
  570. struct eap_pwd_data *data = priv;
  571. struct wpabuf *resp = NULL;
  572. const u8 *pos, *buf;
  573. size_t len;
  574. u16 tot_len = 0;
  575. u8 lm_exch;
  576. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, reqData, &len);
  577. if ((pos == NULL) || (len < 1)) {
  578. wpa_printf(MSG_DEBUG, "EAP-pwd: Got a frame but pos is %s and "
  579. "len is %d",
  580. pos == NULL ? "NULL" : "not NULL", (int) len);
  581. ret->ignore = TRUE;
  582. return NULL;
  583. }
  584. ret->ignore = FALSE;
  585. ret->methodState = METHOD_MAY_CONT;
  586. ret->decision = DECISION_FAIL;
  587. ret->allowNotifications = FALSE;
  588. lm_exch = *pos;
  589. pos++; /* skip over the bits and the exch */
  590. len--;
  591. /*
  592. * we're fragmenting so send out the next fragment
  593. */
  594. if (data->out_frag_pos) {
  595. /*
  596. * this should be an ACK
  597. */
  598. if (len)
  599. wpa_printf(MSG_INFO, "Bad Response! Fragmenting but "
  600. "not an ACK");
  601. wpa_printf(MSG_DEBUG, "EAP-pwd: Got an ACK for a fragment");
  602. /*
  603. * check if there are going to be more fragments
  604. */
  605. len = wpabuf_len(data->outbuf) - data->out_frag_pos;
  606. if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
  607. len = data->mtu - EAP_PWD_HDR_SIZE;
  608. EAP_PWD_SET_MORE_BIT(lm_exch);
  609. }
  610. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
  611. EAP_PWD_HDR_SIZE + len,
  612. EAP_CODE_RESPONSE, eap_get_id(reqData));
  613. if (resp == NULL) {
  614. wpa_printf(MSG_INFO, "Unable to allocate memory for "
  615. "next fragment!");
  616. return NULL;
  617. }
  618. wpabuf_put_u8(resp, lm_exch);
  619. buf = wpabuf_head_u8(data->outbuf);
  620. wpabuf_put_data(resp, buf + data->out_frag_pos, len);
  621. data->out_frag_pos += len;
  622. /*
  623. * this is the last fragment so get rid of the out buffer
  624. */
  625. if (data->out_frag_pos >= wpabuf_len(data->outbuf)) {
  626. wpabuf_free(data->outbuf);
  627. data->out_frag_pos = 0;
  628. }
  629. wpa_printf(MSG_DEBUG, "EAP-pwd: Send %s fragment of %d bytes",
  630. data->out_frag_pos == 0 ? "last" : "next",
  631. (int) len);
  632. return resp;
  633. }
  634. /*
  635. * see if this is a fragment that needs buffering
  636. *
  637. * if it's the first fragment there'll be a length field
  638. */
  639. if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
  640. tot_len = WPA_GET_BE16(pos);
  641. wpa_printf(MSG_DEBUG, "EAP-pwd: Incoming fragments whose "
  642. "total length = %d", tot_len);
  643. data->inbuf = wpabuf_alloc(tot_len);
  644. if (data->inbuf == NULL) {
  645. wpa_printf(MSG_INFO, "Out of memory to buffer "
  646. "fragments!");
  647. return NULL;
  648. }
  649. pos += sizeof(u16);
  650. len -= sizeof(u16);
  651. }
  652. /*
  653. * buffer and ACK the fragment
  654. */
  655. if (EAP_PWD_GET_MORE_BIT(lm_exch)) {
  656. data->in_frag_pos += len;
  657. if (data->in_frag_pos > wpabuf_size(data->inbuf)) {
  658. wpa_printf(MSG_INFO, "EAP-pwd: Buffer overflow attack "
  659. "detected (%d vs. %d)!",
  660. (int) data->in_frag_pos,
  661. (int) wpabuf_len(data->inbuf));
  662. wpabuf_free(data->inbuf);
  663. data->in_frag_pos = 0;
  664. return NULL;
  665. }
  666. wpabuf_put_data(data->inbuf, pos, len);
  667. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
  668. EAP_PWD_HDR_SIZE,
  669. EAP_CODE_RESPONSE, eap_get_id(reqData));
  670. if (resp != NULL)
  671. wpabuf_put_u8(resp, (EAP_PWD_GET_EXCHANGE(lm_exch)));
  672. wpa_printf(MSG_DEBUG, "EAP-pwd: ACKing a %d byte fragment",
  673. (int) len);
  674. return resp;
  675. }
  676. /*
  677. * we're buffering and this is the last fragment
  678. */
  679. if (data->in_frag_pos) {
  680. wpabuf_put_data(data->inbuf, pos, len);
  681. wpa_printf(MSG_DEBUG, "EAP-pwd: Last fragment, %d bytes",
  682. (int) len);
  683. data->in_frag_pos += len;
  684. pos = wpabuf_head_u8(data->inbuf);
  685. len = data->in_frag_pos;
  686. }
  687. wpa_printf(MSG_DEBUG, "EAP-pwd: processing frame: exch %d, len %d",
  688. EAP_PWD_GET_EXCHANGE(lm_exch), (int) len);
  689. switch (EAP_PWD_GET_EXCHANGE(lm_exch)) {
  690. case EAP_PWD_OPCODE_ID_EXCH:
  691. eap_pwd_perform_id_exchange(sm, data, ret, reqData,
  692. pos, len);
  693. break;
  694. case EAP_PWD_OPCODE_COMMIT_EXCH:
  695. eap_pwd_perform_commit_exchange(sm, data, ret, reqData,
  696. pos, len);
  697. break;
  698. case EAP_PWD_OPCODE_CONFIRM_EXCH:
  699. eap_pwd_perform_confirm_exchange(sm, data, ret, reqData,
  700. pos, len);
  701. break;
  702. default:
  703. wpa_printf(MSG_INFO, "EAP-pwd: Ignoring message with unknown "
  704. "opcode %d", lm_exch);
  705. break;
  706. }
  707. /*
  708. * if we buffered the just processed input now's the time to free it
  709. */
  710. if (data->in_frag_pos) {
  711. wpabuf_free(data->inbuf);
  712. data->in_frag_pos = 0;
  713. }
  714. if (data->outbuf == NULL)
  715. return NULL; /* generic failure */
  716. /*
  717. * we have output! Do we need to fragment it?
  718. */
  719. len = wpabuf_len(data->outbuf);
  720. if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
  721. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, data->mtu,
  722. EAP_CODE_RESPONSE, eap_get_id(reqData));
  723. /*
  724. * if so it's the first so include a length field
  725. */
  726. EAP_PWD_SET_LENGTH_BIT(lm_exch);
  727. EAP_PWD_SET_MORE_BIT(lm_exch);
  728. tot_len = len;
  729. /*
  730. * keep the packet at the MTU
  731. */
  732. len = data->mtu - EAP_PWD_HDR_SIZE - sizeof(u16);
  733. wpa_printf(MSG_DEBUG, "EAP-pwd: Fragmenting output, total "
  734. "length = %d", tot_len);
  735. } else {
  736. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
  737. EAP_PWD_HDR_SIZE + len,
  738. EAP_CODE_RESPONSE, eap_get_id(reqData));
  739. }
  740. if (resp == NULL)
  741. return NULL;
  742. wpabuf_put_u8(resp, lm_exch);
  743. if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
  744. wpabuf_put_be16(resp, tot_len);
  745. data->out_frag_pos += len;
  746. }
  747. buf = wpabuf_head_u8(data->outbuf);
  748. wpabuf_put_data(resp, buf, len);
  749. /*
  750. * if we're not fragmenting then there's no need to carry this around
  751. */
  752. if (data->out_frag_pos == 0)
  753. wpabuf_free(data->outbuf);
  754. return resp;
  755. }
  756. static Boolean eap_pwd_key_available(struct eap_sm *sm, void *priv)
  757. {
  758. struct eap_pwd_data *data = priv;
  759. return data->state == SUCCESS;
  760. }
  761. static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  762. {
  763. struct eap_pwd_data *data = priv;
  764. u8 *key;
  765. if (data->state != SUCCESS)
  766. return NULL;
  767. if ((key = os_malloc(EAP_EMSK_LEN)) == NULL)
  768. return NULL;
  769. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  770. *len = EAP_EMSK_LEN;
  771. return key;
  772. }
  773. int eap_peer_pwd_register(void)
  774. {
  775. struct eap_method *eap;
  776. int ret;
  777. EVP_add_digest(EVP_sha256());
  778. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  779. EAP_VENDOR_IETF, EAP_TYPE_PWD, "PWD");
  780. if (eap == NULL)
  781. return -1;
  782. eap->init = eap_pwd_init;
  783. eap->deinit = eap_pwd_deinit;
  784. eap->process = eap_pwd_process;
  785. eap->isKeyAvailable = eap_pwd_key_available;
  786. eap->getKey = eap_pwd_getkey;
  787. eap->get_emsk = eap_pwd_get_emsk;
  788. ret = eap_peer_method_register(eap);
  789. if (ret)
  790. eap_peer_method_free(eap);
  791. return ret;
  792. }