wpa_common.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. /*
  2. * WPA/RSN - Shared functions for supplicant and authenticator
  3. * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/md5.h"
  11. #include "crypto/sha1.h"
  12. #include "crypto/sha256.h"
  13. #include "crypto/sha384.h"
  14. #include "crypto/aes_wrap.h"
  15. #include "crypto/crypto.h"
  16. #include "ieee802_11_defs.h"
  17. #include "defs.h"
  18. #include "wpa_common.h"
  19. static unsigned int wpa_kck_len(int akmp)
  20. {
  21. if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  22. return 24;
  23. return 16;
  24. }
  25. static unsigned int wpa_kek_len(int akmp)
  26. {
  27. if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  28. return 32;
  29. return 16;
  30. }
  31. unsigned int wpa_mic_len(int akmp)
  32. {
  33. if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  34. return 24;
  35. return 16;
  36. }
  37. /**
  38. * wpa_eapol_key_mic - Calculate EAPOL-Key MIC
  39. * @key: EAPOL-Key Key Confirmation Key (KCK)
  40. * @key_len: KCK length in octets
  41. * @akmp: WPA_KEY_MGMT_* used in key derivation
  42. * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*)
  43. * @buf: Pointer to the beginning of the EAPOL header (version field)
  44. * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame)
  45. * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written
  46. * Returns: 0 on success, -1 on failure
  47. *
  48. * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has
  49. * to be cleared (all zeroes) when calling this function.
  50. *
  51. * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the
  52. * description of the Key MIC calculation. It includes packet data from the
  53. * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change
  54. * happened during final editing of the standard and the correct behavior is
  55. * defined in the last draft (IEEE 802.11i/D10).
  56. */
  57. int wpa_eapol_key_mic(const u8 *key, size_t key_len, int akmp, int ver,
  58. const u8 *buf, size_t len, u8 *mic)
  59. {
  60. u8 hash[SHA384_MAC_LEN];
  61. switch (ver) {
  62. #ifndef CONFIG_FIPS
  63. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  64. return hmac_md5(key, key_len, buf, len, mic);
  65. #endif /* CONFIG_FIPS */
  66. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  67. if (hmac_sha1(key, key_len, buf, len, hash))
  68. return -1;
  69. os_memcpy(mic, hash, MD5_MAC_LEN);
  70. break;
  71. #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
  72. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  73. return omac1_aes_128(key, buf, len, mic);
  74. #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
  75. case WPA_KEY_INFO_TYPE_AKM_DEFINED:
  76. switch (akmp) {
  77. #ifdef CONFIG_HS20
  78. case WPA_KEY_MGMT_OSEN:
  79. return omac1_aes_128(key, buf, len, mic);
  80. #endif /* CONFIG_HS20 */
  81. #ifdef CONFIG_SUITEB
  82. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  83. if (hmac_sha256(key, key_len, buf, len, hash))
  84. return -1;
  85. os_memcpy(mic, hash, MD5_MAC_LEN);
  86. break;
  87. #endif /* CONFIG_SUITEB */
  88. #ifdef CONFIG_SUITEB192
  89. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  90. if (hmac_sha384(key, key_len, buf, len, hash))
  91. return -1;
  92. os_memcpy(mic, hash, 24);
  93. break;
  94. #endif /* CONFIG_SUITEB192 */
  95. default:
  96. return -1;
  97. }
  98. break;
  99. default:
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces
  106. * @pmk: Pairwise master key
  107. * @pmk_len: Length of PMK
  108. * @label: Label to use in derivation
  109. * @addr1: AA or SA
  110. * @addr2: SA or AA
  111. * @nonce1: ANonce or SNonce
  112. * @nonce2: SNonce or ANonce
  113. * @ptk: Buffer for pairwise transient key
  114. * @akmp: Negotiated AKM
  115. * @cipher: Negotiated pairwise cipher
  116. * Returns: 0 on success, -1 on failure
  117. *
  118. * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
  119. * PTK = PRF-X(PMK, "Pairwise key expansion",
  120. * Min(AA, SA) || Max(AA, SA) ||
  121. * Min(ANonce, SNonce) || Max(ANonce, SNonce))
  122. *
  123. * STK = PRF-X(SMK, "Peer key expansion",
  124. * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) ||
  125. * Min(INonce, PNonce) || Max(INonce, PNonce))
  126. */
  127. int wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
  128. const u8 *addr1, const u8 *addr2,
  129. const u8 *nonce1, const u8 *nonce2,
  130. struct wpa_ptk *ptk, int akmp, int cipher)
  131. {
  132. u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
  133. u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN];
  134. size_t ptk_len;
  135. if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) {
  136. os_memcpy(data, addr1, ETH_ALEN);
  137. os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
  138. } else {
  139. os_memcpy(data, addr2, ETH_ALEN);
  140. os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
  141. }
  142. if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
  143. os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
  144. os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
  145. WPA_NONCE_LEN);
  146. } else {
  147. os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
  148. os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
  149. WPA_NONCE_LEN);
  150. }
  151. ptk->kck_len = wpa_kck_len(akmp);
  152. ptk->kek_len = wpa_kek_len(akmp);
  153. ptk->tk_len = wpa_cipher_key_len(cipher);
  154. ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len;
  155. #ifdef CONFIG_SUITEB192
  156. if (wpa_key_mgmt_sha384(akmp))
  157. sha384_prf(pmk, pmk_len, label, data, sizeof(data),
  158. tmp, ptk_len);
  159. else
  160. #endif /* CONFIG_SUITEB192 */
  161. #ifdef CONFIG_IEEE80211W
  162. if (wpa_key_mgmt_sha256(akmp))
  163. sha256_prf(pmk, pmk_len, label, data, sizeof(data),
  164. tmp, ptk_len);
  165. else
  166. #endif /* CONFIG_IEEE80211W */
  167. sha1_prf(pmk, pmk_len, label, data, sizeof(data), tmp, ptk_len);
  168. wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR,
  169. MAC2STR(addr1), MAC2STR(addr2));
  170. wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN);
  171. wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN);
  172. wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len);
  173. wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", tmp, ptk_len);
  174. os_memcpy(ptk->kck, tmp, ptk->kck_len);
  175. wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", ptk->kck, ptk->kck_len);
  176. os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
  177. wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
  178. os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
  179. wpa_hexdump_key(MSG_DEBUG, "WPA: TK", ptk->tk, ptk->tk_len);
  180. os_memset(tmp, 0, sizeof(tmp));
  181. return 0;
  182. }
  183. #ifdef CONFIG_IEEE80211R
  184. int wpa_ft_mic(const u8 *kck, size_t kck_len, const u8 *sta_addr,
  185. const u8 *ap_addr, u8 transaction_seqnum,
  186. const u8 *mdie, size_t mdie_len,
  187. const u8 *ftie, size_t ftie_len,
  188. const u8 *rsnie, size_t rsnie_len,
  189. const u8 *ric, size_t ric_len, u8 *mic)
  190. {
  191. const u8 *addr[9];
  192. size_t len[9];
  193. size_t i, num_elem = 0;
  194. u8 zero_mic[16];
  195. if (kck_len != 16) {
  196. wpa_printf(MSG_WARNING, "FT: Unsupported KCK length %u",
  197. (unsigned int) kck_len);
  198. return -1;
  199. }
  200. addr[num_elem] = sta_addr;
  201. len[num_elem] = ETH_ALEN;
  202. num_elem++;
  203. addr[num_elem] = ap_addr;
  204. len[num_elem] = ETH_ALEN;
  205. num_elem++;
  206. addr[num_elem] = &transaction_seqnum;
  207. len[num_elem] = 1;
  208. num_elem++;
  209. if (rsnie) {
  210. addr[num_elem] = rsnie;
  211. len[num_elem] = rsnie_len;
  212. num_elem++;
  213. }
  214. if (mdie) {
  215. addr[num_elem] = mdie;
  216. len[num_elem] = mdie_len;
  217. num_elem++;
  218. }
  219. if (ftie) {
  220. if (ftie_len < 2 + sizeof(struct rsn_ftie))
  221. return -1;
  222. /* IE hdr and mic_control */
  223. addr[num_elem] = ftie;
  224. len[num_elem] = 2 + 2;
  225. num_elem++;
  226. /* MIC field with all zeros */
  227. os_memset(zero_mic, 0, sizeof(zero_mic));
  228. addr[num_elem] = zero_mic;
  229. len[num_elem] = sizeof(zero_mic);
  230. num_elem++;
  231. /* Rest of FTIE */
  232. addr[num_elem] = ftie + 2 + 2 + 16;
  233. len[num_elem] = ftie_len - (2 + 2 + 16);
  234. num_elem++;
  235. }
  236. if (ric) {
  237. addr[num_elem] = ric;
  238. len[num_elem] = ric_len;
  239. num_elem++;
  240. }
  241. for (i = 0; i < num_elem; i++)
  242. wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", addr[i], len[i]);
  243. if (omac1_aes_128_vector(kck, num_elem, addr, len, mic))
  244. return -1;
  245. return 0;
  246. }
  247. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  248. struct wpa_ft_ies *parse)
  249. {
  250. const u8 *end, *pos;
  251. parse->ftie = ie;
  252. parse->ftie_len = ie_len;
  253. pos = ie + sizeof(struct rsn_ftie);
  254. end = ie + ie_len;
  255. while (end - pos >= 2) {
  256. u8 id, len;
  257. id = *pos++;
  258. len = *pos++;
  259. if (len > end - pos)
  260. break;
  261. switch (id) {
  262. case FTIE_SUBELEM_R1KH_ID:
  263. if (len != FT_R1KH_ID_LEN) {
  264. wpa_printf(MSG_DEBUG,
  265. "FT: Invalid R1KH-ID length in FTIE: %d",
  266. len);
  267. return -1;
  268. }
  269. parse->r1kh_id = pos;
  270. break;
  271. case FTIE_SUBELEM_GTK:
  272. parse->gtk = pos;
  273. parse->gtk_len = len;
  274. break;
  275. case FTIE_SUBELEM_R0KH_ID:
  276. if (len < 1 || len > FT_R0KH_ID_MAX_LEN) {
  277. wpa_printf(MSG_DEBUG,
  278. "FT: Invalid R0KH-ID length in FTIE: %d",
  279. len);
  280. return -1;
  281. }
  282. parse->r0kh_id = pos;
  283. parse->r0kh_id_len = len;
  284. break;
  285. #ifdef CONFIG_IEEE80211W
  286. case FTIE_SUBELEM_IGTK:
  287. parse->igtk = pos;
  288. parse->igtk_len = len;
  289. break;
  290. #endif /* CONFIG_IEEE80211W */
  291. }
  292. pos += len;
  293. }
  294. return 0;
  295. }
  296. int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  297. struct wpa_ft_ies *parse)
  298. {
  299. const u8 *end, *pos;
  300. struct wpa_ie_data data;
  301. int ret;
  302. const struct rsn_ftie *ftie;
  303. int prot_ie_count = 0;
  304. os_memset(parse, 0, sizeof(*parse));
  305. if (ies == NULL)
  306. return 0;
  307. pos = ies;
  308. end = ies + ies_len;
  309. while (end - pos >= 2) {
  310. u8 id, len;
  311. id = *pos++;
  312. len = *pos++;
  313. if (len > end - pos)
  314. break;
  315. switch (id) {
  316. case WLAN_EID_RSN:
  317. parse->rsn = pos;
  318. parse->rsn_len = len;
  319. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  320. parse->rsn_len + 2,
  321. &data);
  322. if (ret < 0) {
  323. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  324. "RSN IE: %d", ret);
  325. return -1;
  326. }
  327. if (data.num_pmkid == 1 && data.pmkid)
  328. parse->rsn_pmkid = data.pmkid;
  329. break;
  330. case WLAN_EID_MOBILITY_DOMAIN:
  331. if (len < sizeof(struct rsn_mdie))
  332. return -1;
  333. parse->mdie = pos;
  334. parse->mdie_len = len;
  335. break;
  336. case WLAN_EID_FAST_BSS_TRANSITION:
  337. if (len < sizeof(*ftie))
  338. return -1;
  339. ftie = (const struct rsn_ftie *) pos;
  340. prot_ie_count = ftie->mic_control[1];
  341. if (wpa_ft_parse_ftie(pos, len, parse) < 0)
  342. return -1;
  343. break;
  344. case WLAN_EID_TIMEOUT_INTERVAL:
  345. if (len != 5)
  346. break;
  347. parse->tie = pos;
  348. parse->tie_len = len;
  349. break;
  350. case WLAN_EID_RIC_DATA:
  351. if (parse->ric == NULL)
  352. parse->ric = pos - 2;
  353. break;
  354. }
  355. pos += len;
  356. }
  357. if (prot_ie_count == 0)
  358. return 0; /* no MIC */
  359. /*
  360. * Check that the protected IE count matches with IEs included in the
  361. * frame.
  362. */
  363. if (parse->rsn)
  364. prot_ie_count--;
  365. if (parse->mdie)
  366. prot_ie_count--;
  367. if (parse->ftie)
  368. prot_ie_count--;
  369. if (prot_ie_count < 0) {
  370. wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
  371. "the protected IE count");
  372. return -1;
  373. }
  374. if (prot_ie_count == 0 && parse->ric) {
  375. wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
  376. "included in protected IE count");
  377. return -1;
  378. }
  379. /* Determine the end of the RIC IE(s) */
  380. if (parse->ric) {
  381. pos = parse->ric;
  382. while (end - pos >= 2 && 2 + pos[1] <= end - pos &&
  383. prot_ie_count) {
  384. prot_ie_count--;
  385. pos += 2 + pos[1];
  386. }
  387. parse->ric_len = pos - parse->ric;
  388. }
  389. if (prot_ie_count) {
  390. wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
  391. "frame", (int) prot_ie_count);
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. #endif /* CONFIG_IEEE80211R */
  397. static int rsn_selector_to_bitfield(const u8 *s)
  398. {
  399. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE)
  400. return WPA_CIPHER_NONE;
  401. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP)
  402. return WPA_CIPHER_TKIP;
  403. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP)
  404. return WPA_CIPHER_CCMP;
  405. #ifdef CONFIG_IEEE80211W
  406. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC)
  407. return WPA_CIPHER_AES_128_CMAC;
  408. #endif /* CONFIG_IEEE80211W */
  409. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP)
  410. return WPA_CIPHER_GCMP;
  411. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256)
  412. return WPA_CIPHER_CCMP_256;
  413. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256)
  414. return WPA_CIPHER_GCMP_256;
  415. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128)
  416. return WPA_CIPHER_BIP_GMAC_128;
  417. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256)
  418. return WPA_CIPHER_BIP_GMAC_256;
  419. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256)
  420. return WPA_CIPHER_BIP_CMAC_256;
  421. if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED)
  422. return WPA_CIPHER_GTK_NOT_USED;
  423. return 0;
  424. }
  425. static int rsn_key_mgmt_to_bitfield(const u8 *s)
  426. {
  427. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X)
  428. return WPA_KEY_MGMT_IEEE8021X;
  429. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X)
  430. return WPA_KEY_MGMT_PSK;
  431. #ifdef CONFIG_IEEE80211R
  432. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X)
  433. return WPA_KEY_MGMT_FT_IEEE8021X;
  434. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK)
  435. return WPA_KEY_MGMT_FT_PSK;
  436. #endif /* CONFIG_IEEE80211R */
  437. #ifdef CONFIG_IEEE80211W
  438. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256)
  439. return WPA_KEY_MGMT_IEEE8021X_SHA256;
  440. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256)
  441. return WPA_KEY_MGMT_PSK_SHA256;
  442. #endif /* CONFIG_IEEE80211W */
  443. #ifdef CONFIG_SAE
  444. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE)
  445. return WPA_KEY_MGMT_SAE;
  446. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE)
  447. return WPA_KEY_MGMT_FT_SAE;
  448. #endif /* CONFIG_SAE */
  449. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B)
  450. return WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  451. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192)
  452. return WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
  453. if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_OSEN)
  454. return WPA_KEY_MGMT_OSEN;
  455. return 0;
  456. }
  457. int wpa_cipher_valid_group(int cipher)
  458. {
  459. return wpa_cipher_valid_pairwise(cipher) ||
  460. cipher == WPA_CIPHER_GTK_NOT_USED;
  461. }
  462. #ifdef CONFIG_IEEE80211W
  463. int wpa_cipher_valid_mgmt_group(int cipher)
  464. {
  465. return cipher == WPA_CIPHER_AES_128_CMAC ||
  466. cipher == WPA_CIPHER_BIP_GMAC_128 ||
  467. cipher == WPA_CIPHER_BIP_GMAC_256 ||
  468. cipher == WPA_CIPHER_BIP_CMAC_256;
  469. }
  470. #endif /* CONFIG_IEEE80211W */
  471. /**
  472. * wpa_parse_wpa_ie_rsn - Parse RSN IE
  473. * @rsn_ie: Buffer containing RSN IE
  474. * @rsn_ie_len: RSN IE buffer length (including IE number and length octets)
  475. * @data: Pointer to structure that will be filled in with parsed data
  476. * Returns: 0 on success, <0 on failure
  477. */
  478. int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
  479. struct wpa_ie_data *data)
  480. {
  481. const u8 *pos;
  482. int left;
  483. int i, count;
  484. os_memset(data, 0, sizeof(*data));
  485. data->proto = WPA_PROTO_RSN;
  486. data->pairwise_cipher = WPA_CIPHER_CCMP;
  487. data->group_cipher = WPA_CIPHER_CCMP;
  488. data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  489. data->capabilities = 0;
  490. data->pmkid = NULL;
  491. data->num_pmkid = 0;
  492. #ifdef CONFIG_IEEE80211W
  493. data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  494. #else /* CONFIG_IEEE80211W */
  495. data->mgmt_group_cipher = 0;
  496. #endif /* CONFIG_IEEE80211W */
  497. if (rsn_ie_len == 0) {
  498. /* No RSN IE - fail silently */
  499. return -1;
  500. }
  501. if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) {
  502. wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
  503. __func__, (unsigned long) rsn_ie_len);
  504. return -1;
  505. }
  506. if (rsn_ie_len >= 6 && rsn_ie[1] >= 4 &&
  507. rsn_ie[1] == rsn_ie_len - 2 &&
  508. WPA_GET_BE32(&rsn_ie[2]) == OSEN_IE_VENDOR_TYPE) {
  509. pos = rsn_ie + 6;
  510. left = rsn_ie_len - 6;
  511. data->proto = WPA_PROTO_OSEN;
  512. } else {
  513. const struct rsn_ie_hdr *hdr;
  514. hdr = (const struct rsn_ie_hdr *) rsn_ie;
  515. if (hdr->elem_id != WLAN_EID_RSN ||
  516. hdr->len != rsn_ie_len - 2 ||
  517. WPA_GET_LE16(hdr->version) != RSN_VERSION) {
  518. wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
  519. __func__);
  520. return -2;
  521. }
  522. pos = (const u8 *) (hdr + 1);
  523. left = rsn_ie_len - sizeof(*hdr);
  524. }
  525. if (left >= RSN_SELECTOR_LEN) {
  526. data->group_cipher = rsn_selector_to_bitfield(pos);
  527. if (!wpa_cipher_valid_group(data->group_cipher)) {
  528. wpa_printf(MSG_DEBUG, "%s: invalid group cipher 0x%x",
  529. __func__, data->group_cipher);
  530. return -1;
  531. }
  532. pos += RSN_SELECTOR_LEN;
  533. left -= RSN_SELECTOR_LEN;
  534. } else if (left > 0) {
  535. wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
  536. __func__, left);
  537. return -3;
  538. }
  539. if (left >= 2) {
  540. data->pairwise_cipher = 0;
  541. count = WPA_GET_LE16(pos);
  542. pos += 2;
  543. left -= 2;
  544. if (count == 0 || count > left / RSN_SELECTOR_LEN) {
  545. wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
  546. "count %u left %u", __func__, count, left);
  547. return -4;
  548. }
  549. for (i = 0; i < count; i++) {
  550. data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
  551. pos += RSN_SELECTOR_LEN;
  552. left -= RSN_SELECTOR_LEN;
  553. }
  554. #ifdef CONFIG_IEEE80211W
  555. if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) {
  556. wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as "
  557. "pairwise cipher", __func__);
  558. return -1;
  559. }
  560. #endif /* CONFIG_IEEE80211W */
  561. } else if (left == 1) {
  562. wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
  563. __func__);
  564. return -5;
  565. }
  566. if (left >= 2) {
  567. data->key_mgmt = 0;
  568. count = WPA_GET_LE16(pos);
  569. pos += 2;
  570. left -= 2;
  571. if (count == 0 || count > left / RSN_SELECTOR_LEN) {
  572. wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
  573. "count %u left %u", __func__, count, left);
  574. return -6;
  575. }
  576. for (i = 0; i < count; i++) {
  577. data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
  578. pos += RSN_SELECTOR_LEN;
  579. left -= RSN_SELECTOR_LEN;
  580. }
  581. } else if (left == 1) {
  582. wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
  583. __func__);
  584. return -7;
  585. }
  586. if (left >= 2) {
  587. data->capabilities = WPA_GET_LE16(pos);
  588. pos += 2;
  589. left -= 2;
  590. }
  591. if (left >= 2) {
  592. u16 num_pmkid = WPA_GET_LE16(pos);
  593. pos += 2;
  594. left -= 2;
  595. if (num_pmkid > (unsigned int) left / PMKID_LEN) {
  596. wpa_printf(MSG_DEBUG, "%s: PMKID underflow "
  597. "(num_pmkid=%u left=%d)",
  598. __func__, num_pmkid, left);
  599. data->num_pmkid = 0;
  600. return -9;
  601. } else {
  602. data->num_pmkid = num_pmkid;
  603. data->pmkid = pos;
  604. pos += data->num_pmkid * PMKID_LEN;
  605. left -= data->num_pmkid * PMKID_LEN;
  606. }
  607. }
  608. #ifdef CONFIG_IEEE80211W
  609. if (left >= 4) {
  610. data->mgmt_group_cipher = rsn_selector_to_bitfield(pos);
  611. if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) {
  612. wpa_printf(MSG_DEBUG,
  613. "%s: Unsupported management group cipher 0x%x (%08x)",
  614. __func__, data->mgmt_group_cipher,
  615. WPA_GET_BE32(pos));
  616. return -10;
  617. }
  618. pos += RSN_SELECTOR_LEN;
  619. left -= RSN_SELECTOR_LEN;
  620. }
  621. #endif /* CONFIG_IEEE80211W */
  622. if (left > 0) {
  623. wpa_hexdump(MSG_DEBUG,
  624. "wpa_parse_wpa_ie_rsn: ignore trailing bytes",
  625. pos, left);
  626. }
  627. return 0;
  628. }
  629. static int wpa_selector_to_bitfield(const u8 *s)
  630. {
  631. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE)
  632. return WPA_CIPHER_NONE;
  633. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP)
  634. return WPA_CIPHER_TKIP;
  635. if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP)
  636. return WPA_CIPHER_CCMP;
  637. return 0;
  638. }
  639. static int wpa_key_mgmt_to_bitfield(const u8 *s)
  640. {
  641. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X)
  642. return WPA_KEY_MGMT_IEEE8021X;
  643. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X)
  644. return WPA_KEY_MGMT_PSK;
  645. if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE)
  646. return WPA_KEY_MGMT_WPA_NONE;
  647. return 0;
  648. }
  649. int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
  650. struct wpa_ie_data *data)
  651. {
  652. const struct wpa_ie_hdr *hdr;
  653. const u8 *pos;
  654. int left;
  655. int i, count;
  656. os_memset(data, 0, sizeof(*data));
  657. data->proto = WPA_PROTO_WPA;
  658. data->pairwise_cipher = WPA_CIPHER_TKIP;
  659. data->group_cipher = WPA_CIPHER_TKIP;
  660. data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  661. data->capabilities = 0;
  662. data->pmkid = NULL;
  663. data->num_pmkid = 0;
  664. data->mgmt_group_cipher = 0;
  665. if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) {
  666. wpa_printf(MSG_DEBUG, "%s: ie len too short %lu",
  667. __func__, (unsigned long) wpa_ie_len);
  668. return -1;
  669. }
  670. hdr = (const struct wpa_ie_hdr *) wpa_ie;
  671. if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC ||
  672. hdr->len != wpa_ie_len - 2 ||
  673. RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE ||
  674. WPA_GET_LE16(hdr->version) != WPA_VERSION) {
  675. wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version",
  676. __func__);
  677. return -2;
  678. }
  679. pos = (const u8 *) (hdr + 1);
  680. left = wpa_ie_len - sizeof(*hdr);
  681. if (left >= WPA_SELECTOR_LEN) {
  682. data->group_cipher = wpa_selector_to_bitfield(pos);
  683. pos += WPA_SELECTOR_LEN;
  684. left -= WPA_SELECTOR_LEN;
  685. } else if (left > 0) {
  686. wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much",
  687. __func__, left);
  688. return -3;
  689. }
  690. if (left >= 2) {
  691. data->pairwise_cipher = 0;
  692. count = WPA_GET_LE16(pos);
  693. pos += 2;
  694. left -= 2;
  695. if (count == 0 || count > left / WPA_SELECTOR_LEN) {
  696. wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), "
  697. "count %u left %u", __func__, count, left);
  698. return -4;
  699. }
  700. for (i = 0; i < count; i++) {
  701. data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
  702. pos += WPA_SELECTOR_LEN;
  703. left -= WPA_SELECTOR_LEN;
  704. }
  705. } else if (left == 1) {
  706. wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)",
  707. __func__);
  708. return -5;
  709. }
  710. if (left >= 2) {
  711. data->key_mgmt = 0;
  712. count = WPA_GET_LE16(pos);
  713. pos += 2;
  714. left -= 2;
  715. if (count == 0 || count > left / WPA_SELECTOR_LEN) {
  716. wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), "
  717. "count %u left %u", __func__, count, left);
  718. return -6;
  719. }
  720. for (i = 0; i < count; i++) {
  721. data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
  722. pos += WPA_SELECTOR_LEN;
  723. left -= WPA_SELECTOR_LEN;
  724. }
  725. } else if (left == 1) {
  726. wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)",
  727. __func__);
  728. return -7;
  729. }
  730. if (left >= 2) {
  731. data->capabilities = WPA_GET_LE16(pos);
  732. pos += 2;
  733. left -= 2;
  734. }
  735. if (left > 0) {
  736. wpa_hexdump(MSG_DEBUG,
  737. "wpa_parse_wpa_ie_wpa: ignore trailing bytes",
  738. pos, left);
  739. }
  740. return 0;
  741. }
  742. #ifdef CONFIG_IEEE80211R
  743. /**
  744. * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name
  745. *
  746. * IEEE Std 802.11r-2008 - 8.5.1.5.3
  747. */
  748. void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len,
  749. const u8 *ssid, size_t ssid_len,
  750. const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len,
  751. const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name)
  752. {
  753. u8 buf[1 + SSID_MAX_LEN + MOBILITY_DOMAIN_ID_LEN + 1 +
  754. FT_R0KH_ID_MAX_LEN + ETH_ALEN];
  755. u8 *pos, r0_key_data[48], hash[32];
  756. const u8 *addr[2];
  757. size_t len[2];
  758. /*
  759. * R0-Key-Data = KDF-384(XXKey, "FT-R0",
  760. * SSIDlength || SSID || MDID || R0KHlength ||
  761. * R0KH-ID || S0KH-ID)
  762. * XXKey is either the second 256 bits of MSK or PSK.
  763. * PMK-R0 = L(R0-Key-Data, 0, 256)
  764. * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128)
  765. */
  766. if (ssid_len > SSID_MAX_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN)
  767. return;
  768. pos = buf;
  769. *pos++ = ssid_len;
  770. os_memcpy(pos, ssid, ssid_len);
  771. pos += ssid_len;
  772. os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN);
  773. pos += MOBILITY_DOMAIN_ID_LEN;
  774. *pos++ = r0kh_id_len;
  775. os_memcpy(pos, r0kh_id, r0kh_id_len);
  776. pos += r0kh_id_len;
  777. os_memcpy(pos, s0kh_id, ETH_ALEN);
  778. pos += ETH_ALEN;
  779. sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf,
  780. r0_key_data, sizeof(r0_key_data));
  781. os_memcpy(pmk_r0, r0_key_data, PMK_LEN);
  782. /*
  783. * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt)
  784. */
  785. addr[0] = (const u8 *) "FT-R0N";
  786. len[0] = 6;
  787. addr[1] = r0_key_data + PMK_LEN;
  788. len[1] = 16;
  789. sha256_vector(2, addr, len, hash);
  790. os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN);
  791. }
  792. /**
  793. * wpa_derive_pmk_r1_name - Derive PMKR1Name
  794. *
  795. * IEEE Std 802.11r-2008 - 8.5.1.5.4
  796. */
  797. void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id,
  798. const u8 *s1kh_id, u8 *pmk_r1_name)
  799. {
  800. u8 hash[32];
  801. const u8 *addr[4];
  802. size_t len[4];
  803. /*
  804. * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name ||
  805. * R1KH-ID || S1KH-ID))
  806. */
  807. addr[0] = (const u8 *) "FT-R1N";
  808. len[0] = 6;
  809. addr[1] = pmk_r0_name;
  810. len[1] = WPA_PMK_NAME_LEN;
  811. addr[2] = r1kh_id;
  812. len[2] = FT_R1KH_ID_LEN;
  813. addr[3] = s1kh_id;
  814. len[3] = ETH_ALEN;
  815. sha256_vector(4, addr, len, hash);
  816. os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN);
  817. }
  818. /**
  819. * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0
  820. *
  821. * IEEE Std 802.11r-2008 - 8.5.1.5.4
  822. */
  823. void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name,
  824. const u8 *r1kh_id, const u8 *s1kh_id,
  825. u8 *pmk_r1, u8 *pmk_r1_name)
  826. {
  827. u8 buf[FT_R1KH_ID_LEN + ETH_ALEN];
  828. u8 *pos;
  829. /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */
  830. pos = buf;
  831. os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN);
  832. pos += FT_R1KH_ID_LEN;
  833. os_memcpy(pos, s1kh_id, ETH_ALEN);
  834. pos += ETH_ALEN;
  835. sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN);
  836. wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name);
  837. }
  838. /**
  839. * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1
  840. *
  841. * IEEE Std 802.11r-2008 - 8.5.1.5.5
  842. */
  843. int wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce,
  844. const u8 *sta_addr, const u8 *bssid,
  845. const u8 *pmk_r1_name,
  846. struct wpa_ptk *ptk, u8 *ptk_name, int akmp, int cipher)
  847. {
  848. u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN];
  849. u8 *pos, hash[32];
  850. const u8 *addr[6];
  851. size_t len[6];
  852. u8 tmp[WPA_KCK_MAX_LEN + WPA_KEK_MAX_LEN + WPA_TK_MAX_LEN];
  853. size_t ptk_len;
  854. /*
  855. * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce ||
  856. * BSSID || STA-ADDR)
  857. */
  858. pos = buf;
  859. os_memcpy(pos, snonce, WPA_NONCE_LEN);
  860. pos += WPA_NONCE_LEN;
  861. os_memcpy(pos, anonce, WPA_NONCE_LEN);
  862. pos += WPA_NONCE_LEN;
  863. os_memcpy(pos, bssid, ETH_ALEN);
  864. pos += ETH_ALEN;
  865. os_memcpy(pos, sta_addr, ETH_ALEN);
  866. pos += ETH_ALEN;
  867. ptk->kck_len = wpa_kck_len(akmp);
  868. ptk->kek_len = wpa_kek_len(akmp);
  869. ptk->tk_len = wpa_cipher_key_len(cipher);
  870. ptk_len = ptk->kck_len + ptk->kek_len + ptk->tk_len;
  871. sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, tmp, ptk_len);
  872. /*
  873. * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce ||
  874. * ANonce || BSSID || STA-ADDR))
  875. */
  876. addr[0] = pmk_r1_name;
  877. len[0] = WPA_PMK_NAME_LEN;
  878. addr[1] = (const u8 *) "FT-PTKN";
  879. len[1] = 7;
  880. addr[2] = snonce;
  881. len[2] = WPA_NONCE_LEN;
  882. addr[3] = anonce;
  883. len[3] = WPA_NONCE_LEN;
  884. addr[4] = bssid;
  885. len[4] = ETH_ALEN;
  886. addr[5] = sta_addr;
  887. len[5] = ETH_ALEN;
  888. sha256_vector(6, addr, len, hash);
  889. os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN);
  890. os_memcpy(ptk->kck, tmp, ptk->kck_len);
  891. os_memcpy(ptk->kek, tmp + ptk->kck_len, ptk->kek_len);
  892. os_memcpy(ptk->tk, tmp + ptk->kck_len + ptk->kek_len, ptk->tk_len);
  893. wpa_hexdump_key(MSG_DEBUG, "FT: KCK", ptk->kck, ptk->kck_len);
  894. wpa_hexdump_key(MSG_DEBUG, "FT: KEK", ptk->kek, ptk->kek_len);
  895. wpa_hexdump_key(MSG_DEBUG, "FT: TK", ptk->tk, ptk->tk_len);
  896. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  897. os_memset(tmp, 0, sizeof(tmp));
  898. return 0;
  899. }
  900. #endif /* CONFIG_IEEE80211R */
  901. /**
  902. * rsn_pmkid - Calculate PMK identifier
  903. * @pmk: Pairwise master key
  904. * @pmk_len: Length of pmk in bytes
  905. * @aa: Authenticator address
  906. * @spa: Supplicant address
  907. * @pmkid: Buffer for PMKID
  908. * @use_sha256: Whether to use SHA256-based KDF
  909. *
  910. * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
  911. * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA)
  912. */
  913. void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
  914. u8 *pmkid, int use_sha256)
  915. {
  916. char *title = "PMK Name";
  917. const u8 *addr[3];
  918. const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
  919. unsigned char hash[SHA256_MAC_LEN];
  920. addr[0] = (u8 *) title;
  921. addr[1] = aa;
  922. addr[2] = spa;
  923. #ifdef CONFIG_IEEE80211W
  924. if (use_sha256)
  925. hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash);
  926. else
  927. #endif /* CONFIG_IEEE80211W */
  928. hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
  929. os_memcpy(pmkid, hash, PMKID_LEN);
  930. }
  931. #ifdef CONFIG_SUITEB
  932. /**
  933. * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM
  934. * @kck: Key confirmation key
  935. * @kck_len: Length of kck in bytes
  936. * @aa: Authenticator address
  937. * @spa: Supplicant address
  938. * @pmkid: Buffer for PMKID
  939. * Returns: 0 on success, -1 on failure
  940. *
  941. * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
  942. * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA))
  943. */
  944. int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa,
  945. const u8 *spa, u8 *pmkid)
  946. {
  947. char *title = "PMK Name";
  948. const u8 *addr[3];
  949. const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
  950. unsigned char hash[SHA256_MAC_LEN];
  951. addr[0] = (u8 *) title;
  952. addr[1] = aa;
  953. addr[2] = spa;
  954. if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0)
  955. return -1;
  956. os_memcpy(pmkid, hash, PMKID_LEN);
  957. return 0;
  958. }
  959. #endif /* CONFIG_SUITEB */
  960. #ifdef CONFIG_SUITEB192
  961. /**
  962. * rsn_pmkid_suite_b_192 - Calculate PMK identifier for Suite B AKM
  963. * @kck: Key confirmation key
  964. * @kck_len: Length of kck in bytes
  965. * @aa: Authenticator address
  966. * @spa: Supplicant address
  967. * @pmkid: Buffer for PMKID
  968. * Returns: 0 on success, -1 on failure
  969. *
  970. * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy
  971. * PMKID = Truncate(HMAC-SHA-384(KCK, "PMK Name" || AA || SPA))
  972. */
  973. int rsn_pmkid_suite_b_192(const u8 *kck, size_t kck_len, const u8 *aa,
  974. const u8 *spa, u8 *pmkid)
  975. {
  976. char *title = "PMK Name";
  977. const u8 *addr[3];
  978. const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
  979. unsigned char hash[SHA384_MAC_LEN];
  980. addr[0] = (u8 *) title;
  981. addr[1] = aa;
  982. addr[2] = spa;
  983. if (hmac_sha384_vector(kck, kck_len, 3, addr, len, hash) < 0)
  984. return -1;
  985. os_memcpy(pmkid, hash, PMKID_LEN);
  986. return 0;
  987. }
  988. #endif /* CONFIG_SUITEB192 */
  989. /**
  990. * wpa_cipher_txt - Convert cipher suite to a text string
  991. * @cipher: Cipher suite (WPA_CIPHER_* enum)
  992. * Returns: Pointer to a text string of the cipher suite name
  993. */
  994. const char * wpa_cipher_txt(int cipher)
  995. {
  996. switch (cipher) {
  997. case WPA_CIPHER_NONE:
  998. return "NONE";
  999. case WPA_CIPHER_WEP40:
  1000. return "WEP-40";
  1001. case WPA_CIPHER_WEP104:
  1002. return "WEP-104";
  1003. case WPA_CIPHER_TKIP:
  1004. return "TKIP";
  1005. case WPA_CIPHER_CCMP:
  1006. return "CCMP";
  1007. case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP:
  1008. return "CCMP+TKIP";
  1009. case WPA_CIPHER_GCMP:
  1010. return "GCMP";
  1011. case WPA_CIPHER_GCMP_256:
  1012. return "GCMP-256";
  1013. case WPA_CIPHER_CCMP_256:
  1014. return "CCMP-256";
  1015. case WPA_CIPHER_GTK_NOT_USED:
  1016. return "GTK_NOT_USED";
  1017. default:
  1018. return "UNKNOWN";
  1019. }
  1020. }
  1021. /**
  1022. * wpa_key_mgmt_txt - Convert key management suite to a text string
  1023. * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
  1024. * @proto: WPA/WPA2 version (WPA_PROTO_*)
  1025. * Returns: Pointer to a text string of the key management suite name
  1026. */
  1027. const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
  1028. {
  1029. switch (key_mgmt) {
  1030. case WPA_KEY_MGMT_IEEE8021X:
  1031. if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
  1032. return "WPA2+WPA/IEEE 802.1X/EAP";
  1033. return proto == WPA_PROTO_RSN ?
  1034. "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
  1035. case WPA_KEY_MGMT_PSK:
  1036. if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA))
  1037. return "WPA2-PSK+WPA-PSK";
  1038. return proto == WPA_PROTO_RSN ?
  1039. "WPA2-PSK" : "WPA-PSK";
  1040. case WPA_KEY_MGMT_NONE:
  1041. return "NONE";
  1042. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  1043. return "IEEE 802.1X (no WPA)";
  1044. #ifdef CONFIG_IEEE80211R
  1045. case WPA_KEY_MGMT_FT_IEEE8021X:
  1046. return "FT-EAP";
  1047. case WPA_KEY_MGMT_FT_PSK:
  1048. return "FT-PSK";
  1049. #endif /* CONFIG_IEEE80211R */
  1050. #ifdef CONFIG_IEEE80211W
  1051. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  1052. return "WPA2-EAP-SHA256";
  1053. case WPA_KEY_MGMT_PSK_SHA256:
  1054. return "WPA2-PSK-SHA256";
  1055. #endif /* CONFIG_IEEE80211W */
  1056. case WPA_KEY_MGMT_WPS:
  1057. return "WPS";
  1058. case WPA_KEY_MGMT_SAE:
  1059. return "SAE";
  1060. case WPA_KEY_MGMT_FT_SAE:
  1061. return "FT-SAE";
  1062. case WPA_KEY_MGMT_OSEN:
  1063. return "OSEN";
  1064. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  1065. return "WPA2-EAP-SUITE-B";
  1066. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  1067. return "WPA2-EAP-SUITE-B-192";
  1068. default:
  1069. return "UNKNOWN";
  1070. }
  1071. }
  1072. u32 wpa_akm_to_suite(int akm)
  1073. {
  1074. if (akm & WPA_KEY_MGMT_FT_IEEE8021X)
  1075. return WLAN_AKM_SUITE_FT_8021X;
  1076. if (akm & WPA_KEY_MGMT_FT_PSK)
  1077. return WLAN_AKM_SUITE_FT_PSK;
  1078. if (akm & WPA_KEY_MGMT_IEEE8021X)
  1079. return WLAN_AKM_SUITE_8021X;
  1080. if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256)
  1081. return WLAN_AKM_SUITE_8021X_SHA256;
  1082. if (akm & WPA_KEY_MGMT_IEEE8021X)
  1083. return WLAN_AKM_SUITE_8021X;
  1084. if (akm & WPA_KEY_MGMT_PSK_SHA256)
  1085. return WLAN_AKM_SUITE_PSK_SHA256;
  1086. if (akm & WPA_KEY_MGMT_PSK)
  1087. return WLAN_AKM_SUITE_PSK;
  1088. if (akm & WPA_KEY_MGMT_CCKM)
  1089. return WLAN_AKM_SUITE_CCKM;
  1090. if (akm & WPA_KEY_MGMT_OSEN)
  1091. return WLAN_AKM_SUITE_OSEN;
  1092. if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
  1093. return WLAN_AKM_SUITE_8021X_SUITE_B;
  1094. if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  1095. return WLAN_AKM_SUITE_8021X_SUITE_B_192;
  1096. return 0;
  1097. }
  1098. int wpa_compare_rsn_ie(int ft_initial_assoc,
  1099. const u8 *ie1, size_t ie1len,
  1100. const u8 *ie2, size_t ie2len)
  1101. {
  1102. if (ie1 == NULL || ie2 == NULL)
  1103. return -1;
  1104. if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0)
  1105. return 0; /* identical IEs */
  1106. #ifdef CONFIG_IEEE80211R
  1107. if (ft_initial_assoc) {
  1108. struct wpa_ie_data ie1d, ie2d;
  1109. /*
  1110. * The PMKID-List in RSN IE is different between Beacon/Probe
  1111. * Response/(Re)Association Request frames and EAPOL-Key
  1112. * messages in FT initial mobility domain association. Allow
  1113. * for this, but verify that other parts of the RSN IEs are
  1114. * identical.
  1115. */
  1116. if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 ||
  1117. wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0)
  1118. return -1;
  1119. if (ie1d.proto == ie2d.proto &&
  1120. ie1d.pairwise_cipher == ie2d.pairwise_cipher &&
  1121. ie1d.group_cipher == ie2d.group_cipher &&
  1122. ie1d.key_mgmt == ie2d.key_mgmt &&
  1123. ie1d.capabilities == ie2d.capabilities &&
  1124. ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher)
  1125. return 0;
  1126. }
  1127. #endif /* CONFIG_IEEE80211R */
  1128. return -1;
  1129. }
  1130. #ifdef CONFIG_IEEE80211R
  1131. int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid)
  1132. {
  1133. u8 *start, *end, *rpos, *rend;
  1134. int added = 0;
  1135. start = ies;
  1136. end = ies + ies_len;
  1137. while (start < end) {
  1138. if (*start == WLAN_EID_RSN)
  1139. break;
  1140. start += 2 + start[1];
  1141. }
  1142. if (start >= end) {
  1143. wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in "
  1144. "IEs data");
  1145. return -1;
  1146. }
  1147. wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification",
  1148. start, 2 + start[1]);
  1149. /* Find start of PMKID-Count */
  1150. rpos = start + 2;
  1151. rend = rpos + start[1];
  1152. /* Skip Version and Group Data Cipher Suite */
  1153. rpos += 2 + 4;
  1154. /* Skip Pairwise Cipher Suite Count and List */
  1155. rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
  1156. /* Skip AKM Suite Count and List */
  1157. rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN;
  1158. if (rpos == rend) {
  1159. /* Add RSN Capabilities */
  1160. os_memmove(rpos + 2, rpos, end - rpos);
  1161. *rpos++ = 0;
  1162. *rpos++ = 0;
  1163. added += 2;
  1164. start[1] += 2;
  1165. rend = rpos;
  1166. } else {
  1167. /* Skip RSN Capabilities */
  1168. rpos += 2;
  1169. if (rpos > rend) {
  1170. wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in "
  1171. "IEs data");
  1172. return -1;
  1173. }
  1174. }
  1175. if (rpos == rend) {
  1176. /* No PMKID-Count field included; add it */
  1177. os_memmove(rpos + 2 + PMKID_LEN, rpos, end + added - rpos);
  1178. WPA_PUT_LE16(rpos, 1);
  1179. rpos += 2;
  1180. os_memcpy(rpos, pmkid, PMKID_LEN);
  1181. added += 2 + PMKID_LEN;
  1182. start[1] += 2 + PMKID_LEN;
  1183. } else {
  1184. /* PMKID-Count was included; use it */
  1185. if (WPA_GET_LE16(rpos) != 0) {
  1186. wpa_printf(MSG_ERROR, "FT: Unexpected PMKID "
  1187. "in RSN IE in EAPOL-Key data");
  1188. return -1;
  1189. }
  1190. WPA_PUT_LE16(rpos, 1);
  1191. rpos += 2;
  1192. os_memmove(rpos + PMKID_LEN, rpos, end + added - rpos);
  1193. os_memcpy(rpos, pmkid, PMKID_LEN);
  1194. added += PMKID_LEN;
  1195. start[1] += PMKID_LEN;
  1196. }
  1197. wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification "
  1198. "(PMKID inserted)", start, 2 + start[1]);
  1199. return added;
  1200. }
  1201. #endif /* CONFIG_IEEE80211R */
  1202. int wpa_cipher_key_len(int cipher)
  1203. {
  1204. switch (cipher) {
  1205. case WPA_CIPHER_CCMP_256:
  1206. case WPA_CIPHER_GCMP_256:
  1207. case WPA_CIPHER_BIP_GMAC_256:
  1208. case WPA_CIPHER_BIP_CMAC_256:
  1209. return 32;
  1210. case WPA_CIPHER_CCMP:
  1211. case WPA_CIPHER_GCMP:
  1212. case WPA_CIPHER_AES_128_CMAC:
  1213. case WPA_CIPHER_BIP_GMAC_128:
  1214. return 16;
  1215. case WPA_CIPHER_TKIP:
  1216. return 32;
  1217. }
  1218. return 0;
  1219. }
  1220. int wpa_cipher_rsc_len(int cipher)
  1221. {
  1222. switch (cipher) {
  1223. case WPA_CIPHER_CCMP_256:
  1224. case WPA_CIPHER_GCMP_256:
  1225. case WPA_CIPHER_CCMP:
  1226. case WPA_CIPHER_GCMP:
  1227. case WPA_CIPHER_TKIP:
  1228. return 6;
  1229. }
  1230. return 0;
  1231. }
  1232. int wpa_cipher_to_alg(int cipher)
  1233. {
  1234. switch (cipher) {
  1235. case WPA_CIPHER_CCMP_256:
  1236. return WPA_ALG_CCMP_256;
  1237. case WPA_CIPHER_GCMP_256:
  1238. return WPA_ALG_GCMP_256;
  1239. case WPA_CIPHER_CCMP:
  1240. return WPA_ALG_CCMP;
  1241. case WPA_CIPHER_GCMP:
  1242. return WPA_ALG_GCMP;
  1243. case WPA_CIPHER_TKIP:
  1244. return WPA_ALG_TKIP;
  1245. case WPA_CIPHER_AES_128_CMAC:
  1246. return WPA_ALG_IGTK;
  1247. case WPA_CIPHER_BIP_GMAC_128:
  1248. return WPA_ALG_BIP_GMAC_128;
  1249. case WPA_CIPHER_BIP_GMAC_256:
  1250. return WPA_ALG_BIP_GMAC_256;
  1251. case WPA_CIPHER_BIP_CMAC_256:
  1252. return WPA_ALG_BIP_CMAC_256;
  1253. }
  1254. return WPA_ALG_NONE;
  1255. }
  1256. int wpa_cipher_valid_pairwise(int cipher)
  1257. {
  1258. return cipher == WPA_CIPHER_CCMP_256 ||
  1259. cipher == WPA_CIPHER_GCMP_256 ||
  1260. cipher == WPA_CIPHER_CCMP ||
  1261. cipher == WPA_CIPHER_GCMP ||
  1262. cipher == WPA_CIPHER_TKIP;
  1263. }
  1264. u32 wpa_cipher_to_suite(int proto, int cipher)
  1265. {
  1266. if (cipher & WPA_CIPHER_CCMP_256)
  1267. return RSN_CIPHER_SUITE_CCMP_256;
  1268. if (cipher & WPA_CIPHER_GCMP_256)
  1269. return RSN_CIPHER_SUITE_GCMP_256;
  1270. if (cipher & WPA_CIPHER_CCMP)
  1271. return (proto == WPA_PROTO_RSN ?
  1272. RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
  1273. if (cipher & WPA_CIPHER_GCMP)
  1274. return RSN_CIPHER_SUITE_GCMP;
  1275. if (cipher & WPA_CIPHER_TKIP)
  1276. return (proto == WPA_PROTO_RSN ?
  1277. RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
  1278. if (cipher & WPA_CIPHER_NONE)
  1279. return (proto == WPA_PROTO_RSN ?
  1280. RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
  1281. if (cipher & WPA_CIPHER_GTK_NOT_USED)
  1282. return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
  1283. if (cipher & WPA_CIPHER_AES_128_CMAC)
  1284. return RSN_CIPHER_SUITE_AES_128_CMAC;
  1285. if (cipher & WPA_CIPHER_BIP_GMAC_128)
  1286. return RSN_CIPHER_SUITE_BIP_GMAC_128;
  1287. if (cipher & WPA_CIPHER_BIP_GMAC_256)
  1288. return RSN_CIPHER_SUITE_BIP_GMAC_256;
  1289. if (cipher & WPA_CIPHER_BIP_CMAC_256)
  1290. return RSN_CIPHER_SUITE_BIP_CMAC_256;
  1291. return 0;
  1292. }
  1293. int rsn_cipher_put_suites(u8 *start, int ciphers)
  1294. {
  1295. u8 *pos = start;
  1296. if (ciphers & WPA_CIPHER_CCMP_256) {
  1297. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256);
  1298. pos += RSN_SELECTOR_LEN;
  1299. }
  1300. if (ciphers & WPA_CIPHER_GCMP_256) {
  1301. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256);
  1302. pos += RSN_SELECTOR_LEN;
  1303. }
  1304. if (ciphers & WPA_CIPHER_CCMP) {
  1305. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  1306. pos += RSN_SELECTOR_LEN;
  1307. }
  1308. if (ciphers & WPA_CIPHER_GCMP) {
  1309. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP);
  1310. pos += RSN_SELECTOR_LEN;
  1311. }
  1312. if (ciphers & WPA_CIPHER_TKIP) {
  1313. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
  1314. pos += RSN_SELECTOR_LEN;
  1315. }
  1316. if (ciphers & WPA_CIPHER_NONE) {
  1317. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE);
  1318. pos += RSN_SELECTOR_LEN;
  1319. }
  1320. return (pos - start) / RSN_SELECTOR_LEN;
  1321. }
  1322. int wpa_cipher_put_suites(u8 *start, int ciphers)
  1323. {
  1324. u8 *pos = start;
  1325. if (ciphers & WPA_CIPHER_CCMP) {
  1326. RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP);
  1327. pos += WPA_SELECTOR_LEN;
  1328. }
  1329. if (ciphers & WPA_CIPHER_TKIP) {
  1330. RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP);
  1331. pos += WPA_SELECTOR_LEN;
  1332. }
  1333. if (ciphers & WPA_CIPHER_NONE) {
  1334. RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE);
  1335. pos += WPA_SELECTOR_LEN;
  1336. }
  1337. return (pos - start) / RSN_SELECTOR_LEN;
  1338. }
  1339. int wpa_pick_pairwise_cipher(int ciphers, int none_allowed)
  1340. {
  1341. if (ciphers & WPA_CIPHER_CCMP_256)
  1342. return WPA_CIPHER_CCMP_256;
  1343. if (ciphers & WPA_CIPHER_GCMP_256)
  1344. return WPA_CIPHER_GCMP_256;
  1345. if (ciphers & WPA_CIPHER_CCMP)
  1346. return WPA_CIPHER_CCMP;
  1347. if (ciphers & WPA_CIPHER_GCMP)
  1348. return WPA_CIPHER_GCMP;
  1349. if (ciphers & WPA_CIPHER_TKIP)
  1350. return WPA_CIPHER_TKIP;
  1351. if (none_allowed && (ciphers & WPA_CIPHER_NONE))
  1352. return WPA_CIPHER_NONE;
  1353. return -1;
  1354. }
  1355. int wpa_pick_group_cipher(int ciphers)
  1356. {
  1357. if (ciphers & WPA_CIPHER_CCMP_256)
  1358. return WPA_CIPHER_CCMP_256;
  1359. if (ciphers & WPA_CIPHER_GCMP_256)
  1360. return WPA_CIPHER_GCMP_256;
  1361. if (ciphers & WPA_CIPHER_CCMP)
  1362. return WPA_CIPHER_CCMP;
  1363. if (ciphers & WPA_CIPHER_GCMP)
  1364. return WPA_CIPHER_GCMP;
  1365. if (ciphers & WPA_CIPHER_GTK_NOT_USED)
  1366. return WPA_CIPHER_GTK_NOT_USED;
  1367. if (ciphers & WPA_CIPHER_TKIP)
  1368. return WPA_CIPHER_TKIP;
  1369. return -1;
  1370. }
  1371. int wpa_parse_cipher(const char *value)
  1372. {
  1373. int val = 0, last;
  1374. char *start, *end, *buf;
  1375. buf = os_strdup(value);
  1376. if (buf == NULL)
  1377. return -1;
  1378. start = buf;
  1379. while (*start != '\0') {
  1380. while (*start == ' ' || *start == '\t')
  1381. start++;
  1382. if (*start == '\0')
  1383. break;
  1384. end = start;
  1385. while (*end != ' ' && *end != '\t' && *end != '\0')
  1386. end++;
  1387. last = *end == '\0';
  1388. *end = '\0';
  1389. if (os_strcmp(start, "CCMP-256") == 0)
  1390. val |= WPA_CIPHER_CCMP_256;
  1391. else if (os_strcmp(start, "GCMP-256") == 0)
  1392. val |= WPA_CIPHER_GCMP_256;
  1393. else if (os_strcmp(start, "CCMP") == 0)
  1394. val |= WPA_CIPHER_CCMP;
  1395. else if (os_strcmp(start, "GCMP") == 0)
  1396. val |= WPA_CIPHER_GCMP;
  1397. else if (os_strcmp(start, "TKIP") == 0)
  1398. val |= WPA_CIPHER_TKIP;
  1399. else if (os_strcmp(start, "WEP104") == 0)
  1400. val |= WPA_CIPHER_WEP104;
  1401. else if (os_strcmp(start, "WEP40") == 0)
  1402. val |= WPA_CIPHER_WEP40;
  1403. else if (os_strcmp(start, "NONE") == 0)
  1404. val |= WPA_CIPHER_NONE;
  1405. else if (os_strcmp(start, "GTK_NOT_USED") == 0)
  1406. val |= WPA_CIPHER_GTK_NOT_USED;
  1407. else {
  1408. os_free(buf);
  1409. return -1;
  1410. }
  1411. if (last)
  1412. break;
  1413. start = end + 1;
  1414. }
  1415. os_free(buf);
  1416. return val;
  1417. }
  1418. int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim)
  1419. {
  1420. char *pos = start;
  1421. int ret;
  1422. if (ciphers & WPA_CIPHER_CCMP_256) {
  1423. ret = os_snprintf(pos, end - pos, "%sCCMP-256",
  1424. pos == start ? "" : delim);
  1425. if (os_snprintf_error(end - pos, ret))
  1426. return -1;
  1427. pos += ret;
  1428. }
  1429. if (ciphers & WPA_CIPHER_GCMP_256) {
  1430. ret = os_snprintf(pos, end - pos, "%sGCMP-256",
  1431. pos == start ? "" : delim);
  1432. if (os_snprintf_error(end - pos, ret))
  1433. return -1;
  1434. pos += ret;
  1435. }
  1436. if (ciphers & WPA_CIPHER_CCMP) {
  1437. ret = os_snprintf(pos, end - pos, "%sCCMP",
  1438. pos == start ? "" : delim);
  1439. if (os_snprintf_error(end - pos, ret))
  1440. return -1;
  1441. pos += ret;
  1442. }
  1443. if (ciphers & WPA_CIPHER_GCMP) {
  1444. ret = os_snprintf(pos, end - pos, "%sGCMP",
  1445. pos == start ? "" : delim);
  1446. if (os_snprintf_error(end - pos, ret))
  1447. return -1;
  1448. pos += ret;
  1449. }
  1450. if (ciphers & WPA_CIPHER_TKIP) {
  1451. ret = os_snprintf(pos, end - pos, "%sTKIP",
  1452. pos == start ? "" : delim);
  1453. if (os_snprintf_error(end - pos, ret))
  1454. return -1;
  1455. pos += ret;
  1456. }
  1457. if (ciphers & WPA_CIPHER_NONE) {
  1458. ret = os_snprintf(pos, end - pos, "%sNONE",
  1459. pos == start ? "" : delim);
  1460. if (os_snprintf_error(end - pos, ret))
  1461. return -1;
  1462. pos += ret;
  1463. }
  1464. return pos - start;
  1465. }
  1466. int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise)
  1467. {
  1468. int pairwise = 0;
  1469. /* Select group cipher based on the enabled pairwise cipher suites */
  1470. if (wpa & 1)
  1471. pairwise |= wpa_pairwise;
  1472. if (wpa & 2)
  1473. pairwise |= rsn_pairwise;
  1474. if (pairwise & WPA_CIPHER_TKIP)
  1475. return WPA_CIPHER_TKIP;
  1476. if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP)
  1477. return WPA_CIPHER_GCMP;
  1478. if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP |
  1479. WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256)
  1480. return WPA_CIPHER_GCMP_256;
  1481. if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP |
  1482. WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256)
  1483. return WPA_CIPHER_CCMP_256;
  1484. return WPA_CIPHER_CCMP;
  1485. }