wpa_common.c 36 KB

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