1 /* $OpenBSD: ieee80211_crypto.c,v 1.75 2019/08/16 19:53:32 procter Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/mbuf.h> 22 #include <sys/malloc.h> 23 #include <sys/kernel.h> 24 #include <sys/socket.h> 25 #include <sys/sockio.h> 26 #include <sys/endian.h> 27 #include <sys/errno.h> 28 #include <sys/sysctl.h> 29 30 #include <net/if.h> 31 #include <net/if_dl.h> 32 #include <net/if_media.h> 33 34 #include <netinet/in.h> 35 #include <netinet/if_ether.h> 36 37 #include <net80211/ieee80211_var.h> 38 #include <net80211/ieee80211_priv.h> 39 40 #include <crypto/arc4.h> 41 #include <crypto/md5.h> 42 #include <crypto/sha1.h> 43 #include <crypto/sha2.h> 44 #include <crypto/hmac.h> 45 #include <crypto/aes.h> 46 #include <crypto/cmac.h> 47 #include <crypto/key_wrap.h> 48 49 void ieee80211_prf(const u_int8_t *, size_t, const u_int8_t *, size_t, 50 const u_int8_t *, size_t, u_int8_t *, size_t); 51 void ieee80211_kdf(const u_int8_t *, size_t, const u_int8_t *, size_t, 52 const u_int8_t *, size_t, u_int8_t *, size_t); 53 void ieee80211_derive_pmkid(enum ieee80211_akm, const u_int8_t *, 54 const u_int8_t *, const u_int8_t *, u_int8_t *); 55 56 void 57 ieee80211_crypto_attach(struct ifnet *ifp) 58 { 59 struct ieee80211com *ic = (void *)ifp; 60 61 TAILQ_INIT(&ic->ic_pmksa); 62 if (ic->ic_caps & IEEE80211_C_RSN) { 63 ic->ic_rsnprotos = IEEE80211_PROTO_RSN; 64 ic->ic_rsnakms = IEEE80211_AKM_PSK; 65 ic->ic_rsnciphers = IEEE80211_CIPHER_CCMP; 66 ic->ic_rsngroupcipher = IEEE80211_CIPHER_CCMP; 67 ic->ic_rsngroupmgmtcipher = IEEE80211_CIPHER_BIP; 68 } 69 ic->ic_set_key = ieee80211_set_key; 70 ic->ic_delete_key = ieee80211_delete_key; 71 #ifndef IEEE80211_STA_ONLY 72 timeout_set(&ic->ic_tkip_micfail_timeout, 73 ieee80211_michael_mic_failure_timeout, ic); 74 #endif 75 } 76 77 void 78 ieee80211_crypto_detach(struct ifnet *ifp) 79 { 80 struct ieee80211com *ic = (void *)ifp; 81 struct ieee80211_pmk *pmk; 82 83 /* purge the PMKSA cache */ 84 while ((pmk = TAILQ_FIRST(&ic->ic_pmksa)) != NULL) { 85 TAILQ_REMOVE(&ic->ic_pmksa, pmk, pmk_next); 86 explicit_bzero(pmk, sizeof(*pmk)); 87 free(pmk, M_DEVBUF, sizeof(*pmk)); 88 } 89 90 /* clear all group keys from memory */ 91 ieee80211_crypto_clear_groupkeys(ic); 92 93 /* clear pre-shared key from memory */ 94 explicit_bzero(ic->ic_psk, IEEE80211_PMK_LEN); 95 96 #ifndef IEEE80211_STA_ONLY 97 timeout_del(&ic->ic_tkip_micfail_timeout); 98 #endif 99 } 100 101 void 102 ieee80211_crypto_clear_groupkeys(struct ieee80211com *ic) 103 { 104 int i; 105 106 for (i = 0; i < IEEE80211_GROUP_NKID; i++) { 107 struct ieee80211_key *k = &ic->ic_nw_keys[i]; 108 if (k->k_cipher != IEEE80211_CIPHER_NONE) 109 (*ic->ic_delete_key)(ic, NULL, k); 110 explicit_bzero(k, sizeof(*k)); 111 } 112 } 113 114 /* 115 * Return the length in bytes of a cipher suite key (see Table 60). 116 */ 117 int 118 ieee80211_cipher_keylen(enum ieee80211_cipher cipher) 119 { 120 switch (cipher) { 121 case IEEE80211_CIPHER_WEP40: 122 return 5; 123 case IEEE80211_CIPHER_TKIP: 124 return 32; 125 case IEEE80211_CIPHER_CCMP: 126 return 16; 127 case IEEE80211_CIPHER_WEP104: 128 return 13; 129 case IEEE80211_CIPHER_BIP: 130 return 16; 131 default: /* unknown cipher */ 132 return 0; 133 } 134 } 135 136 int 137 ieee80211_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 138 struct ieee80211_key *k) 139 { 140 int error; 141 142 switch (k->k_cipher) { 143 case IEEE80211_CIPHER_WEP40: 144 case IEEE80211_CIPHER_WEP104: 145 error = ieee80211_wep_set_key(ic, k); 146 break; 147 case IEEE80211_CIPHER_TKIP: 148 error = ieee80211_tkip_set_key(ic, k); 149 break; 150 case IEEE80211_CIPHER_CCMP: 151 error = ieee80211_ccmp_set_key(ic, k); 152 break; 153 case IEEE80211_CIPHER_BIP: 154 error = ieee80211_bip_set_key(ic, k); 155 break; 156 default: 157 /* should not get there */ 158 error = EINVAL; 159 } 160 161 if (error == 0) 162 k->k_flags |= IEEE80211_KEY_SWCRYPTO; 163 164 return error; 165 } 166 167 void 168 ieee80211_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni, 169 struct ieee80211_key *k) 170 { 171 switch (k->k_cipher) { 172 case IEEE80211_CIPHER_WEP40: 173 case IEEE80211_CIPHER_WEP104: 174 ieee80211_wep_delete_key(ic, k); 175 break; 176 case IEEE80211_CIPHER_TKIP: 177 ieee80211_tkip_delete_key(ic, k); 178 break; 179 case IEEE80211_CIPHER_CCMP: 180 ieee80211_ccmp_delete_key(ic, k); 181 break; 182 case IEEE80211_CIPHER_BIP: 183 ieee80211_bip_delete_key(ic, k); 184 break; 185 default: 186 /* should not get there */ 187 break; 188 } 189 explicit_bzero(k, sizeof(*k)); 190 } 191 192 struct ieee80211_key * 193 ieee80211_get_txkey(struct ieee80211com *ic, const struct ieee80211_frame *wh, 194 struct ieee80211_node *ni) 195 { 196 int kid; 197 198 if ((ic->ic_flags & IEEE80211_F_RSNON) && 199 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 200 ni->ni_rsncipher != IEEE80211_CIPHER_USEGROUP) 201 return &ni->ni_pairwise_key; 202 203 /* All other cases (including WEP) use a group key. */ 204 if (ni->ni_flags & IEEE80211_NODE_MFP) 205 kid = ic->ic_igtk_kid; 206 else 207 kid = ic->ic_def_txkey; 208 209 return &ic->ic_nw_keys[kid]; 210 } 211 212 struct mbuf * 213 ieee80211_encrypt(struct ieee80211com *ic, struct mbuf *m0, 214 struct ieee80211_key *k) 215 { 216 if ((k->k_flags & IEEE80211_KEY_SWCRYPTO) == 0) 217 panic("%s: key unset for sw crypto: %d", __func__, k->k_id); 218 219 switch (k->k_cipher) { 220 case IEEE80211_CIPHER_WEP40: 221 case IEEE80211_CIPHER_WEP104: 222 m0 = ieee80211_wep_encrypt(ic, m0, k); 223 break; 224 case IEEE80211_CIPHER_TKIP: 225 m0 = ieee80211_tkip_encrypt(ic, m0, k); 226 break; 227 case IEEE80211_CIPHER_CCMP: 228 m0 = ieee80211_ccmp_encrypt(ic, m0, k); 229 break; 230 case IEEE80211_CIPHER_BIP: 231 m0 = ieee80211_bip_encap(ic, m0, k); 232 break; 233 default: 234 /* should not get there */ 235 panic("invalid key cipher 0x%x", k->k_cipher); 236 } 237 return m0; 238 } 239 240 struct mbuf * 241 ieee80211_decrypt(struct ieee80211com *ic, struct mbuf *m0, 242 struct ieee80211_node *ni) 243 { 244 struct ieee80211_frame *wh; 245 struct ieee80211_key *k; 246 u_int8_t *ivp, *mmie; 247 u_int16_t kid; 248 int hdrlen; 249 250 /* find key for decryption */ 251 wh = mtod(m0, struct ieee80211_frame *); 252 if ((ic->ic_flags & IEEE80211_F_RSNON) && 253 !IEEE80211_IS_MULTICAST(wh->i_addr1) && 254 ni->ni_rsncipher != IEEE80211_CIPHER_USEGROUP) { 255 k = &ni->ni_pairwise_key; 256 257 } else if (!IEEE80211_IS_MULTICAST(wh->i_addr1) || 258 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != 259 IEEE80211_FC0_TYPE_MGT) { 260 /* retrieve group data key id from IV field */ 261 hdrlen = ieee80211_get_hdrlen(wh); 262 /* check that IV field is present */ 263 if (m0->m_len < hdrlen + 4) { 264 m_freem(m0); 265 return NULL; 266 } 267 ivp = (u_int8_t *)wh + hdrlen; 268 kid = ivp[3] >> 6; 269 k = &ic->ic_nw_keys[kid]; 270 } else { 271 /* retrieve integrity group key id from MMIE */ 272 if (m0->m_len < sizeof(*wh) + IEEE80211_MMIE_LEN) { 273 m_freem(m0); 274 return NULL; 275 } 276 /* it is assumed management frames are contiguous */ 277 mmie = (u_int8_t *)wh + m0->m_len - IEEE80211_MMIE_LEN; 278 /* check that MMIE is valid */ 279 if (mmie[0] != IEEE80211_ELEMID_MMIE || mmie[1] != 16) { 280 m_freem(m0); 281 return NULL; 282 } 283 kid = LE_READ_2(&mmie[2]); 284 if (kid != 4 && kid != 5) { 285 m_freem(m0); 286 return NULL; 287 } 288 k = &ic->ic_nw_keys[kid]; 289 } 290 291 if ((k->k_flags & IEEE80211_KEY_SWCRYPTO) == 0) { 292 m_free(m0); 293 return NULL; 294 } 295 296 switch (k->k_cipher) { 297 case IEEE80211_CIPHER_WEP40: 298 case IEEE80211_CIPHER_WEP104: 299 m0 = ieee80211_wep_decrypt(ic, m0, k); 300 break; 301 case IEEE80211_CIPHER_TKIP: 302 m0 = ieee80211_tkip_decrypt(ic, m0, k); 303 break; 304 case IEEE80211_CIPHER_CCMP: 305 m0 = ieee80211_ccmp_decrypt(ic, m0, k); 306 break; 307 case IEEE80211_CIPHER_BIP: 308 m0 = ieee80211_bip_decap(ic, m0, k); 309 break; 310 default: 311 /* key not defined */ 312 m_freem(m0); 313 m0 = NULL; 314 } 315 return m0; 316 } 317 318 /* 319 * SHA1-based Pseudo-Random Function (see 8.5.1.1). 320 */ 321 void 322 ieee80211_prf(const u_int8_t *key, size_t key_len, const u_int8_t *label, 323 size_t label_len, const u_int8_t *context, size_t context_len, 324 u_int8_t *output, size_t len) 325 { 326 HMAC_SHA1_CTX ctx; 327 u_int8_t digest[SHA1_DIGEST_LENGTH]; 328 u_int8_t count; 329 330 for (count = 0; len != 0; count++) { 331 HMAC_SHA1_Init(&ctx, key, key_len); 332 HMAC_SHA1_Update(&ctx, label, label_len); 333 HMAC_SHA1_Update(&ctx, context, context_len); 334 HMAC_SHA1_Update(&ctx, &count, 1); 335 if (len < SHA1_DIGEST_LENGTH) { 336 HMAC_SHA1_Final(digest, &ctx); 337 /* truncate HMAC-SHA1 to len bytes */ 338 memcpy(output, digest, len); 339 break; 340 } 341 HMAC_SHA1_Final(output, &ctx); 342 output += SHA1_DIGEST_LENGTH; 343 len -= SHA1_DIGEST_LENGTH; 344 } 345 } 346 347 /* 348 * SHA256-based Key Derivation Function (see 8.5.1.5.2). 349 */ 350 void 351 ieee80211_kdf(const u_int8_t *key, size_t key_len, const u_int8_t *label, 352 size_t label_len, const u_int8_t *context, size_t context_len, 353 u_int8_t *output, size_t len) 354 { 355 HMAC_SHA256_CTX ctx; 356 u_int8_t digest[SHA256_DIGEST_LENGTH]; 357 u_int16_t i, iter, length; 358 359 length = htole16(len * NBBY); 360 for (i = 1; len != 0; i++) { 361 HMAC_SHA256_Init(&ctx, key, key_len); 362 iter = htole16(i); 363 HMAC_SHA256_Update(&ctx, (u_int8_t *)&iter, sizeof iter); 364 HMAC_SHA256_Update(&ctx, label, label_len); 365 HMAC_SHA256_Update(&ctx, context, context_len); 366 HMAC_SHA256_Update(&ctx, (u_int8_t *)&length, sizeof length); 367 if (len < SHA256_DIGEST_LENGTH) { 368 HMAC_SHA256_Final(digest, &ctx); 369 /* truncate HMAC-SHA-256 to len bytes */ 370 memcpy(output, digest, len); 371 break; 372 } 373 HMAC_SHA256_Final(output, &ctx); 374 output += SHA256_DIGEST_LENGTH; 375 len -= SHA256_DIGEST_LENGTH; 376 } 377 } 378 379 /* 380 * Derive Pairwise Transient Key (PTK) (see 8.5.1.2). 381 */ 382 void 383 ieee80211_derive_ptk(enum ieee80211_akm akm, const u_int8_t *pmk, 384 const u_int8_t *aa, const u_int8_t *spa, const u_int8_t *anonce, 385 const u_int8_t *snonce, struct ieee80211_ptk *ptk) 386 { 387 void (*kdf)(const u_int8_t *, size_t, const u_int8_t *, size_t, 388 const u_int8_t *, size_t, u_int8_t *, size_t); 389 u_int8_t buf[2 * IEEE80211_ADDR_LEN + 2 * EAPOL_KEY_NONCE_LEN]; 390 int ret; 391 392 /* Min(AA,SPA) || Max(AA,SPA) */ 393 ret = memcmp(aa, spa, IEEE80211_ADDR_LEN) < 0; 394 memcpy(&buf[ 0], ret ? aa : spa, IEEE80211_ADDR_LEN); 395 memcpy(&buf[ 6], ret ? spa : aa, IEEE80211_ADDR_LEN); 396 397 /* Min(ANonce,SNonce) || Max(ANonce,SNonce) */ 398 ret = memcmp(anonce, snonce, EAPOL_KEY_NONCE_LEN) < 0; 399 memcpy(&buf[12], ret ? anonce : snonce, EAPOL_KEY_NONCE_LEN); 400 memcpy(&buf[44], ret ? snonce : anonce, EAPOL_KEY_NONCE_LEN); 401 402 kdf = ieee80211_is_sha256_akm(akm) ? ieee80211_kdf : ieee80211_prf; 403 (*kdf)(pmk, IEEE80211_PMK_LEN, "Pairwise key expansion", 23, 404 buf, sizeof buf, (u_int8_t *)ptk, sizeof(*ptk)); 405 } 406 407 static void 408 ieee80211_pmkid_sha1(const u_int8_t *pmk, const u_int8_t *aa, 409 const u_int8_t *spa, u_int8_t *pmkid) 410 { 411 HMAC_SHA1_CTX ctx; 412 u_int8_t digest[SHA1_DIGEST_LENGTH]; 413 414 HMAC_SHA1_Init(&ctx, pmk, IEEE80211_PMK_LEN); 415 HMAC_SHA1_Update(&ctx, "PMK Name", 8); 416 HMAC_SHA1_Update(&ctx, aa, IEEE80211_ADDR_LEN); 417 HMAC_SHA1_Update(&ctx, spa, IEEE80211_ADDR_LEN); 418 HMAC_SHA1_Final(digest, &ctx); 419 /* use the first 128 bits of HMAC-SHA1 */ 420 memcpy(pmkid, digest, IEEE80211_PMKID_LEN); 421 } 422 423 static void 424 ieee80211_pmkid_sha256(const u_int8_t *pmk, const u_int8_t *aa, 425 const u_int8_t *spa, u_int8_t *pmkid) 426 { 427 HMAC_SHA256_CTX ctx; 428 u_int8_t digest[SHA256_DIGEST_LENGTH]; 429 430 HMAC_SHA256_Init(&ctx, pmk, IEEE80211_PMK_LEN); 431 HMAC_SHA256_Update(&ctx, "PMK Name", 8); 432 HMAC_SHA256_Update(&ctx, aa, IEEE80211_ADDR_LEN); 433 HMAC_SHA256_Update(&ctx, spa, IEEE80211_ADDR_LEN); 434 HMAC_SHA256_Final(digest, &ctx); 435 /* use the first 128 bits of HMAC-SHA-256 */ 436 memcpy(pmkid, digest, IEEE80211_PMKID_LEN); 437 } 438 439 /* 440 * Derive Pairwise Master Key Identifier (PMKID) (see 8.5.1.2). 441 */ 442 void 443 ieee80211_derive_pmkid(enum ieee80211_akm akm, const u_int8_t *pmk, 444 const u_int8_t *aa, const u_int8_t *spa, u_int8_t *pmkid) 445 { 446 if (ieee80211_is_sha256_akm(akm)) 447 ieee80211_pmkid_sha256(pmk, aa, spa, pmkid); 448 else 449 ieee80211_pmkid_sha1(pmk, aa, spa, pmkid); 450 } 451 452 typedef union _ANY_CTX { 453 HMAC_MD5_CTX md5; 454 HMAC_SHA1_CTX sha1; 455 AES_CMAC_CTX cmac; 456 } ANY_CTX; 457 458 /* 459 * Compute the Key MIC field of an EAPOL-Key frame using the specified Key 460 * Confirmation Key (KCK). The hash function can be HMAC-MD5, HMAC-SHA1 461 * or AES-128-CMAC depending on the EAPOL-Key Key Descriptor Version. 462 */ 463 void 464 ieee80211_eapol_key_mic(struct ieee80211_eapol_key *key, const u_int8_t *kck) 465 { 466 u_int8_t digest[SHA1_DIGEST_LENGTH]; 467 ANY_CTX ctx; /* XXX off stack? */ 468 u_int len; 469 470 len = BE_READ_2(key->len) + 4; 471 472 switch (BE_READ_2(key->info) & EAPOL_KEY_VERSION_MASK) { 473 case EAPOL_KEY_DESC_V1: 474 HMAC_MD5_Init(&ctx.md5, kck, 16); 475 HMAC_MD5_Update(&ctx.md5, (u_int8_t *)key, len); 476 HMAC_MD5_Final(key->mic, &ctx.md5); 477 break; 478 case EAPOL_KEY_DESC_V2: 479 HMAC_SHA1_Init(&ctx.sha1, kck, 16); 480 HMAC_SHA1_Update(&ctx.sha1, (u_int8_t *)key, len); 481 HMAC_SHA1_Final(digest, &ctx.sha1); 482 /* truncate HMAC-SHA1 to its 128 MSBs */ 483 memcpy(key->mic, digest, EAPOL_KEY_MIC_LEN); 484 break; 485 case EAPOL_KEY_DESC_V3: 486 AES_CMAC_Init(&ctx.cmac); 487 AES_CMAC_SetKey(&ctx.cmac, kck); 488 AES_CMAC_Update(&ctx.cmac, (u_int8_t *)key, len); 489 AES_CMAC_Final(key->mic, &ctx.cmac); 490 break; 491 } 492 } 493 494 /* 495 * Check the MIC of a received EAPOL-Key frame using the specified Key 496 * Confirmation Key (KCK). 497 */ 498 int 499 ieee80211_eapol_key_check_mic(struct ieee80211_eapol_key *key, 500 const u_int8_t *kck) 501 { 502 u_int8_t mic[EAPOL_KEY_MIC_LEN]; 503 504 memcpy(mic, key->mic, EAPOL_KEY_MIC_LEN); 505 memset(key->mic, 0, EAPOL_KEY_MIC_LEN); 506 ieee80211_eapol_key_mic(key, kck); 507 508 return timingsafe_bcmp(key->mic, mic, EAPOL_KEY_MIC_LEN) != 0; 509 } 510 511 #ifndef IEEE80211_STA_ONLY 512 /* 513 * Encrypt the Key Data field of an EAPOL-Key frame using the specified Key 514 * Encryption Key (KEK). The encryption algorithm can be either ARC4 or 515 * AES Key Wrap depending on the EAPOL-Key Key Descriptor Version. 516 */ 517 void 518 ieee80211_eapol_key_encrypt(struct ieee80211com *ic, 519 struct ieee80211_eapol_key *key, const u_int8_t *kek) 520 { 521 union { 522 struct rc4_ctx rc4; 523 aes_key_wrap_ctx aes; 524 } ctx; /* XXX off stack? */ 525 u_int8_t keybuf[EAPOL_KEY_IV_LEN + 16]; 526 u_int16_t len, info; 527 u_int8_t *data; 528 int n; 529 530 len = BE_READ_2(key->paylen); 531 info = BE_READ_2(key->info); 532 data = (u_int8_t *)(key + 1); 533 534 switch (info & EAPOL_KEY_VERSION_MASK) { 535 case EAPOL_KEY_DESC_V1: 536 /* set IV to the lower 16 octets of our global key counter */ 537 memcpy(key->iv, ic->ic_globalcnt + 16, 16); 538 /* increment our global key counter (256-bit, big-endian) */ 539 for (n = 31; n >= 0 && ++ic->ic_globalcnt[n] == 0; n--); 540 541 /* concatenate the EAPOL-Key IV field and the KEK */ 542 memcpy(keybuf, key->iv, EAPOL_KEY_IV_LEN); 543 memcpy(keybuf + EAPOL_KEY_IV_LEN, kek, 16); 544 545 rc4_keysetup(&ctx.rc4, keybuf, sizeof keybuf); 546 /* discard the first 256 octets of the ARC4 key stream */ 547 rc4_skip(&ctx.rc4, RC4STATE); 548 rc4_crypt(&ctx.rc4, data, data, len); 549 break; 550 case EAPOL_KEY_DESC_V2: 551 case EAPOL_KEY_DESC_V3: 552 if (len < 16 || (len & 7) != 0) { 553 /* insert padding */ 554 n = (len < 16) ? 16 - len : 8 - (len & 7); 555 data[len++] = IEEE80211_ELEMID_VENDOR; 556 memset(&data[len], 0, n - 1); 557 len += n - 1; 558 } 559 aes_key_wrap_set_key_wrap_only(&ctx.aes, kek, 16); 560 aes_key_wrap(&ctx.aes, data, len / 8, data); 561 len += 8; /* AES Key Wrap adds 8 bytes */ 562 /* update key data length */ 563 BE_WRITE_2(key->paylen, len); 564 /* update packet body length */ 565 BE_WRITE_2(key->len, sizeof(*key) + len - 4); 566 break; 567 } 568 } 569 #endif /* IEEE80211_STA_ONLY */ 570 571 /* 572 * Decrypt the Key Data field of an EAPOL-Key frame using the specified Key 573 * Encryption Key (KEK). The encryption algorithm can be either ARC4 or 574 * AES Key Wrap depending on the EAPOL-Key Key Descriptor Version. 575 */ 576 int 577 ieee80211_eapol_key_decrypt(struct ieee80211_eapol_key *key, 578 const u_int8_t *kek) 579 { 580 union { 581 struct rc4_ctx rc4; 582 aes_key_wrap_ctx aes; 583 } ctx; /* XXX off stack? */ 584 u_int8_t keybuf[EAPOL_KEY_IV_LEN + 16]; 585 u_int16_t len, info; 586 u_int8_t *data; 587 588 len = BE_READ_2(key->paylen); 589 info = BE_READ_2(key->info); 590 data = (u_int8_t *)(key + 1); 591 592 switch (info & EAPOL_KEY_VERSION_MASK) { 593 case EAPOL_KEY_DESC_V1: 594 /* concatenate the EAPOL-Key IV field and the KEK */ 595 memcpy(keybuf, key->iv, EAPOL_KEY_IV_LEN); 596 memcpy(keybuf + EAPOL_KEY_IV_LEN, kek, 16); 597 598 rc4_keysetup(&ctx.rc4, keybuf, sizeof keybuf); 599 /* discard the first 256 octets of the ARC4 key stream */ 600 rc4_skip(&ctx.rc4, RC4STATE); 601 rc4_crypt(&ctx.rc4, data, data, len); 602 return 0; 603 case EAPOL_KEY_DESC_V2: 604 case EAPOL_KEY_DESC_V3: 605 /* Key Data Length must be a multiple of 8 */ 606 if (len < 16 + 8 || (len & 7) != 0) 607 return 1; 608 len -= 8; /* AES Key Wrap adds 8 bytes */ 609 aes_key_wrap_set_key(&ctx.aes, kek, 16); 610 return aes_key_unwrap(&ctx.aes, data, data, len / 8); 611 } 612 613 return 1; /* unknown Key Descriptor Version */ 614 } 615 616 /* 617 * Add a PMK entry to the PMKSA cache. 618 */ 619 struct ieee80211_pmk * 620 ieee80211_pmksa_add(struct ieee80211com *ic, enum ieee80211_akm akm, 621 const u_int8_t *macaddr, const u_int8_t *key, u_int32_t lifetime) 622 { 623 struct ieee80211_pmk *pmk; 624 625 /* check if an entry already exists for this (STA,AKMP) */ 626 TAILQ_FOREACH(pmk, &ic->ic_pmksa, pmk_next) { 627 if (pmk->pmk_akm == akm && 628 IEEE80211_ADDR_EQ(pmk->pmk_macaddr, macaddr)) 629 break; 630 } 631 if (pmk == NULL) { 632 /* allocate a new PMKSA entry */ 633 if ((pmk = malloc(sizeof(*pmk), M_DEVBUF, M_NOWAIT)) == NULL) 634 return NULL; 635 pmk->pmk_akm = akm; 636 IEEE80211_ADDR_COPY(pmk->pmk_macaddr, macaddr); 637 TAILQ_INSERT_TAIL(&ic->ic_pmksa, pmk, pmk_next); 638 } 639 memcpy(pmk->pmk_key, key, IEEE80211_PMK_LEN); 640 pmk->pmk_lifetime = lifetime; /* XXX not used yet */ 641 #ifndef IEEE80211_STA_ONLY 642 if (ic->ic_opmode == IEEE80211_M_HOSTAP) { 643 ieee80211_derive_pmkid(pmk->pmk_akm, pmk->pmk_key, 644 ic->ic_myaddr, macaddr, pmk->pmk_pmkid); 645 } else 646 #endif 647 { 648 ieee80211_derive_pmkid(pmk->pmk_akm, pmk->pmk_key, 649 macaddr, ic->ic_myaddr, pmk->pmk_pmkid); 650 } 651 return pmk; 652 } 653 654 /* 655 * Check if we have a cached PMK entry for the specified node and PMKID. 656 */ 657 struct ieee80211_pmk * 658 ieee80211_pmksa_find(struct ieee80211com *ic, struct ieee80211_node *ni, 659 const u_int8_t *pmkid) 660 { 661 struct ieee80211_pmk *pmk; 662 663 TAILQ_FOREACH(pmk, &ic->ic_pmksa, pmk_next) { 664 if (pmk->pmk_akm == ni->ni_rsnakms && 665 IEEE80211_ADDR_EQ(pmk->pmk_macaddr, ni->ni_macaddr) && 666 (pmkid == NULL || 667 memcmp(pmk->pmk_pmkid, pmkid, IEEE80211_PMKID_LEN) == 0)) 668 break; 669 } 670 return pmk; 671 } 672