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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 <ipxe/net80211.h>
  20. #include <ipxe/sec80211.h>
  21. #include <ipxe/wpa.h>
  22. #include <ipxe/eapol.h>
  23. #include <ipxe/crypto.h>
  24. #include <ipxe/arc4.h>
  25. #include <ipxe/crc32.h>
  26. #include <ipxe/sha1.h>
  27. #include <ipxe/hmac.h>
  28. #include <ipxe/list.h>
  29. #include <ipxe/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 iPXE, 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 iPXE'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. * Derive pairwise transient key
  233. *
  234. * @v ctx WPA common context
  235. */
  236. static void wpa_derive_ptk ( struct wpa_common_ctx *ctx )
  237. {
  238. struct {
  239. u8 mac1[ETH_ALEN];
  240. u8 mac2[ETH_ALEN];
  241. u8 nonce1[WPA_NONCE_LEN];
  242. u8 nonce2[WPA_NONCE_LEN];
  243. } __attribute__ (( packed )) ptk_data;
  244. /* The addresses and nonces are stored in numerical order (!) */
  245. if ( memcmp ( ctx->dev->netdev->ll_addr, ctx->dev->bssid,
  246. ETH_ALEN ) < 0 ) {
  247. memcpy ( ptk_data.mac1, ctx->dev->netdev->ll_addr, ETH_ALEN );
  248. memcpy ( ptk_data.mac2, ctx->dev->bssid, ETH_ALEN );
  249. } else {
  250. memcpy ( ptk_data.mac1, ctx->dev->bssid, ETH_ALEN );
  251. memcpy ( ptk_data.mac2, ctx->dev->netdev->ll_addr, ETH_ALEN );
  252. }
  253. if ( memcmp ( ctx->Anonce, ctx->Snonce, WPA_NONCE_LEN ) < 0 ) {
  254. memcpy ( ptk_data.nonce1, ctx->Anonce, WPA_NONCE_LEN );
  255. memcpy ( ptk_data.nonce2, ctx->Snonce, WPA_NONCE_LEN );
  256. } else {
  257. memcpy ( ptk_data.nonce1, ctx->Snonce, WPA_NONCE_LEN );
  258. memcpy ( ptk_data.nonce2, ctx->Anonce, WPA_NONCE_LEN );
  259. }
  260. DBGC2 ( ctx, "WPA %p A1 %s, A2 %s\n", ctx, eth_ntoa ( ptk_data.mac1 ),
  261. eth_ntoa ( ptk_data.mac2 ) );
  262. DBGC2 ( ctx, "WPA %p Nonce1, Nonce2:\n", ctx );
  263. DBGC2_HD ( ctx, ptk_data.nonce1, WPA_NONCE_LEN );
  264. DBGC2_HD ( ctx, ptk_data.nonce2, WPA_NONCE_LEN );
  265. prf_sha1 ( ctx->pmk, ctx->pmk_len,
  266. "Pairwise key expansion",
  267. &ptk_data, sizeof ( ptk_data ),
  268. &ctx->ptk, sizeof ( ctx->ptk ) );
  269. DBGC2 ( ctx, "WPA %p PTK:\n", ctx );
  270. DBGC2_HD ( ctx, &ctx->ptk, sizeof ( ctx->ptk ) );
  271. }
  272. /**
  273. * Install pairwise transient key
  274. *
  275. * @v ctx WPA common context
  276. * @v len Key length (16 for CCMP, 32 for TKIP)
  277. * @ret rc Return status code
  278. */
  279. static inline int wpa_install_ptk ( struct wpa_common_ctx *ctx, int len )
  280. {
  281. DBGC ( ctx, "WPA %p: installing %d-byte pairwise transient key\n",
  282. ctx, len );
  283. DBGC2_HD ( ctx, &ctx->ptk.tk, len );
  284. return sec80211_install ( &ctx->dev->crypto, ctx->crypt,
  285. &ctx->ptk.tk, len, NULL );
  286. }
  287. /**
  288. * Install group transient key
  289. *
  290. * @v ctx WPA common context
  291. * @v len Key length (16 for CCMP, 32 for TKIP)
  292. * @v rsc Receive sequence counter field in EAPOL-Key packet
  293. * @ret rc Return status code
  294. */
  295. static inline int wpa_install_gtk ( struct wpa_common_ctx *ctx, int len,
  296. const void *rsc )
  297. {
  298. DBGC ( ctx, "WPA %p: installing %d-byte group transient key\n",
  299. ctx, len );
  300. DBGC2_HD ( ctx, &ctx->gtk.tk, len );
  301. return sec80211_install ( &ctx->dev->gcrypto, ctx->gcrypt,
  302. &ctx->gtk.tk, len, rsc );
  303. }
  304. /**
  305. * Search for group transient key, and install it if found
  306. *
  307. * @v ctx WPA common context
  308. * @v ie Pointer to first IE in key data field
  309. * @v ie_end Pointer to first byte not in key data field
  310. * @v rsc Receive sequence counter field in EAPOL-Key packet
  311. * @ret rc Return status code
  312. */
  313. static int wpa_maybe_install_gtk ( struct wpa_common_ctx *ctx,
  314. union ieee80211_ie *ie, void *ie_end,
  315. const void *rsc )
  316. {
  317. struct wpa_kde *kde;
  318. if ( ! ieee80211_ie_bound ( ie, ie_end ) )
  319. return -ENOENT;
  320. while ( ie ) {
  321. if ( ie->id == IEEE80211_IE_VENDOR &&
  322. ie->vendor.oui == WPA_KDE_GTK )
  323. break;
  324. ie = ieee80211_next_ie ( ie, ie_end );
  325. }
  326. if ( ! ie )
  327. return -ENOENT;
  328. if ( ie->len - 6u > sizeof ( ctx->gtk.tk ) ) {
  329. DBGC ( ctx, "WPA %p: GTK KDE is too long (%d bytes, max %zd)\n",
  330. ctx, ie->len - 4, sizeof ( ctx->gtk.tk ) );
  331. return -EINVAL;
  332. }
  333. /* XXX We ignore key ID for now. */
  334. kde = ( struct wpa_kde * ) ie;
  335. memcpy ( &ctx->gtk.tk, &kde->gtk_encap.gtk, kde->len - 6 );
  336. return wpa_install_gtk ( ctx, kde->len - 6, rsc );
  337. }
  338. /**
  339. * Allocate I/O buffer for construction of outgoing EAPOL-Key frame
  340. *
  341. * @v kdlen Maximum number of bytes in the Key Data field
  342. * @ret iob Newly allocated I/O buffer
  343. *
  344. * The returned buffer will have space reserved for the link-layer and
  345. * EAPOL headers, and will have @c iob->tail pointing to the start of
  346. * the Key Data field. Thus, it is necessary to use iob_put() in
  347. * filling the Key Data.
  348. */
  349. static struct io_buffer * wpa_alloc_frame ( int kdlen )
  350. {
  351. struct io_buffer *ret = alloc_iob ( sizeof ( struct eapol_key_pkt ) +
  352. kdlen + EAPOL_HDR_LEN +
  353. MAX_LL_HEADER_LEN );
  354. if ( ! ret )
  355. return NULL;
  356. iob_reserve ( ret, MAX_LL_HEADER_LEN + EAPOL_HDR_LEN );
  357. memset ( iob_put ( ret, sizeof ( struct eapol_key_pkt ) ), 0,
  358. sizeof ( struct eapol_key_pkt ) );
  359. return ret;
  360. }
  361. /**
  362. * Send EAPOL-Key packet
  363. *
  364. * @v iob I/O buffer, with sufficient headroom for headers
  365. * @v dev 802.11 device
  366. * @v kie Key integrity and encryption handler
  367. * @v is_rsn If TRUE, handshake uses new RSN format
  368. * @ret rc Return status code
  369. *
  370. * If a KIE is specified, the MIC will be filled in before transmission.
  371. */
  372. static int wpa_send_eapol ( struct io_buffer *iob, struct wpa_common_ctx *ctx,
  373. struct wpa_kie *kie )
  374. {
  375. struct eapol_key_pkt *pkt = iob->data;
  376. struct eapol_frame *eapol = iob_push ( iob, EAPOL_HDR_LEN );
  377. pkt->info = htons ( pkt->info );
  378. pkt->keysize = htons ( pkt->keysize );
  379. pkt->datalen = htons ( pkt->datalen );
  380. pkt->replay = cpu_to_be64 ( pkt->replay );
  381. eapol->version = EAPOL_THIS_VERSION;
  382. eapol->type = EAPOL_TYPE_KEY;
  383. eapol->length = htons ( iob->tail - iob->data - sizeof ( *eapol ) );
  384. memset ( pkt->mic, 0, sizeof ( pkt->mic ) );
  385. if ( kie )
  386. kie->mic ( &ctx->ptk.kck, eapol, EAPOL_HDR_LEN +
  387. sizeof ( *pkt ) + ntohs ( pkt->datalen ),
  388. pkt->mic );
  389. return net_tx ( iob, ctx->dev->netdev, &eapol_protocol,
  390. ctx->dev->bssid );
  391. }
  392. /**
  393. * Send second frame in 4-Way Handshake
  394. *
  395. * @v ctx WPA common context
  396. * @v pkt First frame, to which this is a reply
  397. * @v is_rsn If TRUE, handshake uses new RSN format
  398. * @v kie Key integrity and encryption handler
  399. * @ret rc Return status code
  400. */
  401. static int wpa_send_2_of_4 ( struct wpa_common_ctx *ctx,
  402. struct eapol_key_pkt *pkt, int is_rsn,
  403. struct wpa_kie *kie )
  404. {
  405. struct io_buffer *iob = wpa_alloc_frame ( ctx->dev->rsn_ie->len + 2 );
  406. struct eapol_key_pkt *npkt;
  407. if ( ! iob )
  408. return -ENOMEM;
  409. npkt = iob->data;
  410. memcpy ( npkt, pkt, sizeof ( *pkt ) );
  411. npkt->info &= ~EAPOL_KEY_INFO_KEY_ACK;
  412. npkt->info |= EAPOL_KEY_INFO_KEY_MIC;
  413. if ( is_rsn )
  414. npkt->keysize = 0;
  415. memcpy ( npkt->nonce, ctx->Snonce, sizeof ( npkt->nonce ) );
  416. npkt->datalen = ctx->dev->rsn_ie->len + 2;
  417. memcpy ( iob_put ( iob, npkt->datalen ), ctx->dev->rsn_ie,
  418. npkt->datalen );
  419. DBGC ( ctx, "WPA %p: sending 2/4\n", ctx );
  420. return wpa_send_eapol ( iob, ctx, kie );
  421. }
  422. /**
  423. * Handle receipt of first frame in 4-Way Handshake
  424. *
  425. * @v ctx WPA common context
  426. * @v pkt EAPOL-Key packet
  427. * @v is_rsn If TRUE, frame uses new RSN format
  428. * @v kie Key integrity and encryption handler
  429. * @ret rc Return status code
  430. */
  431. static int wpa_handle_1_of_4 ( struct wpa_common_ctx *ctx,
  432. struct eapol_key_pkt *pkt, int is_rsn,
  433. struct wpa_kie *kie )
  434. {
  435. if ( ctx->state == WPA_WAITING )
  436. return -EINVAL;
  437. ctx->state = WPA_WORKING;
  438. memcpy ( ctx->Anonce, pkt->nonce, sizeof ( ctx->Anonce ) );
  439. if ( ! ctx->have_Snonce ) {
  440. get_random_bytes ( ctx->Snonce, sizeof ( ctx->Snonce ) );
  441. ctx->have_Snonce = 1;
  442. }
  443. DBGC ( ctx, "WPA %p: received 1/4, looks OK\n", ctx );
  444. wpa_derive_ptk ( ctx );
  445. return wpa_send_2_of_4 ( ctx, pkt, is_rsn, kie );
  446. }
  447. /**
  448. * Send fourth frame in 4-Way Handshake, or second in Group Key Handshake
  449. *
  450. * @v ctx WPA common context
  451. * @v pkt EAPOL-Key packet for frame to which we're replying
  452. * @v is_rsn If TRUE, frame uses new RSN format
  453. * @v kie Key integrity and encryption handler
  454. * @ret rc Return status code
  455. */
  456. static int wpa_send_final ( struct wpa_common_ctx *ctx,
  457. struct eapol_key_pkt *pkt, int is_rsn,
  458. struct wpa_kie *kie )
  459. {
  460. struct io_buffer *iob = wpa_alloc_frame ( 0 );
  461. struct eapol_key_pkt *npkt;
  462. if ( ! iob )
  463. return -ENOMEM;
  464. npkt = iob->data;
  465. memcpy ( npkt, pkt, sizeof ( *pkt ) );
  466. npkt->info &= ~( EAPOL_KEY_INFO_KEY_ACK | EAPOL_KEY_INFO_INSTALL |
  467. EAPOL_KEY_INFO_KEY_ENC );
  468. if ( is_rsn )
  469. npkt->keysize = 0;
  470. memset ( npkt->nonce, 0, sizeof ( npkt->nonce ) );
  471. memset ( npkt->iv, 0, sizeof ( npkt->iv ) );
  472. npkt->datalen = 0;
  473. if ( npkt->info & EAPOL_KEY_INFO_TYPE )
  474. DBGC ( ctx, "WPA %p: sending 4/4\n", ctx );
  475. else
  476. DBGC ( ctx, "WPA %p: sending 2/2\n", ctx );
  477. return wpa_send_eapol ( iob, ctx, kie );
  478. }
  479. /**
  480. * Handle receipt of third frame in 4-Way Handshake
  481. *
  482. * @v ctx WPA common context
  483. * @v pkt EAPOL-Key packet
  484. * @v is_rsn If TRUE, frame uses new RSN format
  485. * @v kie Key integrity and encryption handler
  486. * @ret rc Return status code
  487. */
  488. static int wpa_handle_3_of_4 ( struct wpa_common_ctx *ctx,
  489. struct eapol_key_pkt *pkt, int is_rsn,
  490. struct wpa_kie *kie )
  491. {
  492. int rc;
  493. u8 *this_rsn, *this_rsn_end;
  494. u8 *new_rsn, *new_rsn_end;
  495. int this_is_rsn, new_is_rsn;
  496. if ( ctx->state == WPA_WAITING )
  497. return -EINVAL;
  498. ctx->state = WPA_WORKING;
  499. /* Check nonce */
  500. if ( memcmp ( ctx->Anonce, pkt->nonce, WPA_NONCE_LEN ) != 0 ) {
  501. DBGC ( ctx, "WPA %p ALERT: nonce mismatch in 3/4\n", ctx );
  502. return wpa_fail ( ctx, -EACCES );
  503. }
  504. /* Check RSN IE */
  505. this_rsn = sec80211_find_rsn ( ( union ieee80211_ie * ) pkt->data,
  506. pkt->data + pkt->datalen,
  507. &this_is_rsn, &this_rsn_end );
  508. if ( this_rsn )
  509. new_rsn = sec80211_find_rsn ( ( union ieee80211_ie * )
  510. this_rsn_end,
  511. pkt->data + pkt->datalen,
  512. &new_is_rsn, &new_rsn_end );
  513. else
  514. new_rsn = NULL;
  515. if ( ! ctx->ap_rsn_ie || ! this_rsn ||
  516. ctx->ap_rsn_ie_len != ( this_rsn_end - this_rsn ) ||
  517. ctx->ap_rsn_is_rsn != this_is_rsn ||
  518. memcmp ( ctx->ap_rsn_ie, this_rsn, ctx->ap_rsn_ie_len ) != 0 ) {
  519. DBGC ( ctx, "WPA %p ALERT: RSN mismatch in 3/4\n", ctx );
  520. DBGC2 ( ctx, "WPA %p RSNs (in 3/4, in beacon):\n", ctx );
  521. DBGC2_HD ( ctx, this_rsn, this_rsn_end - this_rsn );
  522. DBGC2_HD ( ctx, ctx->ap_rsn_ie, ctx->ap_rsn_ie_len );
  523. return wpa_fail ( ctx, -EACCES );
  524. }
  525. /* Don't switch if they just supplied both styles of IE
  526. simultaneously; we need two RSN IEs or two WPA IEs to
  527. switch ciphers. They'll be immediately consecutive because
  528. of ordering guarantees. */
  529. if ( new_rsn && this_is_rsn == new_is_rsn ) {
  530. struct net80211_wlan *assoc = ctx->dev->associating;
  531. DBGC ( ctx, "WPA %p: accommodating bait-and-switch tactics\n",
  532. ctx );
  533. DBGC2 ( ctx, "WPA %p RSNs (in 3/4+beacon, new in 3/4):\n",
  534. ctx );
  535. DBGC2_HD ( ctx, this_rsn, this_rsn_end - this_rsn );
  536. DBGC2_HD ( ctx, new_rsn, new_rsn_end - new_rsn );
  537. if ( ( rc = sec80211_detect_ie ( new_is_rsn, new_rsn,
  538. new_rsn_end,
  539. &assoc->handshaking,
  540. &assoc->crypto ) ) != 0 )
  541. DBGC ( ctx, "WPA %p: bait-and-switch invalid, staying "
  542. "with original request\n", ctx );
  543. } else {
  544. new_rsn = this_rsn;
  545. new_is_rsn = this_is_rsn;
  546. new_rsn_end = this_rsn_end;
  547. }
  548. /* Grab group cryptosystem ID */
  549. ctx->gcrypt = sec80211_rsn_get_net80211_crypt ( *( u32 * )
  550. ( new_rsn + 2 ) );
  551. /* Check for a GTK, if info field is encrypted */
  552. if ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) {
  553. rc = wpa_maybe_install_gtk ( ctx,
  554. ( union ieee80211_ie * ) pkt->data,
  555. pkt->data + pkt->datalen,
  556. pkt->rsc );
  557. if ( rc < 0 ) {
  558. DBGC ( ctx, "WPA %p did not install GTK in 3/4: %s\n",
  559. ctx, strerror ( rc ) );
  560. if ( rc != -ENOENT )
  561. return wpa_fail ( ctx, rc );
  562. }
  563. }
  564. DBGC ( ctx, "WPA %p: received 3/4, looks OK\n", ctx );
  565. /* Send final message */
  566. rc = wpa_send_final ( ctx, pkt, is_rsn, kie );
  567. if ( rc < 0 )
  568. return wpa_fail ( ctx, rc );
  569. /* Install PTK */
  570. rc = wpa_install_ptk ( ctx, pkt->keysize );
  571. if ( rc < 0 ) {
  572. DBGC ( ctx, "WPA %p failed to install PTK: %s\n", ctx,
  573. strerror ( rc ) );
  574. return wpa_fail ( ctx, rc );
  575. }
  576. /* Mark us as needing a new Snonce if we rekey */
  577. ctx->have_Snonce = 0;
  578. /* Done! */
  579. ctx->state = WPA_SUCCESS;
  580. return 0;
  581. }
  582. /**
  583. * Handle receipt of first frame in Group Key Handshake
  584. *
  585. * @v ctx WPA common context
  586. * @v pkt EAPOL-Key packet
  587. * @v is_rsn If TRUE, frame uses new RSN format
  588. * @v kie Key integrity and encryption handler
  589. * @ret rc Return status code
  590. */
  591. static int wpa_handle_1_of_2 ( struct wpa_common_ctx *ctx,
  592. struct eapol_key_pkt *pkt, int is_rsn,
  593. struct wpa_kie *kie )
  594. {
  595. int rc;
  596. /*
  597. * WPA and RSN do this completely differently.
  598. *
  599. * The idea of encoding the GTK (or PMKID, or various other
  600. * things) into a KDE that looks like an information element
  601. * is an RSN innovation; old WPA code never encapsulates
  602. * things like that. If it looks like an info element, it
  603. * really is (for the WPA IE check in frames 2/4 and 3/4). The
  604. * "key data encrypted" bit in the info field is also specific
  605. * to RSN.
  606. *
  607. * So from an old WPA host, 3/4 does not contain an
  608. * encapsulated GTK. The first frame of the GK handshake
  609. * contains it, encrypted, but without a KDE wrapper, and with
  610. * the key ID field (which iPXE doesn't use) shoved away in
  611. * the reserved bits in the info field, and the TxRx bit
  612. * stealing the Install bit's spot.
  613. */
  614. if ( is_rsn && ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) ) {
  615. rc = wpa_maybe_install_gtk ( ctx,
  616. ( union ieee80211_ie * ) pkt->data,
  617. pkt->data + pkt->datalen,
  618. pkt->rsc );
  619. if ( rc < 0 ) {
  620. DBGC ( ctx, "WPA %p: failed to install GTK in 1/2: "
  621. "%s\n", ctx, strerror ( rc ) );
  622. return wpa_fail ( ctx, rc );
  623. }
  624. } else {
  625. rc = kie->decrypt ( &ctx->ptk.kek, pkt->iv, pkt->data,
  626. &pkt->datalen );
  627. if ( rc < 0 ) {
  628. DBGC ( ctx, "WPA %p: failed to decrypt GTK: %s\n",
  629. ctx, strerror ( rc ) );
  630. return rc; /* non-fatal */
  631. }
  632. if ( pkt->datalen > sizeof ( ctx->gtk.tk ) ) {
  633. DBGC ( ctx, "WPA %p: too much GTK data (%d > %zd)\n",
  634. ctx, pkt->datalen, sizeof ( ctx->gtk.tk ) );
  635. return wpa_fail ( ctx, -EINVAL );
  636. }
  637. memcpy ( &ctx->gtk.tk, pkt->data, pkt->datalen );
  638. wpa_install_gtk ( ctx, pkt->datalen, pkt->rsc );
  639. }
  640. DBGC ( ctx, "WPA %p: received 1/2, looks OK\n", ctx );
  641. return wpa_send_final ( ctx, pkt, is_rsn, kie );
  642. }
  643. /**
  644. * Handle receipt of EAPOL-Key frame for WPA
  645. *
  646. * @v iob I/O buffer
  647. * @v netdev Network device
  648. * @v ll_source Source link-layer address
  649. */
  650. static int eapol_key_rx ( struct io_buffer *iob, struct net_device *netdev,
  651. const void *ll_source )
  652. {
  653. struct net80211_device *dev = net80211_get ( netdev );
  654. struct eapol_key_pkt *pkt = iob->data;
  655. int is_rsn, found_ctx;
  656. struct wpa_common_ctx *ctx;
  657. int rc = 0;
  658. struct wpa_kie *kie;
  659. u8 their_mic[16], our_mic[16];
  660. if ( pkt->type != EAPOL_KEY_TYPE_WPA &&
  661. pkt->type != EAPOL_KEY_TYPE_RSN ) {
  662. DBG ( "EAPOL-Key: packet not of 802.11 type\n" );
  663. rc = -EINVAL;
  664. goto drop;
  665. }
  666. is_rsn = ( pkt->type == EAPOL_KEY_TYPE_RSN );
  667. if ( ! dev ) {
  668. DBG ( "EAPOL-Key: packet not from 802.11\n" );
  669. rc = -EINVAL;
  670. goto drop;
  671. }
  672. if ( memcmp ( dev->bssid, ll_source, ETH_ALEN ) != 0 ) {
  673. DBG ( "EAPOL-Key: packet not from associated AP\n" );
  674. rc = -EINVAL;
  675. goto drop;
  676. }
  677. if ( ! ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_KEY_ACK ) ) {
  678. DBG ( "EAPOL-Key: packet sent in wrong direction\n" );
  679. rc = -EINVAL;
  680. goto drop;
  681. }
  682. found_ctx = 0;
  683. list_for_each_entry ( ctx, &wpa_contexts, list ) {
  684. if ( ctx->dev == dev ) {
  685. found_ctx = 1;
  686. break;
  687. }
  688. }
  689. if ( ! found_ctx ) {
  690. DBG ( "EAPOL-Key: no WPA context to handle packet for %p\n",
  691. dev );
  692. rc = -ENOENT;
  693. goto drop;
  694. }
  695. if ( ( void * ) ( pkt + 1 ) + ntohs ( pkt->datalen ) > iob->tail ) {
  696. DBGC ( ctx, "WPA %p: packet truncated (has %zd extra bytes, "
  697. "states %d)\n", ctx, iob->tail - ( void * ) ( pkt + 1 ),
  698. ntohs ( pkt->datalen ) );
  699. rc = -EINVAL;
  700. goto drop;
  701. }
  702. /* Get a handle on key integrity/encryption handler */
  703. kie = wpa_find_kie ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_VERSION );
  704. if ( ! kie ) {
  705. DBGC ( ctx, "WPA %p: no support for packet version %d\n", ctx,
  706. ntohs ( pkt->info ) & EAPOL_KEY_INFO_VERSION );
  707. rc = wpa_fail ( ctx, -ENOTSUP );
  708. goto drop;
  709. }
  710. /* Check MIC */
  711. if ( ntohs ( pkt->info ) & EAPOL_KEY_INFO_KEY_MIC ) {
  712. memcpy ( their_mic, pkt->mic, sizeof ( pkt->mic ) );
  713. memset ( pkt->mic, 0, sizeof ( pkt->mic ) );
  714. kie->mic ( &ctx->ptk.kck, ( void * ) pkt - EAPOL_HDR_LEN,
  715. EAPOL_HDR_LEN + sizeof ( *pkt ) +
  716. ntohs ( pkt->datalen ), our_mic );
  717. DBGC2 ( ctx, "WPA %p MIC comparison (theirs, ours):\n", ctx );
  718. DBGC2_HD ( ctx, their_mic, 16 );
  719. DBGC2_HD ( ctx, our_mic, 16 );
  720. if ( memcmp ( their_mic, our_mic, sizeof ( pkt->mic ) ) != 0 ) {
  721. DBGC ( ctx, "WPA %p: EAPOL MIC failure\n", ctx );
  722. goto drop;
  723. }
  724. }
  725. /* Fix byte order to local */
  726. pkt->info = ntohs ( pkt->info );
  727. pkt->keysize = ntohs ( pkt->keysize );
  728. pkt->datalen = ntohs ( pkt->datalen );
  729. pkt->replay = be64_to_cpu ( pkt->replay );
  730. /* Check replay counter */
  731. if ( ctx->replay != ~0ULL && ctx->replay >= pkt->replay ) {
  732. DBGC ( ctx, "WPA %p ALERT: Replay detected! "
  733. "(%08x:%08x >= %08x:%08x)\n", ctx,
  734. ( u32 ) ( ctx->replay >> 32 ), ( u32 ) ctx->replay,
  735. ( u32 ) ( pkt->replay >> 32 ), ( u32 ) pkt->replay );
  736. rc = 0; /* ignore without error */
  737. goto drop;
  738. }
  739. ctx->replay = pkt->replay;
  740. /* Decrypt key data */
  741. if ( pkt->info & EAPOL_KEY_INFO_KEY_ENC ) {
  742. rc = kie->decrypt ( &ctx->ptk.kek, pkt->iv, pkt->data,
  743. &pkt->datalen );
  744. if ( rc < 0 ) {
  745. DBGC ( ctx, "WPA %p: failed to decrypt packet: %s\n",
  746. ctx, strerror ( rc ) );
  747. goto drop;
  748. }
  749. }
  750. /* Hand it off to appropriate handler */
  751. switch ( pkt->info & ( EAPOL_KEY_INFO_TYPE |
  752. EAPOL_KEY_INFO_KEY_MIC ) ) {
  753. case EAPOL_KEY_TYPE_PTK:
  754. rc = wpa_handle_1_of_4 ( ctx, pkt, is_rsn, kie );
  755. break;
  756. case EAPOL_KEY_TYPE_PTK | EAPOL_KEY_INFO_KEY_MIC:
  757. rc = wpa_handle_3_of_4 ( ctx, pkt, is_rsn, kie );
  758. break;
  759. case EAPOL_KEY_TYPE_GTK | EAPOL_KEY_INFO_KEY_MIC:
  760. rc = wpa_handle_1_of_2 ( ctx, pkt, is_rsn, kie );
  761. break;
  762. default:
  763. DBGC ( ctx, "WPA %p: Invalid combination of key flags %04x\n",
  764. ctx, pkt->info );
  765. rc = -EINVAL;
  766. break;
  767. }
  768. drop:
  769. free_iob ( iob );
  770. return rc;
  771. }
  772. struct eapol_handler eapol_key_handler __eapol_handler = {
  773. .type = EAPOL_TYPE_KEY,
  774. .rx = eapol_key_rx,
  775. };
  776. /* WPA always needs EAPOL in order to be useful */
  777. REQUIRE_OBJECT ( eapol );