dpp_supplicant.c 52 KB

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