]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/dpp_supplicant.c
DPP2: Configuration Result message generation and processing
[thirdparty/hostap.git] / wpa_supplicant / dpp_supplicant.c
1 /*
2 * wpa_supplicant - DPP
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2018, The Linux Foundation
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11
12 #include "utils/common.h"
13 #include "utils/eloop.h"
14 #include "common/dpp.h"
15 #include "common/gas.h"
16 #include "common/gas_server.h"
17 #include "rsn_supp/wpa.h"
18 #include "rsn_supp/pmksa_cache.h"
19 #include "wpa_supplicant_i.h"
20 #include "config.h"
21 #include "driver_i.h"
22 #include "offchannel.h"
23 #include "gas_query.h"
24 #include "bss.h"
25 #include "scan.h"
26 #include "notify.h"
27 #include "dpp_supplicant.h"
28
29
30 static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
31 unsigned int freq);
32 static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx);
33 static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator);
34 static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
35 unsigned int freq, const u8 *dst,
36 const u8 *src, const u8 *bssid,
37 const u8 *data, size_t data_len,
38 enum offchannel_send_action_result result);
39 static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx);
40 static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s);
41 static void
42 wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
43 unsigned int freq, const u8 *dst,
44 const u8 *src, const u8 *bssid,
45 const u8 *data, size_t data_len,
46 enum offchannel_send_action_result result);
47
48 static const u8 broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
49
50 /* Use a hardcoded Transaction ID 1 in Peer Discovery frames since there is only
51 * a single transaction in progress at any point in time. */
52 static const u8 TRANSACTION_ID = 1;
53
54
55 static struct dpp_configurator *
56 dpp_configurator_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
57 {
58 struct dpp_configurator *conf;
59
60 dl_list_for_each(conf, &wpa_s->dpp_configurator,
61 struct dpp_configurator, list) {
62 if (conf->id == id)
63 return conf;
64 }
65 return NULL;
66 }
67
68
69 static unsigned int wpas_dpp_next_id(struct wpa_supplicant *wpa_s)
70 {
71 struct dpp_bootstrap_info *bi;
72 unsigned int max_id = 0;
73
74 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
75 list) {
76 if (bi->id > max_id)
77 max_id = bi->id;
78 }
79 return max_id + 1;
80 }
81
82
83 /**
84 * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
85 * @wpa_s: Pointer to wpa_supplicant data
86 * @cmd: DPP URI read from a QR Code
87 * Returns: Identifier of the stored info or -1 on failure
88 */
89 int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
90 {
91 struct dpp_bootstrap_info *bi;
92 struct dpp_authentication *auth = wpa_s->dpp_auth;
93
94 bi = dpp_parse_qr_code(cmd);
95 if (!bi)
96 return -1;
97
98 bi->id = wpas_dpp_next_id(wpa_s);
99 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
100
101 if (auth && auth->response_pending &&
102 dpp_notify_new_qr_code(auth, bi) == 1) {
103 wpa_printf(MSG_DEBUG,
104 "DPP: Sending out pending authentication response");
105 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
106 " freq=%u type=%d",
107 MAC2STR(auth->peer_mac_addr), auth->curr_freq,
108 DPP_PA_AUTHENTICATION_RESP);
109 offchannel_send_action(wpa_s, auth->curr_freq,
110 auth->peer_mac_addr, wpa_s->own_addr,
111 broadcast,
112 wpabuf_head(auth->resp_msg),
113 wpabuf_len(auth->resp_msg),
114 500, wpas_dpp_tx_status, 0);
115 }
116
117 return bi->id;
118 }
119
120
121 static char * get_param(const char *cmd, const char *param)
122 {
123 const char *pos, *end;
124 char *val;
125 size_t len;
126
127 pos = os_strstr(cmd, param);
128 if (!pos)
129 return NULL;
130
131 pos += os_strlen(param);
132 end = os_strchr(pos, ' ');
133 if (end)
134 len = end - pos;
135 else
136 len = os_strlen(pos);
137 val = os_malloc(len + 1);
138 if (!val)
139 return NULL;
140 os_memcpy(val, pos, len);
141 val[len] = '\0';
142 return val;
143 }
144
145
146 int wpas_dpp_bootstrap_gen(struct wpa_supplicant *wpa_s, const char *cmd)
147 {
148 char *chan = NULL, *mac = NULL, *info = NULL, *pk = NULL, *curve = NULL;
149 char *key = NULL;
150 u8 *privkey = NULL;
151 size_t privkey_len = 0;
152 size_t len;
153 int ret = -1;
154 struct dpp_bootstrap_info *bi;
155
156 bi = os_zalloc(sizeof(*bi));
157 if (!bi)
158 goto fail;
159
160 if (os_strstr(cmd, "type=qrcode"))
161 bi->type = DPP_BOOTSTRAP_QR_CODE;
162 else if (os_strstr(cmd, "type=pkex"))
163 bi->type = DPP_BOOTSTRAP_PKEX;
164 else
165 goto fail;
166
167 chan = get_param(cmd, " chan=");
168 mac = get_param(cmd, " mac=");
169 info = get_param(cmd, " info=");
170 curve = get_param(cmd, " curve=");
171 key = get_param(cmd, " key=");
172
173 if (key) {
174 privkey_len = os_strlen(key) / 2;
175 privkey = os_malloc(privkey_len);
176 if (!privkey ||
177 hexstr2bin(key, privkey, privkey_len) < 0)
178 goto fail;
179 }
180
181 pk = dpp_keygen(bi, curve, privkey, privkey_len);
182 if (!pk)
183 goto fail;
184
185 len = 4; /* "DPP:" */
186 if (chan) {
187 if (dpp_parse_uri_chan_list(bi, chan) < 0)
188 goto fail;
189 len += 3 + os_strlen(chan); /* C:...; */
190 }
191 if (mac) {
192 if (dpp_parse_uri_mac(bi, mac) < 0)
193 goto fail;
194 len += 3 + os_strlen(mac); /* M:...; */
195 }
196 if (info) {
197 if (dpp_parse_uri_info(bi, info) < 0)
198 goto fail;
199 len += 3 + os_strlen(info); /* I:...; */
200 }
201 len += 4 + os_strlen(pk);
202 bi->uri = os_malloc(len + 1);
203 if (!bi->uri)
204 goto fail;
205 os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
206 chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
207 mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
208 info ? "I:" : "", info ? info : "", info ? ";" : "",
209 pk);
210 bi->id = wpas_dpp_next_id(wpa_s);
211 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
212 ret = bi->id;
213 bi = NULL;
214 fail:
215 os_free(curve);
216 os_free(pk);
217 os_free(chan);
218 os_free(mac);
219 os_free(info);
220 str_clear_free(key);
221 bin_clear_free(privkey, privkey_len);
222 dpp_bootstrap_info_free(bi);
223 return ret;
224 }
225
226
227 static struct dpp_bootstrap_info *
228 dpp_bootstrap_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
229 {
230 struct dpp_bootstrap_info *bi;
231
232 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
233 list) {
234 if (bi->id == id)
235 return bi;
236 }
237 return NULL;
238 }
239
240
241 static int dpp_bootstrap_del(struct wpa_supplicant *wpa_s, unsigned int id)
242 {
243 struct dpp_bootstrap_info *bi, *tmp;
244 int found = 0;
245
246 dl_list_for_each_safe(bi, tmp, &wpa_s->dpp_bootstrap,
247 struct dpp_bootstrap_info, list) {
248 if (id && bi->id != id)
249 continue;
250 found = 1;
251 dl_list_del(&bi->list);
252 dpp_bootstrap_info_free(bi);
253 }
254
255 if (id == 0)
256 return 0; /* flush succeeds regardless of entries found */
257 return found ? 0 : -1;
258 }
259
260
261 int wpas_dpp_bootstrap_remove(struct wpa_supplicant *wpa_s, const char *id)
262 {
263 unsigned int id_val;
264
265 if (os_strcmp(id, "*") == 0) {
266 id_val = 0;
267 } else {
268 id_val = atoi(id);
269 if (id_val == 0)
270 return -1;
271 }
272
273 return dpp_bootstrap_del(wpa_s, id_val);
274 }
275
276
277 const char * wpas_dpp_bootstrap_get_uri(struct wpa_supplicant *wpa_s,
278 unsigned int id)
279 {
280 struct dpp_bootstrap_info *bi;
281
282 bi = dpp_bootstrap_get_id(wpa_s, id);
283 if (!bi)
284 return NULL;
285 return bi->uri;
286 }
287
288
289 int wpas_dpp_bootstrap_info(struct wpa_supplicant *wpa_s, int id,
290 char *reply, int reply_size)
291 {
292 struct dpp_bootstrap_info *bi;
293
294 bi = dpp_bootstrap_get_id(wpa_s, id);
295 if (!bi)
296 return -1;
297 return os_snprintf(reply, reply_size, "type=%s\n"
298 "mac_addr=" MACSTR "\n"
299 "info=%s\n"
300 "num_freq=%u\n"
301 "curve=%s\n",
302 dpp_bootstrap_type_txt(bi->type),
303 MAC2STR(bi->mac_addr),
304 bi->info ? bi->info : "",
305 bi->num_freq,
306 bi->curve->name);
307 }
308
309
310 static void wpas_dpp_auth_resp_retry_timeout(void *eloop_ctx, void *timeout_ctx)
311 {
312 struct wpa_supplicant *wpa_s = eloop_ctx;
313 struct dpp_authentication *auth = wpa_s->dpp_auth;
314
315 if (!auth || !auth->resp_msg)
316 return;
317
318 wpa_printf(MSG_DEBUG,
319 "DPP: Retry Authentication Response after timeout");
320 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
321 " freq=%u type=%d",
322 MAC2STR(auth->peer_mac_addr), auth->curr_freq,
323 DPP_PA_AUTHENTICATION_RESP);
324 offchannel_send_action(wpa_s, auth->curr_freq, auth->peer_mac_addr,
325 wpa_s->own_addr, broadcast,
326 wpabuf_head(auth->resp_msg),
327 wpabuf_len(auth->resp_msg),
328 500, wpas_dpp_tx_status, 0);
329 }
330
331
332 static void wpas_dpp_auth_resp_retry(struct wpa_supplicant *wpa_s)
333 {
334 struct dpp_authentication *auth = wpa_s->dpp_auth;
335 unsigned int wait_time, max_tries;
336
337 if (!auth || !auth->resp_msg)
338 return;
339
340 if (wpa_s->dpp_resp_max_tries)
341 max_tries = wpa_s->dpp_resp_max_tries;
342 else
343 max_tries = 5;
344 auth->auth_resp_tries++;
345 if (auth->auth_resp_tries >= max_tries) {
346 wpa_printf(MSG_INFO, "DPP: No confirm received from initiator - stopping exchange");
347 offchannel_send_action_done(wpa_s);
348 dpp_auth_deinit(wpa_s->dpp_auth);
349 wpa_s->dpp_auth = NULL;
350 return;
351 }
352
353 if (wpa_s->dpp_resp_retry_time)
354 wait_time = wpa_s->dpp_resp_retry_time;
355 else
356 wait_time = 1000;
357 wpa_printf(MSG_DEBUG,
358 "DPP: Schedule retransmission of Authentication Response frame in %u ms",
359 wait_time);
360 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
361 eloop_register_timeout(wait_time / 1000,
362 (wait_time % 1000) * 1000,
363 wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
364 }
365
366
367 static void wpas_dpp_try_to_connect(struct wpa_supplicant *wpa_s)
368 {
369 wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
370 wpa_s->disconnected = 0;
371 wpa_s->reassociate = 1;
372 wpa_s->scan_runs = 0;
373 wpa_s->normal_scans = 0;
374 wpa_supplicant_cancel_sched_scan(wpa_s);
375 wpa_supplicant_req_scan(wpa_s, 0, 0);
376 }
377
378
379 static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
380 unsigned int freq, const u8 *dst,
381 const u8 *src, const u8 *bssid,
382 const u8 *data, size_t data_len,
383 enum offchannel_send_action_result result)
384 {
385 const char *res_txt;
386 struct dpp_authentication *auth = wpa_s->dpp_auth;
387
388 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
389 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
390 "FAILED");
391 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
392 " result=%s", freq, MAC2STR(dst), res_txt);
393 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
394 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
395
396 if (!wpa_s->dpp_auth) {
397 wpa_printf(MSG_DEBUG,
398 "DPP: Ignore TX status since there is no ongoing authentication exchange");
399 return;
400 }
401
402 #ifdef CONFIG_DPP2
403 if (auth->connect_on_tx_status) {
404 wpa_printf(MSG_DEBUG,
405 "DPP: Try to connect after completed configuration result");
406 wpas_dpp_try_to_connect(wpa_s);
407 dpp_auth_deinit(wpa_s->dpp_auth);
408 wpa_s->dpp_auth = NULL;
409 return;
410 }
411 #endif /* CONFIG_DPP2 */
412
413 if (wpa_s->dpp_auth->remove_on_tx_status) {
414 wpa_printf(MSG_DEBUG,
415 "DPP: Terminate authentication exchange due to an earlier error");
416 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
417 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
418 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
419 NULL);
420 offchannel_send_action_done(wpa_s);
421 dpp_auth_deinit(wpa_s->dpp_auth);
422 wpa_s->dpp_auth = NULL;
423 return;
424 }
425
426 if (wpa_s->dpp_auth_ok_on_ack)
427 wpas_dpp_auth_success(wpa_s, 1);
428
429 if (!is_broadcast_ether_addr(dst) &&
430 result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
431 wpa_printf(MSG_DEBUG,
432 "DPP: Unicast DPP Action frame was not ACKed");
433 if (auth->waiting_auth_resp) {
434 /* In case of DPP Authentication Request frame, move to
435 * the next channel immediately. */
436 offchannel_send_action_done(wpa_s);
437 wpas_dpp_auth_init_next(wpa_s);
438 return;
439 }
440 if (auth->waiting_auth_conf) {
441 wpas_dpp_auth_resp_retry(wpa_s);
442 return;
443 }
444 }
445
446 if (!is_broadcast_ether_addr(dst) && auth->waiting_auth_resp &&
447 result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
448 /* Allow timeout handling to stop iteration if no response is
449 * received from a peer that has ACKed a request. */
450 auth->auth_req_ack = 1;
451 }
452
453 if (!wpa_s->dpp_auth_ok_on_ack && wpa_s->dpp_auth->neg_freq > 0 &&
454 wpa_s->dpp_auth->curr_freq != wpa_s->dpp_auth->neg_freq) {
455 wpa_printf(MSG_DEBUG,
456 "DPP: Move from curr_freq %u MHz to neg_freq %u MHz for response",
457 wpa_s->dpp_auth->curr_freq,
458 wpa_s->dpp_auth->neg_freq);
459 offchannel_send_action_done(wpa_s);
460 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->neg_freq);
461 }
462
463 if (wpa_s->dpp_auth_ok_on_ack)
464 wpa_s->dpp_auth_ok_on_ack = 0;
465 }
466
467
468 static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
469 {
470 struct wpa_supplicant *wpa_s = eloop_ctx;
471 struct dpp_authentication *auth = wpa_s->dpp_auth;
472 unsigned int freq;
473 struct os_reltime now, diff;
474 unsigned int wait_time, diff_ms;
475
476 if (!auth || !auth->waiting_auth_resp)
477 return;
478
479 wait_time = wpa_s->dpp_resp_wait_time ?
480 wpa_s->dpp_resp_wait_time : 2000;
481 os_get_reltime(&now);
482 os_reltime_sub(&now, &wpa_s->dpp_last_init, &diff);
483 diff_ms = diff.sec * 1000 + diff.usec / 1000;
484 wpa_printf(MSG_DEBUG,
485 "DPP: Reply wait timeout - wait_time=%u diff_ms=%u",
486 wait_time, diff_ms);
487
488 if (auth->auth_req_ack && diff_ms >= wait_time) {
489 /* Peer ACK'ed Authentication Request frame, but did not reply
490 * with Authentication Response frame within two seconds. */
491 wpa_printf(MSG_INFO,
492 "DPP: No response received from responder - stopping initiation attempt");
493 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
494 offchannel_send_action_done(wpa_s);
495 wpas_dpp_listen_stop(wpa_s);
496 dpp_auth_deinit(auth);
497 wpa_s->dpp_auth = NULL;
498 return;
499 }
500
501 if (diff_ms >= wait_time) {
502 /* Authentication Request frame was not ACK'ed and no reply
503 * was receiving within two seconds. */
504 wpa_printf(MSG_DEBUG,
505 "DPP: Continue Initiator channel iteration");
506 offchannel_send_action_done(wpa_s);
507 wpas_dpp_listen_stop(wpa_s);
508 wpas_dpp_auth_init_next(wpa_s);
509 return;
510 }
511
512 /* Driver did not support 2000 ms long wait_time with TX command, so
513 * schedule listen operation to continue waiting for the response.
514 *
515 * DPP listen operations continue until stopped, so simply schedule a
516 * new call to this function at the point when the two second reply
517 * wait has expired. */
518 wait_time -= diff_ms;
519
520 freq = auth->curr_freq;
521 if (auth->neg_freq > 0)
522 freq = auth->neg_freq;
523 wpa_printf(MSG_DEBUG,
524 "DPP: Continue reply wait on channel %u MHz for %u ms",
525 freq, wait_time);
526 wpa_s->dpp_in_response_listen = 1;
527 wpas_dpp_listen_start(wpa_s, freq);
528
529 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
530 wpas_dpp_reply_wait_timeout, wpa_s, NULL);
531 }
532
533
534 static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
535 struct dpp_authentication *auth)
536 {
537 #ifdef CONFIG_TESTING_OPTIONS
538 if (wpa_s->dpp_config_obj_override)
539 auth->config_obj_override =
540 os_strdup(wpa_s->dpp_config_obj_override);
541 if (wpa_s->dpp_discovery_override)
542 auth->discovery_override =
543 os_strdup(wpa_s->dpp_discovery_override);
544 if (wpa_s->dpp_groups_override)
545 auth->groups_override =
546 os_strdup(wpa_s->dpp_groups_override);
547 auth->ignore_netaccesskey_mismatch =
548 wpa_s->dpp_ignore_netaccesskey_mismatch;
549 #endif /* CONFIG_TESTING_OPTIONS */
550 }
551
552
553 static int wpas_dpp_set_configurator(struct wpa_supplicant *wpa_s,
554 struct dpp_authentication *auth,
555 const char *cmd)
556 {
557 const char *pos, *end;
558 struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
559 struct dpp_configurator *conf = NULL;
560 u8 ssid[32] = { "test" };
561 size_t ssid_len = 4;
562 char pass[64] = { };
563 size_t pass_len = 0;
564 u8 psk[PMK_LEN];
565 int psk_set = 0;
566 char *group_id = NULL;
567
568 if (!cmd)
569 return 0;
570
571 wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
572 pos = os_strstr(cmd, " ssid=");
573 if (pos) {
574 pos += 6;
575 end = os_strchr(pos, ' ');
576 ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
577 ssid_len /= 2;
578 if (ssid_len > sizeof(ssid) ||
579 hexstr2bin(pos, ssid, ssid_len) < 0)
580 goto fail;
581 }
582
583 pos = os_strstr(cmd, " pass=");
584 if (pos) {
585 pos += 6;
586 end = os_strchr(pos, ' ');
587 pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
588 pass_len /= 2;
589 if (pass_len > sizeof(pass) - 1 || pass_len < 8 ||
590 hexstr2bin(pos, (u8 *) pass, pass_len) < 0)
591 goto fail;
592 }
593
594 pos = os_strstr(cmd, " psk=");
595 if (pos) {
596 pos += 5;
597 if (hexstr2bin(pos, psk, PMK_LEN) < 0)
598 goto fail;
599 psk_set = 1;
600 }
601
602 pos = os_strstr(cmd, " group_id=");
603 if (pos) {
604 size_t group_id_len;
605
606 pos += 10;
607 end = os_strchr(pos, ' ');
608 group_id_len = end ? (size_t) (end - pos) : os_strlen(pos);
609 group_id = os_malloc(group_id_len + 1);
610 if (!group_id)
611 goto fail;
612 os_memcpy(group_id, pos, group_id_len);
613 group_id[group_id_len] = '\0';
614 }
615
616 if (os_strstr(cmd, " conf=sta-")) {
617 conf_sta = os_zalloc(sizeof(struct dpp_configuration));
618 if (!conf_sta)
619 goto fail;
620 os_memcpy(conf_sta->ssid, ssid, ssid_len);
621 conf_sta->ssid_len = ssid_len;
622 if (os_strstr(cmd, " conf=sta-psk") ||
623 os_strstr(cmd, " conf=sta-sae") ||
624 os_strstr(cmd, " conf=sta-psk-sae")) {
625 if (os_strstr(cmd, " conf=sta-psk-sae"))
626 conf_sta->akm = DPP_AKM_PSK_SAE;
627 else if (os_strstr(cmd, " conf=sta-sae"))
628 conf_sta->akm = DPP_AKM_SAE;
629 else
630 conf_sta->akm = DPP_AKM_PSK;
631 if (psk_set) {
632 os_memcpy(conf_sta->psk, psk, PMK_LEN);
633 } else if (pass_len > 0) {
634 conf_sta->passphrase = os_strdup(pass);
635 if (!conf_sta->passphrase)
636 goto fail;
637 } else {
638 goto fail;
639 }
640 } else if (os_strstr(cmd, " conf=sta-dpp")) {
641 conf_sta->akm = DPP_AKM_DPP;
642 } else {
643 goto fail;
644 }
645 if (os_strstr(cmd, " group_id=")) {
646 conf_sta->group_id = group_id;
647 group_id = NULL;
648 }
649 }
650
651 if (os_strstr(cmd, " conf=ap-")) {
652 conf_ap = os_zalloc(sizeof(struct dpp_configuration));
653 if (!conf_ap)
654 goto fail;
655 os_memcpy(conf_ap->ssid, ssid, ssid_len);
656 conf_ap->ssid_len = ssid_len;
657 if (os_strstr(cmd, " conf=ap-psk") ||
658 os_strstr(cmd, " conf=ap-sae") ||
659 os_strstr(cmd, " conf=ap-psk-sae")) {
660 if (os_strstr(cmd, " conf=ap-psk-sae"))
661 conf_ap->akm = DPP_AKM_PSK_SAE;
662 else if (os_strstr(cmd, " conf=ap-sae"))
663 conf_ap->akm = DPP_AKM_SAE;
664 else
665 conf_ap->akm = DPP_AKM_PSK;
666 if (psk_set) {
667 os_memcpy(conf_ap->psk, psk, PMK_LEN);
668 } else {
669 conf_ap->passphrase = os_strdup(pass);
670 if (!conf_ap->passphrase)
671 goto fail;
672 }
673 } else if (os_strstr(cmd, " conf=ap-dpp")) {
674 conf_ap->akm = DPP_AKM_DPP;
675 } else {
676 goto fail;
677 }
678 if (os_strstr(cmd, " group_id=")) {
679 conf_ap->group_id = group_id;
680 group_id = NULL;
681 }
682 }
683
684 pos = os_strstr(cmd, " expiry=");
685 if (pos) {
686 long int val;
687
688 pos += 8;
689 val = strtol(pos, NULL, 0);
690 if (val <= 0)
691 goto fail;
692 if (conf_sta)
693 conf_sta->netaccesskey_expiry = val;
694 if (conf_ap)
695 conf_ap->netaccesskey_expiry = val;
696 }
697
698 pos = os_strstr(cmd, " configurator=");
699 if (pos) {
700 pos += 14;
701 conf = dpp_configurator_get_id(wpa_s, atoi(pos));
702 if (!conf) {
703 wpa_printf(MSG_INFO,
704 "DPP: Could not find the specified configurator");
705 goto fail;
706 }
707 }
708 auth->conf_sta = conf_sta;
709 auth->conf_ap = conf_ap;
710 auth->conf = conf;
711 os_free(group_id);
712 return 0;
713
714 fail:
715 wpa_msg(wpa_s, MSG_INFO, "DPP: Failed to set configurator parameters");
716 dpp_configuration_free(conf_sta);
717 dpp_configuration_free(conf_ap);
718 os_free(group_id);
719 return -1;
720 }
721
722
723 static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
724 {
725 struct wpa_supplicant *wpa_s = eloop_ctx;
726
727 if (!wpa_s->dpp_auth)
728 return;
729 wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
730 wpas_dpp_auth_init_next(wpa_s);
731 }
732
733
734 static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
735 {
736 struct dpp_authentication *auth = wpa_s->dpp_auth;
737 const u8 *dst;
738 unsigned int wait_time, max_wait_time, freq, max_tries, used;
739 struct os_reltime now, diff;
740
741 wpa_s->dpp_in_response_listen = 0;
742 if (!auth)
743 return -1;
744
745 if (auth->freq_idx == 0)
746 os_get_reltime(&wpa_s->dpp_init_iter_start);
747
748 if (auth->freq_idx >= auth->num_freq) {
749 auth->num_freq_iters++;
750 if (wpa_s->dpp_init_max_tries)
751 max_tries = wpa_s->dpp_init_max_tries;
752 else
753 max_tries = 5;
754 if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
755 wpa_printf(MSG_INFO,
756 "DPP: No response received from responder - stopping initiation attempt");
757 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
758 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
759 wpa_s, NULL);
760 offchannel_send_action_done(wpa_s);
761 dpp_auth_deinit(wpa_s->dpp_auth);
762 wpa_s->dpp_auth = NULL;
763 return -1;
764 }
765 auth->freq_idx = 0;
766 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
767 if (wpa_s->dpp_init_retry_time)
768 wait_time = wpa_s->dpp_init_retry_time;
769 else
770 wait_time = 10000;
771 os_get_reltime(&now);
772 os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
773 used = diff.sec * 1000 + diff.usec / 1000;
774 if (used > wait_time)
775 wait_time = 0;
776 else
777 wait_time -= used;
778 wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
779 wait_time);
780 eloop_register_timeout(wait_time / 1000,
781 (wait_time % 1000) * 1000,
782 wpas_dpp_init_timeout, wpa_s,
783 NULL);
784 return 0;
785 }
786 freq = auth->freq[auth->freq_idx++];
787 auth->curr_freq = freq;
788
789 if (is_zero_ether_addr(auth->peer_bi->mac_addr))
790 dst = broadcast;
791 else
792 dst = auth->peer_bi->mac_addr;
793 wpa_s->dpp_auth_ok_on_ack = 0;
794 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
795 wait_time = wpa_s->max_remain_on_chan;
796 max_wait_time = wpa_s->dpp_resp_wait_time ?
797 wpa_s->dpp_resp_wait_time : 2000;
798 if (wait_time > max_wait_time)
799 wait_time = max_wait_time;
800 wait_time += 10; /* give the driver some extra time to complete */
801 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
802 wpas_dpp_reply_wait_timeout,
803 wpa_s, NULL);
804 wait_time -= 10;
805 if (auth->neg_freq > 0 && freq != auth->neg_freq) {
806 wpa_printf(MSG_DEBUG,
807 "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
808 freq, auth->neg_freq);
809 }
810 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
811 MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
812 auth->auth_req_ack = 0;
813 os_get_reltime(&wpa_s->dpp_last_init);
814 return offchannel_send_action(wpa_s, freq, dst,
815 wpa_s->own_addr, broadcast,
816 wpabuf_head(auth->req_msg),
817 wpabuf_len(auth->req_msg),
818 wait_time, wpas_dpp_tx_status, 0);
819 }
820
821
822 int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
823 {
824 const char *pos;
825 struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
826 u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
827 unsigned int neg_freq = 0;
828
829 wpa_s->dpp_gas_client = 0;
830
831 pos = os_strstr(cmd, " peer=");
832 if (!pos)
833 return -1;
834 pos += 6;
835 peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
836 if (!peer_bi) {
837 wpa_printf(MSG_INFO,
838 "DPP: Could not find bootstrapping info for the identified peer");
839 return -1;
840 }
841
842 pos = os_strstr(cmd, " own=");
843 if (pos) {
844 pos += 5;
845 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
846 if (!own_bi) {
847 wpa_printf(MSG_INFO,
848 "DPP: Could not find bootstrapping info for the identified local entry");
849 return -1;
850 }
851
852 if (peer_bi->curve != own_bi->curve) {
853 wpa_printf(MSG_INFO,
854 "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
855 peer_bi->curve->name, own_bi->curve->name);
856 return -1;
857 }
858 }
859
860 pos = os_strstr(cmd, " role=");
861 if (pos) {
862 pos += 6;
863 if (os_strncmp(pos, "configurator", 12) == 0)
864 allowed_roles = DPP_CAPAB_CONFIGURATOR;
865 else if (os_strncmp(pos, "enrollee", 8) == 0)
866 allowed_roles = DPP_CAPAB_ENROLLEE;
867 else if (os_strncmp(pos, "either", 6) == 0)
868 allowed_roles = DPP_CAPAB_CONFIGURATOR |
869 DPP_CAPAB_ENROLLEE;
870 else
871 goto fail;
872 }
873
874 pos = os_strstr(cmd, " netrole=");
875 if (pos) {
876 pos += 9;
877 wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
878 }
879
880 pos = os_strstr(cmd, " neg_freq=");
881 if (pos)
882 neg_freq = atoi(pos + 10);
883
884 if (wpa_s->dpp_auth) {
885 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
886 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
887 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
888 NULL);
889 offchannel_send_action_done(wpa_s);
890 dpp_auth_deinit(wpa_s->dpp_auth);
891 }
892 wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles,
893 neg_freq,
894 wpa_s->hw.modes, wpa_s->hw.num_modes);
895 if (!wpa_s->dpp_auth)
896 goto fail;
897 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
898 if (wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth, cmd) < 0) {
899 dpp_auth_deinit(wpa_s->dpp_auth);
900 wpa_s->dpp_auth = NULL;
901 goto fail;
902 }
903
904 wpa_s->dpp_auth->neg_freq = neg_freq;
905
906 if (!is_zero_ether_addr(peer_bi->mac_addr))
907 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
908 ETH_ALEN);
909
910 return wpas_dpp_auth_init_next(wpa_s);
911 fail:
912 return -1;
913 }
914
915
916 struct wpas_dpp_listen_work {
917 unsigned int freq;
918 unsigned int duration;
919 struct wpabuf *probe_resp_ie;
920 };
921
922
923 static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
924 {
925 if (!lwork)
926 return;
927 os_free(lwork);
928 }
929
930
931 static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
932 {
933 struct wpas_dpp_listen_work *lwork;
934
935 if (!wpa_s->dpp_listen_work)
936 return;
937
938 lwork = wpa_s->dpp_listen_work->ctx;
939 wpas_dpp_listen_work_free(lwork);
940 radio_work_done(wpa_s->dpp_listen_work);
941 wpa_s->dpp_listen_work = NULL;
942 }
943
944
945 static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
946 {
947 struct wpa_supplicant *wpa_s = work->wpa_s;
948 struct wpas_dpp_listen_work *lwork = work->ctx;
949
950 if (deinit) {
951 if (work->started) {
952 wpa_s->dpp_listen_work = NULL;
953 wpas_dpp_listen_stop(wpa_s);
954 }
955 wpas_dpp_listen_work_free(lwork);
956 return;
957 }
958
959 wpa_s->dpp_listen_work = work;
960
961 wpa_s->dpp_pending_listen_freq = lwork->freq;
962
963 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
964 wpa_s->max_remain_on_chan) < 0) {
965 wpa_printf(MSG_DEBUG,
966 "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
967 lwork->freq);
968 wpa_s->dpp_listen_freq = 0;
969 wpas_dpp_listen_work_done(wpa_s);
970 wpa_s->dpp_pending_listen_freq = 0;
971 return;
972 }
973 wpa_s->off_channel_freq = 0;
974 wpa_s->roc_waiting_drv_freq = lwork->freq;
975 }
976
977
978 static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
979 unsigned int freq)
980 {
981 struct wpas_dpp_listen_work *lwork;
982
983 if (wpa_s->dpp_listen_work) {
984 wpa_printf(MSG_DEBUG,
985 "DPP: Reject start_listen since dpp_listen_work already exists");
986 return -1;
987 }
988
989 if (wpa_s->dpp_listen_freq)
990 wpas_dpp_listen_stop(wpa_s);
991 wpa_s->dpp_listen_freq = freq;
992
993 lwork = os_zalloc(sizeof(*lwork));
994 if (!lwork)
995 return -1;
996 lwork->freq = freq;
997
998 if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
999 lwork) < 0) {
1000 wpas_dpp_listen_work_free(lwork);
1001 return -1;
1002 }
1003
1004 return 0;
1005 }
1006
1007
1008 int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
1009 {
1010 int freq;
1011
1012 freq = atoi(cmd);
1013 if (freq <= 0)
1014 return -1;
1015
1016 if (os_strstr(cmd, " role=configurator"))
1017 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
1018 else if (os_strstr(cmd, " role=enrollee"))
1019 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
1020 else
1021 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
1022 DPP_CAPAB_ENROLLEE;
1023 wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
1024 wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
1025 if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
1026 wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
1027 freq);
1028 return 0;
1029 }
1030
1031 return wpas_dpp_listen_start(wpa_s, freq);
1032 }
1033
1034
1035 void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
1036 {
1037 wpa_s->dpp_in_response_listen = 0;
1038 if (!wpa_s->dpp_listen_freq)
1039 return;
1040
1041 wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
1042 wpa_s->dpp_listen_freq);
1043 wpa_drv_cancel_remain_on_channel(wpa_s);
1044 wpa_s->dpp_listen_freq = 0;
1045 wpas_dpp_listen_work_done(wpa_s);
1046 }
1047
1048
1049 void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
1050 unsigned int freq)
1051 {
1052 wpas_dpp_listen_work_done(wpa_s);
1053
1054 if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
1055 unsigned int new_freq;
1056
1057 /* Continue listen with a new remain-on-channel */
1058 if (wpa_s->dpp_auth->neg_freq > 0)
1059 new_freq = wpa_s->dpp_auth->neg_freq;
1060 else
1061 new_freq = wpa_s->dpp_auth->curr_freq;
1062 wpa_printf(MSG_DEBUG,
1063 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
1064 new_freq);
1065 wpas_dpp_listen_start(wpa_s, new_freq);
1066 return;
1067 }
1068
1069 if (wpa_s->dpp_listen_freq) {
1070 /* Continue listen with a new remain-on-channel */
1071 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
1072 }
1073 }
1074
1075
1076 static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
1077 const u8 *hdr, const u8 *buf, size_t len,
1078 unsigned int freq)
1079 {
1080 const u8 *r_bootstrap, *i_bootstrap;
1081 u16 r_bootstrap_len, i_bootstrap_len;
1082 struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
1083
1084 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
1085 MAC2STR(src));
1086
1087 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
1088 &r_bootstrap_len);
1089 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
1090 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1091 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
1092 return;
1093 }
1094 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
1095 r_bootstrap, r_bootstrap_len);
1096
1097 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
1098 &i_bootstrap_len);
1099 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
1100 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1101 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
1102 return;
1103 }
1104 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
1105 i_bootstrap, i_bootstrap_len);
1106
1107 /* Try to find own and peer bootstrapping key matches based on the
1108 * received hash values */
1109 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
1110 list) {
1111 if (!own_bi && bi->own &&
1112 os_memcmp(bi->pubkey_hash, r_bootstrap,
1113 SHA256_MAC_LEN) == 0) {
1114 wpa_printf(MSG_DEBUG,
1115 "DPP: Found matching own bootstrapping information");
1116 own_bi = bi;
1117 }
1118
1119 if (!peer_bi && !bi->own &&
1120 os_memcmp(bi->pubkey_hash, i_bootstrap,
1121 SHA256_MAC_LEN) == 0) {
1122 wpa_printf(MSG_DEBUG,
1123 "DPP: Found matching peer bootstrapping information");
1124 peer_bi = bi;
1125 }
1126
1127 if (own_bi && peer_bi)
1128 break;
1129 }
1130
1131 if (!own_bi) {
1132 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1133 "No matching own bootstrapping key found - ignore message");
1134 return;
1135 }
1136
1137 if (wpa_s->dpp_auth) {
1138 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1139 "Already in DPP authentication exchange - ignore new one");
1140 return;
1141 }
1142
1143 wpa_s->dpp_gas_client = 0;
1144 wpa_s->dpp_auth_ok_on_ack = 0;
1145 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
1146 wpa_s->dpp_qr_mutual,
1147 peer_bi, own_bi, freq, hdr, buf, len);
1148 if (!wpa_s->dpp_auth) {
1149 wpa_printf(MSG_DEBUG, "DPP: No response generated");
1150 return;
1151 }
1152 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
1153 if (wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth,
1154 wpa_s->dpp_configurator_params) < 0) {
1155 dpp_auth_deinit(wpa_s->dpp_auth);
1156 wpa_s->dpp_auth = NULL;
1157 return;
1158 }
1159 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
1160
1161 if (wpa_s->dpp_listen_freq &&
1162 wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
1163 wpa_printf(MSG_DEBUG,
1164 "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
1165 wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
1166 wpas_dpp_listen_stop(wpa_s);
1167 }
1168
1169 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1170 MAC2STR(src), wpa_s->dpp_auth->curr_freq,
1171 DPP_PA_AUTHENTICATION_RESP);
1172 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
1173 src, wpa_s->own_addr, broadcast,
1174 wpabuf_head(wpa_s->dpp_auth->resp_msg),
1175 wpabuf_len(wpa_s->dpp_auth->resp_msg),
1176 500, wpas_dpp_tx_status, 0);
1177 }
1178
1179
1180 static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
1181 {
1182 /* TODO: stop wait and start ROC */
1183 }
1184
1185
1186 static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
1187 struct dpp_authentication *auth)
1188 {
1189 struct wpa_ssid *ssid;
1190
1191 #ifdef CONFIG_DPP2
1192 if (auth->akm == DPP_AKM_SAE) {
1193 #ifdef CONFIG_SAE
1194 struct wpa_driver_capa capa;
1195 int res;
1196
1197 res = wpa_drv_get_capa(wpa_s, &capa);
1198 if (res == 0 &&
1199 !(capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) &&
1200 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
1201 wpa_printf(MSG_DEBUG,
1202 "DPP: SAE not supported by the driver");
1203 return NULL;
1204 }
1205 #else /* CONFIG_SAE */
1206 wpa_printf(MSG_DEBUG, "DPP: SAE not supported in the build");
1207 return NULL;
1208 #endif /* CONFIG_SAE */
1209 }
1210 #endif /* CONFIG_DPP2 */
1211
1212 ssid = wpa_config_add_network(wpa_s->conf);
1213 if (!ssid)
1214 return NULL;
1215 wpas_notify_network_added(wpa_s, ssid);
1216 wpa_config_set_network_defaults(ssid);
1217 ssid->disabled = 1;
1218
1219 ssid->ssid = os_malloc(auth->ssid_len);
1220 if (!ssid->ssid)
1221 goto fail;
1222 os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
1223 ssid->ssid_len = auth->ssid_len;
1224
1225 if (auth->connector) {
1226 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
1227 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
1228 ssid->dpp_connector = os_strdup(auth->connector);
1229 if (!ssid->dpp_connector)
1230 goto fail;
1231 }
1232
1233 if (auth->c_sign_key) {
1234 ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
1235 if (!ssid->dpp_csign)
1236 goto fail;
1237 os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
1238 wpabuf_len(auth->c_sign_key));
1239 ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
1240 }
1241
1242 if (auth->net_access_key) {
1243 ssid->dpp_netaccesskey =
1244 os_malloc(wpabuf_len(auth->net_access_key));
1245 if (!ssid->dpp_netaccesskey)
1246 goto fail;
1247 os_memcpy(ssid->dpp_netaccesskey,
1248 wpabuf_head(auth->net_access_key),
1249 wpabuf_len(auth->net_access_key));
1250 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
1251 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
1252 }
1253
1254 if (!auth->connector) {
1255 ssid->key_mgmt = 0;
1256 if (auth->akm == DPP_AKM_PSK || auth->akm == DPP_AKM_PSK_SAE)
1257 ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
1258 WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
1259 if (auth->akm == DPP_AKM_SAE || auth->akm == DPP_AKM_PSK_SAE)
1260 ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
1261 WPA_KEY_MGMT_FT_SAE;
1262 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
1263 if (auth->passphrase[0]) {
1264 if (wpa_config_set_quoted(ssid, "psk",
1265 auth->passphrase) < 0)
1266 goto fail;
1267 wpa_config_update_psk(ssid);
1268 ssid->export_keys = 1;
1269 } else {
1270 ssid->psk_set = auth->psk_set;
1271 os_memcpy(ssid->psk, auth->psk, PMK_LEN);
1272 }
1273 }
1274
1275 return ssid;
1276 fail:
1277 wpas_notify_network_removed(wpa_s, ssid);
1278 wpa_config_remove_network(wpa_s->conf, ssid->id);
1279 return NULL;
1280 }
1281
1282
1283 static int wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
1284 struct dpp_authentication *auth)
1285 {
1286 struct wpa_ssid *ssid;
1287
1288 if (wpa_s->conf->dpp_config_processing < 1)
1289 return 0;
1290
1291 ssid = wpas_dpp_add_network(wpa_s, auth);
1292 if (!ssid)
1293 return -1;
1294
1295 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
1296 if (wpa_s->conf->dpp_config_processing == 2)
1297 ssid->disabled = 0;
1298
1299 #ifndef CONFIG_NO_CONFIG_WRITE
1300 if (wpa_s->conf->update_config &&
1301 wpa_config_write(wpa_s->confname, wpa_s->conf))
1302 wpa_printf(MSG_DEBUG, "DPP: Failed to update configuration");
1303 #endif /* CONFIG_NO_CONFIG_WRITE */
1304
1305 if (wpa_s->conf->dpp_config_processing < 2)
1306 return 0;
1307
1308 #ifdef CONFIG_DPP2
1309 if (auth->peer_version >= 2) {
1310 wpa_printf(MSG_DEBUG,
1311 "DPP: Postpone connection attempt to wait for completion of DPP Configuration Result");
1312 auth->connect_on_tx_status = 1;
1313 return 0;
1314 }
1315 #endif /* CONFIG_DPP2 */
1316
1317 wpas_dpp_try_to_connect(wpa_s);
1318 return 0;
1319 }
1320
1321
1322 static int wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
1323 struct dpp_authentication *auth)
1324 {
1325 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1326 if (auth->ssid_len)
1327 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
1328 wpa_ssid_txt(auth->ssid, auth->ssid_len));
1329 if (auth->connector) {
1330 /* TODO: Save the Connector and consider using a command
1331 * to fetch the value instead of sending an event with
1332 * it. The Connector could end up being larger than what
1333 * most clients are ready to receive as an event
1334 * message. */
1335 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
1336 auth->connector);
1337 }
1338 if (auth->c_sign_key) {
1339 char *hex;
1340 size_t hexlen;
1341
1342 hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
1343 hex = os_malloc(hexlen);
1344 if (hex) {
1345 wpa_snprintf_hex(hex, hexlen,
1346 wpabuf_head(auth->c_sign_key),
1347 wpabuf_len(auth->c_sign_key));
1348 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
1349 hex);
1350 os_free(hex);
1351 }
1352 }
1353 if (auth->net_access_key) {
1354 char *hex;
1355 size_t hexlen;
1356
1357 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1358 hex = os_malloc(hexlen);
1359 if (hex) {
1360 wpa_snprintf_hex(hex, hexlen,
1361 wpabuf_head(auth->net_access_key),
1362 wpabuf_len(auth->net_access_key));
1363 if (auth->net_access_key_expiry)
1364 wpa_msg(wpa_s, MSG_INFO,
1365 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1366 (long unsigned)
1367 auth->net_access_key_expiry);
1368 else
1369 wpa_msg(wpa_s, MSG_INFO,
1370 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1371 os_free(hex);
1372 }
1373 }
1374
1375 return wpas_dpp_process_config(wpa_s, auth);
1376 }
1377
1378
1379 static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1380 enum gas_query_result result,
1381 const struct wpabuf *adv_proto,
1382 const struct wpabuf *resp, u16 status_code)
1383 {
1384 struct wpa_supplicant *wpa_s = ctx;
1385 const u8 *pos;
1386 struct dpp_authentication *auth = wpa_s->dpp_auth;
1387 int res;
1388 enum dpp_status_error status = DPP_STATUS_CONFIG_REJECTED;
1389
1390 wpa_s->dpp_gas_dialog_token = -1;
1391
1392 if (!auth || !auth->auth_success) {
1393 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1394 return;
1395 }
1396 if (result != GAS_QUERY_SUCCESS ||
1397 !resp || status_code != WLAN_STATUS_SUCCESS) {
1398 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
1399 goto fail;
1400 }
1401
1402 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
1403 adv_proto);
1404 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
1405 resp);
1406
1407 if (wpabuf_len(adv_proto) != 10 ||
1408 !(pos = wpabuf_head(adv_proto)) ||
1409 pos[0] != WLAN_EID_ADV_PROTO ||
1410 pos[1] != 8 ||
1411 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1412 pos[4] != 5 ||
1413 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1414 pos[8] != 0x1a ||
1415 pos[9] != 1) {
1416 wpa_printf(MSG_DEBUG,
1417 "DPP: Not a DPP Advertisement Protocol ID");
1418 goto fail;
1419 }
1420
1421 if (dpp_conf_resp_rx(auth, resp) < 0) {
1422 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1423 goto fail;
1424 }
1425
1426 res = wpas_dpp_handle_config_obj(wpa_s, auth);
1427 if (res < 0)
1428 goto fail;
1429
1430 status = DPP_STATUS_OK;
1431 fail:
1432 if (status != DPP_STATUS_OK)
1433 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1434 #ifdef CONFIG_DPP2
1435 if (auth->peer_version >= 2 &&
1436 auth->conf_resp_status == DPP_STATUS_OK) {
1437 struct wpabuf *msg;
1438
1439 wpa_printf(MSG_DEBUG, "DPP: Send DPP Configuration Result");
1440 msg = dpp_build_conf_result(auth, status);
1441 if (!msg)
1442 goto fail2;
1443
1444 wpa_msg(wpa_s, MSG_INFO,
1445 DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1446 MAC2STR(addr), auth->curr_freq,
1447 DPP_PA_CONFIGURATION_RESULT);
1448 offchannel_send_action(wpa_s, auth->curr_freq,
1449 addr, wpa_s->own_addr, broadcast,
1450 wpabuf_head(msg),
1451 wpabuf_len(msg),
1452 500, wpas_dpp_tx_status, 0);
1453 wpabuf_free(msg);
1454
1455 /* This exchange will be terminated in the TX status handler */
1456 return;
1457 }
1458 fail2:
1459 #endif /* CONFIG_DPP2 */
1460 dpp_auth_deinit(wpa_s->dpp_auth);
1461 wpa_s->dpp_auth = NULL;
1462 }
1463
1464
1465 static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1466 {
1467 struct dpp_authentication *auth = wpa_s->dpp_auth;
1468 struct wpabuf *buf, *conf_req;
1469 char json[100];
1470 int res;
1471
1472 wpa_s->dpp_gas_client = 1;
1473 os_snprintf(json, sizeof(json),
1474 "{\"name\":\"Test\","
1475 "\"wi-fi_tech\":\"infra\","
1476 "\"netRole\":\"%s\"}",
1477 wpa_s->dpp_netrole_ap ? "ap" : "sta");
1478 #ifdef CONFIG_TESTING_OPTIONS
1479 if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
1480 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
1481 json[29] = 'k'; /* replace "infra" with "knfra" */
1482 }
1483 #endif /* CONFIG_TESTING_OPTIONS */
1484 wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
1485
1486 offchannel_send_action_done(wpa_s);
1487 wpas_dpp_listen_stop(wpa_s);
1488
1489 conf_req = dpp_build_conf_req(auth, json);
1490 if (!conf_req) {
1491 wpa_printf(MSG_DEBUG,
1492 "DPP: No configuration request data available");
1493 return;
1494 }
1495
1496 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
1497 if (!buf) {
1498 wpabuf_free(conf_req);
1499 return;
1500 }
1501
1502 /* Advertisement Protocol IE */
1503 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
1504 wpabuf_put_u8(buf, 8); /* Length */
1505 wpabuf_put_u8(buf, 0x7f);
1506 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
1507 wpabuf_put_u8(buf, 5);
1508 wpabuf_put_be24(buf, OUI_WFA);
1509 wpabuf_put_u8(buf, DPP_OUI_TYPE);
1510 wpabuf_put_u8(buf, 0x01);
1511
1512 /* GAS Query */
1513 wpabuf_put_le16(buf, wpabuf_len(conf_req));
1514 wpabuf_put_buf(buf, conf_req);
1515 wpabuf_free(conf_req);
1516
1517 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1518 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1519
1520 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
1521 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
1522 if (res < 0) {
1523 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1524 wpabuf_free(buf);
1525 } else {
1526 wpa_printf(MSG_DEBUG,
1527 "DPP: GAS query started with dialog token %u", res);
1528 wpa_s->dpp_gas_dialog_token = res;
1529 }
1530 }
1531
1532
1533 static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1534 {
1535 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1536 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
1537 #ifdef CONFIG_TESTING_OPTIONS
1538 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
1539 wpa_printf(MSG_INFO,
1540 "DPP: TESTING - stop at Authentication Confirm");
1541 if (wpa_s->dpp_auth->configurator) {
1542 /* Prevent GAS response */
1543 wpa_s->dpp_auth->auth_success = 0;
1544 }
1545 return;
1546 }
1547 #endif /* CONFIG_TESTING_OPTIONS */
1548
1549 if (wpa_s->dpp_auth->configurator)
1550 wpas_dpp_start_gas_server(wpa_s);
1551 else
1552 wpas_dpp_start_gas_client(wpa_s);
1553 }
1554
1555
1556 static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1557 const u8 *hdr, const u8 *buf, size_t len,
1558 unsigned int freq)
1559 {
1560 struct dpp_authentication *auth = wpa_s->dpp_auth;
1561 struct wpabuf *msg;
1562
1563 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
1564 " (freq %u MHz)", MAC2STR(src), freq);
1565
1566 if (!auth) {
1567 wpa_printf(MSG_DEBUG,
1568 "DPP: No DPP Authentication in progress - drop");
1569 return;
1570 }
1571
1572 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1573 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1574 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1575 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1576 return;
1577 }
1578
1579 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1580
1581 if (auth->curr_freq != freq && auth->neg_freq == freq) {
1582 wpa_printf(MSG_DEBUG,
1583 "DPP: Responder accepted request for different negotiation channel");
1584 auth->curr_freq = freq;
1585 }
1586
1587 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
1588 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
1589 if (!msg) {
1590 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1591 wpa_printf(MSG_DEBUG,
1592 "DPP: Start wait for full response");
1593 offchannel_send_action_done(wpa_s);
1594 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1595 return;
1596 }
1597 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1598 return;
1599 }
1600 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1601
1602 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1603 MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
1604 offchannel_send_action(wpa_s, auth->curr_freq,
1605 src, wpa_s->own_addr, broadcast,
1606 wpabuf_head(msg), wpabuf_len(msg),
1607 500, wpas_dpp_tx_status, 0);
1608 wpabuf_free(msg);
1609 wpa_s->dpp_auth_ok_on_ack = 1;
1610 }
1611
1612
1613 static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
1614 const u8 *hdr, const u8 *buf, size_t len)
1615 {
1616 struct dpp_authentication *auth = wpa_s->dpp_auth;
1617
1618 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1619 MAC2STR(src));
1620
1621 if (!auth) {
1622 wpa_printf(MSG_DEBUG,
1623 "DPP: No DPP Authentication in progress - drop");
1624 return;
1625 }
1626
1627 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1628 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1629 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1630 return;
1631 }
1632
1633 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
1634 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1635 return;
1636 }
1637
1638 wpas_dpp_auth_success(wpa_s, 0);
1639 }
1640
1641
1642 #ifdef CONFIG_DPP2
1643
1644 static void wpas_dpp_config_result_wait_timeout(void *eloop_ctx,
1645 void *timeout_ctx)
1646 {
1647 struct wpa_supplicant *wpa_s = eloop_ctx;
1648 struct dpp_authentication *auth = wpa_s->dpp_auth;
1649
1650 if (!auth || !auth->waiting_conf_result)
1651 return;
1652
1653 wpa_printf(MSG_DEBUG,
1654 "DPP: Timeout while waiting for Configuration Result");
1655 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1656 dpp_auth_deinit(auth);
1657 wpa_s->dpp_auth = NULL;
1658 }
1659
1660
1661 static void wpas_dpp_rx_conf_result(struct wpa_supplicant *wpa_s, const u8 *src,
1662 const u8 *hdr, const u8 *buf, size_t len)
1663 {
1664 struct dpp_authentication *auth = wpa_s->dpp_auth;
1665 enum dpp_status_error status;
1666
1667 wpa_printf(MSG_DEBUG, "DPP: Configuration Result from " MACSTR,
1668 MAC2STR(src));
1669
1670 if (!auth || !auth->waiting_conf_result) {
1671 wpa_printf(MSG_DEBUG,
1672 "DPP: No DPP Configuration waiting for result - drop");
1673 return;
1674 }
1675
1676 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1677 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1678 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1679 return;
1680 }
1681
1682 status = dpp_conf_result_rx(auth, hdr, buf, len);
1683
1684 offchannel_send_action_done(wpa_s);
1685 wpas_dpp_listen_stop(wpa_s);
1686 if (status == DPP_STATUS_OK)
1687 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
1688 else
1689 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1690 dpp_auth_deinit(auth);
1691 wpa_s->dpp_auth = NULL;
1692 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
1693 }
1694
1695 #endif /* CONFIG_DPP2 */
1696
1697
1698 static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1699 const u8 *src,
1700 const u8 *buf, size_t len)
1701 {
1702 struct wpa_ssid *ssid;
1703 const u8 *connector, *trans_id, *status;
1704 u16 connector_len, trans_id_len, status_len;
1705 struct dpp_introduction intro;
1706 struct rsn_pmksa_cache_entry *entry;
1707 struct os_time now;
1708 struct os_reltime rnow;
1709 os_time_t expiry;
1710 unsigned int seconds;
1711 enum dpp_status_error res;
1712
1713 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1714 MAC2STR(src));
1715 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1716 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1717 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1718 MACSTR " - drop", MAC2STR(src));
1719 return;
1720 }
1721 offchannel_send_action_done(wpa_s);
1722
1723 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1724 if (ssid == wpa_s->dpp_intro_network)
1725 break;
1726 }
1727 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1728 !ssid->dpp_csign) {
1729 wpa_printf(MSG_DEBUG,
1730 "DPP: Profile not found for network introduction");
1731 return;
1732 }
1733
1734 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1735 &trans_id_len);
1736 if (!trans_id || trans_id_len != 1) {
1737 wpa_printf(MSG_DEBUG,
1738 "DPP: Peer did not include Transaction ID");
1739 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1740 " fail=missing_transaction_id", MAC2STR(src));
1741 goto fail;
1742 }
1743 if (trans_id[0] != TRANSACTION_ID) {
1744 wpa_printf(MSG_DEBUG,
1745 "DPP: Ignore frame with unexpected Transaction ID %u",
1746 trans_id[0]);
1747 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1748 " fail=transaction_id_mismatch", MAC2STR(src));
1749 goto fail;
1750 }
1751
1752 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1753 if (!status || status_len != 1) {
1754 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1755 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1756 " fail=missing_status", MAC2STR(src));
1757 goto fail;
1758 }
1759 if (status[0] != DPP_STATUS_OK) {
1760 wpa_printf(MSG_DEBUG,
1761 "DPP: Peer rejected network introduction: Status %u",
1762 status[0]);
1763 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1764 " status=%u", MAC2STR(src), status[0]);
1765 goto fail;
1766 }
1767
1768 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1769 if (!connector) {
1770 wpa_printf(MSG_DEBUG,
1771 "DPP: Peer did not include its Connector");
1772 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1773 " fail=missing_connector", MAC2STR(src));
1774 goto fail;
1775 }
1776
1777 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1778 ssid->dpp_netaccesskey,
1779 ssid->dpp_netaccesskey_len,
1780 ssid->dpp_csign,
1781 ssid->dpp_csign_len,
1782 connector, connector_len, &expiry);
1783 if (res != DPP_STATUS_OK) {
1784 wpa_printf(MSG_INFO,
1785 "DPP: Network Introduction protocol resulted in failure");
1786 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1787 " fail=peer_connector_validation_failed", MAC2STR(src));
1788 goto fail;
1789 }
1790
1791 entry = os_zalloc(sizeof(*entry));
1792 if (!entry)
1793 goto fail;
1794 os_memcpy(entry->aa, src, ETH_ALEN);
1795 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1796 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1797 entry->pmk_len = intro.pmk_len;
1798 entry->akmp = WPA_KEY_MGMT_DPP;
1799 if (expiry) {
1800 os_get_time(&now);
1801 seconds = expiry - now.sec;
1802 } else {
1803 seconds = 86400 * 7;
1804 }
1805 os_get_reltime(&rnow);
1806 entry->expiration = rnow.sec + seconds;
1807 entry->reauth_time = rnow.sec + seconds;
1808 entry->network_ctx = ssid;
1809 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1810
1811 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1812 " status=%u", MAC2STR(src), status[0]);
1813
1814 wpa_printf(MSG_DEBUG,
1815 "DPP: Try connection again after successful network introduction");
1816 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1817 wpa_supplicant_cancel_sched_scan(wpa_s);
1818 wpa_supplicant_req_scan(wpa_s, 0, 0);
1819 }
1820 fail:
1821 os_memset(&intro, 0, sizeof(intro));
1822 }
1823
1824
1825 static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1826 {
1827 int i, j;
1828
1829 if (!wpa_s->hw.modes)
1830 return -1;
1831
1832 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1833 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1834
1835 for (j = 0; j < mode->num_channels; j++) {
1836 struct hostapd_channel_data *chan = &mode->channels[j];
1837
1838 if (chan->freq != (int) freq)
1839 continue;
1840
1841 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1842 HOSTAPD_CHAN_NO_IR |
1843 HOSTAPD_CHAN_RADAR))
1844 continue;
1845
1846 return 1;
1847 }
1848 }
1849
1850 wpa_printf(MSG_DEBUG,
1851 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1852 freq);
1853
1854 return 0;
1855 }
1856
1857
1858 static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1859 struct dpp_pkex *pkex)
1860 {
1861 if (pkex->freq == 2437)
1862 pkex->freq = 5745;
1863 else if (pkex->freq == 5745)
1864 pkex->freq = 5220;
1865 else if (pkex->freq == 5220)
1866 pkex->freq = 60480;
1867 else
1868 return -1; /* no more channels to try */
1869
1870 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1871 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1872 pkex->freq);
1873 return 0;
1874 }
1875
1876 /* Could not use this channel - try the next one */
1877 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1878 }
1879
1880
1881 static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1882 {
1883 struct wpa_supplicant *wpa_s = eloop_ctx;
1884 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1885
1886 if (!pkex || !pkex->exchange_req)
1887 return;
1888 if (pkex->exch_req_tries >= 5) {
1889 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1890 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1891 "No response from PKEX peer");
1892 dpp_pkex_free(pkex);
1893 wpa_s->dpp_pkex = NULL;
1894 return;
1895 }
1896 pkex->exch_req_tries = 0;
1897 }
1898
1899 pkex->exch_req_tries++;
1900 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1901 pkex->exch_req_tries);
1902 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1903 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1904 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1905 wpa_s->own_addr, broadcast,
1906 wpabuf_head(pkex->exchange_req),
1907 wpabuf_len(pkex->exchange_req),
1908 pkex->exch_req_wait_time,
1909 wpas_dpp_tx_pkex_status, 0);
1910 }
1911
1912
1913 static void
1914 wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
1915 unsigned int freq, const u8 *dst,
1916 const u8 *src, const u8 *bssid,
1917 const u8 *data, size_t data_len,
1918 enum offchannel_send_action_result result)
1919 {
1920 const char *res_txt;
1921 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1922
1923 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1924 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1925 "FAILED");
1926 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1927 " result=%s (PKEX)",
1928 freq, MAC2STR(dst), res_txt);
1929 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1930 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
1931
1932 if (!pkex) {
1933 wpa_printf(MSG_DEBUG,
1934 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
1935 return;
1936 }
1937
1938 if (pkex->failed) {
1939 wpa_printf(MSG_DEBUG,
1940 "DPP: Terminate PKEX exchange due to an earlier error");
1941 if (pkex->t > pkex->own_bi->pkex_t)
1942 pkex->own_bi->pkex_t = pkex->t;
1943 dpp_pkex_free(pkex);
1944 wpa_s->dpp_pkex = NULL;
1945 return;
1946 }
1947
1948 if (pkex->exch_req_wait_time && pkex->exchange_req) {
1949 /* Wait for PKEX Exchange Response frame and retry request if
1950 * no response is seen. */
1951 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1952 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
1953 (pkex->exch_req_wait_time % 1000) * 1000,
1954 wpas_dpp_pkex_retry_timeout, wpa_s,
1955 NULL);
1956 }
1957 }
1958
1959
1960 static void
1961 wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
1962 const u8 *buf, size_t len, unsigned int freq)
1963 {
1964 struct wpabuf *msg;
1965 unsigned int wait_time;
1966
1967 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
1968 MAC2STR(src));
1969
1970 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1971 * values here */
1972
1973 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
1974 wpa_printf(MSG_DEBUG,
1975 "DPP: No PKEX code configured - ignore request");
1976 return;
1977 }
1978
1979 if (wpa_s->dpp_pkex) {
1980 /* TODO: Support parallel operations */
1981 wpa_printf(MSG_DEBUG,
1982 "DPP: Already in PKEX session - ignore new request");
1983 return;
1984 }
1985
1986 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
1987 wpa_s->own_addr, src,
1988 wpa_s->dpp_pkex_identifier,
1989 wpa_s->dpp_pkex_code,
1990 buf, len);
1991 if (!wpa_s->dpp_pkex) {
1992 wpa_printf(MSG_DEBUG,
1993 "DPP: Failed to process the request - ignore it");
1994 return;
1995 }
1996
1997 msg = wpa_s->dpp_pkex->exchange_resp;
1998 wait_time = wpa_s->max_remain_on_chan;
1999 if (wait_time > 2000)
2000 wait_time = 2000;
2001 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2002 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
2003 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2004 broadcast,
2005 wpabuf_head(msg), wpabuf_len(msg),
2006 wait_time, wpas_dpp_tx_pkex_status, 0);
2007 }
2008
2009
2010 static void
2011 wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2012 const u8 *buf, size_t len, unsigned int freq)
2013 {
2014 struct wpabuf *msg;
2015 unsigned int wait_time;
2016
2017 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
2018 MAC2STR(src));
2019
2020 /* TODO: Support multiple PKEX codes by iterating over all the enabled
2021 * values here */
2022
2023 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
2024 wpa_s->dpp_pkex->exchange_done) {
2025 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2026 return;
2027 }
2028
2029 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2030 wpa_s->dpp_pkex->exch_req_wait_time = 0;
2031
2032 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
2033 if (!msg) {
2034 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2035 return;
2036 }
2037
2038 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
2039 MAC2STR(src));
2040
2041 wait_time = wpa_s->max_remain_on_chan;
2042 if (wait_time > 2000)
2043 wait_time = 2000;
2044 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2045 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
2046 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2047 broadcast,
2048 wpabuf_head(msg), wpabuf_len(msg),
2049 wait_time, wpas_dpp_tx_pkex_status, 0);
2050 wpabuf_free(msg);
2051 }
2052
2053
2054 static struct dpp_bootstrap_info *
2055 wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
2056 unsigned int freq)
2057 {
2058 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2059 struct dpp_bootstrap_info *bi;
2060
2061 bi = os_zalloc(sizeof(*bi));
2062 if (!bi)
2063 return NULL;
2064 bi->id = wpas_dpp_next_id(wpa_s);
2065 bi->type = DPP_BOOTSTRAP_PKEX;
2066 os_memcpy(bi->mac_addr, peer, ETH_ALEN);
2067 bi->num_freq = 1;
2068 bi->freq[0] = freq;
2069 bi->curve = pkex->own_bi->curve;
2070 bi->pubkey = pkex->peer_bootstrap_key;
2071 pkex->peer_bootstrap_key = NULL;
2072 dpp_pkex_free(pkex);
2073 wpa_s->dpp_pkex = NULL;
2074 if (dpp_bootstrap_key_hash(bi) < 0) {
2075 dpp_bootstrap_info_free(bi);
2076 return NULL;
2077 }
2078 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
2079 return bi;
2080 }
2081
2082
2083 static void
2084 wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
2085 const u8 *hdr, const u8 *buf, size_t len,
2086 unsigned int freq)
2087 {
2088 struct wpabuf *msg;
2089 unsigned int wait_time;
2090 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2091
2092 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
2093 MAC2STR(src));
2094
2095 if (!pkex || pkex->initiator || !pkex->exchange_done) {
2096 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2097 return;
2098 }
2099
2100 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
2101 if (!msg) {
2102 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
2103 if (pkex->failed) {
2104 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
2105 if (pkex->t > pkex->own_bi->pkex_t)
2106 pkex->own_bi->pkex_t = pkex->t;
2107 dpp_pkex_free(wpa_s->dpp_pkex);
2108 wpa_s->dpp_pkex = NULL;
2109 }
2110 return;
2111 }
2112
2113 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
2114 MACSTR, MAC2STR(src));
2115
2116 wait_time = wpa_s->max_remain_on_chan;
2117 if (wait_time > 2000)
2118 wait_time = 2000;
2119 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2120 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
2121 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
2122 broadcast,
2123 wpabuf_head(msg), wpabuf_len(msg),
2124 wait_time, wpas_dpp_tx_pkex_status, 0);
2125 wpabuf_free(msg);
2126
2127 wpas_dpp_pkex_finish(wpa_s, src, freq);
2128 }
2129
2130
2131 static void
2132 wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2133 const u8 *hdr, const u8 *buf, size_t len,
2134 unsigned int freq)
2135 {
2136 int res;
2137 struct dpp_bootstrap_info *bi;
2138 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2139 char cmd[500];
2140
2141 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2142 MAC2STR(src));
2143
2144 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2145 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2146 return;
2147 }
2148
2149 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
2150 if (res < 0) {
2151 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2152 return;
2153 }
2154
2155 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
2156 if (!bi)
2157 return;
2158
2159 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2160 bi->id,
2161 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2162 wpa_printf(MSG_DEBUG,
2163 "DPP: Start authentication after PKEX with parameters: %s",
2164 cmd);
2165 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2166 wpa_printf(MSG_DEBUG,
2167 "DPP: Authentication initialization failed");
2168 return;
2169 }
2170 }
2171
2172
2173 void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2174 const u8 *buf, size_t len, unsigned int freq)
2175 {
2176 u8 crypto_suite;
2177 enum dpp_public_action_frame_type type;
2178 const u8 *hdr;
2179 unsigned int pkex_t;
2180
2181 if (len < DPP_HDR_LEN)
2182 return;
2183 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
2184 return;
2185 hdr = buf;
2186 buf += 4;
2187 len -= 4;
2188 crypto_suite = *buf++;
2189 type = *buf++;
2190 len -= 2;
2191
2192 wpa_printf(MSG_DEBUG,
2193 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
2194 MACSTR " freq=%u",
2195 crypto_suite, type, MAC2STR(src), freq);
2196 if (crypto_suite != 1) {
2197 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2198 crypto_suite);
2199 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2200 " freq=%u type=%d ignore=unsupported-crypto-suite",
2201 MAC2STR(src), freq, type);
2202 return;
2203 }
2204 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
2205 if (dpp_check_attrs(buf, len) < 0) {
2206 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2207 " freq=%u type=%d ignore=invalid-attributes",
2208 MAC2STR(src), freq, type);
2209 return;
2210 }
2211 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2212 MAC2STR(src), freq, type);
2213
2214 switch (type) {
2215 case DPP_PA_AUTHENTICATION_REQ:
2216 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
2217 break;
2218 case DPP_PA_AUTHENTICATION_RESP:
2219 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
2220 break;
2221 case DPP_PA_AUTHENTICATION_CONF:
2222 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
2223 break;
2224 case DPP_PA_PEER_DISCOVERY_RESP:
2225 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2226 break;
2227 case DPP_PA_PKEX_EXCHANGE_REQ:
2228 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2229 break;
2230 case DPP_PA_PKEX_EXCHANGE_RESP:
2231 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2232 break;
2233 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
2234 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2235 freq);
2236 break;
2237 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
2238 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2239 freq);
2240 break;
2241 #ifdef CONFIG_DPP2
2242 case DPP_PA_CONFIGURATION_RESULT:
2243 wpas_dpp_rx_conf_result(wpa_s, src, hdr, buf, len);
2244 break;
2245 #endif /* CONFIG_DPP2 */
2246 default:
2247 wpa_printf(MSG_DEBUG,
2248 "DPP: Ignored unsupported frame subtype %d", type);
2249 break;
2250 }
2251
2252 if (wpa_s->dpp_pkex)
2253 pkex_t = wpa_s->dpp_pkex->t;
2254 else if (wpa_s->dpp_pkex_bi)
2255 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2256 else
2257 pkex_t = 0;
2258 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2259 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2260 wpas_dpp_pkex_remove(wpa_s, "*");
2261 }
2262 }
2263
2264
2265 static struct wpabuf *
2266 wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2267 size_t query_len)
2268 {
2269 struct wpa_supplicant *wpa_s = ctx;
2270 struct dpp_authentication *auth = wpa_s->dpp_auth;
2271 struct wpabuf *resp;
2272
2273 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2274 MAC2STR(sa));
2275 if (!auth || !auth->auth_success ||
2276 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2277 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2278 return NULL;
2279 }
2280 wpa_hexdump(MSG_DEBUG,
2281 "DPP: Received Configuration Request (GAS Query Request)",
2282 query, query_len);
2283 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2284 MAC2STR(sa));
2285 resp = dpp_conf_req_rx(auth, query, query_len);
2286 if (!resp)
2287 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2288 auth->conf_resp = resp;
2289 return resp;
2290 }
2291
2292
2293 static void
2294 wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2295 {
2296 struct wpa_supplicant *wpa_s = ctx;
2297 struct dpp_authentication *auth = wpa_s->dpp_auth;
2298
2299 if (!auth) {
2300 wpabuf_free(resp);
2301 return;
2302 }
2303 if (auth->conf_resp != resp) {
2304 wpa_printf(MSG_DEBUG,
2305 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2306 ok);
2307 wpabuf_free(resp);
2308 return;
2309 }
2310
2311 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2312 ok);
2313 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2314 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2315 #ifdef CONFIG_DPP2
2316 if (ok && auth->peer_version >= 2 &&
2317 auth->conf_resp_status == DPP_STATUS_OK) {
2318 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
2319 auth->waiting_conf_result = 1;
2320 auth->conf_resp = NULL;
2321 wpabuf_free(resp);
2322 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
2323 wpa_s, NULL);
2324 eloop_register_timeout(2, 0,
2325 wpas_dpp_config_result_wait_timeout,
2326 wpa_s, NULL);
2327 return;
2328 }
2329 #endif /* CONFIG_DPP2 */
2330 offchannel_send_action_done(wpa_s);
2331 wpas_dpp_listen_stop(wpa_s);
2332 if (ok)
2333 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2334 else
2335 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2336 dpp_auth_deinit(wpa_s->dpp_auth);
2337 wpa_s->dpp_auth = NULL;
2338 wpabuf_free(resp);
2339 }
2340
2341
2342 static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
2343 {
2344 struct dpp_configurator *conf;
2345 unsigned int max_id = 0;
2346
2347 dl_list_for_each(conf, &wpa_s->dpp_configurator,
2348 struct dpp_configurator, list) {
2349 if (conf->id > max_id)
2350 max_id = conf->id;
2351 }
2352 return max_id + 1;
2353 }
2354
2355
2356 int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
2357 {
2358 char *curve = NULL;
2359 char *key = NULL;
2360 u8 *privkey = NULL;
2361 size_t privkey_len = 0;
2362 int ret = -1;
2363 struct dpp_configurator *conf = NULL;
2364
2365 curve = get_param(cmd, " curve=");
2366 key = get_param(cmd, " key=");
2367
2368 if (key) {
2369 privkey_len = os_strlen(key) / 2;
2370 privkey = os_malloc(privkey_len);
2371 if (!privkey ||
2372 hexstr2bin(key, privkey, privkey_len) < 0)
2373 goto fail;
2374 }
2375
2376 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
2377 if (!conf)
2378 goto fail;
2379
2380 conf->id = wpas_dpp_next_configurator_id(wpa_s);
2381 dl_list_add(&wpa_s->dpp_configurator, &conf->list);
2382 ret = conf->id;
2383 conf = NULL;
2384 fail:
2385 os_free(curve);
2386 str_clear_free(key);
2387 bin_clear_free(privkey, privkey_len);
2388 dpp_configurator_free(conf);
2389 return ret;
2390 }
2391
2392
2393 static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
2394 {
2395 struct dpp_configurator *conf, *tmp;
2396 int found = 0;
2397
2398 dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
2399 struct dpp_configurator, list) {
2400 if (id && conf->id != id)
2401 continue;
2402 found = 1;
2403 dl_list_del(&conf->list);
2404 dpp_configurator_free(conf);
2405 }
2406
2407 if (id == 0)
2408 return 0; /* flush succeeds regardless of entries found */
2409 return found ? 0 : -1;
2410 }
2411
2412
2413 int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
2414 {
2415 unsigned int id_val;
2416
2417 if (os_strcmp(id, "*") == 0) {
2418 id_val = 0;
2419 } else {
2420 id_val = atoi(id);
2421 if (id_val == 0)
2422 return -1;
2423 }
2424
2425 return dpp_configurator_del(wpa_s, id_val);
2426 }
2427
2428
2429 int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2430 {
2431 struct dpp_authentication *auth;
2432 int ret = -1;
2433 char *curve = NULL;
2434
2435 auth = os_zalloc(sizeof(*auth));
2436 if (!auth)
2437 return -1;
2438
2439 curve = get_param(cmd, " curve=");
2440 wpas_dpp_set_testing_options(wpa_s, auth);
2441 if (wpas_dpp_set_configurator(wpa_s, auth, cmd) == 0 &&
2442 dpp_configurator_own_config(auth, curve, 0) == 0)
2443 ret = wpas_dpp_handle_config_obj(wpa_s, auth);
2444
2445 dpp_auth_deinit(auth);
2446 os_free(curve);
2447
2448 return ret;
2449 }
2450
2451
2452 int wpas_dpp_configurator_get_key(struct wpa_supplicant *wpa_s, unsigned int id,
2453 char *buf, size_t buflen)
2454 {
2455 struct dpp_configurator *conf;
2456
2457 conf = dpp_configurator_get_id(wpa_s, id);
2458 if (!conf)
2459 return -1;
2460
2461 return dpp_configurator_get_key(conf, buf, buflen);
2462 }
2463
2464
2465 static void
2466 wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2467 unsigned int freq, const u8 *dst,
2468 const u8 *src, const u8 *bssid,
2469 const u8 *data, size_t data_len,
2470 enum offchannel_send_action_result result)
2471 {
2472 const char *res_txt;
2473
2474 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2475 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2476 "FAILED");
2477 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2478 " result=%s (DPP Peer Discovery Request)",
2479 freq, MAC2STR(dst), res_txt);
2480 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2481 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
2482 /* TODO: Time out wait for response more quickly in error cases? */
2483 }
2484
2485
2486 int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2487 struct wpa_bss *bss)
2488 {
2489 struct os_time now;
2490 struct wpabuf *msg;
2491 unsigned int wait_time;
2492
2493 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2494 return 0; /* Not using DPP AKM - continue */
2495 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2496 return 0; /* PMKSA exists for DPP AKM - continue */
2497
2498 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2499 !ssid->dpp_csign) {
2500 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2501 "missing %s",
2502 !ssid->dpp_connector ? "Connector" :
2503 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2504 "C-sign-key"));
2505 return -1;
2506 }
2507
2508 os_get_time(&now);
2509
2510 if (ssid->dpp_netaccesskey_expiry &&
2511 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
2512 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2513 "netAccessKey expired");
2514 return -1;
2515 }
2516
2517 wpa_printf(MSG_DEBUG,
2518 "DPP: Starting network introduction protocol to derive PMKSA for "
2519 MACSTR, MAC2STR(bss->bssid));
2520
2521 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
2522 5 + 4 + os_strlen(ssid->dpp_connector));
2523 if (!msg)
2524 return -1;
2525
2526 #ifdef CONFIG_TESTING_OPTIONS
2527 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2528 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2529 goto skip_trans_id;
2530 }
2531 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2532 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2533 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2534 wpabuf_put_le16(msg, 0);
2535 goto skip_trans_id;
2536 }
2537 #endif /* CONFIG_TESTING_OPTIONS */
2538
2539 /* Transaction ID */
2540 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2541 wpabuf_put_le16(msg, 1);
2542 wpabuf_put_u8(msg, TRANSACTION_ID);
2543
2544 #ifdef CONFIG_TESTING_OPTIONS
2545 skip_trans_id:
2546 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2547 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2548 goto skip_connector;
2549 }
2550 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2551 char *connector;
2552
2553 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2554 connector = dpp_corrupt_connector_signature(
2555 ssid->dpp_connector);
2556 if (!connector) {
2557 wpabuf_free(msg);
2558 return -1;
2559 }
2560 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2561 wpabuf_put_le16(msg, os_strlen(connector));
2562 wpabuf_put_str(msg, connector);
2563 os_free(connector);
2564 goto skip_connector;
2565 }
2566 #endif /* CONFIG_TESTING_OPTIONS */
2567
2568 /* DPP Connector */
2569 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2570 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2571 wpabuf_put_str(msg, ssid->dpp_connector);
2572
2573 #ifdef CONFIG_TESTING_OPTIONS
2574 skip_connector:
2575 #endif /* CONFIG_TESTING_OPTIONS */
2576
2577 /* TODO: Timeout on AP response */
2578 wait_time = wpa_s->max_remain_on_chan;
2579 if (wait_time > 2000)
2580 wait_time = 2000;
2581 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2582 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
2583 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2584 broadcast,
2585 wpabuf_head(msg), wpabuf_len(msg),
2586 wait_time, wpas_dpp_tx_introduction_status, 0);
2587 wpabuf_free(msg);
2588
2589 /* Request this connection attempt to terminate - new one will be
2590 * started when network introduction protocol completes */
2591 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2592 wpa_s->dpp_intro_network = ssid;
2593 return 1;
2594 }
2595
2596
2597 int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2598 {
2599 struct dpp_bootstrap_info *own_bi;
2600 const char *pos, *end;
2601 unsigned int wait_time;
2602
2603 pos = os_strstr(cmd, " own=");
2604 if (!pos)
2605 return -1;
2606 pos += 5;
2607 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
2608 if (!own_bi) {
2609 wpa_printf(MSG_DEBUG,
2610 "DPP: Identified bootstrap info not found");
2611 return -1;
2612 }
2613 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2614 wpa_printf(MSG_DEBUG,
2615 "DPP: Identified bootstrap info not for PKEX");
2616 return -1;
2617 }
2618 wpa_s->dpp_pkex_bi = own_bi;
2619 own_bi->pkex_t = 0; /* clear pending errors on new code */
2620
2621 os_free(wpa_s->dpp_pkex_identifier);
2622 wpa_s->dpp_pkex_identifier = NULL;
2623 pos = os_strstr(cmd, " identifier=");
2624 if (pos) {
2625 pos += 12;
2626 end = os_strchr(pos, ' ');
2627 if (!end)
2628 return -1;
2629 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2630 if (!wpa_s->dpp_pkex_identifier)
2631 return -1;
2632 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2633 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2634 }
2635
2636 pos = os_strstr(cmd, " code=");
2637 if (!pos)
2638 return -1;
2639 os_free(wpa_s->dpp_pkex_code);
2640 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2641 if (!wpa_s->dpp_pkex_code)
2642 return -1;
2643
2644 if (os_strstr(cmd, " init=1")) {
2645 struct dpp_pkex *pkex;
2646 struct wpabuf *msg;
2647
2648 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2649 dpp_pkex_free(wpa_s->dpp_pkex);
2650 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
2651 wpa_s->dpp_pkex_identifier,
2652 wpa_s->dpp_pkex_code);
2653 pkex = wpa_s->dpp_pkex;
2654 if (!pkex)
2655 return -1;
2656
2657 msg = pkex->exchange_req;
2658 wait_time = wpa_s->max_remain_on_chan;
2659 if (wait_time > 2000)
2660 wait_time = 2000;
2661 pkex->freq = 2437;
2662 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2663 " freq=%u type=%d",
2664 MAC2STR(broadcast), pkex->freq,
2665 DPP_PA_PKEX_EXCHANGE_REQ);
2666 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2667 wpa_s->own_addr, broadcast,
2668 wpabuf_head(msg), wpabuf_len(msg),
2669 wait_time, wpas_dpp_tx_pkex_status, 0);
2670 if (wait_time == 0)
2671 wait_time = 2000;
2672 pkex->exch_req_wait_time = wait_time;
2673 pkex->exch_req_tries = 1;
2674 }
2675
2676 /* TODO: Support multiple PKEX info entries */
2677
2678 os_free(wpa_s->dpp_pkex_auth_cmd);
2679 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2680
2681 return 1;
2682 }
2683
2684
2685 int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2686 {
2687 unsigned int id_val;
2688
2689 if (os_strcmp(id, "*") == 0) {
2690 id_val = 0;
2691 } else {
2692 id_val = atoi(id);
2693 if (id_val == 0)
2694 return -1;
2695 }
2696
2697 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2698 return -1;
2699
2700 /* TODO: Support multiple PKEX entries */
2701 os_free(wpa_s->dpp_pkex_code);
2702 wpa_s->dpp_pkex_code = NULL;
2703 os_free(wpa_s->dpp_pkex_identifier);
2704 wpa_s->dpp_pkex_identifier = NULL;
2705 os_free(wpa_s->dpp_pkex_auth_cmd);
2706 wpa_s->dpp_pkex_auth_cmd = NULL;
2707 wpa_s->dpp_pkex_bi = NULL;
2708 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2709 dpp_pkex_free(wpa_s->dpp_pkex);
2710 wpa_s->dpp_pkex = NULL;
2711 return 0;
2712 }
2713
2714
2715 void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2716 {
2717 dpp_auth_deinit(wpa_s->dpp_auth);
2718 wpa_s->dpp_auth = NULL;
2719 dpp_pkex_free(wpa_s->dpp_pkex);
2720 wpa_s->dpp_pkex = NULL;
2721 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2722 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
2723 }
2724
2725
2726 int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2727 {
2728 u8 adv_proto_id[7];
2729
2730 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2731 adv_proto_id[1] = 5;
2732 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2733 adv_proto_id[5] = DPP_OUI_TYPE;
2734 adv_proto_id[6] = 0x01;
2735
2736 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2737 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2738 wpas_dpp_gas_status_handler, wpa_s) < 0)
2739 return -1;
2740 dl_list_init(&wpa_s->dpp_bootstrap);
2741 dl_list_init(&wpa_s->dpp_configurator);
2742 wpa_s->dpp_init_done = 1;
2743 return 0;
2744 }
2745
2746
2747 void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2748 {
2749 #ifdef CONFIG_TESTING_OPTIONS
2750 os_free(wpa_s->dpp_config_obj_override);
2751 wpa_s->dpp_config_obj_override = NULL;
2752 os_free(wpa_s->dpp_discovery_override);
2753 wpa_s->dpp_discovery_override = NULL;
2754 os_free(wpa_s->dpp_groups_override);
2755 wpa_s->dpp_groups_override = NULL;
2756 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2757 #endif /* CONFIG_TESTING_OPTIONS */
2758 if (!wpa_s->dpp_init_done)
2759 return;
2760 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2761 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2762 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
2763 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2764 #ifdef CONFIG_DPP2
2765 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
2766 #endif /* CONFIG_DPP2 */
2767 offchannel_send_action_done(wpa_s);
2768 wpas_dpp_listen_stop(wpa_s);
2769 dpp_bootstrap_del(wpa_s, 0);
2770 dpp_configurator_del(wpa_s, 0);
2771 wpas_dpp_stop(wpa_s);
2772 wpas_dpp_pkex_remove(wpa_s, "*");
2773 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
2774 os_free(wpa_s->dpp_configurator_params);
2775 wpa_s->dpp_configurator_params = NULL;
2776 }