wps_registrar.c 82 KB

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