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