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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. #ifndef _IPXE_WPA_H
  20. #define _IPXE_WPA_H
  21. #include <ipxe/ieee80211.h>
  22. #include <ipxe/list.h>
  23. FILE_LICENCE ( GPL2_OR_LATER );
  24. /** @file
  25. *
  26. * Common definitions for all types of WPA-protected networks.
  27. */
  28. /** EAPOL-Key type field for modern 802.11i/RSN WPA packets */
  29. #define EAPOL_KEY_TYPE_RSN 2
  30. /** Old EAPOL-Key type field used by WPA1 hardware before 802.11i ratified */
  31. #define EAPOL_KEY_TYPE_WPA 254
  32. /**
  33. * @defgroup eapol_key_info EAPOL-Key Info field bits
  34. * @{
  35. */
  36. /** Key descriptor version, indicating WPA or WPA2 */
  37. #define EAPOL_KEY_INFO_VERSION 0x0007
  38. /** Key type bit, indicating pairwise or group */
  39. #define EAPOL_KEY_INFO_TYPE 0x0008
  40. /** Key install bit; set on message 3 except when legacy hacks are used */
  41. #define EAPOL_KEY_INFO_INSTALL 0x0040
  42. /** Key ACK bit; set when a response is required, on all messages except #4 */
  43. #define EAPOL_KEY_INFO_KEY_ACK 0x0080
  44. /** Key MIC bit; set when the MIC field is valid, on messages 3 and 4 */
  45. #define EAPOL_KEY_INFO_KEY_MIC 0x0100
  46. /** Secure bit; set when both sides have both keys, on messages 3 and 4 */
  47. #define EAPOL_KEY_INFO_SECURE 0x0200
  48. /** Error bit; set on a MIC failure for TKIP */
  49. #define EAPOL_KEY_INFO_ERROR 0x0400
  50. /** Request bit; set when authentication is initiated by the Peer (unusual) */
  51. #define EAPOL_KEY_INFO_REQUEST 0x0800
  52. /** Key Encrypted bit; set when the Key Data field is encrypted */
  53. #define EAPOL_KEY_INFO_KEY_ENC 0x1000
  54. /** SMC Message bit; set when this frame is part of an IBSS SMK handshake */
  55. #define EAPOL_KEY_INFO_SMC_MESS 0x2000
  56. /** Key descriptor version field value for WPA (TKIP) */
  57. #define EAPOL_KEY_VERSION_WPA 1
  58. /** Key descriptor version field value for WPA2 (CCMP) */
  59. #define EAPOL_KEY_VERSION_WPA2 2
  60. /** Key type field value for a PTK (pairwise) key handshake */
  61. #define EAPOL_KEY_TYPE_PTK 0x0008
  62. /** Key type field value for a GTK (group) key handshake */
  63. #define EAPOL_KEY_TYPE_GTK 0x0000
  64. /** @} */
  65. /** An EAPOL-Key packet.
  66. *
  67. * These are used for the WPA 4-Way Handshake, whether or not prior
  68. * authentication has been performed using EAP.
  69. *
  70. * On LANs, an eapol_key_pkt is always encapsulated in the data field
  71. * of an eapol_frame, with the frame's type code set to EAPOL_TYPE_KEY.
  72. *
  73. * Unlike 802.11 frame headers, the fields in this structure are
  74. * stored in big-endian!
  75. */
  76. struct eapol_key_pkt
  77. {
  78. /** One of the EAPOL_KEY_TYPE_* defines. */
  79. u8 type;
  80. /** Bitfield of key characteristics, network byte order */
  81. u16 info;
  82. /** Length of encryption key to be used, network byte order
  83. *
  84. * This is 16 for CCMP, 32 for TKIP, and 5 or 13 for WEP.
  85. */
  86. u16 keysize;
  87. /** Monotonically increasing value for EAPOL-Key conversations
  88. *
  89. * In another classic demonstration of overengineering, this
  90. * 8-byte value will rarely be anything above 1. It's stored
  91. * in network byte order.
  92. */
  93. u64 replay;
  94. /** Nonce value
  95. *
  96. * This is the authenticator's ANonce in frame 1, the peer's
  97. * SNonce in frame 2, and 0 in frames 3 and 4.
  98. */
  99. u8 nonce[32];
  100. /** Initialization vector
  101. *
  102. * This contains the IV used with the Key Encryption Key, or 0
  103. * if the key is unencrypted or encrypted using an algorithm
  104. * that does not require an IV.
  105. */
  106. u8 iv[16];
  107. /** Receive sequence counter for GTK
  108. *
  109. * This is used to synchronize the client's replay counter for
  110. * ordinary data packets. The first six bytes contain PN0
  111. * through PN5 for CCMP mode, or TSC0 through TSC5 for TKIP
  112. * mode. The last two bytes are zero.
  113. */
  114. u8 rsc[8];
  115. /** Reserved bytes */
  116. u8 _reserved[8];
  117. /** Message integrity code over the entire EAPOL frame
  118. *
  119. * This is calculated using HMAC-MD5 when the key descriptor
  120. * version field in @a info is 1, and HMAC-SHA1 ignoring the
  121. * last 4 bytes of the hash when the version field in @a info
  122. * is 2.
  123. */
  124. u8 mic[16];
  125. /** Length of the @a data field in bytes, network byte order */
  126. u16 datalen;
  127. /** Key data
  128. *
  129. * This is formatted as a series of 802.11 information
  130. * elements, with cryptographic data encapsulated using a
  131. * "vendor-specific IE" code and an IEEE-specified OUI.
  132. */
  133. u8 data[0];
  134. } __attribute__ (( packed ));
  135. /** WPA handshaking state */
  136. enum wpa_state {
  137. /** Waiting for PMK to be set */
  138. WPA_WAITING = 0,
  139. /** Ready for 4-Way Handshake */
  140. WPA_READY,
  141. /** Performing 4-Way Handshake */
  142. WPA_WORKING,
  143. /** 4-Way Handshake succeeded */
  144. WPA_SUCCESS,
  145. /** 4-Way Handshake failed */
  146. WPA_FAILURE,
  147. };
  148. /** Bitfield indicating a selection of WPA transient keys */
  149. enum wpa_keymask {
  150. /** Pairwise transient key */
  151. WPA_PTK = 1,
  152. /** Group transient key */
  153. WPA_GTK = 2,
  154. };
  155. /** Length of a nonce */
  156. #define WPA_NONCE_LEN 32
  157. /** Length of a TKIP main key */
  158. #define WPA_TKIP_KEY_LEN 16
  159. /** Length of a TKIP MIC key */
  160. #define WPA_TKIP_MIC_KEY_LEN 8
  161. /** Length of a CCMP key */
  162. #define WPA_CCMP_KEY_LEN 16
  163. /** Length of an EAPOL Key Confirmation Key */
  164. #define WPA_KCK_LEN 16
  165. /** Length of an EAPOL Key Encryption Key */
  166. #define WPA_KEK_LEN 16
  167. /** Usual length of a Pairwise Master Key */
  168. #define WPA_PMK_LEN 32
  169. /** Length of a PMKID */
  170. #define WPA_PMKID_LEN 16
  171. /** Structure of the Temporal Key for TKIP encryption */
  172. struct tkip_tk
  173. {
  174. /** Main key: input to TKIP Phase 1 and Phase 2 key mixing functions */
  175. u8 key[WPA_TKIP_KEY_LEN];
  176. /** Michael MIC keys */
  177. struct {
  178. /** MIC key for packets from the AP */
  179. u8 rx[WPA_TKIP_MIC_KEY_LEN];
  180. /** MIC key for packets to the AP */
  181. u8 tx[WPA_TKIP_MIC_KEY_LEN];
  182. } __attribute__ (( packed )) mic;
  183. } __attribute__ (( packed ));
  184. /** Structure of a generic Temporal Key */
  185. union wpa_tk
  186. {
  187. /** CCMP key */
  188. u8 ccmp[WPA_CCMP_KEY_LEN];
  189. /** TKIP keys */
  190. struct tkip_tk tkip;
  191. };
  192. /** Structure of the Pairwise Transient Key */
  193. struct wpa_ptk
  194. {
  195. /** EAPOL-Key Key Confirmation Key (KCK) */
  196. u8 kck[WPA_KCK_LEN];
  197. /** EAPOL-Key Key Encryption Key (KEK) */
  198. u8 kek[WPA_KEK_LEN];
  199. /** Temporal key */
  200. union wpa_tk tk;
  201. } __attribute__ (( packed ));
  202. /** Structure of the Group Transient Key */
  203. struct wpa_gtk
  204. {
  205. /** Temporal key */
  206. union wpa_tk tk;
  207. } __attribute__ (( packed ));
  208. /** Common context for WPA security handshaking
  209. *
  210. * Any implementor of a particular handshaking type (e.g. PSK or EAP)
  211. * must include this structure at the very beginning of their private
  212. * data context structure, to allow the EAPOL-Key handling code to
  213. * work. When the preliminary authentication is done, it is necessary
  214. * to call wpa_start(), passing the PMK (derived from PSK or EAP MSK)
  215. * as an argument. The handshaker can use its @a step function to
  216. * monitor @a state in this wpa_ctx structure for success or
  217. * failure. On success, the keys will be available in @a ptk and @a
  218. * gtk according to the state of the @a valid bitmask.
  219. *
  220. * After an initial success, the parent handshaker does not need to
  221. * concern itself with rekeying; the WPA common code takes care of
  222. * that.
  223. */
  224. struct wpa_common_ctx
  225. {
  226. /** 802.11 device we are authenticating for */
  227. struct net80211_device *dev;
  228. /** The Pairwise Master Key to use in handshaking
  229. *
  230. * This is set either by running the PBKDF2 algorithm on a
  231. * passphrase with the SSID as salt to generate a pre-shared
  232. * key, or by copying the first 32 bytes of the EAP Master
  233. * Session Key in 802.1X-served authentication.
  234. */
  235. u8 pmk[WPA_PMK_LEN];
  236. /** Length of the Pairwise Master Key
  237. *
  238. * This is always 32 except with one EAP method which only
  239. * gives 16 bytes.
  240. */
  241. int pmk_len;
  242. /** State of EAPOL-Key handshaking */
  243. enum wpa_state state;
  244. /** Replay counter for this association
  245. *
  246. * This stores the replay counter value for the most recent
  247. * packet we've accepted. It is initially initialised to ~0 to
  248. * show we'll accept anything.
  249. */
  250. u64 replay;
  251. /** Mask of valid keys after authentication success
  252. *
  253. * If the PTK is not valid, the GTK should be used for both
  254. * unicast and multicast decryption; if the GTK is not valid,
  255. * multicast packets cannot be decrypted.
  256. */
  257. enum wpa_keymask valid;
  258. /** The cipher to use for unicast RX and all TX */
  259. enum net80211_crypto_alg crypt;
  260. /** The cipher to use for broadcast and multicast RX */
  261. enum net80211_crypto_alg gcrypt;
  262. /** The Pairwise Transient Key derived from the handshake */
  263. struct wpa_ptk ptk;
  264. /** The Group Transient Key derived from the handshake */
  265. struct wpa_gtk gtk;
  266. /** Authenticator-provided nonce */
  267. u8 Anonce[WPA_NONCE_LEN];
  268. /** Supplicant-generated nonce (that's us) */
  269. u8 Snonce[WPA_NONCE_LEN];
  270. /** Whether we should refrain from generating another SNonce */
  271. int have_Snonce;
  272. /** Data in WPA or RSN IE from AP's beacon frame */
  273. void *ap_rsn_ie;
  274. /** Length of @a ap_rsn_ie */
  275. int ap_rsn_ie_len;
  276. /** Whether @a ap_rsn_ie is an RSN IE (as opposed to old WPA) */
  277. int ap_rsn_is_rsn;
  278. /** List entry */
  279. struct list_head list;
  280. };
  281. /** WPA handshake key integrity and encryption handler
  282. *
  283. * Note that due to the structure of the 4-Way Handshake we never
  284. * actually need to encrypt key data, only decrypt it.
  285. */
  286. struct wpa_kie {
  287. /** Value of version bits in EAPOL-Key info field for which to use
  288. *
  289. * This should be one of the @c EAPOL_KEY_VERSION_* constants.
  290. */
  291. int version;
  292. /** Calculate MIC over message
  293. *
  294. * @v kck Key Confirmation Key, 16 bytes
  295. * @v msg Message to calculate MIC over
  296. * @v len Number of bytes to calculate MIC over
  297. * @ret mic Calculated MIC, 16 bytes long
  298. *
  299. * The @a mic return may point within @a msg, so it must not
  300. * be filled until the calculation has been performed.
  301. */
  302. void ( * mic ) ( const void *kck, const void *msg, size_t len,
  303. void *mic );
  304. /** Decrypt key data
  305. *
  306. * @v kek Key Encryption Key, 16 bytes
  307. * @v iv Initialisation vector for encryption, 16 bytes
  308. * @v msg Message to decrypt (Key Data field)
  309. * @v len Length of message
  310. * @ret msg Decrypted message in place of original
  311. * @ret len Updated to reflect encrypted length
  312. * @ret rc Return status code
  313. *
  314. * The decrypted message is written over the encrypted one.
  315. */
  316. int ( * decrypt ) ( const void *kek, const void *iv, void *msg,
  317. u16 *len );
  318. };
  319. #define WPA_KIES __table ( struct wpa_kie, "wpa_kies" )
  320. #define __wpa_kie __table_entry ( WPA_KIES, 01 )
  321. /**
  322. * @defgroup wpa_kde Key descriptor element types
  323. * @{
  324. */
  325. /** Payload structure of the GTK-encapsulating KDE
  326. *
  327. * This does not include the IE type, length, or OUI bytes, which are
  328. * generic to all KDEs.
  329. */
  330. struct wpa_kde_gtk_encap
  331. {
  332. /** Key ID and TX bit */
  333. u8 id;
  334. /** Reserved byte */
  335. u8 _rsvd;
  336. /** Encapsulated group transient key */
  337. struct wpa_gtk gtk;
  338. } __attribute__ (( packed ));
  339. /** Mask for Key ID in wpa_kde_gtk::id field */
  340. #define WPA_GTK_KID 0x03
  341. /** Mask for Tx bit in wpa_kde_gtk::id field */
  342. #define WPA_GTK_TXBIT 0x04
  343. /** KDE type for an encapsulated Group Transient Key (requires encryption) */
  344. #define WPA_KDE_GTK _MKOUI ( 0x00, 0x0F, 0xAC, 0x01 )
  345. /** KDE type for a MAC address */
  346. #define WPA_KDE_MAC _MKOUI ( 0x00, 0x0F, 0xAC, 0x03 )
  347. /** KDE type for a PMKID */
  348. #define WPA_KDE_PMKID _MKOUI ( 0x00, 0x0F, 0xAC, 0x04 )
  349. /** KDE type for a nonce */
  350. #define WPA_KDE_NONCE _MKOUI ( 0x00, 0x0F, 0xAC, 0x06 )
  351. /** KDE type for a lifetime value */
  352. #define WPA_KDE_LIFETIME _MKOUI ( 0x00, 0x0F, 0xAC, 0x07 )
  353. /** Any key descriptor element type
  354. *
  355. * KDEs follow the 802.11 information element format of a type byte
  356. * (in this case "vendor-specific", with the requisite OUI+subtype
  357. * after length) and a length byte whose value does not include the
  358. * length of the type and length bytes.
  359. */
  360. struct wpa_kde
  361. {
  362. /** Information element type: always 0xDD (IEEE80211_IE_VENDOR) */
  363. u8 ie_type;
  364. /** Length, not including ie_type and length fields */
  365. u8 len;
  366. /** OUI + type byte */
  367. u32 oui_type;
  368. /** Payload data */
  369. union {
  370. /** For GTK-type KDEs, encapsulated GTK */
  371. struct wpa_kde_gtk_encap gtk_encap;
  372. /** For MAC-type KDEs, the MAC address */
  373. u8 mac[ETH_ALEN];
  374. /** For PMKID-type KDEs, the PMKID */
  375. u8 pmkid[WPA_PMKID_LEN];
  376. /** For Nonce-type KDEs, the nonce */
  377. u8 nonce[WPA_NONCE_LEN];
  378. /** For Lifetime-type KDEs, the lifetime in seconds
  379. *
  380. * This is in network byte order!
  381. */
  382. u32 lifetime;
  383. };
  384. } __attribute__ (( packed ));
  385. /** @} */
  386. int wpa_make_rsn_ie ( struct net80211_device *dev, union ieee80211_ie **ie );
  387. int wpa_start ( struct net80211_device *dev, struct wpa_common_ctx *ctx,
  388. const void *pmk, size_t pmk_len );
  389. void wpa_stop ( struct net80211_device *dev );
  390. #endif /* _IPXE_WPA_H */