eap_wsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
  180. if (res < 0) {
  181. os_free(data);
  182. return NULL;
  183. }
  184. if (res == 1) {
  185. wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
  186. "WPS");
  187. cfg.new_ap_settings = &new_ap_settings;
  188. }
  189. data->wps = wps_init(&cfg);
  190. if (data->wps == NULL) {
  191. os_free(data);
  192. return NULL;
  193. }
  194. data->fragment_size = WSC_FRAGMENT_SIZE;
  195. if (registrar && cfg.pin) {
  196. wps_registrar_add_pin(data->wps_ctx->registrar, NULL,
  197. cfg.pin, cfg.pin_len, 0);
  198. }
  199. return data;
  200. }
  201. static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
  202. {
  203. struct eap_wsc_data *data = priv;
  204. wpabuf_free(data->in_buf);
  205. wpabuf_free(data->out_buf);
  206. wps_deinit(data->wps);
  207. os_free(data->wps_ctx->network_key);
  208. data->wps_ctx->network_key = NULL;
  209. os_free(data);
  210. }
  211. static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
  212. struct eap_method_ret *ret, u8 id)
  213. {
  214. struct wpabuf *resp;
  215. u8 flags;
  216. size_t send_len, plen;
  217. ret->ignore = FALSE;
  218. wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
  219. ret->allowNotifications = TRUE;
  220. flags = 0;
  221. send_len = wpabuf_len(data->out_buf) - data->out_used;
  222. if (2 + send_len > data->fragment_size) {
  223. send_len = data->fragment_size - 2;
  224. flags |= WSC_FLAGS_MF;
  225. if (data->out_used == 0) {
  226. flags |= WSC_FLAGS_LF;
  227. send_len -= 2;
  228. }
  229. }
  230. plen = 2 + send_len;
  231. if (flags & WSC_FLAGS_LF)
  232. plen += 2;
  233. resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
  234. EAP_CODE_RESPONSE, id);
  235. if (resp == NULL)
  236. return NULL;
  237. wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
  238. wpabuf_put_u8(resp, flags); /* Flags */
  239. if (flags & WSC_FLAGS_LF)
  240. wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
  241. wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
  242. send_len);
  243. data->out_used += send_len;
  244. ret->methodState = METHOD_MAY_CONT;
  245. ret->decision = DECISION_FAIL;
  246. if (data->out_used == wpabuf_len(data->out_buf)) {
  247. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  248. "(message sent completely)",
  249. (unsigned long) send_len);
  250. wpabuf_free(data->out_buf);
  251. data->out_buf = NULL;
  252. data->out_used = 0;
  253. if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
  254. data->out_op_code == WSC_NACK ||
  255. data->out_op_code == WSC_Done) {
  256. eap_wsc_state(data, FAIL);
  257. ret->methodState = METHOD_DONE;
  258. } else
  259. eap_wsc_state(data, MESG);
  260. } else {
  261. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  262. "(%lu more to send)", (unsigned long) send_len,
  263. (unsigned long) wpabuf_len(data->out_buf) -
  264. data->out_used);
  265. eap_wsc_state(data, WAIT_FRAG_ACK);
  266. }
  267. return resp;
  268. }
  269. static int eap_wsc_process_cont(struct eap_wsc_data *data,
  270. const u8 *buf, size_t len, u8 op_code)
  271. {
  272. /* Process continuation of a pending message */
  273. if (op_code != data->in_op_code) {
  274. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
  275. "fragment (expected %d)",
  276. op_code, data->in_op_code);
  277. return -1;
  278. }
  279. if (len > wpabuf_tailroom(data->in_buf)) {
  280. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
  281. eap_wsc_state(data, FAIL);
  282. return -1;
  283. }
  284. wpabuf_put_data(data->in_buf, buf, len);
  285. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
  286. "for %lu bytes more", (unsigned long) len,
  287. (unsigned long) wpabuf_tailroom(data->in_buf));
  288. return 0;
  289. }
  290. static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
  291. struct eap_method_ret *ret,
  292. u8 id, u8 flags, u8 op_code,
  293. u16 message_length,
  294. const u8 *buf, size_t len)
  295. {
  296. /* Process a fragment that is not the last one of the message */
  297. if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
  298. wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
  299. "fragmented packet");
  300. ret->ignore = TRUE;
  301. return NULL;
  302. }
  303. if (data->in_buf == NULL) {
  304. /* First fragment of the message */
  305. data->in_buf = wpabuf_alloc(message_length);
  306. if (data->in_buf == NULL) {
  307. wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
  308. "message");
  309. ret->ignore = TRUE;
  310. return NULL;
  311. }
  312. data->in_op_code = op_code;
  313. wpabuf_put_data(data->in_buf, buf, len);
  314. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
  315. "fragment, waiting for %lu bytes more",
  316. (unsigned long) len,
  317. (unsigned long) wpabuf_tailroom(data->in_buf));
  318. }
  319. return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
  320. }
  321. static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
  322. struct eap_method_ret *ret,
  323. const struct wpabuf *reqData)
  324. {
  325. struct eap_wsc_data *data = priv;
  326. const u8 *start, *pos, *end;
  327. size_t len;
  328. u8 op_code, flags, id;
  329. u16 message_length = 0;
  330. enum wps_process_res res;
  331. struct wpabuf tmpbuf;
  332. pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
  333. &len);
  334. if (pos == NULL || len < 2) {
  335. ret->ignore = TRUE;
  336. return NULL;
  337. }
  338. id = eap_get_id(reqData);
  339. start = pos;
  340. end = start + len;
  341. op_code = *pos++;
  342. flags = *pos++;
  343. if (flags & WSC_FLAGS_LF) {
  344. if (end - pos < 2) {
  345. wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
  346. ret->ignore = TRUE;
  347. return NULL;
  348. }
  349. message_length = WPA_GET_BE16(pos);
  350. pos += 2;
  351. if (message_length < end - pos) {
  352. wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
  353. "Length");
  354. ret->ignore = TRUE;
  355. return NULL;
  356. }
  357. }
  358. wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
  359. "Flags 0x%x Message Length %d",
  360. op_code, flags, message_length);
  361. if (data->state == WAIT_FRAG_ACK) {
  362. if (op_code != WSC_FRAG_ACK) {
  363. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
  364. "in WAIT_FRAG_ACK state", op_code);
  365. ret->ignore = TRUE;
  366. return NULL;
  367. }
  368. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
  369. eap_wsc_state(data, MESG);
  370. return eap_wsc_build_msg(data, ret, id);
  371. }
  372. if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
  373. op_code != WSC_Done && op_code != WSC_Start) {
  374. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
  375. op_code);
  376. ret->ignore = TRUE;
  377. return NULL;
  378. }
  379. if (data->state == WAIT_START) {
  380. if (op_code != WSC_Start) {
  381. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
  382. "in WAIT_START state", op_code);
  383. ret->ignore = TRUE;
  384. return NULL;
  385. }
  386. wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
  387. eap_wsc_state(data, MESG);
  388. /* Start message has empty payload, skip processing */
  389. goto send_msg;
  390. } else if (op_code == WSC_Start) {
  391. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
  392. op_code);
  393. ret->ignore = TRUE;
  394. return NULL;
  395. }
  396. if (data->in_buf &&
  397. eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
  398. ret->ignore = TRUE;
  399. return NULL;
  400. }
  401. if (flags & WSC_FLAGS_MF) {
  402. return eap_wsc_process_fragment(data, ret, id, flags, op_code,
  403. message_length, pos,
  404. end - pos);
  405. }
  406. if (data->in_buf == NULL) {
  407. /* Wrap unfragmented messages as wpabuf without extra copy */
  408. wpabuf_set(&tmpbuf, pos, end - pos);
  409. data->in_buf = &tmpbuf;
  410. }
  411. res = wps_process_msg(data->wps, op_code, data->in_buf);
  412. switch (res) {
  413. case WPS_DONE:
  414. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
  415. "successfully - wait for EAP failure");
  416. eap_wsc_state(data, FAIL);
  417. break;
  418. case WPS_CONTINUE:
  419. eap_wsc_state(data, MESG);
  420. break;
  421. case WPS_FAILURE:
  422. case WPS_PENDING:
  423. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
  424. eap_wsc_state(data, FAIL);
  425. break;
  426. }
  427. if (data->in_buf != &tmpbuf)
  428. wpabuf_free(data->in_buf);
  429. data->in_buf = NULL;
  430. send_msg:
  431. if (data->out_buf == NULL) {
  432. data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
  433. if (data->out_buf == NULL) {
  434. wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
  435. "message from WPS");
  436. return NULL;
  437. }
  438. data->out_used = 0;
  439. }
  440. eap_wsc_state(data, MESG);
  441. return eap_wsc_build_msg(data, ret, id);
  442. }
  443. int eap_peer_wsc_register(void)
  444. {
  445. struct eap_method *eap;
  446. int ret;
  447. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  448. EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
  449. "WSC");
  450. if (eap == NULL)
  451. return -1;
  452. eap->init = eap_wsc_init;
  453. eap->deinit = eap_wsc_deinit;
  454. eap->process = eap_wsc_process;
  455. ret = eap_peer_method_register(eap);
  456. if (ret)
  457. eap_peer_method_free(eap);
  458. return ret;
  459. }