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