dpp_supplicant.c 51 KB

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