wps_registrar.c 88 KB

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