wps_hostapd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * hostapd / WPS integration
  3. * Copyright (c) 2008, 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 "hostapd.h"
  16. #include "driver_i.h"
  17. #include "eloop.h"
  18. #include "uuid.h"
  19. #include "wpa_ctrl.h"
  20. #include "ieee802_11_defs.h"
  21. #include "wps/wps.h"
  22. #include "wps/wps_defs.h"
  23. #include "wps/wps_dev_attr.h"
  24. #include "wps_hostapd.h"
  25. static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  26. size_t psk_len)
  27. {
  28. struct hostapd_data *hapd = ctx;
  29. struct hostapd_wpa_psk *p;
  30. struct hostapd_ssid *ssid = &hapd->conf->ssid;
  31. wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
  32. MACSTR, MAC2STR(mac_addr));
  33. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  34. if (psk_len != PMK_LEN) {
  35. wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
  36. (unsigned long) psk_len);
  37. return -1;
  38. }
  39. /* Add the new PSK to runtime PSK list */
  40. p = os_zalloc(sizeof(*p));
  41. if (p == NULL)
  42. return -1;
  43. os_memcpy(p->addr, mac_addr, ETH_ALEN);
  44. os_memcpy(p->psk, psk, PMK_LEN);
  45. p->next = ssid->wpa_psk;
  46. ssid->wpa_psk = p;
  47. if (ssid->wpa_psk_file) {
  48. FILE *f;
  49. char hex[PMK_LEN * 2 + 1];
  50. /* Add the new PSK to PSK list file */
  51. f = fopen(ssid->wpa_psk_file, "a");
  52. if (f == NULL) {
  53. wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
  54. "'%s'", ssid->wpa_psk_file);
  55. return -1;
  56. }
  57. wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
  58. fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
  59. fclose(f);
  60. }
  61. return 0;
  62. }
  63. static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
  64. size_t beacon_ie_len, const u8 *probe_resp_ie,
  65. size_t probe_resp_ie_len)
  66. {
  67. struct hostapd_data *hapd = ctx;
  68. os_free(hapd->wps_beacon_ie);
  69. if (beacon_ie_len == 0) {
  70. hapd->wps_beacon_ie = NULL;
  71. hapd->wps_beacon_ie_len = 0;
  72. } else {
  73. hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
  74. if (hapd->wps_beacon_ie == NULL) {
  75. hapd->wps_beacon_ie_len = 0;
  76. return -1;
  77. }
  78. os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
  79. hapd->wps_beacon_ie_len = beacon_ie_len;
  80. }
  81. hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
  82. hapd->wps_beacon_ie_len);
  83. os_free(hapd->wps_probe_resp_ie);
  84. if (probe_resp_ie_len == 0) {
  85. hapd->wps_probe_resp_ie = NULL;
  86. hapd->wps_probe_resp_ie_len = 0;
  87. } else {
  88. hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
  89. if (hapd->wps_probe_resp_ie == NULL) {
  90. hapd->wps_probe_resp_ie_len = 0;
  91. return -1;
  92. }
  93. os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
  94. probe_resp_ie_len);
  95. hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
  96. }
  97. hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
  98. hapd->wps_probe_resp_ie_len);
  99. return 0;
  100. }
  101. static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  102. const struct wps_device_data *dev)
  103. {
  104. struct hostapd_data *hapd = ctx;
  105. char uuid[40], txt[400];
  106. int len;
  107. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  108. return;
  109. wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
  110. len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
  111. "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
  112. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  113. dev->manufacturer, dev->model_name,
  114. dev->model_number, dev->serial_number,
  115. dev->categ, dev->oui, dev->sub_categ);
  116. if (len > 0 && len < (int) sizeof(txt))
  117. wpa_msg(hapd, MSG_INFO, "%s", txt);
  118. if (hapd->conf->wps_pin_requests) {
  119. FILE *f;
  120. struct os_time t;
  121. f = fopen(hapd->conf->wps_pin_requests, "a");
  122. if (f == NULL)
  123. return;
  124. os_get_time(&t);
  125. fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
  126. "\t%d-%08X-%d\n",
  127. t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
  128. dev->manufacturer, dev->model_name, dev->model_number,
  129. dev->serial_number,
  130. dev->categ, dev->oui, dev->sub_categ);
  131. fclose(f);
  132. }
  133. }
  134. static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
  135. const u8 *uuid_e)
  136. {
  137. struct hostapd_data *hapd = ctx;
  138. char uuid[40];
  139. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  140. return;
  141. wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
  142. MAC2STR(mac_addr), uuid);
  143. }
  144. static int str_starts(const char *str, const char *start)
  145. {
  146. return os_strncmp(str, start, os_strlen(start)) == 0;
  147. }
  148. static void wps_reload_config(void *eloop_data, void *user_ctx)
  149. {
  150. struct hostapd_iface *iface = eloop_data;
  151. wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
  152. if (hostapd_reload_config(iface) < 0) {
  153. wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
  154. "configuration");
  155. }
  156. }
  157. static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
  158. {
  159. struct hostapd_data *hapd = ctx;
  160. FILE *oconf, *nconf;
  161. size_t len, i;
  162. char *tmp_fname;
  163. char buf[1024];
  164. int multi_bss;
  165. int wpa;
  166. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  167. cred->cred_attr, cred->cred_attr_len);
  168. wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
  169. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  170. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  171. cred->auth_type);
  172. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  173. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  174. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  175. cred->key, cred->key_len);
  176. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  177. MAC2STR(cred->mac_addr));
  178. if ((hapd->conf->wps_cred_processing == 1 ||
  179. hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
  180. size_t blen = cred->cred_attr_len * 2 + 1;
  181. char *buf = os_malloc(blen);
  182. if (buf) {
  183. wpa_snprintf_hex(buf, blen,
  184. cred->cred_attr, cred->cred_attr_len);
  185. wpa_msg(hapd, MSG_INFO, "%s%s",
  186. WPS_EVENT_NEW_AP_SETTINGS, buf);
  187. os_free(buf);
  188. }
  189. } else
  190. wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
  191. if (hapd->conf->wps_cred_processing == 1)
  192. return 0;
  193. len = os_strlen(hapd->iface->config_fname) + 5;
  194. tmp_fname = os_malloc(len);
  195. if (tmp_fname == NULL)
  196. return -1;
  197. os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
  198. oconf = fopen(hapd->iface->config_fname, "r");
  199. if (oconf == NULL) {
  200. wpa_printf(MSG_WARNING, "WPS: Could not open current "
  201. "configuration file");
  202. os_free(tmp_fname);
  203. return -1;
  204. }
  205. nconf = fopen(tmp_fname, "w");
  206. if (nconf == NULL) {
  207. wpa_printf(MSG_WARNING, "WPS: Could not write updated "
  208. "configuration file");
  209. os_free(tmp_fname);
  210. fclose(oconf);
  211. return -1;
  212. }
  213. fprintf(nconf, "# WPS configuration - START\n");
  214. fprintf(nconf, "wps_state=2\n");
  215. fprintf(nconf, "ssid=");
  216. for (i = 0; i < cred->ssid_len; i++)
  217. fputc(cred->ssid[i], nconf);
  218. fprintf(nconf, "\n");
  219. if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
  220. (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
  221. wpa = 3;
  222. else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
  223. wpa = 2;
  224. else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
  225. wpa = 1;
  226. else
  227. wpa = 0;
  228. if (wpa) {
  229. char *prefix;
  230. fprintf(nconf, "wpa=%d\n", wpa);
  231. fprintf(nconf, "wpa_key_mgmt=");
  232. prefix = "";
  233. if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
  234. fprintf(nconf, "WPA-EAP");
  235. prefix = " ";
  236. }
  237. if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
  238. fprintf(nconf, "%sWPA-PSK", prefix);
  239. fprintf(nconf, "\n");
  240. fprintf(nconf, "wpa_pairwise=");
  241. prefix = "";
  242. if (cred->encr_type & WPS_ENCR_AES) {
  243. fprintf(nconf, "CCMP");
  244. prefix = " ";
  245. }
  246. if (cred->encr_type & WPS_ENCR_TKIP) {
  247. fprintf(nconf, "%sTKIP", prefix);
  248. }
  249. fprintf(nconf, "\n");
  250. if (cred->key_len >= 8 && cred->key_len < 64) {
  251. fprintf(nconf, "wpa_passphrase=");
  252. for (i = 0; i < cred->key_len; i++)
  253. fputc(cred->key[i], nconf);
  254. fprintf(nconf, "\n");
  255. } else if (cred->key_len == 64) {
  256. fprintf(nconf, "wpa_psk=");
  257. for (i = 0; i < cred->key_len; i++)
  258. fputc(cred->key[i], nconf);
  259. fprintf(nconf, "\n");
  260. } else {
  261. wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
  262. "for WPA/WPA2",
  263. (unsigned long) cred->key_len);
  264. }
  265. fprintf(nconf, "auth_algs=1\n");
  266. } else {
  267. if ((cred->auth_type & WPS_AUTH_OPEN) &&
  268. (cred->auth_type & WPS_AUTH_SHARED))
  269. fprintf(nconf, "auth_algs=3\n");
  270. else if (cred->auth_type & WPS_AUTH_SHARED)
  271. fprintf(nconf, "auth_algs=2\n");
  272. else
  273. fprintf(nconf, "auth_algs=1\n");
  274. if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
  275. fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
  276. fprintf(nconf, "wep_key%d=", cred->key_idx);
  277. if (cred->key_len != 10 && cred->key_len != 26)
  278. fputc('"', nconf);
  279. for (i = 0; i < cred->key_len; i++)
  280. fputc(cred->key[i], nconf);
  281. if (cred->key_len != 10 && cred->key_len != 26)
  282. fputc('"', nconf);
  283. fprintf(nconf, "\n");
  284. }
  285. }
  286. fprintf(nconf, "# WPS configuration - END\n");
  287. multi_bss = 0;
  288. while (fgets(buf, sizeof(buf), oconf)) {
  289. if (os_strncmp(buf, "bss=", 4) == 0)
  290. multi_bss = 1;
  291. if (!multi_bss &&
  292. (str_starts(buf, "ssid=") ||
  293. str_starts(buf, "auth_algs=") ||
  294. str_starts(buf, "wps_state=") ||
  295. str_starts(buf, "wpa=") ||
  296. str_starts(buf, "wpa_psk=") ||
  297. str_starts(buf, "wpa_pairwise=") ||
  298. str_starts(buf, "rsn_pairwise=") ||
  299. str_starts(buf, "wpa_key_mgmt=") ||
  300. str_starts(buf, "wpa_passphrase="))) {
  301. fprintf(nconf, "#WPS# %s", buf);
  302. } else
  303. fprintf(nconf, "%s", buf);
  304. }
  305. fclose(nconf);
  306. fclose(oconf);
  307. if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
  308. wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
  309. "configuration file: %s", strerror(errno));
  310. os_free(tmp_fname);
  311. return -1;
  312. }
  313. os_free(tmp_fname);
  314. /* Schedule configuration reload after short period of time to allow
  315. * EAP-WSC to be finished.
  316. */
  317. eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
  318. NULL);
  319. /* TODO: dualband AP may need to update multiple configuration files */
  320. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  321. return 0;
  322. }
  323. static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
  324. struct wps_event_pwd_auth_fail *data)
  325. {
  326. FILE *f;
  327. if (!data->enrollee)
  328. return;
  329. /*
  330. * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
  331. * if this happens multiple times.
  332. */
  333. hapd->ap_pin_failures++;
  334. if (hapd->ap_pin_failures < 4)
  335. return;
  336. wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
  337. hapd->wps->ap_setup_locked = 1;
  338. wps_registrar_update_ie(hapd->wps->registrar);
  339. if (hapd->conf->wps_cred_processing == 1)
  340. return;
  341. f = fopen(hapd->iface->config_fname, "a");
  342. if (f == NULL) {
  343. wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
  344. "configuration file");
  345. return;
  346. }
  347. fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
  348. fprintf(f, "ap_setup_locked=1\n");
  349. fclose(f);
  350. /* TODO: dualband AP may need to update multiple configuration files */
  351. wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
  352. }
  353. static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
  354. union wps_event_data *data)
  355. {
  356. struct hostapd_data *hapd = ctx;
  357. if (event == WPS_EV_PWD_AUTH_FAIL)
  358. hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
  359. }
  360. static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
  361. {
  362. os_free(hapd->wps_beacon_ie);
  363. hapd->wps_beacon_ie = NULL;
  364. hapd->wps_beacon_ie_len = 0;
  365. hostapd_set_wps_beacon_ie(hapd, NULL, 0);
  366. os_free(hapd->wps_probe_resp_ie);
  367. hapd->wps_probe_resp_ie = NULL;
  368. hapd->wps_probe_resp_ie_len = 0;
  369. hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
  370. }
  371. int hostapd_init_wps(struct hostapd_data *hapd,
  372. struct hostapd_bss_config *conf)
  373. {
  374. struct wps_context *wps;
  375. struct wps_registrar_config cfg;
  376. if (conf->wps_state == 0) {
  377. hostapd_wps_clear_ies(hapd);
  378. return 0;
  379. }
  380. wps = os_zalloc(sizeof(*wps));
  381. if (wps == NULL)
  382. return -1;
  383. wps->cred_cb = hostapd_wps_cred_cb;
  384. wps->event_cb = hostapd_wps_event_cb;
  385. wps->cb_ctx = hapd;
  386. os_memset(&cfg, 0, sizeof(cfg));
  387. wps->wps_state = hapd->conf->wps_state;
  388. wps->ap_setup_locked = hapd->conf->ap_setup_locked;
  389. if (is_nil_uuid(hapd->conf->uuid)) {
  390. uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
  391. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
  392. wps->uuid, UUID_LEN);
  393. } else
  394. os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
  395. wps->ssid_len = hapd->conf->ssid.ssid_len;
  396. os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
  397. wps->ap = 1;
  398. os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
  399. wps->dev.device_name = hapd->conf->device_name ?
  400. os_strdup(hapd->conf->device_name) : NULL;
  401. wps->dev.manufacturer = hapd->conf->manufacturer ?
  402. os_strdup(hapd->conf->manufacturer) : NULL;
  403. wps->dev.model_name = hapd->conf->model_name ?
  404. os_strdup(hapd->conf->model_name) : NULL;
  405. wps->dev.model_number = hapd->conf->model_number ?
  406. os_strdup(hapd->conf->model_number) : NULL;
  407. wps->dev.serial_number = hapd->conf->serial_number ?
  408. os_strdup(hapd->conf->serial_number) : NULL;
  409. if (hapd->conf->config_methods) {
  410. char *m = hapd->conf->config_methods;
  411. if (os_strstr(m, "label"))
  412. wps->config_methods |= WPS_CONFIG_LABEL;
  413. if (os_strstr(m, "display"))
  414. wps->config_methods |= WPS_CONFIG_DISPLAY;
  415. if (os_strstr(m, "push_button"))
  416. wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
  417. if (os_strstr(m, "keypad"))
  418. wps->config_methods |= WPS_CONFIG_KEYPAD;
  419. }
  420. if (hapd->conf->device_type) {
  421. char *pos;
  422. u8 oui[4];
  423. /* <categ>-<OUI>-<subcateg> */
  424. wps->dev.categ = atoi(hapd->conf->device_type);
  425. pos = os_strchr(hapd->conf->device_type, '-');
  426. if (pos == NULL) {
  427. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  428. os_free(wps);
  429. return -1;
  430. }
  431. pos++;
  432. if (hexstr2bin(pos, oui, 4)) {
  433. wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
  434. os_free(wps);
  435. return -1;
  436. }
  437. wps->dev.oui = WPA_GET_BE32(oui);
  438. pos = os_strchr(pos, '-');
  439. if (pos == NULL) {
  440. wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
  441. os_free(wps);
  442. return -1;
  443. }
  444. pos++;
  445. wps->dev.sub_categ = atoi(pos);
  446. }
  447. wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
  448. wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
  449. WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
  450. if (conf->wpa & WPA_PROTO_RSN) {
  451. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  452. wps->auth_types |= WPS_AUTH_WPA2PSK;
  453. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  454. wps->auth_types |= WPS_AUTH_WPA2;
  455. if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
  456. wps->encr_types |= WPS_ENCR_AES;
  457. if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
  458. wps->encr_types |= WPS_ENCR_TKIP;
  459. }
  460. if (conf->wpa & WPA_PROTO_WPA) {
  461. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
  462. wps->auth_types |= WPS_AUTH_WPAPSK;
  463. if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
  464. wps->auth_types |= WPS_AUTH_WPA;
  465. if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
  466. wps->encr_types |= WPS_ENCR_AES;
  467. if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
  468. wps->encr_types |= WPS_ENCR_TKIP;
  469. }
  470. if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
  471. wps->encr_types |= WPS_ENCR_NONE;
  472. wps->auth_types |= WPS_AUTH_OPEN;
  473. } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
  474. wps->encr_types |= WPS_ENCR_WEP;
  475. if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
  476. wps->auth_types |= WPS_AUTH_OPEN;
  477. if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
  478. wps->auth_types |= WPS_AUTH_SHARED;
  479. } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
  480. wps->auth_types |= WPS_AUTH_OPEN;
  481. if (conf->default_wep_key_len)
  482. wps->encr_types |= WPS_ENCR_WEP;
  483. else
  484. wps->encr_types |= WPS_ENCR_NONE;
  485. }
  486. if (conf->ssid.wpa_psk_file) {
  487. /* Use per-device PSKs */
  488. } else if (conf->ssid.wpa_passphrase) {
  489. wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
  490. wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
  491. } else if (conf->ssid.wpa_psk) {
  492. wps->network_key = os_malloc(2 * PMK_LEN + 1);
  493. if (wps->network_key == NULL) {
  494. os_free(wps);
  495. return -1;
  496. }
  497. wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
  498. conf->ssid.wpa_psk->psk, PMK_LEN);
  499. wps->network_key_len = 2 * PMK_LEN;
  500. } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
  501. wps->network_key = os_malloc(conf->ssid.wep.len[0]);
  502. if (wps->network_key == NULL) {
  503. os_free(wps);
  504. return -1;
  505. }
  506. os_memcpy(wps->network_key, conf->ssid.wep.key[0],
  507. conf->ssid.wep.len[0]);
  508. wps->network_key_len = conf->ssid.wep.len[0];
  509. }
  510. if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
  511. /* Override parameters to enable security by default */
  512. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  513. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  514. }
  515. wps->ap_settings = conf->ap_settings;
  516. wps->ap_settings_len = conf->ap_settings_len;
  517. cfg.new_psk_cb = hostapd_wps_new_psk_cb;
  518. cfg.set_ie_cb = hostapd_wps_set_ie_cb;
  519. cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
  520. cfg.reg_success_cb = hostapd_wps_reg_success_cb;
  521. cfg.cb_ctx = hapd;
  522. cfg.skip_cred_build = conf->skip_cred_build;
  523. cfg.extra_cred = conf->extra_cred;
  524. cfg.extra_cred_len = conf->extra_cred_len;
  525. cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
  526. conf->skip_cred_build;
  527. wps->registrar = wps_registrar_init(wps, &cfg);
  528. if (wps->registrar == NULL) {
  529. printf("Failed to initialize WPS Registrar\n");
  530. os_free(wps->network_key);
  531. os_free(wps);
  532. return -1;
  533. }
  534. hapd->wps = wps;
  535. return 0;
  536. }
  537. void hostapd_deinit_wps(struct hostapd_data *hapd)
  538. {
  539. if (hapd->wps == NULL)
  540. return;
  541. wps_registrar_deinit(hapd->wps->registrar);
  542. os_free(hapd->wps->network_key);
  543. wps_device_data_free(&hapd->wps->dev);
  544. os_free(hapd->wps);
  545. hapd->wps = NULL;
  546. hostapd_wps_clear_ies(hapd);
  547. }
  548. int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
  549. const char *pin)
  550. {
  551. u8 u[UUID_LEN];
  552. int any = 0;
  553. if (hapd->wps == NULL)
  554. return -1;
  555. if (os_strcmp(uuid, "any") == 0)
  556. any = 1;
  557. else if (uuid_str2bin(uuid, u))
  558. return -1;
  559. return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
  560. (const u8 *) pin, os_strlen(pin));
  561. }
  562. int hostapd_wps_button_pushed(struct hostapd_data *hapd)
  563. {
  564. if (hapd->wps == NULL)
  565. return -1;
  566. return wps_registrar_button_pushed(hapd->wps->registrar);
  567. }
  568. void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
  569. const u8 *ie, size_t ie_len)
  570. {
  571. struct wpabuf *wps_ie;
  572. const u8 *end, *pos, *wps;
  573. if (hapd->wps == NULL)
  574. return;
  575. pos = ie;
  576. end = ie + ie_len;
  577. wps = NULL;
  578. while (pos + 1 < end) {
  579. if (pos + 2 + pos[1] > end)
  580. return;
  581. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  582. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
  583. wps = pos;
  584. break;
  585. }
  586. pos += 2 + pos[1];
  587. }
  588. if (wps == NULL)
  589. return; /* No WPS IE in Probe Request */
  590. wps_ie = wpabuf_alloc(ie_len);
  591. if (wps_ie == NULL)
  592. return;
  593. /* There may be multiple WPS IEs in the message, so need to concatenate
  594. * their WPS Data fields */
  595. while (pos + 1 < end) {
  596. if (pos + 2 + pos[1] > end)
  597. break;
  598. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  599. WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
  600. wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
  601. pos += 2 + pos[1];
  602. }
  603. if (wpabuf_len(wps_ie) > 0)
  604. wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
  605. wpabuf_free(wps_ie);
  606. }