wps_registrar.c 65 KB

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