]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/dpp_supplicant.c
DPP2: PFS for PTK derivation
[thirdparty/hostap.git] / wpa_supplicant / dpp_supplicant.c
1 /*
2 * wpa_supplicant - DPP
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2019, 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;
558
559 if (!cmd)
560 return 0;
561
562 wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
563
564 pos = os_strstr(cmd, " configurator=");
565 if (pos) {
566 pos += 14;
567 auth->conf = dpp_configurator_get_id(wpa_s, atoi(pos));
568 if (!auth->conf) {
569 wpa_printf(MSG_INFO,
570 "DPP: Could not find the specified configurator");
571 return -1;
572 }
573 }
574
575 if (dpp_configuration_parse(auth, cmd) < 0) {
576 wpa_msg(wpa_s, MSG_INFO,
577 "DPP: Failed to set configurator parameters");
578 return -1;
579 }
580 return 0;
581 }
582
583
584 static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
585 {
586 struct wpa_supplicant *wpa_s = eloop_ctx;
587
588 if (!wpa_s->dpp_auth)
589 return;
590 wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
591 wpas_dpp_auth_init_next(wpa_s);
592 }
593
594
595 static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
596 {
597 struct dpp_authentication *auth = wpa_s->dpp_auth;
598 const u8 *dst;
599 unsigned int wait_time, max_wait_time, freq, max_tries, used;
600 struct os_reltime now, diff;
601
602 wpa_s->dpp_in_response_listen = 0;
603 if (!auth)
604 return -1;
605
606 if (auth->freq_idx == 0)
607 os_get_reltime(&wpa_s->dpp_init_iter_start);
608
609 if (auth->freq_idx >= auth->num_freq) {
610 auth->num_freq_iters++;
611 if (wpa_s->dpp_init_max_tries)
612 max_tries = wpa_s->dpp_init_max_tries;
613 else
614 max_tries = 5;
615 if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
616 wpa_printf(MSG_INFO,
617 "DPP: No response received from responder - stopping initiation attempt");
618 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
619 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
620 wpa_s, NULL);
621 offchannel_send_action_done(wpa_s);
622 dpp_auth_deinit(wpa_s->dpp_auth);
623 wpa_s->dpp_auth = NULL;
624 return -1;
625 }
626 auth->freq_idx = 0;
627 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
628 if (wpa_s->dpp_init_retry_time)
629 wait_time = wpa_s->dpp_init_retry_time;
630 else
631 wait_time = 10000;
632 os_get_reltime(&now);
633 os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
634 used = diff.sec * 1000 + diff.usec / 1000;
635 if (used > wait_time)
636 wait_time = 0;
637 else
638 wait_time -= used;
639 wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
640 wait_time);
641 eloop_register_timeout(wait_time / 1000,
642 (wait_time % 1000) * 1000,
643 wpas_dpp_init_timeout, wpa_s,
644 NULL);
645 return 0;
646 }
647 freq = auth->freq[auth->freq_idx++];
648 auth->curr_freq = freq;
649
650 if (is_zero_ether_addr(auth->peer_bi->mac_addr))
651 dst = broadcast;
652 else
653 dst = auth->peer_bi->mac_addr;
654 wpa_s->dpp_auth_ok_on_ack = 0;
655 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
656 wait_time = wpa_s->max_remain_on_chan;
657 max_wait_time = wpa_s->dpp_resp_wait_time ?
658 wpa_s->dpp_resp_wait_time : 2000;
659 if (wait_time > max_wait_time)
660 wait_time = max_wait_time;
661 wait_time += 10; /* give the driver some extra time to complete */
662 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
663 wpas_dpp_reply_wait_timeout,
664 wpa_s, NULL);
665 wait_time -= 10;
666 if (auth->neg_freq > 0 && freq != auth->neg_freq) {
667 wpa_printf(MSG_DEBUG,
668 "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
669 freq, auth->neg_freq);
670 }
671 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
672 MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
673 auth->auth_req_ack = 0;
674 os_get_reltime(&wpa_s->dpp_last_init);
675 return offchannel_send_action(wpa_s, freq, dst,
676 wpa_s->own_addr, broadcast,
677 wpabuf_head(auth->req_msg),
678 wpabuf_len(auth->req_msg),
679 wait_time, wpas_dpp_tx_status, 0);
680 }
681
682
683 int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
684 {
685 const char *pos;
686 struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
687 u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
688 unsigned int neg_freq = 0;
689
690 wpa_s->dpp_gas_client = 0;
691
692 pos = os_strstr(cmd, " peer=");
693 if (!pos)
694 return -1;
695 pos += 6;
696 peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
697 if (!peer_bi) {
698 wpa_printf(MSG_INFO,
699 "DPP: Could not find bootstrapping info for the identified peer");
700 return -1;
701 }
702
703 pos = os_strstr(cmd, " own=");
704 if (pos) {
705 pos += 5;
706 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
707 if (!own_bi) {
708 wpa_printf(MSG_INFO,
709 "DPP: Could not find bootstrapping info for the identified local entry");
710 return -1;
711 }
712
713 if (peer_bi->curve != own_bi->curve) {
714 wpa_printf(MSG_INFO,
715 "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
716 peer_bi->curve->name, own_bi->curve->name);
717 return -1;
718 }
719 }
720
721 pos = os_strstr(cmd, " role=");
722 if (pos) {
723 pos += 6;
724 if (os_strncmp(pos, "configurator", 12) == 0)
725 allowed_roles = DPP_CAPAB_CONFIGURATOR;
726 else if (os_strncmp(pos, "enrollee", 8) == 0)
727 allowed_roles = DPP_CAPAB_ENROLLEE;
728 else if (os_strncmp(pos, "either", 6) == 0)
729 allowed_roles = DPP_CAPAB_CONFIGURATOR |
730 DPP_CAPAB_ENROLLEE;
731 else
732 goto fail;
733 }
734
735 pos = os_strstr(cmd, " netrole=");
736 if (pos) {
737 pos += 9;
738 wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
739 }
740
741 pos = os_strstr(cmd, " neg_freq=");
742 if (pos)
743 neg_freq = atoi(pos + 10);
744
745 if (wpa_s->dpp_auth) {
746 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
747 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
748 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
749 NULL);
750 offchannel_send_action_done(wpa_s);
751 dpp_auth_deinit(wpa_s->dpp_auth);
752 }
753 wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles,
754 neg_freq,
755 wpa_s->hw.modes, wpa_s->hw.num_modes);
756 if (!wpa_s->dpp_auth)
757 goto fail;
758 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
759 if (wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth, cmd) < 0) {
760 dpp_auth_deinit(wpa_s->dpp_auth);
761 wpa_s->dpp_auth = NULL;
762 goto fail;
763 }
764
765 wpa_s->dpp_auth->neg_freq = neg_freq;
766
767 if (!is_zero_ether_addr(peer_bi->mac_addr))
768 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
769 ETH_ALEN);
770
771 return wpas_dpp_auth_init_next(wpa_s);
772 fail:
773 return -1;
774 }
775
776
777 struct wpas_dpp_listen_work {
778 unsigned int freq;
779 unsigned int duration;
780 struct wpabuf *probe_resp_ie;
781 };
782
783
784 static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
785 {
786 if (!lwork)
787 return;
788 os_free(lwork);
789 }
790
791
792 static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
793 {
794 struct wpas_dpp_listen_work *lwork;
795
796 if (!wpa_s->dpp_listen_work)
797 return;
798
799 lwork = wpa_s->dpp_listen_work->ctx;
800 wpas_dpp_listen_work_free(lwork);
801 radio_work_done(wpa_s->dpp_listen_work);
802 wpa_s->dpp_listen_work = NULL;
803 }
804
805
806 static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
807 {
808 struct wpa_supplicant *wpa_s = work->wpa_s;
809 struct wpas_dpp_listen_work *lwork = work->ctx;
810
811 if (deinit) {
812 if (work->started) {
813 wpa_s->dpp_listen_work = NULL;
814 wpas_dpp_listen_stop(wpa_s);
815 }
816 wpas_dpp_listen_work_free(lwork);
817 return;
818 }
819
820 wpa_s->dpp_listen_work = work;
821
822 wpa_s->dpp_pending_listen_freq = lwork->freq;
823
824 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
825 wpa_s->max_remain_on_chan) < 0) {
826 wpa_printf(MSG_DEBUG,
827 "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
828 lwork->freq);
829 wpa_s->dpp_listen_freq = 0;
830 wpas_dpp_listen_work_done(wpa_s);
831 wpa_s->dpp_pending_listen_freq = 0;
832 return;
833 }
834 wpa_s->off_channel_freq = 0;
835 wpa_s->roc_waiting_drv_freq = lwork->freq;
836 }
837
838
839 static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
840 unsigned int freq)
841 {
842 struct wpas_dpp_listen_work *lwork;
843
844 if (wpa_s->dpp_listen_work) {
845 wpa_printf(MSG_DEBUG,
846 "DPP: Reject start_listen since dpp_listen_work already exists");
847 return -1;
848 }
849
850 if (wpa_s->dpp_listen_freq)
851 wpas_dpp_listen_stop(wpa_s);
852 wpa_s->dpp_listen_freq = freq;
853
854 lwork = os_zalloc(sizeof(*lwork));
855 if (!lwork)
856 return -1;
857 lwork->freq = freq;
858
859 if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
860 lwork) < 0) {
861 wpas_dpp_listen_work_free(lwork);
862 return -1;
863 }
864
865 return 0;
866 }
867
868
869 int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
870 {
871 int freq;
872
873 freq = atoi(cmd);
874 if (freq <= 0)
875 return -1;
876
877 if (os_strstr(cmd, " role=configurator"))
878 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
879 else if (os_strstr(cmd, " role=enrollee"))
880 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
881 else
882 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
883 DPP_CAPAB_ENROLLEE;
884 wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
885 wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
886 if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
887 wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
888 freq);
889 return 0;
890 }
891
892 return wpas_dpp_listen_start(wpa_s, freq);
893 }
894
895
896 void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
897 {
898 wpa_s->dpp_in_response_listen = 0;
899 if (!wpa_s->dpp_listen_freq)
900 return;
901
902 wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
903 wpa_s->dpp_listen_freq);
904 wpa_drv_cancel_remain_on_channel(wpa_s);
905 wpa_s->dpp_listen_freq = 0;
906 wpas_dpp_listen_work_done(wpa_s);
907 }
908
909
910 void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
911 unsigned int freq)
912 {
913 wpas_dpp_listen_work_done(wpa_s);
914
915 if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
916 unsigned int new_freq;
917
918 /* Continue listen with a new remain-on-channel */
919 if (wpa_s->dpp_auth->neg_freq > 0)
920 new_freq = wpa_s->dpp_auth->neg_freq;
921 else
922 new_freq = wpa_s->dpp_auth->curr_freq;
923 wpa_printf(MSG_DEBUG,
924 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
925 new_freq);
926 wpas_dpp_listen_start(wpa_s, new_freq);
927 return;
928 }
929
930 if (wpa_s->dpp_listen_freq) {
931 /* Continue listen with a new remain-on-channel */
932 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
933 }
934 }
935
936
937 static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
938 const u8 *hdr, const u8 *buf, size_t len,
939 unsigned int freq)
940 {
941 const u8 *r_bootstrap, *i_bootstrap;
942 u16 r_bootstrap_len, i_bootstrap_len;
943 struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
944
945 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
946 MAC2STR(src));
947
948 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
949 &r_bootstrap_len);
950 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
951 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
952 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
953 return;
954 }
955 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
956 r_bootstrap, r_bootstrap_len);
957
958 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
959 &i_bootstrap_len);
960 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
961 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
962 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
963 return;
964 }
965 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
966 i_bootstrap, i_bootstrap_len);
967
968 /* Try to find own and peer bootstrapping key matches based on the
969 * received hash values */
970 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
971 list) {
972 if (!own_bi && bi->own &&
973 os_memcmp(bi->pubkey_hash, r_bootstrap,
974 SHA256_MAC_LEN) == 0) {
975 wpa_printf(MSG_DEBUG,
976 "DPP: Found matching own bootstrapping information");
977 own_bi = bi;
978 }
979
980 if (!peer_bi && !bi->own &&
981 os_memcmp(bi->pubkey_hash, i_bootstrap,
982 SHA256_MAC_LEN) == 0) {
983 wpa_printf(MSG_DEBUG,
984 "DPP: Found matching peer bootstrapping information");
985 peer_bi = bi;
986 }
987
988 if (own_bi && peer_bi)
989 break;
990 }
991
992 if (!own_bi) {
993 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
994 "No matching own bootstrapping key found - ignore message");
995 return;
996 }
997
998 if (wpa_s->dpp_auth) {
999 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1000 "Already in DPP authentication exchange - ignore new one");
1001 return;
1002 }
1003
1004 wpa_s->dpp_gas_client = 0;
1005 wpa_s->dpp_auth_ok_on_ack = 0;
1006 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
1007 wpa_s->dpp_qr_mutual,
1008 peer_bi, own_bi, freq, hdr, buf, len);
1009 if (!wpa_s->dpp_auth) {
1010 wpa_printf(MSG_DEBUG, "DPP: No response generated");
1011 return;
1012 }
1013 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
1014 if (wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth,
1015 wpa_s->dpp_configurator_params) < 0) {
1016 dpp_auth_deinit(wpa_s->dpp_auth);
1017 wpa_s->dpp_auth = NULL;
1018 return;
1019 }
1020 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
1021
1022 if (wpa_s->dpp_listen_freq &&
1023 wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
1024 wpa_printf(MSG_DEBUG,
1025 "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
1026 wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
1027 wpas_dpp_listen_stop(wpa_s);
1028 }
1029
1030 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1031 MAC2STR(src), wpa_s->dpp_auth->curr_freq,
1032 DPP_PA_AUTHENTICATION_RESP);
1033 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
1034 src, wpa_s->own_addr, broadcast,
1035 wpabuf_head(wpa_s->dpp_auth->resp_msg),
1036 wpabuf_len(wpa_s->dpp_auth->resp_msg),
1037 500, wpas_dpp_tx_status, 0);
1038 }
1039
1040
1041 static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
1042 {
1043 /* TODO: stop wait and start ROC */
1044 }
1045
1046
1047 static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
1048 struct dpp_authentication *auth)
1049 {
1050 struct wpa_ssid *ssid;
1051
1052 #ifdef CONFIG_DPP2
1053 if (auth->akm == DPP_AKM_SAE) {
1054 #ifdef CONFIG_SAE
1055 struct wpa_driver_capa capa;
1056 int res;
1057
1058 res = wpa_drv_get_capa(wpa_s, &capa);
1059 if (res == 0 &&
1060 !(capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE) &&
1061 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
1062 wpa_printf(MSG_DEBUG,
1063 "DPP: SAE not supported by the driver");
1064 return NULL;
1065 }
1066 #else /* CONFIG_SAE */
1067 wpa_printf(MSG_DEBUG, "DPP: SAE not supported in the build");
1068 return NULL;
1069 #endif /* CONFIG_SAE */
1070 }
1071 #endif /* CONFIG_DPP2 */
1072
1073 ssid = wpa_config_add_network(wpa_s->conf);
1074 if (!ssid)
1075 return NULL;
1076 wpas_notify_network_added(wpa_s, ssid);
1077 wpa_config_set_network_defaults(ssid);
1078 ssid->disabled = 1;
1079
1080 ssid->ssid = os_malloc(auth->ssid_len);
1081 if (!ssid->ssid)
1082 goto fail;
1083 os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
1084 ssid->ssid_len = auth->ssid_len;
1085
1086 if (auth->connector) {
1087 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
1088 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
1089 ssid->dpp_connector = os_strdup(auth->connector);
1090 if (!ssid->dpp_connector)
1091 goto fail;
1092 }
1093
1094 if (auth->c_sign_key) {
1095 ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
1096 if (!ssid->dpp_csign)
1097 goto fail;
1098 os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
1099 wpabuf_len(auth->c_sign_key));
1100 ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
1101 }
1102
1103 if (auth->net_access_key) {
1104 ssid->dpp_netaccesskey =
1105 os_malloc(wpabuf_len(auth->net_access_key));
1106 if (!ssid->dpp_netaccesskey)
1107 goto fail;
1108 os_memcpy(ssid->dpp_netaccesskey,
1109 wpabuf_head(auth->net_access_key),
1110 wpabuf_len(auth->net_access_key));
1111 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
1112 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
1113 }
1114
1115 if (!auth->connector || dpp_akm_psk(auth->akm) ||
1116 dpp_akm_sae(auth->akm)) {
1117 if (!auth->connector)
1118 ssid->key_mgmt = 0;
1119 if (dpp_akm_psk(auth->akm))
1120 ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
1121 WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
1122 if (dpp_akm_sae(auth->akm))
1123 ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
1124 WPA_KEY_MGMT_FT_SAE;
1125 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
1126 if (auth->passphrase[0]) {
1127 if (wpa_config_set_quoted(ssid, "psk",
1128 auth->passphrase) < 0)
1129 goto fail;
1130 wpa_config_update_psk(ssid);
1131 ssid->export_keys = 1;
1132 } else {
1133 ssid->psk_set = auth->psk_set;
1134 os_memcpy(ssid->psk, auth->psk, PMK_LEN);
1135 }
1136 }
1137
1138 return ssid;
1139 fail:
1140 wpas_notify_network_removed(wpa_s, ssid);
1141 wpa_config_remove_network(wpa_s->conf, ssid->id);
1142 return NULL;
1143 }
1144
1145
1146 static int wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
1147 struct dpp_authentication *auth)
1148 {
1149 struct wpa_ssid *ssid;
1150
1151 if (wpa_s->conf->dpp_config_processing < 1)
1152 return 0;
1153
1154 ssid = wpas_dpp_add_network(wpa_s, auth);
1155 if (!ssid)
1156 return -1;
1157
1158 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
1159 if (wpa_s->conf->dpp_config_processing == 2)
1160 ssid->disabled = 0;
1161
1162 #ifndef CONFIG_NO_CONFIG_WRITE
1163 if (wpa_s->conf->update_config &&
1164 wpa_config_write(wpa_s->confname, wpa_s->conf))
1165 wpa_printf(MSG_DEBUG, "DPP: Failed to update configuration");
1166 #endif /* CONFIG_NO_CONFIG_WRITE */
1167
1168 if (wpa_s->conf->dpp_config_processing < 2)
1169 return 0;
1170
1171 #ifdef CONFIG_DPP2
1172 if (auth->peer_version >= 2) {
1173 wpa_printf(MSG_DEBUG,
1174 "DPP: Postpone connection attempt to wait for completion of DPP Configuration Result");
1175 auth->connect_on_tx_status = 1;
1176 return 0;
1177 }
1178 #endif /* CONFIG_DPP2 */
1179
1180 wpas_dpp_try_to_connect(wpa_s);
1181 return 0;
1182 }
1183
1184
1185 static int wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
1186 struct dpp_authentication *auth)
1187 {
1188 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1189 if (auth->ssid_len)
1190 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
1191 wpa_ssid_txt(auth->ssid, auth->ssid_len));
1192 if (auth->connector) {
1193 /* TODO: Save the Connector and consider using a command
1194 * to fetch the value instead of sending an event with
1195 * it. The Connector could end up being larger than what
1196 * most clients are ready to receive as an event
1197 * message. */
1198 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
1199 auth->connector);
1200 }
1201 if (auth->c_sign_key) {
1202 char *hex;
1203 size_t hexlen;
1204
1205 hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
1206 hex = os_malloc(hexlen);
1207 if (hex) {
1208 wpa_snprintf_hex(hex, hexlen,
1209 wpabuf_head(auth->c_sign_key),
1210 wpabuf_len(auth->c_sign_key));
1211 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
1212 hex);
1213 os_free(hex);
1214 }
1215 }
1216 if (auth->net_access_key) {
1217 char *hex;
1218 size_t hexlen;
1219
1220 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1221 hex = os_malloc(hexlen);
1222 if (hex) {
1223 wpa_snprintf_hex(hex, hexlen,
1224 wpabuf_head(auth->net_access_key),
1225 wpabuf_len(auth->net_access_key));
1226 if (auth->net_access_key_expiry)
1227 wpa_msg(wpa_s, MSG_INFO,
1228 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1229 (long unsigned)
1230 auth->net_access_key_expiry);
1231 else
1232 wpa_msg(wpa_s, MSG_INFO,
1233 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1234 os_free(hex);
1235 }
1236 }
1237
1238 return wpas_dpp_process_config(wpa_s, auth);
1239 }
1240
1241
1242 static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1243 enum gas_query_result result,
1244 const struct wpabuf *adv_proto,
1245 const struct wpabuf *resp, u16 status_code)
1246 {
1247 struct wpa_supplicant *wpa_s = ctx;
1248 const u8 *pos;
1249 struct dpp_authentication *auth = wpa_s->dpp_auth;
1250 int res;
1251 enum dpp_status_error status = DPP_STATUS_CONFIG_REJECTED;
1252
1253 wpa_s->dpp_gas_dialog_token = -1;
1254
1255 if (!auth || !auth->auth_success) {
1256 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1257 return;
1258 }
1259 if (result != GAS_QUERY_SUCCESS ||
1260 !resp || status_code != WLAN_STATUS_SUCCESS) {
1261 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
1262 goto fail;
1263 }
1264
1265 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
1266 adv_proto);
1267 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
1268 resp);
1269
1270 if (wpabuf_len(adv_proto) != 10 ||
1271 !(pos = wpabuf_head(adv_proto)) ||
1272 pos[0] != WLAN_EID_ADV_PROTO ||
1273 pos[1] != 8 ||
1274 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1275 pos[4] != 5 ||
1276 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1277 pos[8] != 0x1a ||
1278 pos[9] != 1) {
1279 wpa_printf(MSG_DEBUG,
1280 "DPP: Not a DPP Advertisement Protocol ID");
1281 goto fail;
1282 }
1283
1284 if (dpp_conf_resp_rx(auth, resp) < 0) {
1285 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1286 goto fail;
1287 }
1288
1289 res = wpas_dpp_handle_config_obj(wpa_s, auth);
1290 if (res < 0)
1291 goto fail;
1292
1293 status = DPP_STATUS_OK;
1294 #ifdef CONFIG_TESTING_OPTIONS
1295 if (dpp_test == DPP_TEST_REJECT_CONFIG) {
1296 wpa_printf(MSG_INFO, "DPP: TESTING - Reject Config Object");
1297 status = DPP_STATUS_CONFIG_REJECTED;
1298 }
1299 #endif /* CONFIG_TESTING_OPTIONS */
1300 fail:
1301 if (status != DPP_STATUS_OK)
1302 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1303 #ifdef CONFIG_DPP2
1304 if (auth->peer_version >= 2 &&
1305 auth->conf_resp_status == DPP_STATUS_OK) {
1306 struct wpabuf *msg;
1307
1308 wpa_printf(MSG_DEBUG, "DPP: Send DPP Configuration Result");
1309 msg = dpp_build_conf_result(auth, status);
1310 if (!msg)
1311 goto fail2;
1312
1313 wpa_msg(wpa_s, MSG_INFO,
1314 DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1315 MAC2STR(addr), auth->curr_freq,
1316 DPP_PA_CONFIGURATION_RESULT);
1317 offchannel_send_action(wpa_s, auth->curr_freq,
1318 addr, wpa_s->own_addr, broadcast,
1319 wpabuf_head(msg),
1320 wpabuf_len(msg),
1321 500, wpas_dpp_tx_status, 0);
1322 wpabuf_free(msg);
1323
1324 /* This exchange will be terminated in the TX status handler */
1325 return;
1326 }
1327 fail2:
1328 #endif /* CONFIG_DPP2 */
1329 dpp_auth_deinit(wpa_s->dpp_auth);
1330 wpa_s->dpp_auth = NULL;
1331 }
1332
1333
1334 static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1335 {
1336 struct dpp_authentication *auth = wpa_s->dpp_auth;
1337 struct wpabuf *buf, *conf_req;
1338 char json[100];
1339 int res;
1340
1341 wpa_s->dpp_gas_client = 1;
1342 os_snprintf(json, sizeof(json),
1343 "{\"name\":\"Test\","
1344 "\"wi-fi_tech\":\"infra\","
1345 "\"netRole\":\"%s\"}",
1346 wpa_s->dpp_netrole_ap ? "ap" : "sta");
1347 #ifdef CONFIG_TESTING_OPTIONS
1348 if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
1349 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
1350 json[29] = 'k'; /* replace "infra" with "knfra" */
1351 }
1352 #endif /* CONFIG_TESTING_OPTIONS */
1353 wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
1354
1355 offchannel_send_action_done(wpa_s);
1356 wpas_dpp_listen_stop(wpa_s);
1357
1358 conf_req = dpp_build_conf_req(auth, json);
1359 if (!conf_req) {
1360 wpa_printf(MSG_DEBUG,
1361 "DPP: No configuration request data available");
1362 return;
1363 }
1364
1365 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
1366 if (!buf) {
1367 wpabuf_free(conf_req);
1368 return;
1369 }
1370
1371 /* Advertisement Protocol IE */
1372 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
1373 wpabuf_put_u8(buf, 8); /* Length */
1374 wpabuf_put_u8(buf, 0x7f);
1375 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
1376 wpabuf_put_u8(buf, 5);
1377 wpabuf_put_be24(buf, OUI_WFA);
1378 wpabuf_put_u8(buf, DPP_OUI_TYPE);
1379 wpabuf_put_u8(buf, 0x01);
1380
1381 /* GAS Query */
1382 wpabuf_put_le16(buf, wpabuf_len(conf_req));
1383 wpabuf_put_buf(buf, conf_req);
1384 wpabuf_free(conf_req);
1385
1386 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1387 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1388
1389 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
1390 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
1391 if (res < 0) {
1392 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1393 wpabuf_free(buf);
1394 } else {
1395 wpa_printf(MSG_DEBUG,
1396 "DPP: GAS query started with dialog token %u", res);
1397 wpa_s->dpp_gas_dialog_token = res;
1398 }
1399 }
1400
1401
1402 static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1403 {
1404 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1405 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
1406 #ifdef CONFIG_TESTING_OPTIONS
1407 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
1408 wpa_printf(MSG_INFO,
1409 "DPP: TESTING - stop at Authentication Confirm");
1410 if (wpa_s->dpp_auth->configurator) {
1411 /* Prevent GAS response */
1412 wpa_s->dpp_auth->auth_success = 0;
1413 }
1414 return;
1415 }
1416 #endif /* CONFIG_TESTING_OPTIONS */
1417
1418 if (wpa_s->dpp_auth->configurator)
1419 wpas_dpp_start_gas_server(wpa_s);
1420 else
1421 wpas_dpp_start_gas_client(wpa_s);
1422 }
1423
1424
1425 static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1426 const u8 *hdr, const u8 *buf, size_t len,
1427 unsigned int freq)
1428 {
1429 struct dpp_authentication *auth = wpa_s->dpp_auth;
1430 struct wpabuf *msg;
1431
1432 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
1433 " (freq %u MHz)", MAC2STR(src), freq);
1434
1435 if (!auth) {
1436 wpa_printf(MSG_DEBUG,
1437 "DPP: No DPP Authentication in progress - drop");
1438 return;
1439 }
1440
1441 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1442 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1443 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1444 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1445 return;
1446 }
1447
1448 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1449
1450 if (auth->curr_freq != freq && auth->neg_freq == freq) {
1451 wpa_printf(MSG_DEBUG,
1452 "DPP: Responder accepted request for different negotiation channel");
1453 auth->curr_freq = freq;
1454 }
1455
1456 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
1457 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
1458 if (!msg) {
1459 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1460 wpa_printf(MSG_DEBUG,
1461 "DPP: Start wait for full response");
1462 offchannel_send_action_done(wpa_s);
1463 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1464 return;
1465 }
1466 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1467 return;
1468 }
1469 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1470
1471 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1472 MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
1473 offchannel_send_action(wpa_s, auth->curr_freq,
1474 src, wpa_s->own_addr, broadcast,
1475 wpabuf_head(msg), wpabuf_len(msg),
1476 500, wpas_dpp_tx_status, 0);
1477 wpabuf_free(msg);
1478 wpa_s->dpp_auth_ok_on_ack = 1;
1479 }
1480
1481
1482 static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
1483 const u8 *hdr, const u8 *buf, size_t len)
1484 {
1485 struct dpp_authentication *auth = wpa_s->dpp_auth;
1486
1487 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1488 MAC2STR(src));
1489
1490 if (!auth) {
1491 wpa_printf(MSG_DEBUG,
1492 "DPP: No DPP Authentication in progress - drop");
1493 return;
1494 }
1495
1496 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1497 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1498 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1499 return;
1500 }
1501
1502 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
1503 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1504 return;
1505 }
1506
1507 wpas_dpp_auth_success(wpa_s, 0);
1508 }
1509
1510
1511 #ifdef CONFIG_DPP2
1512
1513 static void wpas_dpp_config_result_wait_timeout(void *eloop_ctx,
1514 void *timeout_ctx)
1515 {
1516 struct wpa_supplicant *wpa_s = eloop_ctx;
1517 struct dpp_authentication *auth = wpa_s->dpp_auth;
1518
1519 if (!auth || !auth->waiting_conf_result)
1520 return;
1521
1522 wpa_printf(MSG_DEBUG,
1523 "DPP: Timeout while waiting for Configuration Result");
1524 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1525 dpp_auth_deinit(auth);
1526 wpa_s->dpp_auth = NULL;
1527 }
1528
1529
1530 static void wpas_dpp_rx_conf_result(struct wpa_supplicant *wpa_s, const u8 *src,
1531 const u8 *hdr, const u8 *buf, size_t len)
1532 {
1533 struct dpp_authentication *auth = wpa_s->dpp_auth;
1534 enum dpp_status_error status;
1535
1536 wpa_printf(MSG_DEBUG, "DPP: Configuration Result from " MACSTR,
1537 MAC2STR(src));
1538
1539 if (!auth || !auth->waiting_conf_result) {
1540 wpa_printf(MSG_DEBUG,
1541 "DPP: No DPP Configuration waiting for result - drop");
1542 return;
1543 }
1544
1545 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1546 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1547 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1548 return;
1549 }
1550
1551 status = dpp_conf_result_rx(auth, hdr, buf, len);
1552
1553 offchannel_send_action_done(wpa_s);
1554 wpas_dpp_listen_stop(wpa_s);
1555 if (status == DPP_STATUS_OK)
1556 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
1557 else
1558 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1559 dpp_auth_deinit(auth);
1560 wpa_s->dpp_auth = NULL;
1561 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
1562 }
1563
1564 #endif /* CONFIG_DPP2 */
1565
1566
1567 static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1568 const u8 *src,
1569 const u8 *buf, size_t len)
1570 {
1571 struct wpa_ssid *ssid;
1572 const u8 *connector, *trans_id, *status;
1573 u16 connector_len, trans_id_len, status_len;
1574 struct dpp_introduction intro;
1575 struct rsn_pmksa_cache_entry *entry;
1576 struct os_time now;
1577 struct os_reltime rnow;
1578 os_time_t expiry;
1579 unsigned int seconds;
1580 enum dpp_status_error res;
1581
1582 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1583 MAC2STR(src));
1584 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1585 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1586 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1587 MACSTR " - drop", MAC2STR(src));
1588 return;
1589 }
1590 offchannel_send_action_done(wpa_s);
1591
1592 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1593 if (ssid == wpa_s->dpp_intro_network)
1594 break;
1595 }
1596 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1597 !ssid->dpp_csign) {
1598 wpa_printf(MSG_DEBUG,
1599 "DPP: Profile not found for network introduction");
1600 return;
1601 }
1602
1603 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1604 &trans_id_len);
1605 if (!trans_id || trans_id_len != 1) {
1606 wpa_printf(MSG_DEBUG,
1607 "DPP: Peer did not include Transaction ID");
1608 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1609 " fail=missing_transaction_id", MAC2STR(src));
1610 goto fail;
1611 }
1612 if (trans_id[0] != TRANSACTION_ID) {
1613 wpa_printf(MSG_DEBUG,
1614 "DPP: Ignore frame with unexpected Transaction ID %u",
1615 trans_id[0]);
1616 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1617 " fail=transaction_id_mismatch", MAC2STR(src));
1618 goto fail;
1619 }
1620
1621 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1622 if (!status || status_len != 1) {
1623 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1624 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1625 " fail=missing_status", MAC2STR(src));
1626 goto fail;
1627 }
1628 if (status[0] != DPP_STATUS_OK) {
1629 wpa_printf(MSG_DEBUG,
1630 "DPP: Peer rejected network introduction: Status %u",
1631 status[0]);
1632 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1633 " status=%u", MAC2STR(src), status[0]);
1634 goto fail;
1635 }
1636
1637 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1638 if (!connector) {
1639 wpa_printf(MSG_DEBUG,
1640 "DPP: Peer did not include its Connector");
1641 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1642 " fail=missing_connector", MAC2STR(src));
1643 goto fail;
1644 }
1645
1646 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1647 ssid->dpp_netaccesskey,
1648 ssid->dpp_netaccesskey_len,
1649 ssid->dpp_csign,
1650 ssid->dpp_csign_len,
1651 connector, connector_len, &expiry);
1652 if (res != DPP_STATUS_OK) {
1653 wpa_printf(MSG_INFO,
1654 "DPP: Network Introduction protocol resulted in failure");
1655 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1656 " fail=peer_connector_validation_failed", MAC2STR(src));
1657 goto fail;
1658 }
1659
1660 entry = os_zalloc(sizeof(*entry));
1661 if (!entry)
1662 goto fail;
1663 os_memcpy(entry->aa, src, ETH_ALEN);
1664 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1665 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1666 entry->pmk_len = intro.pmk_len;
1667 entry->akmp = WPA_KEY_MGMT_DPP;
1668 if (expiry) {
1669 os_get_time(&now);
1670 seconds = expiry - now.sec;
1671 } else {
1672 seconds = 86400 * 7;
1673 }
1674 os_get_reltime(&rnow);
1675 entry->expiration = rnow.sec + seconds;
1676 entry->reauth_time = rnow.sec + seconds;
1677 entry->network_ctx = ssid;
1678 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1679
1680 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1681 " status=%u", MAC2STR(src), status[0]);
1682
1683 wpa_printf(MSG_DEBUG,
1684 "DPP: Try connection again after successful network introduction");
1685 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1686 wpa_supplicant_cancel_sched_scan(wpa_s);
1687 wpa_supplicant_req_scan(wpa_s, 0, 0);
1688 }
1689 fail:
1690 os_memset(&intro, 0, sizeof(intro));
1691 }
1692
1693
1694 static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1695 {
1696 int i, j;
1697
1698 if (!wpa_s->hw.modes)
1699 return -1;
1700
1701 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1702 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1703
1704 for (j = 0; j < mode->num_channels; j++) {
1705 struct hostapd_channel_data *chan = &mode->channels[j];
1706
1707 if (chan->freq != (int) freq)
1708 continue;
1709
1710 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1711 HOSTAPD_CHAN_NO_IR |
1712 HOSTAPD_CHAN_RADAR))
1713 continue;
1714
1715 return 1;
1716 }
1717 }
1718
1719 wpa_printf(MSG_DEBUG,
1720 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1721 freq);
1722
1723 return 0;
1724 }
1725
1726
1727 static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1728 struct dpp_pkex *pkex)
1729 {
1730 if (pkex->freq == 2437)
1731 pkex->freq = 5745;
1732 else if (pkex->freq == 5745)
1733 pkex->freq = 5220;
1734 else if (pkex->freq == 5220)
1735 pkex->freq = 60480;
1736 else
1737 return -1; /* no more channels to try */
1738
1739 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1740 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1741 pkex->freq);
1742 return 0;
1743 }
1744
1745 /* Could not use this channel - try the next one */
1746 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1747 }
1748
1749
1750 static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1751 {
1752 struct wpa_supplicant *wpa_s = eloop_ctx;
1753 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1754
1755 if (!pkex || !pkex->exchange_req)
1756 return;
1757 if (pkex->exch_req_tries >= 5) {
1758 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1759 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1760 "No response from PKEX peer");
1761 dpp_pkex_free(pkex);
1762 wpa_s->dpp_pkex = NULL;
1763 return;
1764 }
1765 pkex->exch_req_tries = 0;
1766 }
1767
1768 pkex->exch_req_tries++;
1769 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1770 pkex->exch_req_tries);
1771 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1772 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1773 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1774 wpa_s->own_addr, broadcast,
1775 wpabuf_head(pkex->exchange_req),
1776 wpabuf_len(pkex->exchange_req),
1777 pkex->exch_req_wait_time,
1778 wpas_dpp_tx_pkex_status, 0);
1779 }
1780
1781
1782 static void
1783 wpas_dpp_tx_pkex_status(struct wpa_supplicant *wpa_s,
1784 unsigned int freq, const u8 *dst,
1785 const u8 *src, const u8 *bssid,
1786 const u8 *data, size_t data_len,
1787 enum offchannel_send_action_result result)
1788 {
1789 const char *res_txt;
1790 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1791
1792 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1793 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1794 "FAILED");
1795 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1796 " result=%s (PKEX)",
1797 freq, MAC2STR(dst), res_txt);
1798 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1799 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
1800
1801 if (!pkex) {
1802 wpa_printf(MSG_DEBUG,
1803 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
1804 return;
1805 }
1806
1807 if (pkex->failed) {
1808 wpa_printf(MSG_DEBUG,
1809 "DPP: Terminate PKEX exchange due to an earlier error");
1810 if (pkex->t > pkex->own_bi->pkex_t)
1811 pkex->own_bi->pkex_t = pkex->t;
1812 dpp_pkex_free(pkex);
1813 wpa_s->dpp_pkex = NULL;
1814 return;
1815 }
1816
1817 if (pkex->exch_req_wait_time && pkex->exchange_req) {
1818 /* Wait for PKEX Exchange Response frame and retry request if
1819 * no response is seen. */
1820 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1821 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
1822 (pkex->exch_req_wait_time % 1000) * 1000,
1823 wpas_dpp_pkex_retry_timeout, wpa_s,
1824 NULL);
1825 }
1826 }
1827
1828
1829 static void
1830 wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
1831 const u8 *buf, size_t len, unsigned int freq)
1832 {
1833 struct wpabuf *msg;
1834 unsigned int wait_time;
1835
1836 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
1837 MAC2STR(src));
1838
1839 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1840 * values here */
1841
1842 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
1843 wpa_printf(MSG_DEBUG,
1844 "DPP: No PKEX code configured - ignore request");
1845 return;
1846 }
1847
1848 if (wpa_s->dpp_pkex) {
1849 /* TODO: Support parallel operations */
1850 wpa_printf(MSG_DEBUG,
1851 "DPP: Already in PKEX session - ignore new request");
1852 return;
1853 }
1854
1855 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
1856 wpa_s->own_addr, src,
1857 wpa_s->dpp_pkex_identifier,
1858 wpa_s->dpp_pkex_code,
1859 buf, len);
1860 if (!wpa_s->dpp_pkex) {
1861 wpa_printf(MSG_DEBUG,
1862 "DPP: Failed to process the request - ignore it");
1863 return;
1864 }
1865
1866 msg = wpa_s->dpp_pkex->exchange_resp;
1867 wait_time = wpa_s->max_remain_on_chan;
1868 if (wait_time > 2000)
1869 wait_time = 2000;
1870 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1871 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
1872 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1873 broadcast,
1874 wpabuf_head(msg), wpabuf_len(msg),
1875 wait_time, wpas_dpp_tx_pkex_status, 0);
1876 }
1877
1878
1879 static void
1880 wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1881 const u8 *buf, size_t len, unsigned int freq)
1882 {
1883 struct wpabuf *msg;
1884 unsigned int wait_time;
1885
1886 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
1887 MAC2STR(src));
1888
1889 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1890 * values here */
1891
1892 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
1893 wpa_s->dpp_pkex->exchange_done) {
1894 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
1895 return;
1896 }
1897
1898 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1899 wpa_s->dpp_pkex->exch_req_wait_time = 0;
1900
1901 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
1902 if (!msg) {
1903 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
1904 return;
1905 }
1906
1907 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
1908 MAC2STR(src));
1909
1910 wait_time = wpa_s->max_remain_on_chan;
1911 if (wait_time > 2000)
1912 wait_time = 2000;
1913 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1914 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
1915 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1916 broadcast,
1917 wpabuf_head(msg), wpabuf_len(msg),
1918 wait_time, wpas_dpp_tx_pkex_status, 0);
1919 wpabuf_free(msg);
1920 }
1921
1922
1923 static struct dpp_bootstrap_info *
1924 wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
1925 unsigned int freq)
1926 {
1927 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1928 struct dpp_bootstrap_info *bi;
1929
1930 bi = os_zalloc(sizeof(*bi));
1931 if (!bi)
1932 return NULL;
1933 bi->id = wpas_dpp_next_id(wpa_s);
1934 bi->type = DPP_BOOTSTRAP_PKEX;
1935 os_memcpy(bi->mac_addr, peer, ETH_ALEN);
1936 bi->num_freq = 1;
1937 bi->freq[0] = freq;
1938 bi->curve = pkex->own_bi->curve;
1939 bi->pubkey = pkex->peer_bootstrap_key;
1940 pkex->peer_bootstrap_key = NULL;
1941 dpp_pkex_free(pkex);
1942 wpa_s->dpp_pkex = NULL;
1943 if (dpp_bootstrap_key_hash(bi) < 0) {
1944 dpp_bootstrap_info_free(bi);
1945 return NULL;
1946 }
1947 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
1948 return bi;
1949 }
1950
1951
1952 static void
1953 wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
1954 const u8 *hdr, const u8 *buf, size_t len,
1955 unsigned int freq)
1956 {
1957 struct wpabuf *msg;
1958 unsigned int wait_time;
1959 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1960
1961 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
1962 MAC2STR(src));
1963
1964 if (!pkex || pkex->initiator || !pkex->exchange_done) {
1965 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
1966 return;
1967 }
1968
1969 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
1970 if (!msg) {
1971 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
1972 if (pkex->failed) {
1973 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
1974 if (pkex->t > pkex->own_bi->pkex_t)
1975 pkex->own_bi->pkex_t = pkex->t;
1976 dpp_pkex_free(wpa_s->dpp_pkex);
1977 wpa_s->dpp_pkex = NULL;
1978 }
1979 return;
1980 }
1981
1982 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
1983 MACSTR, MAC2STR(src));
1984
1985 wait_time = wpa_s->max_remain_on_chan;
1986 if (wait_time > 2000)
1987 wait_time = 2000;
1988 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1989 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
1990 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1991 broadcast,
1992 wpabuf_head(msg), wpabuf_len(msg),
1993 wait_time, wpas_dpp_tx_pkex_status, 0);
1994 wpabuf_free(msg);
1995
1996 wpas_dpp_pkex_finish(wpa_s, src, freq);
1997 }
1998
1999
2000 static void
2001 wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
2002 const u8 *hdr, const u8 *buf, size_t len,
2003 unsigned int freq)
2004 {
2005 int res;
2006 struct dpp_bootstrap_info *bi;
2007 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2008 char cmd[500];
2009
2010 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2011 MAC2STR(src));
2012
2013 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2014 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2015 return;
2016 }
2017
2018 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
2019 if (res < 0) {
2020 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2021 return;
2022 }
2023
2024 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
2025 if (!bi)
2026 return;
2027
2028 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2029 bi->id,
2030 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2031 wpa_printf(MSG_DEBUG,
2032 "DPP: Start authentication after PKEX with parameters: %s",
2033 cmd);
2034 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2035 wpa_printf(MSG_DEBUG,
2036 "DPP: Authentication initialization failed");
2037 return;
2038 }
2039 }
2040
2041
2042 void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2043 const u8 *buf, size_t len, unsigned int freq)
2044 {
2045 u8 crypto_suite;
2046 enum dpp_public_action_frame_type type;
2047 const u8 *hdr;
2048 unsigned int pkex_t;
2049
2050 if (len < DPP_HDR_LEN)
2051 return;
2052 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
2053 return;
2054 hdr = buf;
2055 buf += 4;
2056 len -= 4;
2057 crypto_suite = *buf++;
2058 type = *buf++;
2059 len -= 2;
2060
2061 wpa_printf(MSG_DEBUG,
2062 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
2063 MACSTR " freq=%u",
2064 crypto_suite, type, MAC2STR(src), freq);
2065 if (crypto_suite != 1) {
2066 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2067 crypto_suite);
2068 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2069 " freq=%u type=%d ignore=unsupported-crypto-suite",
2070 MAC2STR(src), freq, type);
2071 return;
2072 }
2073 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
2074 if (dpp_check_attrs(buf, len) < 0) {
2075 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2076 " freq=%u type=%d ignore=invalid-attributes",
2077 MAC2STR(src), freq, type);
2078 return;
2079 }
2080 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2081 MAC2STR(src), freq, type);
2082
2083 switch (type) {
2084 case DPP_PA_AUTHENTICATION_REQ:
2085 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
2086 break;
2087 case DPP_PA_AUTHENTICATION_RESP:
2088 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
2089 break;
2090 case DPP_PA_AUTHENTICATION_CONF:
2091 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
2092 break;
2093 case DPP_PA_PEER_DISCOVERY_RESP:
2094 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2095 break;
2096 case DPP_PA_PKEX_EXCHANGE_REQ:
2097 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2098 break;
2099 case DPP_PA_PKEX_EXCHANGE_RESP:
2100 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2101 break;
2102 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
2103 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2104 freq);
2105 break;
2106 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
2107 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2108 freq);
2109 break;
2110 #ifdef CONFIG_DPP2
2111 case DPP_PA_CONFIGURATION_RESULT:
2112 wpas_dpp_rx_conf_result(wpa_s, src, hdr, buf, len);
2113 break;
2114 #endif /* CONFIG_DPP2 */
2115 default:
2116 wpa_printf(MSG_DEBUG,
2117 "DPP: Ignored unsupported frame subtype %d", type);
2118 break;
2119 }
2120
2121 if (wpa_s->dpp_pkex)
2122 pkex_t = wpa_s->dpp_pkex->t;
2123 else if (wpa_s->dpp_pkex_bi)
2124 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2125 else
2126 pkex_t = 0;
2127 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2128 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2129 wpas_dpp_pkex_remove(wpa_s, "*");
2130 }
2131 }
2132
2133
2134 static struct wpabuf *
2135 wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2136 size_t query_len)
2137 {
2138 struct wpa_supplicant *wpa_s = ctx;
2139 struct dpp_authentication *auth = wpa_s->dpp_auth;
2140 struct wpabuf *resp;
2141
2142 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2143 MAC2STR(sa));
2144 if (!auth || !auth->auth_success ||
2145 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2146 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2147 return NULL;
2148 }
2149 wpa_hexdump(MSG_DEBUG,
2150 "DPP: Received Configuration Request (GAS Query Request)",
2151 query, query_len);
2152 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2153 MAC2STR(sa));
2154 resp = dpp_conf_req_rx(auth, query, query_len);
2155 if (!resp)
2156 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2157 auth->conf_resp = resp;
2158 return resp;
2159 }
2160
2161
2162 static void
2163 wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2164 {
2165 struct wpa_supplicant *wpa_s = ctx;
2166 struct dpp_authentication *auth = wpa_s->dpp_auth;
2167
2168 if (!auth) {
2169 wpabuf_free(resp);
2170 return;
2171 }
2172 if (auth->conf_resp != resp) {
2173 wpa_printf(MSG_DEBUG,
2174 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2175 ok);
2176 wpabuf_free(resp);
2177 return;
2178 }
2179
2180 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2181 ok);
2182 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2183 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2184 #ifdef CONFIG_DPP2
2185 if (ok && auth->peer_version >= 2 &&
2186 auth->conf_resp_status == DPP_STATUS_OK) {
2187 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
2188 auth->waiting_conf_result = 1;
2189 auth->conf_resp = NULL;
2190 wpabuf_free(resp);
2191 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout,
2192 wpa_s, NULL);
2193 eloop_register_timeout(2, 0,
2194 wpas_dpp_config_result_wait_timeout,
2195 wpa_s, NULL);
2196 return;
2197 }
2198 #endif /* CONFIG_DPP2 */
2199 offchannel_send_action_done(wpa_s);
2200 wpas_dpp_listen_stop(wpa_s);
2201 if (ok)
2202 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2203 else
2204 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2205 dpp_auth_deinit(wpa_s->dpp_auth);
2206 wpa_s->dpp_auth = NULL;
2207 wpabuf_free(resp);
2208 }
2209
2210
2211 static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
2212 {
2213 struct dpp_configurator *conf;
2214 unsigned int max_id = 0;
2215
2216 dl_list_for_each(conf, &wpa_s->dpp_configurator,
2217 struct dpp_configurator, list) {
2218 if (conf->id > max_id)
2219 max_id = conf->id;
2220 }
2221 return max_id + 1;
2222 }
2223
2224
2225 int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
2226 {
2227 char *curve = NULL;
2228 char *key = NULL;
2229 u8 *privkey = NULL;
2230 size_t privkey_len = 0;
2231 int ret = -1;
2232 struct dpp_configurator *conf = NULL;
2233
2234 curve = get_param(cmd, " curve=");
2235 key = get_param(cmd, " key=");
2236
2237 if (key) {
2238 privkey_len = os_strlen(key) / 2;
2239 privkey = os_malloc(privkey_len);
2240 if (!privkey ||
2241 hexstr2bin(key, privkey, privkey_len) < 0)
2242 goto fail;
2243 }
2244
2245 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
2246 if (!conf)
2247 goto fail;
2248
2249 conf->id = wpas_dpp_next_configurator_id(wpa_s);
2250 dl_list_add(&wpa_s->dpp_configurator, &conf->list);
2251 ret = conf->id;
2252 conf = NULL;
2253 fail:
2254 os_free(curve);
2255 str_clear_free(key);
2256 bin_clear_free(privkey, privkey_len);
2257 dpp_configurator_free(conf);
2258 return ret;
2259 }
2260
2261
2262 static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
2263 {
2264 struct dpp_configurator *conf, *tmp;
2265 int found = 0;
2266
2267 dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
2268 struct dpp_configurator, list) {
2269 if (id && conf->id != id)
2270 continue;
2271 found = 1;
2272 dl_list_del(&conf->list);
2273 dpp_configurator_free(conf);
2274 }
2275
2276 if (id == 0)
2277 return 0; /* flush succeeds regardless of entries found */
2278 return found ? 0 : -1;
2279 }
2280
2281
2282 int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
2283 {
2284 unsigned int id_val;
2285
2286 if (os_strcmp(id, "*") == 0) {
2287 id_val = 0;
2288 } else {
2289 id_val = atoi(id);
2290 if (id_val == 0)
2291 return -1;
2292 }
2293
2294 return dpp_configurator_del(wpa_s, id_val);
2295 }
2296
2297
2298 int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2299 {
2300 struct dpp_authentication *auth;
2301 int ret = -1;
2302 char *curve = NULL;
2303
2304 auth = os_zalloc(sizeof(*auth));
2305 if (!auth)
2306 return -1;
2307
2308 curve = get_param(cmd, " curve=");
2309 wpas_dpp_set_testing_options(wpa_s, auth);
2310 if (wpas_dpp_set_configurator(wpa_s, auth, cmd) == 0 &&
2311 dpp_configurator_own_config(auth, curve, 0) == 0)
2312 ret = wpas_dpp_handle_config_obj(wpa_s, auth);
2313
2314 dpp_auth_deinit(auth);
2315 os_free(curve);
2316
2317 return ret;
2318 }
2319
2320
2321 int wpas_dpp_configurator_get_key(struct wpa_supplicant *wpa_s, unsigned int id,
2322 char *buf, size_t buflen)
2323 {
2324 struct dpp_configurator *conf;
2325
2326 conf = dpp_configurator_get_id(wpa_s, id);
2327 if (!conf)
2328 return -1;
2329
2330 return dpp_configurator_get_key(conf, buf, buflen);
2331 }
2332
2333
2334 static void
2335 wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2336 unsigned int freq, const u8 *dst,
2337 const u8 *src, const u8 *bssid,
2338 const u8 *data, size_t data_len,
2339 enum offchannel_send_action_result result)
2340 {
2341 const char *res_txt;
2342
2343 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2344 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2345 "FAILED");
2346 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2347 " result=%s (DPP Peer Discovery Request)",
2348 freq, MAC2STR(dst), res_txt);
2349 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2350 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
2351 /* TODO: Time out wait for response more quickly in error cases? */
2352 }
2353
2354
2355 int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2356 struct wpa_bss *bss)
2357 {
2358 struct os_time now;
2359 struct wpabuf *msg;
2360 unsigned int wait_time;
2361 const u8 *rsn;
2362 struct wpa_ie_data ied;
2363
2364 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2365 return 0; /* Not using DPP AKM - continue */
2366 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2367 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ied) == 0 &&
2368 !(ied.key_mgmt & WPA_KEY_MGMT_DPP))
2369 return 0; /* AP does not support DPP AKM - continue */
2370 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2371 return 0; /* PMKSA exists for DPP AKM - continue */
2372
2373 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2374 !ssid->dpp_csign) {
2375 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2376 "missing %s",
2377 !ssid->dpp_connector ? "Connector" :
2378 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2379 "C-sign-key"));
2380 return -1;
2381 }
2382
2383 os_get_time(&now);
2384
2385 if (ssid->dpp_netaccesskey_expiry &&
2386 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
2387 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2388 "netAccessKey expired");
2389 return -1;
2390 }
2391
2392 wpa_printf(MSG_DEBUG,
2393 "DPP: Starting network introduction protocol to derive PMKSA for "
2394 MACSTR, MAC2STR(bss->bssid));
2395
2396 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
2397 5 + 4 + os_strlen(ssid->dpp_connector));
2398 if (!msg)
2399 return -1;
2400
2401 #ifdef CONFIG_TESTING_OPTIONS
2402 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2403 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2404 goto skip_trans_id;
2405 }
2406 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2407 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2408 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2409 wpabuf_put_le16(msg, 0);
2410 goto skip_trans_id;
2411 }
2412 #endif /* CONFIG_TESTING_OPTIONS */
2413
2414 /* Transaction ID */
2415 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2416 wpabuf_put_le16(msg, 1);
2417 wpabuf_put_u8(msg, TRANSACTION_ID);
2418
2419 #ifdef CONFIG_TESTING_OPTIONS
2420 skip_trans_id:
2421 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2422 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2423 goto skip_connector;
2424 }
2425 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2426 char *connector;
2427
2428 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2429 connector = dpp_corrupt_connector_signature(
2430 ssid->dpp_connector);
2431 if (!connector) {
2432 wpabuf_free(msg);
2433 return -1;
2434 }
2435 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2436 wpabuf_put_le16(msg, os_strlen(connector));
2437 wpabuf_put_str(msg, connector);
2438 os_free(connector);
2439 goto skip_connector;
2440 }
2441 #endif /* CONFIG_TESTING_OPTIONS */
2442
2443 /* DPP Connector */
2444 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2445 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2446 wpabuf_put_str(msg, ssid->dpp_connector);
2447
2448 #ifdef CONFIG_TESTING_OPTIONS
2449 skip_connector:
2450 #endif /* CONFIG_TESTING_OPTIONS */
2451
2452 /* TODO: Timeout on AP response */
2453 wait_time = wpa_s->max_remain_on_chan;
2454 if (wait_time > 2000)
2455 wait_time = 2000;
2456 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2457 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
2458 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2459 broadcast,
2460 wpabuf_head(msg), wpabuf_len(msg),
2461 wait_time, wpas_dpp_tx_introduction_status, 0);
2462 wpabuf_free(msg);
2463
2464 /* Request this connection attempt to terminate - new one will be
2465 * started when network introduction protocol completes */
2466 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2467 wpa_s->dpp_intro_network = ssid;
2468 return 1;
2469 }
2470
2471
2472 int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2473 {
2474 struct dpp_bootstrap_info *own_bi;
2475 const char *pos, *end;
2476 unsigned int wait_time;
2477
2478 pos = os_strstr(cmd, " own=");
2479 if (!pos)
2480 return -1;
2481 pos += 5;
2482 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
2483 if (!own_bi) {
2484 wpa_printf(MSG_DEBUG,
2485 "DPP: Identified bootstrap info not found");
2486 return -1;
2487 }
2488 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2489 wpa_printf(MSG_DEBUG,
2490 "DPP: Identified bootstrap info not for PKEX");
2491 return -1;
2492 }
2493 wpa_s->dpp_pkex_bi = own_bi;
2494 own_bi->pkex_t = 0; /* clear pending errors on new code */
2495
2496 os_free(wpa_s->dpp_pkex_identifier);
2497 wpa_s->dpp_pkex_identifier = NULL;
2498 pos = os_strstr(cmd, " identifier=");
2499 if (pos) {
2500 pos += 12;
2501 end = os_strchr(pos, ' ');
2502 if (!end)
2503 return -1;
2504 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2505 if (!wpa_s->dpp_pkex_identifier)
2506 return -1;
2507 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2508 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2509 }
2510
2511 pos = os_strstr(cmd, " code=");
2512 if (!pos)
2513 return -1;
2514 os_free(wpa_s->dpp_pkex_code);
2515 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2516 if (!wpa_s->dpp_pkex_code)
2517 return -1;
2518
2519 if (os_strstr(cmd, " init=1")) {
2520 struct dpp_pkex *pkex;
2521 struct wpabuf *msg;
2522
2523 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2524 dpp_pkex_free(wpa_s->dpp_pkex);
2525 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
2526 wpa_s->dpp_pkex_identifier,
2527 wpa_s->dpp_pkex_code);
2528 pkex = wpa_s->dpp_pkex;
2529 if (!pkex)
2530 return -1;
2531
2532 msg = pkex->exchange_req;
2533 wait_time = wpa_s->max_remain_on_chan;
2534 if (wait_time > 2000)
2535 wait_time = 2000;
2536 pkex->freq = 2437;
2537 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2538 " freq=%u type=%d",
2539 MAC2STR(broadcast), pkex->freq,
2540 DPP_PA_PKEX_EXCHANGE_REQ);
2541 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2542 wpa_s->own_addr, broadcast,
2543 wpabuf_head(msg), wpabuf_len(msg),
2544 wait_time, wpas_dpp_tx_pkex_status, 0);
2545 if (wait_time == 0)
2546 wait_time = 2000;
2547 pkex->exch_req_wait_time = wait_time;
2548 pkex->exch_req_tries = 1;
2549 }
2550
2551 /* TODO: Support multiple PKEX info entries */
2552
2553 os_free(wpa_s->dpp_pkex_auth_cmd);
2554 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2555
2556 return 1;
2557 }
2558
2559
2560 int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2561 {
2562 unsigned int id_val;
2563
2564 if (os_strcmp(id, "*") == 0) {
2565 id_val = 0;
2566 } else {
2567 id_val = atoi(id);
2568 if (id_val == 0)
2569 return -1;
2570 }
2571
2572 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2573 return -1;
2574
2575 /* TODO: Support multiple PKEX entries */
2576 os_free(wpa_s->dpp_pkex_code);
2577 wpa_s->dpp_pkex_code = NULL;
2578 os_free(wpa_s->dpp_pkex_identifier);
2579 wpa_s->dpp_pkex_identifier = NULL;
2580 os_free(wpa_s->dpp_pkex_auth_cmd);
2581 wpa_s->dpp_pkex_auth_cmd = NULL;
2582 wpa_s->dpp_pkex_bi = NULL;
2583 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2584 dpp_pkex_free(wpa_s->dpp_pkex);
2585 wpa_s->dpp_pkex = NULL;
2586 return 0;
2587 }
2588
2589
2590 void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2591 {
2592 dpp_auth_deinit(wpa_s->dpp_auth);
2593 wpa_s->dpp_auth = NULL;
2594 dpp_pkex_free(wpa_s->dpp_pkex);
2595 wpa_s->dpp_pkex = NULL;
2596 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2597 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
2598 }
2599
2600
2601 int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2602 {
2603 u8 adv_proto_id[7];
2604
2605 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2606 adv_proto_id[1] = 5;
2607 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2608 adv_proto_id[5] = DPP_OUI_TYPE;
2609 adv_proto_id[6] = 0x01;
2610
2611 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2612 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2613 wpas_dpp_gas_status_handler, wpa_s) < 0)
2614 return -1;
2615 dl_list_init(&wpa_s->dpp_bootstrap);
2616 dl_list_init(&wpa_s->dpp_configurator);
2617 wpa_s->dpp_init_done = 1;
2618 return 0;
2619 }
2620
2621
2622 void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2623 {
2624 #ifdef CONFIG_TESTING_OPTIONS
2625 os_free(wpa_s->dpp_config_obj_override);
2626 wpa_s->dpp_config_obj_override = NULL;
2627 os_free(wpa_s->dpp_discovery_override);
2628 wpa_s->dpp_discovery_override = NULL;
2629 os_free(wpa_s->dpp_groups_override);
2630 wpa_s->dpp_groups_override = NULL;
2631 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2632 #endif /* CONFIG_TESTING_OPTIONS */
2633 if (!wpa_s->dpp_init_done)
2634 return;
2635 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
2636 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
2637 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
2638 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
2639 #ifdef CONFIG_DPP2
2640 eloop_cancel_timeout(wpas_dpp_config_result_wait_timeout, wpa_s, NULL);
2641 dpp_pfs_free(wpa_s->dpp_pfs);
2642 wpa_s->dpp_pfs = NULL;
2643 #endif /* CONFIG_DPP2 */
2644 offchannel_send_action_done(wpa_s);
2645 wpas_dpp_listen_stop(wpa_s);
2646 dpp_bootstrap_del(wpa_s, 0);
2647 dpp_configurator_del(wpa_s, 0);
2648 wpas_dpp_stop(wpa_s);
2649 wpas_dpp_pkex_remove(wpa_s, "*");
2650 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
2651 os_free(wpa_s->dpp_configurator_params);
2652 wpa_s->dpp_configurator_params = NULL;
2653 }