1 /*
2  * WPA definitions shared between hostapd and wpa_supplicant
3  * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "os.h"
16 #include "esp_supplicant/esp_wifi_driver.h"
17 #ifndef WPA_COMMON_H
18 #define WPA_COMMON_H
19 
20 #define WPA_MAX_SSID_LEN 32
21 
22 /* IEEE 802.11i */
23 #define PMKID_LEN 16
24 #define PMK_LEN 32
25 #define WPA_REPLAY_COUNTER_LEN 8
26 #define WPA_NONCE_LEN 32
27 #define WPA_KEY_RSC_LEN 8
28 #define WPA_GMK_LEN 32
29 #define WPA_GTK_MAX_LEN 32
30 
31 #define WPA_SELECTOR_LEN 4
32 #define WPA_VERSION 1
33 #define RSN_SELECTOR_LEN 4
34 #define RSN_VERSION 1
35 
36 #define RSN_SELECTOR(a, b, c, d) \
37 	((((u32) (a)) << 24) | (((u32) (b)) << 16) | (((u32) (c)) << 8) | \
38 	 (u32) (d))
39 
40 #define WPA_AUTH_KEY_MGMT_NONE RSN_SELECTOR(0x00, 0x50, 0xf2, 0)
41 #define WPA_AUTH_KEY_MGMT_UNSPEC_802_1X RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
42 #define WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X RSN_SELECTOR(0x00, 0x50, 0xf2, 2)
43 #define WPA_CIPHER_SUITE_NONE RSN_SELECTOR(0x00, 0x50, 0xf2, 0)
44 #define WPA_CIPHER_SUITE_WEP40 RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
45 #define WPA_CIPHER_SUITE_TKIP RSN_SELECTOR(0x00, 0x50, 0xf2, 2)
46 #define WPA_CIPHER_SUITE_CCMP RSN_SELECTOR(0x00, 0x50, 0xf2, 4)
47 #define WPA_CIPHER_SUITE_WEP104 RSN_SELECTOR(0x00, 0x50, 0xf2, 5)
48 
49 #define WAPI_SELECTOR(a, b, c, d) \
50 	((((u32) (a)) << 24) | (((u32) (b)) << 16) | (((u32) (c)) << 8) | \
51 	 (u32) (d))
52 
53 #define WAPI_AUTH_KEY_MGMT_NONE WAPI_SELECTOR(0x00, 0x14, 0x72, 0)
54 #define WAPI_AUTH_KEY_MGMT_CERT WAPI_SELECTOR(0x00, 0x14, 0x72, 1)
55 #define WAPI_AUTH_KEY_MGMT_PSK WAPI_SELECTOR(0x00, 0x14, 0x72, 2)
56 #define WAPI_CIPHER_SUITE_NONE WAPI_SELECTOR(0x00, 0x14, 0x72, 0)
57 #define WAPI_CIPHER_SUITE_SMS4 WAPI_SELECTOR(0x00, 0x14, 0x72, 1)
58 
59 #define RSN_AUTH_KEY_MGMT_UNSPEC_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
60 #define RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 2)
61 #ifdef CONFIG_IEEE80211R
62 #define RSN_AUTH_KEY_MGMT_FT_802_1X RSN_SELECTOR(0x00, 0x0f, 0xac, 3)
63 #define RSN_AUTH_KEY_MGMT_FT_PSK RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
64 #endif /* CONFIG_IEEE80211R */
65 #define RSN_AUTH_KEY_MGMT_802_1X_SHA256 RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
66 #define RSN_AUTH_KEY_MGMT_PSK_SHA256 RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
67 #define RSN_AUTH_KEY_MGMT_SAE RSN_SELECTOR(0x00, 0x0f, 0xac, 8)
68 
69 #define RSN_CIPHER_SUITE_NONE RSN_SELECTOR(0x00, 0x0f, 0xac, 0)
70 #define RSN_CIPHER_SUITE_WEP40 RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
71 #define RSN_CIPHER_SUITE_TKIP RSN_SELECTOR(0x00, 0x0f, 0xac, 2)
72 #define RSN_CIPHER_SUITE_CCMP RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
73 #define RSN_CIPHER_SUITE_WEP104 RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
74 #ifdef CONFIG_IEEE80211W
75 #define RSN_CIPHER_SUITE_AES_128_CMAC RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
76 #endif /* CONFIG_IEEE80211W */
77 #define RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED RSN_SELECTOR(0x00, 0x0f, 0xac, 7)
78 #define RSN_CIPHER_SUITE_GCMP RSN_SELECTOR(0x00, 0x0f, 0xac, 8)
79 
80 /* EAPOL-Key Key Data Encapsulation
81  * GroupKey and PeerKey require encryption, otherwise, encryption is optional.
82  */
83 #define RSN_KEY_DATA_GROUPKEY RSN_SELECTOR(0x00, 0x0f, 0xac, 1)
84 #define RSN_KEY_DATA_MAC_ADDR RSN_SELECTOR(0x00, 0x0f, 0xac, 3)
85 #define RSN_KEY_DATA_PMKID RSN_SELECTOR(0x00, 0x0f, 0xac, 4)
86 #ifdef CONFIG_PEERKEY
87 #define RSN_KEY_DATA_SMK RSN_SELECTOR(0x00, 0x0f, 0xac, 5)
88 #define RSN_KEY_DATA_NONCE RSN_SELECTOR(0x00, 0x0f, 0xac, 6)
89 #define RSN_KEY_DATA_LIFETIME RSN_SELECTOR(0x00, 0x0f, 0xac, 7)
90 #define RSN_KEY_DATA_ERROR RSN_SELECTOR(0x00, 0x0f, 0xac, 8)
91 #endif /* CONFIG_PEERKEY */
92 #ifdef CONFIG_IEEE80211W
93 #define RSN_KEY_DATA_IGTK RSN_SELECTOR(0x00, 0x0f, 0xac, 9)
94 #endif /* CONFIG_IEEE80211W */
95 
96 #define WPA_OUI_TYPE RSN_SELECTOR(0x00, 0x50, 0xf2, 1)
97 
98 #define RSN_SELECTOR_PUT(a, val) WPA_PUT_BE32((u8 *) (a), (val))
99 #define RSN_SELECTOR_GET(a) WPA_GET_BE32((const u8 *) (a))
100 
101 #define RSN_NUM_REPLAY_COUNTERS_1 0
102 #define RSN_NUM_REPLAY_COUNTERS_2 1
103 #define RSN_NUM_REPLAY_COUNTERS_4 2
104 #define RSN_NUM_REPLAY_COUNTERS_16 3
105 
106 #ifdef _MSC_VER
107 #pragma pack(push, 1)
108 #endif /* _MSC_VER */
109 
110 #ifdef CONFIG_IEEE80211W
111 #define WPA_IGTK_LEN 16
112 #endif /* CONFIG_IEEE80211W */
113 
114 
115 /* IEEE 802.11, 7.3.2.25.3 RSN Capabilities */
116 #define WPA_CAPABILITY_PREAUTH BIT(0)
117 #define WPA_CAPABILITY_NO_PAIRWISE BIT(1)
118 /* B2-B3: PTKSA Replay Counter */
119 /* B4-B5: GTKSA Replay Counter */
120 #define WPA_CAPABILITY_MFPR BIT(6)
121 #define WPA_CAPABILITY_MFPC BIT(7)
122 #define WPA_CAPABILITY_PEERKEY_ENABLED BIT(9)
123 #define WPA_CAPABILITY_SPP_CAPABLE BIT(10)
124 #define WPA_CAPABILITY_SPP_REQUIRED BIT(11)
125 
126 
127 /* IEEE 802.11r */
128 #define MOBILITY_DOMAIN_ID_LEN 2
129 #define FT_R0KH_ID_MAX_LEN 48
130 #define FT_R1KH_ID_LEN 6
131 #define WPA_PMK_NAME_LEN 16
132 
133 
134 /* IEEE 802.11, 8.5.2 EAPOL-Key frames */
135 #define WPA_KEY_INFO_TYPE_MASK ((u16) (BIT(0) | BIT(1) | BIT(2)))
136 #define WPA_KEY_INFO_TYPE_AKM_DEFINED 0
137 #define WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 BIT(0)
138 #define WPA_KEY_INFO_TYPE_HMAC_SHA1_AES BIT(1)
139 #define WPA_KEY_INFO_TYPE_AES_128_CMAC 3
140 #define WPA_KEY_INFO_KEY_TYPE BIT(3) /* 1 = Pairwise, 0 = Group key */
141 /* bit4..5 is used in WPA, but is reserved in IEEE 802.11i/RSN */
142 #define WPA_KEY_INFO_KEY_INDEX_MASK (BIT(4) | BIT(5))
143 #define WPA_KEY_INFO_KEY_INDEX_SHIFT 4
144 #define WPA_KEY_INFO_INSTALL BIT(6) /* pairwise */
145 #define WPA_KEY_INFO_TXRX BIT(6) /* group */
146 #define WPA_KEY_INFO_ACK BIT(7)
147 #define WPA_KEY_INFO_MIC BIT(8)
148 #define WPA_KEY_INFO_SECURE BIT(9)
149 #define WPA_KEY_INFO_ERROR BIT(10)
150 #define WPA_KEY_INFO_REQUEST BIT(11)
151 #define WPA_KEY_INFO_ENCR_KEY_DATA BIT(12) /* IEEE 802.11i/RSN only */
152 #define WPA_KEY_INFO_SMK_MESSAGE BIT(13)
153 
154 
155 struct wpa_eapol_key {
156 	u8 type;
157 	/* Note: key_info, key_length, and key_data_length are unaligned */
158 	u8 key_info[2]; /* big endian */
159 	u8 key_length[2]; /* big endian */
160 	u8 replay_counter[WPA_REPLAY_COUNTER_LEN];
161 	u8 key_nonce[WPA_NONCE_LEN];
162 	u8 key_iv[16];
163 	u8 key_rsc[WPA_KEY_RSC_LEN];
164 	u8 key_id[8]; /* Reserved in IEEE 802.11i/RSN */
165 	u8 key_mic[16];
166 	u8 key_data_length[2]; /* big endian */
167 	/* followed by key_data_length bytes of key_data */
168 } STRUCT_PACKED;
169 
170 /**
171  * struct wpa_ptk - WPA Pairwise Transient Key
172  * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy
173  */
174 struct wpa_ptk {
175 	u8 kck[16]; /* EAPOL-Key Key Confirmation Key (KCK) */
176 	u8 kek[16]; /* EAPOL-Key Key Encryption Key (KEK) */
177 	u8 tk1[16]; /* Temporal Key 1 (TK1) */
178 	union {
179 		u8 tk2[16]; /* Temporal Key 2 (TK2) */
180 		struct {
181 			u8 tx_mic_key[8];
182 			u8 rx_mic_key[8];
183 		} auth;
184 	} u;
185 } STRUCT_PACKED;
186 
187 struct wpa_gtk_data {
188 	enum wpa_alg alg;
189 	int tx, key_rsc_len, keyidx;
190 	u8 gtk[32];
191 	int gtk_len;
192 };
193 
194 
195 /* WPA IE version 1
196  * 00-50-f2:1 (OUI:OUI type)
197  * 0x01 0x00 (version; little endian)
198  * (all following fields are optional:)
199  * Group Suite Selector (4 octets) (default: TKIP)
200  * Pairwise Suite Count (2 octets, little endian) (default: 1)
201  * Pairwise Suite List (4 * n octets) (default: TKIP)
202  * Authenticated Key Management Suite Count (2 octets, little endian)
203  *    (default: 1)
204  * Authenticated Key Management Suite List (4 * n octets)
205  *    (default: unspec 802.1X)
206  * WPA Capabilities (2 octets, little endian) (default: 0)
207  */
208 
209 struct wpa_ie_hdr {
210 	u8 elem_id;
211 	u8 len;
212 	u8 oui[4]; /* 24-bit OUI followed by 8-bit OUI type */
213 	u8 version[2]; /* little endian */
214 } STRUCT_PACKED;
215 
216 
217 /* 1/4: PMKID
218  * 2/4: RSN IE
219  * 3/4: one or two RSN IEs + GTK IE (encrypted)
220  * 4/4: empty
221  * 1/2: GTK IE (encrypted)
222  * 2/2: empty
223  */
224 
225 /* RSN IE version 1
226  * 0x01 0x00 (version; little endian)
227  * (all following fields are optional:)
228  * Group Suite Selector (4 octets) (default: CCMP)
229  * Pairwise Suite Count (2 octets, little endian) (default: 1)
230  * Pairwise Suite List (4 * n octets) (default: CCMP)
231  * Authenticated Key Management Suite Count (2 octets, little endian)
232  *    (default: 1)
233  * Authenticated Key Management Suite List (4 * n octets)
234  *    (default: unspec 802.1X)
235  * RSN Capabilities (2 octets, little endian) (default: 0)
236  * PMKID Count (2 octets) (default: 0)
237  * PMKID List (16 * n octets)
238  * Management Group Cipher Suite (4 octets) (default: AES-128-CMAC)
239  */
240 
241 struct rsn_ie_hdr {
242 	u8 elem_id; /* WLAN_EID_RSN */
243 	u8 len;
244 	u8 version[2]; /* little endian */
245 } STRUCT_PACKED;
246 
247 
248 #ifdef CONFIG_PEERKEY
249 enum {
250 	STK_MUI_4WAY_STA_AP = 1,
251 	STK_MUI_4WAY_STAT_STA = 2,
252 	STK_MUI_GTK = 3,
253 	STK_MUI_SMK = 4
254 };
255 
256 enum {
257 	STK_ERR_STA_NR = 1,
258 	STK_ERR_STA_NRSN = 2,
259 	STK_ERR_CPHR_NS = 3,
260 	STK_ERR_NO_STSL = 4
261 };
262 #endif /* CONFIG_PEERKEY */
263 
264 struct rsn_error_kde {
265 	be16 mui;
266 	be16 error_type;
267 } STRUCT_PACKED;
268 
269 #ifdef CONFIG_IEEE80211W
270 struct wpa_igtk_kde {
271 	u8 keyid[2];
272 	u8 pn[6];
273 	u8 igtk[WPA_IGTK_LEN];
274 } STRUCT_PACKED;
275 #endif /* CONFIG_IEEE80211W */
276 
277 #ifdef CONFIG_IEEE80211R
278 struct rsn_mdie {
279 	u8 mobility_domain[MOBILITY_DOMAIN_ID_LEN];
280 	u8 ft_capab;
281 } STRUCT_PACKED;
282 
283 #define RSN_FT_CAPAB_FT_OVER_DS BIT(0)
284 #define RSN_FT_CAPAB_FT_RESOURCE_REQ_SUPP BIT(1)
285 
286 struct rsn_ftie {
287 	u8 mic_control[2];
288 	u8 mic[16];
289 	u8 anonce[WPA_NONCE_LEN];
290 	u8 snonce[WPA_NONCE_LEN];
291 	/* followed by optional parameters */
292 } STRUCT_PACKED;
293 
294 #define FTIE_SUBELEM_R1KH_ID 1
295 #define FTIE_SUBELEM_GTK 2
296 #define FTIE_SUBELEM_R0KH_ID 3
297 #define FTIE_SUBELEM_IGTK 4
298 
299 struct rsn_rdie {
300 	u8 id;
301 	u8 descr_count;
302 	le16 status_code;
303 } STRUCT_PACKED;
304 
305 #endif /* CONFIG_IEEE80211R */
306 
307 struct wpa_ie_data {
308 	int proto;
309 	int pairwise_cipher;
310 	int group_cipher;
311 	int key_mgmt;
312 	int capabilities;
313 	size_t num_pmkid;
314 	const u8 *pmkid;
315 	int mgmt_group_cipher;
316 };
317 
318 struct rsn_sppamsdu_sup {
319     bool capable;
320     bool require;
321 };
322 
323 const char * wpa_cipher_txt(int cipher);
324 
325 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
326 			 struct wpa_ie_data *data);
327 
328 int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len,
329 		      u8 *mic);
330 int wpa_compare_rsn_ie(int ft_initial_assoc,
331 		       const u8 *ie1, size_t ie1len,
332 		       const u8 *ie2, size_t ie2len);
333 
334 void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label,
335 		    const u8 *addr1, const u8 *addr2,
336 		    const u8 *nonce1, const u8 *nonce2,
337 		    u8 *ptk, size_t ptk_len, int use_sha256);
338 
339 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
340 	       u8 *pmkid, int use_sha256);
341 
342 int wpa_cipher_key_len(int cipher);
343 
344 int wpa_cipher_to_alg(int cipher);
345 
346 u32 wpa_cipher_to_suite(int proto, int cipher);
347 
348 int wpa_cipher_put_suites(u8 *pos, int ciphers);
349 
350 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
351            struct wpa_ie_data *data);
352 
353 int rsn_cipher_put_suites(u8 *pos, int ciphers);
354 
355 #endif /* WPA_COMMON_H */
356