wpa_common.c 42 KB

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