wps_registrar.c 81 KB

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