wps_registrar.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * Wi-Fi Protected Setup - Registrar
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "sha256.h"
  17. #include "base64.h"
  18. #include "ieee802_11_defs.h"
  19. #include "eloop.h"
  20. #include "wps_i.h"
  21. #include "wps_dev_attr.h"
  22. struct wps_uuid_pin {
  23. struct wps_uuid_pin *next;
  24. u8 uuid[WPS_UUID_LEN];
  25. int wildcard_uuid;
  26. u8 *pin;
  27. size_t pin_len;
  28. int locked;
  29. };
  30. static void wps_free_pin(struct wps_uuid_pin *pin)
  31. {
  32. os_free(pin->pin);
  33. os_free(pin);
  34. }
  35. static void wps_free_pins(struct wps_uuid_pin *pins)
  36. {
  37. struct wps_uuid_pin *pin, *prev;
  38. pin = pins;
  39. while (pin) {
  40. prev = pin;
  41. pin = pin->next;
  42. wps_free_pin(prev);
  43. }
  44. }
  45. struct wps_pbc_session {
  46. struct wps_pbc_session *next;
  47. u8 addr[ETH_ALEN];
  48. u8 uuid_e[WPS_UUID_LEN];
  49. struct os_time timestamp;
  50. };
  51. static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
  52. {
  53. struct wps_pbc_session *prev;
  54. while (pbc) {
  55. prev = pbc;
  56. pbc = pbc->next;
  57. os_free(prev);
  58. }
  59. }
  60. struct wps_registrar {
  61. struct wps_context *wps;
  62. int pbc;
  63. int selected_registrar;
  64. int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
  65. size_t psk_len);
  66. int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
  67. const u8 *probe_resp_ie, size_t probe_resp_ie_len);
  68. void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
  69. const struct wps_device_data *dev);
  70. void *cb_ctx;
  71. struct wps_uuid_pin *pins;
  72. struct wps_pbc_session *pbc_sessions;
  73. };
  74. static int wps_set_ie(struct wps_registrar *reg);
  75. static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
  76. static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
  77. const u8 *addr, const u8 *uuid_e)
  78. {
  79. struct wps_pbc_session *pbc, *prev = NULL;
  80. struct os_time now;
  81. os_get_time(&now);
  82. pbc = reg->pbc_sessions;
  83. while (pbc) {
  84. if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
  85. os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
  86. if (prev)
  87. prev->next = pbc->next;
  88. else
  89. reg->pbc_sessions = pbc->next;
  90. break;
  91. }
  92. prev = pbc;
  93. pbc = pbc->next;
  94. }
  95. if (!pbc) {
  96. pbc = os_zalloc(sizeof(*pbc));
  97. if (pbc == NULL)
  98. return;
  99. os_memcpy(pbc->addr, addr, ETH_ALEN);
  100. if (uuid_e)
  101. os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
  102. }
  103. pbc->next = reg->pbc_sessions;
  104. reg->pbc_sessions = pbc;
  105. pbc->timestamp = now;
  106. /* remove entries that have timed out */
  107. prev = pbc;
  108. pbc = pbc->next;
  109. while (pbc) {
  110. if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
  111. prev->next = NULL;
  112. wps_free_pbc_sessions(pbc);
  113. break;
  114. }
  115. prev = pbc;
  116. pbc = pbc->next;
  117. }
  118. }
  119. static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
  120. const u8 *addr, const u8 *uuid_e)
  121. {
  122. struct wps_pbc_session *pbc, *prev = NULL;
  123. pbc = reg->pbc_sessions;
  124. while (pbc) {
  125. if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
  126. os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
  127. if (prev)
  128. prev->next = pbc->next;
  129. else
  130. reg->pbc_sessions = pbc->next;
  131. os_free(pbc);
  132. break;
  133. }
  134. prev = pbc;
  135. pbc = pbc->next;
  136. }
  137. }
  138. int wps_registrar_pbc_overlap(struct wps_registrar *reg,
  139. const u8 *addr, const u8 *uuid_e)
  140. {
  141. int count = 0;
  142. struct wps_pbc_session *pbc;
  143. struct os_time now;
  144. os_get_time(&now);
  145. for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
  146. if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
  147. break;
  148. if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
  149. uuid_e == NULL ||
  150. os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
  151. count++;
  152. }
  153. if (addr || uuid_e)
  154. count++;
  155. return count > 1 ? 1 : 0;
  156. }
  157. static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
  158. {
  159. wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
  160. wps->wps_state);
  161. wpabuf_put_be16(msg, ATTR_WPS_STATE);
  162. wpabuf_put_be16(msg, 1);
  163. wpabuf_put_u8(msg, wps->wps_state);
  164. return 0;
  165. }
  166. static int wps_build_ap_setup_locked(struct wps_context *wps,
  167. struct wpabuf *msg)
  168. {
  169. if (wps->ap_setup_locked) {
  170. wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
  171. wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
  172. wpabuf_put_be16(msg, 1);
  173. wpabuf_put_u8(msg, 1);
  174. }
  175. return 0;
  176. }
  177. static int wps_build_selected_registrar(struct wps_registrar *reg,
  178. struct wpabuf *msg)
  179. {
  180. if (!reg->selected_registrar)
  181. return 0;
  182. wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
  183. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
  184. wpabuf_put_be16(msg, 1);
  185. wpabuf_put_u8(msg, 1);
  186. return 0;
  187. }
  188. static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
  189. struct wpabuf *msg)
  190. {
  191. u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
  192. if (!reg->selected_registrar)
  193. return 0;
  194. wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
  195. wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
  196. wpabuf_put_be16(msg, 2);
  197. wpabuf_put_be16(msg, id);
  198. return 0;
  199. }
  200. static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
  201. struct wpabuf *msg)
  202. {
  203. u16 methods;
  204. if (!reg->selected_registrar)
  205. return 0;
  206. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  207. if (reg->pbc)
  208. methods |= WPS_CONFIG_PUSHBUTTON;
  209. wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
  210. methods);
  211. wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
  212. wpabuf_put_be16(msg, 2);
  213. wpabuf_put_be16(msg, methods);
  214. return 0;
  215. }
  216. static int wps_build_probe_config_methods(struct wps_registrar *reg,
  217. struct wpabuf *msg)
  218. {
  219. u16 methods;
  220. methods = 0;
  221. wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
  222. wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
  223. wpabuf_put_be16(msg, 2);
  224. wpabuf_put_be16(msg, methods);
  225. return 0;
  226. }
  227. static int wps_build_config_methods_r(struct wps_registrar *reg,
  228. struct wpabuf *msg)
  229. {
  230. u16 methods;
  231. methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
  232. if (reg->pbc)
  233. methods |= WPS_CONFIG_PUSHBUTTON;
  234. return wps_build_config_methods(msg, methods);
  235. }
  236. static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
  237. {
  238. u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
  239. wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
  240. wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
  241. wpabuf_put_be16(msg, 1);
  242. wpabuf_put_u8(msg, resp);
  243. return 0;
  244. }
  245. struct wps_registrar *
  246. wps_registrar_init(struct wps_context *wps,
  247. const struct wps_registrar_config *cfg)
  248. {
  249. struct wps_registrar *reg = os_zalloc(sizeof(*reg));
  250. if (reg == NULL)
  251. return NULL;
  252. reg->wps = wps;
  253. reg->new_psk_cb = cfg->new_psk_cb;
  254. reg->set_ie_cb = cfg->set_ie_cb;
  255. reg->pin_needed_cb = cfg->pin_needed_cb;
  256. reg->cb_ctx = cfg->cb_ctx;
  257. if (wps_set_ie(reg)) {
  258. wps_registrar_deinit(reg);
  259. return NULL;
  260. }
  261. return reg;
  262. }
  263. void wps_registrar_deinit(struct wps_registrar *reg)
  264. {
  265. if (reg == NULL)
  266. return;
  267. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  268. wps_free_pins(reg->pins);
  269. wps_free_pbc_sessions(reg->pbc_sessions);
  270. os_free(reg);
  271. }
  272. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
  273. const u8 *pin, size_t pin_len)
  274. {
  275. struct wps_uuid_pin *p;
  276. p = os_zalloc(sizeof(*p));
  277. if (p == NULL)
  278. return -1;
  279. if (uuid == NULL)
  280. p->wildcard_uuid = 1;
  281. else
  282. os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
  283. p->pin = os_malloc(pin_len);
  284. if (p->pin == NULL) {
  285. os_free(p);
  286. return -1;
  287. }
  288. os_memcpy(p->pin, pin, pin_len);
  289. p->pin_len = pin_len;
  290. p->next = reg->pins;
  291. reg->pins = p;
  292. wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
  293. wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
  294. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
  295. reg->selected_registrar = 1;
  296. reg->pbc = 0;
  297. wps_set_ie(reg);
  298. return 0;
  299. }
  300. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
  301. {
  302. struct wps_uuid_pin *pin, *prev;
  303. prev = NULL;
  304. pin = reg->pins;
  305. while (pin) {
  306. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  307. if (prev == NULL)
  308. reg->pins = pin->next;
  309. else
  310. prev->next = pin->next;
  311. wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
  312. pin->uuid, WPS_UUID_LEN);
  313. wps_free_pin(pin);
  314. return 0;
  315. }
  316. prev = pin;
  317. pin = pin->next;
  318. }
  319. return -1;
  320. }
  321. static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
  322. const u8 *uuid, size_t *pin_len)
  323. {
  324. struct wps_uuid_pin *pin;
  325. pin = reg->pins;
  326. while (pin) {
  327. if (!pin->wildcard_uuid &&
  328. os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
  329. break;
  330. pin = pin->next;
  331. }
  332. if (!pin) {
  333. /* Check for wildcard UUIDs since none of the UUID-specific
  334. * PINs matched */
  335. pin = reg->pins;
  336. while (pin) {
  337. if (pin->wildcard_uuid == 1) {
  338. wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
  339. "PIN. Assigned it for this UUID-E");
  340. pin->wildcard_uuid = 2;
  341. os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
  342. break;
  343. }
  344. pin = pin->next;
  345. }
  346. }
  347. if (!pin)
  348. return NULL;
  349. /*
  350. * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
  351. * that could otherwise avoid PIN invalidations.
  352. */
  353. if (pin->locked) {
  354. wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
  355. "allow concurrent re-use");
  356. return NULL;
  357. }
  358. *pin_len = pin->pin_len;
  359. pin->locked = 1;
  360. return pin->pin;
  361. }
  362. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
  363. {
  364. struct wps_uuid_pin *pin;
  365. pin = reg->pins;
  366. while (pin) {
  367. if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
  368. if (pin->wildcard_uuid == 2) {
  369. wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
  370. "wildcard PIN");
  371. return wps_registrar_invalidate_pin(reg, uuid);
  372. }
  373. pin->locked = 0;
  374. return 0;
  375. }
  376. pin = pin->next;
  377. }
  378. return -1;
  379. }
  380. static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
  381. {
  382. struct wps_registrar *reg = eloop_ctx;
  383. wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
  384. reg->selected_registrar = 0;
  385. reg->pbc = 0;
  386. wps_set_ie(reg);
  387. }
  388. int wps_registrar_button_pushed(struct wps_registrar *reg)
  389. {
  390. if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
  391. wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
  392. "mode");
  393. return -1;
  394. }
  395. wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
  396. reg->selected_registrar = 1;
  397. reg->pbc = 1;
  398. wps_set_ie(reg);
  399. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  400. eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
  401. reg, NULL);
  402. return 0;
  403. }
  404. static void wps_registrar_pbc_completed(struct wps_registrar *reg)
  405. {
  406. wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
  407. eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
  408. reg->selected_registrar = 0;
  409. reg->pbc = 0;
  410. wps_set_ie(reg);
  411. }
  412. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  413. const struct wpabuf *wps_data)
  414. {
  415. struct wps_parse_attr attr;
  416. u16 methods;
  417. wpa_hexdump_buf(MSG_MSGDUMP,
  418. "WPS: Probe Request with WPS data received",
  419. wps_data);
  420. if (wps_parse_msg(wps_data, &attr) < 0 ||
  421. attr.version == NULL || *attr.version != WPS_VERSION) {
  422. wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
  423. "version 0x%x", attr.version ? *attr.version : 0);
  424. return;
  425. }
  426. if (attr.config_methods == NULL) {
  427. wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
  428. "Probe Request");
  429. return;
  430. }
  431. methods = WPA_GET_BE16(attr.config_methods);
  432. if (!(methods & WPS_CONFIG_PUSHBUTTON))
  433. return; /* Not PBC */
  434. wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
  435. MACSTR, MAC2STR(addr));
  436. wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
  437. }
  438. static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
  439. const u8 *psk, size_t psk_len)
  440. {
  441. if (reg->new_psk_cb == NULL)
  442. return 0;
  443. return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
  444. }
  445. static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
  446. const struct wps_device_data *dev)
  447. {
  448. if (reg->pin_needed_cb == NULL)
  449. return;
  450. reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
  451. }
  452. static int wps_cb_set_ie(struct wps_registrar *reg,
  453. const struct wpabuf *beacon_ie,
  454. const struct wpabuf *probe_resp_ie)
  455. {
  456. if (reg->set_ie_cb == NULL)
  457. return 0;
  458. return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
  459. wpabuf_len(beacon_ie),
  460. wpabuf_head(probe_resp_ie),
  461. wpabuf_len(probe_resp_ie));
  462. }
  463. /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
  464. static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
  465. {
  466. struct wpabuf *ie;
  467. const u8 *pos, *end;
  468. ie = wpabuf_alloc(wpabuf_len(data) + 100);
  469. if (ie == NULL) {
  470. wpabuf_free(data);
  471. return NULL;
  472. }
  473. pos = wpabuf_head(data);
  474. end = pos + wpabuf_len(data);
  475. while (end > pos) {
  476. size_t frag_len = end - pos;
  477. if (frag_len > 251)
  478. frag_len = 251;
  479. wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
  480. wpabuf_put_u8(ie, 4 + frag_len);
  481. wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
  482. wpabuf_put_data(ie, pos, frag_len);
  483. pos += frag_len;
  484. }
  485. wpabuf_free(data);
  486. return ie;
  487. }
  488. static int wps_set_ie(struct wps_registrar *reg)
  489. {
  490. struct wpabuf *beacon;
  491. struct wpabuf *probe;
  492. int ret;
  493. wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
  494. beacon = wpabuf_alloc(300);
  495. if (beacon == NULL)
  496. return -1;
  497. probe = wpabuf_alloc(400);
  498. if (probe == NULL) {
  499. wpabuf_free(beacon);
  500. return -1;
  501. }
  502. if (wps_build_version(beacon) ||
  503. wps_build_wps_state(reg->wps, beacon) ||
  504. wps_build_ap_setup_locked(reg->wps, beacon) ||
  505. wps_build_selected_registrar(reg, beacon) ||
  506. wps_build_sel_reg_dev_password_id(reg, beacon) ||
  507. wps_build_sel_reg_config_methods(reg, beacon) ||
  508. wps_build_version(probe) ||
  509. wps_build_wps_state(reg->wps, probe) ||
  510. wps_build_ap_setup_locked(reg->wps, probe) ||
  511. wps_build_selected_registrar(reg, probe) ||
  512. wps_build_sel_reg_dev_password_id(reg, probe) ||
  513. wps_build_sel_reg_config_methods(reg, probe) ||
  514. wps_build_resp_type(reg, probe) ||
  515. wps_build_uuid_e(probe, reg->wps->uuid) ||
  516. wps_build_device_attrs(&reg->wps->dev, probe) ||
  517. wps_build_probe_config_methods(reg, probe) ||
  518. wps_build_rf_bands(&reg->wps->dev, probe)) {
  519. wpabuf_free(beacon);
  520. wpabuf_free(probe);
  521. return -1;
  522. }
  523. beacon = wps_ie_encapsulate(beacon);
  524. probe = wps_ie_encapsulate(probe);
  525. if (!beacon || !probe) {
  526. wpabuf_free(beacon);
  527. wpabuf_free(probe);
  528. return -1;
  529. }
  530. ret = wps_cb_set_ie(reg, beacon, probe);
  531. wpabuf_free(beacon);
  532. wpabuf_free(probe);
  533. return ret;
  534. }
  535. static int wps_get_dev_password(struct wps_data *wps)
  536. {
  537. const u8 *pin;
  538. size_t pin_len;
  539. os_free(wps->dev_password);
  540. wps->dev_password = NULL;
  541. if (wps->pbc) {
  542. wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
  543. pin = (const u8 *) "00000000";
  544. pin_len = 8;
  545. } else {
  546. pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
  547. &pin_len);
  548. }
  549. if (pin == NULL) {
  550. wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
  551. "the Enrollee");
  552. wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
  553. return -1;
  554. }
  555. wps->dev_password = os_malloc(pin_len);
  556. if (wps->dev_password == NULL)
  557. return -1;
  558. os_memcpy(wps->dev_password, pin, pin_len);
  559. wps->dev_password_len = pin_len;
  560. return 0;
  561. }
  562. static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
  563. {
  564. wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
  565. wpabuf_put_be16(msg, ATTR_UUID_R);
  566. wpabuf_put_be16(msg, WPS_UUID_LEN);
  567. wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
  568. return 0;
  569. }
  570. static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
  571. {
  572. u8 *hash;
  573. const u8 *addr[4];
  574. size_t len[4];
  575. if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
  576. return -1;
  577. wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
  578. wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
  579. wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
  580. if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
  581. wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
  582. "R-Hash derivation");
  583. return -1;
  584. }
  585. wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
  586. wpabuf_put_be16(msg, ATTR_R_HASH1);
  587. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  588. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  589. /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
  590. addr[0] = wps->snonce;
  591. len[0] = WPS_SECRET_NONCE_LEN;
  592. addr[1] = wps->psk1;
  593. len[1] = WPS_PSK_LEN;
  594. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  595. len[2] = wpabuf_len(wps->dh_pubkey_e);
  596. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  597. len[3] = wpabuf_len(wps->dh_pubkey_r);
  598. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  599. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
  600. wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
  601. wpabuf_put_be16(msg, ATTR_R_HASH2);
  602. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  603. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  604. /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
  605. addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
  606. addr[1] = wps->psk2;
  607. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  608. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
  609. return 0;
  610. }
  611. static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
  612. {
  613. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
  614. wpabuf_put_be16(msg, ATTR_R_SNONCE1);
  615. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  616. wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
  617. return 0;
  618. }
  619. static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
  620. {
  621. wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
  622. wpabuf_put_be16(msg, ATTR_R_SNONCE2);
  623. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  624. wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
  625. WPS_SECRET_NONCE_LEN);
  626. return 0;
  627. }
  628. static int wps_build_cred_network_idx(struct wpabuf *msg,
  629. struct wps_credential *cred)
  630. {
  631. wpa_printf(MSG_DEBUG, "WPS: * Network Index");
  632. wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
  633. wpabuf_put_be16(msg, 1);
  634. wpabuf_put_u8(msg, 0);
  635. return 0;
  636. }
  637. static int wps_build_cred_ssid(struct wpabuf *msg,
  638. struct wps_credential *cred)
  639. {
  640. wpa_printf(MSG_DEBUG, "WPS: * SSID");
  641. wpabuf_put_be16(msg, ATTR_SSID);
  642. wpabuf_put_be16(msg, cred->ssid_len);
  643. wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
  644. return 0;
  645. }
  646. static int wps_build_cred_auth_type(struct wpabuf *msg,
  647. struct wps_credential *cred)
  648. {
  649. wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
  650. cred->auth_type);
  651. wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
  652. wpabuf_put_be16(msg, 2);
  653. wpabuf_put_be16(msg, cred->auth_type);
  654. return 0;
  655. }
  656. static int wps_build_cred_encr_type(struct wpabuf *msg,
  657. struct wps_credential *cred)
  658. {
  659. wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
  660. cred->encr_type);
  661. wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
  662. wpabuf_put_be16(msg, 2);
  663. wpabuf_put_be16(msg, cred->encr_type);
  664. return 0;
  665. }
  666. static int wps_build_cred_network_key(struct wpabuf *msg,
  667. struct wps_credential *cred)
  668. {
  669. wpa_printf(MSG_DEBUG, "WPS: * Network Key");
  670. wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
  671. wpabuf_put_be16(msg, cred->key_len);
  672. wpabuf_put_data(msg, cred->key, cred->key_len);
  673. return 0;
  674. }
  675. static int wps_build_cred_mac_addr(struct wpabuf *msg,
  676. struct wps_credential *cred)
  677. {
  678. wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
  679. wpabuf_put_be16(msg, ATTR_MAC_ADDR);
  680. wpabuf_put_be16(msg, ETH_ALEN);
  681. wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
  682. return 0;
  683. }
  684. static int wps_build_credential(struct wpabuf *msg,
  685. struct wps_credential *cred)
  686. {
  687. if (wps_build_cred_network_idx(msg, cred) ||
  688. wps_build_cred_ssid(msg, cred) ||
  689. wps_build_cred_auth_type(msg, cred) ||
  690. wps_build_cred_encr_type(msg, cred) ||
  691. wps_build_cred_network_key(msg, cred) ||
  692. wps_build_cred_mac_addr(msg, cred))
  693. return -1;
  694. return 0;
  695. }
  696. static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
  697. {
  698. struct wpabuf *cred;
  699. wpa_printf(MSG_DEBUG, "WPS: * Credential");
  700. os_memset(&wps->cred, 0, sizeof(wps->cred));
  701. os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
  702. wps->cred.ssid_len = wps->wps->ssid_len;
  703. /* Select the best authentication and encryption type */
  704. if (wps->auth_type & WPS_AUTH_WPA2PSK)
  705. wps->auth_type = WPS_AUTH_WPA2PSK;
  706. else if (wps->auth_type & WPS_AUTH_WPAPSK)
  707. wps->auth_type = WPS_AUTH_WPAPSK;
  708. else if (wps->auth_type & WPS_AUTH_OPEN)
  709. wps->auth_type = WPS_AUTH_OPEN;
  710. else if (wps->auth_type & WPS_AUTH_SHARED)
  711. wps->auth_type = WPS_AUTH_SHARED;
  712. else {
  713. wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
  714. wps->auth_type);
  715. return -1;
  716. }
  717. wps->cred.auth_type = wps->auth_type;
  718. if (wps->auth_type == WPS_AUTH_WPA2PSK ||
  719. wps->auth_type == WPS_AUTH_WPAPSK) {
  720. if (wps->encr_type & WPS_ENCR_AES)
  721. wps->encr_type = WPS_ENCR_AES;
  722. else if (wps->encr_type & WPS_ENCR_TKIP)
  723. wps->encr_type = WPS_ENCR_TKIP;
  724. else {
  725. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  726. "type for WPA/WPA2");
  727. return -1;
  728. }
  729. } else {
  730. if (wps->encr_type & WPS_ENCR_WEP)
  731. wps->encr_type = WPS_ENCR_WEP;
  732. else if (wps->encr_type & WPS_ENCR_NONE)
  733. wps->encr_type = WPS_ENCR_NONE;
  734. else {
  735. wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
  736. "type for non-WPA/WPA2 mode");
  737. return -1;
  738. }
  739. }
  740. wps->cred.encr_type = wps->encr_type;
  741. os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
  742. if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
  743. u8 r[16];
  744. /* Generate a random passphrase */
  745. if (os_get_random(r, sizeof(r)) < 0)
  746. return -1;
  747. os_free(wps->new_psk);
  748. wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
  749. if (wps->new_psk == NULL)
  750. return -1;
  751. wps->new_psk_len--; /* remove newline */
  752. while (wps->new_psk_len &&
  753. wps->new_psk[wps->new_psk_len - 1] == '=')
  754. wps->new_psk_len--;
  755. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
  756. wps->new_psk, wps->new_psk_len);
  757. os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
  758. wps->cred.key_len = wps->new_psk_len;
  759. } else if (wps->wps->network_key) {
  760. os_memcpy(wps->cred.key, wps->wps->network_key,
  761. wps->wps->network_key_len);
  762. wps->cred.key_len = wps->wps->network_key_len;
  763. } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
  764. char hex[65];
  765. /* Generate a random per-device PSK */
  766. os_free(wps->new_psk);
  767. wps->new_psk_len = 32;
  768. wps->new_psk = os_malloc(wps->new_psk_len);
  769. if (wps->new_psk == NULL)
  770. return -1;
  771. if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
  772. os_free(wps->new_psk);
  773. wps->new_psk = NULL;
  774. return -1;
  775. }
  776. wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
  777. wps->new_psk, wps->new_psk_len);
  778. wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
  779. wps->new_psk_len);
  780. os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
  781. wps->cred.key_len = wps->new_psk_len * 2;
  782. }
  783. cred = wpabuf_alloc(200);
  784. if (cred == NULL)
  785. return -1;
  786. if (wps_build_credential(cred, &wps->cred)) {
  787. wpabuf_free(cred);
  788. return -1;
  789. }
  790. wpabuf_put_be16(msg, ATTR_CRED);
  791. wpabuf_put_be16(msg, wpabuf_len(cred));
  792. wpabuf_put_buf(msg, cred);
  793. wpabuf_free(cred);
  794. return 0;
  795. }
  796. static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
  797. {
  798. wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
  799. if (wps_build_credential(msg, &wps->cred))
  800. return -1;
  801. return 0;
  802. }
  803. static struct wpabuf * wps_build_m2(struct wps_data *wps)
  804. {
  805. struct wpabuf *msg;
  806. if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
  807. return NULL;
  808. wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
  809. wps->nonce_r, WPS_NONCE_LEN);
  810. os_memcpy(wps->uuid_r, wps->wps->uuid, WPS_UUID_LEN);
  811. wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
  812. wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
  813. msg = wpabuf_alloc(1000);
  814. if (msg == NULL)
  815. return NULL;
  816. if (wps_build_version(msg) ||
  817. wps_build_msg_type(msg, WPS_M2) ||
  818. wps_build_enrollee_nonce(wps, msg) ||
  819. wps_build_registrar_nonce(wps, msg) ||
  820. wps_build_uuid_r(wps, msg) ||
  821. wps_build_public_key(wps, msg) ||
  822. wps_derive_keys(wps) ||
  823. wps_build_auth_type_flags(wps, msg) ||
  824. wps_build_encr_type_flags(wps, msg) ||
  825. wps_build_conn_type_flags(wps, msg) ||
  826. wps_build_config_methods_r(wps->registrar, msg) ||
  827. wps_build_device_attrs(&wps->wps->dev, msg) ||
  828. wps_build_rf_bands(&wps->wps->dev, msg) ||
  829. wps_build_assoc_state(wps, msg) ||
  830. wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  831. wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
  832. wps_build_os_version(&wps->wps->dev, msg) ||
  833. wps_build_authenticator(wps, msg)) {
  834. wpabuf_free(msg);
  835. return NULL;
  836. }
  837. wps->state = RECV_M3;
  838. return msg;
  839. }
  840. static struct wpabuf * wps_build_m2d(struct wps_data *wps)
  841. {
  842. struct wpabuf *msg;
  843. u16 err = WPS_CFG_NO_ERROR;
  844. wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
  845. msg = wpabuf_alloc(1000);
  846. if (msg == NULL)
  847. return NULL;
  848. if (wps->authenticator && wps->wps->ap_setup_locked)
  849. err = WPS_CFG_SETUP_LOCKED;
  850. if (wps_build_version(msg) ||
  851. wps_build_msg_type(msg, WPS_M2D) ||
  852. wps_build_enrollee_nonce(wps, msg) ||
  853. wps_build_registrar_nonce(wps, msg) ||
  854. wps_build_uuid_r(wps, msg) ||
  855. wps_build_auth_type_flags(wps, msg) ||
  856. wps_build_encr_type_flags(wps, msg) ||
  857. wps_build_conn_type_flags(wps, msg) ||
  858. wps_build_config_methods_r(wps->registrar, msg) ||
  859. wps_build_device_attrs(&wps->wps->dev, msg) ||
  860. wps_build_rf_bands(&wps->wps->dev, msg) ||
  861. wps_build_assoc_state(wps, msg) ||
  862. wps_build_config_error(msg, err) ||
  863. wps_build_os_version(&wps->wps->dev, msg)) {
  864. wpabuf_free(msg);
  865. return NULL;
  866. }
  867. wps->state = RECV_M2D_ACK;
  868. return msg;
  869. }
  870. static struct wpabuf * wps_build_m4(struct wps_data *wps)
  871. {
  872. struct wpabuf *msg, *plain;
  873. wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
  874. wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  875. plain = wpabuf_alloc(200);
  876. if (plain == NULL)
  877. return NULL;
  878. msg = wpabuf_alloc(1000);
  879. if (msg == NULL) {
  880. wpabuf_free(plain);
  881. return NULL;
  882. }
  883. if (wps_build_version(msg) ||
  884. wps_build_msg_type(msg, WPS_M4) ||
  885. wps_build_enrollee_nonce(wps, msg) ||
  886. wps_build_r_hash(wps, msg) ||
  887. wps_build_r_snonce1(wps, plain) ||
  888. wps_build_key_wrap_auth(wps, plain) ||
  889. wps_build_encr_settings(wps, msg, plain) ||
  890. wps_build_authenticator(wps, msg)) {
  891. wpabuf_free(plain);
  892. wpabuf_free(msg);
  893. return NULL;
  894. }
  895. wpabuf_free(plain);
  896. wps->state = RECV_M5;
  897. return msg;
  898. }
  899. static struct wpabuf * wps_build_m6(struct wps_data *wps)
  900. {
  901. struct wpabuf *msg, *plain;
  902. wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
  903. plain = wpabuf_alloc(200);
  904. if (plain == NULL)
  905. return NULL;
  906. msg = wpabuf_alloc(1000);
  907. if (msg == NULL) {
  908. wpabuf_free(plain);
  909. return NULL;
  910. }
  911. if (wps_build_version(msg) ||
  912. wps_build_msg_type(msg, WPS_M6) ||
  913. wps_build_enrollee_nonce(wps, msg) ||
  914. wps_build_r_snonce2(wps, plain) ||
  915. wps_build_key_wrap_auth(wps, plain) ||
  916. wps_build_encr_settings(wps, msg, plain) ||
  917. wps_build_authenticator(wps, msg)) {
  918. wpabuf_free(plain);
  919. wpabuf_free(msg);
  920. return NULL;
  921. }
  922. wpabuf_free(plain);
  923. wps->wps_pin_revealed = 1;
  924. wps->state = RECV_M7;
  925. return msg;
  926. }
  927. static struct wpabuf * wps_build_m8(struct wps_data *wps)
  928. {
  929. struct wpabuf *msg, *plain;
  930. wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
  931. plain = wpabuf_alloc(500);
  932. if (plain == NULL)
  933. return NULL;
  934. msg = wpabuf_alloc(1000);
  935. if (msg == NULL) {
  936. wpabuf_free(plain);
  937. return NULL;
  938. }
  939. if (wps_build_version(msg) ||
  940. wps_build_msg_type(msg, WPS_M8) ||
  941. wps_build_enrollee_nonce(wps, msg) ||
  942. (wps->wps->ap && wps_build_cred(wps, plain)) ||
  943. (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
  944. wps_build_key_wrap_auth(wps, plain) ||
  945. wps_build_encr_settings(wps, msg, plain) ||
  946. wps_build_authenticator(wps, msg)) {
  947. wpabuf_free(plain);
  948. wpabuf_free(msg);
  949. return NULL;
  950. }
  951. wpabuf_free(plain);
  952. wps->state = RECV_DONE;
  953. return msg;
  954. }
  955. static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
  956. {
  957. struct wpabuf *msg;
  958. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
  959. msg = wpabuf_alloc(1000);
  960. if (msg == NULL)
  961. return NULL;
  962. if (wps_build_version(msg) ||
  963. wps_build_msg_type(msg, WPS_WSC_ACK) ||
  964. wps_build_enrollee_nonce(wps, msg) ||
  965. wps_build_registrar_nonce(wps, msg)) {
  966. wpabuf_free(msg);
  967. return NULL;
  968. }
  969. return msg;
  970. }
  971. struct wpabuf * wps_registrar_get_msg(struct wps_data *wps, u8 *op_code)
  972. {
  973. struct wpabuf *msg;
  974. switch (wps->state) {
  975. case SEND_M2:
  976. if (wps_get_dev_password(wps) < 0)
  977. msg = wps_build_m2d(wps);
  978. else
  979. msg = wps_build_m2(wps);
  980. *op_code = WSC_MSG;
  981. break;
  982. case SEND_M2D:
  983. msg = wps_build_m2d(wps);
  984. *op_code = WSC_MSG;
  985. break;
  986. case SEND_M4:
  987. msg = wps_build_m4(wps);
  988. *op_code = WSC_MSG;
  989. break;
  990. case SEND_M6:
  991. msg = wps_build_m6(wps);
  992. *op_code = WSC_MSG;
  993. break;
  994. case SEND_M8:
  995. msg = wps_build_m8(wps);
  996. *op_code = WSC_MSG;
  997. break;
  998. case RECV_DONE:
  999. msg = wps_build_wsc_ack(wps);
  1000. *op_code = WSC_ACK;
  1001. break;
  1002. default:
  1003. wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
  1004. "a message", wps->state);
  1005. msg = NULL;
  1006. break;
  1007. }
  1008. if (*op_code == WSC_MSG && msg) {
  1009. /* Save a copy of the last message for Authenticator derivation
  1010. */
  1011. wpabuf_free(wps->last_msg);
  1012. wps->last_msg = wpabuf_dup(msg);
  1013. }
  1014. return msg;
  1015. }
  1016. static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
  1017. {
  1018. if (e_nonce == NULL) {
  1019. wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
  1020. return -1;
  1021. }
  1022. os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
  1023. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
  1024. wps->nonce_e, WPS_NONCE_LEN);
  1025. return 0;
  1026. }
  1027. static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
  1028. {
  1029. if (r_nonce == NULL) {
  1030. wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
  1031. return -1;
  1032. }
  1033. if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
  1034. wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
  1035. return -1;
  1036. }
  1037. return 0;
  1038. }
  1039. static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
  1040. {
  1041. if (uuid_e == NULL) {
  1042. wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
  1043. return -1;
  1044. }
  1045. os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
  1046. wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
  1047. return 0;
  1048. }
  1049. static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
  1050. {
  1051. if (pw_id == NULL) {
  1052. wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
  1053. return -1;
  1054. }
  1055. wps->dev_pw_id = WPA_GET_BE16(pw_id);
  1056. wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
  1057. return 0;
  1058. }
  1059. static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
  1060. {
  1061. if (e_hash1 == NULL) {
  1062. wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
  1063. return -1;
  1064. }
  1065. os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
  1066. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
  1067. return 0;
  1068. }
  1069. static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
  1070. {
  1071. if (e_hash2 == NULL) {
  1072. wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
  1073. return -1;
  1074. }
  1075. os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
  1076. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
  1077. return 0;
  1078. }
  1079. static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
  1080. {
  1081. u8 hash[SHA256_MAC_LEN];
  1082. const u8 *addr[4];
  1083. size_t len[4];
  1084. if (e_snonce1 == NULL) {
  1085. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
  1086. return -1;
  1087. }
  1088. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
  1089. WPS_SECRET_NONCE_LEN);
  1090. /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
  1091. addr[0] = e_snonce1;
  1092. len[0] = WPS_SECRET_NONCE_LEN;
  1093. addr[1] = wps->psk1;
  1094. len[1] = WPS_PSK_LEN;
  1095. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1096. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1097. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1098. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1099. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1100. if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
  1101. wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
  1102. "not match with the pre-committed value");
  1103. return -1;
  1104. }
  1105. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
  1106. "half of the device password");
  1107. return 0;
  1108. }
  1109. static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
  1110. {
  1111. u8 hash[SHA256_MAC_LEN];
  1112. const u8 *addr[4];
  1113. size_t len[4];
  1114. if (e_snonce2 == NULL) {
  1115. wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
  1116. return -1;
  1117. }
  1118. wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
  1119. WPS_SECRET_NONCE_LEN);
  1120. /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
  1121. addr[0] = e_snonce2;
  1122. len[0] = WPS_SECRET_NONCE_LEN;
  1123. addr[1] = wps->psk2;
  1124. len[1] = WPS_PSK_LEN;
  1125. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  1126. len[2] = wpabuf_len(wps->dh_pubkey_e);
  1127. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  1128. len[3] = wpabuf_len(wps->dh_pubkey_r);
  1129. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  1130. if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
  1131. wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
  1132. "not match with the pre-committed value");
  1133. wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
  1134. return -1;
  1135. }
  1136. wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
  1137. "half of the device password");
  1138. wps->wps_pin_revealed = 0;
  1139. wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
  1140. return 0;
  1141. }
  1142. static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
  1143. {
  1144. if (mac_addr == NULL) {
  1145. wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
  1146. return -1;
  1147. }
  1148. wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
  1149. MAC2STR(mac_addr));
  1150. os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
  1151. os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
  1152. return 0;
  1153. }
  1154. static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
  1155. size_t pk_len)
  1156. {
  1157. if (pk == NULL || pk_len == 0) {
  1158. wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
  1159. return -1;
  1160. }
  1161. wpabuf_free(wps->dh_pubkey_e);
  1162. wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
  1163. if (wps->dh_pubkey_e == NULL)
  1164. return -1;
  1165. return 0;
  1166. }
  1167. static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
  1168. {
  1169. u16 auth_types;
  1170. if (auth == NULL) {
  1171. wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
  1172. "received");
  1173. return -1;
  1174. }
  1175. auth_types = WPA_GET_BE16(auth);
  1176. wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
  1177. auth_types);
  1178. wps->auth_type = wps->wps->auth_types & auth_types;
  1179. if (wps->auth_type == 0) {
  1180. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  1181. "authentication types (own 0x%x Enrollee 0x%x)",
  1182. wps->wps->auth_types, auth_types);
  1183. return -1;
  1184. }
  1185. return 0;
  1186. }
  1187. static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
  1188. {
  1189. u16 encr_types;
  1190. if (encr == NULL) {
  1191. wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
  1192. "received");
  1193. return -1;
  1194. }
  1195. encr_types = WPA_GET_BE16(encr);
  1196. wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
  1197. encr_types);
  1198. wps->encr_type = wps->wps->encr_types & encr_types;
  1199. if (wps->encr_type == 0) {
  1200. wpa_printf(MSG_DEBUG, "WPS: No match in supported "
  1201. "encryption types");
  1202. return -1;
  1203. }
  1204. return 0;
  1205. }
  1206. static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
  1207. {
  1208. if (conn == NULL) {
  1209. wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
  1210. "received");
  1211. return -1;
  1212. }
  1213. wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
  1214. *conn);
  1215. return 0;
  1216. }
  1217. static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
  1218. {
  1219. u16 m;
  1220. if (methods == NULL) {
  1221. wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
  1222. return -1;
  1223. }
  1224. m = WPA_GET_BE16(methods);
  1225. wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
  1226. return 0;
  1227. }
  1228. static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
  1229. {
  1230. if (state == NULL) {
  1231. wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
  1232. "received");
  1233. return -1;
  1234. }
  1235. wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
  1236. *state);
  1237. return 0;
  1238. }
  1239. static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
  1240. {
  1241. u16 a;
  1242. if (assoc == NULL) {
  1243. wpa_printf(MSG_DEBUG, "WPS: No Association State received");
  1244. return -1;
  1245. }
  1246. a = WPA_GET_BE16(assoc);
  1247. wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
  1248. return 0;
  1249. }
  1250. static int wps_process_config_error(struct wps_data *wps, const u8 *err)
  1251. {
  1252. u16 e;
  1253. if (err == NULL) {
  1254. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
  1255. return -1;
  1256. }
  1257. e = WPA_GET_BE16(err);
  1258. wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
  1259. return 0;
  1260. }
  1261. static enum wps_process_res wps_process_m1(struct wps_data *wps,
  1262. struct wps_parse_attr *attr)
  1263. {
  1264. wpa_printf(MSG_DEBUG, "WPS: Received M1");
  1265. if (wps->state != RECV_M1) {
  1266. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1267. "receiving M1", wps->state);
  1268. return WPS_FAILURE;
  1269. }
  1270. if (wps_process_uuid_e(wps, attr->uuid_e) ||
  1271. wps_process_mac_addr(wps, attr->mac_addr) ||
  1272. wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  1273. wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
  1274. wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
  1275. wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
  1276. wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
  1277. wps_process_config_methods(wps, attr->config_methods) ||
  1278. wps_process_wps_state(wps, attr->wps_state) ||
  1279. wps_process_device_attrs(&wps->peer_dev, attr) ||
  1280. wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
  1281. wps_process_assoc_state(wps, attr->assoc_state) ||
  1282. wps_process_dev_password_id(wps, attr->dev_password_id) ||
  1283. wps_process_config_error(wps, attr->config_error) ||
  1284. wps_process_os_version(&wps->peer_dev, attr->os_version))
  1285. return WPS_FAILURE;
  1286. if (wps->dev_pw_id != DEV_PW_DEFAULT &&
  1287. wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
  1288. wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
  1289. wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
  1290. (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
  1291. wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
  1292. wps->dev_pw_id);
  1293. wps->state = SEND_M2D;
  1294. return WPS_CONTINUE;
  1295. }
  1296. if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
  1297. if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
  1298. wps->uuid_e)) {
  1299. wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
  1300. "negotiation");
  1301. wps->state = SEND_M2D;
  1302. return WPS_CONTINUE;
  1303. }
  1304. wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
  1305. wps->uuid_e);
  1306. wps->pbc = 1;
  1307. }
  1308. wps->state = SEND_M2;
  1309. return WPS_CONTINUE;
  1310. }
  1311. static enum wps_process_res wps_process_m3(struct wps_data *wps,
  1312. const struct wpabuf *msg,
  1313. struct wps_parse_attr *attr)
  1314. {
  1315. wpa_printf(MSG_DEBUG, "WPS: Received M3");
  1316. if (wps->state != RECV_M3) {
  1317. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1318. "receiving M3", wps->state);
  1319. return WPS_FAILURE;
  1320. }
  1321. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1322. wps_process_authenticator(wps, attr->authenticator, msg) ||
  1323. wps_process_e_hash1(wps, attr->e_hash1) ||
  1324. wps_process_e_hash2(wps, attr->e_hash2))
  1325. return WPS_FAILURE;
  1326. wps->state = SEND_M4;
  1327. return WPS_CONTINUE;
  1328. }
  1329. static enum wps_process_res wps_process_m5(struct wps_data *wps,
  1330. const struct wpabuf *msg,
  1331. struct wps_parse_attr *attr)
  1332. {
  1333. struct wpabuf *decrypted;
  1334. struct wps_parse_attr eattr;
  1335. wpa_printf(MSG_DEBUG, "WPS: Received M5");
  1336. if (wps->state != RECV_M5) {
  1337. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1338. "receiving M5", wps->state);
  1339. return WPS_FAILURE;
  1340. }
  1341. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1342. wps_process_authenticator(wps, attr->authenticator, msg))
  1343. return WPS_FAILURE;
  1344. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  1345. attr->encr_settings_len);
  1346. if (decrypted == NULL) {
  1347. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  1348. "Settings attribute");
  1349. return WPS_FAILURE;
  1350. }
  1351. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  1352. "attribute");
  1353. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  1354. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  1355. wps_process_e_snonce1(wps, eattr.e_snonce1)) {
  1356. wpabuf_free(decrypted);
  1357. return WPS_FAILURE;
  1358. }
  1359. wpabuf_free(decrypted);
  1360. wps->state = SEND_M6;
  1361. return WPS_CONTINUE;
  1362. }
  1363. static int wps_process_ap_settings_r(struct wps_data *wps,
  1364. struct wps_parse_attr *attr)
  1365. {
  1366. if (wps->wps->ap)
  1367. return 0;
  1368. /* AP Settings Attributes in M7 when Enrollee is an AP */
  1369. if (wps_process_ap_settings(attr, &wps->cred) < 0)
  1370. return -1;
  1371. wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
  1372. /*
  1373. * TODO: Provide access to AP settings and allow changes before sending
  1374. * out M8. For now, just copy the settings unchanged into M8.
  1375. */
  1376. return 0;
  1377. }
  1378. static enum wps_process_res wps_process_m7(struct wps_data *wps,
  1379. const struct wpabuf *msg,
  1380. struct wps_parse_attr *attr)
  1381. {
  1382. struct wpabuf *decrypted;
  1383. struct wps_parse_attr eattr;
  1384. wpa_printf(MSG_DEBUG, "WPS: Received M7");
  1385. if (wps->state != RECV_M7) {
  1386. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1387. "receiving M7", wps->state);
  1388. return WPS_FAILURE;
  1389. }
  1390. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  1391. wps_process_authenticator(wps, attr->authenticator, msg))
  1392. return WPS_FAILURE;
  1393. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  1394. attr->encr_settings_len);
  1395. if (decrypted == NULL) {
  1396. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  1397. "Settings attribute");
  1398. return WPS_FAILURE;
  1399. }
  1400. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  1401. "attribute");
  1402. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  1403. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  1404. wps_process_e_snonce2(wps, eattr.e_snonce2) ||
  1405. wps_process_ap_settings_r(wps, &eattr)) {
  1406. wpabuf_free(decrypted);
  1407. return WPS_FAILURE;
  1408. }
  1409. wpabuf_free(decrypted);
  1410. wps->state = SEND_M8;
  1411. return WPS_CONTINUE;
  1412. }
  1413. static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
  1414. const struct wpabuf *msg)
  1415. {
  1416. struct wps_parse_attr attr;
  1417. enum wps_process_res ret = WPS_CONTINUE;
  1418. wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
  1419. if (wps_parse_msg(msg, &attr) < 0)
  1420. return WPS_FAILURE;
  1421. if (attr.version == NULL || *attr.version != WPS_VERSION) {
  1422. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1423. attr.version ? *attr.version : 0);
  1424. return WPS_FAILURE;
  1425. }
  1426. if (attr.msg_type == NULL) {
  1427. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1428. return WPS_FAILURE;
  1429. }
  1430. if (*attr.msg_type != WPS_M1 &&
  1431. (attr.registrar_nonce == NULL ||
  1432. os_memcmp(wps->nonce_r, attr.registrar_nonce,
  1433. WPS_NONCE_LEN != 0))) {
  1434. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1435. return WPS_FAILURE;
  1436. }
  1437. switch (*attr.msg_type) {
  1438. case WPS_M1:
  1439. ret = wps_process_m1(wps, &attr);
  1440. break;
  1441. case WPS_M3:
  1442. ret = wps_process_m3(wps, msg, &attr);
  1443. break;
  1444. case WPS_M5:
  1445. ret = wps_process_m5(wps, msg, &attr);
  1446. break;
  1447. case WPS_M7:
  1448. ret = wps_process_m7(wps, msg, &attr);
  1449. break;
  1450. default:
  1451. wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
  1452. *attr.msg_type);
  1453. return WPS_FAILURE;
  1454. }
  1455. if (ret == WPS_CONTINUE) {
  1456. /* Save a copy of the last message for Authenticator derivation
  1457. */
  1458. wpabuf_free(wps->last_msg);
  1459. wps->last_msg = wpabuf_dup(msg);
  1460. }
  1461. return ret;
  1462. }
  1463. static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
  1464. const struct wpabuf *msg)
  1465. {
  1466. struct wps_parse_attr attr;
  1467. wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
  1468. if (wps_parse_msg(msg, &attr) < 0)
  1469. return WPS_FAILURE;
  1470. if (attr.version == NULL || *attr.version != WPS_VERSION) {
  1471. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1472. attr.version ? *attr.version : 0);
  1473. return WPS_FAILURE;
  1474. }
  1475. if (attr.msg_type == NULL) {
  1476. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1477. return WPS_FAILURE;
  1478. }
  1479. if (*attr.msg_type != WPS_WSC_ACK) {
  1480. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1481. *attr.msg_type);
  1482. return WPS_FAILURE;
  1483. }
  1484. if (attr.registrar_nonce == NULL ||
  1485. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1486. {
  1487. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1488. return WPS_FAILURE;
  1489. }
  1490. if (attr.enrollee_nonce == NULL ||
  1491. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1492. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1493. return WPS_FAILURE;
  1494. }
  1495. if (wps->state == RECV_M2D_ACK) {
  1496. /* TODO: support for multiple registrars and sending of
  1497. * multiple M2/M2D messages */
  1498. wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
  1499. "terminate negotiation");
  1500. }
  1501. return WPS_FAILURE;
  1502. }
  1503. static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
  1504. const struct wpabuf *msg)
  1505. {
  1506. struct wps_parse_attr attr;
  1507. wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
  1508. if (wps_parse_msg(msg, &attr) < 0)
  1509. return WPS_FAILURE;
  1510. if (attr.version == NULL || *attr.version != WPS_VERSION) {
  1511. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1512. attr.version ? *attr.version : 0);
  1513. return WPS_FAILURE;
  1514. }
  1515. if (attr.msg_type == NULL) {
  1516. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1517. return WPS_FAILURE;
  1518. }
  1519. if (*attr.msg_type != WPS_WSC_NACK) {
  1520. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1521. *attr.msg_type);
  1522. return WPS_FAILURE;
  1523. }
  1524. if (attr.registrar_nonce == NULL ||
  1525. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1526. {
  1527. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1528. return WPS_FAILURE;
  1529. }
  1530. if (attr.enrollee_nonce == NULL ||
  1531. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1532. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1533. return WPS_FAILURE;
  1534. }
  1535. if (attr.config_error == NULL) {
  1536. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
  1537. "in WSC_NACK");
  1538. return WPS_FAILURE;
  1539. }
  1540. wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
  1541. "Configuration Error %d", WPA_GET_BE16(attr.config_error));
  1542. return WPS_FAILURE;
  1543. }
  1544. static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
  1545. const struct wpabuf *msg)
  1546. {
  1547. struct wps_parse_attr attr;
  1548. wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
  1549. if (wps->state != RECV_DONE) {
  1550. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  1551. "receiving WSC_Done", wps->state);
  1552. return WPS_FAILURE;
  1553. }
  1554. if (wps_parse_msg(msg, &attr) < 0)
  1555. return WPS_FAILURE;
  1556. if (attr.version == NULL || *attr.version != WPS_VERSION) {
  1557. wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
  1558. attr.version ? *attr.version : 0);
  1559. return WPS_FAILURE;
  1560. }
  1561. if (attr.msg_type == NULL) {
  1562. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1563. return WPS_FAILURE;
  1564. }
  1565. if (*attr.msg_type != WPS_WSC_DONE) {
  1566. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1567. *attr.msg_type);
  1568. return WPS_FAILURE;
  1569. }
  1570. if (attr.registrar_nonce == NULL ||
  1571. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1572. {
  1573. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1574. return WPS_FAILURE;
  1575. }
  1576. if (attr.enrollee_nonce == NULL ||
  1577. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1578. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1579. return WPS_FAILURE;
  1580. }
  1581. wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
  1582. if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
  1583. wps->wps->ap) {
  1584. struct wps_credential cred;
  1585. wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
  1586. "on first Enrollee connection");
  1587. os_memset(&cred, 0, sizeof(cred));
  1588. os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
  1589. cred.ssid_len = wps->wps->ssid_len;
  1590. cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
  1591. cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
  1592. os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
  1593. cred.key_len = wps->new_psk_len;
  1594. wps->wps->wps_state = WPS_STATE_CONFIGURED;
  1595. wpa_hexdump_ascii_key(MSG_DEBUG,
  1596. "WPS: Generated random passphrase",
  1597. wps->new_psk, wps->new_psk_len);
  1598. if (wps->wps->cred_cb)
  1599. wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
  1600. os_free(wps->new_psk);
  1601. wps->new_psk = NULL;
  1602. }
  1603. if (!wps->wps->ap) {
  1604. wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
  1605. "on the modified AP configuration");
  1606. if (wps->wps->cred_cb)
  1607. wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
  1608. }
  1609. if (wps->new_psk) {
  1610. if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
  1611. wps->new_psk, wps->new_psk_len)) {
  1612. wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
  1613. "new PSK");
  1614. }
  1615. os_free(wps->new_psk);
  1616. wps->new_psk = NULL;
  1617. }
  1618. if (wps->pbc) {
  1619. wps_registrar_remove_pbc_session(wps->registrar,
  1620. wps->mac_addr_e, wps->uuid_e);
  1621. wps_registrar_pbc_completed(wps->registrar);
  1622. }
  1623. return WPS_DONE;
  1624. }
  1625. enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
  1626. u8 op_code,
  1627. const struct wpabuf *msg)
  1628. {
  1629. wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
  1630. "op_code=%d)",
  1631. (unsigned long) wpabuf_len(msg), op_code);
  1632. switch (op_code) {
  1633. case WSC_MSG:
  1634. return wps_process_wsc_msg(wps, msg);
  1635. case WSC_ACK:
  1636. return wps_process_wsc_ack(wps, msg);
  1637. case WSC_NACK:
  1638. return wps_process_wsc_nack(wps, msg);
  1639. case WSC_Done:
  1640. return wps_process_wsc_done(wps, msg);
  1641. default:
  1642. wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
  1643. return WPS_FAILURE;
  1644. }
  1645. }