wps_supplicant.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. /*
  2. * wpa_supplicant / WPS integration
  3. * Copyright (c) 2008-2010, 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 "uuid.h"
  12. #include "crypto/random.h"
  13. #include "crypto/dh_group5.h"
  14. #include "common/ieee802_11_defs.h"
  15. #include "common/ieee802_11_common.h"
  16. #include "common/wpa_common.h"
  17. #include "common/wpa_ctrl.h"
  18. #include "eap_common/eap_wsc_common.h"
  19. #include "eap_peer/eap.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "notify.h"
  26. #include "blacklist.h"
  27. #include "bss.h"
  28. #include "scan.h"
  29. #include "ap.h"
  30. #include "p2p/p2p.h"
  31. #include "p2p_supplicant.h"
  32. #include "wps_supplicant.h"
  33. #ifndef WPS_PIN_SCAN_IGNORE_SEL_REG
  34. #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
  35. #endif /* WPS_PIN_SCAN_IGNORE_SEL_REG */
  36. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
  37. static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
  38. int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
  39. {
  40. if (!wpa_s->wps_success &&
  41. wpa_s->current_ssid &&
  42. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  43. const u8 *bssid = wpa_s->bssid;
  44. if (is_zero_ether_addr(bssid))
  45. bssid = wpa_s->pending_bssid;
  46. wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
  47. " did not succeed - continue trying to find "
  48. "suitable AP", MAC2STR(bssid));
  49. wpa_blacklist_add(wpa_s, bssid);
  50. wpa_supplicant_deauthenticate(wpa_s,
  51. WLAN_REASON_DEAUTH_LEAVING);
  52. wpa_s->reassociate = 1;
  53. wpa_supplicant_req_scan(wpa_s,
  54. wpa_s->blacklist_cleared ? 5 : 0, 0);
  55. wpa_s->blacklist_cleared = 0;
  56. return 1;
  57. }
  58. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  59. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && !wpa_s->wps_success)
  60. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL);
  61. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
  62. !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  63. int disabled = wpa_s->current_ssid->disabled;
  64. unsigned int freq = wpa_s->assoc_freq;
  65. wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
  66. "try to associate with the received credential "
  67. "(freq=%u)", freq);
  68. wpa_supplicant_deauthenticate(wpa_s,
  69. WLAN_REASON_DEAUTH_LEAVING);
  70. if (disabled) {
  71. wpa_printf(MSG_DEBUG, "WPS: Current network is "
  72. "disabled - wait for user to enable");
  73. return 1;
  74. }
  75. wpa_s->after_wps = 5;
  76. wpa_s->wps_freq = freq;
  77. wpa_s->normal_scans = 0;
  78. wpa_s->reassociate = 1;
  79. wpa_supplicant_req_scan(wpa_s, 0, 0);
  80. return 1;
  81. }
  82. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
  83. wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
  84. "for external credential processing");
  85. wpas_clear_wps(wpa_s);
  86. wpa_supplicant_deauthenticate(wpa_s,
  87. WLAN_REASON_DEAUTH_LEAVING);
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. static void wpas_wps_security_workaround(struct wpa_supplicant *wpa_s,
  93. struct wpa_ssid *ssid,
  94. const struct wps_credential *cred)
  95. {
  96. struct wpa_driver_capa capa;
  97. struct wpa_bss *bss;
  98. const u8 *ie;
  99. struct wpa_ie_data adv;
  100. int wpa2 = 0, ccmp = 0;
  101. /*
  102. * Many existing WPS APs do not know how to negotiate WPA2 or CCMP in
  103. * case they are configured for mixed mode operation (WPA+WPA2 and
  104. * TKIP+CCMP). Try to use scan results to figure out whether the AP
  105. * actually supports stronger security and select that if the client
  106. * has support for it, too.
  107. */
  108. if (wpa_drv_get_capa(wpa_s, &capa))
  109. return; /* Unknown what driver supports */
  110. if (ssid->ssid == NULL)
  111. return;
  112. bss = wpa_bss_get(wpa_s, cred->mac_addr, ssid->ssid, ssid->ssid_len);
  113. if (bss == NULL) {
  114. wpa_printf(MSG_DEBUG, "WPS: The AP was not found from BSS "
  115. "table - use credential as-is");
  116. return;
  117. }
  118. wpa_printf(MSG_DEBUG, "WPS: AP found from BSS table");
  119. ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  120. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0) {
  121. wpa2 = 1;
  122. if (adv.pairwise_cipher & WPA_CIPHER_CCMP)
  123. ccmp = 1;
  124. } else {
  125. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  126. if (ie && wpa_parse_wpa_ie(ie, 2 + ie[1], &adv) == 0 &&
  127. adv.pairwise_cipher & WPA_CIPHER_CCMP)
  128. ccmp = 1;
  129. }
  130. if (ie == NULL && (ssid->proto & WPA_PROTO_WPA) &&
  131. (ssid->pairwise_cipher & WPA_CIPHER_TKIP)) {
  132. /*
  133. * TODO: This could be the initial AP configuration and the
  134. * Beacon contents could change shortly. Should request a new
  135. * scan and delay addition of the network until the updated
  136. * scan results are available.
  137. */
  138. wpa_printf(MSG_DEBUG, "WPS: The AP did not yet advertise WPA "
  139. "support - use credential as-is");
  140. return;
  141. }
  142. if (ccmp && !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
  143. (ssid->pairwise_cipher & WPA_CIPHER_TKIP) &&
  144. (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  145. wpa_printf(MSG_DEBUG, "WPS: Add CCMP into the credential "
  146. "based on scan results");
  147. if (wpa_s->conf->ap_scan == 1)
  148. ssid->pairwise_cipher |= WPA_CIPHER_CCMP;
  149. else
  150. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  151. }
  152. if (wpa2 && !(ssid->proto & WPA_PROTO_RSN) &&
  153. (ssid->proto & WPA_PROTO_WPA) &&
  154. (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP)) {
  155. wpa_printf(MSG_DEBUG, "WPS: Add WPA2 into the credential "
  156. "based on scan results");
  157. if (wpa_s->conf->ap_scan == 1)
  158. ssid->proto |= WPA_PROTO_RSN;
  159. else
  160. ssid->proto = WPA_PROTO_RSN;
  161. }
  162. }
  163. static int wpa_supplicant_wps_cred(void *ctx,
  164. const struct wps_credential *cred)
  165. {
  166. struct wpa_supplicant *wpa_s = ctx;
  167. struct wpa_ssid *ssid = wpa_s->current_ssid;
  168. u8 key_idx = 0;
  169. u16 auth_type;
  170. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  171. int registrar = 0;
  172. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  173. if ((wpa_s->conf->wps_cred_processing == 1 ||
  174. wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
  175. size_t blen = cred->cred_attr_len * 2 + 1;
  176. char *buf = os_malloc(blen);
  177. if (buf) {
  178. wpa_snprintf_hex(buf, blen,
  179. cred->cred_attr, cred->cred_attr_len);
  180. wpa_msg(wpa_s, MSG_INFO, "%s%s",
  181. WPS_EVENT_CRED_RECEIVED, buf);
  182. os_free(buf);
  183. }
  184. wpas_notify_wps_credential(wpa_s, cred);
  185. } else
  186. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
  187. wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
  188. cred->cred_attr, cred->cred_attr_len);
  189. if (wpa_s->conf->wps_cred_processing == 1)
  190. return 0;
  191. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
  192. wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
  193. cred->auth_type);
  194. wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
  195. wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
  196. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  197. cred->key, cred->key_len);
  198. wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
  199. MAC2STR(cred->mac_addr));
  200. auth_type = cred->auth_type;
  201. if (auth_type == (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  202. wpa_printf(MSG_DEBUG, "WPS: Workaround - convert mixed-mode "
  203. "auth_type into WPA2PSK");
  204. auth_type = WPS_AUTH_WPA2PSK;
  205. }
  206. if (auth_type != WPS_AUTH_OPEN &&
  207. auth_type != WPS_AUTH_SHARED &&
  208. auth_type != WPS_AUTH_WPAPSK &&
  209. auth_type != WPS_AUTH_WPA2PSK) {
  210. wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
  211. "unsupported authentication type 0x%x",
  212. auth_type);
  213. return 0;
  214. }
  215. if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  216. wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
  217. "on the received credential");
  218. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  219. if (ssid->eap.identity &&
  220. ssid->eap.identity_len == WSC_ID_REGISTRAR_LEN &&
  221. os_memcmp(ssid->eap.identity, WSC_ID_REGISTRAR,
  222. WSC_ID_REGISTRAR_LEN) == 0)
  223. registrar = 1;
  224. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  225. os_free(ssid->eap.identity);
  226. ssid->eap.identity = NULL;
  227. ssid->eap.identity_len = 0;
  228. os_free(ssid->eap.phase1);
  229. ssid->eap.phase1 = NULL;
  230. os_free(ssid->eap.eap_methods);
  231. ssid->eap.eap_methods = NULL;
  232. if (!ssid->p2p_group)
  233. ssid->temporary = 0;
  234. ssid->bssid_set = 0;
  235. } else {
  236. wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
  237. "received credential");
  238. ssid = wpa_config_add_network(wpa_s->conf);
  239. if (ssid == NULL)
  240. return -1;
  241. wpas_notify_network_added(wpa_s, ssid);
  242. }
  243. wpa_config_set_network_defaults(ssid);
  244. os_free(ssid->ssid);
  245. ssid->ssid = os_malloc(cred->ssid_len);
  246. if (ssid->ssid) {
  247. os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
  248. ssid->ssid_len = cred->ssid_len;
  249. }
  250. switch (cred->encr_type) {
  251. case WPS_ENCR_NONE:
  252. break;
  253. case WPS_ENCR_WEP:
  254. if (cred->key_len <= 0)
  255. break;
  256. if (cred->key_len != 5 && cred->key_len != 13 &&
  257. cred->key_len != 10 && cred->key_len != 26) {
  258. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
  259. "%lu", (unsigned long) cred->key_len);
  260. return -1;
  261. }
  262. if (cred->key_idx > NUM_WEP_KEYS) {
  263. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
  264. cred->key_idx);
  265. return -1;
  266. }
  267. if (cred->key_idx)
  268. key_idx = cred->key_idx - 1;
  269. if (cred->key_len == 10 || cred->key_len == 26) {
  270. if (hexstr2bin((char *) cred->key,
  271. ssid->wep_key[key_idx],
  272. cred->key_len / 2) < 0) {
  273. wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
  274. "%d", key_idx);
  275. return -1;
  276. }
  277. ssid->wep_key_len[key_idx] = cred->key_len / 2;
  278. } else {
  279. os_memcpy(ssid->wep_key[key_idx], cred->key,
  280. cred->key_len);
  281. ssid->wep_key_len[key_idx] = cred->key_len;
  282. }
  283. ssid->wep_tx_keyidx = key_idx;
  284. break;
  285. case WPS_ENCR_TKIP:
  286. ssid->pairwise_cipher = WPA_CIPHER_TKIP;
  287. break;
  288. case WPS_ENCR_AES:
  289. ssid->pairwise_cipher = WPA_CIPHER_CCMP;
  290. break;
  291. }
  292. switch (auth_type) {
  293. case WPS_AUTH_OPEN:
  294. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  295. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  296. ssid->proto = 0;
  297. #ifdef CONFIG_WPS_REG_DISABLE_OPEN
  298. if (registrar) {
  299. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OPEN_NETWORK
  300. "id=%d - Credentials for an open "
  301. "network disabled by default - use "
  302. "'select_network %d' to enable",
  303. ssid->id, ssid->id);
  304. ssid->disabled = 1;
  305. }
  306. #endif /* CONFIG_WPS_REG_DISABLE_OPEN */
  307. break;
  308. case WPS_AUTH_SHARED:
  309. ssid->auth_alg = WPA_AUTH_ALG_SHARED;
  310. ssid->key_mgmt = WPA_KEY_MGMT_NONE;
  311. ssid->proto = 0;
  312. break;
  313. case WPS_AUTH_WPAPSK:
  314. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  315. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  316. ssid->proto = WPA_PROTO_WPA;
  317. break;
  318. case WPS_AUTH_WPA:
  319. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  320. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  321. ssid->proto = WPA_PROTO_WPA;
  322. break;
  323. case WPS_AUTH_WPA2:
  324. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  325. ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  326. ssid->proto = WPA_PROTO_RSN;
  327. break;
  328. case WPS_AUTH_WPA2PSK:
  329. ssid->auth_alg = WPA_AUTH_ALG_OPEN;
  330. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  331. ssid->proto = WPA_PROTO_RSN;
  332. break;
  333. }
  334. if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
  335. if (cred->key_len == 2 * PMK_LEN) {
  336. if (hexstr2bin((const char *) cred->key, ssid->psk,
  337. PMK_LEN)) {
  338. wpa_printf(MSG_ERROR, "WPS: Invalid Network "
  339. "Key");
  340. return -1;
  341. }
  342. ssid->psk_set = 1;
  343. ssid->export_keys = 1;
  344. } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
  345. os_free(ssid->passphrase);
  346. ssid->passphrase = os_malloc(cred->key_len + 1);
  347. if (ssid->passphrase == NULL)
  348. return -1;
  349. os_memcpy(ssid->passphrase, cred->key, cred->key_len);
  350. ssid->passphrase[cred->key_len] = '\0';
  351. wpa_config_update_psk(ssid);
  352. ssid->export_keys = 1;
  353. } else {
  354. wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
  355. "length %lu",
  356. (unsigned long) cred->key_len);
  357. return -1;
  358. }
  359. }
  360. wpas_wps_security_workaround(wpa_s, ssid, cred);
  361. #ifndef CONFIG_NO_CONFIG_WRITE
  362. if (wpa_s->conf->update_config &&
  363. wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  364. wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
  365. return -1;
  366. }
  367. #endif /* CONFIG_NO_CONFIG_WRITE */
  368. return 0;
  369. }
  370. #ifdef CONFIG_P2P
  371. static void wpas_wps_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
  372. {
  373. struct wpa_supplicant *wpa_s = eloop_ctx;
  374. wpas_p2p_notif_pbc_overlap(wpa_s);
  375. }
  376. #endif /* CONFIG_P2P */
  377. static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
  378. struct wps_event_m2d *m2d)
  379. {
  380. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
  381. "dev_password_id=%d config_error=%d",
  382. m2d->dev_password_id, m2d->config_error);
  383. wpas_notify_wps_event_m2d(wpa_s, m2d);
  384. #ifdef CONFIG_P2P
  385. if (wpa_s->parent && wpa_s->parent != wpa_s) {
  386. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_M2D
  387. "dev_password_id=%d config_error=%d",
  388. m2d->dev_password_id, m2d->config_error);
  389. }
  390. if (m2d->config_error == WPS_CFG_MULTIPLE_PBC_DETECTED) {
  391. /*
  392. * Notify P2P from eloop timeout to avoid issues with the
  393. * interface getting removed while processing a message.
  394. */
  395. eloop_register_timeout(0, 0, wpas_wps_pbc_overlap_cb, wpa_s,
  396. NULL);
  397. }
  398. #endif /* CONFIG_P2P */
  399. }
  400. static const char * wps_event_fail_reason[NUM_WPS_EI_VALUES] = {
  401. "No Error", /* WPS_EI_NO_ERROR */
  402. "TKIP Only Prohibited", /* WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED */
  403. "WEP Prohibited" /* WPS_EI_SECURITY_WEP_PROHIBITED */
  404. };
  405. static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
  406. struct wps_event_fail *fail)
  407. {
  408. if (fail->error_indication > 0 &&
  409. fail->error_indication < NUM_WPS_EI_VALUES) {
  410. wpa_msg(wpa_s, MSG_INFO,
  411. WPS_EVENT_FAIL "msg=%d config_error=%d reason=%d (%s)",
  412. fail->msg, fail->config_error, fail->error_indication,
  413. wps_event_fail_reason[fail->error_indication]);
  414. if (wpa_s->parent && wpa_s->parent != wpa_s)
  415. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  416. "msg=%d config_error=%d reason=%d (%s)",
  417. fail->msg, fail->config_error,
  418. fail->error_indication,
  419. wps_event_fail_reason[fail->error_indication]);
  420. } else {
  421. wpa_msg(wpa_s, MSG_INFO,
  422. WPS_EVENT_FAIL "msg=%d config_error=%d",
  423. fail->msg, fail->config_error);
  424. if (wpa_s->parent && wpa_s->parent != wpa_s)
  425. wpa_msg(wpa_s->parent, MSG_INFO, WPS_EVENT_FAIL
  426. "msg=%d config_error=%d",
  427. fail->msg, fail->config_error);
  428. }
  429. wpas_clear_wps(wpa_s);
  430. wpas_notify_wps_event_fail(wpa_s, fail);
  431. #ifdef CONFIG_P2P
  432. wpas_p2p_wps_failed(wpa_s, fail);
  433. #endif /* CONFIG_P2P */
  434. }
  435. static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
  436. {
  437. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
  438. wpa_s->wps_success = 1;
  439. wpas_notify_wps_event_success(wpa_s);
  440. #ifdef CONFIG_P2P
  441. wpas_p2p_wps_success(wpa_s, wpa_s->bssid, 0);
  442. #endif /* CONFIG_P2P */
  443. }
  444. static void wpa_supplicant_wps_event_er_ap_add(struct wpa_supplicant *wpa_s,
  445. struct wps_event_er_ap *ap)
  446. {
  447. char uuid_str[100];
  448. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  449. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  450. if (ap->pri_dev_type)
  451. wps_dev_type_bin2str(ap->pri_dev_type, dev_type,
  452. sizeof(dev_type));
  453. else
  454. dev_type[0] = '\0';
  455. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_ADD "%s " MACSTR
  456. " pri_dev_type=%s wps_state=%d |%s|%s|%s|%s|%s|%s|",
  457. uuid_str, MAC2STR(ap->mac_addr), dev_type, ap->wps_state,
  458. ap->friendly_name ? ap->friendly_name : "",
  459. ap->manufacturer ? ap->manufacturer : "",
  460. ap->model_description ? ap->model_description : "",
  461. ap->model_name ? ap->model_name : "",
  462. ap->manufacturer_url ? ap->manufacturer_url : "",
  463. ap->model_url ? ap->model_url : "");
  464. }
  465. static void wpa_supplicant_wps_event_er_ap_remove(struct wpa_supplicant *wpa_s,
  466. struct wps_event_er_ap *ap)
  467. {
  468. char uuid_str[100];
  469. uuid_bin2str(ap->uuid, uuid_str, sizeof(uuid_str));
  470. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_REMOVE "%s", uuid_str);
  471. }
  472. static void wpa_supplicant_wps_event_er_enrollee_add(
  473. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  474. {
  475. char uuid_str[100];
  476. char dev_type[WPS_DEV_TYPE_BUFSIZE];
  477. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  478. if (enrollee->pri_dev_type)
  479. wps_dev_type_bin2str(enrollee->pri_dev_type, dev_type,
  480. sizeof(dev_type));
  481. else
  482. dev_type[0] = '\0';
  483. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_ADD "%s " MACSTR
  484. " M1=%d config_methods=0x%x dev_passwd_id=%d pri_dev_type=%s "
  485. "|%s|%s|%s|%s|%s|",
  486. uuid_str, MAC2STR(enrollee->mac_addr), enrollee->m1_received,
  487. enrollee->config_methods, enrollee->dev_passwd_id, dev_type,
  488. enrollee->dev_name ? enrollee->dev_name : "",
  489. enrollee->manufacturer ? enrollee->manufacturer : "",
  490. enrollee->model_name ? enrollee->model_name : "",
  491. enrollee->model_number ? enrollee->model_number : "",
  492. enrollee->serial_number ? enrollee->serial_number : "");
  493. }
  494. static void wpa_supplicant_wps_event_er_enrollee_remove(
  495. struct wpa_supplicant *wpa_s, struct wps_event_er_enrollee *enrollee)
  496. {
  497. char uuid_str[100];
  498. uuid_bin2str(enrollee->uuid, uuid_str, sizeof(uuid_str));
  499. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_ENROLLEE_REMOVE "%s " MACSTR,
  500. uuid_str, MAC2STR(enrollee->mac_addr));
  501. }
  502. static void wpa_supplicant_wps_event_er_ap_settings(
  503. struct wpa_supplicant *wpa_s,
  504. struct wps_event_er_ap_settings *ap_settings)
  505. {
  506. char uuid_str[100];
  507. char key_str[65];
  508. const struct wps_credential *cred = ap_settings->cred;
  509. key_str[0] = '\0';
  510. if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  511. if (cred->key_len >= 8 && cred->key_len <= 64) {
  512. os_memcpy(key_str, cred->key, cred->key_len);
  513. key_str[cred->key_len] = '\0';
  514. }
  515. }
  516. uuid_bin2str(ap_settings->uuid, uuid_str, sizeof(uuid_str));
  517. /* Use wpa_msg_ctrl to avoid showing the key in debug log */
  518. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_ER_AP_SETTINGS
  519. "uuid=%s ssid=%s auth_type=0x%04x encr_type=0x%04x "
  520. "key=%s",
  521. uuid_str, wpa_ssid_txt(cred->ssid, cred->ssid_len),
  522. cred->auth_type, cred->encr_type, key_str);
  523. }
  524. static void wpa_supplicant_wps_event_er_set_sel_reg(
  525. struct wpa_supplicant *wpa_s,
  526. struct wps_event_er_set_selected_registrar *ev)
  527. {
  528. char uuid_str[100];
  529. uuid_bin2str(ev->uuid, uuid_str, sizeof(uuid_str));
  530. switch (ev->state) {
  531. case WPS_ER_SET_SEL_REG_START:
  532. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  533. "uuid=%s state=START sel_reg=%d dev_passwd_id=%u "
  534. "sel_reg_config_methods=0x%x",
  535. uuid_str, ev->sel_reg, ev->dev_passwd_id,
  536. ev->sel_reg_config_methods);
  537. break;
  538. case WPS_ER_SET_SEL_REG_DONE:
  539. wpa_msg(wpa_s, MSG_DEBUG, WPS_EVENT_ER_SET_SEL_REG
  540. "uuid=%s state=DONE", uuid_str);
  541. break;
  542. case WPS_ER_SET_SEL_REG_FAILED:
  543. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_ER_SET_SEL_REG
  544. "uuid=%s state=FAILED", uuid_str);
  545. break;
  546. }
  547. }
  548. static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
  549. union wps_event_data *data)
  550. {
  551. struct wpa_supplicant *wpa_s = ctx;
  552. switch (event) {
  553. case WPS_EV_M2D:
  554. wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
  555. break;
  556. case WPS_EV_FAIL:
  557. wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
  558. break;
  559. case WPS_EV_SUCCESS:
  560. wpa_supplicant_wps_event_success(wpa_s);
  561. break;
  562. case WPS_EV_PWD_AUTH_FAIL:
  563. #ifdef CONFIG_AP
  564. if (wpa_s->ap_iface && data->pwd_auth_fail.enrollee)
  565. wpa_supplicant_ap_pwd_auth_fail(wpa_s);
  566. #endif /* CONFIG_AP */
  567. break;
  568. case WPS_EV_PBC_OVERLAP:
  569. break;
  570. case WPS_EV_PBC_TIMEOUT:
  571. break;
  572. case WPS_EV_ER_AP_ADD:
  573. wpa_supplicant_wps_event_er_ap_add(wpa_s, &data->ap);
  574. break;
  575. case WPS_EV_ER_AP_REMOVE:
  576. wpa_supplicant_wps_event_er_ap_remove(wpa_s, &data->ap);
  577. break;
  578. case WPS_EV_ER_ENROLLEE_ADD:
  579. wpa_supplicant_wps_event_er_enrollee_add(wpa_s,
  580. &data->enrollee);
  581. break;
  582. case WPS_EV_ER_ENROLLEE_REMOVE:
  583. wpa_supplicant_wps_event_er_enrollee_remove(wpa_s,
  584. &data->enrollee);
  585. break;
  586. case WPS_EV_ER_AP_SETTINGS:
  587. wpa_supplicant_wps_event_er_ap_settings(wpa_s,
  588. &data->ap_settings);
  589. break;
  590. case WPS_EV_ER_SET_SELECTED_REGISTRAR:
  591. wpa_supplicant_wps_event_er_set_sel_reg(wpa_s,
  592. &data->set_sel_reg);
  593. break;
  594. case WPS_EV_AP_PIN_SUCCESS:
  595. break;
  596. }
  597. }
  598. enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
  599. {
  600. if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
  601. eap_is_wps_pin_enrollee(&ssid->eap))
  602. return WPS_REQ_ENROLLEE;
  603. else
  604. return WPS_REQ_REGISTRAR;
  605. }
  606. static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
  607. {
  608. int id;
  609. struct wpa_ssid *ssid, *remove_ssid = NULL, *prev_current;
  610. prev_current = wpa_s->current_ssid;
  611. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  612. /* Remove any existing WPS network from configuration */
  613. ssid = wpa_s->conf->ssid;
  614. while (ssid) {
  615. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  616. if (ssid == wpa_s->current_ssid) {
  617. wpa_s->current_ssid = NULL;
  618. if (ssid != NULL)
  619. wpas_notify_network_changed(wpa_s);
  620. }
  621. id = ssid->id;
  622. remove_ssid = ssid;
  623. } else
  624. id = -1;
  625. ssid = ssid->next;
  626. if (id >= 0) {
  627. if (prev_current == remove_ssid) {
  628. wpa_sm_set_config(wpa_s->wpa, NULL);
  629. eapol_sm_notify_config(wpa_s->eapol, NULL,
  630. NULL);
  631. }
  632. wpas_notify_network_removed(wpa_s, remove_ssid);
  633. wpa_config_remove_network(wpa_s->conf, id);
  634. }
  635. }
  636. }
  637. static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
  638. {
  639. struct wpa_supplicant *wpa_s = eloop_ctx;
  640. wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
  641. "out");
  642. wpas_clear_wps(wpa_s);
  643. }
  644. static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
  645. int registrar, const u8 *bssid)
  646. {
  647. struct wpa_ssid *ssid;
  648. ssid = wpa_config_add_network(wpa_s->conf);
  649. if (ssid == NULL)
  650. return NULL;
  651. wpas_notify_network_added(wpa_s, ssid);
  652. wpa_config_set_network_defaults(ssid);
  653. ssid->temporary = 1;
  654. if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
  655. wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
  656. wpa_config_set(ssid, "identity", registrar ?
  657. "\"" WSC_ID_REGISTRAR "\"" :
  658. "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
  659. wpas_notify_network_removed(wpa_s, ssid);
  660. wpa_config_remove_network(wpa_s->conf, ssid->id);
  661. return NULL;
  662. }
  663. if (bssid) {
  664. #ifndef CONFIG_P2P
  665. struct wpa_bss *bss;
  666. int count = 0;
  667. #endif /* CONFIG_P2P */
  668. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  669. ssid->bssid_set = 1;
  670. /*
  671. * Note: With P2P, the SSID may change at the time the WPS
  672. * provisioning is started, so better not filter the AP based
  673. * on the current SSID in the scan results.
  674. */
  675. #ifndef CONFIG_P2P
  676. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  677. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) != 0)
  678. continue;
  679. os_free(ssid->ssid);
  680. ssid->ssid = os_malloc(bss->ssid_len);
  681. if (ssid->ssid == NULL)
  682. break;
  683. os_memcpy(ssid->ssid, bss->ssid, bss->ssid_len);
  684. ssid->ssid_len = bss->ssid_len;
  685. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Picked SSID from "
  686. "scan results",
  687. ssid->ssid, ssid->ssid_len);
  688. count++;
  689. }
  690. if (count > 1) {
  691. wpa_printf(MSG_DEBUG, "WPS: More than one SSID found "
  692. "for the AP; use wildcard");
  693. os_free(ssid->ssid);
  694. ssid->ssid = NULL;
  695. ssid->ssid_len = 0;
  696. }
  697. #endif /* CONFIG_P2P */
  698. }
  699. return ssid;
  700. }
  701. static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
  702. struct wpa_ssid *selected, const u8 *bssid)
  703. {
  704. struct wpa_ssid *ssid;
  705. struct wpa_bss *bss;
  706. wpa_s->known_wps_freq = 0;
  707. if (bssid) {
  708. bss = wpa_bss_get_bssid(wpa_s, bssid);
  709. if (bss && bss->freq > 0) {
  710. wpa_s->known_wps_freq = 1;
  711. wpa_s->wps_freq = bss->freq;
  712. }
  713. }
  714. if (wpa_s->current_ssid)
  715. wpa_supplicant_deauthenticate(
  716. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  717. /* Mark all other networks disabled and trigger reassociation */
  718. ssid = wpa_s->conf->ssid;
  719. while (ssid) {
  720. int was_disabled = ssid->disabled;
  721. /*
  722. * In case the network object corresponds to a persistent group
  723. * then do not send out network disabled signal. In addition,
  724. * do not change disabled status of persistent network objects
  725. * from 2 to 1 should we connect to another network.
  726. */
  727. if (was_disabled != 2) {
  728. ssid->disabled = ssid != selected;
  729. if (was_disabled != ssid->disabled)
  730. wpas_notify_network_enabled_changed(wpa_s,
  731. ssid);
  732. }
  733. ssid = ssid->next;
  734. }
  735. wpa_s->disconnected = 0;
  736. wpa_s->reassociate = 1;
  737. wpa_s->scan_runs = 0;
  738. wpa_s->normal_scans = 0;
  739. wpa_s->wps_success = 0;
  740. wpa_s->blacklist_cleared = 0;
  741. wpa_supplicant_req_scan(wpa_s, 0, 0);
  742. }
  743. int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid,
  744. int p2p_group)
  745. {
  746. struct wpa_ssid *ssid;
  747. wpas_clear_wps(wpa_s);
  748. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  749. if (ssid == NULL)
  750. return -1;
  751. ssid->temporary = 1;
  752. ssid->p2p_group = p2p_group;
  753. #ifdef CONFIG_P2P
  754. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  755. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  756. if (ssid->ssid) {
  757. ssid->ssid_len = wpa_s->go_params->ssid_len;
  758. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  759. ssid->ssid_len);
  760. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  761. "SSID", ssid->ssid, ssid->ssid_len);
  762. }
  763. }
  764. #endif /* CONFIG_P2P */
  765. wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
  766. if (wpa_s->wps_fragment_size)
  767. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  768. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  769. wpa_s, NULL);
  770. wpas_wps_reassoc(wpa_s, ssid, bssid);
  771. return 0;
  772. }
  773. int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
  774. const char *pin, int p2p_group, u16 dev_pw_id)
  775. {
  776. struct wpa_ssid *ssid;
  777. char val[128];
  778. unsigned int rpin = 0;
  779. wpas_clear_wps(wpa_s);
  780. ssid = wpas_wps_add_network(wpa_s, 0, bssid);
  781. if (ssid == NULL)
  782. return -1;
  783. ssid->temporary = 1;
  784. ssid->p2p_group = p2p_group;
  785. #ifdef CONFIG_P2P
  786. if (p2p_group && wpa_s->go_params && wpa_s->go_params->ssid_len) {
  787. ssid->ssid = os_zalloc(wpa_s->go_params->ssid_len + 1);
  788. if (ssid->ssid) {
  789. ssid->ssid_len = wpa_s->go_params->ssid_len;
  790. os_memcpy(ssid->ssid, wpa_s->go_params->ssid,
  791. ssid->ssid_len);
  792. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Use specific AP "
  793. "SSID", ssid->ssid, ssid->ssid_len);
  794. }
  795. }
  796. #endif /* CONFIG_P2P */
  797. if (pin)
  798. os_snprintf(val, sizeof(val), "\"pin=%s dev_pw_id=%u\"",
  799. pin, dev_pw_id);
  800. else {
  801. rpin = wps_generate_pin();
  802. os_snprintf(val, sizeof(val), "\"pin=%08d dev_pw_id=%u\"",
  803. rpin, dev_pw_id);
  804. }
  805. wpa_config_set(ssid, "phase1", val, 0);
  806. if (wpa_s->wps_fragment_size)
  807. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  808. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  809. wpa_s, NULL);
  810. wpas_wps_reassoc(wpa_s, ssid, bssid);
  811. return rpin;
  812. }
  813. /* Cancel the wps pbc/pin requests */
  814. int wpas_wps_cancel(struct wpa_supplicant *wpa_s)
  815. {
  816. #ifdef CONFIG_AP
  817. if (wpa_s->ap_iface) {
  818. wpa_printf(MSG_DEBUG, "WPS: Cancelling in AP mode");
  819. return wpa_supplicant_ap_wps_cancel(wpa_s);
  820. }
  821. #endif /* CONFIG_AP */
  822. if (wpa_s->wpa_state == WPA_SCANNING ||
  823. wpa_s->wpa_state == WPA_DISCONNECTED) {
  824. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - cancel scan");
  825. wpa_supplicant_cancel_scan(wpa_s);
  826. wpas_clear_wps(wpa_s);
  827. } else if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  828. wpa_printf(MSG_DEBUG, "WPS: Cancel operation - "
  829. "deauthenticate");
  830. wpa_supplicant_deauthenticate(wpa_s,
  831. WLAN_REASON_DEAUTH_LEAVING);
  832. wpas_clear_wps(wpa_s);
  833. }
  834. return 0;
  835. }
  836. #ifdef CONFIG_WPS_OOB
  837. int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
  838. char *path, char *method, char *name)
  839. {
  840. struct wps_context *wps = wpa_s->wps;
  841. struct oob_device_data *oob_dev;
  842. oob_dev = wps_get_oob_device(device_type);
  843. if (oob_dev == NULL)
  844. return -1;
  845. oob_dev->device_path = path;
  846. oob_dev->device_name = name;
  847. wps->oob_conf.oob_method = wps_get_oob_method(method);
  848. if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
  849. /*
  850. * Use pre-configured DH keys in order to be able to write the
  851. * key hash into the OOB file.
  852. */
  853. wpabuf_free(wps->dh_pubkey);
  854. wpabuf_free(wps->dh_privkey);
  855. wps->dh_privkey = NULL;
  856. wps->dh_pubkey = NULL;
  857. dh5_free(wps->dh_ctx);
  858. wps->dh_ctx = dh5_init(&wps->dh_privkey, &wps->dh_pubkey);
  859. wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
  860. if (wps->dh_ctx == NULL || wps->dh_pubkey == NULL) {
  861. wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
  862. "Diffie-Hellman handshake");
  863. return -1;
  864. }
  865. }
  866. if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
  867. wpas_clear_wps(wpa_s);
  868. if (wps_process_oob(wps, oob_dev, 0) < 0)
  869. return -1;
  870. if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
  871. wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
  872. wpas_wps_start_pin(wpa_s, NULL,
  873. wpabuf_head(wps->oob_conf.dev_password), 0,
  874. DEV_PW_DEFAULT) < 0)
  875. return -1;
  876. return 0;
  877. }
  878. #endif /* CONFIG_WPS_OOB */
  879. int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
  880. const char *pin, struct wps_new_ap_settings *settings)
  881. {
  882. struct wpa_ssid *ssid;
  883. char val[200];
  884. char *pos, *end;
  885. int res;
  886. if (!pin)
  887. return -1;
  888. wpas_clear_wps(wpa_s);
  889. ssid = wpas_wps_add_network(wpa_s, 1, bssid);
  890. if (ssid == NULL)
  891. return -1;
  892. ssid->temporary = 1;
  893. pos = val;
  894. end = pos + sizeof(val);
  895. res = os_snprintf(pos, end - pos, "\"pin=%s", pin);
  896. if (res < 0 || res >= end - pos)
  897. return -1;
  898. pos += res;
  899. if (settings) {
  900. res = os_snprintf(pos, end - pos, " new_ssid=%s new_auth=%s "
  901. "new_encr=%s new_key=%s",
  902. settings->ssid_hex, settings->auth,
  903. settings->encr, settings->key_hex);
  904. if (res < 0 || res >= end - pos)
  905. return -1;
  906. pos += res;
  907. }
  908. res = os_snprintf(pos, end - pos, "\"");
  909. if (res < 0 || res >= end - pos)
  910. return -1;
  911. wpa_config_set(ssid, "phase1", val, 0);
  912. if (wpa_s->wps_fragment_size)
  913. ssid->eap.fragment_size = wpa_s->wps_fragment_size;
  914. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
  915. wpa_s, NULL);
  916. wpas_wps_reassoc(wpa_s, ssid, bssid);
  917. return 0;
  918. }
  919. static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
  920. size_t psk_len)
  921. {
  922. wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
  923. "STA " MACSTR, MAC2STR(mac_addr));
  924. wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
  925. /* TODO */
  926. return 0;
  927. }
  928. static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
  929. const struct wps_device_data *dev)
  930. {
  931. char uuid[40], txt[400];
  932. int len;
  933. char devtype[WPS_DEV_TYPE_BUFSIZE];
  934. if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
  935. return;
  936. wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
  937. len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
  938. " [%s|%s|%s|%s|%s|%s]",
  939. uuid, MAC2STR(dev->mac_addr), dev->device_name,
  940. dev->manufacturer, dev->model_name,
  941. dev->model_number, dev->serial_number,
  942. wps_dev_type_bin2str(dev->pri_dev_type, devtype,
  943. sizeof(devtype)));
  944. if (len > 0 && len < (int) sizeof(txt))
  945. wpa_printf(MSG_INFO, "%s", txt);
  946. }
  947. static void wpas_wps_set_sel_reg_cb(void *ctx, int sel_reg, u16 dev_passwd_id,
  948. u16 sel_reg_config_methods)
  949. {
  950. #ifdef CONFIG_WPS_ER
  951. struct wpa_supplicant *wpa_s = ctx;
  952. if (wpa_s->wps_er == NULL)
  953. return;
  954. wpa_printf(MSG_DEBUG, "WPS ER: SetSelectedRegistrar - sel_reg=%d "
  955. "dev_password_id=%u sel_reg_config_methods=0x%x",
  956. sel_reg, dev_passwd_id, sel_reg_config_methods);
  957. wps_er_set_sel_reg(wpa_s->wps_er, sel_reg, dev_passwd_id,
  958. sel_reg_config_methods);
  959. #endif /* CONFIG_WPS_ER */
  960. }
  961. static u16 wps_fix_config_methods(u16 config_methods)
  962. {
  963. #ifdef CONFIG_WPS2
  964. if ((config_methods &
  965. (WPS_CONFIG_DISPLAY | WPS_CONFIG_VIRT_DISPLAY |
  966. WPS_CONFIG_PHY_DISPLAY)) == WPS_CONFIG_DISPLAY) {
  967. wpa_printf(MSG_INFO, "WPS: Converting display to "
  968. "virtual_display for WPS 2.0 compliance");
  969. config_methods |= WPS_CONFIG_VIRT_DISPLAY;
  970. }
  971. if ((config_methods &
  972. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_VIRT_PUSHBUTTON |
  973. WPS_CONFIG_PHY_PUSHBUTTON)) == WPS_CONFIG_PUSHBUTTON) {
  974. wpa_printf(MSG_INFO, "WPS: Converting push_button to "
  975. "virtual_push_button for WPS 2.0 compliance");
  976. config_methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  977. }
  978. #endif /* CONFIG_WPS2 */
  979. return config_methods;
  980. }
  981. static void wpas_wps_set_uuid(struct wpa_supplicant *wpa_s,
  982. struct wps_context *wps)
  983. {
  984. wpa_printf(MSG_DEBUG, "WPS: Set UUID for interface %s", wpa_s->ifname);
  985. if (is_nil_uuid(wpa_s->conf->uuid)) {
  986. struct wpa_supplicant *first;
  987. first = wpa_s->global->ifaces;
  988. while (first && first->next)
  989. first = first->next;
  990. if (first && first != wpa_s) {
  991. if (wps != wpa_s->global->ifaces->wps)
  992. os_memcpy(wps->uuid,
  993. wpa_s->global->ifaces->wps->uuid,
  994. WPS_UUID_LEN);
  995. wpa_hexdump(MSG_DEBUG, "WPS: UUID from the first "
  996. "interface", wps->uuid, WPS_UUID_LEN);
  997. } else {
  998. uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
  999. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC "
  1000. "address", wps->uuid, WPS_UUID_LEN);
  1001. }
  1002. } else {
  1003. os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
  1004. wpa_hexdump(MSG_DEBUG, "WPS: UUID based on configuration",
  1005. wps->uuid, WPS_UUID_LEN);
  1006. }
  1007. }
  1008. static void wpas_wps_set_vendor_ext_m1(struct wpa_supplicant *wpa_s,
  1009. struct wps_context *wps)
  1010. {
  1011. wpabuf_free(wps->dev.vendor_ext_m1);
  1012. wps->dev.vendor_ext_m1 = NULL;
  1013. if (wpa_s->conf->wps_vendor_ext_m1) {
  1014. wps->dev.vendor_ext_m1 =
  1015. wpabuf_dup(wpa_s->conf->wps_vendor_ext_m1);
  1016. if (!wps->dev.vendor_ext_m1) {
  1017. wpa_printf(MSG_ERROR, "WPS: Cannot "
  1018. "allocate memory for vendor_ext_m1");
  1019. }
  1020. }
  1021. }
  1022. int wpas_wps_init(struct wpa_supplicant *wpa_s)
  1023. {
  1024. struct wps_context *wps;
  1025. struct wps_registrar_config rcfg;
  1026. struct hostapd_hw_modes *modes;
  1027. u16 m;
  1028. wps = os_zalloc(sizeof(*wps));
  1029. if (wps == NULL)
  1030. return -1;
  1031. wps->cred_cb = wpa_supplicant_wps_cred;
  1032. wps->event_cb = wpa_supplicant_wps_event;
  1033. wps->cb_ctx = wpa_s;
  1034. wps->dev.device_name = wpa_s->conf->device_name;
  1035. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1036. wps->dev.model_name = wpa_s->conf->model_name;
  1037. wps->dev.model_number = wpa_s->conf->model_number;
  1038. wps->dev.serial_number = wpa_s->conf->serial_number;
  1039. wps->config_methods =
  1040. wps_config_methods_str2bin(wpa_s->conf->config_methods);
  1041. if ((wps->config_methods & (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1042. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1043. wpa_printf(MSG_ERROR, "WPS: Both Label and Display config "
  1044. "methods are not allowed at the same time");
  1045. os_free(wps);
  1046. return -1;
  1047. }
  1048. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1049. wps->dev.config_methods = wps->config_methods;
  1050. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1051. WPS_DEV_TYPE_LEN);
  1052. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1053. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1054. WPS_DEV_TYPE_LEN * wps->dev.num_sec_dev_types);
  1055. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1056. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1057. modes = wpa_s->hw.modes;
  1058. if (modes) {
  1059. for (m = 0; m < wpa_s->hw.num_modes; m++) {
  1060. if (modes[m].mode == HOSTAPD_MODE_IEEE80211B ||
  1061. modes[m].mode == HOSTAPD_MODE_IEEE80211G)
  1062. wps->dev.rf_bands |= WPS_RF_24GHZ;
  1063. else if (modes[m].mode == HOSTAPD_MODE_IEEE80211A)
  1064. wps->dev.rf_bands |= WPS_RF_50GHZ;
  1065. }
  1066. }
  1067. if (wps->dev.rf_bands == 0) {
  1068. /*
  1069. * Default to claiming support for both bands if the driver
  1070. * does not provide support for fetching supported bands.
  1071. */
  1072. wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ;
  1073. }
  1074. os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
  1075. wpas_wps_set_uuid(wpa_s, wps);
  1076. wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
  1077. wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
  1078. os_memset(&rcfg, 0, sizeof(rcfg));
  1079. rcfg.new_psk_cb = wpas_wps_new_psk_cb;
  1080. rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
  1081. rcfg.set_sel_reg_cb = wpas_wps_set_sel_reg_cb;
  1082. rcfg.cb_ctx = wpa_s;
  1083. wps->registrar = wps_registrar_init(wps, &rcfg);
  1084. if (wps->registrar == NULL) {
  1085. wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
  1086. os_free(wps);
  1087. return -1;
  1088. }
  1089. wpa_s->wps = wps;
  1090. return 0;
  1091. }
  1092. void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
  1093. {
  1094. eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
  1095. if (wpa_s->wps == NULL)
  1096. return;
  1097. #ifdef CONFIG_WPS_ER
  1098. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1099. wpa_s->wps_er = NULL;
  1100. #endif /* CONFIG_WPS_ER */
  1101. wps_registrar_deinit(wpa_s->wps->registrar);
  1102. wpabuf_free(wpa_s->wps->dh_pubkey);
  1103. wpabuf_free(wpa_s->wps->dh_privkey);
  1104. wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
  1105. wpabuf_free(wpa_s->wps->oob_conf.dev_password);
  1106. wpabuf_free(wpa_s->wps->dev.vendor_ext_m1);
  1107. os_free(wpa_s->wps->network_key);
  1108. os_free(wpa_s->wps);
  1109. wpa_s->wps = NULL;
  1110. }
  1111. int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
  1112. struct wpa_ssid *ssid, struct wpa_scan_res *bss)
  1113. {
  1114. struct wpabuf *wps_ie;
  1115. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  1116. return -1;
  1117. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1118. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1119. if (!wps_ie) {
  1120. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1121. return 0;
  1122. }
  1123. if (!wps_is_selected_pbc_registrar(wps_ie)) {
  1124. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1125. "without active PBC Registrar");
  1126. wpabuf_free(wps_ie);
  1127. return 0;
  1128. }
  1129. /* TODO: overlap detection */
  1130. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1131. "(Active PBC)");
  1132. wpabuf_free(wps_ie);
  1133. return 1;
  1134. }
  1135. if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1136. if (!wps_ie) {
  1137. wpa_printf(MSG_DEBUG, " skip - non-WPS AP");
  1138. return 0;
  1139. }
  1140. /*
  1141. * Start with WPS APs that advertise our address as an
  1142. * authorized MAC (v2.0) or active PIN Registrar (v1.0) and
  1143. * allow any WPS AP after couple of scans since some APs do not
  1144. * set Selected Registrar attribute properly when using
  1145. * external Registrar.
  1146. */
  1147. if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  1148. if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
  1149. wpa_printf(MSG_DEBUG, " skip - WPS AP "
  1150. "without active PIN Registrar");
  1151. wpabuf_free(wps_ie);
  1152. return 0;
  1153. }
  1154. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1155. } else {
  1156. wpa_printf(MSG_DEBUG, " selected based on WPS IE "
  1157. "(Authorized MAC or Active PIN)");
  1158. }
  1159. wpabuf_free(wps_ie);
  1160. return 1;
  1161. }
  1162. if (wps_ie) {
  1163. wpa_printf(MSG_DEBUG, " selected based on WPS IE");
  1164. wpabuf_free(wps_ie);
  1165. return 1;
  1166. }
  1167. return -1;
  1168. }
  1169. int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
  1170. struct wpa_ssid *ssid,
  1171. struct wpa_scan_res *bss)
  1172. {
  1173. struct wpabuf *wps_ie = NULL;
  1174. int ret = 0;
  1175. if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
  1176. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1177. if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
  1178. /* allow wildcard SSID for WPS PBC */
  1179. ret = 1;
  1180. }
  1181. } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
  1182. wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1183. if (wps_ie &&
  1184. (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1) ||
  1185. wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
  1186. /* allow wildcard SSID for WPS PIN */
  1187. ret = 1;
  1188. }
  1189. }
  1190. if (!ret && ssid->bssid_set &&
  1191. os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
  1192. /* allow wildcard SSID due to hardcoded BSSID match */
  1193. ret = 1;
  1194. }
  1195. #ifdef CONFIG_WPS_STRICT
  1196. if (wps_ie) {
  1197. if (wps_validate_beacon_probe_resp(wps_ie, bss->beacon_ie_len >
  1198. 0, bss->bssid) < 0)
  1199. ret = 0;
  1200. if (bss->beacon_ie_len) {
  1201. struct wpabuf *bcn_wps;
  1202. bcn_wps = wpa_scan_get_vendor_ie_multi_beacon(
  1203. bss, WPS_IE_VENDOR_TYPE);
  1204. if (bcn_wps == NULL) {
  1205. wpa_printf(MSG_DEBUG, "WPS: Mandatory WPS IE "
  1206. "missing from AP Beacon");
  1207. ret = 0;
  1208. } else {
  1209. if (wps_validate_beacon(wps_ie) < 0)
  1210. ret = 0;
  1211. wpabuf_free(bcn_wps);
  1212. }
  1213. }
  1214. }
  1215. #endif /* CONFIG_WPS_STRICT */
  1216. wpabuf_free(wps_ie);
  1217. return ret;
  1218. }
  1219. int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
  1220. struct wpa_bss *selected, struct wpa_ssid *ssid)
  1221. {
  1222. const u8 *sel_uuid, *uuid;
  1223. struct wpabuf *wps_ie;
  1224. int ret = 0;
  1225. struct wpa_bss *bss;
  1226. if (!eap_is_wps_pbc_enrollee(&ssid->eap))
  1227. return 0;
  1228. wpa_printf(MSG_DEBUG, "WPS: Check whether PBC session overlap is "
  1229. "present in scan results; selected BSSID " MACSTR,
  1230. MAC2STR(selected->bssid));
  1231. /* Make sure that only one AP is in active PBC mode */
  1232. wps_ie = wpa_bss_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
  1233. if (wps_ie) {
  1234. sel_uuid = wps_get_uuid_e(wps_ie);
  1235. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the selected BSS",
  1236. sel_uuid, UUID_LEN);
  1237. } else {
  1238. wpa_printf(MSG_DEBUG, "WPS: Selected BSS does not include "
  1239. "WPS IE?!");
  1240. sel_uuid = NULL;
  1241. }
  1242. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1243. struct wpabuf *ie;
  1244. if (bss == selected)
  1245. continue;
  1246. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1247. if (!ie)
  1248. continue;
  1249. if (!wps_is_selected_pbc_registrar(ie)) {
  1250. wpabuf_free(ie);
  1251. continue;
  1252. }
  1253. wpa_printf(MSG_DEBUG, "WPS: Another BSS in active PBC mode: "
  1254. MACSTR, MAC2STR(bss->bssid));
  1255. uuid = wps_get_uuid_e(ie);
  1256. wpa_hexdump(MSG_DEBUG, "WPS: UUID of the other BSS",
  1257. uuid, UUID_LEN);
  1258. if (sel_uuid == NULL || uuid == NULL ||
  1259. os_memcmp(sel_uuid, uuid, UUID_LEN) != 0) {
  1260. ret = 1; /* PBC overlap */
  1261. wpa_msg(wpa_s, MSG_INFO, "WPS: PBC overlap detected: "
  1262. MACSTR " and " MACSTR,
  1263. MAC2STR(selected->bssid),
  1264. MAC2STR(bss->bssid));
  1265. wpabuf_free(ie);
  1266. break;
  1267. }
  1268. /* TODO: verify that this is reasonable dual-band situation */
  1269. wpabuf_free(ie);
  1270. }
  1271. wpabuf_free(wps_ie);
  1272. return ret;
  1273. }
  1274. void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
  1275. {
  1276. struct wpa_bss *bss;
  1277. unsigned int pbc = 0, auth = 0, pin = 0, wps = 0;
  1278. if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
  1279. return;
  1280. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1281. struct wpabuf *ie;
  1282. ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1283. if (!ie)
  1284. continue;
  1285. if (wps_is_selected_pbc_registrar(ie))
  1286. pbc++;
  1287. else if (wps_is_addr_authorized(ie, wpa_s->own_addr, 0))
  1288. auth++;
  1289. else if (wps_is_selected_pin_registrar(ie))
  1290. pin++;
  1291. else
  1292. wps++;
  1293. wpabuf_free(ie);
  1294. }
  1295. if (pbc)
  1296. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
  1297. else if (auth)
  1298. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_AUTH);
  1299. else if (pin)
  1300. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
  1301. else if (wps)
  1302. wpa_msg_ctrl(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
  1303. }
  1304. int wpas_wps_searching(struct wpa_supplicant *wpa_s)
  1305. {
  1306. struct wpa_ssid *ssid;
  1307. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1308. if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
  1309. return 1;
  1310. }
  1311. return 0;
  1312. }
  1313. int wpas_wps_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
  1314. char *end)
  1315. {
  1316. struct wpabuf *wps_ie;
  1317. int ret;
  1318. wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len, WPS_DEV_OUI_WFA);
  1319. if (wps_ie == NULL)
  1320. return 0;
  1321. ret = wps_attr_text(wps_ie, buf, end);
  1322. wpabuf_free(wps_ie);
  1323. return ret;
  1324. }
  1325. int wpas_wps_er_start(struct wpa_supplicant *wpa_s, const char *filter)
  1326. {
  1327. #ifdef CONFIG_WPS_ER
  1328. if (wpa_s->wps_er) {
  1329. wps_er_refresh(wpa_s->wps_er);
  1330. return 0;
  1331. }
  1332. wpa_s->wps_er = wps_er_init(wpa_s->wps, wpa_s->ifname, filter);
  1333. if (wpa_s->wps_er == NULL)
  1334. return -1;
  1335. return 0;
  1336. #else /* CONFIG_WPS_ER */
  1337. return 0;
  1338. #endif /* CONFIG_WPS_ER */
  1339. }
  1340. int wpas_wps_er_stop(struct wpa_supplicant *wpa_s)
  1341. {
  1342. #ifdef CONFIG_WPS_ER
  1343. wps_er_deinit(wpa_s->wps_er, NULL, NULL);
  1344. wpa_s->wps_er = NULL;
  1345. #endif /* CONFIG_WPS_ER */
  1346. return 0;
  1347. }
  1348. #ifdef CONFIG_WPS_ER
  1349. int wpas_wps_er_add_pin(struct wpa_supplicant *wpa_s, const u8 *addr,
  1350. const char *uuid, const char *pin)
  1351. {
  1352. u8 u[UUID_LEN];
  1353. int any = 0;
  1354. if (os_strcmp(uuid, "any") == 0)
  1355. any = 1;
  1356. else if (uuid_str2bin(uuid, u))
  1357. return -1;
  1358. return wps_registrar_add_pin(wpa_s->wps->registrar, addr,
  1359. any ? NULL : u,
  1360. (const u8 *) pin, os_strlen(pin), 300);
  1361. }
  1362. int wpas_wps_er_pbc(struct wpa_supplicant *wpa_s, const char *uuid)
  1363. {
  1364. u8 u[UUID_LEN];
  1365. if (uuid_str2bin(uuid, u))
  1366. return -1;
  1367. return wps_er_pbc(wpa_s->wps_er, u);
  1368. }
  1369. int wpas_wps_er_learn(struct wpa_supplicant *wpa_s, const char *uuid,
  1370. const char *pin)
  1371. {
  1372. u8 u[UUID_LEN];
  1373. if (uuid_str2bin(uuid, u))
  1374. return -1;
  1375. return wps_er_learn(wpa_s->wps_er, u, (const u8 *) pin,
  1376. os_strlen(pin));
  1377. }
  1378. int wpas_wps_er_set_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1379. int id)
  1380. {
  1381. u8 u[UUID_LEN];
  1382. struct wpa_ssid *ssid;
  1383. struct wps_credential cred;
  1384. if (uuid_str2bin(uuid, u))
  1385. return -1;
  1386. ssid = wpa_config_get_network(wpa_s->conf, id);
  1387. if (ssid == NULL || ssid->ssid == NULL)
  1388. return -1;
  1389. os_memset(&cred, 0, sizeof(cred));
  1390. if (ssid->ssid_len > 32)
  1391. return -1;
  1392. os_memcpy(cred.ssid, ssid->ssid, ssid->ssid_len);
  1393. cred.ssid_len = ssid->ssid_len;
  1394. if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
  1395. cred.auth_type = (ssid->proto & WPA_PROTO_RSN) ?
  1396. WPS_AUTH_WPA2PSK : WPS_AUTH_WPAPSK;
  1397. if (ssid->pairwise_cipher & WPA_CIPHER_CCMP)
  1398. cred.encr_type = WPS_ENCR_AES;
  1399. else
  1400. cred.encr_type = WPS_ENCR_TKIP;
  1401. if (ssid->passphrase) {
  1402. cred.key_len = os_strlen(ssid->passphrase);
  1403. if (cred.key_len >= 64)
  1404. return -1;
  1405. os_memcpy(cred.key, ssid->passphrase, cred.key_len);
  1406. } else if (ssid->psk_set) {
  1407. cred.key_len = 32;
  1408. os_memcpy(cred.key, ssid->psk, 32);
  1409. } else
  1410. return -1;
  1411. } else {
  1412. cred.auth_type = WPS_AUTH_OPEN;
  1413. cred.encr_type = WPS_ENCR_NONE;
  1414. }
  1415. return wps_er_set_config(wpa_s->wps_er, u, &cred);
  1416. }
  1417. int wpas_wps_er_config(struct wpa_supplicant *wpa_s, const char *uuid,
  1418. const char *pin, struct wps_new_ap_settings *settings)
  1419. {
  1420. u8 u[UUID_LEN];
  1421. struct wps_credential cred;
  1422. size_t len;
  1423. if (uuid_str2bin(uuid, u))
  1424. return -1;
  1425. if (settings->ssid_hex == NULL || settings->auth == NULL ||
  1426. settings->encr == NULL || settings->key_hex == NULL)
  1427. return -1;
  1428. os_memset(&cred, 0, sizeof(cred));
  1429. len = os_strlen(settings->ssid_hex);
  1430. if ((len & 1) || len > 2 * sizeof(cred.ssid) ||
  1431. hexstr2bin(settings->ssid_hex, cred.ssid, len / 2))
  1432. return -1;
  1433. cred.ssid_len = len / 2;
  1434. len = os_strlen(settings->key_hex);
  1435. if ((len & 1) || len > 2 * sizeof(cred.key) ||
  1436. hexstr2bin(settings->key_hex, cred.key, len / 2))
  1437. return -1;
  1438. cred.key_len = len / 2;
  1439. if (os_strcmp(settings->auth, "OPEN") == 0)
  1440. cred.auth_type = WPS_AUTH_OPEN;
  1441. else if (os_strcmp(settings->auth, "WPAPSK") == 0)
  1442. cred.auth_type = WPS_AUTH_WPAPSK;
  1443. else if (os_strcmp(settings->auth, "WPA2PSK") == 0)
  1444. cred.auth_type = WPS_AUTH_WPA2PSK;
  1445. else
  1446. return -1;
  1447. if (os_strcmp(settings->encr, "NONE") == 0)
  1448. cred.encr_type = WPS_ENCR_NONE;
  1449. else if (os_strcmp(settings->encr, "WEP") == 0)
  1450. cred.encr_type = WPS_ENCR_WEP;
  1451. else if (os_strcmp(settings->encr, "TKIP") == 0)
  1452. cred.encr_type = WPS_ENCR_TKIP;
  1453. else if (os_strcmp(settings->encr, "CCMP") == 0)
  1454. cred.encr_type = WPS_ENCR_AES;
  1455. else
  1456. return -1;
  1457. return wps_er_config(wpa_s->wps_er, u, (const u8 *) pin,
  1458. os_strlen(pin), &cred);
  1459. }
  1460. static int callbacks_pending = 0;
  1461. static void wpas_wps_terminate_cb(void *ctx)
  1462. {
  1463. wpa_printf(MSG_DEBUG, "WPS ER: Terminated");
  1464. if (--callbacks_pending <= 0)
  1465. eloop_terminate();
  1466. }
  1467. #endif /* CONFIG_WPS_ER */
  1468. int wpas_wps_terminate_pending(struct wpa_supplicant *wpa_s)
  1469. {
  1470. #ifdef CONFIG_WPS_ER
  1471. if (wpa_s->wps_er) {
  1472. callbacks_pending++;
  1473. wps_er_deinit(wpa_s->wps_er, wpas_wps_terminate_cb, wpa_s);
  1474. wpa_s->wps_er = NULL;
  1475. return 1;
  1476. }
  1477. #endif /* CONFIG_WPS_ER */
  1478. return 0;
  1479. }
  1480. int wpas_wps_in_progress(struct wpa_supplicant *wpa_s)
  1481. {
  1482. struct wpa_ssid *ssid;
  1483. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1484. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  1485. return 1;
  1486. }
  1487. return 0;
  1488. }
  1489. void wpas_wps_update_config(struct wpa_supplicant *wpa_s)
  1490. {
  1491. struct wps_context *wps = wpa_s->wps;
  1492. if (wps == NULL)
  1493. return;
  1494. if (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS) {
  1495. wps->config_methods = wps_config_methods_str2bin(
  1496. wpa_s->conf->config_methods);
  1497. if ((wps->config_methods &
  1498. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) ==
  1499. (WPS_CONFIG_DISPLAY | WPS_CONFIG_LABEL)) {
  1500. wpa_printf(MSG_ERROR, "WPS: Both Label and Display "
  1501. "config methods are not allowed at the "
  1502. "same time");
  1503. wps->config_methods &= ~WPS_CONFIG_LABEL;
  1504. }
  1505. }
  1506. wps->config_methods = wps_fix_config_methods(wps->config_methods);
  1507. if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
  1508. os_memcpy(wps->dev.pri_dev_type, wpa_s->conf->device_type,
  1509. WPS_DEV_TYPE_LEN);
  1510. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) {
  1511. wps->dev.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
  1512. os_memcpy(wps->dev.sec_dev_type, wpa_s->conf->sec_device_type,
  1513. wps->dev.num_sec_dev_types * WPS_DEV_TYPE_LEN);
  1514. }
  1515. if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION)
  1516. wpas_wps_set_vendor_ext_m1(wpa_s, wps);
  1517. if (wpa_s->conf->changed_parameters & CFG_CHANGED_OS_VERSION)
  1518. wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
  1519. if (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)
  1520. wpas_wps_set_uuid(wpa_s, wps);
  1521. if (wpa_s->conf->changed_parameters &
  1522. (CFG_CHANGED_DEVICE_NAME | CFG_CHANGED_WPS_STRING)) {
  1523. /* Update pointers to make sure they refer current values */
  1524. wps->dev.device_name = wpa_s->conf->device_name;
  1525. wps->dev.manufacturer = wpa_s->conf->manufacturer;
  1526. wps->dev.model_name = wpa_s->conf->model_name;
  1527. wps->dev.model_number = wpa_s->conf->model_number;
  1528. wps->dev.serial_number = wpa_s->conf->serial_number;
  1529. }
  1530. }
  1531. #ifdef CONFIG_WPS_NFC
  1532. struct wpabuf * wpas_wps_nfc_token(struct wpa_supplicant *wpa_s, int ndef)
  1533. {
  1534. struct wpabuf *priv = NULL, *pub = NULL, *pw;
  1535. void *dh_ctx;
  1536. struct wpabuf *ret;
  1537. pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
  1538. if (pw == NULL)
  1539. return NULL;
  1540. if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
  1541. WPS_OOB_DEVICE_PASSWORD_LEN)) {
  1542. wpabuf_free(pw);
  1543. return NULL;
  1544. }
  1545. dh_ctx = dh5_init(&priv, &pub);
  1546. if (dh_ctx == NULL) {
  1547. wpabuf_free(pw);
  1548. return NULL;
  1549. }
  1550. dh5_free(dh_ctx);
  1551. wpa_s->conf->wps_nfc_dev_pw_id = 0x10 + os_random() % 0xfff0;
  1552. wpabuf_free(wpa_s->conf->wps_nfc_dh_pubkey);
  1553. wpa_s->conf->wps_nfc_dh_pubkey = pub;
  1554. wpabuf_free(wpa_s->conf->wps_nfc_dh_privkey);
  1555. wpa_s->conf->wps_nfc_dh_privkey = priv;
  1556. wpabuf_free(wpa_s->conf->wps_nfc_dev_pw);
  1557. wpa_s->conf->wps_nfc_dev_pw = pw;
  1558. ret = wps_build_nfc_pw_token(wpa_s->conf->wps_nfc_dev_pw_id,
  1559. wpa_s->conf->wps_nfc_dh_pubkey,
  1560. wpa_s->conf->wps_nfc_dev_pw);
  1561. if (ndef && ret) {
  1562. struct wpabuf *tmp;
  1563. tmp = ndef_build_wifi(ret);
  1564. wpabuf_free(ret);
  1565. if (tmp == NULL)
  1566. return NULL;
  1567. ret = tmp;
  1568. }
  1569. return ret;
  1570. }
  1571. int wpas_wps_start_nfc(struct wpa_supplicant *wpa_s, const u8 *bssid)
  1572. {
  1573. struct wps_context *wps = wpa_s->wps;
  1574. char pw[32 * 2 + 1];
  1575. if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
  1576. wpa_s->conf->wps_nfc_dh_privkey == NULL ||
  1577. wpa_s->conf->wps_nfc_dev_pw == NULL)
  1578. return -1;
  1579. dh5_free(wps->dh_ctx);
  1580. wpabuf_free(wps->dh_pubkey);
  1581. wpabuf_free(wps->dh_privkey);
  1582. wps->dh_privkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
  1583. wps->dh_pubkey = wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
  1584. if (wps->dh_privkey == NULL || wps->dh_pubkey == NULL) {
  1585. wps->dh_ctx = NULL;
  1586. wpabuf_free(wps->dh_pubkey);
  1587. wps->dh_pubkey = NULL;
  1588. wpabuf_free(wps->dh_privkey);
  1589. wps->dh_privkey = NULL;
  1590. return -1;
  1591. }
  1592. wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, wps->dh_pubkey);
  1593. if (wps->dh_ctx == NULL)
  1594. return -1;
  1595. wpa_snprintf_hex_uppercase(pw, sizeof(pw),
  1596. wpabuf_head(wpa_s->conf->wps_nfc_dev_pw),
  1597. wpabuf_len(wpa_s->conf->wps_nfc_dev_pw));
  1598. return wpas_wps_start_pin(wpa_s, bssid, pw, 0,
  1599. wpa_s->conf->wps_nfc_dev_pw_id);
  1600. }
  1601. #endif /* CONFIG_WPS_NFC */