mka: Remove unused body_peer incrementation
[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         Boolean included = FALSE;
952         struct ieee802_1x_mka_hdr *hdr;
953         size_t body_len;
954         size_t left_len;
955         u8 body_type;
956         u32 peer_mn;
957         be32 _peer_mn;
958         const u8 *peer_mi;
959         const u8 *pos;
960         size_t i;
961
962         pos = mka_msg;
963         left_len = msg_len;
964         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
965                 hdr = (struct ieee802_1x_mka_hdr *) pos;
966                 body_len = get_mka_param_body_len(hdr);
967                 body_type = get_mka_param_body_type(hdr);
968
969                 if (body_type != MKA_LIVE_PEER_LIST &&
970                     body_type != MKA_POTENTIAL_PEER_LIST)
971                         goto SKIP_PEER;
972
973                 ieee802_1x_mka_dump_peer_body(
974                         (struct ieee802_1x_mka_peer_body *)pos);
975
976                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
977                         wpa_printf(MSG_ERROR,
978                                    "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",
979                                    left_len, MKA_HDR_LEN,
980                                    body_len, DEFAULT_ICV_LEN);
981                         goto SKIP_PEER;
982                 }
983
984                 if ((body_len % 16) != 0) {
985                         wpa_printf(MSG_ERROR,
986                                    "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
987                                    body_len);
988                         goto SKIP_PEER;
989                 }
990
991                 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
992                         peer_mi = MKA_HDR_LEN + pos + i;
993                         os_memcpy(&_peer_mn, peer_mi + MI_LEN,
994                                   sizeof(_peer_mn));
995                         peer_mn = be_to_host32(_peer_mn);
996                         if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
997                             peer_mn == participant->mn) {
998                                 included = TRUE;
999                                 break;
1000                         }
1001                 }
1002
1003                 if (included)
1004                         return TRUE;
1005
1006 SKIP_PEER:
1007                 left_len -= body_len + MKA_HDR_LEN;
1008                 pos += body_len + MKA_HDR_LEN;
1009         }
1010
1011         return FALSE;
1012 }
1013
1014
1015 /**
1016  * ieee802_1x_mka_decode_live_peer_body -
1017  */
1018 static int ieee802_1x_mka_decode_live_peer_body(
1019         struct ieee802_1x_mka_participant *participant,
1020         const u8 *peer_msg, size_t msg_len)
1021 {
1022         const struct ieee802_1x_mka_hdr *hdr;
1023         struct ieee802_1x_kay_peer *peer;
1024         size_t body_len;
1025         u32 peer_mn;
1026         be32 _peer_mn;
1027         const u8 *peer_mi;
1028         size_t i;
1029         Boolean is_included;
1030
1031         is_included = ieee802_1x_kay_is_in_live_peer(
1032                 participant, participant->current_peer_id.mi);
1033
1034         hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1035         body_len = get_mka_param_body_len(hdr);
1036         if (body_len % 16 != 0) {
1037                 wpa_printf(MSG_ERROR,
1038                            "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1039                            body_len);
1040                 return -1;
1041         }
1042
1043         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1044                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1045                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1046                 peer_mn = be_to_host32(_peer_mn);
1047
1048                 /* it is myself */
1049                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1050                         /* My message id is used by other participant */
1051                         if (peer_mn > participant->mn &&
1052                             !reset_participant_mi(participant))
1053                                 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1054                         continue;
1055                 }
1056                 if (!is_included)
1057                         continue;
1058
1059                 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1060                 if (NULL != peer) {
1061                         peer->mn = peer_mn;
1062                         peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1063                 } else {
1064                         if (!ieee802_1x_kay_create_potential_peer(
1065                                 participant, peer_mi, peer_mn)) {
1066                                 return -1;
1067                         }
1068                 }
1069         }
1070
1071         return 0;
1072 }
1073
1074
1075 /**
1076  * ieee802_1x_mka_decode_potential_peer_body -
1077  */
1078 static int
1079 ieee802_1x_mka_decode_potential_peer_body(
1080         struct ieee802_1x_mka_participant *participant,
1081         const u8 *peer_msg, size_t msg_len)
1082 {
1083         struct ieee802_1x_mka_hdr *hdr;
1084         size_t body_len;
1085         u32 peer_mn;
1086         be32 _peer_mn;
1087         const u8 *peer_mi;
1088         size_t i;
1089
1090         hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1091         body_len = get_mka_param_body_len(hdr);
1092         if (body_len % 16 != 0) {
1093                 wpa_printf(MSG_ERROR,
1094                            "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1095                            body_len);
1096                 return -1;
1097         }
1098
1099         for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1100                 peer_mi = MKA_HDR_LEN + peer_msg + i;
1101                 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1102                 peer_mn = be_to_host32(_peer_mn);
1103
1104                 /* it is myself */
1105                 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1106                         /* My message id is used by other participant */
1107                         if (peer_mn > participant->mn &&
1108                             !reset_participant_mi(participant))
1109                                 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1110                         continue;
1111                 }
1112         }
1113
1114         return 0;
1115 }
1116
1117
1118 /**
1119  * ieee802_1x_mka_sak_use_body_present
1120  */
1121 static Boolean
1122 ieee802_1x_mka_sak_use_body_present(
1123         struct ieee802_1x_mka_participant *participant)
1124 {
1125         if (participant->to_use_sak)
1126                 return TRUE;
1127         else
1128                 return FALSE;
1129 }
1130
1131
1132 /**
1133  * ieee802_1x_mka_get_sak_use_length
1134  */
1135 static int
1136 ieee802_1x_mka_get_sak_use_length(
1137         struct ieee802_1x_mka_participant *participant)
1138 {
1139         int length = MKA_HDR_LEN;
1140
1141         if (participant->kay->macsec_desired && participant->advised_desired)
1142                 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1143
1144         return MKA_ALIGN_LENGTH(length);
1145 }
1146
1147
1148 /**
1149  *
1150  */
1151 static u32
1152 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1153                        struct ieee802_1x_mka_ki *ki)
1154 {
1155         struct receive_sa *rxsa;
1156         struct receive_sc *rxsc;
1157         u32 lpn = 0;
1158
1159         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1160                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1161                 {
1162                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1163                                 secy_get_receive_lowest_pn(principal->kay,
1164                                                            rxsa);
1165
1166                                 lpn = lpn > rxsa->lowest_pn ?
1167                                         lpn : rxsa->lowest_pn;
1168                                 break;
1169                         }
1170                 }
1171         }
1172
1173         if (lpn == 0)
1174                 lpn = 1;
1175
1176         return lpn;
1177 }
1178
1179
1180 /**
1181  * ieee802_1x_mka_encode_sak_use_body -
1182  */
1183 static int
1184 ieee802_1x_mka_encode_sak_use_body(
1185         struct ieee802_1x_mka_participant *participant,
1186         struct wpabuf *buf)
1187 {
1188         struct ieee802_1x_mka_sak_use_body *body;
1189         unsigned int length;
1190         u32 pn = 1;
1191
1192         length = ieee802_1x_mka_get_sak_use_length(participant);
1193         body = wpabuf_put(buf, length);
1194
1195         body->type = MKA_SAK_USE;
1196         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1197
1198         if (length == MKA_HDR_LEN) {
1199                 body->ptx = TRUE;
1200                 body->prx = TRUE;
1201                 body->lan = 0;
1202                 body->lrx = FALSE;
1203                 body->ltx = FALSE;
1204                 body->delay_protect = FALSE;
1205                 return 0;
1206         }
1207
1208         /* data protect, lowest accept packet number */
1209         body->delay_protect = participant->kay->macsec_replay_protect;
1210         pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1211         if (pn > participant->kay->pn_exhaustion) {
1212                 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1213                 if (participant->is_key_server)
1214                         participant->new_sak = TRUE;
1215         }
1216
1217         body->llpn = host_to_be32(pn);
1218         pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1219         body->olpn = host_to_be32(pn);
1220
1221         /* plain tx, plain rx */
1222         if (participant->kay->macsec_protect)
1223                 body->ptx = FALSE;
1224         else
1225                 body->ptx = TRUE;
1226
1227         if (participant->kay->macsec_validate == Strict)
1228                 body->prx = FALSE;
1229         else
1230                 body->prx = TRUE;
1231
1232         /* latest key: rx, tx, key server member identifier key number */
1233         body->lan = participant->lan;
1234         os_memcpy(body->lsrv_mi, participant->lki.mi,
1235                   sizeof(body->lsrv_mi));
1236         body->lkn = host_to_be32(participant->lki.kn);
1237         body->lrx = participant->lrx;
1238         body->ltx = participant->ltx;
1239
1240         /* old key: rx, tx, key server member identifier key number */
1241         body->oan = participant->oan;
1242         if (participant->oki.kn != participant->lki.kn &&
1243             participant->oki.kn != 0) {
1244                 body->otx = TRUE;
1245                 body->orx = TRUE;
1246                 os_memcpy(body->osrv_mi, participant->oki.mi,
1247                           sizeof(body->osrv_mi));
1248                 body->okn = host_to_be32(participant->oki.kn);
1249         } else {
1250                 body->otx = FALSE;
1251                 body->orx = FALSE;
1252         }
1253
1254         /* set CP's variable */
1255         if (body->ltx) {
1256                 if (!participant->kay->tx_enable)
1257                         participant->kay->tx_enable = TRUE;
1258
1259                 if (!participant->kay->port_enable)
1260                         participant->kay->port_enable = TRUE;
1261         }
1262         if (body->lrx) {
1263                 if (!participant->kay->rx_enable)
1264                         participant->kay->rx_enable = TRUE;
1265         }
1266
1267         ieee802_1x_mka_dump_sak_use_body(body);
1268         return 0;
1269 }
1270
1271
1272 /**
1273  * ieee802_1x_mka_decode_sak_use_body -
1274  */
1275 static int
1276 ieee802_1x_mka_decode_sak_use_body(
1277         struct ieee802_1x_mka_participant *participant,
1278         const u8 *mka_msg, size_t msg_len)
1279 {
1280         struct ieee802_1x_mka_hdr *hdr;
1281         struct ieee802_1x_mka_sak_use_body *body;
1282         struct ieee802_1x_kay_peer *peer;
1283         struct transmit_sa *txsa;
1284         struct data_key *sa_key = NULL;
1285         size_t body_len;
1286         struct ieee802_1x_mka_ki ki;
1287         u32 lpn;
1288         Boolean all_receiving;
1289         Boolean founded;
1290
1291         if (!participant->principal) {
1292                 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1293                 return -1;
1294         }
1295         peer = ieee802_1x_kay_get_live_peer(participant,
1296                                             participant->current_peer_id.mi);
1297         if (!peer) {
1298                 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1299                 return -1;
1300         }
1301
1302         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1303         body_len = get_mka_param_body_len(hdr);
1304         body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1305         ieee802_1x_mka_dump_sak_use_body(body);
1306
1307         if ((body_len != 0) && (body_len < 40)) {
1308                 wpa_printf(MSG_ERROR,
1309                            "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
1310                            body_len);
1311                 return -1;
1312         }
1313
1314         /* TODO: what action should I take when peer does not support MACsec */
1315         if (body_len == 0) {
1316                 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1317                 return 0;
1318         }
1319
1320         /* TODO: when the plain tx or rx of peer is true, should I change
1321          * the attribute of controlled port
1322          */
1323         if (body->prx)
1324                 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1325
1326         if (body->ptx)
1327                 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1328
1329         /* check latest key is valid */
1330         if (body->ltx || body->lrx) {
1331                 founded = FALSE;
1332                 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1333                 ki.kn = be_to_host32(body->lkn);
1334                 dl_list_for_each(sa_key, &participant->sak_list,
1335                                  struct data_key, list) {
1336                         if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1337                                 founded = TRUE;
1338                                 break;
1339                         }
1340                 }
1341                 if (!founded) {
1342                         wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1343                         return -1;
1344                 }
1345                 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1346                               sizeof(participant->lki.mi)) == 0 &&
1347                     be_to_host32(body->lkn) == participant->lki.kn &&
1348                     body->lan == participant->lan) {
1349                         peer->sak_used = TRUE;
1350                 }
1351                 if (body->ltx && peer->is_key_server) {
1352                         ieee802_1x_cp_set_servertransmitting(
1353                                 participant->kay->cp, TRUE);
1354                         ieee802_1x_cp_sm_step(participant->kay->cp);
1355                 }
1356         }
1357
1358         /* check old key is valid */
1359         if (body->otx || body->orx) {
1360                 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1361                               sizeof(participant->oki.mi)) != 0 ||
1362                     be_to_host32(body->okn) != participant->oki.kn ||
1363                     body->oan != participant->oan) {
1364                         wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1365                         return -1;
1366                 }
1367         }
1368
1369         /* TODO: how to set the MACsec hardware when delay_protect is true */
1370         if (body->delay_protect &&
1371             (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1372                 wpa_printf(MSG_WARNING,
1373                            "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1374                 return -1;
1375         }
1376
1377         /* check all live peer have used the sak for receiving sa */
1378         all_receiving = TRUE;
1379         dl_list_for_each(peer, &participant->live_peers,
1380                          struct ieee802_1x_kay_peer, list) {
1381                 if (!peer->sak_used) {
1382                         all_receiving = FALSE;
1383                         break;
1384                 }
1385         }
1386         if (all_receiving) {
1387                 participant->to_dist_sak = FALSE;
1388                 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1389                 ieee802_1x_cp_sm_step(participant->kay->cp);
1390         }
1391
1392         /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1393         lpn = be_to_host32(body->llpn);
1394         if (lpn > participant->kay->pn_exhaustion) {
1395                 if (participant->is_key_server) {
1396                         participant->new_sak = TRUE;
1397                         wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1398                 }
1399         }
1400
1401         founded = FALSE;
1402         dl_list_for_each(txsa, &participant->txsc->sa_list,
1403                          struct transmit_sa, list) {
1404                 if (sa_key != NULL && txsa->pkey == sa_key) {
1405                         founded = TRUE;
1406                         break;
1407                 }
1408         }
1409         if (!founded) {
1410                 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1411                 return -1;
1412         }
1413
1414         /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1415          * npn is larger than txsa's npn, set it to txsa.
1416          */
1417         secy_get_transmit_next_pn(participant->kay, txsa);
1418         if (lpn > txsa->next_pn) {
1419                 secy_set_transmit_next_pn(participant->kay, txsa);
1420                 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1421         }
1422
1423         return 0;
1424 }
1425
1426
1427 /**
1428  * ieee802_1x_mka_dist_sak_body_present
1429  */
1430 static Boolean
1431 ieee802_1x_mka_dist_sak_body_present(
1432         struct ieee802_1x_mka_participant *participant)
1433 {
1434         if (!participant->to_dist_sak || !participant->new_key)
1435                 return FALSE;
1436
1437         return TRUE;
1438 }
1439
1440
1441 /**
1442  * ieee802_1x_kay_get_dist_sak_length
1443  */
1444 static int
1445 ieee802_1x_mka_get_dist_sak_length(
1446         struct ieee802_1x_mka_participant *participant)
1447 {
1448         int length = MKA_HDR_LEN;
1449         int cs_index = participant->kay->macsec_csindex;
1450
1451         if (participant->advised_desired) {
1452                 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1453                 if (cs_index != DEFAULT_CS_INDEX)
1454                         length += CS_ID_LEN;
1455
1456                 length += cipher_suite_tbl[cs_index].sak_len + 8;
1457         }
1458
1459         return MKA_ALIGN_LENGTH(length);
1460 }
1461
1462
1463 /**
1464  * ieee802_1x_mka_encode_dist_sak_body -
1465  */
1466 static int
1467 ieee802_1x_mka_encode_dist_sak_body(
1468         struct ieee802_1x_mka_participant *participant,
1469         struct wpabuf *buf)
1470 {
1471         struct ieee802_1x_mka_dist_sak_body *body;
1472         struct data_key *sak;
1473         unsigned int length;
1474         int cs_index;
1475         int sak_pos;
1476
1477         length = ieee802_1x_mka_get_dist_sak_length(participant);
1478         body = wpabuf_put(buf, length);
1479         body->type = MKA_DISTRIBUTED_SAK;
1480         set_mka_param_body_len(body, length - MKA_HDR_LEN);
1481         if (length == MKA_HDR_LEN) {
1482                 body->confid_offset = 0;
1483                 body->dan = 0;
1484                 return 0;
1485         }
1486
1487         sak = participant->new_key;
1488         body->confid_offset = sak->confidentiality_offset;
1489         body->dan = sak->an;
1490         body->kn = host_to_be32(sak->key_identifier.kn);
1491         cs_index = participant->kay->macsec_csindex;
1492         sak_pos = 0;
1493         if (cs_index != DEFAULT_CS_INDEX) {
1494                 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1495                 sak_pos = CS_ID_LEN;
1496         }
1497         if (aes_wrap(participant->kek.key, 16,
1498                      cipher_suite_tbl[cs_index].sak_len / 8,
1499                      sak->key, body->sak + sak_pos)) {
1500                 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1501                 return -1;
1502         }
1503
1504         ieee802_1x_mka_dump_dist_sak_body(body);
1505
1506         return 0;
1507 }
1508
1509
1510 /**
1511  * ieee802_1x_kay_init_data_key -
1512  */
1513 static struct data_key *
1514 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1515 {
1516         struct data_key *pkey;
1517
1518         if (!conf)
1519                 return NULL;
1520
1521         pkey = os_zalloc(sizeof(*pkey));
1522         if (pkey == NULL) {
1523                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1524                 return NULL;
1525         }
1526
1527         pkey->key = os_zalloc(conf->key_len);
1528         if (pkey->key == NULL) {
1529                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1530                 os_free(pkey);
1531                 return NULL;
1532         }
1533
1534         os_memcpy(pkey->key, conf->key, conf->key_len);
1535         os_memcpy(&pkey->key_identifier, &conf->ki,
1536                   sizeof(pkey->key_identifier));
1537         pkey->confidentiality_offset = conf->offset;
1538         pkey->an = conf->an;
1539         pkey->transmits = conf->tx;
1540         pkey->receives = conf->rx;
1541         os_get_time(&pkey->created_time);
1542
1543         pkey->user = 1;
1544
1545         return pkey;
1546 }
1547
1548
1549 /**
1550  * ieee802_1x_kay_decode_dist_sak_body -
1551  */
1552 static int
1553 ieee802_1x_mka_decode_dist_sak_body(
1554         struct ieee802_1x_mka_participant *participant,
1555         const u8 *mka_msg, size_t msg_len)
1556 {
1557         struct ieee802_1x_mka_hdr *hdr;
1558         struct ieee802_1x_mka_dist_sak_body *body;
1559         struct ieee802_1x_kay_peer *peer;
1560         struct macsec_ciphersuite *cs;
1561         size_t body_len;
1562         struct key_conf *conf;
1563         struct data_key *sa_key = NULL;
1564         struct ieee802_1x_mka_ki sak_ki;
1565         int sak_len;
1566         u8 *wrap_sak;
1567         u8 *unwrap_sak;
1568
1569         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1570         body_len = get_mka_param_body_len(hdr);
1571         if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1572                 wpa_printf(MSG_ERROR,
1573                            "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1574                            body_len);
1575                 return -1;
1576         }
1577
1578         if (!participant->principal) {
1579                 wpa_printf(MSG_ERROR,
1580                            "KaY: I can't accept the distributed SAK as I am not principal");
1581                 return -1;
1582         }
1583         if (participant->is_key_server) {
1584                 wpa_printf(MSG_ERROR,
1585                            "KaY: I can't accept the distributed SAK as myself is key server ");
1586                 return -1;
1587         }
1588         if (!participant->kay->macsec_desired ||
1589             participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1590                 wpa_printf(MSG_ERROR,
1591                            "KaY: I am not MACsec-desired or without MACsec capable");
1592                 return -1;
1593         }
1594
1595         peer = ieee802_1x_kay_get_live_peer(participant,
1596                                             participant->current_peer_id.mi);
1597         if (!peer) {
1598                 wpa_printf(MSG_ERROR,
1599                            "KaY: The key server is not in my live peers list");
1600                 return -1;
1601         }
1602         if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
1603                 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1604                 return -1;
1605         }
1606         if (body_len == 0) {
1607                 participant->kay->authenticated = TRUE;
1608                 participant->kay->secured = FALSE;
1609                 participant->kay->failed = FALSE;
1610                 participant->advised_desired = FALSE;
1611                 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1612                 ieee802_1x_cp_sm_step(participant->kay->cp);
1613                 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1614                 participant->to_use_sak = TRUE;
1615                 return 0;
1616         }
1617         participant->advised_desired = TRUE;
1618         participant->kay->authenticated = FALSE;
1619         participant->kay->secured = TRUE;
1620         participant->kay->failed = FALSE;
1621         ieee802_1x_cp_connect_secure(participant->kay->cp);
1622         ieee802_1x_cp_sm_step(participant->kay->cp);
1623
1624         body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1625         ieee802_1x_mka_dump_dist_sak_body(body);
1626         dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1627         {
1628                 if (os_memcmp(sa_key->key_identifier.mi,
1629                               participant->current_peer_id.mi, MI_LEN) == 0 &&
1630                     sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1631                         wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1632                         return 0;
1633                 }
1634         }
1635         if (body_len == 28) {
1636                 sak_len = DEFAULT_SA_KEY_LEN;
1637                 wrap_sak =  body->sak;
1638                 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1639         } else {
1640                 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1641                 if (!cs) {
1642                         wpa_printf(MSG_ERROR,
1643                                    "KaY: I can't support the Cipher Suite advised by key server");
1644                         return -1;
1645                 }
1646                 sak_len = cs->sak_len;
1647                 wrap_sak = body->sak + CS_ID_LEN;
1648                 participant->kay->macsec_csindex = cs->index;
1649         }
1650
1651         unwrap_sak = os_zalloc(sak_len);
1652         if (!unwrap_sak) {
1653                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1654                 return -1;
1655         }
1656         if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1657                        unwrap_sak)) {
1658                 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1659                 os_free(unwrap_sak);
1660                 return -1;
1661         }
1662         wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1663
1664         conf = os_zalloc(sizeof(*conf));
1665         if (!conf) {
1666                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1667                 os_free(unwrap_sak);
1668                 return -1;
1669         }
1670         conf->key_len = sak_len;
1671
1672         conf->key = os_zalloc(conf->key_len);
1673         if (!conf->key) {
1674                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1675                 os_free(unwrap_sak);
1676                 os_free(conf);
1677                 return -1;
1678         }
1679
1680         os_memcpy(conf->key, unwrap_sak, conf->key_len);
1681
1682         os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1683                   sizeof(sak_ki.mi));
1684         sak_ki.kn = be_to_host32(body->kn);
1685
1686         os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1687         conf->ki.kn = sak_ki.kn;
1688         conf->an = body->dan;
1689         conf->offset = body->confid_offset;
1690         conf->rx = TRUE;
1691         conf->tx = TRUE;
1692
1693         sa_key = ieee802_1x_kay_init_data_key(conf);
1694         if (!sa_key) {
1695                 os_free(unwrap_sak);
1696                 os_free(conf->key);
1697                 os_free(conf);
1698                 return -1;
1699         }
1700
1701         dl_list_add(&participant->sak_list, &sa_key->list);
1702
1703         ieee802_1x_cp_set_ciphersuite(
1704                 participant->kay->cp,
1705                 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1706         ieee802_1x_cp_sm_step(participant->kay->cp);
1707         ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1708         ieee802_1x_cp_sm_step(participant->kay->cp);
1709         ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1710         ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1711         ieee802_1x_cp_signal_newsak(participant->kay->cp);
1712         ieee802_1x_cp_sm_step(participant->kay->cp);
1713
1714         participant->to_use_sak = TRUE;
1715
1716         os_free(unwrap_sak);
1717         os_free(conf->key);
1718         os_free(conf);
1719
1720         return 0;
1721 }
1722
1723
1724 /**
1725  * ieee802_1x_mka_icv_body_present
1726  */
1727 static Boolean
1728 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1729 {
1730         return TRUE;
1731 }
1732
1733
1734 /**
1735  * ieee802_1x_kay_get_icv_length
1736  */
1737 static int
1738 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1739 {
1740         int length;
1741
1742         length = sizeof(struct ieee802_1x_mka_icv_body);
1743         length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1744
1745         return MKA_ALIGN_LENGTH(length);
1746 }
1747
1748
1749 /**
1750  * ieee802_1x_mka_encode_icv_body -
1751  */
1752 static int
1753 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1754                                struct wpabuf *buf)
1755 {
1756         struct ieee802_1x_mka_icv_body *body;
1757         unsigned int length;
1758         u8 cmac[MAX_ICV_LEN];
1759
1760         length = ieee802_1x_mka_get_icv_length(participant);
1761         if (length != DEFAULT_ICV_LEN)  {
1762                 body = wpabuf_put(buf, MKA_HDR_LEN);
1763                 body->type = MKA_ICV_INDICATOR;
1764                 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1765         }
1766
1767         if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1768                     participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1769                 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1770                 return -1;
1771         }
1772
1773         if (length != DEFAULT_ICV_LEN)  {
1774                 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1775                           length - MKA_HDR_LEN);
1776         } else {
1777                 os_memcpy(wpabuf_put(buf, length), cmac, length);
1778         }
1779
1780         return 0;
1781 }
1782
1783 /**
1784  * ieee802_1x_mka_decode_icv_body -
1785  */
1786 static u8 *
1787 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1788                                const u8 *mka_msg, size_t msg_len)
1789 {
1790         struct ieee802_1x_mka_hdr *hdr;
1791         struct ieee802_1x_mka_icv_body *body;
1792         size_t body_len;
1793         size_t left_len;
1794         u8 body_type;
1795         const u8 *pos;
1796
1797         pos = mka_msg;
1798         left_len = msg_len;
1799         while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1800                 hdr = (struct ieee802_1x_mka_hdr *) pos;
1801                 body_len = get_mka_param_body_len(hdr);
1802                 body_type = get_mka_param_body_type(hdr);
1803
1804                 if (left_len < (body_len + MKA_HDR_LEN))
1805                         break;
1806
1807                 if (body_type != MKA_ICV_INDICATOR) {
1808                         left_len -= MKA_HDR_LEN + body_len;
1809                         pos += MKA_HDR_LEN + body_len;
1810                         continue;
1811                 }
1812
1813                 body = (struct ieee802_1x_mka_icv_body *)pos;
1814                 if (body_len
1815                         < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1816                         return NULL;
1817                 }
1818
1819                 return body->icv;
1820         }
1821
1822         return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1823 }
1824
1825
1826 /**
1827  * ieee802_1x_mka_decode_dist_cak_body-
1828  */
1829 static int
1830 ieee802_1x_mka_decode_dist_cak_body(
1831         struct ieee802_1x_mka_participant *participant,
1832         const u8 *mka_msg, size_t msg_len)
1833 {
1834         struct ieee802_1x_mka_hdr *hdr;
1835         size_t body_len;
1836
1837         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1838         body_len = get_mka_param_body_len(hdr);
1839         if (body_len < 28) {
1840                 wpa_printf(MSG_ERROR,
1841                            "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
1842                            body_len);
1843                 return -1;
1844         }
1845
1846         return 0;
1847 }
1848
1849
1850 /**
1851  * ieee802_1x_mka_decode_kmd_body -
1852  */
1853 static int
1854 ieee802_1x_mka_decode_kmd_body(
1855         struct ieee802_1x_mka_participant *participant,
1856         const u8 *mka_msg, size_t msg_len)
1857 {
1858         struct ieee802_1x_mka_hdr *hdr;
1859         size_t body_len;
1860
1861         hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1862         body_len = get_mka_param_body_len(hdr);
1863         if (body_len < 5) {
1864                 wpa_printf(MSG_ERROR,
1865                            "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
1866                            body_len);
1867                 return -1;
1868         }
1869
1870         return 0;
1871 }
1872
1873
1874 /**
1875  * ieee802_1x_mka_decode_announce_body -
1876  */
1877 static int ieee802_1x_mka_decode_announce_body(
1878         struct ieee802_1x_mka_participant *participant,
1879         const u8 *mka_msg, size_t msg_len)
1880 {
1881         return 0;
1882 }
1883
1884
1885 static struct mka_param_body_handler mka_body_handler[] = {
1886         /* basic parameter set */
1887         {
1888                 ieee802_1x_mka_encode_basic_body,
1889                 NULL,
1890                 ieee802_1x_mka_basic_body_length,
1891                 ieee802_1x_mka_basic_body_present
1892         },
1893
1894         /* live peer list parameter set */
1895         {
1896                 ieee802_1x_mka_encode_live_peer_body,
1897                 ieee802_1x_mka_decode_live_peer_body,
1898                 ieee802_1x_mka_get_live_peer_length,
1899                 ieee802_1x_mka_live_peer_body_present
1900         },
1901
1902         /* potential peer list parameter set */
1903         {
1904                 ieee802_1x_mka_encode_potential_peer_body,
1905                 ieee802_1x_mka_decode_potential_peer_body,
1906                 ieee802_1x_mka_get_potential_peer_length,
1907                 ieee802_1x_mka_potential_peer_body_present
1908         },
1909
1910         /* sak use parameter set */
1911         {
1912                 ieee802_1x_mka_encode_sak_use_body,
1913                 ieee802_1x_mka_decode_sak_use_body,
1914                 ieee802_1x_mka_get_sak_use_length,
1915                 ieee802_1x_mka_sak_use_body_present
1916         },
1917
1918         /* distribute sak parameter set */
1919         {
1920                 ieee802_1x_mka_encode_dist_sak_body,
1921                 ieee802_1x_mka_decode_dist_sak_body,
1922                 ieee802_1x_mka_get_dist_sak_length,
1923                 ieee802_1x_mka_dist_sak_body_present
1924         },
1925
1926         /* distribute cak parameter set */
1927         {
1928                 NULL,
1929                 ieee802_1x_mka_decode_dist_cak_body,
1930                 NULL,
1931                 NULL
1932         },
1933
1934         /* kmd parameter set */
1935         {
1936                 NULL,
1937                 ieee802_1x_mka_decode_kmd_body,
1938                 NULL,
1939                 NULL
1940         },
1941
1942         /* announce parameter set */
1943         {
1944                 NULL,
1945                 ieee802_1x_mka_decode_announce_body,
1946                 NULL,
1947                 NULL
1948         },
1949
1950         /* icv parameter set */
1951         {
1952                 ieee802_1x_mka_encode_icv_body,
1953                 NULL,
1954                 ieee802_1x_mka_get_icv_length,
1955                 ieee802_1x_mka_icv_body_present
1956         },
1957 };
1958
1959
1960 /**
1961  * ieee802_1x_kay_deinit_data_key -
1962  */
1963 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1964 {
1965         if (!pkey)
1966                 return;
1967
1968         pkey->user--;
1969         if (pkey->user > 1)
1970                 return;
1971
1972         dl_list_del(&pkey->list);
1973         os_free(pkey->key);
1974         os_free(pkey);
1975 }
1976
1977
1978 /**
1979  * ieee802_1x_kay_generate_new_sak -
1980  */
1981 static int
1982 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1983 {
1984         struct data_key *sa_key = NULL;
1985         struct key_conf *conf;
1986         struct ieee802_1x_kay_peer *peer;
1987         struct ieee802_1x_kay *kay = participant->kay;
1988         int ctx_len, ctx_offset;
1989         u8 *context;
1990
1991         /* check condition for generating a fresh SAK:
1992          * must have one live peer
1993          * and MKA life time elapse since last distribution
1994          * or potential peer is empty
1995          */
1996         if (dl_list_empty(&participant->live_peers)) {
1997                 wpa_printf(MSG_ERROR,
1998                            "KaY: Live peers list must not empty when generating fresh SAK");
1999                 return -1;
2000         }
2001
2002         /* FIXME: A fresh SAK not generated until
2003          * the live peer list contains at least one peer and
2004          * MKA life time has elapsed since the prior SAK was first distributed,
2005          * or the Key server's potential peer is empty
2006          * but I can't understand the second item, so
2007          * here only check first item and ingore
2008          *   && (!dl_list_empty(&participant->potential_peers))) {
2009          */
2010         if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
2011                 wpa_printf(MSG_ERROR,
2012                            "KaY: Life time have not elapsed since prior SAK distributed");
2013                 return -1;
2014         }
2015
2016         conf = os_zalloc(sizeof(*conf));
2017         if (!conf) {
2018                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2019                 return -1;
2020         }
2021         conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2022
2023         conf->key = os_zalloc(conf->key_len);
2024         if (!conf->key) {
2025                 os_free(conf);
2026                 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2027                 return -1;
2028         }
2029
2030         ctx_len = conf->key_len + sizeof(kay->dist_kn);
2031         dl_list_for_each(peer, &participant->live_peers,
2032                          struct ieee802_1x_kay_peer, list)
2033                 ctx_len += sizeof(peer->mi);
2034         ctx_len += sizeof(participant->mi);
2035
2036         context = os_zalloc(ctx_len);
2037         if (!context) {
2038                 os_free(conf->key);
2039                 os_free(conf);
2040                 return -1;
2041         }
2042         ctx_offset = 0;
2043         if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2044                 os_free(context);
2045                 os_free(conf->key);
2046                 os_free(conf);
2047                 return -1;
2048         }
2049         ctx_offset += conf->key_len;
2050         dl_list_for_each(peer, &participant->live_peers,
2051                          struct ieee802_1x_kay_peer, list) {
2052                 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2053                 ctx_offset += sizeof(peer->mi);
2054         }
2055         os_memcpy(context + ctx_offset, participant->mi,
2056                   sizeof(participant->mi));
2057         ctx_offset += sizeof(participant->mi);
2058         os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2059
2060         if (conf->key_len == 16) {
2061                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2062                                                 context, ctx_len, conf->key);
2063         } else if (conf->key_len == 32) {
2064                 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2065                                                 context, ctx_len, conf->key);
2066         } else {
2067                 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2068                 os_free(conf->key);
2069                 os_free(conf);
2070                 os_free(context);
2071                 return -1;
2072         }
2073         wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2074                     conf->key, conf->key_len);
2075
2076         os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2077         conf->ki.kn = participant->kay->dist_kn;
2078         conf->an = participant->kay->dist_an;
2079         conf->offset = kay->macsec_confidentiality;
2080         conf->rx = TRUE;
2081         conf->tx = TRUE;
2082
2083         sa_key = ieee802_1x_kay_init_data_key(conf);
2084         if (!sa_key) {
2085                 os_free(conf->key);
2086                 os_free(conf);
2087                 os_free(context);
2088                 return -1;
2089         }
2090         participant->new_key = sa_key;
2091
2092         dl_list_add(&participant->sak_list, &sa_key->list);
2093         ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2094                                       cipher_suite_tbl[kay->macsec_csindex].id);
2095         ieee802_1x_cp_sm_step(kay->cp);
2096         ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2097         ieee802_1x_cp_sm_step(kay->cp);
2098         ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2099         ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2100         ieee802_1x_cp_signal_newsak(kay->cp);
2101         ieee802_1x_cp_sm_step(kay->cp);
2102
2103         dl_list_for_each(peer, &participant->live_peers,
2104                          struct ieee802_1x_kay_peer, list)
2105                 peer->sak_used = FALSE;
2106
2107         participant->kay->dist_kn++;
2108         participant->kay->dist_an++;
2109         if (participant->kay->dist_an > 3)
2110                 participant->kay->dist_an = 0;
2111
2112         participant->kay->dist_time = time(NULL);
2113
2114         os_free(conf->key);
2115         os_free(conf);
2116         os_free(context);
2117         return 0;
2118 }
2119
2120
2121 /**
2122  * ieee802_1x_kay_elect_key_server - elect the key server
2123  * when to elect: whenever the live peers list changes
2124  */
2125 static int
2126 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2127 {
2128         struct ieee802_1x_kay_peer *peer;
2129         struct ieee802_1x_kay_peer *key_server = NULL;
2130         struct ieee802_1x_kay *kay = participant->kay;
2131         Boolean i_is_key_server;
2132
2133         if (participant->is_obliged_key_server) {
2134                 participant->new_sak = TRUE;
2135                 participant->to_dist_sak = FALSE;
2136                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2137                 return 0;
2138         }
2139
2140         /* elect the key server among the peers */
2141         dl_list_for_each(peer, &participant->live_peers,
2142                          struct ieee802_1x_kay_peer, list) {
2143                 if (!peer->is_key_server)
2144                         continue;
2145
2146                 if (!key_server) {
2147                         key_server = peer;
2148                         continue;
2149                 }
2150
2151                 if (peer->key_server_priority <
2152                     key_server->key_server_priority) {
2153                         key_server = peer;
2154                 } else if (peer->key_server_priority ==
2155                            key_server->key_server_priority) {
2156                         if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2157                                       ETH_ALEN) < 0)
2158                                 key_server = peer;
2159                 }
2160         }
2161
2162         /* elect the key server between me and the above elected peer */
2163         i_is_key_server = FALSE;
2164         if (key_server && participant->can_be_key_server) {
2165                 if (kay->actor_priority
2166                            < key_server->key_server_priority) {
2167                         i_is_key_server = TRUE;
2168                 } else if (kay->actor_priority
2169                                         == key_server->key_server_priority) {
2170                         if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2171                                       ETH_ALEN) < 0)
2172                                 i_is_key_server = TRUE;
2173                 }
2174         } else if (participant->can_be_key_server) {
2175                 i_is_key_server = TRUE;
2176         }
2177
2178         if (i_is_key_server) {
2179                 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2180                 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2181                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2182                         ieee802_1x_cp_sm_step(kay->cp);
2183                 }
2184
2185                 participant->is_key_server = TRUE;
2186                 participant->principal = TRUE;
2187                 participant->new_sak = TRUE;
2188                 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2189                 participant->to_dist_sak = FALSE;
2190                 participant->is_elected = TRUE;
2191
2192                 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2193                           sizeof(kay->key_server_sci));
2194                 kay->key_server_priority = kay->actor_priority;
2195         } else if (key_server) {
2196                 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2197                 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2198                         ieee802_1x_cp_signal_chgdserver(kay->cp);
2199                         ieee802_1x_cp_sm_step(kay->cp);
2200                 }
2201
2202                 participant->is_key_server = FALSE;
2203                 participant->principal = TRUE;
2204                 participant->is_elected = TRUE;
2205
2206                 os_memcpy(&kay->key_server_sci, &key_server->sci,
2207                           sizeof(kay->key_server_sci));
2208                 kay->key_server_priority = key_server->key_server_priority;
2209         } else {
2210                 participant->principal = FALSE;
2211                 participant->is_key_server = FALSE;
2212                 participant->is_elected = FALSE;
2213         }
2214
2215         return 0;
2216 }
2217
2218
2219 /**
2220  * ieee802_1x_kay_decide_macsec_use - the key server determinate
2221  *               how to use MACsec: whether use MACsec and its capability
2222  * protectFrames will be advised if the key server and one of its live peers are
2223  * MACsec capable and one of those request MACsec protection
2224  */
2225 static int
2226 ieee802_1x_kay_decide_macsec_use(
2227         struct ieee802_1x_mka_participant *participant)
2228 {
2229         struct ieee802_1x_kay *kay = participant->kay;
2230         struct ieee802_1x_kay_peer *peer;
2231         enum macsec_cap less_capability;
2232         Boolean has_peer;
2233
2234         if (!participant->is_key_server)
2235                 return -1;
2236
2237         /* key server self is MACsec-desired and requesting MACsec */
2238         if (!kay->macsec_desired) {
2239                 participant->advised_desired = FALSE;
2240                 return -1;
2241         }
2242         if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2243                 participant->advised_desired = FALSE;
2244                 return -1;
2245         }
2246         less_capability = kay->macsec_capable;
2247
2248         /* at least one of peers is MACsec-desired and requesting MACsec */
2249         has_peer = FALSE;
2250         dl_list_for_each(peer, &participant->live_peers,
2251                          struct ieee802_1x_kay_peer, list) {
2252                 if (!peer->macsec_desired)
2253                         continue;
2254
2255                 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2256                         continue;
2257
2258                 less_capability = (less_capability < peer->macsec_capability) ?
2259                         less_capability : peer->macsec_capability;
2260                 has_peer = TRUE;
2261         }
2262
2263         if (has_peer) {
2264                 participant->advised_desired = TRUE;
2265                 participant->advised_capability = less_capability;
2266                 kay->authenticated = FALSE;
2267                 kay->secured = TRUE;
2268                 kay->failed = FALSE;
2269                 ieee802_1x_cp_connect_secure(kay->cp);
2270                 ieee802_1x_cp_sm_step(kay->cp);
2271         } else {
2272                 participant->advised_desired = FALSE;
2273                 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2274                 participant->to_use_sak = FALSE;
2275                 kay->authenticated = TRUE;
2276                 kay->secured = FALSE;
2277                 kay->failed = FALSE;
2278                 kay->ltx_kn = 0;
2279                 kay->ltx_an = 0;
2280                 kay->lrx_kn = 0;
2281                 kay->lrx_an = 0;
2282                 kay->otx_kn = 0;
2283                 kay->otx_an = 0;
2284                 kay->orx_kn = 0;
2285                 kay->orx_an = 0;
2286                 ieee802_1x_cp_connect_authenticated(kay->cp);
2287                 ieee802_1x_cp_sm_step(kay->cp);
2288         }
2289
2290         return 0;
2291 }
2292
2293 static const u8 pae_group_addr[ETH_ALEN] = {
2294         0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2295 };
2296
2297
2298 /**
2299  * ieee802_1x_kay_encode_mkpdu -
2300  */
2301 static int
2302 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2303                             struct wpabuf *pbuf)
2304 {
2305         unsigned int i;
2306         struct ieee8023_hdr *ether_hdr;
2307         struct ieee802_1x_hdr *eapol_hdr;
2308
2309         ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2310         os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2311         os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2312                   sizeof(ether_hdr->dest));
2313         ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2314
2315         eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2316         eapol_hdr->version = EAPOL_VERSION;
2317         eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2318         eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2319
2320         for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2321                 if (mka_body_handler[i].body_present &&
2322                     mka_body_handler[i].body_present(participant)) {
2323                         if (mka_body_handler[i].body_tx(participant, pbuf))
2324                                 return -1;
2325                 }
2326         }
2327
2328         return 0;
2329 }
2330
2331 /**
2332  * ieee802_1x_participant_send_mkpdu -
2333  */
2334 static int
2335 ieee802_1x_participant_send_mkpdu(
2336         struct ieee802_1x_mka_participant *participant)
2337 {
2338         struct wpabuf *buf;
2339         struct ieee802_1x_kay *kay = participant->kay;
2340         size_t length = 0;
2341         unsigned int i;
2342
2343         wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2344         length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2345         for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2346                 if (mka_body_handler[i].body_present &&
2347                     mka_body_handler[i].body_present(participant))
2348                         length += mka_body_handler[i].body_length(participant);
2349         }
2350
2351         buf = wpabuf_alloc(length);
2352         if (!buf) {
2353                 wpa_printf(MSG_ERROR, "KaY: out of memory");
2354                 return -1;
2355         }
2356
2357         if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2358                 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2359                 return -1;
2360         }
2361
2362         l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2363         wpabuf_free(buf);
2364
2365         kay->active = TRUE;
2366         participant->active = TRUE;
2367
2368         return 0;
2369 }
2370
2371
2372 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2373 /**
2374  * ieee802_1x_participant_timer -
2375  */
2376 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2377 {
2378         struct ieee802_1x_mka_participant *participant;
2379         struct ieee802_1x_kay *kay;
2380         struct ieee802_1x_kay_peer *peer, *pre_peer;
2381         time_t now = time(NULL);
2382         Boolean lp_changed;
2383         struct receive_sc *rxsc, *pre_rxsc;
2384         struct transmit_sa *txsa, *pre_txsa;
2385
2386         participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2387         kay = participant->kay;
2388         if (participant->cak_life) {
2389                 if (now > participant->cak_life) {
2390                         kay->authenticated = FALSE;
2391                         kay->secured = FALSE;
2392                         kay->failed = TRUE;
2393                         ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2394                         return;
2395                 }
2396         }
2397
2398         /* should delete MKA instance if there are not live peers
2399          * when the MKA life elapsed since its creating */
2400         if (participant->mka_life) {
2401                 if (dl_list_empty(&participant->live_peers)) {
2402                         if (now > participant->mka_life) {
2403                                 kay->authenticated = FALSE;
2404                                 kay->secured = FALSE;
2405                                 kay->failed = TRUE;
2406                                 ieee802_1x_kay_delete_mka(kay,
2407                                                           &participant->ckn);
2408                                 return;
2409                         }
2410                 } else {
2411                         participant->mka_life = 0;
2412                 }
2413         }
2414
2415         lp_changed = FALSE;
2416         dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2417                               struct ieee802_1x_kay_peer, list) {
2418                 if (now > peer->expire) {
2419                         wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2420                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2421                                     sizeof(peer->mi));
2422                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2423                         dl_list_for_each_safe(rxsc, pre_rxsc,
2424                                               &participant->rxsc_list,
2425                                               struct receive_sc, list) {
2426                                 if (sci_equal(&rxsc->sci, &peer->sci)) {
2427                                         secy_delete_receive_sc(kay, rxsc);
2428                                         ieee802_1x_kay_deinit_receive_sc(
2429                                                 participant, rxsc);
2430                                 }
2431                         }
2432                         dl_list_del(&peer->list);
2433                         os_free(peer);
2434                         lp_changed = TRUE;
2435                 }
2436         }
2437
2438         if (lp_changed) {
2439                 if (dl_list_empty(&participant->live_peers)) {
2440                         participant->advised_desired = FALSE;
2441                         participant->advised_capability =
2442                                 MACSEC_CAP_NOT_IMPLEMENTED;
2443                         participant->to_use_sak = FALSE;
2444                         kay->authenticated = TRUE;
2445                         kay->secured = FALSE;
2446                         kay->failed = FALSE;
2447                         kay->ltx_kn = 0;
2448                         kay->ltx_an = 0;
2449                         kay->lrx_kn = 0;
2450                         kay->lrx_an = 0;
2451                         kay->otx_kn = 0;
2452                         kay->otx_an = 0;
2453                         kay->orx_kn = 0;
2454                         kay->orx_an = 0;
2455                         dl_list_for_each_safe(txsa, pre_txsa,
2456                                               &participant->txsc->sa_list,
2457                                               struct transmit_sa, list) {
2458                                 secy_disable_transmit_sa(kay, txsa);
2459                                 ieee802_1x_kay_deinit_transmit_sa(txsa);
2460                         }
2461
2462                         ieee802_1x_cp_connect_authenticated(kay->cp);
2463                         ieee802_1x_cp_sm_step(kay->cp);
2464                 } else {
2465                         ieee802_1x_kay_elect_key_server(participant);
2466                         ieee802_1x_kay_decide_macsec_use(participant);
2467                 }
2468         }
2469
2470         dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2471                               struct ieee802_1x_kay_peer, list) {
2472                 if (now > peer->expire) {
2473                         wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2474                         wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2475                                     sizeof(peer->mi));
2476                         wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2477                         dl_list_del(&peer->list);
2478                         os_free(peer);
2479                 }
2480         }
2481
2482         if (participant->new_sak) {
2483                 if (!ieee802_1x_kay_generate_new_sak(participant))
2484                         participant->to_dist_sak = TRUE;
2485
2486                 participant->new_sak = FALSE;
2487         }
2488
2489         if (participant->retry_count < MAX_RETRY_CNT) {
2490                 ieee802_1x_participant_send_mkpdu(participant);
2491                 participant->retry_count++;
2492         }
2493
2494         eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2495                                ieee802_1x_participant_timer,
2496                                participant, NULL);
2497 }
2498
2499
2500 /**
2501  * ieee802_1x_kay_init_transmit_sa -
2502  */
2503 static struct transmit_sa *
2504 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2505                                 struct data_key *key)
2506 {
2507         struct transmit_sa *psa;
2508
2509         key->tx_latest = TRUE;
2510         key->rx_latest = TRUE;
2511
2512         psa = os_zalloc(sizeof(*psa));
2513         if (!psa) {
2514                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2515                 return NULL;
2516         }
2517
2518         if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2519             key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2520                 psa->confidentiality = TRUE;
2521         else
2522                 psa->confidentiality = FALSE;
2523
2524         psa->an = an;
2525         psa->pkey = key;
2526         psa->next_pn = next_PN;
2527         psa->sc = psc;
2528
2529         os_get_time(&psa->created_time);
2530         psa->in_use = FALSE;
2531
2532         dl_list_add(&psc->sa_list, &psa->list);
2533         wpa_printf(MSG_DEBUG,
2534                    "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC(channel: %d)",
2535                    an, next_PN, psc->channel);
2536
2537         return psa;
2538 }
2539
2540
2541 /**
2542  * ieee802_1x_kay_deinit_transmit_sa -
2543  */
2544 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2545 {
2546         psa->pkey = NULL;
2547         wpa_printf(MSG_DEBUG,
2548                    "KaY: Delete transmit SA(an: %hhu) of SC",
2549                    psa->an);
2550         dl_list_del(&psa->list);
2551         os_free(psa);
2552 }
2553
2554
2555 /**
2556  * init_transmit_sc -
2557  */
2558 static struct transmit_sc *
2559 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2560                                 int channel)
2561 {
2562         struct transmit_sc *psc;
2563
2564         psc = os_zalloc(sizeof(*psc));
2565         if (!psc) {
2566                 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2567                 return NULL;
2568         }
2569         os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2570         psc->channel = channel;
2571
2572         os_get_time(&psc->created_time);
2573         psc->transmitting = FALSE;
2574         psc->encoding_sa = FALSE;
2575         psc->enciphering_sa = FALSE;
2576
2577         dl_list_init(&psc->sa_list);
2578         wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2579         wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2580
2581         return psc;
2582 }
2583
2584
2585 /**
2586  * ieee802_1x_kay_deinit_transmit_sc -
2587  */
2588 static void
2589 ieee802_1x_kay_deinit_transmit_sc(
2590         struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2591 {
2592         struct transmit_sa *psa, *tmp;
2593
2594         wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2595                    psc->channel);
2596         dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2597                               list) {
2598                 secy_disable_transmit_sa(participant->kay, psa);
2599                 ieee802_1x_kay_deinit_transmit_sa(psa);
2600         }
2601
2602         os_free(psc);
2603 }
2604
2605
2606 /****************** Interface between CP and KAY *********************/
2607 /**
2608  * ieee802_1x_kay_set_latest_sa_attr -
2609  */
2610 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2611                                       struct ieee802_1x_mka_ki *lki, u8 lan,
2612                                       Boolean ltx, Boolean lrx)
2613 {
2614         struct ieee802_1x_mka_participant *principal;
2615
2616         principal = ieee802_1x_kay_get_principal_participant(kay);
2617         if (!principal)
2618                 return -1;
2619
2620         if (!lki)
2621                 os_memset(&principal->lki, 0, sizeof(principal->lki));
2622         else
2623                 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2624
2625         principal->lan = lan;
2626         principal->ltx = ltx;
2627         principal->lrx = lrx;
2628         if (!lki) {
2629                 kay->ltx_kn = 0;
2630                 kay->lrx_kn = 0;
2631         } else {
2632                 kay->ltx_kn = lki->kn;
2633                 kay->lrx_kn = lki->kn;
2634         }
2635         kay->ltx_an = lan;
2636         kay->lrx_an = lan;
2637
2638         return 0;
2639 }
2640
2641
2642 /**
2643  * ieee802_1x_kay_set_old_sa_attr -
2644  */
2645 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2646                                    struct ieee802_1x_mka_ki *oki,
2647                                    u8 oan, Boolean otx, Boolean orx)
2648 {
2649         struct ieee802_1x_mka_participant *principal;
2650
2651         principal = ieee802_1x_kay_get_principal_participant(kay);
2652         if (!principal)
2653                 return -1;
2654
2655         if (!oki)
2656                 os_memset(&principal->oki, 0, sizeof(principal->oki));
2657         else
2658                 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2659
2660         principal->oan = oan;
2661         principal->otx = otx;
2662         principal->orx = orx;
2663
2664         if (!oki) {
2665                 kay->otx_kn = 0;
2666                 kay->orx_kn = 0;
2667         } else {
2668                 kay->otx_kn = oki->kn;
2669                 kay->orx_kn = oki->kn;
2670         }
2671         kay->otx_an = oan;
2672         kay->orx_an = oan;
2673
2674         return 0;
2675 }
2676
2677
2678 /**
2679  * ieee802_1x_kay_create_sas -
2680  */
2681 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2682                               struct ieee802_1x_mka_ki *lki)
2683 {
2684         struct data_key *sa_key, *latest_sak;
2685         struct ieee802_1x_mka_participant *principal;
2686         struct receive_sc *rxsc;
2687         struct receive_sa *rxsa;
2688         struct transmit_sa *txsa;
2689
2690         principal = ieee802_1x_kay_get_principal_participant(kay);
2691         if (!principal)
2692                 return -1;
2693
2694         latest_sak = NULL;
2695         dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2696                 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2697                         sa_key->rx_latest = TRUE;
2698                         sa_key->tx_latest = TRUE;
2699                         latest_sak = sa_key;
2700                         principal->to_use_sak = TRUE;
2701                 } else {
2702                         sa_key->rx_latest = FALSE;
2703                         sa_key->tx_latest = FALSE;
2704                 }
2705         }
2706         if (!latest_sak) {
2707                 wpa_printf(MSG_ERROR, "lki related sak not found");
2708                 return -1;
2709         }
2710
2711         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2712                 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2713                                                       latest_sak);
2714                 if (!rxsa)
2715                         return -1;
2716
2717                 secy_create_receive_sa(kay, rxsa);
2718         }
2719
2720         txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2721                                                1, latest_sak);
2722         if (!txsa)
2723                 return -1;
2724
2725         secy_create_transmit_sa(kay, txsa);
2726
2727
2728
2729         return 0;
2730 }
2731
2732
2733 /**
2734  * ieee802_1x_kay_delete_sas -
2735  */
2736 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2737                               struct ieee802_1x_mka_ki *ki)
2738 {
2739         struct data_key *sa_key, *pre_key;
2740         struct transmit_sa *txsa, *pre_txsa;
2741         struct receive_sa *rxsa, *pre_rxsa;
2742         struct receive_sc *rxsc;
2743         struct ieee802_1x_mka_participant *principal;
2744
2745         wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2746         principal = ieee802_1x_kay_get_principal_participant(kay);
2747         if (!principal)
2748                 return -1;
2749
2750         /* remove the transmit sa */
2751         dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2752                               struct transmit_sa, list) {
2753                 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2754                         secy_disable_transmit_sa(kay, txsa);
2755                         ieee802_1x_kay_deinit_transmit_sa(txsa);
2756                 }
2757         }
2758
2759         /* remove the receive sa */
2760         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2761                 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2762                                       struct receive_sa, list) {
2763                         if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2764                                 secy_disable_receive_sa(kay, rxsa);
2765                                 ieee802_1x_kay_deinit_receive_sa(rxsa);
2766                         }
2767                 }
2768         }
2769
2770         /* remove the sak */
2771         dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2772                               struct data_key, list) {
2773                 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2774                         ieee802_1x_kay_deinit_data_key(sa_key);
2775                         break;
2776                 }
2777                 if (principal->new_key == sa_key)
2778                         principal->new_key = NULL;
2779         }
2780
2781         return 0;
2782 }
2783
2784
2785 /**
2786  * ieee802_1x_kay_enable_tx_sas -
2787  */
2788 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2789                                  struct ieee802_1x_mka_ki *lki)
2790 {
2791         struct ieee802_1x_mka_participant *principal;
2792         struct transmit_sa *txsa;
2793
2794         principal = ieee802_1x_kay_get_principal_participant(kay);
2795         if (!principal)
2796                 return -1;
2797
2798         dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2799                          list) {
2800                 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2801                         txsa->in_use = TRUE;
2802                         secy_enable_transmit_sa(kay, txsa);
2803                         ieee802_1x_cp_set_usingtransmitas(
2804                                 principal->kay->cp, TRUE);
2805                         ieee802_1x_cp_sm_step(principal->kay->cp);
2806                 }
2807         }
2808
2809         return 0;
2810 }
2811
2812
2813 /**
2814  * ieee802_1x_kay_enable_rx_sas -
2815  */
2816 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2817                                  struct ieee802_1x_mka_ki *lki)
2818 {
2819         struct ieee802_1x_mka_participant *principal;
2820         struct receive_sa *rxsa;
2821         struct receive_sc *rxsc;
2822
2823         principal = ieee802_1x_kay_get_principal_participant(kay);
2824         if (!principal)
2825                 return -1;
2826
2827         dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2828                 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2829                 {
2830                         if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2831                                 rxsa->in_use = TRUE;
2832                                 secy_enable_receive_sa(kay, rxsa);
2833                                 ieee802_1x_cp_set_usingreceivesas(
2834                                         principal->kay->cp, TRUE);
2835                                 ieee802_1x_cp_sm_step(principal->kay->cp);
2836                         }
2837                 }
2838         }
2839
2840         return 0;
2841 }
2842
2843
2844 /**
2845  * ieee802_1x_kay_enable_new_info -
2846  */
2847 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2848 {
2849         struct ieee802_1x_mka_participant *principal;
2850
2851         principal = ieee802_1x_kay_get_principal_participant(kay);
2852         if (!principal)
2853                 return -1;
2854
2855         if (principal->retry_count < MAX_RETRY_CNT) {
2856                 ieee802_1x_participant_send_mkpdu(principal);
2857                 principal->retry_count++;
2858         }
2859
2860         return 0;
2861 }
2862
2863
2864 /**
2865  * ieee802_1x_kay_cp_conf -
2866  */
2867 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2868                            struct ieee802_1x_cp_conf *pconf)
2869 {
2870         pconf->protect = kay->macsec_protect;
2871         pconf->replay_protect = kay->macsec_replay_protect;
2872         pconf->validate = kay->macsec_validate;
2873
2874         return 0;
2875 }
2876
2877
2878 /**
2879  * ieee802_1x_kay_alloc_cp_sm -
2880  */
2881 static struct ieee802_1x_cp_sm *
2882 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2883 {
2884         struct ieee802_1x_cp_conf conf;
2885
2886         os_memset(&conf, 0, sizeof(conf));
2887         conf.protect = kay->macsec_protect;
2888         conf.replay_protect = kay->macsec_replay_protect;
2889         conf.validate = kay->macsec_validate;
2890         conf.replay_window = kay->macsec_replay_window;
2891
2892         return ieee802_1x_cp_sm_init(kay, &conf);
2893 }
2894
2895
2896 /**
2897  * ieee802_1x_kay_mkpdu_sanity_check -
2898  *     sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2899  */
2900 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2901                                              const u8 *buf, size_t len)
2902 {
2903         struct ieee8023_hdr *eth_hdr;
2904         struct ieee802_1x_hdr *eapol_hdr;
2905         struct ieee802_1x_mka_hdr *mka_hdr;
2906         struct ieee802_1x_mka_basic_body *body;
2907         size_t mka_msg_len;
2908         struct ieee802_1x_mka_participant *participant;
2909         size_t body_len;
2910         u8 icv[MAX_ICV_LEN];
2911         u8 *msg_icv;
2912
2913         eth_hdr = (struct ieee8023_hdr *) buf;
2914         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2915         mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2916
2917         /* destination address should be not individual address */
2918         if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2919                 wpa_printf(MSG_MSGDUMP,
2920                            "KaY: ethernet destination address is not PAE group address");
2921                 return -1;
2922         }
2923
2924         /* MKPDU should not less than 32 octets */
2925         mka_msg_len = be_to_host16(eapol_hdr->length);
2926         if (mka_msg_len < 32) {
2927                 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2928                 return -1;
2929         }
2930         /* MKPDU should multiple 4 octets */
2931         if ((mka_msg_len % 4) != 0) {
2932                 wpa_printf(MSG_MSGDUMP,
2933                            "KaY: MKPDU is not multiple of 4 octets");
2934                 return -1;
2935         }
2936
2937         body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2938         ieee802_1x_mka_dump_basic_body(body);
2939         body_len = get_mka_param_body_len(body);
2940         /* EAPOL-MKA body should comprise basic parameter set and ICV */
2941         if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2942                 wpa_printf(MSG_ERROR,
2943                            "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",
2944                            mka_msg_len, MKA_HDR_LEN,
2945                            body_len, DEFAULT_ICV_LEN);
2946                 return -1;
2947         }
2948
2949         /* CKN should be owned by I */
2950         participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2951         if (!participant) {
2952                 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2953                 return -1;
2954         }
2955
2956         /* algorithm agility check */
2957         if (os_memcmp(body->algo_agility, mka_algo_agility,
2958                       sizeof(body->algo_agility)) != 0) {
2959                 wpa_printf(MSG_ERROR,
2960                            "KaY: peer's algorithm agility not supported for me");
2961                 return -1;
2962         }
2963
2964         /* ICV check */
2965         /*
2966          * The ICV will comprise the final octets of the packet body, whatever
2967          * its size, not the fixed length 16 octets, indicated by the EAPOL
2968          * packet body length.
2969          */
2970         if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2971                     participant->ick.key,
2972                     buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2973                 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2974                 return -1;
2975         }
2976         msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2977                                                  mka_msg_len);
2978
2979         if (msg_icv) {
2980                 if (os_memcmp_const(msg_icv, icv,
2981                                     mka_alg_tbl[kay->mka_algindex].icv_len) !=
2982                     0) {
2983                         wpa_printf(MSG_ERROR,
2984                                    "KaY: Computed ICV is not equal to Received ICV");
2985                 return -1;
2986                 }
2987         } else {
2988                 wpa_printf(MSG_ERROR, "KaY: No ICV");
2989                 return -1;
2990         }
2991
2992         return 0;
2993 }
2994
2995
2996 /**
2997  * ieee802_1x_kay_decode_mkpdu -
2998  */
2999 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
3000                                        const u8 *buf, size_t len)
3001 {
3002         struct ieee802_1x_mka_participant *participant;
3003         struct ieee802_1x_mka_hdr *hdr;
3004         size_t body_len;
3005         size_t left_len;
3006         u8 body_type;
3007         int i;
3008         const u8 *pos;
3009         Boolean my_included;
3010         Boolean handled[256];
3011
3012         if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
3013                 return -1;
3014
3015         /* handle basic parameter set */
3016         pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
3017         left_len = len - sizeof(struct ieee8023_hdr) -
3018                 sizeof(struct ieee802_1x_hdr);
3019         participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3020         if (!participant)
3021                 return -1;
3022
3023         /* to skip basic parameter set */
3024         hdr = (struct ieee802_1x_mka_hdr *) pos;
3025         body_len = get_mka_param_body_len(hdr);
3026         pos += body_len + MKA_HDR_LEN;
3027         left_len -= body_len + MKA_HDR_LEN;
3028
3029         /* check i am in the peer's peer list */
3030         my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3031         if (my_included) {
3032                 /* accept the peer as live peer */
3033                 if (!ieee802_1x_kay_is_in_peer(
3034                             participant,
3035                             participant->current_peer_id.mi)) {
3036                         if (!ieee802_1x_kay_create_live_peer(
3037                                     participant,
3038                                     participant->current_peer_id.mi,
3039                                     be_to_host32(
3040                                             participant->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                 if (ieee802_1x_kay_is_in_potential_peer(
3046                             participant, participant->current_peer_id.mi)) {
3047                         if (!ieee802_1x_kay_move_live_peer(
3048                                     participant,
3049                                     participant->current_peer_id.mi,
3050                                     be_to_host32(participant->
3051                                                  current_peer_id.mn)))
3052                                 return -1;
3053                         ieee802_1x_kay_elect_key_server(participant);
3054                         ieee802_1x_kay_decide_macsec_use(participant);
3055                 }
3056         }
3057
3058         /*
3059          * Handle other parameter set than basic parameter set.
3060          * Each parameter set should be present only once.
3061          */
3062         for (i = 0; i < 256; i++)
3063                 handled[i] = FALSE;
3064
3065         handled[0] = TRUE;
3066         while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3067                 hdr = (struct ieee802_1x_mka_hdr *) pos;
3068                 body_len = get_mka_param_body_len(hdr);
3069                 body_type = get_mka_param_body_type(hdr);
3070
3071                 if (body_type == MKA_ICV_INDICATOR)
3072                         return 0;
3073
3074                 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3075                         wpa_printf(MSG_ERROR,
3076                                    "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",
3077                                    left_len, MKA_HDR_LEN,
3078                                    body_len, DEFAULT_ICV_LEN);
3079                         goto next_para_set;
3080                 }
3081
3082                 if (handled[body_type])
3083                         goto next_para_set;
3084
3085                 handled[body_type] = TRUE;
3086                 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3087                     mka_body_handler[body_type].body_rx) {
3088                         mka_body_handler[body_type].body_rx
3089                                 (participant, pos, left_len);
3090                 } else {
3091                         wpa_printf(MSG_ERROR,
3092                                    "The type %d is not supported in this MKA version %d",
3093                                    body_type, MKA_VERSION_ID);
3094                 }
3095
3096 next_para_set:
3097                 pos += body_len + MKA_HDR_LEN;
3098                 left_len -= body_len + MKA_HDR_LEN;
3099         }
3100
3101         kay->active = TRUE;
3102         participant->retry_count = 0;
3103         participant->active = TRUE;
3104
3105         return 0;
3106 }
3107
3108
3109
3110 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3111                            size_t len)
3112 {
3113         struct ieee802_1x_kay *kay = ctx;
3114         struct ieee8023_hdr *eth_hdr;
3115         struct ieee802_1x_hdr *eapol_hdr;
3116
3117         /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3118         if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3119                 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3120                            (unsigned long) len);
3121                 return;
3122         }
3123
3124         eth_hdr = (struct ieee8023_hdr *) buf;
3125         eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3126         if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3127             be_to_host16(eapol_hdr->length)) {
3128                 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3129                            (unsigned long) len,
3130                            (unsigned long) be_to_host16(eapol_hdr->length));
3131                 return;
3132         }
3133
3134         if (eapol_hdr->version < EAPOL_VERSION) {
3135                 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3136                            eapol_hdr->version);
3137                 return;
3138         }
3139         if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3140             eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3141                 return;
3142
3143         wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3144         if (dl_list_empty(&kay->participant_list)) {
3145                 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3146                 return;
3147         }
3148
3149         ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3150 }
3151
3152
3153 /**
3154  * ieee802_1x_kay_init -
3155  */
3156 struct ieee802_1x_kay *
3157 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3158                     const char *ifname, const u8 *addr)
3159 {
3160         struct ieee802_1x_kay *kay;
3161
3162         kay = os_zalloc(sizeof(*kay));
3163         if (!kay) {
3164                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3165                 return NULL;
3166         }
3167
3168         kay->ctx = ctx;
3169
3170         kay->enable = TRUE;
3171         kay->active = FALSE;
3172
3173         kay->authenticated = FALSE;
3174         kay->secured = FALSE;
3175         kay->failed = FALSE;
3176         kay->policy = policy;
3177
3178         os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3179         os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3180         kay->actor_sci.port = host_to_be16(0x0001);
3181         kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3182
3183         /* While actor acts as a key server, shall distribute sakey */
3184         kay->dist_kn = 1;
3185         kay->dist_an = 0;
3186         kay->dist_time = 0;
3187
3188         kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3189         kay->macsec_csindex = DEFAULT_CS_INDEX;
3190         kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3191         kay->mka_version = MKA_VERSION_ID;
3192
3193         os_memcpy(kay->algo_agility, mka_algo_agility,
3194                   sizeof(kay->algo_agility));
3195
3196         dl_list_init(&kay->participant_list);
3197
3198         if (policy == DO_NOT_SECURE) {
3199                 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3200                 kay->macsec_desired = FALSE;
3201                 kay->macsec_protect = FALSE;
3202                 kay->macsec_validate = Disabled;
3203                 kay->macsec_replay_protect = FALSE;
3204                 kay->macsec_replay_window = 0;
3205                 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3206         } else {
3207                 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3208                 kay->macsec_desired = TRUE;
3209                 kay->macsec_protect = TRUE;
3210                 kay->macsec_validate = Strict;
3211                 kay->macsec_replay_protect = FALSE;
3212                 kay->macsec_replay_window = 0;
3213                 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3214         }
3215
3216         wpa_printf(MSG_DEBUG, "KaY: state machine created");
3217
3218         /* Initialize the SecY must be prio to CP, as CP will control SecY */
3219         secy_init_macsec(kay);
3220         secy_get_available_transmit_sc(kay, &kay->sc_ch);
3221
3222         wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3223
3224         /* init CP */
3225         kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3226         if (kay->cp == NULL) {
3227                 ieee802_1x_kay_deinit(kay);
3228                 return NULL;
3229         }
3230
3231         if (policy == DO_NOT_SECURE) {
3232                 ieee802_1x_cp_connect_authenticated(kay->cp);
3233                 ieee802_1x_cp_sm_step(kay->cp);
3234         } else {
3235                 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3236                                              kay_l2_receive, kay, 1);
3237                 if (kay->l2_mka == NULL) {
3238                         wpa_printf(MSG_WARNING,
3239                                    "KaY: Failed to initialize L2 packet processing for MKA packet");
3240                         ieee802_1x_kay_deinit(kay);
3241                         return NULL;
3242                 }
3243         }
3244
3245         return kay;
3246 }
3247
3248
3249 /**
3250  * ieee802_1x_kay_deinit -
3251  */
3252 void
3253 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3254 {
3255         struct ieee802_1x_mka_participant *participant;
3256
3257         if (!kay)
3258                 return;
3259
3260         wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3261
3262         while (!dl_list_empty(&kay->participant_list)) {
3263                 participant = dl_list_entry(kay->participant_list.next,
3264                                             struct ieee802_1x_mka_participant,
3265                                             list);
3266                 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3267         }
3268
3269         ieee802_1x_cp_sm_deinit(kay->cp);
3270         secy_deinit_macsec(kay);
3271
3272         if (kay->l2_mka) {
3273                 l2_packet_deinit(kay->l2_mka);
3274                 kay->l2_mka = NULL;
3275         }
3276
3277         os_free(kay->ctx);
3278         os_free(kay);
3279 }
3280
3281
3282 /**
3283  * ieee802_1x_kay_create_mka -
3284  */
3285 struct ieee802_1x_mka_participant *
3286 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3287                           struct mka_key *cak, u32 life,
3288                           enum mka_created_mode mode, Boolean is_authenticator)
3289 {
3290         struct ieee802_1x_mka_participant *participant;
3291         unsigned int usecs;
3292
3293         if (!kay || !ckn || !cak) {
3294                 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3295                 return NULL;
3296         }
3297
3298         if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3299                 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3300                 return NULL;
3301         }
3302         if (ckn->len > MAX_CKN_LEN) {
3303                 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3304                 return NULL;
3305         }
3306         if (!kay->enable) {
3307                 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3308                 return NULL;
3309         }
3310
3311         participant = os_zalloc(sizeof(*participant));
3312         if (!participant) {
3313                 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3314                 return NULL;
3315         }
3316
3317         participant->ckn.len = ckn->len;
3318         os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3319         participant->cak.len = cak->len;
3320         os_memcpy(participant->cak.key, cak->key, cak->len);
3321         if (life)
3322                 participant->cak_life = life + time(NULL);
3323
3324         switch (mode) {
3325         case EAP_EXCHANGE:
3326                 if (is_authenticator) {
3327                         participant->is_obliged_key_server = TRUE;
3328                         participant->can_be_key_server = TRUE;
3329                         participant->is_key_server = TRUE;
3330                         participant->principal = TRUE;
3331
3332                         os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3333                                   sizeof(kay->key_server_sci));
3334                         kay->key_server_priority = kay->actor_priority;
3335                         participant->is_elected = TRUE;
3336                 } else {
3337                         participant->is_obliged_key_server = FALSE;
3338                         participant->can_be_key_server = FALSE;
3339                         participant->is_key_server = FALSE;
3340                         participant->is_elected = TRUE;
3341                 }
3342                 break;
3343
3344         default:
3345                 participant->is_obliged_key_server = FALSE;
3346                 participant->can_be_key_server = TRUE;
3347                 participant->is_key_server = TRUE;
3348                 participant->is_elected = FALSE;
3349                 break;
3350         }
3351
3352         participant->cached = FALSE;
3353
3354         participant->active = FALSE;
3355         participant->participant = FALSE;
3356         participant->retain = FALSE;
3357         participant->activate = DEFAULT;
3358
3359         if (participant->is_key_server)
3360                 participant->principal = TRUE;
3361
3362         dl_list_init(&participant->live_peers);
3363         dl_list_init(&participant->potential_peers);
3364
3365         participant->retry_count = 0;
3366         participant->kay = kay;
3367
3368         if (!reset_participant_mi(participant))
3369                 goto fail;
3370
3371         participant->lrx = FALSE;
3372         participant->ltx = FALSE;
3373         participant->orx = FALSE;
3374         participant->otx = FALSE;
3375         participant->to_dist_sak = FALSE;
3376         participant->to_use_sak = FALSE;
3377         participant->new_sak = FALSE;
3378         dl_list_init(&participant->sak_list);
3379         participant->new_key = NULL;
3380         dl_list_init(&participant->rxsc_list);
3381         participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3382                                                             kay->sc_ch);
3383         secy_cp_control_protect_frames(kay, kay->macsec_protect);
3384         secy_cp_control_replay(kay, kay->macsec_replay_protect,
3385                                kay->macsec_replay_window);
3386         secy_create_transmit_sc(kay, participant->txsc);
3387
3388         /* to derive KEK from CAK and CKN */
3389         participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3390         if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3391                                                     participant->ckn.name,
3392                                                     participant->ckn.len,
3393                                                     participant->kek.key)) {
3394                 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3395                 goto fail;
3396         }
3397         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3398                         participant->kek.key, participant->kek.len);
3399
3400         /* to derive ICK from CAK and CKN */
3401         participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3402         if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3403                                                     participant->ckn.name,
3404                                                     participant->ckn.len,
3405                                                     participant->ick.key)) {
3406                 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3407                 goto fail;
3408         }
3409         wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3410                         participant->ick.key, participant->ick.len);
3411
3412         dl_list_add(&kay->participant_list, &participant->list);
3413         wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3414                     ckn->name, ckn->len);
3415
3416         usecs = os_random() % (MKA_HELLO_TIME * 1000);
3417         eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3418                                participant, NULL);
3419         participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3420                 usecs / 1000000;
3421
3422         return participant;
3423
3424 fail:
3425         os_free(participant);
3426         return NULL;
3427 }
3428
3429
3430 /**
3431  * ieee802_1x_kay_delete_mka -
3432  */
3433 void
3434 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3435 {
3436         struct ieee802_1x_mka_participant *participant;
3437         struct ieee802_1x_kay_peer *peer;
3438         struct data_key *sak;
3439         struct receive_sc *rxsc;
3440
3441         if (!kay || !ckn)
3442                 return;
3443
3444         wpa_printf(MSG_DEBUG, "KaY: participant removed");
3445
3446         /* get the participant */
3447         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3448         if (!participant) {
3449                 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3450                             ckn->name, ckn->len);
3451                 return;
3452         }
3453
3454         eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3455         dl_list_del(&participant->list);
3456
3457         /* remove live peer */
3458         while (!dl_list_empty(&participant->live_peers)) {
3459                 peer = dl_list_entry(participant->live_peers.next,
3460                                      struct ieee802_1x_kay_peer, list);
3461                 dl_list_del(&peer->list);
3462                 os_free(peer);
3463         }
3464
3465         /* remove potential peer */
3466         while (!dl_list_empty(&participant->potential_peers)) {
3467                 peer = dl_list_entry(participant->potential_peers.next,
3468                                      struct ieee802_1x_kay_peer, list);
3469                 dl_list_del(&peer->list);
3470                 os_free(peer);
3471         }
3472
3473         /* remove sak */
3474         while (!dl_list_empty(&participant->sak_list)) {
3475                 sak = dl_list_entry(participant->sak_list.next,
3476                                     struct data_key, list);
3477                 dl_list_del(&sak->list);
3478                 os_free(sak->key);
3479                 os_free(sak);
3480         }
3481         while (!dl_list_empty(&participant->rxsc_list)) {
3482                 rxsc = dl_list_entry(participant->rxsc_list.next,
3483                                      struct receive_sc, list);
3484                 secy_delete_receive_sc(kay, rxsc);
3485                 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3486         }
3487         secy_delete_transmit_sc(kay, participant->txsc);
3488         ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3489
3490         os_memset(&participant->cak, 0, sizeof(participant->cak));
3491         os_memset(&participant->kek, 0, sizeof(participant->kek));
3492         os_memset(&participant->ick, 0, sizeof(participant->ick));
3493         os_free(participant);
3494 }
3495
3496
3497 /**
3498  * ieee802_1x_kay_mka_participate -
3499  */
3500 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3501                                     struct mka_key_name *ckn,
3502                                     Boolean status)
3503 {
3504         struct ieee802_1x_mka_participant *participant;
3505
3506         if (!kay || !ckn)
3507                 return;
3508
3509         participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3510         if (!participant)
3511                 return;
3512
3513         participant->active = status;
3514 }
3515
3516
3517 /**
3518  * ieee802_1x_kay_new_sak -
3519  */
3520 int
3521 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3522 {
3523         struct ieee802_1x_mka_participant *participant;
3524
3525         if (!kay)
3526                 return -1;
3527
3528         participant = ieee802_1x_kay_get_principal_participant(kay);
3529         if (!participant)
3530                 return -1;
3531
3532         participant->new_sak = TRUE;
3533         wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3534
3535         return 0;
3536 }
3537
3538
3539 /**
3540  * ieee802_1x_kay_change_cipher_suite -
3541  */
3542 int
3543 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3544 {
3545         struct ieee802_1x_mka_participant *participant;
3546
3547         if (!kay)
3548                 return -1;
3549
3550         if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3551                 wpa_printf(MSG_ERROR,
3552                            "KaY: Configured cipher suite index is out of range");
3553                 return -1;
3554         }
3555         if (kay->macsec_csindex == cs_index)
3556                 return -2;
3557
3558         if (cs_index == 0)
3559                 kay->macsec_desired = FALSE;
3560
3561         kay->macsec_csindex = cs_index;
3562         kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3563
3564         participant = ieee802_1x_kay_get_principal_participant(kay);
3565         if (participant) {
3566                 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3567                 participant->new_sak = TRUE;
3568         }
3569
3570         return 0;
3571 }