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