dpp_supplicant.c 42 KB

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