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