]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/dpp_supplicant.c
tests: DPP protocol testing - Auth Conf RX processing failure
[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
b65b22d6
JM
530static void 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
JM
545 if (!cmd)
546 return;
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);
610 } else {
611 conf_sta->passphrase = os_strdup(pass);
612 if (!conf_sta->passphrase)
613 goto fail;
614 }
461d39af 615 } else if (os_strstr(cmd, " conf=sta-dpp")) {
e3a5882b 616 conf_sta->akm = DPP_AKM_DPP;
461d39af
JM
617 } else {
618 goto fail;
619 }
20f612d9
PK
620 if (os_strstr(cmd, " group_id=")) {
621 conf_sta->group_id = group_id;
622 group_id = NULL;
623 }
461d39af
JM
624 }
625
626 if (os_strstr(cmd, " conf=ap-")) {
627 conf_ap = os_zalloc(sizeof(struct dpp_configuration));
628 if (!conf_ap)
629 goto fail;
68cb6dce
JM
630 os_memcpy(conf_ap->ssid, ssid, ssid_len);
631 conf_ap->ssid_len = ssid_len;
e3a5882b
JM
632 if (os_strstr(cmd, " conf=ap-psk") ||
633 os_strstr(cmd, " conf=ap-sae") ||
634 os_strstr(cmd, " conf=ap-psk-sae")) {
635 if (os_strstr(cmd, " conf=ap-psk-sae"))
636 conf_ap->akm = DPP_AKM_PSK_SAE;
637 else if (os_strstr(cmd, " conf=ap-sae"))
638 conf_ap->akm = DPP_AKM_SAE;
639 else
640 conf_ap->akm = DPP_AKM_PSK;
5030d7d9
JM
641 if (psk_set) {
642 os_memcpy(conf_ap->psk, psk, PMK_LEN);
643 } else {
644 conf_ap->passphrase = os_strdup(pass);
645 if (!conf_ap->passphrase)
646 goto fail;
647 }
461d39af 648 } else if (os_strstr(cmd, " conf=ap-dpp")) {
e3a5882b 649 conf_ap->akm = DPP_AKM_DPP;
461d39af
JM
650 } else {
651 goto fail;
652 }
20f612d9
PK
653 if (os_strstr(cmd, " group_id=")) {
654 conf_ap->group_id = group_id;
655 group_id = NULL;
656 }
461d39af
JM
657 }
658
659 pos = os_strstr(cmd, " expiry=");
660 if (pos) {
661 long int val;
662
663 pos += 8;
664 val = strtol(pos, NULL, 0);
665 if (val <= 0)
666 goto fail;
667 if (conf_sta)
668 conf_sta->netaccesskey_expiry = val;
669 if (conf_ap)
670 conf_ap->netaccesskey_expiry = val;
671 }
672
673 pos = os_strstr(cmd, " configurator=");
674 if (pos) {
675 pos += 14;
676 conf = dpp_configurator_get_id(wpa_s, atoi(pos));
677 if (!conf) {
678 wpa_printf(MSG_INFO,
679 "DPP: Could not find the specified configurator");
680 goto fail;
681 }
30d27b04 682 }
b65b22d6
JM
683 auth->conf_sta = conf_sta;
684 auth->conf_ap = conf_ap;
685 auth->conf = conf;
20f612d9 686 os_free(group_id);
b65b22d6
JM
687 return;
688
689fail:
690 wpa_printf(MSG_DEBUG, "DPP: Failed to set configurator parameters");
691 dpp_configuration_free(conf_sta);
692 dpp_configuration_free(conf_ap);
20f612d9 693 os_free(group_id);
b65b22d6
JM
694}
695
696
f97ace34
JM
697static void wpas_dpp_init_timeout(void *eloop_ctx, void *timeout_ctx)
698{
699 struct wpa_supplicant *wpa_s = eloop_ctx;
700
701 if (!wpa_s->dpp_auth)
702 return;
703 wpa_printf(MSG_DEBUG, "DPP: Retry initiation after timeout");
704 wpas_dpp_auth_init_next(wpa_s);
705}
706
707
708static int wpas_dpp_auth_init_next(struct wpa_supplicant *wpa_s)
709{
710 struct dpp_authentication *auth = wpa_s->dpp_auth;
711 const u8 *dst;
0db637ca 712 unsigned int wait_time, max_wait_time, freq, max_tries, used;
921f5acd 713 struct os_reltime now, diff;
f97ace34 714
0db637ca 715 wpa_s->dpp_in_response_listen = 0;
f97ace34
JM
716 if (!auth)
717 return -1;
0db637ca
JM
718
719 if (auth->freq_idx == 0)
720 os_get_reltime(&wpa_s->dpp_init_iter_start);
721
f97ace34
JM
722 if (auth->freq_idx >= auth->num_freq) {
723 auth->num_freq_iters++;
724 if (wpa_s->dpp_init_max_tries)
725 max_tries = wpa_s->dpp_init_max_tries;
726 else
727 max_tries = 5;
248264c6 728 if (auth->num_freq_iters >= max_tries || auth->auth_req_ack) {
f97ace34
JM
729 wpa_printf(MSG_INFO,
730 "DPP: No response received from responder - stopping initiation attempt");
731 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_INIT_FAILED);
732 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout,
733 wpa_s, NULL);
734 offchannel_send_action_done(wpa_s);
735 dpp_auth_deinit(wpa_s->dpp_auth);
736 wpa_s->dpp_auth = NULL;
737 return -1;
738 }
739 auth->freq_idx = 0;
740 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
741 if (wpa_s->dpp_init_retry_time)
742 wait_time = wpa_s->dpp_init_retry_time;
743 else
744 wait_time = 10000;
921f5acd 745 os_get_reltime(&now);
0db637ca 746 os_reltime_sub(&now, &wpa_s->dpp_init_iter_start, &diff);
921f5acd
JM
747 used = diff.sec * 1000 + diff.usec / 1000;
748 if (used > wait_time)
749 wait_time = 0;
750 else
751 wait_time -= used;
752 wpa_printf(MSG_DEBUG, "DPP: Next init attempt in %u ms",
753 wait_time);
f97ace34
JM
754 eloop_register_timeout(wait_time / 1000,
755 (wait_time % 1000) * 1000,
756 wpas_dpp_init_timeout, wpa_s,
757 NULL);
758 return 0;
759 }
760 freq = auth->freq[auth->freq_idx++];
761 auth->curr_freq = freq;
762
763 if (is_zero_ether_addr(auth->peer_bi->mac_addr))
764 dst = broadcast;
765 else
766 dst = auth->peer_bi->mac_addr;
767 wpa_s->dpp_auth_ok_on_ack = 0;
768 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
769 wait_time = wpa_s->max_remain_on_chan;
0db637ca
JM
770 max_wait_time = wpa_s->dpp_resp_wait_time ?
771 wpa_s->dpp_resp_wait_time : 2000;
772 if (wait_time > max_wait_time)
773 wait_time = max_wait_time;
774 wait_time += 10; /* give the driver some extra time to complete */
f97ace34
JM
775 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
776 wpas_dpp_reply_wait_timeout,
777 wpa_s, NULL);
0db637ca 778 wait_time -= 10;
f97ace34
JM
779 if (auth->neg_freq > 0 && freq != auth->neg_freq) {
780 wpa_printf(MSG_DEBUG,
781 "DPP: Initiate on %u MHz and move to neg_freq %u MHz for response",
782 freq, auth->neg_freq);
783 }
784 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
785 MAC2STR(dst), freq, DPP_PA_AUTHENTICATION_REQ);
248264c6 786 auth->auth_req_ack = 0;
f97ace34
JM
787 os_get_reltime(&wpa_s->dpp_last_init);
788 return offchannel_send_action(wpa_s, freq, dst,
789 wpa_s->own_addr, broadcast,
790 wpabuf_head(auth->req_msg),
791 wpabuf_len(auth->req_msg),
792 wait_time, wpas_dpp_tx_status, 0);
793}
794
795
b65b22d6
JM
796int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
797{
798 const char *pos;
799 struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
d1f08264 800 u8 allowed_roles = DPP_CAPAB_CONFIGURATOR;
d2709206 801 unsigned int neg_freq = 0;
b65b22d6
JM
802
803 wpa_s->dpp_gas_client = 0;
804
805 pos = os_strstr(cmd, " peer=");
806 if (!pos)
807 return -1;
808 pos += 6;
809 peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
810 if (!peer_bi) {
811 wpa_printf(MSG_INFO,
812 "DPP: Could not find bootstrapping info for the identified peer");
813 return -1;
814 }
815
816 pos = os_strstr(cmd, " own=");
817 if (pos) {
818 pos += 5;
819 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
820 if (!own_bi) {
821 wpa_printf(MSG_INFO,
822 "DPP: Could not find bootstrapping info for the identified local entry");
823 return -1;
824 }
825
826 if (peer_bi->curve != own_bi->curve) {
827 wpa_printf(MSG_INFO,
828 "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
829 peer_bi->curve->name, own_bi->curve->name);
830 return -1;
831 }
832 }
833
834 pos = os_strstr(cmd, " role=");
835 if (pos) {
836 pos += 6;
837 if (os_strncmp(pos, "configurator", 12) == 0)
d1f08264 838 allowed_roles = DPP_CAPAB_CONFIGURATOR;
b65b22d6 839 else if (os_strncmp(pos, "enrollee", 8) == 0)
d1f08264
JM
840 allowed_roles = DPP_CAPAB_ENROLLEE;
841 else if (os_strncmp(pos, "either", 6) == 0)
842 allowed_roles = DPP_CAPAB_CONFIGURATOR |
843 DPP_CAPAB_ENROLLEE;
b65b22d6
JM
844 else
845 goto fail;
846 }
847
848 pos = os_strstr(cmd, " netrole=");
849 if (pos) {
850 pos += 9;
851 wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
852 }
30d27b04 853
d2709206
JM
854 pos = os_strstr(cmd, " neg_freq=");
855 if (pos)
856 neg_freq = atoi(pos + 10);
857
30d27b04 858 if (wpa_s->dpp_auth) {
f97ace34 859 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
30d27b04 860 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a
JM
861 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s,
862 NULL);
30d27b04
JM
863 offchannel_send_action_done(wpa_s);
864 dpp_auth_deinit(wpa_s->dpp_auth);
865 }
d1f08264 866 wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, allowed_roles,
f97ace34
JM
867 neg_freq,
868 wpa_s->hw.modes, wpa_s->hw.num_modes);
30d27b04 869 if (!wpa_s->dpp_auth)
461d39af
JM
870 goto fail;
871 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
b65b22d6 872 wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth, cmd);
30d27b04 873
d2709206 874 wpa_s->dpp_auth->neg_freq = neg_freq;
30d27b04 875
f97ace34 876 if (!is_zero_ether_addr(peer_bi->mac_addr))
30d27b04
JM
877 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
878 ETH_ALEN);
f97ace34
JM
879
880 return wpas_dpp_auth_init_next(wpa_s);
461d39af 881fail:
461d39af 882 return -1;
30d27b04
JM
883}
884
885
886struct wpas_dpp_listen_work {
887 unsigned int freq;
888 unsigned int duration;
889 struct wpabuf *probe_resp_ie;
890};
891
892
893static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
894{
895 if (!lwork)
896 return;
897 os_free(lwork);
898}
899
900
901static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
902{
903 struct wpas_dpp_listen_work *lwork;
904
905 if (!wpa_s->dpp_listen_work)
906 return;
907
908 lwork = wpa_s->dpp_listen_work->ctx;
909 wpas_dpp_listen_work_free(lwork);
910 radio_work_done(wpa_s->dpp_listen_work);
911 wpa_s->dpp_listen_work = NULL;
912}
913
914
915static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
916{
917 struct wpa_supplicant *wpa_s = work->wpa_s;
918 struct wpas_dpp_listen_work *lwork = work->ctx;
919
920 if (deinit) {
921 if (work->started) {
922 wpa_s->dpp_listen_work = NULL;
923 wpas_dpp_listen_stop(wpa_s);
924 }
925 wpas_dpp_listen_work_free(lwork);
926 return;
927 }
928
929 wpa_s->dpp_listen_work = work;
930
931 wpa_s->dpp_pending_listen_freq = lwork->freq;
932
933 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
934 wpa_s->max_remain_on_chan) < 0) {
935 wpa_printf(MSG_DEBUG,
936 "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
937 lwork->freq);
938 wpas_dpp_listen_work_done(wpa_s);
939 wpa_s->dpp_pending_listen_freq = 0;
940 return;
941 }
942 wpa_s->off_channel_freq = 0;
943 wpa_s->roc_waiting_drv_freq = lwork->freq;
944}
945
946
947static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
948 unsigned int freq)
949{
950 struct wpas_dpp_listen_work *lwork;
951
952 if (wpa_s->dpp_listen_work) {
953 wpa_printf(MSG_DEBUG,
954 "DPP: Reject start_listen since dpp_listen_work already exists");
955 return -1;
956 }
957
958 if (wpa_s->dpp_listen_freq)
959 wpas_dpp_listen_stop(wpa_s);
960 wpa_s->dpp_listen_freq = freq;
961
962 lwork = os_zalloc(sizeof(*lwork));
963 if (!lwork)
964 return -1;
965 lwork->freq = freq;
966
967 if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
968 lwork) < 0) {
969 wpas_dpp_listen_work_free(lwork);
970 return -1;
971 }
972
973 return 0;
974}
975
976
977int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
978{
979 int freq;
980
981 freq = atoi(cmd);
982 if (freq <= 0)
983 return -1;
984
985 if (os_strstr(cmd, " role=configurator"))
986 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
987 else if (os_strstr(cmd, " role=enrollee"))
988 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
989 else
990 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
991 DPP_CAPAB_ENROLLEE;
992 wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
461d39af 993 wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
30d27b04
JM
994 if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
995 wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
996 freq);
997 return 0;
998 }
999
1000 return wpas_dpp_listen_start(wpa_s, freq);
1001}
1002
1003
1004void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
1005{
0db637ca 1006 wpa_s->dpp_in_response_listen = 0;
30d27b04
JM
1007 if (!wpa_s->dpp_listen_freq)
1008 return;
1009
1010 wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
1011 wpa_s->dpp_listen_freq);
1012 wpa_drv_cancel_remain_on_channel(wpa_s);
1013 wpa_s->dpp_listen_freq = 0;
1014 wpas_dpp_listen_work_done(wpa_s);
1015}
1016
1017
1018void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
1019 unsigned int freq)
1020{
1021 if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
1022 return;
1023
1024 wpa_printf(MSG_DEBUG,
1025 "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
1026 wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
1027 wpa_s->roc_waiting_drv_freq, freq);
1028 if (wpa_s->off_channel_freq &&
1029 wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
1030 wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
1031 wpa_s->dpp_pending_listen_freq = 0;
1032 } else {
1033 wpa_printf(MSG_DEBUG,
1034 "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
1035 wpa_s->off_channel_freq,
1036 wpa_s->dpp_pending_listen_freq, freq);
1037 }
1038}
1039
1040
1041void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
1042 unsigned int freq)
1043{
1044 wpas_dpp_listen_work_done(wpa_s);
1045
0db637ca
JM
1046 if (wpa_s->dpp_auth && wpa_s->dpp_in_response_listen) {
1047 unsigned int new_freq;
1048
30d27b04 1049 /* Continue listen with a new remain-on-channel */
0db637ca
JM
1050 if (wpa_s->dpp_auth->neg_freq > 0)
1051 new_freq = wpa_s->dpp_auth->neg_freq;
1052 else
1053 new_freq = wpa_s->dpp_auth->curr_freq;
30d27b04
JM
1054 wpa_printf(MSG_DEBUG,
1055 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
0db637ca
JM
1056 new_freq);
1057 wpas_dpp_listen_start(wpa_s, new_freq);
30d27b04
JM
1058 return;
1059 }
1060
1061 if (wpa_s->dpp_listen_freq) {
1062 /* Continue listen with a new remain-on-channel */
1063 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
1064 }
1065}
1066
1067
1068static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
dc4d271c
JM
1069 const u8 *hdr, const u8 *buf, size_t len,
1070 unsigned int freq)
30d27b04 1071{
27fefbbb
JM
1072 const u8 *r_bootstrap, *i_bootstrap;
1073 u16 r_bootstrap_len, i_bootstrap_len;
30d27b04 1074 struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
30d27b04
JM
1075
1076 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
1077 MAC2STR(src));
1078
30d27b04
JM
1079 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
1080 &r_bootstrap_len);
27fefbbb 1081 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
26806abe
JM
1082 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1083 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
30d27b04
JM
1084 return;
1085 }
1086 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
1087 r_bootstrap, r_bootstrap_len);
1088
1089 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
1090 &i_bootstrap_len);
27fefbbb 1091 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
26806abe
JM
1092 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1093 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
30d27b04
JM
1094 return;
1095 }
1096 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
1097 i_bootstrap, i_bootstrap_len);
1098
1099 /* Try to find own and peer bootstrapping key matches based on the
1100 * received hash values */
1101 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
1102 list) {
1103 if (!own_bi && bi->own &&
1104 os_memcmp(bi->pubkey_hash, r_bootstrap,
1105 SHA256_MAC_LEN) == 0) {
1106 wpa_printf(MSG_DEBUG,
1107 "DPP: Found matching own bootstrapping information");
1108 own_bi = bi;
1109 }
1110
1111 if (!peer_bi && !bi->own &&
1112 os_memcmp(bi->pubkey_hash, i_bootstrap,
1113 SHA256_MAC_LEN) == 0) {
1114 wpa_printf(MSG_DEBUG,
1115 "DPP: Found matching peer bootstrapping information");
1116 peer_bi = bi;
1117 }
1118
1119 if (own_bi && peer_bi)
1120 break;
1121 }
1122
1123 if (!own_bi) {
26806abe
JM
1124 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1125 "No matching own bootstrapping key found - ignore message");
30d27b04
JM
1126 return;
1127 }
1128
1129 if (wpa_s->dpp_auth) {
26806abe
JM
1130 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1131 "Already in DPP authentication exchange - ignore new one");
30d27b04
JM
1132 return;
1133 }
1134
461d39af
JM
1135 wpa_s->dpp_gas_client = 0;
1136 wpa_s->dpp_auth_ok_on_ack = 0;
30d27b04
JM
1137 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
1138 wpa_s->dpp_qr_mutual,
27fefbbb 1139 peer_bi, own_bi, freq, hdr, buf, len);
30d27b04
JM
1140 if (!wpa_s->dpp_auth) {
1141 wpa_printf(MSG_DEBUG, "DPP: No response generated");
1142 return;
1143 }
461d39af 1144 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
b65b22d6
JM
1145 wpas_dpp_set_configurator(wpa_s, wpa_s->dpp_auth,
1146 wpa_s->dpp_configurator_params);
30d27b04
JM
1147 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
1148
d2709206
JM
1149 if (wpa_s->dpp_listen_freq &&
1150 wpa_s->dpp_listen_freq != wpa_s->dpp_auth->curr_freq) {
1151 wpa_printf(MSG_DEBUG,
1152 "DPP: Stop listen on %u MHz to allow response on the request %u MHz",
1153 wpa_s->dpp_listen_freq, wpa_s->dpp_auth->curr_freq);
1154 wpas_dpp_listen_stop(wpa_s);
1155 }
1156
af48810b
JM
1157 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1158 MAC2STR(src), wpa_s->dpp_auth->curr_freq,
1159 DPP_PA_AUTHENTICATION_RESP);
30d27b04
JM
1160 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
1161 src, wpa_s->own_addr, broadcast,
dc4d271c
JM
1162 wpabuf_head(wpa_s->dpp_auth->resp_msg),
1163 wpabuf_len(wpa_s->dpp_auth->resp_msg),
30d27b04 1164 500, wpas_dpp_tx_status, 0);
30d27b04
JM
1165}
1166
1167
461d39af
JM
1168static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
1169{
1170 /* TODO: stop wait and start ROC */
1171}
1172
1173
8528994e
JM
1174static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
1175 struct dpp_authentication *auth)
1176{
1177 struct wpa_ssid *ssid;
1178
1179 ssid = wpa_config_add_network(wpa_s->conf);
1180 if (!ssid)
1181 return NULL;
1182 wpas_notify_network_added(wpa_s, ssid);
1183 wpa_config_set_network_defaults(ssid);
1184 ssid->disabled = 1;
1185
1186 ssid->ssid = os_malloc(auth->ssid_len);
1187 if (!ssid->ssid)
1188 goto fail;
1189 os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
1190 ssid->ssid_len = auth->ssid_len;
1191
1192 if (auth->connector) {
1193 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
70e19013 1194 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
8528994e
JM
1195 ssid->dpp_connector = os_strdup(auth->connector);
1196 if (!ssid->dpp_connector)
1197 goto fail;
1198 }
1199
1200 if (auth->c_sign_key) {
1201 ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
1202 if (!ssid->dpp_csign)
1203 goto fail;
1204 os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
1205 wpabuf_len(auth->c_sign_key));
1206 ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
8528994e
JM
1207 }
1208
1209 if (auth->net_access_key) {
1210 ssid->dpp_netaccesskey =
1211 os_malloc(wpabuf_len(auth->net_access_key));
1212 if (!ssid->dpp_netaccesskey)
1213 goto fail;
1214 os_memcpy(ssid->dpp_netaccesskey,
1215 wpabuf_head(auth->net_access_key),
1216 wpabuf_len(auth->net_access_key));
1217 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
1218 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
1219 }
1220
1221 if (!auth->connector) {
5dd745b7
JM
1222 ssid->key_mgmt = 0;
1223 if (auth->akm == DPP_AKM_PSK || auth->akm == DPP_AKM_PSK_SAE)
1224 ssid->key_mgmt |= WPA_KEY_MGMT_PSK |
1225 WPA_KEY_MGMT_PSK_SHA256 | WPA_KEY_MGMT_FT_PSK;
1226 if (auth->akm == DPP_AKM_SAE || auth->akm == DPP_AKM_PSK_SAE)
1227 ssid->key_mgmt |= WPA_KEY_MGMT_SAE |
1228 WPA_KEY_MGMT_FT_SAE;
70e19013 1229 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
8528994e
JM
1230 if (auth->passphrase[0]) {
1231 if (wpa_config_set_quoted(ssid, "psk",
1232 auth->passphrase) < 0)
1233 goto fail;
1234 wpa_config_update_psk(ssid);
1235 ssid->export_keys = 1;
1236 } else {
1237 ssid->psk_set = auth->psk_set;
1238 os_memcpy(ssid->psk, auth->psk, PMK_LEN);
1239 }
1240 }
1241
1242 return ssid;
1243fail:
1244 wpas_notify_network_removed(wpa_s, ssid);
1245 wpa_config_remove_network(wpa_s->conf, ssid->id);
1246 return NULL;
1247}
1248
1249
1250static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
1251 struct dpp_authentication *auth)
1252{
1253 struct wpa_ssid *ssid;
1254
1255 if (wpa_s->conf->dpp_config_processing < 1)
1256 return;
1257
1258 ssid = wpas_dpp_add_network(wpa_s, auth);
1259 if (!ssid)
1260 return;
1261
1262 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
1263 if (wpa_s->conf->dpp_config_processing < 2)
1264 return;
1265
1266 wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
1267 ssid->disabled = 0;
1268 wpa_s->disconnected = 0;
1269 wpa_s->reassociate = 1;
1270 wpa_s->scan_runs = 0;
1271 wpa_s->normal_scans = 0;
1272 wpa_supplicant_cancel_sched_scan(wpa_s);
1273 wpa_supplicant_req_scan(wpa_s, 0, 0);
1274}
1275
1276
f522bb23
JM
1277static void wpas_dpp_handle_config_obj(struct wpa_supplicant *wpa_s,
1278 struct dpp_authentication *auth)
461d39af 1279{
461d39af
JM
1280 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1281 if (auth->ssid_len)
1282 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
1283 wpa_ssid_txt(auth->ssid, auth->ssid_len));
1284 if (auth->connector) {
1285 /* TODO: Save the Connector and consider using a command
1286 * to fetch the value instead of sending an event with
1287 * it. The Connector could end up being larger than what
1288 * most clients are ready to receive as an event
1289 * message. */
1290 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
1291 auth->connector);
1292 }
1293 if (auth->c_sign_key) {
1294 char *hex;
1295 size_t hexlen;
1296
1297 hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
1298 hex = os_malloc(hexlen);
1299 if (hex) {
1300 wpa_snprintf_hex(hex, hexlen,
1301 wpabuf_head(auth->c_sign_key),
1302 wpabuf_len(auth->c_sign_key));
c77e2ff0
JM
1303 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY "%s",
1304 hex);
461d39af
JM
1305 os_free(hex);
1306 }
1307 }
1308 if (auth->net_access_key) {
1309 char *hex;
1310 size_t hexlen;
1311
1312 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1313 hex = os_malloc(hexlen);
1314 if (hex) {
1315 wpa_snprintf_hex(hex, hexlen,
1316 wpabuf_head(auth->net_access_key),
1317 wpabuf_len(auth->net_access_key));
1318 if (auth->net_access_key_expiry)
1319 wpa_msg(wpa_s, MSG_INFO,
1320 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1321 (long unsigned)
1322 auth->net_access_key_expiry);
1323 else
1324 wpa_msg(wpa_s, MSG_INFO,
1325 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1326 os_free(hex);
1327 }
1328 }
8528994e
JM
1329
1330 wpas_dpp_process_config(wpa_s, auth);
f522bb23
JM
1331}
1332
1333
1334static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
1335 enum gas_query_result result,
1336 const struct wpabuf *adv_proto,
1337 const struct wpabuf *resp, u16 status_code)
1338{
1339 struct wpa_supplicant *wpa_s = ctx;
1340 const u8 *pos;
1341 struct dpp_authentication *auth = wpa_s->dpp_auth;
1342
1866dfb5
JM
1343 wpa_s->dpp_gas_dialog_token = -1;
1344
f522bb23
JM
1345 if (!auth || !auth->auth_success) {
1346 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1347 return;
1348 }
931f7ff6
JM
1349 if (result != GAS_QUERY_SUCCESS ||
1350 !resp || status_code != WLAN_STATUS_SUCCESS) {
f522bb23
JM
1351 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
1352 goto fail;
1353 }
1354
1355 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
1356 adv_proto);
1357 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
1358 resp);
1359
1360 if (wpabuf_len(adv_proto) != 10 ||
1361 !(pos = wpabuf_head(adv_proto)) ||
1362 pos[0] != WLAN_EID_ADV_PROTO ||
1363 pos[1] != 8 ||
1364 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1365 pos[4] != 5 ||
1366 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1367 pos[8] != 0x1a ||
1368 pos[9] != 1) {
1369 wpa_printf(MSG_DEBUG,
1370 "DPP: Not a DPP Advertisement Protocol ID");
1371 goto fail;
1372 }
8528994e 1373
f522bb23
JM
1374 if (dpp_conf_resp_rx(auth, resp) < 0) {
1375 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1376 goto fail;
1377 }
1378
1379 wpas_dpp_handle_config_obj(wpa_s, auth);
461d39af
JM
1380 dpp_auth_deinit(wpa_s->dpp_auth);
1381 wpa_s->dpp_auth = NULL;
1382 return;
1383
1384fail:
1385 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1386 dpp_auth_deinit(wpa_s->dpp_auth);
1387 wpa_s->dpp_auth = NULL;
1388}
1389
1390
1391static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1392{
1393 struct dpp_authentication *auth = wpa_s->dpp_auth;
1394 struct wpabuf *buf, *conf_req;
1395 char json[100];
1396 int res;
1397
1398 wpa_s->dpp_gas_client = 1;
1399 os_snprintf(json, sizeof(json),
1400 "{\"name\":\"Test\","
1401 "\"wi-fi_tech\":\"infra\","
1402 "\"netRole\":\"%s\"}",
1403 wpa_s->dpp_netrole_ap ? "ap" : "sta");
f9cf7d03
JM
1404#ifdef CONFIG_TESTING_OPTIONS
1405 if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
1406 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
1407 json[29] = 'k'; /* replace "infra" with "knfra" */
1408 }
1409#endif /* CONFIG_TESTING_OPTIONS */
461d39af
JM
1410 wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
1411
1412 offchannel_send_action_done(wpa_s);
1413 wpas_dpp_listen_stop(wpa_s);
1414
1415 conf_req = dpp_build_conf_req(auth, json);
1416 if (!conf_req) {
1417 wpa_printf(MSG_DEBUG,
1418 "DPP: No configuration request data available");
1419 return;
1420 }
1421
1422 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
1423 if (!buf) {
1424 wpabuf_free(conf_req);
1425 return;
1426 }
1427
1428 /* Advertisement Protocol IE */
1429 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
1430 wpabuf_put_u8(buf, 8); /* Length */
1431 wpabuf_put_u8(buf, 0x7f);
1432 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
1433 wpabuf_put_u8(buf, 5);
1434 wpabuf_put_be24(buf, OUI_WFA);
1435 wpabuf_put_u8(buf, DPP_OUI_TYPE);
1436 wpabuf_put_u8(buf, 0x01);
1437
1438 /* GAS Query */
1439 wpabuf_put_le16(buf, wpabuf_len(conf_req));
1440 wpabuf_put_buf(buf, conf_req);
1441 wpabuf_free(conf_req);
1442
1443 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1444 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1445
1446 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
aca4d84e 1447 1, buf, wpas_dpp_gas_resp_cb, wpa_s);
461d39af
JM
1448 if (res < 0) {
1449 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1450 wpabuf_free(buf);
1451 } else {
1452 wpa_printf(MSG_DEBUG,
1453 "DPP: GAS query started with dialog token %u", res);
1866dfb5 1454 wpa_s->dpp_gas_dialog_token = res;
461d39af
JM
1455 }
1456}
1457
1458
1459static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1460{
1461 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1462 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
d74963d4
SD
1463#ifdef CONFIG_TESTING_OPTIONS
1464 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
1465 wpa_printf(MSG_INFO,
1466 "DPP: TESTING - stop at Authentication Confirm");
1467 if (wpa_s->dpp_auth->configurator) {
1468 /* Prevent GAS response */
1469 wpa_s->dpp_auth->auth_success = 0;
1470 }
1471 return;
1472 }
1473#endif /* CONFIG_TESTING_OPTIONS */
461d39af
JM
1474
1475 if (wpa_s->dpp_auth->configurator)
1476 wpas_dpp_start_gas_server(wpa_s);
1477 else
1478 wpas_dpp_start_gas_client(wpa_s);
1479}
1480
1481
30d27b04 1482static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
d2709206
JM
1483 const u8 *hdr, const u8 *buf, size_t len,
1484 unsigned int freq)
30d27b04
JM
1485{
1486 struct dpp_authentication *auth = wpa_s->dpp_auth;
dc4d271c 1487 struct wpabuf *msg;
30d27b04 1488
d2709206
JM
1489 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR
1490 " (freq %u MHz)", MAC2STR(src), freq);
30d27b04
JM
1491
1492 if (!auth) {
1493 wpa_printf(MSG_DEBUG,
1494 "DPP: No DPP Authentication in progress - drop");
1495 return;
1496 }
1497
1498 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1499 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1500 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1501 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1502 return;
1503 }
1504
1505 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1506
d2709206
JM
1507 if (auth->curr_freq != freq && auth->neg_freq == freq) {
1508 wpa_printf(MSG_DEBUG,
1509 "DPP: Responder accepted request for different negotiation channel");
1510 auth->curr_freq = freq;
1511 }
1512
f97ace34 1513 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
dc4d271c
JM
1514 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
1515 if (!msg) {
30d27b04
JM
1516 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1517 wpa_printf(MSG_DEBUG,
1518 "DPP: Start wait for full response");
1519 offchannel_send_action_done(wpa_s);
1520 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1521 return;
1522 }
1523 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1524 return;
1525 }
1526 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1527
af48810b
JM
1528 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1529 MAC2STR(src), auth->curr_freq, DPP_PA_AUTHENTICATION_CONF);
30d27b04
JM
1530 offchannel_send_action(wpa_s, auth->curr_freq,
1531 src, wpa_s->own_addr, broadcast,
1532 wpabuf_head(msg), wpabuf_len(msg),
1533 500, wpas_dpp_tx_status, 0);
1534 wpabuf_free(msg);
461d39af 1535 wpa_s->dpp_auth_ok_on_ack = 1;
30d27b04
JM
1536}
1537
1538
1539static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
dc4d271c 1540 const u8 *hdr, const u8 *buf, size_t len)
30d27b04
JM
1541{
1542 struct dpp_authentication *auth = wpa_s->dpp_auth;
1543
1544 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1545 MAC2STR(src));
1546
1547 if (!auth) {
1548 wpa_printf(MSG_DEBUG,
1549 "DPP: No DPP Authentication in progress - drop");
1550 return;
1551 }
1552
1553 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1554 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1555 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1556 return;
1557 }
1558
dc4d271c 1559 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
30d27b04
JM
1560 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1561 return;
1562 }
1563
461d39af 1564 wpas_dpp_auth_success(wpa_s, 0);
30d27b04
JM
1565}
1566
1567
a0d5c56f
JM
1568static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1569 const u8 *src,
1570 const u8 *buf, size_t len)
1571{
1572 struct wpa_ssid *ssid;
e85b6601
JM
1573 const u8 *connector, *trans_id, *status;
1574 u16 connector_len, trans_id_len, status_len;
a0d5c56f
JM
1575 struct dpp_introduction intro;
1576 struct rsn_pmksa_cache_entry *entry;
787615b3
JM
1577 struct os_time now;
1578 struct os_reltime rnow;
1579 os_time_t expiry;
1580 unsigned int seconds;
e85b6601 1581 enum dpp_status_error res;
a0d5c56f
JM
1582
1583 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1584 MAC2STR(src));
1585 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1586 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1587 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1588 MACSTR " - drop", MAC2STR(src));
1589 return;
1590 }
1591 offchannel_send_action_done(wpa_s);
1592
1593 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1594 if (ssid == wpa_s->dpp_intro_network)
1595 break;
1596 }
1597 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1598 !ssid->dpp_csign) {
1599 wpa_printf(MSG_DEBUG,
1600 "DPP: Profile not found for network introduction");
1601 return;
1602 }
1603
85fd8263
JM
1604 trans_id = dpp_get_attr(buf, len, DPP_ATTR_TRANSACTION_ID,
1605 &trans_id_len);
1606 if (!trans_id || trans_id_len != 1) {
1607 wpa_printf(MSG_DEBUG,
1608 "DPP: Peer did not include Transaction ID");
e85b6601
JM
1609 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1610 " fail=missing_transaction_id", MAC2STR(src));
85fd8263
JM
1611 goto fail;
1612 }
1613 if (trans_id[0] != TRANSACTION_ID) {
1614 wpa_printf(MSG_DEBUG,
1615 "DPP: Ignore frame with unexpected Transaction ID %u",
1616 trans_id[0]);
e85b6601
JM
1617 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1618 " fail=transaction_id_mismatch", MAC2STR(src));
1619 goto fail;
1620 }
1621
1622 status = dpp_get_attr(buf, len, DPP_ATTR_STATUS, &status_len);
1623 if (!status || status_len != 1) {
1624 wpa_printf(MSG_DEBUG, "DPP: Peer did not include Status");
1625 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1626 " fail=missing_status", MAC2STR(src));
1627 goto fail;
1628 }
1629 if (status[0] != DPP_STATUS_OK) {
1630 wpa_printf(MSG_DEBUG,
1631 "DPP: Peer rejected network introduction: Status %u",
1632 status[0]);
1633 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1634 " status=%u", MAC2STR(src), status[0]);
85fd8263
JM
1635 goto fail;
1636 }
1637
a0d5c56f
JM
1638 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1639 if (!connector) {
1640 wpa_printf(MSG_DEBUG,
1641 "DPP: Peer did not include its Connector");
e85b6601
JM
1642 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1643 " fail=missing_connector", MAC2STR(src));
1644 goto fail;
a0d5c56f
JM
1645 }
1646
e85b6601
JM
1647 res = dpp_peer_intro(&intro, ssid->dpp_connector,
1648 ssid->dpp_netaccesskey,
1649 ssid->dpp_netaccesskey_len,
1650 ssid->dpp_csign,
1651 ssid->dpp_csign_len,
1652 connector, connector_len, &expiry);
1653 if (res != DPP_STATUS_OK) {
a0d5c56f
JM
1654 wpa_printf(MSG_INFO,
1655 "DPP: Network Introduction protocol resulted in failure");
e85b6601
JM
1656 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1657 " fail=peer_connector_validation_failed", MAC2STR(src));
a0d5c56f
JM
1658 goto fail;
1659 }
1660
a0d5c56f
JM
1661 entry = os_zalloc(sizeof(*entry));
1662 if (!entry)
1663 goto fail;
1664 os_memcpy(entry->aa, src, ETH_ALEN);
1665 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1666 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1667 entry->pmk_len = intro.pmk_len;
1668 entry->akmp = WPA_KEY_MGMT_DPP;
787615b3
JM
1669 if (expiry) {
1670 os_get_time(&now);
1671 seconds = expiry - now.sec;
1672 } else {
1673 seconds = 86400 * 7;
1674 }
1675 os_get_reltime(&rnow);
1676 entry->expiration = rnow.sec + seconds;
1677 entry->reauth_time = rnow.sec + seconds;
a0d5c56f
JM
1678 entry->network_ctx = ssid;
1679 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1680
e85b6601
JM
1681 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_INTRO "peer=" MACSTR
1682 " status=%u", MAC2STR(src), status[0]);
1683
a0d5c56f
JM
1684 wpa_printf(MSG_DEBUG,
1685 "DPP: Try connection again after successful network introduction");
1686 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1687 wpa_supplicant_cancel_sched_scan(wpa_s);
1688 wpa_supplicant_req_scan(wpa_s, 0, 0);
1689 }
1690fail:
1691 os_memset(&intro, 0, sizeof(intro));
1692}
1693
1694
3b50f8a4
JM
1695static int wpas_dpp_allow_ir(struct wpa_supplicant *wpa_s, unsigned int freq)
1696{
1697 int i, j;
1698
1699 if (!wpa_s->hw.modes)
1700 return -1;
1701
1702 for (i = 0; i < wpa_s->hw.num_modes; i++) {
1703 struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
1704
1705 for (j = 0; j < mode->num_channels; j++) {
1706 struct hostapd_channel_data *chan = &mode->channels[j];
1707
1708 if (chan->freq != (int) freq)
1709 continue;
1710
1711 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
1712 HOSTAPD_CHAN_NO_IR |
1713 HOSTAPD_CHAN_RADAR))
1714 continue;
1715
1716 return 1;
1717 }
1718 }
1719
1720 wpa_printf(MSG_DEBUG,
1721 "DPP: Frequency %u MHz not supported or does not allow PKEX initiation in the current channel list",
1722 freq);
1723
1724 return 0;
1725}
1726
1727
1728static int wpas_dpp_pkex_next_channel(struct wpa_supplicant *wpa_s,
1729 struct dpp_pkex *pkex)
1730{
1731 if (pkex->freq == 2437)
1732 pkex->freq = 5745;
1733 else if (pkex->freq == 5745)
1734 pkex->freq = 5220;
1735 else if (pkex->freq == 5220)
1736 pkex->freq = 60480;
1737 else
1738 return -1; /* no more channels to try */
1739
1740 if (wpas_dpp_allow_ir(wpa_s, pkex->freq) == 1) {
1741 wpa_printf(MSG_DEBUG, "DPP: Try to initiate on %u MHz",
1742 pkex->freq);
1743 return 0;
1744 }
1745
1746 /* Could not use this channel - try the next one */
1747 return wpas_dpp_pkex_next_channel(wpa_s, pkex);
1748}
1749
1750
00d2d13d
JM
1751static void wpas_dpp_pkex_retry_timeout(void *eloop_ctx, void *timeout_ctx)
1752{
1753 struct wpa_supplicant *wpa_s = eloop_ctx;
1754 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1755
1756 if (!pkex || !pkex->exchange_req)
1757 return;
1758 if (pkex->exch_req_tries >= 5) {
3b50f8a4
JM
1759 if (wpas_dpp_pkex_next_channel(wpa_s, pkex) < 0) {
1760 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_FAIL
1761 "No response from PKEX peer");
1762 dpp_pkex_free(pkex);
1763 wpa_s->dpp_pkex = NULL;
1764 return;
1765 }
1766 pkex->exch_req_tries = 0;
00d2d13d
JM
1767 }
1768
1769 pkex->exch_req_tries++;
1770 wpa_printf(MSG_DEBUG, "DPP: Retransmit PKEX Exchange Request (try %u)",
1771 pkex->exch_req_tries);
1772 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1773 MAC2STR(broadcast), pkex->freq, DPP_PA_PKEX_EXCHANGE_REQ);
1774 offchannel_send_action(wpa_s, pkex->freq, broadcast,
1775 wpa_s->own_addr, broadcast,
1776 wpabuf_head(pkex->exchange_req),
1777 wpabuf_len(pkex->exchange_req),
1778 pkex->exch_req_wait_time,
1779 wpas_dpp_tx_pkex_status, 0);
1780}
1781
1782
500ed7f0
JM
1783static void
1784wpas_dpp_tx_pkex_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{
af48810b 1790 const char *res_txt;
00d2d13d 1791 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
af48810b
JM
1792
1793 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1794 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1795 "FAILED");
500ed7f0
JM
1796 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1797 " result=%s (PKEX)",
af48810b
JM
1798 freq, MAC2STR(dst), res_txt);
1799 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
1800 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
e0247e79 1801
00d2d13d 1802 if (!pkex) {
e0247e79
JM
1803 wpa_printf(MSG_DEBUG,
1804 "DPP: Ignore TX status since there is no ongoing PKEX exchange");
1805 return;
1806 }
1807
00d2d13d 1808 if (pkex->failed) {
e0247e79
JM
1809 wpa_printf(MSG_DEBUG,
1810 "DPP: Terminate PKEX exchange due to an earlier error");
00d2d13d
JM
1811 if (pkex->t > pkex->own_bi->pkex_t)
1812 pkex->own_bi->pkex_t = pkex->t;
1813 dpp_pkex_free(pkex);
e0247e79 1814 wpa_s->dpp_pkex = NULL;
00d2d13d
JM
1815 return;
1816 }
1817
1818 if (pkex->exch_req_wait_time && pkex->exchange_req) {
1819 /* Wait for PKEX Exchange Response frame and retry request if
1820 * no response is seen. */
1821 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1822 eloop_register_timeout(pkex->exch_req_wait_time / 1000,
1823 (pkex->exch_req_wait_time % 1000) * 1000,
1824 wpas_dpp_pkex_retry_timeout, wpa_s,
1825 NULL);
e0247e79 1826 }
500ed7f0
JM
1827}
1828
1829
1830static void
1831wpas_dpp_rx_pkex_exchange_req(struct wpa_supplicant *wpa_s, const u8 *src,
1832 const u8 *buf, size_t len, unsigned int freq)
1833{
1834 struct wpabuf *msg;
1835 unsigned int wait_time;
1836
1837 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request from " MACSTR,
1838 MAC2STR(src));
1839
1840 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1841 * values here */
1842
1843 if (!wpa_s->dpp_pkex_code || !wpa_s->dpp_pkex_bi) {
1844 wpa_printf(MSG_DEBUG,
1845 "DPP: No PKEX code configured - ignore request");
1846 return;
1847 }
1848
1849 if (wpa_s->dpp_pkex) {
1850 /* TODO: Support parallel operations */
1851 wpa_printf(MSG_DEBUG,
1852 "DPP: Already in PKEX session - ignore new request");
1853 return;
1854 }
1855
219d4c9f 1856 wpa_s->dpp_pkex = dpp_pkex_rx_exchange_req(wpa_s, wpa_s->dpp_pkex_bi,
500ed7f0
JM
1857 wpa_s->own_addr, src,
1858 wpa_s->dpp_pkex_identifier,
1859 wpa_s->dpp_pkex_code,
1860 buf, len);
1861 if (!wpa_s->dpp_pkex) {
1862 wpa_printf(MSG_DEBUG,
1863 "DPP: Failed to process the request - ignore it");
1864 return;
1865 }
1866
1867 msg = wpa_s->dpp_pkex->exchange_resp;
1868 wait_time = wpa_s->max_remain_on_chan;
1869 if (wait_time > 2000)
1870 wait_time = 2000;
af48810b
JM
1871 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1872 MAC2STR(src), freq, DPP_PA_PKEX_EXCHANGE_RESP);
500ed7f0
JM
1873 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1874 broadcast,
1875 wpabuf_head(msg), wpabuf_len(msg),
1876 wait_time, wpas_dpp_tx_pkex_status, 0);
1877}
1878
1879
1880static void
1881wpas_dpp_rx_pkex_exchange_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1882 const u8 *buf, size_t len, unsigned int freq)
1883{
1884 struct wpabuf *msg;
1885 unsigned int wait_time;
1886
1887 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response from " MACSTR,
1888 MAC2STR(src));
1889
1890 /* TODO: Support multiple PKEX codes by iterating over all the enabled
1891 * values here */
1892
1893 if (!wpa_s->dpp_pkex || !wpa_s->dpp_pkex->initiator ||
1894 wpa_s->dpp_pkex->exchange_done) {
1895 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
1896 return;
1897 }
1898
00d2d13d
JM
1899 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
1900 wpa_s->dpp_pkex->exch_req_wait_time = 0;
1901
af4103e5 1902 msg = dpp_pkex_rx_exchange_resp(wpa_s->dpp_pkex, src, buf, len);
500ed7f0
JM
1903 if (!msg) {
1904 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
1905 return;
1906 }
1907
1908 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Request to " MACSTR,
1909 MAC2STR(src));
1910
1911 wait_time = wpa_s->max_remain_on_chan;
1912 if (wait_time > 2000)
1913 wait_time = 2000;
af48810b
JM
1914 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1915 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_REQ);
500ed7f0
JM
1916 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1917 broadcast,
1918 wpabuf_head(msg), wpabuf_len(msg),
1919 wait_time, wpas_dpp_tx_pkex_status, 0);
1920 wpabuf_free(msg);
1921}
1922
1923
de029861
JM
1924static struct dpp_bootstrap_info *
1925wpas_dpp_pkex_finish(struct wpa_supplicant *wpa_s, const u8 *peer,
1926 unsigned int freq)
1927{
1928 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
1929 struct dpp_bootstrap_info *bi;
1930
1931 bi = os_zalloc(sizeof(*bi));
1932 if (!bi)
1933 return NULL;
1934 bi->id = wpas_dpp_next_id(wpa_s);
1935 bi->type = DPP_BOOTSTRAP_PKEX;
1936 os_memcpy(bi->mac_addr, peer, ETH_ALEN);
1937 bi->num_freq = 1;
1938 bi->freq[0] = freq;
1939 bi->curve = pkex->own_bi->curve;
1940 bi->pubkey = pkex->peer_bootstrap_key;
1941 pkex->peer_bootstrap_key = NULL;
1942 dpp_pkex_free(pkex);
1943 wpa_s->dpp_pkex = NULL;
1944 if (dpp_bootstrap_key_hash(bi) < 0) {
1945 dpp_bootstrap_info_free(bi);
1946 return NULL;
1947 }
1948 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
1949 return bi;
1950}
1951
1952
500ed7f0
JM
1953static void
1954wpas_dpp_rx_pkex_commit_reveal_req(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98
JM
1955 const u8 *hdr, const u8 *buf, size_t len,
1956 unsigned int freq)
500ed7f0
JM
1957{
1958 struct wpabuf *msg;
1959 unsigned int wait_time;
1960 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
500ed7f0
JM
1961
1962 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Request from " MACSTR,
1963 MAC2STR(src));
1964
1965 if (!pkex || pkex->initiator || !pkex->exchange_done) {
1966 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
1967 return;
1968 }
1969
4be5bc98 1970 msg = dpp_pkex_rx_commit_reveal_req(pkex, hdr, buf, len);
500ed7f0
JM
1971 if (!msg) {
1972 wpa_printf(MSG_DEBUG, "DPP: Failed to process the request");
039b8e73
JM
1973 if (pkex->failed) {
1974 wpa_printf(MSG_DEBUG, "DPP: Terminate PKEX exchange");
29ab69e4
JM
1975 if (pkex->t > pkex->own_bi->pkex_t)
1976 pkex->own_bi->pkex_t = pkex->t;
039b8e73
JM
1977 dpp_pkex_free(wpa_s->dpp_pkex);
1978 wpa_s->dpp_pkex = NULL;
1979 }
500ed7f0
JM
1980 return;
1981 }
1982
1983 wpa_printf(MSG_DEBUG, "DPP: Send PKEX Commit-Reveal Response to "
1984 MACSTR, MAC2STR(src));
1985
1986 wait_time = wpa_s->max_remain_on_chan;
1987 if (wait_time > 2000)
1988 wait_time = 2000;
af48810b
JM
1989 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
1990 MAC2STR(src), freq, DPP_PA_PKEX_COMMIT_REVEAL_RESP);
500ed7f0
JM
1991 offchannel_send_action(wpa_s, freq, src, wpa_s->own_addr,
1992 broadcast,
1993 wpabuf_head(msg), wpabuf_len(msg),
1994 wait_time, wpas_dpp_tx_pkex_status, 0);
1995 wpabuf_free(msg);
1996
de029861 1997 wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
1998}
1999
2000
2001static void
2002wpas_dpp_rx_pkex_commit_reveal_resp(struct wpa_supplicant *wpa_s, const u8 *src,
4be5bc98 2003 const u8 *hdr, const u8 *buf, size_t len,
500ed7f0
JM
2004 unsigned int freq)
2005{
2006 int res;
de029861 2007 struct dpp_bootstrap_info *bi;
500ed7f0
JM
2008 struct dpp_pkex *pkex = wpa_s->dpp_pkex;
2009 char cmd[500];
2010
2011 wpa_printf(MSG_DEBUG, "DPP: PKEX Commit-Reveal Response from " MACSTR,
2012 MAC2STR(src));
2013
2014 if (!pkex || !pkex->initiator || !pkex->exchange_done) {
2015 wpa_printf(MSG_DEBUG, "DPP: No matching PKEX session");
2016 return;
2017 }
2018
4be5bc98 2019 res = dpp_pkex_rx_commit_reveal_resp(pkex, hdr, buf, len);
500ed7f0
JM
2020 if (res < 0) {
2021 wpa_printf(MSG_DEBUG, "DPP: Failed to process the response");
2022 return;
2023 }
2024
de029861 2025 bi = wpas_dpp_pkex_finish(wpa_s, src, freq);
500ed7f0
JM
2026 if (!bi)
2027 return;
500ed7f0
JM
2028
2029 os_snprintf(cmd, sizeof(cmd), " peer=%u %s",
2030 bi->id,
2031 wpa_s->dpp_pkex_auth_cmd ? wpa_s->dpp_pkex_auth_cmd : "");
2032 wpa_printf(MSG_DEBUG,
2033 "DPP: Start authentication after PKEX with parameters: %s",
2034 cmd);
2035 if (wpas_dpp_auth_init(wpa_s, cmd) < 0) {
2036 wpa_printf(MSG_DEBUG,
2037 "DPP: Authentication initialization failed");
2038 return;
2039 }
2040}
2041
2042
30d27b04
JM
2043void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
2044 const u8 *buf, size_t len, unsigned int freq)
2045{
8c19ea3f 2046 u8 crypto_suite;
30d27b04 2047 enum dpp_public_action_frame_type type;
dc4d271c 2048 const u8 *hdr;
29ab69e4 2049 unsigned int pkex_t;
30d27b04 2050
dc4d271c
JM
2051 if (len < DPP_HDR_LEN)
2052 return;
2053 if (WPA_GET_BE24(buf) != OUI_WFA || buf[3] != DPP_OUI_TYPE)
30d27b04 2054 return;
dc4d271c
JM
2055 hdr = buf;
2056 buf += 4;
2057 len -= 4;
8c19ea3f
JM
2058 crypto_suite = *buf++;
2059 type = *buf++;
2060 len -= 2;
30d27b04
JM
2061
2062 wpa_printf(MSG_DEBUG,
8c19ea3f 2063 "DPP: Received DPP Public Action frame crypto suite %u type %d from "
30d27b04 2064 MACSTR " freq=%u",
8c19ea3f
JM
2065 crypto_suite, type, MAC2STR(src), freq);
2066 if (crypto_suite != 1) {
2067 wpa_printf(MSG_DEBUG, "DPP: Unsupported crypto suite %u",
2068 crypto_suite);
a7073934
JM
2069 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2070 " freq=%u type=%d ignore=unsupported-crypto-suite",
2071 MAC2STR(src), freq, type);
8c19ea3f
JM
2072 return;
2073 }
30d27b04 2074 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
a7073934
JM
2075 if (dpp_check_attrs(buf, len) < 0) {
2076 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR
2077 " freq=%u type=%d ignore=invalid-attributes",
2078 MAC2STR(src), freq, type);
30d27b04 2079 return;
a7073934
JM
2080 }
2081 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_RX "src=" MACSTR " freq=%u type=%d",
2082 MAC2STR(src), freq, type);
30d27b04
JM
2083
2084 switch (type) {
2085 case DPP_PA_AUTHENTICATION_REQ:
dc4d271c 2086 wpas_dpp_rx_auth_req(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2087 break;
2088 case DPP_PA_AUTHENTICATION_RESP:
d2709206 2089 wpas_dpp_rx_auth_resp(wpa_s, src, hdr, buf, len, freq);
30d27b04
JM
2090 break;
2091 case DPP_PA_AUTHENTICATION_CONF:
dc4d271c 2092 wpas_dpp_rx_auth_conf(wpa_s, src, hdr, buf, len);
30d27b04 2093 break;
a0d5c56f
JM
2094 case DPP_PA_PEER_DISCOVERY_RESP:
2095 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
2096 break;
500ed7f0
JM
2097 case DPP_PA_PKEX_EXCHANGE_REQ:
2098 wpas_dpp_rx_pkex_exchange_req(wpa_s, src, buf, len, freq);
2099 break;
2100 case DPP_PA_PKEX_EXCHANGE_RESP:
2101 wpas_dpp_rx_pkex_exchange_resp(wpa_s, src, buf, len, freq);
2102 break;
2103 case DPP_PA_PKEX_COMMIT_REVEAL_REQ:
4be5bc98
JM
2104 wpas_dpp_rx_pkex_commit_reveal_req(wpa_s, src, hdr, buf, len,
2105 freq);
500ed7f0
JM
2106 break;
2107 case DPP_PA_PKEX_COMMIT_REVEAL_RESP:
4be5bc98
JM
2108 wpas_dpp_rx_pkex_commit_reveal_resp(wpa_s, src, hdr, buf, len,
2109 freq);
500ed7f0 2110 break;
30d27b04
JM
2111 default:
2112 wpa_printf(MSG_DEBUG,
2113 "DPP: Ignored unsupported frame subtype %d", type);
2114 break;
2115 }
29ab69e4
JM
2116
2117 if (wpa_s->dpp_pkex)
2118 pkex_t = wpa_s->dpp_pkex->t;
2119 else if (wpa_s->dpp_pkex_bi)
2120 pkex_t = wpa_s->dpp_pkex_bi->pkex_t;
2121 else
2122 pkex_t = 0;
2123 if (pkex_t >= PKEX_COUNTER_T_LIMIT) {
2124 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_PKEX_T_LIMIT "id=0");
2125 wpas_dpp_pkex_remove(wpa_s, "*");
2126 }
30d27b04
JM
2127}
2128
2129
461d39af
JM
2130static struct wpabuf *
2131wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
2132 size_t query_len)
2133{
2134 struct wpa_supplicant *wpa_s = ctx;
2135 struct dpp_authentication *auth = wpa_s->dpp_auth;
2136 struct wpabuf *resp;
2137
2138 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
2139 MAC2STR(sa));
2140 if (!auth || !auth->auth_success ||
2141 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
2142 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
2143 return NULL;
2144 }
2145 wpa_hexdump(MSG_DEBUG,
2146 "DPP: Received Configuration Request (GAS Query Request)",
2147 query, query_len);
fd920954
JM
2148 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_REQ_RX "src=" MACSTR,
2149 MAC2STR(sa));
461d39af
JM
2150 resp = dpp_conf_req_rx(auth, query, query_len);
2151 if (!resp)
2152 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
82feacce 2153 auth->conf_resp = resp;
461d39af
JM
2154 return resp;
2155}
2156
2157
2158static void
2159wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
2160{
2161 struct wpa_supplicant *wpa_s = ctx;
2162 struct dpp_authentication *auth = wpa_s->dpp_auth;
2163
2164 if (!auth) {
2165 wpabuf_free(resp);
2166 return;
2167 }
82feacce
JM
2168 if (auth->conf_resp != resp) {
2169 wpa_printf(MSG_DEBUG,
2170 "DPP: Ignore GAS status report (ok=%d) for unknown response",
2171 ok);
2172 wpabuf_free(resp);
2173 return;
2174 }
461d39af
JM
2175
2176 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
2177 ok);
2178 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
95b0104a 2179 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
461d39af
JM
2180 offchannel_send_action_done(wpa_s);
2181 wpas_dpp_listen_stop(wpa_s);
2182 if (ok)
2183 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
2184 else
2185 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
2186 dpp_auth_deinit(wpa_s->dpp_auth);
2187 wpa_s->dpp_auth = NULL;
2188 wpabuf_free(resp);
2189}
2190
2191
2192static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
2193{
2194 struct dpp_configurator *conf;
2195 unsigned int max_id = 0;
2196
2197 dl_list_for_each(conf, &wpa_s->dpp_configurator,
2198 struct dpp_configurator, list) {
2199 if (conf->id > max_id)
2200 max_id = conf->id;
2201 }
2202 return max_id + 1;
2203}
2204
2205
2206int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
2207{
c77e2ff0 2208 char *curve = NULL;
461d39af
JM
2209 char *key = NULL;
2210 u8 *privkey = NULL;
2211 size_t privkey_len = 0;
2212 int ret = -1;
2213 struct dpp_configurator *conf = NULL;
2214
461d39af
JM
2215 curve = get_param(cmd, " curve=");
2216 key = get_param(cmd, " key=");
2217
2218 if (key) {
2219 privkey_len = os_strlen(key) / 2;
2220 privkey = os_malloc(privkey_len);
2221 if (!privkey ||
2222 hexstr2bin(key, privkey, privkey_len) < 0)
2223 goto fail;
2224 }
2225
2226 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
2227 if (!conf)
2228 goto fail;
2229
461d39af
JM
2230 conf->id = wpas_dpp_next_configurator_id(wpa_s);
2231 dl_list_add(&wpa_s->dpp_configurator, &conf->list);
2232 ret = conf->id;
2233 conf = NULL;
2234fail:
2235 os_free(curve);
461d39af
JM
2236 str_clear_free(key);
2237 bin_clear_free(privkey, privkey_len);
2238 dpp_configurator_free(conf);
2239 return ret;
2240}
2241
2242
2243static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
2244{
2245 struct dpp_configurator *conf, *tmp;
2246 int found = 0;
2247
2248 dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
2249 struct dpp_configurator, list) {
2250 if (id && conf->id != id)
2251 continue;
2252 found = 1;
2253 dl_list_del(&conf->list);
2254 dpp_configurator_free(conf);
2255 }
2256
2257 if (id == 0)
2258 return 0; /* flush succeeds regardless of entries found */
2259 return found ? 0 : -1;
2260}
2261
2262
2263int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
2264{
2265 unsigned int id_val;
2266
2267 if (os_strcmp(id, "*") == 0) {
2268 id_val = 0;
2269 } else {
2270 id_val = atoi(id);
2271 if (id_val == 0)
2272 return -1;
2273 }
2274
2275 return dpp_configurator_del(wpa_s, id_val);
2276}
2277
2278
f522bb23
JM
2279int wpas_dpp_configurator_sign(struct wpa_supplicant *wpa_s, const char *cmd)
2280{
2281 struct dpp_authentication *auth;
2282 int ret = -1;
2283 char *curve = NULL;
2284
2285 auth = os_zalloc(sizeof(*auth));
2286 if (!auth)
2287 return -1;
2288
2289 curve = get_param(cmd, " curve=");
2290 wpas_dpp_set_configurator(wpa_s, auth, cmd);
2291
a2588be8 2292 if (dpp_configurator_own_config(auth, curve, 0) == 0) {
f522bb23
JM
2293 wpas_dpp_handle_config_obj(wpa_s, auth);
2294 ret = 0;
2295 }
2296
2297 dpp_auth_deinit(auth);
2298 os_free(curve);
2299
2300 return ret;
2301}
2302
2303
8179ae3a
PK
2304int wpas_dpp_configurator_get_key(struct wpa_supplicant *wpa_s, unsigned int id,
2305 char *buf, size_t buflen)
2306{
2307 struct dpp_configurator *conf;
2308
2309 conf = dpp_configurator_get_id(wpa_s, id);
2310 if (!conf)
2311 return -1;
2312
2313 return dpp_configurator_get_key(conf, buf, buflen);
2314}
2315
2316
a0d5c56f
JM
2317static void
2318wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
2319 unsigned int freq, const u8 *dst,
2320 const u8 *src, const u8 *bssid,
2321 const u8 *data, size_t data_len,
2322 enum offchannel_send_action_result result)
2323{
af48810b
JM
2324 const char *res_txt;
2325
2326 res_txt = result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
2327 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
2328 "FAILED");
a0d5c56f
JM
2329 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
2330 " result=%s (DPP Peer Discovery Request)",
af48810b
JM
2331 freq, MAC2STR(dst), res_txt);
2332 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX_STATUS "dst=" MACSTR
2333 " freq=%u result=%s", MAC2STR(dst), freq, res_txt);
a0d5c56f
JM
2334 /* TODO: Time out wait for response more quickly in error cases? */
2335}
2336
2337
2338int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2339 struct wpa_bss *bss)
2340{
2341 struct os_time now;
2342 struct wpabuf *msg;
2343 unsigned int wait_time;
2344
2345 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
2346 return 0; /* Not using DPP AKM - continue */
2347 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
2348 return 0; /* PMKSA exists for DPP AKM - continue */
2349
2350 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
2351 !ssid->dpp_csign) {
2352 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2353 "missing %s",
2354 !ssid->dpp_connector ? "Connector" :
2355 (!ssid->dpp_netaccesskey ? "netAccessKey" :
2356 "C-sign-key"));
2357 return -1;
2358 }
2359
2360 os_get_time(&now);
2361
a0d5c56f 2362 if (ssid->dpp_netaccesskey_expiry &&
3ca4be1e 2363 (os_time_t) ssid->dpp_netaccesskey_expiry < now.sec) {
a0d5c56f
JM
2364 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
2365 "netAccessKey expired");
2366 return -1;
2367 }
2368
2369 wpa_printf(MSG_DEBUG,
2370 "DPP: Starting network introduction protocol to derive PMKSA for "
2371 MACSTR, MAC2STR(bss->bssid));
2372
2373 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
85fd8263 2374 5 + 4 + os_strlen(ssid->dpp_connector));
a0d5c56f
JM
2375 if (!msg)
2376 return -1;
2377
a306ed5a
JM
2378#ifdef CONFIG_TESTING_OPTIONS
2379 if (dpp_test == DPP_TEST_NO_TRANSACTION_ID_PEER_DISC_REQ) {
2380 wpa_printf(MSG_INFO, "DPP: TESTING - no Transaction ID");
2381 goto skip_trans_id;
2382 }
55c6c858
JM
2383 if (dpp_test == DPP_TEST_INVALID_TRANSACTION_ID_PEER_DISC_REQ) {
2384 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Transaction ID");
2385 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2386 wpabuf_put_le16(msg, 0);
2387 goto skip_trans_id;
2388 }
a306ed5a
JM
2389#endif /* CONFIG_TESTING_OPTIONS */
2390
85fd8263
JM
2391 /* Transaction ID */
2392 wpabuf_put_le16(msg, DPP_ATTR_TRANSACTION_ID);
2393 wpabuf_put_le16(msg, 1);
2394 wpabuf_put_u8(msg, TRANSACTION_ID);
2395
a306ed5a
JM
2396#ifdef CONFIG_TESTING_OPTIONS
2397skip_trans_id:
2398 if (dpp_test == DPP_TEST_NO_CONNECTOR_PEER_DISC_REQ) {
2399 wpa_printf(MSG_INFO, "DPP: TESTING - no Connector");
2400 goto skip_connector;
2401 }
4b8de0c9
JM
2402 if (dpp_test == DPP_TEST_INVALID_CONNECTOR_PEER_DISC_REQ) {
2403 char *connector;
2404
2405 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Connector");
2406 connector = dpp_corrupt_connector_signature(
2407 ssid->dpp_connector);
2408 if (!connector) {
2409 wpabuf_free(msg);
2410 return -1;
2411 }
2412 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2413 wpabuf_put_le16(msg, os_strlen(connector));
2414 wpabuf_put_str(msg, connector);
2415 os_free(connector);
2416 goto skip_connector;
2417 }
a306ed5a
JM
2418#endif /* CONFIG_TESTING_OPTIONS */
2419
a0d5c56f
JM
2420 /* DPP Connector */
2421 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
2422 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
2423 wpabuf_put_str(msg, ssid->dpp_connector);
2424
a306ed5a
JM
2425#ifdef CONFIG_TESTING_OPTIONS
2426skip_connector:
2427#endif /* CONFIG_TESTING_OPTIONS */
2428
a0d5c56f
JM
2429 /* TODO: Timeout on AP response */
2430 wait_time = wpa_s->max_remain_on_chan;
2431 if (wait_time > 2000)
2432 wait_time = 2000;
af48810b
JM
2433 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR " freq=%u type=%d",
2434 MAC2STR(bss->bssid), bss->freq, DPP_PA_PEER_DISCOVERY_REQ);
a0d5c56f
JM
2435 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
2436 broadcast,
2437 wpabuf_head(msg), wpabuf_len(msg),
2438 wait_time, wpas_dpp_tx_introduction_status, 0);
2439 wpabuf_free(msg);
2440
2441 /* Request this connection attempt to terminate - new one will be
2442 * started when network introduction protocol completes */
2443 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
2444 wpa_s->dpp_intro_network = ssid;
2445 return 1;
2446}
2447
2448
500ed7f0
JM
2449int wpas_dpp_pkex_add(struct wpa_supplicant *wpa_s, const char *cmd)
2450{
2451 struct dpp_bootstrap_info *own_bi;
2452 const char *pos, *end;
2453 unsigned int wait_time;
2454
2455 pos = os_strstr(cmd, " own=");
2456 if (!pos)
2457 return -1;
2458 pos += 5;
2459 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
2460 if (!own_bi) {
2461 wpa_printf(MSG_DEBUG,
2462 "DPP: Identified bootstrap info not found");
2463 return -1;
2464 }
2465 if (own_bi->type != DPP_BOOTSTRAP_PKEX) {
2466 wpa_printf(MSG_DEBUG,
2467 "DPP: Identified bootstrap info not for PKEX");
2468 return -1;
2469 }
2470 wpa_s->dpp_pkex_bi = own_bi;
29ab69e4 2471 own_bi->pkex_t = 0; /* clear pending errors on new code */
500ed7f0
JM
2472
2473 os_free(wpa_s->dpp_pkex_identifier);
2474 wpa_s->dpp_pkex_identifier = NULL;
2475 pos = os_strstr(cmd, " identifier=");
2476 if (pos) {
2477 pos += 12;
2478 end = os_strchr(pos, ' ');
2479 if (!end)
2480 return -1;
2481 wpa_s->dpp_pkex_identifier = os_malloc(end - pos + 1);
2482 if (!wpa_s->dpp_pkex_identifier)
2483 return -1;
2484 os_memcpy(wpa_s->dpp_pkex_identifier, pos, end - pos);
2485 wpa_s->dpp_pkex_identifier[end - pos] = '\0';
2486 }
2487
2488 pos = os_strstr(cmd, " code=");
2489 if (!pos)
2490 return -1;
2491 os_free(wpa_s->dpp_pkex_code);
2492 wpa_s->dpp_pkex_code = os_strdup(pos + 6);
2493 if (!wpa_s->dpp_pkex_code)
2494 return -1;
2495
2496 if (os_strstr(cmd, " init=1")) {
00d2d13d 2497 struct dpp_pkex *pkex;
500ed7f0
JM
2498 struct wpabuf *msg;
2499
2500 wpa_printf(MSG_DEBUG, "DPP: Initiating PKEX");
2501 dpp_pkex_free(wpa_s->dpp_pkex);
219d4c9f 2502 wpa_s->dpp_pkex = dpp_pkex_init(wpa_s, own_bi, wpa_s->own_addr,
500ed7f0
JM
2503 wpa_s->dpp_pkex_identifier,
2504 wpa_s->dpp_pkex_code);
00d2d13d
JM
2505 pkex = wpa_s->dpp_pkex;
2506 if (!pkex)
500ed7f0
JM
2507 return -1;
2508
00d2d13d 2509 msg = pkex->exchange_req;
500ed7f0
JM
2510 wait_time = wpa_s->max_remain_on_chan;
2511 if (wait_time > 2000)
2512 wait_time = 2000;
00d2d13d 2513 pkex->freq = 2437;
af48810b
JM
2514 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_TX "dst=" MACSTR
2515 " freq=%u type=%d",
00d2d13d
JM
2516 MAC2STR(broadcast), pkex->freq,
2517 DPP_PA_PKEX_EXCHANGE_REQ);
2518 offchannel_send_action(wpa_s, pkex->freq, broadcast,
2519 wpa_s->own_addr, broadcast,
500ed7f0
JM
2520 wpabuf_head(msg), wpabuf_len(msg),
2521 wait_time, wpas_dpp_tx_pkex_status, 0);
00d2d13d
JM
2522 if (wait_time == 0)
2523 wait_time = 2000;
2524 pkex->exch_req_wait_time = wait_time;
2525 pkex->exch_req_tries = 1;
500ed7f0
JM
2526 }
2527
2528 /* TODO: Support multiple PKEX info entries */
2529
2530 os_free(wpa_s->dpp_pkex_auth_cmd);
2531 wpa_s->dpp_pkex_auth_cmd = os_strdup(cmd);
2532
2533 return 1;
2534}
2535
2536
2537int wpas_dpp_pkex_remove(struct wpa_supplicant *wpa_s, const char *id)
2538{
2539 unsigned int id_val;
2540
2541 if (os_strcmp(id, "*") == 0) {
2542 id_val = 0;
2543 } else {
2544 id_val = atoi(id);
2545 if (id_val == 0)
2546 return -1;
2547 }
2548
2549 if ((id_val != 0 && id_val != 1) || !wpa_s->dpp_pkex_code)
2550 return -1;
2551
2552 /* TODO: Support multiple PKEX entries */
2553 os_free(wpa_s->dpp_pkex_code);
2554 wpa_s->dpp_pkex_code = NULL;
2555 os_free(wpa_s->dpp_pkex_identifier);
2556 wpa_s->dpp_pkex_identifier = NULL;
2557 os_free(wpa_s->dpp_pkex_auth_cmd);
2558 wpa_s->dpp_pkex_auth_cmd = NULL;
2559 wpa_s->dpp_pkex_bi = NULL;
2560 /* TODO: Remove dpp_pkex only if it is for the identified PKEX code */
2561 dpp_pkex_free(wpa_s->dpp_pkex);
2562 wpa_s->dpp_pkex = NULL;
2563 return 0;
2564}
2565
2566
c1d37739
JM
2567void wpas_dpp_stop(struct wpa_supplicant *wpa_s)
2568{
2569 dpp_auth_deinit(wpa_s->dpp_auth);
2570 wpa_s->dpp_auth = NULL;
ed62d401
JM
2571 dpp_pkex_free(wpa_s->dpp_pkex);
2572 wpa_s->dpp_pkex = NULL;
1866dfb5
JM
2573 if (wpa_s->dpp_gas_client && wpa_s->dpp_gas_dialog_token >= 0)
2574 gas_query_stop(wpa_s->gas, wpa_s->dpp_gas_dialog_token);
c1d37739
JM
2575}
2576
2577
be27e185
JM
2578int wpas_dpp_init(struct wpa_supplicant *wpa_s)
2579{
461d39af
JM
2580 u8 adv_proto_id[7];
2581
2582 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
2583 adv_proto_id[1] = 5;
2584 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
2585 adv_proto_id[5] = DPP_OUI_TYPE;
2586 adv_proto_id[6] = 0x01;
2587
2588 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
2589 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
2590 wpas_dpp_gas_status_handler, wpa_s) < 0)
2591 return -1;
be27e185 2592 dl_list_init(&wpa_s->dpp_bootstrap);
461d39af 2593 dl_list_init(&wpa_s->dpp_configurator);
be27e185
JM
2594 wpa_s->dpp_init_done = 1;
2595 return 0;
2596}
2597
2598
2599void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
2600{
461d39af
JM
2601#ifdef CONFIG_TESTING_OPTIONS
2602 os_free(wpa_s->dpp_config_obj_override);
2603 wpa_s->dpp_config_obj_override = NULL;
2604 os_free(wpa_s->dpp_discovery_override);
2605 wpa_s->dpp_discovery_override = NULL;
2606 os_free(wpa_s->dpp_groups_override);
2607 wpa_s->dpp_groups_override = NULL;
461d39af
JM
2608 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
2609#endif /* CONFIG_TESTING_OPTIONS */
be27e185
JM
2610 if (!wpa_s->dpp_init_done)
2611 return;
00d2d13d 2612 eloop_cancel_timeout(wpas_dpp_pkex_retry_timeout, wpa_s, NULL);
30d27b04 2613 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
f97ace34 2614 eloop_cancel_timeout(wpas_dpp_init_timeout, wpa_s, NULL);
95b0104a 2615 eloop_cancel_timeout(wpas_dpp_auth_resp_retry_timeout, wpa_s, NULL);
30d27b04
JM
2616 offchannel_send_action_done(wpa_s);
2617 wpas_dpp_listen_stop(wpa_s);
be27e185 2618 dpp_bootstrap_del(wpa_s, 0);
461d39af 2619 dpp_configurator_del(wpa_s, 0);
fa5c9074 2620 wpas_dpp_stop(wpa_s);
500ed7f0 2621 wpas_dpp_pkex_remove(wpa_s, "*");
a0d5c56f 2622 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
b65b22d6
JM
2623 os_free(wpa_s->dpp_configurator_params);
2624 wpa_s->dpp_configurator_params = NULL;
be27e185 2625}