dpp_supplicant.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /*
  2. * wpa_supplicant - DPP
  3. * Copyright (c) 2017, Qualcomm Atheros, Inc.
  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/eloop.h"
  11. #include "common/dpp.h"
  12. #include "common/gas.h"
  13. #include "common/gas_server.h"
  14. #include "rsn_supp/wpa.h"
  15. #include "rsn_supp/pmksa_cache.h"
  16. #include "wpa_supplicant_i.h"
  17. #include "config.h"
  18. #include "driver_i.h"
  19. #include "offchannel.h"
  20. #include "gas_query.h"
  21. #include "bss.h"
  22. #include "scan.h"
  23. #include "notify.h"
  24. #include "dpp_supplicant.h"
  25. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  26. unsigned int freq);
  27. static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx);
  28. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator);
  29. static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
  30. unsigned int freq, const u8 *dst,
  31. const u8 *src, const u8 *bssid,
  32. const u8 *data, size_t data_len,
  33. enum offchannel_send_action_result result);
  34. static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx);
  35. static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s);
  36. static void
  37. wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
  38. unsigned int freq, const u8 *dst,
  39. const u8 *src, const u8 *bssid,
  40. const u8 *data, size_t data_len,
  41. enum offchannel_send_action_result result);
  42. static const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  43. /* Use a hardcoded Transaction ID 1 in Peer Discovery frames since there is only
  44. * a single transaction in progress at any point in time. */
  45. static const u8 TRANSACTION_ID = 1;
  46. static struct dpp_configurator *
  47. dpp_configurator_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  48. {
  49. struct dpp_configurator *conf;
  50. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  51. struct dpp_configurator, list) {
  52. if (conf->id == id)
  53. return conf;
  54. }
  55. return NULL;
  56. }
  57. static unsigned int wpas_dpp_next_id(struct wpa_supplicant *wpa_s)
  58. {
  59. struct dpp_bootstrap_info *bi;
  60. unsigned int max_id = 0;
  61. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  62. list) {
  63. if (bi->id > max_id)
  64. max_id = bi->id;
  65. }
  66. return max_id + 1;
  67. }
  68. /**
  69. * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
  70. * @wpa_s: Pointer to wpa_supplicant data
  71. * @cmd: DPP URI read from a QR Code
  72. * Returns: Identifier of the stored info or -1 on failure
  73. */
  74. int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
  75. {
  76. struct dpp_bootstrap_info *bi;
  77. struct dpp_authentication *auth = wpa_s->dpp_auth;
  78. bi = dpp_parse_qr_code(cmd);
  79. if (!bi)
  80. return -1;
  81. bi->id = wpas_dpp_next_id(wpa_s);
  82. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  83. if (auth && auth->response_pending &&
  84. dpp_notify_new_qr_code(auth, bi) == 1) {
  85. wpa_printf(MSG_DEBUG,
  86. "DPP: Sending out pending authentication response");
  87. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  88. " freq=%u type=%d",
  89. MAC2STR(auth->peer_mac_addr), auth->curr_freq,
  90. DPP_PA_AUTHENTICATION_RESP);
  91. offchannel_send_action(wpa_s, auth->curr_freq,
  92. auth->peer_mac_addr, wpa_s->own_addr,
  93. broadcast,
  94. wpabuf_head(auth->resp_msg),
  95. wpabuf_len(auth->resp_msg),
  96. 500, wpas_dpp_tx_status, 0);
  97. }
  98. return bi->id;
  99. }
  100. static char * get_param(const char *cmd, const char *param)
  101. {
  102. const char *pos, *end;
  103. char *val;
  104. size_t len;
  105. pos = os_strstr(cmd, param);
  106. if (!pos)
  107. return NULL;
  108. pos += os_strlen(param);
  109. end = os_strchr(pos, ' ');
  110. if (end)
  111. len = end - pos;
  112. else
  113. len = os_strlen(pos);
  114. val = os_malloc(len + 1);
  115. if (!val)
  116. return NULL;
  117. os_memcpy(val, pos, len);
  118. val[len] = '\0';
  119. return val;
  120. }
  121. int wpas_dpp_bootstrap_gen(struct wpa_supplicant *wpa_s, const char *cmd)
  122. {
  123. char *chan = NULL, *mac = NULL, *info = NULL, *pk = NULL, *curve = NULL;
  124. char *key = NULL;
  125. u8 *privkey = NULL;
  126. size_t privkey_len = 0;
  127. size_t len;
  128. int ret = -1;
  129. struct dpp_bootstrap_info *bi;
  130. bi = os_zalloc(sizeof(*bi));
  131. if (!bi)
  132. goto fail;
  133. if (os_strstr(cmd, "type=qrcode"))
  134. bi->type = DPP_BOOTSTRAP_QR_CODE;
  135. else if (os_strstr(cmd, "type=pkex"))
  136. bi->type = DPP_BOOTSTRAP_PKEX;
  137. else
  138. goto fail;
  139. chan = get_param(cmd, " chan=");
  140. mac = get_param(cmd, " mac=");
  141. info = get_param(cmd, " info=");
  142. curve = get_param(cmd, " curve=");
  143. key = get_param(cmd, " key=");
  144. if (key) {
  145. privkey_len = os_strlen(key) / 2;
  146. privkey = os_malloc(privkey_len);
  147. if (!privkey ||
  148. hexstr2bin(key, privkey, privkey_len) < 0)
  149. goto fail;
  150. }
  151. pk = dpp_keygen(bi, curve, privkey, privkey_len);
  152. if (!pk)
  153. goto fail;
  154. len = 4; /* "DPP:" */
  155. if (chan) {
  156. if (dpp_parse_uri_chan_list(bi, chan) < 0)
  157. goto fail;
  158. len += 3 + os_strlen(chan); /* C:...; */
  159. }
  160. if (mac) {
  161. if (dpp_parse_uri_mac(bi, mac) < 0)
  162. goto fail;
  163. len += 3 + os_strlen(mac); /* M:...; */
  164. }
  165. if (info) {
  166. if (dpp_parse_uri_info(bi, info) < 0)
  167. goto fail;
  168. len += 3 + os_strlen(info); /* I:...; */
  169. }
  170. len += 4 + os_strlen(pk);
  171. bi->uri = os_malloc(len + 1);
  172. if (!bi->uri)
  173. goto fail;
  174. os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
  175. chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
  176. mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
  177. info ? "I:" : "", info ? info : "", info ? ";" : "",
  178. pk);
  179. bi->id = wpas_dpp_next_id(wpa_s);
  180. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  181. ret = bi->id;
  182. bi = NULL;
  183. fail:
  184. os_free(curve);
  185. os_free(pk);
  186. os_free(chan);
  187. os_free(mac);
  188. os_free(info);
  189. str_clear_free(key);
  190. bin_clear_free(privkey, privkey_len);
  191. dpp_bootstrap_info_free(bi);
  192. return ret;
  193. }
  194. static struct dpp_bootstrap_info *
  195. dpp_bootstrap_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  196. {
  197. struct dpp_bootstrap_info *bi;
  198. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  199. list) {
  200. if (bi->id == id)
  201. return bi;
  202. }
  203. return NULL;
  204. }
  205. static int dpp_bootstrap_del(struct wpa_supplicant *wpa_s, unsigned int id)
  206. {
  207. struct dpp_bootstrap_info *bi, *tmp;
  208. int found = 0;
  209. dl_list_for_each_safe(bi, tmp, &wpa_s->dpp_bootstrap,
  210. struct dpp_bootstrap_info, list) {
  211. if (id && bi->id != id)
  212. continue;
  213. found = 1;
  214. dl_list_del(&bi->list);
  215. dpp_bootstrap_info_free(bi);
  216. }
  217. if (id == 0)
  218. return 0; /* flush succeeds regardless of entries found */
  219. return found ? 0 : -1;
  220. }
  221. int wpas_dpp_bootstrap_remove(struct wpa_supplicant *wpa_s, const char *id)
  222. {
  223. unsigned int id_val;
  224. if (os_strcmp(id, "*") == 0) {
  225. id_val = 0;
  226. } else {
  227. id_val = atoi(id);
  228. if (id_val == 0)
  229. return -1;
  230. }
  231. return dpp_bootstrap_del(wpa_s, id_val);
  232. }
  233. const char * wpas_dpp_bootstrap_get_uri(struct wpa_supplicant *wpa_s,
  234. unsigned int id)
  235. {
  236. struct dpp_bootstrap_info *bi;
  237. bi = dpp_bootstrap_get_id(wpa_s, id);
  238. if (!bi)
  239. return NULL;
  240. return bi->uri;
  241. }
  242. int wpas_dpp_bootstrap_info(struct wpa_supplicant *wpa_s, int id,
  243. char *reply, int reply_size)
  244. {
  245. struct dpp_bootstrap_info *bi;
  246. bi = dpp_bootstrap_get_id(wpa_s, id);
  247. if (!bi)
  248. return -1;
  249. return os_snprintf(reply, reply_size, "type=%s\n"
  250. "mac_addr=" MACSTR "\n"
  251. "info=%s\n"
  252. "num_freq=%u\n"
  253. "curve=%s\n",
  254. dpp_bootstrap_type_txt(bi->type),
  255. MAC2STR(bi->mac_addr),
  256. bi->info ? bi->info : "",
  257. bi->num_freq,
  258. bi->curve->name);
  259. }
  260. static void wpas_dpp_auth_resp_retry_timeout(void *eloop_ctx, void *timeout_ctx)
  261. {
  262. struct wpa_supplicant *wpa_s = eloop_ctx;
  263. struct dpp_authentication *auth = wpa_s->dpp_auth;
  264. if (!auth || !auth->resp_msg)
  265. return;
  266. wpa_printf(MSG_DEBUG,
  267. "DPP: Retry Authentication Response after timeout");
  268. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  269. " freq=%u type=%d",
  270. MAC2STR(auth->peer_mac_addr), auth->curr_freq,
  271. DPP_PA_AUTHENTICATION_RESP);
  272. offchannel_send_action(wpa_s, auth->curr_freq, auth->peer_mac_addr,
  273. wpa_s->own_addr, broadcast,
  274. wpabuf_head(auth->resp_msg),
  275. wpabuf_len(auth->resp_msg),
  276. 500, wpas_dpp_tx_status, 0);
  277. }
  278. static void wpas_dpp_auth_resp_retry(struct wpa_supplicant *wpa_s)
  279. {
  280. struct dpp_authentication *auth = wpa_s->dpp_auth;
  281. unsigned int wait_time, max_tries;
  282. if (!auth || !auth->resp_msg)
  283. return;
  284. if (wpa_s->dpp_resp_max_tries)
  285. max_tries = wpa_s->dpp_resp_max_tries;
  286. else
  287. max_tries = 5;
  288. auth->auth_resp_tries++;
  289. if (auth->auth_resp_tries >= max_tries) {
  290. wpa_printf(MSG_INFO, "DPP: No confirm received from initiator - stopping exchange");
  291. offchannel_send_action_done(wpa_s);
  292. dpp_auth_deinit(wpa_s->dpp_auth);
  293. wpa_s->dpp_auth = NULL;
  294. return;
  295. }
  296. if (wpa_s->dpp_resp_retry_time)
  297. wait_time = wpa_s->dpp_resp_retry_time;
  298. else
  299. wait_time = 1000;
  300. wpa_printf(MSG_DEBUG,
  301. "DPP: Schedule retransmission of Authentication Response frame in %u ms",
  302. wait_time);
  303. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  304. eloop_register_timeout(wait_time / 1000,
  305. (wait_time % 1000) * 1000,
  306. wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  307. }
  308. static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
  309. unsigned int freq, const u8 *dst,
  310. const u8 *src, const u8 *bssid,
  311. const u8 *data, size_t data_len,
  312. enum offchannel_send_action_result result)
  313. {
  314. const char *res_txt;
  315. struct dpp_authentication *auth = wpa_s->dpp_auth;
  316. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  317. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  318. "FAILED");
  319. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  320. " result=%s", freq, MAC2STR(dst), res_txt);
  321. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  322. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  323. if (!wpa_s->dpp_auth) {
  324. wpa_printf(MSG_DEBUG,
  325. "DPP: Ignore TX status since there is no ongoing authentication exchange");
  326. return;
  327. }
  328. if (wpa_s->dpp_auth->remove_on_tx_status) {
  329. wpa_printf(MSG_DEBUG,
  330. "DPP: Terminate authentication exchange due to an earlier error");
  331. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  332. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  333. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
  334. NULL);
  335. offchannel_send_action_done(wpa_s);
  336. dpp_auth_deinit(wpa_s->dpp_auth);
  337. wpa_s->dpp_auth = NULL;
  338. return;
  339. }
  340. if (wpa_s->dpp_auth_ok_on_ack)
  341. wpas_dpp_auth_success(wpa_s, 1);
  342. if (!is_broadcast_ether_addr(dst) &&
  343. result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
  344. wpa_printf(MSG_DEBUG,
  345. "DPP: Unicast DPP Action frame was not ACKed");
  346. if (auth->waiting_auth_resp) {
  347. /* In case of DPP Authentication Request frame, move to
  348. * the next channel immediately. */
  349. offchannel_send_action_done(wpa_s);
  350. wpas_dpp_auth_init_next(wpa_s);
  351. return;
  352. }
  353. if (auth->waiting_auth_conf) {
  354. wpas_dpp_auth_resp_retry(wpa_s);
  355. return;
  356. }
  357. }
  358. if (!is_broadcast_ether_addr(dst) && auth->waiting_auth_resp &&
  359. result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
  360. /* Allow timeout handling to stop iteration if no response is
  361. * received from a peer that has ACKed a request. */
  362. auth->auth_req_ack = 1;
  363. }
  364. if (!wpa_s->dpp_auth_ok_on_ack && wpa_s->dpp_auth->neg_freq > 0 &&
  365. wpa_s->dpp_auth->curr_freq != wpa_s->dpp_auth->neg_freq) {
  366. wpa_printf(MSG_DEBUG,
  367. "DPP: Move from curr_freq %u MHz to neg_freq %u MHz for response",
  368. wpa_s->dpp_auth->curr_freq,
  369. wpa_s->dpp_auth->neg_freq);
  370. offchannel_send_action_done(wpa_s);
  371. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->neg_freq);
  372. }
  373. if (wpa_s->dpp_auth_ok_on_ack)
  374. wpa_s->dpp_auth_ok_on_ack = 0;
  375. }
  376. static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
  377. {
  378. struct wpa_supplicant *wpa_s = eloop_ctx;
  379. struct dpp_authentication *auth = wpa_s->dpp_auth;
  380. unsigned int freq;
  381. struct os_reltime now, diff;
  382. unsigned int wait_time, diff_ms;
  383. if (!auth || !auth->waiting_auth_resp)
  384. return;
  385. wait_time = wpa_s->dpp_resp_wait_time ?
  386. wpa_s->dpp_resp_wait_time : 2000;
  387. os_get_reltime(&now);
  388. os_reltime_sub(&now, &wpa_s->dpp_last_init, &diff);
  389. diff_ms = diff.sec * 1000 + diff.usec / 1000;
  390. wpa_printf(MSG_DEBUG,
  391. "DPP: Reply wait timeout - wait_time=%u diff_ms=%u",
  392. wait_time, diff_ms);
  393. if (auth->auth_req_ack && diff_ms >= wait_time) {
  394. /* Peer ACK'ed Authentication Request frame, but did not reply
  395. * with Authentication Response frame within two seconds. */
  396. wpa_printf(MSG_INFO,
  397. "DPP: No response received from responder - stopping initiation attempt");
  398. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
  399. offchannel_send_action_done(wpa_s);
  400. wpas_dpp_listen_stop(wpa_s);
  401. dpp_auth_deinit(auth);
  402. wpa_s->dpp_auth = NULL;
  403. return;
  404. }
  405. if (diff_ms >= wait_time) {
  406. /* Authentication Request frame was not ACK'ed and no reply
  407. * was receiving within two seconds. */
  408. wpa_printf(MSG_DEBUG,
  409. "DPP: Continue Initiator channel iteration");
  410. offchannel_send_action_done(wpa_s);
  411. wpas_dpp_listen_stop(wpa_s);
  412. wpas_dpp_auth_init_next(wpa_s);
  413. return;
  414. }
  415. /* Driver did not support 2000 ms long wait_time with TX command, so
  416. * schedule listen operation to continue waiting for the response.
  417. *
  418. * DPP listen operations continue until stopped, so simply schedule a
  419. * new call to this function at the point when the two second reply
  420. * wait has expired. */
  421. wait_time -= diff_ms;
  422. freq = auth->curr_freq;
  423. if (auth->neg_freq > 0)
  424. freq = auth->neg_freq;
  425. wpa_printf(MSG_DEBUG,
  426. "DPP: Continue reply wait on channel %u MHz for %u ms",
  427. freq, wait_time);
  428. wpa_s->dpp_in_response_listen = 1;
  429. wpas_dpp_listen_start(wpa_s, freq);
  430. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  431. wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  432. }
  433. static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
  434. struct dpp_authentication *auth)
  435. {
  436. #ifdef CONFIG_TESTING_OPTIONS
  437. if (wpa_s->dpp_config_obj_override)
  438. auth->config_obj_override =
  439. os_strdup(wpa_s->dpp_config_obj_override);
  440. if (wpa_s->dpp_discovery_override)
  441. auth->discovery_override =
  442. os_strdup(wpa_s->dpp_discovery_override);
  443. if (wpa_s->dpp_groups_override)
  444. auth->groups_override =
  445. os_strdup(wpa_s->dpp_groups_override);
  446. auth->ignore_netaccesskey_mismatch =
  447. wpa_s->dpp_ignore_netaccesskey_mismatch;
  448. #endif /* CONFIG_TESTING_OPTIONS */
  449. }
  450. static void wpas_dpp_set_configurator(struct wpa_supplicant *wpa_s,
  451. struct dpp_authentication *auth,
  452. const char *cmd)
  453. {
  454. const char *pos, *end;
  455. struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
  456. struct dpp_configurator *conf = NULL;
  457. u8 ssid[32] = { "test" };
  458. size_t ssid_len = 4;
  459. char pass[64] = { };
  460. size_t pass_len = 0;
  461. u8 psk[PMK_LEN];
  462. int psk_set = 0;
  463. if (!cmd)
  464. return;
  465. wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
  466. pos = os_strstr(cmd, " ssid=");
  467. if (pos) {
  468. pos += 6;
  469. end = os_strchr(pos, ' ');
  470. ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
  471. ssid_len /= 2;
  472. if (ssid_len > sizeof(ssid) ||
  473. hexstr2bin(pos, ssid, ssid_len) < 0)
  474. goto fail;
  475. }
  476. pos = os_strstr(cmd, " pass=");
  477. if (pos) {
  478. pos += 6;
  479. end = os_strchr(pos, ' ');
  480. pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
  481. pass_len /= 2;
  482. if (pass_len > sizeof(pass) - 1 || pass_len < 8 ||
  483. hexstr2bin(pos, (u8 *) pass, pass_len) < 0)
  484. goto fail;
  485. }
  486. pos = os_strstr(cmd, " psk=");
  487. if (pos) {
  488. pos += 5;
  489. if (hexstr2bin(pos, psk, PMK_LEN) < 0)
  490. goto fail;
  491. psk_set = 1;
  492. }
  493. if (os_strstr(cmd, " conf=sta-")) {
  494. conf_sta = os_zalloc(sizeof(struct dpp_configuration));
  495. if (!conf_sta)
  496. goto fail;
  497. os_memcpy(conf_sta->ssid, ssid, ssid_len);
  498. conf_sta->ssid_len = ssid_len;
  499. if (os_strstr(cmd, " conf=sta-psk") ||
  500. os_strstr(cmd, " conf=sta-sae") ||
  501. os_strstr(cmd, " conf=sta-psk-sae")) {
  502. if (os_strstr(cmd, " conf=sta-psk-sae"))
  503. conf_sta->akm = DPP_AKM_PSK_SAE;
  504. else if (os_strstr(cmd, " conf=sta-sae"))
  505. conf_sta->akm = DPP_AKM_SAE;
  506. else
  507. conf_sta->akm = DPP_AKM_PSK;
  508. if (psk_set) {
  509. os_memcpy(conf_sta->psk, psk, PMK_LEN);
  510. } else {
  511. conf_sta->passphrase = os_strdup(pass);
  512. if (!conf_sta->passphrase)
  513. goto fail;
  514. }
  515. } else if (os_strstr(cmd, " conf=sta-dpp")) {
  516. conf_sta->akm = DPP_AKM_DPP;
  517. } else {
  518. goto fail;
  519. }
  520. }
  521. if (os_strstr(cmd, " conf=ap-")) {
  522. conf_ap = os_zalloc(sizeof(struct dpp_configuration));
  523. if (!conf_ap)
  524. goto fail;
  525. os_memcpy(conf_ap->ssid, ssid, ssid_len);
  526. conf_ap->ssid_len = ssid_len;
  527. if (os_strstr(cmd, " conf=ap-psk") ||
  528. os_strstr(cmd, " conf=ap-sae") ||
  529. os_strstr(cmd, " conf=ap-psk-sae")) {
  530. if (os_strstr(cmd, " conf=ap-psk-sae"))
  531. conf_ap->akm = DPP_AKM_PSK_SAE;
  532. else if (os_strstr(cmd, " conf=ap-sae"))
  533. conf_ap->akm = DPP_AKM_SAE;
  534. else
  535. conf_ap->akm = DPP_AKM_PSK;
  536. if (psk_set) {
  537. os_memcpy(conf_ap->psk, psk, PMK_LEN);
  538. } else {
  539. conf_ap->passphrase = os_strdup(pass);
  540. if (!conf_ap->passphrase)
  541. goto fail;
  542. }
  543. } else if (os_strstr(cmd, " conf=ap-dpp")) {
  544. conf_ap->akm = DPP_AKM_DPP;
  545. } else {
  546. goto fail;
  547. }
  548. }
  549. pos = os_strstr(cmd, " expiry=");
  550. if (pos) {
  551. long int val;
  552. pos += 8;
  553. val = strtol(pos, NULL, 0);
  554. if (val <= 0)
  555. goto fail;
  556. if (conf_sta)
  557. conf_sta->netaccesskey_expiry = val;
  558. if (conf_ap)
  559. conf_ap->netaccesskey_expiry = val;
  560. }
  561. pos = os_strstr(cmd, " configurator=");
  562. if (pos) {
  563. pos += 14;
  564. conf = dpp_configurator_get_id(wpa_s, atoi(pos));
  565. if (!conf) {
  566. wpa_printf(MSG_INFO,
  567. "DPP: Could not find the specified configurator");
  568. goto fail;
  569. }
  570. }
  571. auth->conf_sta = conf_sta;
  572. auth->conf_ap = conf_ap;
  573. auth->conf = conf;
  574. return;
  575. fail:
  576. wpa_printf(MSG_DEBUG, "DPP: Failed to set configurator parameters");
  577. dpp_configuration_free(conf_sta);
  578. dpp_configuration_free(conf_ap);
  579. }
  580. static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
  581. {
  582. struct wpa_supplicant *wpa_s = eloop_ctx;
  583. if (!wpa_s->dpp_auth)
  584. return;
  585. wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
  586. wpas_dpp_auth_init_next(wpa_s);
  587. }
  588. static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
  589. {
  590. struct dpp_authentication *auth = wpa_s->dpp_auth;
  591. const u8 *dst;
  592. unsigned int wait_time, max_wait_time, freq, max_tries, used;
  593. struct os_reltime now, diff;
  594. wpa_s->dpp_in_response_listen = 0;
  595. if (!auth)
  596. return -1;
  597. if (auth->freq_idx == 0)
  598. os_get_reltime(&wpa_s->dpp_init_iter_start);
  599. if (auth->freq_idx >= auth->num_freq) {
  600. auth->num_freq_iters++;
  601. if (wpa_s->dpp_init_max_tries)
  602. max_tries = wpa_s->dpp_init_max_tries;
  603. else
  604. max_tries = 5;
  605. if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
  606. wpa_printf(MSG_INFO,
  607. "DPP: No response received from responder - stopping initiation attempt");
  608. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
  609. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
  610. wpa_s, NULL);
  611. offchannel_send_action_done(wpa_s);
  612. dpp_auth_deinit(wpa_s->dpp_auth);
  613. wpa_s->dpp_auth = NULL;
  614. return -1;
  615. }
  616. auth->freq_idx = 0;
  617. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  618. if (wpa_s->dpp_init_retry_time)
  619. wait_time = wpa_s->dpp_init_retry_time;
  620. else
  621. wait_time = 10000;
  622. os_get_reltime(&now);
  623. os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
  624. used = diff.sec * 1000 + diff.usec / 1000;
  625. if (used > wait_time)
  626. wait_time = 0;
  627. else
  628. wait_time -= used;
  629. wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
  630. wait_time);
  631. eloop_register_timeout(wait_time / 1000,
  632. (wait_time % 1000) * 1000,
  633. wpas_dpp_init_timeout, wpa_s,
  634. NULL);
  635. return 0;
  636. }
  637. freq = auth->freq[auth->freq_idx++];
  638. auth->curr_freq = freq;
  639. if (is_zero_ether_addr(auth->peer_bi->mac_addr))
  640. dst = broadcast;
  641. else
  642. dst = auth->peer_bi->mac_addr;
  643. wpa_s->dpp_auth_ok_on_ack = 0;
  644. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  645. wait_time = wpa_s->max_remain_on_chan;
  646. max_wait_time = wpa_s->dpp_resp_wait_time ?
  647. wpa_s->dpp_resp_wait_time : 2000;
  648. if (wait_time > max_wait_time)
  649. wait_time = max_wait_time;
  650. wait_time += 10; /* give the driver some extra time to complete */
  651. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  652. wpas_dpp_reply_wait_timeout,
  653. wpa_s, NULL);
  654. wait_time -= 10;
  655. if (auth->neg_freq > 0 && freq != auth->neg_freq) {
  656. wpa_printf(MSG_DEBUG,
  657. "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
  658. freq, auth->neg_freq);
  659. }
  660. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  661. MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
  662. auth->auth_req_ack = 0;
  663. os_get_reltime(&wpa_s->dpp_last_init);
  664. return offchannel_send_action(wpa_s, freq, dst,
  665. wpa_s->own_addr, broadcast,
  666. wpabuf_head(auth->req_msg),
  667. wpabuf_len(auth->req_msg),
  668. wait_time, wpas_dpp_tx_status, 0);
  669. }
  670. int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
  671. {
  672. const char *pos;
  673. struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
  674. u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
  675. unsigned int neg_freq = 0;
  676. wpa_s->dpp_gas_client = 0;
  677. pos = os_strstr(cmd, " peer=");
  678. if (!pos)
  679. return -1;
  680. pos += 6;
  681. peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  682. if (!peer_bi) {
  683. wpa_printf(MSG_INFO,
  684. "DPP: Could not find bootstrapping info for the identified peer");
  685. return -1;
  686. }
  687. pos = os_strstr(cmd, " own=");
  688. if (pos) {
  689. pos += 5;
  690. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  691. if (!own_bi) {
  692. wpa_printf(MSG_INFO,
  693. "DPP: Could not find bootstrapping info for the identified local entry");
  694. return -1;
  695. }
  696. if (peer_bi->curve != own_bi->curve) {
  697. wpa_printf(MSG_INFO,
  698. "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
  699. peer_bi->curve->name, own_bi->curve->name);
  700. return -1;
  701. }
  702. }
  703. pos = os_strstr(cmd, " role=");
  704. if (pos) {
  705. pos += 6;
  706. if (os_strncmp(pos, "configurator", 12) == 0)
  707. allowed_roles = DPP_CAPAB_CONFIGURATOR;
  708. else if (os_strncmp(pos, "enrollee", 8) == 0)
  709. allowed_roles = DPP_CAPAB_ENROLLEE;
  710. else if (os_strncmp(pos, "either", 6) == 0)
  711. allowed_roles = DPP_CAPAB_CONFIGURATOR |
  712. DPP_CAPAB_ENROLLEE;
  713. else
  714. goto fail;
  715. }
  716. pos = os_strstr(cmd, " netrole=");
  717. if (pos) {
  718. pos += 9;
  719. wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
  720. }
  721. pos = os_strstr(cmd, " neg_freq=");
  722. if (pos)
  723. neg_freq = atoi(pos + 10);
  724. if (wpa_s->dpp_auth) {
  725. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  726. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  727. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
  728. NULL);
  729. offchannel_send_action_done(wpa_s);
  730. dpp_auth_deinit(wpa_s->dpp_auth);
  731. }
  732. wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles,
  733. neg_freq,
  734. wpa_s->hw.modes, wpa_s->hw.num_modes);
  735. if (!wpa_s->dpp_auth)
  736. goto fail;
  737. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  738. wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth, cmd);
  739. wpa_s->dpp_auth->neg_freq = neg_freq;
  740. if (!is_zero_ether_addr(peer_bi->mac_addr))
  741. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
  742. ETH_ALEN);
  743. return wpas_dpp_auth_init_next(wpa_s);
  744. fail:
  745. return -1;
  746. }
  747. struct wpas_dpp_listen_work {
  748. unsigned int freq;
  749. unsigned int duration;
  750. struct wpabuf *probe_resp_ie;
  751. };
  752. static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
  753. {
  754. if (!lwork)
  755. return;
  756. os_free(lwork);
  757. }
  758. static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
  759. {
  760. struct wpas_dpp_listen_work *lwork;
  761. if (!wpa_s->dpp_listen_work)
  762. return;
  763. lwork = wpa_s->dpp_listen_work->ctx;
  764. wpas_dpp_listen_work_free(lwork);
  765. radio_work_done(wpa_s->dpp_listen_work);
  766. wpa_s->dpp_listen_work = NULL;
  767. }
  768. static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
  769. {
  770. struct wpa_supplicant *wpa_s = work->wpa_s;
  771. struct wpas_dpp_listen_work *lwork = work->ctx;
  772. if (deinit) {
  773. if (work->started) {
  774. wpa_s->dpp_listen_work = NULL;
  775. wpas_dpp_listen_stop(wpa_s);
  776. }
  777. wpas_dpp_listen_work_free(lwork);
  778. return;
  779. }
  780. wpa_s->dpp_listen_work = work;
  781. wpa_s->dpp_pending_listen_freq = lwork->freq;
  782. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
  783. wpa_s->max_remain_on_chan) < 0) {
  784. wpa_printf(MSG_DEBUG,
  785. "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
  786. lwork->freq);
  787. wpas_dpp_listen_work_done(wpa_s);
  788. wpa_s->dpp_pending_listen_freq = 0;
  789. return;
  790. }
  791. wpa_s->off_channel_freq = 0;
  792. wpa_s->roc_waiting_drv_freq = lwork->freq;
  793. }
  794. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  795. unsigned int freq)
  796. {
  797. struct wpas_dpp_listen_work *lwork;
  798. if (wpa_s->dpp_listen_work) {
  799. wpa_printf(MSG_DEBUG,
  800. "DPP: Reject start_listen since dpp_listen_work already exists");
  801. return -1;
  802. }
  803. if (wpa_s->dpp_listen_freq)
  804. wpas_dpp_listen_stop(wpa_s);
  805. wpa_s->dpp_listen_freq = freq;
  806. lwork = os_zalloc(sizeof(*lwork));
  807. if (!lwork)
  808. return -1;
  809. lwork->freq = freq;
  810. if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
  811. lwork) < 0) {
  812. wpas_dpp_listen_work_free(lwork);
  813. return -1;
  814. }
  815. return 0;
  816. }
  817. int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
  818. {
  819. int freq;
  820. freq = atoi(cmd);
  821. if (freq <= 0)
  822. return -1;
  823. if (os_strstr(cmd, " role=configurator"))
  824. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
  825. else if (os_strstr(cmd, " role=enrollee"))
  826. wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
  827. else
  828. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
  829. DPP_CAPAB_ENROLLEE;
  830. wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
  831. wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
  832. if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
  833. wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
  834. freq);
  835. return 0;
  836. }
  837. return wpas_dpp_listen_start(wpa_s, freq);
  838. }
  839. void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
  840. {
  841. wpa_s->dpp_in_response_listen = 0;
  842. if (!wpa_s->dpp_listen_freq)
  843. return;
  844. wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
  845. wpa_s->dpp_listen_freq);
  846. wpa_drv_cancel_remain_on_channel(wpa_s);
  847. wpa_s->dpp_listen_freq = 0;
  848. wpas_dpp_listen_work_done(wpa_s);
  849. }
  850. void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  851. unsigned int freq)
  852. {
  853. if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
  854. return;
  855. wpa_printf(MSG_DEBUG,
  856. "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
  857. wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
  858. wpa_s->roc_waiting_drv_freq, freq);
  859. if (wpa_s->off_channel_freq &&
  860. wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
  861. wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
  862. wpa_s->dpp_pending_listen_freq = 0;
  863. } else {
  864. wpa_printf(MSG_DEBUG,
  865. "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
  866. wpa_s->off_channel_freq,
  867. wpa_s->dpp_pending_listen_freq, freq);
  868. }
  869. }
  870. void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  871. unsigned int freq)
  872. {
  873. wpas_dpp_listen_work_done(wpa_s);
  874. if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
  875. unsigned int new_freq;
  876. /* Continue listen with a new remain-on-channel */
  877. if (wpa_s->dpp_auth->neg_freq > 0)
  878. new_freq = wpa_s->dpp_auth->neg_freq;
  879. else
  880. new_freq = wpa_s->dpp_auth->curr_freq;
  881. wpa_printf(MSG_DEBUG,
  882. "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
  883. new_freq);
  884. wpas_dpp_listen_start(wpa_s, new_freq);
  885. return;
  886. }
  887. if (wpa_s->dpp_listen_freq) {
  888. /* Continue listen with a new remain-on-channel */
  889. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
  890. }
  891. }
  892. static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
  893. const u8 *hdr, const u8 *buf, size_t len,
  894. unsigned int freq)
  895. {
  896. const u8 *r_bootstrap, *i_bootstrap;
  897. u16 r_bootstrap_len, i_bootstrap_len;
  898. struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
  899. wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
  900. MAC2STR(src));
  901. r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  902. &r_bootstrap_len);
  903. if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
  904. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  905. "Missing or invalid required Responder Bootstrapping Key Hash attribute");
  906. return;
  907. }
  908. wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
  909. r_bootstrap, r_bootstrap_len);
  910. i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  911. &i_bootstrap_len);
  912. if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
  913. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  914. "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
  915. return;
  916. }
  917. wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
  918. i_bootstrap, i_bootstrap_len);
  919. /* Try to find own and peer bootstrapping key matches based on the
  920. * received hash values */
  921. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  922. list) {
  923. if (!own_bi && bi->own &&
  924. os_memcmp(bi->pubkey_hash, r_bootstrap,
  925. SHA256_MAC_LEN) == 0) {
  926. wpa_printf(MSG_DEBUG,
  927. "DPP: Found matching own bootstrapping information");
  928. own_bi = bi;
  929. }
  930. if (!peer_bi && !bi->own &&
  931. os_memcmp(bi->pubkey_hash, i_bootstrap,
  932. SHA256_MAC_LEN) == 0) {
  933. wpa_printf(MSG_DEBUG,
  934. "DPP: Found matching peer bootstrapping information");
  935. peer_bi = bi;
  936. }
  937. if (own_bi && peer_bi)
  938. break;
  939. }
  940. if (!own_bi) {
  941. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  942. "No matching own bootstrapping key found - ignore message");
  943. return;
  944. }
  945. if (wpa_s->dpp_auth) {
  946. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  947. "Already in DPP authentication exchange - ignore new one");
  948. return;
  949. }
  950. wpa_s->dpp_gas_client = 0;
  951. wpa_s->dpp_auth_ok_on_ack = 0;
  952. wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
  953. wpa_s->dpp_qr_mutual,
  954. peer_bi, own_bi, freq, hdr, buf, len);
  955. if (!wpa_s->dpp_auth) {
  956. wpa_printf(MSG_DEBUG, "DPP: No response generated");
  957. return;
  958. }
  959. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  960. wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth,
  961. wpa_s->dpp_configurator_params);
  962. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
  963. if (wpa_s->dpp_listen_freq &&
  964. wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
  965. wpa_printf(MSG_DEBUG,
  966. "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
  967. wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
  968. wpas_dpp_listen_stop(wpa_s);
  969. }
  970. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  971. MAC2STR(src), wpa_s->dpp_auth->curr_freq,
  972. DPP_PA_AUTHENTICATION_RESP);
  973. offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  974. src, wpa_s->own_addr, broadcast,
  975. wpabuf_head(wpa_s->dpp_auth->resp_msg),
  976. wpabuf_len(wpa_s->dpp_auth->resp_msg),
  977. 500, wpas_dpp_tx_status, 0);
  978. }
  979. static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
  980. {
  981. /* TODO: stop wait and start ROC */
  982. }
  983. static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
  984. struct dpp_authentication *auth)
  985. {
  986. struct wpa_ssid *ssid;
  987. ssid = wpa_config_add_network(wpa_s->conf);
  988. if (!ssid)
  989. return NULL;
  990. wpas_notify_network_added(wpa_s, ssid);
  991. wpa_config_set_network_defaults(ssid);
  992. ssid->disabled = 1;
  993. ssid->ssid = os_malloc(auth->ssid_len);
  994. if (!ssid->ssid)
  995. goto fail;
  996. os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
  997. ssid->ssid_len = auth->ssid_len;
  998. if (auth->connector) {
  999. ssid->key_mgmt = WPA_KEY_MGMT_DPP;
  1000. ssid->ieee80211w = 1;
  1001. ssid->dpp_connector = os_strdup(auth->connector);
  1002. if (!ssid->dpp_connector)
  1003. goto fail;
  1004. }
  1005. if (auth->c_sign_key) {
  1006. ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
  1007. if (!ssid->dpp_csign)
  1008. goto fail;
  1009. os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
  1010. wpabuf_len(auth->c_sign_key));
  1011. ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
  1012. }
  1013. if (auth->net_access_key) {
  1014. ssid->dpp_netaccesskey =
  1015. os_malloc(wpabuf_len(auth->net_access_key));
  1016. if (!ssid->dpp_netaccesskey)
  1017. goto fail;
  1018. os_memcpy(ssid->dpp_netaccesskey,
  1019. wpabuf_head(auth->net_access_key),
  1020. wpabuf_len(auth->net_access_key));
  1021. ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
  1022. ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
  1023. }
  1024. if (!auth->connector) {
  1025. ssid->key_mgmt = 0;
  1026. if (auth->akm == DPP_AKM_PSK || auth->akm == DPP_AKM_PSK_SAE)
  1027. ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
  1028. WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
  1029. if (auth->akm == DPP_AKM_SAE || auth->akm == DPP_AKM_PSK_SAE)
  1030. ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
  1031. WPA_KEY_MGMT_FT_SAE;
  1032. ssid->ieee80211w = 1;
  1033. if (auth->passphrase[0]) {
  1034. if (wpa_config_set_quoted(ssid, "psk",
  1035. auth->passphrase) < 0)
  1036. goto fail;
  1037. wpa_config_update_psk(ssid);
  1038. ssid->export_keys = 1;
  1039. } else {
  1040. ssid->psk_set = auth->psk_set;
  1041. os_memcpy(ssid->psk, auth->psk, PMK_LEN);
  1042. }
  1043. }
  1044. return ssid;
  1045. fail:
  1046. wpas_notify_network_removed(wpa_s, ssid);
  1047. wpa_config_remove_network(wpa_s->conf, ssid->id);
  1048. return NULL;
  1049. }
  1050. static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
  1051. struct dpp_authentication *auth)
  1052. {
  1053. struct wpa_ssid *ssid;
  1054. if (wpa_s->conf->dpp_config_processing < 1)
  1055. return;
  1056. ssid = wpas_dpp_add_network(wpa_s, auth);
  1057. if (!ssid)
  1058. return;
  1059. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
  1060. if (wpa_s->conf->dpp_config_processing < 2)
  1061. return;
  1062. wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
  1063. ssid->disabled = 0;
  1064. wpa_s->disconnected = 0;
  1065. wpa_s->reassociate = 1;
  1066. wpa_s->scan_runs = 0;
  1067. wpa_s->normal_scans = 0;
  1068. wpa_supplicant_cancel_sched_scan(wpa_s);
  1069. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1070. }
  1071. static void wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
  1072. struct dpp_authentication *auth)
  1073. {
  1074. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
  1075. if (auth->ssid_len)
  1076. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
  1077. wpa_ssid_txt(auth->ssid, auth->ssid_len));
  1078. if (auth->connector) {
  1079. /* TODO: Save the Connector and consider using a command
  1080. * to fetch the value instead of sending an event with
  1081. * it. The Connector could end up being larger than what
  1082. * most clients are ready to receive as an event
  1083. * message. */
  1084. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
  1085. auth->connector);
  1086. }
  1087. if (auth->c_sign_key) {
  1088. char *hex;
  1089. size_t hexlen;
  1090. hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
  1091. hex = os_malloc(hexlen);
  1092. if (hex) {
  1093. wpa_snprintf_hex(hex, hexlen,
  1094. wpabuf_head(auth->c_sign_key),
  1095. wpabuf_len(auth->c_sign_key));
  1096. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
  1097. hex);
  1098. os_free(hex);
  1099. }
  1100. }
  1101. if (auth->net_access_key) {
  1102. char *hex;
  1103. size_t hexlen;
  1104. hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
  1105. hex = os_malloc(hexlen);
  1106. if (hex) {
  1107. wpa_snprintf_hex(hex, hexlen,
  1108. wpabuf_head(auth->net_access_key),
  1109. wpabuf_len(auth->net_access_key));
  1110. if (auth->net_access_key_expiry)
  1111. wpa_msg(wpa_s, MSG_INFO,
  1112. DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
  1113. (long unsigned)
  1114. auth->net_access_key_expiry);
  1115. else
  1116. wpa_msg(wpa_s, MSG_INFO,
  1117. DPP_EVENT_NET_ACCESS_KEY "%s", hex);
  1118. os_free(hex);
  1119. }
  1120. }
  1121. wpas_dpp_process_config(wpa_s, auth);
  1122. }
  1123. static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  1124. enum gas_query_result result,
  1125. const struct wpabuf *adv_proto,
  1126. const struct wpabuf *resp, u16 status_code)
  1127. {
  1128. struct wpa_supplicant *wpa_s = ctx;
  1129. const u8 *pos;
  1130. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1131. wpa_s->dpp_gas_dialog_token = -1;
  1132. if (!auth || !auth->auth_success) {
  1133. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1134. return;
  1135. }
  1136. if (!resp || status_code != WLAN_STATUS_SUCCESS) {
  1137. wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
  1138. goto fail;
  1139. }
  1140. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
  1141. adv_proto);
  1142. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
  1143. resp);
  1144. if (wpabuf_len(adv_proto) != 10 ||
  1145. !(pos = wpabuf_head(adv_proto)) ||
  1146. pos[0] != WLAN_EID_ADV_PROTO ||
  1147. pos[1] != 8 ||
  1148. pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
  1149. pos[4] != 5 ||
  1150. WPA_GET_BE24(&pos[5]) != OUI_WFA ||
  1151. pos[8] != 0x1a ||
  1152. pos[9] != 1) {
  1153. wpa_printf(MSG_DEBUG,
  1154. "DPP: Not a DPP Advertisement Protocol ID");
  1155. goto fail;
  1156. }
  1157. if (dpp_conf_resp_rx(auth, resp) < 0) {
  1158. wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
  1159. goto fail;
  1160. }
  1161. wpas_dpp_handle_config_obj(wpa_s, auth);
  1162. dpp_auth_deinit(wpa_s->dpp_auth);
  1163. wpa_s->dpp_auth = NULL;
  1164. return;
  1165. fail:
  1166. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1167. dpp_auth_deinit(wpa_s->dpp_auth);
  1168. wpa_s->dpp_auth = NULL;
  1169. }
  1170. static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
  1171. {
  1172. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1173. struct wpabuf *buf, *conf_req;
  1174. char json[100];
  1175. int res;
  1176. wpa_s->dpp_gas_client = 1;
  1177. os_snprintf(json, sizeof(json),
  1178. "{\"name\":\"Test\","
  1179. "\"wi-fi_tech\":\"infra\","
  1180. "\"netRole\":\"%s\"}",
  1181. wpa_s->dpp_netrole_ap ? "ap" : "sta");
  1182. #ifdef CONFIG_TESTING_OPTIONS
  1183. if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
  1184. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
  1185. json[29] = 'k'; /* replace "infra" with "knfra" */
  1186. }
  1187. #endif /* CONFIG_TESTING_OPTIONS */
  1188. wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
  1189. offchannel_send_action_done(wpa_s);
  1190. wpas_dpp_listen_stop(wpa_s);
  1191. conf_req = dpp_build_conf_req(auth, json);
  1192. if (!conf_req) {
  1193. wpa_printf(MSG_DEBUG,
  1194. "DPP: No configuration request data available");
  1195. return;
  1196. }
  1197. buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
  1198. if (!buf) {
  1199. wpabuf_free(conf_req);
  1200. return;
  1201. }
  1202. /* Advertisement Protocol IE */
  1203. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  1204. wpabuf_put_u8(buf, 8); /* Length */
  1205. wpabuf_put_u8(buf, 0x7f);
  1206. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  1207. wpabuf_put_u8(buf, 5);
  1208. wpabuf_put_be24(buf, OUI_WFA);
  1209. wpabuf_put_u8(buf, DPP_OUI_TYPE);
  1210. wpabuf_put_u8(buf, 0x01);
  1211. /* GAS Query */
  1212. wpabuf_put_le16(buf, wpabuf_len(conf_req));
  1213. wpabuf_put_buf(buf, conf_req);
  1214. wpabuf_free(conf_req);
  1215. wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
  1216. MAC2STR(auth->peer_mac_addr), auth->curr_freq);
  1217. res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
  1218. buf, wpas_dpp_gas_resp_cb, wpa_s);
  1219. if (res < 0) {
  1220. wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
  1221. wpabuf_free(buf);
  1222. } else {
  1223. wpa_printf(MSG_DEBUG,
  1224. "DPP: GAS query started with dialog token %u", res);
  1225. wpa_s->dpp_gas_dialog_token = res;
  1226. }
  1227. }
  1228. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
  1229. {
  1230. wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
  1231. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
  1232. if (wpa_s->dpp_auth->configurator)
  1233. wpas_dpp_start_gas_server(wpa_s);
  1234. else
  1235. wpas_dpp_start_gas_client(wpa_s);
  1236. }
  1237. static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1238. const u8 *hdr, const u8 *buf, size_t len,
  1239. unsigned int freq)
  1240. {
  1241. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1242. struct wpabuf *msg;
  1243. wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
  1244. " (freq %u MHz)", MAC2STR(src), freq);
  1245. if (!auth) {
  1246. wpa_printf(MSG_DEBUG,
  1247. "DPP: No DPP Authentication in progress - drop");
  1248. return;
  1249. }
  1250. if (!is_zero_ether_addr(auth->peer_mac_addr) &&
  1251. os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1252. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1253. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1254. return;
  1255. }
  1256. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1257. if (auth->curr_freq != freq && auth->neg_freq == freq) {
  1258. wpa_printf(MSG_DEBUG,
  1259. "DPP: Responder accepted request for different negotiation channel");
  1260. auth->curr_freq = freq;
  1261. }
  1262. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  1263. msg = dpp_auth_resp_rx(auth, hdr, buf, len);
  1264. if (!msg) {
  1265. if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
  1266. wpa_printf(MSG_DEBUG,
  1267. "DPP: Start wait for full response");
  1268. offchannel_send_action_done(wpa_s);
  1269. wpas_dpp_listen_start(wpa_s, auth->curr_freq);
  1270. return;
  1271. }
  1272. wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
  1273. return;
  1274. }
  1275. os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
  1276. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1277. MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
  1278. offchannel_send_action(wpa_s, auth->curr_freq,
  1279. src, wpa_s->own_addr, broadcast,
  1280. wpabuf_head(msg), wpabuf_len(msg),
  1281. 500, wpas_dpp_tx_status, 0);
  1282. wpabuf_free(msg);
  1283. wpa_s->dpp_auth_ok_on_ack = 1;
  1284. }
  1285. static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
  1286. const u8 *hdr, const u8 *buf, size_t len)
  1287. {
  1288. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1289. wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
  1290. MAC2STR(src));
  1291. if (!auth) {
  1292. wpa_printf(MSG_DEBUG,
  1293. "DPP: No DPP Authentication in progress - drop");
  1294. return;
  1295. }
  1296. if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1297. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1298. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1299. return;
  1300. }
  1301. if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
  1302. wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
  1303. return;
  1304. }
  1305. wpas_dpp_auth_success(wpa_s, 0);
  1306. }
  1307. static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
  1308. const u8 *src,
  1309. const u8 *buf, size_t len)
  1310. {
  1311. struct wpa_ssid *ssid;
  1312. const u8 *connector, *trans_id, *status;
  1313. u16 connector_len, trans_id_len, status_len;
  1314. struct dpp_introduction intro;
  1315. struct rsn_pmksa_cache_entry *entry;
  1316. struct os_time now;
  1317. struct os_reltime rnow;
  1318. os_time_t expiry;
  1319. unsigned int seconds;
  1320. enum dpp_status_error res;
  1321. wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
  1322. MAC2STR(src));
  1323. if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
  1324. os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
  1325. wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
  1326. MACSTR " - drop", MAC2STR(src));
  1327. return;
  1328. }
  1329. offchannel_send_action_done(wpa_s);
  1330. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1331. if (ssid == wpa_s->dpp_intro_network)
  1332. break;
  1333. }
  1334. if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1335. !ssid->dpp_csign) {
  1336. wpa_printf(MSG_DEBUG,
  1337. "DPP: Profile not found for network introduction");
  1338. return;
  1339. }
  1340. trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
  1341. &trans_id_len);
  1342. if (!trans_id || trans_id_len != 1) {
  1343. wpa_printf(MSG_DEBUG,
  1344. "DPP: Peer did not include Transaction ID");
  1345. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1346. " fail=missing_transaction_id", MAC2STR(src));
  1347. goto fail;
  1348. }
  1349. if (trans_id[0] != TRANSACTION_ID) {
  1350. wpa_printf(MSG_DEBUG,
  1351. "DPP: Ignore frame with unexpected Transaction ID %u",
  1352. trans_id[0]);
  1353. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1354. " fail=transaction_id_mismatch", MAC2STR(src));
  1355. goto fail;
  1356. }
  1357. status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
  1358. if (!status || status_len != 1) {
  1359. wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
  1360. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1361. " fail=missing_status", MAC2STR(src));
  1362. goto fail;
  1363. }
  1364. if (status[0] != DPP_STATUS_OK) {
  1365. wpa_printf(MSG_DEBUG,
  1366. "DPP: Peer rejected network introduction: Status %u",
  1367. status[0]);
  1368. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1369. " status=%u", MAC2STR(src), status[0]);
  1370. goto fail;
  1371. }
  1372. connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
  1373. if (!connector) {
  1374. wpa_printf(MSG_DEBUG,
  1375. "DPP: Peer did not include its Connector");
  1376. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1377. " fail=missing_connector", MAC2STR(src));
  1378. goto fail;
  1379. }
  1380. res = dpp_peer_intro(&intro, ssid->dpp_connector,
  1381. ssid->dpp_netaccesskey,
  1382. ssid->dpp_netaccesskey_len,
  1383. ssid->dpp_csign,
  1384. ssid->dpp_csign_len,
  1385. connector, connector_len, &expiry);
  1386. if (res != DPP_STATUS_OK) {
  1387. wpa_printf(MSG_INFO,
  1388. "DPP: Network Introduction protocol resulted in failure");
  1389. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1390. " fail=peer_connector_validation_failed", MAC2STR(src));
  1391. goto fail;
  1392. }
  1393. entry = os_zalloc(sizeof(*entry));
  1394. if (!entry)
  1395. goto fail;
  1396. os_memcpy(entry->aa, src, ETH_ALEN);
  1397. os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
  1398. os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
  1399. entry->pmk_len = intro.pmk_len;
  1400. entry->akmp = WPA_KEY_MGMT_DPP;
  1401. if (expiry) {
  1402. os_get_time(&now);
  1403. seconds = expiry - now.sec;
  1404. } else {
  1405. seconds = 86400 * 7;
  1406. }
  1407. os_get_reltime(&rnow);
  1408. entry->expiration = rnow.sec + seconds;
  1409. entry->reauth_time = rnow.sec + seconds;
  1410. entry->network_ctx = ssid;
  1411. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  1412. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
  1413. " status=%u", MAC2STR(src), status[0]);
  1414. wpa_printf(MSG_DEBUG,
  1415. "DPP: Try connection again after successful network introduction");
  1416. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  1417. wpa_supplicant_cancel_sched_scan(wpa_s);
  1418. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1419. }
  1420. fail:
  1421. os_memset(&intro, 0, sizeof(intro));
  1422. }
  1423. static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
  1424. {
  1425. struct wpa_supplicant *wpa_s = eloop_ctx;
  1426. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1427. if (!pkex || !pkex->exchange_req)
  1428. return;
  1429. if (pkex->exch_req_tries >= 5) {
  1430. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
  1431. "No response from PKEX peer");
  1432. dpp_pkex_free(pkex);
  1433. wpa_s->dpp_pkex = NULL;
  1434. return;
  1435. }
  1436. pkex->exch_req_tries++;
  1437. wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
  1438. pkex->exch_req_tries);
  1439. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1440. MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
  1441. offchannel_send_action(wpa_s, pkex->freq, broadcast,
  1442. wpa_s->own_addr, broadcast,
  1443. wpabuf_head(pkex->exchange_req),
  1444. wpabuf_len(pkex->exchange_req),
  1445. pkex->exch_req_wait_time,
  1446. wpas_dpp_tx_pkex_status, 0);
  1447. }
  1448. static void
  1449. wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
  1450. unsigned int freq, const u8 *dst,
  1451. const u8 *src, const u8 *bssid,
  1452. const u8 *data, size_t data_len,
  1453. enum offchannel_send_action_result result)
  1454. {
  1455. const char *res_txt;
  1456. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1457. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1458. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1459. "FAILED");
  1460. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1461. " result=%s (PKEX)",
  1462. freq, MAC2STR(dst), res_txt);
  1463. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  1464. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  1465. if (!pkex) {
  1466. wpa_printf(MSG_DEBUG,
  1467. "DPP: Ignore TX status since there is no ongoing PKEX exchange");
  1468. return;
  1469. }
  1470. if (pkex->failed) {
  1471. wpa_printf(MSG_DEBUG,
  1472. "DPP: Terminate PKEX exchange due to an earlier error");
  1473. if (pkex->t > pkex->own_bi->pkex_t)
  1474. pkex->own_bi->pkex_t = pkex->t;
  1475. dpp_pkex_free(pkex);
  1476. wpa_s->dpp_pkex = NULL;
  1477. return;
  1478. }
  1479. if (pkex->exch_req_wait_time && pkex->exchange_req) {
  1480. /* Wait for PKEX Exchange Response frame and retry request if
  1481. * no response is seen. */
  1482. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  1483. eloop_register_timeout(pkex->exch_req_wait_time / 1000,
  1484. (pkex->exch_req_wait_time % 1000) * 1000,
  1485. wpas_dpp_pkex_retry_timeout, wpa_s,
  1486. NULL);
  1487. }
  1488. }
  1489. static void
  1490. wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
  1491. const u8 *buf, size_t len, unsigned int freq)
  1492. {
  1493. struct wpabuf *msg;
  1494. unsigned int wait_time;
  1495. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
  1496. MAC2STR(src));
  1497. /* TODO: Support multiple PKEX codes by iterating over all the enabled
  1498. * values here */
  1499. if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
  1500. wpa_printf(MSG_DEBUG,
  1501. "DPP: No PKEX code configured - ignore request");
  1502. return;
  1503. }
  1504. if (wpa_s->dpp_pkex) {
  1505. /* TODO: Support parallel operations */
  1506. wpa_printf(MSG_DEBUG,
  1507. "DPP: Already in PKEX session - ignore new request");
  1508. return;
  1509. }
  1510. wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
  1511. wpa_s->own_addr, src,
  1512. wpa_s->dpp_pkex_identifier,
  1513. wpa_s->dpp_pkex_code,
  1514. buf, len);
  1515. if (!wpa_s->dpp_pkex) {
  1516. wpa_printf(MSG_DEBUG,
  1517. "DPP: Failed to process the request - ignore it");
  1518. return;
  1519. }
  1520. msg = wpa_s->dpp_pkex->exchange_resp;
  1521. wait_time = wpa_s->max_remain_on_chan;
  1522. if (wait_time > 2000)
  1523. wait_time = 2000;
  1524. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1525. MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
  1526. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1527. broadcast,
  1528. wpabuf_head(msg), wpabuf_len(msg),
  1529. wait_time, wpas_dpp_tx_pkex_status, 0);
  1530. }
  1531. static void
  1532. wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1533. const u8 *buf, size_t len, unsigned int freq)
  1534. {
  1535. struct wpabuf *msg;
  1536. unsigned int wait_time;
  1537. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
  1538. MAC2STR(src));
  1539. /* TODO: Support multiple PKEX codes by iterating over all the enabled
  1540. * values here */
  1541. if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
  1542. wpa_s->dpp_pkex->exchange_done) {
  1543. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1544. return;
  1545. }
  1546. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  1547. wpa_s->dpp_pkex->exch_req_wait_time = 0;
  1548. msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
  1549. if (!msg) {
  1550. wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
  1551. return;
  1552. }
  1553. wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
  1554. MAC2STR(src));
  1555. wait_time = wpa_s->max_remain_on_chan;
  1556. if (wait_time > 2000)
  1557. wait_time = 2000;
  1558. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1559. MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
  1560. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1561. broadcast,
  1562. wpabuf_head(msg), wpabuf_len(msg),
  1563. wait_time, wpas_dpp_tx_pkex_status, 0);
  1564. wpabuf_free(msg);
  1565. }
  1566. static struct dpp_bootstrap_info *
  1567. wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
  1568. unsigned int freq)
  1569. {
  1570. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1571. struct dpp_bootstrap_info *bi;
  1572. bi = os_zalloc(sizeof(*bi));
  1573. if (!bi)
  1574. return NULL;
  1575. bi->id = wpas_dpp_next_id(wpa_s);
  1576. bi->type = DPP_BOOTSTRAP_PKEX;
  1577. os_memcpy(bi->mac_addr, peer, ETH_ALEN);
  1578. bi->num_freq = 1;
  1579. bi->freq[0] = freq;
  1580. bi->curve = pkex->own_bi->curve;
  1581. bi->pubkey = pkex->peer_bootstrap_key;
  1582. pkex->peer_bootstrap_key = NULL;
  1583. dpp_pkex_free(pkex);
  1584. wpa_s->dpp_pkex = NULL;
  1585. if (dpp_bootstrap_key_hash(bi) < 0) {
  1586. dpp_bootstrap_info_free(bi);
  1587. return NULL;
  1588. }
  1589. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  1590. return bi;
  1591. }
  1592. static void
  1593. wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
  1594. const u8 *hdr, const u8 *buf, size_t len,
  1595. unsigned int freq)
  1596. {
  1597. struct wpabuf *msg;
  1598. unsigned int wait_time;
  1599. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1600. wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
  1601. MAC2STR(src));
  1602. if (!pkex || pkex->initiator || !pkex->exchange_done) {
  1603. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1604. return;
  1605. }
  1606. msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
  1607. if (!msg) {
  1608. wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
  1609. if (pkex->failed) {
  1610. wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
  1611. if (pkex->t > pkex->own_bi->pkex_t)
  1612. pkex->own_bi->pkex_t = pkex->t;
  1613. dpp_pkex_free(wpa_s->dpp_pkex);
  1614. wpa_s->dpp_pkex = NULL;
  1615. }
  1616. return;
  1617. }
  1618. wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
  1619. MACSTR, MAC2STR(src));
  1620. wait_time = wpa_s->max_remain_on_chan;
  1621. if (wait_time > 2000)
  1622. wait_time = 2000;
  1623. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1624. MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
  1625. offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
  1626. broadcast,
  1627. wpabuf_head(msg), wpabuf_len(msg),
  1628. wait_time, wpas_dpp_tx_pkex_status, 0);
  1629. wpabuf_free(msg);
  1630. wpas_dpp_pkex_finish(wpa_s, src, freq);
  1631. }
  1632. static void
  1633. wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  1634. const u8 *hdr, const u8 *buf, size_t len,
  1635. unsigned int freq)
  1636. {
  1637. int res;
  1638. struct dpp_bootstrap_info *bi;
  1639. struct dpp_pkex *pkex = wpa_s->dpp_pkex;
  1640. char cmd[500];
  1641. wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
  1642. MAC2STR(src));
  1643. if (!pkex || !pkex->initiator || !pkex->exchange_done) {
  1644. wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
  1645. return;
  1646. }
  1647. res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
  1648. if (res < 0) {
  1649. wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
  1650. return;
  1651. }
  1652. bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
  1653. if (!bi)
  1654. return;
  1655. os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
  1656. bi->id,
  1657. wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
  1658. wpa_printf(MSG_DEBUG,
  1659. "DPP: Start authentication after PKEX with parameters: %s",
  1660. cmd);
  1661. if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
  1662. wpa_printf(MSG_DEBUG,
  1663. "DPP: Authentication initialization failed");
  1664. return;
  1665. }
  1666. }
  1667. void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
  1668. const u8 *buf, size_t len, unsigned int freq)
  1669. {
  1670. u8 crypto_suite;
  1671. enum dpp_public_action_frame_type type;
  1672. const u8 *hdr;
  1673. unsigned int pkex_t;
  1674. if (len < DPP_HDR_LEN)
  1675. return;
  1676. if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
  1677. return;
  1678. hdr = buf;
  1679. buf += 4;
  1680. len -= 4;
  1681. crypto_suite = *buf++;
  1682. type = *buf++;
  1683. len -= 2;
  1684. wpa_printf(MSG_DEBUG,
  1685. "DPP: Received DPP Public Action frame crypto suite %u type %d from "
  1686. MACSTR " freq=%u",
  1687. crypto_suite, type, MAC2STR(src), freq);
  1688. if (crypto_suite != 1) {
  1689. wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
  1690. crypto_suite);
  1691. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
  1692. " freq=%u type=%d ignore=unsupported-crypto-suite",
  1693. MAC2STR(src), freq, type);
  1694. return;
  1695. }
  1696. wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
  1697. if (dpp_check_attrs(buf, len) < 0) {
  1698. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
  1699. " freq=%u type=%d ignore=invalid-attributes",
  1700. MAC2STR(src), freq, type);
  1701. return;
  1702. }
  1703. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
  1704. MAC2STR(src), freq, type);
  1705. switch (type) {
  1706. case DPP_PA_AUTHENTICATION_REQ:
  1707. wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
  1708. break;
  1709. case DPP_PA_AUTHENTICATION_RESP:
  1710. wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
  1711. break;
  1712. case DPP_PA_AUTHENTICATION_CONF:
  1713. wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
  1714. break;
  1715. case DPP_PA_PEER_DISCOVERY_RESP:
  1716. wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
  1717. break;
  1718. case DPP_PA_PKEX_EXCHANGE_REQ:
  1719. wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
  1720. break;
  1721. case DPP_PA_PKEX_EXCHANGE_RESP:
  1722. wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
  1723. break;
  1724. case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
  1725. wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
  1726. freq);
  1727. break;
  1728. case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
  1729. wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
  1730. freq);
  1731. break;
  1732. default:
  1733. wpa_printf(MSG_DEBUG,
  1734. "DPP: Ignored unsupported frame subtype %d", type);
  1735. break;
  1736. }
  1737. if (wpa_s->dpp_pkex)
  1738. pkex_t = wpa_s->dpp_pkex->t;
  1739. else if (wpa_s->dpp_pkex_bi)
  1740. pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
  1741. else
  1742. pkex_t = 0;
  1743. if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
  1744. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
  1745. wpas_dpp_pkex_remove(wpa_s, "*");
  1746. }
  1747. }
  1748. static struct wpabuf *
  1749. wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
  1750. size_t query_len)
  1751. {
  1752. struct wpa_supplicant *wpa_s = ctx;
  1753. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1754. struct wpabuf *resp;
  1755. wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
  1756. MAC2STR(sa));
  1757. if (!auth || !auth->auth_success ||
  1758. os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1759. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1760. return NULL;
  1761. }
  1762. wpa_hexdump(MSG_DEBUG,
  1763. "DPP: Received Configuration Request (GAS Query Request)",
  1764. query, query_len);
  1765. resp = dpp_conf_req_rx(auth, query, query_len);
  1766. if (!resp)
  1767. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1768. auth->conf_resp = resp;
  1769. return resp;
  1770. }
  1771. static void
  1772. wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
  1773. {
  1774. struct wpa_supplicant *wpa_s = ctx;
  1775. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1776. if (!auth) {
  1777. wpabuf_free(resp);
  1778. return;
  1779. }
  1780. if (auth->conf_resp != resp) {
  1781. wpa_printf(MSG_DEBUG,
  1782. "DPP: Ignore GAS status report (ok=%d) for unknown response",
  1783. ok);
  1784. wpabuf_free(resp);
  1785. return;
  1786. }
  1787. wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
  1788. ok);
  1789. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1790. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  1791. offchannel_send_action_done(wpa_s);
  1792. wpas_dpp_listen_stop(wpa_s);
  1793. if (ok)
  1794. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
  1795. else
  1796. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1797. dpp_auth_deinit(wpa_s->dpp_auth);
  1798. wpa_s->dpp_auth = NULL;
  1799. wpabuf_free(resp);
  1800. }
  1801. static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
  1802. {
  1803. struct dpp_configurator *conf;
  1804. unsigned int max_id = 0;
  1805. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  1806. struct dpp_configurator, list) {
  1807. if (conf->id > max_id)
  1808. max_id = conf->id;
  1809. }
  1810. return max_id + 1;
  1811. }
  1812. int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
  1813. {
  1814. char *curve = NULL;
  1815. char *key = NULL;
  1816. u8 *privkey = NULL;
  1817. size_t privkey_len = 0;
  1818. int ret = -1;
  1819. struct dpp_configurator *conf = NULL;
  1820. curve = get_param(cmd, " curve=");
  1821. key = get_param(cmd, " key=");
  1822. if (key) {
  1823. privkey_len = os_strlen(key) / 2;
  1824. privkey = os_malloc(privkey_len);
  1825. if (!privkey ||
  1826. hexstr2bin(key, privkey, privkey_len) < 0)
  1827. goto fail;
  1828. }
  1829. conf = dpp_keygen_configurator(curve, privkey, privkey_len);
  1830. if (!conf)
  1831. goto fail;
  1832. conf->id = wpas_dpp_next_configurator_id(wpa_s);
  1833. dl_list_add(&wpa_s->dpp_configurator, &conf->list);
  1834. ret = conf->id;
  1835. conf = NULL;
  1836. fail:
  1837. os_free(curve);
  1838. str_clear_free(key);
  1839. bin_clear_free(privkey, privkey_len);
  1840. dpp_configurator_free(conf);
  1841. return ret;
  1842. }
  1843. static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
  1844. {
  1845. struct dpp_configurator *conf, *tmp;
  1846. int found = 0;
  1847. dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
  1848. struct dpp_configurator, list) {
  1849. if (id && conf->id != id)
  1850. continue;
  1851. found = 1;
  1852. dl_list_del(&conf->list);
  1853. dpp_configurator_free(conf);
  1854. }
  1855. if (id == 0)
  1856. return 0; /* flush succeeds regardless of entries found */
  1857. return found ? 0 : -1;
  1858. }
  1859. int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
  1860. {
  1861. unsigned int id_val;
  1862. if (os_strcmp(id, "*") == 0) {
  1863. id_val = 0;
  1864. } else {
  1865. id_val = atoi(id);
  1866. if (id_val == 0)
  1867. return -1;
  1868. }
  1869. return dpp_configurator_del(wpa_s, id_val);
  1870. }
  1871. int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
  1872. {
  1873. struct dpp_authentication *auth;
  1874. int ret = -1;
  1875. char *curve = NULL;
  1876. auth = os_zalloc(sizeof(*auth));
  1877. if (!auth)
  1878. return -1;
  1879. curve = get_param(cmd, " curve=");
  1880. wpas_dpp_set_configurator(wpa_s, auth, cmd);
  1881. if (dpp_configurator_own_config(auth, curve, 0) == 0) {
  1882. wpas_dpp_handle_config_obj(wpa_s, auth);
  1883. ret = 0;
  1884. }
  1885. dpp_auth_deinit(auth);
  1886. os_free(curve);
  1887. return ret;
  1888. }
  1889. static void
  1890. wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
  1891. unsigned int freq, const u8 *dst,
  1892. const u8 *src, const u8 *bssid,
  1893. const u8 *data, size_t data_len,
  1894. enum offchannel_send_action_result result)
  1895. {
  1896. const char *res_txt;
  1897. res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1898. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1899. "FAILED");
  1900. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1901. " result=%s (DPP Peer Discovery Request)",
  1902. freq, MAC2STR(dst), res_txt);
  1903. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
  1904. " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
  1905. /* TODO: Time out wait for response more quickly in error cases? */
  1906. }
  1907. int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  1908. struct wpa_bss *bss)
  1909. {
  1910. struct os_time now;
  1911. struct wpabuf *msg;
  1912. unsigned int wait_time;
  1913. if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
  1914. return 0; /* Not using DPP AKM - continue */
  1915. if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
  1916. return 0; /* PMKSA exists for DPP AKM - continue */
  1917. if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1918. !ssid->dpp_csign) {
  1919. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1920. "missing %s",
  1921. !ssid->dpp_connector ? "Connector" :
  1922. (!ssid->dpp_netaccesskey ? "netAccessKey" :
  1923. "C-sign-key"));
  1924. return -1;
  1925. }
  1926. os_get_time(&now);
  1927. if (ssid->dpp_netaccesskey_expiry &&
  1928. (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
  1929. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1930. "netAccessKey expired");
  1931. return -1;
  1932. }
  1933. wpa_printf(MSG_DEBUG,
  1934. "DPP: Starting network introduction protocol to derive PMKSA for "
  1935. MACSTR, MAC2STR(bss->bssid));
  1936. msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
  1937. 5 + 4 + os_strlen(ssid->dpp_connector));
  1938. if (!msg)
  1939. return -1;
  1940. #ifdef CONFIG_TESTING_OPTIONS
  1941. if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
  1942. wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
  1943. goto skip_trans_id;
  1944. }
  1945. if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
  1946. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
  1947. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  1948. wpabuf_put_le16(msg, 0);
  1949. goto skip_trans_id;
  1950. }
  1951. #endif /* CONFIG_TESTING_OPTIONS */
  1952. /* Transaction ID */
  1953. wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
  1954. wpabuf_put_le16(msg, 1);
  1955. wpabuf_put_u8(msg, TRANSACTION_ID);
  1956. #ifdef CONFIG_TESTING_OPTIONS
  1957. skip_trans_id:
  1958. if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
  1959. wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
  1960. goto skip_connector;
  1961. }
  1962. if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
  1963. char *connector;
  1964. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
  1965. connector = dpp_corrupt_connector_signature(
  1966. ssid->dpp_connector);
  1967. if (!connector) {
  1968. wpabuf_free(msg);
  1969. return -1;
  1970. }
  1971. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  1972. wpabuf_put_le16(msg, os_strlen(connector));
  1973. wpabuf_put_str(msg, connector);
  1974. os_free(connector);
  1975. goto skip_connector;
  1976. }
  1977. #endif /* CONFIG_TESTING_OPTIONS */
  1978. /* DPP Connector */
  1979. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  1980. wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
  1981. wpabuf_put_str(msg, ssid->dpp_connector);
  1982. #ifdef CONFIG_TESTING_OPTIONS
  1983. skip_connector:
  1984. #endif /* CONFIG_TESTING_OPTIONS */
  1985. /* TODO: Timeout on AP response */
  1986. wait_time = wpa_s->max_remain_on_chan;
  1987. if (wait_time > 2000)
  1988. wait_time = 2000;
  1989. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
  1990. MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
  1991. offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
  1992. broadcast,
  1993. wpabuf_head(msg), wpabuf_len(msg),
  1994. wait_time, wpas_dpp_tx_introduction_status, 0);
  1995. wpabuf_free(msg);
  1996. /* Request this connection attempt to terminate - new one will be
  1997. * started when network introduction protocol completes */
  1998. os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
  1999. wpa_s->dpp_intro_network = ssid;
  2000. return 1;
  2001. }
  2002. int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
  2003. {
  2004. struct dpp_bootstrap_info *own_bi;
  2005. const char *pos, *end;
  2006. unsigned int wait_time;
  2007. pos = os_strstr(cmd, " own=");
  2008. if (!pos)
  2009. return -1;
  2010. pos += 5;
  2011. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  2012. if (!own_bi) {
  2013. wpa_printf(MSG_DEBUG,
  2014. "DPP: Identified bootstrap info not found");
  2015. return -1;
  2016. }
  2017. if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
  2018. wpa_printf(MSG_DEBUG,
  2019. "DPP: Identified bootstrap info not for PKEX");
  2020. return -1;
  2021. }
  2022. wpa_s->dpp_pkex_bi = own_bi;
  2023. own_bi->pkex_t = 0; /* clear pending errors on new code */
  2024. os_free(wpa_s->dpp_pkex_identifier);
  2025. wpa_s->dpp_pkex_identifier = NULL;
  2026. pos = os_strstr(cmd, " identifier=");
  2027. if (pos) {
  2028. pos += 12;
  2029. end = os_strchr(pos, ' ');
  2030. if (!end)
  2031. return -1;
  2032. wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
  2033. if (!wpa_s->dpp_pkex_identifier)
  2034. return -1;
  2035. os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
  2036. wpa_s->dpp_pkex_identifier[end - pos] = '\0';
  2037. }
  2038. pos = os_strstr(cmd, " code=");
  2039. if (!pos)
  2040. return -1;
  2041. os_free(wpa_s->dpp_pkex_code);
  2042. wpa_s->dpp_pkex_code = os_strdup(pos + 6);
  2043. if (!wpa_s->dpp_pkex_code)
  2044. return -1;
  2045. if (os_strstr(cmd, " init=1")) {
  2046. struct dpp_pkex *pkex;
  2047. struct wpabuf *msg;
  2048. wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
  2049. dpp_pkex_free(wpa_s->dpp_pkex);
  2050. wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
  2051. wpa_s->dpp_pkex_identifier,
  2052. wpa_s->dpp_pkex_code);
  2053. pkex = wpa_s->dpp_pkex;
  2054. if (!pkex)
  2055. return -1;
  2056. msg = pkex->exchange_req;
  2057. wait_time = wpa_s->max_remain_on_chan;
  2058. if (wait_time > 2000)
  2059. wait_time = 2000;
  2060. /* TODO: Support for 5 GHz channels */
  2061. pkex->freq = 2437;
  2062. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
  2063. " freq=%u type=%d",
  2064. MAC2STR(broadcast), pkex->freq,
  2065. DPP_PA_PKEX_EXCHANGE_REQ);
  2066. offchannel_send_action(wpa_s, pkex->freq, broadcast,
  2067. wpa_s->own_addr, broadcast,
  2068. wpabuf_head(msg), wpabuf_len(msg),
  2069. wait_time, wpas_dpp_tx_pkex_status, 0);
  2070. if (wait_time == 0)
  2071. wait_time = 2000;
  2072. pkex->exch_req_wait_time = wait_time;
  2073. pkex->exch_req_tries = 1;
  2074. }
  2075. /* TODO: Support multiple PKEX info entries */
  2076. os_free(wpa_s->dpp_pkex_auth_cmd);
  2077. wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
  2078. return 1;
  2079. }
  2080. int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
  2081. {
  2082. unsigned int id_val;
  2083. if (os_strcmp(id, "*") == 0) {
  2084. id_val = 0;
  2085. } else {
  2086. id_val = atoi(id);
  2087. if (id_val == 0)
  2088. return -1;
  2089. }
  2090. if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
  2091. return -1;
  2092. /* TODO: Support multiple PKEX entries */
  2093. os_free(wpa_s->dpp_pkex_code);
  2094. wpa_s->dpp_pkex_code = NULL;
  2095. os_free(wpa_s->dpp_pkex_identifier);
  2096. wpa_s->dpp_pkex_identifier = NULL;
  2097. os_free(wpa_s->dpp_pkex_auth_cmd);
  2098. wpa_s->dpp_pkex_auth_cmd = NULL;
  2099. wpa_s->dpp_pkex_bi = NULL;
  2100. /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
  2101. dpp_pkex_free(wpa_s->dpp_pkex);
  2102. wpa_s->dpp_pkex = NULL;
  2103. return 0;
  2104. }
  2105. void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
  2106. {
  2107. dpp_auth_deinit(wpa_s->dpp_auth);
  2108. wpa_s->dpp_auth = NULL;
  2109. dpp_pkex_free(wpa_s->dpp_pkex);
  2110. wpa_s->dpp_pkex = NULL;
  2111. if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
  2112. gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
  2113. }
  2114. int wpas_dpp_init(struct wpa_supplicant *wpa_s)
  2115. {
  2116. u8 adv_proto_id[7];
  2117. adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
  2118. adv_proto_id[1] = 5;
  2119. WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
  2120. adv_proto_id[5] = DPP_OUI_TYPE;
  2121. adv_proto_id[6] = 0x01;
  2122. if (gas_server_register(wpa_s->gas_server, adv_proto_id,
  2123. sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
  2124. wpas_dpp_gas_status_handler, wpa_s) < 0)
  2125. return -1;
  2126. dl_list_init(&wpa_s->dpp_bootstrap);
  2127. dl_list_init(&wpa_s->dpp_configurator);
  2128. wpa_s->dpp_init_done = 1;
  2129. return 0;
  2130. }
  2131. void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
  2132. {
  2133. #ifdef CONFIG_TESTING_OPTIONS
  2134. os_free(wpa_s->dpp_config_obj_override);
  2135. wpa_s->dpp_config_obj_override = NULL;
  2136. os_free(wpa_s->dpp_discovery_override);
  2137. wpa_s->dpp_discovery_override = NULL;
  2138. os_free(wpa_s->dpp_groups_override);
  2139. wpa_s->dpp_groups_override = NULL;
  2140. wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
  2141. #endif /* CONFIG_TESTING_OPTIONS */
  2142. if (!wpa_s->dpp_init_done)
  2143. return;
  2144. eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
  2145. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  2146. eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
  2147. eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
  2148. offchannel_send_action_done(wpa_s);
  2149. wpas_dpp_listen_stop(wpa_s);
  2150. dpp_bootstrap_del(wpa_s, 0);
  2151. dpp_configurator_del(wpa_s, 0);
  2152. wpas_dpp_stop(wpa_s);
  2153. wpas_dpp_pkex_remove(wpa_s, "*");
  2154. os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
  2155. os_free(wpa_s->dpp_configurator_params);
  2156. wpa_s->dpp_configurator_params = NULL;
  2157. }