dpp_supplicant.c 41 KB

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