dpp_supplicant.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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. wpa_s->dpp_gas_client = 0;
  337. pos = os_strstr(cmd, " peer=");
  338. if (!pos)
  339. return -1;
  340. pos += 6;
  341. peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  342. if (!peer_bi) {
  343. wpa_printf(MSG_INFO,
  344. "DPP: Could not find bootstrapping info for the identified peer");
  345. return -1;
  346. }
  347. pos = os_strstr(cmd, " own=");
  348. if (pos) {
  349. pos += 5;
  350. own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
  351. if (!own_bi) {
  352. wpa_printf(MSG_INFO,
  353. "DPP: Could not find bootstrapping info for the identified local entry");
  354. return -1;
  355. }
  356. if (peer_bi->curve != own_bi->curve) {
  357. wpa_printf(MSG_INFO,
  358. "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
  359. peer_bi->curve->name, own_bi->curve->name);
  360. return -1;
  361. }
  362. }
  363. pos = os_strstr(cmd, " role=");
  364. if (pos) {
  365. pos += 6;
  366. if (os_strncmp(pos, "configurator", 12) == 0)
  367. configurator = 1;
  368. else if (os_strncmp(pos, "enrollee", 8) == 0)
  369. configurator = 0;
  370. else
  371. goto fail;
  372. }
  373. pos = os_strstr(cmd, " netrole=");
  374. if (pos) {
  375. pos += 9;
  376. wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
  377. }
  378. pos = os_strstr(cmd, " ssid=");
  379. if (pos) {
  380. pos += 6;
  381. end = os_strchr(pos, ' ');
  382. ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
  383. ssid_len /= 2;
  384. if (ssid_len > sizeof(ssid) ||
  385. hexstr2bin(pos, ssid, ssid_len) < 0)
  386. goto fail;
  387. }
  388. if (os_strstr(cmd, " conf=sta-")) {
  389. conf_sta = os_zalloc(sizeof(struct dpp_configuration));
  390. if (!conf_sta)
  391. goto fail;
  392. /* TODO: Configuration of network parameters from upper layers
  393. */
  394. os_memcpy(conf_sta->ssid, ssid, ssid_len);
  395. conf_sta->ssid_len = ssid_len;
  396. if (os_strstr(cmd, " conf=sta-psk")) {
  397. conf_sta->dpp = 0;
  398. conf_sta->passphrase = os_strdup("secret passphrase");
  399. if (!conf_sta->passphrase)
  400. goto fail;
  401. } else if (os_strstr(cmd, " conf=sta-dpp")) {
  402. conf_sta->dpp = 1;
  403. } else {
  404. goto fail;
  405. }
  406. }
  407. if (os_strstr(cmd, " conf=ap-")) {
  408. conf_ap = os_zalloc(sizeof(struct dpp_configuration));
  409. if (!conf_ap)
  410. goto fail;
  411. /* TODO: Configuration of network parameters from upper layers
  412. */
  413. os_memcpy(conf_ap->ssid, ssid, ssid_len);
  414. conf_ap->ssid_len = ssid_len;
  415. if (os_strstr(cmd, " conf=ap-psk")) {
  416. conf_ap->dpp = 0;
  417. conf_ap->passphrase = os_strdup("secret passphrase");
  418. if (!conf_ap->passphrase)
  419. goto fail;
  420. } else if (os_strstr(cmd, " conf=ap-dpp")) {
  421. conf_ap->dpp = 1;
  422. } else {
  423. goto fail;
  424. }
  425. }
  426. pos = os_strstr(cmd, " expiry=");
  427. if (pos) {
  428. long int val;
  429. pos += 8;
  430. val = strtol(pos, NULL, 0);
  431. if (val <= 0)
  432. goto fail;
  433. if (conf_sta)
  434. conf_sta->netaccesskey_expiry = val;
  435. if (conf_ap)
  436. conf_ap->netaccesskey_expiry = val;
  437. }
  438. pos = os_strstr(cmd, " configurator=");
  439. if (pos) {
  440. pos += 14;
  441. conf = dpp_configurator_get_id(wpa_s, atoi(pos));
  442. if (!conf) {
  443. wpa_printf(MSG_INFO,
  444. "DPP: Could not find the specified configurator");
  445. goto fail;
  446. }
  447. }
  448. if (wpa_s->dpp_auth) {
  449. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  450. offchannel_send_action_done(wpa_s);
  451. dpp_auth_deinit(wpa_s->dpp_auth);
  452. }
  453. wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, configurator);
  454. if (!wpa_s->dpp_auth)
  455. goto fail;
  456. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  457. wpa_s->dpp_auth->conf_sta = conf_sta;
  458. wpa_s->dpp_auth->conf_ap = conf_ap;
  459. wpa_s->dpp_auth->conf = conf;
  460. /* TODO: Support iteration over all frequencies and filtering of
  461. * frequencies based on locally enabled channels that allow initiation
  462. * of transmission. */
  463. if (peer_bi->num_freq > 0)
  464. wpa_s->dpp_auth->curr_freq = peer_bi->freq[0];
  465. else
  466. wpa_s->dpp_auth->curr_freq = 2412;
  467. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_REQ,
  468. wpabuf_len(wpa_s->dpp_auth->req_attr));
  469. if (!msg)
  470. return -1;
  471. wpabuf_put_buf(msg, wpa_s->dpp_auth->req_attr);
  472. if (is_zero_ether_addr(peer_bi->mac_addr)) {
  473. dst = broadcast;
  474. } else {
  475. dst = peer_bi->mac_addr;
  476. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
  477. ETH_ALEN);
  478. }
  479. wpa_s->dpp_auth_ok_on_ack = 0;
  480. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  481. wait_time = wpa_s->max_remain_on_chan;
  482. if (wait_time > 2000)
  483. wait_time = 2000;
  484. eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
  485. wpas_dpp_reply_wait_timeout,
  486. wpa_s, NULL);
  487. res = offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  488. dst, wpa_s->own_addr, broadcast,
  489. wpabuf_head(msg), wpabuf_len(msg),
  490. wait_time, wpas_dpp_tx_status, 0);
  491. wpabuf_free(msg);
  492. return res;
  493. fail:
  494. dpp_configuration_free(conf_sta);
  495. dpp_configuration_free(conf_ap);
  496. return -1;
  497. }
  498. struct wpas_dpp_listen_work {
  499. unsigned int freq;
  500. unsigned int duration;
  501. struct wpabuf *probe_resp_ie;
  502. };
  503. static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
  504. {
  505. if (!lwork)
  506. return;
  507. os_free(lwork);
  508. }
  509. static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
  510. {
  511. struct wpas_dpp_listen_work *lwork;
  512. if (!wpa_s->dpp_listen_work)
  513. return;
  514. lwork = wpa_s->dpp_listen_work->ctx;
  515. wpas_dpp_listen_work_free(lwork);
  516. radio_work_done(wpa_s->dpp_listen_work);
  517. wpa_s->dpp_listen_work = NULL;
  518. }
  519. static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
  520. {
  521. struct wpa_supplicant *wpa_s = work->wpa_s;
  522. struct wpas_dpp_listen_work *lwork = work->ctx;
  523. if (deinit) {
  524. if (work->started) {
  525. wpa_s->dpp_listen_work = NULL;
  526. wpas_dpp_listen_stop(wpa_s);
  527. }
  528. wpas_dpp_listen_work_free(lwork);
  529. return;
  530. }
  531. wpa_s->dpp_listen_work = work;
  532. wpa_s->dpp_pending_listen_freq = lwork->freq;
  533. if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
  534. wpa_s->max_remain_on_chan) < 0) {
  535. wpa_printf(MSG_DEBUG,
  536. "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
  537. lwork->freq);
  538. wpas_dpp_listen_work_done(wpa_s);
  539. wpa_s->dpp_pending_listen_freq = 0;
  540. return;
  541. }
  542. wpa_s->off_channel_freq = 0;
  543. wpa_s->roc_waiting_drv_freq = lwork->freq;
  544. }
  545. static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
  546. unsigned int freq)
  547. {
  548. struct wpas_dpp_listen_work *lwork;
  549. if (wpa_s->dpp_listen_work) {
  550. wpa_printf(MSG_DEBUG,
  551. "DPP: Reject start_listen since dpp_listen_work already exists");
  552. return -1;
  553. }
  554. if (wpa_s->dpp_listen_freq)
  555. wpas_dpp_listen_stop(wpa_s);
  556. wpa_s->dpp_listen_freq = freq;
  557. lwork = os_zalloc(sizeof(*lwork));
  558. if (!lwork)
  559. return -1;
  560. lwork->freq = freq;
  561. if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
  562. lwork) < 0) {
  563. wpas_dpp_listen_work_free(lwork);
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
  569. {
  570. int freq;
  571. freq = atoi(cmd);
  572. if (freq <= 0)
  573. return -1;
  574. if (os_strstr(cmd, " role=configurator"))
  575. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
  576. else if (os_strstr(cmd, " role=enrollee"))
  577. wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
  578. else
  579. wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
  580. DPP_CAPAB_ENROLLEE;
  581. wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
  582. wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
  583. if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
  584. wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
  585. freq);
  586. return 0;
  587. }
  588. return wpas_dpp_listen_start(wpa_s, freq);
  589. }
  590. void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
  591. {
  592. if (!wpa_s->dpp_listen_freq)
  593. return;
  594. wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
  595. wpa_s->dpp_listen_freq);
  596. wpa_drv_cancel_remain_on_channel(wpa_s);
  597. wpa_s->dpp_listen_freq = 0;
  598. wpas_dpp_listen_work_done(wpa_s);
  599. }
  600. void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  601. unsigned int freq)
  602. {
  603. if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
  604. return;
  605. wpa_printf(MSG_DEBUG,
  606. "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
  607. wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
  608. wpa_s->roc_waiting_drv_freq, freq);
  609. if (wpa_s->off_channel_freq &&
  610. wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
  611. wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
  612. wpa_s->dpp_pending_listen_freq = 0;
  613. } else {
  614. wpa_printf(MSG_DEBUG,
  615. "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
  616. wpa_s->off_channel_freq,
  617. wpa_s->dpp_pending_listen_freq, freq);
  618. }
  619. }
  620. void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
  621. unsigned int freq)
  622. {
  623. wpas_dpp_listen_work_done(wpa_s);
  624. if (wpa_s->dpp_auth && !wpa_s->dpp_gas_client) {
  625. /* Continue listen with a new remain-on-channel */
  626. wpa_printf(MSG_DEBUG,
  627. "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
  628. wpa_s->dpp_auth->curr_freq);
  629. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->curr_freq);
  630. return;
  631. }
  632. if (wpa_s->dpp_listen_freq) {
  633. /* Continue listen with a new remain-on-channel */
  634. wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
  635. }
  636. }
  637. static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
  638. const u8 *buf, size_t len, unsigned int freq)
  639. {
  640. const u8 *r_bootstrap, *i_bootstrap, *wrapped_data;
  641. u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len;
  642. struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
  643. struct wpabuf *msg;
  644. wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
  645. MAC2STR(src));
  646. wrapped_data = dpp_get_attr(buf, len, DPP_ATTR_WRAPPED_DATA,
  647. &wrapped_data_len);
  648. if (!wrapped_data) {
  649. wpa_printf(MSG_DEBUG,
  650. "DPP: Missing required Wrapped data attribute");
  651. return;
  652. }
  653. wpa_hexdump(MSG_MSGDUMP, "DPP: Wrapped data",
  654. wrapped_data, wrapped_data_len);
  655. r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  656. &r_bootstrap_len);
  657. if (!r_bootstrap || r_bootstrap > wrapped_data ||
  658. r_bootstrap_len != SHA256_MAC_LEN) {
  659. wpa_printf(MSG_DEBUG,
  660. "DPP: Missing or invalid required Responder Bootstrapping Key Hash attribute");
  661. return;
  662. }
  663. wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
  664. r_bootstrap, r_bootstrap_len);
  665. i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  666. &i_bootstrap_len);
  667. if (!i_bootstrap || i_bootstrap > wrapped_data ||
  668. i_bootstrap_len != SHA256_MAC_LEN) {
  669. wpa_printf(MSG_DEBUG,
  670. "DPP: Missing or invalid required Initiator Bootstrapping Key Hash attribute");
  671. return;
  672. }
  673. wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
  674. i_bootstrap, i_bootstrap_len);
  675. /* Try to find own and peer bootstrapping key matches based on the
  676. * received hash values */
  677. dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
  678. list) {
  679. if (!own_bi && bi->own &&
  680. os_memcmp(bi->pubkey_hash, r_bootstrap,
  681. SHA256_MAC_LEN) == 0) {
  682. wpa_printf(MSG_DEBUG,
  683. "DPP: Found matching own bootstrapping information");
  684. own_bi = bi;
  685. }
  686. if (!peer_bi && !bi->own &&
  687. os_memcmp(bi->pubkey_hash, i_bootstrap,
  688. SHA256_MAC_LEN) == 0) {
  689. wpa_printf(MSG_DEBUG,
  690. "DPP: Found matching peer bootstrapping information");
  691. peer_bi = bi;
  692. }
  693. if (own_bi && peer_bi)
  694. break;
  695. }
  696. if (!own_bi) {
  697. wpa_printf(MSG_DEBUG,
  698. "DPP: No matching own bootstrapping key found - ignore message");
  699. return;
  700. }
  701. if (wpa_s->dpp_auth) {
  702. wpa_printf(MSG_DEBUG,
  703. "DPP: Already in DPP authentication exchange - ignore new one");
  704. return;
  705. }
  706. wpa_s->dpp_gas_client = 0;
  707. wpa_s->dpp_auth_ok_on_ack = 0;
  708. wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
  709. wpa_s->dpp_qr_mutual,
  710. peer_bi, own_bi, freq, buf,
  711. wrapped_data, wrapped_data_len);
  712. if (!wpa_s->dpp_auth) {
  713. wpa_printf(MSG_DEBUG, "DPP: No response generated");
  714. return;
  715. }
  716. wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
  717. os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
  718. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP,
  719. wpabuf_len(wpa_s->dpp_auth->resp_attr));
  720. if (!msg)
  721. return;
  722. wpabuf_put_buf(msg, wpa_s->dpp_auth->resp_attr);
  723. offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
  724. src, wpa_s->own_addr, broadcast,
  725. wpabuf_head(msg), wpabuf_len(msg),
  726. 500, wpas_dpp_tx_status, 0);
  727. wpabuf_free(msg);
  728. }
  729. static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
  730. {
  731. /* TODO: stop wait and start ROC */
  732. }
  733. static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
  734. struct dpp_authentication *auth)
  735. {
  736. struct wpa_ssid *ssid;
  737. ssid = wpa_config_add_network(wpa_s->conf);
  738. if (!ssid)
  739. return NULL;
  740. wpas_notify_network_added(wpa_s, ssid);
  741. wpa_config_set_network_defaults(ssid);
  742. ssid->disabled = 1;
  743. ssid->ssid = os_malloc(auth->ssid_len);
  744. if (!ssid->ssid)
  745. goto fail;
  746. os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
  747. ssid->ssid_len = auth->ssid_len;
  748. if (auth->connector) {
  749. ssid->key_mgmt = WPA_KEY_MGMT_DPP;
  750. ssid->dpp_connector = os_strdup(auth->connector);
  751. if (!ssid->dpp_connector)
  752. goto fail;
  753. }
  754. if (auth->c_sign_key) {
  755. ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
  756. if (!ssid->dpp_csign)
  757. goto fail;
  758. os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
  759. wpabuf_len(auth->c_sign_key));
  760. ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
  761. ssid->dpp_csign_expiry = auth->c_sign_key_expiry;
  762. }
  763. if (auth->net_access_key) {
  764. ssid->dpp_netaccesskey =
  765. os_malloc(wpabuf_len(auth->net_access_key));
  766. if (!ssid->dpp_netaccesskey)
  767. goto fail;
  768. os_memcpy(ssid->dpp_netaccesskey,
  769. wpabuf_head(auth->net_access_key),
  770. wpabuf_len(auth->net_access_key));
  771. ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
  772. ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
  773. }
  774. if (!auth->connector) {
  775. ssid->key_mgmt = WPA_KEY_MGMT_PSK;
  776. if (auth->passphrase[0]) {
  777. if (wpa_config_set_quoted(ssid, "psk",
  778. auth->passphrase) < 0)
  779. goto fail;
  780. wpa_config_update_psk(ssid);
  781. ssid->export_keys = 1;
  782. } else {
  783. ssid->psk_set = auth->psk_set;
  784. os_memcpy(ssid->psk, auth->psk, PMK_LEN);
  785. }
  786. }
  787. return ssid;
  788. fail:
  789. wpas_notify_network_removed(wpa_s, ssid);
  790. wpa_config_remove_network(wpa_s->conf, ssid->id);
  791. return NULL;
  792. }
  793. static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
  794. struct dpp_authentication *auth)
  795. {
  796. struct wpa_ssid *ssid;
  797. if (wpa_s->conf->dpp_config_processing < 1)
  798. return;
  799. ssid = wpas_dpp_add_network(wpa_s, auth);
  800. if (!ssid)
  801. return;
  802. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
  803. if (wpa_s->conf->dpp_config_processing < 2)
  804. return;
  805. wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
  806. ssid->disabled = 0;
  807. wpa_s->disconnected = 0;
  808. wpa_s->reassociate = 1;
  809. wpa_s->scan_runs = 0;
  810. wpa_s->normal_scans = 0;
  811. wpa_supplicant_cancel_sched_scan(wpa_s);
  812. wpa_supplicant_req_scan(wpa_s, 0, 0);
  813. }
  814. static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
  815. enum gas_query_result result,
  816. const struct wpabuf *adv_proto,
  817. const struct wpabuf *resp, u16 status_code)
  818. {
  819. struct wpa_supplicant *wpa_s = ctx;
  820. const u8 *pos;
  821. struct dpp_authentication *auth = wpa_s->dpp_auth;
  822. if (!auth || !auth->auth_success) {
  823. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  824. return;
  825. }
  826. if (!resp || status_code != WLAN_STATUS_SUCCESS) {
  827. wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
  828. goto fail;
  829. }
  830. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
  831. adv_proto);
  832. wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
  833. resp);
  834. if (wpabuf_len(adv_proto) != 10 ||
  835. !(pos = wpabuf_head(adv_proto)) ||
  836. pos[0] != WLAN_EID_ADV_PROTO ||
  837. pos[1] != 8 ||
  838. pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
  839. pos[4] != 5 ||
  840. WPA_GET_BE24(&pos[5]) != OUI_WFA ||
  841. pos[8] != 0x1a ||
  842. pos[9] != 1) {
  843. wpa_printf(MSG_DEBUG,
  844. "DPP: Not a DPP Advertisement Protocol ID");
  845. goto fail;
  846. }
  847. if (dpp_conf_resp_rx(auth, resp) < 0) {
  848. wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
  849. goto fail;
  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. dpp_auth_deinit(wpa_s->dpp_auth);
  906. wpa_s->dpp_auth = NULL;
  907. return;
  908. fail:
  909. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  910. dpp_auth_deinit(wpa_s->dpp_auth);
  911. wpa_s->dpp_auth = NULL;
  912. }
  913. static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
  914. {
  915. struct dpp_authentication *auth = wpa_s->dpp_auth;
  916. struct wpabuf *buf, *conf_req;
  917. char json[100];
  918. int res;
  919. wpa_s->dpp_gas_client = 1;
  920. os_snprintf(json, sizeof(json),
  921. "{\"name\":\"Test\","
  922. "\"wi-fi_tech\":\"infra\","
  923. "\"netRole\":\"%s\"}",
  924. wpa_s->dpp_netrole_ap ? "ap" : "sta");
  925. wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
  926. offchannel_send_action_done(wpa_s);
  927. wpas_dpp_listen_stop(wpa_s);
  928. conf_req = dpp_build_conf_req(auth, json);
  929. if (!conf_req) {
  930. wpa_printf(MSG_DEBUG,
  931. "DPP: No configuration request data available");
  932. return;
  933. }
  934. buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
  935. if (!buf) {
  936. wpabuf_free(conf_req);
  937. return;
  938. }
  939. /* Advertisement Protocol IE */
  940. wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
  941. wpabuf_put_u8(buf, 8); /* Length */
  942. wpabuf_put_u8(buf, 0x7f);
  943. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  944. wpabuf_put_u8(buf, 5);
  945. wpabuf_put_be24(buf, OUI_WFA);
  946. wpabuf_put_u8(buf, DPP_OUI_TYPE);
  947. wpabuf_put_u8(buf, 0x01);
  948. /* GAS Query */
  949. wpabuf_put_le16(buf, wpabuf_len(conf_req));
  950. wpabuf_put_buf(buf, conf_req);
  951. wpabuf_free(conf_req);
  952. wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
  953. MAC2STR(auth->peer_mac_addr), auth->curr_freq);
  954. res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
  955. buf, wpas_dpp_gas_resp_cb, wpa_s);
  956. if (res < 0) {
  957. wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
  958. wpabuf_free(buf);
  959. } else {
  960. wpa_printf(MSG_DEBUG,
  961. "DPP: GAS query started with dialog token %u", res);
  962. }
  963. }
  964. static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
  965. {
  966. wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
  967. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
  968. if (wpa_s->dpp_auth->configurator)
  969. wpas_dpp_start_gas_server(wpa_s);
  970. else
  971. wpas_dpp_start_gas_client(wpa_s);
  972. }
  973. static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
  974. const u8 *buf, size_t len)
  975. {
  976. struct dpp_authentication *auth = wpa_s->dpp_auth;
  977. struct wpabuf *msg, *attr;
  978. wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR,
  979. MAC2STR(src));
  980. if (!auth) {
  981. wpa_printf(MSG_DEBUG,
  982. "DPP: No DPP Authentication in progress - drop");
  983. return;
  984. }
  985. if (!is_zero_ether_addr(auth->peer_mac_addr) &&
  986. os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  987. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  988. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  989. return;
  990. }
  991. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  992. attr = dpp_auth_resp_rx(auth, buf, len);
  993. if (!attr) {
  994. if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
  995. wpa_printf(MSG_DEBUG,
  996. "DPP: Start wait for full response");
  997. offchannel_send_action_done(wpa_s);
  998. wpas_dpp_listen_start(wpa_s, auth->curr_freq);
  999. return;
  1000. }
  1001. wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
  1002. return;
  1003. }
  1004. os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
  1005. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_CONF, wpabuf_len(attr));
  1006. if (!msg) {
  1007. wpabuf_free(attr);
  1008. return;
  1009. }
  1010. wpabuf_put_buf(msg, attr);
  1011. wpabuf_free(attr);
  1012. offchannel_send_action(wpa_s, auth->curr_freq,
  1013. src, wpa_s->own_addr, broadcast,
  1014. wpabuf_head(msg), wpabuf_len(msg),
  1015. 500, wpas_dpp_tx_status, 0);
  1016. wpabuf_free(msg);
  1017. wpa_s->dpp_auth_ok_on_ack = 1;
  1018. }
  1019. static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
  1020. const u8 *buf, size_t len)
  1021. {
  1022. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1023. wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
  1024. MAC2STR(src));
  1025. if (!auth) {
  1026. wpa_printf(MSG_DEBUG,
  1027. "DPP: No DPP Authentication in progress - drop");
  1028. return;
  1029. }
  1030. if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1031. wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
  1032. MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
  1033. return;
  1034. }
  1035. if (dpp_auth_conf_rx(auth, buf, len) < 0) {
  1036. wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
  1037. return;
  1038. }
  1039. wpas_dpp_auth_success(wpa_s, 0);
  1040. }
  1041. static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
  1042. const u8 *src,
  1043. const u8 *buf, size_t len)
  1044. {
  1045. struct wpa_ssid *ssid;
  1046. const u8 *connector, *pk_hash, *nk_hash;
  1047. u16 connector_len, pk_hash_len, nk_hash_len;
  1048. struct dpp_introduction intro;
  1049. struct rsn_pmksa_cache_entry *entry;
  1050. wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
  1051. MAC2STR(src));
  1052. if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
  1053. os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
  1054. wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
  1055. MACSTR " - drop", MAC2STR(src));
  1056. return;
  1057. }
  1058. offchannel_send_action_done(wpa_s);
  1059. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1060. if (ssid == wpa_s->dpp_intro_network)
  1061. break;
  1062. }
  1063. if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1064. !ssid->dpp_csign) {
  1065. wpa_printf(MSG_DEBUG,
  1066. "DPP: Profile not found for network introduction");
  1067. return;
  1068. }
  1069. connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
  1070. if (!connector) {
  1071. wpa_printf(MSG_DEBUG,
  1072. "DPP: Peer did not include its Connector");
  1073. return;
  1074. }
  1075. if (dpp_peer_intro(&intro, ssid->dpp_connector,
  1076. ssid->dpp_netaccesskey,
  1077. ssid->dpp_netaccesskey_len,
  1078. ssid->dpp_csign,
  1079. ssid->dpp_csign_len,
  1080. connector, connector_len) < 0) {
  1081. wpa_printf(MSG_INFO,
  1082. "DPP: Network Introduction protocol resulted in failure");
  1083. goto fail;
  1084. }
  1085. pk_hash = dpp_get_attr(buf, len, DPP_ATTR_PEER_NET_PK_HASH,
  1086. &pk_hash_len);
  1087. if (!pk_hash || pk_hash_len != SHA256_MAC_LEN) {
  1088. wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(PK)");
  1089. goto fail;
  1090. }
  1091. if (os_memcmp(pk_hash, intro.nk_hash, SHA256_MAC_LEN) != 0) {
  1092. wpa_printf(MSG_DEBUG, "DPP: SHA256(PK) mismatch");
  1093. wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(PK)",
  1094. pk_hash, pk_hash_len);
  1095. wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(PK)",
  1096. intro.nk_hash, SHA256_MAC_LEN);
  1097. goto fail;
  1098. }
  1099. nk_hash = dpp_get_attr(buf, len, DPP_ATTR_OWN_NET_NK_HASH,
  1100. &nk_hash_len);
  1101. if (!nk_hash || nk_hash_len != SHA256_MAC_LEN) {
  1102. wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(NK)");
  1103. goto fail;
  1104. }
  1105. if (os_memcmp(nk_hash, intro.pk_hash, SHA256_MAC_LEN) != 0) {
  1106. wpa_printf(MSG_DEBUG, "DPP: SHA256(NK) mismatch");
  1107. wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(NK)",
  1108. nk_hash, nk_hash_len);
  1109. wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(NK)",
  1110. intro.pk_hash, SHA256_MAC_LEN);
  1111. goto fail;
  1112. }
  1113. entry = os_zalloc(sizeof(*entry));
  1114. if (!entry)
  1115. goto fail;
  1116. os_memcpy(entry->aa, src, ETH_ALEN);
  1117. os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
  1118. os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
  1119. entry->pmk_len = intro.pmk_len;
  1120. entry->akmp = WPA_KEY_MGMT_DPP;
  1121. /* TODO: expiration */
  1122. entry->network_ctx = ssid;
  1123. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  1124. wpa_printf(MSG_DEBUG,
  1125. "DPP: Try connection again after successful network introduction");
  1126. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  1127. wpa_supplicant_cancel_sched_scan(wpa_s);
  1128. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1129. }
  1130. fail:
  1131. os_memset(&intro, 0, sizeof(intro));
  1132. }
  1133. void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
  1134. const u8 *buf, size_t len, unsigned int freq)
  1135. {
  1136. enum dpp_public_action_frame_type type;
  1137. if (len < 1)
  1138. return;
  1139. type = buf[0];
  1140. buf++;
  1141. len--;
  1142. wpa_printf(MSG_DEBUG,
  1143. "DPP: Received DPP Public Action frame type %d from "
  1144. MACSTR " freq=%u",
  1145. type, MAC2STR(src), freq);
  1146. wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
  1147. if (dpp_check_attrs(buf, len) < 0)
  1148. return;
  1149. switch (type) {
  1150. case DPP_PA_AUTHENTICATION_REQ:
  1151. wpas_dpp_rx_auth_req(wpa_s, src, buf, len, freq);
  1152. break;
  1153. case DPP_PA_AUTHENTICATION_RESP:
  1154. wpas_dpp_rx_auth_resp(wpa_s, src, buf, len);
  1155. break;
  1156. case DPP_PA_AUTHENTICATION_CONF:
  1157. wpas_dpp_rx_auth_conf(wpa_s, src, buf, len);
  1158. break;
  1159. case DPP_PA_PEER_DISCOVERY_RESP:
  1160. wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
  1161. break;
  1162. default:
  1163. wpa_printf(MSG_DEBUG,
  1164. "DPP: Ignored unsupported frame subtype %d", type);
  1165. break;
  1166. }
  1167. }
  1168. static struct wpabuf *
  1169. wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
  1170. size_t query_len)
  1171. {
  1172. struct wpa_supplicant *wpa_s = ctx;
  1173. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1174. struct wpabuf *resp;
  1175. wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
  1176. MAC2STR(sa));
  1177. if (!auth || !auth->auth_success ||
  1178. os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
  1179. wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
  1180. return NULL;
  1181. }
  1182. wpa_hexdump(MSG_DEBUG,
  1183. "DPP: Received Configuration Request (GAS Query Request)",
  1184. query, query_len);
  1185. resp = dpp_conf_req_rx(auth, query, query_len);
  1186. if (!resp)
  1187. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1188. return resp;
  1189. }
  1190. static void
  1191. wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
  1192. {
  1193. struct wpa_supplicant *wpa_s = ctx;
  1194. struct dpp_authentication *auth = wpa_s->dpp_auth;
  1195. if (!auth) {
  1196. wpabuf_free(resp);
  1197. return;
  1198. }
  1199. wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
  1200. ok);
  1201. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1202. offchannel_send_action_done(wpa_s);
  1203. wpas_dpp_listen_stop(wpa_s);
  1204. if (ok)
  1205. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
  1206. else
  1207. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
  1208. dpp_auth_deinit(wpa_s->dpp_auth);
  1209. wpa_s->dpp_auth = NULL;
  1210. wpabuf_free(resp);
  1211. }
  1212. static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
  1213. {
  1214. struct dpp_configurator *conf;
  1215. unsigned int max_id = 0;
  1216. dl_list_for_each(conf, &wpa_s->dpp_configurator,
  1217. struct dpp_configurator, list) {
  1218. if (conf->id > max_id)
  1219. max_id = conf->id;
  1220. }
  1221. return max_id + 1;
  1222. }
  1223. int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
  1224. {
  1225. char *expiry = NULL, *curve = NULL;
  1226. char *key = NULL;
  1227. u8 *privkey = NULL;
  1228. size_t privkey_len = 0;
  1229. int ret = -1;
  1230. struct dpp_configurator *conf = NULL;
  1231. expiry = get_param(cmd, " expiry=");
  1232. curve = get_param(cmd, " curve=");
  1233. key = get_param(cmd, " key=");
  1234. if (key) {
  1235. privkey_len = os_strlen(key) / 2;
  1236. privkey = os_malloc(privkey_len);
  1237. if (!privkey ||
  1238. hexstr2bin(key, privkey, privkey_len) < 0)
  1239. goto fail;
  1240. }
  1241. conf = dpp_keygen_configurator(curve, privkey, privkey_len);
  1242. if (!conf)
  1243. goto fail;
  1244. if (expiry) {
  1245. long int val;
  1246. val = strtol(expiry, NULL, 0);
  1247. if (val <= 0)
  1248. goto fail;
  1249. conf->csign_expiry = val;
  1250. }
  1251. conf->id = wpas_dpp_next_configurator_id(wpa_s);
  1252. dl_list_add(&wpa_s->dpp_configurator, &conf->list);
  1253. ret = conf->id;
  1254. conf = NULL;
  1255. fail:
  1256. os_free(curve);
  1257. os_free(expiry);
  1258. str_clear_free(key);
  1259. bin_clear_free(privkey, privkey_len);
  1260. dpp_configurator_free(conf);
  1261. return ret;
  1262. }
  1263. static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
  1264. {
  1265. struct dpp_configurator *conf, *tmp;
  1266. int found = 0;
  1267. dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
  1268. struct dpp_configurator, list) {
  1269. if (id && conf->id != id)
  1270. continue;
  1271. found = 1;
  1272. dl_list_del(&conf->list);
  1273. dpp_configurator_free(conf);
  1274. }
  1275. if (id == 0)
  1276. return 0; /* flush succeeds regardless of entries found */
  1277. return found ? 0 : -1;
  1278. }
  1279. int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
  1280. {
  1281. unsigned int id_val;
  1282. if (os_strcmp(id, "*") == 0) {
  1283. id_val = 0;
  1284. } else {
  1285. id_val = atoi(id);
  1286. if (id_val == 0)
  1287. return -1;
  1288. }
  1289. return dpp_configurator_del(wpa_s, id_val);
  1290. }
  1291. static void
  1292. wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
  1293. unsigned int freq, const u8 *dst,
  1294. const u8 *src, const u8 *bssid,
  1295. const u8 *data, size_t data_len,
  1296. enum offchannel_send_action_result result)
  1297. {
  1298. wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
  1299. " result=%s (DPP Peer Discovery Request)",
  1300. freq, MAC2STR(dst),
  1301. result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
  1302. (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
  1303. "FAILED"));
  1304. /* TODO: Time out wait for response more quickly in error cases? */
  1305. }
  1306. int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  1307. struct wpa_bss *bss)
  1308. {
  1309. struct os_time now;
  1310. struct wpabuf *msg;
  1311. unsigned int wait_time;
  1312. if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
  1313. return 0; /* Not using DPP AKM - continue */
  1314. if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
  1315. return 0; /* PMKSA exists for DPP AKM - continue */
  1316. if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
  1317. !ssid->dpp_csign) {
  1318. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1319. "missing %s",
  1320. !ssid->dpp_connector ? "Connector" :
  1321. (!ssid->dpp_netaccesskey ? "netAccessKey" :
  1322. "C-sign-key"));
  1323. return -1;
  1324. }
  1325. os_get_time(&now);
  1326. if (ssid->dpp_csign_expiry && ssid->dpp_csign_expiry < now.sec) {
  1327. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1328. "C-sign-key expired");
  1329. return -1;
  1330. }
  1331. if (ssid->dpp_netaccesskey_expiry &&
  1332. ssid->dpp_netaccesskey_expiry < now.sec) {
  1333. wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
  1334. "netAccessKey expired");
  1335. return -1;
  1336. }
  1337. wpa_printf(MSG_DEBUG,
  1338. "DPP: Starting network introduction protocol to derive PMKSA for "
  1339. MACSTR, MAC2STR(bss->bssid));
  1340. msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
  1341. 4 + os_strlen(ssid->dpp_connector));
  1342. if (!msg)
  1343. return -1;
  1344. /* DPP Connector */
  1345. wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
  1346. wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
  1347. wpabuf_put_str(msg, ssid->dpp_connector);
  1348. /* TODO: Timeout on AP response */
  1349. wait_time = wpa_s->max_remain_on_chan;
  1350. if (wait_time > 2000)
  1351. wait_time = 2000;
  1352. offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
  1353. broadcast,
  1354. wpabuf_head(msg), wpabuf_len(msg),
  1355. wait_time, wpas_dpp_tx_introduction_status, 0);
  1356. wpabuf_free(msg);
  1357. /* Request this connection attempt to terminate - new one will be
  1358. * started when network introduction protocol completes */
  1359. os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
  1360. wpa_s->dpp_intro_network = ssid;
  1361. return 1;
  1362. }
  1363. int wpas_dpp_init(struct wpa_supplicant *wpa_s)
  1364. {
  1365. u8 adv_proto_id[7];
  1366. adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
  1367. adv_proto_id[1] = 5;
  1368. WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
  1369. adv_proto_id[5] = DPP_OUI_TYPE;
  1370. adv_proto_id[6] = 0x01;
  1371. if (gas_server_register(wpa_s->gas_server, adv_proto_id,
  1372. sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
  1373. wpas_dpp_gas_status_handler, wpa_s) < 0)
  1374. return -1;
  1375. dl_list_init(&wpa_s->dpp_bootstrap);
  1376. dl_list_init(&wpa_s->dpp_configurator);
  1377. wpa_s->dpp_init_done = 1;
  1378. return 0;
  1379. }
  1380. void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
  1381. {
  1382. #ifdef CONFIG_TESTING_OPTIONS
  1383. os_free(wpa_s->dpp_config_obj_override);
  1384. wpa_s->dpp_config_obj_override = NULL;
  1385. os_free(wpa_s->dpp_discovery_override);
  1386. wpa_s->dpp_discovery_override = NULL;
  1387. os_free(wpa_s->dpp_groups_override);
  1388. wpa_s->dpp_groups_override = NULL;
  1389. os_free(wpa_s->dpp_devices_override);
  1390. wpa_s->dpp_devices_override = NULL;
  1391. wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
  1392. #endif /* CONFIG_TESTING_OPTIONS */
  1393. if (!wpa_s->dpp_init_done)
  1394. return;
  1395. eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
  1396. offchannel_send_action_done(wpa_s);
  1397. wpas_dpp_listen_stop(wpa_s);
  1398. dpp_bootstrap_del(wpa_s, 0);
  1399. dpp_configurator_del(wpa_s, 0);
  1400. dpp_auth_deinit(wpa_s->dpp_auth);
  1401. wpa_s->dpp_auth = NULL;
  1402. os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
  1403. }