d42502c19471eee71f67efdc99786bb21581603f
[mech_eap.git] / wpa_supplicant / mesh_rsn.c
1 /*
2  * WPA Supplicant - Mesh RSN routines
3  * Copyright (c) 2013-2014, cozybit, Inc.  All rights reserved.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "crypto/sha256.h"
14 #include "crypto/random.h"
15 #include "crypto/aes.h"
16 #include "crypto/aes_siv.h"
17 #include "rsn_supp/wpa.h"
18 #include "ap/hostapd.h"
19 #include "ap/wpa_auth.h"
20 #include "ap/sta_info.h"
21 #include "ap/ieee802_11.h"
22 #include "wpa_supplicant_i.h"
23 #include "driver_i.h"
24 #include "wpas_glue.h"
25 #include "mesh_mpm.h"
26 #include "mesh_rsn.h"
27
28 #define MESH_AUTH_TIMEOUT 10
29 #define MESH_AUTH_RETRY 3
30
31 void mesh_auth_timer(void *eloop_ctx, void *user_data)
32 {
33         struct wpa_supplicant *wpa_s = eloop_ctx;
34         struct sta_info *sta = user_data;
35
36         if (sta->sae->state != SAE_ACCEPTED) {
37                 wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
38                            " (attempt %d) ",
39                            MAC2STR(sta->addr), sta->sae_auth_retry);
40                 wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
41                         MAC2STR(sta->addr));
42                 if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
43                         mesh_rsn_auth_sae_sta(wpa_s, sta);
44                 } else {
45                         /* block the STA if exceeded the number of attempts */
46                         wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
47                         sta->sae->state = SAE_NOTHING;
48                 }
49                 sta->sae_auth_retry++;
50         }
51 }
52
53
54 static void auth_logger(void *ctx, const u8 *addr, logger_level level,
55                         const char *txt)
56 {
57         if (addr)
58                 wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
59                            MAC2STR(addr), txt);
60         else
61                 wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
62 }
63
64
65 static const u8 *auth_get_psk(void *ctx, const u8 *addr,
66                               const u8 *p2p_dev_addr, const u8 *prev_psk)
67 {
68         struct mesh_rsn *mesh_rsn = ctx;
69         struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
70         struct sta_info *sta = ap_get_sta(hapd, addr);
71
72         wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
73                    __func__, MAC2STR(addr), prev_psk);
74
75         if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
76                 if (!sta->sae || prev_psk)
77                         return NULL;
78                 return sta->sae->pmk;
79         }
80
81         return NULL;
82 }
83
84
85 static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
86                         const u8 *addr, int idx, u8 *key, size_t key_len)
87 {
88         struct mesh_rsn *mesh_rsn = ctx;
89         u8 seq[6];
90
91         os_memset(seq, 0, sizeof(seq));
92
93         if (addr) {
94                 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
95                            " key_idx=%d)",
96                            __func__, alg, MAC2STR(addr), idx);
97         } else {
98                 wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
99                            __func__, alg, idx);
100         }
101         wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
102
103         return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
104                                1, seq, 6, key, key_len);
105 }
106
107
108 static int auth_start_ampe(void *ctx, const u8 *addr)
109 {
110         struct mesh_rsn *mesh_rsn = ctx;
111         struct hostapd_data *hapd;
112         struct sta_info *sta;
113
114         if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
115                 return -1;
116
117         hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
118         sta = ap_get_sta(hapd, addr);
119         if (sta)
120                 eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
121
122         mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
123         return 0;
124 }
125
126
127 static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr)
128 {
129         struct wpa_auth_config conf;
130         struct wpa_auth_callbacks cb;
131         u8 seq[6] = {};
132
133         wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
134
135         os_memset(&conf, 0, sizeof(conf));
136         conf.wpa = 2;
137         conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
138         conf.wpa_pairwise = WPA_CIPHER_CCMP;
139         conf.rsn_pairwise = WPA_CIPHER_CCMP;
140         conf.wpa_group = WPA_CIPHER_CCMP;
141         conf.eapol_version = 0;
142         conf.wpa_group_rekey = -1;
143
144         os_memset(&cb, 0, sizeof(cb));
145         cb.ctx = rsn;
146         cb.logger = auth_logger;
147         cb.get_psk = auth_get_psk;
148         cb.set_key = auth_set_key;
149         cb.start_ampe = auth_start_ampe;
150
151         rsn->auth = wpa_init(addr, &conf, &cb);
152         if (rsn->auth == NULL) {
153                 wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
154                 return -1;
155         }
156
157         /* TODO: support rekeying */
158         if (random_get_bytes(rsn->mgtk, 16) < 0) {
159                 wpa_deinit(rsn->auth);
160                 return -1;
161         }
162
163         /* group mgmt */
164         wpa_drv_set_key(rsn->wpa_s, WPA_ALG_IGTK, NULL, 4, 1,
165                         seq, sizeof(seq), rsn->mgtk, sizeof(rsn->mgtk));
166
167         /* group privacy / data frames */
168         wpa_drv_set_key(rsn->wpa_s, WPA_ALG_CCMP, NULL, 1, 1,
169                         seq, sizeof(seq), rsn->mgtk, sizeof(rsn->mgtk));
170
171         return 0;
172 }
173
174
175 static void mesh_rsn_deinit(struct mesh_rsn *rsn)
176 {
177         os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
178         wpa_deinit(rsn->auth);
179 }
180
181
182 struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
183                                     struct mesh_conf *conf)
184 {
185         struct mesh_rsn *mesh_rsn;
186         struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
187         const u8 *ie;
188         size_t ie_len;
189
190         mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
191         if (mesh_rsn == NULL)
192                 return NULL;
193         mesh_rsn->wpa_s = wpa_s;
194
195         if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr) < 0) {
196                 mesh_rsn_deinit(mesh_rsn);
197                 return NULL;
198         }
199
200         bss->wpa_auth = mesh_rsn->auth;
201
202         ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
203         conf->ies = (u8 *) ie;
204         conf->ie_len = ie_len;
205
206         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
207
208         return mesh_rsn;
209 }
210
211
212 static int index_within_array(const int *array, int idx)
213 {
214         int i;
215
216         for (i = 0; i < idx; i++) {
217                 if (array[i] == -1)
218                         return 0;
219         }
220
221         return 1;
222 }
223
224
225 static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
226                               struct sae_data *sae)
227 {
228         int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
229
230         /* Configuration may have changed, so validate current index */
231         if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
232                 return -1;
233
234         for (;;) {
235                 int group = groups[wpa_s->mesh_rsn->sae_group_index];
236
237                 if (group <= 0)
238                         break;
239                 if (sae_set_group(sae, group) == 0) {
240                         wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
241                                 sae->group);
242                         return 0;
243                 }
244                 wpa_s->mesh_rsn->sae_group_index++;
245         }
246
247         return -1;
248 }
249
250
251 static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
252                                      struct wpa_ssid *ssid,
253                                      struct sta_info *sta)
254 {
255         if (ssid->passphrase == NULL) {
256                 wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
257                 return -1;
258         }
259
260         if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
261                 wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
262                 return -1;
263         }
264
265         return sae_prepare_commit(wpa_s->own_addr, sta->addr,
266                                   (u8 *) ssid->passphrase,
267                                   os_strlen(ssid->passphrase), sta->sae);
268 }
269
270
271 /* initiate new SAE authentication with sta */
272 int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
273                           struct sta_info *sta)
274 {
275         struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
276         struct wpa_ssid *ssid = wpa_s->current_ssid;
277         unsigned int rnd;
278         int ret;
279
280         if (!ssid) {
281                 wpa_msg(wpa_s, MSG_DEBUG,
282                         "AUTH: No current_ssid known to initiate new SAE");
283                 return -1;
284         }
285
286         if (!sta->sae) {
287                 sta->sae = os_zalloc(sizeof(*sta->sae));
288                 if (sta->sae == NULL)
289                         return -1;
290         }
291
292         if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
293                 return -1;
294
295         wpa_msg(wpa_s, MSG_DEBUG,
296                 "AUTH: started authentication with SAE peer: " MACSTR,
297                 MAC2STR(sta->addr));
298
299         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
300         ret = auth_sae_init_committed(hapd, sta);
301         if (ret)
302                 return ret;
303
304         rnd = rand() % MESH_AUTH_TIMEOUT;
305         eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
306                                wpa_s, sta);
307         return 0;
308 }
309
310
311 void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
312 {
313         /* don't expect wpa auth to cache the pmkid for now */
314         rsn_pmkid(sta->sae->pmk, PMK_LEN, rsn->wpa_s->own_addr,
315                   sta->addr, pmkid,
316                   wpa_key_mgmt_sha256(wpa_auth_sta_key_mgmt(sta->wpa_sm)));
317 }
318
319
320 static void
321 mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
322 {
323         u8 *myaddr = rsn->wpa_s->own_addr;
324         u8 *peer = sta->addr;
325         u8 *addr1 = peer, *addr2 = myaddr;
326         u8 context[AES_BLOCK_SIZE];
327
328         /* SAE */
329         RSN_SELECTOR_PUT(context, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
330
331         if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
332                 addr1 = myaddr;
333                 addr2 = peer;
334         }
335         os_memcpy(context + 4, addr1, ETH_ALEN);
336         os_memcpy(context + 10, addr2, ETH_ALEN);
337
338         sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
339                    context, sizeof(context), sta->aek, sizeof(sta->aek));
340 }
341
342
343 /* derive mesh temporal key from pmk */
344 int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
345 {
346         u8 *ptr;
347         u8 *min, *max;
348         u16 min_lid, max_lid;
349         size_t nonce_len = sizeof(sta->my_nonce);
350         size_t lid_len = sizeof(sta->my_lid);
351         u8 *myaddr = wpa_s->own_addr;
352         u8 *peer = sta->addr;
353         /* 2 nonces, 2 linkids, akm suite, 2 mac addrs */
354         u8 context[64 + 4 + 4 + 12];
355
356         ptr = context;
357         if (os_memcmp(sta->my_nonce, sta->peer_nonce, nonce_len) < 0) {
358                 min = sta->my_nonce;
359                 max = sta->peer_nonce;
360         } else {
361                 min = sta->peer_nonce;
362                 max = sta->my_nonce;
363         }
364         os_memcpy(ptr, min, nonce_len);
365         os_memcpy(ptr + nonce_len, max, nonce_len);
366         ptr += 2 * nonce_len;
367
368         if (sta->my_lid < sta->peer_lid) {
369                 min_lid = host_to_le16(sta->my_lid);
370                 max_lid = host_to_le16(sta->peer_lid);
371         } else {
372                 min_lid = host_to_le16(sta->peer_lid);
373                 max_lid = host_to_le16(sta->my_lid);
374         }
375         os_memcpy(ptr, &min_lid, lid_len);
376         os_memcpy(ptr + lid_len, &max_lid, lid_len);
377         ptr += 2 * lid_len;
378
379         /* SAE */
380         RSN_SELECTOR_PUT(ptr, wpa_cipher_to_suite(0, WPA_CIPHER_GCMP));
381         ptr += 4;
382
383         if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
384                 min = myaddr;
385                 max = peer;
386         } else {
387                 min = peer;
388                 max = myaddr;
389         }
390         os_memcpy(ptr, min, ETH_ALEN);
391         os_memcpy(ptr + ETH_ALEN, max, ETH_ALEN);
392
393         sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk),
394                    "Temporal Key Derivation", context, sizeof(context),
395                    sta->mtk, sizeof(sta->mtk));
396         return 0;
397 }
398
399
400 void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
401 {
402         if (random_get_bytes(sta->my_nonce, 32) < 0) {
403                 wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
404                 /* TODO: How to handle this more cleanly? */
405         }
406         os_memset(sta->peer_nonce, 0, 32);
407         mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
408 }
409
410
411 /* insert AMPE and encrypted MIC at @ie.
412  * @mesh_rsn: mesh RSN context
413  * @sta: STA we're sending to
414  * @cat: pointer to category code in frame header.
415  * @buf: wpabuf to add encrypted AMPE and MIC to.
416  * */
417 int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
418                            const u8 *cat, struct wpabuf *buf)
419 {
420         struct ieee80211_ampe_ie *ampe;
421         u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
422         u8 *ampe_ie = NULL, *mic_ie = NULL, *mic_payload;
423         const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
424         const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
425         int ret = 0;
426
427         if (AES_BLOCK_SIZE + 2 + sizeof(*ampe) + 2 > wpabuf_tailroom(buf)) {
428                 wpa_printf(MSG_ERROR, "protect frame: buffer too small");
429                 return -EINVAL;
430         }
431
432         ampe_ie = os_zalloc(2 + sizeof(*ampe));
433         if (!ampe_ie) {
434                 wpa_printf(MSG_ERROR, "protect frame: out of memory");
435                 return -ENOMEM;
436         }
437
438         mic_ie = os_zalloc(2 + AES_BLOCK_SIZE);
439         if (!mic_ie) {
440                 wpa_printf(MSG_ERROR, "protect frame: out of memory");
441                 ret = -ENOMEM;
442                 goto free;
443         }
444
445         /*  IE: AMPE */
446         ampe_ie[0] = WLAN_EID_AMPE;
447         ampe_ie[1] = sizeof(*ampe);
448         ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
449
450         RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
451                      wpa_cipher_to_suite(WPA_PROTO_RSN, WPA_CIPHER_CCMP));
452         os_memcpy(ampe->local_nonce, sta->my_nonce, 32);
453         os_memcpy(ampe->peer_nonce, sta->peer_nonce, 32);
454         /* incomplete: see 13.5.4 */
455         /* TODO: static mgtk for now since we don't support rekeying! */
456         os_memcpy(ampe->mgtk, rsn->mgtk, 16);
457         /*  TODO: Populate Key RSC */
458         /*  expire in 13 decades or so */
459         os_memset(ampe->key_expiration, 0xff, 4);
460
461         /* IE: MIC */
462         mic_ie[0] = WLAN_EID_MIC;
463         mic_ie[1] = AES_BLOCK_SIZE;
464         wpabuf_put_data(buf, mic_ie, 2);
465         /* MIC field is output ciphertext */
466
467         /* encrypt after MIC */
468         mic_payload = (u8 *) wpabuf_put(buf, 2 + sizeof(*ampe) +
469                                         AES_BLOCK_SIZE);
470
471         if (aes_siv_encrypt(sta->aek, ampe_ie, 2 + sizeof(*ampe), 3,
472                             aad, aad_len, mic_payload)) {
473                 wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
474                 ret = -ENOMEM;
475                 goto free;
476         }
477
478 free:
479         os_free(ampe_ie);
480         os_free(mic_ie);
481
482         return ret;
483 }
484
485
486 int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
487                           struct ieee802_11_elems *elems, const u8 *cat,
488                           const u8 *start, size_t elems_len)
489 {
490         int ret = 0;
491         struct ieee80211_ampe_ie *ampe;
492         u8 null_nonce[32] = {};
493         u8 ampe_eid;
494         u8 ampe_ie_len;
495         u8 *ampe_buf, *crypt = NULL;
496         size_t crypt_len;
497         const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
498         const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
499                                    (elems->mic - 2) - cat };
500
501         if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
502                 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
503                 return -1;
504         }
505
506         ampe_buf = (u8 *) elems->mic + elems->mic_len;
507         if ((int) elems_len < ampe_buf - start)
508                 return -1;
509
510         crypt_len = elems_len - (elems->mic - start);
511         if (crypt_len < 2) {
512                 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
513                 return -1;
514         }
515
516         /* crypt is modified by siv_decrypt */
517         crypt = os_zalloc(crypt_len);
518         if (!crypt) {
519                 wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
520                 ret = -ENOMEM;
521                 goto free;
522         }
523
524         os_memcpy(crypt, elems->mic, crypt_len);
525
526         if (aes_siv_decrypt(sta->aek, crypt, crypt_len, 3,
527                             aad, aad_len, ampe_buf)) {
528                 wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
529                 ret = -1;
530                 goto free;
531         }
532
533         ampe_eid = *ampe_buf++;
534         ampe_ie_len = *ampe_buf++;
535
536         if (ampe_eid != WLAN_EID_AMPE ||
537             ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
538                 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
539                 ret = -1;
540                 goto free;
541         }
542
543         ampe = (struct ieee80211_ampe_ie *) ampe_buf;
544         if (os_memcmp(ampe->peer_nonce, null_nonce, 32) != 0 &&
545             os_memcmp(ampe->peer_nonce, sta->my_nonce, 32) != 0) {
546                 wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
547                 ret = -1;
548                 goto free;
549         }
550         os_memcpy(sta->peer_nonce, ampe->local_nonce,
551                   sizeof(ampe->local_nonce));
552         os_memcpy(sta->mgtk, ampe->mgtk, sizeof(ampe->mgtk));
553
554         /* todo parse mgtk expiration */
555 free:
556         os_free(crypt);
557         return ret;
558 }