mka: Avoid reading past the end of mka_body_handler
[mech_eap.git] / src / pae / ieee802_1x_kay.c
1 /*
2  * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
3  * Copyright (c) 2013, Qualcomm Atheros, Inc.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include <time.h>
10 #include "includes.h"
11 #include "common.h"
12 #include "list.h"
13 #include "eloop.h"
14 #include "wpabuf.h"
15 #include "state_machine.h"
16 #include "l2_packet/l2_packet.h"
17 #include "common/eapol_common.h"
18 #include "crypto/aes_wrap.h"
19 #include "ieee802_1x_cp.h"
20 #include "ieee802_1x_key.h"
21 #include "ieee802_1x_kay.h"
22 #include "ieee802_1x_kay_i.h"
23 #include "ieee802_1x_secy_ops.h"
24
25
26 #define DEFAULT_SA_KEY_LEN      16
27 #define DEFAULT_ICV_LEN         16
28 #define MAX_ICV_LEN             32  /* 32 bytes, 256 bits */
29
30 #define PENDING_PN_EXHAUSTION 0xC0000000
31
32 /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
33 #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
34 static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
35
36 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
37 static struct macsec_ciphersuite cipher_suite_tbl[] = {
38         /* GCM-AES-128 */
39         {
40                 CS_ID_GCM_AES_128,
41                 CS_NAME_GCM_AES_128,
42                 MACSEC_CAP_INTEG_AND_CONF_0_30_50,
43                 16,
44
45                 0 /* index */
46         },
47 };
48 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
49 #define DEFAULT_CS_INDEX  0
50
51 static struct mka_alg mka_alg_tbl[] = {
52         {
53                 MKA_ALGO_AGILITY_2009,
54                 /* 128-bit CAK, KEK, ICK, ICV */
55                 16, 16, 16, 16,
56                 ieee802_1x_cak_128bits_aes_cmac,
57                 ieee802_1x_ckn_128bits_aes_cmac,
58                 ieee802_1x_kek_128bits_aes_cmac,
59                 ieee802_1x_ick_128bits_aes_cmac,
60                 ieee802_1x_icv_128bits_aes_cmac,
61
62                 1, /* index */
63         },
64 };
65 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
66
67
68 static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
69                        struct ieee802_1x_mka_ki *ki2)
70 {
71         return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
72                 ki1->kn == ki2->kn;
73 }
74
75
76 struct mka_param_body_handler {
77         int (*body_tx)(struct ieee802_1x_mka_participant *participant,
78                        struct wpabuf *buf);
79         int (*body_rx)(struct ieee802_1x_mka_participant *participant,
80                        const u8 *mka_msg, size_t msg_len);
81         int (*body_length)(struct ieee802_1x_mka_participant *participant);
82         Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
83 };
84
85
86 static void set_mka_param_body_len(void *body, unsigned int len)
87 {
88         struct ieee802_1x_mka_hdr *hdr = body;
89         hdr->length = (len >> 8) & 0x0f;
90         hdr->length1 = len & 0xff;
91 }
92
93
94 static unsigned int get_mka_param_body_len(const void *body)
95 {
96         const struct ieee802_1x_mka_hdr *hdr = body;
97         return (hdr->length << 8) | hdr->length1;
98 }
99
100
101 static u8 get_mka_param_body_type(const void *body)
102 {
103         const struct ieee802_1x_mka_hdr *hdr = body;
104         return hdr->type;
105 }
106
107
108 /**
109  * ieee802_1x_mka_dump_basic_body -
110  */
111 static void
112 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
113 {
114         size_t body_len;
115
116         if (!body)
117                 return;
118
119         body_len = get_mka_param_body_len(body);
120         wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
121         wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
122         wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
123         wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
124         wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
125         wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capbility);
126         wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
127         wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
128                    MAC2STR(body->actor_sci.addr));
129         wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
130                    be_to_host16(body->actor_sci.port));
131         wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
132                     body->actor_mi, sizeof(body->actor_mi));
133         wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
134                    be_to_host32(body->actor_mn));
135         wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
136                     body->algo_agility, sizeof(body->algo_agility));
137         wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
138                           body_len + MKA_HDR_LEN - sizeof(*body));
139 }
140
141
142 /**
143  * ieee802_1x_mka_dump_peer_body -
144  */
145 static void
146 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
147 {
148         size_t body_len;
149         size_t i;
150         u8 *mi;
151         be32 mn;
152
153         if (body == NULL)
154                 return;
155
156         body_len = get_mka_param_body_len(body);
157         if (body->type == MKA_LIVE_PEER_LIST) {
158                 wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
159                 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
160         } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
161                 wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
162                 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
163         }
164
165         for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
166                 mi = body->peer + i;
167                 os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
168                 wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
169                 wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
170         }
171 }
172
173
174 /**
175  * ieee802_1x_mka_dump_dist_sak_body -
176  */
177 static void
178 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
179 {
180         size_t body_len;
181
182         if (body == NULL)
183                 return;
184
185         body_len = get_mka_param_body_len(body);
186         wpa_printf(MSG_INFO, "*** Distributed SAK ***");
187         wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
188         wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
189                    body->confid_offset);
190         wpa_printf(MSG_INFO, "\tBody Length...........: %d", (int) body_len);
191         if (!body_len)
192                 return;
193
194         wpa_printf(MSG_INFO, "\tKey Number............: %d",
195                    be_to_host32(body->kn));
196         wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
197 }
198
199
200 static const char * yes_no(int val)
201 {
202         return val ? "Yes" : "No";
203 }
204
205
206 /**
207  * ieee802_1x_mka_dump_sak_use_body -
208  */
209 static void
210 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
211 {
212         int body_len;
213
214         if (body == NULL)
215                 return;
216
217         body_len = get_mka_param_body_len(body);
218         wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
219         wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
220         wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
221         wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
222         wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
223         wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
224         wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
225         wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
226         wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
227         wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
228                    yes_no(body->delay_protect));
229         wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
230         if (!body_len)
231                 return;
232
233         wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
234                     body->lsrv_mi, sizeof(body->lsrv_mi));
235         wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
236                    be_to_host32(body->lkn));
237         wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
238                    be_to_host32(body->llpn));
239         wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
240                           body->osrv_mi, sizeof(body->osrv_mi));
241         wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
242                    be_to_host32(body->okn));
243         wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
244                    be_to_host32(body->olpn));
245 }
246
247
248 /**
249  * ieee802_1x_kay_get_participant -
250  */
251 static struct ieee802_1x_mka_participant *
252 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
253 {
254         struct ieee802_1x_mka_participant *participant;
255
256         dl_list_for_each(participant, &kay->participant_list,
257                          struct ieee802_1x_mka_participant, list) {
258                 if (os_memcmp(participant->ckn.name, ckn,
259                               participant->ckn.len) == 0)
260                         return participant;
261         }
262
263         wpa_printf(MSG_DEBUG, "KaY: participant is not found");
264
265         return NULL;
266 }
267
268
269 /**
270  * ieee802_1x_kay_get_principal_participant -
271  */
272 static struct ieee802_1x_mka_participant *
273 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
274 {
275         struct ieee802_1x_mka_participant *participant;
276
277         dl_list_for_each(participant, &kay->participant_list,
278                          struct ieee802_1x_mka_participant, list) {
279                 if (participant->principal)
280                         return participant;
281         }
282
283         wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
284         return NULL;
285 }
286
287
288 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
289                                                 const u8 *mi)
290 {
291         struct ieee802_1x_kay_peer *peer;
292
293         dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
294                 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
295                         return peer;
296         }
297
298         return NULL;
299 }
300
301
302 /**
303  * ieee802_1x_kay_is_in_potential_peer
304  */
305 static Boolean
306 ieee802_1x_kay_is_in_potential_peer(
307         struct ieee802_1x_mka_participant *participant, const u8 *mi)
308 {
309         return get_peer_mi(&participant->potential_peers, mi) != NULL;
310 }
311
312
313 /**
314  * ieee802_1x_kay_is_in_live_peer
315  */
316 static Boolean
317 ieee802_1x_kay_is_in_live_peer(
318         struct ieee802_1x_mka_participant *participant, const u8 *mi)
319 {
320         return get_peer_mi(&participant->live_peers, mi) != NULL;
321 }
322
323
324 /**
325  * ieee802_1x_kay_is_in_peer
326  */
327 static Boolean
328 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
329                           const u8 *mi)
330 {
331         return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
332                 ieee802_1x_kay_is_in_potential_peer(participant, mi);
333 }
334
335
336 /**
337  * ieee802_1x_kay_get_peer
338  */
339 static struct ieee802_1x_kay_peer *
340 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
341                         const u8 *mi)
342 {
343         struct ieee802_1x_kay_peer *peer;
344
345         peer = get_peer_mi(&participant->live_peers, mi);
346         if (peer)
347                 return peer;
348
349         return get_peer_mi(&participant->potential_peers, mi);
350 }
351
352
353 /**
354  * ieee802_1x_kay_get_live_peer
355  */
356 static struct ieee802_1x_kay_peer *
357 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
358                              const u8 *mi)
359 {
360         return get_peer_mi(&participant->live_peers, mi);
361 }
362
363
364 /**
365  * ieee802_1x_kay_get_cipher_suite
366  */
367 static struct macsec_ciphersuite *
368 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
369                                 u8 *cs_id)
370 {
371         unsigned int i;
372
373         for (i = 0; i < CS_TABLE_SIZE; i++) {
374                 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
375                         break;
376         }
377         if (i >= CS_TABLE_SIZE)
378                 return NULL;
379
380         return &cipher_suite_tbl[i];
381 }
382
383
384 /**
385  * ieee802_1x_kay_get_peer_sci
386  */
387 static struct ieee802_1x_kay_peer *
388 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
389                             const struct ieee802_1x_mka_sci *sci)
390 {
391         struct ieee802_1x_kay_peer *peer;
392
393         dl_list_for_each(peer, &participant->live_peers,
394                          struct ieee802_1x_kay_peer, list) {
395                 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
396                         return peer;
397         }
398
399         dl_list_for_each(peer, &participant->potential_peers,
400                          struct ieee802_1x_kay_peer, list) {
401                 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
402                         return peer;
403         }
404
405         return NULL;
406 }
407
408
409 /**
410  * ieee802_1x_kay_init_receive_sa -
411  */
412 static struct receive_sa *
413 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
414                                struct data_key *key)
415 {
416         struct receive_sa *psa;
417
418         if (!psc || !key)
419                 return NULL;
420
421         psa = os_zalloc(sizeof(*psa));
422         if (!psa) {
423                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
424                 return NULL;
425         }
426
427         psa->pkey = key;
428         psa->lowest_pn = lowest_pn;
429         psa->next_pn = lowest_pn;
430         psa->an = an;
431         psa->sc = psc;
432
433         os_get_time(&psa->created_time);
434         psa->in_use = FALSE;
435
436         dl_list_add(&psc->sa_list, &psa->list);
437         wpa_printf(MSG_DEBUG,
438                    "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
439                    (int) an, lowest_pn, psc->channel);
440
441         return psa;
442 }
443
444
445 /**
446  * ieee802_1x_kay_deinit_receive_sa -
447  */
448 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
449 {
450         psa->pkey = NULL;
451         wpa_printf(MSG_DEBUG,
452                    "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
453                    psa->an, psa->sc->channel);
454         dl_list_del(&psa->list);
455         os_free(psa);
456 }
457
458
459 /**
460  * ieee802_1x_kay_init_receive_sc -
461  */
462 static struct receive_sc *
463 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
464                                int channel)
465 {
466         struct receive_sc *psc;
467
468         if (!psci)
469                 return NULL;
470
471         psc = os_zalloc(sizeof(*psc));
472         if (!psc) {
473                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
474                 return NULL;
475         }
476
477         os_memcpy(&psc->sci, psci, sizeof(psc->sci));
478         psc->channel = channel;
479
480         os_get_time(&psc->created_time);
481         psc->receiving = FALSE;
482
483         dl_list_init(&psc->sa_list);
484         wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
485         wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
486
487         return psc;
488 }
489
490
491 /**
492  * ieee802_1x_kay_deinit_receive_sc -
493  **/
494 static void
495 ieee802_1x_kay_deinit_receive_sc(
496         struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
497 {
498         struct receive_sa *psa, *pre_sa;
499
500         wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
501                    psc->channel);
502         dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
503                               list)  {
504                 secy_disable_receive_sa(participant->kay, psa);
505                 ieee802_1x_kay_deinit_receive_sa(psa);
506         }
507         dl_list_del(&psc->list);
508         os_free(psc);
509 }
510
511
512 /**
513  * ieee802_1x_kay_create_live_peer
514  */
515 static struct ieee802_1x_kay_peer *
516 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
517                                 u8 *mi, u32 mn)
518 {
519         struct ieee802_1x_kay_peer *peer;
520         struct receive_sc *rxsc;
521         u32 sc_ch = 0;
522
523         peer = os_zalloc(sizeof(*peer));
524         if (peer == NULL) {
525                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
526                 return NULL;
527         }
528
529         os_memcpy(peer->mi, mi, MI_LEN);
530         peer->mn = mn;
531         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
532         peer->sak_used = FALSE;
533         os_memcpy(&peer->sci, &participant->current_peer_sci,
534                   sizeof(peer->sci));
535
536         secy_get_available_receive_sc(participant->kay, &sc_ch);
537
538         rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
539         if (!rxsc) {
540                 os_free(peer);
541                 return NULL;
542         }
543
544         dl_list_add(&participant->live_peers, &peer->list);
545         dl_list_add(&participant->rxsc_list, &rxsc->list);
546         secy_create_receive_sc(participant->kay, rxsc);
547
548         wpa_printf(MSG_DEBUG, "KaY: Live peer created");
549         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
550         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
551         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
552         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
553
554         return peer;
555 }
556
557
558 /**
559  * ieee802_1x_kay_create_potential_peer
560  */
561 static struct ieee802_1x_kay_peer *
562 ieee802_1x_kay_create_potential_peer(
563         struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
564 {
565         struct ieee802_1x_kay_peer *peer;
566
567         peer = os_zalloc(sizeof(*peer));
568         if (peer == NULL) {
569                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
570                 return NULL;
571         }
572
573         os_memcpy(peer->mi, mi, MI_LEN);
574         peer->mn = mn;
575         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
576         peer->sak_used = FALSE;
577
578         dl_list_add(&participant->potential_peers, &peer->list);
579
580         wpa_printf(MSG_DEBUG, "KaY: potential peer created");
581         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
582         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
583         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
584         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
585
586         return peer;
587 }
588
589
590 /**
591  * ieee802_1x_kay_move_live_peer
592  */
593 static struct ieee802_1x_kay_peer *
594 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
595                               u8 *mi, u32 mn)
596 {
597         struct ieee802_1x_kay_peer *peer;
598         struct receive_sc *rxsc;
599         u32 sc_ch = 0;
600
601         dl_list_for_each(peer, &participant->potential_peers,
602                          struct ieee802_1x_kay_peer, list) {
603                 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
604                         break;
605         }
606
607         rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
608                                               sc_ch);
609         if (!rxsc)
610                 return NULL;
611
612         os_memcpy(&peer->sci, &participant->current_peer_sci,
613                   sizeof(peer->sci));
614         peer->mn = mn;
615         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
616
617         wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
618         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
619         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
620         wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
621         wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
622
623         dl_list_del(&peer->list);
624         dl_list_add_tail(&participant->live_peers, &peer->list);
625
626         secy_get_available_receive_sc(participant->kay, &sc_ch);
627
628         dl_list_add(&participant->rxsc_list, &rxsc->list);
629         secy_create_receive_sc(participant->kay, rxsc);
630
631         return peer;
632 }
633
634
635
636 /**
637  *  ieee802_1x_mka_basic_body_present -
638  */
639 static Boolean
640 ieee802_1x_mka_basic_body_present(
641         struct ieee802_1x_mka_participant *participant)
642 {
643         return TRUE;
644 }
645
646
647 /**
648  * ieee802_1x_mka_basic_body_length -
649  */
650 static int
651 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
652 {
653         int length;
654
655         length = sizeof(struct ieee802_1x_mka_basic_body);
656         length += participant->ckn.len;
657         return (length + 0x3) & ~0x3;
658 }
659
660
661 /**
662  * ieee802_1x_mka_encode_basic_body
663  */
664 static int
665 ieee802_1x_mka_encode_basic_body(
666         struct ieee802_1x_mka_participant *participant,
667         struct wpabuf *buf)
668 {
669         struct ieee802_1x_mka_basic_body *body;
670         struct ieee802_1x_kay *kay = participant->kay;
671         unsigned int length = ieee802_1x_mka_basic_body_length(participant);
672
673         body = wpabuf_put(buf, length);
674
675         body->version = kay->mka_version;
676         body->priority = kay->actor_priority;
677         if (participant->is_elected)
678                 body->key_server = participant->is_key_server;
679         else
680                 body->key_server = participant->can_be_key_server;
681
682         body->macsec_desired = kay->macsec_desired;
683         body->macsec_capbility = kay->macsec_capable;
684         set_mka_param_body_len(body, length - MKA_HDR_LEN);
685
686         os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
687                   sizeof(kay->actor_sci.addr));
688         body->actor_sci.port = kay->actor_sci.port;
689
690         os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
691         participant->mn = participant->mn + 1;
692         body->actor_mn = host_to_be32(participant->mn);
693         os_memcpy(body->algo_agility, participant->kay->algo_agility,
694                   sizeof(body->algo_agility));
695
696         os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
697
698         ieee802_1x_mka_dump_basic_body(body);
699
700         return 0;
701 }
702
703
704 /**
705  * ieee802_1x_mka_decode_basic_body -
706  */
707 static struct ieee802_1x_mka_participant *
708 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
709                                  size_t msg_len)
710 {
711         struct ieee802_1x_mka_participant *participant;
712         const struct ieee802_1x_mka_basic_body *body;
713         struct ieee802_1x_kay_peer *peer;
714
715         body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
716
717         if (body->version > MKA_VERSION_ID) {
718                 wpa_printf(MSG_DEBUG,
719                            "KaY: peer's version(%d) greater than mka current version(%d)",
720                            body->version, MKA_VERSION_ID);
721         }
722         if (kay->is_obliged_key_server && body->key_server) {
723                 wpa_printf(MSG_DEBUG, "I must be as key server");
724                 return NULL;
725         }
726
727         participant = ieee802_1x_kay_get_participant(kay, body->ckn);
728         if (!participant) {
729                 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
730                 return NULL;
731         }
732
733         /* If the peer's MI is my MI, I will choose new MI */
734         if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
735                 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
736                         return NULL;
737                 participant->mn = 0;
738         }
739
740         os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
741         participant->current_peer_id.mn = body->actor_mn;
742         os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
743                   sizeof(participant->current_peer_sci.addr));
744         participant->current_peer_sci.port = body->actor_sci.port;
745
746         /* handler peer */
747         peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
748         if (!peer) {
749                 /* Check duplicated SCI */
750                 /* TODO: What policy should be applied to detect duplicated SCI
751                  * is active attacker or a valid peer whose MI is be changed?
752                  */
753                 peer = ieee802_1x_kay_get_peer_sci(participant,
754                                                    &body->actor_sci);
755                 if (peer) {
756                         wpa_printf(MSG_WARNING,
757                                    "KaY: duplicated SCI detected, Maybe active attacker");
758                         dl_list_del(&peer->list);
759                         os_free(peer);
760                 }
761
762                 peer = ieee802_1x_kay_create_potential_peer(
763                         participant, body->actor_mi,
764                         be_to_host32(body->actor_mn));
765                 if (!peer)
766                         return NULL;
767
768                 peer->macsec_desired = body->macsec_desired;
769                 peer->macsec_capbility = body->macsec_capbility;
770                 peer->is_key_server = (Boolean) body->key_server;
771                 peer->key_server_priority = body->priority;
772         } else if (peer->mn < be_to_host32(body->actor_mn)) {
773                 peer->mn = be_to_host32(body->actor_mn);
774                 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
775                 peer->macsec_desired = body->macsec_desired;
776                 peer->macsec_capbility = body->macsec_capbility;
777                 peer->is_key_server = (Boolean) body->key_server;
778                 peer->key_server_priority = body->priority;
779         } else {
780                 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
781                 return NULL;
782         }
783
784         return participant;
785 }
786
787
788 /**
789  * ieee802_1x_mka_live_peer_body_present
790  */
791 static Boolean
792 ieee802_1x_mka_live_peer_body_present(
793         struct ieee802_1x_mka_participant *participant)
794 {
795         return !dl_list_empty(&participant->live_peers);
796 }
797
798
799 /**
800  * ieee802_1x_kay_get_live_peer_length
801  */
802 static int
803 ieee802_1x_mka_get_live_peer_length(
804         struct ieee802_1x_mka_participant *participant)
805 {
806         int len = MKA_HDR_LEN;
807         struct ieee802_1x_kay_peer *peer;
808
809         dl_list_for_each(peer, &participant->live_peers,
810                          struct ieee802_1x_kay_peer, list)
811                 len += sizeof(struct ieee802_1x_mka_peer_id);
812
813         return (len + 0x3) & ~0x3;
814 }
815
816
817 /**
818  * ieee802_1x_mka_encode_live_peer_body -
819  */
820 static int
821 ieee802_1x_mka_encode_live_peer_body(
822         struct ieee802_1x_mka_participant *participant,
823         struct wpabuf *buf)
824 {
825         struct ieee802_1x_mka_peer_body *body;
826         struct ieee802_1x_kay_peer *peer;
827         unsigned int length;
828         struct ieee802_1x_mka_peer_id *body_peer;
829
830         length = ieee802_1x_mka_get_live_peer_length(participant);
831         body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
832
833         body->type = MKA_LIVE_PEER_LIST;
834         set_mka_param_body_len(body, length - MKA_HDR_LEN);
835
836         dl_list_for_each(peer, &participant->live_peers,
837                          struct ieee802_1x_kay_peer, list) {
838                 body_peer = wpabuf_put(buf,
839                                        sizeof(struct ieee802_1x_mka_peer_id));
840                 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
841                 body_peer->mn = host_to_be32(peer->mn);
842                 body_peer++;
843         }
844
845         ieee802_1x_mka_dump_peer_body(body);
846         return 0;
847 }
848
849 /**
850  * ieee802_1x_mka_potential_peer_body_present
851  */
852 static Boolean
853 ieee802_1x_mka_potential_peer_body_present(
854         struct ieee802_1x_mka_participant *participant)
855 {
856         return !dl_list_empty(&participant->potential_peers);
857 }
858
859
860 /**
861  * ieee802_1x_kay_get_potential_peer_length
862  */
863 static int
864 ieee802_1x_mka_get_potential_peer_length(
865         struct ieee802_1x_mka_participant *participant)
866 {
867         int len = MKA_HDR_LEN;
868         struct ieee802_1x_kay_peer *peer;
869
870         dl_list_for_each(peer, &participant->potential_peers,
871                          struct ieee802_1x_kay_peer, list)
872                 len += sizeof(struct ieee802_1x_mka_peer_id);
873
874         return (len + 0x3) & ~0x3;
875 }
876
877
878 /**
879  * ieee802_1x_mka_encode_potential_peer_body -
880  */
881 static int
882 ieee802_1x_mka_encode_potential_peer_body(
883         struct ieee802_1x_mka_participant *participant,
884         struct wpabuf *buf)
885 {
886         struct ieee802_1x_mka_peer_body *body;
887         struct ieee802_1x_kay_peer *peer;
888         unsigned int length;
889         struct ieee802_1x_mka_peer_id *body_peer;
890
891         length = ieee802_1x_mka_get_potential_peer_length(participant);
892         body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
893
894         body->type = MKA_POTENTIAL_PEER_LIST;
895         set_mka_param_body_len(body, length - MKA_HDR_LEN);
896
897         dl_list_for_each(peer, &participant->potential_peers,
898                          struct ieee802_1x_kay_peer, list) {
899                 body_peer = wpabuf_put(buf,
900                                        sizeof(struct ieee802_1x_mka_peer_id));
901                 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
902                 body_peer->mn = host_to_be32(peer->mn);
903                 body_peer++;
904         }
905
906         ieee802_1x_mka_dump_peer_body(body);
907         return 0;
908 }
909
910
911 /**
912  * ieee802_1x_mka_i_in_peerlist -
913  */
914 static Boolean
915 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
916                              const u8 *mka_msg, size_t msg_len)
917 {
918         Boolean included = FALSE;
919         struct ieee802_1x_mka_hdr *hdr;
920         size_t body_len;
921         size_t left_len;
922         u8 body_type;
923         u32 peer_mn;
924         be32 _peer_mn;
925         const u8 *peer_mi;
926         const u8 *pos;
927         size_t i;
928
929         pos = mka_msg;
930         left_len = msg_len;
931         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
932                 hdr = (struct ieee802_1x_mka_hdr *) pos;
933                 body_len = get_mka_param_body_len(hdr);
934                 body_type = get_mka_param_body_type(hdr);
935
936                 if (body_type != MKA_LIVE_PEER_LIST &&
937                     body_type != MKA_POTENTIAL_PEER_LIST)
938                         goto SKIP_PEER;
939
940                 ieee802_1x_mka_dump_peer_body(
941                         (struct ieee802_1x_mka_peer_body *)pos);
942
943                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
944                         wpa_printf(MSG_ERROR,
945                                    "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
946                                    (int) left_len, (int) MKA_HDR_LEN,
947                                    (int) body_len, DEFAULT_ICV_LEN);
948                         goto SKIP_PEER;
949                 }
950
951                 if ((body_len % 16) != 0) {
952                         wpa_printf(MSG_ERROR,
953                                    "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
954                                    (int) body_len);
955                         goto SKIP_PEER;
956                 }
957
958                 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
959                         peer_mi = MKA_HDR_LEN + pos + i;
960                         os_memcpy(&_peer_mn, peer_mi + MI_LEN,
961                                   sizeof(_peer_mn));
962                         peer_mn = be_to_host32(_peer_mn);
963                         if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
964                             peer_mn == participant->mn) {
965                                 included = TRUE;
966                                 break;
967                         }
968                 }
969
970                 if (included)
971                         return TRUE;
972
973 SKIP_PEER:
974                 left_len -= body_len + MKA_HDR_LEN;
975                 pos += body_len + MKA_HDR_LEN;
976         }
977
978         return FALSE;
979 }
980
981
982 /**
983  * ieee802_1x_mka_decode_live_peer_body -
984  */
985 static int ieee802_1x_mka_decode_live_peer_body(
986         struct ieee802_1x_mka_participant *participant,
987         const u8 *peer_msg, size_t msg_len)
988 {
989         const struct ieee802_1x_mka_hdr *hdr;
990         struct ieee802_1x_kay_peer *peer;
991         size_t body_len;
992         u32 peer_mn;
993         be32 _peer_mn;
994         const u8 *peer_mi;
995         size_t i;
996         Boolean is_included;
997
998         is_included = ieee802_1x_kay_is_in_live_peer(
999                 participant, participant->current_peer_id.mi);
1000
1001         hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1002         body_len = get_mka_param_body_len(hdr);
1003
1004         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1005                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1006                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1007                 peer_mn = be_to_host32(_peer_mn);
1008
1009                 /* it is myself */
1010                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1011                         /* My message id is used by other participant */
1012                         if (peer_mn > participant->mn) {
1013                                 if (os_get_random(participant->mi,
1014                                                   sizeof(participant->mi)) < 0)
1015                                         wpa_printf(MSG_DEBUG,
1016                                                    "KaY: Could not update mi");
1017                                 participant->mn = 0;
1018                         }
1019                         continue;
1020                 }
1021                 if (!is_included)
1022                         continue;
1023
1024                 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1025                 if (NULL != peer) {
1026                         peer->mn = peer_mn;
1027                         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1028                 } else {
1029                         if (!ieee802_1x_kay_create_potential_peer(
1030                                 participant, peer_mi, peer_mn)) {
1031                                 return -1;
1032                         }
1033                 }
1034         }
1035
1036         return 0;
1037 }
1038
1039
1040 /**
1041  * ieee802_1x_mka_decode_potential_peer_body -
1042  */
1043 static int
1044 ieee802_1x_mka_decode_potential_peer_body(
1045         struct ieee802_1x_mka_participant *participant,
1046         const u8 *peer_msg, size_t msg_len)
1047 {
1048         struct ieee802_1x_mka_hdr *hdr;
1049         size_t body_len;
1050         u32 peer_mn;
1051         be32 _peer_mn;
1052         const u8 *peer_mi;
1053         size_t i;
1054
1055         hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1056         body_len = get_mka_param_body_len(hdr);
1057
1058         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1059                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1060                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1061                 peer_mn = be_to_host32(_peer_mn);
1062
1063                 /* it is myself */
1064                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1065                         /* My message id is used by other participant */
1066                         if (peer_mn > participant->mn) {
1067                                 if (os_get_random(participant->mi,
1068                                                   sizeof(participant->mi)) < 0)
1069                                         wpa_printf(MSG_DEBUG,
1070                                                    "KaY: Could not update mi");
1071                                 participant->mn = 0;
1072                         }
1073                         continue;
1074                 }
1075         }
1076
1077         return 0;
1078 }
1079
1080
1081 /**
1082  * ieee802_1x_mka_sak_use_body_present
1083  */
1084 static Boolean
1085 ieee802_1x_mka_sak_use_body_present(
1086         struct ieee802_1x_mka_participant *participant)
1087 {
1088         if (participant->to_use_sak)
1089                 return TRUE;
1090         else
1091                 return FALSE;
1092 }
1093
1094
1095 /**
1096  * ieee802_1x_mka_get_sak_use_length
1097  */
1098 static int
1099 ieee802_1x_mka_get_sak_use_length(
1100         struct ieee802_1x_mka_participant *participant)
1101 {
1102         int length = MKA_HDR_LEN;
1103
1104         if (participant->kay->macsec_desired && participant->advised_desired)
1105                 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1106         else
1107                 length = MKA_HDR_LEN;
1108
1109         length = (length + 0x3) & ~0x3;
1110
1111         return length;
1112 }
1113
1114
1115 /**
1116  *
1117  */
1118 static u32
1119 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1120                        struct ieee802_1x_mka_ki *ki)
1121 {
1122         struct receive_sa *rxsa;
1123         struct receive_sc *rxsc;
1124         u32 lpn = 0;
1125
1126         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1127                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1128                 {
1129                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1130                                 secy_get_receive_lowest_pn(principal->kay,
1131                                                            rxsa);
1132
1133                                 lpn = lpn > rxsa->lowest_pn ?
1134                                         lpn : rxsa->lowest_pn;
1135                                 break;
1136                         }
1137                 }
1138         }
1139
1140         if (lpn == 0)
1141                 lpn = 1;
1142
1143         return lpn;
1144 }
1145
1146
1147 /**
1148  * ieee802_1x_mka_encode_sak_use_body -
1149  */
1150 static int
1151 ieee802_1x_mka_encode_sak_use_body(
1152         struct ieee802_1x_mka_participant *participant,
1153         struct wpabuf *buf)
1154 {
1155         struct ieee802_1x_mka_sak_use_body *body;
1156         unsigned int length;
1157         u32 pn = 1;
1158
1159         length = ieee802_1x_mka_get_sak_use_length(participant);
1160         body = wpabuf_put(buf, length);
1161
1162         body->type = MKA_SAK_USE;
1163         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1164
1165         if (length == MKA_HDR_LEN) {
1166                 body->ptx = TRUE;
1167                 body->prx = TRUE;
1168                 body->lan = 0;
1169                 body->lrx = FALSE;
1170                 body->ltx = FALSE;
1171                 body->delay_protect = FALSE;
1172                 return 0;
1173         }
1174
1175         /* data protect, lowest accept packet number */
1176         body->delay_protect = participant->kay->macsec_replay_protect;
1177         pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1178         if (pn > participant->kay->pn_exhaustion) {
1179                 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1180                 if (participant->is_key_server)
1181                         participant->new_sak = TRUE;
1182         }
1183
1184         body->llpn = host_to_be32(pn);
1185         pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1186         body->olpn = host_to_be32(pn);
1187
1188         /* plain tx, plain rx */
1189         if (participant->kay->macsec_protect)
1190                 body->ptx = FALSE;
1191         else
1192                 body->ptx = TRUE;
1193
1194         if (participant->kay->macsec_validate == Strict)
1195                 body->prx = FALSE;
1196         else
1197                 body->prx = TRUE;
1198
1199         /* latest key: rx, tx, key server member identifier key number */
1200         body->lan = participant->lan;
1201         os_memcpy(body->lsrv_mi, participant->lki.mi,
1202                   sizeof(body->lsrv_mi));
1203         body->lkn = host_to_be32(participant->lki.kn);
1204         body->lrx = participant->lrx;
1205         body->ltx = participant->ltx;
1206
1207         /* old key: rx, tx, key server member identifier key number */
1208         body->oan = participant->oan;
1209         if (participant->oki.kn != participant->lki.kn &&
1210             participant->oki.kn != 0) {
1211                 body->otx = TRUE;
1212                 body->orx = TRUE;
1213                 os_memcpy(body->osrv_mi, participant->oki.mi,
1214                           sizeof(body->osrv_mi));
1215                 body->okn = host_to_be32(participant->oki.kn);
1216         } else {
1217                 body->otx = FALSE;
1218                 body->orx = FALSE;
1219         }
1220
1221         /* set CP's variable */
1222         if (body->ltx) {
1223                 if (!participant->kay->tx_enable)
1224                         participant->kay->tx_enable = TRUE;
1225
1226                 if (!participant->kay->port_enable)
1227                         participant->kay->port_enable = TRUE;
1228         }
1229         if (body->lrx) {
1230                 if (!participant->kay->rx_enable)
1231                         participant->kay->rx_enable = TRUE;
1232         }
1233
1234         ieee802_1x_mka_dump_sak_use_body(body);
1235         return 0;
1236 }
1237
1238
1239 /**
1240  * ieee802_1x_mka_decode_sak_use_body -
1241  */
1242 static int
1243 ieee802_1x_mka_decode_sak_use_body(
1244         struct ieee802_1x_mka_participant *participant,
1245         const u8 *mka_msg, size_t msg_len)
1246 {
1247         struct ieee802_1x_mka_hdr *hdr;
1248         struct ieee802_1x_mka_sak_use_body *body;
1249         struct ieee802_1x_kay_peer *peer;
1250         struct transmit_sa *txsa;
1251         struct data_key *sa_key = NULL;
1252         size_t body_len;
1253         struct ieee802_1x_mka_ki ki;
1254         u32 lpn;
1255         Boolean all_receiving;
1256         Boolean founded;
1257
1258         if (!participant->principal) {
1259                 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1260                 return -1;
1261         }
1262         peer = ieee802_1x_kay_get_live_peer(participant,
1263                                             participant->current_peer_id.mi);
1264         if (!peer) {
1265                 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1266                 return -1;
1267         }
1268
1269         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1270         body_len = get_mka_param_body_len(hdr);
1271         body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1272         ieee802_1x_mka_dump_sak_use_body(body);
1273
1274         if ((body_len != 0) && (body_len < 40)) {
1275                 wpa_printf(MSG_ERROR,
1276                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1277                            (int) body_len);
1278                 return -1;
1279         }
1280
1281         /* TODO: what action should I take when peer does not support MACsec */
1282         if (body_len == 0) {
1283                 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1284                 return 0;
1285         }
1286
1287         /* TODO: when the plain tx or rx of peer is true, should I change
1288          * the attribute of controlled port
1289          */
1290         if (body->prx)
1291                 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1292
1293         if (body->ptx)
1294                 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1295
1296         /* check latest key is valid */
1297         if (body->ltx || body->lrx) {
1298                 founded = FALSE;
1299                 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1300                 ki.kn = be_to_host32(body->lkn);
1301                 dl_list_for_each(sa_key, &participant->sak_list,
1302                                  struct data_key, list) {
1303                         if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1304                                 founded = TRUE;
1305                                 break;
1306                         }
1307                 }
1308                 if (!founded) {
1309                         wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1310                         return -1;
1311                 }
1312                 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1313                               sizeof(participant->lki.mi)) == 0 &&
1314                     be_to_host32(body->lkn) == participant->lki.kn &&
1315                     body->lan == participant->lan) {
1316                         peer->sak_used = TRUE;
1317                 }
1318                 if (body->ltx && peer->is_key_server) {
1319                         ieee802_1x_cp_set_servertransmitting(
1320                                 participant->kay->cp, TRUE);
1321                         ieee802_1x_cp_sm_step(participant->kay->cp);
1322                 }
1323         }
1324
1325         /* check old key is valid */
1326         if (body->otx || body->orx) {
1327                 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1328                               sizeof(participant->oki.mi)) != 0 ||
1329                     be_to_host32(body->okn) != participant->oki.kn ||
1330                     body->oan != participant->oan) {
1331                         wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1332                         return -1;
1333                 }
1334         }
1335
1336         /* TODO: how to set the MACsec hardware when delay_protect is true */
1337         if (body->delay_protect &&
1338             (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1339                 wpa_printf(MSG_WARNING,
1340                            "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1341                 return -1;
1342         }
1343
1344         /* check all live peer have used the sak for receiving sa */
1345         all_receiving = TRUE;
1346         dl_list_for_each(peer, &participant->live_peers,
1347                          struct ieee802_1x_kay_peer, list) {
1348                 if (!peer->sak_used) {
1349                         all_receiving = FALSE;
1350                         break;
1351                 }
1352         }
1353         if (all_receiving) {
1354                 participant->to_dist_sak = FALSE;
1355                 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1356                 ieee802_1x_cp_sm_step(participant->kay->cp);
1357         }
1358
1359         /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1360         lpn = be_to_host32(body->llpn);
1361         if (lpn > participant->kay->pn_exhaustion) {
1362                 if (participant->is_key_server) {
1363                         participant->new_sak = TRUE;
1364                         wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1365                 }
1366         }
1367
1368         founded = FALSE;
1369         dl_list_for_each(txsa, &participant->txsc->sa_list,
1370                          struct transmit_sa, list) {
1371                 if (sa_key != NULL && txsa->pkey == sa_key) {
1372                         founded = TRUE;
1373                         break;
1374                 }
1375         }
1376         if (!founded) {
1377                 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1378                 return -1;
1379         }
1380
1381         /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1382          * npn is larger than txsa's npn, set it to txsa.
1383          */
1384         secy_get_transmit_next_pn(participant->kay, txsa);
1385         if (lpn > txsa->next_pn) {
1386                 secy_set_transmit_next_pn(participant->kay, txsa);
1387                 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1388         }
1389
1390         return 0;
1391 }
1392
1393
1394 /**
1395  * ieee802_1x_mka_dist_sak_body_present
1396  */
1397 static Boolean
1398 ieee802_1x_mka_dist_sak_body_present(
1399         struct ieee802_1x_mka_participant *participant)
1400 {
1401         if (!participant->to_dist_sak || !participant->new_key)
1402                 return FALSE;
1403
1404         return TRUE;
1405 }
1406
1407
1408 /**
1409  * ieee802_1x_kay_get_dist_sak_length
1410  */
1411 static int
1412 ieee802_1x_mka_get_dist_sak_length(
1413         struct ieee802_1x_mka_participant *participant)
1414 {
1415         int length;
1416         int cs_index = participant->kay->macsec_csindex;
1417
1418         if (participant->advised_desired) {
1419                 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1420                 if (cs_index != DEFAULT_CS_INDEX)
1421                         length += CS_ID_LEN;
1422
1423                 length += cipher_suite_tbl[cs_index].sak_len + 8;
1424         } else {
1425                 length = MKA_HDR_LEN;
1426         }
1427         length = (length + 0x3) & ~0x3;
1428
1429         return length;
1430 }
1431
1432
1433 /**
1434  * ieee802_1x_mka_encode_dist_sak_body -
1435  */
1436 static int
1437 ieee802_1x_mka_encode_dist_sak_body(
1438         struct ieee802_1x_mka_participant *participant,
1439         struct wpabuf *buf)
1440 {
1441         struct ieee802_1x_mka_dist_sak_body *body;
1442         struct data_key *sak;
1443         unsigned int length;
1444         int cs_index;
1445         int sak_pos;
1446
1447         length = ieee802_1x_mka_get_dist_sak_length(participant);
1448         body = wpabuf_put(buf, length);
1449         body->type = MKA_DISTRIBUTED_SAK;
1450         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1451         if (length == MKA_HDR_LEN) {
1452                 body->confid_offset = 0;
1453                 body->dan = 0;
1454                 return 0;
1455         }
1456
1457         sak = participant->new_key;
1458         body->confid_offset = sak->confidentiality_offset;
1459         body->dan = sak->an;
1460         body->kn = host_to_be32(sak->key_identifier.kn);
1461         cs_index = participant->kay->macsec_csindex;
1462         sak_pos = 0;
1463         if (cs_index != DEFAULT_CS_INDEX) {
1464                 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1465                 sak_pos = CS_ID_LEN;
1466         }
1467         if (aes_wrap(participant->kek.key, 16,
1468                      cipher_suite_tbl[cs_index].sak_len / 8,
1469                      sak->key, body->sak + sak_pos)) {
1470                 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1471                 return -1;
1472         }
1473
1474         ieee802_1x_mka_dump_dist_sak_body(body);
1475
1476         return 0;
1477 }
1478
1479
1480 /**
1481  * ieee802_1x_kay_init_data_key -
1482  */
1483 static struct data_key *
1484 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1485 {
1486         struct data_key *pkey;
1487
1488         if (!conf)
1489                 return NULL;
1490
1491         pkey = os_zalloc(sizeof(*pkey));
1492         if (pkey == NULL) {
1493                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1494                 return NULL;
1495         }
1496
1497         pkey->key = os_zalloc(conf->key_len);
1498         if (pkey->key == NULL) {
1499                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1500                 os_free(pkey);
1501                 return NULL;
1502         }
1503
1504         os_memcpy(pkey->key, conf->key, conf->key_len);
1505         os_memcpy(&pkey->key_identifier, &conf->ki,
1506                   sizeof(pkey->key_identifier));
1507         pkey->confidentiality_offset = conf->offset;
1508         pkey->an = conf->an;
1509         pkey->transmits = conf->tx;
1510         pkey->receives = conf->rx;
1511         os_get_time(&pkey->created_time);
1512
1513         pkey->user = 1;
1514
1515         return pkey;
1516 }
1517
1518
1519 /**
1520  * ieee802_1x_kay_decode_dist_sak_body -
1521  */
1522 static int
1523 ieee802_1x_mka_decode_dist_sak_body(
1524         struct ieee802_1x_mka_participant *participant,
1525         const u8 *mka_msg, size_t msg_len)
1526 {
1527         struct ieee802_1x_mka_hdr *hdr;
1528         struct ieee802_1x_mka_dist_sak_body *body;
1529         struct ieee802_1x_kay_peer *peer;
1530         struct macsec_ciphersuite *cs;
1531         size_t body_len;
1532         struct key_conf *conf;
1533         struct data_key *sa_key = NULL;
1534         struct ieee802_1x_mka_ki sak_ki;
1535         int sak_len;
1536         u8 *wrap_sak;
1537         u8 *unwrap_sak;
1538
1539         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1540         body_len = get_mka_param_body_len(hdr);
1541         if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1542                 wpa_printf(MSG_ERROR,
1543                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1544                            (int) body_len);
1545                 return -1;
1546         }
1547
1548         if (!participant->principal) {
1549                 wpa_printf(MSG_ERROR,
1550                            "KaY: I can't accept the distributed SAK as I am not principal");
1551                 return -1;
1552         }
1553         if (participant->is_key_server) {
1554                 wpa_printf(MSG_ERROR,
1555                            "KaY: I can't accept the distributed SAK as myself is key server ");
1556                 return -1;
1557         }
1558         if (!participant->kay->macsec_desired ||
1559             participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1560                 wpa_printf(MSG_ERROR,
1561                            "KaY: I am not MACsec-desired or without MACsec capable");
1562                 return -1;
1563         }
1564
1565         peer = ieee802_1x_kay_get_live_peer(participant,
1566                                             participant->current_peer_id.mi);
1567         if (!peer) {
1568                 wpa_printf(MSG_ERROR,
1569                            "KaY: The key server is not in my live peers list");
1570                 return -1;
1571         }
1572         if (os_memcmp(&participant->kay->key_server_sci,
1573                       &peer->sci, sizeof(struct ieee802_1x_mka_sci)) != 0) {
1574                 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1575                 return -1;
1576         }
1577         if (body_len == 0) {
1578                 participant->kay->authenticated = TRUE;
1579                 participant->kay->secured = FALSE;
1580                 participant->kay->failed = FALSE;
1581                 participant->advised_desired = FALSE;
1582                 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1583                 ieee802_1x_cp_sm_step(participant->kay->cp);
1584                 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1585                 participant->to_use_sak = TRUE;
1586                 return 0;
1587         }
1588         participant->advised_desired = TRUE;
1589         participant->kay->authenticated = FALSE;
1590         participant->kay->secured = TRUE;
1591         participant->kay->failed = FALSE;
1592         ieee802_1x_cp_connect_secure(participant->kay->cp);
1593         ieee802_1x_cp_sm_step(participant->kay->cp);
1594
1595         body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1596         ieee802_1x_mka_dump_dist_sak_body(body);
1597         dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1598         {
1599                 if (os_memcmp(sa_key->key_identifier.mi,
1600                               participant->current_peer_id.mi, MI_LEN) == 0 &&
1601                     sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1602                         wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1603                         return 0;
1604                 }
1605         }
1606         if (body_len == 28) {
1607                 sak_len = DEFAULT_SA_KEY_LEN;
1608                 wrap_sak =  body->sak;
1609                 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1610         } else {
1611                 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1612                 if (!cs) {
1613                         wpa_printf(MSG_ERROR,
1614                                    "KaY: I can't support the Cipher Suite advised by key server");
1615                         return -1;
1616                 }
1617                 sak_len = cs->sak_len;
1618                 wrap_sak = body->sak + CS_ID_LEN;
1619                 participant->kay->macsec_csindex = cs->index;
1620         }
1621
1622         unwrap_sak = os_zalloc(sak_len);
1623         if (!unwrap_sak) {
1624                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1625                 return -1;
1626         }
1627         if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1628                        unwrap_sak)) {
1629                 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1630                 os_free(unwrap_sak);
1631                 return -1;
1632         }
1633         wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1634
1635         conf = os_zalloc(sizeof(*conf));
1636         if (!conf) {
1637                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1638                 os_free(unwrap_sak);
1639                 return -1;
1640         }
1641         conf->key_len = sak_len;
1642
1643         conf->key = os_zalloc(conf->key_len);
1644         if (!conf->key) {
1645                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1646                 os_free(unwrap_sak);
1647                 os_free(conf);
1648                 return -1;
1649         }
1650
1651         os_memcpy(conf->key, unwrap_sak, conf->key_len);
1652
1653         os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1654                   sizeof(sak_ki.mi));
1655         sak_ki.kn = be_to_host32(body->kn);
1656
1657         os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1658         conf->ki.kn = sak_ki.kn;
1659         conf->an = body->dan;
1660         conf->offset = body->confid_offset;
1661         conf->rx = TRUE;
1662         conf->tx = TRUE;
1663
1664         sa_key = ieee802_1x_kay_init_data_key(conf);
1665         if (!sa_key) {
1666                 os_free(unwrap_sak);
1667                 os_free(conf->key);
1668                 os_free(conf);
1669                 return -1;
1670         }
1671
1672         dl_list_add(&participant->sak_list, &sa_key->list);
1673
1674         ieee802_1x_cp_set_ciphersuite(
1675                 participant->kay->cp,
1676                 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1677         ieee802_1x_cp_sm_step(participant->kay->cp);
1678         ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1679         ieee802_1x_cp_sm_step(participant->kay->cp);
1680         ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1681         ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1682         ieee802_1x_cp_signal_newsak(participant->kay->cp);
1683         ieee802_1x_cp_sm_step(participant->kay->cp);
1684
1685         participant->to_use_sak = TRUE;
1686
1687         os_free(unwrap_sak);
1688         os_free(conf->key);
1689         os_free(conf);
1690
1691         return 0;
1692 }
1693
1694
1695 /**
1696  * ieee802_1x_mka_icv_body_present
1697  */
1698 static Boolean
1699 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1700 {
1701         return TRUE;
1702 }
1703
1704
1705 /**
1706  * ieee802_1x_kay_get_icv_length
1707  */
1708 static int
1709 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1710 {
1711         int length;
1712
1713         length = sizeof(struct ieee802_1x_mka_icv_body);
1714         length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1715
1716         return (length + 0x3) & ~0x3;
1717 }
1718
1719
1720 /**
1721  * ieee802_1x_mka_encode_icv_body -
1722  */
1723 static int
1724 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1725                                struct wpabuf *buf)
1726 {
1727         struct ieee802_1x_mka_icv_body *body;
1728         unsigned int length;
1729         u8 cmac[MAX_ICV_LEN];
1730
1731         length = ieee802_1x_mka_get_icv_length(participant);
1732         if (length != DEFAULT_ICV_LEN)  {
1733                 body = wpabuf_put(buf, MKA_HDR_LEN);
1734                 body->type = MKA_ICV_INDICATOR;
1735                 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1736         }
1737
1738         if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1739                     participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1740                 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1741                 return -1;
1742         }
1743
1744         if (length != DEFAULT_ICV_LEN)  {
1745                 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1746                           length - MKA_HDR_LEN);
1747         } else {
1748                 os_memcpy(wpabuf_put(buf, length), cmac, length);
1749         }
1750
1751         return 0;
1752 }
1753
1754 /**
1755  * ieee802_1x_mka_decode_icv_body -
1756  */
1757 static u8 *
1758 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1759                                const u8 *mka_msg, size_t msg_len)
1760 {
1761         struct ieee802_1x_mka_hdr *hdr;
1762         struct ieee802_1x_mka_icv_body *body;
1763         size_t body_len;
1764         size_t left_len;
1765         u8 body_type;
1766         const u8 *pos;
1767
1768         pos = mka_msg;
1769         left_len = msg_len;
1770         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1771                 hdr = (struct ieee802_1x_mka_hdr *) pos;
1772                 body_len = get_mka_param_body_len(hdr);
1773                 body_type = get_mka_param_body_type(hdr);
1774
1775                 if (left_len < (body_len + MKA_HDR_LEN))
1776                         break;
1777
1778                 if (body_type != MKA_ICV_INDICATOR) {
1779                         left_len -= MKA_HDR_LEN + body_len;
1780                         pos += MKA_HDR_LEN + body_len;
1781                         continue;
1782                 }
1783
1784                 body = (struct ieee802_1x_mka_icv_body *)pos;
1785                 if (body_len
1786                         < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1787                         return NULL;
1788                 }
1789
1790                 return body->icv;
1791         }
1792
1793         return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1794 }
1795
1796
1797 /**
1798  * ieee802_1x_mka_decode_dist_cak_body-
1799  */
1800 static int
1801 ieee802_1x_mka_decode_dist_cak_body(
1802         struct ieee802_1x_mka_participant *participant,
1803         const u8 *mka_msg, size_t msg_len)
1804 {
1805         struct ieee802_1x_mka_hdr *hdr;
1806         size_t body_len;
1807
1808         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1809         body_len = get_mka_param_body_len(hdr);
1810         if (body_len < 28) {
1811                 wpa_printf(MSG_ERROR,
1812                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1813                            (int) body_len);
1814                 return -1;
1815         }
1816
1817         return 0;
1818 }
1819
1820
1821 /**
1822  * ieee802_1x_mka_decode_kmd_body -
1823  */
1824 static int
1825 ieee802_1x_mka_decode_kmd_body(
1826         struct ieee802_1x_mka_participant *participant,
1827         const u8 *mka_msg, size_t msg_len)
1828 {
1829         struct ieee802_1x_mka_hdr *hdr;
1830         size_t body_len;
1831
1832         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1833         body_len = get_mka_param_body_len(hdr);
1834         if (body_len < 5) {
1835                 wpa_printf(MSG_ERROR,
1836                            "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1837                            (int) body_len);
1838                 return -1;
1839         }
1840
1841         return 0;
1842 }
1843
1844
1845 /**
1846  * ieee802_1x_mka_decode_announce_body -
1847  */
1848 static int ieee802_1x_mka_decode_announce_body(
1849         struct ieee802_1x_mka_participant *participant,
1850         const u8 *mka_msg, size_t msg_len)
1851 {
1852         return 0;
1853 }
1854
1855
1856 static struct mka_param_body_handler mak_body_handler[] = {
1857         /* basic parameter set */
1858         {
1859                 ieee802_1x_mka_encode_basic_body,
1860                 NULL,
1861                 ieee802_1x_mka_basic_body_length,
1862                 ieee802_1x_mka_basic_body_present
1863         },
1864
1865         /* live peer list parameter set */
1866         {
1867                 ieee802_1x_mka_encode_live_peer_body,
1868                 ieee802_1x_mka_decode_live_peer_body,
1869                 ieee802_1x_mka_get_live_peer_length,
1870                 ieee802_1x_mka_live_peer_body_present
1871         },
1872
1873         /* potential peer list parameter set */
1874         {
1875                 ieee802_1x_mka_encode_potential_peer_body,
1876                 ieee802_1x_mka_decode_potential_peer_body,
1877                 ieee802_1x_mka_get_potential_peer_length,
1878                 ieee802_1x_mka_potential_peer_body_present
1879         },
1880
1881         /* sak use parameter set */
1882         {
1883                 ieee802_1x_mka_encode_sak_use_body,
1884                 ieee802_1x_mka_decode_sak_use_body,
1885                 ieee802_1x_mka_get_sak_use_length,
1886                 ieee802_1x_mka_sak_use_body_present
1887         },
1888
1889         /* distribute sak parameter set */
1890         {
1891                 ieee802_1x_mka_encode_dist_sak_body,
1892                 ieee802_1x_mka_decode_dist_sak_body,
1893                 ieee802_1x_mka_get_dist_sak_length,
1894                 ieee802_1x_mka_dist_sak_body_present
1895         },
1896
1897         /* distribute cak parameter set */
1898         {
1899                 NULL,
1900                 ieee802_1x_mka_decode_dist_cak_body,
1901                 NULL,
1902                 NULL
1903         },
1904
1905         /* kmd parameter set */
1906         {
1907                 NULL,
1908                 ieee802_1x_mka_decode_kmd_body,
1909                 NULL,
1910                 NULL
1911         },
1912
1913         /* announce parameter set */
1914         {
1915                 NULL,
1916                 ieee802_1x_mka_decode_announce_body,
1917                 NULL,
1918                 NULL
1919         },
1920
1921         /* icv parameter set */
1922         {
1923                 ieee802_1x_mka_encode_icv_body,
1924                 NULL,
1925                 ieee802_1x_mka_get_icv_length,
1926                 ieee802_1x_mka_icv_body_present
1927         },
1928 };
1929
1930
1931 /**
1932  * ieee802_1x_kay_deinit_data_key -
1933  */
1934 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1935 {
1936         if (!pkey)
1937                 return;
1938
1939         pkey->user--;
1940         if (pkey->user > 1)
1941                 return;
1942
1943         dl_list_del(&pkey->list);
1944         os_free(pkey->key);
1945         os_free(pkey);
1946 }
1947
1948
1949 /**
1950  * ieee802_1x_kay_generate_new_sak -
1951  */
1952 static int
1953 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1954 {
1955         struct data_key *sa_key = NULL;
1956         struct key_conf *conf;
1957         struct ieee802_1x_kay_peer *peer;
1958         struct ieee802_1x_kay *kay = participant->kay;
1959         int ctx_len, ctx_offset;
1960         u8 *context;
1961
1962         /* check condition for generating a fresh SAK:
1963          * must have one live peer
1964          * and MKA life time elapse since last distribution
1965          * or potential peer is empty
1966          */
1967         if (dl_list_empty(&participant->live_peers)) {
1968                 wpa_printf(MSG_ERROR,
1969                            "KaY: Live peers list must not empty when generating fresh SAK");
1970                 return -1;
1971         }
1972
1973         /* FIXME: A fresh SAK not generated until
1974          * the live peer list contains at least one peer and
1975          * MKA life time has elapsed since the prior SAK was first distributed,
1976          * or the Key server's potential peer is empty
1977          * but I can't understand the second item, so
1978          * here only check first item and ingore
1979          *   && (!dl_list_empty(&participant->potential_peers))) {
1980          */
1981         if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1982                 wpa_printf(MSG_ERROR,
1983                            "KaY: Life time have not elapsed since prior SAK distributed");
1984                 return -1;
1985         }
1986
1987         conf = os_zalloc(sizeof(*conf));
1988         if (!conf) {
1989                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1990                 return -1;
1991         }
1992         conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1993
1994         conf->key = os_zalloc(conf->key_len);
1995         if (!conf->key) {
1996                 os_free(conf);
1997                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1998                 return -1;
1999         }
2000
2001         ctx_len = conf->key_len + sizeof(kay->dist_kn);
2002         dl_list_for_each(peer, &participant->live_peers,
2003                          struct ieee802_1x_kay_peer, list)
2004                 ctx_len += sizeof(peer->mi);
2005         ctx_len += sizeof(participant->mi);
2006
2007         context = os_zalloc(ctx_len);
2008         if (!context) {
2009                 os_free(conf->key);
2010                 os_free(conf);
2011                 return -1;
2012         }
2013         ctx_offset = 0;
2014         if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2015                 os_free(context);
2016                 os_free(conf->key);
2017                 os_free(conf);
2018                 return -1;
2019         }
2020         ctx_offset += conf->key_len;
2021         dl_list_for_each(peer, &participant->live_peers,
2022                          struct ieee802_1x_kay_peer, list) {
2023                 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2024                 ctx_offset += sizeof(peer->mi);
2025         }
2026         os_memcpy(context + ctx_offset, participant->mi,
2027                   sizeof(participant->mi));
2028         ctx_offset += sizeof(participant->mi);
2029         os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2030
2031         if (conf->key_len == 16) {
2032                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2033                                                 context, ctx_len, conf->key);
2034         } else if (conf->key_len == 32) {
2035                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2036                                                 context, ctx_len, conf->key);
2037         } else {
2038                 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2039                 os_free(conf->key);
2040                 os_free(conf);
2041                 os_free(context);
2042                 return -1;
2043         }
2044         wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2045                     conf->key, conf->key_len);
2046
2047         os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2048         conf->ki.kn = participant->kay->dist_kn;
2049         conf->an = participant->kay->dist_an;
2050         conf->offset = kay->macsec_confidentiality;
2051         conf->rx = TRUE;
2052         conf->tx = TRUE;
2053
2054         sa_key = ieee802_1x_kay_init_data_key(conf);
2055         if (!sa_key) {
2056                 os_free(conf->key);
2057                 os_free(conf);
2058                 os_free(context);
2059                 return -1;
2060         }
2061         participant->new_key = sa_key;
2062
2063         dl_list_add(&participant->sak_list, &sa_key->list);
2064         ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2065                                       cipher_suite_tbl[kay->macsec_csindex].id);
2066         ieee802_1x_cp_sm_step(kay->cp);
2067         ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2068         ieee802_1x_cp_sm_step(kay->cp);
2069         ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2070         ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2071         ieee802_1x_cp_signal_newsak(kay->cp);
2072         ieee802_1x_cp_sm_step(kay->cp);
2073
2074         dl_list_for_each(peer, &participant->live_peers,
2075                          struct ieee802_1x_kay_peer, list)
2076                 peer->sak_used = FALSE;
2077
2078         participant->kay->dist_kn++;
2079         participant->kay->dist_an++;
2080         if (participant->kay->dist_an > 3)
2081                 participant->kay->dist_an = 0;
2082
2083         participant->kay->dist_time = time(NULL);
2084
2085         os_free(conf->key);
2086         os_free(conf);
2087         os_free(context);
2088         return 0;
2089 }
2090
2091
2092 /**
2093  * ieee802_1x_kay_elect_key_server - elect the key server
2094  * when to elect: whenever the live peers list changes
2095  */
2096 static int
2097 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2098 {
2099         struct ieee802_1x_kay_peer *peer;
2100         struct ieee802_1x_kay_peer *key_server = NULL;
2101         struct ieee802_1x_kay *kay = participant->kay;
2102         Boolean i_is_key_server;
2103
2104         if (participant->is_obliged_key_server) {
2105                 participant->new_sak = TRUE;
2106                 participant->to_dist_sak = FALSE;
2107                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2108                 return 0;
2109         }
2110
2111         /* elect the key server among the peers */
2112         dl_list_for_each(peer, &participant->live_peers,
2113                          struct ieee802_1x_kay_peer, list) {
2114                 if (!peer->is_key_server)
2115                         continue;
2116
2117                 if (!key_server) {
2118                         key_server = peer;
2119                         continue;
2120                 }
2121
2122                 if (peer->key_server_priority <
2123                     key_server->key_server_priority) {
2124                         key_server = peer;
2125                 } else if (peer->key_server_priority ==
2126                            key_server->key_server_priority) {
2127                         if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2128                                       ETH_ALEN) < 0)
2129                                 key_server = peer;
2130                 }
2131         }
2132
2133         /* elect the key server between me and the above elected peer */
2134         i_is_key_server = FALSE;
2135         if (key_server && participant->can_be_key_server) {
2136                 if (kay->actor_priority
2137                            < key_server->key_server_priority) {
2138                         i_is_key_server = TRUE;
2139                 } else if (kay->actor_priority
2140                                         == key_server->key_server_priority) {
2141                         if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2142                                       ETH_ALEN) < 0)
2143                                 i_is_key_server = TRUE;
2144                 }
2145         } else if (participant->can_be_key_server) {
2146                 i_is_key_server = TRUE;
2147         }
2148
2149         if (i_is_key_server) {
2150                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2151                 if (os_memcmp(&kay->key_server_sci, &kay->actor_sci,
2152                               sizeof(kay->key_server_sci))) {
2153                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2154                         ieee802_1x_cp_sm_step(kay->cp);
2155                 }
2156
2157                 participant->is_key_server = TRUE;
2158                 participant->principal = TRUE;
2159                 participant->new_sak = TRUE;
2160                 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2161                 participant->to_dist_sak = FALSE;
2162                 participant->is_elected = TRUE;
2163
2164                 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2165                           sizeof(kay->key_server_sci));
2166                 kay->key_server_priority = kay->actor_priority;
2167         } else if (key_server) {
2168                 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2169                 if (os_memcmp(&kay->key_server_sci, &key_server->sci,
2170                               sizeof(kay->key_server_sci))) {
2171                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2172                         ieee802_1x_cp_sm_step(kay->cp);
2173                 }
2174
2175                 participant->is_key_server = FALSE;
2176                 participant->principal = TRUE;
2177                 participant->is_elected = TRUE;
2178
2179                 os_memcpy(&kay->key_server_sci, &key_server->sci,
2180                           sizeof(kay->key_server_sci));
2181                 kay->key_server_priority = key_server->key_server_priority;
2182         } else {
2183                 participant->principal = FALSE;
2184                 participant->is_key_server = FALSE;
2185                 participant->is_elected = FALSE;
2186         }
2187
2188         return 0;
2189 }
2190
2191
2192 /**
2193  * ieee802_1x_kay_decide_macsec_use - the key server determinate
2194  *               how to use MACsec: whether use MACsec and its capability
2195  * protectFrames will be advised if the key server and one of its live peers are
2196  * MACsec capable and one of those request MACsec protection
2197  */
2198 static int
2199 ieee802_1x_kay_decide_macsec_use(
2200         struct ieee802_1x_mka_participant *participant)
2201 {
2202         struct ieee802_1x_kay *kay = participant->kay;
2203         struct ieee802_1x_kay_peer *peer;
2204         enum macsec_cap less_capability;
2205         Boolean has_peer;
2206
2207         if (!participant->is_key_server)
2208                 return -1;
2209
2210         /* key server self is MACsec-desired and requesting MACsec */
2211         if (!kay->macsec_desired) {
2212                 participant->advised_desired = FALSE;
2213                 return -1;
2214         }
2215         if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2216                 participant->advised_desired = FALSE;
2217                 return -1;
2218         }
2219         less_capability = kay->macsec_capable;
2220
2221         /* at least one of peers is MACsec-desired and requesting MACsec */
2222         has_peer = FALSE;
2223         dl_list_for_each(peer, &participant->live_peers,
2224                          struct ieee802_1x_kay_peer, list) {
2225                 if (!peer->macsec_desired)
2226                         continue;
2227
2228                 if (peer->macsec_capbility == MACSEC_CAP_NOT_IMPLEMENTED)
2229                         continue;
2230
2231                 less_capability = (less_capability < peer->macsec_capbility) ?
2232                         less_capability : peer->macsec_capbility;
2233                 has_peer = TRUE;
2234         }
2235
2236         if (has_peer) {
2237                 participant->advised_desired = TRUE;
2238                 participant->advised_capability = less_capability;
2239                 kay->authenticated = FALSE;
2240                 kay->secured = TRUE;
2241                 kay->failed = FALSE;
2242                 ieee802_1x_cp_connect_secure(kay->cp);
2243                 ieee802_1x_cp_sm_step(kay->cp);
2244         } else {
2245                 participant->advised_desired = FALSE;
2246                 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2247                 participant->to_use_sak = FALSE;
2248                 kay->authenticated = TRUE;
2249                 kay->secured = FALSE;
2250                 kay->failed = FALSE;
2251                 kay->ltx_kn = 0;
2252                 kay->ltx_an = 0;
2253                 kay->lrx_kn = 0;
2254                 kay->lrx_an = 0;
2255                 kay->otx_kn = 0;
2256                 kay->otx_an = 0;
2257                 kay->orx_kn = 0;
2258                 kay->orx_an = 0;
2259                 ieee802_1x_cp_connect_authenticated(kay->cp);
2260                 ieee802_1x_cp_sm_step(kay->cp);
2261         }
2262
2263         return 0;
2264 }
2265
2266 static const u8 pae_group_addr[ETH_ALEN] = {
2267         0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2268 };
2269
2270
2271 /**
2272  * ieee802_1x_kay_encode_mkpdu -
2273  */
2274 static int
2275 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2276                             struct wpabuf *pbuf)
2277 {
2278         unsigned int i;
2279         struct ieee8023_hdr *ether_hdr;
2280         struct ieee802_1x_hdr *eapol_hdr;
2281
2282         ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2283         os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2284         os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2285                   sizeof(ether_hdr->dest));
2286         ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2287
2288         eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2289         eapol_hdr->version = EAPOL_VERSION;
2290         eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2291         eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2292
2293         for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2294                 if (mak_body_handler[i].body_present &&
2295                     mak_body_handler[i].body_present(participant)) {
2296                         if (mak_body_handler[i].body_tx(participant, pbuf))
2297                                 return -1;
2298                 }
2299         }
2300
2301         return 0;
2302 }
2303
2304 /**
2305  * ieee802_1x_participant_send_mkpdu -
2306  */
2307 static int
2308 ieee802_1x_participant_send_mkpdu(
2309         struct ieee802_1x_mka_participant *participant)
2310 {
2311         struct wpabuf *buf;
2312         struct ieee802_1x_kay *kay = participant->kay;
2313         size_t length = 0;
2314         unsigned int i;
2315
2316         wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2317         length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2318         for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2319                 if (mak_body_handler[i].body_present &&
2320                     mak_body_handler[i].body_present(participant))
2321                         length += mak_body_handler[i].body_length(participant);
2322         }
2323
2324         buf = wpabuf_alloc(length);
2325         if (!buf) {
2326                 wpa_printf(MSG_ERROR, "KaY: out of memory");
2327                 return -1;
2328         }
2329
2330         if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2331                 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2332                 return -1;
2333         }
2334
2335         l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2336         wpabuf_free(buf);
2337
2338         kay->active = TRUE;
2339         participant->active = TRUE;
2340
2341         return 0;
2342 }
2343
2344
2345 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2346 /**
2347  * ieee802_1x_participant_timer -
2348  */
2349 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2350 {
2351         struct ieee802_1x_mka_participant *participant;
2352         struct ieee802_1x_kay *kay;
2353         struct ieee802_1x_kay_peer *peer, *pre_peer;
2354         time_t now = time(NULL);
2355         Boolean lp_changed;
2356         struct receive_sc *rxsc, *pre_rxsc;
2357         struct transmit_sa *txsa, *pre_txsa;
2358
2359         participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2360         kay = participant->kay;
2361         if (participant->cak_life) {
2362                 if (now > participant->cak_life) {
2363                         kay->authenticated = FALSE;
2364                         kay->secured = FALSE;
2365                         kay->failed = TRUE;
2366                         ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2367                         return;
2368                 }
2369         }
2370
2371         /* should delete MKA instance if there are not live peers
2372          * when the MKA life elapsed since its creating */
2373         if (participant->mka_life) {
2374                 if (dl_list_empty(&participant->live_peers)) {
2375                         if (now > participant->mka_life) {
2376                                 kay->authenticated = FALSE;
2377                                 kay->secured = FALSE;
2378                                 kay->failed = TRUE;
2379                                 ieee802_1x_kay_delete_mka(kay,
2380                                                           &participant->ckn);
2381                                 return;
2382                         }
2383                 } else {
2384                         participant->mka_life = 0;
2385                 }
2386         }
2387
2388         lp_changed = FALSE;
2389         dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2390                               struct ieee802_1x_kay_peer, list) {
2391                 if (now > peer->expire) {
2392                         wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2393                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2394                                     sizeof(peer->mi));
2395                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2396                         dl_list_for_each_safe(rxsc, pre_rxsc,
2397                                               &participant->rxsc_list,
2398                                               struct receive_sc, list) {
2399                                 if (os_memcmp(&rxsc->sci, &peer->sci,
2400                                               sizeof(rxsc->sci)) == 0) {
2401                                         secy_delete_receive_sc(kay, rxsc);
2402                                         ieee802_1x_kay_deinit_receive_sc(
2403                                                 participant, rxsc);
2404                                 }
2405                         }
2406                         dl_list_del(&peer->list);
2407                         os_free(peer);
2408                         lp_changed = TRUE;
2409                 }
2410         }
2411
2412         if (lp_changed) {
2413                 if (dl_list_empty(&participant->live_peers)) {
2414                         participant->advised_desired = FALSE;
2415                         participant->advised_capability =
2416                                 MACSEC_CAP_NOT_IMPLEMENTED;
2417                         participant->to_use_sak = FALSE;
2418                         kay->authenticated = TRUE;
2419                         kay->secured = FALSE;
2420                         kay->failed = FALSE;
2421                         kay->ltx_kn = 0;
2422                         kay->ltx_an = 0;
2423                         kay->lrx_kn = 0;
2424                         kay->lrx_an = 0;
2425                         kay->otx_kn = 0;
2426                         kay->otx_an = 0;
2427                         kay->orx_kn = 0;
2428                         kay->orx_an = 0;
2429                         dl_list_for_each_safe(txsa, pre_txsa,
2430                                               &participant->txsc->sa_list,
2431                                               struct transmit_sa, list) {
2432                                 secy_disable_transmit_sa(kay, txsa);
2433                                 ieee802_1x_kay_deinit_transmit_sa(txsa);
2434                         }
2435
2436                         ieee802_1x_cp_connect_authenticated(kay->cp);
2437                         ieee802_1x_cp_sm_step(kay->cp);
2438                 } else {
2439                         ieee802_1x_kay_elect_key_server(participant);
2440                         ieee802_1x_kay_decide_macsec_use(participant);
2441                 }
2442         }
2443
2444         dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2445                               struct ieee802_1x_kay_peer, list) {
2446                 if (now > peer->expire) {
2447                         wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2448                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2449                                     sizeof(peer->mi));
2450                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2451                         dl_list_del(&peer->list);
2452                         os_free(peer);
2453                 }
2454         }
2455
2456         if (participant->new_sak) {
2457                 if (!ieee802_1x_kay_generate_new_sak(participant))
2458                         participant->to_dist_sak = TRUE;
2459
2460                 participant->new_sak = FALSE;
2461         }
2462
2463         if (participant->retry_count < MAX_RETRY_CNT) {
2464                 ieee802_1x_participant_send_mkpdu(participant);
2465                 participant->retry_count++;
2466         }
2467
2468         eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2469                                ieee802_1x_participant_timer,
2470                                participant, NULL);
2471 }
2472
2473
2474 /**
2475  * ieee802_1x_kay_init_transmit_sa -
2476  */
2477 static struct transmit_sa *
2478 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2479                                 struct data_key *key)
2480 {
2481         struct transmit_sa *psa;
2482
2483         key->tx_latest = TRUE;
2484         key->rx_latest = TRUE;
2485
2486         psa = os_zalloc(sizeof(*psa));
2487         if (!psa) {
2488                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2489                 return NULL;
2490         }
2491
2492         if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2493             key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2494                 psa->confidentiality = TRUE;
2495         else
2496                 psa->confidentiality = FALSE;
2497
2498         psa->an = an;
2499         psa->pkey = key;
2500         psa->next_pn = next_PN;
2501         psa->sc = psc;
2502
2503         os_get_time(&psa->created_time);
2504         psa->in_use = FALSE;
2505
2506         dl_list_add(&psc->sa_list, &psa->list);
2507         wpa_printf(MSG_DEBUG,
2508                    "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2509                    (int) an, next_PN, psc->channel);
2510
2511         return psa;
2512 }
2513
2514
2515 /**
2516  * ieee802_1x_kay_deinit_transmit_sa -
2517  */
2518 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2519 {
2520         psa->pkey = NULL;
2521         wpa_printf(MSG_DEBUG,
2522                    "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2523                    psa->an, psa->sc->channel);
2524         dl_list_del(&psa->list);
2525         os_free(psa);
2526 }
2527
2528
2529 /**
2530  * init_transmit_sc -
2531  */
2532 static struct transmit_sc *
2533 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2534                                 int channel)
2535 {
2536         struct transmit_sc *psc;
2537
2538         psc = os_zalloc(sizeof(*psc));
2539         if (!psc) {
2540                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2541                 return NULL;
2542         }
2543         os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2544         psc->channel = channel;
2545
2546         os_get_time(&psc->created_time);
2547         psc->transmitting = FALSE;
2548         psc->encoding_sa = FALSE;
2549         psc->enciphering_sa = FALSE;
2550
2551         dl_list_init(&psc->sa_list);
2552         wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2553         wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2554
2555         return psc;
2556 }
2557
2558
2559 /**
2560  * ieee802_1x_kay_deinit_transmit_sc -
2561  */
2562 static void
2563 ieee802_1x_kay_deinit_transmit_sc(
2564         struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2565 {
2566         struct transmit_sa *psa, *tmp;
2567
2568         wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2569                    psc->channel);
2570         dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2571                               list) {
2572                 secy_disable_transmit_sa(participant->kay, psa);
2573                 ieee802_1x_kay_deinit_transmit_sa(psa);
2574         }
2575
2576         os_free(psc);
2577 }
2578
2579
2580 /****************** Interface between CP and KAY *********************/
2581 /**
2582  * ieee802_1x_kay_set_latest_sa_attr -
2583  */
2584 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2585                                       struct ieee802_1x_mka_ki *lki, u8 lan,
2586                                       Boolean ltx, Boolean lrx)
2587 {
2588         struct ieee802_1x_mka_participant *principal;
2589
2590         principal = ieee802_1x_kay_get_principal_participant(kay);
2591         if (!principal)
2592                 return -1;
2593
2594         if (!lki)
2595                 os_memset(&principal->lki, 0, sizeof(principal->lki));
2596         else
2597                 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2598
2599         principal->lan = lan;
2600         principal->ltx = ltx;
2601         principal->lrx = lrx;
2602         if (!lki) {
2603                 kay->ltx_kn = 0;
2604                 kay->lrx_kn = 0;
2605         } else {
2606                 kay->ltx_kn = lki->kn;
2607                 kay->lrx_kn = lki->kn;
2608         }
2609         kay->ltx_an = lan;
2610         kay->lrx_an = lan;
2611
2612         return 0;
2613 }
2614
2615
2616 /**
2617  * ieee802_1x_kay_set_old_sa_attr -
2618  */
2619 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2620                                    struct ieee802_1x_mka_ki *oki,
2621                                    u8 oan, Boolean otx, Boolean orx)
2622 {
2623         struct ieee802_1x_mka_participant *principal;
2624
2625         principal = ieee802_1x_kay_get_principal_participant(kay);
2626         if (!principal)
2627                 return -1;
2628
2629         if (!oki)
2630                 os_memset(&principal->oki, 0, sizeof(principal->oki));
2631         else
2632                 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2633
2634         principal->oan = oan;
2635         principal->otx = otx;
2636         principal->orx = orx;
2637
2638         if (!oki) {
2639                 kay->otx_kn = 0;
2640                 kay->orx_kn = 0;
2641         } else {
2642                 kay->otx_kn = oki->kn;
2643                 kay->orx_kn = oki->kn;
2644         }
2645         kay->otx_an = oan;
2646         kay->orx_an = oan;
2647
2648         return 0;
2649 }
2650
2651
2652 /**
2653  * ieee802_1x_kay_create_sas -
2654  */
2655 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2656                               struct ieee802_1x_mka_ki *lki)
2657 {
2658         struct data_key *sa_key, *latest_sak;
2659         struct ieee802_1x_mka_participant *principal;
2660         struct receive_sc *rxsc;
2661         struct receive_sa *rxsa;
2662         struct transmit_sa *txsa;
2663
2664         principal = ieee802_1x_kay_get_principal_participant(kay);
2665         if (!principal)
2666                 return -1;
2667
2668         latest_sak = NULL;
2669         dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2670                 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2671                         sa_key->rx_latest = TRUE;
2672                         sa_key->tx_latest = TRUE;
2673                         latest_sak = sa_key;
2674                         principal->to_use_sak = TRUE;
2675                 } else {
2676                         sa_key->rx_latest = FALSE;
2677                         sa_key->tx_latest = FALSE;
2678                 }
2679         }
2680         if (!latest_sak) {
2681                 wpa_printf(MSG_ERROR, "lki related sak not found");
2682                 return -1;
2683         }
2684
2685         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2686                 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2687                                                       latest_sak);
2688                 if (!rxsa)
2689                         return -1;
2690
2691                 secy_create_receive_sa(kay, rxsa);
2692         }
2693
2694         txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2695                                                1, latest_sak);
2696         if (!txsa)
2697                 return -1;
2698
2699         secy_create_transmit_sa(kay, txsa);
2700
2701
2702
2703         return 0;
2704 }
2705
2706
2707 /**
2708  * ieee802_1x_kay_delete_sas -
2709  */
2710 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2711                               struct ieee802_1x_mka_ki *ki)
2712 {
2713         struct data_key *sa_key, *pre_key;
2714         struct transmit_sa *txsa, *pre_txsa;
2715         struct receive_sa *rxsa, *pre_rxsa;
2716         struct receive_sc *rxsc;
2717         struct ieee802_1x_mka_participant *principal;
2718
2719         wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2720         principal = ieee802_1x_kay_get_principal_participant(kay);
2721         if (!principal)
2722                 return -1;
2723
2724         /* remove the transmit sa */
2725         dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2726                               struct transmit_sa, list) {
2727                 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2728                         secy_disable_transmit_sa(kay, txsa);
2729                         ieee802_1x_kay_deinit_transmit_sa(txsa);
2730                 }
2731         }
2732
2733         /* remove the receive sa */
2734         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2735                 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2736                                       struct receive_sa, list) {
2737                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2738                                 secy_disable_receive_sa(kay, rxsa);
2739                                 ieee802_1x_kay_deinit_receive_sa(rxsa);
2740                         }
2741                 }
2742         }
2743
2744         /* remove the sak */
2745         dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2746                               struct data_key, list) {
2747                 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2748                         ieee802_1x_kay_deinit_data_key(sa_key);
2749                         break;
2750                 }
2751                 if (principal->new_key == sa_key)
2752                         principal->new_key = NULL;
2753         }
2754
2755         return 0;
2756 }
2757
2758
2759 /**
2760  * ieee802_1x_kay_enable_tx_sas -
2761  */
2762 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2763                                  struct ieee802_1x_mka_ki *lki)
2764 {
2765         struct ieee802_1x_mka_participant *principal;
2766         struct transmit_sa *txsa;
2767
2768         principal = ieee802_1x_kay_get_principal_participant(kay);
2769         if (!principal)
2770                 return -1;
2771
2772         dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2773                          list) {
2774                 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2775                         txsa->in_use = TRUE;
2776                         secy_enable_transmit_sa(kay, txsa);
2777                         ieee802_1x_cp_set_usingtransmitas(
2778                                 principal->kay->cp, TRUE);
2779                         ieee802_1x_cp_sm_step(principal->kay->cp);
2780                 }
2781         }
2782
2783         return 0;
2784 }
2785
2786
2787 /**
2788  * ieee802_1x_kay_enable_rx_sas -
2789  */
2790 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2791                                  struct ieee802_1x_mka_ki *lki)
2792 {
2793         struct ieee802_1x_mka_participant *principal;
2794         struct receive_sa *rxsa;
2795         struct receive_sc *rxsc;
2796
2797         principal = ieee802_1x_kay_get_principal_participant(kay);
2798         if (!principal)
2799                 return -1;
2800
2801         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2802                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2803                 {
2804                         if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2805                                 rxsa->in_use = TRUE;
2806                                 secy_enable_receive_sa(kay, rxsa);
2807                                 ieee802_1x_cp_set_usingreceivesas(
2808                                         principal->kay->cp, TRUE);
2809                                 ieee802_1x_cp_sm_step(principal->kay->cp);
2810                         }
2811                 }
2812         }
2813
2814         return 0;
2815 }
2816
2817
2818 /**
2819  * ieee802_1x_kay_enable_new_info -
2820  */
2821 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2822 {
2823         struct ieee802_1x_mka_participant *principal;
2824
2825         principal = ieee802_1x_kay_get_principal_participant(kay);
2826         if (!principal)
2827                 return -1;
2828
2829         if (principal->retry_count < MAX_RETRY_CNT) {
2830                 ieee802_1x_participant_send_mkpdu(principal);
2831                 principal->retry_count++;
2832         }
2833
2834         return 0;
2835 }
2836
2837
2838 /**
2839  * ieee802_1x_kay_cp_conf -
2840  */
2841 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2842                            struct ieee802_1x_cp_conf *pconf)
2843 {
2844         pconf->protect = kay->macsec_protect;
2845         pconf->replay_protect = kay->macsec_replay_protect;
2846         pconf->validate = kay->macsec_validate;
2847
2848         return 0;
2849 }
2850
2851
2852 /**
2853  * ieee802_1x_kay_alloc_cp_sm -
2854  */
2855 static struct ieee802_1x_cp_sm *
2856 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2857 {
2858         struct ieee802_1x_cp_conf conf;
2859
2860         os_memset(&conf, 0, sizeof(conf));
2861         conf.protect = kay->macsec_protect;
2862         conf.replay_protect = kay->macsec_replay_protect;
2863         conf.validate = kay->macsec_validate;
2864         conf.replay_window = kay->macsec_replay_window;
2865
2866         return ieee802_1x_cp_sm_init(kay, &conf);
2867 }
2868
2869
2870 /**
2871  * ieee802_1x_kay_mkpdu_sanity_check -
2872  *     sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2873  */
2874 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2875                                              const u8 *buf, size_t len)
2876 {
2877         struct ieee8023_hdr *eth_hdr;
2878         struct ieee802_1x_hdr *eapol_hdr;
2879         struct ieee802_1x_mka_hdr *mka_hdr;
2880         struct ieee802_1x_mka_basic_body *body;
2881         size_t mka_msg_len;
2882         struct ieee802_1x_mka_participant *participant;
2883         size_t body_len;
2884         u8 icv[MAX_ICV_LEN];
2885         u8 *msg_icv;
2886
2887         eth_hdr = (struct ieee8023_hdr *) buf;
2888         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2889         mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2890
2891         /* destination address should be not individual address */
2892         if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2893                 wpa_printf(MSG_MSGDUMP,
2894                            "KaY: ethernet destination address is not PAE group address");
2895                 return -1;
2896         }
2897
2898         /* MKPDU should not less than 32 octets */
2899         mka_msg_len = be_to_host16(eapol_hdr->length);
2900         if (mka_msg_len < 32) {
2901                 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2902                 return -1;
2903         }
2904         /* MKPDU should multiple 4 octets */
2905         if ((mka_msg_len % 4) != 0) {
2906                 wpa_printf(MSG_MSGDUMP,
2907                            "KaY: MKPDU is not multiple of 4 octets");
2908                 return -1;
2909         }
2910
2911         body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2912         ieee802_1x_mka_dump_basic_body(body);
2913         body_len = get_mka_param_body_len(body);
2914         /* EAPOL-MKA body should comprise basic parameter set and ICV */
2915         if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2916                 wpa_printf(MSG_ERROR,
2917                            "KaY: Received EAPOL-MKA Packet Body Length (%d bytes) is less than the Basic Parameter Set Header Length (%d bytes) + the Basic Parameter Set Body Length (%d bytes) + %d bytes of ICV",
2918                            (int) mka_msg_len, (int) MKA_HDR_LEN,
2919                            (int) body_len, DEFAULT_ICV_LEN);
2920                 return -1;
2921         }
2922
2923         /* CKN should be owned by I */
2924         participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2925         if (!participant) {
2926                 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2927                 return -1;
2928         }
2929
2930         /* algorithm agility check */
2931         if (os_memcmp(body->algo_agility, mka_algo_agility,
2932                       sizeof(body->algo_agility)) != 0) {
2933                 wpa_printf(MSG_ERROR,
2934                            "KaY: peer's algorithm agility not supported for me");
2935                 return -1;
2936         }
2937
2938         /* ICV check */
2939         /*
2940          * The ICV will comprise the final octets of the packet body, whatever
2941          * its size, not the fixed length 16 octets, indicated by the EAPOL
2942          * packet body length.
2943          */
2944         if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2945                     participant->ick.key,
2946                     buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2947                 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2948                 return -1;
2949         }
2950         msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2951                                                  mka_msg_len);
2952
2953         if (msg_icv) {
2954                 if (os_memcmp_const(msg_icv, icv,
2955                                     mka_alg_tbl[kay->mka_algindex].icv_len) !=
2956                     0) {
2957                         wpa_printf(MSG_ERROR,
2958                                    "KaY: Computed ICV is not equal to Received ICV");
2959                 return -1;
2960                 }
2961         } else {
2962                 wpa_printf(MSG_ERROR, "KaY: No ICV");
2963                 return -1;
2964         }
2965
2966         return 0;
2967 }
2968
2969
2970 /**
2971  * ieee802_1x_kay_decode_mkpdu -
2972  */
2973 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2974                                        const u8 *buf, size_t len)
2975 {
2976         struct ieee802_1x_mka_participant *participant;
2977         struct ieee802_1x_mka_hdr *hdr;
2978         size_t body_len;
2979         size_t left_len;
2980         u8 body_type;
2981         int i;
2982         const u8 *pos;
2983         Boolean my_included;
2984         Boolean handled[256];
2985
2986         if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2987                 return -1;
2988
2989         /* handle basic parameter set */
2990         pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2991         left_len = len - sizeof(struct ieee8023_hdr) -
2992                 sizeof(struct ieee802_1x_hdr);
2993         participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2994         if (!participant)
2995                 return -1;
2996
2997         /* to skip basic parameter set */
2998         hdr = (struct ieee802_1x_mka_hdr *) pos;
2999         body_len = get_mka_param_body_len(hdr);
3000         pos += body_len + MKA_HDR_LEN;
3001         left_len -= body_len + MKA_HDR_LEN;
3002
3003         /* check i am in the peer's peer list */
3004         my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3005         if (my_included) {
3006                 /* accept the peer as live peer */
3007                 if (!ieee802_1x_kay_is_in_peer(
3008                             participant,
3009                             participant->current_peer_id.mi)) {
3010                         if (!ieee802_1x_kay_create_live_peer(
3011                                     participant,
3012                                     participant->current_peer_id.mi,
3013                                     be_to_host32(
3014                                             participant->current_peer_id.mn)))
3015                                 return -1;
3016                         ieee802_1x_kay_elect_key_server(participant);
3017                         ieee802_1x_kay_decide_macsec_use(participant);
3018                 }
3019                 if (ieee802_1x_kay_is_in_potential_peer(
3020                             participant, participant->current_peer_id.mi)) {
3021                         if (!ieee802_1x_kay_move_live_peer(
3022                                     participant,
3023                                     participant->current_peer_id.mi,
3024                                     be_to_host32(participant->
3025                                                  current_peer_id.mn)))
3026                                 return -1;
3027                         ieee802_1x_kay_elect_key_server(participant);
3028                         ieee802_1x_kay_decide_macsec_use(participant);
3029                 }
3030         }
3031
3032         /*
3033          * Handle other parameter set than basic parameter set.
3034          * Each parameter set should be present only once.
3035          */
3036         for (i = 0; i < 256; i++)
3037                 handled[i] = FALSE;
3038
3039         handled[0] = TRUE;
3040         while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3041                 hdr = (struct ieee802_1x_mka_hdr *) pos;
3042                 body_len = get_mka_param_body_len(hdr);
3043                 body_type = get_mka_param_body_type(hdr);
3044
3045                 if (body_type == MKA_ICV_INDICATOR)
3046                         return 0;
3047
3048                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3049                         wpa_printf(MSG_ERROR,
3050                                    "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
3051                                    (int) left_len, (int) MKA_HDR_LEN,
3052                                    (int) body_len, DEFAULT_ICV_LEN);
3053                         goto next_para_set;
3054                 }
3055
3056                 if (handled[body_type])
3057                         goto next_para_set;
3058
3059                 handled[body_type] = TRUE;
3060                 if (body_type < ARRAY_SIZE(mak_body_handler) &&
3061                     mak_body_handler[body_type].body_rx) {
3062                         mak_body_handler[body_type].body_rx
3063                                 (participant, pos, left_len);
3064                 } else {
3065                         wpa_printf(MSG_ERROR,
3066                                    "The type %d not supported in this MKA version %d",
3067                                    body_type, MKA_VERSION_ID);
3068                 }
3069
3070 next_para_set:
3071                 pos += body_len + MKA_HDR_LEN;
3072                 left_len -= body_len + MKA_HDR_LEN;
3073         }
3074
3075         kay->active = TRUE;
3076         participant->retry_count = 0;
3077         participant->active = TRUE;
3078
3079         return 0;
3080 }
3081
3082
3083
3084 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3085                            size_t len)
3086 {
3087         struct ieee802_1x_kay *kay = ctx;
3088         struct ieee8023_hdr *eth_hdr;
3089         struct ieee802_1x_hdr *eapol_hdr;
3090
3091         /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3092         if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3093                 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3094                            (unsigned long) len);
3095                 return;
3096         }
3097
3098         eth_hdr = (struct ieee8023_hdr *) buf;
3099         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3100         if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3101             be_to_host16(eapol_hdr->length)) {
3102                 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3103                            (unsigned long) len,
3104                            (unsigned long) be_to_host16(eapol_hdr->length));
3105                 return;
3106         }
3107
3108         if (eapol_hdr->version < EAPOL_VERSION) {
3109                 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3110                            eapol_hdr->version);
3111                 return;
3112         }
3113         if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3114             eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3115                 return;
3116
3117         wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3118         if (dl_list_empty(&kay->participant_list)) {
3119                 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3120                 return;
3121         }
3122
3123         ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3124 }
3125
3126
3127 /**
3128  * ieee802_1x_kay_init -
3129  */
3130 struct ieee802_1x_kay *
3131 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3132                     const char *ifname, const u8 *addr)
3133 {
3134         struct ieee802_1x_kay *kay;
3135
3136         kay = os_zalloc(sizeof(*kay));
3137         if (!kay) {
3138                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3139                 return NULL;
3140         }
3141
3142         kay->ctx = ctx;
3143
3144         kay->enable = TRUE;
3145         kay->active = FALSE;
3146
3147         kay->authenticated = FALSE;
3148         kay->secured = FALSE;
3149         kay->failed = FALSE;
3150         kay->policy = policy;
3151
3152         os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3153         os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3154         kay->actor_sci.port = host_to_be16(0x0001);
3155         kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3156
3157         /* While actor acts as a key server, shall distribute sakey */
3158         kay->dist_kn = 1;
3159         kay->dist_an = 0;
3160         kay->dist_time = 0;
3161
3162         kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3163         kay->macsec_csindex = DEFAULT_CS_INDEX;
3164         kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3165         kay->mka_version = MKA_VERSION_ID;
3166
3167         os_memcpy(kay->algo_agility, mka_algo_agility,
3168                   sizeof(kay->algo_agility));
3169
3170         dl_list_init(&kay->participant_list);
3171
3172         if (policy == DO_NOT_SECURE) {
3173                 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3174                 kay->macsec_desired = FALSE;
3175                 kay->macsec_protect = FALSE;
3176                 kay->macsec_validate = Disabled;
3177                 kay->macsec_replay_protect = FALSE;
3178                 kay->macsec_replay_window = 0;
3179                 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3180         } else {
3181                 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3182                 kay->macsec_desired = TRUE;
3183                 kay->macsec_protect = TRUE;
3184                 kay->macsec_validate = Strict;
3185                 kay->macsec_replay_protect = FALSE;
3186                 kay->macsec_replay_window = 0;
3187                 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3188         }
3189
3190         wpa_printf(MSG_DEBUG, "KaY: state machine created");
3191
3192         /* Initialize the SecY must be prio to CP, as CP will control SecY */
3193         secy_init_macsec(kay);
3194         secy_get_available_transmit_sc(kay, &kay->sc_ch);
3195
3196         wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3197
3198         /* init CP */
3199         kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3200         if (kay->cp == NULL) {
3201                 ieee802_1x_kay_deinit(kay);
3202                 return NULL;
3203         }
3204
3205         if (policy == DO_NOT_SECURE) {
3206                 ieee802_1x_cp_connect_authenticated(kay->cp);
3207                 ieee802_1x_cp_sm_step(kay->cp);
3208         } else {
3209                 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3210                                              kay_l2_receive, kay, 1);
3211                 if (kay->l2_mka == NULL) {
3212                         wpa_printf(MSG_WARNING,
3213                                    "KaY: Failed to initialize L2 packet processing for MKA packet");
3214                         ieee802_1x_kay_deinit(kay);
3215                         return NULL;
3216                 }
3217         }
3218
3219         return kay;
3220 }
3221
3222
3223 /**
3224  * ieee802_1x_kay_deinit -
3225  */
3226 void
3227 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3228 {
3229         struct ieee802_1x_mka_participant *participant;
3230
3231         if (!kay)
3232                 return;
3233
3234         wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3235
3236         while (!dl_list_empty(&kay->participant_list)) {
3237                 participant = dl_list_entry(kay->participant_list.next,
3238                                             struct ieee802_1x_mka_participant,
3239                                             list);
3240                 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3241         }
3242
3243         ieee802_1x_cp_sm_deinit(kay->cp);
3244         secy_deinit_macsec(kay);
3245
3246         if (kay->l2_mka) {
3247                 l2_packet_deinit(kay->l2_mka);
3248                 kay->l2_mka = NULL;
3249         }
3250
3251         os_free(kay->ctx);
3252         os_free(kay);
3253 }
3254
3255
3256 /**
3257  * ieee802_1x_kay_create_mka -
3258  */
3259 struct ieee802_1x_mka_participant *
3260 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3261                           struct mka_key *cak, u32 life,
3262                           enum mka_created_mode mode, Boolean is_authenticator)
3263 {
3264         struct ieee802_1x_mka_participant *participant;
3265         unsigned int usecs;
3266
3267         if (!kay || !ckn || !cak) {
3268                 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3269                 return NULL;
3270         }
3271
3272         if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3273                 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3274                 return NULL;
3275         }
3276         if (ckn->len > MAX_CKN_LEN) {
3277                 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3278                 return NULL;
3279         }
3280         if (!kay->enable) {
3281                 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3282                 return NULL;
3283         }
3284
3285         participant = os_zalloc(sizeof(*participant));
3286         if (!participant) {
3287                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3288                 return NULL;
3289         }
3290
3291         participant->ckn.len = ckn->len;
3292         os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3293         participant->cak.len = cak->len;
3294         os_memcpy(participant->cak.key, cak->key, cak->len);
3295         if (life)
3296                 participant->cak_life = life + time(NULL);
3297
3298         switch (mode) {
3299         case EAP_EXCHANGE:
3300                 if (is_authenticator) {
3301                         participant->is_obliged_key_server = TRUE;
3302                         participant->can_be_key_server = TRUE;
3303                         participant->is_key_server = TRUE;
3304                         participant->principal = TRUE;
3305
3306                         os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3307                                   sizeof(kay->key_server_sci));
3308                         kay->key_server_priority = kay->actor_priority;
3309                         participant->is_elected = TRUE;
3310                 } else {
3311                         participant->is_obliged_key_server = FALSE;
3312                         participant->can_be_key_server = FALSE;
3313                         participant->is_key_server = FALSE;
3314                         participant->is_elected = TRUE;
3315                 }
3316                 break;
3317
3318         default:
3319                 participant->is_obliged_key_server = FALSE;
3320                 participant->can_be_key_server = TRUE;
3321                 participant->is_key_server = TRUE;
3322                 participant->is_elected = FALSE;
3323                 break;
3324         }
3325
3326         participant->cached = FALSE;
3327
3328         participant->active = FALSE;
3329         participant->participant = FALSE;
3330         participant->retain = FALSE;
3331         participant->activate = DEFAULT;
3332
3333         if (participant->is_key_server)
3334                 participant->principal = TRUE;
3335
3336         dl_list_init(&participant->live_peers);
3337         dl_list_init(&participant->potential_peers);
3338
3339         participant->retry_count = 0;
3340         participant->kay = kay;
3341
3342         if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3343                 goto fail;
3344         participant->mn = 0;
3345
3346         participant->lrx = FALSE;
3347         participant->ltx = FALSE;
3348         participant->orx = FALSE;
3349         participant->otx = FALSE;
3350         participant->to_dist_sak = FALSE;
3351         participant->to_use_sak = FALSE;
3352         participant->new_sak = FALSE;
3353         dl_list_init(&participant->sak_list);
3354         participant->new_key = NULL;
3355         dl_list_init(&participant->rxsc_list);
3356         participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3357                                                             kay->sc_ch);
3358         secy_cp_control_protect_frames(kay, kay->macsec_protect);
3359         secy_cp_control_replay(kay, kay->macsec_replay_protect,
3360                                kay->macsec_replay_window);
3361         secy_create_transmit_sc(kay, participant->txsc);
3362
3363         /* to derive KEK from CAK and CKN */
3364         participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3365         if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3366                                                     participant->ckn.name,
3367                                                     participant->ckn.len,
3368                                                     participant->kek.key)) {
3369                 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3370                 goto fail;
3371         }
3372         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3373                         participant->kek.key, participant->kek.len);
3374
3375         /* to derive ICK from CAK and CKN */
3376         participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3377         if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3378                                                     participant->ckn.name,
3379                                                     participant->ckn.len,
3380                                                     participant->ick.key)) {
3381                 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3382                 goto fail;
3383         }
3384         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3385                         participant->ick.key, participant->ick.len);
3386
3387         dl_list_add(&kay->participant_list, &participant->list);
3388         wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3389                     ckn->name, ckn->len);
3390
3391         usecs = os_random() % (MKA_HELLO_TIME * 1000);
3392         eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3393                                participant, NULL);
3394         participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3395                 usecs / 1000000;
3396
3397         return participant;
3398
3399 fail:
3400         os_free(participant);
3401         return NULL;
3402 }
3403
3404
3405 /**
3406  * ieee802_1x_kay_delete_mka -
3407  */
3408 void
3409 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3410 {
3411         struct ieee802_1x_mka_participant *participant;
3412         struct ieee802_1x_kay_peer *peer;
3413         struct data_key *sak;
3414         struct receive_sc *rxsc;
3415
3416         if (!kay || !ckn)
3417                 return;
3418
3419         wpa_printf(MSG_DEBUG, "KaY: participant removed");
3420
3421         /* get the participant */
3422         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3423         if (!participant) {
3424                 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3425                             ckn->name, ckn->len);
3426                 return;
3427         }
3428
3429         dl_list_del(&participant->list);
3430
3431         /* remove live peer */
3432         while (!dl_list_empty(&participant->live_peers)) {
3433                 peer = dl_list_entry(participant->live_peers.next,
3434                                      struct ieee802_1x_kay_peer, list);
3435                 dl_list_del(&peer->list);
3436                 os_free(peer);
3437         }
3438
3439         /* remove potential peer */
3440         while (!dl_list_empty(&participant->potential_peers)) {
3441                 peer = dl_list_entry(participant->potential_peers.next,
3442                                      struct ieee802_1x_kay_peer, list);
3443                 dl_list_del(&peer->list);
3444                 os_free(peer);
3445         }
3446
3447         /* remove sak */
3448         while (!dl_list_empty(&participant->sak_list)) {
3449                 sak = dl_list_entry(participant->sak_list.next,
3450                                     struct data_key, list);
3451                 dl_list_del(&sak->list);
3452                 os_free(sak->key);
3453                 os_free(sak);
3454         }
3455         while (!dl_list_empty(&participant->rxsc_list)) {
3456                 rxsc = dl_list_entry(participant->rxsc_list.next,
3457                                      struct receive_sc, list);
3458                 secy_delete_receive_sc(kay, rxsc);
3459                 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3460         }
3461         secy_delete_transmit_sc(kay, participant->txsc);
3462         ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3463
3464         os_memset(&participant->cak, 0, sizeof(participant->cak));
3465         os_memset(&participant->kek, 0, sizeof(participant->kek));
3466         os_memset(&participant->ick, 0, sizeof(participant->ick));
3467         os_free(participant);
3468 }
3469
3470
3471 /**
3472  * ieee802_1x_kay_mka_participate -
3473  */
3474 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3475                                     struct mka_key_name *ckn,
3476                                     Boolean status)
3477 {
3478         struct ieee802_1x_mka_participant *participant;
3479
3480         if (!kay || !ckn)
3481                 return;
3482
3483         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3484         if (!participant)
3485                 return;
3486
3487         participant->active = status;
3488 }
3489
3490
3491 /**
3492  * ieee802_1x_kay_new_sak -
3493  */
3494 int
3495 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3496 {
3497         struct ieee802_1x_mka_participant *participant;
3498
3499         if (!kay)
3500                 return -1;
3501
3502         participant = ieee802_1x_kay_get_principal_participant(kay);
3503         if (!participant)
3504                 return -1;
3505
3506         participant->new_sak = TRUE;
3507         wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3508
3509         return 0;
3510 }
3511
3512
3513 /**
3514  * ieee802_1x_kay_change_cipher_suite -
3515  */
3516 int
3517 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3518 {
3519         struct ieee802_1x_mka_participant *participant;
3520
3521         if (!kay)
3522                 return -1;
3523
3524         if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3525                 wpa_printf(MSG_ERROR,
3526                            "KaY: Configured cipher suite index is out of range");
3527                 return -1;
3528         }
3529         if (kay->macsec_csindex == cs_index)
3530                 return -2;
3531
3532         if (cs_index == 0)
3533                 kay->macsec_desired = FALSE;
3534
3535         kay->macsec_csindex = cs_index;
3536         kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3537
3538         participant = ieee802_1x_kay_get_principal_participant(kay);
3539         if (participant) {
3540                 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3541                 participant->new_sak = TRUE;
3542         }
3543
3544         return 0;
3545 }