dpp_supplicant.c 39 KB

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