dpp.c (119066B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <errno.h> 29 #include <string.h> 30 31 #include <ell/ell.h> 32 33 #include "linux/nl80211.h" 34 35 #include "src/missing.h" 36 #include "src/dbus.h" 37 #include "src/netdev.h" 38 #include "src/module.h" 39 #include "src/dpp-util.h" 40 #include "src/band.h" 41 #include "src/frame-xchg.h" 42 #include "src/offchannel.h" 43 #include "src/wiphy.h" 44 #include "src/ie.h" 45 #include "src/iwd.h" 46 #include "src/util.h" 47 #include "src/crypto.h" 48 #include "src/mpdu.h" 49 #include "ell/useful.h" 50 #include "src/common.h" 51 #include "src/json.h" 52 #include "src/storage.h" 53 #include "src/station.h" 54 #include "src/scan.h" 55 #include "src/network.h" 56 #include "src/handshake.h" 57 #include "src/nl80211util.h" 58 #include "src/knownnetworks.h" 59 60 #define DPP_FRAME_MAX_RETRIES 5 61 #define DPP_FRAME_RETRY_TIMEOUT 1 62 #define DPP_AUTH_PROTO_TIMEOUT 10 63 #define DPP_PKEX_PROTO_TIMEOUT 120 64 #define DPP_PKEX_PROTO_PER_FREQ_TIMEOUT 10 65 66 static uint32_t netdev_watch; 67 static struct l_genl_family *nl80211; 68 static uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 69 static struct l_queue *dpp_list; 70 static uint32_t mlme_watch; 71 72 static uint8_t dpp_prefix[] = { 0x04, 0x09, 0x50, 0x6f, 0x9a, 0x1a, 0x01 }; 73 74 enum dpp_state { 75 DPP_STATE_NOTHING, 76 DPP_STATE_PRESENCE, 77 DPP_STATE_PKEX_EXCHANGE, 78 DPP_STATE_PKEX_COMMIT_REVEAL, 79 DPP_STATE_AUTHENTICATING, 80 DPP_STATE_CONFIGURING, 81 }; 82 83 enum dpp_capability { 84 DPP_CAPABILITY_ENROLLEE = 0x01, 85 DPP_CAPABILITY_CONFIGURATOR = 0x02, 86 }; 87 88 enum dpp_interface { 89 DPP_INTERFACE_UNBOUND, 90 DPP_INTERFACE_DPP, 91 DPP_INTERFACE_PKEX, 92 }; 93 94 struct pkex_agent { 95 char *owner; 96 char *path; 97 unsigned int disconnect_watch; 98 uint32_t pending_id; 99 }; 100 101 struct dpp_sm { 102 struct netdev *netdev; 103 char *uri; 104 uint8_t role; 105 int refcount; 106 uint32_t station_watch; 107 uint32_t known_network_watch; 108 109 uint64_t wdev_id; 110 111 uint8_t *own_asn1; 112 size_t own_asn1_len; 113 uint8_t *peer_asn1; 114 size_t peer_asn1_len; 115 uint8_t own_boot_hash[32]; 116 uint8_t peer_boot_hash[32]; 117 const struct l_ecc_curve *curve; 118 size_t key_len; 119 size_t nonce_len; 120 struct l_ecc_scalar *boot_private; 121 struct l_ecc_point *boot_public; 122 struct l_ecc_point *peer_boot_public; 123 124 enum dpp_state state; 125 enum dpp_interface interface; 126 127 struct pkex_agent *agent; 128 129 /* 130 * List of frequencies to jump between. The presence of this list is 131 * also used to signify that a configurator is an initiator vs responder 132 */ 133 uint32_t *freqs; 134 size_t freqs_len; 135 size_t freqs_idx; 136 uint32_t dwell; 137 uint32_t current_freq; 138 uint32_t new_freq; 139 struct scan_freq_set *presence_list; 140 uint32_t max_roc; 141 142 uint32_t offchannel_id; 143 144 uint8_t peer_addr[6]; 145 uint8_t r_nonce[32]; 146 uint8_t i_nonce[32]; 147 uint8_t e_nonce[32]; 148 149 struct l_ecc_scalar *m; 150 uint64_t ke[L_ECC_MAX_DIGITS]; 151 uint64_t k1[L_ECC_MAX_DIGITS]; 152 uint64_t k2[L_ECC_MAX_DIGITS]; 153 uint64_t auth_tag[L_ECC_MAX_DIGITS]; 154 155 struct l_ecc_scalar *proto_private; 156 struct l_ecc_point *own_proto_public; 157 158 struct l_ecc_point *peer_proto_public; 159 160 uint8_t diag_token; 161 162 /* Timeout of either auth/config protocol */ 163 struct l_timeout *timeout; 164 165 struct dpp_configuration *config; 166 uint32_t connect_scan_id; 167 uint64_t frame_cookie; 168 uint8_t frame_retry; 169 void *frame_pending; 170 size_t frame_size; 171 struct l_timeout *retry_timeout; 172 173 struct l_dbus_message *pending; 174 175 struct l_idle *connect_idle; 176 177 /* PKEX-specific values */ 178 char *pkex_id; 179 char *pkex_key; 180 uint8_t pkex_version; 181 struct l_ecc_point *peer_encr_key; 182 struct l_ecc_point *pkex_m; 183 /* Ephemeral key Y' or X' for enrollee or configurator */ 184 struct l_ecc_point *y_or_x; 185 /* Ephemeral key pair y/Y or x/X */ 186 struct l_ecc_point *pkex_public; 187 struct l_ecc_scalar *pkex_private; 188 uint8_t z[L_ECC_SCALAR_MAX_BYTES]; 189 size_t z_len; 190 uint8_t u[L_ECC_SCALAR_MAX_BYTES]; 191 size_t u_len; 192 uint32_t pkex_scan_id; 193 194 bool mcast_support : 1; 195 bool roc_started : 1; 196 bool channel_switch : 1; 197 bool mutual_auth : 1; 198 bool autoconnect : 1; 199 }; 200 201 static const char *dpp_role_to_string(enum dpp_capability role) 202 { 203 switch (role) { 204 case DPP_CAPABILITY_ENROLLEE: 205 return "enrollee"; 206 case DPP_CAPABILITY_CONFIGURATOR: 207 return "configurator"; 208 default: 209 return NULL; 210 } 211 } 212 213 static bool dpp_pkex_get_started(struct l_dbus *dbus, 214 struct l_dbus_message *message, 215 struct l_dbus_message_builder *builder, 216 void *user_data) 217 { 218 struct dpp_sm *dpp = user_data; 219 bool started = (dpp->state != DPP_STATE_NOTHING && 220 dpp->interface == DPP_INTERFACE_PKEX); 221 222 l_dbus_message_builder_append_basic(builder, 'b', &started); 223 224 return true; 225 } 226 227 static bool dpp_pkex_get_role(struct l_dbus *dbus, 228 struct l_dbus_message *message, 229 struct l_dbus_message_builder *builder, 230 void *user_data) 231 { 232 struct dpp_sm *dpp = user_data; 233 const char *role; 234 235 if (dpp->state == DPP_STATE_NOTHING || 236 dpp->interface != DPP_INTERFACE_PKEX) 237 return false; 238 239 role = dpp_role_to_string(dpp->role); 240 if (L_WARN_ON(!role)) 241 return false; 242 243 l_dbus_message_builder_append_basic(builder, 's', role); 244 return true; 245 } 246 247 static bool dpp_get_started(struct l_dbus *dbus, 248 struct l_dbus_message *message, 249 struct l_dbus_message_builder *builder, 250 void *user_data) 251 { 252 struct dpp_sm *dpp = user_data; 253 bool started = (dpp->state != DPP_STATE_NOTHING && 254 dpp->interface == DPP_INTERFACE_DPP); 255 256 l_dbus_message_builder_append_basic(builder, 'b', &started); 257 258 return true; 259 } 260 261 static bool dpp_get_role(struct l_dbus *dbus, 262 struct l_dbus_message *message, 263 struct l_dbus_message_builder *builder, 264 void *user_data) 265 { 266 struct dpp_sm *dpp = user_data; 267 const char *role; 268 269 if (dpp->state == DPP_STATE_NOTHING || 270 dpp->interface != DPP_INTERFACE_DPP) 271 return false; 272 273 role = dpp_role_to_string(dpp->role); 274 if (L_WARN_ON(!role)) 275 return false; 276 277 l_dbus_message_builder_append_basic(builder, 's', role); 278 return true; 279 } 280 281 static bool dpp_get_uri(struct l_dbus *dbus, 282 struct l_dbus_message *message, 283 struct l_dbus_message_builder *builder, 284 void *user_data) 285 { 286 struct dpp_sm *dpp = user_data; 287 288 if (dpp->state == DPP_STATE_NOTHING || 289 dpp->interface != DPP_INTERFACE_DPP) 290 return false; 291 292 l_dbus_message_builder_append_basic(builder, 's', dpp->uri); 293 return true; 294 } 295 296 static void dpp_property_changed_notify(struct dpp_sm *dpp) 297 { 298 const char *path = netdev_get_path(dpp->netdev); 299 300 switch (dpp->interface) { 301 case DPP_INTERFACE_DPP: 302 l_dbus_property_changed(dbus_get_bus(), path, IWD_DPP_INTERFACE, 303 "Started"); 304 l_dbus_property_changed(dbus_get_bus(), path, IWD_DPP_INTERFACE, 305 "Role"); 306 l_dbus_property_changed(dbus_get_bus(), path, IWD_DPP_INTERFACE, 307 "URI"); 308 break; 309 case DPP_INTERFACE_PKEX: 310 l_dbus_property_changed(dbus_get_bus(), path, 311 IWD_DPP_PKEX_INTERFACE, 312 "Started"); 313 l_dbus_property_changed(dbus_get_bus(), path, 314 IWD_DPP_PKEX_INTERFACE, 315 "Role"); 316 break; 317 default: 318 break; 319 } 320 } 321 322 static void *dpp_serialize_iovec(struct iovec *iov, size_t iov_len, 323 size_t *out_len) 324 { 325 unsigned int i; 326 size_t size = 0; 327 uint8_t *ret; 328 329 for (i = 0; i < iov_len; i++) 330 size += iov[i].iov_len; 331 332 ret = l_malloc(size); 333 size = 0; 334 335 for (i = 0; i < iov_len; i++) { 336 memcpy(ret + size, iov[i].iov_base, iov[i].iov_len); 337 size += iov[i].iov_len; 338 } 339 340 if (out_len) 341 *out_len = size; 342 343 return ret; 344 } 345 346 static void dpp_free_pending_pkex_data(struct dpp_sm *dpp) 347 { 348 if (dpp->pkex_id) { 349 l_free(dpp->pkex_id); 350 dpp->pkex_id = NULL; 351 } 352 353 if (dpp->pkex_key) { 354 l_free(dpp->pkex_key); 355 dpp->pkex_key = NULL; 356 } 357 358 memset(dpp->peer_addr, 0, sizeof(dpp->peer_addr)); 359 360 if (dpp->peer_encr_key) { 361 l_ecc_point_free(dpp->peer_encr_key); 362 dpp->peer_encr_key = NULL; 363 } 364 } 365 366 static void pkex_agent_free(void *data) 367 { 368 struct pkex_agent *agent = data; 369 370 l_free(agent->owner); 371 l_free(agent->path); 372 l_dbus_remove_watch(dbus_get_bus(), agent->disconnect_watch); 373 l_free(agent); 374 } 375 376 static void dpp_agent_cancel(struct dpp_sm *dpp) 377 { 378 struct l_dbus_message *msg; 379 380 const char *reason = "shutdown"; 381 382 msg = l_dbus_message_new_method_call(dbus_get_bus(), 383 dpp->agent->owner, 384 dpp->agent->path, 385 IWD_SHARED_CODE_AGENT_INTERFACE, 386 "Cancel"); 387 l_dbus_message_set_arguments(msg, "s", reason); 388 l_dbus_message_set_no_reply(msg, true); 389 l_dbus_send(dbus_get_bus(), msg); 390 } 391 392 static void dpp_agent_release(struct dpp_sm *dpp) 393 { 394 struct l_dbus_message *msg; 395 396 msg = l_dbus_message_new_method_call(dbus_get_bus(), 397 dpp->agent->owner, 398 dpp->agent->path, 399 IWD_SHARED_CODE_AGENT_INTERFACE, 400 "Release"); 401 l_dbus_message_set_arguments(msg, ""); 402 l_dbus_message_set_no_reply(msg, true); 403 l_dbus_send(dbus_get_bus(), msg); 404 } 405 406 static void dpp_destroy_agent(struct dpp_sm *dpp) 407 { 408 if (!dpp->agent) 409 return; 410 411 if (dpp->agent->pending_id) { 412 dpp_agent_cancel(dpp); 413 l_dbus_cancel(dbus_get_bus(), dpp->agent->pending_id); 414 } 415 416 dpp_agent_release(dpp); 417 418 l_debug("Released SharedCodeAgent on path %s", dpp->agent->path); 419 420 pkex_agent_free(dpp->agent); 421 dpp->agent = NULL; 422 } 423 424 static void dpp_free_auth_data(struct dpp_sm *dpp) 425 { 426 if (dpp->own_proto_public) { 427 l_ecc_point_free(dpp->own_proto_public); 428 dpp->own_proto_public = NULL; 429 } 430 431 if (dpp->proto_private) { 432 l_ecc_scalar_free(dpp->proto_private); 433 dpp->proto_private = NULL; 434 } 435 436 if (dpp->peer_proto_public) { 437 l_ecc_point_free(dpp->peer_proto_public); 438 dpp->peer_proto_public = NULL; 439 } 440 441 if (dpp->peer_boot_public) { 442 l_ecc_point_free(dpp->peer_boot_public); 443 dpp->peer_boot_public = NULL; 444 } 445 446 if (dpp->m) { 447 l_ecc_scalar_free(dpp->m); 448 dpp->m = NULL; 449 } 450 451 if (dpp->pkex_m) { 452 l_ecc_point_free(dpp->pkex_m); 453 dpp->pkex_m = NULL; 454 } 455 456 if (dpp->y_or_x) { 457 l_ecc_point_free(dpp->y_or_x); 458 dpp->y_or_x = NULL; 459 } 460 461 if (dpp->pkex_public) { 462 l_ecc_point_free(dpp->pkex_public); 463 dpp->pkex_public = NULL; 464 } 465 466 if (dpp->pkex_private) { 467 l_ecc_scalar_free(dpp->pkex_private); 468 dpp->pkex_private = NULL; 469 } 470 471 } 472 473 static void dpp_reset(struct dpp_sm *dpp) 474 { 475 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 476 477 if (dpp->uri) { 478 l_free(dpp->uri); 479 dpp->uri = NULL; 480 } 481 482 if (dpp->freqs) { 483 l_free(dpp->freqs); 484 dpp->freqs = NULL; 485 } 486 487 if (dpp->offchannel_id) { 488 offchannel_cancel(dpp->wdev_id, dpp->offchannel_id); 489 dpp->offchannel_id = 0; 490 } 491 492 if (dpp->timeout) { 493 l_timeout_remove(dpp->timeout); 494 dpp->timeout = NULL; 495 } 496 497 if (dpp->config) { 498 dpp_configuration_free(dpp->config); 499 dpp->config = NULL; 500 } 501 502 if (dpp->connect_scan_id) { 503 scan_cancel(dpp->wdev_id, dpp->connect_scan_id); 504 dpp->connect_scan_id = 0; 505 } 506 507 if (dpp->peer_asn1) { 508 l_free(dpp->peer_asn1); 509 dpp->peer_asn1 = NULL; 510 } 511 512 if (dpp->frame_pending) { 513 l_free(dpp->frame_pending); 514 dpp->frame_pending = NULL; 515 } 516 517 if (dpp->retry_timeout) { 518 l_timeout_remove(dpp->retry_timeout); 519 dpp->retry_timeout = NULL; 520 } 521 522 if (dpp->pkex_scan_id) { 523 scan_cancel(dpp->wdev_id, dpp->pkex_scan_id); 524 dpp->pkex_scan_id = 0; 525 } 526 527 if (dpp->connect_idle) { 528 l_idle_remove(dpp->connect_idle); 529 dpp->connect_idle = NULL; 530 } 531 532 dpp->state = DPP_STATE_NOTHING; 533 dpp->new_freq = 0; 534 dpp->frame_retry = 0; 535 dpp->frame_cookie = 0; 536 dpp->pkex_version = 0; 537 538 explicit_bzero(dpp->r_nonce, dpp->nonce_len); 539 explicit_bzero(dpp->i_nonce, dpp->nonce_len); 540 explicit_bzero(dpp->e_nonce, dpp->nonce_len); 541 explicit_bzero(dpp->ke, dpp->key_len); 542 explicit_bzero(dpp->k1, dpp->key_len); 543 explicit_bzero(dpp->k2, dpp->key_len); 544 explicit_bzero(dpp->auth_tag, dpp->key_len); 545 explicit_bzero(dpp->z, dpp->key_len); 546 explicit_bzero(dpp->u, dpp->u_len); 547 548 dpp_destroy_agent(dpp); 549 550 dpp_free_pending_pkex_data(dpp); 551 552 dpp_free_auth_data(dpp); 553 554 dpp_property_changed_notify(dpp); 555 556 frame_watch_group_remove(dpp->wdev_id, FRAME_GROUP_DPP); 557 558 dpp->interface = DPP_INTERFACE_UNBOUND; 559 560 if (station) { 561 if (dpp->station_watch) 562 station_remove_state_watch(station, dpp->station_watch); 563 564 /* Set the old autoconnect state back to what it was */ 565 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 566 station_set_autoconnect(station, dpp->autoconnect); 567 } 568 } 569 570 static void dpp_free(struct dpp_sm *dpp) 571 { 572 dpp_reset(dpp); 573 574 if (dpp->own_asn1) { 575 l_free(dpp->own_asn1); 576 dpp->own_asn1 = NULL; 577 } 578 579 if (dpp->boot_public) { 580 l_ecc_point_free(dpp->boot_public); 581 dpp->boot_public = NULL; 582 } 583 584 if (dpp->boot_private) { 585 l_ecc_scalar_free(dpp->boot_private); 586 dpp->boot_private = NULL; 587 } 588 589 known_networks_watch_remove(dpp->known_network_watch); 590 591 l_free(dpp); 592 } 593 594 static void dpp_send_frame_cb(struct l_genl_msg *msg, void *user_data) 595 { 596 struct dpp_sm *dpp = user_data; 597 int err = l_genl_msg_get_error(msg); 598 599 if (err < 0) { 600 l_error("Error sending frame (%d)", err); 601 return; 602 } 603 604 if (nl80211_parse_attrs(msg, NL80211_ATTR_COOKIE, &dpp->frame_cookie, 605 NL80211_ATTR_UNSPEC) < 0) 606 l_error("Error parsing frame cookie"); 607 } 608 609 static void dpp_send_frame(struct dpp_sm *dpp, 610 struct iovec *iov, size_t iov_len, 611 uint32_t freq) 612 { 613 struct l_genl_msg *msg; 614 615 /* 616 * A received frame could potentially come in after the ROC session has 617 * ended. In this case the frame needs to be stored until ROC is started 618 * and sent at that time. The offchannel_id is also checked since 619 * this is not applicable when DPP is in a responder role waiting 620 * on the currently connected channel i.e. offchannel is never used. 621 */ 622 if (!dpp->roc_started && dpp->offchannel_id) { 623 dpp->frame_pending = dpp_serialize_iovec(iov, iov_len, 624 &dpp->frame_size); 625 return; 626 } 627 628 msg = l_genl_msg_new_sized(NL80211_CMD_FRAME, 512); 629 l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &dpp->wdev_id); 630 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &freq); 631 l_genl_msg_append_attr(msg, NL80211_ATTR_OFFCHANNEL_TX_OK, 0, NULL); 632 l_genl_msg_append_attrv(msg, NL80211_ATTR_FRAME, iov, iov_len); 633 634 l_debug("Sending frame on frequency %u", freq); 635 636 if (!l_genl_family_send(nl80211, msg, dpp_send_frame_cb, dpp, NULL)) { 637 l_error("Could not send CMD_FRAME"); 638 l_genl_msg_unref(msg); 639 } 640 } 641 642 static void dpp_frame_retry(struct dpp_sm *dpp) 643 { 644 struct iovec iov; 645 646 iov.iov_base = dpp->frame_pending; 647 iov.iov_len = dpp->frame_size; 648 649 dpp_send_frame(dpp, &iov, 1, dpp->current_freq); 650 651 l_free(dpp->frame_pending); 652 dpp->frame_pending = NULL; 653 } 654 655 656 static size_t dpp_build_header(const uint8_t *src, const uint8_t *dest, 657 enum dpp_frame_type type, 658 uint8_t buf[static 32]) 659 { 660 uint8_t *ptr = buf + 24; 661 662 memset(buf, 0, 32); 663 664 l_put_le16(0x00d0, buf); 665 memcpy(buf + 4, dest, 6); 666 memcpy(buf + 10, src, 6); 667 memcpy(buf + 16, broadcast, 6); 668 669 *ptr++ = 0x04; /* Category: Public */ 670 *ptr++ = 0x09; /* Action: Vendor specific usage */ 671 memcpy(ptr, wifi_alliance_oui, 3); 672 ptr += 3; 673 *ptr++ = 0x1a; /* WiFi Alliance DPP OI type */ 674 *ptr++ = 1; /* Cryptosuite */ 675 *ptr++ = type; 676 677 return ptr - buf; 678 } 679 680 static size_t dpp_build_config_header(const uint8_t *src, const uint8_t *dest, 681 uint8_t diag_token, 682 uint8_t buf[static 37]) 683 { 684 uint8_t *ptr = buf + 24; 685 686 memset(buf, 0, 37); 687 688 l_put_le16(0x00d0, buf); 689 memcpy(buf + 4, dest, 6); 690 memcpy(buf + 10, src, 6); 691 memcpy(buf + 16, broadcast, 6); 692 693 *ptr++ = 0x04; /* Public */ 694 *ptr++ = 0x0a; /* Action */ 695 *ptr++ = diag_token; 696 697 *ptr++ = IE_TYPE_ADVERTISEMENT_PROTOCOL; 698 *ptr++ = 8; /* len */ 699 *ptr++ = 0x00; 700 *ptr++ = IE_TYPE_VENDOR_SPECIFIC; 701 *ptr++ = 5; 702 memcpy(ptr, wifi_alliance_oui, 3); 703 ptr += 3; 704 *ptr++ = 0x1a; 705 *ptr++ = 1; 706 707 return ptr - buf; 708 } 709 710 static void dpp_protocol_timeout(struct l_timeout *timeout, void *user_data) 711 { 712 struct dpp_sm *dpp = user_data; 713 714 l_debug("DPP timed out"); 715 716 dpp_reset(dpp); 717 } 718 719 static void dpp_reset_protocol_timer(struct dpp_sm *dpp, uint32_t time) 720 { 721 if (dpp->timeout) 722 l_timeout_modify(dpp->timeout, time); 723 else 724 dpp->timeout = l_timeout_create(time, dpp_protocol_timeout, 725 dpp, NULL); 726 } 727 728 /* 729 * The configuration protocols use of AD components is somewhat confusing 730 * since the request/response frames are of a different format than the rest. 731 * In addition there are situations where the components length is zero yet it 732 * is still passed as such to AES-SIV. 733 * 734 * For the configuration request/response frames: 735 * 736 * "AAD for use with AES-SIV for protected messages in the DPP Configuration 737 * protocol shall consist of all octets in the Query Request and Query Response 738 * fields up to the first octet of the Wrapped Data attribute, which is the last 739 * attribute in a DPP Configuration frame. When the number of octets of AAD is 740 * zero, the number of components of AAD passed to AES-SIV is zero." 741 * 742 * - For configuration requests the optional query request field is not 743 * included, therefore no AAD data is passed. (dpp_configuration_start) 744 * 745 * - The configuration response does contain a query response field which is 746 * 5 bytes. (dpp_handle_config_response_frame) 747 * 748 * For the configuration result/status, the same rules are used as the 749 * authentication protocol. This is reiterated in section 6.4.1. 750 * 751 * - For the configuration result there is some confusion as to exactly how the 752 * second AAD component should be passed (since the spec specifically 753 * mentions using two components). There are no attributes prior to the 754 * wrapped data component meaning the length would be zero. 755 * Hostapd/wpa_supplicant pass a zero length AAD component to AES-SIV which 756 * does effect the resulting encryption/decryption so this is also what IWD 757 * will do to remain compliant with it. 758 */ 759 static void dpp_configuration_start(struct dpp_sm *dpp, const uint8_t *addr) 760 { 761 const char *json = "{\"name\":\"IWD\",\"wi-fi_tech\":\"infra\"," 762 "\"netRole\":\"sta\"}"; 763 struct iovec iov[3]; 764 uint8_t hdr[37]; 765 uint8_t attrs[512]; 766 size_t json_len = strlen(json); 767 uint8_t *ptr = attrs; 768 769 l_getrandom(&dpp->diag_token, 1); 770 771 iov[0].iov_len = dpp_build_config_header( 772 netdev_get_address(dpp->netdev), 773 addr, dpp->diag_token, hdr); 774 iov[0].iov_base = hdr; 775 776 l_getrandom(dpp->e_nonce, dpp->nonce_len); 777 778 /* length */ 779 ptr += 2; 780 781 /* 782 * "AAD for use with AES-SIV for protected messages in the DPP 783 * Configuration protocol shall consist of all octets in the Query 784 * Request and Query Response fields up to the first octet of the 785 * Wrapped Data attribute" 786 * 787 * In this case there is no query request/response fields, nor any 788 * attributes besides wrapped data meaning zero AD components. 789 */ 790 ptr += dpp_append_wrapped_data(NULL, 0, NULL, 0, ptr, sizeof(attrs), 791 dpp->ke, dpp->key_len, 2, 792 DPP_ATTR_ENROLLEE_NONCE, dpp->nonce_len, dpp->e_nonce, 793 DPP_ATTR_CONFIGURATION_REQUEST, json_len, json); 794 795 l_put_le16(ptr - attrs - 2, attrs); 796 797 iov[1].iov_base = attrs; 798 iov[1].iov_len = ptr - attrs; 799 800 dpp->state = DPP_STATE_CONFIGURING; 801 802 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 803 } 804 805 static void send_config_result(struct dpp_sm *dpp, const uint8_t *to) 806 { 807 uint8_t hdr[32]; 808 struct iovec iov[2]; 809 uint8_t attrs[256]; 810 uint8_t *ptr = attrs; 811 uint8_t zero = 0; 812 813 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), to, 814 DPP_FRAME_CONFIGURATION_RESULT, hdr); 815 iov[0].iov_base = hdr; 816 817 ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, 0, ptr, 818 sizeof(attrs), dpp->ke, dpp->key_len, 2, 819 DPP_ATTR_STATUS, (size_t) 1, &zero, 820 DPP_ATTR_ENROLLEE_NONCE, dpp->nonce_len, dpp->e_nonce); 821 822 iov[1].iov_base = attrs; 823 iov[1].iov_len = ptr - attrs; 824 825 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 826 } 827 828 static void dpp_write_config(struct dpp_configuration *config, 829 struct network *network) 830 { 831 _auto_(l_settings_free) struct l_settings *settings = l_settings_new(); 832 _auto_(l_free) char *path; 833 834 path = storage_get_network_file_path(SECURITY_PSK, config->ssid); 835 836 if (l_settings_load_from_file(settings, path)) { 837 /* Remove any existing Security keys */ 838 l_settings_remove_group(settings, "Security"); 839 } 840 841 if (config->passphrase) 842 l_settings_set_string(settings, "Security", "Passphrase", 843 config->passphrase); 844 else if (config->psk) 845 l_settings_set_string(settings, "Security", "PreSharedKey", 846 config->psk); 847 848 if (config->send_hostname) 849 l_settings_set_bool(settings, "IPv4", "SendHostname", true); 850 851 if (config->hidden) 852 l_settings_set_bool(settings, "Settings", "Hidden", true); 853 854 l_debug("Storing credential for '%s(%s)'", config->ssid, 855 security_to_str(SECURITY_PSK)); 856 storage_network_sync(SECURITY_PSK, config->ssid, settings); 857 } 858 859 static void dpp_scan_triggered(int err, void *user_data) 860 { 861 /* Not much can be done in this case */ 862 if (err < 0) 863 l_error("Failed to trigger DPP scan"); 864 } 865 866 static void dpp_start_connect(struct l_idle *idle, void *user_data) 867 { 868 struct dpp_sm *dpp = user_data; 869 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 870 struct scan_bss *bss; 871 struct network *network; 872 int ret; 873 874 network = station_network_find(station, dpp->config->ssid, 875 SECURITY_PSK); 876 877 dpp_reset(dpp); 878 879 if (!network) { 880 l_debug("Network was not found!"); 881 return; 882 } 883 884 l_debug("connecting to %s from DPP", network_get_ssid(network)); 885 886 bss = network_bss_select(network, true); 887 ret = network_autoconnect(network, bss); 888 if (ret < 0) 889 l_warn("failed to connect after DPP (%d) %s", ret, 890 strerror(-ret)); 891 } 892 893 static bool dpp_scan_results(int err, struct l_queue *bss_list, 894 const struct scan_freq_set *freqs, 895 void *userdata) 896 { 897 struct dpp_sm *dpp = userdata; 898 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 899 900 if (err < 0) 901 goto reset; 902 903 if (!bss_list || l_queue_length(bss_list) == 0) 904 goto reset; 905 906 /* 907 * The station watch _should_ detect this and reset, which cancels the 908 * scan. But just in case... 909 */ 910 if (L_WARN_ON(station_get_connected_network(station))) 911 goto reset; 912 913 station_set_scan_results(station, bss_list, freqs, false); 914 915 dpp_start_connect(NULL, dpp); 916 917 return true; 918 919 reset: 920 return false; 921 } 922 923 static void dpp_scan_destroy(void *userdata) 924 { 925 struct dpp_sm *dpp = userdata; 926 927 dpp->connect_scan_id = 0; 928 dpp_reset(dpp); 929 } 930 931 static void dpp_known_network_watch(enum known_networks_event event, 932 const struct network_info *info, 933 void *user_data) 934 { 935 struct dpp_sm *dpp = user_data; 936 937 /* 938 * Check the following 939 * - DPP is enrolling 940 * - DPP finished (dpp->config is set) 941 * - This is for the network DPP just configured 942 * - DPP isn't already trying to connect (e.g. if the profile was 943 * immediately modified after DPP synced it). 944 * - DPP didn't start a scan for the network. 945 */ 946 if (dpp->role != DPP_CAPABILITY_ENROLLEE) 947 return; 948 if (!dpp->config) 949 return; 950 if (strcmp(info->ssid, dpp->config->ssid)) 951 return; 952 if (dpp->connect_idle) 953 return; 954 if (dpp->connect_scan_id) 955 return; 956 957 switch (event) { 958 case KNOWN_NETWORKS_EVENT_ADDED: 959 case KNOWN_NETWORKS_EVENT_UPDATED: 960 /* 961 * network.c takes care of updating the settings for the 962 * network. This callback just tells us to begin the connection. 963 * We do have use an idle here because there is no strict 964 * guarantee of ordering between known network events, e.g. DPP 965 * could have been called into prior to network and the network 966 * object isn't updated yet. 967 */ 968 dpp->connect_idle = l_idle_create(dpp_start_connect, dpp, NULL); 969 break; 970 case KNOWN_NETWORKS_EVENT_REMOVED: 971 l_warn("profile was removed before DPP could connect"); 972 break; 973 } 974 } 975 976 static void dpp_handle_config_response_frame(const struct mmpdu_header *frame, 977 const void *body, size_t body_len, 978 int rssi, void *user_data) 979 { 980 struct dpp_sm *dpp = user_data; 981 const uint8_t *ptr = body; 982 uint16_t status; 983 uint16_t fragmented; /* Fragmented/Comeback delay field */ 984 uint8_t adv_protocol_element[] = { 0x6C, 0x08, 0x7F }; 985 uint8_t adv_protocol_id[] = { 0xDD, 0x05, 0x50, 0x6F, 986 0x9A, 0x1A, 0x01 }; 987 uint16_t query_len; 988 struct dpp_attr_iter iter; 989 enum dpp_attribute_type type; 990 size_t len; 991 const uint8_t *data; 992 const char *json = NULL; 993 size_t json_len = 0; 994 int dstatus = -1; 995 const uint8_t *wrapped = NULL; 996 const uint8_t *e_nonce = NULL; 997 size_t wrapped_len = 0; 998 _auto_(l_free) uint8_t *unwrapped = NULL; 999 struct dpp_configuration *config; 1000 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 1001 struct network *network = NULL; 1002 struct scan_bss *bss = NULL; 1003 1004 if (dpp->state != DPP_STATE_CONFIGURING) 1005 return; 1006 1007 /* 1008 * Can a configuration request come from someone other than who you 1009 * authenticated to? 1010 */ 1011 if (memcmp(dpp->peer_addr, frame->address_2, 6)) 1012 return; 1013 1014 if (body_len < 19) 1015 return; 1016 1017 ptr += 2; 1018 1019 if (*ptr++ != dpp->diag_token) 1020 return; 1021 1022 status = l_get_le16(ptr); 1023 ptr += 2; 1024 1025 if (status != 0) { 1026 l_debug("Bad configuration status %u", status); 1027 return; 1028 } 1029 1030 fragmented = l_get_le16(ptr); 1031 ptr += 2; 1032 1033 /* 1034 * TODO: handle 0x0001 (fragmented), as well as comeback delay. 1035 */ 1036 if (fragmented != 0) { 1037 l_debug("Fragmented messages not currently supported"); 1038 return; 1039 } 1040 1041 if (memcmp(ptr, adv_protocol_element, sizeof(adv_protocol_element))) { 1042 l_debug("Invalid Advertisement protocol element"); 1043 return; 1044 } 1045 1046 ptr += sizeof(adv_protocol_element); 1047 1048 if (memcmp(ptr, adv_protocol_id, sizeof(adv_protocol_id))) { 1049 l_debug("Invalid Advertisement protocol ID"); 1050 return; 1051 } 1052 1053 ptr += sizeof(adv_protocol_id); 1054 1055 query_len = l_get_le16(ptr); 1056 ptr += 2; 1057 1058 if (query_len > body_len - 19) 1059 return; 1060 1061 dpp_attr_iter_init(&iter, ptr, query_len); 1062 1063 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1064 switch (type) { 1065 case DPP_ATTR_STATUS: 1066 dstatus = l_get_u8(data); 1067 break; 1068 case DPP_ATTR_WRAPPED_DATA: 1069 wrapped = data; 1070 wrapped_len = len; 1071 break; 1072 default: 1073 /* 1074 * TODO: CSR Attribute 1075 */ 1076 break; 1077 } 1078 } 1079 1080 if (dstatus != DPP_STATUS_OK || !wrapped) { 1081 l_debug("Bad status or missing attributes"); 1082 return; 1083 } 1084 1085 unwrapped = dpp_unwrap_attr(ptr, wrapped - ptr - 4, NULL, 0, dpp->ke, 1086 dpp->key_len, wrapped, wrapped_len, 1087 &wrapped_len); 1088 if (!unwrapped) { 1089 l_debug("Failed to unwrap"); 1090 return; 1091 } 1092 1093 dpp_attr_iter_init(&iter, unwrapped, wrapped_len); 1094 1095 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1096 switch (type) { 1097 case DPP_ATTR_ENROLLEE_NONCE: 1098 if (len != dpp->nonce_len) 1099 break; 1100 1101 if (memcmp(data, dpp->e_nonce, dpp->nonce_len)) 1102 break; 1103 1104 e_nonce = data; 1105 break; 1106 case DPP_ATTR_CONFIGURATION_OBJECT: 1107 json = (const char *)data; 1108 json_len = len; 1109 break; 1110 default: 1111 break; 1112 } 1113 } 1114 1115 if (!json || !e_nonce) { 1116 l_debug("No configuration object in response"); 1117 return; 1118 } 1119 1120 config = dpp_parse_configuration_object(json, json_len); 1121 if (!config) { 1122 l_error("Configuration object did not parse"); 1123 return; 1124 } 1125 1126 /* 1127 * We should have a station device, but if not DPP can write the 1128 * credentials out and be done 1129 */ 1130 if (station) { 1131 network = station_network_find(station, config->ssid, 1132 SECURITY_PSK); 1133 if (network) 1134 bss = network_bss_select(network, true); 1135 } 1136 1137 dpp_write_config(config, network); 1138 1139 send_config_result(dpp, dpp->peer_addr); 1140 1141 offchannel_cancel(dpp->wdev_id, dpp->offchannel_id); 1142 1143 if (network && bss) { 1144 l_debug("delaying connect until settings are synced"); 1145 dpp->config = config; 1146 return; 1147 } else if (station) { 1148 struct scan_parameters params = {0}; 1149 1150 params.ssid = (void *) config->ssid; 1151 params.ssid_len = config->ssid_len; 1152 1153 l_debug("Scanning for %s", config->ssid); 1154 1155 dpp->connect_scan_id = scan_active_full(dpp->wdev_id, ¶ms, 1156 dpp_scan_triggered, 1157 dpp_scan_results, dpp, 1158 dpp_scan_destroy); 1159 if (dpp->connect_scan_id) { 1160 dpp->config = config; 1161 return; 1162 } 1163 } 1164 1165 dpp_configuration_free(config); 1166 dpp_reset(dpp); 1167 } 1168 1169 static void dpp_send_config_response(struct dpp_sm *dpp, uint8_t status) 1170 { 1171 _auto_(l_free) char *json = NULL; 1172 struct iovec iov[3]; 1173 uint8_t hdr[41]; 1174 uint8_t attrs[512]; 1175 size_t json_len; 1176 uint8_t *ptr = hdr + 24; 1177 1178 memset(hdr, 0, sizeof(hdr)); 1179 1180 l_put_le16(0x00d0, hdr); 1181 memcpy(hdr + 4, dpp->peer_addr, 6); 1182 memcpy(hdr + 10, netdev_get_address(dpp->netdev), 6); 1183 memcpy(hdr + 16, broadcast, 6); 1184 1185 *ptr++ = 0x04; 1186 *ptr++ = 0x0b; 1187 *ptr++ = dpp->diag_token; 1188 l_put_le16(0, ptr); /* status */ 1189 ptr += 2; 1190 l_put_le16(0, ptr); /* fragmented (no) */ 1191 ptr += 2; 1192 *ptr++ = IE_TYPE_ADVERTISEMENT_PROTOCOL; 1193 *ptr++ = 0x08; 1194 *ptr++ = 0x7f; 1195 *ptr++ = IE_TYPE_VENDOR_SPECIFIC; 1196 *ptr++ = 5; 1197 memcpy(ptr, wifi_alliance_oui, sizeof(wifi_alliance_oui)); 1198 ptr += sizeof(wifi_alliance_oui); 1199 *ptr++ = 0x1a; 1200 *ptr++ = 1; 1201 1202 iov[0].iov_base = hdr; 1203 iov[0].iov_len = ptr - hdr; 1204 1205 ptr = attrs; 1206 1207 ptr += 2; /* length */ 1208 1209 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1); 1210 1211 /* 1212 * There are several failure status codes that can be used (defined in 1213 * 6.4.3.1), each with their own set of attributes that should be 1214 * included. For now IWD's basic DPP implementation will assume 1215 * STATUS_CONFIGURE_FAILURE which only includes the E-Nonce. 1216 */ 1217 if (status == DPP_STATUS_OK) { 1218 json = dpp_configuration_to_json(dpp->config); 1219 json_len = strlen(json); 1220 1221 ptr += dpp_append_wrapped_data(attrs + 2, ptr - attrs - 2, 1222 NULL, 0, ptr, sizeof(attrs), 1223 dpp->ke, dpp->key_len, 2, 1224 DPP_ATTR_ENROLLEE_NONCE, 1225 dpp->nonce_len, dpp->e_nonce, 1226 DPP_ATTR_CONFIGURATION_OBJECT, 1227 json_len, json); 1228 } else 1229 ptr += dpp_append_wrapped_data(attrs + 2, ptr - attrs - 2, 1230 NULL, 0, ptr, sizeof(attrs), 1231 dpp->ke, dpp->key_len, 2, 1232 DPP_ATTR_ENROLLEE_NONCE, 1233 dpp->nonce_len, dpp->e_nonce); 1234 1235 l_put_le16(ptr - attrs - 2, attrs); 1236 1237 iov[1].iov_base = attrs; 1238 iov[1].iov_len = ptr - attrs; 1239 1240 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1241 } 1242 1243 static bool dpp_check_config_header(const uint8_t *ptr) 1244 { 1245 /* 1246 * Table 58. General Format of DPP Configuration Request frame 1247 * 1248 * Unfortunately wpa_supplicant hard codes 0x7f as the Query Response 1249 * Info so we need to handle both cases. 1250 */ 1251 return ptr[0] == IE_TYPE_ADVERTISEMENT_PROTOCOL && 1252 ptr[1] == 0x08 && 1253 (ptr[2] == 0x7f || ptr[2] == 0x00) && 1254 ptr[3] == IE_TYPE_VENDOR_SPECIFIC && 1255 ptr[4] == 5; 1256 } 1257 1258 static void dpp_handle_config_request_frame(const struct mmpdu_header *frame, 1259 const void *body, size_t body_len, 1260 int rssi, void *user_data) 1261 { 1262 struct dpp_sm *dpp = user_data; 1263 const uint8_t *ptr = body; 1264 struct dpp_attr_iter iter; 1265 enum dpp_attribute_type type; 1266 size_t len; 1267 const uint8_t *data; 1268 const char *json = NULL; 1269 size_t json_len = 0; 1270 struct json_contents *c; 1271 const uint8_t *wrapped = NULL; 1272 const uint8_t *e_nonce = NULL; 1273 size_t wrapped_len = 0; 1274 _auto_(l_free) uint8_t *unwrapped = NULL; 1275 struct json_iter jsiter; 1276 _auto_(l_free) char *tech = NULL; 1277 _auto_(l_free) char *role = NULL; 1278 1279 if (dpp->state != DPP_STATE_AUTHENTICATING) { 1280 l_debug("Configuration request in wrong state"); 1281 return; 1282 } 1283 1284 if (dpp->role != DPP_CAPABILITY_CONFIGURATOR) 1285 return; 1286 1287 if (memcmp(dpp->peer_addr, frame->address_2, 6)) { 1288 l_debug("Configuration request not from authenticated peer"); 1289 return; 1290 } 1291 1292 if (body_len < 15) { 1293 l_debug("Configuration request data not long enough"); 1294 return; 1295 } 1296 1297 ptr += 2; 1298 1299 dpp->diag_token = *ptr++; 1300 1301 if (!dpp_check_config_header(ptr)) 1302 return; 1303 1304 ptr += 5; 1305 1306 if (memcmp(ptr, wifi_alliance_oui, sizeof(wifi_alliance_oui))) 1307 return; 1308 1309 ptr += sizeof(wifi_alliance_oui); 1310 1311 if (*ptr != 0x1a && *(ptr + 1) != 1) 1312 return; 1313 1314 ptr += 2; 1315 1316 len = l_get_le16(ptr); 1317 ptr += 2; 1318 1319 if (len > body_len - 15) 1320 return; 1321 1322 dpp_attr_iter_init(&iter, ptr, len); 1323 1324 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1325 switch (type) { 1326 case DPP_ATTR_WRAPPED_DATA: 1327 wrapped = data; 1328 wrapped_len = len; 1329 break; 1330 default: 1331 /* Wrapped data should be only attribute */ 1332 return; 1333 } 1334 } 1335 1336 if (!wrapped) { 1337 l_debug("Wrapped data missing"); 1338 return; 1339 } 1340 1341 unwrapped = dpp_unwrap_attr(NULL, 0, NULL, 0, dpp->ke, 1342 dpp->key_len, wrapped, wrapped_len, 1343 &wrapped_len); 1344 if (!unwrapped) { 1345 l_debug("Failed to unwrap"); 1346 return; 1347 } 1348 1349 dpp_attr_iter_init(&iter, unwrapped, wrapped_len); 1350 1351 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1352 switch (type) { 1353 case DPP_ATTR_ENROLLEE_NONCE: 1354 if (len != dpp->nonce_len) 1355 break; 1356 1357 e_nonce = data; 1358 break; 1359 case DPP_ATTR_CONFIGURATION_REQUEST: 1360 json = (const char *)data; 1361 json_len = len; 1362 break; 1363 default: 1364 break; 1365 } 1366 } 1367 1368 if (!json || !e_nonce) { 1369 l_debug("No configuration object in response"); 1370 return; 1371 } 1372 1373 c = json_contents_new(json, json_len); 1374 if (!c) { 1375 json_contents_free(c); 1376 return; 1377 } 1378 1379 json_iter_init(&jsiter, c); 1380 1381 /* 1382 * Check mandatory values (Table 7). There isn't much that can be done 1383 * with these, but the spec requires they be included. 1384 */ 1385 if (!json_iter_parse(&jsiter, 1386 JSON_MANDATORY("name", JSON_STRING, NULL), 1387 JSON_MANDATORY("wi-fi_tech", JSON_STRING, &tech), 1388 JSON_MANDATORY("netRole", JSON_STRING, &role), 1389 JSON_UNDEFINED)) 1390 goto configure_failure; 1391 1392 if (strcmp(tech, "infra")) 1393 goto configure_failure; 1394 1395 if (strcmp(role, "sta")) 1396 goto configure_failure; 1397 1398 json_contents_free(c); 1399 1400 memcpy(dpp->e_nonce, e_nonce, dpp->nonce_len); 1401 1402 dpp->state = DPP_STATE_CONFIGURING; 1403 1404 dpp_send_config_response(dpp, DPP_STATUS_OK); 1405 1406 return; 1407 1408 configure_failure: 1409 dpp_send_config_response(dpp, DPP_STATUS_CONFIGURE_FAILURE); 1410 /* 1411 * The other peer is still authenticated, and can potentially send 1412 * additional requests so keep this session alive. 1413 */ 1414 } 1415 1416 static void dpp_handle_config_result_frame(struct dpp_sm *dpp, 1417 const uint8_t *from, const void *body, 1418 size_t body_len) 1419 { 1420 struct dpp_attr_iter iter; 1421 enum dpp_attribute_type type; 1422 size_t len; 1423 const uint8_t *data; 1424 int status = -1; 1425 const void *e_nonce = NULL; 1426 const void *wrapped = NULL; 1427 size_t wrapped_len; 1428 _auto_(l_free) void *unwrapped = NULL; 1429 1430 if (dpp->state != DPP_STATE_CONFIGURING) 1431 return; 1432 1433 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 1434 1435 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1436 switch (type) { 1437 case DPP_ATTR_WRAPPED_DATA: 1438 wrapped = data; 1439 wrapped_len = len; 1440 break; 1441 default: 1442 /* Wrapped data should be only attribute */ 1443 return; 1444 } 1445 } 1446 1447 if (!wrapped) 1448 return; 1449 1450 unwrapped = dpp_unwrap_attr(body + 2, wrapped - body - 6, wrapped, 0, 1451 dpp->ke, dpp->key_len, wrapped, 1452 wrapped_len, &wrapped_len); 1453 if (!unwrapped) { 1454 l_debug("Failed to unwrap DPP configuration result"); 1455 return; 1456 } 1457 1458 dpp_attr_iter_init(&iter, unwrapped, wrapped_len); 1459 1460 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1461 switch (type) { 1462 case DPP_ATTR_STATUS: 1463 status = l_get_u8(data); 1464 break; 1465 case DPP_ATTR_ENROLLEE_NONCE: 1466 e_nonce = data; 1467 break; 1468 default: 1469 break; 1470 } 1471 } 1472 1473 if (status != DPP_STATUS_OK || !e_nonce) 1474 l_debug("Enrollee signaled a failed configuration"); 1475 else 1476 l_debug("Configuration success"); 1477 1478 dpp_reset(dpp); 1479 } 1480 1481 /* 1482 * The Authentication protocol has a consistent use of AD components, and this 1483 * use is defined in 6.3.1.4: 1484 * 1485 * "Invocations of AES-SIV in the DPP Authentication protocol that produce 1486 * ciphertext that is part of an additional AES-SIV invocation do not use AAD; 1487 * in other words, the number of AAD components is set to zero. All other 1488 * invocations of AES-SIV in the DPP Authentication protocol shall pass a vector 1489 * of AAD having two components of AAD in the following order: (1) the DPP 1490 * header, as defined in Table 30, from the OUI field (inclusive) to the DPP 1491 * Frame Type field (inclusive); and (2) all octets in a DPP Public Action frame 1492 * after the DPP Frame Type field up to and including the last octet of the last 1493 * attribute before the Wrapped Data attribute" 1494 * 1495 * In practice you see this as AD0 being some offset in the frame (offset to the 1496 * OUI). For outgoing packets this is 26 bytes offset since the header is built 1497 * manually. For incoming packets the offset is 2 bytes. The length is always 1498 * 6 bytes for AD0. 1499 * 1500 * The AD1 data is always the start of the attributes, and length is the number 1501 * of bytes from these attributes to wrapped data. e.g. 1502 * 1503 * ad1 = attrs 1504 * ad1_len = ptr - attrs 1505 */ 1506 static void send_authenticate_response(struct dpp_sm *dpp) 1507 { 1508 uint8_t hdr[32]; 1509 uint8_t attrs[512]; 1510 uint8_t *ptr = attrs; 1511 uint8_t status = DPP_STATUS_OK; 1512 uint64_t r_proto_key[L_ECC_MAX_DIGITS * 2]; 1513 uint8_t version = 2; 1514 struct iovec iov[3]; 1515 uint8_t wrapped2_plaintext[dpp->key_len + 4]; 1516 uint8_t wrapped2[dpp->key_len + 16 + 8]; 1517 size_t wrapped2_len; 1518 1519 l_ecc_point_get_data(dpp->own_proto_public, r_proto_key, 1520 sizeof(r_proto_key)); 1521 1522 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), 1523 dpp->peer_addr, 1524 DPP_FRAME_AUTHENTICATION_RESPONSE, hdr); 1525 iov[0].iov_base = hdr; 1526 1527 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1); 1528 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, 1529 dpp->own_boot_hash, 32); 1530 if (dpp->mutual_auth) 1531 ptr += dpp_append_attr(ptr, DPP_ATTR_INITIATOR_BOOT_KEY_HASH, 1532 dpp->peer_boot_hash, 32); 1533 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_PROTOCOL_KEY, 1534 r_proto_key, dpp->key_len * 2); 1535 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, &version, 1); 1536 1537 /* Wrap up secondary data (R-Auth) */ 1538 wrapped2_len = dpp_append_attr(wrapped2_plaintext, 1539 DPP_ATTR_RESPONDER_AUTH_TAG, 1540 dpp->auth_tag, dpp->key_len); 1541 /* 1542 * "Invocations of AES-SIV in the DPP Authentication protocol that 1543 * produce ciphertext that is part of an additional AES-SIV invocation 1544 * do not use AAD; in other words, the number of AAD components is set 1545 * to zero."" 1546 */ 1547 if (!aes_siv_encrypt(dpp->ke, dpp->key_len, wrapped2_plaintext, 1548 dpp->key_len + 4, NULL, 0, wrapped2)) { 1549 l_error("Failed to encrypt wrapped data"); 1550 return; 1551 } 1552 1553 wrapped2_len += 16; 1554 1555 ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs, 1556 ptr, sizeof(attrs), dpp->k2, dpp->key_len, 4, 1557 DPP_ATTR_RESPONDER_NONCE, dpp->nonce_len, dpp->r_nonce, 1558 DPP_ATTR_INITIATOR_NONCE, dpp->nonce_len, dpp->i_nonce, 1559 DPP_ATTR_RESPONDER_CAPABILITIES, (size_t) 1, &dpp->role, 1560 DPP_ATTR_WRAPPED_DATA, wrapped2_len, wrapped2); 1561 1562 iov[1].iov_base = attrs; 1563 iov[1].iov_len = ptr - attrs; 1564 1565 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1566 } 1567 1568 static void authenticate_confirm(struct dpp_sm *dpp, const uint8_t *from, 1569 const uint8_t *body, size_t body_len) 1570 { 1571 struct dpp_attr_iter iter; 1572 enum dpp_attribute_type type; 1573 size_t len; 1574 const uint8_t *data; 1575 int status = -1; 1576 const uint8_t *r_boot_hash = NULL; 1577 const void *wrapped = NULL; 1578 const uint8_t *i_auth = NULL; 1579 size_t i_auth_len; 1580 _auto_(l_free) uint8_t *unwrapped = NULL; 1581 size_t wrapped_len = 0; 1582 uint64_t i_auth_check[L_ECC_MAX_DIGITS]; 1583 const void *unwrap_key; 1584 const void *ad0 = body + 2; 1585 const void *ad1 = body + 8; 1586 struct l_ecc_point *bi = NULL; 1587 1588 if (dpp->state != DPP_STATE_AUTHENTICATING) 1589 return; 1590 1591 if (memcmp(from, dpp->peer_addr, 6)) 1592 return; 1593 1594 l_debug("authenticate confirm"); 1595 1596 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 1597 1598 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1599 switch (type) { 1600 case DPP_ATTR_STATUS: 1601 status = l_get_u8(data); 1602 break; 1603 case DPP_ATTR_RESPONDER_BOOT_KEY_HASH: 1604 r_boot_hash = data; 1605 /* 1606 * Spec requires this, but does not mention if anything 1607 * is to be done with it. 1608 */ 1609 break; 1610 case DPP_ATTR_INITIATOR_BOOT_KEY_HASH: 1611 /* No mutual authentication */ 1612 break; 1613 case DPP_ATTR_WRAPPED_DATA: 1614 wrapped = data; 1615 wrapped_len = len; 1616 break; 1617 default: 1618 break; 1619 } 1620 } 1621 1622 if (!r_boot_hash || !wrapped) { 1623 l_debug("Attributes missing from authenticate confirm"); 1624 return; 1625 } 1626 1627 /* 1628 * "The Responder obtains the DPP Authentication Confirm frame and 1629 * checks the value of the DPP Status field. If the value of the DPP 1630 * Status field is STATUS_NOT_COMPATIBLE or STATUS_AUTH_FAILURE, the 1631 * Responder unwraps the wrapped data portion of the frame using k2" 1632 */ 1633 if (status == DPP_STATUS_OK) 1634 unwrap_key = dpp->ke; 1635 else if (status == DPP_STATUS_NOT_COMPATIBLE || 1636 status == DPP_STATUS_AUTH_FAILURE) 1637 unwrap_key = dpp->k2; 1638 else 1639 goto auth_confirm_failed; 1640 1641 unwrapped = dpp_unwrap_attr(ad0, 6, ad1, wrapped - 4 - ad1, 1642 unwrap_key, dpp->key_len, wrapped, wrapped_len, 1643 &wrapped_len); 1644 if (!unwrapped) 1645 goto auth_confirm_failed; 1646 1647 if (status != DPP_STATUS_OK) { 1648 /* 1649 * "If unwrapping is successful, the Responder should generate 1650 * an alert indicating the reason for the protocol failure." 1651 */ 1652 l_debug("Authentication failed due to status %s", 1653 status == DPP_STATUS_NOT_COMPATIBLE ? 1654 "NOT_COMPATIBLE" : "AUTH_FAILURE"); 1655 goto auth_confirm_failed; 1656 } 1657 1658 dpp_attr_iter_init(&iter, unwrapped, wrapped_len); 1659 1660 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 1661 switch (type) { 1662 case DPP_ATTR_INITIATOR_AUTH_TAG: 1663 i_auth = data; 1664 i_auth_len = len; 1665 break; 1666 case DPP_ATTR_RESPONDER_NONCE: 1667 /* Only if error */ 1668 break; 1669 default: 1670 break; 1671 } 1672 } 1673 1674 if (!i_auth || i_auth_len != dpp->key_len) { 1675 l_debug("I-Auth missing from wrapped data"); 1676 goto auth_confirm_failed; 1677 } 1678 1679 if (dpp->mutual_auth) 1680 bi = dpp->peer_boot_public; 1681 1682 dpp_derive_i_auth(dpp->r_nonce, dpp->i_nonce, dpp->nonce_len, 1683 dpp->own_proto_public, dpp->peer_proto_public, 1684 dpp->boot_public, bi, i_auth_check); 1685 1686 if (memcmp(i_auth, i_auth_check, i_auth_len)) { 1687 l_error("I-Auth did not verify"); 1688 goto auth_confirm_failed; 1689 } 1690 1691 l_debug("Authentication successful"); 1692 1693 dpp_reset_protocol_timer(dpp, DPP_AUTH_PROTO_TIMEOUT); 1694 1695 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 1696 dpp_configuration_start(dpp, from); 1697 1698 return; 1699 1700 auth_confirm_failed: 1701 dpp->state = DPP_STATE_PRESENCE; 1702 dpp_free_auth_data(dpp); 1703 } 1704 1705 static void dpp_auth_request_failed(struct dpp_sm *dpp, 1706 enum dpp_status status, 1707 void *k1) 1708 { 1709 uint8_t hdr[32]; 1710 uint8_t attrs[128]; 1711 uint8_t *ptr = attrs; 1712 uint8_t version = 2; 1713 uint8_t s = status; 1714 struct iovec iov[2]; 1715 1716 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), 1717 dpp->peer_addr, 1718 DPP_FRAME_AUTHENTICATION_RESPONSE, hdr); 1719 iov[0].iov_base = hdr; 1720 1721 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &s, 1); 1722 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, 1723 dpp->own_boot_hash, 32); 1724 1725 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, &version, 1); 1726 1727 ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs, 1728 ptr, sizeof(attrs) - (ptr - attrs), k1, dpp->key_len, 2, 1729 DPP_ATTR_INITIATOR_NONCE, dpp->nonce_len, dpp->i_nonce, 1730 DPP_ATTR_RESPONDER_CAPABILITIES, 1731 (size_t) 1, &dpp->role); 1732 1733 iov[1].iov_base = attrs; 1734 iov[1].iov_len = ptr - attrs; 1735 1736 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1737 } 1738 1739 static bool dpp_check_roles(struct dpp_sm *dpp, uint8_t peer_capa) 1740 { 1741 if (dpp->role == DPP_CAPABILITY_ENROLLEE && 1742 !(peer_capa & DPP_CAPABILITY_CONFIGURATOR)) 1743 return false; 1744 else if (dpp->role == DPP_CAPABILITY_CONFIGURATOR && 1745 !(peer_capa & DPP_CAPABILITY_ENROLLEE)) 1746 return false; 1747 1748 return true; 1749 } 1750 1751 static void dpp_presence_announce(struct dpp_sm *dpp) 1752 { 1753 struct netdev *netdev = dpp->netdev; 1754 uint8_t hdr[32]; 1755 uint8_t attrs[32 + 4]; 1756 uint8_t hash[32]; 1757 uint8_t *ptr = attrs; 1758 const uint8_t *addr = netdev_get_address(netdev); 1759 struct iovec iov[2]; 1760 1761 iov[0].iov_len = dpp_build_header(addr, broadcast, 1762 DPP_FRAME_PRESENCE_ANNOUNCEMENT, hdr); 1763 iov[0].iov_base = hdr; 1764 1765 dpp_hash(L_CHECKSUM_SHA256, hash, 2, "chirp", strlen("chirp"), 1766 dpp->own_asn1, dpp->own_asn1_len); 1767 1768 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, hash, 32); 1769 1770 iov[1].iov_base = attrs; 1771 iov[1].iov_len = ptr - attrs; 1772 1773 l_debug("Sending presence announcement on frequency %u and waiting %u", 1774 dpp->current_freq, dpp->dwell); 1775 1776 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1777 } 1778 1779 static bool dpp_send_authenticate_request(struct dpp_sm *dpp) 1780 { 1781 uint8_t hdr[32]; 1782 uint8_t attrs[256]; 1783 uint8_t *ptr = attrs; 1784 uint64_t i_proto_key[L_ECC_MAX_DIGITS * 2]; 1785 uint8_t version = 2; 1786 struct iovec iov[2]; 1787 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 1788 struct scan_bss *bss = station_get_connected_bss(station); 1789 1790 /* Got disconnected by the time the peer was discovered */ 1791 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR && !bss) { 1792 dpp_reset(dpp); 1793 return false; 1794 } 1795 1796 l_ecc_point_get_data(dpp->own_proto_public, i_proto_key, 1797 sizeof(i_proto_key)); 1798 1799 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), 1800 dpp->peer_addr, 1801 DPP_FRAME_AUTHENTICATION_REQUEST, hdr); 1802 iov[0].iov_base = hdr; 1803 1804 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, 1805 dpp->peer_boot_hash, 32); 1806 ptr += dpp_append_attr(ptr, DPP_ATTR_INITIATOR_BOOT_KEY_HASH, 1807 dpp->own_boot_hash, 32); 1808 ptr += dpp_append_attr(ptr, DPP_ATTR_INITIATOR_PROTOCOL_KEY, 1809 i_proto_key, dpp->key_len * 2); 1810 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, &version, 1); 1811 1812 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR && 1813 dpp->current_freq != bss->frequency) { 1814 uint8_t pair[2] = { 81, 1815 band_freq_to_channel(bss->frequency, NULL) }; 1816 1817 ptr += dpp_append_attr(ptr, DPP_ATTR_CHANNEL, pair, 2); 1818 } 1819 1820 ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs, 1821 ptr, sizeof(attrs), dpp->k1, dpp->key_len, 2, 1822 DPP_ATTR_INITIATOR_NONCE, dpp->nonce_len, dpp->i_nonce, 1823 DPP_ATTR_INITIATOR_CAPABILITIES, 1824 (size_t) 1, &dpp->role); 1825 1826 iov[1].iov_base = attrs; 1827 iov[1].iov_len = ptr - attrs; 1828 1829 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1830 1831 return true; 1832 } 1833 1834 static void dpp_send_pkex_exchange_request(struct dpp_sm *dpp) 1835 { 1836 uint8_t hdr[32]; 1837 uint8_t attrs[256]; 1838 uint8_t *ptr = attrs; 1839 uint64_t m_data[L_ECC_MAX_DIGITS * 2]; 1840 uint16_t group; 1841 struct iovec iov[2]; 1842 const uint8_t *own_mac = netdev_get_address(dpp->netdev); 1843 1844 l_put_le16(l_ecc_curve_get_ike_group(dpp->curve), &group); 1845 1846 iov[0].iov_len = dpp_build_header(own_mac, dpp->peer_addr, 1847 DPP_FRAME_PKEX_VERSION1_XCHG_REQUEST, hdr); 1848 iov[0].iov_base = hdr; 1849 1850 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, 1851 &dpp->pkex_version, 1); 1852 ptr += dpp_append_attr(ptr, DPP_ATTR_FINITE_CYCLIC_GROUP, 1853 &group, 2); 1854 1855 if (dpp->pkex_id) 1856 ptr += dpp_append_attr(ptr, DPP_ATTR_CODE_IDENTIFIER, 1857 dpp->pkex_id, strlen(dpp->pkex_id)); 1858 1859 l_ecc_point_get_data(dpp->pkex_m, m_data, sizeof(m_data)); 1860 1861 ptr += dpp_append_attr(ptr, DPP_ATTR_ENCRYPTED_KEY, 1862 m_data, dpp->key_len * 2); 1863 1864 iov[1].iov_base = attrs; 1865 iov[1].iov_len = ptr - attrs; 1866 1867 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1868 } 1869 1870 static void dpp_send_commit_reveal_request(struct dpp_sm *dpp) 1871 { 1872 struct iovec iov[2]; 1873 uint8_t hdr[41]; 1874 uint8_t attrs[512]; 1875 uint8_t *ptr = attrs; 1876 uint8_t zero = 0; 1877 uint8_t a_pub[L_ECC_POINT_MAX_BYTES]; 1878 ssize_t a_len; 1879 1880 a_len = l_ecc_point_get_data(dpp->boot_public, a_pub, sizeof(a_pub)); 1881 1882 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), 1883 dpp->peer_addr, 1884 DPP_FRAME_PKEX_COMMIT_REVEAL_REQUEST, 1885 hdr); 1886 iov[0].iov_base = hdr; 1887 1888 ptr += dpp_append_wrapped_data(hdr + 26, 6, &zero, 1, ptr, 1889 sizeof(attrs), dpp->z, dpp->z_len, 2, 1890 DPP_ATTR_BOOTSTRAPPING_KEY, a_len, a_pub, 1891 DPP_ATTR_INITIATOR_AUTH_TAG, dpp->u_len, dpp->u); 1892 1893 iov[1].iov_base = attrs; 1894 iov[1].iov_len = ptr - attrs; 1895 1896 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 1897 } 1898 1899 static void dpp_roc_started(void *user_data) 1900 { 1901 struct dpp_sm *dpp = user_data; 1902 1903 /* 1904 * - If a configurator, nothing to do but wait for a request 1905 * (unless multicast frame registration is unsupported in which case 1906 * send an authenticate request now) 1907 * - If in the presence state continue sending announcements. 1908 * - If authenticating, and this is a result of a channel switch send 1909 * the authenticate response now. 1910 */ 1911 1912 dpp->roc_started = true; 1913 1914 /* 1915 * The retry timer indicates a frame was not acked in which case we 1916 * should not change any state or send any frames until that expires. 1917 */ 1918 if (dpp->retry_timeout) 1919 return; 1920 1921 if (dpp->frame_pending) { 1922 dpp_frame_retry(dpp); 1923 return; 1924 } 1925 1926 switch (dpp->state) { 1927 case DPP_STATE_PRESENCE: 1928 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR) 1929 return; 1930 1931 if (dpp->pending) { 1932 struct l_dbus_message *reply = 1933 l_dbus_message_new_method_return(dpp->pending); 1934 1935 l_dbus_message_set_arguments(reply, "s", dpp->uri); 1936 1937 dbus_pending_reply(&dpp->pending, reply); 1938 } 1939 1940 dpp_presence_announce(dpp); 1941 break; 1942 case DPP_STATE_AUTHENTICATING: 1943 /* 1944 * No multicast frame registration support, jump right into 1945 * sending auth frames. This diverges from the 2.0 spec, but in 1946 * reality the the main path nearly all drivers will hit. 1947 */ 1948 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR) { 1949 if (dpp->mcast_support) 1950 return; 1951 1952 dpp_send_authenticate_request(dpp); 1953 return; 1954 } 1955 1956 if (dpp->new_freq) { 1957 dpp->current_freq = dpp->new_freq; 1958 dpp->new_freq = 0; 1959 send_authenticate_response(dpp); 1960 } 1961 1962 break; 1963 case DPP_STATE_PKEX_EXCHANGE: 1964 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 1965 dpp_send_pkex_exchange_request(dpp); 1966 1967 break; 1968 case DPP_STATE_PKEX_COMMIT_REVEAL: 1969 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 1970 dpp_send_commit_reveal_request(dpp); 1971 1972 break; 1973 default: 1974 break; 1975 } 1976 } 1977 1978 static void dpp_start_offchannel(struct dpp_sm *dpp, uint32_t freq); 1979 1980 static void dpp_offchannel_timeout(int error, void *user_data) 1981 { 1982 struct dpp_sm *dpp = user_data; 1983 1984 dpp->offchannel_id = 0; 1985 dpp->roc_started = false; 1986 1987 /* 1988 * If cancelled this is likely due to netdev going down or from Stop(). 1989 * Otherwise there was some other problem which is probably not 1990 * recoverable. 1991 */ 1992 if (error == -ECANCELED) 1993 return; 1994 else if (error == -EIO) 1995 goto next_roc; 1996 else if (error < 0) 1997 goto protocol_failed; 1998 1999 switch (dpp->state) { 2000 case DPP_STATE_PKEX_EXCHANGE: 2001 if (dpp->role != DPP_CAPABILITY_CONFIGURATOR || !dpp->agent) 2002 break; 2003 2004 /* 2005 * We have a pending agent request but it did not arrive in 2006 * time, we can't assume the enrollee will be waiting around 2007 * for our response so cancel the request and continue waiting 2008 * for another request 2009 */ 2010 if (dpp->agent->pending_id) { 2011 dpp_free_pending_pkex_data(dpp); 2012 dpp_agent_cancel(dpp); 2013 } 2014 /* Fall through */ 2015 case DPP_STATE_PRESENCE: 2016 break; 2017 case DPP_STATE_NOTHING: 2018 /* Protocol already terminated */ 2019 return; 2020 case DPP_STATE_AUTHENTICATING: 2021 case DPP_STATE_CONFIGURING: 2022 case DPP_STATE_PKEX_COMMIT_REVEAL: 2023 goto next_roc; 2024 } 2025 2026 dpp->freqs_idx++; 2027 2028 if (dpp->freqs_idx >= dpp->freqs_len) { 2029 l_debug("Max retries offchannel"); 2030 dpp->freqs_idx = 0; 2031 } 2032 2033 dpp->current_freq = dpp->freqs[dpp->freqs_idx]; 2034 2035 l_debug("Offchannel timeout, moving to next frequency %u, duration %u", 2036 dpp->current_freq, dpp->dwell); 2037 2038 next_roc: 2039 dpp_start_offchannel(dpp, dpp->current_freq); 2040 2041 return; 2042 2043 protocol_failed: 2044 dpp_reset(dpp); 2045 } 2046 2047 static void dpp_start_offchannel(struct dpp_sm *dpp, uint32_t freq) 2048 { 2049 /* 2050 * This needs to be handled carefully for a few reasons: 2051 * 2052 * First, the next offchannel operation needs to be started prior to 2053 * canceling an existing one. This is so the offchannel work can 2054 * continue uninterrupted without any other work items starting in 2055 * between canceling and starting the next (e.g. if a scan request is 2056 * sitting in the queue). 2057 * 2058 * Second, dpp_offchannel_timeout resets dpp->offchannel_id to zero 2059 * which is why the new ID is saved and only set to dpp->offchannel_id 2060 * once the previous offchannel work is cancelled (i.e. destroy() has 2061 * been called). 2062 */ 2063 uint32_t id = offchannel_start(netdev_get_wdev_id(dpp->netdev), 2064 WIPHY_WORK_PRIORITY_OFFCHANNEL, 2065 freq, dpp->dwell, dpp_roc_started, 2066 dpp, dpp_offchannel_timeout); 2067 2068 if (dpp->offchannel_id) 2069 offchannel_cancel(dpp->wdev_id, dpp->offchannel_id); 2070 2071 dpp->offchannel_id = id; 2072 } 2073 2074 static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from, 2075 const uint8_t *body, size_t body_len) 2076 { 2077 struct dpp_attr_iter iter; 2078 enum dpp_attribute_type type; 2079 size_t len; 2080 const uint8_t *data; 2081 const uint8_t *r_boot = NULL; 2082 const uint8_t *i_boot = NULL; 2083 const uint8_t *i_proto = NULL; 2084 const void *wrapped = NULL; 2085 const uint8_t *i_nonce = NULL; 2086 uint8_t i_capa = 0; 2087 size_t r_boot_len = 0, i_proto_len = 0, wrapped_len = 0; 2088 size_t i_nonce_len = 0; 2089 _auto_(l_free) uint8_t *unwrapped = NULL; 2090 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *m = NULL; 2091 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL; 2092 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 2093 struct l_ecc_point *bi = NULL; 2094 uint64_t k1[L_ECC_MAX_DIGITS]; 2095 const void *ad0 = body + 2; 2096 const void *ad1 = body + 8; 2097 uint32_t freq; 2098 2099 if (util_is_broadcast_address(from)) 2100 return; 2101 2102 if (dpp->state != DPP_STATE_PRESENCE && 2103 dpp->state != DPP_STATE_AUTHENTICATING) 2104 return; 2105 2106 l_debug("authenticate request"); 2107 2108 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 2109 2110 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2111 switch (type) { 2112 case DPP_ATTR_INITIATOR_BOOT_KEY_HASH: 2113 i_boot = data; 2114 /* 2115 * This attribute is required by the spec, but only 2116 * used for mutual authentication. 2117 */ 2118 break; 2119 case DPP_ATTR_RESPONDER_BOOT_KEY_HASH: 2120 r_boot = data; 2121 r_boot_len = len; 2122 break; 2123 case DPP_ATTR_INITIATOR_PROTOCOL_KEY: 2124 i_proto = data; 2125 i_proto_len = len; 2126 break; 2127 case DPP_ATTR_WRAPPED_DATA: 2128 /* I-Nonce/I-Capabilities part of wrapped data */ 2129 wrapped = data; 2130 wrapped_len = len; 2131 break; 2132 2133 /* Optional attributes */ 2134 case DPP_ATTR_PROTOCOL_VERSION: 2135 if (l_get_u8(data) != 2) { 2136 l_debug("Protocol version did not match"); 2137 return; 2138 } 2139 2140 break; 2141 2142 case DPP_ATTR_CHANNEL: 2143 if (len != 2) 2144 return; 2145 2146 freq = oci_to_frequency(l_get_u8(data), 2147 l_get_u8(data + 1)); 2148 2149 if (freq == dpp->current_freq) 2150 break; 2151 2152 /* 2153 * Configurators are already connected to a network, so 2154 * to preserve wireless performance the enrollee will 2155 * be required to be on this channel, not a channel it 2156 * requests. 2157 */ 2158 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR) 2159 return; 2160 2161 /* 2162 * Otherwise, as an enrollee, we can jump to whatever 2163 * channel the configurator requests 2164 */ 2165 dpp->new_freq = freq; 2166 2167 l_debug("Configurator requested a new frequency %u", 2168 dpp->new_freq); 2169 2170 dpp_start_offchannel(dpp, dpp->new_freq); 2171 2172 break; 2173 default: 2174 break; 2175 } 2176 } 2177 2178 if (!r_boot || !i_boot || !i_proto || !wrapped) 2179 goto auth_request_failed; 2180 2181 if (r_boot_len != 32 || memcmp(dpp->own_boot_hash, 2182 r_boot, r_boot_len)) { 2183 l_debug("Responder boot key hash failed to verify"); 2184 goto auth_request_failed; 2185 } 2186 2187 dpp->peer_proto_public = l_ecc_point_from_data(dpp->curve, 2188 L_ECC_POINT_TYPE_FULL, 2189 i_proto, i_proto_len); 2190 if (!dpp->peer_proto_public) { 2191 l_debug("Initiators protocol key invalid"); 2192 goto auth_request_failed; 2193 } 2194 2195 m = dpp_derive_k1(dpp->peer_proto_public, dpp->boot_private, k1); 2196 if (!m) 2197 goto auth_request_failed; 2198 2199 unwrapped = dpp_unwrap_attr(ad0, 6, ad1, wrapped - 4 - ad1, 2200 k1, dpp->key_len, wrapped, wrapped_len, &wrapped_len); 2201 if (!unwrapped) 2202 goto auth_request_failed; 2203 2204 dpp_attr_iter_init(&iter, unwrapped, wrapped_len); 2205 2206 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2207 switch (type) { 2208 case DPP_ATTR_INITIATOR_NONCE: 2209 i_nonce = data; 2210 i_nonce_len = len; 2211 break; 2212 case DPP_ATTR_INITIATOR_CAPABILITIES: 2213 /* 2214 * "If the Responder is not capable of supporting the 2215 * role indicated by the Initiator, it shall respond 2216 * with a DPP Authentication Response frame indicating 2217 * failure by adding the DPP Status field set to 2218 * STATUS_NOT_COMPATIBLE" 2219 */ 2220 i_capa = l_get_u8(data); 2221 2222 if (!dpp_check_roles(dpp, i_capa)) { 2223 l_debug("Peer does not support required role"); 2224 dpp_auth_request_failed(dpp, 2225 DPP_STATUS_NOT_COMPATIBLE, k1); 2226 goto auth_request_failed; 2227 } 2228 2229 break; 2230 default: 2231 break; 2232 } 2233 } 2234 2235 if (i_nonce_len != dpp->nonce_len) { 2236 l_debug("I-Nonce has unexpected length %zu", i_nonce_len); 2237 goto auth_request_failed; 2238 } 2239 2240 memcpy(dpp->i_nonce, i_nonce, i_nonce_len); 2241 2242 if (dpp->mutual_auth) { 2243 l = dpp_derive_lr(dpp->boot_private, dpp->proto_private, 2244 dpp->peer_boot_public); 2245 bi = dpp->peer_boot_public; 2246 } 2247 2248 /* Derive keys k2, ke, and R-Auth for authentication response */ 2249 2250 n = dpp_derive_k2(dpp->peer_proto_public, dpp->proto_private, dpp->k2); 2251 if (!n) 2252 goto auth_request_failed; 2253 2254 l_getrandom(dpp->r_nonce, dpp->nonce_len); 2255 2256 if (!dpp_derive_ke(dpp->i_nonce, dpp->r_nonce, m, n, l, dpp->ke)) 2257 goto auth_request_failed; 2258 2259 if (!dpp_derive_r_auth(dpp->i_nonce, dpp->r_nonce, dpp->nonce_len, 2260 dpp->peer_proto_public, dpp->own_proto_public, 2261 bi, dpp->boot_public, dpp->auth_tag)) 2262 goto auth_request_failed; 2263 2264 memcpy(dpp->peer_addr, from, 6); 2265 2266 dpp->state = DPP_STATE_AUTHENTICATING; 2267 dpp_reset_protocol_timer(dpp, DPP_AUTH_PROTO_TIMEOUT); 2268 2269 /* Don't send if the frequency is changing */ 2270 if (!dpp->new_freq) 2271 send_authenticate_response(dpp); 2272 2273 return; 2274 2275 auth_request_failed: 2276 dpp->state = DPP_STATE_PRESENCE; 2277 dpp_free_auth_data(dpp); 2278 } 2279 2280 static void dpp_send_authenticate_confirm(struct dpp_sm *dpp) 2281 { 2282 uint8_t hdr[32]; 2283 struct iovec iov[2]; 2284 uint8_t attrs[256]; 2285 uint8_t *ptr = attrs; 2286 uint8_t zero = 0; 2287 2288 iov[0].iov_len = dpp_build_header(netdev_get_address(dpp->netdev), 2289 dpp->peer_addr, 2290 DPP_FRAME_AUTHENTICATION_CONFIRM, hdr); 2291 iov[0].iov_base = hdr; 2292 2293 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &zero, 1); 2294 ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, 2295 dpp->peer_boot_hash, 32); 2296 if (dpp->mutual_auth) 2297 ptr += dpp_append_attr(ptr, DPP_ATTR_INITIATOR_BOOT_KEY_HASH, 2298 dpp->own_boot_hash, 32); 2299 2300 ptr += dpp_append_wrapped_data(hdr + 26, 6, attrs, ptr - attrs, ptr, 2301 sizeof(attrs), dpp->ke, dpp->key_len, 1, 2302 DPP_ATTR_INITIATOR_AUTH_TAG, dpp->key_len, 2303 dpp->auth_tag); 2304 2305 iov[1].iov_base = attrs; 2306 iov[1].iov_len = ptr - attrs; 2307 2308 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 2309 } 2310 2311 static void authenticate_response(struct dpp_sm *dpp, const uint8_t *from, 2312 const uint8_t *body, size_t body_len) 2313 { 2314 struct dpp_attr_iter iter; 2315 enum dpp_attribute_type type; 2316 size_t len; 2317 const uint8_t *data; 2318 int status = -1; 2319 const void *r_boot_hash = NULL; 2320 const void *r_proto = NULL; 2321 size_t r_proto_len = 0; 2322 const void *wrapped = NULL; 2323 size_t wrapped_len; 2324 _auto_(l_free) uint8_t *unwrapped1 = NULL; 2325 _auto_(l_free) uint8_t *unwrapped2 = NULL; 2326 const void *r_nonce = NULL; 2327 const void *i_nonce = NULL; 2328 const void *r_auth = NULL; 2329 _auto_(l_ecc_point_free) struct l_ecc_point *r_proto_key = NULL; 2330 _auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL; 2331 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 2332 struct l_ecc_point *bi = NULL; 2333 const void *ad0 = body + 2; 2334 const void *ad1 = body + 8; 2335 uint64_t r_auth_derived[L_ECC_MAX_DIGITS]; 2336 2337 l_debug("Authenticate response"); 2338 2339 if (dpp->state != DPP_STATE_AUTHENTICATING) 2340 return; 2341 2342 if (!dpp->freqs) 2343 return; 2344 2345 if (memcmp(from, dpp->peer_addr, 6)) 2346 return; 2347 2348 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 2349 2350 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2351 switch (type) { 2352 case DPP_ATTR_STATUS: 2353 if (len != 1) 2354 return; 2355 2356 status = l_get_u8(data); 2357 break; 2358 case DPP_ATTR_RESPONDER_BOOT_KEY_HASH: 2359 r_boot_hash = data; 2360 break; 2361 case DPP_ATTR_RESPONDER_PROTOCOL_KEY: 2362 r_proto = data; 2363 r_proto_len = len; 2364 break; 2365 case DPP_ATTR_PROTOCOL_VERSION: 2366 if (len != 1 || l_get_u8(data) != 2) 2367 return; 2368 break; 2369 case DPP_ATTR_WRAPPED_DATA: 2370 wrapped = data; 2371 wrapped_len = len; 2372 break; 2373 default: 2374 break; 2375 } 2376 } 2377 2378 if (status != DPP_STATUS_OK || !r_boot_hash || !r_proto || !wrapped) { 2379 l_debug("Auth response bad status or missing attributes"); 2380 return; 2381 } 2382 2383 r_proto_key = l_ecc_point_from_data(dpp->curve, L_ECC_POINT_TYPE_FULL, 2384 r_proto, r_proto_len); 2385 if (!r_proto_key) { 2386 l_debug("Peers protocol key was invalid"); 2387 return; 2388 } 2389 2390 n = dpp_derive_k2(r_proto_key, dpp->proto_private, dpp->k2); 2391 2392 unwrapped1 = dpp_unwrap_attr(ad0, 6, ad1, wrapped - 4 - ad1, dpp->k2, 2393 dpp->key_len, wrapped, wrapped_len, 2394 &wrapped_len); 2395 if (!unwrapped1) { 2396 l_debug("Failed to unwrap primary data"); 2397 return; 2398 } 2399 2400 wrapped = NULL; 2401 2402 dpp_attr_iter_init(&iter, unwrapped1, wrapped_len); 2403 2404 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2405 switch (type) { 2406 case DPP_ATTR_RESPONDER_NONCE: 2407 if (len != dpp->nonce_len) 2408 return; 2409 2410 r_nonce = data; 2411 break; 2412 case DPP_ATTR_INITIATOR_NONCE: 2413 if (len != dpp->nonce_len) 2414 return; 2415 2416 i_nonce = data; 2417 break; 2418 case DPP_ATTR_RESPONDER_CAPABILITIES: 2419 break; 2420 case DPP_ATTR_WRAPPED_DATA: 2421 wrapped = data; 2422 wrapped_len = len; 2423 break; 2424 default: 2425 break; 2426 } 2427 } 2428 2429 if (!r_nonce || !i_nonce || !wrapped) { 2430 l_debug("Wrapped data missing attributes"); 2431 return; 2432 } 2433 2434 if (dpp->mutual_auth) { 2435 l = dpp_derive_li(dpp->peer_boot_public, r_proto_key, 2436 dpp->boot_private); 2437 bi = dpp->boot_public; 2438 } 2439 2440 if (!dpp_derive_ke(i_nonce, r_nonce, dpp->m, n, l, dpp->ke)) { 2441 l_debug("Failed to derive ke"); 2442 return; 2443 } 2444 2445 unwrapped2 = dpp_unwrap_attr(NULL, 0, NULL, 0, dpp->ke, dpp->key_len, 2446 wrapped, wrapped_len, &wrapped_len); 2447 if (!unwrapped2) { 2448 l_debug("Failed to unwrap secondary data"); 2449 return; 2450 } 2451 2452 dpp_attr_iter_init(&iter, unwrapped2, wrapped_len); 2453 2454 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2455 switch (type) { 2456 case DPP_ATTR_RESPONDER_AUTH_TAG: 2457 if (len != dpp->key_len) 2458 return; 2459 2460 r_auth = data; 2461 break; 2462 default: 2463 break; 2464 } 2465 } 2466 2467 if (!r_auth) { 2468 l_debug("R-Auth was not in secondary wrapped data"); 2469 return; 2470 } 2471 2472 if (!dpp_derive_r_auth(i_nonce, r_nonce, dpp->nonce_len, 2473 dpp->own_proto_public, r_proto_key, bi, 2474 dpp->peer_boot_public, r_auth_derived)) { 2475 l_debug("Failed to derive r_auth"); 2476 return; 2477 } 2478 2479 if (memcmp(r_auth, r_auth_derived, dpp->key_len)) { 2480 l_debug("R-Auth did not verify"); 2481 return; 2482 } 2483 2484 if (!dpp_derive_i_auth(r_nonce, i_nonce, dpp->nonce_len, 2485 r_proto_key, dpp->own_proto_public, 2486 dpp->peer_boot_public, bi, dpp->auth_tag)) { 2487 l_debug("Could not derive I-Auth"); 2488 return; 2489 } 2490 2491 dpp->channel_switch = false; 2492 dpp->current_freq = dpp->new_freq; 2493 2494 dpp_send_authenticate_confirm(dpp); 2495 2496 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 2497 dpp_configuration_start(dpp, from); 2498 2499 } 2500 2501 static void dpp_handle_presence_announcement(struct dpp_sm *dpp, 2502 const uint8_t *from, 2503 const uint8_t *body, 2504 size_t body_len) 2505 { 2506 struct dpp_attr_iter iter; 2507 enum dpp_attribute_type type; 2508 size_t len; 2509 const uint8_t *data; 2510 const void *r_boot = NULL; 2511 size_t r_boot_len = 0; 2512 uint8_t hash[32]; 2513 2514 l_debug("Presence announcement "MAC, MAC_STR(from)); 2515 2516 /* Must be a configurator, in an initiator role, in PRESENCE state */ 2517 if (dpp->state != DPP_STATE_PRESENCE) 2518 return; 2519 2520 if (dpp->role != DPP_CAPABILITY_CONFIGURATOR) 2521 return; 2522 2523 if (!dpp->freqs) 2524 return; 2525 2526 /* 2527 * The URI may not have contained a MAC address, if this announcement 2528 * verifies set peer_addr then. 2529 */ 2530 if (!l_memeqzero(dpp->peer_addr, 6) && 2531 memcmp(from, dpp->peer_addr, 6)) { 2532 l_debug("Unexpected source "MAC" expected "MAC, MAC_STR(from), 2533 MAC_STR(dpp->peer_addr)); 2534 return; 2535 } 2536 2537 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 2538 2539 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2540 switch (type) { 2541 case DPP_ATTR_RESPONDER_BOOT_KEY_HASH: 2542 r_boot = data; 2543 r_boot_len = len; 2544 break; 2545 default: 2546 break; 2547 } 2548 } 2549 2550 if (!r_boot || r_boot_len != 32) { 2551 l_debug("No responder boot hash"); 2552 return; 2553 } 2554 2555 /* Hash what we have for the peer and check its our enrollee */ 2556 dpp_hash(L_CHECKSUM_SHA256, hash, 2, "chirp", strlen("chirp"), 2557 dpp->peer_asn1, dpp->peer_asn1_len); 2558 2559 if (memcmp(hash, r_boot, sizeof(hash))) { 2560 l_debug("Peers boot hash did not match"); 2561 return; 2562 } 2563 2564 /* 2565 * This is the peer we expected, save away the address and derive the 2566 * initial keys. 2567 */ 2568 memcpy(dpp->peer_addr, from, 6); 2569 2570 dpp->state = DPP_STATE_AUTHENTICATING; 2571 2572 if (!dpp_send_authenticate_request(dpp)) 2573 return; 2574 2575 /* 2576 * Requested the peer to move to another channel for the remainder of 2577 * the protocol. IWD's current logic prohibits a configurator from 2578 * running while not connected, so we can assume here that the new 2579 * frequency is the same of the connected BSS. Wait until an ACK is 2580 * received for the auth request then cancel the offchannel request. 2581 */ 2582 if (dpp->current_freq != dpp->new_freq) 2583 dpp->channel_switch = true; 2584 } 2585 2586 static void dpp_pkex_bad_group(struct dpp_sm *dpp, uint16_t group) 2587 { 2588 uint16_t own_group = l_ecc_curve_get_ike_group(dpp->curve); 2589 2590 /* 2591 * TODO: The spec allows group negotiation, but it is not yet 2592 * implemented. 2593 */ 2594 if (!group) 2595 return; 2596 /* 2597 * Section 5.6.2 2598 * "If the Responder's offered group offers less security 2599 * than the Initiator's offered group, then the Initiator should 2600 * ignore this message" 2601 */ 2602 if (group < own_group) { 2603 l_debug("Offered group %u is less secure, ignoring", 2604 group); 2605 return; 2606 } 2607 /* 2608 * Section 5.6.2 2609 * "If the Responder's offered group offers equivalent or better 2610 * security than the Initiator's offered group, then the 2611 * Initiator may choose to abort its original request and try 2612 * another exchange using the group offered by the Responder" 2613 */ 2614 if (group >= own_group) { 2615 l_debug("Offered group %u is the same or more secure, " 2616 " but group negotiation is not supported", group); 2617 return; 2618 } 2619 } 2620 2621 static void dpp_pkex_bad_code(struct dpp_sm *dpp) 2622 { 2623 _auto_(l_ecc_point_free) struct l_ecc_point *qr = NULL; 2624 2625 qr = dpp_derive_qr(dpp->curve, dpp->pkex_key, dpp->pkex_id, 2626 netdev_get_address(dpp->netdev)); 2627 if (!qr || l_ecc_point_is_infinity(qr)) { 2628 l_debug("Qr computed to zero, new code should be provisioned"); 2629 return; 2630 } 2631 2632 l_debug("Qr computed successfully but responder indicated otherwise"); 2633 } 2634 2635 static void dpp_handle_pkex_exchange_response(struct dpp_sm *dpp, 2636 const uint8_t *from, 2637 const uint8_t *body, size_t body_len) 2638 { 2639 struct dpp_attr_iter iter; 2640 enum dpp_attribute_type type; 2641 size_t len; 2642 const uint8_t *data; 2643 const uint8_t *status = NULL; 2644 uint8_t version = 0; 2645 const void *identifier = NULL; 2646 size_t identifier_len = 0; 2647 const void *key = NULL; 2648 size_t key_len = 0; 2649 uint16_t group = 0; 2650 _auto_(l_ecc_point_free) struct l_ecc_point *n = NULL; 2651 _auto_(l_ecc_point_free) struct l_ecc_point *j = NULL; 2652 _auto_(l_ecc_point_free) struct l_ecc_point *qr = NULL; 2653 _auto_(l_ecc_point_free) struct l_ecc_point *k = NULL; 2654 const uint8_t *own_addr = netdev_get_address(dpp->netdev); 2655 2656 l_debug("PKEX response "MAC, MAC_STR(from)); 2657 2658 if (dpp->state != DPP_STATE_PKEX_EXCHANGE) 2659 return; 2660 2661 if (dpp->role != DPP_CAPABILITY_ENROLLEE) 2662 return; 2663 2664 memcpy(dpp->peer_addr, from, 6); 2665 2666 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 2667 2668 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2669 switch (type) { 2670 case DPP_ATTR_STATUS: 2671 if (len != 1) 2672 return; 2673 2674 status = data; 2675 break; 2676 case DPP_ATTR_PROTOCOL_VERSION: 2677 if (len != 1) 2678 return; 2679 2680 version = l_get_u8(data); 2681 break; 2682 case DPP_ATTR_CODE_IDENTIFIER: 2683 identifier = data; 2684 identifier_len = len; 2685 break; 2686 case DPP_ATTR_ENCRYPTED_KEY: 2687 if (len != dpp->key_len * 2) 2688 return; 2689 2690 key = data; 2691 key_len = len; 2692 break; 2693 case DPP_ATTR_FINITE_CYCLIC_GROUP: 2694 if (len != 2) 2695 return; 2696 2697 group = l_get_le16(data); 2698 break; 2699 default: 2700 break; 2701 } 2702 } 2703 2704 if (!status) { 2705 l_debug("No status attribute, ignoring"); 2706 return; 2707 } 2708 2709 if (!key) { 2710 l_debug("No encrypted key, ignoring"); 2711 return; 2712 } 2713 2714 if (*status != DPP_STATUS_OK) 2715 goto handle_status; 2716 2717 if (dpp->pkex_id) { 2718 if (!identifier || identifier_len != strlen(dpp->pkex_id) || 2719 memcmp(dpp->pkex_id, identifier, 2720 identifier_len)) { 2721 l_debug("mismatch identifier, ignoring"); 2722 return; 2723 } 2724 } 2725 2726 if (version && version != dpp->pkex_version) { 2727 l_debug("PKEX version does not match, ignoring"); 2728 return; 2729 } 2730 2731 n = l_ecc_point_from_data(dpp->curve, L_ECC_POINT_TYPE_FULL, 2732 key, key_len); 2733 if (!n) { 2734 l_debug("failed to parse peers encrypted key"); 2735 goto failed; 2736 } 2737 2738 qr = dpp_derive_qr(dpp->curve, dpp->pkex_key, dpp->pkex_id, 2739 dpp->peer_addr); 2740 if (!qr) 2741 goto failed; 2742 2743 dpp->y_or_x = l_ecc_point_new(dpp->curve); 2744 2745 /* Y' = N - Qr */ 2746 l_ecc_point_inverse(qr); 2747 l_ecc_point_add(dpp->y_or_x, n, qr); 2748 2749 /* 2750 * "The resulting ephemeral key, denoted Y’, is then checked whether it 2751 * is the point-at-infinity. If it is not valid, the protocol ends 2752 * unsuccessfully" 2753 */ 2754 if (l_ecc_point_is_infinity(dpp->y_or_x)) { 2755 l_debug("Y' computed to infinity, failing"); 2756 goto failed; 2757 } 2758 2759 k = l_ecc_point_new(dpp->curve); 2760 2761 /* K = Y' * x */ 2762 l_ecc_point_multiply(k, dpp->pkex_private, dpp->y_or_x); 2763 2764 dpp_derive_z(own_addr, dpp->peer_addr, n, dpp->pkex_m, k, 2765 dpp->pkex_key, dpp->pkex_id, 2766 dpp->z, &dpp->z_len); 2767 2768 /* J = a * Y' */ 2769 j = l_ecc_point_new(dpp->curve); 2770 2771 l_ecc_point_multiply(j, dpp->boot_private, dpp->y_or_x); 2772 2773 if (!dpp_derive_u(j, own_addr, dpp->boot_public, dpp->y_or_x, 2774 dpp->pkex_public, dpp->u, &dpp->u_len)) { 2775 l_debug("failed to compute u"); 2776 goto failed; 2777 } 2778 2779 /* 2780 * Now that a response was successfully received, start another 2781 * offchannel with more time for the remainder of the protocol. After 2782 * PKEX, authentication will begin which handles the protocol timeout. 2783 * If the remainder of PKEX (commit-reveal exchange) cannot complete in 2784 * this time it will fail. 2785 */ 2786 dpp->dwell = (dpp->max_roc < 2000) ? dpp->max_roc : 2000; 2787 dpp->state = DPP_STATE_PKEX_COMMIT_REVEAL; 2788 2789 dpp_property_changed_notify(dpp); 2790 2791 dpp_start_offchannel(dpp, dpp->current_freq); 2792 2793 return; 2794 2795 handle_status: 2796 switch (*status) { 2797 case DPP_STATUS_BAD_GROUP: 2798 dpp_pkex_bad_group(dpp, group); 2799 break; 2800 case DPP_STATUS_BAD_CODE: 2801 dpp_pkex_bad_code(dpp); 2802 break; 2803 default: 2804 l_debug("Unhandled status %u", *status); 2805 break; 2806 } 2807 2808 failed: 2809 dpp_reset(dpp); 2810 } 2811 2812 static bool dpp_pkex_start_authentication(struct dpp_sm *dpp) 2813 { 2814 dpp->uri = dpp_generate_uri(dpp->own_asn1, dpp->own_asn1_len, 2, 2815 netdev_get_address(dpp->netdev), 2816 &dpp->current_freq, 1, NULL, NULL); 2817 2818 l_ecdh_generate_key_pair(dpp->curve, &dpp->proto_private, 2819 &dpp->own_proto_public); 2820 2821 l_getrandom(dpp->i_nonce, dpp->nonce_len); 2822 2823 dpp->peer_asn1 = dpp_point_to_asn1(dpp->peer_boot_public, 2824 &dpp->peer_asn1_len); 2825 2826 dpp->m = dpp_derive_k1(dpp->peer_boot_public, dpp->proto_private, 2827 dpp->k1); 2828 2829 dpp_hash(L_CHECKSUM_SHA256, dpp->peer_boot_hash, 1, dpp->peer_asn1, 2830 dpp->peer_asn1_len); 2831 2832 dpp->state = DPP_STATE_AUTHENTICATING; 2833 dpp->mutual_auth = true; 2834 2835 dpp_property_changed_notify(dpp); 2836 2837 /* 2838 * No longer waiting for an arbitrary peer to respond, reduce the 2839 * timeout now that we are proceeding to authentication 2840 */ 2841 dpp_reset_protocol_timer(dpp, DPP_AUTH_PROTO_TIMEOUT); 2842 2843 if (dpp->role == DPP_CAPABILITY_ENROLLEE) { 2844 dpp->new_freq = dpp->current_freq; 2845 2846 return dpp_send_authenticate_request(dpp); 2847 } 2848 2849 return true; 2850 } 2851 2852 static void dpp_handle_pkex_commit_reveal_response(struct dpp_sm *dpp, 2853 const uint8_t *from, 2854 const uint8_t *body, size_t body_len) 2855 { 2856 struct dpp_attr_iter iter; 2857 enum dpp_attribute_type type; 2858 size_t len; 2859 const uint8_t *data; 2860 const uint8_t *wrapped = NULL; 2861 size_t wrapped_len = 0; 2862 uint8_t one = 1; 2863 _auto_(l_free) uint8_t *unwrapped = NULL; 2864 size_t unwrapped_len = 0; 2865 const uint8_t *boot_key = NULL; 2866 size_t boot_key_len = 0; 2867 const uint8_t *r_auth = NULL; 2868 uint8_t v[L_ECC_SCALAR_MAX_BYTES]; 2869 size_t v_len; 2870 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 2871 2872 l_debug("PKEX commit reveal "MAC, MAC_STR(from)); 2873 2874 if (dpp->state != DPP_STATE_PKEX_COMMIT_REVEAL) 2875 return; 2876 2877 if (dpp->role != DPP_CAPABILITY_ENROLLEE) 2878 return; 2879 2880 /* 2881 * The URI may not have contained a MAC address, if this announcement 2882 * verifies set peer_addr then. 2883 */ 2884 if (memcmp(from, dpp->peer_addr, 6)) { 2885 l_debug("Unexpected source "MAC" expected "MAC, MAC_STR(from), 2886 MAC_STR(dpp->peer_addr)); 2887 return; 2888 } 2889 2890 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 2891 2892 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2893 switch (type) { 2894 case DPP_ATTR_WRAPPED_DATA: 2895 wrapped = data; 2896 wrapped_len = len; 2897 break; 2898 default: 2899 break; 2900 } 2901 } 2902 2903 if (!wrapped) { 2904 l_debug("No wrapped data"); 2905 return; 2906 } 2907 2908 unwrapped = dpp_unwrap_attr(body + 2, 6, &one, 1, dpp->z, dpp->z_len, 2909 wrapped, wrapped_len, &unwrapped_len); 2910 if (!unwrapped) { 2911 l_debug("Failed to unwrap Reveal-Commit message"); 2912 return; 2913 } 2914 2915 dpp_attr_iter_init(&iter, unwrapped, unwrapped_len); 2916 2917 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 2918 switch (type) { 2919 case DPP_ATTR_BOOTSTRAPPING_KEY: 2920 if (len != dpp->key_len * 2) 2921 return; 2922 2923 boot_key = data; 2924 boot_key_len = len; 2925 break; 2926 case DPP_ATTR_RESPONDER_AUTH_TAG: 2927 if (len != 32) 2928 return; 2929 2930 r_auth = data; 2931 break; 2932 default: 2933 break; 2934 } 2935 } 2936 2937 dpp->peer_boot_public = l_ecc_point_from_data(dpp->curve, 2938 L_ECC_POINT_TYPE_FULL, 2939 boot_key, boot_key_len); 2940 if (!dpp->peer_boot_public) { 2941 l_debug("Peer public bootstrapping key was invalid"); 2942 goto failed; 2943 } 2944 2945 /* L = b * X' */ 2946 l = l_ecc_point_new(dpp->curve); 2947 2948 l_ecc_point_multiply(l, dpp->pkex_private, dpp->peer_boot_public); 2949 2950 if (!dpp_derive_v(l, dpp->peer_addr, dpp->peer_boot_public, 2951 dpp->pkex_public, dpp->y_or_x, v, &v_len)) { 2952 l_debug("Failed to derive v"); 2953 goto failed; 2954 } 2955 2956 if (memcmp(v, r_auth, v_len)) { 2957 l_debug("Bootstrapping data did not verify"); 2958 goto failed; 2959 } 2960 2961 if (dpp_pkex_start_authentication(dpp)) 2962 return; 2963 2964 failed: 2965 dpp_reset(dpp); 2966 } 2967 2968 static void dpp_send_bad_group(struct dpp_sm *dpp, const uint8_t *addr) 2969 { 2970 uint8_t hdr[32]; 2971 uint8_t attrs[256]; 2972 uint8_t *ptr = attrs; 2973 uint16_t group; 2974 uint8_t status = DPP_STATUS_BAD_GROUP; 2975 struct iovec iov[2]; 2976 const uint8_t *own_mac = netdev_get_address(dpp->netdev); 2977 2978 l_put_le16(l_ecc_curve_get_ike_group(dpp->curve), &group); 2979 2980 iov[0].iov_len = dpp_build_header(own_mac, addr, 2981 DPP_FRAME_PKEX_XCHG_RESPONSE, hdr); 2982 iov[0].iov_base = hdr; 2983 2984 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1); 2985 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, 2986 &dpp->pkex_version, 1); 2987 ptr += dpp_append_attr(ptr, DPP_ATTR_FINITE_CYCLIC_GROUP, &group, 2); 2988 2989 iov[1].iov_base = attrs; 2990 iov[1].iov_len = ptr - attrs; 2991 2992 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 2993 } 2994 2995 static void dpp_send_bad_code(struct dpp_sm *dpp, const uint8_t *addr) 2996 { 2997 uint8_t hdr[32]; 2998 uint8_t attrs[256]; 2999 uint8_t *ptr = attrs; 3000 uint8_t status = DPP_STATUS_BAD_CODE; 3001 struct iovec iov[2]; 3002 const uint8_t *own_mac = netdev_get_address(dpp->netdev); 3003 3004 iov[0].iov_len = dpp_build_header(own_mac, addr, 3005 DPP_FRAME_PKEX_XCHG_RESPONSE, hdr); 3006 iov[0].iov_base = hdr; 3007 3008 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1); 3009 ptr += dpp_append_attr(ptr, DPP_ATTR_PROTOCOL_VERSION, 3010 &dpp->pkex_version, 1); 3011 if (dpp->pkex_id) 3012 ptr += dpp_append_attr(ptr, DPP_ATTR_CODE_IDENTIFIER, 3013 dpp->pkex_id, strlen(dpp->pkex_id)); 3014 3015 iov[1].iov_base = attrs; 3016 iov[1].iov_len = ptr - attrs; 3017 3018 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 3019 } 3020 3021 static void dpp_send_pkex_exchange_response(struct dpp_sm *dpp, 3022 struct l_ecc_point *n) 3023 { 3024 uint8_t hdr[32]; 3025 uint8_t attrs[256]; 3026 uint8_t *ptr = attrs; 3027 uint64_t n_data[L_ECC_MAX_DIGITS * 2]; 3028 uint16_t group; 3029 uint8_t status = DPP_STATUS_OK; 3030 struct iovec iov[2]; 3031 const uint8_t *own_mac = netdev_get_address(dpp->netdev); 3032 3033 l_put_le16(l_ecc_curve_get_ike_group(dpp->curve), &group); 3034 3035 iov[0].iov_len = dpp_build_header(own_mac, dpp->peer_addr, 3036 DPP_FRAME_PKEX_XCHG_RESPONSE, hdr); 3037 iov[0].iov_base = hdr; 3038 3039 ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1); 3040 3041 if (dpp->pkex_id) 3042 ptr += dpp_append_attr(ptr, DPP_ATTR_CODE_IDENTIFIER, 3043 dpp->pkex_id, strlen(dpp->pkex_id)); 3044 3045 l_ecc_point_get_data(n, n_data, sizeof(n_data)); 3046 3047 ptr += dpp_append_attr(ptr, DPP_ATTR_ENCRYPTED_KEY, 3048 n_data, dpp->key_len * 2); 3049 3050 iov[1].iov_base = attrs; 3051 iov[1].iov_len = ptr - attrs; 3052 3053 dpp->state = DPP_STATE_PKEX_COMMIT_REVEAL; 3054 3055 dpp_property_changed_notify(dpp); 3056 3057 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 3058 } 3059 3060 static void dpp_process_pkex_exchange_request(struct dpp_sm *dpp, 3061 struct l_ecc_point *m) 3062 { 3063 _auto_(l_ecc_point_free) struct l_ecc_point *n = NULL; 3064 _auto_(l_ecc_point_free) struct l_ecc_point *qr = NULL; 3065 _auto_(l_ecc_point_free) struct l_ecc_point *qi = NULL; 3066 _auto_(l_ecc_point_free) struct l_ecc_point *k = NULL; 3067 const uint8_t *own_addr = netdev_get_address(dpp->netdev); 3068 3069 /* Qi = H(MAC-Initiator | [identifier | ] code) * Pi */ 3070 qi = dpp_derive_qi(dpp->curve, dpp->pkex_key, dpp->pkex_id, 3071 dpp->peer_addr); 3072 if (!qi) { 3073 l_debug("could not derive Qi"); 3074 return; 3075 } 3076 3077 /* X' = M - Qi */ 3078 dpp->y_or_x = l_ecc_point_new(dpp->curve); 3079 3080 l_ecc_point_inverse(qi); 3081 l_ecc_point_add(dpp->y_or_x, m, qi); 3082 3083 /* 3084 * "The resulting ephemeral key, denoted X’, is checked whether it is 3085 * the point-at-infinity. If it is not valid, the protocol silently 3086 * fails" 3087 */ 3088 if (l_ecc_point_is_infinity(dpp->y_or_x)) { 3089 l_debug("X' is at infinity, ignore message"); 3090 dpp_reset(dpp); 3091 return; 3092 } 3093 3094 qr = dpp_derive_qr(dpp->curve, dpp->pkex_key, dpp->pkex_id, own_addr); 3095 if (!qr || l_ecc_point_is_infinity(qr)) { 3096 l_debug("Qr did not derive"); 3097 l_ecc_point_free(dpp->y_or_x); 3098 dpp->y_or_x = NULL; 3099 goto bad_code; 3100 } 3101 3102 /* 3103 * "The Responder then generates a random ephemeral keypair, y/Y, 3104 * encrypts Y with Qr to obtain the result, denoted N." 3105 */ 3106 l_ecdh_generate_key_pair(dpp->curve, &dpp->pkex_private, 3107 &dpp->pkex_public); 3108 3109 /* N = Y + Qr */ 3110 n = l_ecc_point_new(dpp->curve); 3111 3112 l_ecc_point_add(n, dpp->pkex_public, qr); 3113 3114 /* K = y * X' */ 3115 3116 k = l_ecc_point_new(dpp->curve); 3117 3118 l_ecc_point_multiply(k, dpp->pkex_private, dpp->y_or_x); 3119 3120 /* z = HKDF(<>, info | M.x | N.x | code, K.x) */ 3121 dpp_derive_z(dpp->peer_addr, own_addr, n, m, k, dpp->pkex_key, 3122 dpp->pkex_id, dpp->z, &dpp->z_len); 3123 3124 dpp_send_pkex_exchange_response(dpp, n); 3125 3126 return; 3127 3128 bad_code: 3129 dpp_send_bad_code(dpp, dpp->peer_addr); 3130 return; 3131 } 3132 3133 static void dpp_pkex_agent_reply(struct l_dbus_message *message, 3134 void *user_data) 3135 { 3136 struct dpp_sm *dpp = user_data; 3137 const char *error, *text; 3138 const char *code; 3139 3140 dpp->agent->pending_id = 0; 3141 3142 l_debug("SharedCodeAgent %s path %s replied", dpp->agent->owner, 3143 dpp->agent->path); 3144 3145 if (l_dbus_message_get_error(message, &error, &text)) { 3146 l_error("RequestSharedCode(%s) returned %s(\"%s\")", 3147 dpp->pkex_id, error, text); 3148 goto reset; 3149 } 3150 3151 if (!l_dbus_message_get_arguments(message, "s", &code)) { 3152 l_debug("Invalid arguments, check SharedCodeAgent!"); 3153 goto reset; 3154 } 3155 3156 dpp->pkex_key = l_strdup(code); 3157 dpp_process_pkex_exchange_request(dpp, dpp->peer_encr_key); 3158 3159 return; 3160 3161 reset: 3162 dpp_free_pending_pkex_data(dpp); 3163 } 3164 3165 static bool dpp_pkex_agent_request(struct dpp_sm *dpp) 3166 { 3167 struct l_dbus_message *msg; 3168 3169 if (!dpp->agent) 3170 return false; 3171 3172 if (L_WARN_ON(dpp->agent->pending_id)) 3173 return false; 3174 3175 msg = l_dbus_message_new_method_call(dbus_get_bus(), 3176 dpp->agent->owner, 3177 dpp->agent->path, 3178 IWD_SHARED_CODE_AGENT_INTERFACE, 3179 "RequestSharedCode"); 3180 l_dbus_message_set_arguments(msg, "s", dpp->pkex_id); 3181 3182 3183 dpp->agent->pending_id = l_dbus_send_with_reply(dbus_get_bus(), 3184 msg, 3185 dpp_pkex_agent_reply, 3186 dpp, NULL); 3187 return dpp->agent->pending_id != 0; 3188 } 3189 3190 static void dpp_handle_pkex_exchange_request(struct dpp_sm *dpp, 3191 const uint8_t *from, 3192 const uint8_t *body, size_t body_len) 3193 { 3194 struct dpp_attr_iter iter; 3195 enum dpp_attribute_type type; 3196 size_t len; 3197 const uint8_t *data; 3198 uint8_t version = 0; 3199 uint16_t group = 0; 3200 const void *id = NULL; 3201 size_t id_len = 0; 3202 const void *key = NULL; 3203 size_t key_len = 0; 3204 _auto_(l_ecc_point_free) struct l_ecc_point *m = NULL; 3205 3206 l_debug("PKEX exchange request "MAC, MAC_STR(from)); 3207 3208 if (dpp->state != DPP_STATE_PKEX_EXCHANGE) 3209 return; 3210 3211 if (dpp->role != DPP_CAPABILITY_CONFIGURATOR) 3212 return; 3213 3214 if (!l_memeqzero(dpp->peer_addr, 6)) { 3215 l_debug("Already configuring enrollee, ignoring"); 3216 return; 3217 } 3218 3219 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 3220 3221 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 3222 switch (type) { 3223 case DPP_ATTR_PROTOCOL_VERSION: 3224 if (len != 1) 3225 return; 3226 3227 version = l_get_u8(data); 3228 break; 3229 case DPP_ATTR_FINITE_CYCLIC_GROUP: 3230 if (len != 2) 3231 return; 3232 3233 group = l_get_le16(data); 3234 break; 3235 case DPP_ATTR_CODE_IDENTIFIER: 3236 id = data; 3237 id_len = len; 3238 break; 3239 case DPP_ATTR_ENCRYPTED_KEY: 3240 key = data; 3241 key_len = len; 3242 break; 3243 default: 3244 break; 3245 } 3246 } 3247 3248 if (!key || !group) { 3249 l_debug("initiator did not provide group or key, ignoring"); 3250 return; 3251 } 3252 3253 if (group != l_ecc_curve_get_ike_group(dpp->curve)) { 3254 l_debug("initiator is not using the same group"); 3255 goto bad_group; 3256 } 3257 3258 /* 3259 * If the group isn't the same the key length won't match, so check 3260 * this here after we've determined the groups are equal 3261 */ 3262 if (key_len != dpp->key_len * 2) { 3263 l_debug("Unexpected encrypted key length"); 3264 return; 3265 } 3266 3267 if (version && version != dpp->pkex_version) { 3268 l_debug("initiator is not using the same version, ignoring"); 3269 return; 3270 } 3271 3272 if (dpp->pkex_id) { 3273 if (!id || id_len != strlen(dpp->pkex_id) || 3274 memcmp(dpp->pkex_id, id, id_len)) { 3275 l_debug("mismatch identifier, ignoring"); 3276 return; 3277 } 3278 } 3279 3280 m = l_ecc_point_from_data(dpp->curve, L_ECC_POINT_TYPE_FULL, 3281 key, key_len); 3282 if (!m) { 3283 l_debug("could not parse key from initiator, ignoring"); 3284 return; 3285 } 3286 3287 memcpy(dpp->peer_addr, from, 6); 3288 3289 if (!dpp->pkex_key) { 3290 /* 3291 * "If an optional code identifier is used, it shall be a UTF-8 3292 * string not greater than eighty (80) octets" 3293 */ 3294 if (!id || id_len > 80 || !l_utf8_validate(id, id_len, NULL)) { 3295 l_debug("Configurator started with agent but enrollee " 3296 "sent invalid or no identifier, ignoring"); 3297 return; 3298 } 3299 3300 dpp->pkex_id = l_strndup(id, id_len); 3301 3302 /* Need to obtain code from agent */ 3303 if (!dpp_pkex_agent_request(dpp)) { 3304 l_debug("Failed to request code from agent!"); 3305 dpp_free_pending_pkex_data(dpp); 3306 return; 3307 } 3308 3309 /* Save the encrypted key/identifier for the agent callback */ 3310 3311 dpp->peer_encr_key = l_steal_ptr(m); 3312 3313 return; 3314 } 3315 3316 dpp_process_pkex_exchange_request(dpp, m); 3317 3318 return; 3319 3320 bad_group: 3321 dpp_send_bad_group(dpp, from); 3322 } 3323 3324 static void dpp_send_commit_reveal_response(struct dpp_sm *dpp, 3325 const uint8_t *v, size_t v_len) 3326 { 3327 uint8_t hdr[32]; 3328 uint8_t attrs[256]; 3329 uint8_t *ptr = attrs; 3330 uint8_t one = 1; 3331 struct iovec iov[2]; 3332 const uint8_t *own_mac = netdev_get_address(dpp->netdev); 3333 uint8_t b_pub[L_ECC_POINT_MAX_BYTES]; 3334 size_t b_len; 3335 3336 b_len = l_ecc_point_get_data(dpp->boot_public, b_pub, sizeof(b_pub)); 3337 3338 3339 iov[0].iov_len = dpp_build_header(own_mac, dpp->peer_addr, 3340 DPP_FRAME_PKEX_COMMIT_REVEAL_RESPONSE, hdr); 3341 iov[0].iov_base = hdr; 3342 3343 ptr += dpp_append_wrapped_data(hdr + 26, 6, &one, 1, ptr, 3344 sizeof(attrs), dpp->z, dpp->z_len, 2, 3345 DPP_ATTR_BOOTSTRAPPING_KEY, b_len, b_pub, 3346 DPP_ATTR_RESPONDER_AUTH_TAG, v_len, v); 3347 3348 iov[1].iov_base = attrs; 3349 iov[1].iov_len = ptr - attrs; 3350 3351 dpp_send_frame(dpp, iov, 2, dpp->current_freq); 3352 } 3353 3354 static void dpp_handle_pkex_commit_reveal_request(struct dpp_sm *dpp, 3355 const uint8_t *from, 3356 const uint8_t *body, size_t body_len) 3357 { 3358 struct dpp_attr_iter iter; 3359 enum dpp_attribute_type type; 3360 size_t len; 3361 const uint8_t *data; 3362 const void *wrapped = NULL; 3363 size_t wrapped_len = 0; 3364 _auto_(l_free) uint8_t *unwrapped = NULL; 3365 size_t unwrapped_len; 3366 uint8_t zero = 0; 3367 const void *key = 0; 3368 size_t key_len = 0; 3369 const void *i_auth = NULL; 3370 size_t i_auth_len = 0; 3371 _auto_(l_ecc_point_free) struct l_ecc_point *j = NULL; 3372 _auto_(l_ecc_point_free) struct l_ecc_point *l = NULL; 3373 uint8_t u[L_ECC_SCALAR_MAX_BYTES]; 3374 size_t u_len = 0; 3375 uint8_t v[L_ECC_SCALAR_MAX_BYTES]; 3376 size_t v_len = 0; 3377 const uint8_t *own_addr = netdev_get_address(dpp->netdev); 3378 3379 l_debug("PKEX commit-reveal request "MAC, MAC_STR(from)); 3380 3381 if (dpp->state != DPP_STATE_PKEX_COMMIT_REVEAL) 3382 return; 3383 3384 if (dpp->role != DPP_CAPABILITY_CONFIGURATOR) 3385 return; 3386 3387 dpp_attr_iter_init(&iter, body + 8, body_len - 8); 3388 3389 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 3390 switch (type) { 3391 case DPP_ATTR_WRAPPED_DATA: 3392 wrapped = data; 3393 wrapped_len = len; 3394 break; 3395 default: 3396 break; 3397 } 3398 } 3399 3400 if (!wrapped) { 3401 l_debug("No wrapped data"); 3402 return; 3403 } 3404 3405 unwrapped = dpp_unwrap_attr(body + 2, 6, &zero, 1, dpp->z, dpp->z_len, 3406 wrapped, wrapped_len, &unwrapped_len); 3407 if (!unwrapped) { 3408 l_debug("Failed to unwrap attributes"); 3409 return; 3410 } 3411 3412 dpp_attr_iter_init(&iter, unwrapped, unwrapped_len); 3413 3414 while (dpp_attr_iter_next(&iter, &type, &len, &data)) { 3415 switch (type) { 3416 case DPP_ATTR_BOOTSTRAPPING_KEY: 3417 if (len != dpp->key_len * 2) 3418 return; 3419 3420 key = data; 3421 key_len = len; 3422 break; 3423 case DPP_ATTR_INITIATOR_AUTH_TAG: 3424 if (len != 32) 3425 return; 3426 3427 i_auth = data; 3428 i_auth_len = len; 3429 break; 3430 default: 3431 break; 3432 } 3433 } 3434 3435 if (!key || !i_auth) { 3436 l_debug("missing attributes"); 3437 return; 3438 } 3439 3440 dpp->peer_boot_public = l_ecc_point_from_data(dpp->curve, 3441 L_ECC_POINT_TYPE_FULL, key, key_len); 3442 if (!dpp->peer_boot_public) { 3443 l_debug("peers bootstrapping key did not validate"); 3444 goto failed; 3445 } 3446 3447 /* J' = y * A' */ 3448 j = l_ecc_point_new(dpp->curve); 3449 3450 l_ecc_point_multiply(j, dpp->pkex_private, dpp->peer_boot_public); 3451 3452 dpp_derive_u(j, dpp->peer_addr, dpp->peer_boot_public, dpp->pkex_public, 3453 dpp->y_or_x, u, &u_len); 3454 3455 if (memcmp(u, i_auth, i_auth_len)) { 3456 l_debug("Initiator auth tag did not verify"); 3457 goto failed; 3458 } 3459 3460 /* L' = x * B' */ 3461 l = l_ecc_point_new(dpp->curve); 3462 3463 l_ecc_point_multiply(l, dpp->boot_private, dpp->y_or_x); 3464 3465 if (!dpp_derive_v(l, own_addr, dpp->boot_public, dpp->y_or_x, 3466 dpp->pkex_public, v, &v_len)) { 3467 l_debug("Failed to derive v"); 3468 goto failed; 3469 } 3470 3471 dpp_send_commit_reveal_response(dpp, v, v_len); 3472 3473 dpp_pkex_start_authentication(dpp); 3474 3475 return; 3476 3477 failed: 3478 dpp_reset(dpp); 3479 } 3480 3481 static void dpp_handle_frame(const struct mmpdu_header *frame, 3482 const void *body, size_t body_len, 3483 int rssi, void *user_data) 3484 { 3485 struct dpp_sm *dpp = user_data; 3486 const uint8_t *ptr; 3487 3488 /* 3489 * Both handlers offset by 8 bytes to reach the beginning of the DPP 3490 * attributes. Easier checking this in one place, which also covers the 3491 * frame type byte. 3492 */ 3493 if (body_len < 8) 3494 return; 3495 3496 ptr = body + 7; 3497 3498 switch (*ptr) { 3499 case DPP_FRAME_AUTHENTICATION_REQUEST: 3500 authenticate_request(dpp, frame->address_2, body, body_len); 3501 break; 3502 case DPP_FRAME_AUTHENTICATION_RESPONSE: 3503 authenticate_response(dpp, frame->address_2, body, body_len); 3504 break; 3505 case DPP_FRAME_AUTHENTICATION_CONFIRM: 3506 authenticate_confirm(dpp, frame->address_2, body, body_len); 3507 break; 3508 case DPP_FRAME_CONFIGURATION_RESULT: 3509 dpp_handle_config_result_frame(dpp, frame->address_2, 3510 body, body_len); 3511 break; 3512 case DPP_FRAME_PRESENCE_ANNOUNCEMENT: 3513 dpp_handle_presence_announcement(dpp, frame->address_2, 3514 body, body_len); 3515 break; 3516 case DPP_FRAME_PKEX_XCHG_RESPONSE: 3517 dpp_handle_pkex_exchange_response(dpp, frame->address_2, body, 3518 body_len); 3519 break; 3520 case DPP_FRAME_PKEX_COMMIT_REVEAL_RESPONSE: 3521 dpp_handle_pkex_commit_reveal_response(dpp, frame->address_2, 3522 body, body_len); 3523 break; 3524 case DPP_FRAME_PKEX_VERSION1_XCHG_REQUEST: 3525 dpp_handle_pkex_exchange_request(dpp, frame->address_2, body, 3526 body_len); 3527 break; 3528 case DPP_FRAME_PKEX_COMMIT_REVEAL_REQUEST: 3529 dpp_handle_pkex_commit_reveal_request(dpp, frame->address_2, 3530 body, body_len); 3531 break; 3532 default: 3533 l_debug("Unhandled DPP frame %u", *ptr); 3534 break; 3535 } 3536 } 3537 3538 static bool match_wdev(const void *a, const void *b) 3539 { 3540 const struct dpp_sm *dpp = a; 3541 const uint64_t *wdev_id = b; 3542 3543 return *wdev_id == dpp->wdev_id; 3544 } 3545 3546 static void dpp_frame_timeout(struct l_timeout *timeout, void *user_data) 3547 { 3548 struct dpp_sm *dpp = user_data; 3549 3550 l_timeout_remove(timeout); 3551 dpp->retry_timeout = NULL; 3552 3553 /* 3554 * ROC has not yet started (in between an ROC timeout and starting a 3555 * new session), this will most likely result in the frame failing to 3556 * send. Just bail out now and the roc_started callback will take care 3557 * of sending this out. 3558 */ 3559 if (dpp->offchannel_id && !dpp->roc_started) 3560 return; 3561 3562 dpp_frame_retry(dpp); 3563 } 3564 3565 static void dpp_mlme_notify(struct l_genl_msg *msg, void *user_data) 3566 { 3567 struct dpp_sm *dpp; 3568 uint64_t wdev_id = 0; 3569 uint64_t cookie = 0; 3570 bool ack = false; 3571 struct iovec iov; 3572 uint8_t cmd = l_genl_msg_get_command(msg); 3573 3574 if (cmd != NL80211_CMD_FRAME_TX_STATUS) 3575 return; 3576 3577 if (nl80211_parse_attrs(msg, NL80211_ATTR_WDEV, &wdev_id, 3578 NL80211_ATTR_COOKIE, &cookie, 3579 NL80211_ATTR_ACK, &ack, 3580 NL80211_ATTR_FRAME, &iov, 3581 NL80211_ATTR_UNSPEC) < 0) 3582 return; 3583 3584 dpp = l_queue_find(dpp_list, match_wdev, &wdev_id); 3585 if (!dpp) 3586 return; 3587 3588 /* 3589 * Don't retransmit for presence or PKEX exchange if an enrollee, both 3590 * are broadcast frames which don't expect an ack. 3591 */ 3592 if (dpp->state == DPP_STATE_NOTHING || 3593 dpp->state == DPP_STATE_PRESENCE || 3594 (dpp->state == DPP_STATE_PKEX_EXCHANGE && 3595 dpp->role == DPP_CAPABILITY_ENROLLEE)) 3596 return; 3597 3598 if (dpp->frame_cookie != cookie) 3599 return; 3600 3601 /* 3602 * Only want to handle the no-ACK case. Re-transmitting an ACKed 3603 * frame likely won't do any good, at least in the case of DPP. 3604 */ 3605 if (!ack) 3606 goto retransmit; 3607 3608 /* 3609 * Special handling for a channel transition when acting as a 3610 * configurator. The auth request was sent offchannel so we need to 3611 * wait for the ACK before going back to the connected channel. 3612 */ 3613 if (dpp->channel_switch) { 3614 if (dpp->offchannel_id) { 3615 offchannel_cancel(dpp->wdev_id, dpp->offchannel_id); 3616 dpp->offchannel_id = 0; 3617 } 3618 3619 dpp->channel_switch = false; 3620 } 3621 3622 return; 3623 3624 retransmit: 3625 if (dpp->frame_retry > DPP_FRAME_MAX_RETRIES) { 3626 dpp_reset(dpp); 3627 return; 3628 } 3629 3630 /* This should never happen */ 3631 if (L_WARN_ON(dpp->frame_pending)) 3632 return; 3633 3634 l_debug("No ACK from peer, re-transmitting in %us", 3635 DPP_FRAME_RETRY_TIMEOUT); 3636 3637 dpp->frame_retry++; 3638 3639 dpp->frame_pending = l_memdup(iov.iov_base, iov.iov_len); 3640 dpp->frame_size = iov.iov_len; 3641 dpp->retry_timeout = l_timeout_create(DPP_FRAME_RETRY_TIMEOUT, 3642 dpp_frame_timeout, dpp, NULL); 3643 } 3644 3645 static void dpp_start_enrollee(struct dpp_sm *dpp); 3646 static bool dpp_start_pkex_enrollee(struct dpp_sm *dpp); 3647 3648 /* 3649 * Station is unaware of DPP's state so we need to handle a few cases here so 3650 * weird stuff doesn't happen: 3651 * 3652 * - While configuring we should stay connected, a disconnection/roam should 3653 * stop DPP since it would fail regardless due to the hardware going idle 3654 * or changing channels since configurators assume all comms will be 3655 * on-channel. 3656 * - While enrolling we should stay disconnected. If station connects during 3657 * enrolling it would cause 2x calls to __station_connect_network after 3658 * DPP finishes. 3659 * 3660 * Other conditions shouldn't ever happen i.e. configuring and going into a 3661 * connecting state or enrolling and going to a roaming state. 3662 */ 3663 static void dpp_station_state_watch(enum station_state state, void *user_data) 3664 { 3665 struct dpp_sm *dpp = user_data; 3666 3667 if (dpp->state == DPP_STATE_NOTHING || 3668 dpp->interface == DPP_INTERFACE_UNBOUND) 3669 return; 3670 3671 switch (state) { 3672 case STATION_STATE_DISCONNECTED: 3673 /* DPP-initiated disconnect, the enrollee can now start */ 3674 if (dpp->role == DPP_CAPABILITY_ENROLLEE) { 3675 l_debug("Starting enrollee after disconnect"); 3676 3677 if (dpp->interface == DPP_INTERFACE_DPP) 3678 dpp_start_enrollee(dpp); 3679 else 3680 dpp_start_pkex_enrollee(dpp); 3681 3682 return; 3683 } 3684 3685 break; 3686 case STATION_STATE_DISCONNECTING: 3687 /* Normal part of disconnecting prior to enrolling */ 3688 if (dpp->role == DPP_CAPABILITY_ENROLLEE) 3689 return; 3690 3691 /* fall through */ 3692 case STATION_STATE_ROAMING: 3693 case STATION_STATE_FT_ROAMING: 3694 case STATION_STATE_FW_ROAMING: 3695 /* 3696 * An enrollee will always be disconnected prior to starting 3697 * so a roaming condition should never happen. 3698 */ 3699 if (L_WARN_ON(dpp->role == DPP_CAPABILITY_ENROLLEE)) 3700 goto reset; 3701 3702 if (dpp->role == DPP_CAPABILITY_CONFIGURATOR) { 3703 l_debug("Disconnected while configuring, stopping DPP"); 3704 goto reset; 3705 } 3706 3707 break; 3708 case STATION_STATE_CONNECTING: 3709 case STATION_STATE_CONNECTED: 3710 case STATION_STATE_CONNECTING_AUTO: 3711 case STATION_STATE_NETCONFIG: 3712 case STATION_STATE_AUTOCONNECT_FULL: 3713 case STATION_STATE_AUTOCONNECT_QUICK: 3714 /* 3715 * The user could have issued a connection request during 3716 * enrolling, in which case DPP should be canceled. We should 3717 * never hit this case as a configurator as a disconnect would 3718 * need to come prior. 3719 */ 3720 L_WARN_ON(dpp->role == DPP_CAPABILITY_CONFIGURATOR); 3721 3722 goto reset; 3723 } 3724 3725 reset: 3726 l_debug("Resetting DPP after station state change (state=%u)", state); 3727 dpp_reset(dpp); 3728 } 3729 3730 static void dpp_create(struct netdev *netdev) 3731 { 3732 struct l_dbus *dbus = dbus_get_bus(); 3733 struct dpp_sm *dpp = l_new(struct dpp_sm, 1); 3734 uint64_t wdev_id = netdev_get_wdev_id(netdev); 3735 3736 dpp->netdev = netdev; 3737 dpp->state = DPP_STATE_NOTHING; 3738 dpp->interface = DPP_INTERFACE_UNBOUND; 3739 dpp->wdev_id = wdev_id; 3740 dpp->curve = l_ecc_curve_from_ike_group(19); 3741 dpp->key_len = l_ecc_curve_get_scalar_bytes(dpp->curve); 3742 dpp->nonce_len = dpp_nonce_len_from_key_len(dpp->key_len); 3743 dpp->max_roc = wiphy_get_max_roc_duration(wiphy_find_by_wdev(wdev_id)); 3744 dpp->mcast_support = wiphy_supports_multicast_rx( 3745 wiphy_find_by_wdev(dpp->wdev_id)); 3746 3747 l_ecdh_generate_key_pair(dpp->curve, &dpp->boot_private, 3748 &dpp->boot_public); 3749 3750 dpp->own_asn1 = dpp_point_to_asn1(dpp->boot_public, &dpp->own_asn1_len); 3751 3752 dpp_hash(L_CHECKSUM_SHA256, dpp->own_boot_hash, 1, 3753 dpp->own_asn1, dpp->own_asn1_len); 3754 3755 l_dbus_object_add_interface(dbus, netdev_get_path(netdev), 3756 IWD_DPP_INTERFACE, dpp); 3757 l_dbus_object_add_interface(dbus, netdev_get_path(netdev), 3758 IWD_DPP_PKEX_INTERFACE, dpp); 3759 /* 3760 * Since both interfaces share the dpp_sm set this to 2. Currently both 3761 * interfaces are added/removed in unison so we _could_ simply omit the 3762 * destroy callback on one of them. But for consistency and future 3763 * proofing use a reference count and the final interface being removed 3764 * will destroy the dpp_sm. 3765 */ 3766 dpp->refcount = 2; 3767 3768 dpp->known_network_watch = known_networks_watch_add( 3769 dpp_known_network_watch, dpp, NULL); 3770 3771 l_queue_push_tail(dpp_list, dpp); 3772 } 3773 3774 static void dpp_netdev_watch(struct netdev *netdev, 3775 enum netdev_watch_event event, void *userdata) 3776 { 3777 switch (event) { 3778 case NETDEV_WATCH_EVENT_NEW: 3779 case NETDEV_WATCH_EVENT_UP: 3780 if (netdev_get_iftype(netdev) == NETDEV_IFTYPE_STATION && 3781 netdev_get_is_up(netdev)) 3782 dpp_create(netdev); 3783 break; 3784 case NETDEV_WATCH_EVENT_DEL: 3785 case NETDEV_WATCH_EVENT_DOWN: 3786 l_dbus_object_remove_interface(dbus_get_bus(), 3787 netdev_get_path(netdev), 3788 IWD_DPP_INTERFACE); 3789 l_dbus_object_remove_interface(dbus_get_bus(), 3790 netdev_get_path(netdev), 3791 IWD_DPP_PKEX_INTERFACE); 3792 break; 3793 default: 3794 break; 3795 } 3796 } 3797 3798 /* 3799 * EasyConnect 2.0 - 6.2.2 3800 */ 3801 static uint32_t *dpp_add_default_channels(struct dpp_sm *dpp, size_t *len_out) 3802 { 3803 struct wiphy *wiphy = wiphy_find_by_wdev( 3804 netdev_get_wdev_id(dpp->netdev)); 3805 const struct scan_freq_set *list = wiphy_get_supported_freqs(wiphy); 3806 uint32_t freq; 3807 3808 if (!dpp->presence_list) 3809 dpp->presence_list = scan_freq_set_new(); 3810 3811 scan_freq_set_add(dpp->presence_list, band_channel_to_freq(6, 3812 BAND_FREQ_2_4_GHZ)); 3813 /* 3814 * "5 GHz: Channel 44 (5.220 GHz) if local regulations permit operation 3815 * only in the 5.150 - 5.250 GHz band and Channel 149 (5.745 GHz) 3816 * otherwise" 3817 */ 3818 freq = band_channel_to_freq(149, BAND_FREQ_5_GHZ); 3819 3820 if (scan_freq_set_contains(list, freq)) 3821 scan_freq_set_add(dpp->presence_list, freq); 3822 else 3823 scan_freq_set_add(dpp->presence_list, 3824 band_channel_to_freq(44, BAND_FREQ_5_GHZ)); 3825 3826 /* TODO: 60GHz: Channel 2 */ 3827 3828 return scan_freq_set_to_fixed_array(dpp->presence_list, len_out); 3829 } 3830 3831 /* 3832 * TODO: There is an entire procedure defined in the spec where you increase 3833 * the ROC timeout with each unsuccessful iteration of channels, wait on channel 3834 * for long periods of time etc. Due to offchannel issues in the kernel this 3835 * procedure is not being fully implemented. In reality doing this would result 3836 * in quite terrible DPP performance anyways. 3837 */ 3838 static void dpp_start_presence(struct dpp_sm *dpp, uint32_t *limit_freqs, 3839 size_t limit_len) 3840 { 3841 if (limit_freqs) { 3842 dpp->freqs = l_memdup(limit_freqs, sizeof(uint32_t) * limit_len); 3843 dpp->freqs_len = limit_len; 3844 } else 3845 dpp->freqs = dpp_add_default_channels(dpp, &dpp->freqs_len); 3846 3847 dpp->dwell = (dpp->max_roc < 2000) ? dpp->max_roc : 2000; 3848 dpp->freqs_idx = 0; 3849 dpp->current_freq = dpp->freqs[0]; 3850 3851 dpp_start_offchannel(dpp, dpp->current_freq); 3852 } 3853 3854 static int dpp_try_disconnect_station(struct dpp_sm *dpp, 3855 bool *out_disconnect_started) 3856 { 3857 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 3858 int ret; 3859 bool started = false; 3860 3861 /* Unusual case, but an enrollee could still be started */ 3862 if (!station) 3863 goto done; 3864 3865 dpp->autoconnect = station_get_autoconnect(station); 3866 station_set_autoconnect(station, false); 3867 3868 switch (station_get_state(station)) { 3869 case STATION_STATE_AUTOCONNECT_QUICK: 3870 case STATION_STATE_AUTOCONNECT_FULL: 3871 /* Should never happen since we just set autoconnect false */ 3872 l_warn("Still in autoconnect state after setting false!"); 3873 3874 /* fall through */ 3875 case STATION_STATE_DISCONNECTED: 3876 break; 3877 case STATION_STATE_ROAMING: 3878 case STATION_STATE_FT_ROAMING: 3879 case STATION_STATE_FW_ROAMING: 3880 case STATION_STATE_CONNECTING: 3881 case STATION_STATE_CONNECTING_AUTO: 3882 case STATION_STATE_CONNECTED: 3883 case STATION_STATE_NETCONFIG: 3884 /* 3885 * For any connected or connection in progress state, start a 3886 * disconnect 3887 */ 3888 ret = station_disconnect(station); 3889 if (ret < 0) { 3890 l_warn("failed to start disconnecting (%d)", ret); 3891 station_set_autoconnect(station, dpp->autoconnect); 3892 3893 return ret; 3894 } 3895 /* fall through */ 3896 case STATION_STATE_DISCONNECTING: 3897 l_debug("Delaying DPP start until after disconnect"); 3898 started = true; 3899 3900 break; 3901 } 3902 3903 dpp->station_watch = station_add_state_watch(station, 3904 dpp_station_state_watch, 3905 dpp, NULL); 3906 done: 3907 *out_disconnect_started = started; 3908 3909 return 0; 3910 } 3911 3912 static void dpp_add_frame_watches(struct dpp_sm *dpp, bool multicast_rx) 3913 { 3914 uint8_t dpp_conf_response_prefix[] = { 0x04, 0x0b }; 3915 uint8_t dpp_conf_request_prefix[] = { 0x04, 0x0a }; 3916 3917 frame_watch_add(dpp->wdev_id, FRAME_GROUP_DPP, 0x00d0, 3918 dpp_prefix, sizeof(dpp_prefix), multicast_rx, 3919 dpp_handle_frame, dpp, NULL); 3920 3921 frame_watch_add(dpp->wdev_id, FRAME_GROUP_DPP, 0x00d0, 3922 dpp_conf_response_prefix, 3923 sizeof(dpp_conf_response_prefix), false, 3924 dpp_handle_config_response_frame, dpp, NULL); 3925 3926 frame_watch_add(dpp->wdev_id, FRAME_GROUP_DPP, 0x00d0, 3927 dpp_conf_request_prefix, 3928 sizeof(dpp_conf_request_prefix), false, 3929 dpp_handle_config_request_frame, dpp, NULL); 3930 } 3931 3932 static void dpp_start_enrollee(struct dpp_sm *dpp) 3933 { 3934 uint32_t freq = band_channel_to_freq(6, BAND_FREQ_2_4_GHZ); 3935 3936 dpp->uri = dpp_generate_uri(dpp->own_asn1, dpp->own_asn1_len, 2, 3937 netdev_get_address(dpp->netdev), &freq, 3938 1, NULL, NULL); 3939 3940 l_ecdh_generate_key_pair(dpp->curve, &dpp->proto_private, 3941 &dpp->own_proto_public); 3942 3943 l_debug("DPP Start Enrollee: %s", dpp->uri); 3944 3945 /* 3946 * Going off spec here. Select a single channel to send presence 3947 * announcements on. This will be advertised in the URI. The full 3948 * presence procedure can be implemented if it is ever needed. 3949 */ 3950 dpp_start_presence(dpp, &freq, 1); 3951 3952 dpp_property_changed_notify(dpp); 3953 } 3954 3955 static struct l_dbus_message *dpp_dbus_start_enrollee(struct l_dbus *dbus, 3956 struct l_dbus_message *message, 3957 void *user_data) 3958 { 3959 struct dpp_sm *dpp = user_data; 3960 bool wait_for_disconnect; 3961 int ret; 3962 3963 if (dpp->state != DPP_STATE_NOTHING || 3964 dpp->interface != DPP_INTERFACE_UNBOUND) 3965 return dbus_error_busy(message); 3966 3967 dpp->state = DPP_STATE_PRESENCE; 3968 dpp->role = DPP_CAPABILITY_ENROLLEE; 3969 dpp->interface = DPP_INTERFACE_DPP; 3970 3971 dpp_add_frame_watches(dpp, false); 3972 3973 ret = dpp_try_disconnect_station(dpp, &wait_for_disconnect); 3974 if (ret < 0) { 3975 dpp_reset(dpp); 3976 return dbus_error_from_errno(ret, message); 3977 } 3978 3979 if (!wait_for_disconnect) 3980 dpp_start_enrollee(dpp); 3981 3982 /* 3983 * If a disconnect was started/in progress the enrollee will start once 3984 * that has finished 3985 */ 3986 3987 dpp->pending = l_dbus_message_ref(message); 3988 3989 return NULL; 3990 } 3991 3992 /* 3993 * Set up the configurator for an initiator role. The configurator 3994 * will go offchannel to frequencies advertised by the enrollees URI or, 3995 * if no channels are provided, use a default channel list. 3996 */ 3997 static bool dpp_configurator_start_presence(struct dpp_sm *dpp, const char *uri) 3998 { 3999 _auto_(l_free) uint32_t *freqs = NULL; 4000 size_t freqs_len = 0; 4001 struct dpp_uri_info *info; 4002 4003 info = dpp_parse_uri(uri); 4004 if (!info) 4005 return false; 4006 4007 /* 4008 * Very few drivers actually support registration of multicast frames. 4009 * This renders the presence procedure impossible on most drivers. 4010 * But not all is lost. If the URI contains the MAC and channel 4011 * info we an start going through channels sending auth requests which 4012 * is basically DPP 1.0. Otherwise DPP cannot start. 4013 */ 4014 if (!dpp->mcast_support && 4015 (l_memeqzero(info->mac, 6) || !info->freqs)) { 4016 l_error("No multicast registration support, URI must contain " 4017 "MAC and channel information"); 4018 dpp_free_uri_info(info); 4019 return false; 4020 } 4021 4022 if (!l_memeqzero(info->mac, 6)) 4023 memcpy(dpp->peer_addr, info->mac, 6); 4024 4025 if (info->freqs) 4026 freqs = scan_freq_set_to_fixed_array(info->freqs, &freqs_len); 4027 4028 dpp->peer_boot_public = l_ecc_point_clone(info->boot_public); 4029 dpp->peer_asn1 = dpp_point_to_asn1(info->boot_public, 4030 &dpp->peer_asn1_len); 4031 4032 dpp_free_uri_info(info); 4033 4034 if (!dpp->peer_asn1) { 4035 l_debug("Peer boot key did not convert to asn1"); 4036 return false; 4037 } 4038 4039 dpp_hash(L_CHECKSUM_SHA256, dpp->peer_boot_hash, 1, dpp->peer_asn1, 4040 dpp->peer_asn1_len); 4041 4042 dpp_start_presence(dpp, freqs, freqs_len); 4043 4044 return true; 4045 } 4046 4047 static struct l_dbus_message *dpp_start_configurator_common( 4048 struct l_dbus *dbus, 4049 struct l_dbus_message *message, 4050 void *user_data, 4051 bool responder) 4052 { 4053 struct dpp_sm *dpp = user_data; 4054 struct l_dbus_message *reply; 4055 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 4056 struct scan_bss *bss; 4057 struct network *network; 4058 struct l_settings *settings; 4059 struct handshake_state *hs = netdev_get_handshake(dpp->netdev); 4060 const char *uri; 4061 4062 /* 4063 * For now limit the configurator to only configuring enrollees to the 4064 * currently connected network. 4065 */ 4066 if (!station) 4067 return dbus_error_not_available(message); 4068 4069 bss = station_get_connected_bss(station); 4070 network = station_get_connected_network(station); 4071 if (!bss || !network) 4072 return dbus_error_not_connected(message); 4073 4074 settings = network_get_settings(network); 4075 if (!settings) 4076 return dbus_error_not_configured(message); 4077 4078 if (network_get_security(network) != SECURITY_PSK) 4079 return dbus_error_not_supported(message); 4080 4081 if (dpp->state != DPP_STATE_NOTHING || 4082 dpp->interface != DPP_INTERFACE_UNBOUND) 4083 return dbus_error_busy(message); 4084 4085 l_ecdh_generate_key_pair(dpp->curve, &dpp->proto_private, 4086 &dpp->own_proto_public); 4087 4088 dpp->state = DPP_STATE_PRESENCE; 4089 4090 if (!responder) { 4091 if (!l_dbus_message_get_arguments(message, "s", &uri)) 4092 return dbus_error_invalid_args(message); 4093 4094 if (!dpp_configurator_start_presence(dpp, uri)) 4095 return dbus_error_invalid_args(message); 4096 4097 /* Since we have the peer's URI generate the keys now */ 4098 l_getrandom(dpp->i_nonce, dpp->nonce_len); 4099 4100 dpp->m = dpp_derive_k1(dpp->peer_boot_public, 4101 dpp->proto_private, dpp->k1); 4102 4103 if (!dpp->mcast_support) 4104 dpp->state = DPP_STATE_AUTHENTICATING; 4105 4106 dpp->new_freq = bss->frequency; 4107 } else 4108 dpp->current_freq = bss->frequency; 4109 4110 dpp_add_frame_watches(dpp, responder && dpp->mcast_support); 4111 4112 dpp->uri = dpp_generate_uri(dpp->own_asn1, dpp->own_asn1_len, 2, 4113 netdev_get_address(dpp->netdev), 4114 &bss->frequency, 1, NULL, NULL); 4115 dpp->role = DPP_CAPABILITY_CONFIGURATOR; 4116 dpp->interface = DPP_INTERFACE_DPP; 4117 dpp->config = dpp_configuration_new(settings, 4118 network_get_ssid(network), 4119 hs->akm_suite); 4120 4121 dpp_property_changed_notify(dpp); 4122 4123 dpp->station_watch = station_add_state_watch(station, 4124 dpp_station_state_watch, dpp, NULL); 4125 4126 l_debug("DPP Start Configurator: %s", dpp->uri); 4127 4128 reply = l_dbus_message_new_method_return(message); 4129 4130 l_dbus_message_set_arguments(reply, "s", dpp->uri); 4131 4132 return reply; 4133 } 4134 4135 static struct l_dbus_message *dpp_dbus_start_configurator(struct l_dbus *dbus, 4136 struct l_dbus_message *message, 4137 void *user_data) 4138 { 4139 return dpp_start_configurator_common(dbus, message, user_data, true); 4140 } 4141 4142 static struct l_dbus_message *dpp_dbus_configure_enrollee(struct l_dbus *dbus, 4143 struct l_dbus_message *message, 4144 void *user_data) 4145 { 4146 return dpp_start_configurator_common(dbus, message, user_data, false); 4147 } 4148 4149 static struct l_dbus_message *dpp_dbus_stop(struct l_dbus *dbus, 4150 struct l_dbus_message *message, 4151 void *user_data) 4152 { 4153 struct dpp_sm *dpp = user_data; 4154 4155 l_debug(""); 4156 4157 if (dpp->interface != DPP_INTERFACE_DPP) 4158 return dbus_error_not_found(message); 4159 4160 dpp_reset(dpp); 4161 4162 return l_dbus_message_new_method_return(message); 4163 } 4164 4165 static void dpp_pkex_scan_trigger(int err, void *user_data) 4166 { 4167 struct dpp_sm *dpp = user_data; 4168 4169 if (err < 0) 4170 dpp_reset(dpp); 4171 } 4172 4173 /* 4174 * Section 5.6.1 4175 * In lieu of specific channel information obtained in a manner outside 4176 * the scope of this specification, PKEX responders shall select one of 4177 * the following channels: 4178 * - 2.4 GHz: Channel 6 (2.437 GHz) 4179 * - 5 GHz: Channel 44 (5.220 GHz) if local regulations permit 4180 * operation only in the 5.150 - 5.250 GHz band and Channel 4181 * 149 (5.745 GHz) otherwise 4182 */ 4183 static uint32_t *dpp_default_freqs(struct dpp_sm *dpp, size_t *out_len) 4184 { 4185 struct wiphy *wiphy = wiphy_find_by_wdev(dpp->wdev_id); 4186 uint32_t default_channels[3] = { 2437, 5220, 5745 }; 4187 uint32_t *freqs_out; 4188 size_t len = 0; 4189 4190 if ((wiphy_get_supported_bands(wiphy) & BAND_FREQ_2_4_GHZ) && 4191 scan_get_band_rank_modifier(BAND_FREQ_2_4_GHZ) != 0) 4192 default_channels[len++] = 2437; 4193 4194 if ((wiphy_get_supported_bands(wiphy) & BAND_FREQ_5_GHZ) && 4195 scan_get_band_rank_modifier(BAND_FREQ_5_GHZ) != 0) { 4196 default_channels[len++] = 5220; 4197 default_channels[len++] = 5745; 4198 } 4199 4200 if (!len) { 4201 l_warn("No bands are allowed, check BandModifier* settings!"); 4202 return NULL; 4203 } 4204 4205 freqs_out = l_memdup(default_channels, sizeof(uint32_t) * len); 4206 *out_len = len; 4207 4208 return freqs_out; 4209 } 4210 4211 static void __dpp_pkex_start_enrollee(struct dpp_sm *dpp) 4212 { 4213 uint32_t timeout = minsize(DPP_PKEX_PROTO_TIMEOUT, 4214 dpp->freqs_len * DPP_PKEX_PROTO_PER_FREQ_TIMEOUT); 4215 dpp->current_freq = dpp->freqs[0]; 4216 4217 dpp_reset_protocol_timer(dpp, timeout); 4218 4219 l_debug("PKEX start enrollee (id=%s)", dpp->pkex_id ?: "unset"); 4220 4221 dpp_start_offchannel(dpp, dpp->current_freq); 4222 } 4223 4224 static bool dpp_pkex_scan_notify(int err, struct l_queue *bss_list, 4225 const struct scan_freq_set *freqs, 4226 void *user_data) 4227 { 4228 struct dpp_sm *dpp = user_data; 4229 const struct l_queue_entry *e; 4230 _auto_(scan_freq_set_free) struct scan_freq_set *freq_set = NULL; 4231 4232 if (err < 0) 4233 goto failed; 4234 4235 freq_set = scan_freq_set_new(); 4236 4237 if (!bss_list || l_queue_isempty(bss_list)) { 4238 dpp->freqs = dpp_default_freqs(dpp, &dpp->freqs_len); 4239 if (!dpp->freqs) 4240 goto failed; 4241 4242 l_debug("No BSS's seen, using default frequency list"); 4243 goto start; 4244 } 4245 4246 for (e = l_queue_get_entries(bss_list); e; e = e->next) { 4247 const struct scan_bss *bss = e->data; 4248 4249 scan_freq_set_add(freq_set, bss->frequency); 4250 } 4251 4252 l_debug("Found %u frequencies to search for configurator", 4253 l_queue_length(bss_list)); 4254 4255 dpp->freqs = scan_freq_set_to_fixed_array(freq_set, &dpp->freqs_len); 4256 4257 start: 4258 __dpp_pkex_start_enrollee(dpp); 4259 4260 return false; 4261 4262 failed: 4263 dpp_reset(dpp); 4264 return false; 4265 } 4266 4267 static void dpp_pkex_scan_destroy(void *user_data) 4268 { 4269 struct dpp_sm *dpp = user_data; 4270 4271 dpp->pkex_scan_id = 0; 4272 } 4273 4274 static bool dpp_start_pkex_enrollee(struct dpp_sm *dpp) 4275 { 4276 dpp_property_changed_notify(dpp); 4277 4278 /* Already have a set (or single) frequency */ 4279 if (dpp->freqs) { 4280 __dpp_pkex_start_enrollee(dpp); 4281 return true; 4282 } 4283 4284 /* 4285 * The 'dpp_default_freqs' function returns the default frequencies 4286 * outlined in section 5.6.1. For 2.4/5GHz this is only 3 frequencies 4287 * which is unlikely to result in discovery of a configurator. The spec 4288 * does allow frequencies to be "obtained in a manner outside the scope 4289 * of this specification" which is what is being done here. 4290 * 4291 * This is mainly geared towards IWD-based configurators; banking on the 4292 * fact that they are currently connected to nearby APs. Scanning lets 4293 * us see nearby BSS's which should be the same frequencies as our 4294 * target configurator. 4295 */ 4296 l_debug("Performing scan for frequencies to start PKEX"); 4297 4298 dpp->pkex_scan_id = scan_active(dpp->wdev_id, NULL, 0, 4299 dpp_pkex_scan_trigger, dpp_pkex_scan_notify, 4300 dpp, dpp_pkex_scan_destroy); 4301 if (!dpp->pkex_scan_id) 4302 goto failed; 4303 4304 return true; 4305 4306 failed: 4307 dpp_reset(dpp); 4308 return false; 4309 } 4310 4311 static bool dpp_parse_pkex_args(struct l_dbus_message *message, 4312 const char **key_out, 4313 const char **id_out, 4314 const char **mac_out, 4315 uint32_t *freq_out) 4316 { 4317 struct l_dbus_message_iter iter; 4318 struct l_dbus_message_iter variant; 4319 const char *dict_key; 4320 const char *key = NULL; 4321 const char *id = NULL; 4322 const char *mac = NULL; 4323 uint32_t freq = 0; 4324 4325 if (!l_dbus_message_get_arguments(message, "a{sv}", &iter)) 4326 return false; 4327 4328 while (l_dbus_message_iter_next_entry(&iter, &dict_key, &variant)) { 4329 if (!strcmp(dict_key, "Code")) { 4330 if (!l_dbus_message_iter_get_variant(&variant, "s", 4331 &key)) 4332 return false; 4333 } else if (!strcmp(dict_key, "Identifier")) { 4334 if (!l_dbus_message_iter_get_variant(&variant, "s", 4335 &id)) 4336 return false; 4337 } else if (!strcmp(dict_key, "Address")) { 4338 if (!l_dbus_message_iter_get_variant(&variant, "s", 4339 &mac)) 4340 return false; 4341 } else if (!strcmp(dict_key, "Frequency")) { 4342 if (!l_dbus_message_iter_get_variant(&variant, "u", 4343 &freq)) 4344 return false; 4345 } 4346 } 4347 4348 if (!key) 4349 return false; 4350 4351 if (id && strlen(id) > 80) 4352 return false; 4353 4354 *key_out = key; 4355 *id_out = id; 4356 4357 if (mac_out) 4358 *mac_out = mac; 4359 4360 if (freq_out) 4361 *freq_out = freq; 4362 4363 return true; 4364 } 4365 4366 static bool dpp_pkex_derive_keys(struct dpp_sm *dpp) 4367 { 4368 _auto_(l_ecc_point_free) struct l_ecc_point *qi = NULL; 4369 4370 /* 4371 * In theory a driver could support a lesser duration than 200ms. This 4372 * complicates things since we would need to tack on additional 4373 * offchannel requests to meet the 200ms requirement. This could be done 4374 * but for now use max_roc or 200ms, whichever is less. 4375 */ 4376 dpp->dwell = (dpp->max_roc < 200) ? dpp->max_roc : 200; 4377 /* "DPP R2 devices are expected to use PKEXv1 by default" */ 4378 dpp->pkex_version = 1; 4379 4380 if (!l_ecdh_generate_key_pair(dpp->curve, &dpp->pkex_private, 4381 &dpp->pkex_public)) 4382 return false; 4383 4384 /* 4385 * "If Qi is the point-at-infinity, the code shall be deleted and the 4386 * user should be notified to provision a new code" 4387 */ 4388 qi = dpp_derive_qi(dpp->curve, dpp->pkex_key, dpp->pkex_id, 4389 netdev_get_address(dpp->netdev)); 4390 if (!qi || l_ecc_point_is_infinity(qi)) { 4391 l_debug("Cannot derive Qi, provision a new code"); 4392 return false; 4393 } 4394 4395 dpp->pkex_m = l_ecc_point_new(dpp->curve); 4396 4397 return l_ecc_point_add(dpp->pkex_m, dpp->pkex_public, qi); 4398 } 4399 4400 static struct l_dbus_message *dpp_dbus_pkex_start_enrollee(struct l_dbus *dbus, 4401 struct l_dbus_message *message, 4402 void *user_data) 4403 { 4404 struct dpp_sm *dpp = user_data; 4405 const char *key; 4406 const char *id; 4407 const char *mac_str; 4408 uint32_t freq; 4409 bool wait_for_disconnect; 4410 int ret; 4411 4412 l_debug(""); 4413 4414 if (dpp->state != DPP_STATE_NOTHING || 4415 dpp->interface != DPP_INTERFACE_UNBOUND) 4416 return dbus_error_busy(message); 4417 4418 if (!dpp_parse_pkex_args(message, &key, &id, &mac_str, &freq)) 4419 goto invalid_args; 4420 4421 if (mac_str && !util_string_to_address(mac_str, dpp->peer_addr)) 4422 goto invalid_args; 4423 else if (!mac_str) 4424 memcpy(dpp->peer_addr, broadcast, 6); 4425 4426 if (freq) { 4427 dpp->freqs = l_new(uint32_t, 1); 4428 dpp->freqs[0] = freq; 4429 dpp->freqs_len = 1; 4430 } 4431 4432 dpp->pkex_key = l_strdup(key); 4433 4434 if (id) 4435 dpp->pkex_id = l_strdup(id); 4436 4437 dpp->role = DPP_CAPABILITY_ENROLLEE; 4438 dpp->state = DPP_STATE_PKEX_EXCHANGE; 4439 dpp->interface = DPP_INTERFACE_PKEX; 4440 4441 dpp_add_frame_watches(dpp, false); 4442 4443 ret = dpp_try_disconnect_station(dpp, &wait_for_disconnect); 4444 if (ret < 0) { 4445 dpp_reset(dpp); 4446 return dbus_error_from_errno(ret, message); 4447 } 4448 4449 if (!dpp_pkex_derive_keys(dpp)) { 4450 dpp_reset(dpp); 4451 return dbus_error_failed(message); 4452 } 4453 4454 if (!wait_for_disconnect && !dpp_start_pkex_enrollee(dpp)) 4455 goto invalid_args; 4456 4457 /* 4458 * If a disconnect was started/in progress the PKEX enrollee will start 4459 * once that has finished 4460 */ 4461 4462 return l_dbus_message_new_method_return(message); 4463 4464 invalid_args: 4465 return dbus_error_invalid_args(message); 4466 } 4467 4468 static void pkex_agent_disconnect(struct l_dbus *dbus, void *user_data) 4469 { 4470 struct dpp_sm *dpp = user_data; 4471 4472 l_debug("SharedCodeAgent %s disconnected", dpp->agent->path); 4473 4474 dpp_reset(dpp); 4475 } 4476 4477 static void dpp_create_agent(struct dpp_sm *dpp, const char *path, 4478 struct l_dbus_message *message) 4479 { 4480 const char *sender = l_dbus_message_get_sender(message); 4481 4482 dpp->agent = l_new(struct pkex_agent, 1); 4483 dpp->agent->owner = l_strdup(sender); 4484 dpp->agent->path = l_strdup(path); 4485 dpp->agent->disconnect_watch = l_dbus_add_disconnect_watch( 4486 dbus_get_bus(), 4487 sender, 4488 pkex_agent_disconnect, 4489 dpp, NULL); 4490 4491 l_debug("Registered a SharedCodeAgent on path %s", path); 4492 } 4493 4494 static struct l_dbus_message *dpp_start_pkex_configurator(struct dpp_sm *dpp, 4495 const char *key, const char *identifier, 4496 const char *agent_path, 4497 struct l_dbus_message *message) 4498 { 4499 struct handshake_state *hs = netdev_get_handshake(dpp->netdev); 4500 struct station *station = station_find(netdev_get_ifindex(dpp->netdev)); 4501 struct network *network = station_get_connected_network(station); 4502 struct scan_bss *bss = station_get_connected_bss(station); 4503 const struct l_settings *settings; 4504 4505 if (dpp->state != DPP_STATE_NOTHING || 4506 dpp->interface != DPP_INTERFACE_UNBOUND) 4507 return dbus_error_busy(message); 4508 4509 if (!dpp->mcast_support) 4510 l_debug("Multicast frame registration not supported, only " 4511 "enrollees sending uncast will be supported"); 4512 4513 if (!network || !bss) 4514 return dbus_error_not_connected(message); 4515 4516 settings = network_get_settings(network); 4517 if (!settings) { 4518 l_debug("No settings for network, is this a known network?"); 4519 return dbus_error_not_configured(message); 4520 } 4521 4522 if (identifier) 4523 dpp->pkex_id = l_strdup(identifier); 4524 4525 if (key) 4526 dpp->pkex_key = l_strdup(key); 4527 4528 if (agent_path) 4529 dpp_create_agent(dpp, agent_path, message); 4530 4531 dpp->role = DPP_CAPABILITY_CONFIGURATOR; 4532 dpp->state = DPP_STATE_PKEX_EXCHANGE; 4533 dpp->interface = DPP_INTERFACE_PKEX; 4534 dpp->current_freq = bss->frequency; 4535 dpp->pkex_version = 1; 4536 dpp->config = dpp_configuration_new(network_get_settings(network), 4537 network_get_ssid(network), 4538 hs->akm_suite); 4539 dpp_add_frame_watches(dpp, dpp->mcast_support); 4540 4541 dpp_reset_protocol_timer(dpp, DPP_PKEX_PROTO_TIMEOUT); 4542 dpp_property_changed_notify(dpp); 4543 4544 dpp->station_watch = station_add_state_watch(station, 4545 dpp_station_state_watch, dpp, NULL); 4546 4547 if (dpp->pkex_key) 4548 l_debug("Starting PKEX configurator for single enrollee"); 4549 else 4550 l_debug("Starting PKEX configurator with agent"); 4551 4552 return l_dbus_message_new_method_return(message); 4553 } 4554 4555 static struct l_dbus_message *dpp_dbus_pkex_configure_enrollee( 4556 struct l_dbus *dbus, 4557 struct l_dbus_message *message, 4558 void *user_data) 4559 { 4560 struct dpp_sm *dpp = user_data; 4561 const char *key; 4562 const char *id; 4563 4564 l_debug(""); 4565 4566 if (!dpp_parse_pkex_args(message, &key, &id, NULL, NULL)) 4567 return dbus_error_invalid_args(message); 4568 4569 return dpp_start_pkex_configurator(dpp, key, id, NULL, message); 4570 } 4571 4572 static struct l_dbus_message *dpp_dbus_pkex_start_configurator( 4573 struct l_dbus *dbus, 4574 struct l_dbus_message *message, 4575 void *user_data) 4576 { 4577 struct dpp_sm *dpp = user_data; 4578 const char *path; 4579 4580 if (!l_dbus_message_get_arguments(message, "o", &path)) 4581 return dbus_error_invalid_args(message); 4582 4583 return dpp_start_pkex_configurator(dpp, NULL, NULL, path, message); 4584 } 4585 4586 static void dpp_setup_interface(struct l_dbus_interface *interface) 4587 { 4588 l_dbus_interface_method(interface, "StartEnrollee", 0, 4589 dpp_dbus_start_enrollee, "s", "", "uri"); 4590 l_dbus_interface_method(interface, "StartConfigurator", 0, 4591 dpp_dbus_start_configurator, "s", "", "uri"); 4592 l_dbus_interface_method(interface, "ConfigureEnrollee", 0, 4593 dpp_dbus_configure_enrollee, "", "s", "uri"); 4594 l_dbus_interface_method(interface, "Stop", 0, 4595 dpp_dbus_stop, "", ""); 4596 4597 l_dbus_interface_property(interface, "Started", 0, "b", dpp_get_started, 4598 NULL); 4599 l_dbus_interface_property(interface, "Role", 0, "s", dpp_get_role, 4600 NULL); 4601 l_dbus_interface_property(interface, "URI", 0, "s", dpp_get_uri, NULL); 4602 } 4603 4604 static struct l_dbus_message *dpp_dbus_pkex_stop(struct l_dbus *dbus, 4605 struct l_dbus_message *message, void *user_data) 4606 { 4607 struct dpp_sm *dpp = user_data; 4608 4609 l_debug(""); 4610 4611 if (dpp->interface != DPP_INTERFACE_PKEX) 4612 return dbus_error_not_found(message); 4613 4614 dpp_reset(dpp); 4615 4616 return l_dbus_message_new_method_return(message); 4617 } 4618 4619 static void dpp_setup_pkex_interface(struct l_dbus_interface *interface) 4620 { 4621 l_dbus_interface_method(interface, "StartEnrollee", 0, 4622 dpp_dbus_pkex_start_enrollee, "", "a{sv}", "args"); 4623 l_dbus_interface_method(interface, "Stop", 0, 4624 dpp_dbus_pkex_stop, "", ""); 4625 l_dbus_interface_method(interface, "ConfigureEnrollee", 0, 4626 dpp_dbus_pkex_configure_enrollee, "", "a{sv}", "args"); 4627 l_dbus_interface_method(interface, "StartConfigurator", 0, 4628 dpp_dbus_pkex_start_configurator, "", "o", "path"); 4629 4630 l_dbus_interface_property(interface, "Started", 0, "b", 4631 dpp_pkex_get_started, NULL); 4632 l_dbus_interface_property(interface, "Role", 0, "s", 4633 dpp_pkex_get_role, NULL); 4634 } 4635 4636 static void dpp_destroy_interface(void *user_data) 4637 { 4638 struct dpp_sm *dpp = user_data; 4639 4640 if (--dpp->refcount) 4641 return; 4642 4643 l_queue_remove(dpp_list, dpp); 4644 4645 dpp_free(dpp); 4646 } 4647 4648 static int dpp_init(void) 4649 { 4650 nl80211 = l_genl_family_new(iwd_get_genl(), NL80211_GENL_NAME); 4651 if (!nl80211) { 4652 l_error("Failed to obtain nl80211"); 4653 return -EIO; 4654 } 4655 4656 netdev_watch = netdev_watch_add(dpp_netdev_watch, NULL, NULL); 4657 4658 l_dbus_register_interface(dbus_get_bus(), IWD_DPP_INTERFACE, 4659 dpp_setup_interface, 4660 dpp_destroy_interface, false); 4661 l_dbus_register_interface(dbus_get_bus(), IWD_DPP_PKEX_INTERFACE, 4662 dpp_setup_pkex_interface, 4663 dpp_destroy_interface, false); 4664 4665 mlme_watch = l_genl_family_register(nl80211, "mlme", dpp_mlme_notify, 4666 NULL, NULL); 4667 4668 dpp_list = l_queue_new(); 4669 4670 return 0; 4671 } 4672 4673 static void dpp_exit(void) 4674 { 4675 l_debug(""); 4676 4677 l_dbus_unregister_interface(dbus_get_bus(), IWD_DPP_INTERFACE); 4678 l_dbus_unregister_interface(dbus_get_bus(), IWD_DPP_PKEX_INTERFACE); 4679 4680 netdev_watch_remove(netdev_watch); 4681 4682 l_genl_family_unregister(nl80211, mlme_watch); 4683 mlme_watch = 0; 4684 4685 l_genl_family_free(nl80211); 4686 nl80211 = NULL; 4687 4688 l_queue_destroy(dpp_list, (l_queue_destroy_func_t) dpp_free); 4689 } 4690 4691 IWD_MODULE(dpp, dpp_init, dpp_exit); 4692 IWD_MODULE_DEPENDS(dpp, netdev);