dpp_supplicant.c 56 KB

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