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