dpp_supplicant.c 68 KB

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