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