You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wpa.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <gpxe/net80211.h>
  20. #include <gpxe/sec80211.h>
  21. #include <gpxe/wpa.h>
  22. #include <gpxe/eapol.h>
  23. #include <gpxe/crypto.h>
  24. #include <gpxe/arc4.h>
  25. #include <gpxe/crc32.h>
  26. #include <gpxe/sha1.h>
  27. #include <gpxe/hmac.h>
  28. #include <gpxe/list.h>
  29. #include <gpxe/ethernet.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. /** @file
  34. *
  35. * Handler for the aspects of WPA handshaking that are independent of
  36. * 802.1X/PSK or TKIP/CCMP; this mostly involves the 4-Way Handshake.
  37. */
  38. /** List of WPA contexts in active use. */
  39. struct list_head wpa_contexts = LIST_HEAD_INIT ( wpa_contexts );
  40. /**
  41. * Return an error code and deauthenticate
  42. *
  43. * @v ctx WPA common context
  44. * @v rc Return status code
  45. * @ret rc The passed return status code
  46. */
  47. static int wpa_fail ( struct wpa_common_ctx *ctx, int rc )
  48. {
  49. net80211_deauthenticate ( ctx->dev, rc );
  50. return rc;
  51. }
  52. /**
  53. * Find a cryptosystem handler structure from a crypto ID
  54. *
  55. * @v crypt Cryptosystem ID
  56. * @ret crypto Cryptosystem handler structure
  57. *
  58. * If support for @a crypt is not compiled in to gPXE, or if @a crypt
  59. * is NET80211_CRYPT_UNKNOWN, returns @c NULL.
  60. */
  61. static struct net80211_crypto *
  62. wpa_find_cryptosystem ( enum net80211_crypto_alg crypt )
  63. {
  64. struct net80211_crypto *crypto;
  65. for_each_table_entry ( crypto, NET80211_CRYPTOS ) {
  66. if ( crypto->algorithm == crypt )
  67. return crypto;
  68. }
  69. return NULL;
  70. }
  71. /**
  72. * Find WPA key integrity and encryption handler from key version field
  73. *
  74. * @v ver Version bits of EAPOL-Key info field
  75. * @ret kie Key integrity and encryption handler
  76. */
  77. struct wpa_kie * wpa_find_kie ( int version )
  78. {
  79. struct wpa_kie *kie;
  80. for_each_table_entry ( kie, WPA_KIES ) {
  81. if ( kie->version == version )
  82. return kie;
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * Construct RSN or WPA information element
  88. *
  89. * @v dev 802.11 device
  90. * @ret ie_ret RSN or WPA information element
  91. * @ret rc Return status code
  92. *
  93. * This function allocates, fills, and returns a RSN or WPA
  94. * information element suitable for including in an association
  95. * request frame to the network identified by @c dev->associating.
  96. * If it is impossible to construct an information element consistent
  97. * with gPXE's capabilities that is compatible with that network, or
  98. * if none should be sent because that network's beacon included no
  99. * security information, returns an error indication and leaves
  100. * @a ie_ret unchanged.
  101. *
  102. * The returned IE will be of the same type (RSN or WPA) as was
  103. * included in the beacon for the network it is destined for.
  104. */
  105. int wpa_make_rsn_ie ( struct net80211_device *dev, union ieee80211_ie **ie_ret )
  106. {
  107. u8 *rsn, *rsn_end;
  108. int is_rsn;
  109. u32 group_cipher;
  110. enum net80211_crypto_alg gcrypt;
  111. int ie_len;
  112. u8 *iep;
  113. struct ieee80211_ie_rsn *ie;
  114. struct ieee80211_frame *hdr;
  115. struct ieee80211_beacon *beacon;
  116. if ( ! dev->associating ) {
  117. DBG ( "WPA: Can't make RSN IE for a non-associating device\n" );
  118. return -EINVAL;
  119. }
  120. hdr = dev->associating->beacon->data;
  121. beacon = ( struct ieee80211_beacon * ) hdr->data;
  122. rsn = sec80211_find_rsn ( beacon->info_element,
  123. dev->associating->beacon->tail, &is_rsn,
  124. &rsn_end );
  125. if ( ! rsn ) {
  126. DBG ( "WPA: Can't make RSN IE when we didn't get one\n" );
  127. return -EINVAL;
  128. }
  129. rsn += 2; /* skip version */
  130. group_cipher = *( u32 * ) rsn;
  131. gcrypt = sec80211_rsn_get_net80211_crypt ( group_cipher );
  132. if ( ! wpa_find_cryptosystem ( gcrypt ) ||
  133. ! wpa_find_cryptosystem ( dev->associating->crypto ) ) {
  134. DBG ( "WPA: No support for (GC:%d, PC:%d)\n",
  135. gcrypt, dev->associating->crypto );
  136. return -ENOTSUP;
  137. }
  138. /* Everything looks good - make our IE. */
  139. /* WPA IEs need 4 more bytes for the OUI+type */
  140. ie_len = ieee80211_rsn_size ( 1, 1, 0, is_rsn ) + ( 4 * ! is_rsn );
  141. iep = malloc ( ie_len );
  142. if ( ! iep )
  143. return -ENOMEM;
  144. *ie_ret = ( union ieee80211_ie * ) iep;
  145. /* Store ID and length bytes. */
  146. *iep++ = ( is_rsn ? IEEE80211_IE_RSN : IEEE80211_IE_VENDOR );
  147. *iep++ = ie_len - 2;
  148. /* Store OUI+type for WPA IEs. */
  149. if ( ! is_rsn ) {
  150. *( u32 * ) iep = IEEE80211_WPA_OUI_VEN;
  151. iep += 4;
  152. }
  153. /* If this is a WPA IE, the id and len bytes in the
  154. ieee80211_ie_rsn structure will not be valid, but by doing
  155. the cast we can fill all the other fields much more
  156. readily. */
  157. ie = ( struct ieee80211_ie_rsn * ) ( iep - 2 );
  158. ie->version = IEEE80211_RSN_VERSION;
  159. ie->group_cipher = group_cipher;
  160. ie->pairwise_count = 1;
  161. ie->pairwise_cipher[0] =
  162. sec80211_rsn_get_crypto_desc ( dev->associating->crypto,
  163. is_rsn );
  164. ie->akm_count = 1;
  165. ie->akm_list[0] =
  166. sec80211_rsn_get_akm_desc ( dev->associating->handshaking,
  167. is_rsn );
  168. if ( is_rsn ) {
  169. ie->rsn_capab = 0;
  170. ie->pmkid_count = 0;
  171. }
  172. return 0;
  173. }
  174. /**
  175. * Set up generic WPA support to handle 4-Way Handshake
  176. *
  177. * @v dev 802.11 device
  178. * @v ctx WPA common context
  179. * @v pmk Pairwise Master Key to use for session
  180. * @v pmk_len Length of PMK, almost always 32
  181. * @ret rc Return status code
  182. */
  183. int wpa_start ( struct net80211_device *dev, struct wpa_common_ctx *ctx,
  184. const void *pmk, size_t pmk_len )
  185. {
  186. struct io_buffer *iob;
  187. struct ieee80211_frame *hdr;
  188. struct ieee80211_beacon *beacon;
  189. u8 *ap_rsn_ie = NULL, *ap_rsn_ie_end;
  190. if ( ! dev->rsn_ie || ! dev->associating )
  191. return -EINVAL;
  192. ctx->dev = dev;
  193. memcpy ( ctx->pmk, pmk, ctx->pmk_len = pmk_len );
  194. ctx->state = WPA_READY;
  195. ctx->replay = ~0ULL;
  196. iob = dev->associating->beacon;
  197. hdr = iob->data;
  198. beacon = ( struct ieee80211_beacon * ) hdr->data;
  199. ap_rsn_ie = sec80211_find_rsn ( beacon->info_element, iob->tail,
  200. &ctx->ap_rsn_is_rsn, &ap_rsn_ie_end );
  201. if ( ap_rsn_ie ) {
  202. ctx->ap_rsn_ie = malloc ( ap_rsn_ie_end - ap_rsn_ie );
  203. if ( ! ctx->ap_rsn_ie )
  204. return -ENOMEM;
  205. memcpy ( ctx->ap_rsn_ie, ap_rsn_ie, ap_rsn_ie_end - ap_rsn_ie );
  206. ctx->ap_rsn_ie_len = ap_rsn_ie_end - ap_rsn_ie;
  207. } else {
  208. return -ENOENT;
  209. }
  210. ctx->crypt = dev->associating->crypto;
  211. ctx->gcrypt = NET80211_CRYPT_UNKNOWN;
  212. list_add_tail ( &ctx->list, &wpa_contexts );
  213. return 0;
  214. }
  215. /**
  216. * Disable handling of received WPA handshake frames
  217. *
  218. * @v dev 802.11 device
  219. */
  220. void wpa_stop ( struct net80211_device *dev )
  221. {
  222. struct wpa_common_ctx *ctx, *tmp;
  223. list_for_each_entry_safe ( ctx, tmp, &wpa_contexts, list ) {
  224. if ( ctx->dev == dev ) {
  225. free ( ctx->ap_rsn_ie );
  226. ctx->ap_rsn_ie = NULL;
  227. list_del ( &ctx->list );
  228. }
  229. }
  230. }
  231. /**
  232. * Check PMKID consistency
  233. *
  234. * @v ctx WPA common context
  235. * @v pmkid PMKID to check against (16 bytes long)
  236. * @ret rc Zero if they match, or a negative error code if not
  237. */
  238. int wpa_check_pmkid ( struct wpa_common_ctx *ctx, const u8 *pmkid )
  239. {
  240. u8 sha1_ctx[SHA1_CTX_SIZE];
  241. u8 my_pmkid[SHA1_SIZE];
  242. u8 pmk[ctx->pmk_len];
  243. size_t pmk_len;
  244. struct {
  245. char name[8];
  246. u8 aa[ETH_ALEN];
  247. u8 spa[ETH_ALEN];
  248. } __attribute__ (( packed )) pmkid_data;
  249. memcpy ( pmk, ctx->pmk, ctx->pmk_len );
  250. pmk_len = ctx->pmk_len;
  251. memcpy ( pmkid_data.name, "PMK Name", 8 );
  252. memcpy ( pmkid_data.aa, ctx->dev->bssid, ETH_ALEN );
  253. memcpy ( pmkid_data.spa, ctx->dev->netdev->ll_addr, ETH_ALEN );
  254. hmac_init ( &sha1_algorithm, sha1_ctx, pmk, &pmk_len );
  255. hmac_update ( &sha1_algorithm, sha1_ctx, &pmkid_data,
  256. sizeof ( pmkid_data ) );
  257. hmac_final ( &sha1_algorithm, sha1_ctx, pmk, &pmk_len, my_pmkid );
  258. if ( memcmp ( my_pmkid, pmkid, WPA_PMKID_LEN ) != 0 )
  259. return -EACCES;
  260. return 0;
  261. }
  262. /**
  263. * Derive pairwise transient key
  264. *
  265. * @v ctx WPA common context
  266. */
  267. static void wpa_derive_ptk ( struct wpa_common_ctx *ctx )
  268. {
  269. struct {
  270. u8 mac1[ETH_ALEN];
  271. u8 mac2[ETH_ALEN];
  272. u8 nonce1[WPA_NONCE_LEN];
  273. u8 nonce2[WPA_NONCE_LEN];
  274. } __attribute__ (( packed )) ptk_data;
  275. /* The addresses and nonces are stored in numerical order (!) */
  276. if ( memcmp ( ctx->dev->netdev->ll_addr, ctx->dev->bssid,
  277. ETH_ALEN ) < 0 ) {
  278. memcpy ( ptk_data.mac1, ctx->dev->netdev->ll_addr, ETH_ALEN );
  279. memcpy ( ptk_data.mac2, ctx->dev->bssid, ETH_ALEN );
  280. } else {
  281. memcpy ( ptk_data.mac1, ctx->dev->bssid, ETH_ALEN );
  282. memcpy ( ptk_data.mac2, ctx->dev->netdev->ll_addr, ETH_ALEN );
  283. }
  284. if ( memcmp ( ctx->Anonce, ctx->Snonce, WPA_NONCE_LEN ) < 0 ) {
  285. memcpy ( ptk_data.nonce1, ctx->Anonce, WPA_NONCE_LEN );
  286. memcpy ( ptk_data.nonce2, ctx->Snonce, WPA_NONCE_LEN );
  287. } else {
  288. memcpy ( ptk_data.nonce1, ctx->Snonce, WPA_NONCE_LEN );
  289. memcpy ( ptk_data.nonce2, ctx->Anonce, WPA_NONCE_LEN );
  290. }
  291. DBGC2 ( ctx, "WPA %p A1 %s, A2 %s\n", ctx, eth_ntoa ( ptk_data.mac1 ),
  292. eth_ntoa ( ptk_data.mac2 ) );
  293. DBGC2 ( ctx, "WPA %p Nonce1, Nonce2:\n", ctx );
  294. DBGC2_HD ( ctx, ptk_data.nonce1, WPA_NONCE_LEN );
  295. DBGC2_HD ( ctx, ptk_data.nonce2, WPA_NONCE_LEN );
  296. prf_sha1 ( ctx->pmk, ctx->pmk_len,
  297. "Pairwise key expansion",
  298. &ptk_data, sizeof ( ptk_data ),
  299. &ctx->ptk, sizeof ( ctx->ptk ) );
  300. DBGC2 ( ctx, "WPA %p PTK:\n", ctx );
  301. DBGC2_HD ( ctx, &ctx->ptk, sizeof ( ctx->ptk ) );
  302. }
  303. /**
  304. * Install pairwise transient key
  305. *
  306. * @v ctx WPA common context
  307. * @v len Key length (16 for CCMP, 32 for TKIP)
  308. * @ret rc Return status code
  309. */
  310. static inline int wpa_install_ptk ( struct wpa_common_ctx *ctx, int len )
  311. {
  312. DBGC ( ctx, "WPA %p: installing %d-byte pairwise transient key\n",
  313. ctx, len );
  314. DBGC2_HD ( ctx, &ctx->ptk.tk, len );
  315. return sec80211_install ( &ctx->dev->crypto, ctx->crypt,
  316. &ctx->ptk.tk, len, NULL );
  317. }
  318. /**
  319. * Install group transient key
  320. *
  321. * @v ctx WPA common context
  322. * @v len Key length (16 for CCMP, 32 for TKIP)
  323. * @v rsc Receive sequence counter field in EAPOL-Key packet
  324. * @ret rc Return status code
  325. */
  326. static inline int wpa_install_gtk ( struct wpa_common_ctx *ctx, int len,
  327. const void *rsc )
  328. {
  329. DBGC ( ctx, "WPA %p: installing %d-byte group transient key\n",
  330. ctx, len );
  331. DBGC2_HD ( ctx, &ctx->gtk.tk, len );
  332. return sec80211_install ( &ctx->dev->gcrypto, ctx->gcrypt,
  333. &ctx->gtk.tk, len, rsc );
  334. }
  335. /**
  336. * Search for group transient key, and install it if found
  337. *
  338. * @v ctx WPA common context
  339. * @v ie Pointer to first IE in key data field
  340. * @v ie_end Pointer to first byte not in key data field
  341. * @v rsc Receive sequence counter field in EAPOL-Key packet
  342. * @ret rc Return status code
  343. */
  344. static int wpa_maybe_install_gtk ( struct wpa_common_ctx *ctx,
  345. union ieee80211_ie *ie, void *ie_end,
  346. const void *rsc )
  347. {
  348. struct wpa_kde *kde;
  349. if ( ! ieee80211_ie_bound ( ie, ie_end ) )
  350. return -ENOENT;
  351. while ( ie ) {
  352. if ( ie->id == IEEE80211_IE_VENDOR &&
  353. ie->vendor.oui == WPA_KDE_GTK )
  354. break;
  355. ie = ieee80211_next_ie ( ie, ie_end );
  356. }
  357. if ( ! ie )
  358. return -ENOENT;
  359. if ( ie->len - 6u > sizeof ( ctx->gtk.tk ) ) {
  360. DBGC ( ctx, "WPA %p: GTK KDE is too long (%d bytes, max %d)\n",
  361. ctx, ie->len - 4, sizeof ( ctx->gtk.tk ) );
  362. return -EINVAL;
  363. }
  364. /* XXX We ignore key ID for now. */
  365. kde = ( struct wpa_kde * ) ie;
  366. memcpy ( &ctx->gtk.tk, &kde->gtk_encap.gtk, kde->len - 6 );
  367. return wpa_install_gtk ( ctx, kde->len - 6, rsc );
  368. }
  369. /**
  370. * Allocate I/O buffer for construction of outgoing EAPOL-Key frame
  371. *
  372. * @v kdlen Maximum number of bytes in the Key Data field
  373. * @ret iob Newly allocated I/O buffer
  374. *
  375. * The returned buffer will have space reserved for the link-layer and
  376. * EAPOL headers, and will have @c iob->tail pointing to the start of
  377. * the Key Data field. Thus, it is necessary to use iob_put() in
  378. * filling the Key Data.
  379. */
  380. static struct io_buffer * wpa_alloc_frame ( int kdlen )
  381. {
  382. struct io_buffer *ret = alloc_iob ( sizeof ( struct eapol_key_pkt ) +
  383. kdlen + EAPOL_HDR_LEN +
  384. MAX_LL_HEADER_LEN );
  385. if ( ! ret )
  386. return NULL;
  387. iob_reserve ( ret, MAX_LL_HEADER_LEN + EAPOL_HDR_LEN );
  388. memset ( iob_put ( ret, sizeof ( struct eapol_key_pkt ) ), 0,
  389. sizeof ( struct eapol_key_pkt ) );
  390. return ret;
  391. }
  392. /**
  393. * Send EAPOL-Key packet
  394. *
  395. * @v iob I/O buffer, with sufficient headroom for headers
  396. * @v dev 802.11 device
  397. * @v kie Key integrity and encryption handler
  398. * @v is_rsn If TRUE, handshake uses new RSN format
  399. * @ret rc Return status code
  400. *
  401. * If a KIE is specified, the MIC will be filled in before transmission.
  402. */
  403. static int wpa_send_eapol ( struct io_buffer *iob, struct wpa_common_ctx *ctx,
  404. struct wpa_kie *kie )
  405. {
  406. struct eapol_key_pkt *pkt = iob->data;
  407. struct eapol_frame *eapol = iob_push ( iob, EAPOL_HDR_LEN );
  408. pkt->info = htons ( pkt->info );
  409. pkt->keysize = htons ( pkt->keysize );
  410. pkt->datalen = htons ( pkt->datalen );
  411. pkt->replay = cpu_to_be64 ( pkt->replay );
  412. eapol->version = EAPOL_THIS_VERSION;
  413. eapol->type = EAPOL_TYPE_KEY;
  414. eapol->length = htons ( iob->tail - iob->data - sizeof ( *eapol ) );
  415. memset ( pkt->mic, 0, sizeof ( pkt->mic ) );
  416. if ( kie )
  417. kie->mic ( &ctx->ptk.kck, eapol, EAPOL_HDR_LEN +
  418. sizeof ( *pkt ) + ntohs ( pkt->datalen ),
  419. pkt->mic );
  420. return net_tx ( iob, ctx->dev->netdev, &eapol_protocol,
  421. ctx->dev->bssid );
  422. }
  423. /**
  424. * Send second frame in 4-Way Handshake
  425. *
  426. * @v ctx WPA common context
  427. * @v pkt First frame, to which this is a reply
  428. * @v is_rsn If TRUE, handshake uses new RSN format
  429. * @v kie Key integrity and encryption handler
  430. * @ret rc Return status code
  431. */
  432. static int wpa_send_2_of_4 ( struct wpa_common_ctx *ctx,
  433. struct eapol_key_pkt *pkt, int is_rsn,
  434. struct wpa_kie *kie )
  435. {
  436. struct io_buffer *iob = wpa_alloc_frame ( ctx->dev->rsn_ie->len + 2 );
  437. struct eapol_key_pkt *npkt;
  438. if ( ! iob )
  439. return -ENOMEM;
  440. npkt = iob->data;
  441. memcpy ( npkt, pkt, sizeof ( *pkt ) );
  442. npkt->info &= ~EAPOL_KEY_INFO_KEY_ACK;
  443. npkt->info |= EAPOL_KEY_INFO_KEY_MIC;
  444. if ( is_rsn )
  445. npkt->keysize = 0;
  446. memcpy ( npkt->nonce, ctx->Snonce, sizeof ( npkt->nonce ) );
  447. npkt->datalen = ctx->dev->rsn_ie->len + 2;
  448. memcpy ( iob_put ( iob, npkt->datalen ), ctx->dev->rsn_ie,
  449. npkt->datalen );
  450. DBGC ( ctx, "WPA %p: sending 2/4\n", ctx );
  451. return wpa_send_eapol ( iob, ctx, kie );
  452. }
  453. /**
  454. * Handle receipt of first frame in 4-Way Handshake
  455. *
  456. * @v ctx WPA common context
  457. * @v pkt EAPOL-Key packet
  458. * @v is_rsn If TRUE, frame uses new RSN format
  459. * @v kie Key integrity and encryption handler
  460. * @ret rc Return status code
  461. */
  462. static int wpa_handle_1_of_4 ( struct wpa_common_ctx *ctx,
  463. struct eapol_key_pkt *pkt, int is_rsn,
  464. struct wpa_kie *kie )
  465. {
  466. int rc;
  467. if ( ctx->state == WPA_WAITING )
  468. return -EINVAL;
  469. ctx->state = WPA_WORKING;
  470. memcpy ( ctx->Anonce, pkt->nonce, sizeof ( ctx->Anonce ) );
  471. if ( ! ctx->have_Snonce ) {
  472. get_random_bytes ( ctx->Snonce, sizeof ( ctx->Snonce ) );
  473. ctx->have_Snonce = 1;
  474. }
  475. if ( is_rsn && pkt->datalen ) {
  476. union ieee80211_ie *ie = ( union ieee80211_ie * ) pkt->data;
  477. void *ie_end = pkt->data + pkt->datalen;
  478. if ( ! ieee80211_ie_bound ( ie, ie_end ) ) {
  479. DBGC ( ctx, "WPA %p: malformed PMKID KDE\n", ctx );
  480. return wpa_fail ( ctx, -EINVAL );
  481. }
  482. while ( ie ) {
  483. if ( ie->id == IEEE80211_IE_VENDOR &&
  484. ie->vendor.oui == WPA_KDE_PMKID ) {
  485. rc = wpa_check_pmkid ( ctx, ie->vendor.data );
  486. if ( rc < 0 ) {
  487. DBGC ( ctx, "WPA %p ALERT: PMKID "
  488. "mismatch in 1/4\n", ctx );
  489. return wpa_fail ( ctx, rc );
  490. }
  491. }
  492. ie = ieee80211_next_ie ( ie, ie_end );
  493. }
  494. }
  495. DBGC ( ctx, "WPA %p: received 1/4, looks OK\n", ctx );
  496. wpa_derive_ptk ( ctx );
  497. return wpa_send_2_of_4 ( ctx, pkt, is_rsn, kie );
  498. }
  499. /**
  500. * Send fourth frame in 4-Way Handshake, or second in Group Key Handshake
  501. *
  502. * @v ctx WPA common context
  503. * @v pkt EAPOL-Key packet for frame to which we're replying
  504. * @v is_rsn If TRUE, frame uses new RSN format
  505. * @v kie Key integrity and encryption handler
  506. * @ret rc Return status code
  507. */
  508. static int wpa_send_final ( struct wpa_common_ctx *ctx,
  509. struct eapol_key_pkt *pkt, int is_rsn,
  510. struct wpa_kie *kie )
  511. {
  512. struct io_buffer *iob = wpa_alloc_frame ( 0 );
  513. struct eapol_key_pkt *npkt;
  514. if ( ! iob )
  515. return -ENOMEM;
  516. npkt = iob->data;
  517. memcpy ( npkt, pkt, sizeof ( *pkt ) );
  518. npkt->info &= ~( EAPOL_KEY_INFO_KEY_ACK | EAPOL_KEY_INFO_INSTALL |
  519. EAPOL_KEY_INFO_KEY_ENC );
  520. if ( is_rsn )
  521. npkt->keysize = 0;
  522. memset ( npkt->nonce, 0, sizeof ( npkt->nonce ) );
  523. memset ( npkt->iv, 0, sizeof ( npkt->iv ) );
  524. npkt->datalen = 0;
  525. if ( npkt->info & EAPOL_KEY_INFO_TYPE )
  526. DBGC ( ctx, "WPA %p: sending 4/4\n", ctx );
  527. else
  528. DBGC ( ctx, "WPA %p: sending 2/2\n", ctx );
  529. return wpa_send_eapol ( iob, ctx, kie );
  530. }
  531. /**
  532. * Handle receipt of third frame in 4-Way Handshake
  533. *
  534. * @v ctx WPA common context
  535. * @v pkt EAPOL-Key packet
  536. * @v is_rsn If TRUE, frame uses new RSN format
  537. * @v kie Key integrity and encryption handler
  538. * @ret rc Return status code
  539. */
  540. static int wpa_handle_3_of_4 ( struct wpa_common_ctx *ctx,
  541. struct eapol_key_pkt *pkt, int is_rsn,
  542. struct wpa_kie *kie )
  543. {
  544. int rc;
  545. u8 *this_rsn, *this_rsn_end;
  546. u8 *new_rsn, *new_rsn_end;
  547. int this_is_rsn, new_is_rsn;
  548. if ( ctx->state == WPA_WAITING )
  549. return -EINVAL;
  550. ctx->state = WPA_WORKING;
  551. /* Check nonce */
  552. if ( memcmp ( ctx->Anonce, pkt->nonce, WPA_NONCE_LEN ) != 0 ) {
  553. DBGC ( ctx, "WPA %p ALERT: nonce mismatch in 3/4\n", ctx );
  554. return wpa_fail ( ctx, -EACCES );
  555. }
  556. /* Check RSN IE */
  557. this_rsn = sec80211_find_rsn ( ( union ieee80211_ie * ) pkt->data,
  558. pkt->data + pkt->datalen,
  559. &this_is_rsn, &this_rsn_end );
  560. if ( this_rsn )
  561. new_rsn = sec80211_find_rsn ( ( union ieee80211_ie * )
  562. this_rsn_end,
  563. pkt->data + pkt->datalen,
  564. &new_is_rsn, &new_rsn_end );
  565. else
  566. new_rsn = NULL;
  567. if ( ! ctx->ap_rsn_ie || ! this_rsn ||
  568. ctx->ap_rsn_ie_len != ( this_rsn_end - this_rsn ) ||
  569. ctx->ap_rsn_is_rsn != this_is_rsn ||
  570. memcmp ( ctx->ap_rsn_ie, this_rsn, ctx->ap_rsn_ie_len ) != 0 ) {
  571. DBGC ( ctx, "WPA %p ALERT: RSN mismatch in 3/4\n", ctx );
  572. DBGC2 ( ctx, "WPA %p RSNs (in 3/4, in beacon):\n", ctx );
  573. DBGC2_HD ( ctx, this_rsn, this_rsn_end - this_rsn );
  574. DBGC2_HD ( ctx, ctx->ap_rsn_ie, ctx->ap_rsn_ie_len );
  575. return wpa_fail ( ctx, -EACCES );
  576. }
  577. /* Don't switch if they just supplied both styles of IE
  578. simultaneously; we need two RSN IEs or two WPA IEs to
  579. switch ciphers. They'll be immediately consecutive because
  580. of ordering guarantees. */
  581. if ( new_rsn && this_is_rsn == new_is_rsn ) {
  582. struct net80211_wlan *assoc = ctx->dev->associating;
  583. DBGC ( ctx, "WPA %p: accommodating bait-and-switch tactics\n",
  584. ctx );
  585. DBGC2 ( ctx, "WPA %p RSNs (in 3/4+beacon, new in 3/4):\n",
  586. ctx );
  587. DBGC2_HD ( ctx, this_rsn, this_rsn_end - this_rsn );
  588. DBGC2_HD ( ctx, new_rsn, new_rsn_end - new_rsn );
  589. if ( ( rc = sec80211_detect_ie ( new_is_rsn, new_rsn,
  590. new_rsn_end,
  591. &assoc->handshaking,
  592. &assoc->crypto ) ) != 0 )
  593. DBGC ( ctx, "WPA %p: bait-and-switch invalid, staying "
  594. "with original request\n", ctx );
  595. } else {
  596. new_rsn = this_rsn;
  597. new_is_rsn = this_is_rsn;
  598. new_rsn_end = this_rsn_end;
  599. }
  600. /* Grab group cryptosystem ID */
  601. ctx->gcrypt = sec80211_rsn_get_net80211_crypt ( *( u32 * )
  602. ( new_rsn + 2 ) );
  603. /* Check for a GTK, if info field is encrypted */
  604. if ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) {
  605. rc = wpa_maybe_install_gtk ( ctx,
  606. ( union ieee80211_ie * ) pkt->data,
  607. pkt->data + pkt->datalen,
  608. pkt->rsc );
  609. if ( rc < 0 ) {
  610. DBGC ( ctx, "WPA %p did not install GTK in 3/4: %s\n",
  611. ctx, strerror ( rc ) );
  612. if ( rc != -ENOENT )
  613. return wpa_fail ( ctx, rc );
  614. }
  615. }
  616. DBGC ( ctx, "WPA %p: received 3/4, looks OK\n", ctx );
  617. /* Send final message */
  618. rc = wpa_send_final ( ctx, pkt, is_rsn, kie );
  619. if ( rc < 0 )
  620. return wpa_fail ( ctx, rc );
  621. /* Install PTK */
  622. rc = wpa_install_ptk ( ctx, pkt->keysize );
  623. if ( rc < 0 ) {
  624. DBGC ( ctx, "WPA %p failed to install PTK: %s\n", ctx,
  625. strerror ( rc ) );
  626. return wpa_fail ( ctx, rc );
  627. }
  628. /* Mark us as needing a new Snonce if we rekey */
  629. ctx->have_Snonce = 0;
  630. /* Done! */
  631. ctx->state = WPA_SUCCESS;
  632. return 0;
  633. }
  634. /**
  635. * Handle receipt of first frame in Group Key Handshake
  636. *
  637. * @v ctx WPA common context
  638. * @v pkt EAPOL-Key packet
  639. * @v is_rsn If TRUE, frame uses new RSN format
  640. * @v kie Key integrity and encryption handler
  641. * @ret rc Return status code
  642. */
  643. static int wpa_handle_1_of_2 ( struct wpa_common_ctx *ctx,
  644. struct eapol_key_pkt *pkt, int is_rsn,
  645. struct wpa_kie *kie )
  646. {
  647. int rc;
  648. /*
  649. * WPA and RSN do this completely differently.
  650. *
  651. * The idea of encoding the GTK (or PMKID, or various other
  652. * things) into a KDE that looks like an information element
  653. * is an RSN innovation; old WPA code never encapsulates
  654. * things like that. If it looks like an info element, it
  655. * really is (for the WPA IE check in frames 2/4 and 3/4). The
  656. * "key data encrypted" bit in the info field is also specific
  657. * to RSN.
  658. *
  659. * So from an old WPA host, 3/4 does not contain an
  660. * encapsulated GTK. The first frame of the GK handshake
  661. * contains it, encrypted, but without a KDE wrapper, and with
  662. * the key ID field (which gPXE doesn't use) shoved away in
  663. * the reserved bits in the info field, and the TxRx bit
  664. * stealing the Install bit's spot.
  665. */
  666. if ( is_rsn && ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) ) {
  667. rc = wpa_maybe_install_gtk ( ctx,
  668. ( union ieee80211_ie * ) pkt->data,
  669. pkt->data + pkt->datalen,
  670. pkt->rsc );
  671. if ( rc < 0 ) {
  672. DBGC ( ctx, "WPA %p: failed to install GTK in 1/2: "
  673. "%s\n", ctx, strerror ( rc ) );
  674. return wpa_fail ( ctx, rc );
  675. }
  676. } else {
  677. rc = kie->decrypt ( &ctx->ptk.kek, pkt->iv, pkt->data,
  678. &pkt->datalen );
  679. if ( rc < 0 ) {
  680. DBGC ( ctx, "WPA %p: failed to decrypt GTK: %s\n",
  681. ctx, strerror ( rc ) );
  682. return rc; /* non-fatal */
  683. }
  684. if ( pkt->datalen > sizeof ( ctx->gtk.tk ) ) {
  685. DBGC ( ctx, "WPA %p: too much GTK data (%d > %d)\n",
  686. ctx, pkt->datalen, sizeof ( ctx->gtk.tk ) );
  687. return wpa_fail ( ctx, -EINVAL );
  688. }
  689. memcpy ( &ctx->gtk.tk, pkt->data, pkt->datalen );
  690. wpa_install_gtk ( ctx, pkt->datalen, pkt->rsc );
  691. }
  692. DBGC ( ctx, "WPA %p: received 1/2, looks OK\n", ctx );
  693. return wpa_send_final ( ctx, pkt, is_rsn, kie );
  694. }
  695. /**
  696. * Handle receipt of EAPOL-Key frame for WPA
  697. *
  698. * @v iob I/O buffer
  699. * @v netdev Network device
  700. * @v ll_source Source link-layer address
  701. */
  702. static int eapol_key_rx ( struct io_buffer *iob, struct net_device *netdev,
  703. const void *ll_source )
  704. {
  705. struct net80211_device *dev = net80211_get ( netdev );
  706. struct eapol_key_pkt *pkt = iob->data;
  707. int is_rsn, found_ctx;
  708. struct wpa_common_ctx *ctx;
  709. int rc = 0;
  710. struct wpa_kie *kie;
  711. u8 their_mic[16], our_mic[16];
  712. if ( pkt->type != EAPOL_KEY_TYPE_WPA &&
  713. pkt->type != EAPOL_KEY_TYPE_RSN ) {
  714. DBG ( "EAPOL-Key: packet not of 802.11 type\n" );
  715. rc = -EINVAL;
  716. goto drop;
  717. }
  718. is_rsn = ( pkt->type == EAPOL_KEY_TYPE_RSN );
  719. if ( ! dev ) {
  720. DBG ( "EAPOL-Key: packet not from 802.11\n" );
  721. rc = -EINVAL;
  722. goto drop;
  723. }
  724. if ( memcmp ( dev->bssid, ll_source, ETH_ALEN ) != 0 ) {
  725. DBG ( "EAPOL-Key: packet not from associated AP\n" );
  726. rc = -EINVAL;
  727. goto drop;
  728. }
  729. if ( ! ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_KEY_ACK ) ) {
  730. DBG ( "EAPOL-Key: packet sent in wrong direction\n" );
  731. rc = -EINVAL;
  732. goto drop;
  733. }
  734. found_ctx = 0;
  735. list_for_each_entry ( ctx, &wpa_contexts, list ) {
  736. if ( ctx->dev == dev ) {
  737. found_ctx = 1;
  738. break;
  739. }
  740. }
  741. if ( ! found_ctx ) {
  742. DBG ( "EAPOL-Key: no WPA context to handle packet for %p\n",
  743. dev );
  744. rc = -ENOENT;
  745. goto drop;
  746. }
  747. if ( ( void * ) ( pkt + 1 ) + ntohs ( pkt->datalen ) > iob->tail ) {
  748. DBGC ( ctx, "WPA %p: packet truncated (has %d extra bytes, "
  749. "states %d)\n", ctx, iob->tail - ( void * ) ( pkt + 1 ),
  750. ntohs ( pkt->datalen ) );
  751. rc = -EINVAL;
  752. goto drop;
  753. }
  754. /* Get a handle on key integrity/encryption handler */
  755. kie = wpa_find_kie ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_VERSION );
  756. if ( ! kie ) {
  757. DBGC ( ctx, "WPA %p: no support for packet version %d\n", ctx,
  758. ntohs ( pkt->info ) & EAPOL_KEY_INFO_VERSION );
  759. rc = wpa_fail ( ctx, -ENOTSUP );
  760. goto drop;
  761. }
  762. /* Check MIC */
  763. if ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_KEY_MIC ) {
  764. memcpy ( their_mic, pkt->mic, sizeof ( pkt->mic ) );
  765. memset ( pkt->mic, 0, sizeof ( pkt->mic ) );
  766. kie->mic ( &ctx->ptk.kck, ( void * ) pkt - EAPOL_HDR_LEN,
  767. EAPOL_HDR_LEN + sizeof ( *pkt ) +
  768. ntohs ( pkt->datalen ), our_mic );
  769. DBGC2 ( ctx, "WPA %p MIC comparison (theirs, ours):\n", ctx );
  770. DBGC2_HD ( ctx, their_mic, 16 );
  771. DBGC2_HD ( ctx, our_mic, 16 );
  772. if ( memcmp ( their_mic, our_mic, sizeof ( pkt->mic ) ) != 0 ) {
  773. DBGC ( ctx, "WPA %p: EAPOL MIC failure\n", ctx );
  774. goto drop;
  775. }
  776. }
  777. /* Fix byte order to local */
  778. pkt->info = ntohs ( pkt->info );
  779. pkt->keysize = ntohs ( pkt->keysize );
  780. pkt->datalen = ntohs ( pkt->datalen );
  781. pkt->replay = be64_to_cpu ( pkt->replay );
  782. /* Check replay counter */
  783. if ( ctx->replay != ~0ULL && ctx->replay >= pkt->replay ) {
  784. DBGC ( ctx, "WPA %p ALERT: Replay detected! "
  785. "(%08x:%08x >= %08x:%08x)\n", ctx,
  786. ( u32 ) ( ctx->replay >> 32 ), ( u32 ) ctx->replay,
  787. ( u32 ) ( pkt->replay >> 32 ), ( u32 ) pkt->replay );
  788. rc = 0; /* ignore without error */
  789. goto drop;
  790. }
  791. ctx->replay = pkt->replay;
  792. /* Decrypt key data */
  793. if ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) {
  794. rc = kie->decrypt ( &ctx->ptk.kek, pkt->iv, pkt->data,
  795. &pkt->datalen );
  796. if ( rc < 0 ) {
  797. DBGC ( ctx, "WPA %p: failed to decrypt packet: %s\n",
  798. ctx, strerror ( rc ) );
  799. goto drop;
  800. }
  801. }
  802. /* Hand it off to appropriate handler */
  803. switch ( pkt->info & ( EAPOL_KEY_INFO_TYPE |
  804. EAPOL_KEY_INFO_KEY_MIC ) ) {
  805. case EAPOL_KEY_TYPE_PTK:
  806. rc = wpa_handle_1_of_4 ( ctx, pkt, is_rsn, kie );
  807. break;
  808. case EAPOL_KEY_TYPE_PTK | EAPOL_KEY_INFO_KEY_MIC:
  809. rc = wpa_handle_3_of_4 ( ctx, pkt, is_rsn, kie );
  810. break;
  811. case EAPOL_KEY_TYPE_GTK | EAPOL_KEY_INFO_KEY_MIC:
  812. rc = wpa_handle_1_of_2 ( ctx, pkt, is_rsn, kie );
  813. break;
  814. default:
  815. DBGC ( ctx, "WPA %p: Invalid combination of key flags %04x\n",
  816. ctx, pkt->info );
  817. rc = -EINVAL;
  818. break;
  819. }
  820. drop:
  821. free_iob ( iob );
  822. return rc;
  823. }
  824. struct eapol_handler eapol_key_handler __eapol_handler = {
  825. .type = EAPOL_TYPE_KEY,
  826. .rx = eapol_key_rx,
  827. };
  828. /* WPA always needs EAPOL in order to be useful */
  829. REQUIRE_OBJECT ( eapol );