wps_registrar.c 97 KB

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