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