dpp_supplicant.c 53 KB

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