]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/dpp_supplicant.c
DPP: Add helper functions for running hash operations
[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
41
461d39af
JM
42static struct dpp_configurator *
43dpp_configurator_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
44{
45 struct dpp_configurator *conf;
46
47 dl_list_for_each(conf, &wpa_s->dpp_configurator,
48 struct dpp_configurator, list) {
49 if (conf->id == id)
50 return conf;
51 }
52 return NULL;
53}
54
55
be27e185
JM
56static unsigned int wpas_dpp_next_id(struct wpa_supplicant *wpa_s)
57{
58 struct dpp_bootstrap_info *bi;
59 unsigned int max_id = 0;
60
61 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
62 list) {
63 if (bi->id > max_id)
64 max_id = bi->id;
65 }
66 return max_id + 1;
67}
68
69
70/**
71 * wpas_dpp_qr_code - Parse and add DPP bootstrapping info from a QR Code
72 * @wpa_s: Pointer to wpa_supplicant data
73 * @cmd: DPP URI read from a QR Code
74 * Returns: Identifier of the stored info or -1 on failure
75 */
76int wpas_dpp_qr_code(struct wpa_supplicant *wpa_s, const char *cmd)
77{
78 struct dpp_bootstrap_info *bi;
30d27b04 79 struct dpp_authentication *auth = wpa_s->dpp_auth;
be27e185
JM
80
81 bi = dpp_parse_qr_code(cmd);
82 if (!bi)
83 return -1;
84
85 bi->id = wpas_dpp_next_id(wpa_s);
86 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
87
30d27b04
JM
88 if (auth && auth->response_pending &&
89 dpp_notify_new_qr_code(auth, bi) == 1) {
90 struct wpabuf *msg;
91
92 wpa_printf(MSG_DEBUG,
93 "DPP: Sending out pending authentication response");
94 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP,
95 wpabuf_len(auth->resp_attr));
96 if (!msg)
97 goto out;
98 wpabuf_put_buf(msg, wpa_s->dpp_auth->resp_attr);
99
100 offchannel_send_action(wpa_s, auth->curr_freq,
101 auth->peer_mac_addr, wpa_s->own_addr,
102 broadcast,
103 wpabuf_head(msg), wpabuf_len(msg),
104 500, wpas_dpp_tx_status, 0);
105 wpabuf_free(msg);
106 }
107
108out:
be27e185
JM
109 return bi->id;
110}
111
112
113static char * get_param(const char *cmd, const char *param)
114{
115 const char *pos, *end;
116 char *val;
117 size_t len;
118
119 pos = os_strstr(cmd, param);
120 if (!pos)
121 return NULL;
122
123 pos += os_strlen(param);
124 end = os_strchr(pos, ' ');
125 if (end)
126 len = end - pos;
127 else
128 len = os_strlen(pos);
129 val = os_malloc(len + 1);
130 if (!val)
131 return NULL;
132 os_memcpy(val, pos, len);
133 val[len] = '\0';
134 return val;
135}
136
137
138int wpas_dpp_bootstrap_gen(struct wpa_supplicant *wpa_s, const char *cmd)
139{
140 char *chan = NULL, *mac = NULL, *info = NULL, *pk = NULL, *curve = NULL;
141 char *key = NULL;
142 u8 *privkey = NULL;
143 size_t privkey_len = 0;
144 size_t len;
145 int ret = -1;
146 struct dpp_bootstrap_info *bi;
147
148 bi = os_zalloc(sizeof(*bi));
149 if (!bi)
150 goto fail;
151
152 if (os_strstr(cmd, "type=qrcode"))
153 bi->type = DPP_BOOTSTRAP_QR_CODE;
154 else
155 goto fail;
156
157 chan = get_param(cmd, " chan=");
158 mac = get_param(cmd, " mac=");
159 info = get_param(cmd, " info=");
160 curve = get_param(cmd, " curve=");
161 key = get_param(cmd, " key=");
162
163 if (key) {
164 privkey_len = os_strlen(key) / 2;
165 privkey = os_malloc(privkey_len);
166 if (!privkey ||
167 hexstr2bin(key, privkey, privkey_len) < 0)
168 goto fail;
169 }
170
171 pk = dpp_keygen(bi, curve, privkey, privkey_len);
172 if (!pk)
173 goto fail;
174
175 len = 4; /* "DPP:" */
176 if (chan) {
177 if (dpp_parse_uri_chan_list(bi, chan) < 0)
178 goto fail;
179 len += 3 + os_strlen(chan); /* C:...; */
180 }
181 if (mac) {
182 if (dpp_parse_uri_mac(bi, mac) < 0)
183 goto fail;
184 len += 3 + os_strlen(mac); /* M:...; */
185 }
186 if (info) {
187 if (dpp_parse_uri_info(bi, info) < 0)
188 goto fail;
189 len += 3 + os_strlen(info); /* I:...; */
190 }
191 len += 4 + os_strlen(pk);
192 bi->uri = os_malloc(len + 1);
193 if (!bi->uri)
194 goto fail;
195 os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
196 chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
197 mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
198 info ? "I:" : "", info ? info : "", info ? ";" : "",
199 pk);
200 bi->id = wpas_dpp_next_id(wpa_s);
201 dl_list_add(&wpa_s->dpp_bootstrap, &bi->list);
202 ret = bi->id;
203 bi = NULL;
204fail:
205 os_free(curve);
206 os_free(pk);
207 os_free(chan);
208 os_free(mac);
209 os_free(info);
210 str_clear_free(key);
211 bin_clear_free(privkey, privkey_len);
212 dpp_bootstrap_info_free(bi);
213 return ret;
214}
215
216
217static struct dpp_bootstrap_info *
218dpp_bootstrap_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
219{
220 struct dpp_bootstrap_info *bi;
221
222 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
223 list) {
224 if (bi->id == id)
225 return bi;
226 }
227 return NULL;
228}
229
230
231static int dpp_bootstrap_del(struct wpa_supplicant *wpa_s, unsigned int id)
232{
233 struct dpp_bootstrap_info *bi, *tmp;
234 int found = 0;
235
236 dl_list_for_each_safe(bi, tmp, &wpa_s->dpp_bootstrap,
237 struct dpp_bootstrap_info, list) {
238 if (id && bi->id != id)
239 continue;
240 found = 1;
241 dl_list_del(&bi->list);
242 dpp_bootstrap_info_free(bi);
243 }
244
245 if (id == 0)
246 return 0; /* flush succeeds regardless of entries found */
247 return found ? 0 : -1;
248}
249
250
251int wpas_dpp_bootstrap_remove(struct wpa_supplicant *wpa_s, const char *id)
252{
253 unsigned int id_val;
254
255 if (os_strcmp(id, "*") == 0) {
256 id_val = 0;
257 } else {
258 id_val = atoi(id);
259 if (id_val == 0)
260 return -1;
261 }
262
263 return dpp_bootstrap_del(wpa_s, id_val);
264}
265
266
267const char * wpas_dpp_bootstrap_get_uri(struct wpa_supplicant *wpa_s,
268 unsigned int id)
269{
270 struct dpp_bootstrap_info *bi;
271
272 bi = dpp_bootstrap_get_id(wpa_s, id);
273 if (!bi)
274 return NULL;
275 return bi->uri;
276}
277
278
6a7182a9
JM
279static const char * wpas_dpp_bootstrap_type(enum dpp_bootstrap_type type)
280{
281 switch (type) {
282 case DPP_BOOTSTRAP_QR_CODE:
283 return "QRCODE";
284 }
285 return "??";
286}
287
288
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",
302 wpas_dpp_bootstrap_type(bi->type),
303 MAC2STR(bi->mac_addr),
304 bi->info ? bi->info : "",
305 bi->num_freq,
306 bi->curve->name);
307}
308
309
30d27b04
JM
310static void wpas_dpp_tx_status(struct wpa_supplicant *wpa_s,
311 unsigned int freq, const u8 *dst,
312 const u8 *src, const u8 *bssid,
313 const u8 *data, size_t data_len,
314 enum offchannel_send_action_result result)
315{
316 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
317 " result=%s",
318 freq, MAC2STR(dst),
319 result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
320 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
321 "FAILED"));
322
323 if (!wpa_s->dpp_auth) {
324 wpa_printf(MSG_DEBUG,
325 "DPP: Ignore TX status since there is no ongoing authentication exchange");
326 return;
327 }
328
329 if (wpa_s->dpp_auth->remove_on_tx_status) {
330 wpa_printf(MSG_DEBUG,
331 "DPP: Terminate authentication exchange due to an earlier error");
461d39af
JM
332 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
333 offchannel_send_action_done(wpa_s);
30d27b04
JM
334 dpp_auth_deinit(wpa_s->dpp_auth);
335 wpa_s->dpp_auth = NULL;
336 return;
337 }
338
461d39af
JM
339 if (wpa_s->dpp_auth_ok_on_ack)
340 wpas_dpp_auth_success(wpa_s, 1);
341
30d27b04
JM
342 if (!is_broadcast_ether_addr(dst) &&
343 result != OFFCHANNEL_SEND_ACTION_SUCCESS) {
344 wpa_printf(MSG_DEBUG,
345 "DPP: Unicast DPP Action frame was not ACKed");
346 /* TODO: In case of DPP Authentication Request frame, move to
347 * the next channel immediately */
348 }
349}
350
351
352static void wpas_dpp_reply_wait_timeout(void *eloop_ctx, void *timeout_ctx)
353{
354 struct wpa_supplicant *wpa_s = eloop_ctx;
355
356 if (!wpa_s->dpp_auth)
357 return;
358 wpa_printf(MSG_DEBUG, "DPP: Continue reply wait on channel %u MHz",
359 wpa_s->dpp_auth->curr_freq);
360 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->curr_freq);
361}
362
363
461d39af
JM
364static void wpas_dpp_set_testing_options(struct wpa_supplicant *wpa_s,
365 struct dpp_authentication *auth)
366{
367#ifdef CONFIG_TESTING_OPTIONS
368 if (wpa_s->dpp_config_obj_override)
369 auth->config_obj_override =
370 os_strdup(wpa_s->dpp_config_obj_override);
371 if (wpa_s->dpp_discovery_override)
372 auth->discovery_override =
373 os_strdup(wpa_s->dpp_discovery_override);
374 if (wpa_s->dpp_groups_override)
375 auth->groups_override =
376 os_strdup(wpa_s->dpp_groups_override);
377 if (wpa_s->dpp_devices_override)
378 auth->devices_override =
379 os_strdup(wpa_s->dpp_devices_override);
380 auth->ignore_netaccesskey_mismatch =
381 wpa_s->dpp_ignore_netaccesskey_mismatch;
382#endif /* CONFIG_TESTING_OPTIONS */
383}
384
385
30d27b04
JM
386int wpas_dpp_auth_init(struct wpa_supplicant *wpa_s, const char *cmd)
387{
68cb6dce 388 const char *pos, *end;
30d27b04
JM
389 struct dpp_bootstrap_info *peer_bi, *own_bi = NULL;
390 struct wpabuf *msg;
391 const u8 *dst;
392 int res;
393 int configurator = 1;
394 unsigned int wait_time;
461d39af
JM
395 struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
396 struct dpp_configurator *conf = NULL;
68cb6dce
JM
397 u8 ssid[32] = { "test" };
398 size_t ssid_len = 4;
35f06421
JM
399 char pass[64] = { };
400 size_t pass_len = 0;
461d39af
JM
401
402 wpa_s->dpp_gas_client = 0;
30d27b04
JM
403
404 pos = os_strstr(cmd, " peer=");
405 if (!pos)
406 return -1;
407 pos += 6;
408 peer_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
409 if (!peer_bi) {
410 wpa_printf(MSG_INFO,
411 "DPP: Could not find bootstrapping info for the identified peer");
412 return -1;
413 }
414
415 pos = os_strstr(cmd, " own=");
416 if (pos) {
417 pos += 5;
418 own_bi = dpp_bootstrap_get_id(wpa_s, atoi(pos));
419 if (!own_bi) {
420 wpa_printf(MSG_INFO,
421 "DPP: Could not find bootstrapping info for the identified local entry");
422 return -1;
423 }
424
425 if (peer_bi->curve != own_bi->curve) {
426 wpa_printf(MSG_INFO,
427 "DPP: Mismatching curves in bootstrapping info (peer=%s own=%s)",
428 peer_bi->curve->name, own_bi->curve->name);
429 return -1;
430 }
431 }
432
433 pos = os_strstr(cmd, " role=");
434 if (pos) {
435 pos += 6;
436 if (os_strncmp(pos, "configurator", 12) == 0)
437 configurator = 1;
438 else if (os_strncmp(pos, "enrollee", 8) == 0)
439 configurator = 0;
440 else
461d39af
JM
441 goto fail;
442 }
443
444 pos = os_strstr(cmd, " netrole=");
445 if (pos) {
446 pos += 9;
447 wpa_s->dpp_netrole_ap = os_strncmp(pos, "ap", 2) == 0;
448 }
449
68cb6dce
JM
450 pos = os_strstr(cmd, " ssid=");
451 if (pos) {
452 pos += 6;
453 end = os_strchr(pos, ' ');
454 ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
455 ssid_len /= 2;
456 if (ssid_len > sizeof(ssid) ||
457 hexstr2bin(pos, ssid, ssid_len) < 0)
458 goto fail;
459 }
460
35f06421
JM
461 pos = os_strstr(cmd, " pass=");
462 if (pos) {
463 pos += 6;
464 end = os_strchr(pos, ' ');
465 pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
466 pass_len /= 2;
467 if (pass_len > sizeof(pass) - 1 || pass_len < 8 ||
468 hexstr2bin(pos, (u8 *) pass, pass_len) < 0)
469 goto fail;
470 }
471
461d39af
JM
472 if (os_strstr(cmd, " conf=sta-")) {
473 conf_sta = os_zalloc(sizeof(struct dpp_configuration));
474 if (!conf_sta)
475 goto fail;
68cb6dce
JM
476 os_memcpy(conf_sta->ssid, ssid, ssid_len);
477 conf_sta->ssid_len = ssid_len;
461d39af
JM
478 if (os_strstr(cmd, " conf=sta-psk")) {
479 conf_sta->dpp = 0;
35f06421 480 conf_sta->passphrase = os_strdup(pass);
461d39af
JM
481 if (!conf_sta->passphrase)
482 goto fail;
483 } else if (os_strstr(cmd, " conf=sta-dpp")) {
484 conf_sta->dpp = 1;
485 } else {
486 goto fail;
487 }
488 }
489
490 if (os_strstr(cmd, " conf=ap-")) {
491 conf_ap = os_zalloc(sizeof(struct dpp_configuration));
492 if (!conf_ap)
493 goto fail;
68cb6dce
JM
494 os_memcpy(conf_ap->ssid, ssid, ssid_len);
495 conf_ap->ssid_len = ssid_len;
461d39af
JM
496 if (os_strstr(cmd, " conf=ap-psk")) {
497 conf_ap->dpp = 0;
35f06421 498 conf_ap->passphrase = os_strdup(pass);
461d39af
JM
499 if (!conf_ap->passphrase)
500 goto fail;
501 } else if (os_strstr(cmd, " conf=ap-dpp")) {
502 conf_ap->dpp = 1;
503 } else {
504 goto fail;
505 }
506 }
507
508 pos = os_strstr(cmd, " expiry=");
509 if (pos) {
510 long int val;
511
512 pos += 8;
513 val = strtol(pos, NULL, 0);
514 if (val <= 0)
515 goto fail;
516 if (conf_sta)
517 conf_sta->netaccesskey_expiry = val;
518 if (conf_ap)
519 conf_ap->netaccesskey_expiry = val;
520 }
521
522 pos = os_strstr(cmd, " configurator=");
523 if (pos) {
524 pos += 14;
525 conf = dpp_configurator_get_id(wpa_s, atoi(pos));
526 if (!conf) {
527 wpa_printf(MSG_INFO,
528 "DPP: Could not find the specified configurator");
529 goto fail;
530 }
30d27b04
JM
531 }
532
533 if (wpa_s->dpp_auth) {
534 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
535 offchannel_send_action_done(wpa_s);
536 dpp_auth_deinit(wpa_s->dpp_auth);
537 }
538 wpa_s->dpp_auth = dpp_auth_init(wpa_s, peer_bi, own_bi, configurator);
539 if (!wpa_s->dpp_auth)
461d39af
JM
540 goto fail;
541 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
542 wpa_s->dpp_auth->conf_sta = conf_sta;
543 wpa_s->dpp_auth->conf_ap = conf_ap;
544 wpa_s->dpp_auth->conf = conf;
30d27b04
JM
545
546 /* TODO: Support iteration over all frequencies and filtering of
547 * frequencies based on locally enabled channels that allow initiation
548 * of transmission. */
549 if (peer_bi->num_freq > 0)
550 wpa_s->dpp_auth->curr_freq = peer_bi->freq[0];
551 else
552 wpa_s->dpp_auth->curr_freq = 2412;
553
554 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_REQ,
555 wpabuf_len(wpa_s->dpp_auth->req_attr));
556 if (!msg)
557 return -1;
558 wpabuf_put_buf(msg, wpa_s->dpp_auth->req_attr);
559
560 if (is_zero_ether_addr(peer_bi->mac_addr)) {
561 dst = broadcast;
562 } else {
563 dst = peer_bi->mac_addr;
564 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, peer_bi->mac_addr,
565 ETH_ALEN);
566 }
461d39af 567 wpa_s->dpp_auth_ok_on_ack = 0;
30d27b04
JM
568 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
569 wait_time = wpa_s->max_remain_on_chan;
570 if (wait_time > 2000)
571 wait_time = 2000;
572 eloop_register_timeout(wait_time / 1000, (wait_time % 1000) * 1000,
573 wpas_dpp_reply_wait_timeout,
574 wpa_s, NULL);
575 res = offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
576 dst, wpa_s->own_addr, broadcast,
577 wpabuf_head(msg), wpabuf_len(msg),
578 wait_time, wpas_dpp_tx_status, 0);
579 wpabuf_free(msg);
580
581 return res;
461d39af
JM
582fail:
583 dpp_configuration_free(conf_sta);
584 dpp_configuration_free(conf_ap);
585 return -1;
30d27b04
JM
586}
587
588
589struct wpas_dpp_listen_work {
590 unsigned int freq;
591 unsigned int duration;
592 struct wpabuf *probe_resp_ie;
593};
594
595
596static void wpas_dpp_listen_work_free(struct wpas_dpp_listen_work *lwork)
597{
598 if (!lwork)
599 return;
600 os_free(lwork);
601}
602
603
604static void wpas_dpp_listen_work_done(struct wpa_supplicant *wpa_s)
605{
606 struct wpas_dpp_listen_work *lwork;
607
608 if (!wpa_s->dpp_listen_work)
609 return;
610
611 lwork = wpa_s->dpp_listen_work->ctx;
612 wpas_dpp_listen_work_free(lwork);
613 radio_work_done(wpa_s->dpp_listen_work);
614 wpa_s->dpp_listen_work = NULL;
615}
616
617
618static void dpp_start_listen_cb(struct wpa_radio_work *work, int deinit)
619{
620 struct wpa_supplicant *wpa_s = work->wpa_s;
621 struct wpas_dpp_listen_work *lwork = work->ctx;
622
623 if (deinit) {
624 if (work->started) {
625 wpa_s->dpp_listen_work = NULL;
626 wpas_dpp_listen_stop(wpa_s);
627 }
628 wpas_dpp_listen_work_free(lwork);
629 return;
630 }
631
632 wpa_s->dpp_listen_work = work;
633
634 wpa_s->dpp_pending_listen_freq = lwork->freq;
635
636 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq,
637 wpa_s->max_remain_on_chan) < 0) {
638 wpa_printf(MSG_DEBUG,
639 "DPP: Failed to request the driver to remain on channel (%u MHz) for listen",
640 lwork->freq);
641 wpas_dpp_listen_work_done(wpa_s);
642 wpa_s->dpp_pending_listen_freq = 0;
643 return;
644 }
645 wpa_s->off_channel_freq = 0;
646 wpa_s->roc_waiting_drv_freq = lwork->freq;
647}
648
649
650static int wpas_dpp_listen_start(struct wpa_supplicant *wpa_s,
651 unsigned int freq)
652{
653 struct wpas_dpp_listen_work *lwork;
654
655 if (wpa_s->dpp_listen_work) {
656 wpa_printf(MSG_DEBUG,
657 "DPP: Reject start_listen since dpp_listen_work already exists");
658 return -1;
659 }
660
661 if (wpa_s->dpp_listen_freq)
662 wpas_dpp_listen_stop(wpa_s);
663 wpa_s->dpp_listen_freq = freq;
664
665 lwork = os_zalloc(sizeof(*lwork));
666 if (!lwork)
667 return -1;
668 lwork->freq = freq;
669
670 if (radio_add_work(wpa_s, freq, "dpp-listen", 0, dpp_start_listen_cb,
671 lwork) < 0) {
672 wpas_dpp_listen_work_free(lwork);
673 return -1;
674 }
675
676 return 0;
677}
678
679
680int wpas_dpp_listen(struct wpa_supplicant *wpa_s, const char *cmd)
681{
682 int freq;
683
684 freq = atoi(cmd);
685 if (freq <= 0)
686 return -1;
687
688 if (os_strstr(cmd, " role=configurator"))
689 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR;
690 else if (os_strstr(cmd, " role=enrollee"))
691 wpa_s->dpp_allowed_roles = DPP_CAPAB_ENROLLEE;
692 else
693 wpa_s->dpp_allowed_roles = DPP_CAPAB_CONFIGURATOR |
694 DPP_CAPAB_ENROLLEE;
695 wpa_s->dpp_qr_mutual = os_strstr(cmd, " qr=mutual") != NULL;
461d39af 696 wpa_s->dpp_netrole_ap = os_strstr(cmd, " netrole=ap") != NULL;
30d27b04
JM
697 if (wpa_s->dpp_listen_freq == (unsigned int) freq) {
698 wpa_printf(MSG_DEBUG, "DPP: Already listening on %u MHz",
699 freq);
700 return 0;
701 }
702
703 return wpas_dpp_listen_start(wpa_s, freq);
704}
705
706
707void wpas_dpp_listen_stop(struct wpa_supplicant *wpa_s)
708{
709 if (!wpa_s->dpp_listen_freq)
710 return;
711
712 wpa_printf(MSG_DEBUG, "DPP: Stop listen on %u MHz",
713 wpa_s->dpp_listen_freq);
714 wpa_drv_cancel_remain_on_channel(wpa_s);
715 wpa_s->dpp_listen_freq = 0;
716 wpas_dpp_listen_work_done(wpa_s);
717}
718
719
720void wpas_dpp_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
721 unsigned int freq)
722{
723 if (!wpa_s->dpp_listen_freq && !wpa_s->dpp_pending_listen_freq)
724 return;
725
726 wpa_printf(MSG_DEBUG,
727 "DPP: remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u)",
728 wpa_s->off_channel_freq, wpa_s->dpp_pending_listen_freq,
729 wpa_s->roc_waiting_drv_freq, freq);
730 if (wpa_s->off_channel_freq &&
731 wpa_s->off_channel_freq == wpa_s->dpp_pending_listen_freq) {
732 wpa_printf(MSG_DEBUG, "DPP: Listen on %u MHz started", freq);
733 wpa_s->dpp_pending_listen_freq = 0;
734 } else {
735 wpa_printf(MSG_DEBUG,
736 "DPP: Ignore remain-on-channel callback (off_channel_freq=%u dpp_pending_listen_freq=%d freq=%u)",
737 wpa_s->off_channel_freq,
738 wpa_s->dpp_pending_listen_freq, freq);
739 }
740}
741
742
743void wpas_dpp_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
744 unsigned int freq)
745{
746 wpas_dpp_listen_work_done(wpa_s);
747
461d39af 748 if (wpa_s->dpp_auth && !wpa_s->dpp_gas_client) {
30d27b04
JM
749 /* Continue listen with a new remain-on-channel */
750 wpa_printf(MSG_DEBUG,
751 "DPP: Continue wait on %u MHz for the ongoing DPP provisioning session",
752 wpa_s->dpp_auth->curr_freq);
753 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_auth->curr_freq);
754 return;
755 }
756
757 if (wpa_s->dpp_listen_freq) {
758 /* Continue listen with a new remain-on-channel */
759 wpas_dpp_listen_start(wpa_s, wpa_s->dpp_listen_freq);
760 }
761}
762
763
764static void wpas_dpp_rx_auth_req(struct wpa_supplicant *wpa_s, const u8 *src,
765 const u8 *buf, size_t len, unsigned int freq)
766{
767 const u8 *r_bootstrap, *i_bootstrap, *wrapped_data;
768 u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len;
769 struct dpp_bootstrap_info *bi, *own_bi = NULL, *peer_bi = NULL;
770 struct wpabuf *msg;
771
772 wpa_printf(MSG_DEBUG, "DPP: Authentication Request from " MACSTR,
773 MAC2STR(src));
774
775 wrapped_data = dpp_get_attr(buf, len, DPP_ATTR_WRAPPED_DATA,
776 &wrapped_data_len);
777 if (!wrapped_data) {
778 wpa_printf(MSG_DEBUG,
779 "DPP: Missing required Wrapped data attribute");
780 return;
781 }
782 wpa_hexdump(MSG_MSGDUMP, "DPP: Wrapped data",
783 wrapped_data, wrapped_data_len);
784
785 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
786 &r_bootstrap_len);
787 if (!r_bootstrap || r_bootstrap > wrapped_data ||
788 r_bootstrap_len != SHA256_MAC_LEN) {
789 wpa_printf(MSG_DEBUG,
790 "DPP: Missing or invalid required Responder Bootstrapping Key Hash attribute");
791 return;
792 }
793 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
794 r_bootstrap, r_bootstrap_len);
795
796 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
797 &i_bootstrap_len);
798 if (!i_bootstrap || i_bootstrap > wrapped_data ||
799 i_bootstrap_len != SHA256_MAC_LEN) {
800 wpa_printf(MSG_DEBUG,
801 "DPP: Missing or invalid required Initiator Bootstrapping Key Hash attribute");
802 return;
803 }
804 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
805 i_bootstrap, i_bootstrap_len);
806
807 /* Try to find own and peer bootstrapping key matches based on the
808 * received hash values */
809 dl_list_for_each(bi, &wpa_s->dpp_bootstrap, struct dpp_bootstrap_info,
810 list) {
811 if (!own_bi && bi->own &&
812 os_memcmp(bi->pubkey_hash, r_bootstrap,
813 SHA256_MAC_LEN) == 0) {
814 wpa_printf(MSG_DEBUG,
815 "DPP: Found matching own bootstrapping information");
816 own_bi = bi;
817 }
818
819 if (!peer_bi && !bi->own &&
820 os_memcmp(bi->pubkey_hash, i_bootstrap,
821 SHA256_MAC_LEN) == 0) {
822 wpa_printf(MSG_DEBUG,
823 "DPP: Found matching peer bootstrapping information");
824 peer_bi = bi;
825 }
826
827 if (own_bi && peer_bi)
828 break;
829 }
830
831 if (!own_bi) {
832 wpa_printf(MSG_DEBUG,
833 "DPP: No matching own bootstrapping key found - ignore message");
834 return;
835 }
836
837 if (wpa_s->dpp_auth) {
838 wpa_printf(MSG_DEBUG,
839 "DPP: Already in DPP authentication exchange - ignore new one");
840 return;
841 }
842
461d39af
JM
843 wpa_s->dpp_gas_client = 0;
844 wpa_s->dpp_auth_ok_on_ack = 0;
30d27b04
JM
845 wpa_s->dpp_auth = dpp_auth_req_rx(wpa_s, wpa_s->dpp_allowed_roles,
846 wpa_s->dpp_qr_mutual,
847 peer_bi, own_bi, freq, buf,
848 wrapped_data, wrapped_data_len);
849 if (!wpa_s->dpp_auth) {
850 wpa_printf(MSG_DEBUG, "DPP: No response generated");
851 return;
852 }
461d39af 853 wpas_dpp_set_testing_options(wpa_s, wpa_s->dpp_auth);
30d27b04
JM
854 os_memcpy(wpa_s->dpp_auth->peer_mac_addr, src, ETH_ALEN);
855
856 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP,
857 wpabuf_len(wpa_s->dpp_auth->resp_attr));
858 if (!msg)
859 return;
860 wpabuf_put_buf(msg, wpa_s->dpp_auth->resp_attr);
861
862 offchannel_send_action(wpa_s, wpa_s->dpp_auth->curr_freq,
863 src, wpa_s->own_addr, broadcast,
864 wpabuf_head(msg), wpabuf_len(msg),
865 500, wpas_dpp_tx_status, 0);
866 wpabuf_free(msg);
867}
868
869
461d39af
JM
870static void wpas_dpp_start_gas_server(struct wpa_supplicant *wpa_s)
871{
872 /* TODO: stop wait and start ROC */
873}
874
875
8528994e
JM
876static struct wpa_ssid * wpas_dpp_add_network(struct wpa_supplicant *wpa_s,
877 struct dpp_authentication *auth)
878{
879 struct wpa_ssid *ssid;
880
881 ssid = wpa_config_add_network(wpa_s->conf);
882 if (!ssid)
883 return NULL;
884 wpas_notify_network_added(wpa_s, ssid);
885 wpa_config_set_network_defaults(ssid);
886 ssid->disabled = 1;
887
888 ssid->ssid = os_malloc(auth->ssid_len);
889 if (!ssid->ssid)
890 goto fail;
891 os_memcpy(ssid->ssid, auth->ssid, auth->ssid_len);
892 ssid->ssid_len = auth->ssid_len;
893
894 if (auth->connector) {
895 ssid->key_mgmt = WPA_KEY_MGMT_DPP;
896 ssid->dpp_connector = os_strdup(auth->connector);
897 if (!ssid->dpp_connector)
898 goto fail;
899 }
900
901 if (auth->c_sign_key) {
902 ssid->dpp_csign = os_malloc(wpabuf_len(auth->c_sign_key));
903 if (!ssid->dpp_csign)
904 goto fail;
905 os_memcpy(ssid->dpp_csign, wpabuf_head(auth->c_sign_key),
906 wpabuf_len(auth->c_sign_key));
907 ssid->dpp_csign_len = wpabuf_len(auth->c_sign_key);
908 ssid->dpp_csign_expiry = auth->c_sign_key_expiry;
909 }
910
911 if (auth->net_access_key) {
912 ssid->dpp_netaccesskey =
913 os_malloc(wpabuf_len(auth->net_access_key));
914 if (!ssid->dpp_netaccesskey)
915 goto fail;
916 os_memcpy(ssid->dpp_netaccesskey,
917 wpabuf_head(auth->net_access_key),
918 wpabuf_len(auth->net_access_key));
919 ssid->dpp_netaccesskey_len = wpabuf_len(auth->net_access_key);
920 ssid->dpp_netaccesskey_expiry = auth->net_access_key_expiry;
921 }
922
923 if (!auth->connector) {
924 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
925 if (auth->passphrase[0]) {
926 if (wpa_config_set_quoted(ssid, "psk",
927 auth->passphrase) < 0)
928 goto fail;
929 wpa_config_update_psk(ssid);
930 ssid->export_keys = 1;
931 } else {
932 ssid->psk_set = auth->psk_set;
933 os_memcpy(ssid->psk, auth->psk, PMK_LEN);
934 }
935 }
936
937 return ssid;
938fail:
939 wpas_notify_network_removed(wpa_s, ssid);
940 wpa_config_remove_network(wpa_s->conf, ssid->id);
941 return NULL;
942}
943
944
945static void wpas_dpp_process_config(struct wpa_supplicant *wpa_s,
946 struct dpp_authentication *auth)
947{
948 struct wpa_ssid *ssid;
949
950 if (wpa_s->conf->dpp_config_processing < 1)
951 return;
952
953 ssid = wpas_dpp_add_network(wpa_s, auth);
954 if (!ssid)
955 return;
956
957 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_NETWORK_ID "%d", ssid->id);
958 if (wpa_s->conf->dpp_config_processing < 2)
959 return;
960
961 wpa_printf(MSG_DEBUG, "DPP: Trying to connect to the new network");
962 ssid->disabled = 0;
963 wpa_s->disconnected = 0;
964 wpa_s->reassociate = 1;
965 wpa_s->scan_runs = 0;
966 wpa_s->normal_scans = 0;
967 wpa_supplicant_cancel_sched_scan(wpa_s);
968 wpa_supplicant_req_scan(wpa_s, 0, 0);
969}
970
971
461d39af
JM
972static void wpas_dpp_gas_resp_cb(void *ctx, const u8 *addr, u8 dialog_token,
973 enum gas_query_result result,
974 const struct wpabuf *adv_proto,
975 const struct wpabuf *resp, u16 status_code)
976{
977 struct wpa_supplicant *wpa_s = ctx;
978 const u8 *pos;
979 struct dpp_authentication *auth = wpa_s->dpp_auth;
980
981 if (!auth || !auth->auth_success) {
982 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
983 return;
984 }
985 if (!resp || status_code != WLAN_STATUS_SUCCESS) {
986 wpa_printf(MSG_DEBUG, "DPP: GAS query did not succeed");
987 goto fail;
988 }
989
990 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response adv_proto",
991 adv_proto);
992 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Response (GAS response)",
993 resp);
994
995 if (wpabuf_len(adv_proto) != 10 ||
996 !(pos = wpabuf_head(adv_proto)) ||
997 pos[0] != WLAN_EID_ADV_PROTO ||
998 pos[1] != 8 ||
999 pos[3] != WLAN_EID_VENDOR_SPECIFIC ||
1000 pos[4] != 5 ||
1001 WPA_GET_BE24(&pos[5]) != OUI_WFA ||
1002 pos[8] != 0x1a ||
1003 pos[9] != 1) {
1004 wpa_printf(MSG_DEBUG,
1005 "DPP: Not a DPP Advertisement Protocol ID");
1006 goto fail;
1007 }
1008
1009 if (dpp_conf_resp_rx(auth, resp) < 0) {
1010 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
1011 goto fail;
1012 }
1013
1014 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_RECEIVED);
1015 if (auth->ssid_len)
1016 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONFOBJ_SSID "%s",
1017 wpa_ssid_txt(auth->ssid, auth->ssid_len));
1018 if (auth->connector) {
1019 /* TODO: Save the Connector and consider using a command
1020 * to fetch the value instead of sending an event with
1021 * it. The Connector could end up being larger than what
1022 * most clients are ready to receive as an event
1023 * message. */
1024 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONNECTOR "%s",
1025 auth->connector);
1026 }
1027 if (auth->c_sign_key) {
1028 char *hex;
1029 size_t hexlen;
1030
1031 hexlen = 2 * wpabuf_len(auth->c_sign_key) + 1;
1032 hex = os_malloc(hexlen);
1033 if (hex) {
1034 wpa_snprintf_hex(hex, hexlen,
1035 wpabuf_head(auth->c_sign_key),
1036 wpabuf_len(auth->c_sign_key));
1037 if (auth->c_sign_key_expiry)
1038 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY
1039 "%s %lu", hex,
1040 (long unsigned)
1041 auth->c_sign_key_expiry);
1042 else
1043 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_C_SIGN_KEY
1044 "%s", hex);
1045 os_free(hex);
1046 }
1047 }
1048 if (auth->net_access_key) {
1049 char *hex;
1050 size_t hexlen;
1051
1052 hexlen = 2 * wpabuf_len(auth->net_access_key) + 1;
1053 hex = os_malloc(hexlen);
1054 if (hex) {
1055 wpa_snprintf_hex(hex, hexlen,
1056 wpabuf_head(auth->net_access_key),
1057 wpabuf_len(auth->net_access_key));
1058 if (auth->net_access_key_expiry)
1059 wpa_msg(wpa_s, MSG_INFO,
1060 DPP_EVENT_NET_ACCESS_KEY "%s %lu", hex,
1061 (long unsigned)
1062 auth->net_access_key_expiry);
1063 else
1064 wpa_msg(wpa_s, MSG_INFO,
1065 DPP_EVENT_NET_ACCESS_KEY "%s", hex);
1066 os_free(hex);
1067 }
1068 }
8528994e
JM
1069
1070 wpas_dpp_process_config(wpa_s, auth);
1071
461d39af
JM
1072 dpp_auth_deinit(wpa_s->dpp_auth);
1073 wpa_s->dpp_auth = NULL;
1074 return;
1075
1076fail:
1077 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1078 dpp_auth_deinit(wpa_s->dpp_auth);
1079 wpa_s->dpp_auth = NULL;
1080}
1081
1082
1083static void wpas_dpp_start_gas_client(struct wpa_supplicant *wpa_s)
1084{
1085 struct dpp_authentication *auth = wpa_s->dpp_auth;
1086 struct wpabuf *buf, *conf_req;
1087 char json[100];
1088 int res;
1089
1090 wpa_s->dpp_gas_client = 1;
1091 os_snprintf(json, sizeof(json),
1092 "{\"name\":\"Test\","
1093 "\"wi-fi_tech\":\"infra\","
1094 "\"netRole\":\"%s\"}",
1095 wpa_s->dpp_netrole_ap ? "ap" : "sta");
1096 wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
1097
1098 offchannel_send_action_done(wpa_s);
1099 wpas_dpp_listen_stop(wpa_s);
1100
1101 conf_req = dpp_build_conf_req(auth, json);
1102 if (!conf_req) {
1103 wpa_printf(MSG_DEBUG,
1104 "DPP: No configuration request data available");
1105 return;
1106 }
1107
1108 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
1109 if (!buf) {
1110 wpabuf_free(conf_req);
1111 return;
1112 }
1113
1114 /* Advertisement Protocol IE */
1115 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
1116 wpabuf_put_u8(buf, 8); /* Length */
1117 wpabuf_put_u8(buf, 0x7f);
1118 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
1119 wpabuf_put_u8(buf, 5);
1120 wpabuf_put_be24(buf, OUI_WFA);
1121 wpabuf_put_u8(buf, DPP_OUI_TYPE);
1122 wpabuf_put_u8(buf, 0x01);
1123
1124 /* GAS Query */
1125 wpabuf_put_le16(buf, wpabuf_len(conf_req));
1126 wpabuf_put_buf(buf, conf_req);
1127 wpabuf_free(conf_req);
1128
1129 wpa_printf(MSG_DEBUG, "DPP: GAS request to " MACSTR " (freq %u MHz)",
1130 MAC2STR(auth->peer_mac_addr), auth->curr_freq);
1131
1132 res = gas_query_req(wpa_s->gas, auth->peer_mac_addr, auth->curr_freq,
1133 buf, wpas_dpp_gas_resp_cb, wpa_s);
1134 if (res < 0) {
1135 wpa_msg(wpa_s, MSG_DEBUG, "GAS: Failed to send Query Request");
1136 wpabuf_free(buf);
1137 } else {
1138 wpa_printf(MSG_DEBUG,
1139 "DPP: GAS query started with dialog token %u", res);
1140 }
1141}
1142
1143
1144static void wpas_dpp_auth_success(struct wpa_supplicant *wpa_s, int initiator)
1145{
1146 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
1147 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
1148
1149 if (wpa_s->dpp_auth->configurator)
1150 wpas_dpp_start_gas_server(wpa_s);
1151 else
1152 wpas_dpp_start_gas_client(wpa_s);
1153}
1154
1155
30d27b04
JM
1156static void wpas_dpp_rx_auth_resp(struct wpa_supplicant *wpa_s, const u8 *src,
1157 const u8 *buf, size_t len)
1158{
1159 struct dpp_authentication *auth = wpa_s->dpp_auth;
1160 struct wpabuf *msg, *attr;
1161
1162 wpa_printf(MSG_DEBUG, "DPP: Authentication Response from " MACSTR,
1163 MAC2STR(src));
1164
1165 if (!auth) {
1166 wpa_printf(MSG_DEBUG,
1167 "DPP: No DPP Authentication in progress - drop");
1168 return;
1169 }
1170
1171 if (!is_zero_ether_addr(auth->peer_mac_addr) &&
1172 os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1173 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1174 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1175 return;
1176 }
1177
1178 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1179
1180 attr = dpp_auth_resp_rx(auth, buf, len);
1181 if (!attr) {
1182 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
1183 wpa_printf(MSG_DEBUG,
1184 "DPP: Start wait for full response");
1185 offchannel_send_action_done(wpa_s);
1186 wpas_dpp_listen_start(wpa_s, auth->curr_freq);
1187 return;
1188 }
1189 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
1190 return;
1191 }
1192 os_memcpy(auth->peer_mac_addr, src, ETH_ALEN);
1193
1194 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_CONF, wpabuf_len(attr));
1195 if (!msg) {
1196 wpabuf_free(attr);
1197 return;
1198 }
1199 wpabuf_put_buf(msg, attr);
1200 wpabuf_free(attr);
1201
1202 offchannel_send_action(wpa_s, auth->curr_freq,
1203 src, wpa_s->own_addr, broadcast,
1204 wpabuf_head(msg), wpabuf_len(msg),
1205 500, wpas_dpp_tx_status, 0);
1206 wpabuf_free(msg);
461d39af 1207 wpa_s->dpp_auth_ok_on_ack = 1;
30d27b04
JM
1208}
1209
1210
1211static void wpas_dpp_rx_auth_conf(struct wpa_supplicant *wpa_s, const u8 *src,
1212 const u8 *buf, size_t len)
1213{
1214 struct dpp_authentication *auth = wpa_s->dpp_auth;
1215
1216 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation from " MACSTR,
1217 MAC2STR(src));
1218
1219 if (!auth) {
1220 wpa_printf(MSG_DEBUG,
1221 "DPP: No DPP Authentication in progress - drop");
1222 return;
1223 }
1224
1225 if (os_memcmp(src, auth->peer_mac_addr, ETH_ALEN) != 0) {
1226 wpa_printf(MSG_DEBUG, "DPP: MAC address mismatch (expected "
1227 MACSTR ") - drop", MAC2STR(auth->peer_mac_addr));
1228 return;
1229 }
1230
1231 if (dpp_auth_conf_rx(auth, buf, len) < 0) {
1232 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
1233 return;
1234 }
1235
461d39af 1236 wpas_dpp_auth_success(wpa_s, 0);
30d27b04
JM
1237}
1238
1239
a0d5c56f
JM
1240static void wpas_dpp_rx_peer_disc_resp(struct wpa_supplicant *wpa_s,
1241 const u8 *src,
1242 const u8 *buf, size_t len)
1243{
1244 struct wpa_ssid *ssid;
1245 const u8 *connector, *pk_hash, *nk_hash;
1246 u16 connector_len, pk_hash_len, nk_hash_len;
1247 struct dpp_introduction intro;
1248 struct rsn_pmksa_cache_entry *entry;
1249
1250 wpa_printf(MSG_DEBUG, "DPP: Peer Discovery Response from " MACSTR,
1251 MAC2STR(src));
1252 if (is_zero_ether_addr(wpa_s->dpp_intro_bssid) ||
1253 os_memcmp(src, wpa_s->dpp_intro_bssid, ETH_ALEN) != 0) {
1254 wpa_printf(MSG_DEBUG, "DPP: Not waiting for response from "
1255 MACSTR " - drop", MAC2STR(src));
1256 return;
1257 }
1258 offchannel_send_action_done(wpa_s);
1259
1260 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1261 if (ssid == wpa_s->dpp_intro_network)
1262 break;
1263 }
1264 if (!ssid || !ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1265 !ssid->dpp_csign) {
1266 wpa_printf(MSG_DEBUG,
1267 "DPP: Profile not found for network introduction");
1268 return;
1269 }
1270
1271 connector = dpp_get_attr(buf, len, DPP_ATTR_CONNECTOR, &connector_len);
1272 if (!connector) {
1273 wpa_printf(MSG_DEBUG,
1274 "DPP: Peer did not include its Connector");
1275 return;
1276 }
1277
1278 if (dpp_peer_intro(&intro, ssid->dpp_connector,
1279 ssid->dpp_netaccesskey,
1280 ssid->dpp_netaccesskey_len,
1281 ssid->dpp_csign,
1282 ssid->dpp_csign_len,
1283 connector, connector_len) < 0) {
1284 wpa_printf(MSG_INFO,
1285 "DPP: Network Introduction protocol resulted in failure");
1286 goto fail;
1287 }
1288
1289 pk_hash = dpp_get_attr(buf, len, DPP_ATTR_PEER_NET_PK_HASH,
1290 &pk_hash_len);
1291 if (!pk_hash || pk_hash_len != SHA256_MAC_LEN) {
1292 wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(PK)");
1293 goto fail;
1294 }
1295 if (os_memcmp(pk_hash, intro.nk_hash, SHA256_MAC_LEN) != 0) {
1296 wpa_printf(MSG_DEBUG, "DPP: SHA256(PK) mismatch");
1297 wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(PK)",
1298 pk_hash, pk_hash_len);
1299 wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(PK)",
1300 intro.nk_hash, SHA256_MAC_LEN);
1301 goto fail;
1302 }
1303
1304 nk_hash = dpp_get_attr(buf, len, DPP_ATTR_OWN_NET_NK_HASH,
1305 &nk_hash_len);
1306 if (!nk_hash || nk_hash_len != SHA256_MAC_LEN) {
1307 wpa_printf(MSG_DEBUG, "DPP: Peer did not include SHA256(NK)");
1308 goto fail;
1309 }
1310 if (os_memcmp(nk_hash, intro.pk_hash, SHA256_MAC_LEN) != 0) {
1311 wpa_printf(MSG_DEBUG, "DPP: SHA256(NK) mismatch");
1312 wpa_hexdump(MSG_DEBUG, "DPP: Received SHA256(NK)",
1313 nk_hash, nk_hash_len);
1314 wpa_hexdump(MSG_DEBUG, "DPP: Calculated SHA256(NK)",
1315 intro.pk_hash, SHA256_MAC_LEN);
1316 goto fail;
1317 }
1318
1319 entry = os_zalloc(sizeof(*entry));
1320 if (!entry)
1321 goto fail;
1322 os_memcpy(entry->aa, src, ETH_ALEN);
1323 os_memcpy(entry->pmkid, intro.pmkid, PMKID_LEN);
1324 os_memcpy(entry->pmk, intro.pmk, intro.pmk_len);
1325 entry->pmk_len = intro.pmk_len;
1326 entry->akmp = WPA_KEY_MGMT_DPP;
1327 /* TODO: expiration */
1328 entry->network_ctx = ssid;
1329 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
1330
1331 wpa_printf(MSG_DEBUG,
1332 "DPP: Try connection again after successful network introduction");
1333 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
1334 wpa_supplicant_cancel_sched_scan(wpa_s);
1335 wpa_supplicant_req_scan(wpa_s, 0, 0);
1336 }
1337fail:
1338 os_memset(&intro, 0, sizeof(intro));
1339}
1340
1341
30d27b04
JM
1342void wpas_dpp_rx_action(struct wpa_supplicant *wpa_s, const u8 *src,
1343 const u8 *buf, size_t len, unsigned int freq)
1344{
1345 enum dpp_public_action_frame_type type;
1346
1347 if (len < 1)
1348 return;
1349 type = buf[0];
1350 buf++;
1351 len--;
1352
1353 wpa_printf(MSG_DEBUG,
1354 "DPP: Received DPP Public Action frame type %d from "
1355 MACSTR " freq=%u",
1356 type, MAC2STR(src), freq);
1357 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes", buf, len);
1358 if (dpp_check_attrs(buf, len) < 0)
1359 return;
1360
1361 switch (type) {
1362 case DPP_PA_AUTHENTICATION_REQ:
1363 wpas_dpp_rx_auth_req(wpa_s, src, buf, len, freq);
1364 break;
1365 case DPP_PA_AUTHENTICATION_RESP:
1366 wpas_dpp_rx_auth_resp(wpa_s, src, buf, len);
1367 break;
1368 case DPP_PA_AUTHENTICATION_CONF:
1369 wpas_dpp_rx_auth_conf(wpa_s, src, buf, len);
1370 break;
a0d5c56f
JM
1371 case DPP_PA_PEER_DISCOVERY_RESP:
1372 wpas_dpp_rx_peer_disc_resp(wpa_s, src, buf, len);
1373 break;
30d27b04
JM
1374 default:
1375 wpa_printf(MSG_DEBUG,
1376 "DPP: Ignored unsupported frame subtype %d", type);
1377 break;
1378 }
1379}
1380
1381
461d39af
JM
1382static struct wpabuf *
1383wpas_dpp_gas_req_handler(void *ctx, const u8 *sa, const u8 *query,
1384 size_t query_len)
1385{
1386 struct wpa_supplicant *wpa_s = ctx;
1387 struct dpp_authentication *auth = wpa_s->dpp_auth;
1388 struct wpabuf *resp;
1389
1390 wpa_printf(MSG_DEBUG, "DPP: GAS request from " MACSTR,
1391 MAC2STR(sa));
1392 if (!auth || !auth->auth_success ||
1393 os_memcmp(sa, auth->peer_mac_addr, ETH_ALEN) != 0) {
1394 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
1395 return NULL;
1396 }
1397 wpa_hexdump(MSG_DEBUG,
1398 "DPP: Received Configuration Request (GAS Query Request)",
1399 query, query_len);
1400 resp = dpp_conf_req_rx(auth, query, query_len);
1401 if (!resp)
1402 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1403 return resp;
1404}
1405
1406
1407static void
1408wpas_dpp_gas_status_handler(void *ctx, struct wpabuf *resp, int ok)
1409{
1410 struct wpa_supplicant *wpa_s = ctx;
1411 struct dpp_authentication *auth = wpa_s->dpp_auth;
1412
1413 if (!auth) {
1414 wpabuf_free(resp);
1415 return;
1416 }
1417
1418 wpa_printf(MSG_DEBUG, "DPP: Configuration exchange completed (ok=%d)",
1419 ok);
1420 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1421 offchannel_send_action_done(wpa_s);
1422 wpas_dpp_listen_stop(wpa_s);
1423 if (ok)
1424 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_SENT);
1425 else
1426 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_CONF_FAILED);
1427 dpp_auth_deinit(wpa_s->dpp_auth);
1428 wpa_s->dpp_auth = NULL;
1429 wpabuf_free(resp);
1430}
1431
1432
1433static unsigned int wpas_dpp_next_configurator_id(struct wpa_supplicant *wpa_s)
1434{
1435 struct dpp_configurator *conf;
1436 unsigned int max_id = 0;
1437
1438 dl_list_for_each(conf, &wpa_s->dpp_configurator,
1439 struct dpp_configurator, list) {
1440 if (conf->id > max_id)
1441 max_id = conf->id;
1442 }
1443 return max_id + 1;
1444}
1445
1446
1447int wpas_dpp_configurator_add(struct wpa_supplicant *wpa_s, const char *cmd)
1448{
1449 char *expiry = NULL, *curve = NULL;
1450 char *key = NULL;
1451 u8 *privkey = NULL;
1452 size_t privkey_len = 0;
1453 int ret = -1;
1454 struct dpp_configurator *conf = NULL;
1455
1456 expiry = get_param(cmd, " expiry=");
1457 curve = get_param(cmd, " curve=");
1458 key = get_param(cmd, " key=");
1459
1460 if (key) {
1461 privkey_len = os_strlen(key) / 2;
1462 privkey = os_malloc(privkey_len);
1463 if (!privkey ||
1464 hexstr2bin(key, privkey, privkey_len) < 0)
1465 goto fail;
1466 }
1467
1468 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
1469 if (!conf)
1470 goto fail;
1471
1472 if (expiry) {
1473 long int val;
1474
1475 val = strtol(expiry, NULL, 0);
1476 if (val <= 0)
1477 goto fail;
1478 conf->csign_expiry = val;
1479 }
1480
1481 conf->id = wpas_dpp_next_configurator_id(wpa_s);
1482 dl_list_add(&wpa_s->dpp_configurator, &conf->list);
1483 ret = conf->id;
1484 conf = NULL;
1485fail:
1486 os_free(curve);
1487 os_free(expiry);
1488 str_clear_free(key);
1489 bin_clear_free(privkey, privkey_len);
1490 dpp_configurator_free(conf);
1491 return ret;
1492}
1493
1494
1495static int dpp_configurator_del(struct wpa_supplicant *wpa_s, unsigned int id)
1496{
1497 struct dpp_configurator *conf, *tmp;
1498 int found = 0;
1499
1500 dl_list_for_each_safe(conf, tmp, &wpa_s->dpp_configurator,
1501 struct dpp_configurator, list) {
1502 if (id && conf->id != id)
1503 continue;
1504 found = 1;
1505 dl_list_del(&conf->list);
1506 dpp_configurator_free(conf);
1507 }
1508
1509 if (id == 0)
1510 return 0; /* flush succeeds regardless of entries found */
1511 return found ? 0 : -1;
1512}
1513
1514
1515int wpas_dpp_configurator_remove(struct wpa_supplicant *wpa_s, const char *id)
1516{
1517 unsigned int id_val;
1518
1519 if (os_strcmp(id, "*") == 0) {
1520 id_val = 0;
1521 } else {
1522 id_val = atoi(id);
1523 if (id_val == 0)
1524 return -1;
1525 }
1526
1527 return dpp_configurator_del(wpa_s, id_val);
1528}
1529
1530
a0d5c56f
JM
1531static void
1532wpas_dpp_tx_introduction_status(struct wpa_supplicant *wpa_s,
1533 unsigned int freq, const u8 *dst,
1534 const u8 *src, const u8 *bssid,
1535 const u8 *data, size_t data_len,
1536 enum offchannel_send_action_result result)
1537{
1538 wpa_printf(MSG_DEBUG, "DPP: TX status: freq=%u dst=" MACSTR
1539 " result=%s (DPP Peer Discovery Request)",
1540 freq, MAC2STR(dst),
1541 result == OFFCHANNEL_SEND_ACTION_SUCCESS ? "SUCCESS" :
1542 (result == OFFCHANNEL_SEND_ACTION_NO_ACK ? "no-ACK" :
1543 "FAILED"));
1544 /* TODO: Time out wait for response more quickly in error cases? */
1545}
1546
1547
1548int wpas_dpp_check_connect(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
1549 struct wpa_bss *bss)
1550{
1551 struct os_time now;
1552 struct wpabuf *msg;
1553 unsigned int wait_time;
1554
1555 if (!(ssid->key_mgmt & WPA_KEY_MGMT_DPP) || !bss)
1556 return 0; /* Not using DPP AKM - continue */
1557 if (wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid))
1558 return 0; /* PMKSA exists for DPP AKM - continue */
1559
1560 if (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
1561 !ssid->dpp_csign) {
1562 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
1563 "missing %s",
1564 !ssid->dpp_connector ? "Connector" :
1565 (!ssid->dpp_netaccesskey ? "netAccessKey" :
1566 "C-sign-key"));
1567 return -1;
1568 }
1569
1570 os_get_time(&now);
1571
1572 if (ssid->dpp_csign_expiry && ssid->dpp_csign_expiry < now.sec) {
1573 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
1574 "C-sign-key expired");
1575 return -1;
1576 }
1577
1578 if (ssid->dpp_netaccesskey_expiry &&
1579 ssid->dpp_netaccesskey_expiry < now.sec) {
1580 wpa_msg(wpa_s, MSG_INFO, DPP_EVENT_MISSING_CONNECTOR
1581 "netAccessKey expired");
1582 return -1;
1583 }
1584
1585 wpa_printf(MSG_DEBUG,
1586 "DPP: Starting network introduction protocol to derive PMKSA for "
1587 MACSTR, MAC2STR(bss->bssid));
1588
1589 msg = dpp_alloc_msg(DPP_PA_PEER_DISCOVERY_REQ,
1590 4 + os_strlen(ssid->dpp_connector));
1591 if (!msg)
1592 return -1;
1593
1594 /* DPP Connector */
1595 wpabuf_put_le16(msg, DPP_ATTR_CONNECTOR);
1596 wpabuf_put_le16(msg, os_strlen(ssid->dpp_connector));
1597 wpabuf_put_str(msg, ssid->dpp_connector);
1598
1599 /* TODO: Timeout on AP response */
1600 wait_time = wpa_s->max_remain_on_chan;
1601 if (wait_time > 2000)
1602 wait_time = 2000;
1603 offchannel_send_action(wpa_s, bss->freq, bss->bssid, wpa_s->own_addr,
1604 broadcast,
1605 wpabuf_head(msg), wpabuf_len(msg),
1606 wait_time, wpas_dpp_tx_introduction_status, 0);
1607 wpabuf_free(msg);
1608
1609 /* Request this connection attempt to terminate - new one will be
1610 * started when network introduction protocol completes */
1611 os_memcpy(wpa_s->dpp_intro_bssid, bss->bssid, ETH_ALEN);
1612 wpa_s->dpp_intro_network = ssid;
1613 return 1;
1614}
1615
1616
be27e185
JM
1617int wpas_dpp_init(struct wpa_supplicant *wpa_s)
1618{
461d39af
JM
1619 u8 adv_proto_id[7];
1620
1621 adv_proto_id[0] = WLAN_EID_VENDOR_SPECIFIC;
1622 adv_proto_id[1] = 5;
1623 WPA_PUT_BE24(&adv_proto_id[2], OUI_WFA);
1624 adv_proto_id[5] = DPP_OUI_TYPE;
1625 adv_proto_id[6] = 0x01;
1626
1627 if (gas_server_register(wpa_s->gas_server, adv_proto_id,
1628 sizeof(adv_proto_id), wpas_dpp_gas_req_handler,
1629 wpas_dpp_gas_status_handler, wpa_s) < 0)
1630 return -1;
be27e185 1631 dl_list_init(&wpa_s->dpp_bootstrap);
461d39af 1632 dl_list_init(&wpa_s->dpp_configurator);
be27e185
JM
1633 wpa_s->dpp_init_done = 1;
1634 return 0;
1635}
1636
1637
1638void wpas_dpp_deinit(struct wpa_supplicant *wpa_s)
1639{
461d39af
JM
1640#ifdef CONFIG_TESTING_OPTIONS
1641 os_free(wpa_s->dpp_config_obj_override);
1642 wpa_s->dpp_config_obj_override = NULL;
1643 os_free(wpa_s->dpp_discovery_override);
1644 wpa_s->dpp_discovery_override = NULL;
1645 os_free(wpa_s->dpp_groups_override);
1646 wpa_s->dpp_groups_override = NULL;
1647 os_free(wpa_s->dpp_devices_override);
1648 wpa_s->dpp_devices_override = NULL;
1649 wpa_s->dpp_ignore_netaccesskey_mismatch = 0;
1650#endif /* CONFIG_TESTING_OPTIONS */
be27e185
JM
1651 if (!wpa_s->dpp_init_done)
1652 return;
30d27b04
JM
1653 eloop_cancel_timeout(wpas_dpp_reply_wait_timeout, wpa_s, NULL);
1654 offchannel_send_action_done(wpa_s);
1655 wpas_dpp_listen_stop(wpa_s);
be27e185 1656 dpp_bootstrap_del(wpa_s, 0);
461d39af 1657 dpp_configurator_del(wpa_s, 0);
30d27b04
JM
1658 dpp_auth_deinit(wpa_s->dpp_auth);
1659 wpa_s->dpp_auth = NULL;
a0d5c56f 1660 os_memset(wpa_s->dpp_intro_bssid, 0, ETH_ALEN);
be27e185 1661}