dpp_supplicant.c 62 KB

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