dpp_supplicant.c 65 KB

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