dpp_hostapd.c 37 KB

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