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