wps_registrar.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644
  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. #ifdef CONFIG_WPS2
  448. if ((conf_methods & WPS_CONFIG_VIRT_PUSHBUTTON) ==
  449. WPS_CONFIG_VIRT_PUSHBUTTON)
  450. *methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
  451. if ((conf_methods & WPS_CONFIG_PHY_PUSHBUTTON) ==
  452. WPS_CONFIG_PHY_PUSHBUTTON)
  453. *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
  454. if ((*methods & WPS_CONFIG_VIRT_PUSHBUTTON) !=
  455. WPS_CONFIG_VIRT_PUSHBUTTON &&
  456. (*methods & WPS_CONFIG_PHY_PUSHBUTTON) !=
  457. WPS_CONFIG_PHY_PUSHBUTTON) {
  458. /*
  459. * Required to include virtual/physical flag, but we were not
  460. * configured with push button type, so have to default to one
  461. * of them.
  462. */
  463. *methods |= WPS_CONFIG_PHY_PUSHBUTTON;
  464. }
  465. #endif /* CONFIG_WPS2 */
  466. }
  467. static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
  468. struct wpabuf *msg)
  469. {
  470. u16 methods;
  471. if (!reg->sel_reg_union)
  472. return 0;
  473. methods = reg->wps->config_methods;
  474. methods &= ~WPS_CONFIG_PUSHBUTTON;
  475. #ifdef CONFIG_WPS2
  476. methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
  477. WPS_CONFIG_PHY_PUSHBUTTON);
  478. #endif /* CONFIG_WPS2 */
  479. if (reg->pbc)
  480. wps_set_pushbutton(&methods, reg->wps->config_methods);
  481. if (reg->sel_reg_config_methods_override >= 0)
  482. methods = reg->sel_reg_config_methods_override;
  483. wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
  484. methods);
  485. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
  486. wpabuf_put_be16(msg, 2);
  487. wpabuf_put_be16(msg, methods);
  488. return 0;
  489. }
  490. static int wps_build_probe_config_methods(struct wps_registrar *reg,
  491. struct wpabuf *msg)
  492. {
  493. u16 methods;
  494. /*
  495. * These are the methods that the AP supports as an Enrollee for adding
  496. * external Registrars.
  497. */
  498. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  499. #ifdef CONFIG_WPS2
  500. methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
  501. WPS_CONFIG_PHY_PUSHBUTTON);
  502. #endif /* CONFIG_WPS2 */
  503. wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
  504. wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
  505. wpabuf_put_be16(msg, 2);
  506. wpabuf_put_be16(msg, methods);
  507. return 0;
  508. }
  509. static int wps_build_config_methods_r(struct wps_registrar *reg,
  510. struct wpabuf *msg)
  511. {
  512. return wps_build_config_methods(msg, reg->wps->config_methods);
  513. }
  514. const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count)
  515. {
  516. *count = 0;
  517. #ifdef CONFIG_WPS2
  518. while (*count < WPS_MAX_AUTHORIZED_MACS) {
  519. if (is_zero_ether_addr(reg->authorized_macs_union[*count]))
  520. break;
  521. (*count)++;
  522. }
  523. #endif /* CONFIG_WPS2 */
  524. return (const u8 *) reg->authorized_macs_union;
  525. }
  526. /**
  527. * wps_registrar_init - Initialize WPS Registrar data
  528. * @wps: Pointer to longterm WPS context
  529. * @cfg: Registrar configuration
  530. * Returns: Pointer to allocated Registrar data or %NULL on failure
  531. *
  532. * This function is used to initialize WPS Registrar functionality. It can be
  533. * used for a single Registrar run (e.g., when run in a supplicant) or multiple
  534. * runs (e.g., when run as an internal Registrar in an AP). Caller is
  535. * responsible for freeing the returned data with wps_registrar_deinit() when
  536. * Registrar functionality is not needed anymore.
  537. */
  538. struct wps_registrar *
  539. wps_registrar_init(struct wps_context *wps,
  540. const struct wps_registrar_config *cfg)
  541. {
  542. struct wps_registrar *reg = os_zalloc(sizeof(*reg));
  543. if (reg == NULL)
  544. return NULL;
  545. dl_list_init(&reg->pins);
  546. dl_list_init(&reg->nfc_pw_tokens);
  547. reg->wps = wps;
  548. reg->new_psk_cb = cfg->new_psk_cb;
  549. reg->set_ie_cb = cfg->set_ie_cb;
  550. reg->pin_needed_cb = cfg->pin_needed_cb;
  551. reg->reg_success_cb = cfg->reg_success_cb;
  552. reg->set_sel_reg_cb = cfg->set_sel_reg_cb;
  553. reg->enrollee_seen_cb = cfg->enrollee_seen_cb;
  554. reg->cb_ctx = cfg->cb_ctx;
  555. reg->skip_cred_build = cfg->skip_cred_build;
  556. if (cfg->extra_cred) {
  557. reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
  558. cfg->extra_cred_len);
  559. if (reg->extra_cred == NULL) {
  560. os_free(reg);
  561. return NULL;
  562. }
  563. }
  564. reg->disable_auto_conf = cfg->disable_auto_conf;
  565. reg->sel_reg_dev_password_id_override = -1;
  566. reg->sel_reg_config_methods_override = -1;
  567. reg->static_wep_only = cfg->static_wep_only;
  568. reg->dualband = cfg->dualband;
  569. reg->force_per_enrollee_psk = cfg->force_per_enrollee_psk;
  570. if (wps_set_ie(reg)) {
  571. wps_registrar_deinit(reg);
  572. return NULL;
  573. }
  574. return reg;
  575. }
  576. /**
  577. * wps_registrar_deinit - Deinitialize WPS Registrar data
  578. * @reg: Registrar data from wps_registrar_init()
  579. */
  580. void wps_registrar_deinit(struct wps_registrar *reg)
  581. {
  582. if (reg == NULL)
  583. return;
  584. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  585. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  586. wps_free_pins(&reg->pins);
  587. wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, 0);
  588. wps_free_pbc_sessions(reg->pbc_sessions);
  589. wpabuf_free(reg->extra_cred);
  590. wps_free_devices(reg->devices);
  591. os_free(reg);
  592. }
  593. static void wps_registrar_invalidate_unused(struct wps_registrar *reg)
  594. {
  595. struct wps_uuid_pin *pin;
  596. dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
  597. if (pin->wildcard_uuid == 1 && !(pin->flags & PIN_LOCKED)) {
  598. wpa_printf(MSG_DEBUG, "WPS: Invalidate previously "
  599. "configured wildcard PIN");
  600. wps_registrar_remove_pin(reg, pin);
  601. break;
  602. }
  603. }
  604. }
  605. /**
  606. * wps_registrar_add_pin - Configure a new PIN for Registrar
  607. * @reg: Registrar data from wps_registrar_init()
  608. * @addr: Enrollee MAC address or %NULL if not known
  609. * @uuid: UUID-E or %NULL for wildcard (any UUID)
  610. * @pin: PIN (Device Password)
  611. * @pin_len: Length of pin in octets
  612. * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
  613. * Returns: 0 on success, -1 on failure
  614. */
  615. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *addr,
  616. const u8 *uuid, const u8 *pin, size_t pin_len,
  617. int timeout)
  618. {
  619. struct wps_uuid_pin *p;
  620. p = os_zalloc(sizeof(*p));
  621. if (p == NULL)
  622. return -1;
  623. if (addr)
  624. os_memcpy(p->enrollee_addr, addr, ETH_ALEN);
  625. if (uuid == NULL)
  626. p->wildcard_uuid = 1;
  627. else
  628. os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
  629. p->pin = os_malloc(pin_len);
  630. if (p->pin == NULL) {
  631. os_free(p);
  632. return -1;
  633. }
  634. os_memcpy(p->pin, pin, pin_len);
  635. p->pin_len = pin_len;
  636. if (timeout) {
  637. p->flags |= PIN_EXPIRES;
  638. os_get_reltime(&p->expiration);
  639. p->expiration.sec += timeout;
  640. }
  641. if (p->wildcard_uuid)
  642. wps_registrar_invalidate_unused(reg);
  643. dl_list_add(&reg->pins, &p->list);
  644. wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
  645. timeout);
  646. wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
  647. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
  648. reg->selected_registrar = 1;
  649. reg->pbc = 0;
  650. if (addr)
  651. wps_registrar_add_authorized_mac(reg, addr);
  652. else
  653. wps_registrar_add_authorized_mac(
  654. reg, (u8 *) "\xff\xff\xff\xff\xff\xff");
  655. wps_registrar_selected_registrar_changed(reg, 0);
  656. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  657. eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
  658. wps_registrar_set_selected_timeout,
  659. reg, NULL);
  660. return 0;
  661. }
  662. static void wps_registrar_remove_pin(struct wps_registrar *reg,
  663. struct wps_uuid_pin *pin)
  664. {
  665. u8 *addr;
  666. u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  667. if (is_zero_ether_addr(pin->enrollee_addr))
  668. addr = bcast;
  669. else
  670. addr = pin->enrollee_addr;
  671. wps_registrar_remove_authorized_mac(reg, addr);
  672. wps_remove_pin(pin);
  673. wps_registrar_selected_registrar_changed(reg, 0);
  674. }
  675. static void wps_registrar_expire_pins(struct wps_registrar *reg)
  676. {
  677. struct wps_uuid_pin *pin, *prev;
  678. struct os_reltime now;
  679. os_get_reltime(&now);
  680. dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
  681. {
  682. if ((pin->flags & PIN_EXPIRES) &&
  683. os_reltime_before(&pin->expiration, &now)) {
  684. wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
  685. pin->uuid, WPS_UUID_LEN);
  686. wps_registrar_remove_pin(reg, pin);
  687. }
  688. }
  689. }
  690. /**
  691. * wps_registrar_invalidate_wildcard_pin - Invalidate a wildcard PIN
  692. * @reg: Registrar data from wps_registrar_init()
  693. * @dev_pw: PIN to search for or %NULL to match any
  694. * @dev_pw_len: Length of dev_pw in octets
  695. * Returns: 0 on success, -1 if not wildcard PIN is enabled
  696. */
  697. static int wps_registrar_invalidate_wildcard_pin(struct wps_registrar *reg,
  698. const u8 *dev_pw,
  699. size_t dev_pw_len)
  700. {
  701. struct wps_uuid_pin *pin, *prev;
  702. dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
  703. {
  704. if (dev_pw && pin->pin &&
  705. (dev_pw_len != pin->pin_len ||
  706. os_memcmp(dev_pw, pin->pin, dev_pw_len) != 0))
  707. continue; /* different PIN */
  708. if (pin->wildcard_uuid) {
  709. wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
  710. pin->uuid, WPS_UUID_LEN);
  711. wps_registrar_remove_pin(reg, pin);
  712. return 0;
  713. }
  714. }
  715. return -1;
  716. }
  717. /**
  718. * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
  719. * @reg: Registrar data from wps_registrar_init()
  720. * @uuid: UUID-E
  721. * Returns: 0 on success, -1 on failure (e.g., PIN not found)
  722. */
  723. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
  724. {
  725. struct wps_uuid_pin *pin, *prev;
  726. dl_list_for_each_safe(pin, prev, &reg->pins, struct wps_uuid_pin, list)
  727. {
  728. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  729. wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
  730. pin->uuid, WPS_UUID_LEN);
  731. wps_registrar_remove_pin(reg, pin);
  732. return 0;
  733. }
  734. }
  735. return -1;
  736. }
  737. static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
  738. const u8 *uuid, size_t *pin_len)
  739. {
  740. struct wps_uuid_pin *pin, *found = NULL;
  741. wps_registrar_expire_pins(reg);
  742. dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
  743. if (!pin->wildcard_uuid &&
  744. os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  745. found = pin;
  746. break;
  747. }
  748. }
  749. if (!found) {
  750. /* Check for wildcard UUIDs since none of the UUID-specific
  751. * PINs matched */
  752. dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
  753. if (pin->wildcard_uuid == 1 ||
  754. pin->wildcard_uuid == 2) {
  755. wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
  756. "PIN. Assigned it for this UUID-E");
  757. pin->wildcard_uuid++;
  758. os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
  759. found = pin;
  760. break;
  761. }
  762. }
  763. }
  764. if (!found)
  765. return NULL;
  766. /*
  767. * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
  768. * that could otherwise avoid PIN invalidations.
  769. */
  770. if (found->flags & PIN_LOCKED) {
  771. wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
  772. "allow concurrent re-use");
  773. return NULL;
  774. }
  775. *pin_len = found->pin_len;
  776. found->flags |= PIN_LOCKED;
  777. return found->pin;
  778. }
  779. /**
  780. * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
  781. * @reg: Registrar data from wps_registrar_init()
  782. * @uuid: UUID-E
  783. * Returns: 0 on success, -1 on failure
  784. *
  785. * PINs are locked to enforce only one concurrent use. This function unlocks a
  786. * PIN to allow it to be used again. If the specified PIN was configured using
  787. * a wildcard UUID, it will be removed instead of allowing multiple uses.
  788. */
  789. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
  790. {
  791. struct wps_uuid_pin *pin;
  792. dl_list_for_each(pin, &reg->pins, struct wps_uuid_pin, list) {
  793. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  794. if (pin->wildcard_uuid == 3) {
  795. wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
  796. "wildcard PIN");
  797. return wps_registrar_invalidate_pin(reg, uuid);
  798. }
  799. pin->flags &= ~PIN_LOCKED;
  800. return 0;
  801. }
  802. }
  803. return -1;
  804. }
  805. static void wps_registrar_stop_pbc(struct wps_registrar *reg)
  806. {
  807. reg->selected_registrar = 0;
  808. reg->pbc = 0;
  809. os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
  810. wps_registrar_remove_authorized_mac(reg,
  811. (u8 *) "\xff\xff\xff\xff\xff\xff");
  812. wps_registrar_selected_registrar_changed(reg, 0);
  813. }
  814. static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
  815. {
  816. struct wps_registrar *reg = eloop_ctx;
  817. wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
  818. wps_pbc_timeout_event(reg->wps);
  819. wps_registrar_stop_pbc(reg);
  820. }
  821. /**
  822. * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
  823. * @reg: Registrar data from wps_registrar_init()
  824. * @p2p_dev_addr: Limit allowed PBC devices to the specified P2P device, %NULL
  825. * indicates no such filtering
  826. * Returns: 0 on success, -1 on failure, -2 on session overlap
  827. *
  828. * This function is called on an AP when a push button is pushed to activate
  829. * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
  830. * or when a PBC registration is completed. If more than one Enrollee in active
  831. * PBC mode has been detected during the monitor time (previous 2 minutes), the
  832. * PBC mode is not activated and -2 is returned to indicate session overlap.
  833. * This is skipped if a specific Enrollee is selected.
  834. */
  835. int wps_registrar_button_pushed(struct wps_registrar *reg,
  836. const u8 *p2p_dev_addr)
  837. {
  838. if (p2p_dev_addr == NULL &&
  839. wps_registrar_pbc_overlap(reg, NULL, NULL)) {
  840. wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
  841. "mode");
  842. wps_pbc_overlap_event(reg->wps);
  843. return -2;
  844. }
  845. wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
  846. reg->force_pbc_overlap = 0;
  847. reg->selected_registrar = 1;
  848. reg->pbc = 1;
  849. if (p2p_dev_addr)
  850. os_memcpy(reg->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  851. else
  852. os_memset(reg->p2p_dev_addr, 0, ETH_ALEN);
  853. wps_registrar_add_authorized_mac(reg,
  854. (u8 *) "\xff\xff\xff\xff\xff\xff");
  855. wps_registrar_selected_registrar_changed(reg, 0);
  856. wps_pbc_active_event(reg->wps);
  857. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  858. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  859. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
  860. reg, NULL);
  861. return 0;
  862. }
  863. static void wps_registrar_pbc_completed(struct wps_registrar *reg)
  864. {
  865. wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
  866. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  867. wps_registrar_stop_pbc(reg);
  868. wps_pbc_disable_event(reg->wps);
  869. }
  870. static void wps_registrar_pin_completed(struct wps_registrar *reg)
  871. {
  872. wpa_printf(MSG_DEBUG, "WPS: PIN completed using internal Registrar");
  873. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  874. reg->selected_registrar = 0;
  875. wps_registrar_selected_registrar_changed(reg, 0);
  876. }
  877. void wps_registrar_complete(struct wps_registrar *registrar, const u8 *uuid_e,
  878. const u8 *dev_pw, size_t dev_pw_len)
  879. {
  880. if (registrar->pbc) {
  881. wps_registrar_remove_pbc_session(registrar,
  882. uuid_e, NULL);
  883. wps_registrar_pbc_completed(registrar);
  884. #ifdef WPS_WORKAROUNDS
  885. os_get_reltime(&registrar->pbc_ignore_start);
  886. #endif /* WPS_WORKAROUNDS */
  887. os_memcpy(registrar->pbc_ignore_uuid, uuid_e, WPS_UUID_LEN);
  888. } else {
  889. wps_registrar_pin_completed(registrar);
  890. }
  891. if (dev_pw &&
  892. wps_registrar_invalidate_wildcard_pin(registrar, dev_pw,
  893. dev_pw_len) == 0) {
  894. wpa_hexdump_key(MSG_DEBUG, "WPS: Invalidated wildcard PIN",
  895. dev_pw, dev_pw_len);
  896. }
  897. }
  898. int wps_registrar_wps_cancel(struct wps_registrar *reg)
  899. {
  900. if (reg->pbc) {
  901. wpa_printf(MSG_DEBUG, "WPS: PBC is set - cancelling it");
  902. wps_registrar_pbc_timeout(reg, NULL);
  903. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  904. return 1;
  905. } else if (reg->selected_registrar) {
  906. /* PIN Method */
  907. wpa_printf(MSG_DEBUG, "WPS: PIN is set - cancelling it");
  908. wps_registrar_pin_completed(reg);
  909. wps_registrar_invalidate_wildcard_pin(reg, NULL, 0);
  910. return 1;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
  916. * @reg: Registrar data from wps_registrar_init()
  917. * @addr: MAC address of the Probe Request sender
  918. * @wps_data: WPS IE contents
  919. *
  920. * This function is called on an AP when a Probe Request with WPS IE is
  921. * received. This is used to track PBC mode use and to detect possible overlap
  922. * situation with other WPS APs.
  923. */
  924. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  925. const struct wpabuf *wps_data,
  926. int p2p_wildcard)
  927. {
  928. struct wps_parse_attr attr;
  929. int skip_add = 0;
  930. wpa_hexdump_buf(MSG_MSGDUMP,
  931. "WPS: Probe Request with WPS data received",
  932. wps_data);
  933. if (wps_parse_msg(wps_data, &attr) < 0)
  934. return;
  935. if (attr.config_methods == NULL) {
  936. wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
  937. "Probe Request");
  938. return;
  939. }
  940. if (attr.dev_password_id == NULL) {
  941. wpa_printf(MSG_DEBUG, "WPS: No Device Password Id attribute "
  942. "in Probe Request");
  943. return;
  944. }
  945. if (reg->enrollee_seen_cb && attr.uuid_e &&
  946. attr.primary_dev_type && attr.request_type && !p2p_wildcard) {
  947. char *dev_name = NULL;
  948. if (attr.dev_name) {
  949. dev_name = os_zalloc(attr.dev_name_len + 1);
  950. if (dev_name) {
  951. os_memcpy(dev_name, attr.dev_name,
  952. attr.dev_name_len);
  953. }
  954. }
  955. reg->enrollee_seen_cb(reg->cb_ctx, addr, attr.uuid_e,
  956. attr.primary_dev_type,
  957. WPA_GET_BE16(attr.config_methods),
  958. WPA_GET_BE16(attr.dev_password_id),
  959. *attr.request_type, dev_name);
  960. os_free(dev_name);
  961. }
  962. if (WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
  963. return; /* Not PBC */
  964. wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
  965. MACSTR, MAC2STR(addr));
  966. if (attr.uuid_e == NULL) {
  967. wpa_printf(MSG_DEBUG, "WPS: Invalid Probe Request WPS IE: No "
  968. "UUID-E included");
  969. return;
  970. }
  971. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E from Probe Request", attr.uuid_e,
  972. WPS_UUID_LEN);
  973. #ifdef WPS_WORKAROUNDS
  974. if (reg->pbc_ignore_start.sec &&
  975. os_memcmp(attr.uuid_e, reg->pbc_ignore_uuid, WPS_UUID_LEN) == 0) {
  976. struct os_reltime now, dur;
  977. os_get_reltime(&now);
  978. os_reltime_sub(&now, &reg->pbc_ignore_start, &dur);
  979. if (dur.sec >= 0 && dur.sec < 5) {
  980. wpa_printf(MSG_DEBUG, "WPS: Ignore PBC activation "
  981. "based on Probe Request from the Enrollee "
  982. "that just completed PBC provisioning");
  983. skip_add = 1;
  984. } else
  985. reg->pbc_ignore_start.sec = 0;
  986. }
  987. #endif /* WPS_WORKAROUNDS */
  988. if (!skip_add)
  989. wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
  990. if (wps_registrar_pbc_overlap(reg, addr, attr.uuid_e)) {
  991. wpa_printf(MSG_DEBUG, "WPS: PBC session overlap detected");
  992. reg->force_pbc_overlap = 1;
  993. wps_pbc_overlap_event(reg->wps);
  994. }
  995. }
  996. static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
  997. const u8 *p2p_dev_addr, const u8 *psk, size_t psk_len)
  998. {
  999. if (reg->new_psk_cb == NULL)
  1000. return 0;
  1001. return reg->new_psk_cb(reg->cb_ctx, mac_addr, p2p_dev_addr, psk,
  1002. psk_len);
  1003. }
  1004. static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
  1005. const struct wps_device_data *dev)
  1006. {
  1007. if (reg->pin_needed_cb == NULL)
  1008. return;
  1009. reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
  1010. }
  1011. static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
  1012. const u8 *uuid_e, const u8 *dev_pw,
  1013. size_t dev_pw_len)
  1014. {
  1015. if (reg->reg_success_cb == NULL)
  1016. return;
  1017. reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e, dev_pw, dev_pw_len);
  1018. }
  1019. static int wps_cb_set_ie(struct wps_registrar *reg, struct wpabuf *beacon_ie,
  1020. struct wpabuf *probe_resp_ie)
  1021. {
  1022. return reg->set_ie_cb(reg->cb_ctx, beacon_ie, probe_resp_ie);
  1023. }
  1024. static void wps_cb_set_sel_reg(struct wps_registrar *reg)
  1025. {
  1026. u16 methods = 0;
  1027. if (reg->set_sel_reg_cb == NULL)
  1028. return;
  1029. if (reg->selected_registrar) {
  1030. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  1031. #ifdef CONFIG_WPS2
  1032. methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
  1033. WPS_CONFIG_PHY_PUSHBUTTON);
  1034. #endif /* CONFIG_WPS2 */
  1035. if (reg->pbc)
  1036. wps_set_pushbutton(&methods, reg->wps->config_methods);
  1037. }
  1038. wpa_printf(MSG_DEBUG, "WPS: wps_cb_set_sel_reg: sel_reg=%d "
  1039. "config_methods=0x%x pbc=%d methods=0x%x",
  1040. reg->selected_registrar, reg->wps->config_methods,
  1041. reg->pbc, methods);
  1042. reg->set_sel_reg_cb(reg->cb_ctx, reg->selected_registrar,
  1043. reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT,
  1044. methods);
  1045. }
  1046. static int wps_set_ie(struct wps_registrar *reg)
  1047. {
  1048. struct wpabuf *beacon;
  1049. struct wpabuf *probe;
  1050. const u8 *auth_macs;
  1051. size_t count;
  1052. size_t vendor_len = 0;
  1053. int i;
  1054. if (reg->set_ie_cb == NULL)
  1055. return 0;
  1056. for (i = 0; i < MAX_WPS_VENDOR_EXTENSIONS; i++) {
  1057. if (reg->wps->dev.vendor_ext[i]) {
  1058. vendor_len += 2 + 2;
  1059. vendor_len += wpabuf_len(reg->wps->dev.vendor_ext[i]);
  1060. }
  1061. }
  1062. beacon = wpabuf_alloc(400 + vendor_len);
  1063. if (beacon == NULL)
  1064. return -1;
  1065. probe = wpabuf_alloc(500 + vendor_len);
  1066. if (probe == NULL) {
  1067. wpabuf_free(beacon);
  1068. return -1;
  1069. }
  1070. auth_macs = wps_authorized_macs(reg, &count);
  1071. wpa_printf(MSG_DEBUG, "WPS: Build Beacon IEs");
  1072. if (wps_build_version(beacon) ||
  1073. wps_build_wps_state(reg->wps, beacon) ||
  1074. wps_build_ap_setup_locked(reg->wps, beacon) ||
  1075. wps_build_selected_registrar(reg, beacon) ||
  1076. wps_build_sel_reg_dev_password_id(reg, beacon) ||
  1077. wps_build_sel_reg_config_methods(reg, beacon) ||
  1078. wps_build_sel_pbc_reg_uuid_e(reg, beacon) ||
  1079. (reg->dualband && wps_build_rf_bands(&reg->wps->dev, beacon, 0)) ||
  1080. wps_build_wfa_ext(beacon, 0, auth_macs, count) ||
  1081. wps_build_vendor_ext(&reg->wps->dev, beacon)) {
  1082. wpabuf_free(beacon);
  1083. wpabuf_free(probe);
  1084. return -1;
  1085. }
  1086. #ifdef CONFIG_P2P
  1087. if (wps_build_dev_name(&reg->wps->dev, beacon) ||
  1088. wps_build_primary_dev_type(&reg->wps->dev, beacon)) {
  1089. wpabuf_free(beacon);
  1090. wpabuf_free(probe);
  1091. return -1;
  1092. }
  1093. #endif /* CONFIG_P2P */
  1094. wpa_printf(MSG_DEBUG, "WPS: Build Probe Response IEs");
  1095. if (wps_build_version(probe) ||
  1096. wps_build_wps_state(reg->wps, probe) ||
  1097. wps_build_ap_setup_locked(reg->wps, probe) ||
  1098. wps_build_selected_registrar(reg, probe) ||
  1099. wps_build_sel_reg_dev_password_id(reg, probe) ||
  1100. wps_build_sel_reg_config_methods(reg, probe) ||
  1101. wps_build_resp_type(probe, reg->wps->ap ? WPS_RESP_AP :
  1102. WPS_RESP_REGISTRAR) ||
  1103. wps_build_uuid_e(probe, reg->wps->uuid) ||
  1104. wps_build_device_attrs(&reg->wps->dev, probe) ||
  1105. wps_build_probe_config_methods(reg, probe) ||
  1106. (reg->dualband && wps_build_rf_bands(&reg->wps->dev, probe, 0)) ||
  1107. wps_build_wfa_ext(probe, 0, auth_macs, count) ||
  1108. wps_build_vendor_ext(&reg->wps->dev, probe)) {
  1109. wpabuf_free(beacon);
  1110. wpabuf_free(probe);
  1111. return -1;
  1112. }
  1113. beacon = wps_ie_encapsulate(beacon);
  1114. probe = wps_ie_encapsulate(probe);
  1115. if (!beacon || !probe) {
  1116. wpabuf_free(beacon);
  1117. wpabuf_free(probe);
  1118. return -1;
  1119. }
  1120. if (reg->static_wep_only) {
  1121. /*
  1122. * Windows XP and Vista clients can get confused about
  1123. * EAP-Identity/Request when they probe the network with
  1124. * EAPOL-Start. In such a case, they may assume the network is
  1125. * using IEEE 802.1X and prompt user for a certificate while
  1126. * the correct (non-WPS) behavior would be to ask for the
  1127. * static WEP key. As a workaround, use Microsoft Provisioning
  1128. * IE to advertise that legacy 802.1X is not supported.
  1129. */
  1130. const u8 ms_wps[7] = {
  1131. WLAN_EID_VENDOR_SPECIFIC, 5,
  1132. /* Microsoft Provisioning IE (00:50:f2:5) */
  1133. 0x00, 0x50, 0xf2, 5,
  1134. 0x00 /* no legacy 802.1X or MS WPS */
  1135. };
  1136. wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
  1137. "into Beacon/Probe Response frames");
  1138. wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
  1139. wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
  1140. }
  1141. return wps_cb_set_ie(reg, beacon, probe);
  1142. }
  1143. static int wps_get_dev_password(struct wps_data *wps)
  1144. {
  1145. const u8 *pin;
  1146. size_t pin_len = 0;
  1147. os_free(wps->dev_password);
  1148. wps->dev_password = NULL;
  1149. if (wps->pbc) {
  1150. wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
  1151. pin = (const u8 *) "00000000";
  1152. pin_len = 8;
  1153. #ifdef CONFIG_WPS_NFC
  1154. } else if (wps->nfc_pw_token) {
  1155. if (wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
  1156. {
  1157. wpa_printf(MSG_DEBUG, "WPS: Using NFC connection "
  1158. "handover and abbreviated WPS handshake "
  1159. "without Device Password");
  1160. return 0;
  1161. }
  1162. wpa_printf(MSG_DEBUG, "WPS: Use OOB Device Password from NFC "
  1163. "Password Token");
  1164. pin = wps->nfc_pw_token->dev_pw;
  1165. pin_len = wps->nfc_pw_token->dev_pw_len;
  1166. #endif /* CONFIG_WPS_NFC */
  1167. } else {
  1168. pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
  1169. &pin_len);
  1170. if (pin && wps->dev_pw_id >= 0x10) {
  1171. wpa_printf(MSG_DEBUG, "WPS: No match for OOB Device "
  1172. "Password ID, but PIN found");
  1173. /*
  1174. * See whether Enrollee is willing to use PIN instead.
  1175. */
  1176. wps->dev_pw_id = DEV_PW_DEFAULT;
  1177. }
  1178. }
  1179. if (pin == NULL) {
  1180. wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
  1181. "the Enrollee (context %p registrar %p)",
  1182. wps->wps, wps->wps->registrar);
  1183. wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
  1184. &wps->peer_dev);
  1185. return -1;
  1186. }
  1187. wps->dev_password = os_malloc(pin_len);
  1188. if (wps->dev_password == NULL)
  1189. return -1;
  1190. os_memcpy(wps->dev_password, pin, pin_len);
  1191. wps->dev_password_len = pin_len;
  1192. return 0;
  1193. }
  1194. static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
  1195. {
  1196. wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
  1197. wpabuf_put_be16(msg, ATTR_UUID_R);
  1198. wpabuf_put_be16(msg, WPS_UUID_LEN);
  1199. wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
  1200. return 0;
  1201. }
  1202. static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
  1203. {
  1204. u8 *hash;
  1205. const u8 *addr[4];
  1206. size_t len[4];
  1207. if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
  1208. return -1;
  1209. wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
  1210. wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
  1211. wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
  1212. if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
  1213. wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
  1214. "R-Hash derivation");
  1215. return -1;
  1216. }
  1217. wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
  1218. wpabuf_put_be16(msg, ATTR_R_HASH1);
  1219. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  1220. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  1221. /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
  1222. addr[0] = wps->snonce;
  1223. len[0] = WPS_SECRET_NONCE_LEN;
  1224. addr[1] = wps->psk1;
  1225. len[1] = WPS_PSK_LEN;
  1226. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1227. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1228. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1229. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1230. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1231. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
  1232. wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
  1233. wpabuf_put_be16(msg, ATTR_R_HASH2);
  1234. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  1235. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  1236. /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
  1237. addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
  1238. addr[1] = wps->psk2;
  1239. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1240. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
  1241. return 0;
  1242. }
  1243. static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
  1244. {
  1245. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
  1246. wpabuf_put_be16(msg, ATTR_R_SNONCE1);
  1247. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  1248. wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
  1249. return 0;
  1250. }
  1251. static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
  1252. {
  1253. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
  1254. wpabuf_put_be16(msg, ATTR_R_SNONCE2);
  1255. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  1256. wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
  1257. WPS_SECRET_NONCE_LEN);
  1258. return 0;
  1259. }
  1260. static int wps_build_cred_network_idx(struct wpabuf *msg,
  1261. const struct wps_credential *cred)
  1262. {
  1263. wpa_printf(MSG_DEBUG, "WPS: * Network Index (1)");
  1264. wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
  1265. wpabuf_put_be16(msg, 1);
  1266. wpabuf_put_u8(msg, 1);
  1267. return 0;
  1268. }
  1269. static int wps_build_cred_ssid(struct wpabuf *msg,
  1270. const struct wps_credential *cred)
  1271. {
  1272. wpa_printf(MSG_DEBUG, "WPS: * SSID");
  1273. wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID for Credential",
  1274. cred->ssid, cred->ssid_len);
  1275. wpabuf_put_be16(msg, ATTR_SSID);
  1276. wpabuf_put_be16(msg, cred->ssid_len);
  1277. wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
  1278. return 0;
  1279. }
  1280. static int wps_build_cred_auth_type(struct wpabuf *msg,
  1281. const struct wps_credential *cred)
  1282. {
  1283. wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
  1284. cred->auth_type);
  1285. wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
  1286. wpabuf_put_be16(msg, 2);
  1287. wpabuf_put_be16(msg, cred->auth_type);
  1288. return 0;
  1289. }
  1290. static int wps_build_cred_encr_type(struct wpabuf *msg,
  1291. const struct wps_credential *cred)
  1292. {
  1293. wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
  1294. cred->encr_type);
  1295. wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
  1296. wpabuf_put_be16(msg, 2);
  1297. wpabuf_put_be16(msg, cred->encr_type);
  1298. return 0;
  1299. }
  1300. static int wps_build_cred_network_key(struct wpabuf *msg,
  1301. const struct wps_credential *cred)
  1302. {
  1303. wpa_printf(MSG_DEBUG, "WPS: * Network Key (len=%d)",
  1304. (int) cred->key_len);
  1305. wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
  1306. cred->key, cred->key_len);
  1307. wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
  1308. wpabuf_put_be16(msg, cred->key_len);
  1309. wpabuf_put_data(msg, cred->key, cred->key_len);
  1310. return 0;
  1311. }
  1312. static int wps_build_credential(struct wpabuf *msg,
  1313. const struct wps_credential *cred)
  1314. {
  1315. if (wps_build_cred_network_idx(msg, cred) ||
  1316. wps_build_cred_ssid(msg, cred) ||
  1317. wps_build_cred_auth_type(msg, cred) ||
  1318. wps_build_cred_encr_type(msg, cred) ||
  1319. wps_build_cred_network_key(msg, cred) ||
  1320. wps_build_mac_addr(msg, cred->mac_addr))
  1321. return -1;
  1322. return 0;
  1323. }
  1324. int wps_build_credential_wrap(struct wpabuf *msg,
  1325. const struct wps_credential *cred)
  1326. {
  1327. struct wpabuf *wbuf;
  1328. wbuf = wpabuf_alloc(200);
  1329. if (wbuf == NULL)
  1330. return -1;
  1331. if (wps_build_credential(wbuf, cred)) {
  1332. wpabuf_free(wbuf);
  1333. return -1;
  1334. }
  1335. wpabuf_put_be16(msg, ATTR_CRED);
  1336. wpabuf_put_be16(msg, wpabuf_len(wbuf));
  1337. wpabuf_put_buf(msg, wbuf);
  1338. wpabuf_free(wbuf);
  1339. return 0;
  1340. }
  1341. int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
  1342. {
  1343. struct wpabuf *cred;
  1344. if (wps->wps->registrar->skip_cred_build)
  1345. goto skip_cred_build;
  1346. wpa_printf(MSG_DEBUG, "WPS: * Credential");
  1347. if (wps->use_cred) {
  1348. os_memcpy(&wps->cred, wps->use_cred, sizeof(wps->cred));
  1349. goto use_provided;
  1350. }
  1351. os_memset(&wps->cred, 0, sizeof(wps->cred));
  1352. os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
  1353. wps->cred.ssid_len = wps->wps->ssid_len;
  1354. /* Select the best authentication and encryption type */
  1355. if (wps->auth_type & WPS_AUTH_WPA2PSK)
  1356. wps->auth_type = WPS_AUTH_WPA2PSK;
  1357. else if (wps->auth_type & WPS_AUTH_WPAPSK)
  1358. wps->auth_type = WPS_AUTH_WPAPSK;
  1359. else if (wps->auth_type & WPS_AUTH_OPEN)
  1360. wps->auth_type = WPS_AUTH_OPEN;
  1361. else if (wps->auth_type & WPS_AUTH_SHARED)
  1362. wps->auth_type = WPS_AUTH_SHARED;
  1363. else {
  1364. wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
  1365. wps->auth_type);
  1366. return -1;
  1367. }
  1368. wps->cred.auth_type = wps->auth_type;
  1369. if (wps->auth_type == WPS_AUTH_WPA2PSK ||
  1370. wps->auth_type == WPS_AUTH_WPAPSK) {
  1371. if (wps->encr_type & WPS_ENCR_AES)
  1372. wps->encr_type = WPS_ENCR_AES;
  1373. else if (wps->encr_type & WPS_ENCR_TKIP)
  1374. wps->encr_type = WPS_ENCR_TKIP;
  1375. else {
  1376. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  1377. "type for WPA/WPA2");
  1378. return -1;
  1379. }
  1380. } else {
  1381. if (wps->encr_type & WPS_ENCR_WEP)
  1382. wps->encr_type = WPS_ENCR_WEP;
  1383. else if (wps->encr_type & WPS_ENCR_NONE)
  1384. wps->encr_type = WPS_ENCR_NONE;
  1385. else {
  1386. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  1387. "type for non-WPA/WPA2 mode");
  1388. return -1;
  1389. }
  1390. }
  1391. wps->cred.encr_type = wps->encr_type;
  1392. /*
  1393. * Set MAC address in the Credential to be the Enrollee's MAC address
  1394. */
  1395. os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
  1396. if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
  1397. !wps->wps->registrar->disable_auto_conf) {
  1398. u8 r[16];
  1399. /* Generate a random passphrase */
  1400. if (random_get_bytes(r, sizeof(r)) < 0)
  1401. return -1;
  1402. os_free(wps->new_psk);
  1403. wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
  1404. if (wps->new_psk == NULL)
  1405. return -1;
  1406. wps->new_psk_len--; /* remove newline */
  1407. while (wps->new_psk_len &&
  1408. wps->new_psk[wps->new_psk_len - 1] == '=')
  1409. wps->new_psk_len--;
  1410. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
  1411. wps->new_psk, wps->new_psk_len);
  1412. os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
  1413. wps->cred.key_len = wps->new_psk_len;
  1414. } else if (!wps->wps->registrar->force_per_enrollee_psk &&
  1415. wps->use_psk_key && wps->wps->psk_set) {
  1416. char hex[65];
  1417. wpa_printf(MSG_DEBUG, "WPS: Use PSK format for Network Key");
  1418. wpa_snprintf_hex(hex, sizeof(hex), wps->wps->psk, 32);
  1419. os_memcpy(wps->cred.key, hex, 32 * 2);
  1420. wps->cred.key_len = 32 * 2;
  1421. } else if (!wps->wps->registrar->force_per_enrollee_psk &&
  1422. wps->wps->network_key) {
  1423. os_memcpy(wps->cred.key, wps->wps->network_key,
  1424. wps->wps->network_key_len);
  1425. wps->cred.key_len = wps->wps->network_key_len;
  1426. } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  1427. char hex[65];
  1428. /* Generate a random per-device PSK */
  1429. os_free(wps->new_psk);
  1430. wps->new_psk_len = 32;
  1431. wps->new_psk = os_malloc(wps->new_psk_len);
  1432. if (wps->new_psk == NULL)
  1433. return -1;
  1434. if (random_get_bytes(wps->new_psk, wps->new_psk_len) < 0) {
  1435. os_free(wps->new_psk);
  1436. wps->new_psk = NULL;
  1437. return -1;
  1438. }
  1439. wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
  1440. wps->new_psk, wps->new_psk_len);
  1441. wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
  1442. wps->new_psk_len);
  1443. os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
  1444. wps->cred.key_len = wps->new_psk_len * 2;
  1445. }
  1446. use_provided:
  1447. #ifdef CONFIG_WPS_TESTING
  1448. if (wps_testing_dummy_cred)
  1449. cred = wpabuf_alloc(200);
  1450. else
  1451. cred = NULL;
  1452. if (cred) {
  1453. struct wps_credential dummy;
  1454. wpa_printf(MSG_DEBUG, "WPS: Add dummy credential");
  1455. os_memset(&dummy, 0, sizeof(dummy));
  1456. os_memcpy(dummy.ssid, "dummy", 5);
  1457. dummy.ssid_len = 5;
  1458. dummy.auth_type = WPS_AUTH_WPA2PSK;
  1459. dummy.encr_type = WPS_ENCR_AES;
  1460. os_memcpy(dummy.key, "dummy psk", 9);
  1461. dummy.key_len = 9;
  1462. os_memcpy(dummy.mac_addr, wps->mac_addr_e, ETH_ALEN);
  1463. wps_build_credential(cred, &dummy);
  1464. wpa_hexdump_buf(MSG_DEBUG, "WPS: Dummy Credential", cred);
  1465. wpabuf_put_be16(msg, ATTR_CRED);
  1466. wpabuf_put_be16(msg, wpabuf_len(cred));
  1467. wpabuf_put_buf(msg, cred);
  1468. wpabuf_free(cred);
  1469. }
  1470. #endif /* CONFIG_WPS_TESTING */
  1471. cred = wpabuf_alloc(200);
  1472. if (cred == NULL)
  1473. return -1;
  1474. if (wps_build_credential(cred, &wps->cred)) {
  1475. wpabuf_free(cred);
  1476. return -1;
  1477. }
  1478. wpabuf_put_be16(msg, ATTR_CRED);
  1479. wpabuf_put_be16(msg, wpabuf_len(cred));
  1480. wpabuf_put_buf(msg, cred);
  1481. wpabuf_free(cred);
  1482. skip_cred_build:
  1483. if (wps->wps->registrar->extra_cred) {
  1484. wpa_printf(MSG_DEBUG, "WPS: * Credential (pre-configured)");
  1485. wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
  1486. }
  1487. return 0;
  1488. }
  1489. static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
  1490. {
  1491. wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
  1492. if (wps_build_credential(msg, &wps->cred))
  1493. return -1;
  1494. return 0;
  1495. }
  1496. static struct wpabuf * wps_build_ap_cred(struct wps_data *wps)
  1497. {
  1498. struct wpabuf *msg, *plain;
  1499. msg = wpabuf_alloc(1000);
  1500. if (msg == NULL)
  1501. return NULL;
  1502. plain = wpabuf_alloc(200);
  1503. if (plain == NULL) {
  1504. wpabuf_free(msg);
  1505. return NULL;
  1506. }
  1507. if (wps_build_ap_settings(wps, plain)) {
  1508. wpabuf_free(plain);
  1509. wpabuf_free(msg);
  1510. return NULL;
  1511. }
  1512. wpabuf_put_be16(msg, ATTR_CRED);
  1513. wpabuf_put_be16(msg, wpabuf_len(plain));
  1514. wpabuf_put_buf(msg, plain);
  1515. wpabuf_free(plain);
  1516. return msg;
  1517. }
  1518. static struct wpabuf * wps_build_m2(struct wps_data *wps)
  1519. {
  1520. struct wpabuf *msg;
  1521. int config_in_m2 = 0;
  1522. if (random_get_bytes(wps->nonce_r, WPS_NONCE_LEN) < 0)
  1523. return NULL;
  1524. wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
  1525. wps->nonce_r, WPS_NONCE_LEN);
  1526. wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
  1527. wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
  1528. msg = wpabuf_alloc(1000);
  1529. if (msg == NULL)
  1530. return NULL;
  1531. if (wps_build_version(msg) ||
  1532. wps_build_msg_type(msg, WPS_M2) ||
  1533. wps_build_enrollee_nonce(wps, msg) ||
  1534. wps_build_registrar_nonce(wps, msg) ||
  1535. wps_build_uuid_r(wps, msg) ||
  1536. wps_build_public_key(wps, msg) ||
  1537. wps_derive_keys(wps) ||
  1538. wps_build_auth_type_flags(wps, msg) ||
  1539. wps_build_encr_type_flags(wps, msg) ||
  1540. wps_build_conn_type_flags(wps, msg) ||
  1541. wps_build_config_methods_r(wps->wps->registrar, msg) ||
  1542. wps_build_device_attrs(&wps->wps->dev, msg) ||
  1543. wps_build_rf_bands(&wps->wps->dev, msg,
  1544. wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
  1545. wps_build_assoc_state(wps, msg) ||
  1546. wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  1547. wps_build_dev_password_id(msg, wps->dev_pw_id) ||
  1548. wps_build_os_version(&wps->wps->dev, msg) ||
  1549. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  1550. wpabuf_free(msg);
  1551. return NULL;
  1552. }
  1553. #ifdef CONFIG_WPS_NFC
  1554. if (wps->nfc_pw_token && wps->nfc_pw_token->pk_hash_provided_oob &&
  1555. wps->nfc_pw_token->pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
  1556. /*
  1557. * Use abbreviated handshake since public key hash allowed
  1558. * Enrollee to validate our public key similarly to how Enrollee
  1559. * public key was validated. There is no need to validate Device
  1560. * Password in this case.
  1561. */
  1562. struct wpabuf *plain = wpabuf_alloc(500);
  1563. if (plain == NULL ||
  1564. wps_build_cred(wps, plain) ||
  1565. wps_build_key_wrap_auth(wps, plain) ||
  1566. wps_build_encr_settings(wps, msg, plain)) {
  1567. wpabuf_free(msg);
  1568. wpabuf_free(plain);
  1569. return NULL;
  1570. }
  1571. wpabuf_free(plain);
  1572. config_in_m2 = 1;
  1573. }
  1574. #endif /* CONFIG_WPS_NFC */
  1575. if (wps_build_authenticator(wps, msg)) {
  1576. wpabuf_free(msg);
  1577. return NULL;
  1578. }
  1579. wps->int_reg = 1;
  1580. wps->state = config_in_m2 ? RECV_DONE : RECV_M3;
  1581. return msg;
  1582. }
  1583. static struct wpabuf * wps_build_m2d(struct wps_data *wps)
  1584. {
  1585. struct wpabuf *msg;
  1586. u16 err = wps->config_error;
  1587. wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
  1588. msg = wpabuf_alloc(1000);
  1589. if (msg == NULL)
  1590. return NULL;
  1591. if (wps->wps->ap && wps->wps->ap_setup_locked &&
  1592. err == WPS_CFG_NO_ERROR)
  1593. err = WPS_CFG_SETUP_LOCKED;
  1594. if (wps_build_version(msg) ||
  1595. wps_build_msg_type(msg, WPS_M2D) ||
  1596. wps_build_enrollee_nonce(wps, msg) ||
  1597. wps_build_registrar_nonce(wps, msg) ||
  1598. wps_build_uuid_r(wps, msg) ||
  1599. wps_build_auth_type_flags(wps, msg) ||
  1600. wps_build_encr_type_flags(wps, msg) ||
  1601. wps_build_conn_type_flags(wps, msg) ||
  1602. wps_build_config_methods_r(wps->wps->registrar, msg) ||
  1603. wps_build_device_attrs(&wps->wps->dev, msg) ||
  1604. wps_build_rf_bands(&wps->wps->dev, msg,
  1605. wps->wps->rf_band_cb(wps->wps->cb_ctx)) ||
  1606. wps_build_assoc_state(wps, msg) ||
  1607. wps_build_config_error(msg, err) ||
  1608. wps_build_os_version(&wps->wps->dev, msg) ||
  1609. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  1610. wpabuf_free(msg);
  1611. return NULL;
  1612. }
  1613. wps->state = RECV_M2D_ACK;
  1614. return msg;
  1615. }
  1616. static struct wpabuf * wps_build_m4(struct wps_data *wps)
  1617. {
  1618. struct wpabuf *msg, *plain;
  1619. wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
  1620. wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  1621. plain = wpabuf_alloc(200);
  1622. if (plain == NULL)
  1623. return NULL;
  1624. msg = wpabuf_alloc(1000);
  1625. if (msg == NULL) {
  1626. wpabuf_free(plain);
  1627. return NULL;
  1628. }
  1629. if (wps_build_version(msg) ||
  1630. wps_build_msg_type(msg, WPS_M4) ||
  1631. wps_build_enrollee_nonce(wps, msg) ||
  1632. wps_build_r_hash(wps, msg) ||
  1633. wps_build_r_snonce1(wps, plain) ||
  1634. wps_build_key_wrap_auth(wps, plain) ||
  1635. wps_build_encr_settings(wps, msg, plain) ||
  1636. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  1637. wps_build_authenticator(wps, msg)) {
  1638. wpabuf_free(plain);
  1639. wpabuf_free(msg);
  1640. return NULL;
  1641. }
  1642. wpabuf_free(plain);
  1643. wps->state = RECV_M5;
  1644. return msg;
  1645. }
  1646. static struct wpabuf * wps_build_m6(struct wps_data *wps)
  1647. {
  1648. struct wpabuf *msg, *plain;
  1649. wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
  1650. plain = wpabuf_alloc(200);
  1651. if (plain == NULL)
  1652. return NULL;
  1653. msg = wpabuf_alloc(1000);
  1654. if (msg == NULL) {
  1655. wpabuf_free(plain);
  1656. return NULL;
  1657. }
  1658. if (wps_build_version(msg) ||
  1659. wps_build_msg_type(msg, WPS_M6) ||
  1660. wps_build_enrollee_nonce(wps, msg) ||
  1661. wps_build_r_snonce2(wps, plain) ||
  1662. wps_build_key_wrap_auth(wps, plain) ||
  1663. wps_build_encr_settings(wps, msg, plain) ||
  1664. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  1665. wps_build_authenticator(wps, msg)) {
  1666. wpabuf_free(plain);
  1667. wpabuf_free(msg);
  1668. return NULL;
  1669. }
  1670. wpabuf_free(plain);
  1671. wps->wps_pin_revealed = 1;
  1672. wps->state = RECV_M7;
  1673. return msg;
  1674. }
  1675. static struct wpabuf * wps_build_m8(struct wps_data *wps)
  1676. {
  1677. struct wpabuf *msg, *plain;
  1678. wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
  1679. plain = wpabuf_alloc(500);
  1680. if (plain == NULL)
  1681. return NULL;
  1682. msg = wpabuf_alloc(1000);
  1683. if (msg == NULL) {
  1684. wpabuf_free(plain);
  1685. return NULL;
  1686. }
  1687. if (wps_build_version(msg) ||
  1688. wps_build_msg_type(msg, WPS_M8) ||
  1689. wps_build_enrollee_nonce(wps, msg) ||
  1690. ((wps->wps->ap || wps->er) && wps_build_cred(wps, plain)) ||
  1691. (!wps->wps->ap && !wps->er && wps_build_ap_settings(wps, plain)) ||
  1692. wps_build_key_wrap_auth(wps, plain) ||
  1693. wps_build_encr_settings(wps, msg, plain) ||
  1694. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  1695. wps_build_authenticator(wps, msg)) {
  1696. wpabuf_free(plain);
  1697. wpabuf_free(msg);
  1698. return NULL;
  1699. }
  1700. wpabuf_free(plain);
  1701. wps->state = RECV_DONE;
  1702. return msg;
  1703. }
  1704. struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
  1705. enum wsc_op_code *op_code)
  1706. {
  1707. struct wpabuf *msg;
  1708. #ifdef CONFIG_WPS_UPNP
  1709. if (!wps->int_reg && wps->wps->wps_upnp) {
  1710. struct upnp_pending_message *p, *prev = NULL;
  1711. if (wps->ext_reg > 1)
  1712. wps_registrar_free_pending_m2(wps->wps);
  1713. p = wps->wps->upnp_msgs;
  1714. /* TODO: check pending message MAC address */
  1715. while (p && p->next) {
  1716. prev = p;
  1717. p = p->next;
  1718. }
  1719. if (p) {
  1720. wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
  1721. "UPnP");
  1722. if (prev)
  1723. prev->next = NULL;
  1724. else
  1725. wps->wps->upnp_msgs = NULL;
  1726. msg = p->msg;
  1727. switch (p->type) {
  1728. case WPS_WSC_ACK:
  1729. *op_code = WSC_ACK;
  1730. break;
  1731. case WPS_WSC_NACK:
  1732. *op_code = WSC_NACK;
  1733. break;
  1734. default:
  1735. *op_code = WSC_MSG;
  1736. break;
  1737. }
  1738. os_free(p);
  1739. if (wps->ext_reg == 0)
  1740. wps->ext_reg = 1;
  1741. return msg;
  1742. }
  1743. }
  1744. if (wps->ext_reg) {
  1745. wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
  1746. "pending message available");
  1747. return NULL;
  1748. }
  1749. #endif /* CONFIG_WPS_UPNP */
  1750. switch (wps->state) {
  1751. case SEND_M2:
  1752. if (wps_get_dev_password(wps) < 0)
  1753. msg = wps_build_m2d(wps);
  1754. else
  1755. msg = wps_build_m2(wps);
  1756. *op_code = WSC_MSG;
  1757. break;
  1758. case SEND_M2D:
  1759. msg = wps_build_m2d(wps);
  1760. *op_code = WSC_MSG;
  1761. break;
  1762. case SEND_M4:
  1763. msg = wps_build_m4(wps);
  1764. *op_code = WSC_MSG;
  1765. break;
  1766. case SEND_M6:
  1767. msg = wps_build_m6(wps);
  1768. *op_code = WSC_MSG;
  1769. break;
  1770. case SEND_M8:
  1771. msg = wps_build_m8(wps);
  1772. *op_code = WSC_MSG;
  1773. break;
  1774. case RECV_DONE:
  1775. msg = wps_build_wsc_ack(wps);
  1776. *op_code = WSC_ACK;
  1777. break;
  1778. case SEND_WSC_NACK:
  1779. msg = wps_build_wsc_nack(wps);
  1780. *op_code = WSC_NACK;
  1781. break;
  1782. default:
  1783. wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
  1784. "a message", wps->state);
  1785. msg = NULL;
  1786. break;
  1787. }
  1788. if (*op_code == WSC_MSG && msg) {
  1789. /* Save a copy of the last message for Authenticator derivation
  1790. */
  1791. wpabuf_free(wps->last_msg);
  1792. wps->last_msg = wpabuf_dup(msg);
  1793. }
  1794. return msg;
  1795. }
  1796. static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
  1797. {
  1798. if (e_nonce == NULL) {
  1799. wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
  1800. return -1;
  1801. }
  1802. os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
  1803. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
  1804. wps->nonce_e, WPS_NONCE_LEN);
  1805. return 0;
  1806. }
  1807. static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
  1808. {
  1809. if (r_nonce == NULL) {
  1810. wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
  1811. return -1;
  1812. }
  1813. if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
  1814. wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
  1815. return -1;
  1816. }
  1817. return 0;
  1818. }
  1819. static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
  1820. {
  1821. if (uuid_e == NULL) {
  1822. wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
  1823. return -1;
  1824. }
  1825. os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
  1826. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
  1827. return 0;
  1828. }
  1829. static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
  1830. {
  1831. if (pw_id == NULL) {
  1832. wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
  1833. return -1;
  1834. }
  1835. wps->dev_pw_id = WPA_GET_BE16(pw_id);
  1836. wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
  1837. return 0;
  1838. }
  1839. static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
  1840. {
  1841. if (e_hash1 == NULL) {
  1842. wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
  1843. return -1;
  1844. }
  1845. os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
  1846. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
  1847. return 0;
  1848. }
  1849. static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
  1850. {
  1851. if (e_hash2 == NULL) {
  1852. wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
  1853. return -1;
  1854. }
  1855. os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
  1856. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
  1857. return 0;
  1858. }
  1859. static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
  1860. {
  1861. u8 hash[SHA256_MAC_LEN];
  1862. const u8 *addr[4];
  1863. size_t len[4];
  1864. if (e_snonce1 == NULL) {
  1865. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
  1866. return -1;
  1867. }
  1868. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
  1869. WPS_SECRET_NONCE_LEN);
  1870. /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
  1871. addr[0] = e_snonce1;
  1872. len[0] = WPS_SECRET_NONCE_LEN;
  1873. addr[1] = wps->psk1;
  1874. len[1] = WPS_PSK_LEN;
  1875. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1876. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1877. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1878. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1879. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1880. if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
  1881. wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
  1882. "not match with the pre-committed value");
  1883. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  1884. wps_pwd_auth_fail_event(wps->wps, 0, 1, wps->mac_addr_e);
  1885. return -1;
  1886. }
  1887. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
  1888. "half of the device password");
  1889. return 0;
  1890. }
  1891. static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
  1892. {
  1893. u8 hash[SHA256_MAC_LEN];
  1894. const u8 *addr[4];
  1895. size_t len[4];
  1896. if (e_snonce2 == NULL) {
  1897. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
  1898. return -1;
  1899. }
  1900. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
  1901. WPS_SECRET_NONCE_LEN);
  1902. /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
  1903. addr[0] = e_snonce2;
  1904. len[0] = WPS_SECRET_NONCE_LEN;
  1905. addr[1] = wps->psk2;
  1906. len[1] = WPS_PSK_LEN;
  1907. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1908. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1909. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1910. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1911. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1912. if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
  1913. wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
  1914. "not match with the pre-committed value");
  1915. wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
  1916. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  1917. wps_pwd_auth_fail_event(wps->wps, 0, 2, wps->mac_addr_e);
  1918. return -1;
  1919. }
  1920. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
  1921. "half of the device password");
  1922. wps->wps_pin_revealed = 0;
  1923. wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
  1924. /*
  1925. * In case wildcard PIN is used and WPS handshake succeeds in the first
  1926. * attempt, wps_registrar_unlock_pin() would not free the PIN, so make
  1927. * sure the PIN gets invalidated here.
  1928. */
  1929. wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
  1930. return 0;
  1931. }
  1932. static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
  1933. {
  1934. if (mac_addr == NULL) {
  1935. wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
  1936. return -1;
  1937. }
  1938. wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
  1939. MAC2STR(mac_addr));
  1940. os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
  1941. os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
  1942. return 0;
  1943. }
  1944. static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
  1945. size_t pk_len)
  1946. {
  1947. if (pk == NULL || pk_len == 0) {
  1948. wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
  1949. return -1;
  1950. }
  1951. wpabuf_free(wps->dh_pubkey_e);
  1952. wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
  1953. if (wps->dh_pubkey_e == NULL)
  1954. return -1;
  1955. return 0;
  1956. }
  1957. static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
  1958. {
  1959. u16 auth_types;
  1960. if (auth == NULL) {
  1961. wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
  1962. "received");
  1963. return -1;
  1964. }
  1965. auth_types = WPA_GET_BE16(auth);
  1966. wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
  1967. auth_types);
  1968. wps->auth_type = wps->wps->auth_types & auth_types;
  1969. if (wps->auth_type == 0) {
  1970. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  1971. "authentication types (own 0x%x Enrollee 0x%x)",
  1972. wps->wps->auth_types, auth_types);
  1973. #ifdef WPS_WORKAROUNDS
  1974. /*
  1975. * Some deployed implementations seem to advertise incorrect
  1976. * information in this attribute. For example, Linksys WRT350N
  1977. * seems to have a byteorder bug that breaks this negotiation.
  1978. * In order to interoperate with existing implementations,
  1979. * assume that the Enrollee supports everything we do.
  1980. */
  1981. wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
  1982. "does not advertise supported authentication types "
  1983. "correctly");
  1984. wps->auth_type = wps->wps->auth_types;
  1985. #else /* WPS_WORKAROUNDS */
  1986. return -1;
  1987. #endif /* WPS_WORKAROUNDS */
  1988. }
  1989. return 0;
  1990. }
  1991. static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
  1992. {
  1993. u16 encr_types;
  1994. if (encr == NULL) {
  1995. wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
  1996. "received");
  1997. return -1;
  1998. }
  1999. encr_types = WPA_GET_BE16(encr);
  2000. wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
  2001. encr_types);
  2002. wps->encr_type = wps->wps->encr_types & encr_types;
  2003. if (wps->encr_type == 0) {
  2004. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  2005. "encryption types (own 0x%x Enrollee 0x%x)",
  2006. wps->wps->encr_types, encr_types);
  2007. #ifdef WPS_WORKAROUNDS
  2008. /*
  2009. * Some deployed implementations seem to advertise incorrect
  2010. * information in this attribute. For example, Linksys WRT350N
  2011. * seems to have a byteorder bug that breaks this negotiation.
  2012. * In order to interoperate with existing implementations,
  2013. * assume that the Enrollee supports everything we do.
  2014. */
  2015. wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
  2016. "does not advertise supported encryption types "
  2017. "correctly");
  2018. wps->encr_type = wps->wps->encr_types;
  2019. #else /* WPS_WORKAROUNDS */
  2020. return -1;
  2021. #endif /* WPS_WORKAROUNDS */
  2022. }
  2023. return 0;
  2024. }
  2025. static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
  2026. {
  2027. if (conn == NULL) {
  2028. wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
  2029. "received");
  2030. return -1;
  2031. }
  2032. wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
  2033. *conn);
  2034. return 0;
  2035. }
  2036. static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
  2037. {
  2038. u16 m;
  2039. if (methods == NULL) {
  2040. wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
  2041. return -1;
  2042. }
  2043. m = WPA_GET_BE16(methods);
  2044. wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x"
  2045. "%s%s%s%s%s%s%s%s%s", m,
  2046. m & WPS_CONFIG_USBA ? " [USBA]" : "",
  2047. m & WPS_CONFIG_ETHERNET ? " [Ethernet]" : "",
  2048. m & WPS_CONFIG_LABEL ? " [Label]" : "",
  2049. m & WPS_CONFIG_DISPLAY ? " [Display]" : "",
  2050. m & WPS_CONFIG_EXT_NFC_TOKEN ? " [Ext NFC Token]" : "",
  2051. m & WPS_CONFIG_INT_NFC_TOKEN ? " [Int NFC Token]" : "",
  2052. m & WPS_CONFIG_NFC_INTERFACE ? " [NFC]" : "",
  2053. m & WPS_CONFIG_PUSHBUTTON ? " [PBC]" : "",
  2054. m & WPS_CONFIG_KEYPAD ? " [Keypad]" : "");
  2055. if (!(m & WPS_CONFIG_DISPLAY) && !wps->use_psk_key) {
  2056. /*
  2057. * The Enrollee does not have a display so it is unlikely to be
  2058. * able to show the passphrase to a user and as such, could
  2059. * benefit from receiving PSK to reduce key derivation time.
  2060. */
  2061. wpa_printf(MSG_DEBUG, "WPS: Prefer PSK format key due to "
  2062. "Enrollee not supporting display");
  2063. wps->use_psk_key = 1;
  2064. }
  2065. return 0;
  2066. }
  2067. static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
  2068. {
  2069. if (state == NULL) {
  2070. wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
  2071. "received");
  2072. return -1;
  2073. }
  2074. wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
  2075. *state);
  2076. return 0;
  2077. }
  2078. static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
  2079. {
  2080. u16 a;
  2081. if (assoc == NULL) {
  2082. wpa_printf(MSG_DEBUG, "WPS: No Association State received");
  2083. return -1;
  2084. }
  2085. a = WPA_GET_BE16(assoc);
  2086. wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
  2087. return 0;
  2088. }
  2089. static int wps_process_config_error(struct wps_data *wps, const u8 *err)
  2090. {
  2091. u16 e;
  2092. if (err == NULL) {
  2093. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
  2094. return -1;
  2095. }
  2096. e = WPA_GET_BE16(err);
  2097. wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
  2098. return 0;
  2099. }
  2100. static int wps_registrar_p2p_dev_addr_match(struct wps_data *wps)
  2101. {
  2102. #ifdef CONFIG_P2P
  2103. struct wps_registrar *reg = wps->wps->registrar;
  2104. if (is_zero_ether_addr(reg->p2p_dev_addr))
  2105. return 1; /* no filtering in use */
  2106. if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) != 0) {
  2107. wpa_printf(MSG_DEBUG, "WPS: No match on P2P Device Address "
  2108. "filtering for PBC: expected " MACSTR " was "
  2109. MACSTR " - indicate PBC session overlap",
  2110. MAC2STR(reg->p2p_dev_addr),
  2111. MAC2STR(wps->p2p_dev_addr));
  2112. return 0;
  2113. }
  2114. #endif /* CONFIG_P2P */
  2115. return 1;
  2116. }
  2117. static int wps_registrar_skip_overlap(struct wps_data *wps)
  2118. {
  2119. #ifdef CONFIG_P2P
  2120. struct wps_registrar *reg = wps->wps->registrar;
  2121. if (is_zero_ether_addr(reg->p2p_dev_addr))
  2122. return 0; /* no specific Enrollee selected */
  2123. if (os_memcmp(reg->p2p_dev_addr, wps->p2p_dev_addr, ETH_ALEN) == 0) {
  2124. wpa_printf(MSG_DEBUG, "WPS: Skip PBC overlap due to selected "
  2125. "Enrollee match");
  2126. return 1;
  2127. }
  2128. #endif /* CONFIG_P2P */
  2129. return 0;
  2130. }
  2131. static enum wps_process_res wps_process_m1(struct wps_data *wps,
  2132. struct wps_parse_attr *attr)
  2133. {
  2134. wpa_printf(MSG_DEBUG, "WPS: Received M1");
  2135. if (wps->state != RECV_M1) {
  2136. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  2137. "receiving M1", wps->state);
  2138. return WPS_FAILURE;
  2139. }
  2140. if (wps_process_uuid_e(wps, attr->uuid_e) ||
  2141. wps_process_mac_addr(wps, attr->mac_addr) ||
  2142. wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  2143. wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
  2144. wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
  2145. wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
  2146. wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
  2147. wps_process_config_methods(wps, attr->config_methods) ||
  2148. wps_process_wps_state(wps, attr->wps_state) ||
  2149. wps_process_device_attrs(&wps->peer_dev, attr) ||
  2150. wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
  2151. wps_process_assoc_state(wps, attr->assoc_state) ||
  2152. wps_process_dev_password_id(wps, attr->dev_password_id) ||
  2153. wps_process_config_error(wps, attr->config_error) ||
  2154. wps_process_os_version(&wps->peer_dev, attr->os_version))
  2155. return WPS_FAILURE;
  2156. if (wps->dev_pw_id < 0x10 &&
  2157. wps->dev_pw_id != DEV_PW_DEFAULT &&
  2158. wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
  2159. wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
  2160. wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
  2161. #ifdef CONFIG_WPS_NFC
  2162. wps->dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
  2163. #endif /* CONFIG_WPS_NFC */
  2164. (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
  2165. !wps->wps->registrar->pbc)) {
  2166. wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
  2167. wps->dev_pw_id);
  2168. wps->state = SEND_M2D;
  2169. return WPS_CONTINUE;
  2170. }
  2171. #ifdef CONFIG_WPS_NFC
  2172. if (wps->dev_pw_id >= 0x10 ||
  2173. wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) {
  2174. struct wps_nfc_pw_token *token;
  2175. const u8 *addr[1];
  2176. u8 hash[WPS_HASH_LEN];
  2177. wpa_printf(MSG_DEBUG, "WPS: Searching for NFC token match for id=%d (ctx %p registrar %p)",
  2178. wps->dev_pw_id, wps->wps, wps->wps->registrar);
  2179. token = wps_get_nfc_pw_token(
  2180. &wps->wps->registrar->nfc_pw_tokens, wps->dev_pw_id);
  2181. if (token && token->peer_pk_hash_known) {
  2182. wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
  2183. "Password Token");
  2184. dl_list_del(&token->list);
  2185. wps->nfc_pw_token = token;
  2186. addr[0] = attr->public_key;
  2187. sha256_vector(1, addr, &attr->public_key_len, hash);
  2188. if (os_memcmp(hash, wps->nfc_pw_token->pubkey_hash,
  2189. WPS_OOB_PUBKEY_HASH_LEN) != 0) {
  2190. wpa_printf(MSG_ERROR, "WPS: Public Key hash "
  2191. "mismatch");
  2192. wps->state = SEND_M2D;
  2193. wps->config_error =
  2194. WPS_CFG_PUBLIC_KEY_HASH_MISMATCH;
  2195. return WPS_CONTINUE;
  2196. }
  2197. } else if (token) {
  2198. wpa_printf(MSG_DEBUG, "WPS: Found matching NFC "
  2199. "Password Token (no peer PK hash)");
  2200. wps->nfc_pw_token = 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. #ifdef CONFIG_WPS2
  2907. methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
  2908. WPS_CONFIG_PHY_PUSHBUTTON);
  2909. #endif /* CONFIG_WPS2 */
  2910. if (reg->pbc) {
  2911. reg->sel_reg_dev_password_id_override =
  2912. DEV_PW_PUSHBUTTON;
  2913. wps_set_pushbutton(&methods, reg->wps->config_methods);
  2914. } else if (dev_pw_id)
  2915. reg->sel_reg_dev_password_id_override = dev_pw_id;
  2916. wpa_printf(MSG_DEBUG, "WPS: Internal Registrar selected "
  2917. "(pbc=%d)", reg->pbc);
  2918. reg->sel_reg_config_methods_override = methods;
  2919. } else
  2920. wpa_printf(MSG_DEBUG, "WPS: Internal Registrar not selected");
  2921. wps_registrar_sel_reg_union(reg);
  2922. wps_set_ie(reg);
  2923. wps_cb_set_sel_reg(reg);
  2924. }
  2925. int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
  2926. char *buf, size_t buflen)
  2927. {
  2928. struct wps_registrar_device *d;
  2929. int len = 0, ret;
  2930. char uuid[40];
  2931. char devtype[WPS_DEV_TYPE_BUFSIZE];
  2932. d = wps_device_get(reg, addr);
  2933. if (d == NULL)
  2934. return 0;
  2935. if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
  2936. return 0;
  2937. ret = os_snprintf(buf + len, buflen - len,
  2938. "wpsUuid=%s\n"
  2939. "wpsPrimaryDeviceType=%s\n"
  2940. "wpsDeviceName=%s\n"
  2941. "wpsManufacturer=%s\n"
  2942. "wpsModelName=%s\n"
  2943. "wpsModelNumber=%s\n"
  2944. "wpsSerialNumber=%s\n",
  2945. uuid,
  2946. wps_dev_type_bin2str(d->dev.pri_dev_type, devtype,
  2947. sizeof(devtype)),
  2948. d->dev.device_name ? d->dev.device_name : "",
  2949. d->dev.manufacturer ? d->dev.manufacturer : "",
  2950. d->dev.model_name ? d->dev.model_name : "",
  2951. d->dev.model_number ? d->dev.model_number : "",
  2952. d->dev.serial_number ? d->dev.serial_number : "");
  2953. if (ret < 0 || (size_t) ret >= buflen - len)
  2954. return len;
  2955. len += ret;
  2956. return len;
  2957. }
  2958. int wps_registrar_config_ap(struct wps_registrar *reg,
  2959. struct wps_credential *cred)
  2960. {
  2961. #ifdef CONFIG_WPS2
  2962. wpa_printf(MSG_DEBUG, "WPS: encr_type=0x%x", cred->encr_type);
  2963. if (!(cred->encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP |
  2964. WPS_ENCR_AES))) {
  2965. if (cred->encr_type & WPS_ENCR_WEP) {
  2966. wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
  2967. "due to WEP configuration");
  2968. return -1;
  2969. }
  2970. wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
  2971. "invalid encr_type 0x%x", cred->encr_type);
  2972. return -1;
  2973. }
  2974. if ((cred->encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
  2975. WPS_ENCR_TKIP) {
  2976. wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
  2977. "TKIP+AES");
  2978. cred->encr_type |= WPS_ENCR_AES;
  2979. }
  2980. if ((cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
  2981. WPS_AUTH_WPAPSK) {
  2982. wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
  2983. "WPAPSK+WPA2PSK");
  2984. cred->auth_type |= WPS_AUTH_WPA2PSK;
  2985. }
  2986. #endif /* CONFIG_WPS2 */
  2987. if (reg->wps->cred_cb)
  2988. return reg->wps->cred_cb(reg->wps->cb_ctx, cred);
  2989. return -1;
  2990. }
  2991. #ifdef CONFIG_WPS_NFC
  2992. int wps_registrar_add_nfc_pw_token(struct wps_registrar *reg,
  2993. const u8 *pubkey_hash, u16 pw_id,
  2994. const u8 *dev_pw, size_t dev_pw_len,
  2995. int pk_hash_provided_oob)
  2996. {
  2997. struct wps_nfc_pw_token *token;
  2998. if (dev_pw_len > WPS_OOB_DEVICE_PASSWORD_LEN)
  2999. return -1;
  3000. if (pw_id == DEV_PW_NFC_CONNECTION_HANDOVER &&
  3001. (pubkey_hash == NULL || !pk_hash_provided_oob)) {
  3002. wpa_printf(MSG_DEBUG, "WPS: Unexpected NFC Password Token "
  3003. "addition - missing public key hash");
  3004. return -1;
  3005. }
  3006. wps_free_nfc_pw_tokens(&reg->nfc_pw_tokens, pw_id);
  3007. token = os_zalloc(sizeof(*token));
  3008. if (token == NULL)
  3009. return -1;
  3010. token->peer_pk_hash_known = pubkey_hash != NULL;
  3011. if (pubkey_hash)
  3012. os_memcpy(token->pubkey_hash, pubkey_hash,
  3013. WPS_OOB_PUBKEY_HASH_LEN);
  3014. token->pw_id = pw_id;
  3015. token->pk_hash_provided_oob = pk_hash_provided_oob;
  3016. if (dev_pw) {
  3017. wpa_snprintf_hex_uppercase((char *) token->dev_pw,
  3018. sizeof(token->dev_pw),
  3019. dev_pw, dev_pw_len);
  3020. token->dev_pw_len = dev_pw_len * 2;
  3021. }
  3022. dl_list_add(&reg->nfc_pw_tokens, &token->list);
  3023. reg->selected_registrar = 1;
  3024. reg->pbc = 0;
  3025. wps_registrar_add_authorized_mac(reg,
  3026. (u8 *) "\xff\xff\xff\xff\xff\xff");
  3027. wps_registrar_selected_registrar_changed(reg, pw_id);
  3028. eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
  3029. eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
  3030. wps_registrar_set_selected_timeout,
  3031. reg, NULL);
  3032. wpa_printf(MSG_DEBUG, "WPS: Added NFC Device Password %u to Registrar",
  3033. pw_id);
  3034. return 0;
  3035. }
  3036. int wps_registrar_add_nfc_password_token(struct wps_registrar *reg,
  3037. const u8 *oob_dev_pw,
  3038. size_t oob_dev_pw_len)
  3039. {
  3040. const u8 *pos, *hash, *dev_pw;
  3041. u16 id;
  3042. size_t dev_pw_len;
  3043. if (oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2 ||
  3044. oob_dev_pw_len > WPS_OOB_PUBKEY_HASH_LEN + 2 +
  3045. WPS_OOB_DEVICE_PASSWORD_LEN)
  3046. return -1;
  3047. hash = oob_dev_pw;
  3048. pos = oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN;
  3049. id = WPA_GET_BE16(pos);
  3050. dev_pw = pos + 2;
  3051. dev_pw_len = oob_dev_pw + oob_dev_pw_len - dev_pw;
  3052. wpa_printf(MSG_DEBUG, "WPS: Add NFC Password Token for Password ID %u",
  3053. id);
  3054. wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
  3055. hash, WPS_OOB_PUBKEY_HASH_LEN);
  3056. wpa_hexdump_key(MSG_DEBUG, "WPS: Device Password", dev_pw, dev_pw_len);
  3057. return wps_registrar_add_nfc_pw_token(reg, hash, id, dev_pw,
  3058. dev_pw_len, 0);
  3059. }
  3060. void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
  3061. struct wps_nfc_pw_token *token)
  3062. {
  3063. wps_registrar_remove_authorized_mac(reg,
  3064. (u8 *) "\xff\xff\xff\xff\xff\xff");
  3065. wps_registrar_selected_registrar_changed(reg, 0);
  3066. /*
  3067. * Free the NFC password token if it was used only for a single protocol
  3068. * run. The static handover case uses the same password token multiple
  3069. * times, so do not free that case here.
  3070. */
  3071. if (token->peer_pk_hash_known)
  3072. os_free(token);
  3073. }
  3074. #endif /* CONFIG_WPS_NFC */