wps_registrar.c 51 KB

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