dpp_supplicant.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. static struct dpp_configurator *
  36. dpp_configurator_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  37. {
  38. struct dpp_configurator *conf;
  39. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  40. struct dpp_configurator, list) {
  41. if (conf->id == id)
  42. return conf;
  43. }
  44. return NULL;
  45. }
  46. static unsigned int wpas_dpp_next_id(struct wpa_supplicant *wpa_s)
  47. {
  48. struct dpp_bootstrap_info *bi;
  49. unsigned int max_id = 0;
  50. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  51. list) {
  52. if (bi->id > max_id)
  53. max_id = bi->id;
  54. }
  55. return max_id + 1;
  56. }
  57. /**
  58. * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
  59. * @wpa_s: Pointer to wpa_supplicant data
  60. * @cmd: DPP URI read from a QR Code
  61. * Returns: Identifier of the stored info or -1 on failure
  62. */
  63. int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
  64. {
  65. struct dpp_bootstrap_info *bi;
  66. struct dpp_authentication *auth = wpa_s->dpp_auth;
  67. bi = dpp_parse_qr_code(cmd);
  68. if (!bi)
  69. return -1;
  70. bi->id = wpas_dpp_next_id(wpa_s);
  71. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  72. if (auth && auth->response_pending &&
  73. dpp_notify_new_qr_code(auth, bi) == 1) {
  74. struct wpabuf *msg;
  75. wpa_printf(MSG_DEBUG,
  76. "DPP: Sending out pending authentication response");
  77. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP,
  78. wpabuf_len(auth->resp_attr));
  79. if (!msg)
  80. goto out;
  81. wpabuf_put_buf(msg, wpa_s->dpp_auth->resp_attr);
  82. offchannel_send_action(wpa_s, auth->curr_freq,
  83. auth->peer_mac_addr, wpa_s->own_addr,
  84. broadcast,
  85. wpabuf_head(msg), wpabuf_len(msg),
  86. 500, wpas_dpp_tx_status, 0);
  87. wpabuf_free(msg);
  88. }
  89. out:
  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
  128. goto fail;
  129. chan = get_param(cmd, " chan=");
  130. mac = get_param(cmd, " mac=");
  131. info = get_param(cmd, " info=");
  132. curve = get_param(cmd, " curve=");
  133. key = get_param(cmd, " key=");
  134. if (key) {
  135. privkey_len = os_strlen(key) / 2;
  136. privkey = os_malloc(privkey_len);
  137. if (!privkey ||
  138. hexstr2bin(key, privkey, privkey_len) < 0)
  139. goto fail;
  140. }
  141. pk = dpp_keygen(bi, curve, privkey, privkey_len);
  142. if (!pk)
  143. goto fail;
  144. len = 4; /* "DPP:" */
  145. if (chan) {
  146. if (dpp_parse_uri_chan_list(bi, chan) < 0)
  147. goto fail;
  148. len += 3 + os_strlen(chan); /* C:...; */
  149. }
  150. if (mac) {
  151. if (dpp_parse_uri_mac(bi, mac) < 0)
  152. goto fail;
  153. len += 3 + os_strlen(mac); /* M:...; */
  154. }
  155. if (info) {
  156. if (dpp_parse_uri_info(bi, info) < 0)
  157. goto fail;
  158. len += 3 + os_strlen(info); /* I:...; */
  159. }
  160. len += 4 + os_strlen(pk);
  161. bi->uri = os_malloc(len + 1);
  162. if (!bi->uri)
  163. goto fail;
  164. os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
  165. chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
  166. mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
  167. info ? "I:" : "", info ? info : "", info ? ";" : "",
  168. pk);
  169. bi->id = wpas_dpp_next_id(wpa_s);
  170. dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
  171. ret = bi->id;
  172. bi = NULL;
  173. fail:
  174. os_free(curve);
  175. os_free(pk);
  176. os_free(chan);
  177. os_free(mac);
  178. os_free(info);
  179. str_clear_free(key);
  180. bin_clear_free(privkey, privkey_len);
  181. dpp_bootstrap_info_free(bi);
  182. return ret;
  183. }
  184. static struct dpp_bootstrap_info *
  185. dpp_bootstrap_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  186. {
  187. struct dpp_bootstrap_info *bi;
  188. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  189. list) {
  190. if (bi->id == id)
  191. return bi;
  192. }
  193. return NULL;
  194. }
  195. static int dpp_bootstrap_del(struct wpa_supplicant *wpa_s, unsigned int id)
  196. {
  197. struct dpp_bootstrap_info *bi, *tmp;
  198. int found = 0;
  199. dl_list_for_each_safe(bi, tmp, &wpa_s->dpp_bootstrap,
  200. struct dpp_bootstrap_info, list) {
  201. if (id && bi->id != id)
  202. continue;
  203. found = 1;
  204. dl_list_del(&bi->list);
  205. dpp_bootstrap_info_free(bi);
  206. }
  207. if (id == 0)
  208. return 0; /* flush succeeds regardless of entries found */
  209. return found ? 0 : -1;
  210. }
  211. int wpas_dpp_bootstrap_remove(struct wpa_supplicant *wpa_s, const char *id)
  212. {
  213. unsigned int id_val;
  214. if (os_strcmp(id, "*") == 0) {
  215. id_val = 0;
  216. } else {
  217. id_val = atoi(id);
  218. if (id_val == 0)
  219. return -1;
  220. }
  221. return dpp_bootstrap_del(wpa_s, id_val);
  222. }
  223. const char * wpas_dpp_bootstrap_get_uri(struct wpa_supplicant *wpa_s,
  224. unsigned int id)
  225. {
  226. struct dpp_bootstrap_info *bi;
  227. bi = dpp_bootstrap_get_id(wpa_s, id);
  228. if (!bi)
  229. return NULL;
  230. return bi->uri;
  231. }
  232. static const char * wpas_dpp_bootstrap_type(enum dpp_bootstrap_type type)
  233. {
  234. switch (type) {
  235. case DPP_BOOTSTRAP_QR_CODE:
  236. return "QRCODE";
  237. }
  238. return "??";
  239. }
  240. int wpas_dpp_bootstrap_info(struct wpa_supplicant *wpa_s, int id,
  241. char *reply, int reply_size)
  242. {
  243. struct dpp_bootstrap_info *bi;
  244. bi = dpp_bootstrap_get_id(wpa_s, id);
  245. if (!bi)
  246. return -1;
  247. return os_snprintf(reply, reply_size, "type=%s\n"
  248. "mac_addr=" MACSTR "\n"
  249. "info=%s\n"
  250. "num_freq=%u\n"
  251. "curve=%s\n",
  252. wpas_dpp_bootstrap_type(bi->type),
  253. MAC2STR(bi->mac_addr),
  254. bi->info ? bi->info : "",
  255. bi->num_freq,
  256. bi->curve->name);
  257. }
  258. static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
  259. unsigned int freq, const u8 *dst,
  260. const u8 *src, const u8 *bssid,
  261. const u8 *data, size_t data_len,
  262. enum offchannel_send_action_result result)
  263. {
  264. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  265. " result=%s",
  266. freq, MAC2STR(dst),
  267. result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  268. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  269. "FAILED"));
  270. if (!wpa_s->dpp_auth) {
  271. wpa_printf(MSG_DEBUG,
  272. "DPP: Ignore TX status since there is no ongoing authentication exchange");
  273. return;
  274. }
  275. if (wpa_s->dpp_auth->remove_on_tx_status) {
  276. wpa_printf(MSG_DEBUG,
  277. "DPP: Terminate authentication exchange due to an earlier error");
  278. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  279. offchannel_send_action_done(wpa_s);
  280. dpp_auth_deinit(wpa_s->dpp_auth);
  281. wpa_s->dpp_auth = NULL;
  282. return;
  283. }
  284. if (wpa_s->dpp_auth_ok_on_ack)
  285. wpas_dpp_auth_success(wpa_s, 1);
  286. if (!is_broadcast_ether_addr(dst) &&
  287. result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
  288. wpa_printf(MSG_DEBUG,
  289. "DPP: Unicast DPP Action frame was not ACKed");
  290. /* TODO: In case of DPP Authentication Request frame, move to
  291. * the next channel immediately */
  292. }
  293. }
  294. static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
  295. {
  296. struct wpa_supplicant *wpa_s = eloop_ctx;
  297. if (!wpa_s->dpp_auth)
  298. return;
  299. wpa_printf(MSG_DEBUG, "DPP: Continue reply wait on channel %u MHz",
  300. wpa_s->dpp_auth->curr_freq);
  301. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->curr_freq);
  302. }
  303. static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
  304. struct dpp_authentication *auth)
  305. {
  306. #ifdef CONFIG_TESTING_OPTIONS
  307. if (wpa_s->dpp_config_obj_override)
  308. auth->config_obj_override =
  309. os_strdup(wpa_s->dpp_config_obj_override);
  310. if (wpa_s->dpp_discovery_override)
  311. auth->discovery_override =
  312. os_strdup(wpa_s->dpp_discovery_override);
  313. if (wpa_s->dpp_groups_override)
  314. auth->groups_override =
  315. os_strdup(wpa_s->dpp_groups_override);
  316. if (wpa_s->dpp_devices_override)
  317. auth->devices_override =
  318. os_strdup(wpa_s->dpp_devices_override);
  319. auth->ignore_netaccesskey_mismatch =
  320. wpa_s->dpp_ignore_netaccesskey_mismatch;
  321. #endif /* CONFIG_TESTING_OPTIONS */
  322. }
  323. int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
  324. {
  325. const char *pos;
  326. struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
  327. struct wpabuf *msg;
  328. const u8 *dst;
  329. int res;
  330. int configurator = 1;
  331. unsigned int wait_time;
  332. struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
  333. struct dpp_configurator *conf = NULL;
  334. wpa_s->dpp_gas_client = 0;
  335. pos = os_strstr(cmd, " peer=");
  336. if (!pos)
  337. return -1;
  338. pos += 6;
  339. peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  340. if (!peer_bi) {
  341. wpa_printf(MSG_INFO,
  342. "DPP: Could not find bootstrapping info for the identified peer");
  343. return -1;
  344. }
  345. pos = os_strstr(cmd, " own=");
  346. if (pos) {
  347. pos += 5;
  348. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  349. if (!own_bi) {
  350. wpa_printf(MSG_INFO,
  351. "DPP: Could not find bootstrapping info for the identified local entry");
  352. return -1;
  353. }
  354. if (peer_bi->curve != own_bi->curve) {
  355. wpa_printf(MSG_INFO,
  356. "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
  357. peer_bi->curve->name, own_bi->curve->name);
  358. return -1;
  359. }
  360. }
  361. pos = os_strstr(cmd, " role=");
  362. if (pos) {
  363. pos += 6;
  364. if (os_strncmp(pos, "configurator", 12) == 0)
  365. configurator = 1;
  366. else if (os_strncmp(pos, "enrollee", 8) == 0)
  367. configurator = 0;
  368. else
  369. goto fail;
  370. }
  371. pos = os_strstr(cmd, " netrole=");
  372. if (pos) {
  373. pos += 9;
  374. wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
  375. }
  376. if (os_strstr(cmd, " conf=sta-")) {
  377. conf_sta = os_zalloc(sizeof(struct dpp_configuration));
  378. if (!conf_sta)
  379. goto fail;
  380. /* TODO: Configuration of network parameters from upper layers
  381. */
  382. os_memcpy(conf_sta->ssid, "test", 4);
  383. conf_sta->ssid_len = 4;
  384. if (os_strstr(cmd, " conf=sta-psk")) {
  385. conf_sta->dpp = 0;
  386. conf_sta->passphrase = os_strdup("secret passphrase");
  387. if (!conf_sta->passphrase)
  388. goto fail;
  389. } else if (os_strstr(cmd, " conf=sta-dpp")) {
  390. conf_sta->dpp = 1;
  391. } else {
  392. goto fail;
  393. }
  394. }
  395. if (os_strstr(cmd, " conf=ap-")) {
  396. conf_ap = os_zalloc(sizeof(struct dpp_configuration));
  397. if (!conf_ap)
  398. goto fail;
  399. /* TODO: Configuration of network parameters from upper layers
  400. */
  401. os_memcpy(conf_ap->ssid, "test", 4);
  402. conf_ap->ssid_len = 4;
  403. if (os_strstr(cmd, " conf=ap-psk")) {
  404. conf_ap->dpp = 0;
  405. conf_ap->passphrase = os_strdup("secret passphrase");
  406. if (!conf_ap->passphrase)
  407. goto fail;
  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. if (wpa_s->dpp_auth) {
  437. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  438. offchannel_send_action_done(wpa_s);
  439. dpp_auth_deinit(wpa_s->dpp_auth);
  440. }
  441. wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, configurator);
  442. if (!wpa_s->dpp_auth)
  443. goto fail;
  444. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  445. wpa_s->dpp_auth->conf_sta = conf_sta;
  446. wpa_s->dpp_auth->conf_ap = conf_ap;
  447. wpa_s->dpp_auth->conf = conf;
  448. /* TODO: Support iteration over all frequencies and filtering of
  449. * frequencies based on locally enabled channels that allow initiation
  450. * of transmission. */
  451. if (peer_bi->num_freq > 0)
  452. wpa_s->dpp_auth->curr_freq = peer_bi->freq[0];
  453. else
  454. wpa_s->dpp_auth->curr_freq = 2412;
  455. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_REQ,
  456. wpabuf_len(wpa_s->dpp_auth->req_attr));
  457. if (!msg)
  458. return -1;
  459. wpabuf_put_buf(msg, wpa_s->dpp_auth->req_attr);
  460. if (is_zero_ether_addr(peer_bi->mac_addr)) {
  461. dst = broadcast;
  462. } else {
  463. dst = peer_bi->mac_addr;
  464. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
  465. ETH_ALEN);
  466. }
  467. wpa_s->dpp_auth_ok_on_ack = 0;
  468. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  469. wait_time = wpa_s->max_remain_on_chan;
  470. if (wait_time > 2000)
  471. wait_time = 2000;
  472. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  473. wpas_dpp_reply_wait_timeout,
  474. wpa_s, NULL);
  475. res = offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  476. dst, wpa_s->own_addr, broadcast,
  477. wpabuf_head(msg), wpabuf_len(msg),
  478. wait_time, wpas_dpp_tx_status, 0);
  479. wpabuf_free(msg);
  480. return res;
  481. fail:
  482. dpp_configuration_free(conf_sta);
  483. dpp_configuration_free(conf_ap);
  484. return -1;
  485. }
  486. struct wpas_dpp_listen_work {
  487. unsigned int freq;
  488. unsigned int duration;
  489. struct wpabuf *probe_resp_ie;
  490. };
  491. static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
  492. {
  493. if (!lwork)
  494. return;
  495. os_free(lwork);
  496. }
  497. static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
  498. {
  499. struct wpas_dpp_listen_work *lwork;
  500. if (!wpa_s->dpp_listen_work)
  501. return;
  502. lwork = wpa_s->dpp_listen_work->ctx;
  503. wpas_dpp_listen_work_free(lwork);
  504. radio_work_done(wpa_s->dpp_listen_work);
  505. wpa_s->dpp_listen_work = NULL;
  506. }
  507. static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
  508. {
  509. struct wpa_supplicant *wpa_s = work->wpa_s;
  510. struct wpas_dpp_listen_work *lwork = work->ctx;
  511. if (deinit) {
  512. if (work->started) {
  513. wpa_s->dpp_listen_work = NULL;
  514. wpas_dpp_listen_stop(wpa_s);
  515. }
  516. wpas_dpp_listen_work_free(lwork);
  517. return;
  518. }
  519. wpa_s->dpp_listen_work = work;
  520. wpa_s->dpp_pending_listen_freq = lwork->freq;
  521. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
  522. wpa_s->max_remain_on_chan) < 0) {
  523. wpa_printf(MSG_DEBUG,
  524. "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
  525. lwork->freq);
  526. wpas_dpp_listen_work_done(wpa_s);
  527. wpa_s->dpp_pending_listen_freq = 0;
  528. return;
  529. }
  530. wpa_s->off_channel_freq = 0;
  531. wpa_s->roc_waiting_drv_freq = lwork->freq;
  532. }
  533. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  534. unsigned int freq)
  535. {
  536. struct wpas_dpp_listen_work *lwork;
  537. if (wpa_s->dpp_listen_work) {
  538. wpa_printf(MSG_DEBUG,
  539. "DPP: Reject start_listen since dpp_listen_work already exists");
  540. return -1;
  541. }
  542. if (wpa_s->dpp_listen_freq)
  543. wpas_dpp_listen_stop(wpa_s);
  544. wpa_s->dpp_listen_freq = freq;
  545. lwork = os_zalloc(sizeof(*lwork));
  546. if (!lwork)
  547. return -1;
  548. lwork->freq = freq;
  549. if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
  550. lwork) < 0) {
  551. wpas_dpp_listen_work_free(lwork);
  552. return -1;
  553. }
  554. return 0;
  555. }
  556. int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
  557. {
  558. int freq;
  559. freq = atoi(cmd);
  560. if (freq <= 0)
  561. return -1;
  562. if (os_strstr(cmd, " role=configurator"))
  563. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
  564. else if (os_strstr(cmd, " role=enrollee"))
  565. wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
  566. else
  567. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
  568. DPP_CAPAB_ENROLLEE;
  569. wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
  570. wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
  571. if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
  572. wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
  573. freq);
  574. return 0;
  575. }
  576. return wpas_dpp_listen_start(wpa_s, freq);
  577. }
  578. void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
  579. {
  580. if (!wpa_s->dpp_listen_freq)
  581. return;
  582. wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
  583. wpa_s->dpp_listen_freq);
  584. wpa_drv_cancel_remain_on_channel(wpa_s);
  585. wpa_s->dpp_listen_freq = 0;
  586. wpas_dpp_listen_work_done(wpa_s);
  587. }
  588. void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  589. unsigned int freq)
  590. {
  591. if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
  592. return;
  593. wpa_printf(MSG_DEBUG,
  594. "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
  595. wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
  596. wpa_s->roc_waiting_drv_freq, freq);
  597. if (wpa_s->off_channel_freq &&
  598. wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
  599. wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
  600. wpa_s->dpp_pending_listen_freq = 0;
  601. } else {
  602. wpa_printf(MSG_DEBUG,
  603. "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
  604. wpa_s->off_channel_freq,
  605. wpa_s->dpp_pending_listen_freq, freq);
  606. }
  607. }
  608. void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  609. unsigned int freq)
  610. {
  611. wpas_dpp_listen_work_done(wpa_s);
  612. if (wpa_s->dpp_auth && !wpa_s->dpp_gas_client) {
  613. /* Continue listen with a new remain-on-channel */
  614. wpa_printf(MSG_DEBUG,
  615. "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
  616. wpa_s->dpp_auth->curr_freq);
  617. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->curr_freq);
  618. return;
  619. }
  620. if (wpa_s->dpp_listen_freq) {
  621. /* Continue listen with a new remain-on-channel */
  622. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
  623. }
  624. }
  625. static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
  626. const u8 *buf, size_t len, unsigned int freq)
  627. {
  628. const u8 *r_bootstrap, *i_bootstrap, *wrapped_data;
  629. u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len;
  630. struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
  631. struct wpabuf *msg;
  632. wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
  633. MAC2STR(src));
  634. wrapped_data = dpp_get_attr(buf, len, DPP_ATTR_WRAPPED_DATA,
  635. &wrapped_data_len);
  636. if (!wrapped_data) {
  637. wpa_printf(MSG_DEBUG,
  638. "DPP: Missing required Wrapped data attribute");
  639. return;
  640. }
  641. wpa_hexdump(MSG_MSGDUMP, "DPP: Wrapped data",
  642. wrapped_data, wrapped_data_len);
  643. r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  644. &r_bootstrap_len);
  645. if (!r_bootstrap || r_bootstrap > wrapped_data ||
  646. r_bootstrap_len != SHA256_MAC_LEN) {
  647. wpa_printf(MSG_DEBUG,
  648. "DPP: Missing or invalid required Responder Bootstrapping Key Hash attribute");
  649. return;
  650. }
  651. wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
  652. r_bootstrap, r_bootstrap_len);
  653. i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  654. &i_bootstrap_len);
  655. if (!i_bootstrap || i_bootstrap > wrapped_data ||
  656. i_bootstrap_len != SHA256_MAC_LEN) {
  657. wpa_printf(MSG_DEBUG,
  658. "DPP: Missing or invalid required Initiator Bootstrapping Key Hash attribute");
  659. return;
  660. }
  661. wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
  662. i_bootstrap, i_bootstrap_len);
  663. /* Try to find own and peer bootstrapping key matches based on the
  664. * received hash values */
  665. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  666. list) {
  667. if (!own_bi && bi->own &&
  668. os_memcmp(bi->pubkey_hash, r_bootstrap,
  669. SHA256_MAC_LEN) == 0) {
  670. wpa_printf(MSG_DEBUG,
  671. "DPP: Found matching own bootstrapping information");
  672. own_bi = bi;
  673. }
  674. if (!peer_bi && !bi->own &&
  675. os_memcmp(bi->pubkey_hash, i_bootstrap,
  676. SHA256_MAC_LEN) == 0) {
  677. wpa_printf(MSG_DEBUG,
  678. "DPP: Found matching peer bootstrapping information");
  679. peer_bi = bi;
  680. }
  681. if (own_bi && peer_bi)
  682. break;
  683. }
  684. if (!own_bi) {
  685. wpa_printf(MSG_DEBUG,
  686. "DPP: No matching own bootstrapping key found - ignore message");
  687. return;
  688. }
  689. if (wpa_s->dpp_auth) {
  690. wpa_printf(MSG_DEBUG,
  691. "DPP: Already in DPP authentication exchange - ignore new one");
  692. return;
  693. }
  694. wpa_s->dpp_gas_client = 0;
  695. wpa_s->dpp_auth_ok_on_ack = 0;
  696. wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
  697. wpa_s->dpp_qr_mutual,
  698. peer_bi, own_bi, freq, buf,
  699. wrapped_data, wrapped_data_len);
  700. if (!wpa_s->dpp_auth) {
  701. wpa_printf(MSG_DEBUG, "DPP: No response generated");
  702. return;
  703. }
  704. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  705. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
  706. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP,
  707. wpabuf_len(wpa_s->dpp_auth->resp_attr));
  708. if (!msg)
  709. return;
  710. wpabuf_put_buf(msg, wpa_s->dpp_auth->resp_attr);
  711. offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  712. src, wpa_s->own_addr, broadcast,
  713. wpabuf_head(msg), wpabuf_len(msg),
  714. 500, wpas_dpp_tx_status, 0);
  715. wpabuf_free(msg);
  716. }
  717. static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
  718. {
  719. /* TODO: stop wait and start ROC */
  720. }
  721. static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
  722. struct dpp_authentication *auth)
  723. {
  724. struct wpa_ssid *ssid;
  725. ssid = wpa_config_add_network(wpa_s->conf);
  726. if (!ssid)
  727. return NULL;
  728. wpas_notify_network_added(wpa_s, ssid);
  729. wpa_config_set_network_defaults(ssid);
  730. ssid->disabled = 1;
  731. ssid->ssid = os_malloc(auth->ssid_len);
  732. if (!ssid->ssid)
  733. goto fail;
  734. os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
  735. ssid->ssid_len = auth->ssid_len;
  736. if (auth->connector) {
  737. ssid->key_mgmt = WPA_KEY_MGMT_DPP;
  738. ssid->dpp_connector = os_strdup(auth->connector);
  739. if (!ssid->dpp_connector)
  740. goto fail;
  741. }
  742. if (auth->c_sign_key) {
  743. ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
  744. if (!ssid->dpp_csign)
  745. goto fail;
  746. os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
  747. wpabuf_len(auth->c_sign_key));
  748. ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
  749. ssid->dpp_csign_expiry = auth->c_sign_key_expiry;
  750. }
  751. if (auth->net_access_key) {
  752. ssid->dpp_netaccesskey =
  753. os_malloc(wpabuf_len(auth->net_access_key));
  754. if (!ssid->dpp_netaccesskey)
  755. goto fail;
  756. os_memcpy(ssid->dpp_netaccesskey,
  757. wpabuf_head(auth->net_access_key),
  758. wpabuf_len(auth->net_access_key));
  759. ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
  760. ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
  761. }
  762. if (!auth->connector) {
  763. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  764. if (auth->passphrase[0]) {
  765. if (wpa_config_set_quoted(ssid, "psk",
  766. auth->passphrase) < 0)
  767. goto fail;
  768. wpa_config_update_psk(ssid);
  769. ssid->export_keys = 1;
  770. } else {
  771. ssid->psk_set = auth->psk_set;
  772. os_memcpy(ssid->psk, auth->psk, PMK_LEN);
  773. }
  774. }
  775. return ssid;
  776. fail:
  777. wpas_notify_network_removed(wpa_s, ssid);
  778. wpa_config_remove_network(wpa_s->conf, ssid->id);
  779. return NULL;
  780. }
  781. static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
  782. struct dpp_authentication *auth)
  783. {
  784. struct wpa_ssid *ssid;
  785. if (wpa_s->conf->dpp_config_processing < 1)
  786. return;
  787. ssid = wpas_dpp_add_network(wpa_s, auth);
  788. if (!ssid)
  789. return;
  790. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
  791. if (wpa_s->conf->dpp_config_processing < 2)
  792. return;
  793. wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
  794. ssid->disabled = 0;
  795. wpa_s->disconnected = 0;
  796. wpa_s->reassociate = 1;
  797. wpa_s->scan_runs = 0;
  798. wpa_s->normal_scans = 0;
  799. wpa_supplicant_cancel_sched_scan(wpa_s);
  800. wpa_supplicant_req_scan(wpa_s, 0, 0);
  801. }
  802. static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  803. enum gas_query_result result,
  804. const struct wpabuf *adv_proto,
  805. const struct wpabuf *resp, u16 status_code)
  806. {
  807. struct wpa_supplicant *wpa_s = ctx;
  808. const u8 *pos;
  809. struct dpp_authentication *auth = wpa_s->dpp_auth;
  810. if (!auth || !auth->auth_success) {
  811. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  812. return;
  813. }
  814. if (!resp || status_code != WLAN_STATUS_SUCCESS) {
  815. wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
  816. goto fail;
  817. }
  818. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
  819. adv_proto);
  820. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
  821. resp);
  822. if (wpabuf_len(adv_proto) != 10 ||
  823. !(pos = wpabuf_head(adv_proto)) ||
  824. pos[0] != WLAN_EID_ADV_PROTO ||
  825. pos[1] != 8 ||
  826. pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
  827. pos[4] != 5 ||
  828. WPA_GET_BE24(&pos[5]) != OUI_WFA ||
  829. pos[8] != 0x1a ||
  830. pos[9] != 1) {
  831. wpa_printf(MSG_DEBUG,
  832. "DPP: Not a DPP Advertisement Protocol ID");
  833. goto fail;
  834. }
  835. if (dpp_conf_resp_rx(auth, resp) < 0) {
  836. wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
  837. goto fail;
  838. }
  839. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
  840. if (auth->ssid_len)
  841. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
  842. wpa_ssid_txt(auth->ssid, auth->ssid_len));
  843. if (auth->connector) {
  844. /* TODO: Save the Connector and consider using a command
  845. * to fetch the value instead of sending an event with
  846. * it. The Connector could end up being larger than what
  847. * most clients are ready to receive as an event
  848. * message. */
  849. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
  850. auth->connector);
  851. }
  852. if (auth->c_sign_key) {
  853. char *hex;
  854. size_t hexlen;
  855. hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
  856. hex = os_malloc(hexlen);
  857. if (hex) {
  858. wpa_snprintf_hex(hex, hexlen,
  859. wpabuf_head(auth->c_sign_key),
  860. wpabuf_len(auth->c_sign_key));
  861. if (auth->c_sign_key_expiry)
  862. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY
  863. "%s %lu", hex,
  864. (long unsigned)
  865. auth->c_sign_key_expiry);
  866. else
  867. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY
  868. "%s", hex);
  869. os_free(hex);
  870. }
  871. }
  872. if (auth->net_access_key) {
  873. char *hex;
  874. size_t hexlen;
  875. hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
  876. hex = os_malloc(hexlen);
  877. if (hex) {
  878. wpa_snprintf_hex(hex, hexlen,
  879. wpabuf_head(auth->net_access_key),
  880. wpabuf_len(auth->net_access_key));
  881. if (auth->net_access_key_expiry)
  882. wpa_msg(wpa_s, MSG_INFO,
  883. DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
  884. (long unsigned)
  885. auth->net_access_key_expiry);
  886. else
  887. wpa_msg(wpa_s, MSG_INFO,
  888. DPP_EVENT_NET_ACCESS_KEY "%s", hex);
  889. os_free(hex);
  890. }
  891. }
  892. wpas_dpp_process_config(wpa_s, auth);
  893. dpp_auth_deinit(wpa_s->dpp_auth);
  894. wpa_s->dpp_auth = NULL;
  895. return;
  896. fail:
  897. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  898. dpp_auth_deinit(wpa_s->dpp_auth);
  899. wpa_s->dpp_auth = NULL;
  900. }
  901. static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
  902. {
  903. struct dpp_authentication *auth = wpa_s->dpp_auth;
  904. struct wpabuf *buf, *conf_req;
  905. char json[100];
  906. int res;
  907. wpa_s->dpp_gas_client = 1;
  908. os_snprintf(json, sizeof(json),
  909. "{\"name\":\"Test\","
  910. "\"wi-fi_tech\":\"infra\","
  911. "\"netRole\":\"%s\"}",
  912. wpa_s->dpp_netrole_ap ? "ap" : "sta");
  913. wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
  914. offchannel_send_action_done(wpa_s);
  915. wpas_dpp_listen_stop(wpa_s);
  916. conf_req = dpp_build_conf_req(auth, json);
  917. if (!conf_req) {
  918. wpa_printf(MSG_DEBUG,
  919. "DPP: No configuration request data available");
  920. return;
  921. }
  922. buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
  923. if (!buf) {
  924. wpabuf_free(conf_req);
  925. return;
  926. }
  927. /* Advertisement Protocol IE */
  928. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  929. wpabuf_put_u8(buf, 8); /* Length */
  930. wpabuf_put_u8(buf, 0x7f);
  931. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  932. wpabuf_put_u8(buf, 5);
  933. wpabuf_put_be24(buf, OUI_WFA);
  934. wpabuf_put_u8(buf, DPP_OUI_TYPE);
  935. wpabuf_put_u8(buf, 0x01);
  936. /* GAS Query */
  937. wpabuf_put_le16(buf, wpabuf_len(conf_req));
  938. wpabuf_put_buf(buf, conf_req);
  939. wpabuf_free(conf_req);
  940. wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
  941. MAC2STR(auth->peer_mac_addr), auth->curr_freq);
  942. res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
  943. buf, wpas_dpp_gas_resp_cb, wpa_s);
  944. if (res < 0) {
  945. wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
  946. wpabuf_free(buf);
  947. } else {
  948. wpa_printf(MSG_DEBUG,
  949. "DPP: GAS query started with dialog token %u", res);
  950. }
  951. }
  952. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
  953. {
  954. wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
  955. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
  956. if (wpa_s->dpp_auth->configurator)
  957. wpas_dpp_start_gas_server(wpa_s);
  958. else
  959. wpas_dpp_start_gas_client(wpa_s);
  960. }
  961. static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  962. const u8 *buf, size_t len)
  963. {
  964. struct dpp_authentication *auth = wpa_s->dpp_auth;
  965. struct wpabuf *msg, *attr;
  966. wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR,
  967. MAC2STR(src));
  968. if (!auth) {
  969. wpa_printf(MSG_DEBUG,
  970. "DPP: No DPP Authentication in progress - drop");
  971. return;
  972. }
  973. if (!is_zero_ether_addr(auth->peer_mac_addr) &&
  974. os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  975. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  976. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  977. return;
  978. }
  979. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  980. attr = dpp_auth_resp_rx(auth, buf, len);
  981. if (!attr) {
  982. if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
  983. wpa_printf(MSG_DEBUG,
  984. "DPP: Start wait for full response");
  985. offchannel_send_action_done(wpa_s);
  986. wpas_dpp_listen_start(wpa_s, auth->curr_freq);
  987. return;
  988. }
  989. wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
  990. return;
  991. }
  992. os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
  993. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_CONF, wpabuf_len(attr));
  994. if (!msg) {
  995. wpabuf_free(attr);
  996. return;
  997. }
  998. wpabuf_put_buf(msg, attr);
  999. wpabuf_free(attr);
  1000. offchannel_send_action(wpa_s, auth->curr_freq,
  1001. src, wpa_s->own_addr, broadcast,
  1002. wpabuf_head(msg), wpabuf_len(msg),
  1003. 500, wpas_dpp_tx_status, 0);
  1004. wpabuf_free(msg);
  1005. wpa_s->dpp_auth_ok_on_ack = 1;
  1006. }
  1007. static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
  1008. const u8 *buf, size_t len)
  1009. {
  1010. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1011. wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
  1012. MAC2STR(src));
  1013. if (!auth) {
  1014. wpa_printf(MSG_DEBUG,
  1015. "DPP: No DPP Authentication in progress - drop");
  1016. return;
  1017. }
  1018. if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1019. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1020. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1021. return;
  1022. }
  1023. if (dpp_auth_conf_rx(auth, buf, len) < 0) {
  1024. wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
  1025. return;
  1026. }
  1027. wpas_dpp_auth_success(wpa_s, 0);
  1028. }
  1029. static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
  1030. const u8 *src,
  1031. const u8 *buf, size_t len)
  1032. {
  1033. struct wpa_ssid *ssid;
  1034. const u8 *connector, *pk_hash, *nk_hash;
  1035. u16 connector_len, pk_hash_len, nk_hash_len;
  1036. struct dpp_introduction intro;
  1037. struct rsn_pmksa_cache_entry *entry;
  1038. wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
  1039. MAC2STR(src));
  1040. if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
  1041. os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
  1042. wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
  1043. MACSTR " - drop", MAC2STR(src));
  1044. return;
  1045. }
  1046. offchannel_send_action_done(wpa_s);
  1047. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1048. if (ssid == wpa_s->dpp_intro_network)
  1049. break;
  1050. }
  1051. if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1052. !ssid->dpp_csign) {
  1053. wpa_printf(MSG_DEBUG,
  1054. "DPP: Profile not found for network introduction");
  1055. return;
  1056. }
  1057. connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
  1058. if (!connector) {
  1059. wpa_printf(MSG_DEBUG,
  1060. "DPP: Peer did not include its Connector");
  1061. return;
  1062. }
  1063. if (dpp_peer_intro(&intro, ssid->dpp_connector,
  1064. ssid->dpp_netaccesskey,
  1065. ssid->dpp_netaccesskey_len,
  1066. ssid->dpp_csign,
  1067. ssid->dpp_csign_len,
  1068. connector, connector_len) < 0) {
  1069. wpa_printf(MSG_INFO,
  1070. "DPP: Network Introduction protocol resulted in failure");
  1071. goto fail;
  1072. }
  1073. pk_hash = dpp_get_attr(buf, len, DPP_ATTR_PEER_NET_PK_HASH,
  1074. &pk_hash_len);
  1075. if (!pk_hash || pk_hash_len != SHA256_MAC_LEN) {
  1076. wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(PK)");
  1077. goto fail;
  1078. }
  1079. if (os_memcmp(pk_hash, intro.nk_hash, SHA256_MAC_LEN) != 0) {
  1080. wpa_printf(MSG_DEBUG, "DPP: SHA256(PK) mismatch");
  1081. wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(PK)",
  1082. pk_hash, pk_hash_len);
  1083. wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(PK)",
  1084. intro.nk_hash, SHA256_MAC_LEN);
  1085. goto fail;
  1086. }
  1087. nk_hash = dpp_get_attr(buf, len, DPP_ATTR_OWN_NET_NK_HASH,
  1088. &nk_hash_len);
  1089. if (!nk_hash || nk_hash_len != SHA256_MAC_LEN) {
  1090. wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(NK)");
  1091. goto fail;
  1092. }
  1093. if (os_memcmp(nk_hash, intro.pk_hash, SHA256_MAC_LEN) != 0) {
  1094. wpa_printf(MSG_DEBUG, "DPP: SHA256(NK) mismatch");
  1095. wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(NK)",
  1096. nk_hash, nk_hash_len);
  1097. wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(NK)",
  1098. intro.pk_hash, SHA256_MAC_LEN);
  1099. goto fail;
  1100. }
  1101. entry = os_zalloc(sizeof(*entry));
  1102. if (!entry)
  1103. goto fail;
  1104. os_memcpy(entry->aa, src, ETH_ALEN);
  1105. os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
  1106. os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
  1107. entry->pmk_len = intro.pmk_len;
  1108. entry->akmp = WPA_KEY_MGMT_DPP;
  1109. /* TODO: expiration */
  1110. entry->network_ctx = ssid;
  1111. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  1112. wpa_printf(MSG_DEBUG,
  1113. "DPP: Try connection again after successful network introduction");
  1114. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  1115. wpa_supplicant_cancel_sched_scan(wpa_s);
  1116. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1117. }
  1118. fail:
  1119. os_memset(&intro, 0, sizeof(intro));
  1120. }
  1121. void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
  1122. const u8 *buf, size_t len, unsigned int freq)
  1123. {
  1124. enum dpp_public_action_frame_type type;
  1125. if (len < 1)
  1126. return;
  1127. type = buf[0];
  1128. buf++;
  1129. len--;
  1130. wpa_printf(MSG_DEBUG,
  1131. "DPP: Received DPP Public Action frame type %d from "
  1132. MACSTR " freq=%u",
  1133. type, MAC2STR(src), freq);
  1134. wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
  1135. if (dpp_check_attrs(buf, len) < 0)
  1136. return;
  1137. switch (type) {
  1138. case DPP_PA_AUTHENTICATION_REQ:
  1139. wpas_dpp_rx_auth_req(wpa_s, src, buf, len, freq);
  1140. break;
  1141. case DPP_PA_AUTHENTICATION_RESP:
  1142. wpas_dpp_rx_auth_resp(wpa_s, src, buf, len);
  1143. break;
  1144. case DPP_PA_AUTHENTICATION_CONF:
  1145. wpas_dpp_rx_auth_conf(wpa_s, src, buf, len);
  1146. break;
  1147. case DPP_PA_PEER_DISCOVERY_RESP:
  1148. wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
  1149. break;
  1150. default:
  1151. wpa_printf(MSG_DEBUG,
  1152. "DPP: Ignored unsupported frame subtype %d", type);
  1153. break;
  1154. }
  1155. }
  1156. static struct wpabuf *
  1157. wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
  1158. size_t query_len)
  1159. {
  1160. struct wpa_supplicant *wpa_s = ctx;
  1161. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1162. struct wpabuf *resp;
  1163. wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
  1164. MAC2STR(sa));
  1165. if (!auth || !auth->auth_success ||
  1166. os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1167. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1168. return NULL;
  1169. }
  1170. wpa_hexdump(MSG_DEBUG,
  1171. "DPP: Received Configuration Request (GAS Query Request)",
  1172. query, query_len);
  1173. resp = dpp_conf_req_rx(auth, query, query_len);
  1174. if (!resp)
  1175. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1176. return resp;
  1177. }
  1178. static void
  1179. wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
  1180. {
  1181. struct wpa_supplicant *wpa_s = ctx;
  1182. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1183. if (!auth) {
  1184. wpabuf_free(resp);
  1185. return;
  1186. }
  1187. wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
  1188. ok);
  1189. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1190. offchannel_send_action_done(wpa_s);
  1191. wpas_dpp_listen_stop(wpa_s);
  1192. if (ok)
  1193. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
  1194. else
  1195. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1196. dpp_auth_deinit(wpa_s->dpp_auth);
  1197. wpa_s->dpp_auth = NULL;
  1198. wpabuf_free(resp);
  1199. }
  1200. static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
  1201. {
  1202. struct dpp_configurator *conf;
  1203. unsigned int max_id = 0;
  1204. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  1205. struct dpp_configurator, list) {
  1206. if (conf->id > max_id)
  1207. max_id = conf->id;
  1208. }
  1209. return max_id + 1;
  1210. }
  1211. int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
  1212. {
  1213. char *expiry = NULL, *curve = NULL;
  1214. char *key = NULL;
  1215. u8 *privkey = NULL;
  1216. size_t privkey_len = 0;
  1217. int ret = -1;
  1218. struct dpp_configurator *conf = NULL;
  1219. expiry = get_param(cmd, " expiry=");
  1220. curve = get_param(cmd, " curve=");
  1221. key = get_param(cmd, " key=");
  1222. if (key) {
  1223. privkey_len = os_strlen(key) / 2;
  1224. privkey = os_malloc(privkey_len);
  1225. if (!privkey ||
  1226. hexstr2bin(key, privkey, privkey_len) < 0)
  1227. goto fail;
  1228. }
  1229. conf = dpp_keygen_configurator(curve, privkey, privkey_len);
  1230. if (!conf)
  1231. goto fail;
  1232. if (expiry) {
  1233. long int val;
  1234. val = strtol(expiry, NULL, 0);
  1235. if (val <= 0)
  1236. goto fail;
  1237. conf->csign_expiry = val;
  1238. }
  1239. conf->id = wpas_dpp_next_configurator_id(wpa_s);
  1240. dl_list_add(&wpa_s->dpp_configurator, &conf->list);
  1241. ret = conf->id;
  1242. conf = NULL;
  1243. fail:
  1244. os_free(curve);
  1245. os_free(expiry);
  1246. str_clear_free(key);
  1247. bin_clear_free(privkey, privkey_len);
  1248. dpp_configurator_free(conf);
  1249. return ret;
  1250. }
  1251. static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
  1252. {
  1253. struct dpp_configurator *conf, *tmp;
  1254. int found = 0;
  1255. dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
  1256. struct dpp_configurator, list) {
  1257. if (id && conf->id != id)
  1258. continue;
  1259. found = 1;
  1260. dl_list_del(&conf->list);
  1261. dpp_configurator_free(conf);
  1262. }
  1263. if (id == 0)
  1264. return 0; /* flush succeeds regardless of entries found */
  1265. return found ? 0 : -1;
  1266. }
  1267. int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
  1268. {
  1269. unsigned int id_val;
  1270. if (os_strcmp(id, "*") == 0) {
  1271. id_val = 0;
  1272. } else {
  1273. id_val = atoi(id);
  1274. if (id_val == 0)
  1275. return -1;
  1276. }
  1277. return dpp_configurator_del(wpa_s, id_val);
  1278. }
  1279. static void
  1280. wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
  1281. unsigned int freq, const u8 *dst,
  1282. const u8 *src, const u8 *bssid,
  1283. const u8 *data, size_t data_len,
  1284. enum offchannel_send_action_result result)
  1285. {
  1286. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1287. " result=%s (DPP Peer Discovery Request)",
  1288. freq, MAC2STR(dst),
  1289. result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1290. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1291. "FAILED"));
  1292. /* TODO: Time out wait for response more quickly in error cases? */
  1293. }
  1294. int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  1295. struct wpa_bss *bss)
  1296. {
  1297. struct os_time now;
  1298. struct wpabuf *msg;
  1299. unsigned int wait_time;
  1300. if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
  1301. return 0; /* Not using DPP AKM - continue */
  1302. if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
  1303. return 0; /* PMKSA exists for DPP AKM - continue */
  1304. if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1305. !ssid->dpp_csign) {
  1306. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1307. "missing %s",
  1308. !ssid->dpp_connector ? "Connector" :
  1309. (!ssid->dpp_netaccesskey ? "netAccessKey" :
  1310. "C-sign-key"));
  1311. return -1;
  1312. }
  1313. os_get_time(&now);
  1314. if (ssid->dpp_csign_expiry && ssid->dpp_csign_expiry < now.sec) {
  1315. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1316. "C-sign-key expired");
  1317. return -1;
  1318. }
  1319. if (ssid->dpp_netaccesskey_expiry &&
  1320. ssid->dpp_netaccesskey_expiry < now.sec) {
  1321. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1322. "netAccessKey expired");
  1323. return -1;
  1324. }
  1325. wpa_printf(MSG_DEBUG,
  1326. "DPP: Starting network introduction protocol to derive PMKSA for "
  1327. MACSTR, MAC2STR(bss->bssid));
  1328. msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
  1329. 4 + os_strlen(ssid->dpp_connector));
  1330. if (!msg)
  1331. return -1;
  1332. /* DPP Connector */
  1333. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  1334. wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
  1335. wpabuf_put_str(msg, ssid->dpp_connector);
  1336. /* TODO: Timeout on AP response */
  1337. wait_time = wpa_s->max_remain_on_chan;
  1338. if (wait_time > 2000)
  1339. wait_time = 2000;
  1340. offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
  1341. broadcast,
  1342. wpabuf_head(msg), wpabuf_len(msg),
  1343. wait_time, wpas_dpp_tx_introduction_status, 0);
  1344. wpabuf_free(msg);
  1345. /* Request this connection attempt to terminate - new one will be
  1346. * started when network introduction protocol completes */
  1347. os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
  1348. wpa_s->dpp_intro_network = ssid;
  1349. return 1;
  1350. }
  1351. int wpas_dpp_init(struct wpa_supplicant *wpa_s)
  1352. {
  1353. u8 adv_proto_id[7];
  1354. adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
  1355. adv_proto_id[1] = 5;
  1356. WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
  1357. adv_proto_id[5] = DPP_OUI_TYPE;
  1358. adv_proto_id[6] = 0x01;
  1359. if (gas_server_register(wpa_s->gas_server, adv_proto_id,
  1360. sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
  1361. wpas_dpp_gas_status_handler, wpa_s) < 0)
  1362. return -1;
  1363. dl_list_init(&wpa_s->dpp_bootstrap);
  1364. dl_list_init(&wpa_s->dpp_configurator);
  1365. wpa_s->dpp_init_done = 1;
  1366. return 0;
  1367. }
  1368. void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
  1369. {
  1370. #ifdef CONFIG_TESTING_OPTIONS
  1371. os_free(wpa_s->dpp_config_obj_override);
  1372. wpa_s->dpp_config_obj_override = NULL;
  1373. os_free(wpa_s->dpp_discovery_override);
  1374. wpa_s->dpp_discovery_override = NULL;
  1375. os_free(wpa_s->dpp_groups_override);
  1376. wpa_s->dpp_groups_override = NULL;
  1377. os_free(wpa_s->dpp_devices_override);
  1378. wpa_s->dpp_devices_override = NULL;
  1379. wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
  1380. #endif /* CONFIG_TESTING_OPTIONS */
  1381. if (!wpa_s->dpp_init_done)
  1382. return;
  1383. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1384. offchannel_send_action_done(wpa_s);
  1385. wpas_dpp_listen_stop(wpa_s);
  1386. dpp_bootstrap_del(wpa_s, 0);
  1387. dpp_configurator_del(wpa_s, 0);
  1388. dpp_auth_deinit(wpa_s->dpp_auth);
  1389. wpa_s->dpp_auth = NULL;
  1390. os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
  1391. }