dpp_supplicant.c 51 KB

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