eap_server_pwd.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * hostapd / EAP-pwd (RFC 5931) server
  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 "crypto/sha256.h"
  11. #include "crypto/ms_funcs.h"
  12. #include "eap_server/eap_i.h"
  13. #include "eap_common/eap_pwd_common.h"
  14. struct eap_pwd_data {
  15. enum {
  16. PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE
  17. } state;
  18. u8 *id_peer;
  19. size_t id_peer_len;
  20. u8 *id_server;
  21. size_t id_server_len;
  22. u8 *password;
  23. size_t password_len;
  24. int password_hash;
  25. u32 token;
  26. u16 group_num;
  27. EAP_PWD_group *grp;
  28. struct wpabuf *inbuf;
  29. size_t in_frag_pos;
  30. struct wpabuf *outbuf;
  31. size_t out_frag_pos;
  32. size_t mtu;
  33. BIGNUM *k;
  34. BIGNUM *private_value;
  35. BIGNUM *peer_scalar;
  36. BIGNUM *my_scalar;
  37. EC_POINT *my_element;
  38. EC_POINT *peer_element;
  39. u8 my_confirm[SHA256_MAC_LEN];
  40. u8 msk[EAP_MSK_LEN];
  41. u8 emsk[EAP_EMSK_LEN];
  42. u8 session_id[1 + SHA256_MAC_LEN];
  43. BN_CTX *bnctx;
  44. };
  45. static const char * eap_pwd_state_txt(int state)
  46. {
  47. switch (state) {
  48. case PWD_ID_Req:
  49. return "PWD-ID-Req";
  50. case PWD_Commit_Req:
  51. return "PWD-Commit-Req";
  52. case PWD_Confirm_Req:
  53. return "PWD-Confirm-Req";
  54. case SUCCESS:
  55. return "SUCCESS";
  56. case FAILURE:
  57. return "FAILURE";
  58. default:
  59. return "PWD-Unk";
  60. }
  61. }
  62. static void eap_pwd_state(struct eap_pwd_data *data, int state)
  63. {
  64. wpa_printf(MSG_DEBUG, "EAP-pwd: %s -> %s",
  65. eap_pwd_state_txt(data->state), eap_pwd_state_txt(state));
  66. data->state = state;
  67. }
  68. static void * eap_pwd_init(struct eap_sm *sm)
  69. {
  70. struct eap_pwd_data *data;
  71. if (sm->user == NULL || sm->user->password == NULL ||
  72. sm->user->password_len == 0) {
  73. wpa_printf(MSG_INFO, "EAP-PWD (server): Password is not "
  74. "configured");
  75. return NULL;
  76. }
  77. data = os_zalloc(sizeof(*data));
  78. if (data == NULL)
  79. return NULL;
  80. data->group_num = sm->pwd_group;
  81. wpa_printf(MSG_DEBUG, "EAP-pwd: Selected group number %d",
  82. data->group_num);
  83. data->state = PWD_ID_Req;
  84. data->id_server = (u8 *) os_strdup("server");
  85. if (data->id_server)
  86. data->id_server_len = os_strlen((char *) data->id_server);
  87. data->password = os_malloc(sm->user->password_len);
  88. if (data->password == NULL) {
  89. wpa_printf(MSG_INFO, "EAP-PWD: Memory allocation password "
  90. "fail");
  91. bin_clear_free(data->id_server, data->id_server_len);
  92. os_free(data);
  93. return NULL;
  94. }
  95. data->password_len = sm->user->password_len;
  96. os_memcpy(data->password, sm->user->password, data->password_len);
  97. data->password_hash = sm->user->password_hash;
  98. data->bnctx = BN_CTX_new();
  99. if (data->bnctx == NULL) {
  100. wpa_printf(MSG_INFO, "EAP-PWD: bn context allocation fail");
  101. bin_clear_free(data->password, data->password_len);
  102. bin_clear_free(data->id_server, data->id_server_len);
  103. os_free(data);
  104. return NULL;
  105. }
  106. data->in_frag_pos = data->out_frag_pos = 0;
  107. data->inbuf = data->outbuf = NULL;
  108. /* use default MTU from RFC 5931 if not configured otherwise */
  109. data->mtu = sm->fragment_size > 0 ? sm->fragment_size : 1020;
  110. return data;
  111. }
  112. static void eap_pwd_reset(struct eap_sm *sm, void *priv)
  113. {
  114. struct eap_pwd_data *data = priv;
  115. BN_clear_free(data->private_value);
  116. BN_clear_free(data->peer_scalar);
  117. BN_clear_free(data->my_scalar);
  118. BN_clear_free(data->k);
  119. BN_CTX_free(data->bnctx);
  120. EC_POINT_clear_free(data->my_element);
  121. EC_POINT_clear_free(data->peer_element);
  122. bin_clear_free(data->id_peer, data->id_peer_len);
  123. bin_clear_free(data->id_server, data->id_server_len);
  124. bin_clear_free(data->password, data->password_len);
  125. if (data->grp) {
  126. EC_GROUP_free(data->grp->group);
  127. EC_POINT_clear_free(data->grp->pwe);
  128. BN_clear_free(data->grp->order);
  129. BN_clear_free(data->grp->prime);
  130. os_free(data->grp);
  131. }
  132. wpabuf_free(data->inbuf);
  133. wpabuf_free(data->outbuf);
  134. bin_clear_free(data, sizeof(*data));
  135. }
  136. static void eap_pwd_build_id_req(struct eap_sm *sm, struct eap_pwd_data *data,
  137. u8 id)
  138. {
  139. wpa_printf(MSG_DEBUG, "EAP-pwd: ID/Request");
  140. /*
  141. * if we're fragmenting then we already have an id request, just return
  142. */
  143. if (data->out_frag_pos)
  144. return;
  145. data->outbuf = wpabuf_alloc(sizeof(struct eap_pwd_id) +
  146. data->id_server_len);
  147. if (data->outbuf == NULL) {
  148. eap_pwd_state(data, FAILURE);
  149. return;
  150. }
  151. /* an lfsr is good enough to generate unpredictable tokens */
  152. data->token = os_random();
  153. wpabuf_put_be16(data->outbuf, data->group_num);
  154. wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_RAND_FUNC);
  155. wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_PRF);
  156. wpabuf_put_data(data->outbuf, &data->token, sizeof(data->token));
  157. wpabuf_put_u8(data->outbuf, data->password_hash ? EAP_PWD_PREP_MS :
  158. EAP_PWD_PREP_NONE);
  159. wpabuf_put_data(data->outbuf, data->id_server, data->id_server_len);
  160. }
  161. static void eap_pwd_build_commit_req(struct eap_sm *sm,
  162. struct eap_pwd_data *data, u8 id)
  163. {
  164. BIGNUM *mask = NULL, *x = NULL, *y = NULL;
  165. u8 *scalar = NULL, *element = NULL;
  166. u16 offset;
  167. wpa_printf(MSG_DEBUG, "EAP-pwd: Commit/Request");
  168. /*
  169. * if we're fragmenting then we already have an commit request, just
  170. * return
  171. */
  172. if (data->out_frag_pos)
  173. return;
  174. if (((data->private_value = BN_new()) == NULL) ||
  175. ((data->my_element = EC_POINT_new(data->grp->group)) == NULL) ||
  176. ((data->my_scalar = BN_new()) == NULL) ||
  177. ((mask = BN_new()) == NULL)) {
  178. wpa_printf(MSG_INFO, "EAP-PWD (server): scalar allocation "
  179. "fail");
  180. goto fin;
  181. }
  182. if (BN_rand_range(data->private_value, data->grp->order) != 1 ||
  183. BN_rand_range(mask, data->grp->order) != 1 ||
  184. BN_add(data->my_scalar, data->private_value, mask) != 1 ||
  185. BN_mod(data->my_scalar, data->my_scalar, data->grp->order,
  186. data->bnctx) != 1) {
  187. wpa_printf(MSG_INFO,
  188. "EAP-pwd (server): unable to get randomness");
  189. goto fin;
  190. }
  191. if (!EC_POINT_mul(data->grp->group, data->my_element, NULL,
  192. data->grp->pwe, mask, data->bnctx)) {
  193. wpa_printf(MSG_INFO, "EAP-PWD (server): element allocation "
  194. "fail");
  195. eap_pwd_state(data, FAILURE);
  196. goto fin;
  197. }
  198. if (!EC_POINT_invert(data->grp->group, data->my_element, data->bnctx))
  199. {
  200. wpa_printf(MSG_INFO, "EAP-PWD (server): element inversion "
  201. "fail");
  202. goto fin;
  203. }
  204. BN_clear_free(mask);
  205. if (((x = BN_new()) == NULL) ||
  206. ((y = BN_new()) == NULL)) {
  207. wpa_printf(MSG_INFO, "EAP-PWD (server): point allocation "
  208. "fail");
  209. goto fin;
  210. }
  211. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  212. data->my_element, x, y,
  213. data->bnctx)) {
  214. wpa_printf(MSG_INFO, "EAP-PWD (server): point assignment "
  215. "fail");
  216. goto fin;
  217. }
  218. if (((scalar = os_malloc(BN_num_bytes(data->grp->order))) == NULL) ||
  219. ((element = os_malloc(BN_num_bytes(data->grp->prime) * 2)) ==
  220. NULL)) {
  221. wpa_printf(MSG_INFO, "EAP-PWD (server): data allocation fail");
  222. goto fin;
  223. }
  224. /*
  225. * bignums occupy as little memory as possible so one that is
  226. * sufficiently smaller than the prime or order might need pre-pending
  227. * with zeros.
  228. */
  229. os_memset(scalar, 0, BN_num_bytes(data->grp->order));
  230. os_memset(element, 0, BN_num_bytes(data->grp->prime) * 2);
  231. offset = BN_num_bytes(data->grp->order) -
  232. BN_num_bytes(data->my_scalar);
  233. BN_bn2bin(data->my_scalar, scalar + offset);
  234. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  235. BN_bn2bin(x, element + offset);
  236. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  237. BN_bn2bin(y, element + BN_num_bytes(data->grp->prime) + offset);
  238. data->outbuf = wpabuf_alloc(2 * BN_num_bytes(data->grp->prime) +
  239. BN_num_bytes(data->grp->order));
  240. if (data->outbuf == NULL)
  241. goto fin;
  242. /* We send the element as (x,y) followed by the scalar */
  243. wpabuf_put_data(data->outbuf, element,
  244. 2 * BN_num_bytes(data->grp->prime));
  245. wpabuf_put_data(data->outbuf, scalar, BN_num_bytes(data->grp->order));
  246. fin:
  247. os_free(scalar);
  248. os_free(element);
  249. BN_clear_free(x);
  250. BN_clear_free(y);
  251. if (data->outbuf == NULL)
  252. eap_pwd_state(data, FAILURE);
  253. }
  254. static void eap_pwd_build_confirm_req(struct eap_sm *sm,
  255. struct eap_pwd_data *data, u8 id)
  256. {
  257. BIGNUM *x = NULL, *y = NULL;
  258. struct crypto_hash *hash;
  259. u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr;
  260. u16 grp;
  261. int offset;
  262. wpa_printf(MSG_DEBUG, "EAP-pwd: Confirm/Request");
  263. /*
  264. * if we're fragmenting then we already have an confirm request, just
  265. * return
  266. */
  267. if (data->out_frag_pos)
  268. return;
  269. /* Each component of the cruft will be at most as big as the prime */
  270. if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) ||
  271. ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
  272. wpa_printf(MSG_INFO, "EAP-PWD (server): debug allocation "
  273. "fail");
  274. goto fin;
  275. }
  276. /*
  277. * commit is H(k | server_element | server_scalar | peer_element |
  278. * peer_scalar | ciphersuite)
  279. */
  280. hash = eap_pwd_h_init();
  281. if (hash == NULL)
  282. goto fin;
  283. /*
  284. * Zero the memory each time because this is mod prime math and some
  285. * value may start with a few zeros and the previous one did not.
  286. *
  287. * First is k
  288. */
  289. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  290. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
  291. BN_bn2bin(data->k, cruft + offset);
  292. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  293. /* server element: x, y */
  294. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  295. data->my_element, x, y,
  296. data->bnctx)) {
  297. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  298. "assignment fail");
  299. goto fin;
  300. }
  301. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  302. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  303. BN_bn2bin(x, cruft + offset);
  304. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  305. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  306. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  307. BN_bn2bin(y, cruft + offset);
  308. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  309. /* server scalar */
  310. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  311. offset = BN_num_bytes(data->grp->order) -
  312. BN_num_bytes(data->my_scalar);
  313. BN_bn2bin(data->my_scalar, cruft + offset);
  314. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
  315. /* peer element: x, y */
  316. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  317. data->peer_element, x, y,
  318. data->bnctx)) {
  319. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  320. "assignment fail");
  321. goto fin;
  322. }
  323. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  324. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  325. BN_bn2bin(x, cruft + offset);
  326. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  327. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  328. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  329. BN_bn2bin(y, cruft + offset);
  330. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  331. /* peer scalar */
  332. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  333. offset = BN_num_bytes(data->grp->order) -
  334. BN_num_bytes(data->peer_scalar);
  335. BN_bn2bin(data->peer_scalar, cruft + offset);
  336. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
  337. /* ciphersuite */
  338. grp = htons(data->group_num);
  339. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  340. ptr = cruft;
  341. os_memcpy(ptr, &grp, sizeof(u16));
  342. ptr += sizeof(u16);
  343. *ptr = EAP_PWD_DEFAULT_RAND_FUNC;
  344. ptr += sizeof(u8);
  345. *ptr = EAP_PWD_DEFAULT_PRF;
  346. ptr += sizeof(u8);
  347. eap_pwd_h_update(hash, cruft, ptr - cruft);
  348. /* all done with the random function */
  349. eap_pwd_h_final(hash, conf);
  350. os_memcpy(data->my_confirm, conf, SHA256_MAC_LEN);
  351. data->outbuf = wpabuf_alloc(SHA256_MAC_LEN);
  352. if (data->outbuf == NULL)
  353. goto fin;
  354. wpabuf_put_data(data->outbuf, conf, SHA256_MAC_LEN);
  355. fin:
  356. bin_clear_free(cruft, BN_num_bytes(data->grp->prime));
  357. BN_clear_free(x);
  358. BN_clear_free(y);
  359. if (data->outbuf == NULL)
  360. eap_pwd_state(data, FAILURE);
  361. }
  362. static struct wpabuf *
  363. eap_pwd_build_req(struct eap_sm *sm, void *priv, u8 id)
  364. {
  365. struct eap_pwd_data *data = priv;
  366. struct wpabuf *req;
  367. u8 lm_exch;
  368. const u8 *buf;
  369. u16 totlen = 0;
  370. size_t len;
  371. /*
  372. * if we're buffering response fragments then just ACK
  373. */
  374. if (data->in_frag_pos) {
  375. wpa_printf(MSG_DEBUG, "EAP-pwd: ACKing a fragment!!");
  376. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
  377. EAP_PWD_HDR_SIZE, EAP_CODE_REQUEST, id);
  378. if (req == NULL) {
  379. eap_pwd_state(data, FAILURE);
  380. return NULL;
  381. }
  382. switch (data->state) {
  383. case PWD_ID_Req:
  384. wpabuf_put_u8(req, EAP_PWD_OPCODE_ID_EXCH);
  385. break;
  386. case PWD_Commit_Req:
  387. wpabuf_put_u8(req, EAP_PWD_OPCODE_COMMIT_EXCH);
  388. break;
  389. case PWD_Confirm_Req:
  390. wpabuf_put_u8(req, EAP_PWD_OPCODE_CONFIRM_EXCH);
  391. break;
  392. default:
  393. eap_pwd_state(data, FAILURE); /* just to be sure */
  394. wpabuf_free(req);
  395. return NULL;
  396. }
  397. return req;
  398. }
  399. /*
  400. * build the data portion of a request
  401. */
  402. switch (data->state) {
  403. case PWD_ID_Req:
  404. eap_pwd_build_id_req(sm, data, id);
  405. lm_exch = EAP_PWD_OPCODE_ID_EXCH;
  406. break;
  407. case PWD_Commit_Req:
  408. eap_pwd_build_commit_req(sm, data, id);
  409. lm_exch = EAP_PWD_OPCODE_COMMIT_EXCH;
  410. break;
  411. case PWD_Confirm_Req:
  412. eap_pwd_build_confirm_req(sm, data, id);
  413. lm_exch = EAP_PWD_OPCODE_CONFIRM_EXCH;
  414. break;
  415. default:
  416. wpa_printf(MSG_INFO, "EAP-pwd: Unknown state %d in build_req",
  417. data->state);
  418. eap_pwd_state(data, FAILURE);
  419. lm_exch = 0; /* hush now, sweet compiler */
  420. break;
  421. }
  422. if (data->state == FAILURE)
  423. return NULL;
  424. /*
  425. * determine whether that data needs to be fragmented
  426. */
  427. len = wpabuf_len(data->outbuf) - data->out_frag_pos;
  428. if ((len + EAP_PWD_HDR_SIZE) > data->mtu) {
  429. len = data->mtu - EAP_PWD_HDR_SIZE;
  430. EAP_PWD_SET_MORE_BIT(lm_exch);
  431. /*
  432. * if this is the first fragment, need to set the M bit
  433. * and add the total length to the eap_pwd_hdr
  434. */
  435. if (data->out_frag_pos == 0) {
  436. EAP_PWD_SET_LENGTH_BIT(lm_exch);
  437. totlen = wpabuf_len(data->outbuf) +
  438. EAP_PWD_HDR_SIZE + sizeof(u16);
  439. len -= sizeof(u16);
  440. wpa_printf(MSG_DEBUG, "EAP-pwd: Fragmenting output, "
  441. "total length = %d", totlen);
  442. }
  443. wpa_printf(MSG_DEBUG, "EAP-pwd: Send a %d byte fragment",
  444. (int) len);
  445. }
  446. /*
  447. * alloc an eap request and populate it with the data
  448. */
  449. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD,
  450. EAP_PWD_HDR_SIZE + len +
  451. (totlen ? sizeof(u16) : 0),
  452. EAP_CODE_REQUEST, id);
  453. if (req == NULL) {
  454. eap_pwd_state(data, FAILURE);
  455. return NULL;
  456. }
  457. wpabuf_put_u8(req, lm_exch);
  458. if (EAP_PWD_GET_LENGTH_BIT(lm_exch))
  459. wpabuf_put_be16(req, totlen);
  460. buf = wpabuf_head_u8(data->outbuf);
  461. wpabuf_put_data(req, buf + data->out_frag_pos, len);
  462. data->out_frag_pos += len;
  463. /*
  464. * either not fragged or last fragment, either way free up the data
  465. */
  466. if (data->out_frag_pos >= wpabuf_len(data->outbuf)) {
  467. wpabuf_free(data->outbuf);
  468. data->outbuf = NULL;
  469. data->out_frag_pos = 0;
  470. }
  471. return req;
  472. }
  473. static Boolean eap_pwd_check(struct eap_sm *sm, void *priv,
  474. struct wpabuf *respData)
  475. {
  476. struct eap_pwd_data *data = priv;
  477. const u8 *pos;
  478. size_t len;
  479. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len);
  480. if (pos == NULL || len < 1) {
  481. wpa_printf(MSG_INFO, "EAP-pwd: Invalid frame");
  482. return TRUE;
  483. }
  484. wpa_printf(MSG_DEBUG, "EAP-pwd: Received frame: exch = %d, len = %d",
  485. EAP_PWD_GET_EXCHANGE(*pos), (int) len);
  486. if (data->state == PWD_ID_Req &&
  487. ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_ID_EXCH))
  488. return FALSE;
  489. if (data->state == PWD_Commit_Req &&
  490. ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_COMMIT_EXCH))
  491. return FALSE;
  492. if (data->state == PWD_Confirm_Req &&
  493. ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_CONFIRM_EXCH))
  494. return FALSE;
  495. wpa_printf(MSG_INFO, "EAP-pwd: Unexpected opcode=%d in state=%d",
  496. *pos, data->state);
  497. return TRUE;
  498. }
  499. static void eap_pwd_process_id_resp(struct eap_sm *sm,
  500. struct eap_pwd_data *data,
  501. const u8 *payload, size_t payload_len)
  502. {
  503. struct eap_pwd_id *id;
  504. const u8 *password;
  505. size_t password_len;
  506. u8 pwhashhash[16];
  507. int res;
  508. if (payload_len < sizeof(struct eap_pwd_id)) {
  509. wpa_printf(MSG_INFO, "EAP-pwd: Invalid ID response");
  510. return;
  511. }
  512. id = (struct eap_pwd_id *) payload;
  513. if ((data->group_num != be_to_host16(id->group_num)) ||
  514. (id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) ||
  515. (os_memcmp(id->token, (u8 *)&data->token, sizeof(data->token))) ||
  516. (id->prf != EAP_PWD_DEFAULT_PRF)) {
  517. wpa_printf(MSG_INFO, "EAP-pwd: peer changed parameters");
  518. eap_pwd_state(data, FAILURE);
  519. return;
  520. }
  521. data->id_peer = os_malloc(payload_len - sizeof(struct eap_pwd_id));
  522. if (data->id_peer == NULL) {
  523. wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail");
  524. return;
  525. }
  526. data->id_peer_len = payload_len - sizeof(struct eap_pwd_id);
  527. os_memcpy(data->id_peer, id->identity, data->id_peer_len);
  528. wpa_hexdump_ascii(MSG_DEBUG, "EAP-PWD (server): peer sent id of",
  529. data->id_peer, data->id_peer_len);
  530. data->grp = os_zalloc(sizeof(EAP_PWD_group));
  531. if (data->grp == NULL) {
  532. wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for "
  533. "group");
  534. return;
  535. }
  536. if (data->password_hash) {
  537. res = hash_nt_password_hash(data->password, pwhashhash);
  538. if (res)
  539. return;
  540. password = pwhashhash;
  541. password_len = sizeof(pwhashhash);
  542. } else {
  543. password = data->password;
  544. password_len = data->password_len;
  545. }
  546. res = compute_password_element(data->grp, data->group_num,
  547. password, password_len,
  548. data->id_server, data->id_server_len,
  549. data->id_peer, data->id_peer_len,
  550. (u8 *) &data->token);
  551. os_memset(pwhashhash, 0, sizeof(pwhashhash));
  552. if (res) {
  553. wpa_printf(MSG_INFO, "EAP-PWD (server): unable to compute "
  554. "PWE");
  555. return;
  556. }
  557. wpa_printf(MSG_DEBUG, "EAP-PWD (server): computed %d bit PWE...",
  558. BN_num_bits(data->grp->prime));
  559. eap_pwd_state(data, PWD_Commit_Req);
  560. }
  561. static void
  562. eap_pwd_process_commit_resp(struct eap_sm *sm, struct eap_pwd_data *data,
  563. const u8 *payload, size_t payload_len)
  564. {
  565. u8 *ptr;
  566. BIGNUM *x = NULL, *y = NULL, *cofactor = NULL;
  567. EC_POINT *K = NULL, *point = NULL;
  568. int res = 0;
  569. size_t prime_len, order_len;
  570. wpa_printf(MSG_DEBUG, "EAP-pwd: Received commit response");
  571. prime_len = BN_num_bytes(data->grp->prime);
  572. order_len = BN_num_bytes(data->grp->order);
  573. if (payload_len != 2 * prime_len + order_len) {
  574. wpa_printf(MSG_INFO,
  575. "EAP-pwd: Unexpected Commit payload length %u (expected %u)",
  576. (unsigned int) payload_len,
  577. (unsigned int) (2 * prime_len + order_len));
  578. goto fin;
  579. }
  580. if (((data->peer_scalar = BN_new()) == NULL) ||
  581. ((data->k = BN_new()) == NULL) ||
  582. ((cofactor = BN_new()) == NULL) ||
  583. ((x = BN_new()) == NULL) ||
  584. ((y = BN_new()) == NULL) ||
  585. ((point = EC_POINT_new(data->grp->group)) == NULL) ||
  586. ((K = EC_POINT_new(data->grp->group)) == NULL) ||
  587. ((data->peer_element = EC_POINT_new(data->grp->group)) == NULL)) {
  588. wpa_printf(MSG_INFO, "EAP-PWD (server): peer data allocation "
  589. "fail");
  590. goto fin;
  591. }
  592. if (!EC_GROUP_get_cofactor(data->grp->group, cofactor, NULL)) {
  593. wpa_printf(MSG_INFO, "EAP-PWD (server): unable to get "
  594. "cofactor for curve");
  595. goto fin;
  596. }
  597. /* element, x then y, followed by scalar */
  598. ptr = (u8 *) payload;
  599. BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), x);
  600. ptr += BN_num_bytes(data->grp->prime);
  601. BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), y);
  602. ptr += BN_num_bytes(data->grp->prime);
  603. BN_bin2bn(ptr, BN_num_bytes(data->grp->order), data->peer_scalar);
  604. if (!EC_POINT_set_affine_coordinates_GFp(data->grp->group,
  605. data->peer_element, x, y,
  606. data->bnctx)) {
  607. wpa_printf(MSG_INFO, "EAP-PWD (server): setting peer element "
  608. "fail");
  609. goto fin;
  610. }
  611. /* check to ensure peer's element is not in a small sub-group */
  612. if (BN_cmp(cofactor, BN_value_one())) {
  613. if (!EC_POINT_mul(data->grp->group, point, NULL,
  614. data->peer_element, cofactor, NULL)) {
  615. wpa_printf(MSG_INFO, "EAP-PWD (server): cannot "
  616. "multiply peer element by order");
  617. goto fin;
  618. }
  619. if (EC_POINT_is_at_infinity(data->grp->group, point)) {
  620. wpa_printf(MSG_INFO, "EAP-PWD (server): peer element "
  621. "is at infinity!\n");
  622. goto fin;
  623. }
  624. }
  625. /* compute the shared key, k */
  626. if ((!EC_POINT_mul(data->grp->group, K, NULL, data->grp->pwe,
  627. data->peer_scalar, data->bnctx)) ||
  628. (!EC_POINT_add(data->grp->group, K, K, data->peer_element,
  629. data->bnctx)) ||
  630. (!EC_POINT_mul(data->grp->group, K, NULL, K, data->private_value,
  631. data->bnctx))) {
  632. wpa_printf(MSG_INFO, "EAP-PWD (server): computing shared key "
  633. "fail");
  634. goto fin;
  635. }
  636. /* ensure that the shared key isn't in a small sub-group */
  637. if (BN_cmp(cofactor, BN_value_one())) {
  638. if (!EC_POINT_mul(data->grp->group, K, NULL, K, cofactor,
  639. NULL)) {
  640. wpa_printf(MSG_INFO, "EAP-PWD (server): cannot "
  641. "multiply shared key point by order!\n");
  642. goto fin;
  643. }
  644. }
  645. /*
  646. * This check is strictly speaking just for the case above where
  647. * co-factor > 1 but it was suggested that even though this is probably
  648. * never going to happen it is a simple and safe check "just to be
  649. * sure" so let's be safe.
  650. */
  651. if (EC_POINT_is_at_infinity(data->grp->group, K)) {
  652. wpa_printf(MSG_INFO, "EAP-PWD (server): shared key point is "
  653. "at infinity");
  654. goto fin;
  655. }
  656. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, K, data->k,
  657. NULL, data->bnctx)) {
  658. wpa_printf(MSG_INFO, "EAP-PWD (server): unable to extract "
  659. "shared secret from secret point");
  660. goto fin;
  661. }
  662. res = 1;
  663. fin:
  664. EC_POINT_clear_free(K);
  665. EC_POINT_clear_free(point);
  666. BN_clear_free(cofactor);
  667. BN_clear_free(x);
  668. BN_clear_free(y);
  669. if (res)
  670. eap_pwd_state(data, PWD_Confirm_Req);
  671. else
  672. eap_pwd_state(data, FAILURE);
  673. }
  674. static void
  675. eap_pwd_process_confirm_resp(struct eap_sm *sm, struct eap_pwd_data *data,
  676. const u8 *payload, size_t payload_len)
  677. {
  678. BIGNUM *x = NULL, *y = NULL;
  679. struct crypto_hash *hash;
  680. u32 cs;
  681. u16 grp;
  682. u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr;
  683. int offset;
  684. if (payload_len != SHA256_MAC_LEN) {
  685. wpa_printf(MSG_INFO,
  686. "EAP-pwd: Unexpected Confirm payload length %u (expected %u)",
  687. (unsigned int) payload_len, SHA256_MAC_LEN);
  688. goto fin;
  689. }
  690. /* build up the ciphersuite: group | random_function | prf */
  691. grp = htons(data->group_num);
  692. ptr = (u8 *) &cs;
  693. os_memcpy(ptr, &grp, sizeof(u16));
  694. ptr += sizeof(u16);
  695. *ptr = EAP_PWD_DEFAULT_RAND_FUNC;
  696. ptr += sizeof(u8);
  697. *ptr = EAP_PWD_DEFAULT_PRF;
  698. /* each component of the cruft will be at most as big as the prime */
  699. if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) ||
  700. ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) {
  701. wpa_printf(MSG_INFO, "EAP-PWD (peer): allocation fail");
  702. goto fin;
  703. }
  704. /*
  705. * commit is H(k | peer_element | peer_scalar | server_element |
  706. * server_scalar | ciphersuite)
  707. */
  708. hash = eap_pwd_h_init();
  709. if (hash == NULL)
  710. goto fin;
  711. /* k */
  712. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  713. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(data->k);
  714. BN_bn2bin(data->k, cruft + offset);
  715. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  716. /* peer element: x, y */
  717. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  718. data->peer_element, x, y,
  719. data->bnctx)) {
  720. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  721. "assignment fail");
  722. goto fin;
  723. }
  724. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  725. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  726. BN_bn2bin(x, cruft + offset);
  727. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  728. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  729. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  730. BN_bn2bin(y, cruft + offset);
  731. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  732. /* peer scalar */
  733. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  734. offset = BN_num_bytes(data->grp->order) -
  735. BN_num_bytes(data->peer_scalar);
  736. BN_bn2bin(data->peer_scalar, cruft + offset);
  737. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
  738. /* server element: x, y */
  739. if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group,
  740. data->my_element, x, y,
  741. data->bnctx)) {
  742. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point "
  743. "assignment fail");
  744. goto fin;
  745. }
  746. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  747. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x);
  748. BN_bn2bin(x, cruft + offset);
  749. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  750. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  751. offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y);
  752. BN_bn2bin(y, cruft + offset);
  753. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->prime));
  754. /* server scalar */
  755. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  756. offset = BN_num_bytes(data->grp->order) -
  757. BN_num_bytes(data->my_scalar);
  758. BN_bn2bin(data->my_scalar, cruft + offset);
  759. eap_pwd_h_update(hash, cruft, BN_num_bytes(data->grp->order));
  760. /* ciphersuite */
  761. os_memset(cruft, 0, BN_num_bytes(data->grp->prime));
  762. eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32));
  763. /* all done */
  764. eap_pwd_h_final(hash, conf);
  765. ptr = (u8 *) payload;
  766. if (os_memcmp_const(conf, ptr, SHA256_MAC_LEN)) {
  767. wpa_printf(MSG_INFO, "EAP-PWD (server): confirm did not "
  768. "verify");
  769. goto fin;
  770. }
  771. wpa_printf(MSG_DEBUG, "EAP-pwd (server): confirm verified");
  772. if (compute_keys(data->grp, data->bnctx, data->k,
  773. data->peer_scalar, data->my_scalar, conf,
  774. data->my_confirm, &cs, data->msk, data->emsk,
  775. data->session_id) < 0)
  776. eap_pwd_state(data, FAILURE);
  777. else
  778. eap_pwd_state(data, SUCCESS);
  779. fin:
  780. bin_clear_free(cruft, BN_num_bytes(data->grp->prime));
  781. BN_clear_free(x);
  782. BN_clear_free(y);
  783. }
  784. static void eap_pwd_process(struct eap_sm *sm, void *priv,
  785. struct wpabuf *respData)
  786. {
  787. struct eap_pwd_data *data = priv;
  788. const u8 *pos;
  789. size_t len;
  790. u8 lm_exch;
  791. u16 tot_len;
  792. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len);
  793. if ((pos == NULL) || (len < 1)) {
  794. wpa_printf(MSG_INFO, "Bad EAP header! pos %s and len = %d",
  795. (pos == NULL) ? "is NULL" : "is not NULL",
  796. (int) len);
  797. return;
  798. }
  799. lm_exch = *pos;
  800. pos++; /* skip over the bits and the exch */
  801. len--;
  802. /*
  803. * if we're fragmenting then this should be an ACK with no data,
  804. * just return and continue fragmenting in the "build" section above
  805. */
  806. if (data->out_frag_pos) {
  807. if (len > 1)
  808. wpa_printf(MSG_INFO, "EAP-pwd: Bad response! "
  809. "Fragmenting but not an ACK");
  810. else
  811. wpa_printf(MSG_DEBUG, "EAP-pwd: received ACK from "
  812. "peer");
  813. return;
  814. }
  815. /*
  816. * if we're receiving fragmented packets then we need to buffer...
  817. *
  818. * the first fragment has a total length
  819. */
  820. if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) {
  821. if (len < 2) {
  822. wpa_printf(MSG_DEBUG,
  823. "EAP-pwd: Frame too short to contain Total-Length field");
  824. return;
  825. }
  826. tot_len = WPA_GET_BE16(pos);
  827. wpa_printf(MSG_DEBUG, "EAP-pwd: Incoming fragments, total "
  828. "length = %d", tot_len);
  829. if (tot_len > 15000)
  830. return;
  831. if (data->inbuf) {
  832. wpa_printf(MSG_DEBUG,
  833. "EAP-pwd: Unexpected new fragment start when previous fragment is still in use");
  834. return;
  835. }
  836. data->inbuf = wpabuf_alloc(tot_len);
  837. if (data->inbuf == NULL) {
  838. wpa_printf(MSG_INFO, "EAP-pwd: Out of memory to "
  839. "buffer fragments!");
  840. return;
  841. }
  842. data->in_frag_pos = 0;
  843. pos += sizeof(u16);
  844. len -= sizeof(u16);
  845. }
  846. /*
  847. * the first and all intermediate fragments have the M bit set
  848. */
  849. if (EAP_PWD_GET_MORE_BIT(lm_exch)) {
  850. if ((data->in_frag_pos + len) > wpabuf_size(data->inbuf)) {
  851. wpa_printf(MSG_DEBUG, "EAP-pwd: Buffer overflow "
  852. "attack detected! (%d+%d > %d)",
  853. (int) data->in_frag_pos, (int) len,
  854. (int) wpabuf_size(data->inbuf));
  855. eap_pwd_state(data, FAILURE);
  856. return;
  857. }
  858. wpabuf_put_data(data->inbuf, pos, len);
  859. data->in_frag_pos += len;
  860. wpa_printf(MSG_DEBUG, "EAP-pwd: Got a %d byte fragment",
  861. (int) len);
  862. return;
  863. }
  864. /*
  865. * last fragment won't have the M bit set (but we're obviously
  866. * buffering fragments so that's how we know it's the last)
  867. */
  868. if (data->in_frag_pos) {
  869. wpabuf_put_data(data->inbuf, pos, len);
  870. data->in_frag_pos += len;
  871. pos = wpabuf_head_u8(data->inbuf);
  872. len = data->in_frag_pos;
  873. wpa_printf(MSG_DEBUG, "EAP-pwd: Last fragment, %d bytes",
  874. (int) len);
  875. }
  876. switch (EAP_PWD_GET_EXCHANGE(lm_exch)) {
  877. case EAP_PWD_OPCODE_ID_EXCH:
  878. eap_pwd_process_id_resp(sm, data, pos, len);
  879. break;
  880. case EAP_PWD_OPCODE_COMMIT_EXCH:
  881. eap_pwd_process_commit_resp(sm, data, pos, len);
  882. break;
  883. case EAP_PWD_OPCODE_CONFIRM_EXCH:
  884. eap_pwd_process_confirm_resp(sm, data, pos, len);
  885. break;
  886. }
  887. /*
  888. * if we had been buffering fragments, here's a great place
  889. * to clean up
  890. */
  891. if (data->in_frag_pos) {
  892. wpabuf_free(data->inbuf);
  893. data->inbuf = NULL;
  894. data->in_frag_pos = 0;
  895. }
  896. }
  897. static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len)
  898. {
  899. struct eap_pwd_data *data = priv;
  900. u8 *key;
  901. if (data->state != SUCCESS)
  902. return NULL;
  903. key = os_malloc(EAP_MSK_LEN);
  904. if (key == NULL)
  905. return NULL;
  906. os_memcpy(key, data->msk, EAP_MSK_LEN);
  907. *len = EAP_MSK_LEN;
  908. return key;
  909. }
  910. static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  911. {
  912. struct eap_pwd_data *data = priv;
  913. u8 *key;
  914. if (data->state != SUCCESS)
  915. return NULL;
  916. key = os_malloc(EAP_EMSK_LEN);
  917. if (key == NULL)
  918. return NULL;
  919. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  920. *len = EAP_EMSK_LEN;
  921. return key;
  922. }
  923. static Boolean eap_pwd_is_success(struct eap_sm *sm, void *priv)
  924. {
  925. struct eap_pwd_data *data = priv;
  926. return data->state == SUCCESS;
  927. }
  928. static Boolean eap_pwd_is_done(struct eap_sm *sm, void *priv)
  929. {
  930. struct eap_pwd_data *data = priv;
  931. return (data->state == SUCCESS) || (data->state == FAILURE);
  932. }
  933. static u8 * eap_pwd_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  934. {
  935. struct eap_pwd_data *data = priv;
  936. u8 *id;
  937. if (data->state != SUCCESS)
  938. return NULL;
  939. id = os_malloc(1 + SHA256_MAC_LEN);
  940. if (id == NULL)
  941. return NULL;
  942. os_memcpy(id, data->session_id, 1 + SHA256_MAC_LEN);
  943. *len = 1 + SHA256_MAC_LEN;
  944. return id;
  945. }
  946. int eap_server_pwd_register(void)
  947. {
  948. struct eap_method *eap;
  949. int ret;
  950. struct timeval tp;
  951. struct timezone tz;
  952. u32 sr;
  953. sr = 0xdeaddada;
  954. (void) gettimeofday(&tp, &tz);
  955. sr ^= (tp.tv_sec ^ tp.tv_usec);
  956. srandom(sr);
  957. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  958. EAP_VENDOR_IETF, EAP_TYPE_PWD,
  959. "PWD");
  960. if (eap == NULL)
  961. return -1;
  962. eap->init = eap_pwd_init;
  963. eap->reset = eap_pwd_reset;
  964. eap->buildReq = eap_pwd_build_req;
  965. eap->check = eap_pwd_check;
  966. eap->process = eap_pwd_process;
  967. eap->isDone = eap_pwd_is_done;
  968. eap->getKey = eap_pwd_getkey;
  969. eap->get_emsk = eap_pwd_get_emsk;
  970. eap->isSuccess = eap_pwd_is_success;
  971. eap->getSessionId = eap_pwd_get_session_id;
  972. ret = eap_server_method_register(eap);
  973. if (ret)
  974. eap_server_method_free(eap);
  975. return ret;
  976. }