wps_registrar.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  1. /*
  2. * Wi-Fi Protected Setup - Registrar
  3. * Copyright (c) 2008-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sha256.h"
  17. #include "base64.h"
  18. #include "ieee802_11_defs.h"
  19. #include "eloop.h"
  20. #include "wps_i.h"
  21. #include "wps_dev_attr.h"
  22. #include "wps_upnp.h"
  23. #include "crypto.h"
  24. #define WPS_WORKAROUNDS
  25. struct wps_uuid_pin {
  26. struct wps_uuid_pin *next;
  27. u8 uuid[WPS_UUID_LEN];
  28. int wildcard_uuid;
  29. u8 *pin;
  30. size_t pin_len;
  31. #define PIN_LOCKED BIT(0)
  32. #define PIN_EXPIRES BIT(1)
  33. int flags;
  34. struct os_time expiration;
  35. };
  36. static void wps_free_pin(struct wps_uuid_pin *pin)
  37. {
  38. os_free(pin->pin);
  39. os_free(pin);
  40. }
  41. static void wps_free_pins(struct wps_uuid_pin *pins)
  42. {
  43. struct wps_uuid_pin *pin, *prev;
  44. pin = pins;
  45. while (pin) {
  46. prev = pin;
  47. pin = pin->next;
  48. wps_free_pin(prev);
  49. }
  50. }
  51. struct wps_pbc_session {
  52. struct wps_pbc_session *next;
  53. u8 addr[ETH_ALEN];
  54. u8 uuid_e[WPS_UUID_LEN];
  55. struct os_time timestamp;
  56. };
  57. static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
  58. {
  59. struct wps_pbc_session *prev;
  60. while (pbc) {
  61. prev = pbc;
  62. pbc = pbc->next;
  63. os_free(prev);
  64. }
  65. }
  66. struct wps_registrar {
  67. struct wps_context *wps;
  68. int pbc;
  69. int selected_registrar;
  70. int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
  71. size_t psk_len);
  72. int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
  73. const u8 *probe_resp_ie, size_t probe_resp_ie_len);
  74. void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
  75. const struct wps_device_data *dev);
  76. void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
  77. const u8 *uuid_e);
  78. void *cb_ctx;
  79. struct wps_uuid_pin *pins;
  80. struct wps_pbc_session *pbc_sessions;
  81. int skip_cred_build;
  82. struct wpabuf *extra_cred;
  83. int disable_auto_conf;
  84. int sel_reg_dev_password_id_override;
  85. int sel_reg_config_methods_override;
  86. int static_wep_only;
  87. };
  88. static int wps_set_ie(struct wps_registrar *reg);
  89. static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
  90. static void wps_registrar_set_selected_timeout(void *eloop_ctx,
  91. void *timeout_ctx);
  92. static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
  93. const u8 *addr, const u8 *uuid_e)
  94. {
  95. struct wps_pbc_session *pbc, *prev = NULL;
  96. struct os_time now;
  97. os_get_time(&now);
  98. pbc = reg->pbc_sessions;
  99. while (pbc) {
  100. if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
  101. os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
  102. if (prev)
  103. prev->next = pbc->next;
  104. else
  105. reg->pbc_sessions = pbc->next;
  106. break;
  107. }
  108. prev = pbc;
  109. pbc = pbc->next;
  110. }
  111. if (!pbc) {
  112. pbc = os_zalloc(sizeof(*pbc));
  113. if (pbc == NULL)
  114. return;
  115. os_memcpy(pbc->addr, addr, ETH_ALEN);
  116. if (uuid_e)
  117. os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
  118. }
  119. pbc->next = reg->pbc_sessions;
  120. reg->pbc_sessions = pbc;
  121. pbc->timestamp = now;
  122. /* remove entries that have timed out */
  123. prev = pbc;
  124. pbc = pbc->next;
  125. while (pbc) {
  126. if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
  127. prev->next = NULL;
  128. wps_free_pbc_sessions(pbc);
  129. break;
  130. }
  131. prev = pbc;
  132. pbc = pbc->next;
  133. }
  134. }
  135. static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
  136. const u8 *addr, const u8 *uuid_e)
  137. {
  138. struct wps_pbc_session *pbc, *prev = NULL;
  139. pbc = reg->pbc_sessions;
  140. while (pbc) {
  141. if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
  142. os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
  143. if (prev)
  144. prev->next = pbc->next;
  145. else
  146. reg->pbc_sessions = pbc->next;
  147. os_free(pbc);
  148. break;
  149. }
  150. prev = pbc;
  151. pbc = pbc->next;
  152. }
  153. }
  154. static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
  155. const u8 *addr, const u8 *uuid_e)
  156. {
  157. int count = 0;
  158. struct wps_pbc_session *pbc;
  159. struct os_time now;
  160. os_get_time(&now);
  161. for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
  162. if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
  163. break;
  164. if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
  165. uuid_e == NULL ||
  166. os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
  167. count++;
  168. }
  169. if (addr || uuid_e)
  170. count++;
  171. return count > 1 ? 1 : 0;
  172. }
  173. static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
  174. {
  175. wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
  176. wps->wps_state);
  177. wpabuf_put_be16(msg, ATTR_WPS_STATE);
  178. wpabuf_put_be16(msg, 1);
  179. wpabuf_put_u8(msg, wps->wps_state);
  180. return 0;
  181. }
  182. #ifdef CONFIG_WPS_UPNP
  183. static void wps_registrar_free_pending_m2(struct wps_context *wps)
  184. {
  185. struct upnp_pending_message *p, *p2, *prev = NULL;
  186. p = wps->upnp_msgs;
  187. while (p) {
  188. if (p->type == WPS_M2 || p->type == WPS_M2D) {
  189. if (prev == NULL)
  190. wps->upnp_msgs = p->next;
  191. else
  192. prev->next = p->next;
  193. wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
  194. p2 = p;
  195. p = p->next;
  196. wpabuf_free(p2->msg);
  197. os_free(p2);
  198. continue;
  199. }
  200. prev = p;
  201. p = p->next;
  202. }
  203. }
  204. #endif /* CONFIG_WPS_UPNP */
  205. static int wps_build_ap_setup_locked(struct wps_context *wps,
  206. struct wpabuf *msg)
  207. {
  208. if (wps->ap_setup_locked) {
  209. wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
  210. wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
  211. wpabuf_put_be16(msg, 1);
  212. wpabuf_put_u8(msg, 1);
  213. }
  214. return 0;
  215. }
  216. static int wps_build_selected_registrar(struct wps_registrar *reg,
  217. struct wpabuf *msg)
  218. {
  219. if (!reg->selected_registrar)
  220. return 0;
  221. wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
  222. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
  223. wpabuf_put_be16(msg, 1);
  224. wpabuf_put_u8(msg, 1);
  225. return 0;
  226. }
  227. static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
  228. struct wpabuf *msg)
  229. {
  230. u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
  231. if (!reg->selected_registrar)
  232. return 0;
  233. if (reg->sel_reg_dev_password_id_override >= 0)
  234. id = reg->sel_reg_dev_password_id_override;
  235. wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
  236. wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
  237. wpabuf_put_be16(msg, 2);
  238. wpabuf_put_be16(msg, id);
  239. return 0;
  240. }
  241. static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
  242. struct wpabuf *msg)
  243. {
  244. u16 methods;
  245. if (!reg->selected_registrar)
  246. return 0;
  247. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  248. if (reg->pbc)
  249. methods |= WPS_CONFIG_PUSHBUTTON;
  250. if (reg->sel_reg_config_methods_override >= 0)
  251. methods = reg->sel_reg_config_methods_override;
  252. wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
  253. methods);
  254. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
  255. wpabuf_put_be16(msg, 2);
  256. wpabuf_put_be16(msg, methods);
  257. return 0;
  258. }
  259. static int wps_build_probe_config_methods(struct wps_registrar *reg,
  260. struct wpabuf *msg)
  261. {
  262. u16 methods;
  263. methods = 0;
  264. wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
  265. wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
  266. wpabuf_put_be16(msg, 2);
  267. wpabuf_put_be16(msg, methods);
  268. return 0;
  269. }
  270. static int wps_build_config_methods_r(struct wps_registrar *reg,
  271. struct wpabuf *msg)
  272. {
  273. u16 methods;
  274. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  275. if (reg->pbc)
  276. methods |= WPS_CONFIG_PUSHBUTTON;
  277. return wps_build_config_methods(msg, methods);
  278. }
  279. static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
  280. {
  281. u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
  282. wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
  283. wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
  284. wpabuf_put_be16(msg, 1);
  285. wpabuf_put_u8(msg, resp);
  286. return 0;
  287. }
  288. /**
  289. * wps_registrar_init - Initialize WPS Registrar data
  290. * @wps: Pointer to longterm WPS context
  291. * @cfg: Registrar configuration
  292. * Returns: Pointer to allocated Registrar data or %NULL on failure
  293. *
  294. * This function is used to initialize WPS Registrar functionality. It can be
  295. * used for a single Registrar run (e.g., when run in a supplicant) or multiple
  296. * runs (e.g., when run as an internal Registrar in an AP). Caller is
  297. * responsible for freeing the returned data with wps_registrar_deinit() when
  298. * Registrar functionality is not needed anymore.
  299. */
  300. struct wps_registrar *
  301. wps_registrar_init(struct wps_context *wps,
  302. const struct wps_registrar_config *cfg)
  303. {
  304. struct wps_registrar *reg = os_zalloc(sizeof(*reg));
  305. if (reg == NULL)
  306. return NULL;
  307. reg->wps = wps;
  308. reg->new_psk_cb = cfg->new_psk_cb;
  309. reg->set_ie_cb = cfg->set_ie_cb;
  310. reg->pin_needed_cb = cfg->pin_needed_cb;
  311. reg->reg_success_cb = cfg->reg_success_cb;
  312. reg->cb_ctx = cfg->cb_ctx;
  313. reg->skip_cred_build = cfg->skip_cred_build;
  314. if (cfg->extra_cred) {
  315. reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
  316. cfg->extra_cred_len);
  317. if (reg->extra_cred == NULL) {
  318. os_free(reg);
  319. return NULL;
  320. }
  321. }
  322. reg->disable_auto_conf = cfg->disable_auto_conf;
  323. reg->sel_reg_dev_password_id_override = -1;
  324. reg->sel_reg_config_methods_override = -1;
  325. reg->static_wep_only = cfg->static_wep_only;
  326. if (wps_set_ie(reg)) {
  327. wps_registrar_deinit(reg);
  328. return NULL;
  329. }
  330. return reg;
  331. }
  332. /**
  333. * wps_registrar_deinit - Deinitialize WPS Registrar data
  334. * @reg: Registrar data from wps_registrar_init()
  335. */
  336. void wps_registrar_deinit(struct wps_registrar *reg)
  337. {
  338. if (reg == NULL)
  339. return;
  340. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  341. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  342. wps_free_pins(reg->pins);
  343. wps_free_pbc_sessions(reg->pbc_sessions);
  344. wpabuf_free(reg->extra_cred);
  345. os_free(reg);
  346. }
  347. /**
  348. * wps_registrar_add_pin - Configure a new PIN for Registrar
  349. * @reg: Registrar data from wps_registrar_init()
  350. * @uuid: UUID-E or %NULL for wildcard (any UUID)
  351. * @pin: PIN (Device Password)
  352. * @pin_len: Length of pin in octets
  353. * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
  354. * Returns: 0 on success, -1 on failure
  355. */
  356. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
  357. const u8 *pin, size_t pin_len, int timeout)
  358. {
  359. struct wps_uuid_pin *p;
  360. p = os_zalloc(sizeof(*p));
  361. if (p == NULL)
  362. return -1;
  363. if (uuid == NULL)
  364. p->wildcard_uuid = 1;
  365. else
  366. os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
  367. p->pin = os_malloc(pin_len);
  368. if (p->pin == NULL) {
  369. os_free(p);
  370. return -1;
  371. }
  372. os_memcpy(p->pin, pin, pin_len);
  373. p->pin_len = pin_len;
  374. if (timeout) {
  375. p->flags |= PIN_EXPIRES;
  376. os_get_time(&p->expiration);
  377. p->expiration.sec += timeout;
  378. }
  379. p->next = reg->pins;
  380. reg->pins = p;
  381. wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
  382. timeout);
  383. wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
  384. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
  385. reg->selected_registrar = 1;
  386. reg->pbc = 0;
  387. wps_set_ie(reg);
  388. return 0;
  389. }
  390. static void wps_registrar_expire_pins(struct wps_registrar *reg)
  391. {
  392. struct wps_uuid_pin *pin, *prev, *del;
  393. struct os_time now;
  394. os_get_time(&now);
  395. prev = NULL;
  396. pin = reg->pins;
  397. while (pin) {
  398. if ((pin->flags & PIN_EXPIRES) &&
  399. os_time_before(&pin->expiration, &now)) {
  400. if (prev == NULL)
  401. reg->pins = pin->next;
  402. else
  403. prev->next = pin->next;
  404. del = pin;
  405. pin = pin->next;
  406. wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
  407. del->uuid, WPS_UUID_LEN);
  408. wps_free_pin(del);
  409. continue;
  410. }
  411. prev = pin;
  412. pin = pin->next;
  413. }
  414. }
  415. /**
  416. * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
  417. * @reg: Registrar data from wps_registrar_init()
  418. * @uuid: UUID-E
  419. * Returns: 0 on success, -1 on failure (e.g., PIN not found)
  420. */
  421. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
  422. {
  423. struct wps_uuid_pin *pin, *prev;
  424. prev = NULL;
  425. pin = reg->pins;
  426. while (pin) {
  427. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  428. if (prev == NULL)
  429. reg->pins = pin->next;
  430. else
  431. prev->next = pin->next;
  432. wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
  433. pin->uuid, WPS_UUID_LEN);
  434. wps_free_pin(pin);
  435. return 0;
  436. }
  437. prev = pin;
  438. pin = pin->next;
  439. }
  440. return -1;
  441. }
  442. static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
  443. const u8 *uuid, size_t *pin_len)
  444. {
  445. struct wps_uuid_pin *pin;
  446. wps_registrar_expire_pins(reg);
  447. pin = reg->pins;
  448. while (pin) {
  449. if (!pin->wildcard_uuid &&
  450. os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
  451. break;
  452. pin = pin->next;
  453. }
  454. if (!pin) {
  455. /* Check for wildcard UUIDs since none of the UUID-specific
  456. * PINs matched */
  457. pin = reg->pins;
  458. while (pin) {
  459. if (pin->wildcard_uuid == 1) {
  460. wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
  461. "PIN. Assigned it for this UUID-E");
  462. pin->wildcard_uuid = 2;
  463. os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
  464. break;
  465. }
  466. pin = pin->next;
  467. }
  468. }
  469. if (!pin)
  470. return NULL;
  471. /*
  472. * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
  473. * that could otherwise avoid PIN invalidations.
  474. */
  475. if (pin->flags & PIN_LOCKED) {
  476. wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
  477. "allow concurrent re-use");
  478. return NULL;
  479. }
  480. *pin_len = pin->pin_len;
  481. pin->flags |= PIN_LOCKED;
  482. return pin->pin;
  483. }
  484. /**
  485. * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
  486. * @reg: Registrar data from wps_registrar_init()
  487. * @uuid: UUID-E
  488. * Returns: 0 on success, -1 on failure
  489. *
  490. * PINs are locked to enforce only one concurrent use. This function unlocks a
  491. * PIN to allow it to be used again. If the specified PIN was configured using
  492. * a wildcard UUID, it will be removed instead of allowing multiple uses.
  493. */
  494. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
  495. {
  496. struct wps_uuid_pin *pin;
  497. pin = reg->pins;
  498. while (pin) {
  499. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  500. if (pin->wildcard_uuid == 2) {
  501. wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
  502. "wildcard PIN");
  503. return wps_registrar_invalidate_pin(reg, uuid);
  504. }
  505. pin->flags &= ~PIN_LOCKED;
  506. return 0;
  507. }
  508. pin = pin->next;
  509. }
  510. return -1;
  511. }
  512. static void wps_registrar_stop_pbc(struct wps_registrar *reg)
  513. {
  514. reg->selected_registrar = 0;
  515. reg->pbc = 0;
  516. wps_set_ie(reg);
  517. }
  518. static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
  519. {
  520. struct wps_registrar *reg = eloop_ctx;
  521. wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
  522. wps_registrar_stop_pbc(reg);
  523. }
  524. /**
  525. * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
  526. * @reg: Registrar data from wps_registrar_init()
  527. * Returns: 0 on success, -1 on failure
  528. *
  529. * This function is called on an AP when a push button is pushed to activate
  530. * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
  531. * or when a PBC registration is completed.
  532. */
  533. int wps_registrar_button_pushed(struct wps_registrar *reg)
  534. {
  535. if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
  536. wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
  537. "mode");
  538. return -1;
  539. }
  540. wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
  541. reg->selected_registrar = 1;
  542. reg->pbc = 1;
  543. wps_set_ie(reg);
  544. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  545. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
  546. reg, NULL);
  547. return 0;
  548. }
  549. static void wps_registrar_pbc_completed(struct wps_registrar *reg)
  550. {
  551. wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
  552. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  553. wps_registrar_stop_pbc(reg);
  554. }
  555. /**
  556. * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
  557. * @reg: Registrar data from wps_registrar_init()
  558. * @addr: MAC address of the Probe Request sender
  559. * @wps_data: WPS IE contents
  560. *
  561. * This function is called on an AP when a Probe Request with WPS IE is
  562. * received. This is used to track PBC mode use and to detect possible overlap
  563. * situation with other WPS APs.
  564. */
  565. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  566. const struct wpabuf *wps_data)
  567. {
  568. struct wps_parse_attr attr;
  569. u16 methods;
  570. wpa_hexdump_buf(MSG_MSGDUMP,
  571. "WPS: Probe Request with WPS data received",
  572. wps_data);
  573. if (wps_parse_msg(wps_data, &attr) < 0)
  574. return;
  575. if (!wps_version_supported(attr.version)) {
  576. wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
  577. "version 0x%x", attr.version ? *attr.version : 0);
  578. return;
  579. }
  580. if (attr.config_methods == NULL) {
  581. wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
  582. "Probe Request");
  583. return;
  584. }
  585. methods = WPA_GET_BE16(attr.config_methods);
  586. if (!(methods & WPS_CONFIG_PUSHBUTTON))
  587. return; /* Not PBC */
  588. wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
  589. MACSTR, MAC2STR(addr));
  590. wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
  591. }
  592. static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
  593. const u8 *psk, size_t psk_len)
  594. {
  595. if (reg->new_psk_cb == NULL)
  596. return 0;
  597. return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
  598. }
  599. static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
  600. const struct wps_device_data *dev)
  601. {
  602. if (reg->pin_needed_cb == NULL)
  603. return;
  604. reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
  605. }
  606. static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
  607. const u8 *uuid_e)
  608. {
  609. if (reg->reg_success_cb == NULL)
  610. return;
  611. reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
  612. }
  613. static int wps_cb_set_ie(struct wps_registrar *reg,
  614. const struct wpabuf *beacon_ie,
  615. const struct wpabuf *probe_resp_ie)
  616. {
  617. if (reg->set_ie_cb == NULL)
  618. return 0;
  619. return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
  620. wpabuf_len(beacon_ie),
  621. wpabuf_head(probe_resp_ie),
  622. wpabuf_len(probe_resp_ie));
  623. }
  624. /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
  625. static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
  626. {
  627. struct wpabuf *ie;
  628. const u8 *pos, *end;
  629. ie = wpabuf_alloc(wpabuf_len(data) + 100);
  630. if (ie == NULL) {
  631. wpabuf_free(data);
  632. return NULL;
  633. }
  634. pos = wpabuf_head(data);
  635. end = pos + wpabuf_len(data);
  636. while (end > pos) {
  637. size_t frag_len = end - pos;
  638. if (frag_len > 251)
  639. frag_len = 251;
  640. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  641. wpabuf_put_u8(ie, 4 + frag_len);
  642. wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
  643. wpabuf_put_data(ie, pos, frag_len);
  644. pos += frag_len;
  645. }
  646. wpabuf_free(data);
  647. return ie;
  648. }
  649. static int wps_set_ie(struct wps_registrar *reg)
  650. {
  651. struct wpabuf *beacon;
  652. struct wpabuf *probe;
  653. int ret;
  654. wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
  655. beacon = wpabuf_alloc(300);
  656. if (beacon == NULL)
  657. return -1;
  658. probe = wpabuf_alloc(400);
  659. if (probe == NULL) {
  660. wpabuf_free(beacon);
  661. return -1;
  662. }
  663. if (wps_build_version(beacon) ||
  664. wps_build_wps_state(reg->wps, beacon) ||
  665. wps_build_ap_setup_locked(reg->wps, beacon) ||
  666. wps_build_selected_registrar(reg, beacon) ||
  667. wps_build_sel_reg_dev_password_id(reg, beacon) ||
  668. wps_build_sel_reg_config_methods(reg, beacon) ||
  669. wps_build_version(probe) ||
  670. wps_build_wps_state(reg->wps, probe) ||
  671. wps_build_ap_setup_locked(reg->wps, probe) ||
  672. wps_build_selected_registrar(reg, probe) ||
  673. wps_build_sel_reg_dev_password_id(reg, probe) ||
  674. wps_build_sel_reg_config_methods(reg, probe) ||
  675. wps_build_resp_type(reg, probe) ||
  676. wps_build_uuid_e(probe, reg->wps->uuid) ||
  677. wps_build_device_attrs(&reg->wps->dev, probe) ||
  678. wps_build_probe_config_methods(reg, probe) ||
  679. wps_build_rf_bands(&reg->wps->dev, probe)) {
  680. wpabuf_free(beacon);
  681. wpabuf_free(probe);
  682. return -1;
  683. }
  684. beacon = wps_ie_encapsulate(beacon);
  685. probe = wps_ie_encapsulate(probe);
  686. if (!beacon || !probe) {
  687. wpabuf_free(beacon);
  688. wpabuf_free(probe);
  689. return -1;
  690. }
  691. if (reg->static_wep_only) {
  692. /*
  693. * Windows XP and Vista clients can get confused about
  694. * EAP-Identity/Request when they probe the network with
  695. * EAPOL-Start. In such a case, they may assume the network is
  696. * using IEEE 802.1X and prompt user for a certificate while
  697. * the correct (non-WPS) behavior would be to ask for the
  698. * static WEP key. As a workaround, use Microsoft Provisioning
  699. * IE to advertise that legacy 802.1X is not supported.
  700. */
  701. const u8 ms_wps[7] = {
  702. WLAN_EID_VENDOR_SPECIFIC, 5,
  703. /* Microsoft Provisioning IE (00:50:f2:5) */
  704. 0x00, 0x50, 0xf2, 5,
  705. 0x00 /* no legacy 802.1X or MS WPS */
  706. };
  707. wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
  708. "into Beacon/Probe Response frames");
  709. wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
  710. wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
  711. }
  712. ret = wps_cb_set_ie(reg, beacon, probe);
  713. wpabuf_free(beacon);
  714. wpabuf_free(probe);
  715. return ret;
  716. }
  717. static int wps_get_dev_password(struct wps_data *wps)
  718. {
  719. const u8 *pin;
  720. size_t pin_len = 0;
  721. os_free(wps->dev_password);
  722. wps->dev_password = NULL;
  723. if (wps->pbc) {
  724. wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
  725. pin = (const u8 *) "00000000";
  726. pin_len = 8;
  727. } else {
  728. pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
  729. &pin_len);
  730. }
  731. if (pin == NULL) {
  732. wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
  733. "the Enrollee");
  734. wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
  735. &wps->peer_dev);
  736. return -1;
  737. }
  738. wps->dev_password = os_malloc(pin_len);
  739. if (wps->dev_password == NULL)
  740. return -1;
  741. os_memcpy(wps->dev_password, pin, pin_len);
  742. wps->dev_password_len = pin_len;
  743. return 0;
  744. }
  745. static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
  746. {
  747. wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
  748. wpabuf_put_be16(msg, ATTR_UUID_R);
  749. wpabuf_put_be16(msg, WPS_UUID_LEN);
  750. wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
  751. return 0;
  752. }
  753. static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
  754. {
  755. u8 *hash;
  756. const u8 *addr[4];
  757. size_t len[4];
  758. if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
  759. return -1;
  760. wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
  761. wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
  762. wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
  763. if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
  764. wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
  765. "R-Hash derivation");
  766. return -1;
  767. }
  768. wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
  769. wpabuf_put_be16(msg, ATTR_R_HASH1);
  770. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  771. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  772. /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
  773. addr[0] = wps->snonce;
  774. len[0] = WPS_SECRET_NONCE_LEN;
  775. addr[1] = wps->psk1;
  776. len[1] = WPS_PSK_LEN;
  777. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  778. len[2] = wpabuf_len(wps->dh_pubkey_e);
  779. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  780. len[3] = wpabuf_len(wps->dh_pubkey_r);
  781. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  782. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
  783. wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
  784. wpabuf_put_be16(msg, ATTR_R_HASH2);
  785. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  786. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  787. /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
  788. addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
  789. addr[1] = wps->psk2;
  790. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  791. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
  792. return 0;
  793. }
  794. static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
  795. {
  796. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
  797. wpabuf_put_be16(msg, ATTR_R_SNONCE1);
  798. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  799. wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
  800. return 0;
  801. }
  802. static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
  803. {
  804. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
  805. wpabuf_put_be16(msg, ATTR_R_SNONCE2);
  806. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  807. wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
  808. WPS_SECRET_NONCE_LEN);
  809. return 0;
  810. }
  811. static int wps_build_cred_network_idx(struct wpabuf *msg,
  812. struct wps_credential *cred)
  813. {
  814. wpa_printf(MSG_DEBUG, "WPS: * Network Index");
  815. wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
  816. wpabuf_put_be16(msg, 1);
  817. wpabuf_put_u8(msg, 1);
  818. return 0;
  819. }
  820. static int wps_build_cred_ssid(struct wpabuf *msg,
  821. struct wps_credential *cred)
  822. {
  823. wpa_printf(MSG_DEBUG, "WPS: * SSID");
  824. wpabuf_put_be16(msg, ATTR_SSID);
  825. wpabuf_put_be16(msg, cred->ssid_len);
  826. wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
  827. return 0;
  828. }
  829. static int wps_build_cred_auth_type(struct wpabuf *msg,
  830. struct wps_credential *cred)
  831. {
  832. wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
  833. cred->auth_type);
  834. wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
  835. wpabuf_put_be16(msg, 2);
  836. wpabuf_put_be16(msg, cred->auth_type);
  837. return 0;
  838. }
  839. static int wps_build_cred_encr_type(struct wpabuf *msg,
  840. struct wps_credential *cred)
  841. {
  842. wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
  843. cred->encr_type);
  844. wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
  845. wpabuf_put_be16(msg, 2);
  846. wpabuf_put_be16(msg, cred->encr_type);
  847. return 0;
  848. }
  849. static int wps_build_cred_network_key(struct wpabuf *msg,
  850. struct wps_credential *cred)
  851. {
  852. wpa_printf(MSG_DEBUG, "WPS: * Network Key");
  853. wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
  854. wpabuf_put_be16(msg, cred->key_len);
  855. wpabuf_put_data(msg, cred->key, cred->key_len);
  856. return 0;
  857. }
  858. static int wps_build_cred_mac_addr(struct wpabuf *msg,
  859. struct wps_credential *cred)
  860. {
  861. wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
  862. MAC2STR(cred->mac_addr));
  863. wpabuf_put_be16(msg, ATTR_MAC_ADDR);
  864. wpabuf_put_be16(msg, ETH_ALEN);
  865. wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
  866. return 0;
  867. }
  868. static int wps_build_credential(struct wpabuf *msg,
  869. struct wps_credential *cred)
  870. {
  871. if (wps_build_cred_network_idx(msg, cred) ||
  872. wps_build_cred_ssid(msg, cred) ||
  873. wps_build_cred_auth_type(msg, cred) ||
  874. wps_build_cred_encr_type(msg, cred) ||
  875. wps_build_cred_network_key(msg, cred) ||
  876. wps_build_cred_mac_addr(msg, cred))
  877. return -1;
  878. return 0;
  879. }
  880. int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
  881. {
  882. struct wpabuf *cred;
  883. if (wps->wps->registrar->skip_cred_build)
  884. goto skip_cred_build;
  885. wpa_printf(MSG_DEBUG, "WPS: * Credential");
  886. os_memset(&wps->cred, 0, sizeof(wps->cred));
  887. os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
  888. wps->cred.ssid_len = wps->wps->ssid_len;
  889. /* Select the best authentication and encryption type */
  890. if (wps->auth_type & WPS_AUTH_WPA2PSK)
  891. wps->auth_type = WPS_AUTH_WPA2PSK;
  892. else if (wps->auth_type & WPS_AUTH_WPAPSK)
  893. wps->auth_type = WPS_AUTH_WPAPSK;
  894. else if (wps->auth_type & WPS_AUTH_OPEN)
  895. wps->auth_type = WPS_AUTH_OPEN;
  896. else if (wps->auth_type & WPS_AUTH_SHARED)
  897. wps->auth_type = WPS_AUTH_SHARED;
  898. else {
  899. wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
  900. wps->auth_type);
  901. return -1;
  902. }
  903. wps->cred.auth_type = wps->auth_type;
  904. if (wps->auth_type == WPS_AUTH_WPA2PSK ||
  905. wps->auth_type == WPS_AUTH_WPAPSK) {
  906. if (wps->encr_type & WPS_ENCR_AES)
  907. wps->encr_type = WPS_ENCR_AES;
  908. else if (wps->encr_type & WPS_ENCR_TKIP)
  909. wps->encr_type = WPS_ENCR_TKIP;
  910. else {
  911. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  912. "type for WPA/WPA2");
  913. return -1;
  914. }
  915. } else {
  916. if (wps->encr_type & WPS_ENCR_WEP)
  917. wps->encr_type = WPS_ENCR_WEP;
  918. else if (wps->encr_type & WPS_ENCR_NONE)
  919. wps->encr_type = WPS_ENCR_NONE;
  920. else {
  921. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  922. "type for non-WPA/WPA2 mode");
  923. return -1;
  924. }
  925. }
  926. wps->cred.encr_type = wps->encr_type;
  927. /* Set MAC address in the Credential to be the AP's address (BSSID) */
  928. os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
  929. if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
  930. !wps->wps->registrar->disable_auto_conf) {
  931. u8 r[16];
  932. /* Generate a random passphrase */
  933. if (os_get_random(r, sizeof(r)) < 0)
  934. return -1;
  935. os_free(wps->new_psk);
  936. wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
  937. if (wps->new_psk == NULL)
  938. return -1;
  939. wps->new_psk_len--; /* remove newline */
  940. while (wps->new_psk_len &&
  941. wps->new_psk[wps->new_psk_len - 1] == '=')
  942. wps->new_psk_len--;
  943. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
  944. wps->new_psk, wps->new_psk_len);
  945. os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
  946. wps->cred.key_len = wps->new_psk_len;
  947. } else if (wps->wps->network_key) {
  948. os_memcpy(wps->cred.key, wps->wps->network_key,
  949. wps->wps->network_key_len);
  950. wps->cred.key_len = wps->wps->network_key_len;
  951. } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  952. char hex[65];
  953. /* Generate a random per-device PSK */
  954. os_free(wps->new_psk);
  955. wps->new_psk_len = 32;
  956. wps->new_psk = os_malloc(wps->new_psk_len);
  957. if (wps->new_psk == NULL)
  958. return -1;
  959. if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
  960. os_free(wps->new_psk);
  961. wps->new_psk = NULL;
  962. return -1;
  963. }
  964. wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
  965. wps->new_psk, wps->new_psk_len);
  966. wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
  967. wps->new_psk_len);
  968. os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
  969. wps->cred.key_len = wps->new_psk_len * 2;
  970. }
  971. cred = wpabuf_alloc(200);
  972. if (cred == NULL)
  973. return -1;
  974. if (wps_build_credential(cred, &wps->cred)) {
  975. wpabuf_free(cred);
  976. return -1;
  977. }
  978. wpabuf_put_be16(msg, ATTR_CRED);
  979. wpabuf_put_be16(msg, wpabuf_len(cred));
  980. wpabuf_put_buf(msg, cred);
  981. wpabuf_free(cred);
  982. skip_cred_build:
  983. if (wps->wps->registrar->extra_cred) {
  984. wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
  985. wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
  986. }
  987. return 0;
  988. }
  989. static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
  990. {
  991. wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
  992. if (wps_build_credential(msg, &wps->cred))
  993. return -1;
  994. return 0;
  995. }
  996. static struct wpabuf * wps_build_m2(struct wps_data *wps)
  997. {
  998. struct wpabuf *msg;
  999. if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
  1000. return NULL;
  1001. wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
  1002. wps->nonce_r, WPS_NONCE_LEN);
  1003. wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
  1004. wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
  1005. msg = wpabuf_alloc(1000);
  1006. if (msg == NULL)
  1007. return NULL;
  1008. if (wps_build_version(msg) ||
  1009. wps_build_msg_type(msg, WPS_M2) ||
  1010. wps_build_enrollee_nonce(wps, msg) ||
  1011. wps_build_registrar_nonce(wps, msg) ||
  1012. wps_build_uuid_r(wps, msg) ||
  1013. wps_build_public_key(wps, msg) ||
  1014. wps_derive_keys(wps) ||
  1015. wps_build_auth_type_flags(wps, msg) ||
  1016. wps_build_encr_type_flags(wps, msg) ||
  1017. wps_build_conn_type_flags(wps, msg) ||
  1018. wps_build_config_methods_r(wps->wps->registrar, msg) ||
  1019. wps_build_device_attrs(&wps->wps->dev, msg) ||
  1020. wps_build_rf_bands(&wps->wps->dev, msg) ||
  1021. wps_build_assoc_state(wps, msg) ||
  1022. wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  1023. wps_build_dev_password_id(msg, wps->dev_pw_id) ||
  1024. wps_build_os_version(&wps->wps->dev, msg) ||
  1025. wps_build_authenticator(wps, msg)) {
  1026. wpabuf_free(msg);
  1027. return NULL;
  1028. }
  1029. wps->state = RECV_M3;
  1030. return msg;
  1031. }
  1032. static struct wpabuf * wps_build_m2d(struct wps_data *wps)
  1033. {
  1034. struct wpabuf *msg;
  1035. u16 err = WPS_CFG_NO_ERROR;
  1036. wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
  1037. msg = wpabuf_alloc(1000);
  1038. if (msg == NULL)
  1039. return NULL;
  1040. if (wps->wps->ap && wps->wps->ap_setup_locked)
  1041. err = WPS_CFG_SETUP_LOCKED;
  1042. if (wps_build_version(msg) ||
  1043. wps_build_msg_type(msg, WPS_M2D) ||
  1044. wps_build_enrollee_nonce(wps, msg) ||
  1045. wps_build_registrar_nonce(wps, msg) ||
  1046. wps_build_uuid_r(wps, msg) ||
  1047. wps_build_auth_type_flags(wps, msg) ||
  1048. wps_build_encr_type_flags(wps, msg) ||
  1049. wps_build_conn_type_flags(wps, msg) ||
  1050. wps_build_config_methods_r(wps->wps->registrar, msg) ||
  1051. wps_build_device_attrs(&wps->wps->dev, msg) ||
  1052. wps_build_rf_bands(&wps->wps->dev, msg) ||
  1053. wps_build_assoc_state(wps, msg) ||
  1054. wps_build_config_error(msg, err) ||
  1055. wps_build_os_version(&wps->wps->dev, msg)) {
  1056. wpabuf_free(msg);
  1057. return NULL;
  1058. }
  1059. wps->state = RECV_M2D_ACK;
  1060. return msg;
  1061. }
  1062. static struct wpabuf * wps_build_m4(struct wps_data *wps)
  1063. {
  1064. struct wpabuf *msg, *plain;
  1065. wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
  1066. wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  1067. plain = wpabuf_alloc(200);
  1068. if (plain == NULL)
  1069. return NULL;
  1070. msg = wpabuf_alloc(1000);
  1071. if (msg == NULL) {
  1072. wpabuf_free(plain);
  1073. return NULL;
  1074. }
  1075. if (wps_build_version(msg) ||
  1076. wps_build_msg_type(msg, WPS_M4) ||
  1077. wps_build_enrollee_nonce(wps, msg) ||
  1078. wps_build_r_hash(wps, msg) ||
  1079. wps_build_r_snonce1(wps, plain) ||
  1080. wps_build_key_wrap_auth(wps, plain) ||
  1081. wps_build_encr_settings(wps, msg, plain) ||
  1082. wps_build_authenticator(wps, msg)) {
  1083. wpabuf_free(plain);
  1084. wpabuf_free(msg);
  1085. return NULL;
  1086. }
  1087. wpabuf_free(plain);
  1088. wps->state = RECV_M5;
  1089. return msg;
  1090. }
  1091. static struct wpabuf * wps_build_m6(struct wps_data *wps)
  1092. {
  1093. struct wpabuf *msg, *plain;
  1094. wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
  1095. plain = wpabuf_alloc(200);
  1096. if (plain == NULL)
  1097. return NULL;
  1098. msg = wpabuf_alloc(1000);
  1099. if (msg == NULL) {
  1100. wpabuf_free(plain);
  1101. return NULL;
  1102. }
  1103. if (wps_build_version(msg) ||
  1104. wps_build_msg_type(msg, WPS_M6) ||
  1105. wps_build_enrollee_nonce(wps, msg) ||
  1106. wps_build_r_snonce2(wps, plain) ||
  1107. wps_build_key_wrap_auth(wps, plain) ||
  1108. wps_build_encr_settings(wps, msg, plain) ||
  1109. wps_build_authenticator(wps, msg)) {
  1110. wpabuf_free(plain);
  1111. wpabuf_free(msg);
  1112. return NULL;
  1113. }
  1114. wpabuf_free(plain);
  1115. wps->wps_pin_revealed = 1;
  1116. wps->state = RECV_M7;
  1117. return msg;
  1118. }
  1119. static struct wpabuf * wps_build_m8(struct wps_data *wps)
  1120. {
  1121. struct wpabuf *msg, *plain;
  1122. wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
  1123. plain = wpabuf_alloc(500);
  1124. if (plain == NULL)
  1125. return NULL;
  1126. msg = wpabuf_alloc(1000);
  1127. if (msg == NULL) {
  1128. wpabuf_free(plain);
  1129. return NULL;
  1130. }
  1131. if (wps_build_version(msg) ||
  1132. wps_build_msg_type(msg, WPS_M8) ||
  1133. wps_build_enrollee_nonce(wps, msg) ||
  1134. (wps->wps->ap && wps_build_cred(wps, plain)) ||
  1135. (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
  1136. wps_build_key_wrap_auth(wps, plain) ||
  1137. wps_build_encr_settings(wps, msg, plain) ||
  1138. wps_build_authenticator(wps, msg)) {
  1139. wpabuf_free(plain);
  1140. wpabuf_free(msg);
  1141. return NULL;
  1142. }
  1143. wpabuf_free(plain);
  1144. wps->state = RECV_DONE;
  1145. return msg;
  1146. }
  1147. static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
  1148. {
  1149. struct wpabuf *msg;
  1150. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
  1151. msg = wpabuf_alloc(1000);
  1152. if (msg == NULL)
  1153. return NULL;
  1154. if (wps_build_version(msg) ||
  1155. wps_build_msg_type(msg, WPS_WSC_ACK) ||
  1156. wps_build_enrollee_nonce(wps, msg) ||
  1157. wps_build_registrar_nonce(wps, msg)) {
  1158. wpabuf_free(msg);
  1159. return NULL;
  1160. }
  1161. return msg;
  1162. }
  1163. static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
  1164. {
  1165. struct wpabuf *msg;
  1166. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
  1167. msg = wpabuf_alloc(1000);
  1168. if (msg == NULL)
  1169. return NULL;
  1170. if (wps_build_version(msg) ||
  1171. wps_build_msg_type(msg, WPS_WSC_NACK) ||
  1172. wps_build_enrollee_nonce(wps, msg) ||
  1173. wps_build_registrar_nonce(wps, msg) ||
  1174. wps_build_config_error(msg, wps->config_error)) {
  1175. wpabuf_free(msg);
  1176. return NULL;
  1177. }
  1178. return msg;
  1179. }
  1180. struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
  1181. enum wsc_op_code *op_code)
  1182. {
  1183. struct wpabuf *msg;
  1184. #ifdef CONFIG_WPS_UPNP
  1185. if (wps->wps->wps_upnp) {
  1186. struct upnp_pending_message *p, *prev = NULL;
  1187. if (wps->ext_reg > 1)
  1188. wps_registrar_free_pending_m2(wps->wps);
  1189. p = wps->wps->upnp_msgs;
  1190. /* TODO: check pending message MAC address */
  1191. while (p && p->next) {
  1192. prev = p;
  1193. p = p->next;
  1194. }
  1195. if (p) {
  1196. wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
  1197. "UPnP");
  1198. if (prev)
  1199. prev->next = NULL;
  1200. else
  1201. wps->wps->upnp_msgs = NULL;
  1202. msg = p->msg;
  1203. os_free(p);
  1204. *op_code = WSC_MSG;
  1205. if (wps->ext_reg == 0)
  1206. wps->ext_reg = 1;
  1207. return msg;
  1208. }
  1209. }
  1210. if (wps->ext_reg) {
  1211. wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
  1212. "pending message available");
  1213. return NULL;
  1214. }
  1215. #endif /* CONFIG_WPS_UPNP */
  1216. switch (wps->state) {
  1217. case SEND_M2:
  1218. if (wps_get_dev_password(wps) < 0)
  1219. msg = wps_build_m2d(wps);
  1220. else
  1221. msg = wps_build_m2(wps);
  1222. *op_code = WSC_MSG;
  1223. break;
  1224. case SEND_M2D:
  1225. msg = wps_build_m2d(wps);
  1226. *op_code = WSC_MSG;
  1227. break;
  1228. case SEND_M4:
  1229. msg = wps_build_m4(wps);
  1230. *op_code = WSC_MSG;
  1231. break;
  1232. case SEND_M6:
  1233. msg = wps_build_m6(wps);
  1234. *op_code = WSC_MSG;
  1235. break;
  1236. case SEND_M8:
  1237. msg = wps_build_m8(wps);
  1238. *op_code = WSC_MSG;
  1239. break;
  1240. case RECV_DONE:
  1241. msg = wps_build_wsc_ack(wps);
  1242. *op_code = WSC_ACK;
  1243. break;
  1244. case SEND_WSC_NACK:
  1245. msg = wps_build_wsc_nack(wps);
  1246. *op_code = WSC_NACK;
  1247. break;
  1248. default:
  1249. wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
  1250. "a message", wps->state);
  1251. msg = NULL;
  1252. break;
  1253. }
  1254. if (*op_code == WSC_MSG && msg) {
  1255. /* Save a copy of the last message for Authenticator derivation
  1256. */
  1257. wpabuf_free(wps->last_msg);
  1258. wps->last_msg = wpabuf_dup(msg);
  1259. }
  1260. return msg;
  1261. }
  1262. static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
  1263. {
  1264. if (e_nonce == NULL) {
  1265. wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
  1266. return -1;
  1267. }
  1268. os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
  1269. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
  1270. wps->nonce_e, WPS_NONCE_LEN);
  1271. return 0;
  1272. }
  1273. static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
  1274. {
  1275. if (r_nonce == NULL) {
  1276. wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
  1277. return -1;
  1278. }
  1279. if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
  1280. wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
  1281. return -1;
  1282. }
  1283. return 0;
  1284. }
  1285. static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
  1286. {
  1287. if (uuid_e == NULL) {
  1288. wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
  1289. return -1;
  1290. }
  1291. os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
  1292. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
  1293. return 0;
  1294. }
  1295. static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
  1296. {
  1297. if (pw_id == NULL) {
  1298. wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
  1299. return -1;
  1300. }
  1301. wps->dev_pw_id = WPA_GET_BE16(pw_id);
  1302. wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
  1303. return 0;
  1304. }
  1305. static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
  1306. {
  1307. if (e_hash1 == NULL) {
  1308. wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
  1309. return -1;
  1310. }
  1311. os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
  1312. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
  1313. return 0;
  1314. }
  1315. static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
  1316. {
  1317. if (e_hash2 == NULL) {
  1318. wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
  1319. return -1;
  1320. }
  1321. os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
  1322. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
  1323. return 0;
  1324. }
  1325. static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
  1326. {
  1327. u8 hash[SHA256_MAC_LEN];
  1328. const u8 *addr[4];
  1329. size_t len[4];
  1330. if (e_snonce1 == NULL) {
  1331. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
  1332. return -1;
  1333. }
  1334. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
  1335. WPS_SECRET_NONCE_LEN);
  1336. /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
  1337. addr[0] = e_snonce1;
  1338. len[0] = WPS_SECRET_NONCE_LEN;
  1339. addr[1] = wps->psk1;
  1340. len[1] = WPS_PSK_LEN;
  1341. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1342. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1343. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1344. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1345. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1346. if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
  1347. wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
  1348. "not match with the pre-committed value");
  1349. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  1350. wps_pwd_auth_fail_event(wps->wps, 0, 1);
  1351. return -1;
  1352. }
  1353. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
  1354. "half of the device password");
  1355. return 0;
  1356. }
  1357. static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
  1358. {
  1359. u8 hash[SHA256_MAC_LEN];
  1360. const u8 *addr[4];
  1361. size_t len[4];
  1362. if (e_snonce2 == NULL) {
  1363. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
  1364. return -1;
  1365. }
  1366. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
  1367. WPS_SECRET_NONCE_LEN);
  1368. /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
  1369. addr[0] = e_snonce2;
  1370. len[0] = WPS_SECRET_NONCE_LEN;
  1371. addr[1] = wps->psk2;
  1372. len[1] = WPS_PSK_LEN;
  1373. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1374. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1375. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1376. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1377. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1378. if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
  1379. wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
  1380. "not match with the pre-committed value");
  1381. wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
  1382. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  1383. wps_pwd_auth_fail_event(wps->wps, 0, 2);
  1384. return -1;
  1385. }
  1386. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
  1387. "half of the device password");
  1388. wps->wps_pin_revealed = 0;
  1389. wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
  1390. return 0;
  1391. }
  1392. static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
  1393. {
  1394. if (mac_addr == NULL) {
  1395. wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
  1396. return -1;
  1397. }
  1398. wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
  1399. MAC2STR(mac_addr));
  1400. os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
  1401. os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
  1402. return 0;
  1403. }
  1404. static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
  1405. size_t pk_len)
  1406. {
  1407. if (pk == NULL || pk_len == 0) {
  1408. wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
  1409. return -1;
  1410. }
  1411. #ifdef CONFIG_WPS_OOB
  1412. if (wps->wps->oob_conf.pubkey_hash != NULL) {
  1413. const u8 *addr[1];
  1414. u8 hash[WPS_HASH_LEN];
  1415. addr[0] = pk;
  1416. sha256_vector(1, addr, &pk_len, hash);
  1417. if (os_memcmp(hash,
  1418. wpabuf_head(wps->wps->oob_conf.pubkey_hash),
  1419. WPS_OOB_PUBKEY_HASH_LEN) != 0) {
  1420. wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
  1421. return -1;
  1422. }
  1423. }
  1424. #endif /* CONFIG_WPS_OOB */
  1425. wpabuf_free(wps->dh_pubkey_e);
  1426. wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
  1427. if (wps->dh_pubkey_e == NULL)
  1428. return -1;
  1429. return 0;
  1430. }
  1431. static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
  1432. {
  1433. u16 auth_types;
  1434. if (auth == NULL) {
  1435. wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
  1436. "received");
  1437. return -1;
  1438. }
  1439. auth_types = WPA_GET_BE16(auth);
  1440. wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
  1441. auth_types);
  1442. wps->auth_type = wps->wps->auth_types & auth_types;
  1443. if (wps->auth_type == 0) {
  1444. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  1445. "authentication types (own 0x%x Enrollee 0x%x)",
  1446. wps->wps->auth_types, auth_types);
  1447. #ifdef WPS_WORKAROUNDS
  1448. /*
  1449. * Some deployed implementations seem to advertise incorrect
  1450. * information in this attribute. For example, Linksys WRT350N
  1451. * seems to have a byteorder bug that breaks this negotiation.
  1452. * In order to interoperate with existing implementations,
  1453. * assume that the Enrollee supports everything we do.
  1454. */
  1455. wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
  1456. "does not advertise supported authentication types "
  1457. "correctly");
  1458. wps->auth_type = wps->wps->auth_types;
  1459. #else /* WPS_WORKAROUNDS */
  1460. return -1;
  1461. #endif /* WPS_WORKAROUNDS */
  1462. }
  1463. return 0;
  1464. }
  1465. static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
  1466. {
  1467. u16 encr_types;
  1468. if (encr == NULL) {
  1469. wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
  1470. "received");
  1471. return -1;
  1472. }
  1473. encr_types = WPA_GET_BE16(encr);
  1474. wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
  1475. encr_types);
  1476. wps->encr_type = wps->wps->encr_types & encr_types;
  1477. if (wps->encr_type == 0) {
  1478. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  1479. "encryption types (own 0x%x Enrollee 0x%x)",
  1480. wps->wps->encr_types, encr_types);
  1481. #ifdef WPS_WORKAROUNDS
  1482. /*
  1483. * Some deployed implementations seem to advertise incorrect
  1484. * information in this attribute. For example, Linksys WRT350N
  1485. * seems to have a byteorder bug that breaks this negotiation.
  1486. * In order to interoperate with existing implementations,
  1487. * assume that the Enrollee supports everything we do.
  1488. */
  1489. wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
  1490. "does not advertise supported encryption types "
  1491. "correctly");
  1492. wps->encr_type = wps->wps->encr_types;
  1493. #else /* WPS_WORKAROUNDS */
  1494. return -1;
  1495. #endif /* WPS_WORKAROUNDS */
  1496. }
  1497. return 0;
  1498. }
  1499. static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
  1500. {
  1501. if (conn == NULL) {
  1502. wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
  1503. "received");
  1504. return -1;
  1505. }
  1506. wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
  1507. *conn);
  1508. return 0;
  1509. }
  1510. static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
  1511. {
  1512. u16 m;
  1513. if (methods == NULL) {
  1514. wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
  1515. return -1;
  1516. }
  1517. m = WPA_GET_BE16(methods);
  1518. wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
  1519. return 0;
  1520. }
  1521. static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
  1522. {
  1523. if (state == NULL) {
  1524. wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
  1525. "received");
  1526. return -1;
  1527. }
  1528. wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
  1529. *state);
  1530. return 0;
  1531. }
  1532. static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
  1533. {
  1534. u16 a;
  1535. if (assoc == NULL) {
  1536. wpa_printf(MSG_DEBUG, "WPS: No Association State received");
  1537. return -1;
  1538. }
  1539. a = WPA_GET_BE16(assoc);
  1540. wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
  1541. return 0;
  1542. }
  1543. static int wps_process_config_error(struct wps_data *wps, const u8 *err)
  1544. {
  1545. u16 e;
  1546. if (err == NULL) {
  1547. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
  1548. return -1;
  1549. }
  1550. e = WPA_GET_BE16(err);
  1551. wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
  1552. return 0;
  1553. }
  1554. static enum wps_process_res wps_process_m1(struct wps_data *wps,
  1555. struct wps_parse_attr *attr)
  1556. {
  1557. wpa_printf(MSG_DEBUG, "WPS: Received M1");
  1558. if (wps->state != RECV_M1) {
  1559. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1560. "receiving M1", wps->state);
  1561. return WPS_FAILURE;
  1562. }
  1563. if (wps_process_uuid_e(wps, attr->uuid_e) ||
  1564. wps_process_mac_addr(wps, attr->mac_addr) ||
  1565. wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  1566. wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
  1567. wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
  1568. wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
  1569. wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
  1570. wps_process_config_methods(wps, attr->config_methods) ||
  1571. wps_process_wps_state(wps, attr->wps_state) ||
  1572. wps_process_device_attrs(&wps->peer_dev, attr) ||
  1573. wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
  1574. wps_process_assoc_state(wps, attr->assoc_state) ||
  1575. wps_process_dev_password_id(wps, attr->dev_password_id) ||
  1576. wps_process_config_error(wps, attr->config_error) ||
  1577. wps_process_os_version(&wps->peer_dev, attr->os_version))
  1578. return WPS_FAILURE;
  1579. if (wps->dev_pw_id < 0x10 &&
  1580. wps->dev_pw_id != DEV_PW_DEFAULT &&
  1581. wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
  1582. wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
  1583. wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
  1584. (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
  1585. !wps->wps->registrar->pbc)) {
  1586. wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
  1587. wps->dev_pw_id);
  1588. wps->state = SEND_M2D;
  1589. return WPS_CONTINUE;
  1590. }
  1591. #ifdef CONFIG_WPS_OOB
  1592. if (wps->dev_pw_id >= 0x10 &&
  1593. wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
  1594. wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
  1595. "%d mismatch", wps->dev_pw_id);
  1596. wps->state = SEND_M2D;
  1597. return WPS_CONTINUE;
  1598. }
  1599. #endif /* CONFIG_WPS_OOB */
  1600. if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
  1601. if (wps_registrar_pbc_overlap(wps->wps->registrar,
  1602. wps->mac_addr_e, wps->uuid_e)) {
  1603. wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
  1604. "negotiation");
  1605. wps->state = SEND_M2D;
  1606. return WPS_CONTINUE;
  1607. }
  1608. wps_registrar_add_pbc_session(wps->wps->registrar,
  1609. wps->mac_addr_e, wps->uuid_e);
  1610. wps->pbc = 1;
  1611. }
  1612. wps->state = SEND_M2;
  1613. return WPS_CONTINUE;
  1614. }
  1615. static enum wps_process_res wps_process_m3(struct wps_data *wps,
  1616. const struct wpabuf *msg,
  1617. struct wps_parse_attr *attr)
  1618. {
  1619. wpa_printf(MSG_DEBUG, "WPS: Received M3");
  1620. if (wps->state != RECV_M3) {
  1621. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1622. "receiving M3", wps->state);
  1623. wps->state = SEND_WSC_NACK;
  1624. return WPS_CONTINUE;
  1625. }
  1626. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1627. wps_process_authenticator(wps, attr->authenticator, msg) ||
  1628. wps_process_e_hash1(wps, attr->e_hash1) ||
  1629. wps_process_e_hash2(wps, attr->e_hash2)) {
  1630. wps->state = SEND_WSC_NACK;
  1631. return WPS_CONTINUE;
  1632. }
  1633. wps->state = SEND_M4;
  1634. return WPS_CONTINUE;
  1635. }
  1636. static enum wps_process_res wps_process_m5(struct wps_data *wps,
  1637. const struct wpabuf *msg,
  1638. struct wps_parse_attr *attr)
  1639. {
  1640. struct wpabuf *decrypted;
  1641. struct wps_parse_attr eattr;
  1642. wpa_printf(MSG_DEBUG, "WPS: Received M5");
  1643. if (wps->state != RECV_M5) {
  1644. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1645. "receiving M5", wps->state);
  1646. wps->state = SEND_WSC_NACK;
  1647. return WPS_CONTINUE;
  1648. }
  1649. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1650. wps_process_authenticator(wps, attr->authenticator, msg)) {
  1651. wps->state = SEND_WSC_NACK;
  1652. return WPS_CONTINUE;
  1653. }
  1654. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  1655. attr->encr_settings_len);
  1656. if (decrypted == NULL) {
  1657. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  1658. "Settings attribute");
  1659. wps->state = SEND_WSC_NACK;
  1660. return WPS_CONTINUE;
  1661. }
  1662. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  1663. "attribute");
  1664. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  1665. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  1666. wps_process_e_snonce1(wps, eattr.e_snonce1)) {
  1667. wpabuf_free(decrypted);
  1668. wps->state = SEND_WSC_NACK;
  1669. return WPS_CONTINUE;
  1670. }
  1671. wpabuf_free(decrypted);
  1672. wps->state = SEND_M6;
  1673. return WPS_CONTINUE;
  1674. }
  1675. static void wps_sta_cred_cb(struct wps_data *wps)
  1676. {
  1677. /*
  1678. * Update credential to only include a single authentication and
  1679. * encryption type in case the AP configuration includes more than one
  1680. * option.
  1681. */
  1682. if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
  1683. wps->cred.auth_type = WPS_AUTH_WPA2PSK;
  1684. else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
  1685. wps->cred.auth_type = WPS_AUTH_WPAPSK;
  1686. if (wps->cred.encr_type & WPS_ENCR_AES)
  1687. wps->cred.encr_type = WPS_ENCR_AES;
  1688. else if (wps->cred.encr_type & WPS_ENCR_TKIP)
  1689. wps->cred.encr_type = WPS_ENCR_TKIP;
  1690. wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
  1691. "AP configuration");
  1692. if (wps->wps->cred_cb)
  1693. wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
  1694. }
  1695. static void wps_cred_update(struct wps_credential *dst,
  1696. struct wps_credential *src)
  1697. {
  1698. os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
  1699. dst->ssid_len = src->ssid_len;
  1700. dst->auth_type = src->auth_type;
  1701. dst->encr_type = src->encr_type;
  1702. dst->key_idx = src->key_idx;
  1703. os_memcpy(dst->key, src->key, sizeof(dst->key));
  1704. dst->key_len = src->key_len;
  1705. }
  1706. static int wps_process_ap_settings_r(struct wps_data *wps,
  1707. struct wps_parse_attr *attr)
  1708. {
  1709. if (wps->wps->ap)
  1710. return 0;
  1711. /* AP Settings Attributes in M7 when Enrollee is an AP */
  1712. if (wps_process_ap_settings(attr, &wps->cred) < 0)
  1713. return -1;
  1714. wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
  1715. if (wps->new_ap_settings) {
  1716. wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
  1717. "new settings");
  1718. wps_cred_update(&wps->cred, wps->new_ap_settings);
  1719. return 0;
  1720. } else {
  1721. /*
  1722. * Use the AP PIN only to receive the current AP settings, not
  1723. * to reconfigure the AP.
  1724. */
  1725. wps_sta_cred_cb(wps);
  1726. return 1;
  1727. }
  1728. }
  1729. static enum wps_process_res wps_process_m7(struct wps_data *wps,
  1730. const struct wpabuf *msg,
  1731. struct wps_parse_attr *attr)
  1732. {
  1733. struct wpabuf *decrypted;
  1734. struct wps_parse_attr eattr;
  1735. wpa_printf(MSG_DEBUG, "WPS: Received M7");
  1736. if (wps->state != RECV_M7) {
  1737. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1738. "receiving M7", wps->state);
  1739. wps->state = SEND_WSC_NACK;
  1740. return WPS_CONTINUE;
  1741. }
  1742. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1743. wps_process_authenticator(wps, attr->authenticator, msg)) {
  1744. wps->state = SEND_WSC_NACK;
  1745. return WPS_CONTINUE;
  1746. }
  1747. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  1748. attr->encr_settings_len);
  1749. if (decrypted == NULL) {
  1750. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  1751. "Settings attribute");
  1752. wps->state = SEND_WSC_NACK;
  1753. return WPS_CONTINUE;
  1754. }
  1755. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  1756. "attribute");
  1757. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  1758. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  1759. wps_process_e_snonce2(wps, eattr.e_snonce2) ||
  1760. wps_process_ap_settings_r(wps, &eattr)) {
  1761. wpabuf_free(decrypted);
  1762. wps->state = SEND_WSC_NACK;
  1763. return WPS_CONTINUE;
  1764. }
  1765. wpabuf_free(decrypted);
  1766. wps->state = SEND_M8;
  1767. return WPS_CONTINUE;
  1768. }
  1769. static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
  1770. const struct wpabuf *msg)
  1771. {
  1772. struct wps_parse_attr attr;
  1773. enum wps_process_res ret = WPS_CONTINUE;
  1774. wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
  1775. if (wps_parse_msg(msg, &attr) < 0)
  1776. return WPS_FAILURE;
  1777. if (!wps_version_supported(attr.version)) {
  1778. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1779. attr.version ? *attr.version : 0);
  1780. return WPS_FAILURE;
  1781. }
  1782. if (attr.msg_type == NULL) {
  1783. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1784. return WPS_FAILURE;
  1785. }
  1786. if (*attr.msg_type != WPS_M1 &&
  1787. (attr.registrar_nonce == NULL ||
  1788. os_memcmp(wps->nonce_r, attr.registrar_nonce,
  1789. WPS_NONCE_LEN != 0))) {
  1790. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1791. return WPS_FAILURE;
  1792. }
  1793. switch (*attr.msg_type) {
  1794. case WPS_M1:
  1795. #ifdef CONFIG_WPS_UPNP
  1796. if (wps->wps->wps_upnp && attr.mac_addr) {
  1797. /* Remove old pending messages when starting new run */
  1798. wps_free_pending_msgs(wps->wps->upnp_msgs);
  1799. wps->wps->upnp_msgs = NULL;
  1800. upnp_wps_device_send_wlan_event(
  1801. wps->wps->wps_upnp, attr.mac_addr,
  1802. UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
  1803. }
  1804. #endif /* CONFIG_WPS_UPNP */
  1805. ret = wps_process_m1(wps, &attr);
  1806. break;
  1807. case WPS_M3:
  1808. ret = wps_process_m3(wps, msg, &attr);
  1809. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  1810. wps_fail_event(wps->wps, WPS_M3);
  1811. break;
  1812. case WPS_M5:
  1813. ret = wps_process_m5(wps, msg, &attr);
  1814. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  1815. wps_fail_event(wps->wps, WPS_M5);
  1816. break;
  1817. case WPS_M7:
  1818. ret = wps_process_m7(wps, msg, &attr);
  1819. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  1820. wps_fail_event(wps->wps, WPS_M7);
  1821. break;
  1822. default:
  1823. wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
  1824. *attr.msg_type);
  1825. return WPS_FAILURE;
  1826. }
  1827. if (ret == WPS_CONTINUE) {
  1828. /* Save a copy of the last message for Authenticator derivation
  1829. */
  1830. wpabuf_free(wps->last_msg);
  1831. wps->last_msg = wpabuf_dup(msg);
  1832. }
  1833. return ret;
  1834. }
  1835. static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
  1836. const struct wpabuf *msg)
  1837. {
  1838. struct wps_parse_attr attr;
  1839. wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
  1840. if (wps_parse_msg(msg, &attr) < 0)
  1841. return WPS_FAILURE;
  1842. if (!wps_version_supported(attr.version)) {
  1843. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1844. attr.version ? *attr.version : 0);
  1845. return WPS_FAILURE;
  1846. }
  1847. if (attr.msg_type == NULL) {
  1848. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1849. return WPS_FAILURE;
  1850. }
  1851. if (*attr.msg_type != WPS_WSC_ACK) {
  1852. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1853. *attr.msg_type);
  1854. return WPS_FAILURE;
  1855. }
  1856. #ifdef CONFIG_WPS_UPNP
  1857. if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
  1858. upnp_wps_subscribers(wps->wps->wps_upnp)) {
  1859. if (wps->wps->upnp_msgs)
  1860. return WPS_CONTINUE;
  1861. wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
  1862. "external Registrar");
  1863. return WPS_PENDING;
  1864. }
  1865. #endif /* CONFIG_WPS_UPNP */
  1866. if (attr.registrar_nonce == NULL ||
  1867. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1868. {
  1869. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1870. return WPS_FAILURE;
  1871. }
  1872. if (attr.enrollee_nonce == NULL ||
  1873. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1874. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1875. return WPS_FAILURE;
  1876. }
  1877. if (wps->state == RECV_M2D_ACK) {
  1878. #ifdef CONFIG_WPS_UPNP
  1879. if (wps->wps->wps_upnp &&
  1880. upnp_wps_subscribers(wps->wps->wps_upnp)) {
  1881. if (wps->wps->upnp_msgs)
  1882. return WPS_CONTINUE;
  1883. if (wps->ext_reg == 0)
  1884. wps->ext_reg = 1;
  1885. wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
  1886. "external Registrar");
  1887. return WPS_PENDING;
  1888. }
  1889. #endif /* CONFIG_WPS_UPNP */
  1890. wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
  1891. "terminate negotiation");
  1892. }
  1893. return WPS_FAILURE;
  1894. }
  1895. static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
  1896. const struct wpabuf *msg)
  1897. {
  1898. struct wps_parse_attr attr;
  1899. int old_state;
  1900. wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
  1901. old_state = wps->state;
  1902. wps->state = SEND_WSC_NACK;
  1903. if (wps_parse_msg(msg, &attr) < 0)
  1904. return WPS_FAILURE;
  1905. if (!wps_version_supported(attr.version)) {
  1906. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1907. attr.version ? *attr.version : 0);
  1908. return WPS_FAILURE;
  1909. }
  1910. if (attr.msg_type == NULL) {
  1911. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1912. return WPS_FAILURE;
  1913. }
  1914. if (*attr.msg_type != WPS_WSC_NACK) {
  1915. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1916. *attr.msg_type);
  1917. return WPS_FAILURE;
  1918. }
  1919. #ifdef CONFIG_WPS_UPNP
  1920. if (wps->wps->wps_upnp && wps->ext_reg) {
  1921. wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
  1922. "Registrar terminated by the Enrollee");
  1923. return WPS_FAILURE;
  1924. }
  1925. #endif /* CONFIG_WPS_UPNP */
  1926. if (attr.registrar_nonce == NULL ||
  1927. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1928. {
  1929. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1930. return WPS_FAILURE;
  1931. }
  1932. if (attr.enrollee_nonce == NULL ||
  1933. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1934. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1935. return WPS_FAILURE;
  1936. }
  1937. if (attr.config_error == NULL) {
  1938. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
  1939. "in WSC_NACK");
  1940. return WPS_FAILURE;
  1941. }
  1942. wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
  1943. "Configuration Error %d", WPA_GET_BE16(attr.config_error));
  1944. switch (old_state) {
  1945. case RECV_M3:
  1946. wps_fail_event(wps->wps, WPS_M2);
  1947. break;
  1948. case RECV_M5:
  1949. wps_fail_event(wps->wps, WPS_M4);
  1950. break;
  1951. case RECV_M7:
  1952. wps_fail_event(wps->wps, WPS_M6);
  1953. break;
  1954. case RECV_DONE:
  1955. wps_fail_event(wps->wps, WPS_M8);
  1956. break;
  1957. default:
  1958. break;
  1959. }
  1960. return WPS_FAILURE;
  1961. }
  1962. static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
  1963. const struct wpabuf *msg)
  1964. {
  1965. struct wps_parse_attr attr;
  1966. wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
  1967. if (wps->state != RECV_DONE &&
  1968. (!wps->wps->wps_upnp || !wps->ext_reg)) {
  1969. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1970. "receiving WSC_Done", wps->state);
  1971. return WPS_FAILURE;
  1972. }
  1973. if (wps_parse_msg(msg, &attr) < 0)
  1974. return WPS_FAILURE;
  1975. if (!wps_version_supported(attr.version)) {
  1976. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1977. attr.version ? *attr.version : 0);
  1978. return WPS_FAILURE;
  1979. }
  1980. if (attr.msg_type == NULL) {
  1981. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1982. return WPS_FAILURE;
  1983. }
  1984. if (*attr.msg_type != WPS_WSC_DONE) {
  1985. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1986. *attr.msg_type);
  1987. return WPS_FAILURE;
  1988. }
  1989. #ifdef CONFIG_WPS_UPNP
  1990. if (wps->wps->wps_upnp && wps->ext_reg) {
  1991. wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
  1992. "Registrar completed successfully");
  1993. return WPS_DONE;
  1994. }
  1995. #endif /* CONFIG_WPS_UPNP */
  1996. if (attr.registrar_nonce == NULL ||
  1997. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1998. {
  1999. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  2000. return WPS_FAILURE;
  2001. }
  2002. if (attr.enrollee_nonce == NULL ||
  2003. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  2004. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  2005. return WPS_FAILURE;
  2006. }
  2007. wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
  2008. if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
  2009. wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
  2010. struct wps_credential cred;
  2011. wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
  2012. "on first Enrollee connection");
  2013. os_memset(&cred, 0, sizeof(cred));
  2014. os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
  2015. cred.ssid_len = wps->wps->ssid_len;
  2016. cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
  2017. cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
  2018. os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
  2019. cred.key_len = wps->new_psk_len;
  2020. wps->wps->wps_state = WPS_STATE_CONFIGURED;
  2021. wpa_hexdump_ascii_key(MSG_DEBUG,
  2022. "WPS: Generated random passphrase",
  2023. wps->new_psk, wps->new_psk_len);
  2024. if (wps->wps->cred_cb)
  2025. wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
  2026. os_free(wps->new_psk);
  2027. wps->new_psk = NULL;
  2028. }
  2029. if (!wps->wps->ap)
  2030. wps_sta_cred_cb(wps);
  2031. if (wps->new_psk) {
  2032. if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
  2033. wps->new_psk, wps->new_psk_len)) {
  2034. wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
  2035. "new PSK");
  2036. }
  2037. os_free(wps->new_psk);
  2038. wps->new_psk = NULL;
  2039. }
  2040. wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
  2041. if (wps->pbc) {
  2042. wps_registrar_remove_pbc_session(wps->wps->registrar,
  2043. wps->mac_addr_e, wps->uuid_e);
  2044. wps_registrar_pbc_completed(wps->wps->registrar);
  2045. }
  2046. wps_success_event(wps->wps);
  2047. return WPS_DONE;
  2048. }
  2049. enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
  2050. enum wsc_op_code op_code,
  2051. const struct wpabuf *msg)
  2052. {
  2053. enum wps_process_res ret;
  2054. wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
  2055. "op_code=%d)",
  2056. (unsigned long) wpabuf_len(msg), op_code);
  2057. #ifdef CONFIG_WPS_UPNP
  2058. if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
  2059. struct wps_parse_attr attr;
  2060. if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
  2061. *attr.msg_type == WPS_M3)
  2062. wps->ext_reg = 2; /* past M2/M2D phase */
  2063. }
  2064. if (wps->ext_reg > 1)
  2065. wps_registrar_free_pending_m2(wps->wps);
  2066. if (wps->wps->wps_upnp && wps->ext_reg &&
  2067. wps->wps->upnp_msgs == NULL &&
  2068. (op_code == WSC_MSG || op_code == WSC_Done)) {
  2069. struct wps_parse_attr attr;
  2070. int type;
  2071. if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
  2072. type = -1;
  2073. else
  2074. type = *attr.msg_type;
  2075. wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
  2076. " to external Registrar for processing", type);
  2077. upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
  2078. wps->mac_addr_e,
  2079. UPNP_WPS_WLANEVENT_TYPE_EAP,
  2080. msg);
  2081. if (op_code == WSC_MSG)
  2082. return WPS_PENDING;
  2083. } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
  2084. wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
  2085. "external Registrar");
  2086. return WPS_CONTINUE;
  2087. }
  2088. #endif /* CONFIG_WPS_UPNP */
  2089. switch (op_code) {
  2090. case WSC_MSG:
  2091. return wps_process_wsc_msg(wps, msg);
  2092. case WSC_ACK:
  2093. return wps_process_wsc_ack(wps, msg);
  2094. case WSC_NACK:
  2095. return wps_process_wsc_nack(wps, msg);
  2096. case WSC_Done:
  2097. ret = wps_process_wsc_done(wps, msg);
  2098. if (ret == WPS_FAILURE) {
  2099. wps->state = SEND_WSC_NACK;
  2100. wps_fail_event(wps->wps, WPS_WSC_DONE);
  2101. }
  2102. return ret;
  2103. default:
  2104. wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
  2105. return WPS_FAILURE;
  2106. }
  2107. }
  2108. int wps_registrar_update_ie(struct wps_registrar *reg)
  2109. {
  2110. return wps_set_ie(reg);
  2111. }
  2112. static void wps_registrar_set_selected_timeout(void *eloop_ctx,
  2113. void *timeout_ctx)
  2114. {
  2115. struct wps_registrar *reg = eloop_ctx;
  2116. wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
  2117. "unselect Registrar");
  2118. reg->selected_registrar = 0;
  2119. reg->pbc = 0;
  2120. reg->sel_reg_dev_password_id_override = -1;
  2121. reg->sel_reg_config_methods_override = -1;
  2122. wps_set_ie(reg);
  2123. }
  2124. /**
  2125. * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
  2126. * @reg: Registrar data from wps_registrar_init()
  2127. * @msg: Received message from SetSelectedRegistrar
  2128. * Returns: 0 on success, -1 on failure
  2129. *
  2130. * This function is called when an AP receives a SetSelectedRegistrar UPnP
  2131. * message.
  2132. */
  2133. int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
  2134. const struct wpabuf *msg)
  2135. {
  2136. struct wps_parse_attr attr;
  2137. wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
  2138. msg);
  2139. if (wps_parse_msg(msg, &attr) < 0)
  2140. return -1;
  2141. if (!wps_version_supported(attr.version)) {
  2142. wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
  2143. "version 0x%x", attr.version ? *attr.version : 0);
  2144. return -1;
  2145. }
  2146. if (attr.selected_registrar == NULL ||
  2147. *attr.selected_registrar == 0) {
  2148. wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
  2149. "Selected Registrar");
  2150. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
  2151. NULL);
  2152. wps_registrar_set_selected_timeout(reg, NULL);
  2153. return 0;
  2154. }
  2155. reg->selected_registrar = 1;
  2156. reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
  2157. WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
  2158. reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
  2159. WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
  2160. wps_set_ie(reg);
  2161. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  2162. eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
  2163. wps_registrar_set_selected_timeout,
  2164. reg, NULL);
  2165. return 0;
  2166. }