dpp_supplicant.c 52 KB

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