1 /*
2 * DPP functionality shared between hostapd and wpa_supplicant
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2020, The Linux Foundation
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11 #include <openssl/opensslv.h>
12 #include <openssl/err.h>
13
14 #include "utils/common.h"
15 #include "utils/base64.h"
16 #include "utils/json.h"
17 #include "common/ieee802_11_common.h"
18 #include "common/wpa_ctrl.h"
19 #include "common/gas.h"
20 #include "crypto/crypto.h"
21 #include "crypto/random.h"
22 #include "crypto/aes.h"
23 #include "crypto/aes_siv.h"
24 #include "drivers/driver.h"
25 #include "dpp.h"
26 #include "dpp_i.h"
27
28
29 static const char * dpp_netrole_str(enum dpp_netrole netrole);
30
31 #ifdef CONFIG_TESTING_OPTIONS
32 #ifdef CONFIG_DPP2
33 int dpp_version_override = 2;
34 #else
35 int dpp_version_override = 1;
36 #endif
37 enum dpp_test_behavior dpp_test = DPP_TEST_DISABLED;
38 #endif /* CONFIG_TESTING_OPTIONS */
39
40 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
41 (defined(LIBRESSL_VERSION_NUMBER) && \
42 LIBRESSL_VERSION_NUMBER < 0x20700000L)
43 /* Compatibility wrappers for older versions. */
44
45 #ifdef CONFIG_DPP2
EVP_PKEY_get0_EC_KEY(EVP_PKEY * pkey)46 static EC_KEY * EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
47 {
48 if (pkey->type != EVP_PKEY_EC)
49 return NULL;
50 return pkey->pkey.ec;
51 }
52 #endif /* CONFIG_DPP2 */
53
54 #endif
55
56
dpp_auth_fail(struct dpp_authentication * auth,const char * txt)57 void dpp_auth_fail(struct dpp_authentication *auth, const char *txt)
58 {
59 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
60 }
61
62
dpp_alloc_msg(enum dpp_public_action_frame_type type,size_t len)63 struct wpabuf * dpp_alloc_msg(enum dpp_public_action_frame_type type,
64 size_t len)
65 {
66 struct wpabuf *msg;
67
68 msg = wpabuf_alloc(8 + len);
69 if (!msg)
70 return NULL;
71 wpabuf_put_u8(msg, WLAN_ACTION_PUBLIC);
72 wpabuf_put_u8(msg, WLAN_PA_VENDOR_SPECIFIC);
73 wpabuf_put_be24(msg, OUI_WFA);
74 wpabuf_put_u8(msg, DPP_OUI_TYPE);
75 wpabuf_put_u8(msg, 1); /* Crypto Suite */
76 wpabuf_put_u8(msg, type);
77 return msg;
78 }
79
80
dpp_get_attr(const u8 * buf,size_t len,u16 req_id,u16 * ret_len)81 const u8 * dpp_get_attr(const u8 *buf, size_t len, u16 req_id, u16 *ret_len)
82 {
83 u16 id, alen;
84 const u8 *pos = buf, *end = buf + len;
85
86 while (end - pos >= 4) {
87 id = WPA_GET_LE16(pos);
88 pos += 2;
89 alen = WPA_GET_LE16(pos);
90 pos += 2;
91 if (alen > end - pos)
92 return NULL;
93 if (id == req_id) {
94 *ret_len = alen;
95 return pos;
96 }
97 pos += alen;
98 }
99
100 return NULL;
101 }
102
103
dpp_get_attr_next(const u8 * prev,const u8 * buf,size_t len,u16 req_id,u16 * ret_len)104 static const u8 * dpp_get_attr_next(const u8 *prev, const u8 *buf, size_t len,
105 u16 req_id, u16 *ret_len)
106 {
107 u16 id, alen;
108 const u8 *pos, *end = buf + len;
109
110 if (!prev)
111 pos = buf;
112 else
113 pos = prev + WPA_GET_LE16(prev - 2);
114 while (end - pos >= 4) {
115 id = WPA_GET_LE16(pos);
116 pos += 2;
117 alen = WPA_GET_LE16(pos);
118 pos += 2;
119 if (alen > end - pos)
120 return NULL;
121 if (id == req_id) {
122 *ret_len = alen;
123 return pos;
124 }
125 pos += alen;
126 }
127
128 return NULL;
129 }
130
131
dpp_check_attrs(const u8 * buf,size_t len)132 int dpp_check_attrs(const u8 *buf, size_t len)
133 {
134 const u8 *pos, *end;
135 int wrapped_data = 0;
136
137 pos = buf;
138 end = buf + len;
139 while (end - pos >= 4) {
140 u16 id, alen;
141
142 id = WPA_GET_LE16(pos);
143 pos += 2;
144 alen = WPA_GET_LE16(pos);
145 pos += 2;
146 wpa_printf(MSG_MSGDUMP, "DPP: Attribute ID %04x len %u",
147 id, alen);
148 if (alen > end - pos) {
149 wpa_printf(MSG_DEBUG,
150 "DPP: Truncated message - not enough room for the attribute - dropped");
151 return -1;
152 }
153 if (wrapped_data) {
154 wpa_printf(MSG_DEBUG,
155 "DPP: An unexpected attribute included after the Wrapped Data attribute");
156 return -1;
157 }
158 if (id == DPP_ATTR_WRAPPED_DATA)
159 wrapped_data = 1;
160 pos += alen;
161 }
162
163 if (end != pos) {
164 wpa_printf(MSG_DEBUG,
165 "DPP: Unexpected octets (%d) after the last attribute",
166 (int) (end - pos));
167 return -1;
168 }
169
170 return 0;
171 }
172
173
dpp_bootstrap_info_free(struct dpp_bootstrap_info * info)174 void dpp_bootstrap_info_free(struct dpp_bootstrap_info *info)
175 {
176 if (!info)
177 return;
178 os_free(info->uri);
179 os_free(info->info);
180 os_free(info->chan);
181 os_free(info->pk);
182 EVP_PKEY_free(info->pubkey);
183 str_clear_free(info->configurator_params);
184 os_free(info);
185 }
186
187
dpp_bootstrap_type_txt(enum dpp_bootstrap_type type)188 const char * dpp_bootstrap_type_txt(enum dpp_bootstrap_type type)
189 {
190 switch (type) {
191 case DPP_BOOTSTRAP_QR_CODE:
192 return "QRCODE";
193 case DPP_BOOTSTRAP_PKEX:
194 return "PKEX";
195 case DPP_BOOTSTRAP_NFC_URI:
196 return "NFC-URI";
197 }
198 return "??";
199 }
200
201
dpp_uri_valid_info(const char * info)202 static int dpp_uri_valid_info(const char *info)
203 {
204 while (*info) {
205 unsigned char val = *info++;
206
207 if (val < 0x20 || val > 0x7e || val == 0x3b)
208 return 0;
209 }
210
211 return 1;
212 }
213
214
dpp_clone_uri(struct dpp_bootstrap_info * bi,const char * uri)215 static int dpp_clone_uri(struct dpp_bootstrap_info *bi, const char *uri)
216 {
217 bi->uri = os_strdup(uri);
218 return bi->uri ? 0 : -1;
219 }
220
221
dpp_parse_uri_chan_list(struct dpp_bootstrap_info * bi,const char * chan_list)222 int dpp_parse_uri_chan_list(struct dpp_bootstrap_info *bi,
223 const char *chan_list)
224 {
225 const char *pos = chan_list, *pos2;
226 int opclass = -1, channel, freq;
227
228 while (pos && *pos && *pos != ';') {
229 pos2 = pos;
230 while (*pos2 >= '0' && *pos2 <= '9')
231 pos2++;
232 if (*pos2 == '/') {
233 opclass = atoi(pos);
234 pos = pos2 + 1;
235 }
236 if (opclass <= 0)
237 goto fail;
238 channel = atoi(pos);
239 if (channel <= 0)
240 goto fail;
241 while (*pos >= '0' && *pos <= '9')
242 pos++;
243 freq = ieee80211_chan_to_freq(NULL, opclass, channel);
244 wpa_printf(MSG_DEBUG,
245 "DPP: URI channel-list: opclass=%d channel=%d ==> freq=%d",
246 opclass, channel, freq);
247 if (freq < 0) {
248 wpa_printf(MSG_DEBUG,
249 "DPP: Ignore unknown URI channel-list channel (opclass=%d channel=%d)",
250 opclass, channel);
251 } else if (bi->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
252 wpa_printf(MSG_DEBUG,
253 "DPP: Too many channels in URI channel-list - ignore list");
254 bi->num_freq = 0;
255 break;
256 } else {
257 bi->freq[bi->num_freq++] = freq;
258 }
259
260 if (*pos == ';' || *pos == '\0')
261 break;
262 if (*pos != ',')
263 goto fail;
264 pos++;
265 }
266
267 return 0;
268 fail:
269 wpa_printf(MSG_DEBUG, "DPP: Invalid URI channel-list");
270 return -1;
271 }
272
273
dpp_parse_uri_mac(struct dpp_bootstrap_info * bi,const char * mac)274 int dpp_parse_uri_mac(struct dpp_bootstrap_info *bi, const char *mac)
275 {
276 if (!mac)
277 return 0;
278
279 if (hwaddr_aton2(mac, bi->mac_addr) < 0) {
280 wpa_printf(MSG_DEBUG, "DPP: Invalid URI mac");
281 return -1;
282 }
283
284 wpa_printf(MSG_DEBUG, "DPP: URI mac: " MACSTR, MAC2STR(bi->mac_addr));
285
286 return 0;
287 }
288
289
dpp_parse_uri_info(struct dpp_bootstrap_info * bi,const char * info)290 int dpp_parse_uri_info(struct dpp_bootstrap_info *bi, const char *info)
291 {
292 const char *end;
293
294 if (!info)
295 return 0;
296
297 end = os_strchr(info, ';');
298 if (!end)
299 end = info + os_strlen(info);
300 bi->info = os_malloc(end - info + 1);
301 if (!bi->info)
302 return -1;
303 os_memcpy(bi->info, info, end - info);
304 bi->info[end - info] = '\0';
305 wpa_printf(MSG_DEBUG, "DPP: URI(information): %s", bi->info);
306 if (!dpp_uri_valid_info(bi->info)) {
307 wpa_printf(MSG_DEBUG, "DPP: Invalid URI information payload");
308 return -1;
309 }
310
311 return 0;
312 }
313
314
dpp_parse_uri_version(struct dpp_bootstrap_info * bi,const char * version)315 int dpp_parse_uri_version(struct dpp_bootstrap_info *bi, const char *version)
316 {
317 #ifdef CONFIG_DPP2
318 if (!version || DPP_VERSION < 2)
319 return 0;
320
321 if (*version == '1')
322 bi->version = 1;
323 else if (*version == '2')
324 bi->version = 2;
325 else
326 wpa_printf(MSG_DEBUG, "DPP: Unknown URI version");
327
328 wpa_printf(MSG_DEBUG, "DPP: URI version: %d", bi->version);
329 #endif /* CONFIG_DPP2 */
330
331 return 0;
332 }
333
334
dpp_parse_uri_pk(struct dpp_bootstrap_info * bi,const char * info)335 static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
336 {
337 u8 *data;
338 size_t data_len;
339 int res;
340 const char *end;
341
342 end = os_strchr(info, ';');
343 if (!end)
344 return -1;
345
346 data = base64_decode(info, end - info, &data_len);
347 if (!data) {
348 wpa_printf(MSG_DEBUG,
349 "DPP: Invalid base64 encoding on URI public-key");
350 return -1;
351 }
352 wpa_hexdump(MSG_DEBUG, "DPP: Base64 decoded URI public-key",
353 data, data_len);
354
355 res = dpp_get_subject_public_key(bi, data, data_len);
356 os_free(data);
357 return res;
358 }
359
360
dpp_parse_uri(const char * uri)361 static struct dpp_bootstrap_info * dpp_parse_uri(const char *uri)
362 {
363 const char *pos = uri;
364 const char *end;
365 const char *chan_list = NULL, *mac = NULL, *info = NULL, *pk = NULL;
366 const char *version = NULL;
367 struct dpp_bootstrap_info *bi;
368
369 wpa_hexdump_ascii(MSG_DEBUG, "DPP: URI", uri, os_strlen(uri));
370
371 if (os_strncmp(pos, "DPP:", 4) != 0) {
372 wpa_printf(MSG_INFO, "DPP: Not a DPP URI");
373 return NULL;
374 }
375 pos += 4;
376
377 for (;;) {
378 end = os_strchr(pos, ';');
379 if (!end)
380 break;
381
382 if (end == pos) {
383 /* Handle terminating ";;" and ignore unexpected ";"
384 * for parsing robustness. */
385 pos++;
386 continue;
387 }
388
389 if (pos[0] == 'C' && pos[1] == ':' && !chan_list)
390 chan_list = pos + 2;
391 else if (pos[0] == 'M' && pos[1] == ':' && !mac)
392 mac = pos + 2;
393 else if (pos[0] == 'I' && pos[1] == ':' && !info)
394 info = pos + 2;
395 else if (pos[0] == 'K' && pos[1] == ':' && !pk)
396 pk = pos + 2;
397 else if (pos[0] == 'V' && pos[1] == ':' && !version)
398 version = pos + 2;
399 else
400 wpa_hexdump_ascii(MSG_DEBUG,
401 "DPP: Ignore unrecognized URI parameter",
402 pos, end - pos);
403 pos = end + 1;
404 }
405
406 if (!pk) {
407 wpa_printf(MSG_INFO, "DPP: URI missing public-key");
408 return NULL;
409 }
410
411 bi = os_zalloc(sizeof(*bi));
412 if (!bi)
413 return NULL;
414
415 if (dpp_clone_uri(bi, uri) < 0 ||
416 dpp_parse_uri_chan_list(bi, chan_list) < 0 ||
417 dpp_parse_uri_mac(bi, mac) < 0 ||
418 dpp_parse_uri_info(bi, info) < 0 ||
419 dpp_parse_uri_version(bi, version) < 0 ||
420 dpp_parse_uri_pk(bi, pk) < 0) {
421 dpp_bootstrap_info_free(bi);
422 bi = NULL;
423 }
424
425 return bi;
426 }
427
428
dpp_build_attr_status(struct wpabuf * msg,enum dpp_status_error status)429 void dpp_build_attr_status(struct wpabuf *msg, enum dpp_status_error status)
430 {
431 wpa_printf(MSG_DEBUG, "DPP: Status %d", status);
432 wpabuf_put_le16(msg, DPP_ATTR_STATUS);
433 wpabuf_put_le16(msg, 1);
434 wpabuf_put_u8(msg, status);
435 }
436
437
dpp_build_attr_r_bootstrap_key_hash(struct wpabuf * msg,const u8 * hash)438 void dpp_build_attr_r_bootstrap_key_hash(struct wpabuf *msg, const u8 *hash)
439 {
440 if (hash) {
441 wpa_printf(MSG_DEBUG, "DPP: R-Bootstrap Key Hash");
442 wpabuf_put_le16(msg, DPP_ATTR_R_BOOTSTRAP_KEY_HASH);
443 wpabuf_put_le16(msg, SHA256_MAC_LEN);
444 wpabuf_put_data(msg, hash, SHA256_MAC_LEN);
445 }
446 }
447
448
dpp_channel_ok_init(struct hostapd_hw_modes * own_modes,u16 num_modes,unsigned int freq)449 static int dpp_channel_ok_init(struct hostapd_hw_modes *own_modes,
450 u16 num_modes, unsigned int freq)
451 {
452 u16 m;
453 int c, flag;
454
455 if (!own_modes || !num_modes)
456 return 1;
457
458 for (m = 0; m < num_modes; m++) {
459 for (c = 0; c < own_modes[m].num_channels; c++) {
460 if ((unsigned int) own_modes[m].channels[c].freq !=
461 freq)
462 continue;
463 flag = own_modes[m].channels[c].flag;
464 if (!(flag & (HOSTAPD_CHAN_DISABLED |
465 HOSTAPD_CHAN_NO_IR |
466 HOSTAPD_CHAN_RADAR)))
467 return 1;
468 }
469 }
470
471 wpa_printf(MSG_DEBUG, "DPP: Peer channel %u MHz not supported", freq);
472 return 0;
473 }
474
475
freq_included(const unsigned int freqs[],unsigned int num,unsigned int freq)476 static int freq_included(const unsigned int freqs[], unsigned int num,
477 unsigned int freq)
478 {
479 while (num > 0) {
480 if (freqs[--num] == freq)
481 return 1;
482 }
483 return 0;
484 }
485
486
freq_to_start(unsigned int freqs[],unsigned int num,unsigned int freq)487 static void freq_to_start(unsigned int freqs[], unsigned int num,
488 unsigned int freq)
489 {
490 unsigned int i;
491
492 for (i = 0; i < num; i++) {
493 if (freqs[i] == freq)
494 break;
495 }
496 if (i == 0 || i >= num)
497 return;
498 os_memmove(&freqs[1], &freqs[0], i * sizeof(freqs[0]));
499 freqs[0] = freq;
500 }
501
502
dpp_channel_intersect(struct dpp_authentication * auth,struct hostapd_hw_modes * own_modes,u16 num_modes)503 static int dpp_channel_intersect(struct dpp_authentication *auth,
504 struct hostapd_hw_modes *own_modes,
505 u16 num_modes)
506 {
507 struct dpp_bootstrap_info *peer_bi = auth->peer_bi;
508 unsigned int i, freq;
509
510 for (i = 0; i < peer_bi->num_freq; i++) {
511 freq = peer_bi->freq[i];
512 if (freq_included(auth->freq, auth->num_freq, freq))
513 continue;
514 if (dpp_channel_ok_init(own_modes, num_modes, freq))
515 auth->freq[auth->num_freq++] = freq;
516 }
517 if (!auth->num_freq) {
518 wpa_printf(MSG_INFO,
519 "DPP: No available channels for initiating DPP Authentication");
520 return -1;
521 }
522 auth->curr_freq = auth->freq[0];
523 return 0;
524 }
525
526
dpp_channel_local_list(struct dpp_authentication * auth,struct hostapd_hw_modes * own_modes,u16 num_modes)527 static int dpp_channel_local_list(struct dpp_authentication *auth,
528 struct hostapd_hw_modes *own_modes,
529 u16 num_modes)
530 {
531 u16 m;
532 int c, flag;
533 unsigned int freq;
534
535 auth->num_freq = 0;
536
537 if (!own_modes || !num_modes) {
538 auth->freq[0] = 2412;
539 auth->freq[1] = 2437;
540 auth->freq[2] = 2462;
541 auth->num_freq = 3;
542 return 0;
543 }
544
545 for (m = 0; m < num_modes; m++) {
546 for (c = 0; c < own_modes[m].num_channels; c++) {
547 freq = own_modes[m].channels[c].freq;
548 flag = own_modes[m].channels[c].flag;
549 if (flag & (HOSTAPD_CHAN_DISABLED |
550 HOSTAPD_CHAN_NO_IR |
551 HOSTAPD_CHAN_RADAR))
552 continue;
553 if (freq_included(auth->freq, auth->num_freq, freq))
554 continue;
555 auth->freq[auth->num_freq++] = freq;
556 if (auth->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
557 m = num_modes;
558 break;
559 }
560 }
561 }
562
563 return auth->num_freq == 0 ? -1 : 0;
564 }
565
566
dpp_prepare_channel_list(struct dpp_authentication * auth,unsigned int neg_freq,struct hostapd_hw_modes * own_modes,u16 num_modes)567 int dpp_prepare_channel_list(struct dpp_authentication *auth,
568 unsigned int neg_freq,
569 struct hostapd_hw_modes *own_modes, u16 num_modes)
570 {
571 int res;
572 char freqs[DPP_BOOTSTRAP_MAX_FREQ * 6 + 10], *pos, *end;
573 unsigned int i;
574
575 if (!own_modes) {
576 if (!neg_freq)
577 return -1;
578 auth->num_freq = 1;
579 auth->freq[0] = neg_freq;
580 auth->curr_freq = neg_freq;
581 return 0;
582 }
583
584 if (auth->peer_bi->num_freq > 0)
585 res = dpp_channel_intersect(auth, own_modes, num_modes);
586 else
587 res = dpp_channel_local_list(auth, own_modes, num_modes);
588 if (res < 0)
589 return res;
590
591 /* Prioritize 2.4 GHz channels 6, 1, 11 (in this order) to hit the most
592 * likely channels first. */
593 freq_to_start(auth->freq, auth->num_freq, 2462);
594 freq_to_start(auth->freq, auth->num_freq, 2412);
595 freq_to_start(auth->freq, auth->num_freq, 2437);
596
597 auth->freq_idx = 0;
598 auth->curr_freq = auth->freq[0];
599
600 pos = freqs;
601 end = pos + sizeof(freqs);
602 for (i = 0; i < auth->num_freq; i++) {
603 res = os_snprintf(pos, end - pos, " %u", auth->freq[i]);
604 if (os_snprintf_error(end - pos, res))
605 break;
606 pos += res;
607 }
608 *pos = '\0';
609 wpa_printf(MSG_DEBUG, "DPP: Possible frequencies for initiating:%s",
610 freqs);
611
612 return 0;
613 }
614
615
dpp_gen_uri(struct dpp_bootstrap_info * bi)616 int dpp_gen_uri(struct dpp_bootstrap_info *bi)
617 {
618 char macstr[ETH_ALEN * 2 + 10];
619 size_t len;
620
621 len = 4; /* "DPP:" */
622 if (bi->chan)
623 len += 3 + os_strlen(bi->chan); /* C:...; */
624 if (is_zero_ether_addr(bi->mac_addr))
625 macstr[0] = '\0';
626 else
627 os_snprintf(macstr, sizeof(macstr), "M:" COMPACT_MACSTR ";",
628 MAC2STR(bi->mac_addr));
629 len += os_strlen(macstr); /* M:...; */
630 if (bi->info)
631 len += 3 + os_strlen(bi->info); /* I:...; */
632 #ifdef CONFIG_DPP2
633 len += 4; /* V:2; */
634 #endif /* CONFIG_DPP2 */
635 len += 4 + os_strlen(bi->pk); /* K:...;; */
636
637 os_free(bi->uri);
638 bi->uri = os_malloc(len + 1);
639 if (!bi->uri)
640 return -1;
641 os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%sK:%s;;",
642 bi->chan ? "C:" : "", bi->chan ? bi->chan : "",
643 bi->chan ? ";" : "",
644 macstr,
645 bi->info ? "I:" : "", bi->info ? bi->info : "",
646 bi->info ? ";" : "",
647 DPP_VERSION == 2 ? "V:2;" : "",
648 bi->pk);
649 return 0;
650 }
651
652
653 struct dpp_authentication *
dpp_alloc_auth(struct dpp_global * dpp,void * msg_ctx)654 dpp_alloc_auth(struct dpp_global *dpp, void *msg_ctx)
655 {
656 struct dpp_authentication *auth;
657
658 auth = os_zalloc(sizeof(*auth));
659 if (!auth)
660 return NULL;
661 auth->global = dpp;
662 auth->msg_ctx = msg_ctx;
663 auth->conf_resp_status = 255;
664 return auth;
665 }
666
667
dpp_build_conf_req_attr(struct dpp_authentication * auth,const char * json)668 static struct wpabuf * dpp_build_conf_req_attr(struct dpp_authentication *auth,
669 const char *json)
670 {
671 size_t nonce_len;
672 size_t json_len, clear_len;
673 struct wpabuf *clear = NULL, *msg = NULL;
674 u8 *wrapped;
675 size_t attr_len;
676
677 wpa_printf(MSG_DEBUG, "DPP: Build configuration request");
678
679 nonce_len = auth->curve->nonce_len;
680 if (random_get_bytes(auth->e_nonce, nonce_len)) {
681 wpa_printf(MSG_ERROR, "DPP: Failed to generate E-nonce");
682 goto fail;
683 }
684 wpa_hexdump(MSG_DEBUG, "DPP: E-nonce", auth->e_nonce, nonce_len);
685 json_len = os_strlen(json);
686 wpa_hexdump_ascii(MSG_DEBUG, "DPP: configRequest JSON", json, json_len);
687
688 /* { E-nonce, configAttrib }ke */
689 clear_len = 4 + nonce_len + 4 + json_len;
690 clear = wpabuf_alloc(clear_len);
691 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
692 #ifdef CONFIG_TESTING_OPTIONS
693 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ)
694 attr_len += 5;
695 #endif /* CONFIG_TESTING_OPTIONS */
696 msg = wpabuf_alloc(attr_len);
697 if (!clear || !msg)
698 goto fail;
699
700 #ifdef CONFIG_TESTING_OPTIONS
701 if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_REQ) {
702 wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
703 goto skip_e_nonce;
704 }
705 if (dpp_test == DPP_TEST_INVALID_E_NONCE_CONF_REQ) {
706 wpa_printf(MSG_INFO, "DPP: TESTING - invalid E-nonce");
707 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
708 wpabuf_put_le16(clear, nonce_len - 1);
709 wpabuf_put_data(clear, auth->e_nonce, nonce_len - 1);
710 goto skip_e_nonce;
711 }
712 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_REQ) {
713 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
714 goto skip_wrapped_data;
715 }
716 #endif /* CONFIG_TESTING_OPTIONS */
717
718 /* E-nonce */
719 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
720 wpabuf_put_le16(clear, nonce_len);
721 wpabuf_put_data(clear, auth->e_nonce, nonce_len);
722
723 #ifdef CONFIG_TESTING_OPTIONS
724 skip_e_nonce:
725 if (dpp_test == DPP_TEST_NO_CONFIG_ATTR_OBJ_CONF_REQ) {
726 wpa_printf(MSG_INFO, "DPP: TESTING - no configAttrib");
727 goto skip_conf_attr_obj;
728 }
729 #endif /* CONFIG_TESTING_OPTIONS */
730
731 /* configAttrib */
732 wpabuf_put_le16(clear, DPP_ATTR_CONFIG_ATTR_OBJ);
733 wpabuf_put_le16(clear, json_len);
734 wpabuf_put_data(clear, json, json_len);
735
736 #ifdef CONFIG_TESTING_OPTIONS
737 skip_conf_attr_obj:
738 #endif /* CONFIG_TESTING_OPTIONS */
739
740 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
741 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
742 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
743
744 /* No AES-SIV AD */
745 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
746 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
747 wpabuf_head(clear), wpabuf_len(clear),
748 0, NULL, NULL, wrapped) < 0)
749 goto fail;
750 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
751 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
752
753 #ifdef CONFIG_TESTING_OPTIONS
754 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ) {
755 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
756 dpp_build_attr_status(msg, DPP_STATUS_OK);
757 }
758 skip_wrapped_data:
759 #endif /* CONFIG_TESTING_OPTIONS */
760
761 wpa_hexdump_buf(MSG_DEBUG,
762 "DPP: Configuration Request frame attributes", msg);
763 wpabuf_free(clear);
764 return msg;
765
766 fail:
767 wpabuf_free(clear);
768 wpabuf_free(msg);
769 return NULL;
770 }
771
772
dpp_write_adv_proto(struct wpabuf * buf)773 void dpp_write_adv_proto(struct wpabuf *buf)
774 {
775 /* Advertisement Protocol IE */
776 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
777 wpabuf_put_u8(buf, 8); /* Length */
778 wpabuf_put_u8(buf, 0x7f);
779 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
780 wpabuf_put_u8(buf, 5);
781 wpabuf_put_be24(buf, OUI_WFA);
782 wpabuf_put_u8(buf, DPP_OUI_TYPE);
783 wpabuf_put_u8(buf, 0x01);
784 }
785
786
dpp_write_gas_query(struct wpabuf * buf,struct wpabuf * query)787 void dpp_write_gas_query(struct wpabuf *buf, struct wpabuf *query)
788 {
789 /* GAS Query */
790 wpabuf_put_le16(buf, wpabuf_len(query));
791 wpabuf_put_buf(buf, query);
792 }
793
794
dpp_build_conf_req(struct dpp_authentication * auth,const char * json)795 struct wpabuf * dpp_build_conf_req(struct dpp_authentication *auth,
796 const char *json)
797 {
798 struct wpabuf *buf, *conf_req;
799
800 conf_req = dpp_build_conf_req_attr(auth, json);
801 if (!conf_req) {
802 wpa_printf(MSG_DEBUG,
803 "DPP: No configuration request data available");
804 return NULL;
805 }
806
807 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
808 if (!buf) {
809 wpabuf_free(conf_req);
810 return NULL;
811 }
812
813 dpp_write_adv_proto(buf);
814 dpp_write_gas_query(buf, conf_req);
815 wpabuf_free(conf_req);
816 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: GAS Config Request", buf);
817
818 return buf;
819 }
820
821
dpp_build_conf_req_helper(struct dpp_authentication * auth,const char * name,enum dpp_netrole netrole,const char * mud_url,int * opclasses)822 struct wpabuf * dpp_build_conf_req_helper(struct dpp_authentication *auth,
823 const char *name,
824 enum dpp_netrole netrole,
825 const char *mud_url, int *opclasses)
826 {
827 size_t len, name_len;
828 const char *tech = "infra";
829 const char *dpp_name;
830 struct wpabuf *buf, *json;
831
832 #ifdef CONFIG_TESTING_OPTIONS
833 if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
834 static const char *bogus_tech = "knfra";
835
836 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
837 tech = bogus_tech;
838 }
839 #endif /* CONFIG_TESTING_OPTIONS */
840
841 dpp_name = name ? name : "Test";
842 name_len = os_strlen(dpp_name);
843
844 len = 100 + name_len * 6 + 1 + int_array_len(opclasses) * 4;
845 if (mud_url && mud_url[0])
846 len += 10 + os_strlen(mud_url);
847 json = wpabuf_alloc(len);
848 if (!json)
849 return NULL;
850
851 json_start_object(json, NULL);
852 if (json_add_string_escape(json, "name", dpp_name, name_len) < 0) {
853 wpabuf_free(json);
854 return NULL;
855 }
856 json_value_sep(json);
857 json_add_string(json, "wi-fi_tech", tech);
858 json_value_sep(json);
859 json_add_string(json, "netRole", dpp_netrole_str(netrole));
860 if (mud_url && mud_url[0]) {
861 json_value_sep(json);
862 json_add_string(json, "mudurl", mud_url);
863 }
864 if (opclasses) {
865 int i;
866
867 json_value_sep(json);
868 json_start_array(json, "bandSupport");
869 for (i = 0; opclasses[i]; i++)
870 wpabuf_printf(json, "%s%u", i ? "," : "", opclasses[i]);
871 json_end_array(json);
872 }
873 json_end_object(json);
874
875 buf = dpp_build_conf_req(auth, wpabuf_head(json));
876 wpabuf_free(json);
877
878 return buf;
879 }
880
881
bin_str_eq(const char * val,size_t len,const char * cmp)882 static int bin_str_eq(const char *val, size_t len, const char *cmp)
883 {
884 return os_strlen(cmp) == len && os_memcmp(val, cmp, len) == 0;
885 }
886
887
dpp_configuration_alloc(const char * type)888 struct dpp_configuration * dpp_configuration_alloc(const char *type)
889 {
890 struct dpp_configuration *conf;
891 const char *end;
892 size_t len;
893
894 conf = os_zalloc(sizeof(*conf));
895 if (!conf)
896 goto fail;
897
898 end = os_strchr(type, ' ');
899 if (end)
900 len = end - type;
901 else
902 len = os_strlen(type);
903
904 if (bin_str_eq(type, len, "psk"))
905 conf->akm = DPP_AKM_PSK;
906 else if (bin_str_eq(type, len, "sae"))
907 conf->akm = DPP_AKM_SAE;
908 else if (bin_str_eq(type, len, "psk-sae") ||
909 bin_str_eq(type, len, "psk+sae"))
910 conf->akm = DPP_AKM_PSK_SAE;
911 else if (bin_str_eq(type, len, "sae-dpp") ||
912 bin_str_eq(type, len, "dpp+sae"))
913 conf->akm = DPP_AKM_SAE_DPP;
914 else if (bin_str_eq(type, len, "psk-sae-dpp") ||
915 bin_str_eq(type, len, "dpp+psk+sae"))
916 conf->akm = DPP_AKM_PSK_SAE_DPP;
917 else if (bin_str_eq(type, len, "dpp"))
918 conf->akm = DPP_AKM_DPP;
919 else
920 goto fail;
921
922 return conf;
923 fail:
924 dpp_configuration_free(conf);
925 return NULL;
926 }
927
928
dpp_akm_psk(enum dpp_akm akm)929 int dpp_akm_psk(enum dpp_akm akm)
930 {
931 return akm == DPP_AKM_PSK || akm == DPP_AKM_PSK_SAE ||
932 akm == DPP_AKM_PSK_SAE_DPP;
933 }
934
935
dpp_akm_sae(enum dpp_akm akm)936 int dpp_akm_sae(enum dpp_akm akm)
937 {
938 return akm == DPP_AKM_SAE || akm == DPP_AKM_PSK_SAE ||
939 akm == DPP_AKM_SAE_DPP || akm == DPP_AKM_PSK_SAE_DPP;
940 }
941
942
dpp_akm_legacy(enum dpp_akm akm)943 int dpp_akm_legacy(enum dpp_akm akm)
944 {
945 return akm == DPP_AKM_PSK || akm == DPP_AKM_PSK_SAE ||
946 akm == DPP_AKM_SAE;
947 }
948
949
dpp_akm_dpp(enum dpp_akm akm)950 int dpp_akm_dpp(enum dpp_akm akm)
951 {
952 return akm == DPP_AKM_DPP || akm == DPP_AKM_SAE_DPP ||
953 akm == DPP_AKM_PSK_SAE_DPP;
954 }
955
956
dpp_akm_ver2(enum dpp_akm akm)957 int dpp_akm_ver2(enum dpp_akm akm)
958 {
959 return akm == DPP_AKM_SAE_DPP || akm == DPP_AKM_PSK_SAE_DPP;
960 }
961
962
dpp_configuration_valid(const struct dpp_configuration * conf)963 int dpp_configuration_valid(const struct dpp_configuration *conf)
964 {
965 if (conf->ssid_len == 0)
966 return 0;
967 if (dpp_akm_psk(conf->akm) && !conf->passphrase && !conf->psk_set)
968 return 0;
969 if (dpp_akm_sae(conf->akm) && !conf->passphrase)
970 return 0;
971 return 1;
972 }
973
974
dpp_configuration_free(struct dpp_configuration * conf)975 void dpp_configuration_free(struct dpp_configuration *conf)
976 {
977 if (!conf)
978 return;
979 str_clear_free(conf->passphrase);
980 os_free(conf->group_id);
981 bin_clear_free(conf, sizeof(*conf));
982 }
983
984
dpp_configuration_parse_helper(struct dpp_authentication * auth,const char * cmd,int idx)985 static int dpp_configuration_parse_helper(struct dpp_authentication *auth,
986 const char *cmd, int idx)
987 {
988 const char *pos, *end;
989 struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
990 struct dpp_configuration *conf = NULL;
991
992 pos = os_strstr(cmd, " conf=sta-");
993 if (pos) {
994 conf_sta = dpp_configuration_alloc(pos + 10);
995 if (!conf_sta)
996 goto fail;
997 conf_sta->netrole = DPP_NETROLE_STA;
998 conf = conf_sta;
999 }
1000
1001 pos = os_strstr(cmd, " conf=ap-");
1002 if (pos) {
1003 conf_ap = dpp_configuration_alloc(pos + 9);
1004 if (!conf_ap)
1005 goto fail;
1006 conf_ap->netrole = DPP_NETROLE_AP;
1007 conf = conf_ap;
1008 }
1009
1010 pos = os_strstr(cmd, " conf=configurator");
1011 if (pos)
1012 auth->provision_configurator = 1;
1013
1014 if (!conf)
1015 return 0;
1016
1017 pos = os_strstr(cmd, " ssid=");
1018 if (pos) {
1019 pos += 6;
1020 end = os_strchr(pos, ' ');
1021 conf->ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
1022 conf->ssid_len /= 2;
1023 if (conf->ssid_len > sizeof(conf->ssid) ||
1024 hexstr2bin(pos, conf->ssid, conf->ssid_len) < 0)
1025 goto fail;
1026 } else {
1027 #ifdef CONFIG_TESTING_OPTIONS
1028 /* use a default SSID for legacy testing reasons */
1029 os_memcpy(conf->ssid, "test", 4);
1030 conf->ssid_len = 4;
1031 #else /* CONFIG_TESTING_OPTIONS */
1032 goto fail;
1033 #endif /* CONFIG_TESTING_OPTIONS */
1034 }
1035
1036 pos = os_strstr(cmd, " ssid_charset=");
1037 if (pos) {
1038 if (conf_ap) {
1039 wpa_printf(MSG_INFO,
1040 "DPP: ssid64 option (ssid_charset param) not allowed for AP enrollee");
1041 goto fail;
1042 }
1043 conf->ssid_charset = atoi(pos + 14);
1044 }
1045
1046 pos = os_strstr(cmd, " pass=");
1047 if (pos) {
1048 size_t pass_len;
1049
1050 pos += 6;
1051 end = os_strchr(pos, ' ');
1052 pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
1053 pass_len /= 2;
1054 if (pass_len > 63 || pass_len < 8)
1055 goto fail;
1056 conf->passphrase = os_zalloc(pass_len + 1);
1057 if (!conf->passphrase ||
1058 hexstr2bin(pos, (u8 *) conf->passphrase, pass_len) < 0)
1059 goto fail;
1060 }
1061
1062 pos = os_strstr(cmd, " psk=");
1063 if (pos) {
1064 pos += 5;
1065 if (hexstr2bin(pos, conf->psk, PMK_LEN) < 0)
1066 goto fail;
1067 conf->psk_set = 1;
1068 }
1069
1070 pos = os_strstr(cmd, " group_id=");
1071 if (pos) {
1072 size_t group_id_len;
1073
1074 pos += 10;
1075 end = os_strchr(pos, ' ');
1076 group_id_len = end ? (size_t) (end - pos) : os_strlen(pos);
1077 conf->group_id = os_malloc(group_id_len + 1);
1078 if (!conf->group_id)
1079 goto fail;
1080 os_memcpy(conf->group_id, pos, group_id_len);
1081 conf->group_id[group_id_len] = '\0';
1082 }
1083
1084 pos = os_strstr(cmd, " expiry=");
1085 if (pos) {
1086 long int val;
1087
1088 pos += 8;
1089 val = strtol(pos, NULL, 0);
1090 if (val <= 0)
1091 goto fail;
1092 conf->netaccesskey_expiry = val;
1093 }
1094
1095 if (!dpp_configuration_valid(conf))
1096 goto fail;
1097
1098 if (idx == 0) {
1099 auth->conf_sta = conf_sta;
1100 auth->conf_ap = conf_ap;
1101 } else if (idx == 1) {
1102 auth->conf2_sta = conf_sta;
1103 auth->conf2_ap = conf_ap;
1104 } else {
1105 goto fail;
1106 }
1107 return 0;
1108
1109 fail:
1110 dpp_configuration_free(conf_sta);
1111 dpp_configuration_free(conf_ap);
1112 return -1;
1113 }
1114
1115
dpp_configuration_parse(struct dpp_authentication * auth,const char * cmd)1116 static int dpp_configuration_parse(struct dpp_authentication *auth,
1117 const char *cmd)
1118 {
1119 const char *pos;
1120 char *tmp;
1121 size_t len;
1122 int res;
1123
1124 pos = os_strstr(cmd, " @CONF-OBJ-SEP@ ");
1125 if (!pos)
1126 return dpp_configuration_parse_helper(auth, cmd, 0);
1127
1128 len = pos - cmd;
1129 tmp = os_malloc(len + 1);
1130 if (!tmp)
1131 goto fail;
1132 os_memcpy(tmp, cmd, len);
1133 tmp[len] = '\0';
1134 res = dpp_configuration_parse_helper(auth, cmd, 0);
1135 str_clear_free(tmp);
1136 if (res)
1137 goto fail;
1138 res = dpp_configuration_parse_helper(auth, cmd + len, 1);
1139 if (res)
1140 goto fail;
1141 return 0;
1142 fail:
1143 dpp_configuration_free(auth->conf_sta);
1144 dpp_configuration_free(auth->conf2_sta);
1145 dpp_configuration_free(auth->conf_ap);
1146 dpp_configuration_free(auth->conf2_ap);
1147 return -1;
1148 }
1149
1150
1151 static struct dpp_configurator *
dpp_configurator_get_id(struct dpp_global * dpp,unsigned int id)1152 dpp_configurator_get_id(struct dpp_global *dpp, unsigned int id)
1153 {
1154 struct dpp_configurator *conf;
1155
1156 if (!dpp)
1157 return NULL;
1158
1159 dl_list_for_each(conf, &dpp->configurator,
1160 struct dpp_configurator, list) {
1161 if (conf->id == id)
1162 return conf;
1163 }
1164 return NULL;
1165 }
1166
1167
dpp_set_configurator(struct dpp_authentication * auth,const char * cmd)1168 int dpp_set_configurator(struct dpp_authentication *auth, const char *cmd)
1169 {
1170 const char *pos;
1171 char *tmp = NULL;
1172 int ret = -1;
1173
1174 if (!cmd || auth->configurator_set)
1175 return 0;
1176 auth->configurator_set = 1;
1177
1178 if (cmd[0] != ' ') {
1179 size_t len;
1180
1181 len = os_strlen(cmd);
1182 tmp = os_malloc(len + 2);
1183 if (!tmp)
1184 goto fail;
1185 tmp[0] = ' ';
1186 os_memcpy(tmp + 1, cmd, len + 1);
1187 cmd = tmp;
1188 }
1189
1190 wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
1191
1192 pos = os_strstr(cmd, " configurator=");
1193 if (!auth->conf && pos) {
1194 pos += 14;
1195 auth->conf = dpp_configurator_get_id(auth->global, atoi(pos));
1196 if (!auth->conf) {
1197 wpa_printf(MSG_INFO,
1198 "DPP: Could not find the specified configurator");
1199 goto fail;
1200 }
1201 }
1202
1203 pos = os_strstr(cmd, " conn_status=");
1204 if (pos) {
1205 pos += 13;
1206 auth->send_conn_status = atoi(pos);
1207 }
1208
1209 pos = os_strstr(cmd, " akm_use_selector=");
1210 if (pos) {
1211 pos += 18;
1212 auth->akm_use_selector = atoi(pos);
1213 }
1214
1215 if (dpp_configuration_parse(auth, cmd) < 0) {
1216 wpa_msg(auth->msg_ctx, MSG_INFO,
1217 "DPP: Failed to set configurator parameters");
1218 goto fail;
1219 }
1220 ret = 0;
1221 fail:
1222 os_free(tmp);
1223 return ret;
1224 }
1225
1226
dpp_auth_deinit(struct dpp_authentication * auth)1227 void dpp_auth_deinit(struct dpp_authentication *auth)
1228 {
1229 unsigned int i;
1230
1231 if (!auth)
1232 return;
1233 dpp_configuration_free(auth->conf_ap);
1234 dpp_configuration_free(auth->conf2_ap);
1235 dpp_configuration_free(auth->conf_sta);
1236 dpp_configuration_free(auth->conf2_sta);
1237 EVP_PKEY_free(auth->own_protocol_key);
1238 EVP_PKEY_free(auth->peer_protocol_key);
1239 EVP_PKEY_free(auth->reconfig_old_protocol_key);
1240 wpabuf_free(auth->req_msg);
1241 wpabuf_free(auth->resp_msg);
1242 wpabuf_free(auth->conf_req);
1243 wpabuf_free(auth->reconfig_req_msg);
1244 wpabuf_free(auth->reconfig_resp_msg);
1245 for (i = 0; i < auth->num_conf_obj; i++) {
1246 struct dpp_config_obj *conf = &auth->conf_obj[i];
1247
1248 os_free(conf->connector);
1249 wpabuf_free(conf->c_sign_key);
1250 }
1251 #ifdef CONFIG_DPP2
1252 dpp_free_asymmetric_key(auth->conf_key_pkg);
1253 #endif /* CONFIG_DPP2 */
1254 wpabuf_free(auth->net_access_key);
1255 dpp_bootstrap_info_free(auth->tmp_own_bi);
1256 #ifdef CONFIG_TESTING_OPTIONS
1257 os_free(auth->config_obj_override);
1258 os_free(auth->discovery_override);
1259 os_free(auth->groups_override);
1260 #endif /* CONFIG_TESTING_OPTIONS */
1261 bin_clear_free(auth, sizeof(*auth));
1262 }
1263
1264
1265 static struct wpabuf *
dpp_build_conf_start(struct dpp_authentication * auth,struct dpp_configuration * conf,size_t tailroom)1266 dpp_build_conf_start(struct dpp_authentication *auth,
1267 struct dpp_configuration *conf, size_t tailroom)
1268 {
1269 struct wpabuf *buf;
1270
1271 #ifdef CONFIG_TESTING_OPTIONS
1272 if (auth->discovery_override)
1273 tailroom += os_strlen(auth->discovery_override);
1274 #endif /* CONFIG_TESTING_OPTIONS */
1275
1276 buf = wpabuf_alloc(200 + tailroom);
1277 if (!buf)
1278 return NULL;
1279 json_start_object(buf, NULL);
1280 json_add_string(buf, "wi-fi_tech", "infra");
1281 json_value_sep(buf);
1282 #ifdef CONFIG_TESTING_OPTIONS
1283 if (auth->discovery_override) {
1284 wpa_printf(MSG_DEBUG, "DPP: TESTING - discovery override: '%s'",
1285 auth->discovery_override);
1286 wpabuf_put_str(buf, "\"discovery\":");
1287 wpabuf_put_str(buf, auth->discovery_override);
1288 json_value_sep(buf);
1289 return buf;
1290 }
1291 #endif /* CONFIG_TESTING_OPTIONS */
1292 json_start_object(buf, "discovery");
1293 if (((!conf->ssid_charset || auth->peer_version < 2) &&
1294 json_add_string_escape(buf, "ssid", conf->ssid,
1295 conf->ssid_len) < 0) ||
1296 ((conf->ssid_charset && auth->peer_version >= 2) &&
1297 json_add_base64url(buf, "ssid64", conf->ssid,
1298 conf->ssid_len) < 0)) {
1299 wpabuf_free(buf);
1300 return NULL;
1301 }
1302 if (conf->ssid_charset > 0) {
1303 json_value_sep(buf);
1304 json_add_int(buf, "ssid_charset", conf->ssid_charset);
1305 }
1306 json_end_object(buf);
1307 json_value_sep(buf);
1308
1309 return buf;
1310 }
1311
1312
dpp_build_jwk(struct wpabuf * buf,const char * name,EVP_PKEY * key,const char * kid,const struct dpp_curve_params * curve)1313 int dpp_build_jwk(struct wpabuf *buf, const char *name, EVP_PKEY *key,
1314 const char *kid, const struct dpp_curve_params *curve)
1315 {
1316 struct wpabuf *pub;
1317 const u8 *pos;
1318 int ret = -1;
1319
1320 pub = dpp_get_pubkey_point(key, 0);
1321 if (!pub)
1322 goto fail;
1323
1324 json_start_object(buf, name);
1325 json_add_string(buf, "kty", "EC");
1326 json_value_sep(buf);
1327 json_add_string(buf, "crv", curve->jwk_crv);
1328 json_value_sep(buf);
1329 pos = wpabuf_head(pub);
1330 if (json_add_base64url(buf, "x", pos, curve->prime_len) < 0)
1331 goto fail;
1332 json_value_sep(buf);
1333 pos += curve->prime_len;
1334 if (json_add_base64url(buf, "y", pos, curve->prime_len) < 0)
1335 goto fail;
1336 if (kid) {
1337 json_value_sep(buf);
1338 json_add_string(buf, "kid", kid);
1339 }
1340 json_end_object(buf);
1341 ret = 0;
1342 fail:
1343 wpabuf_free(pub);
1344 return ret;
1345 }
1346
1347
dpp_build_legacy_cred_params(struct wpabuf * buf,struct dpp_configuration * conf)1348 static void dpp_build_legacy_cred_params(struct wpabuf *buf,
1349 struct dpp_configuration *conf)
1350 {
1351 if (conf->passphrase && os_strlen(conf->passphrase) < 64) {
1352 json_add_string_escape(buf, "pass", conf->passphrase,
1353 os_strlen(conf->passphrase));
1354 } else if (conf->psk_set) {
1355 char psk[2 * sizeof(conf->psk) + 1];
1356
1357 wpa_snprintf_hex(psk, sizeof(psk),
1358 conf->psk, sizeof(conf->psk));
1359 json_add_string(buf, "psk_hex", psk);
1360 forced_memzero(psk, sizeof(psk));
1361 }
1362 }
1363
1364
dpp_netrole_str(enum dpp_netrole netrole)1365 static const char * dpp_netrole_str(enum dpp_netrole netrole)
1366 {
1367 switch (netrole) {
1368 case DPP_NETROLE_STA:
1369 return "sta";
1370 case DPP_NETROLE_AP:
1371 return "ap";
1372 case DPP_NETROLE_CONFIGURATOR:
1373 return "configurator";
1374 default:
1375 return "??";
1376 }
1377 }
1378
1379
1380 static struct wpabuf *
dpp_build_conf_obj_dpp(struct dpp_authentication * auth,struct dpp_configuration * conf)1381 dpp_build_conf_obj_dpp(struct dpp_authentication *auth,
1382 struct dpp_configuration *conf)
1383 {
1384 struct wpabuf *buf = NULL;
1385 char *signed_conn = NULL;
1386 size_t tailroom;
1387 const struct dpp_curve_params *curve;
1388 struct wpabuf *dppcon = NULL;
1389 size_t extra_len = 1000;
1390 int incl_legacy;
1391 enum dpp_akm akm;
1392 const char *akm_str;
1393
1394 if (!auth->conf) {
1395 wpa_printf(MSG_INFO,
1396 "DPP: No configurator specified - cannot generate DPP config object");
1397 goto fail;
1398 }
1399 curve = auth->conf->curve;
1400
1401 akm = conf->akm;
1402 if (dpp_akm_ver2(akm) && auth->peer_version < 2) {
1403 wpa_printf(MSG_DEBUG,
1404 "DPP: Convert DPP+legacy credential to DPP-only for peer that does not support version 2");
1405 akm = DPP_AKM_DPP;
1406 }
1407
1408 #ifdef CONFIG_TESTING_OPTIONS
1409 if (auth->groups_override)
1410 extra_len += os_strlen(auth->groups_override);
1411 #endif /* CONFIG_TESTING_OPTIONS */
1412
1413 if (conf->group_id)
1414 extra_len += os_strlen(conf->group_id);
1415
1416 /* Connector (JSON dppCon object) */
1417 dppcon = wpabuf_alloc(extra_len + 2 * auth->curve->prime_len * 4 / 3);
1418 if (!dppcon)
1419 goto fail;
1420 #ifdef CONFIG_TESTING_OPTIONS
1421 if (auth->groups_override) {
1422 wpabuf_put_u8(dppcon, '{');
1423 if (auth->groups_override) {
1424 wpa_printf(MSG_DEBUG,
1425 "DPP: TESTING - groups override: '%s'",
1426 auth->groups_override);
1427 wpabuf_put_str(dppcon, "\"groups\":");
1428 wpabuf_put_str(dppcon, auth->groups_override);
1429 json_value_sep(dppcon);
1430 }
1431 goto skip_groups;
1432 }
1433 #endif /* CONFIG_TESTING_OPTIONS */
1434 json_start_object(dppcon, NULL);
1435 json_start_array(dppcon, "groups");
1436 json_start_object(dppcon, NULL);
1437 json_add_string(dppcon, "groupId",
1438 conf->group_id ? conf->group_id : "*");
1439 json_value_sep(dppcon);
1440 json_add_string(dppcon, "netRole", dpp_netrole_str(conf->netrole));
1441 json_end_object(dppcon);
1442 json_end_array(dppcon);
1443 json_value_sep(dppcon);
1444 #ifdef CONFIG_TESTING_OPTIONS
1445 skip_groups:
1446 #endif /* CONFIG_TESTING_OPTIONS */
1447 if (!auth->peer_protocol_key ||
1448 dpp_build_jwk(dppcon, "netAccessKey", auth->peer_protocol_key, NULL,
1449 auth->curve) < 0) {
1450 wpa_printf(MSG_DEBUG, "DPP: Failed to build netAccessKey JWK");
1451 goto fail;
1452 }
1453 if (conf->netaccesskey_expiry) {
1454 struct os_tm tm;
1455 char expiry[30];
1456
1457 if (os_gmtime(conf->netaccesskey_expiry, &tm) < 0) {
1458 wpa_printf(MSG_DEBUG,
1459 "DPP: Failed to generate expiry string");
1460 goto fail;
1461 }
1462 os_snprintf(expiry, sizeof(expiry),
1463 "%04u-%02u-%02uT%02u:%02u:%02uZ",
1464 tm.year, tm.month, tm.day,
1465 tm.hour, tm.min, tm.sec);
1466 json_value_sep(dppcon);
1467 json_add_string(dppcon, "expiry", expiry);
1468 }
1469 json_end_object(dppcon);
1470 wpa_printf(MSG_DEBUG, "DPP: dppCon: %s",
1471 (const char *) wpabuf_head(dppcon));
1472
1473 signed_conn = dpp_sign_connector(auth->conf, dppcon);
1474 if (!signed_conn)
1475 goto fail;
1476
1477 incl_legacy = dpp_akm_psk(akm) || dpp_akm_sae(akm);
1478 tailroom = 1000;
1479 tailroom += 2 * curve->prime_len * 4 / 3 + os_strlen(auth->conf->kid);
1480 tailroom += os_strlen(signed_conn);
1481 if (incl_legacy)
1482 tailroom += 1000;
1483 buf = dpp_build_conf_start(auth, conf, tailroom);
1484 if (!buf)
1485 goto fail;
1486
1487 if (auth->akm_use_selector && dpp_akm_ver2(akm))
1488 akm_str = dpp_akm_selector_str(akm);
1489 else
1490 akm_str = dpp_akm_str(akm);
1491 json_start_object(buf, "cred");
1492 json_add_string(buf, "akm", akm_str);
1493 json_value_sep(buf);
1494 if (incl_legacy) {
1495 dpp_build_legacy_cred_params(buf, conf);
1496 json_value_sep(buf);
1497 }
1498 wpabuf_put_str(buf, "\"signedConnector\":\"");
1499 wpabuf_put_str(buf, signed_conn);
1500 wpabuf_put_str(buf, "\"");
1501 json_value_sep(buf);
1502 if (dpp_build_jwk(buf, "csign", auth->conf->csign, auth->conf->kid,
1503 curve) < 0) {
1504 wpa_printf(MSG_DEBUG, "DPP: Failed to build csign JWK");
1505 goto fail;
1506 }
1507
1508 json_end_object(buf);
1509 json_end_object(buf);
1510
1511 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object",
1512 wpabuf_head(buf), wpabuf_len(buf));
1513
1514 out:
1515 os_free(signed_conn);
1516 wpabuf_free(dppcon);
1517 return buf;
1518 fail:
1519 wpa_printf(MSG_DEBUG, "DPP: Failed to build configuration object");
1520 wpabuf_free(buf);
1521 buf = NULL;
1522 goto out;
1523 }
1524
1525
1526 static struct wpabuf *
dpp_build_conf_obj_legacy(struct dpp_authentication * auth,struct dpp_configuration * conf)1527 dpp_build_conf_obj_legacy(struct dpp_authentication *auth,
1528 struct dpp_configuration *conf)
1529 {
1530 struct wpabuf *buf;
1531 const char *akm_str;
1532
1533 buf = dpp_build_conf_start(auth, conf, 1000);
1534 if (!buf)
1535 return NULL;
1536
1537 if (auth->akm_use_selector && dpp_akm_ver2(conf->akm))
1538 akm_str = dpp_akm_selector_str(conf->akm);
1539 else
1540 akm_str = dpp_akm_str(conf->akm);
1541 json_start_object(buf, "cred");
1542 json_add_string(buf, "akm", akm_str);
1543 json_value_sep(buf);
1544 dpp_build_legacy_cred_params(buf, conf);
1545 json_end_object(buf);
1546 json_end_object(buf);
1547
1548 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object (legacy)",
1549 wpabuf_head(buf), wpabuf_len(buf));
1550
1551 return buf;
1552 }
1553
1554
1555 static struct wpabuf *
dpp_build_conf_obj(struct dpp_authentication * auth,enum dpp_netrole netrole,int idx)1556 dpp_build_conf_obj(struct dpp_authentication *auth, enum dpp_netrole netrole,
1557 int idx)
1558 {
1559 struct dpp_configuration *conf = NULL;
1560
1561 #ifdef CONFIG_TESTING_OPTIONS
1562 if (auth->config_obj_override) {
1563 if (idx != 0)
1564 return NULL;
1565 wpa_printf(MSG_DEBUG, "DPP: Testing - Config Object override");
1566 return wpabuf_alloc_copy(auth->config_obj_override,
1567 os_strlen(auth->config_obj_override));
1568 }
1569 #endif /* CONFIG_TESTING_OPTIONS */
1570
1571 if (idx == 0) {
1572 if (netrole == DPP_NETROLE_STA)
1573 conf = auth->conf_sta;
1574 else if (netrole == DPP_NETROLE_AP)
1575 conf = auth->conf_ap;
1576 } else if (idx == 1) {
1577 if (netrole == DPP_NETROLE_STA)
1578 conf = auth->conf2_sta;
1579 else if (netrole == DPP_NETROLE_AP)
1580 conf = auth->conf2_ap;
1581 }
1582 if (!conf) {
1583 if (idx == 0)
1584 wpa_printf(MSG_DEBUG,
1585 "DPP: No configuration available for Enrollee(%s) - reject configuration request",
1586 dpp_netrole_str(netrole));
1587 return NULL;
1588 }
1589
1590 if (dpp_akm_dpp(conf->akm) || (auth->peer_version >= 2 && auth->conf))
1591 return dpp_build_conf_obj_dpp(auth, conf);
1592 return dpp_build_conf_obj_legacy(auth, conf);
1593 }
1594
1595
1596 static struct wpabuf *
dpp_build_conf_resp(struct dpp_authentication * auth,const u8 * e_nonce,u16 e_nonce_len,enum dpp_netrole netrole)1597 dpp_build_conf_resp(struct dpp_authentication *auth, const u8 *e_nonce,
1598 u16 e_nonce_len, enum dpp_netrole netrole)
1599 {
1600 struct wpabuf *conf = NULL, *conf2 = NULL, *env_data = NULL;
1601 size_t clear_len, attr_len;
1602 struct wpabuf *clear = NULL, *msg = NULL;
1603 u8 *wrapped;
1604 const u8 *addr[1];
1605 size_t len[1];
1606 enum dpp_status_error status;
1607
1608 if (netrole == DPP_NETROLE_CONFIGURATOR) {
1609 #ifdef CONFIG_DPP2
1610 env_data = dpp_build_enveloped_data(auth);
1611 #endif /* CONFIG_DPP2 */
1612 } else {
1613 conf = dpp_build_conf_obj(auth, netrole, 0);
1614 if (conf) {
1615 wpa_hexdump_ascii(MSG_DEBUG,
1616 "DPP: configurationObject JSON",
1617 wpabuf_head(conf), wpabuf_len(conf));
1618 conf2 = dpp_build_conf_obj(auth, netrole, 1);
1619 }
1620 }
1621 status = (conf || env_data) ? DPP_STATUS_OK :
1622 DPP_STATUS_CONFIGURE_FAILURE;
1623 auth->conf_resp_status = status;
1624
1625 /* { E-nonce, configurationObject[, sendConnStatus]}ke */
1626 clear_len = 4 + e_nonce_len;
1627 if (conf)
1628 clear_len += 4 + wpabuf_len(conf);
1629 if (conf2)
1630 clear_len += 4 + wpabuf_len(conf2);
1631 if (env_data)
1632 clear_len += 4 + wpabuf_len(env_data);
1633 if (auth->peer_version >= 2 && auth->send_conn_status &&
1634 netrole == DPP_NETROLE_STA)
1635 clear_len += 4;
1636 clear = wpabuf_alloc(clear_len);
1637 attr_len = 4 + 1 + 4 + clear_len + AES_BLOCK_SIZE;
1638 #ifdef CONFIG_TESTING_OPTIONS
1639 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP)
1640 attr_len += 5;
1641 #endif /* CONFIG_TESTING_OPTIONS */
1642 msg = wpabuf_alloc(attr_len);
1643 if (!clear || !msg)
1644 goto fail;
1645
1646 #ifdef CONFIG_TESTING_OPTIONS
1647 if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_RESP) {
1648 wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
1649 goto skip_e_nonce;
1650 }
1651 if (dpp_test == DPP_TEST_E_NONCE_MISMATCH_CONF_RESP) {
1652 wpa_printf(MSG_INFO, "DPP: TESTING - E-nonce mismatch");
1653 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
1654 wpabuf_put_le16(clear, e_nonce_len);
1655 wpabuf_put_data(clear, e_nonce, e_nonce_len - 1);
1656 wpabuf_put_u8(clear, e_nonce[e_nonce_len - 1] ^ 0x01);
1657 goto skip_e_nonce;
1658 }
1659 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_RESP) {
1660 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
1661 goto skip_wrapped_data;
1662 }
1663 #endif /* CONFIG_TESTING_OPTIONS */
1664
1665 /* E-nonce */
1666 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
1667 wpabuf_put_le16(clear, e_nonce_len);
1668 wpabuf_put_data(clear, e_nonce, e_nonce_len);
1669
1670 #ifdef CONFIG_TESTING_OPTIONS
1671 skip_e_nonce:
1672 if (dpp_test == DPP_TEST_NO_CONFIG_OBJ_CONF_RESP) {
1673 wpa_printf(MSG_INFO, "DPP: TESTING - Config Object");
1674 goto skip_config_obj;
1675 }
1676 #endif /* CONFIG_TESTING_OPTIONS */
1677
1678 if (conf) {
1679 wpabuf_put_le16(clear, DPP_ATTR_CONFIG_OBJ);
1680 wpabuf_put_le16(clear, wpabuf_len(conf));
1681 wpabuf_put_buf(clear, conf);
1682 }
1683 if (auth->peer_version >= 2 && conf2) {
1684 wpabuf_put_le16(clear, DPP_ATTR_CONFIG_OBJ);
1685 wpabuf_put_le16(clear, wpabuf_len(conf2));
1686 wpabuf_put_buf(clear, conf2);
1687 } else if (conf2) {
1688 wpa_printf(MSG_DEBUG,
1689 "DPP: Second Config Object available, but peer does not support more than one");
1690 }
1691 if (env_data) {
1692 wpabuf_put_le16(clear, DPP_ATTR_ENVELOPED_DATA);
1693 wpabuf_put_le16(clear, wpabuf_len(env_data));
1694 wpabuf_put_buf(clear, env_data);
1695 }
1696
1697 if (auth->peer_version >= 2 && auth->send_conn_status &&
1698 netrole == DPP_NETROLE_STA) {
1699 wpa_printf(MSG_DEBUG, "DPP: sendConnStatus");
1700 wpabuf_put_le16(clear, DPP_ATTR_SEND_CONN_STATUS);
1701 wpabuf_put_le16(clear, 0);
1702 }
1703
1704 #ifdef CONFIG_TESTING_OPTIONS
1705 skip_config_obj:
1706 if (dpp_test == DPP_TEST_NO_STATUS_CONF_RESP) {
1707 wpa_printf(MSG_INFO, "DPP: TESTING - Status");
1708 goto skip_status;
1709 }
1710 if (dpp_test == DPP_TEST_INVALID_STATUS_CONF_RESP) {
1711 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
1712 status = 255;
1713 }
1714 #endif /* CONFIG_TESTING_OPTIONS */
1715
1716 /* DPP Status */
1717 dpp_build_attr_status(msg, status);
1718
1719 #ifdef CONFIG_TESTING_OPTIONS
1720 skip_status:
1721 #endif /* CONFIG_TESTING_OPTIONS */
1722
1723 addr[0] = wpabuf_head(msg);
1724 len[0] = wpabuf_len(msg);
1725 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
1726
1727 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
1728 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
1729 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
1730
1731 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
1732 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
1733 wpabuf_head(clear), wpabuf_len(clear),
1734 1, addr, len, wrapped) < 0)
1735 goto fail;
1736 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
1737 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
1738
1739 #ifdef CONFIG_TESTING_OPTIONS
1740 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP) {
1741 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
1742 dpp_build_attr_status(msg, DPP_STATUS_OK);
1743 }
1744 skip_wrapped_data:
1745 #endif /* CONFIG_TESTING_OPTIONS */
1746
1747 wpa_hexdump_buf(MSG_DEBUG,
1748 "DPP: Configuration Response attributes", msg);
1749 out:
1750 wpabuf_clear_free(conf);
1751 wpabuf_clear_free(conf2);
1752 wpabuf_clear_free(env_data);
1753 wpabuf_clear_free(clear);
1754
1755 return msg;
1756 fail:
1757 wpabuf_free(msg);
1758 msg = NULL;
1759 goto out;
1760 }
1761
1762
1763 struct wpabuf *
dpp_conf_req_rx(struct dpp_authentication * auth,const u8 * attr_start,size_t attr_len)1764 dpp_conf_req_rx(struct dpp_authentication *auth, const u8 *attr_start,
1765 size_t attr_len)
1766 {
1767 const u8 *wrapped_data, *e_nonce, *config_attr;
1768 u16 wrapped_data_len, e_nonce_len, config_attr_len;
1769 u8 *unwrapped = NULL;
1770 size_t unwrapped_len = 0;
1771 struct wpabuf *resp = NULL;
1772 struct json_token *root = NULL, *token;
1773 enum dpp_netrole netrole;
1774
1775 #ifdef CONFIG_TESTING_OPTIONS
1776 if (dpp_test == DPP_TEST_STOP_AT_CONF_REQ) {
1777 wpa_printf(MSG_INFO,
1778 "DPP: TESTING - stop at Config Request");
1779 return NULL;
1780 }
1781 #endif /* CONFIG_TESTING_OPTIONS */
1782
1783 if (dpp_check_attrs(attr_start, attr_len) < 0) {
1784 dpp_auth_fail(auth, "Invalid attribute in config request");
1785 return NULL;
1786 }
1787
1788 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
1789 &wrapped_data_len);
1790 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
1791 dpp_auth_fail(auth,
1792 "Missing or invalid required Wrapped Data attribute");
1793 return NULL;
1794 }
1795
1796 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
1797 wrapped_data, wrapped_data_len);
1798 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
1799 unwrapped = os_malloc(unwrapped_len);
1800 if (!unwrapped)
1801 return NULL;
1802 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
1803 wrapped_data, wrapped_data_len,
1804 0, NULL, NULL, unwrapped) < 0) {
1805 dpp_auth_fail(auth, "AES-SIV decryption failed");
1806 goto fail;
1807 }
1808 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
1809 unwrapped, unwrapped_len);
1810
1811 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
1812 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
1813 goto fail;
1814 }
1815
1816 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
1817 DPP_ATTR_ENROLLEE_NONCE,
1818 &e_nonce_len);
1819 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
1820 dpp_auth_fail(auth,
1821 "Missing or invalid Enrollee Nonce attribute");
1822 goto fail;
1823 }
1824 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
1825 os_memcpy(auth->e_nonce, e_nonce, e_nonce_len);
1826
1827 config_attr = dpp_get_attr(unwrapped, unwrapped_len,
1828 DPP_ATTR_CONFIG_ATTR_OBJ,
1829 &config_attr_len);
1830 if (!config_attr) {
1831 dpp_auth_fail(auth,
1832 "Missing or invalid Config Attributes attribute");
1833 goto fail;
1834 }
1835 wpa_hexdump_ascii(MSG_DEBUG, "DPP: Config Attributes",
1836 config_attr, config_attr_len);
1837
1838 root = json_parse((const char *) config_attr, config_attr_len);
1839 if (!root) {
1840 dpp_auth_fail(auth, "Could not parse Config Attributes");
1841 goto fail;
1842 }
1843
1844 token = json_get_member(root, "name");
1845 if (!token || token->type != JSON_STRING) {
1846 dpp_auth_fail(auth, "No Config Attributes - name");
1847 goto fail;
1848 }
1849 wpa_printf(MSG_DEBUG, "DPP: Enrollee name = '%s'", token->string);
1850
1851 token = json_get_member(root, "wi-fi_tech");
1852 if (!token || token->type != JSON_STRING) {
1853 dpp_auth_fail(auth, "No Config Attributes - wi-fi_tech");
1854 goto fail;
1855 }
1856 wpa_printf(MSG_DEBUG, "DPP: wi-fi_tech = '%s'", token->string);
1857 if (os_strcmp(token->string, "infra") != 0) {
1858 wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech '%s'",
1859 token->string);
1860 dpp_auth_fail(auth, "Unsupported wi-fi_tech");
1861 goto fail;
1862 }
1863
1864 token = json_get_member(root, "netRole");
1865 if (!token || token->type != JSON_STRING) {
1866 dpp_auth_fail(auth, "No Config Attributes - netRole");
1867 goto fail;
1868 }
1869 wpa_printf(MSG_DEBUG, "DPP: netRole = '%s'", token->string);
1870 if (os_strcmp(token->string, "sta") == 0) {
1871 netrole = DPP_NETROLE_STA;
1872 } else if (os_strcmp(token->string, "ap") == 0) {
1873 netrole = DPP_NETROLE_AP;
1874 } else if (os_strcmp(token->string, "configurator") == 0) {
1875 netrole = DPP_NETROLE_CONFIGURATOR;
1876 } else {
1877 wpa_printf(MSG_DEBUG, "DPP: Unsupported netRole '%s'",
1878 token->string);
1879 dpp_auth_fail(auth, "Unsupported netRole");
1880 goto fail;
1881 }
1882
1883 token = json_get_member(root, "mudurl");
1884 if (token && token->type == JSON_STRING) {
1885 wpa_printf(MSG_DEBUG, "DPP: mudurl = '%s'", token->string);
1886 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_MUD_URL "%s",
1887 token->string);
1888 }
1889
1890 token = json_get_member(root, "bandSupport");
1891 auth->band_list_size = 0;
1892 if (token && token->type == JSON_ARRAY) {
1893 int *opclass = NULL;
1894 char txt[200], *pos, *end;
1895 int i, res;
1896
1897 memset(auth->band_list, 0, sizeof(auth->band_list));
1898 wpa_printf(MSG_DEBUG, "DPP: bandSupport");
1899 token = token->child;
1900 while (token) {
1901 if (token->type != JSON_NUMBER) {
1902 wpa_printf(MSG_DEBUG,
1903 "DPP: Invalid bandSupport array member type");
1904 } else {
1905 if (auth->band_list_size < DPP_MAX_CHANNELS) {
1906 auth->band_list[auth->band_list_size++] = token->number;
1907 }
1908 wpa_printf(MSG_DEBUG,
1909 "DPP: Supported global operating class: %d",
1910 token->number);
1911 int_array_add_unique(&opclass, token->number);
1912 }
1913 token = token->sibling;
1914 }
1915
1916 txt[0] = '\0';
1917 pos = txt;
1918 end = txt + sizeof(txt);
1919 for (i = 0; opclass && opclass[i]; i++) {
1920 res = os_snprintf(pos, end - pos, "%s%d",
1921 pos == txt ? "" : ",", opclass[i]);
1922 if (os_snprintf_error(end - pos, res)) {
1923 *pos = '\0';
1924 break;
1925 }
1926 pos += res;
1927 }
1928 os_free(opclass);
1929 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_BAND_SUPPORT "%s",
1930 txt);
1931 }
1932
1933 resp = dpp_build_conf_resp(auth, e_nonce, e_nonce_len, netrole);
1934
1935 fail:
1936 json_free(root);
1937 os_free(unwrapped);
1938 return resp;
1939 }
1940
1941
dpp_parse_cred_legacy(struct dpp_config_obj * conf,struct json_token * cred)1942 static int dpp_parse_cred_legacy(struct dpp_config_obj *conf,
1943 struct json_token *cred)
1944 {
1945 struct json_token *pass, *psk_hex;
1946
1947 wpa_printf(MSG_DEBUG, "DPP: Legacy akm=psk credential");
1948
1949 pass = json_get_member(cred, "pass");
1950 psk_hex = json_get_member(cred, "psk_hex");
1951
1952 if (pass && pass->type == JSON_STRING) {
1953 size_t len = os_strlen(pass->string);
1954
1955 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Legacy passphrase",
1956 pass->string, len);
1957 if (len < 8 || len > 63)
1958 return -1;
1959 os_strlcpy(conf->passphrase, pass->string,
1960 sizeof(conf->passphrase));
1961 } else if (psk_hex && psk_hex->type == JSON_STRING) {
1962 if (dpp_akm_sae(conf->akm) && !dpp_akm_psk(conf->akm)) {
1963 wpa_printf(MSG_DEBUG,
1964 "DPP: Unexpected psk_hex with akm=sae");
1965 return -1;
1966 }
1967 if (os_strlen(psk_hex->string) != PMK_LEN * 2 ||
1968 hexstr2bin(psk_hex->string, conf->psk, PMK_LEN) < 0) {
1969 wpa_printf(MSG_DEBUG, "DPP: Invalid psk_hex encoding");
1970 return -1;
1971 }
1972 wpa_hexdump_key(MSG_DEBUG, "DPP: Legacy PSK",
1973 conf->psk, PMK_LEN);
1974 conf->psk_set = 1;
1975 } else {
1976 wpa_printf(MSG_DEBUG, "DPP: No pass or psk_hex strings found");
1977 return -1;
1978 }
1979
1980 if (dpp_akm_sae(conf->akm) && !conf->passphrase[0]) {
1981 wpa_printf(MSG_DEBUG, "DPP: No pass for sae found");
1982 return -1;
1983 }
1984
1985 return 0;
1986 }
1987
1988
dpp_parse_jwk(struct json_token * jwk,const struct dpp_curve_params ** key_curve)1989 EVP_PKEY * dpp_parse_jwk(struct json_token *jwk,
1990 const struct dpp_curve_params **key_curve)
1991 {
1992 struct json_token *token;
1993 const struct dpp_curve_params *curve;
1994 struct wpabuf *x = NULL, *y = NULL;
1995 EC_GROUP *group;
1996 EVP_PKEY *pkey = NULL;
1997
1998 token = json_get_member(jwk, "kty");
1999 if (!token || token->type != JSON_STRING) {
2000 wpa_printf(MSG_DEBUG, "DPP: No kty in JWK");
2001 goto fail;
2002 }
2003 if (os_strcmp(token->string, "EC") != 0) {
2004 wpa_printf(MSG_DEBUG, "DPP: Unexpected JWK kty '%s'",
2005 token->string);
2006 goto fail;
2007 }
2008
2009 token = json_get_member(jwk, "crv");
2010 if (!token || token->type != JSON_STRING) {
2011 wpa_printf(MSG_DEBUG, "DPP: No crv in JWK");
2012 goto fail;
2013 }
2014 curve = dpp_get_curve_jwk_crv(token->string);
2015 if (!curve) {
2016 wpa_printf(MSG_DEBUG, "DPP: Unsupported JWK crv '%s'",
2017 token->string);
2018 goto fail;
2019 }
2020
2021 x = json_get_member_base64url(jwk, "x");
2022 if (!x) {
2023 wpa_printf(MSG_DEBUG, "DPP: No x in JWK");
2024 goto fail;
2025 }
2026 wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK x", x);
2027 if (wpabuf_len(x) != curve->prime_len) {
2028 wpa_printf(MSG_DEBUG,
2029 "DPP: Unexpected JWK x length %u (expected %u for curve %s)",
2030 (unsigned int) wpabuf_len(x),
2031 (unsigned int) curve->prime_len, curve->name);
2032 goto fail;
2033 }
2034
2035 y = json_get_member_base64url(jwk, "y");
2036 if (!y) {
2037 wpa_printf(MSG_DEBUG, "DPP: No y in JWK");
2038 goto fail;
2039 }
2040 wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK y", y);
2041 if (wpabuf_len(y) != curve->prime_len) {
2042 wpa_printf(MSG_DEBUG,
2043 "DPP: Unexpected JWK y length %u (expected %u for curve %s)",
2044 (unsigned int) wpabuf_len(y),
2045 (unsigned int) curve->prime_len, curve->name);
2046 goto fail;
2047 }
2048
2049 group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
2050 if (!group) {
2051 wpa_printf(MSG_DEBUG, "DPP: Could not prepare group for JWK");
2052 goto fail;
2053 }
2054
2055 pkey = dpp_set_pubkey_point_group(group, wpabuf_head(x), wpabuf_head(y),
2056 wpabuf_len(x));
2057 EC_GROUP_free(group);
2058 *key_curve = curve;
2059
2060 fail:
2061 wpabuf_free(x);
2062 wpabuf_free(y);
2063
2064 return pkey;
2065 }
2066
2067
dpp_key_expired(const char * timestamp,os_time_t * expiry)2068 int dpp_key_expired(const char *timestamp, os_time_t *expiry)
2069 {
2070 struct os_time now;
2071 unsigned int year, month, day, hour, min, sec;
2072 os_time_t utime;
2073 const char *pos;
2074
2075 /* ISO 8601 date and time:
2076 * <date>T<time>
2077 * YYYY-MM-DDTHH:MM:SSZ
2078 * YYYY-MM-DDTHH:MM:SS+03:00
2079 */
2080 if (os_strlen(timestamp) < 19) {
2081 wpa_printf(MSG_DEBUG,
2082 "DPP: Too short timestamp - assume expired key");
2083 return 1;
2084 }
2085 if (sscanf(timestamp, "%04u-%02u-%02uT%02u:%02u:%02u",
2086 &year, &month, &day, &hour, &min, &sec) != 6) {
2087 wpa_printf(MSG_DEBUG,
2088 "DPP: Failed to parse expiration day - assume expired key");
2089 return 1;
2090 }
2091
2092 if (os_mktime(year, month, day, hour, min, sec, &utime) < 0) {
2093 wpa_printf(MSG_DEBUG,
2094 "DPP: Invalid date/time information - assume expired key");
2095 return 1;
2096 }
2097
2098 pos = timestamp + 19;
2099 if (*pos == 'Z' || *pos == '\0') {
2100 /* In UTC - no need to adjust */
2101 } else if (*pos == '-' || *pos == '+') {
2102 int items;
2103
2104 /* Adjust local time to UTC */
2105 items = sscanf(pos + 1, "%02u:%02u", &hour, &min);
2106 if (items < 1) {
2107 wpa_printf(MSG_DEBUG,
2108 "DPP: Invalid time zone designator (%s) - assume expired key",
2109 pos);
2110 return 1;
2111 }
2112 if (*pos == '-')
2113 utime += 3600 * hour;
2114 if (*pos == '+')
2115 utime -= 3600 * hour;
2116 if (items > 1) {
2117 if (*pos == '-')
2118 utime += 60 * min;
2119 if (*pos == '+')
2120 utime -= 60 * min;
2121 }
2122 } else {
2123 wpa_printf(MSG_DEBUG,
2124 "DPP: Invalid time zone designator (%s) - assume expired key",
2125 pos);
2126 return 1;
2127 }
2128 if (expiry)
2129 *expiry = utime;
2130
2131 if (os_get_time(&now) < 0) {
2132 wpa_printf(MSG_DEBUG,
2133 "DPP: Cannot get current time - assume expired key");
2134 return 1;
2135 }
2136
2137 if (now.sec > utime) {
2138 wpa_printf(MSG_DEBUG, "DPP: Key has expired (%lu < %lu)",
2139 utime, now.sec);
2140 return 1;
2141 }
2142
2143 return 0;
2144 }
2145
2146
dpp_parse_connector(struct dpp_authentication * auth,struct dpp_config_obj * conf,const unsigned char * payload,u16 payload_len)2147 static int dpp_parse_connector(struct dpp_authentication *auth,
2148 struct dpp_config_obj *conf,
2149 const unsigned char *payload,
2150 u16 payload_len)
2151 {
2152 struct json_token *root, *groups, *netkey, *token;
2153 int ret = -1;
2154 EVP_PKEY *key = NULL;
2155 const struct dpp_curve_params *curve;
2156 unsigned int rules = 0;
2157
2158 root = json_parse((const char *) payload, payload_len);
2159 if (!root) {
2160 wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
2161 goto fail;
2162 }
2163
2164 groups = json_get_member(root, "groups");
2165 if (!groups || groups->type != JSON_ARRAY) {
2166 wpa_printf(MSG_DEBUG, "DPP: No groups array found");
2167 goto skip_groups;
2168 }
2169 for (token = groups->child; token; token = token->sibling) {
2170 struct json_token *id, *role;
2171
2172 id = json_get_member(token, "groupId");
2173 if (!id || id->type != JSON_STRING) {
2174 wpa_printf(MSG_DEBUG, "DPP: Missing groupId string");
2175 goto fail;
2176 }
2177
2178 role = json_get_member(token, "netRole");
2179 if (!role || role->type != JSON_STRING) {
2180 wpa_printf(MSG_DEBUG, "DPP: Missing netRole string");
2181 goto fail;
2182 }
2183 wpa_printf(MSG_DEBUG,
2184 "DPP: connector group: groupId='%s' netRole='%s'",
2185 id->string, role->string);
2186 rules++;
2187 }
2188 skip_groups:
2189
2190 if (!rules) {
2191 wpa_printf(MSG_DEBUG,
2192 "DPP: Connector includes no groups");
2193 goto fail;
2194 }
2195
2196 token = json_get_member(root, "expiry");
2197 if (!token || token->type != JSON_STRING) {
2198 wpa_printf(MSG_DEBUG,
2199 "DPP: No expiry string found - connector does not expire");
2200 } else {
2201 wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
2202 if (dpp_key_expired(token->string,
2203 &auth->net_access_key_expiry)) {
2204 wpa_printf(MSG_DEBUG,
2205 "DPP: Connector (netAccessKey) has expired");
2206 goto fail;
2207 }
2208 }
2209
2210 netkey = json_get_member(root, "netAccessKey");
2211 if (!netkey || netkey->type != JSON_OBJECT) {
2212 wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
2213 goto fail;
2214 }
2215
2216 key = dpp_parse_jwk(netkey, &curve);
2217 if (!key)
2218 goto fail;
2219 dpp_debug_print_key("DPP: Received netAccessKey", key);
2220
2221 if (EVP_PKEY_cmp(key, auth->own_protocol_key) != 1) {
2222 wpa_printf(MSG_DEBUG,
2223 "DPP: netAccessKey in connector does not match own protocol key");
2224 #ifdef CONFIG_TESTING_OPTIONS
2225 if (auth->ignore_netaccesskey_mismatch) {
2226 wpa_printf(MSG_DEBUG,
2227 "DPP: TESTING - skip netAccessKey mismatch");
2228 } else {
2229 goto fail;
2230 }
2231 #else /* CONFIG_TESTING_OPTIONS */
2232 goto fail;
2233 #endif /* CONFIG_TESTING_OPTIONS */
2234 }
2235
2236 ret = 0;
2237 fail:
2238 EVP_PKEY_free(key);
2239 json_free(root);
2240 return ret;
2241 }
2242
2243
dpp_copy_csign(struct dpp_config_obj * conf,EVP_PKEY * csign)2244 static void dpp_copy_csign(struct dpp_config_obj *conf, EVP_PKEY *csign)
2245 {
2246 unsigned char *der = NULL;
2247 int der_len;
2248
2249 der_len = i2d_PUBKEY(csign, &der);
2250 if (der_len <= 0)
2251 return;
2252 wpabuf_free(conf->c_sign_key);
2253 conf->c_sign_key = wpabuf_alloc_copy(der, der_len);
2254 OPENSSL_free(der);
2255 }
2256
2257
dpp_copy_netaccesskey(struct dpp_authentication * auth,struct dpp_config_obj * conf)2258 static void dpp_copy_netaccesskey(struct dpp_authentication *auth,
2259 struct dpp_config_obj *conf)
2260 {
2261 unsigned char *der = NULL;
2262 int der_len;
2263 EC_KEY *eckey;
2264 EVP_PKEY *own_key;
2265
2266 own_key = auth->own_protocol_key;
2267 #ifdef CONFIG_DPP2
2268 if (auth->reconfig_connector_key == DPP_CONFIG_REUSEKEY &&
2269 auth->reconfig_old_protocol_key)
2270 own_key = auth->reconfig_old_protocol_key;
2271 #endif /* CONFIG_DPP2 */
2272 eckey = EVP_PKEY_get1_EC_KEY(own_key);
2273 if (!eckey)
2274 return;
2275
2276 der_len = i2d_ECPrivateKey(eckey, &der);
2277 if (der_len <= 0) {
2278 EC_KEY_free(eckey);
2279 return;
2280 }
2281 wpabuf_free(auth->net_access_key);
2282 auth->net_access_key = wpabuf_alloc_copy(der, der_len);
2283 OPENSSL_free(der);
2284 EC_KEY_free(eckey);
2285 }
2286
2287
dpp_parse_cred_dpp(struct dpp_authentication * auth,struct dpp_config_obj * conf,struct json_token * cred)2288 static int dpp_parse_cred_dpp(struct dpp_authentication *auth,
2289 struct dpp_config_obj *conf,
2290 struct json_token *cred)
2291 {
2292 struct dpp_signed_connector_info info;
2293 struct json_token *token, *csign;
2294 int ret = -1;
2295 EVP_PKEY *csign_pub = NULL;
2296 const struct dpp_curve_params *key_curve = NULL;
2297 const char *signed_connector;
2298
2299 os_memset(&info, 0, sizeof(info));
2300
2301 if (dpp_akm_psk(conf->akm) || dpp_akm_sae(conf->akm)) {
2302 wpa_printf(MSG_DEBUG,
2303 "DPP: Legacy credential included in Connector credential");
2304 if (dpp_parse_cred_legacy(conf, cred) < 0)
2305 return -1;
2306 }
2307
2308 wpa_printf(MSG_DEBUG, "DPP: Connector credential");
2309
2310 csign = json_get_member(cred, "csign");
2311 if (!csign || csign->type != JSON_OBJECT) {
2312 wpa_printf(MSG_DEBUG, "DPP: No csign JWK in JSON");
2313 goto fail;
2314 }
2315
2316 csign_pub = dpp_parse_jwk(csign, &key_curve);
2317 if (!csign_pub) {
2318 wpa_printf(MSG_DEBUG, "DPP: Failed to parse csign JWK");
2319 goto fail;
2320 }
2321 dpp_debug_print_key("DPP: Received C-sign-key", csign_pub);
2322
2323 token = json_get_member(cred, "signedConnector");
2324 if (!token || token->type != JSON_STRING) {
2325 wpa_printf(MSG_DEBUG, "DPP: No signedConnector string found");
2326 goto fail;
2327 }
2328 wpa_hexdump_ascii(MSG_DEBUG, "DPP: signedConnector",
2329 token->string, os_strlen(token->string));
2330 signed_connector = token->string;
2331
2332 if (os_strchr(signed_connector, '"') ||
2333 os_strchr(signed_connector, '\n')) {
2334 wpa_printf(MSG_DEBUG,
2335 "DPP: Unexpected character in signedConnector");
2336 goto fail;
2337 }
2338
2339 if (dpp_process_signed_connector(&info, csign_pub,
2340 signed_connector) != DPP_STATUS_OK)
2341 goto fail;
2342
2343 if (dpp_parse_connector(auth, conf,
2344 info.payload, info.payload_len) < 0) {
2345 wpa_printf(MSG_DEBUG, "DPP: Failed to parse connector");
2346 goto fail;
2347 }
2348
2349 os_free(conf->connector);
2350 conf->connector = os_strdup(signed_connector);
2351
2352 dpp_copy_csign(conf, csign_pub);
2353 if (dpp_akm_dpp(conf->akm) || auth->peer_version >= 2)
2354 dpp_copy_netaccesskey(auth, conf);
2355
2356 ret = 0;
2357 fail:
2358 EVP_PKEY_free(csign_pub);
2359 os_free(info.payload);
2360 return ret;
2361 }
2362
2363
dpp_akm_str(enum dpp_akm akm)2364 const char * dpp_akm_str(enum dpp_akm akm)
2365 {
2366 switch (akm) {
2367 case DPP_AKM_DPP:
2368 return "dpp";
2369 case DPP_AKM_PSK:
2370 return "psk";
2371 case DPP_AKM_SAE:
2372 return "sae";
2373 case DPP_AKM_PSK_SAE:
2374 return "psk+sae";
2375 case DPP_AKM_SAE_DPP:
2376 return "dpp+sae";
2377 case DPP_AKM_PSK_SAE_DPP:
2378 return "dpp+psk+sae";
2379 default:
2380 return "??";
2381 }
2382 }
2383
2384
dpp_akm_selector_str(enum dpp_akm akm)2385 const char * dpp_akm_selector_str(enum dpp_akm akm)
2386 {
2387 switch (akm) {
2388 case DPP_AKM_DPP:
2389 return "506F9A02";
2390 case DPP_AKM_PSK:
2391 return "000FAC02+000FAC06";
2392 case DPP_AKM_SAE:
2393 return "000FAC08";
2394 case DPP_AKM_PSK_SAE:
2395 return "000FAC02+000FAC06+000FAC08";
2396 case DPP_AKM_SAE_DPP:
2397 return "506F9A02+000FAC08";
2398 case DPP_AKM_PSK_SAE_DPP:
2399 return "506F9A02+000FAC08+000FAC02+000FAC06";
2400 default:
2401 return "??";
2402 }
2403 }
2404
2405
dpp_akm_from_str(const char * akm)2406 static enum dpp_akm dpp_akm_from_str(const char *akm)
2407 {
2408 const char *pos;
2409 int dpp = 0, psk = 0, sae = 0;
2410
2411 if (os_strcmp(akm, "psk") == 0)
2412 return DPP_AKM_PSK;
2413 if (os_strcmp(akm, "sae") == 0)
2414 return DPP_AKM_SAE;
2415 if (os_strcmp(akm, "psk+sae") == 0)
2416 return DPP_AKM_PSK_SAE;
2417 if (os_strcmp(akm, "dpp") == 0)
2418 return DPP_AKM_DPP;
2419 if (os_strcmp(akm, "dpp+sae") == 0)
2420 return DPP_AKM_SAE_DPP;
2421 if (os_strcmp(akm, "dpp+psk+sae") == 0)
2422 return DPP_AKM_PSK_SAE_DPP;
2423
2424 pos = akm;
2425 while (*pos) {
2426 if (os_strlen(pos) < 8)
2427 break;
2428 if (os_strncasecmp(pos, "506F9A02", 8) == 0)
2429 dpp = 1;
2430 else if (os_strncasecmp(pos, "000FAC02", 8) == 0)
2431 psk = 1;
2432 else if (os_strncasecmp(pos, "000FAC06", 8) == 0)
2433 psk = 1;
2434 else if (os_strncasecmp(pos, "000FAC08", 8) == 0)
2435 sae = 1;
2436 pos += 8;
2437 if (*pos != '+')
2438 break;
2439 pos++;
2440 }
2441
2442 if (dpp && psk && sae)
2443 return DPP_AKM_PSK_SAE_DPP;
2444 if (dpp && sae)
2445 return DPP_AKM_SAE_DPP;
2446 if (dpp)
2447 return DPP_AKM_DPP;
2448 if (psk && sae)
2449 return DPP_AKM_PSK_SAE;
2450 if (sae)
2451 return DPP_AKM_SAE;
2452 if (psk)
2453 return DPP_AKM_PSK;
2454
2455 return DPP_AKM_UNKNOWN;
2456 }
2457
2458
dpp_parse_conf_obj(struct dpp_authentication * auth,const u8 * conf_obj,u16 conf_obj_len)2459 static int dpp_parse_conf_obj(struct dpp_authentication *auth,
2460 const u8 *conf_obj, u16 conf_obj_len)
2461 {
2462 int ret = -1;
2463 struct json_token *root, *token, *discovery, *cred;
2464 struct dpp_config_obj *conf;
2465 struct wpabuf *ssid64 = NULL;
2466 int legacy;
2467
2468 root = json_parse((const char *) conf_obj, conf_obj_len);
2469 if (!root)
2470 return -1;
2471 if (root->type != JSON_OBJECT) {
2472 dpp_auth_fail(auth, "JSON root is not an object");
2473 goto fail;
2474 }
2475
2476 token = json_get_member(root, "wi-fi_tech");
2477 if (!token || token->type != JSON_STRING) {
2478 dpp_auth_fail(auth, "No wi-fi_tech string value found");
2479 goto fail;
2480 }
2481 if (os_strcmp(token->string, "infra") != 0) {
2482 wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech value: '%s'",
2483 token->string);
2484 dpp_auth_fail(auth, "Unsupported wi-fi_tech value");
2485 goto fail;
2486 }
2487
2488 discovery = json_get_member(root, "discovery");
2489 if (!discovery || discovery->type != JSON_OBJECT) {
2490 dpp_auth_fail(auth, "No discovery object in JSON");
2491 goto fail;
2492 }
2493
2494 ssid64 = json_get_member_base64url(discovery, "ssid64");
2495 if (ssid64) {
2496 wpa_hexdump_ascii(MSG_DEBUG, "DPP: discovery::ssid64",
2497 wpabuf_head(ssid64), wpabuf_len(ssid64));
2498 if (wpabuf_len(ssid64) > SSID_MAX_LEN) {
2499 dpp_auth_fail(auth, "Too long discovery::ssid64 value");
2500 goto fail;
2501 }
2502 } else {
2503 token = json_get_member(discovery, "ssid");
2504 if (!token || token->type != JSON_STRING) {
2505 dpp_auth_fail(auth,
2506 "No discovery::ssid string value found");
2507 goto fail;
2508 }
2509 wpa_hexdump_ascii(MSG_DEBUG, "DPP: discovery::ssid",
2510 token->string, os_strlen(token->string));
2511 if (os_strlen(token->string) > SSID_MAX_LEN) {
2512 dpp_auth_fail(auth,
2513 "Too long discovery::ssid string value");
2514 goto fail;
2515 }
2516 }
2517
2518 if (auth->num_conf_obj == DPP_MAX_CONF_OBJ) {
2519 wpa_printf(MSG_DEBUG,
2520 "DPP: No room for this many Config Objects - ignore this one");
2521 ret = 0;
2522 goto fail;
2523 }
2524 conf = &auth->conf_obj[auth->num_conf_obj++];
2525
2526 if (ssid64) {
2527 conf->ssid_len = wpabuf_len(ssid64);
2528 os_memcpy(conf->ssid, wpabuf_head(ssid64), conf->ssid_len);
2529 } else {
2530 conf->ssid_len = os_strlen(token->string);
2531 os_memcpy(conf->ssid, token->string, conf->ssid_len);
2532 }
2533
2534 token = json_get_member(discovery, "ssid_charset");
2535 if (token && token->type == JSON_NUMBER) {
2536 conf->ssid_charset = token->number;
2537 wpa_printf(MSG_DEBUG, "DPP: ssid_charset=%d",
2538 conf->ssid_charset);
2539 }
2540
2541 cred = json_get_member(root, "cred");
2542 if (!cred || cred->type != JSON_OBJECT) {
2543 dpp_auth_fail(auth, "No cred object in JSON");
2544 goto fail;
2545 }
2546
2547 token = json_get_member(cred, "akm");
2548 if (!token || token->type != JSON_STRING) {
2549 dpp_auth_fail(auth, "No cred::akm string value found");
2550 goto fail;
2551 }
2552 conf->akm = dpp_akm_from_str(token->string);
2553
2554 legacy = dpp_akm_legacy(conf->akm);
2555 if (legacy && auth->peer_version >= 2) {
2556 struct json_token *csign, *s_conn;
2557
2558 csign = json_get_member(cred, "csign");
2559 s_conn = json_get_member(cred, "signedConnector");
2560 if (csign && csign->type == JSON_OBJECT &&
2561 s_conn && s_conn->type == JSON_STRING)
2562 legacy = 0;
2563 }
2564 if (legacy) {
2565 if (dpp_parse_cred_legacy(conf, cred) < 0)
2566 goto fail;
2567 } else if (dpp_akm_dpp(conf->akm) ||
2568 (auth->peer_version >= 2 && dpp_akm_legacy(conf->akm))) {
2569 if (dpp_parse_cred_dpp(auth, conf, cred) < 0)
2570 goto fail;
2571 } else {
2572 wpa_printf(MSG_DEBUG, "DPP: Unsupported akm: %s",
2573 token->string);
2574 dpp_auth_fail(auth, "Unsupported akm");
2575 goto fail;
2576 }
2577
2578 wpa_printf(MSG_DEBUG, "DPP: JSON parsing completed successfully");
2579 ret = 0;
2580 fail:
2581 wpabuf_free(ssid64);
2582 json_free(root);
2583 return ret;
2584 }
2585
2586
dpp_conf_resp_rx(struct dpp_authentication * auth,const struct wpabuf * resp)2587 int dpp_conf_resp_rx(struct dpp_authentication *auth,
2588 const struct wpabuf *resp)
2589 {
2590 const u8 *wrapped_data, *e_nonce, *status, *conf_obj;
2591 u16 wrapped_data_len, e_nonce_len, status_len, conf_obj_len;
2592 const u8 *env_data;
2593 u16 env_data_len;
2594 const u8 *addr[1];
2595 size_t len[1];
2596 u8 *unwrapped = NULL;
2597 size_t unwrapped_len = 0;
2598 int ret = -1;
2599
2600 auth->conf_resp_status = 255;
2601
2602 if (dpp_check_attrs(wpabuf_head(resp), wpabuf_len(resp)) < 0) {
2603 dpp_auth_fail(auth, "Invalid attribute in config response");
2604 return -1;
2605 }
2606
2607 wrapped_data = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
2608 DPP_ATTR_WRAPPED_DATA,
2609 &wrapped_data_len);
2610 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
2611 dpp_auth_fail(auth,
2612 "Missing or invalid required Wrapped Data attribute");
2613 return -1;
2614 }
2615
2616 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
2617 wrapped_data, wrapped_data_len);
2618 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
2619 unwrapped = os_malloc(unwrapped_len);
2620 if (!unwrapped)
2621 return -1;
2622
2623 addr[0] = wpabuf_head(resp);
2624 len[0] = wrapped_data - 4 - (const u8 *) wpabuf_head(resp);
2625 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
2626
2627 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
2628 wrapped_data, wrapped_data_len,
2629 1, addr, len, unwrapped) < 0) {
2630 dpp_auth_fail(auth, "AES-SIV decryption failed");
2631 goto fail;
2632 }
2633 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
2634 unwrapped, unwrapped_len);
2635
2636 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
2637 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
2638 goto fail;
2639 }
2640
2641 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
2642 DPP_ATTR_ENROLLEE_NONCE,
2643 &e_nonce_len);
2644 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
2645 dpp_auth_fail(auth,
2646 "Missing or invalid Enrollee Nonce attribute");
2647 goto fail;
2648 }
2649 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
2650 if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
2651 dpp_auth_fail(auth, "Enrollee Nonce mismatch");
2652 goto fail;
2653 }
2654
2655 status = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
2656 DPP_ATTR_STATUS, &status_len);
2657 if (!status || status_len < 1) {
2658 dpp_auth_fail(auth,
2659 "Missing or invalid required DPP Status attribute");
2660 goto fail;
2661 }
2662 auth->conf_resp_status = status[0];
2663 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
2664 if (status[0] != DPP_STATUS_OK) {
2665 dpp_auth_fail(auth, "Configurator rejected configuration");
2666 goto fail;
2667 }
2668
2669 env_data = dpp_get_attr(unwrapped, unwrapped_len,
2670 DPP_ATTR_ENVELOPED_DATA, &env_data_len);
2671 #ifdef CONFIG_DPP2
2672 if (env_data &&
2673 dpp_conf_resp_env_data(auth, env_data, env_data_len) < 0)
2674 goto fail;
2675 #endif /* CONFIG_DPP2 */
2676
2677 conf_obj = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_CONFIG_OBJ,
2678 &conf_obj_len);
2679 if (!conf_obj && !env_data) {
2680 dpp_auth_fail(auth,
2681 "Missing required Configuration Object attribute");
2682 goto fail;
2683 }
2684 while (conf_obj) {
2685 wpa_hexdump_ascii(MSG_DEBUG, "DPP: configurationObject JSON",
2686 conf_obj, conf_obj_len);
2687 if (dpp_parse_conf_obj(auth, conf_obj, conf_obj_len) < 0)
2688 goto fail;
2689 conf_obj = dpp_get_attr_next(conf_obj, unwrapped, unwrapped_len,
2690 DPP_ATTR_CONFIG_OBJ,
2691 &conf_obj_len);
2692 }
2693
2694 #ifdef CONFIG_DPP2
2695 status = dpp_get_attr(unwrapped, unwrapped_len,
2696 DPP_ATTR_SEND_CONN_STATUS, &status_len);
2697 if (status) {
2698 wpa_printf(MSG_DEBUG,
2699 "DPP: Configurator requested connection status result");
2700 auth->conn_status_requested = 1;
2701 }
2702 #endif /* CONFIG_DPP2 */
2703
2704 ret = 0;
2705
2706 fail:
2707 os_free(unwrapped);
2708 return ret;
2709 }
2710
2711
2712 #ifdef CONFIG_DPP2
2713
dpp_conf_result_rx(struct dpp_authentication * auth,const u8 * hdr,const u8 * attr_start,size_t attr_len)2714 enum dpp_status_error dpp_conf_result_rx(struct dpp_authentication *auth,
2715 const u8 *hdr,
2716 const u8 *attr_start, size_t attr_len)
2717 {
2718 const u8 *wrapped_data, *status, *e_nonce;
2719 u16 wrapped_data_len, status_len, e_nonce_len;
2720 const u8 *addr[2];
2721 size_t len[2];
2722 u8 *unwrapped = NULL;
2723 size_t unwrapped_len = 0;
2724 enum dpp_status_error ret = 256;
2725
2726 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
2727 &wrapped_data_len);
2728 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
2729 dpp_auth_fail(auth,
2730 "Missing or invalid required Wrapped Data attribute");
2731 goto fail;
2732 }
2733 wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
2734 wrapped_data, wrapped_data_len);
2735
2736 attr_len = wrapped_data - 4 - attr_start;
2737
2738 addr[0] = hdr;
2739 len[0] = DPP_HDR_LEN;
2740 addr[1] = attr_start;
2741 len[1] = attr_len;
2742 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
2743 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
2744 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
2745 wrapped_data, wrapped_data_len);
2746 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
2747 unwrapped = os_malloc(unwrapped_len);
2748 if (!unwrapped)
2749 goto fail;
2750 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
2751 wrapped_data, wrapped_data_len,
2752 2, addr, len, unwrapped) < 0) {
2753 dpp_auth_fail(auth, "AES-SIV decryption failed");
2754 goto fail;
2755 }
2756 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
2757 unwrapped, unwrapped_len);
2758
2759 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
2760 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
2761 goto fail;
2762 }
2763
2764 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
2765 DPP_ATTR_ENROLLEE_NONCE,
2766 &e_nonce_len);
2767 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
2768 dpp_auth_fail(auth,
2769 "Missing or invalid Enrollee Nonce attribute");
2770 goto fail;
2771 }
2772 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
2773 if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
2774 dpp_auth_fail(auth, "Enrollee Nonce mismatch");
2775 wpa_hexdump(MSG_DEBUG, "DPP: Expected Enrollee Nonce",
2776 auth->e_nonce, e_nonce_len);
2777 goto fail;
2778 }
2779
2780 status = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_STATUS,
2781 &status_len);
2782 if (!status || status_len < 1) {
2783 dpp_auth_fail(auth,
2784 "Missing or invalid required DPP Status attribute");
2785 goto fail;
2786 }
2787 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
2788 ret = status[0];
2789
2790 fail:
2791 bin_clear_free(unwrapped, unwrapped_len);
2792 return ret;
2793 }
2794
2795
dpp_build_conf_result(struct dpp_authentication * auth,enum dpp_status_error status)2796 struct wpabuf * dpp_build_conf_result(struct dpp_authentication *auth,
2797 enum dpp_status_error status)
2798 {
2799 struct wpabuf *msg, *clear;
2800 size_t nonce_len, clear_len, attr_len;
2801 const u8 *addr[2];
2802 size_t len[2];
2803 u8 *wrapped;
2804
2805 nonce_len = auth->curve->nonce_len;
2806 clear_len = 5 + 4 + nonce_len;
2807 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
2808 clear = wpabuf_alloc(clear_len);
2809 msg = dpp_alloc_msg(DPP_PA_CONFIGURATION_RESULT, attr_len);
2810 if (!clear || !msg)
2811 goto fail;
2812
2813 /* DPP Status */
2814 dpp_build_attr_status(clear, status);
2815
2816 /* E-nonce */
2817 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
2818 wpabuf_put_le16(clear, nonce_len);
2819 wpabuf_put_data(clear, auth->e_nonce, nonce_len);
2820
2821 /* OUI, OUI type, Crypto Suite, DPP frame type */
2822 addr[0] = wpabuf_head_u8(msg) + 2;
2823 len[0] = 3 + 1 + 1 + 1;
2824 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
2825
2826 /* Attributes before Wrapped Data (none) */
2827 addr[1] = wpabuf_put(msg, 0);
2828 len[1] = 0;
2829 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
2830
2831 /* Wrapped Data */
2832 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
2833 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
2834 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
2835
2836 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
2837 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
2838 wpabuf_head(clear), wpabuf_len(clear),
2839 2, addr, len, wrapped) < 0)
2840 goto fail;
2841
2842 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Result attributes", msg);
2843 wpabuf_free(clear);
2844 return msg;
2845 fail:
2846 wpabuf_free(clear);
2847 wpabuf_free(msg);
2848 return NULL;
2849 }
2850
2851
valid_channel_list(const char * val)2852 static int valid_channel_list(const char *val)
2853 {
2854 while (*val) {
2855 if (!((*val >= '0' && *val <= '9') ||
2856 *val == '/' || *val == ','))
2857 return 0;
2858 val++;
2859 }
2860
2861 return 1;
2862 }
2863
2864
dpp_conn_status_result_rx(struct dpp_authentication * auth,const u8 * hdr,const u8 * attr_start,size_t attr_len,u8 * ssid,size_t * ssid_len,char ** channel_list)2865 enum dpp_status_error dpp_conn_status_result_rx(struct dpp_authentication *auth,
2866 const u8 *hdr,
2867 const u8 *attr_start,
2868 size_t attr_len,
2869 u8 *ssid, size_t *ssid_len,
2870 char **channel_list)
2871 {
2872 const u8 *wrapped_data, *status, *e_nonce;
2873 u16 wrapped_data_len, status_len, e_nonce_len;
2874 const u8 *addr[2];
2875 size_t len[2];
2876 u8 *unwrapped = NULL;
2877 size_t unwrapped_len = 0;
2878 enum dpp_status_error ret = 256;
2879 struct json_token *root = NULL, *token;
2880 struct wpabuf *ssid64;
2881
2882 *ssid_len = 0;
2883 *channel_list = NULL;
2884
2885 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
2886 &wrapped_data_len);
2887 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
2888 dpp_auth_fail(auth,
2889 "Missing or invalid required Wrapped Data attribute");
2890 goto fail;
2891 }
2892 wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
2893 wrapped_data, wrapped_data_len);
2894
2895 attr_len = wrapped_data - 4 - attr_start;
2896
2897 addr[0] = hdr;
2898 len[0] = DPP_HDR_LEN;
2899 addr[1] = attr_start;
2900 len[1] = attr_len;
2901 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
2902 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
2903 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
2904 wrapped_data, wrapped_data_len);
2905 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
2906 unwrapped = os_malloc(unwrapped_len);
2907 if (!unwrapped)
2908 goto fail;
2909 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
2910 wrapped_data, wrapped_data_len,
2911 2, addr, len, unwrapped) < 0) {
2912 dpp_auth_fail(auth, "AES-SIV decryption failed");
2913 goto fail;
2914 }
2915 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
2916 unwrapped, unwrapped_len);
2917
2918 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
2919 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
2920 goto fail;
2921 }
2922
2923 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
2924 DPP_ATTR_ENROLLEE_NONCE,
2925 &e_nonce_len);
2926 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
2927 dpp_auth_fail(auth,
2928 "Missing or invalid Enrollee Nonce attribute");
2929 goto fail;
2930 }
2931 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
2932 if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
2933 dpp_auth_fail(auth, "Enrollee Nonce mismatch");
2934 wpa_hexdump(MSG_DEBUG, "DPP: Expected Enrollee Nonce",
2935 auth->e_nonce, e_nonce_len);
2936 goto fail;
2937 }
2938
2939 status = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_CONN_STATUS,
2940 &status_len);
2941 if (!status) {
2942 dpp_auth_fail(auth,
2943 "Missing required DPP Connection Status attribute");
2944 goto fail;
2945 }
2946 wpa_hexdump_ascii(MSG_DEBUG, "DPP: connStatus JSON",
2947 status, status_len);
2948
2949 root = json_parse((const char *) status, status_len);
2950 if (!root) {
2951 dpp_auth_fail(auth, "Could not parse connStatus");
2952 goto fail;
2953 }
2954
2955 ssid64 = json_get_member_base64url(root, "ssid64");
2956 if (ssid64 && wpabuf_len(ssid64) <= SSID_MAX_LEN) {
2957 *ssid_len = wpabuf_len(ssid64);
2958 os_memcpy(ssid, wpabuf_head(ssid64), *ssid_len);
2959 }
2960 wpabuf_free(ssid64);
2961
2962 token = json_get_member(root, "channelList");
2963 if (token && token->type == JSON_STRING &&
2964 valid_channel_list(token->string))
2965 *channel_list = os_strdup(token->string);
2966
2967 token = json_get_member(root, "result");
2968 if (!token || token->type != JSON_NUMBER) {
2969 dpp_auth_fail(auth, "No connStatus - result");
2970 goto fail;
2971 }
2972 wpa_printf(MSG_DEBUG, "DPP: result %d", token->number);
2973 ret = token->number;
2974
2975 fail:
2976 json_free(root);
2977 bin_clear_free(unwrapped, unwrapped_len);
2978 return ret;
2979 }
2980
2981
dpp_build_conn_status(enum dpp_status_error result,const u8 * ssid,size_t ssid_len,const char * channel_list)2982 struct wpabuf * dpp_build_conn_status(enum dpp_status_error result,
2983 const u8 *ssid, size_t ssid_len,
2984 const char *channel_list)
2985 {
2986 struct wpabuf *json;
2987
2988 json = wpabuf_alloc(1000);
2989 if (!json)
2990 return NULL;
2991 json_start_object(json, NULL);
2992 json_add_int(json, "result", result);
2993 if (ssid) {
2994 json_value_sep(json);
2995 if (json_add_base64url(json, "ssid64", ssid, ssid_len) < 0) {
2996 wpabuf_free(json);
2997 return NULL;
2998 }
2999 }
3000 if (channel_list) {
3001 json_value_sep(json);
3002 json_add_string(json, "channelList", channel_list);
3003 }
3004 json_end_object(json);
3005 wpa_hexdump_ascii(MSG_DEBUG, "DPP: connStatus JSON",
3006 wpabuf_head(json), wpabuf_len(json));
3007
3008 return json;
3009 }
3010
3011
dpp_build_conn_status_result(struct dpp_authentication * auth,enum dpp_status_error result,const u8 * ssid,size_t ssid_len,const char * channel_list)3012 struct wpabuf * dpp_build_conn_status_result(struct dpp_authentication *auth,
3013 enum dpp_status_error result,
3014 const u8 *ssid, size_t ssid_len,
3015 const char *channel_list)
3016 {
3017 struct wpabuf *msg = NULL, *clear = NULL, *json;
3018 size_t nonce_len, clear_len, attr_len;
3019 const u8 *addr[2];
3020 size_t len[2];
3021 u8 *wrapped;
3022
3023 json = dpp_build_conn_status(result, ssid, ssid_len, channel_list);
3024 if (!json)
3025 return NULL;
3026
3027 nonce_len = auth->curve->nonce_len;
3028 clear_len = 5 + 4 + nonce_len + 4 + wpabuf_len(json);
3029 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
3030 clear = wpabuf_alloc(clear_len);
3031 msg = dpp_alloc_msg(DPP_PA_CONNECTION_STATUS_RESULT, attr_len);
3032 if (!clear || !msg)
3033 goto fail;
3034
3035 /* E-nonce */
3036 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
3037 wpabuf_put_le16(clear, nonce_len);
3038 wpabuf_put_data(clear, auth->e_nonce, nonce_len);
3039
3040 /* DPP Connection Status */
3041 wpabuf_put_le16(clear, DPP_ATTR_CONN_STATUS);
3042 wpabuf_put_le16(clear, wpabuf_len(json));
3043 wpabuf_put_buf(clear, json);
3044
3045 /* OUI, OUI type, Crypto Suite, DPP frame type */
3046 addr[0] = wpabuf_head_u8(msg) + 2;
3047 len[0] = 3 + 1 + 1 + 1;
3048 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3049
3050 /* Attributes before Wrapped Data (none) */
3051 addr[1] = wpabuf_put(msg, 0);
3052 len[1] = 0;
3053 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3054
3055 /* Wrapped Data */
3056 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
3057 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
3058 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
3059
3060 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
3061 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
3062 wpabuf_head(clear), wpabuf_len(clear),
3063 2, addr, len, wrapped) < 0)
3064 goto fail;
3065
3066 wpa_hexdump_buf(MSG_DEBUG, "DPP: Connection Status Result attributes",
3067 msg);
3068 wpabuf_free(json);
3069 wpabuf_free(clear);
3070 return msg;
3071 fail:
3072 wpabuf_free(json);
3073 wpabuf_free(clear);
3074 wpabuf_free(msg);
3075 return NULL;
3076 }
3077
3078 #endif /* CONFIG_DPP2 */
3079
3080
dpp_configurator_free(struct dpp_configurator * conf)3081 void dpp_configurator_free(struct dpp_configurator *conf)
3082 {
3083 if (!conf)
3084 return;
3085 EVP_PKEY_free(conf->csign);
3086 os_free(conf->kid);
3087 os_free(conf->connector);
3088 EVP_PKEY_free(conf->connector_key);
3089 os_free(conf);
3090 }
3091
3092
dpp_configurator_get_key(const struct dpp_configurator * conf,char * buf,size_t buflen)3093 int dpp_configurator_get_key(const struct dpp_configurator *conf, char *buf,
3094 size_t buflen)
3095 {
3096 EC_KEY *eckey;
3097 int key_len, ret = -1;
3098 unsigned char *key = NULL;
3099
3100 if (!conf->csign)
3101 return -1;
3102
3103 eckey = EVP_PKEY_get1_EC_KEY(conf->csign);
3104 if (!eckey)
3105 return -1;
3106
3107 key_len = i2d_ECPrivateKey(eckey, &key);
3108 if (key_len > 0)
3109 ret = wpa_snprintf_hex(buf, buflen, key, key_len);
3110
3111 EC_KEY_free(eckey);
3112 OPENSSL_free(key);
3113 return ret;
3114 }
3115
3116
dpp_configurator_gen_kid(struct dpp_configurator * conf)3117 static int dpp_configurator_gen_kid(struct dpp_configurator *conf)
3118 {
3119 struct wpabuf *csign_pub = NULL;
3120 const u8 *addr[1];
3121 size_t len[1];
3122 int res;
3123
3124 csign_pub = dpp_get_pubkey_point(conf->csign, 1);
3125 if (!csign_pub) {
3126 wpa_printf(MSG_INFO, "DPP: Failed to extract C-sign-key");
3127 return -1;
3128 }
3129
3130 /* kid = SHA256(ANSI X9.63 uncompressed C-sign-key) */
3131 addr[0] = wpabuf_head(csign_pub);
3132 len[0] = wpabuf_len(csign_pub);
3133 res = sha256_vector(1, addr, len, conf->kid_hash);
3134 wpabuf_free(csign_pub);
3135 if (res < 0) {
3136 wpa_printf(MSG_DEBUG,
3137 "DPP: Failed to derive kid for C-sign-key");
3138 return -1;
3139 }
3140
3141 conf->kid = base64_url_encode(conf->kid_hash, sizeof(conf->kid_hash),
3142 NULL);
3143 return conf->kid ? 0 : -1;
3144 }
3145
3146
3147 struct dpp_configurator *
dpp_keygen_configurator(const char * curve,const u8 * privkey,size_t privkey_len)3148 dpp_keygen_configurator(const char *curve, const u8 *privkey,
3149 size_t privkey_len)
3150 {
3151 struct dpp_configurator *conf;
3152
3153 conf = os_zalloc(sizeof(*conf));
3154 if (!conf)
3155 return NULL;
3156
3157 conf->curve = dpp_get_curve_name(curve);
3158 if (!conf->curve) {
3159 wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s", curve);
3160 os_free(conf);
3161 return NULL;
3162 }
3163
3164 if (privkey)
3165 conf->csign = dpp_set_keypair(&conf->curve, privkey,
3166 privkey_len);
3167 else
3168 conf->csign = dpp_gen_keypair(conf->curve);
3169 if (!conf->csign)
3170 goto fail;
3171 conf->own = 1;
3172
3173 if (dpp_configurator_gen_kid(conf) < 0)
3174 goto fail;
3175 return conf;
3176 fail:
3177 dpp_configurator_free(conf);
3178 return NULL;
3179 }
3180
3181
dpp_configurator_own_config(struct dpp_authentication * auth,const char * curve,int ap)3182 int dpp_configurator_own_config(struct dpp_authentication *auth,
3183 const char *curve, int ap)
3184 {
3185 struct wpabuf *conf_obj;
3186 int ret = -1;
3187
3188 if (!auth->conf) {
3189 wpa_printf(MSG_DEBUG, "DPP: No configurator specified");
3190 return -1;
3191 }
3192
3193 auth->curve = dpp_get_curve_name(curve);
3194 if (!auth->curve) {
3195 wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s", curve);
3196 return -1;
3197 }
3198
3199 wpa_printf(MSG_DEBUG,
3200 "DPP: Building own configuration/connector with curve %s",
3201 auth->curve->name);
3202
3203 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
3204 if (!auth->own_protocol_key)
3205 return -1;
3206 dpp_copy_netaccesskey(auth, &auth->conf_obj[0]);
3207 auth->peer_protocol_key = auth->own_protocol_key;
3208 dpp_copy_csign(&auth->conf_obj[0], auth->conf->csign);
3209
3210 conf_obj = dpp_build_conf_obj(auth, ap, 0);
3211 if (!conf_obj) {
3212 wpabuf_free(auth->conf_obj[0].c_sign_key);
3213 auth->conf_obj[0].c_sign_key = NULL;
3214 goto fail;
3215 }
3216 ret = dpp_parse_conf_obj(auth, wpabuf_head(conf_obj),
3217 wpabuf_len(conf_obj));
3218 fail:
3219 wpabuf_free(conf_obj);
3220 auth->peer_protocol_key = NULL;
3221 return ret;
3222 }
3223
3224
dpp_compatible_netrole(const char * role1,const char * role2)3225 static int dpp_compatible_netrole(const char *role1, const char *role2)
3226 {
3227 return (os_strcmp(role1, "sta") == 0 && os_strcmp(role2, "ap") == 0) ||
3228 (os_strcmp(role1, "ap") == 0 && os_strcmp(role2, "sta") == 0);
3229 }
3230
3231
dpp_connector_compatible_group(struct json_token * root,const char * group_id,const char * net_role,bool reconfig)3232 static int dpp_connector_compatible_group(struct json_token *root,
3233 const char *group_id,
3234 const char *net_role,
3235 bool reconfig)
3236 {
3237 struct json_token *groups, *token;
3238
3239 groups = json_get_member(root, "groups");
3240 if (!groups || groups->type != JSON_ARRAY)
3241 return 0;
3242
3243 for (token = groups->child; token; token = token->sibling) {
3244 struct json_token *id, *role;
3245
3246 id = json_get_member(token, "groupId");
3247 if (!id || id->type != JSON_STRING)
3248 continue;
3249
3250 role = json_get_member(token, "netRole");
3251 if (!role || role->type != JSON_STRING)
3252 continue;
3253
3254 if (os_strcmp(id->string, "*") != 0 &&
3255 os_strcmp(group_id, "*") != 0 &&
3256 os_strcmp(id->string, group_id) != 0)
3257 continue;
3258
3259 if (reconfig && os_strcmp(net_role, "configurator") == 0)
3260 return 1;
3261 if (!reconfig && dpp_compatible_netrole(role->string, net_role))
3262 return 1;
3263 }
3264
3265 return 0;
3266 }
3267
3268
dpp_connector_match_groups(struct json_token * own_root,struct json_token * peer_root,bool reconfig)3269 int dpp_connector_match_groups(struct json_token *own_root,
3270 struct json_token *peer_root, bool reconfig)
3271 {
3272 struct json_token *groups, *token;
3273
3274 groups = json_get_member(peer_root, "groups");
3275 if (!groups || groups->type != JSON_ARRAY) {
3276 wpa_printf(MSG_DEBUG, "DPP: No peer groups array found");
3277 return 0;
3278 }
3279
3280 for (token = groups->child; token; token = token->sibling) {
3281 struct json_token *id, *role;
3282
3283 id = json_get_member(token, "groupId");
3284 if (!id || id->type != JSON_STRING) {
3285 wpa_printf(MSG_DEBUG,
3286 "DPP: Missing peer groupId string");
3287 continue;
3288 }
3289
3290 role = json_get_member(token, "netRole");
3291 if (!role || role->type != JSON_STRING) {
3292 wpa_printf(MSG_DEBUG,
3293 "DPP: Missing peer groups::netRole string");
3294 continue;
3295 }
3296 wpa_printf(MSG_DEBUG,
3297 "DPP: peer connector group: groupId='%s' netRole='%s'",
3298 id->string, role->string);
3299 if (dpp_connector_compatible_group(own_root, id->string,
3300 role->string, reconfig)) {
3301 wpa_printf(MSG_DEBUG,
3302 "DPP: Compatible group/netRole in own connector");
3303 return 1;
3304 }
3305 }
3306
3307 return 0;
3308 }
3309
3310
dpp_parse_own_connector(const char * own_connector)3311 struct json_token * dpp_parse_own_connector(const char *own_connector)
3312 {
3313 unsigned char *own_conn;
3314 size_t own_conn_len;
3315 const char *pos, *end;
3316 struct json_token *own_root;
3317
3318 pos = os_strchr(own_connector, '.');
3319 if (!pos) {
3320 wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the first dot (.)");
3321 return NULL;
3322 }
3323 pos++;
3324 end = os_strchr(pos, '.');
3325 if (!end) {
3326 wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the second dot (.)");
3327 return NULL;
3328 }
3329 own_conn = base64_url_decode(pos, end - pos, &own_conn_len);
3330 if (!own_conn) {
3331 wpa_printf(MSG_DEBUG,
3332 "DPP: Failed to base64url decode own signedConnector JWS Payload");
3333 return NULL;
3334 }
3335
3336 own_root = json_parse((const char *) own_conn, own_conn_len);
3337 os_free(own_conn);
3338 if (!own_root)
3339 wpa_printf(MSG_DEBUG, "DPP: Failed to parse local connector");
3340
3341 return own_root;
3342 }
3343
3344
3345 enum dpp_status_error
dpp_peer_intro(struct dpp_introduction * intro,const char * own_connector,const u8 * net_access_key,size_t net_access_key_len,const u8 * csign_key,size_t csign_key_len,const u8 * peer_connector,size_t peer_connector_len,os_time_t * expiry)3346 dpp_peer_intro(struct dpp_introduction *intro, const char *own_connector,
3347 const u8 *net_access_key, size_t net_access_key_len,
3348 const u8 *csign_key, size_t csign_key_len,
3349 const u8 *peer_connector, size_t peer_connector_len,
3350 os_time_t *expiry)
3351 {
3352 struct json_token *root = NULL, *netkey, *token;
3353 struct json_token *own_root = NULL;
3354 enum dpp_status_error ret = 255, res;
3355 EVP_PKEY *own_key = NULL, *peer_key = NULL;
3356 struct wpabuf *own_key_pub = NULL;
3357 const struct dpp_curve_params *curve, *own_curve;
3358 struct dpp_signed_connector_info info;
3359 size_t Nx_len;
3360 u8 Nx[DPP_MAX_SHARED_SECRET_LEN];
3361
3362 os_memset(intro, 0, sizeof(*intro));
3363 os_memset(&info, 0, sizeof(info));
3364 if (expiry)
3365 *expiry = 0;
3366
3367 own_key = dpp_set_keypair(&own_curve, net_access_key,
3368 net_access_key_len);
3369 if (!own_key) {
3370 wpa_printf(MSG_ERROR, "DPP: Failed to parse own netAccessKey");
3371 goto fail;
3372 }
3373
3374 own_root = dpp_parse_own_connector(own_connector);
3375 if (!own_root)
3376 goto fail;
3377
3378 res = dpp_check_signed_connector(&info, csign_key, csign_key_len,
3379 peer_connector, peer_connector_len);
3380 if (res != DPP_STATUS_OK) {
3381 ret = res;
3382 goto fail;
3383 }
3384
3385 root = json_parse((const char *) info.payload, info.payload_len);
3386 if (!root) {
3387 wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
3388 ret = DPP_STATUS_INVALID_CONNECTOR;
3389 goto fail;
3390 }
3391
3392 if (!dpp_connector_match_groups(own_root, root, false)) {
3393 wpa_printf(MSG_DEBUG,
3394 "DPP: Peer connector does not include compatible group netrole with own connector");
3395 ret = DPP_STATUS_NO_MATCH;
3396 goto fail;
3397 }
3398
3399 token = json_get_member(root, "expiry");
3400 if (!token || token->type != JSON_STRING) {
3401 wpa_printf(MSG_DEBUG,
3402 "DPP: No expiry string found - connector does not expire");
3403 } else {
3404 wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
3405 if (dpp_key_expired(token->string, expiry)) {
3406 wpa_printf(MSG_DEBUG,
3407 "DPP: Connector (netAccessKey) has expired");
3408 ret = DPP_STATUS_INVALID_CONNECTOR;
3409 goto fail;
3410 }
3411 }
3412
3413 netkey = json_get_member(root, "netAccessKey");
3414 if (!netkey || netkey->type != JSON_OBJECT) {
3415 wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
3416 ret = DPP_STATUS_INVALID_CONNECTOR;
3417 goto fail;
3418 }
3419
3420 peer_key = dpp_parse_jwk(netkey, &curve);
3421 if (!peer_key) {
3422 ret = DPP_STATUS_INVALID_CONNECTOR;
3423 goto fail;
3424 }
3425 dpp_debug_print_key("DPP: Received netAccessKey", peer_key);
3426
3427 if (own_curve != curve) {
3428 wpa_printf(MSG_DEBUG,
3429 "DPP: Mismatching netAccessKey curves (%s != %s)",
3430 own_curve->name, curve->name);
3431 ret = DPP_STATUS_INVALID_CONNECTOR;
3432 goto fail;
3433 }
3434
3435 /* ECDH: N = nk * PK */
3436 if (dpp_ecdh(own_key, peer_key, Nx, &Nx_len) < 0)
3437 goto fail;
3438
3439 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
3440 Nx, Nx_len);
3441
3442 /* PMK = HKDF(<>, "DPP PMK", N.x) */
3443 if (dpp_derive_pmk(Nx, Nx_len, intro->pmk, curve->hash_len) < 0) {
3444 wpa_printf(MSG_ERROR, "DPP: Failed to derive PMK");
3445 goto fail;
3446 }
3447 intro->pmk_len = curve->hash_len;
3448
3449 /* PMKID = Truncate-128(H(min(NK.x, PK.x) | max(NK.x, PK.x))) */
3450 if (dpp_derive_pmkid(curve, own_key, peer_key, intro->pmkid) < 0) {
3451 wpa_printf(MSG_ERROR, "DPP: Failed to derive PMKID");
3452 goto fail;
3453 }
3454
3455 ret = DPP_STATUS_OK;
3456 fail:
3457 if (ret != DPP_STATUS_OK)
3458 os_memset(intro, 0, sizeof(*intro));
3459 os_memset(Nx, 0, sizeof(Nx));
3460 os_free(info.payload);
3461 EVP_PKEY_free(own_key);
3462 wpabuf_free(own_key_pub);
3463 EVP_PKEY_free(peer_key);
3464 json_free(root);
3465 json_free(own_root);
3466 return ret;
3467 }
3468
3469
dpp_next_id(struct dpp_global * dpp)3470 unsigned int dpp_next_id(struct dpp_global *dpp)
3471 {
3472 struct dpp_bootstrap_info *bi;
3473 unsigned int max_id = 0;
3474
3475 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
3476 if (bi->id > max_id)
3477 max_id = bi->id;
3478 }
3479 return max_id + 1;
3480 }
3481
3482
dpp_bootstrap_del(struct dpp_global * dpp,unsigned int id)3483 static int dpp_bootstrap_del(struct dpp_global *dpp, unsigned int id)
3484 {
3485 struct dpp_bootstrap_info *bi, *tmp;
3486 int found = 0;
3487
3488 if (!dpp)
3489 return -1;
3490
3491 dl_list_for_each_safe(bi, tmp, &dpp->bootstrap,
3492 struct dpp_bootstrap_info, list) {
3493 if (id && bi->id != id)
3494 continue;
3495 found = 1;
3496 #ifdef CONFIG_DPP2
3497 if (dpp->remove_bi)
3498 dpp->remove_bi(dpp->cb_ctx, bi);
3499 #endif /* CONFIG_DPP2 */
3500 dl_list_del(&bi->list);
3501 dpp_bootstrap_info_free(bi);
3502 }
3503
3504 if (id == 0)
3505 return 0; /* flush succeeds regardless of entries found */
3506 return found ? 0 : -1;
3507 }
3508
3509
dpp_add_qr_code(struct dpp_global * dpp,const char * uri)3510 struct dpp_bootstrap_info * dpp_add_qr_code(struct dpp_global *dpp,
3511 const char *uri)
3512 {
3513 struct dpp_bootstrap_info *bi;
3514
3515 if (!dpp)
3516 return NULL;
3517
3518 bi = dpp_parse_uri(uri);
3519 if (!bi)
3520 return NULL;
3521
3522 bi->type = DPP_BOOTSTRAP_QR_CODE;
3523 bi->id = dpp_next_id(dpp);
3524 dl_list_add(&dpp->bootstrap, &bi->list);
3525 return bi;
3526 }
3527
3528
dpp_add_nfc_uri(struct dpp_global * dpp,const char * uri)3529 struct dpp_bootstrap_info * dpp_add_nfc_uri(struct dpp_global *dpp,
3530 const char *uri)
3531 {
3532 struct dpp_bootstrap_info *bi;
3533
3534 if (!dpp)
3535 return NULL;
3536
3537 bi = dpp_parse_uri(uri);
3538 if (!bi)
3539 return NULL;
3540
3541 bi->type = DPP_BOOTSTRAP_NFC_URI;
3542 bi->id = dpp_next_id(dpp);
3543 dl_list_add(&dpp->bootstrap, &bi->list);
3544 return bi;
3545 }
3546
3547
dpp_bootstrap_gen(struct dpp_global * dpp,const char * cmd)3548 int dpp_bootstrap_gen(struct dpp_global *dpp, const char *cmd)
3549 {
3550 char *mac = NULL, *info = NULL, *curve = NULL;
3551 char *key = NULL;
3552 u8 *privkey = NULL;
3553 size_t privkey_len = 0;
3554 int ret = -1;
3555 struct dpp_bootstrap_info *bi;
3556
3557 if (!dpp)
3558 return -1;
3559
3560 bi = os_zalloc(sizeof(*bi));
3561 if (!bi)
3562 goto fail;
3563
3564 if (os_strstr(cmd, "type=qrcode"))
3565 bi->type = DPP_BOOTSTRAP_QR_CODE;
3566 else if (os_strstr(cmd, "type=pkex"))
3567 bi->type = DPP_BOOTSTRAP_PKEX;
3568 else if (os_strstr(cmd, "type=nfc-uri"))
3569 bi->type = DPP_BOOTSTRAP_NFC_URI;
3570 else
3571 goto fail;
3572
3573 bi->chan = get_param(cmd, " chan=");
3574 mac = get_param(cmd, " mac=");
3575 info = get_param(cmd, " info=");
3576 curve = get_param(cmd, " curve=");
3577 key = get_param(cmd, " key=");
3578
3579 if (key) {
3580 privkey_len = os_strlen(key) / 2;
3581 privkey = os_malloc(privkey_len);
3582 if (!privkey ||
3583 hexstr2bin(key, privkey, privkey_len) < 0)
3584 goto fail;
3585 }
3586
3587 if (dpp_keygen(bi, curve, privkey, privkey_len) < 0 ||
3588 dpp_parse_uri_chan_list(bi, bi->chan) < 0 ||
3589 dpp_parse_uri_mac(bi, mac) < 0 ||
3590 dpp_parse_uri_info(bi, info) < 0 ||
3591 dpp_gen_uri(bi) < 0)
3592 goto fail;
3593
3594 bi->id = dpp_next_id(dpp);
3595 dl_list_add(&dpp->bootstrap, &bi->list);
3596 ret = bi->id;
3597 bi = NULL;
3598 fail:
3599 os_free(curve);
3600 os_free(mac);
3601 os_free(info);
3602 str_clear_free(key);
3603 bin_clear_free(privkey, privkey_len);
3604 dpp_bootstrap_info_free(bi);
3605 return ret;
3606 }
3607
3608
3609 struct dpp_bootstrap_info *
dpp_bootstrap_get_id(struct dpp_global * dpp,unsigned int id)3610 dpp_bootstrap_get_id(struct dpp_global *dpp, unsigned int id)
3611 {
3612 struct dpp_bootstrap_info *bi;
3613
3614 if (!dpp)
3615 return NULL;
3616
3617 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
3618 if (bi->id == id)
3619 return bi;
3620 }
3621 return NULL;
3622 }
3623
3624
dpp_bootstrap_remove(struct dpp_global * dpp,const char * id)3625 int dpp_bootstrap_remove(struct dpp_global *dpp, const char *id)
3626 {
3627 unsigned int id_val;
3628
3629 if (os_strcmp(id, "*") == 0) {
3630 id_val = 0;
3631 } else {
3632 id_val = atoi(id);
3633 if (id_val == 0)
3634 return -1;
3635 }
3636
3637 return dpp_bootstrap_del(dpp, id_val);
3638 }
3639
3640
dpp_bootstrap_get_uri(struct dpp_global * dpp,unsigned int id)3641 const char * dpp_bootstrap_get_uri(struct dpp_global *dpp, unsigned int id)
3642 {
3643 struct dpp_bootstrap_info *bi;
3644
3645 bi = dpp_bootstrap_get_id(dpp, id);
3646 if (!bi)
3647 return NULL;
3648 return bi->uri;
3649 }
3650
3651
dpp_bootstrap_info(struct dpp_global * dpp,int id,char * reply,int reply_size)3652 int dpp_bootstrap_info(struct dpp_global *dpp, int id,
3653 char *reply, int reply_size)
3654 {
3655 struct dpp_bootstrap_info *bi;
3656 char pkhash[2 * SHA256_MAC_LEN + 1];
3657
3658 bi = dpp_bootstrap_get_id(dpp, id);
3659 if (!bi)
3660 return -1;
3661 wpa_snprintf_hex(pkhash, sizeof(pkhash), bi->pubkey_hash,
3662 SHA256_MAC_LEN);
3663 return os_snprintf(reply, reply_size, "type=%s\n"
3664 "mac_addr=" MACSTR "\n"
3665 "info=%s\n"
3666 "num_freq=%u\n"
3667 "use_freq=%u\n"
3668 "curve=%s\n"
3669 "pkhash=%s\n"
3670 "version=%d\n",
3671 dpp_bootstrap_type_txt(bi->type),
3672 MAC2STR(bi->mac_addr),
3673 bi->info ? bi->info : "",
3674 bi->num_freq,
3675 bi->num_freq == 1 ? bi->freq[0] : 0,
3676 bi->curve->name,
3677 pkhash,
3678 bi->version);
3679 }
3680
3681
dpp_bootstrap_set(struct dpp_global * dpp,int id,const char * params)3682 int dpp_bootstrap_set(struct dpp_global *dpp, int id, const char *params)
3683 {
3684 struct dpp_bootstrap_info *bi;
3685
3686 bi = dpp_bootstrap_get_id(dpp, id);
3687 if (!bi)
3688 return -1;
3689
3690 str_clear_free(bi->configurator_params);
3691
3692 if (params) {
3693 bi->configurator_params = os_strdup(params);
3694 return bi->configurator_params ? 0 : -1;
3695 }
3696
3697 bi->configurator_params = NULL;
3698 return 0;
3699 }
3700
3701
dpp_bootstrap_find_pair(struct dpp_global * dpp,const u8 * i_bootstrap,const u8 * r_bootstrap,struct dpp_bootstrap_info ** own_bi,struct dpp_bootstrap_info ** peer_bi)3702 void dpp_bootstrap_find_pair(struct dpp_global *dpp, const u8 *i_bootstrap,
3703 const u8 *r_bootstrap,
3704 struct dpp_bootstrap_info **own_bi,
3705 struct dpp_bootstrap_info **peer_bi)
3706 {
3707 struct dpp_bootstrap_info *bi;
3708
3709 *own_bi = NULL;
3710 *peer_bi = NULL;
3711 if (!dpp)
3712 return;
3713
3714 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
3715 if (!*own_bi && bi->own &&
3716 os_memcmp(bi->pubkey_hash, r_bootstrap,
3717 SHA256_MAC_LEN) == 0) {
3718 wpa_printf(MSG_DEBUG,
3719 "DPP: Found matching own bootstrapping information");
3720 *own_bi = bi;
3721 }
3722
3723 if (!*peer_bi && !bi->own &&
3724 os_memcmp(bi->pubkey_hash, i_bootstrap,
3725 SHA256_MAC_LEN) == 0) {
3726 wpa_printf(MSG_DEBUG,
3727 "DPP: Found matching peer bootstrapping information");
3728 *peer_bi = bi;
3729 }
3730
3731 if (*own_bi && *peer_bi)
3732 break;
3733 }
3734 }
3735
3736
3737 #ifdef CONFIG_DPP2
dpp_bootstrap_find_chirp(struct dpp_global * dpp,const u8 * hash)3738 struct dpp_bootstrap_info * dpp_bootstrap_find_chirp(struct dpp_global *dpp,
3739 const u8 *hash)
3740 {
3741 struct dpp_bootstrap_info *bi;
3742
3743 if (!dpp)
3744 return NULL;
3745
3746 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
3747 if (!bi->own && os_memcmp(bi->pubkey_hash_chirp, hash,
3748 SHA256_MAC_LEN) == 0)
3749 return bi;
3750 }
3751
3752 return NULL;
3753 }
3754 #endif /* CONFIG_DPP2 */
3755
3756
dpp_nfc_update_bi_channel(struct dpp_bootstrap_info * own_bi,struct dpp_bootstrap_info * peer_bi)3757 static int dpp_nfc_update_bi_channel(struct dpp_bootstrap_info *own_bi,
3758 struct dpp_bootstrap_info *peer_bi)
3759 {
3760 unsigned int i, freq = 0;
3761 enum hostapd_hw_mode mode;
3762 u8 op_class, channel;
3763 char chan[20];
3764
3765 if (peer_bi->num_freq == 0)
3766 return 0; /* no channel preference/constraint */
3767
3768 for (i = 0; i < peer_bi->num_freq; i++) {
3769 if (own_bi->num_freq == 0 ||
3770 freq_included(own_bi->freq, own_bi->num_freq,
3771 peer_bi->freq[i])) {
3772 freq = peer_bi->freq[i];
3773 break;
3774 }
3775 }
3776 if (!freq) {
3777 wpa_printf(MSG_DEBUG, "DPP: No common channel found");
3778 return -1;
3779 }
3780
3781 mode = ieee80211_freq_to_channel_ext(freq, 0, 0, &op_class, &channel);
3782 if (mode == NUM_HOSTAPD_MODES) {
3783 wpa_printf(MSG_DEBUG,
3784 "DPP: Could not determine operating class or channel number for %u MHz",
3785 freq);
3786 }
3787
3788 wpa_printf(MSG_DEBUG,
3789 "DPP: Selected %u MHz (op_class %u channel %u) as the negotiation channel based on information from NFC negotiated handover",
3790 freq, op_class, channel);
3791 os_snprintf(chan, sizeof(chan), "%u/%u", op_class, channel);
3792 os_free(own_bi->chan);
3793 own_bi->chan = os_strdup(chan);
3794 own_bi->freq[0] = freq;
3795 own_bi->num_freq = 1;
3796 os_free(peer_bi->chan);
3797 peer_bi->chan = os_strdup(chan);
3798 peer_bi->freq[0] = freq;
3799 peer_bi->num_freq = 1;
3800
3801 return dpp_gen_uri(own_bi);
3802 }
3803
3804
dpp_nfc_update_bi_key(struct dpp_bootstrap_info * own_bi,struct dpp_bootstrap_info * peer_bi)3805 static int dpp_nfc_update_bi_key(struct dpp_bootstrap_info *own_bi,
3806 struct dpp_bootstrap_info *peer_bi)
3807 {
3808 if (peer_bi->curve == own_bi->curve)
3809 return 0;
3810
3811 wpa_printf(MSG_DEBUG,
3812 "DPP: Update own bootstrapping key to match peer curve from NFC handover");
3813
3814 EVP_PKEY_free(own_bi->pubkey);
3815 own_bi->pubkey = NULL;
3816
3817 if (dpp_keygen(own_bi, peer_bi->curve->name, NULL, 0) < 0 ||
3818 dpp_gen_uri(own_bi) < 0)
3819 goto fail;
3820
3821 return 0;
3822 fail:
3823 dl_list_del(&own_bi->list);
3824 dpp_bootstrap_info_free(own_bi);
3825 return -1;
3826 }
3827
3828
dpp_nfc_update_bi(struct dpp_bootstrap_info * own_bi,struct dpp_bootstrap_info * peer_bi)3829 int dpp_nfc_update_bi(struct dpp_bootstrap_info *own_bi,
3830 struct dpp_bootstrap_info *peer_bi)
3831 {
3832 if (dpp_nfc_update_bi_channel(own_bi, peer_bi) < 0 ||
3833 dpp_nfc_update_bi_key(own_bi, peer_bi) < 0)
3834 return -1;
3835 return 0;
3836 }
3837
3838
dpp_next_configurator_id(struct dpp_global * dpp)3839 static unsigned int dpp_next_configurator_id(struct dpp_global *dpp)
3840 {
3841 struct dpp_configurator *conf;
3842 unsigned int max_id = 0;
3843
3844 dl_list_for_each(conf, &dpp->configurator, struct dpp_configurator,
3845 list) {
3846 if (conf->id > max_id)
3847 max_id = conf->id;
3848 }
3849 return max_id + 1;
3850 }
3851
3852
dpp_configurator_add(struct dpp_global * dpp,const char * cmd)3853 int dpp_configurator_add(struct dpp_global *dpp, const char *cmd)
3854 {
3855 char *curve = NULL;
3856 char *key = NULL;
3857 u8 *privkey = NULL;
3858 size_t privkey_len = 0;
3859 int ret = -1;
3860 struct dpp_configurator *conf = NULL;
3861
3862 curve = get_param(cmd, " curve=");
3863 key = get_param(cmd, " key=");
3864
3865 if (key) {
3866 privkey_len = os_strlen(key) / 2;
3867 privkey = os_malloc(privkey_len);
3868 if (!privkey ||
3869 hexstr2bin(key, privkey, privkey_len) < 0)
3870 goto fail;
3871 }
3872
3873 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
3874 if (!conf)
3875 goto fail;
3876
3877 conf->id = dpp_next_configurator_id(dpp);
3878 dl_list_add(&dpp->configurator, &conf->list);
3879 ret = conf->id;
3880 conf = NULL;
3881 fail:
3882 os_free(curve);
3883 str_clear_free(key);
3884 bin_clear_free(privkey, privkey_len);
3885 dpp_configurator_free(conf);
3886 return ret;
3887 }
3888
3889
dpp_configurator_del(struct dpp_global * dpp,unsigned int id)3890 static int dpp_configurator_del(struct dpp_global *dpp, unsigned int id)
3891 {
3892 struct dpp_configurator *conf, *tmp;
3893 int found = 0;
3894
3895 if (!dpp)
3896 return -1;
3897
3898 dl_list_for_each_safe(conf, tmp, &dpp->configurator,
3899 struct dpp_configurator, list) {
3900 if (id && conf->id != id)
3901 continue;
3902 found = 1;
3903 dl_list_del(&conf->list);
3904 dpp_configurator_free(conf);
3905 }
3906
3907 if (id == 0)
3908 return 0; /* flush succeeds regardless of entries found */
3909 return found ? 0 : -1;
3910 }
3911
3912
dpp_configurator_remove(struct dpp_global * dpp,const char * id)3913 int dpp_configurator_remove(struct dpp_global *dpp, const char *id)
3914 {
3915 unsigned int id_val;
3916
3917 if (os_strcmp(id, "*") == 0) {
3918 id_val = 0;
3919 } else {
3920 id_val = atoi(id);
3921 if (id_val == 0)
3922 return -1;
3923 }
3924
3925 return dpp_configurator_del(dpp, id_val);
3926 }
3927
3928
dpp_configurator_get_key_id(struct dpp_global * dpp,unsigned int id,char * buf,size_t buflen)3929 int dpp_configurator_get_key_id(struct dpp_global *dpp, unsigned int id,
3930 char *buf, size_t buflen)
3931 {
3932 struct dpp_configurator *conf;
3933
3934 conf = dpp_configurator_get_id(dpp, id);
3935 if (!conf)
3936 return -1;
3937
3938 return dpp_configurator_get_key(conf, buf, buflen);
3939 }
3940
3941
3942 #ifdef CONFIG_DPP2
3943
dpp_configurator_from_backup(struct dpp_global * dpp,struct dpp_asymmetric_key * key)3944 int dpp_configurator_from_backup(struct dpp_global *dpp,
3945 struct dpp_asymmetric_key *key)
3946 {
3947 struct dpp_configurator *conf;
3948 const EC_KEY *eckey;
3949 const EC_GROUP *group;
3950 int nid;
3951 const struct dpp_curve_params *curve;
3952
3953 if (!key->csign)
3954 return -1;
3955 eckey = EVP_PKEY_get0_EC_KEY(key->csign);
3956 if (!eckey)
3957 return -1;
3958 group = EC_KEY_get0_group(eckey);
3959 if (!group)
3960 return -1;
3961 nid = EC_GROUP_get_curve_name(group);
3962 curve = dpp_get_curve_nid(nid);
3963 if (!curve) {
3964 wpa_printf(MSG_INFO, "DPP: Unsupported group in c-sign-key");
3965 return -1;
3966 }
3967
3968 conf = os_zalloc(sizeof(*conf));
3969 if (!conf)
3970 return -1;
3971 conf->curve = curve;
3972 conf->csign = key->csign;
3973 key->csign = NULL;
3974 conf->own = 1;
3975 if (dpp_configurator_gen_kid(conf) < 0) {
3976 dpp_configurator_free(conf);
3977 return -1;
3978 }
3979
3980 conf->id = dpp_next_configurator_id(dpp);
3981 dl_list_add(&dpp->configurator, &conf->list);
3982 return conf->id;
3983 }
3984
3985
dpp_configurator_find_kid(struct dpp_global * dpp,const u8 * kid)3986 struct dpp_configurator * dpp_configurator_find_kid(struct dpp_global *dpp,
3987 const u8 *kid)
3988 {
3989 struct dpp_configurator *conf;
3990
3991 if (!dpp)
3992 return NULL;
3993
3994 dl_list_for_each(conf, &dpp->configurator,
3995 struct dpp_configurator, list) {
3996 if (os_memcmp(conf->kid_hash, kid, SHA256_MAC_LEN) == 0)
3997 return conf;
3998 }
3999 return NULL;
4000 }
4001
4002 #endif /* CONFIG_DPP2 */
4003
4004
dpp_global_init(struct dpp_global_config * config)4005 struct dpp_global * dpp_global_init(struct dpp_global_config *config)
4006 {
4007 struct dpp_global *dpp;
4008
4009 dpp = os_zalloc(sizeof(*dpp));
4010 if (!dpp)
4011 return NULL;
4012 dpp->msg_ctx = config->msg_ctx;
4013 #ifdef CONFIG_DPP2
4014 dpp->cb_ctx = config->cb_ctx;
4015 dpp->process_conf_obj = config->process_conf_obj;
4016 dpp->remove_bi = config->remove_bi;
4017 #endif /* CONFIG_DPP2 */
4018
4019 dl_list_init(&dpp->bootstrap);
4020 dl_list_init(&dpp->configurator);
4021 #ifdef CONFIG_DPP2
4022 dl_list_init(&dpp->controllers);
4023 dl_list_init(&dpp->tcp_init);
4024 #endif /* CONFIG_DPP2 */
4025
4026 return dpp;
4027 }
4028
4029
dpp_global_clear(struct dpp_global * dpp)4030 void dpp_global_clear(struct dpp_global *dpp)
4031 {
4032 if (!dpp)
4033 return;
4034
4035 dpp_bootstrap_del(dpp, 0);
4036 dpp_configurator_del(dpp, 0);
4037 #ifdef CONFIG_DPP2
4038 dpp_tcp_init_flush(dpp);
4039 dpp_relay_flush_controllers(dpp);
4040 dpp_controller_stop(dpp);
4041 #endif /* CONFIG_DPP2 */
4042 }
4043
4044
dpp_global_deinit(struct dpp_global * dpp)4045 void dpp_global_deinit(struct dpp_global *dpp)
4046 {
4047 dpp_global_clear(dpp);
4048 os_free(dpp);
4049 }
4050
4051
4052 #ifdef CONFIG_DPP2
dpp_build_presence_announcement(struct dpp_bootstrap_info * bi)4053 struct wpabuf * dpp_build_presence_announcement(struct dpp_bootstrap_info *bi)
4054 {
4055 struct wpabuf *msg;
4056
4057 wpa_printf(MSG_DEBUG, "DPP: Build Presence Announcement frame");
4058
4059 msg = dpp_alloc_msg(DPP_PA_PRESENCE_ANNOUNCEMENT, 4 + SHA256_MAC_LEN);
4060 if (!msg)
4061 return NULL;
4062
4063 /* Responder Bootstrapping Key Hash */
4064 dpp_build_attr_r_bootstrap_key_hash(msg, bi->pubkey_hash_chirp);
4065 wpa_hexdump_buf(MSG_DEBUG,
4066 "DPP: Presence Announcement frame attributes", msg);
4067 return msg;
4068 }
4069 #endif /* CONFIG_DPP2 */
4070