sae.c (42495B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-2019 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 <stdlib.h> 29 30 #include <ell/ell.h> 31 32 #include "src/missing.h" 33 #include "src/util.h" 34 #include "src/ie.h" 35 #include "src/handshake.h" 36 #include "src/crypto.h" 37 #include "src/mpdu.h" 38 #include "src/auth-proto.h" 39 #include "src/sae.h" 40 #include "src/module.h" 41 42 static bool debug; 43 44 /* SHA-512 is the highest supported hashing function as of 802.11-2020 */ 45 #define SAE_MAX_HASH_LEN 64 46 47 #define SAE_RETRANSMIT_TIMEOUT 2 48 #define SAE_SYNC_MAX 3 49 #define SAE_MAX_ASSOC_RETRY 3 50 51 #define sae_debug(fmat, ...) \ 52 ({ \ 53 if (debug) \ 54 l_info("[SAE]: "fmat, ##__VA_ARGS__); \ 55 }) 56 57 enum sae_state { 58 SAE_STATE_NOTHING = 0, 59 SAE_STATE_COMMITTED = 1, 60 SAE_STATE_CONFIRMED = 2, 61 SAE_STATE_ACCEPTED = 3, 62 }; 63 64 struct sae_sm { 65 struct auth_proto ap; 66 struct handshake_state *handshake; 67 struct l_ecc_point *pwe; 68 enum sae_state state; 69 const struct l_ecc_curve *curve; 70 unsigned int group; 71 int group_retry; 72 uint16_t *rejected_groups; 73 struct l_ecc_scalar *rand; 74 struct l_ecc_scalar *scalar; 75 struct l_ecc_scalar *p_scalar; 76 struct l_ecc_point *element; 77 struct l_ecc_point *p_element; 78 uint16_t send_confirm; 79 uint8_t kck[SAE_MAX_HASH_LEN]; 80 uint8_t pmk[32]; 81 uint8_t pmkid[16]; 82 uint8_t *token; 83 size_t token_len; 84 /* number of state resyncs that have occurred */ 85 uint16_t sync; 86 /* number of SAE confirm messages that have been sent */ 87 uint16_t sc; 88 /* received value of the send-confirm counter */ 89 uint16_t rc; 90 /* remote peer */ 91 uint8_t peer[6]; 92 uint8_t assoc_retry; 93 94 sae_tx_authenticate_func_t tx_auth; 95 sae_tx_associate_func_t tx_assoc; 96 void *user_data; 97 enum crypto_sae sae_type; 98 99 bool force_default_group : 1; 100 }; 101 102 static enum mmpdu_status_code sae_status_code(struct sae_sm *sm) 103 { 104 switch (sm->sae_type) { 105 case CRYPTO_SAE_LOOPING: 106 return MMPDU_STATUS_CODE_SUCCESS; 107 case CRYPTO_SAE_HASH_TO_ELEMENT: 108 return MMPDU_STATUS_CODE_SAE_HASH_TO_ELEMENT; 109 } 110 111 return MMPDU_STATUS_CODE_UNSPECIFIED; 112 } 113 114 static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group) 115 { 116 uint16_t i; 117 118 if (!sm->rejected_groups) { 119 sm->rejected_groups = reallocarray(NULL, 2, sizeof(uint16_t)); 120 sm->rejected_groups[0] = 1; 121 sm->rejected_groups[1] = group; 122 return; 123 } 124 125 for (i = 1; i <= sm->rejected_groups[0]; i++) 126 if (sm->rejected_groups[i] == group) 127 return; 128 129 sm->rejected_groups = reallocarray(sm->rejected_groups, 130 i + 1, sizeof(uint16_t)); 131 sm->rejected_groups[0] += 1; 132 sm->rejected_groups[i] = group; 133 } 134 135 static void sae_reset_state(struct sae_sm *sm) 136 { 137 l_ecc_scalar_free(sm->scalar); 138 sm->scalar = NULL; 139 l_ecc_scalar_free(sm->p_scalar); 140 sm->p_scalar = NULL; 141 l_ecc_scalar_free(sm->rand); 142 sm->rand = NULL; 143 l_ecc_point_free(sm->element); 144 sm->element = NULL; 145 l_ecc_point_free(sm->p_element); 146 sm->p_element = NULL; 147 l_ecc_point_free(sm->pwe); 148 sm->pwe = NULL; 149 } 150 151 static int sae_choose_next_group(struct sae_sm *sm) 152 { 153 const unsigned int *ecc_groups = l_ecc_supported_ike_groups(); 154 bool reset = sm->group_retry >= 0; 155 unsigned int group; 156 157 /* Find the next group in the list */ 158 while ((group = ecc_groups[++sm->group_retry])) { 159 /* 160 * Forcing the default group; only choose group 19. If we have 161 * already passed 19 (due to a retry) we will exhaust all other 162 * groups and should fail. 163 */ 164 if (sm->force_default_group && group != 19) 165 continue; 166 167 /* Ensure the PT was derived for this group */ 168 if (sm->sae_type == CRYPTO_SAE_HASH_TO_ELEMENT && 169 !sm->handshake->ecc_sae_pts[sm->group_retry]) 170 continue; 171 172 /* 173 * TODO: Groups for P192, P224 and P521 are currently 174 * non-functional with SAE. Until this is fixed we need to 175 * avoid these groups from being used. 176 */ 177 if (group == 21 || group == 25 || group == 26) 178 continue; 179 180 break; 181 } 182 183 if (!group) 184 return -ENOENT; 185 186 sm->group = group; 187 188 if (reset) 189 sae_reset_state(sm); 190 191 sae_debug("Using group %u", sm->group); 192 193 sm->curve = l_ecc_curve_from_ike_group(sm->group); 194 195 return 0; 196 } 197 198 static int sae_valid_group(struct sae_sm *sm, unsigned int group) 199 { 200 const unsigned int *ecc_groups = l_ecc_supported_ike_groups(); 201 unsigned int i; 202 203 for (i = sm->group_retry; ecc_groups[i]; i++) { 204 if (ecc_groups[i] != group) 205 continue; 206 207 if (sm->sae_type != CRYPTO_SAE_LOOPING && 208 !sm->handshake->ecc_sae_pts[i]) 209 continue; 210 211 return i; 212 } 213 214 return -ENOENT; 215 } 216 217 static bool sae_pwd_seed(const uint8_t *addr1, const uint8_t *addr2, 218 uint8_t *base, size_t base_len, 219 uint8_t counter, uint8_t *out) 220 { 221 uint8_t key[12]; 222 223 if (memcmp(addr1, addr2, 6) > 0) { 224 memcpy(key, addr1, 6); 225 memcpy(key + 6, addr2, 6); 226 } else { 227 memcpy(key, addr2, 6); 228 memcpy(key + 6, addr1, 6); 229 } 230 231 return hkdf_extract(L_CHECKSUM_SHA256, key, 12, 2, out, base, base_len, 232 &counter, (size_t) 1); 233 } 234 235 /* 236 * Computes KDF-256(pwd_seed, "SAE Hunting and Pecking", p). If the output is 237 * greater than p, the output is set to qnr, a quadratic non-residue. 238 * Since this happens with very low probability, using the same qnr is fine. 239 */ 240 static struct l_ecc_scalar *sae_pwd_value(const struct l_ecc_curve *curve, 241 uint8_t *pwd_seed, uint8_t *qnr) 242 { 243 uint8_t pwd_value[L_ECC_SCALAR_MAX_BYTES]; 244 uint8_t prime[L_ECC_SCALAR_MAX_BYTES]; 245 ssize_t len; 246 int is_in_range; 247 struct l_ecc_scalar *p = l_ecc_curve_get_prime(curve); 248 249 len = l_ecc_scalar_get_data(p, prime, sizeof(prime)); 250 l_ecc_scalar_free(p); 251 252 if (!kdf_sha256(pwd_seed, 32, "SAE Hunting and Pecking", 253 strlen("SAE Hunting and Pecking"), prime, len, 254 pwd_value, len)) 255 return NULL; 256 257 /* 258 * If pwd_value >= prime, this iteration should fail. We need a smooth 259 * control flow, so we need to continue anyway. 260 */ 261 is_in_range = l_secure_memcmp(pwd_value, prime, len); 262 /* 263 * We only consider is_in_range == -1 as valid, meaning the value of the 264 * MSB defines the mask. 265 */ 266 is_in_range = util_secure_fill_with_msb(is_in_range); 267 268 /* 269 * ELL has public Legendre symbol only for l_ecc_scalar, but they 270 * cannot be created if the coordinate is greater than the p. Hence, 271 * to avoid control flow dependencies, we replace pwd_value by a dummy 272 * quadratic non residue if we generate a value >= prime. 273 */ 274 util_secure_select((uint8_t) is_in_range, pwd_value, qnr, 275 pwd_value, sizeof(pwd_value)); 276 277 return l_ecc_scalar_new(curve, pwd_value, sizeof(pwd_value)); 278 } 279 280 /* IEEE 802.11-2016 - Section 12.4.2 Assumptions on SAE */ 281 static ssize_t sae_cn(struct sae_sm *sm, uint16_t send_confirm, 282 struct l_ecc_scalar *scalar1, 283 struct l_ecc_point *element1, 284 struct l_ecc_scalar *scalar2, 285 struct l_ecc_point *element2, 286 uint8_t *confirm) 287 { 288 enum l_checksum_type hash = 289 crypto_sae_hash_from_ecc_prime_len(sm->sae_type, 290 l_ecc_curve_get_scalar_bytes(sm->curve)); 291 size_t hash_len = l_checksum_digest_length(hash); 292 uint8_t s1[L_ECC_SCALAR_MAX_BYTES]; 293 uint8_t s2[L_ECC_SCALAR_MAX_BYTES]; 294 uint8_t e1[L_ECC_POINT_MAX_BYTES]; 295 uint8_t e2[L_ECC_POINT_MAX_BYTES]; 296 struct l_checksum *hmac; 297 struct iovec iov[5]; 298 ssize_t ret; 299 300 hmac = l_checksum_new_hmac(hash, sm->kck, hash_len); 301 if (!hmac) 302 return false; 303 304 send_confirm = L_CPU_TO_LE16(send_confirm); 305 306 iov[0].iov_base = &send_confirm; 307 iov[0].iov_len = 2; 308 iov[1].iov_base = (void *) s1; 309 iov[1].iov_len = l_ecc_scalar_get_data(scalar1, s1, sizeof(s1)); 310 iov[2].iov_base = (void *) e1; 311 iov[2].iov_len = l_ecc_point_get_data(element1, e1, sizeof(e1)); 312 iov[3].iov_base = (void *) s2; 313 iov[3].iov_len = l_ecc_scalar_get_data(scalar2, s2, sizeof(s2)); 314 iov[4].iov_base = (void *) e2; 315 iov[4].iov_len = l_ecc_point_get_data(element2, e2, sizeof(e2)); 316 317 l_checksum_updatev(hmac, iov, 5); 318 ret = l_checksum_get_digest(hmac, confirm, hash_len); 319 l_checksum_free(hmac); 320 321 return ret; 322 } 323 324 static int sae_reject(struct sae_sm *sm, uint16_t transaction, uint16_t status) 325 { 326 uint8_t reject[6]; 327 uint8_t *ptr = reject; 328 329 if (!sm->handshake->authenticator) 330 return -EPROTO; 331 332 /* transaction */ 333 l_put_u16(transaction, ptr); 334 ptr += 2; 335 336 l_put_u16(status, ptr); 337 ptr += 2; 338 339 if (status == MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP) { 340 l_put_u16(sm->group, ptr); 341 ptr += 2; 342 } 343 344 sae_debug("Rejecting exchange transaction=%u status=%u", 345 transaction, status); 346 347 sm->tx_auth(reject, ptr - reject, sm->user_data); 348 349 return status; 350 } 351 352 static struct l_ecc_scalar *sae_new_residue(const struct l_ecc_curve *curve, 353 bool residue) 354 { 355 struct l_ecc_scalar *s = l_ecc_scalar_new_random(curve); 356 357 while (l_ecc_scalar_legendre(s) != ((residue) ? -1 : 1)) { 358 l_ecc_scalar_free(s); 359 s = l_ecc_scalar_new_random(curve); 360 } 361 362 return s; 363 } 364 365 static uint8_t sae_is_quadradic_residue(const struct l_ecc_curve *curve, 366 struct l_ecc_scalar *value, 367 struct l_ecc_scalar *qr, 368 struct l_ecc_scalar *qnr) 369 { 370 uint64_t rbuf[L_ECC_MAX_DIGITS]; 371 struct l_ecc_scalar *y_sqr = l_ecc_scalar_new(curve, NULL, 0); 372 struct l_ecc_scalar *r = l_ecc_scalar_new_random(curve); 373 struct l_ecc_scalar *num = l_ecc_scalar_new(curve, NULL, 0); 374 size_t bytes; 375 376 l_ecc_scalar_sum_x(y_sqr, value); 377 378 l_ecc_scalar_multiply(num, y_sqr, r); 379 l_ecc_scalar_multiply(num, num, r); 380 381 l_ecc_scalar_free(y_sqr); 382 383 bytes = l_ecc_scalar_get_data(r, rbuf, sizeof(rbuf)); 384 l_ecc_scalar_free(r); 385 386 if (bytes <= 0) { 387 l_ecc_scalar_free(num); 388 return 0; 389 } 390 391 if (rbuf[bytes / 8 - 1] & 1) { 392 l_ecc_scalar_multiply(num, num, qr); 393 394 if (l_ecc_scalar_legendre(num) == -1) { 395 l_ecc_scalar_free(num); 396 return 1; 397 } 398 } else { 399 l_ecc_scalar_multiply(num, num, qnr); 400 401 if (l_ecc_scalar_legendre(num) == 1) { 402 l_ecc_scalar_free(num); 403 return 1; 404 } 405 } 406 407 l_ecc_scalar_free(num); 408 409 return 0; 410 } 411 412 /* 413 * IEEE 802.11-2016 Section 12.4.4.2.2 414 * Generation of the password element with ECC groups 415 */ 416 static struct l_ecc_point *sae_compute_pwe(const struct l_ecc_curve *curve, 417 const char *password, 418 const uint8_t *addr1, 419 const uint8_t *addr2) 420 { 421 uint8_t found = 0; 422 uint8_t is_residue; 423 uint8_t is_odd = 0; 424 uint8_t counter; 425 uint8_t pwd_seed[32]; 426 uint8_t x[L_ECC_SCALAR_MAX_BYTES]; 427 uint8_t x_cand[L_ECC_SCALAR_MAX_BYTES]; 428 struct l_ecc_scalar *pwd_value; 429 uint8_t *dummy; 430 uint8_t *base; 431 size_t base_len; 432 struct l_ecc_scalar *qr; 433 struct l_ecc_scalar *qnr; 434 uint8_t qnr_bin[L_ECC_SCALAR_MAX_BYTES] = {0}; 435 struct l_ecc_point *pwe; 436 unsigned int bytes = l_ecc_curve_get_scalar_bytes(curve); 437 438 /* create qr/qnr prior to beginning hunting-and-pecking loop */ 439 qr = sae_new_residue(curve, true); 440 qnr = sae_new_residue(curve, false); 441 l_ecc_scalar_get_data(qnr, qnr_bin, sizeof(qnr_bin)); 442 443 /* 444 * Allocate memory for the base, and set a random dummy to be used in 445 * additional iterations, once a valid value is found 446 */ 447 base_len = strlen(password); 448 base = l_malloc(base_len * sizeof(*base)); 449 dummy = l_malloc(base_len * sizeof(*dummy)); 450 l_getrandom(dummy, base_len); 451 452 /* 453 * Loop with constant time and memory access 454 * We do 30 iterations instead of the 40 recommended to achieve a 455 * resonnable security/complexity trade-off. 456 */ 457 for (counter = 1; counter <= 30; counter++) { 458 /* 459 * Set base to either dummy or password, depending on found's 460 * value. 461 * A non-secure version would be: 462 * base = (found ? dummy : password); 463 */ 464 util_secure_select(found, dummy, (uint8_t *)password, 465 base, base_len); 466 467 /* 468 * pwd-seed = H(max(addr1, addr2) || min(addr1, addr2), 469 * base || counter) 470 * pwd-value = KDF-256(pwd-seed, "SAE Hunting and Pecking", p) 471 */ 472 sae_pwd_seed(addr1, addr2, base, base_len, counter, pwd_seed); 473 /* 474 * The case pwd_value > prime is handled inside, so that 475 * execution can continue whatever the result is, without 476 * changing the outcome. 477 */ 478 pwd_value = sae_pwd_value(curve, pwd_seed, qnr_bin); 479 480 /* 481 * Check if the candidate is a valid x-coordinate on our curve, 482 * and convert it from scalar to binary. 483 */ 484 is_residue = sae_is_quadradic_residue(curve, pwd_value, 485 qr, qnr); 486 l_ecc_scalar_get_data(pwd_value, x_cand, sizeof(x_cand)); 487 488 /* 489 * If we already found the point, we overwrite x with itself. 490 * Otherwise, we copy the new candidate into x. 491 */ 492 util_secure_select(found, x, x_cand, x, sizeof(x)); 493 is_odd = util_secure_select_byte(found, is_odd, 494 pwd_seed[31] & 0x01); 495 496 /* 497 * found is 0 or 0xff here and is_residue is 0 or 1. Bitwise OR 498 * of them (with is_residue converted to 0/0xff) handles this 499 * in constant time. 500 */ 501 found |= is_residue * 0xff; 502 503 memset(pwd_seed, 0, sizeof(pwd_seed)); 504 l_ecc_scalar_free(pwd_value); 505 } 506 507 l_ecc_scalar_free(qr); 508 l_ecc_scalar_free(qnr); 509 l_free(dummy); 510 l_free(base); 511 512 if (!found) { 513 l_error("max PWE iterations reached!"); 514 return NULL; 515 } 516 517 /* 518 * The 802.11 spec requires the point be solved unambiguously (since 519 * solving for Y results in two solutions). The correct Y value 520 * is chosen based on the LSB of the pwd-seed: 521 * 522 * if (LSB(y) == LSB(pwd-seed)) 523 * then 524 * PWE = (x, y) 525 * else 526 * PWE = (x, p-y) 527 * 528 * The ELL API (somewhat hidden from view here) automatically 529 * performs a subtraction (P - Y) when: 530 * - Y is even and BIT1 531 * - Y is odd and BIT0 532 * 533 * So we choose the point type which matches the parity of 534 * pwd-seed. This means a subtraction will be performed (P - Y) 535 * if the parity of pwd-seed and the computed Y do not match. 536 */ 537 pwe = l_ecc_point_from_data(curve, 538 is_odd ? L_ECC_POINT_TYPE_COMPRESSED_BIT1 : 539 L_ECC_POINT_TYPE_COMPRESSED_BIT0, x, bytes); 540 if (!pwe) 541 l_error("computing y failed, was x quadratic residue?"); 542 543 return pwe; 544 } 545 546 static int sae_build_commit(struct sae_sm *sm, const uint8_t *addr1, 547 const uint8_t *addr2, uint8_t *commit, 548 size_t len, bool retry) 549 { 550 struct l_ecc_scalar *mask; 551 uint8_t *ptr = commit; 552 struct l_ecc_scalar *order; 553 struct ie_tlv_builder builder; 554 555 if (retry) 556 goto old_commit; 557 558 switch (sm->sae_type) { 559 case CRYPTO_SAE_HASH_TO_ELEMENT: 560 { 561 const struct l_ecc_point *pt = 562 sm->handshake->ecc_sae_pts[sm->group_retry]; 563 564 sm->pwe = crypto_derive_sae_pwe_from_pt_ecc(addr1, addr2, pt); 565 break; 566 } 567 case CRYPTO_SAE_LOOPING: 568 sm->pwe = sae_compute_pwe(sm->curve, sm->handshake->passphrase, 569 addr1, addr2); 570 break; 571 } 572 573 if (!sm->pwe) { 574 l_error("could not compute PWE"); 575 return -EIO; 576 } 577 578 sm->scalar = l_ecc_scalar_new(sm->curve, NULL, 0); 579 sm->rand = l_ecc_scalar_new_random(sm->curve); 580 mask = l_ecc_scalar_new_random(sm->curve); 581 582 order = l_ecc_curve_get_order(sm->curve); 583 584 /* commit-scalar = (rand + mask) mod r */ 585 l_ecc_scalar_add(sm->scalar, sm->rand, mask, order); 586 587 l_ecc_scalar_free(order); 588 589 /* commit-element = inv(mask * PWE) */ 590 sm->element = l_ecc_point_new(sm->curve); 591 l_ecc_point_multiply(sm->element, mask, sm->pwe); 592 l_ecc_point_inverse(sm->element); 593 594 l_ecc_scalar_free(mask); 595 596 /* 597 * Several cases require retransmitting the same commit message. The 598 * anti-clogging code path requires this as well as the retransmission 599 * timeout. 600 */ 601 old_commit: 602 603 /* 604 * 12.4.7.4 Encoding and decoding of SAE Commit messages 605 * Refer to Table 9-40 for order and Table 9-41 for presence 606 * of elements 607 */ 608 609 /* "a Transaction Sequence Number of 1" */ 610 l_put_le16(1, ptr); 611 ptr += 2; 612 613 /* "a Status Code of SUCCESS or SAE_HASH_TO_ELEMENT" */ 614 l_put_le16(sae_status_code(sm), ptr); 615 ptr += 2; 616 617 /* group */ 618 l_put_le16(sm->group, ptr); 619 ptr += 2; 620 621 if (sm->sae_type == CRYPTO_SAE_LOOPING && sm->token) { 622 memcpy(ptr, sm->token, sm->token_len); 623 ptr += sm->token_len; 624 } 625 626 ptr += l_ecc_scalar_get_data(sm->scalar, ptr, L_ECC_SCALAR_MAX_BYTES); 627 ptr += l_ecc_point_get_data(sm->element, ptr, L_ECC_POINT_MAX_BYTES); 628 629 ie_tlv_builder_init(&builder, ptr, len - (ptr - commit)); 630 631 if (sm->sae_type != CRYPTO_SAE_LOOPING && sm->rejected_groups) { 632 ie_tlv_builder_next(&builder, IE_TYPE_REJECTED_GROUPS); 633 ie_tlv_builder_set_data(&builder, sm->rejected_groups + 1, 634 sm->rejected_groups[0] * sizeof(uint16_t)); 635 } 636 637 if (sm->sae_type != CRYPTO_SAE_LOOPING && sm->token) { 638 ie_tlv_builder_next(&builder, 639 IE_TYPE_ANTI_CLOGGING_TOKEN_CONTAINER); 640 ie_tlv_builder_set_data(&builder, sm->token, sm->token_len); 641 } 642 643 if (sm->sae_type == CRYPTO_SAE_HASH_TO_ELEMENT && 644 sm->handshake->password_identifier) { 645 ie_tlv_builder_next(&builder, IE_TYPE_PASSWORD_IDENTIFIER); 646 ie_tlv_builder_set_data(&builder, 647 sm->handshake->password_identifier, 648 strlen(sm->handshake->password_identifier)); 649 } 650 651 ie_tlv_builder_finalize(&builder, &len); 652 653 return ptr - commit + len; 654 } 655 656 static bool sae_send_confirm(struct sae_sm *sm) 657 { 658 uint8_t confirm[SAE_MAX_HASH_LEN]; 659 uint8_t body[sizeof(confirm) + 6]; 660 uint8_t *ptr = body; 661 ssize_t r; 662 663 /* 664 * confirm = CN(KCK, send-confirm, commit-scalar, COMMIT-ELEMENT, 665 * peer-commit-scalar, PEER-COMMIT-ELEMENT) 666 */ 667 r = sae_cn(sm, sm->sc, sm->scalar, sm->element, sm->p_scalar, 668 sm->p_element, confirm); 669 if (r < 0) 670 return false; 671 672 l_put_le16(2, ptr); 673 ptr += 2; 674 l_put_le16(0, ptr); 675 ptr += 2; 676 l_put_le16(sm->sc, ptr); 677 ptr += 2; 678 memcpy(ptr, confirm, r); 679 ptr += r; 680 681 sae_debug("Sending Confirm to "MAC" sc=%u", 682 MAC_STR(sm->handshake->aa), sm->sc); 683 684 sm->tx_auth(body, ptr - body, sm->user_data); 685 return true; 686 } 687 688 static int sae_calculate_keys(struct sae_sm *sm) 689 { 690 unsigned int nbytes = l_ecc_curve_get_scalar_bytes(sm->curve); 691 enum l_checksum_type hash = 692 crypto_sae_hash_from_ecc_prime_len(sm->sae_type, nbytes); 693 size_t hash_len = l_checksum_digest_length(hash); 694 struct l_ecc_point *k_point; 695 uint8_t k[L_ECC_SCALAR_MAX_BYTES]; 696 ssize_t klen; 697 const void *salt = NULL; 698 size_t salt_len = 0; 699 uint8_t keyseed[SAE_MAX_HASH_LEN]; 700 uint8_t kck_and_pmk[SAE_MAX_HASH_LEN + 32]; 701 uint8_t tmp[L_ECC_SCALAR_MAX_BYTES]; 702 struct l_ecc_scalar *tmp_scalar; 703 struct l_ecc_scalar *order; 704 705 /* 706 * K = scalar-op(rand, (element-op(scalar-op(peer-commit-scalar, PWE), 707 * PEER-COMMIT-ELEMENT))) 708 */ 709 k_point = l_ecc_point_new(sm->curve); 710 711 /* k_point = scalar-op(peer-commit-scalar, PWE) */ 712 l_ecc_point_multiply(k_point, sm->p_scalar, sm->pwe); 713 714 /* k_point = element-op(k_point, PEER-COMMIT-ELEMENT) */ 715 l_ecc_point_add(k_point, k_point, sm->p_element); 716 717 /* k_point = scalar-op(rand, k_point) */ 718 l_ecc_point_multiply(k_point, sm->rand, k_point); 719 720 /* 721 * IEEE 802.11-2016 - Section 12.4.4.2.1 ECC group definition 722 * ECC groups make use of a mapping function, F, that maps a 723 * point (x, y) that satisfies the curve equation to its x-coordinate- 724 * i.e., if P = (x, y) then F(P) = x. 725 */ 726 klen = l_ecc_point_get_x(k_point, k, sizeof(k)); 727 l_ecc_point_free(k_point); 728 729 if (klen < 0) 730 return sae_reject(sm, SAE_STATE_COMMITTED, 731 MMPDU_STATUS_CODE_UNSPECIFIED); 732 733 /* 734 * keyseed = H(salt, k) 735 * 736 * 802.11-2020 12.4.5.4: 737 * Hash to Element case: 738 * "... a salt consisting of the concatenation of the rejected groups 739 * from each peer's Rejected Groups element shall be 740 * passed to the KDF; those of the peer with the highest MAC address go 741 * first (if only one sent a Rejected Groups element then the salt will 742 * consist of that list). " 743 * 744 * Looping case: 745 * "...the salt shall consist of a series of octets of the value zero 746 * whose length equals the length of the digest of the hash function 747 * used to instantiate H()." 748 * 749 * NOTE: We use hkdf_extract here since it is just an hmac invocation 750 * and it handles the case of the zero key for us. 751 */ 752 if (sm->sae_type != CRYPTO_SAE_LOOPING && sm->rejected_groups) { 753 salt = sm->rejected_groups + 1; 754 salt_len = sm->rejected_groups[0] * sizeof(uint16_t); 755 } 756 757 hkdf_extract(hash, salt, salt_len, 1, keyseed, k, klen); 758 759 /* 760 * context = (commit-scalar + peer-commit-scalar) mod r 761 * Length = Q + 256 762 * kck_and_pmk = KDF-Hash-Length(keyseed, "SAE KCK and PMK", context) 763 * KCK = L(kck_and_pmk, 0, Q) 764 * PMK = L(kck_and_pmk, Q, 256) 765 * 766 * Q is the length of the digest of the H(), the hash function used 767 */ 768 tmp_scalar = l_ecc_scalar_new(sm->curve, NULL, 0); 769 order = l_ecc_curve_get_order(sm->curve); 770 771 l_ecc_scalar_add(tmp_scalar, sm->p_scalar, sm->scalar, order); 772 l_ecc_scalar_get_data(tmp_scalar, tmp, sizeof(tmp)); 773 774 crypto_kdf(hash, keyseed, hash_len, 775 "SAE KCK and PMK", strlen("SAE KCK and PMK"), 776 tmp, nbytes, kck_and_pmk, hash_len + 32); 777 778 memcpy(sm->kck, kck_and_pmk, hash_len); 779 memcpy(sm->pmk, kck_and_pmk + hash_len, 32); 780 781 /* 782 * PMKID = L((commit-scalar + peer-commit-scalar) mod r, 0, 128) 783 */ 784 l_ecc_scalar_add(tmp_scalar, sm->scalar, sm->p_scalar, order); 785 l_ecc_scalar_get_data(tmp_scalar, tmp, sizeof(tmp)); 786 787 l_ecc_scalar_free(order); 788 789 l_ecc_scalar_free(tmp_scalar); 790 /* don't set the handshakes pmkid until confirm is verified */ 791 memcpy(sm->pmkid, tmp, 16); 792 793 return 0; 794 } 795 796 797 static int sae_process_commit(struct sae_sm *sm, const uint8_t *from, 798 const uint8_t *frame, size_t len) 799 { 800 uint8_t *ptr = (uint8_t *) frame; 801 unsigned int nbytes = l_ecc_curve_get_scalar_bytes(sm->curve); 802 int r; 803 804 ptr += 2; 805 806 sm->p_scalar = l_ecc_scalar_new(sm->curve, ptr, nbytes); 807 if (!sm->p_scalar) { 808 l_error("Server sent invalid P_Scalar during commit"); 809 return sae_reject(sm, SAE_STATE_COMMITTED, 810 MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP); 811 } 812 813 ptr += nbytes; 814 815 sm->p_element = l_ecc_point_from_data(sm->curve, L_ECC_POINT_TYPE_FULL, 816 ptr, nbytes * 2); 817 if (!sm->p_element) { 818 l_error("Server sent invalid P_Element during commit"); 819 return sae_reject(sm, SAE_STATE_COMMITTED, 820 MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP); 821 } 822 823 /* 824 * If they match those sent as part of the protocol instance's own 825 * SAE Commit message, the frame shall be silently discarded (because 826 * it is evidence of a reflection attack) and the t0 (retransmission) 827 * timer shall be set. 828 */ 829 if (l_ecc_scalars_are_equal(sm->p_scalar, sm->scalar) || 830 l_ecc_points_are_equal(sm->p_element, sm->element)) { 831 l_warn("peer scalar or element matched own, discarding frame"); 832 return -ENOMSG; 833 } 834 835 sm->sc++; 836 837 r = sae_calculate_keys(sm); 838 if (r != 0) 839 return r; 840 841 if (!sae_send_confirm(sm)) 842 return -EPROTO; 843 844 sm->state = SAE_STATE_CONFIRMED; 845 846 return 0; 847 } 848 849 static bool sae_verify_confirm(struct sae_sm *sm, const uint8_t *frame) 850 { 851 uint8_t check[SAE_MAX_HASH_LEN]; 852 uint16_t rc = l_get_le16(frame); 853 ssize_t r; 854 855 r = sae_cn(sm, rc, sm->p_scalar, sm->p_element, sm->scalar, 856 sm->element, check); 857 if (r < 0) 858 return false; 859 860 if (memcmp(frame + 2, check, r)) 861 return false; 862 863 sm->rc = rc; 864 865 return true; 866 } 867 868 static int sae_process_confirm(struct sae_sm *sm, const uint8_t *from, 869 const uint8_t *frame, size_t len) 870 { 871 const uint8_t *ptr = frame; 872 873 /* 874 * If processing is unsuccessful and the SAE Confirm message is not 875 * verified, protocol instance shall remain in Confirmed state. 876 * 877 * NOTE: We diverge from the protocol here and bail out early 878 */ 879 if (!sae_verify_confirm(sm, ptr)) { 880 l_error("SAE: Confirm could not be verified"); 881 return sae_reject(sm, SAE_STATE_CONFIRMED, 882 MMPDU_STATUS_CODE_UNSPECIFIED); 883 } 884 885 /* Sc shall be set to the value 2^16 - 1 */ 886 sm->sc = 0xffff; 887 888 handshake_state_set_pmkid(sm->handshake, sm->pmkid); 889 handshake_state_set_pmk(sm->handshake, sm->pmk, 32); 890 891 sm->state = SAE_STATE_ACCEPTED; 892 893 if (!sm->handshake->authenticator) { 894 sae_debug("Sending Associate to " 895 MAC, MAC_STR(sm->handshake->aa)); 896 sm->tx_assoc(sm->user_data); 897 } else { 898 if (!sae_send_confirm(sm)) 899 return -EPROTO; 900 } 901 902 return 0; 903 } 904 905 static bool sae_send_commit(struct sae_sm *sm, bool retry) 906 { 907 struct handshake_state *hs = sm->handshake; 908 /* regular commit + 3x IEs (257 bytes) + 6 bytes header */ 909 uint8_t commit[L_ECC_SCALAR_MAX_BYTES + L_ECC_POINT_MAX_BYTES + 777]; 910 int r; 911 912 r = sae_build_commit(sm, hs->spa, hs->aa, 913 commit, sizeof(commit), retry); 914 if (r < 0) 915 return false; 916 917 sae_debug("Sending Commit to "MAC, MAC_STR(hs->aa)); 918 919 sm->tx_auth(commit, r, sm->user_data); 920 921 return true; 922 } 923 924 static bool sae_assoc_timeout(struct auth_proto *ap) 925 { 926 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 927 928 if (sm->assoc_retry >= SAE_MAX_ASSOC_RETRY) 929 return false; 930 931 sm->assoc_retry++; 932 933 sae_debug("Retry Associate to "MAC, MAC_STR(sm->handshake->aa)); 934 935 sm->tx_assoc(sm->user_data); 936 937 return true; 938 } 939 940 /* 941 * 802.11-2016 - Section 12.4.8.6.4 942 * If the Status code is ANTI_CLOGGING_TOKEN_REQUIRED, a new SAE Commit message 943 * shall be constructed with the Anti-Clogging Token from the received 944 * Authentication frame, and the commit-scalar and COMMIT-ELEMENT previously 945 * sent. The new SAE Commit message shall be transmitted to the peer, Sync shall 946 * be zeroed, and the t0 (retransmission) timer shall be set. 947 */ 948 static int sae_process_anti_clogging(struct sae_sm *sm, const uint8_t *ptr, 949 size_t len) 950 { 951 /* 952 * 802.11 doesn't talk about validating the group of the Anti-Clogging 953 * Request message. We assume here that the group is something that 954 * we would have potentially sent 955 */ 956 if (len < 2) 957 return -EBADMSG; 958 959 if (sae_valid_group(sm, l_get_le16(ptr)) < 0) 960 return -EBADMSG; 961 962 len -= 2; 963 ptr += 2; 964 965 /* 966 * 802.11-2020, Table 9-41: 967 * When the hash-to-element method is used to derive the PWE, the 968 * Anti-Clogging Token Container element is present if the 969 * Status Code field is ANTI_CLOGGING_TOKEN_REQUIRED 970 */ 971 if (sm->sae_type != CRYPTO_SAE_LOOPING) { 972 if (len < 3) 973 return -EBADMSG; 974 975 if (ptr[0] != IE_TYPE_EXTENSION || ptr[2] != 93 || 976 ptr[1] < 2 || len < ptr[1] + 2u) 977 return -EBADMSG; 978 979 len = ptr[1] - 1; 980 ptr += 3; 981 } 982 983 /* 984 * IEEE 802.11-2016 - Section 12.4.6 Anti-clogging tokens 985 * 986 * "It is suggested that an Anti-Clogging Token not exceed 256 octets" 987 * 988 * Also ensure the token is at least 1 byte. The packet passed in will 989 * contain the group number, meaning the anti-clogging token length is 990 * going to be 2 bytes less than the passed in length. This is why we 991 * are checking 3 > len > 258. 992 */ 993 if (len < 1 || len > 256) { 994 l_error("anti-clogging token size invalid %zu", len); 995 return -EBADMSG; 996 } 997 998 sae_debug("Processed anti-clogging token"); 999 1000 l_free(sm->token); 1001 sm->token = l_memdup(ptr, len); 1002 sm->token_len = len; 1003 sm->sync = 0; 1004 1005 if (L_WARN_ON(!sae_send_commit(sm, true))) 1006 return -EPROTO; 1007 1008 return -EAGAIN; 1009 } 1010 1011 /* 1012 * 802.11-2016 - 12.4.8.6.3 Protocol instance behavior - Nothing state 1013 */ 1014 static int sae_verify_nothing(struct sae_sm *sm, uint16_t transaction, 1015 uint16_t status, const uint8_t *frame, 1016 size_t len) 1017 { 1018 /* 1019 * TODO: This does not handle the transition from NOTHING -> CONFIRMED 1020 * as this is only relevant to the AP or in Mesh mode which is not 1021 * yet supported. 1022 */ 1023 if (transaction != SAE_STATE_COMMITTED) 1024 return -EBADMSG; 1025 1026 /* frame shall be silently discarded and Del event sent */ 1027 if (status != 0) 1028 return -EBADMSG; 1029 1030 if (len < 2) 1031 return -EBADMSG; 1032 1033 /* reject with unsupported group */ 1034 if (l_get_le16(frame) != sm->group) 1035 return sae_reject(sm, SAE_STATE_COMMITTED, 1036 MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP); 1037 1038 return 0; 1039 } 1040 1041 /* 1042 * 802.11-2016 - 12.4.8.6.4 Protocol instance behavior - Committed state 1043 */ 1044 static int sae_verify_committed(struct sae_sm *sm, uint16_t transaction, 1045 uint16_t status, const uint8_t *frame, 1046 size_t len) 1047 { 1048 unsigned int skip; 1049 struct ie_tlv_iter iter; 1050 1051 if (sm->handshake->authenticator && 1052 transaction == SAE_STATE_CONFIRMED) { 1053 enum l_checksum_type hash = 1054 crypto_sae_hash_from_ecc_prime_len(sm->sae_type, 1055 l_ecc_curve_get_scalar_bytes(sm->curve)); 1056 size_t hash_len = l_checksum_digest_length(hash); 1057 1058 if (len < hash_len + 2) { 1059 l_error("SAE: Confirm packet too short"); 1060 return -EBADMSG; 1061 } 1062 1063 /* 1064 * TODO: Add extra functionality such as supporting 1065 * anti-clogging tokens and tracking rejected groups. Note 1066 * that the cryptographic confirm field value will be checked 1067 * at a later point. 1068 */ 1069 1070 return 0; 1071 } else if (transaction == SAE_STATE_CONFIRMED) { 1072 /* 1073 * Upon receipt of a Con event... 1074 * Then the protocol instance checks the value of Sync. If it 1075 * is greater than dot11RSNASAESync, the protocol instance 1076 * shall send a Del event to the parent process and transition 1077 * back to Nothing state. 1078 * If Sync is not greater than dot11RSNASAESync, the protocol 1079 * instance shall increment Sync, transmit the last SAE Commit 1080 * message sent to the peer... 1081 */ 1082 if (sm->sync > SAE_SYNC_MAX) 1083 return -ETIMEDOUT; 1084 1085 sm->sync++; 1086 1087 if (L_WARN_ON(!sae_send_commit(sm, true))) 1088 return -EPROTO; 1089 1090 return -EAGAIN; 1091 } 1092 1093 if (status == MMPDU_STATUS_CODE_ANTI_CLOGGING_TOKEN_REQ) 1094 return sae_process_anti_clogging(sm, frame, len); 1095 1096 if (status == MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP) { 1097 /* 1098 * TODO: hostapd in its current state does not include the 1099 * group number as it should. This is a violation of the spec, 1100 * but there isn't much we can do about it. We simply treat this 1101 * response as if its rejecting our last commit message (which 1102 * it most likely is). If/When this is fixed we should be 1103 * checking that the group matches here, e.g. 1104 * 1105 * if (l_get_le16(frame) != sm->group) 1106 * return false; 1107 * 1108 * According to 802.11 Section 12.4.8.6.4: 1109 * 1110 * "If the rejected group does not match the last offered group 1111 * the protocol instance shall silently discard the message and 1112 * set the t0 (retransmission) timer" 1113 */ 1114 if (len == 0) 1115 l_warn("AP did not include group number in response!"); 1116 else if (len >= 2 && (l_get_le16(frame) != sm->group)) 1117 return -ENOMSG; 1118 1119 sae_rejected_groups_append(sm, L_CPU_TO_LE16(sm->group)); 1120 1121 /* 1122 * "If the rejected group matches the last offered group, the 1123 * protocol instance shall choose a different group and generate 1124 * the PWE and the secret values according to 12.4.5.2; it then 1125 * generates and transmits a new SAE Commit message to the peer, 1126 * zeros Sync, sets the t0 (retransmission) timer, and remains 1127 * in Committed state" 1128 */ 1129 if (sae_choose_next_group(sm) < 0) { 1130 /* 1131 * "If there are no other groups to choose, the protocol 1132 * instance shall send a Del event to the parent process 1133 * and transitions back to Nothing state" 1134 */ 1135 sm->state = SAE_STATE_NOTHING; 1136 goto reject_unsupp_group; 1137 } 1138 1139 sae_debug("AP rejected group, trying again with group %u", 1140 sm->group); 1141 1142 sm->sync = 0; 1143 1144 if (L_WARN_ON(!sae_send_commit(sm, false))) 1145 return -EPROTO; 1146 1147 return -EAGAIN; 1148 } 1149 1150 /* 1151 * If the Status is some other nonzero value, the frame shall be 1152 * silently discarded and the t0 (retransmission) timer shall be set. 1153 */ 1154 switch (status) { 1155 case 0: 1156 case MMPDU_STATUS_CODE_SAE_HASH_TO_ELEMENT: 1157 if (status != sae_status_code(sm)) 1158 return -EBADMSG; 1159 break; 1160 case MMPDU_STATUS_CODE_UNKNOWN_PASSWORD_IDENTIFIER: 1161 sae_debug("Incorrect password identifier, check " 1162 "[Security].PasswordIdentifier"); 1163 /* fall through */ 1164 default: 1165 return -ENOMSG; 1166 } 1167 1168 if (len < 2) 1169 return -EBADMSG; 1170 1171 if (l_get_le16(frame) != sm->group) { 1172 l_error("SAE: Peer tried to change group -- Reject"); 1173 goto reject_unsupp_group; 1174 } 1175 1176 len -= 2; 1177 frame += 2; 1178 1179 skip = l_ecc_curve_get_scalar_bytes(sm->curve) * 3; 1180 if (len < skip) 1181 return -EBADMSG; 1182 1183 /* If H2E isn't being used, there should be no IEs in use */ 1184 if (status == 0) 1185 return 0; 1186 1187 len -= skip; 1188 frame += skip; 1189 1190 ie_tlv_iter_init(&iter, frame, len); 1191 1192 while (ie_tlv_iter_next(&iter)) { 1193 switch (ie_tlv_iter_get_tag(&iter)) { 1194 /* 1195 * If the peer's SAE Commit message contains a Rejected Groups 1196 * element, the list of rejected groups shall be checked to 1197 * ensure that all of the groups in the list are groups that 1198 * would be rejected. If any groups in the list would not be 1199 * rejected then processing of the SAE Commit message 1200 * terminates and the STA shall reject the peer's 1201 * authentication. 1202 * 1203 * NOTE: We currently only support the Initiator role, and so 1204 * do not reject any groups. We should never receive this 1205 * element 1206 */ 1207 case IE_TYPE_REJECTED_GROUPS: 1208 l_error("SAE: Unexpected Rejected Groups IE"); 1209 return sae_reject(sm, SAE_STATE_COMMITTED, 1210 MMPDU_STATUS_CODE_UNSPECIFIED); 1211 /* We don't request tokens, so we shouldn't get any */ 1212 case IE_TYPE_ANTI_CLOGGING_TOKEN_CONTAINER: 1213 l_error("SAE: Unexpected Anti-Clogging Container IE"); 1214 return sae_reject(sm, SAE_STATE_COMMITTED, 1215 MMPDU_STATUS_CODE_UNSPECIFIED); 1216 } 1217 } 1218 1219 return 0; 1220 1221 reject_unsupp_group: 1222 return sae_reject(sm, SAE_STATE_COMMITTED, 1223 MMPDU_STATUS_CODE_UNSUPP_FINITE_CYCLIC_GROUP); 1224 } 1225 1226 /* 1227 * 802.11-2016 - 12.4.8.6.5 Protocol instance behavior - Confirmed state 1228 */ 1229 static int sae_verify_confirmed(struct sae_sm *sm, uint16_t trans, 1230 uint16_t status, const uint8_t *frame, 1231 size_t len) 1232 { 1233 if (trans == SAE_STATE_CONFIRMED) { 1234 enum l_checksum_type hash = 1235 crypto_sae_hash_from_ecc_prime_len(sm->sae_type, 1236 l_ecc_curve_get_scalar_bytes(sm->curve)); 1237 size_t hash_len = l_checksum_digest_length(hash); 1238 1239 /* Most likely the password is wrong */ 1240 if (status == MMPDU_STATUS_CODE_UNSPECIFIED && len == 0) 1241 return -ENOKEY; 1242 1243 if (status != MMPDU_STATUS_CODE_SUCCESS) 1244 return -EPROTO; 1245 1246 if (len < hash_len + 2) { 1247 l_error("SAE: Confirm packet too short"); 1248 return -EBADMSG; 1249 } 1250 1251 return 0; 1252 } 1253 1254 /* 1255 * Upon receipt of a Com event, the t0 (retransmission) timer shall be 1256 * canceled. If the Status is nonzero, the frame shall be silently 1257 * discarded, the t0 (retransmission) timer set, and the protocol 1258 * instance shall remain in the Confirmed state. 1259 */ 1260 if (status != 0) 1261 return -ENOMSG; 1262 1263 /* 1264 * If Sync is greater than dot11RSNASAESync, the protocol instance 1265 * shall send the parent process a Del event and transitions back to 1266 * Nothing state. 1267 */ 1268 if (sm->sync > SAE_SYNC_MAX) 1269 return -ETIMEDOUT; 1270 1271 if (len < 2) 1272 return -EBADMSG; 1273 1274 /* frame shall be silently discarded */ 1275 if (l_get_le16(frame) != sm->group) 1276 return -EBADMSG; 1277 1278 /* 1279 * Because of kernel retransmit behavior on missed ACKs plus hostapd's 1280 * incorrect handling of confirm packets while in accepted state the 1281 * following can happen: 1282 * 1283 * 1. Client sends commit, not acked (committed state) 1284 * 2. AP receives commit, sends commit reply (committed state) 1285 * 3. Client retransmits original commit 1286 * 4. Client receives AP's commit, sends confirm (confirmed state) 1287 * 5. AP receives clients retransmitted commit, sends only commit 1288 * 6. AP receives clients confirm and accepts (accepted state) 1289 * 7. Client receives AP's commit and sends both commit + confirm 1290 * (the code below). 1291 * 8. AP receives clients commit while in accepted state, and deauths 1292 * 1293 * Due to this, any commit received while in a confirmed state will be 1294 * ignored by IWD since it is probably caused by this retransmission 1295 * and sending the commit/confirm below would likely cause hostapd to 1296 * deauth us. 1297 * 1298 * As for non-sta (currently not used) we want to keep with the spec. 1299 */ 1300 if (!sm->handshake->authenticator) 1301 return -EBADMSG; 1302 1303 /* 1304 * the protocol instance shall increment Sync, increment Sc, and 1305 * transmit its Commit and Confirm (with the new Sc value) messages. 1306 */ 1307 sm->sync++; 1308 sm->sc++; 1309 1310 if (L_WARN_ON(!sae_send_commit(sm, true))) 1311 return -EPROTO; 1312 1313 if (!sae_send_confirm(sm)) 1314 return -EPROTO; 1315 1316 return -EAGAIN; 1317 } 1318 1319 /* 1320 * 802.11-2016 - 12.4.8.6.6 Protocol instance behavior - Accepted state 1321 */ 1322 static int sae_verify_accepted(struct sae_sm *sm, uint16_t trans, 1323 uint16_t status, const uint8_t *frame, 1324 size_t len) 1325 { 1326 uint16_t sc; 1327 1328 /* 1329 * 12.4.8.6.1 Parent process behavior 1330 * 1331 * "Upon receipt of an SAE Commit message... and it is in Accepted 1332 * state, the scalar in the received frame is checked against the 1333 * peer-scalar used in authentication of the existing protocol instance 1334 * (in Accepted state). If it is identical, the frame shall be dropped" 1335 */ 1336 if (trans == SAE_STATE_COMMITTED) { 1337 bool drop; 1338 unsigned int nbytes = l_ecc_curve_get_scalar_bytes(sm->curve); 1339 struct l_ecc_scalar *p_scalar; 1340 1341 if (len < nbytes + 2) 1342 return -EMSGSIZE; 1343 1344 p_scalar = l_ecc_scalar_new(sm->curve, frame + 2, nbytes); 1345 1346 drop = l_ecc_scalars_are_equal(sm->p_scalar, p_scalar); 1347 l_ecc_scalar_free(p_scalar); 1348 1349 if (drop) 1350 return -EBADMSG; 1351 1352 l_error("received transaction %u in accepted state", trans); 1353 return -EPROTO; 1354 } 1355 1356 if (sm->sync > SAE_SYNC_MAX) 1357 return -ETIMEDOUT; 1358 1359 if (len < 2) 1360 return -EBADMSG; 1361 1362 sc = l_get_le16(frame); 1363 1364 /* 1365 * ... the value of send-confirm shall be checked. If the value is not 1366 * greater than Rc or is equal to 2^16 - 1, the received frame shall be 1367 * silently discarded. 1368 */ 1369 if (sc <= sm->rc || sc == 0xffff) 1370 return -EBADMSG; 1371 1372 /* 1373 * If the verification fails, the received frame shall be silently 1374 * discarded. 1375 */ 1376 if (!sae_verify_confirm(sm, frame)) 1377 return -EBADMSG; 1378 1379 /* 1380 * If the verification succeeds, the Rc variable shall be set to the 1381 * send-confirm portion of the frame, the Sync shall be incremented and 1382 * a new SAE Confirm message shall be constructed (with Sc set to 1383 * 2^16 - 1) and sent to the peer. 1384 */ 1385 sm->sync++; 1386 sm->sc = 0xffff; 1387 1388 if (!sae_send_confirm(sm)) 1389 return -EPROTO; 1390 1391 return -EAGAIN; 1392 } 1393 1394 static const char *sae_state_to_str(enum sae_state state) 1395 { 1396 switch (state) { 1397 case SAE_STATE_NOTHING: 1398 return "nothing"; 1399 case SAE_STATE_COMMITTED: 1400 return "committed"; 1401 case SAE_STATE_CONFIRMED: 1402 return "confirmed"; 1403 case SAE_STATE_ACCEPTED: 1404 return "accepted"; 1405 } 1406 1407 return "unknown"; 1408 } 1409 1410 static int sae_verify_packet(struct sae_sm *sm, uint16_t trans, 1411 uint16_t status, const uint8_t *frame, 1412 size_t len) 1413 { 1414 if (trans != SAE_STATE_COMMITTED && trans != SAE_STATE_CONFIRMED) 1415 return -EBADMSG; 1416 1417 switch (sm->state) { 1418 case SAE_STATE_NOTHING: 1419 return sae_verify_nothing(sm, trans, status, frame, len); 1420 case SAE_STATE_COMMITTED: 1421 return sae_verify_committed(sm, trans, status, frame, len); 1422 case SAE_STATE_CONFIRMED: 1423 return sae_verify_confirmed(sm, trans, status, frame, len); 1424 case SAE_STATE_ACCEPTED: 1425 return sae_verify_accepted(sm, trans, status, frame, len); 1426 } 1427 1428 /* should never get here */ 1429 return -EPROTO; 1430 } 1431 1432 static int sae_rx_authenticate(struct auth_proto *ap, 1433 const uint8_t *frame, size_t len) 1434 { 1435 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1436 const struct mmpdu_header *hdr = (const struct mmpdu_header *) frame; 1437 const struct mmpdu_authentication *auth = mmpdu_body(hdr); 1438 int ret; 1439 uint16_t transaction = L_LE16_TO_CPU(auth->transaction_sequence); 1440 uint16_t status = L_LE16_TO_CPU(auth->status); 1441 1442 sae_debug("Received frame transaction=%u status=%u state=%s", 1443 transaction, status, sae_state_to_str(sm->state)); 1444 1445 len -= mmpdu_header_len(hdr); 1446 1447 ret = sae_verify_packet(sm, transaction, status, auth->ies, len - 6); 1448 if (ret != 0) { 1449 if (ret < 0 && ret != -EAGAIN) 1450 sae_debug("Frame did not verify (%s)", strerror(-ret)); 1451 1452 return ret; 1453 } 1454 1455 switch (transaction) { 1456 case SAE_STATE_COMMITTED: 1457 return sae_process_commit(sm, hdr->address_2, auth->ies, 1458 len - 6); 1459 case SAE_STATE_CONFIRMED: 1460 return sae_process_confirm(sm, hdr->address_2, auth->ies, 1461 len - 6); 1462 default: 1463 l_error("invalid transaction sequence %u", transaction); 1464 } 1465 1466 /* should never get here */ 1467 return -EPROTO; 1468 } 1469 1470 static int sae_rx_associate(struct auth_proto *ap, const uint8_t *frame, 1471 size_t len) 1472 { 1473 const struct mmpdu_header *mpdu = (const struct mmpdu_header *)frame; 1474 const struct mmpdu_association_response *body = mmpdu_body(mpdu); 1475 1476 if (body->status_code != 0) 1477 return -EPROTO; 1478 1479 return 0; 1480 } 1481 1482 static bool sae_start(struct auth_proto *ap) 1483 { 1484 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1485 1486 if (sm->handshake->authenticator) 1487 memcpy(sm->peer, sm->handshake->spa, 6); 1488 else 1489 memcpy(sm->peer, sm->handshake->aa, 6); 1490 1491 if (sm->sae_type == CRYPTO_SAE_LOOPING && !sm->handshake->passphrase) { 1492 l_error("SAE: No passphrase set"); 1493 return false; 1494 } 1495 1496 if (sae_choose_next_group(sm) < 0) 1497 return false; 1498 1499 sm->state = SAE_STATE_COMMITTED; 1500 return sae_send_commit(sm, false); 1501 } 1502 1503 bool sae_sm_is_h2e(struct auth_proto *ap) 1504 { 1505 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1506 1507 return sm->sae_type != CRYPTO_SAE_LOOPING; 1508 } 1509 1510 static void sae_free(struct auth_proto *ap) 1511 { 1512 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1513 1514 sae_reset_state(sm); 1515 1516 l_free(sm->token); 1517 sm->token = NULL; 1518 1519 if (sm->rejected_groups) 1520 free(sm->rejected_groups); 1521 1522 /* zero out whole structure, including keys */ 1523 explicit_bzero(sm, sizeof(struct sae_sm)); 1524 1525 l_free(sm); 1526 } 1527 1528 struct auth_proto *sae_sm_new(struct handshake_state *hs, 1529 sae_tx_authenticate_func_t tx_auth, 1530 sae_tx_associate_func_t tx_assoc, 1531 void *user_data) 1532 { 1533 struct sae_sm *sm; 1534 const void *rsnxe; 1535 1536 sm = l_new(struct sae_sm, 1); 1537 1538 sm->group_retry = -1; 1539 1540 sm->tx_auth = tx_auth; 1541 sm->tx_assoc = tx_assoc; 1542 sm->user_data = user_data; 1543 sm->handshake = hs; 1544 sm->state = SAE_STATE_NOTHING; 1545 sm->force_default_group = hs->force_default_ecc_group; 1546 1547 sm->ap.start = sae_start; 1548 sm->ap.free = sae_free; 1549 sm->ap.rx_authenticate = sae_rx_authenticate; 1550 sm->ap.rx_associate = sae_rx_associate; 1551 sm->ap.assoc_timeout = sae_assoc_timeout; 1552 1553 rsnxe = hs->authenticator ? hs->supplicant_rsnxe : 1554 hs->authenticator_rsnxe; 1555 1556 if (ie_rsnxe_capable(rsnxe, IE_RSNX_SAE_H2E) && hs->ecc_sae_pts) { 1557 sae_debug("Using SAE H2E"); 1558 sm->sae_type = CRYPTO_SAE_HASH_TO_ELEMENT; 1559 } else { 1560 sae_debug("Using SAE Hunting and Pecking"); 1561 sm->sae_type = CRYPTO_SAE_LOOPING; 1562 } 1563 1564 return &sm->ap; 1565 } 1566 1567 bool sae_sm_force_hunt_and_peck(struct auth_proto *ap) 1568 { 1569 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1570 1571 sae_debug("Forcing SAE Hunting and Pecking"); 1572 sm->sae_type = CRYPTO_SAE_LOOPING; 1573 1574 return true; 1575 } 1576 1577 bool sae_sm_force_default_group(struct auth_proto *ap) 1578 { 1579 struct sae_sm *sm = l_container_of(ap, struct sae_sm, ap); 1580 1581 sae_debug("Forcing Default Group"); 1582 sm->force_default_group = true; 1583 1584 return true; 1585 } 1586 1587 static int sae_init(void) 1588 { 1589 if (getenv("IWD_SAE_DEBUG")) 1590 debug = true; 1591 1592 return 0; 1593 } 1594 1595 static void sae_exit(void) 1596 { 1597 } 1598 1599 IWD_MODULE(sae, sae_init, sae_exit);