dpp_supplicant.c 67 KB

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