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