wps_registrar.c 74 KB

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