dpp_supplicant.c 52 KB

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