eap_wsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * EAP-WSC peer for Wi-Fi Protected Setup
  3. * Copyright (c) 2007-2009, 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 "uuid.h"
  17. #include "eap_i.h"
  18. #include "eap_common/eap_wsc_common.h"
  19. #include "wps/wps.h"
  20. #include "wps/wps_defs.h"
  21. struct eap_wsc_data {
  22. enum { WAIT_START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
  23. int registrar;
  24. struct wpabuf *in_buf;
  25. struct wpabuf *out_buf;
  26. enum wsc_op_code in_op_code, out_op_code;
  27. size_t out_used;
  28. size_t fragment_size;
  29. struct wps_data *wps;
  30. struct wps_context *wps_ctx;
  31. };
  32. static const char * eap_wsc_state_txt(int state)
  33. {
  34. switch (state) {
  35. case WAIT_START:
  36. return "WAIT_START";
  37. case MESG:
  38. return "MESG";
  39. case FRAG_ACK:
  40. return "FRAG_ACK";
  41. case WAIT_FRAG_ACK:
  42. return "WAIT_FRAG_ACK";
  43. case DONE:
  44. return "DONE";
  45. case FAIL:
  46. return "FAIL";
  47. default:
  48. return "?";
  49. }
  50. }
  51. static void eap_wsc_state(struct eap_wsc_data *data, int state)
  52. {
  53. wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
  54. eap_wsc_state_txt(data->state),
  55. eap_wsc_state_txt(state));
  56. data->state = state;
  57. }
  58. static int eap_wsc_new_ap_settings(struct wps_credential *cred,
  59. const char *params)
  60. {
  61. const char *pos, *end;
  62. size_t len;
  63. os_memset(cred, 0, sizeof(*cred));
  64. pos = os_strstr(params, "new_ssid=");
  65. if (pos == NULL)
  66. return 0;
  67. pos += 9;
  68. end = os_strchr(pos, ' ');
  69. if (end == NULL)
  70. len = os_strlen(pos);
  71. else
  72. len = end - pos;
  73. if ((len & 1) || len > 2 * sizeof(cred->ssid) ||
  74. hexstr2bin(pos, cred->ssid, len / 2))
  75. return -1;
  76. cred->ssid_len = len / 2;
  77. pos = os_strstr(params, "new_auth=");
  78. if (pos == NULL)
  79. return -1;
  80. if (os_strncmp(pos + 9, "OPEN", 4) == 0)
  81. cred->auth_type = WPS_AUTH_OPEN;
  82. else if (os_strncmp(pos + 9, "WPAPSK", 6) == 0)
  83. cred->auth_type = WPS_AUTH_WPAPSK;
  84. else if (os_strncmp(pos + 9, "WPA2PSK", 7) == 0)
  85. cred->auth_type = WPS_AUTH_WPA2PSK;
  86. else
  87. return -1;
  88. pos = os_strstr(params, "new_encr=");
  89. if (pos == NULL)
  90. return -1;
  91. if (os_strncmp(pos + 9, "NONE", 4) == 0)
  92. cred->encr_type = WPS_ENCR_NONE;
  93. else if (os_strncmp(pos + 9, "WEP", 3) == 0)
  94. cred->encr_type = WPS_ENCR_WEP;
  95. else if (os_strncmp(pos + 9, "TKIP", 4) == 0)
  96. cred->encr_type = WPS_ENCR_TKIP;
  97. else if (os_strncmp(pos + 9, "CCMP", 4) == 0)
  98. cred->encr_type = WPS_ENCR_AES;
  99. else
  100. return -1;
  101. pos = os_strstr(params, "new_key=");
  102. if (pos == NULL)
  103. return 0;
  104. pos += 8;
  105. end = os_strchr(pos, ' ');
  106. if (end == NULL)
  107. len = os_strlen(pos);
  108. else
  109. len = end - pos;
  110. if ((len & 1) || len > 2 * sizeof(cred->key) ||
  111. hexstr2bin(pos, cred->key, len / 2))
  112. return -1;
  113. cred->key_len = len / 2;
  114. return 1;
  115. }
  116. static void * eap_wsc_init(struct eap_sm *sm)
  117. {
  118. struct eap_wsc_data *data;
  119. const u8 *identity;
  120. size_t identity_len;
  121. int registrar;
  122. struct wps_config cfg;
  123. const char *pos;
  124. const char *phase1;
  125. struct wps_context *wps;
  126. struct wps_credential new_ap_settings;
  127. int res;
  128. wps = sm->wps;
  129. if (wps == NULL) {
  130. wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
  131. return NULL;
  132. }
  133. identity = eap_get_config_identity(sm, &identity_len);
  134. if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
  135. os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
  136. registrar = 1; /* Supplicant is Registrar */
  137. else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
  138. os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
  139. registrar = 0; /* Supplicant is Enrollee */
  140. else {
  141. wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
  142. identity, identity_len);
  143. return NULL;
  144. }
  145. data = os_zalloc(sizeof(*data));
  146. if (data == NULL)
  147. return NULL;
  148. data->state = registrar ? MESG : WAIT_START;
  149. data->registrar = registrar;
  150. data->wps_ctx = wps;
  151. os_memset(&cfg, 0, sizeof(cfg));
  152. cfg.wps = wps;
  153. cfg.registrar = registrar;
  154. phase1 = eap_get_config_phase1(sm);
  155. if (phase1 == NULL) {
  156. wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
  157. "set");
  158. os_free(data);
  159. return NULL;
  160. }
  161. pos = os_strstr(phase1, "pin=");
  162. if (pos) {
  163. pos += 4;
  164. cfg.pin = (const u8 *) pos;
  165. while (*pos != '\0' && *pos != ' ')
  166. pos++;
  167. cfg.pin_len = pos - (const char *) cfg.pin;
  168. } else {
  169. pos = os_strstr(phase1, "pbc=1");
  170. if (pos)
  171. cfg.pbc = 1;
  172. }
  173. if (cfg.pin == NULL && !cfg.pbc) {
  174. wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
  175. "configuration data");
  176. os_free(data);
  177. return NULL;
  178. }
  179. pos = os_strstr(phase1, "dev_pw_id=");
  180. if (pos && cfg.pin)
  181. cfg.dev_pw_id = atoi(pos + 10);
  182. res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
  183. if (res < 0) {
  184. os_free(data);
  185. return NULL;
  186. }
  187. if (res == 1) {
  188. wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
  189. "WPS");
  190. cfg.new_ap_settings = &new_ap_settings;
  191. }
  192. data->wps = wps_init(&cfg);
  193. if (data->wps == NULL) {
  194. os_free(data);
  195. return NULL;
  196. }
  197. res = eap_get_config_fragment_size(sm);
  198. if (res > 0)
  199. data->fragment_size = res;
  200. else
  201. data->fragment_size = WSC_FRAGMENT_SIZE;
  202. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
  203. (unsigned int) data->fragment_size);
  204. if (registrar && cfg.pin) {
  205. wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
  206. cfg.pin, cfg.pin_len, 0);
  207. }
  208. /* Use reduced client timeout for WPS to avoid long wait */
  209. if (sm->ClientTimeout > 30)
  210. sm->ClientTimeout = 30;
  211. return data;
  212. }
  213. static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
  214. {
  215. struct eap_wsc_data *data = priv;
  216. wpabuf_free(data->in_buf);
  217. wpabuf_free(data->out_buf);
  218. wps_deinit(data->wps);
  219. os_free(data->wps_ctx->network_key);
  220. data->wps_ctx->network_key = NULL;
  221. os_free(data);
  222. }
  223. static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
  224. struct eap_method_ret *ret, u8 id)
  225. {
  226. struct wpabuf *resp;
  227. u8 flags;
  228. size_t send_len, plen;
  229. ret->ignore = FALSE;
  230. wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
  231. ret->allowNotifications = TRUE;
  232. flags = 0;
  233. send_len = wpabuf_len(data->out_buf) - data->out_used;
  234. if (2 + send_len > data->fragment_size) {
  235. send_len = data->fragment_size - 2;
  236. flags |= WSC_FLAGS_MF;
  237. if (data->out_used == 0) {
  238. flags |= WSC_FLAGS_LF;
  239. send_len -= 2;
  240. }
  241. }
  242. plen = 2 + send_len;
  243. if (flags & WSC_FLAGS_LF)
  244. plen += 2;
  245. resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
  246. EAP_CODE_RESPONSE, id);
  247. if (resp == NULL)
  248. return NULL;
  249. wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
  250. wpabuf_put_u8(resp, flags); /* Flags */
  251. if (flags & WSC_FLAGS_LF)
  252. wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
  253. wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
  254. send_len);
  255. data->out_used += send_len;
  256. ret->methodState = METHOD_MAY_CONT;
  257. ret->decision = DECISION_FAIL;
  258. if (data->out_used == wpabuf_len(data->out_buf)) {
  259. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  260. "(message sent completely)",
  261. (unsigned long) send_len);
  262. wpabuf_free(data->out_buf);
  263. data->out_buf = NULL;
  264. data->out_used = 0;
  265. if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
  266. data->out_op_code == WSC_NACK ||
  267. data->out_op_code == WSC_Done) {
  268. eap_wsc_state(data, FAIL);
  269. ret->methodState = METHOD_DONE;
  270. } else
  271. eap_wsc_state(data, MESG);
  272. } else {
  273. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  274. "(%lu more to send)", (unsigned long) send_len,
  275. (unsigned long) wpabuf_len(data->out_buf) -
  276. data->out_used);
  277. eap_wsc_state(data, WAIT_FRAG_ACK);
  278. }
  279. return resp;
  280. }
  281. static int eap_wsc_process_cont(struct eap_wsc_data *data,
  282. const u8 *buf, size_t len, u8 op_code)
  283. {
  284. /* Process continuation of a pending message */
  285. if (op_code != data->in_op_code) {
  286. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
  287. "fragment (expected %d)",
  288. op_code, data->in_op_code);
  289. return -1;
  290. }
  291. if (len > wpabuf_tailroom(data->in_buf)) {
  292. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
  293. eap_wsc_state(data, FAIL);
  294. return -1;
  295. }
  296. wpabuf_put_data(data->in_buf, buf, len);
  297. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
  298. "for %lu bytes more", (unsigned long) len,
  299. (unsigned long) wpabuf_tailroom(data->in_buf));
  300. return 0;
  301. }
  302. static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
  303. struct eap_method_ret *ret,
  304. u8 id, u8 flags, u8 op_code,
  305. u16 message_length,
  306. const u8 *buf, size_t len)
  307. {
  308. /* Process a fragment that is not the last one of the message */
  309. if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
  310. wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
  311. "fragmented packet");
  312. ret->ignore = TRUE;
  313. return NULL;
  314. }
  315. if (data->in_buf == NULL) {
  316. /* First fragment of the message */
  317. data->in_buf = wpabuf_alloc(message_length);
  318. if (data->in_buf == NULL) {
  319. wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
  320. "message");
  321. ret->ignore = TRUE;
  322. return NULL;
  323. }
  324. data->in_op_code = op_code;
  325. wpabuf_put_data(data->in_buf, buf, len);
  326. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
  327. "fragment, waiting for %lu bytes more",
  328. (unsigned long) len,
  329. (unsigned long) wpabuf_tailroom(data->in_buf));
  330. }
  331. return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
  332. }
  333. static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
  334. struct eap_method_ret *ret,
  335. const struct wpabuf *reqData)
  336. {
  337. struct eap_wsc_data *data = priv;
  338. const u8 *start, *pos, *end;
  339. size_t len;
  340. u8 op_code, flags, id;
  341. u16 message_length = 0;
  342. enum wps_process_res res;
  343. struct wpabuf tmpbuf;
  344. struct wpabuf *r;
  345. pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
  346. &len);
  347. if (pos == NULL || len < 2) {
  348. ret->ignore = TRUE;
  349. return NULL;
  350. }
  351. id = eap_get_id(reqData);
  352. start = pos;
  353. end = start + len;
  354. op_code = *pos++;
  355. flags = *pos++;
  356. if (flags & WSC_FLAGS_LF) {
  357. if (end - pos < 2) {
  358. wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
  359. ret->ignore = TRUE;
  360. return NULL;
  361. }
  362. message_length = WPA_GET_BE16(pos);
  363. pos += 2;
  364. if (message_length < end - pos) {
  365. wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
  366. "Length");
  367. ret->ignore = TRUE;
  368. return NULL;
  369. }
  370. }
  371. wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
  372. "Flags 0x%x Message Length %d",
  373. op_code, flags, message_length);
  374. if (data->state == WAIT_FRAG_ACK) {
  375. if (op_code != WSC_FRAG_ACK) {
  376. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
  377. "in WAIT_FRAG_ACK state", op_code);
  378. ret->ignore = TRUE;
  379. return NULL;
  380. }
  381. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
  382. eap_wsc_state(data, MESG);
  383. return eap_wsc_build_msg(data, ret, id);
  384. }
  385. if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
  386. op_code != WSC_Done && op_code != WSC_Start) {
  387. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
  388. op_code);
  389. ret->ignore = TRUE;
  390. return NULL;
  391. }
  392. if (data->state == WAIT_START) {
  393. if (op_code != WSC_Start) {
  394. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
  395. "in WAIT_START state", op_code);
  396. ret->ignore = TRUE;
  397. return NULL;
  398. }
  399. wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
  400. eap_wsc_state(data, MESG);
  401. /* Start message has empty payload, skip processing */
  402. goto send_msg;
  403. } else if (op_code == WSC_Start) {
  404. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
  405. op_code);
  406. ret->ignore = TRUE;
  407. return NULL;
  408. }
  409. if (data->in_buf &&
  410. eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
  411. ret->ignore = TRUE;
  412. return NULL;
  413. }
  414. if (flags & WSC_FLAGS_MF) {
  415. return eap_wsc_process_fragment(data, ret, id, flags, op_code,
  416. message_length, pos,
  417. end - pos);
  418. }
  419. if (data->in_buf == NULL) {
  420. /* Wrap unfragmented messages as wpabuf without extra copy */
  421. wpabuf_set(&tmpbuf, pos, end - pos);
  422. data->in_buf = &tmpbuf;
  423. }
  424. res = wps_process_msg(data->wps, op_code, data->in_buf);
  425. switch (res) {
  426. case WPS_DONE:
  427. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
  428. "successfully - wait for EAP failure");
  429. eap_wsc_state(data, FAIL);
  430. break;
  431. case WPS_CONTINUE:
  432. eap_wsc_state(data, MESG);
  433. break;
  434. case WPS_FAILURE:
  435. case WPS_PENDING:
  436. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
  437. eap_wsc_state(data, FAIL);
  438. break;
  439. }
  440. if (data->in_buf != &tmpbuf)
  441. wpabuf_free(data->in_buf);
  442. data->in_buf = NULL;
  443. send_msg:
  444. if (data->out_buf == NULL) {
  445. data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
  446. if (data->out_buf == NULL) {
  447. wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
  448. "message from WPS");
  449. return NULL;
  450. }
  451. data->out_used = 0;
  452. }
  453. eap_wsc_state(data, MESG);
  454. r = eap_wsc_build_msg(data, ret, id);
  455. if (data->state == FAIL && ret->methodState == METHOD_DONE) {
  456. /* Use reduced client timeout for WPS to avoid long wait */
  457. if (sm->ClientTimeout > 2)
  458. sm->ClientTimeout = 2;
  459. }
  460. return r;
  461. }
  462. int eap_peer_wsc_register(void)
  463. {
  464. struct eap_method *eap;
  465. int ret;
  466. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  467. EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
  468. "WSC");
  469. if (eap == NULL)
  470. return -1;
  471. eap->init = eap_wsc_init;
  472. eap->deinit = eap_wsc_deinit;
  473. eap->process = eap_wsc_process;
  474. ret = eap_peer_method_register(eap);
  475. if (ret)
  476. eap_peer_method_free(eap);
  477. return ret;
  478. }