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