eap_server_wsc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * EAP-WSC server for Wi-Fi Protected Setup
  3. * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eloop.h"
  11. #include "eap_i.h"
  12. #include "eap_common/eap_wsc_common.h"
  13. #include "p2p/p2p.h"
  14. #include "wps/wps.h"
  15. struct eap_wsc_data {
  16. enum { START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
  17. int registrar;
  18. struct wpabuf *in_buf;
  19. struct wpabuf *out_buf;
  20. enum wsc_op_code in_op_code, out_op_code;
  21. size_t out_used;
  22. size_t fragment_size;
  23. struct wps_data *wps;
  24. int ext_reg_timeout;
  25. };
  26. #ifndef CONFIG_NO_STDOUT_DEBUG
  27. static const char * eap_wsc_state_txt(int state)
  28. {
  29. switch (state) {
  30. case START:
  31. return "START";
  32. case MESG:
  33. return "MESG";
  34. case FRAG_ACK:
  35. return "FRAG_ACK";
  36. case WAIT_FRAG_ACK:
  37. return "WAIT_FRAG_ACK";
  38. case DONE:
  39. return "DONE";
  40. case FAIL:
  41. return "FAIL";
  42. default:
  43. return "?";
  44. }
  45. }
  46. #endif /* CONFIG_NO_STDOUT_DEBUG */
  47. static void eap_wsc_state(struct eap_wsc_data *data, int state)
  48. {
  49. wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
  50. eap_wsc_state_txt(data->state),
  51. eap_wsc_state_txt(state));
  52. data->state = state;
  53. }
  54. static void eap_wsc_ext_reg_timeout(void *eloop_ctx, void *timeout_ctx)
  55. {
  56. struct eap_sm *sm = eloop_ctx;
  57. struct eap_wsc_data *data = timeout_ctx;
  58. if (sm->method_pending != METHOD_PENDING_WAIT)
  59. return;
  60. wpa_printf(MSG_DEBUG, "EAP-WSC: Timeout while waiting for an External "
  61. "Registrar");
  62. data->ext_reg_timeout = 1;
  63. eap_sm_pending_cb(sm);
  64. }
  65. static void * eap_wsc_init(struct eap_sm *sm)
  66. {
  67. struct eap_wsc_data *data;
  68. int registrar;
  69. struct wps_config cfg;
  70. if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
  71. os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
  72. 0)
  73. registrar = 0; /* Supplicant is Registrar */
  74. else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
  75. os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
  76. == 0)
  77. registrar = 1; /* Supplicant is Enrollee */
  78. else {
  79. wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
  80. sm->identity, sm->identity_len);
  81. return NULL;
  82. }
  83. data = os_zalloc(sizeof(*data));
  84. if (data == NULL)
  85. return NULL;
  86. data->state = registrar ? START : MESG;
  87. data->registrar = registrar;
  88. os_memset(&cfg, 0, sizeof(cfg));
  89. cfg.wps = sm->wps;
  90. cfg.registrar = registrar;
  91. if (registrar) {
  92. if (sm->wps == NULL || sm->wps->registrar == NULL) {
  93. wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
  94. "initialized");
  95. os_free(data);
  96. return NULL;
  97. }
  98. } else {
  99. if (sm->user == NULL || sm->user->password == NULL) {
  100. /*
  101. * In theory, this should not really be needed, but
  102. * Windows 7 uses Registrar mode to probe AP's WPS
  103. * capabilities before trying to use Enrollee and fails
  104. * if the AP does not allow that probing to happen..
  105. */
  106. wpa_printf(MSG_DEBUG, "EAP-WSC: No AP PIN (password) "
  107. "configured for Enrollee functionality - "
  108. "allow for probing capabilities (M1)");
  109. } else {
  110. cfg.pin = sm->user->password;
  111. cfg.pin_len = sm->user->password_len;
  112. }
  113. }
  114. cfg.assoc_wps_ie = sm->assoc_wps_ie;
  115. cfg.peer_addr = sm->peer_addr;
  116. #ifdef CONFIG_P2P
  117. if (sm->assoc_p2p_ie) {
  118. wpa_printf(MSG_DEBUG, "EAP-WSC: Prefer PSK format for P2P "
  119. "client");
  120. cfg.use_psk_key = 1;
  121. cfg.p2p_dev_addr = p2p_get_go_dev_addr(sm->assoc_p2p_ie);
  122. }
  123. #endif /* CONFIG_P2P */
  124. cfg.pbc_in_m1 = sm->pbc_in_m1;
  125. data->wps = wps_init(&cfg);
  126. if (data->wps == NULL) {
  127. os_free(data);
  128. return NULL;
  129. }
  130. data->fragment_size = sm->fragment_size > 0 ? sm->fragment_size :
  131. WSC_FRAGMENT_SIZE;
  132. return data;
  133. }
  134. static void eap_wsc_reset(struct eap_sm *sm, void *priv)
  135. {
  136. struct eap_wsc_data *data = priv;
  137. eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
  138. wpabuf_free(data->in_buf);
  139. wpabuf_free(data->out_buf);
  140. wps_deinit(data->wps);
  141. os_free(data);
  142. }
  143. static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
  144. struct eap_wsc_data *data, u8 id)
  145. {
  146. struct wpabuf *req;
  147. req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
  148. EAP_CODE_REQUEST, id);
  149. if (req == NULL) {
  150. wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
  151. "request");
  152. return NULL;
  153. }
  154. wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
  155. wpabuf_put_u8(req, WSC_Start); /* Op-Code */
  156. wpabuf_put_u8(req, 0); /* Flags */
  157. return req;
  158. }
  159. static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
  160. {
  161. struct wpabuf *req;
  162. u8 flags;
  163. size_t send_len, plen;
  164. flags = 0;
  165. send_len = wpabuf_len(data->out_buf) - data->out_used;
  166. if (2 + send_len > data->fragment_size) {
  167. send_len = data->fragment_size - 2;
  168. flags |= WSC_FLAGS_MF;
  169. if (data->out_used == 0) {
  170. flags |= WSC_FLAGS_LF;
  171. send_len -= 2;
  172. }
  173. }
  174. plen = 2 + send_len;
  175. if (flags & WSC_FLAGS_LF)
  176. plen += 2;
  177. req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
  178. EAP_CODE_REQUEST, id);
  179. if (req == NULL) {
  180. wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
  181. "request");
  182. return NULL;
  183. }
  184. wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
  185. wpabuf_put_u8(req, flags); /* Flags */
  186. if (flags & WSC_FLAGS_LF)
  187. wpabuf_put_be16(req, wpabuf_len(data->out_buf));
  188. wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
  189. send_len);
  190. data->out_used += send_len;
  191. if (data->out_used == wpabuf_len(data->out_buf)) {
  192. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  193. "(message sent completely)",
  194. (unsigned long) send_len);
  195. wpabuf_free(data->out_buf);
  196. data->out_buf = NULL;
  197. data->out_used = 0;
  198. eap_wsc_state(data, MESG);
  199. } else {
  200. wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
  201. "(%lu more to send)", (unsigned long) send_len,
  202. (unsigned long) wpabuf_len(data->out_buf) -
  203. data->out_used);
  204. eap_wsc_state(data, WAIT_FRAG_ACK);
  205. }
  206. return req;
  207. }
  208. static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
  209. {
  210. struct eap_wsc_data *data = priv;
  211. switch (data->state) {
  212. case START:
  213. return eap_wsc_build_start(sm, data, id);
  214. case MESG:
  215. if (data->out_buf == NULL) {
  216. data->out_buf = wps_get_msg(data->wps,
  217. &data->out_op_code);
  218. if (data->out_buf == NULL) {
  219. wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
  220. "receive message from WPS");
  221. return NULL;
  222. }
  223. data->out_used = 0;
  224. }
  225. /* pass through */
  226. case WAIT_FRAG_ACK:
  227. return eap_wsc_build_msg(data, id);
  228. case FRAG_ACK:
  229. return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
  230. default:
  231. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
  232. "buildReq", data->state);
  233. return NULL;
  234. }
  235. }
  236. static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
  237. struct wpabuf *respData)
  238. {
  239. const u8 *pos;
  240. size_t len;
  241. pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
  242. respData, &len);
  243. if (pos == NULL || len < 2) {
  244. wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
  245. return TRUE;
  246. }
  247. return FALSE;
  248. }
  249. static int eap_wsc_process_cont(struct eap_wsc_data *data,
  250. const u8 *buf, size_t len, u8 op_code)
  251. {
  252. /* Process continuation of a pending message */
  253. if (op_code != data->in_op_code) {
  254. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
  255. "fragment (expected %d)",
  256. op_code, data->in_op_code);
  257. eap_wsc_state(data, FAIL);
  258. return -1;
  259. }
  260. if (len > wpabuf_tailroom(data->in_buf)) {
  261. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
  262. eap_wsc_state(data, FAIL);
  263. return -1;
  264. }
  265. wpabuf_put_data(data->in_buf, buf, len);
  266. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
  267. "bytes more", (unsigned long) len,
  268. (unsigned long) wpabuf_tailroom(data->in_buf));
  269. return 0;
  270. }
  271. static int eap_wsc_process_fragment(struct eap_wsc_data *data,
  272. u8 flags, u8 op_code, u16 message_length,
  273. const u8 *buf, size_t len)
  274. {
  275. /* Process a fragment that is not the last one of the message */
  276. if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
  277. wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
  278. "field in a fragmented packet");
  279. return -1;
  280. }
  281. if (data->in_buf == NULL) {
  282. /* First fragment of the message */
  283. data->in_buf = wpabuf_alloc(message_length);
  284. if (data->in_buf == NULL) {
  285. wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
  286. "message");
  287. return -1;
  288. }
  289. data->in_op_code = op_code;
  290. wpabuf_put_data(data->in_buf, buf, len);
  291. wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
  292. "first fragment, waiting for %lu bytes more",
  293. (unsigned long) len,
  294. (unsigned long) wpabuf_tailroom(data->in_buf));
  295. }
  296. return 0;
  297. }
  298. static void eap_wsc_process(struct eap_sm *sm, void *priv,
  299. struct wpabuf *respData)
  300. {
  301. struct eap_wsc_data *data = priv;
  302. const u8 *start, *pos, *end;
  303. size_t len;
  304. u8 op_code, flags;
  305. u16 message_length = 0;
  306. enum wps_process_res res;
  307. struct wpabuf tmpbuf;
  308. eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
  309. if (data->ext_reg_timeout) {
  310. eap_wsc_state(data, FAIL);
  311. return;
  312. }
  313. pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
  314. respData, &len);
  315. if (pos == NULL || len < 2)
  316. return; /* Should not happen; message already verified */
  317. start = pos;
  318. end = start + len;
  319. op_code = *pos++;
  320. flags = *pos++;
  321. if (flags & WSC_FLAGS_LF) {
  322. if (end - pos < 2) {
  323. wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
  324. return;
  325. }
  326. message_length = WPA_GET_BE16(pos);
  327. pos += 2;
  328. if (message_length < end - pos) {
  329. wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
  330. "Length");
  331. return;
  332. }
  333. }
  334. wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
  335. "Flags 0x%x Message Length %d",
  336. op_code, flags, message_length);
  337. if (data->state == WAIT_FRAG_ACK) {
  338. if (op_code != WSC_FRAG_ACK) {
  339. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
  340. "in WAIT_FRAG_ACK state", op_code);
  341. eap_wsc_state(data, FAIL);
  342. return;
  343. }
  344. wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
  345. eap_wsc_state(data, MESG);
  346. return;
  347. }
  348. if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
  349. op_code != WSC_Done) {
  350. wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
  351. op_code);
  352. eap_wsc_state(data, FAIL);
  353. return;
  354. }
  355. if (data->in_buf &&
  356. eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
  357. eap_wsc_state(data, FAIL);
  358. return;
  359. }
  360. if (flags & WSC_FLAGS_MF) {
  361. if (eap_wsc_process_fragment(data, flags, op_code,
  362. message_length, pos, end - pos) <
  363. 0)
  364. eap_wsc_state(data, FAIL);
  365. else
  366. eap_wsc_state(data, FRAG_ACK);
  367. return;
  368. }
  369. if (data->in_buf == NULL) {
  370. /* Wrap unfragmented messages as wpabuf without extra copy */
  371. wpabuf_set(&tmpbuf, pos, end - pos);
  372. data->in_buf = &tmpbuf;
  373. }
  374. res = wps_process_msg(data->wps, op_code, data->in_buf);
  375. switch (res) {
  376. case WPS_DONE:
  377. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
  378. "successfully - report EAP failure");
  379. eap_wsc_state(data, FAIL);
  380. break;
  381. case WPS_CONTINUE:
  382. eap_wsc_state(data, MESG);
  383. break;
  384. case WPS_FAILURE:
  385. wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
  386. eap_wsc_state(data, FAIL);
  387. break;
  388. case WPS_PENDING:
  389. eap_wsc_state(data, MESG);
  390. sm->method_pending = METHOD_PENDING_WAIT;
  391. eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
  392. eloop_register_timeout(5, 0, eap_wsc_ext_reg_timeout,
  393. sm, data);
  394. break;
  395. }
  396. if (data->in_buf != &tmpbuf)
  397. wpabuf_free(data->in_buf);
  398. data->in_buf = NULL;
  399. }
  400. static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
  401. {
  402. struct eap_wsc_data *data = priv;
  403. return data->state == FAIL;
  404. }
  405. static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
  406. {
  407. /* EAP-WSC will always result in EAP-Failure */
  408. return FALSE;
  409. }
  410. static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
  411. {
  412. /* Recommended retransmit times: retransmit timeout 5 seconds,
  413. * per-message timeout 15 seconds, i.e., 3 tries. */
  414. sm->MaxRetrans = 2; /* total 3 attempts */
  415. return 5;
  416. }
  417. int eap_server_wsc_register(void)
  418. {
  419. struct eap_method *eap;
  420. int ret;
  421. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  422. EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
  423. "WSC");
  424. if (eap == NULL)
  425. return -1;
  426. eap->init = eap_wsc_init;
  427. eap->reset = eap_wsc_reset;
  428. eap->buildReq = eap_wsc_buildReq;
  429. eap->check = eap_wsc_check;
  430. eap->process = eap_wsc_process;
  431. eap->isDone = eap_wsc_isDone;
  432. eap->isSuccess = eap_wsc_isSuccess;
  433. eap->getTimeout = eap_wsc_getTimeout;
  434. ret = eap_server_method_register(eap);
  435. if (ret)
  436. eap_server_method_free(eap);
  437. return ret;
  438. }