dpp_supplicant.c 56 KB

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