wps_registrar.c 54 KB

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