2 * IEEE 802.1X-2010 Key Agree Protocol of PAE state machine
3 * Copyright (c) 2013, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
26 #define DEFAULT_SA_KEY_LEN 16
27 #define DEFAULT_ICV_LEN 16
28 #define MAX_ICV_LEN 32 /* 32 bytes, 256 bits */
30 #define PENDING_PN_EXHAUSTION 0xC0000000
32 #define MKA_ALIGN_LENGTH(len) (((len) + 0x3) & ~0x3)
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;
38 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
39 static struct macsec_ciphersuite cipher_suite_tbl[] = {
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,
49 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
50 #define DEFAULT_CS_INDEX 0
52 static struct mka_alg mka_alg_tbl[] = {
54 .parameter = MKA_ALGO_AGILITY_2009,
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,
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,
71 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
74 static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
75 struct ieee802_1x_mka_ki *ki2)
77 return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
82 struct mka_param_body_handler {
83 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
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);
92 static void set_mka_param_body_len(void *body, unsigned int len)
94 struct ieee802_1x_mka_hdr *hdr = body;
95 hdr->length = (len >> 8) & 0x0f;
96 hdr->length1 = len & 0xff;
100 static unsigned int get_mka_param_body_len(const void *body)
102 const struct ieee802_1x_mka_hdr *hdr = body;
103 return (hdr->length << 8) | hdr->length1;
107 static u8 get_mka_param_body_type(const void *body)
109 const struct ieee802_1x_mka_hdr *hdr = body;
115 * ieee802_1x_mka_dump_basic_body -
118 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
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...: %d", (int) 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));
149 * ieee802_1x_mka_dump_peer_body -
152 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
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...: %d", (int) 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...: %d", (int) body_len);
171 for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
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));
181 * ieee802_1x_mka_dump_dist_sak_body -
184 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
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...........: %d", (int) body_len);
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);
206 static const char * yes_no(int val)
208 return val ? "Yes" : "No";
213 * ieee802_1x_mka_dump_sak_use_body -
216 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
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);
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));
255 * ieee802_1x_kay_get_participant -
257 static struct ieee802_1x_mka_participant *
258 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
260 struct ieee802_1x_mka_participant *participant;
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)
269 wpa_printf(MSG_DEBUG, "KaY: participant is not found");
276 * ieee802_1x_kay_get_principal_participant -
278 static struct ieee802_1x_mka_participant *
279 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
281 struct ieee802_1x_mka_participant *participant;
283 dl_list_for_each(participant, &kay->participant_list,
284 struct ieee802_1x_mka_participant, list) {
285 if (participant->principal)
289 wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
294 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
297 struct ieee802_1x_kay_peer *peer;
299 dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
300 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
309 * ieee802_1x_kay_get_potential_peer
311 static struct ieee802_1x_kay_peer *
312 ieee802_1x_kay_get_potential_peer(
313 struct ieee802_1x_mka_participant *participant, const u8 *mi)
315 return get_peer_mi(&participant->potential_peers, mi);
320 * ieee802_1x_kay_get_live_peer
322 static struct ieee802_1x_kay_peer *
323 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
326 return get_peer_mi(&participant->live_peers, mi);
331 * ieee802_1x_kay_is_in_potential_peer
334 ieee802_1x_kay_is_in_potential_peer(
335 struct ieee802_1x_mka_participant *participant, const u8 *mi)
337 return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
342 * ieee802_1x_kay_is_in_live_peer
345 ieee802_1x_kay_is_in_live_peer(
346 struct ieee802_1x_mka_participant *participant, const u8 *mi)
348 return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
353 * ieee802_1x_kay_is_in_peer
356 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
359 return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
360 ieee802_1x_kay_is_in_potential_peer(participant, mi);
365 * ieee802_1x_kay_get_peer
367 static struct ieee802_1x_kay_peer *
368 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
371 struct ieee802_1x_kay_peer *peer;
373 peer = ieee802_1x_kay_get_live_peer(participant, mi);
377 return ieee802_1x_kay_get_potential_peer(participant, mi);
382 * ieee802_1x_kay_get_cipher_suite
384 static struct macsec_ciphersuite *
385 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
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];
399 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
400 const struct ieee802_1x_mka_sci *b)
402 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
407 * ieee802_1x_kay_get_peer_sci
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)
413 struct ieee802_1x_kay_peer *peer;
415 dl_list_for_each(peer, &participant->live_peers,
416 struct ieee802_1x_kay_peer, list) {
417 if (sci_equal(&peer->sci, sci))
421 dl_list_for_each(peer, &participant->potential_peers,
422 struct ieee802_1x_kay_peer, list) {
423 if (sci_equal(&peer->sci, sci))
432 * ieee802_1x_kay_init_receive_sa -
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)
438 struct receive_sa *psa;
443 psa = os_zalloc(sizeof(*psa));
445 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
450 psa->lowest_pn = lowest_pn;
451 psa->next_pn = lowest_pn;
455 os_get_time(&psa->created_time);
458 dl_list_add(&psc->sa_list, &psa->list);
459 wpa_printf(MSG_DEBUG,
460 "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
461 (int) an, lowest_pn, psc->channel);
468 * ieee802_1x_kay_deinit_receive_sa -
470 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
473 wpa_printf(MSG_DEBUG,
474 "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
475 psa->an, psa->sc->channel);
476 dl_list_del(&psa->list);
482 * ieee802_1x_kay_init_receive_sc -
484 static struct receive_sc *
485 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
488 struct receive_sc *psc;
493 psc = os_zalloc(sizeof(*psc));
495 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
499 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
500 psc->channel = channel;
502 os_get_time(&psc->created_time);
503 psc->receiving = FALSE;
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));
514 * ieee802_1x_kay_deinit_receive_sc -
517 ieee802_1x_kay_deinit_receive_sc(
518 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
520 struct receive_sa *psa, *pre_sa;
522 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
524 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
526 secy_disable_receive_sa(participant->kay, psa);
527 ieee802_1x_kay_deinit_receive_sa(psa);
529 dl_list_del(&psc->list);
534 static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
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);
543 static struct ieee802_1x_kay_peer *
544 ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
546 struct ieee802_1x_kay_peer *peer;
548 peer = os_zalloc(sizeof(*peer));
550 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
554 os_memcpy(peer->mi, mi, MI_LEN);
556 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
557 peer->sak_used = FALSE;
564 * ieee802_1x_kay_create_live_peer
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)
570 struct ieee802_1x_kay_peer *peer;
571 struct receive_sc *rxsc;
574 peer = ieee802_1x_kay_create_peer(mi, mn);
578 os_memcpy(&peer->sci, &participant->current_peer_sci,
581 secy_get_available_receive_sc(participant->kay, &sc_ch);
583 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
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);
593 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
594 ieee802_1x_kay_dump_peer(peer);
601 * ieee802_1x_kay_create_potential_peer
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)
607 struct ieee802_1x_kay_peer *peer;
609 peer = ieee802_1x_kay_create_peer(mi, mn);
613 dl_list_add(&participant->potential_peers, &peer->list);
615 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
616 ieee802_1x_kay_dump_peer(peer);
623 * ieee802_1x_kay_move_live_peer
625 static struct ieee802_1x_kay_peer *
626 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
629 struct ieee802_1x_kay_peer *peer;
630 struct receive_sc *rxsc;
633 peer = ieee802_1x_kay_get_potential_peer(participant, mi);
635 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
640 os_memcpy(&peer->sci, &participant->current_peer_sci,
643 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
645 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
646 ieee802_1x_kay_dump_peer(peer);
648 dl_list_del(&peer->list);
649 dl_list_add_tail(&participant->live_peers, &peer->list);
651 secy_get_available_receive_sc(participant->kay, &sc_ch);
653 dl_list_add(&participant->rxsc_list, &rxsc->list);
654 secy_create_receive_sc(participant->kay, rxsc);
662 * ieee802_1x_mka_basic_body_present -
665 ieee802_1x_mka_basic_body_present(
666 struct ieee802_1x_mka_participant *participant)
673 * ieee802_1x_mka_basic_body_length -
676 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
680 length = sizeof(struct ieee802_1x_mka_basic_body);
681 length += participant->ckn.len;
682 return MKA_ALIGN_LENGTH(length);
687 * ieee802_1x_mka_encode_basic_body
690 ieee802_1x_mka_encode_basic_body(
691 struct ieee802_1x_mka_participant *participant,
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);
698 body = wpabuf_put(buf, length);
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;
705 body->key_server = participant->can_be_key_server;
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);
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;
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));
721 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
723 ieee802_1x_mka_dump_basic_body(body);
730 * ieee802_1x_mka_decode_basic_body -
732 static struct ieee802_1x_mka_participant *
733 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
736 struct ieee802_1x_mka_participant *participant;
737 const struct ieee802_1x_mka_basic_body *body;
738 struct ieee802_1x_kay_peer *peer;
740 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
742 if (body->version > MKA_VERSION_ID) {
743 wpa_printf(MSG_DEBUG,
744 "KaY: peer's version(%d) greater than mka current version(%d)",
745 body->version, MKA_VERSION_ID);
747 if (kay->is_obliged_key_server && body->key_server) {
748 wpa_printf(MSG_DEBUG, "I must be as key server");
752 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
754 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
758 /* If the peer's MI is my MI, I will choose new MI */
759 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
760 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
765 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
766 participant->current_peer_id.mn = body->actor_mn;
767 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
768 sizeof(participant->current_peer_sci.addr));
769 participant->current_peer_sci.port = body->actor_sci.port;
772 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
774 /* Check duplicated SCI */
775 /* TODO: What policy should be applied to detect duplicated SCI
776 * is active attacker or a valid peer whose MI is be changed?
778 peer = ieee802_1x_kay_get_peer_sci(participant,
781 wpa_printf(MSG_WARNING,
782 "KaY: duplicated SCI detected, Maybe active attacker");
783 dl_list_del(&peer->list);
787 peer = ieee802_1x_kay_create_potential_peer(
788 participant, body->actor_mi,
789 be_to_host32(body->actor_mn));
793 peer->macsec_desired = body->macsec_desired;
794 peer->macsec_capability = body->macsec_capability;
795 peer->is_key_server = (Boolean) body->key_server;
796 peer->key_server_priority = body->priority;
797 } else if (peer->mn < be_to_host32(body->actor_mn)) {
798 peer->mn = be_to_host32(body->actor_mn);
799 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
800 peer->macsec_desired = body->macsec_desired;
801 peer->macsec_capability = body->macsec_capability;
802 peer->is_key_server = (Boolean) body->key_server;
803 peer->key_server_priority = body->priority;
805 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
814 * ieee802_1x_mka_live_peer_body_present
817 ieee802_1x_mka_live_peer_body_present(
818 struct ieee802_1x_mka_participant *participant)
820 return !dl_list_empty(&participant->live_peers);
825 * ieee802_1x_kay_get_live_peer_length
828 ieee802_1x_mka_get_live_peer_length(
829 struct ieee802_1x_mka_participant *participant)
831 int len = MKA_HDR_LEN;
832 struct ieee802_1x_kay_peer *peer;
834 dl_list_for_each(peer, &participant->live_peers,
835 struct ieee802_1x_kay_peer, list)
836 len += sizeof(struct ieee802_1x_mka_peer_id);
838 return MKA_ALIGN_LENGTH(len);
843 * ieee802_1x_mka_encode_live_peer_body -
846 ieee802_1x_mka_encode_live_peer_body(
847 struct ieee802_1x_mka_participant *participant,
850 struct ieee802_1x_mka_peer_body *body;
851 struct ieee802_1x_kay_peer *peer;
853 struct ieee802_1x_mka_peer_id *body_peer;
855 length = ieee802_1x_mka_get_live_peer_length(participant);
856 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
858 body->type = MKA_LIVE_PEER_LIST;
859 set_mka_param_body_len(body, length - MKA_HDR_LEN);
861 dl_list_for_each(peer, &participant->live_peers,
862 struct ieee802_1x_kay_peer, list) {
863 body_peer = wpabuf_put(buf,
864 sizeof(struct ieee802_1x_mka_peer_id));
865 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
866 body_peer->mn = host_to_be32(peer->mn);
870 ieee802_1x_mka_dump_peer_body(body);
875 * ieee802_1x_mka_potential_peer_body_present
878 ieee802_1x_mka_potential_peer_body_present(
879 struct ieee802_1x_mka_participant *participant)
881 return !dl_list_empty(&participant->potential_peers);
886 * ieee802_1x_kay_get_potential_peer_length
889 ieee802_1x_mka_get_potential_peer_length(
890 struct ieee802_1x_mka_participant *participant)
892 int len = MKA_HDR_LEN;
893 struct ieee802_1x_kay_peer *peer;
895 dl_list_for_each(peer, &participant->potential_peers,
896 struct ieee802_1x_kay_peer, list)
897 len += sizeof(struct ieee802_1x_mka_peer_id);
899 return MKA_ALIGN_LENGTH(len);
904 * ieee802_1x_mka_encode_potential_peer_body -
907 ieee802_1x_mka_encode_potential_peer_body(
908 struct ieee802_1x_mka_participant *participant,
911 struct ieee802_1x_mka_peer_body *body;
912 struct ieee802_1x_kay_peer *peer;
914 struct ieee802_1x_mka_peer_id *body_peer;
916 length = ieee802_1x_mka_get_potential_peer_length(participant);
917 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
919 body->type = MKA_POTENTIAL_PEER_LIST;
920 set_mka_param_body_len(body, length - MKA_HDR_LEN);
922 dl_list_for_each(peer, &participant->potential_peers,
923 struct ieee802_1x_kay_peer, list) {
924 body_peer = wpabuf_put(buf,
925 sizeof(struct ieee802_1x_mka_peer_id));
926 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
927 body_peer->mn = host_to_be32(peer->mn);
931 ieee802_1x_mka_dump_peer_body(body);
937 * ieee802_1x_mka_i_in_peerlist -
940 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
941 const u8 *mka_msg, size_t msg_len)
943 Boolean included = FALSE;
944 struct ieee802_1x_mka_hdr *hdr;
956 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
957 hdr = (struct ieee802_1x_mka_hdr *) pos;
958 body_len = get_mka_param_body_len(hdr);
959 body_type = get_mka_param_body_type(hdr);
961 if (body_type != MKA_LIVE_PEER_LIST &&
962 body_type != MKA_POTENTIAL_PEER_LIST)
965 ieee802_1x_mka_dump_peer_body(
966 (struct ieee802_1x_mka_peer_body *)pos);
968 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
969 wpa_printf(MSG_ERROR,
970 "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
971 (int) left_len, (int) MKA_HDR_LEN,
972 (int) body_len, DEFAULT_ICV_LEN);
976 if ((body_len % 16) != 0) {
977 wpa_printf(MSG_ERROR,
978 "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
983 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
984 peer_mi = MKA_HDR_LEN + pos + i;
985 os_memcpy(&_peer_mn, peer_mi + MI_LEN,
987 peer_mn = be_to_host32(_peer_mn);
988 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
989 peer_mn == participant->mn) {
999 left_len -= body_len + MKA_HDR_LEN;
1000 pos += body_len + MKA_HDR_LEN;
1008 * ieee802_1x_mka_decode_live_peer_body -
1010 static int ieee802_1x_mka_decode_live_peer_body(
1011 struct ieee802_1x_mka_participant *participant,
1012 const u8 *peer_msg, size_t msg_len)
1014 const struct ieee802_1x_mka_hdr *hdr;
1015 struct ieee802_1x_kay_peer *peer;
1021 Boolean is_included;
1023 is_included = ieee802_1x_kay_is_in_live_peer(
1024 participant, participant->current_peer_id.mi);
1026 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1027 body_len = get_mka_param_body_len(hdr);
1028 if (body_len % 16 != 0) {
1029 wpa_printf(MSG_ERROR,
1030 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1035 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1036 peer_mi = MKA_HDR_LEN + peer_msg + i;
1037 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1038 peer_mn = be_to_host32(_peer_mn);
1041 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1042 /* My message id is used by other participant */
1043 if (peer_mn > participant->mn) {
1044 if (os_get_random(participant->mi,
1045 sizeof(participant->mi)) < 0)
1046 wpa_printf(MSG_DEBUG,
1047 "KaY: Could not update mi");
1048 participant->mn = 0;
1055 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1058 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1060 if (!ieee802_1x_kay_create_potential_peer(
1061 participant, peer_mi, peer_mn)) {
1072 * ieee802_1x_mka_decode_potential_peer_body -
1075 ieee802_1x_mka_decode_potential_peer_body(
1076 struct ieee802_1x_mka_participant *participant,
1077 const u8 *peer_msg, size_t msg_len)
1079 struct ieee802_1x_mka_hdr *hdr;
1086 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1087 body_len = get_mka_param_body_len(hdr);
1088 if (body_len % 16 != 0) {
1089 wpa_printf(MSG_ERROR,
1090 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1095 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1096 peer_mi = MKA_HDR_LEN + peer_msg + i;
1097 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1098 peer_mn = be_to_host32(_peer_mn);
1101 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1102 /* My message id is used by other participant */
1103 if (peer_mn > participant->mn) {
1104 if (os_get_random(participant->mi,
1105 sizeof(participant->mi)) < 0)
1106 wpa_printf(MSG_DEBUG,
1107 "KaY: Could not update mi");
1108 participant->mn = 0;
1119 * ieee802_1x_mka_sak_use_body_present
1122 ieee802_1x_mka_sak_use_body_present(
1123 struct ieee802_1x_mka_participant *participant)
1125 if (participant->to_use_sak)
1133 * ieee802_1x_mka_get_sak_use_length
1136 ieee802_1x_mka_get_sak_use_length(
1137 struct ieee802_1x_mka_participant *participant)
1139 int length = MKA_HDR_LEN;
1141 if (participant->kay->macsec_desired && participant->advised_desired)
1142 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1144 return MKA_ALIGN_LENGTH(length);
1152 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1153 struct ieee802_1x_mka_ki *ki)
1155 struct receive_sa *rxsa;
1156 struct receive_sc *rxsc;
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)
1162 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1163 secy_get_receive_lowest_pn(principal->kay,
1166 lpn = lpn > rxsa->lowest_pn ?
1167 lpn : rxsa->lowest_pn;
1181 * ieee802_1x_mka_encode_sak_use_body -
1184 ieee802_1x_mka_encode_sak_use_body(
1185 struct ieee802_1x_mka_participant *participant,
1188 struct ieee802_1x_mka_sak_use_body *body;
1189 unsigned int length;
1192 length = ieee802_1x_mka_get_sak_use_length(participant);
1193 body = wpabuf_put(buf, length);
1195 body->type = MKA_SAK_USE;
1196 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1198 if (length == MKA_HDR_LEN) {
1204 body->delay_protect = FALSE;
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;
1217 body->llpn = host_to_be32(pn);
1218 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1219 body->olpn = host_to_be32(pn);
1221 /* plain tx, plain rx */
1222 if (participant->kay->macsec_protect)
1227 if (participant->kay->macsec_validate == Strict)
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;
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) {
1246 os_memcpy(body->osrv_mi, participant->oki.mi,
1247 sizeof(body->osrv_mi));
1248 body->okn = host_to_be32(participant->oki.kn);
1254 /* set CP's variable */
1256 if (!participant->kay->tx_enable)
1257 participant->kay->tx_enable = TRUE;
1259 if (!participant->kay->port_enable)
1260 participant->kay->port_enable = TRUE;
1263 if (!participant->kay->rx_enable)
1264 participant->kay->rx_enable = TRUE;
1267 ieee802_1x_mka_dump_sak_use_body(body);
1273 * ieee802_1x_mka_decode_sak_use_body -
1276 ieee802_1x_mka_decode_sak_use_body(
1277 struct ieee802_1x_mka_participant *participant,
1278 const u8 *mka_msg, size_t msg_len)
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;
1286 struct ieee802_1x_mka_ki ki;
1288 Boolean all_receiving;
1291 if (!participant->principal) {
1292 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1295 peer = ieee802_1x_kay_get_live_peer(participant,
1296 participant->current_peer_id.mi);
1298 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
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);
1307 if ((body_len != 0) && (body_len < 40)) {
1308 wpa_printf(MSG_ERROR,
1309 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
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");
1320 /* TODO: when the plain tx or rx of peer is true, should I change
1321 * the attribute of controlled port
1324 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1327 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1329 /* check latest key is valid */
1330 if (body->ltx || body->lrx) {
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)) {
1342 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
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;
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);
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");
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");
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;
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);
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");
1402 dl_list_for_each(txsa, &participant->txsc->sa_list,
1403 struct transmit_sa, list) {
1404 if (sa_key != NULL && txsa->pkey == sa_key) {
1410 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
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.
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);
1428 * ieee802_1x_mka_dist_sak_body_present
1431 ieee802_1x_mka_dist_sak_body_present(
1432 struct ieee802_1x_mka_participant *participant)
1434 if (!participant->to_dist_sak || !participant->new_key)
1442 * ieee802_1x_kay_get_dist_sak_length
1445 ieee802_1x_mka_get_dist_sak_length(
1446 struct ieee802_1x_mka_participant *participant)
1448 int length = MKA_HDR_LEN;
1449 int cs_index = participant->kay->macsec_csindex;
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;
1456 length += cipher_suite_tbl[cs_index].sak_len + 8;
1459 return MKA_ALIGN_LENGTH(length);
1464 * ieee802_1x_mka_encode_dist_sak_body -
1467 ieee802_1x_mka_encode_dist_sak_body(
1468 struct ieee802_1x_mka_participant *participant,
1471 struct ieee802_1x_mka_dist_sak_body *body;
1472 struct data_key *sak;
1473 unsigned int length;
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;
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;
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;
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");
1504 ieee802_1x_mka_dump_dist_sak_body(body);
1511 * ieee802_1x_kay_init_data_key -
1513 static struct data_key *
1514 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1516 struct data_key *pkey;
1521 pkey = os_zalloc(sizeof(*pkey));
1523 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1527 pkey->key = os_zalloc(conf->key_len);
1528 if (pkey->key == NULL) {
1529 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
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);
1550 * ieee802_1x_kay_decode_dist_sak_body -
1553 ieee802_1x_mka_decode_dist_sak_body(
1554 struct ieee802_1x_mka_participant *participant,
1555 const u8 *mka_msg, size_t msg_len)
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;
1562 struct key_conf *conf;
1563 struct data_key *sa_key = NULL;
1564 struct ieee802_1x_mka_ki sak_ki;
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 (%d bytes) should be 0, 28, 36, or more octets",
1578 if (!participant->principal) {
1579 wpa_printf(MSG_ERROR,
1580 "KaY: I can't accept the distributed SAK as I am not principal");
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 ");
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");
1595 peer = ieee802_1x_kay_get_live_peer(participant,
1596 participant->current_peer_id.mi);
1598 wpa_printf(MSG_ERROR,
1599 "KaY: The key server is not in my live peers list");
1602 if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
1603 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
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;
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);
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)
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");
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;
1640 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1642 wpa_printf(MSG_ERROR,
1643 "KaY: I can't support the Cipher Suite advised by key server");
1646 sak_len = cs->sak_len;
1647 wrap_sak = body->sak + CS_ID_LEN;
1648 participant->kay->macsec_csindex = cs->index;
1651 unwrap_sak = os_zalloc(sak_len);
1653 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1656 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1658 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1659 os_free(unwrap_sak);
1662 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1664 conf = os_zalloc(sizeof(*conf));
1666 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1667 os_free(unwrap_sak);
1670 conf->key_len = sak_len;
1672 conf->key = os_zalloc(conf->key_len);
1674 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1675 os_free(unwrap_sak);
1680 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1682 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1684 sak_ki.kn = be_to_host32(body->kn);
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;
1693 sa_key = ieee802_1x_kay_init_data_key(conf);
1695 os_free(unwrap_sak);
1701 dl_list_add(&participant->sak_list, &sa_key->list);
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);
1714 participant->to_use_sak = TRUE;
1716 os_free(unwrap_sak);
1725 * ieee802_1x_mka_icv_body_present
1728 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1735 * ieee802_1x_kay_get_icv_length
1738 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1742 length = sizeof(struct ieee802_1x_mka_icv_body);
1743 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1745 return MKA_ALIGN_LENGTH(length);
1750 * ieee802_1x_mka_encode_icv_body -
1753 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1756 struct ieee802_1x_mka_icv_body *body;
1757 unsigned int length;
1758 u8 cmac[MAX_ICV_LEN];
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);
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");
1773 if (length != DEFAULT_ICV_LEN) {
1774 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1775 length - MKA_HDR_LEN);
1777 os_memcpy(wpabuf_put(buf, length), cmac, length);
1784 * ieee802_1x_mka_decode_icv_body -
1787 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1788 const u8 *mka_msg, size_t msg_len)
1790 struct ieee802_1x_mka_hdr *hdr;
1791 struct ieee802_1x_mka_icv_body *body;
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);
1804 if (left_len < (body_len + MKA_HDR_LEN))
1807 if (body_type != MKA_ICV_INDICATOR) {
1808 left_len -= MKA_HDR_LEN + body_len;
1809 pos += MKA_HDR_LEN + body_len;
1813 body = (struct ieee802_1x_mka_icv_body *)pos;
1815 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1822 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1827 * ieee802_1x_mka_decode_dist_cak_body-
1830 ieee802_1x_mka_decode_dist_cak_body(
1831 struct ieee802_1x_mka_participant *participant,
1832 const u8 *mka_msg, size_t msg_len)
1834 struct ieee802_1x_mka_hdr *hdr;
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 (%d bytes) should be 28 or more octets",
1851 * ieee802_1x_mka_decode_kmd_body -
1854 ieee802_1x_mka_decode_kmd_body(
1855 struct ieee802_1x_mka_participant *participant,
1856 const u8 *mka_msg, size_t msg_len)
1858 struct ieee802_1x_mka_hdr *hdr;
1861 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1862 body_len = get_mka_param_body_len(hdr);
1864 wpa_printf(MSG_ERROR,
1865 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1875 * ieee802_1x_mka_decode_announce_body -
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)
1885 static struct mka_param_body_handler mka_body_handler[] = {
1886 /* basic parameter set */
1888 ieee802_1x_mka_encode_basic_body,
1890 ieee802_1x_mka_basic_body_length,
1891 ieee802_1x_mka_basic_body_present
1894 /* live peer list parameter set */
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
1902 /* potential peer list parameter set */
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
1910 /* sak use parameter set */
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
1918 /* distribute sak parameter set */
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
1926 /* distribute cak parameter set */
1929 ieee802_1x_mka_decode_dist_cak_body,
1934 /* kmd parameter set */
1937 ieee802_1x_mka_decode_kmd_body,
1942 /* announce parameter set */
1945 ieee802_1x_mka_decode_announce_body,
1950 /* icv parameter set */
1952 ieee802_1x_mka_encode_icv_body,
1954 ieee802_1x_mka_get_icv_length,
1955 ieee802_1x_mka_icv_body_present
1961 * ieee802_1x_kay_deinit_data_key -
1963 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1972 dl_list_del(&pkey->list);
1979 * ieee802_1x_kay_generate_new_sak -
1982 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
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;
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
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");
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))) {
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");
2016 conf = os_zalloc(sizeof(*conf));
2018 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2021 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2023 conf->key = os_zalloc(conf->key_len);
2026 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
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);
2036 context = os_zalloc(ctx_len);
2043 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
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);
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));
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);
2067 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2073 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2074 conf->key, conf->key_len);
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;
2083 sa_key = ieee802_1x_kay_init_data_key(conf);
2090 participant->new_key = sa_key;
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);
2103 dl_list_for_each(peer, &participant->live_peers,
2104 struct ieee802_1x_kay_peer, list)
2105 peer->sak_used = FALSE;
2107 participant->kay->dist_kn++;
2108 participant->kay->dist_an++;
2109 if (participant->kay->dist_an > 3)
2110 participant->kay->dist_an = 0;
2112 participant->kay->dist_time = time(NULL);
2122 * ieee802_1x_kay_elect_key_server - elect the key server
2123 * when to elect: whenever the live peers list changes
2126 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
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;
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);
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)
2151 if (peer->key_server_priority <
2152 key_server->key_server_priority) {
2154 } else if (peer->key_server_priority ==
2155 key_server->key_server_priority) {
2156 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
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,
2172 i_is_key_server = TRUE;
2174 } else if (participant->can_be_key_server) {
2175 i_is_key_server = TRUE;
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);
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;
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);
2202 participant->is_key_server = FALSE;
2203 participant->principal = TRUE;
2204 participant->is_elected = TRUE;
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;
2210 participant->principal = FALSE;
2211 participant->is_key_server = FALSE;
2212 participant->is_elected = FALSE;
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
2226 ieee802_1x_kay_decide_macsec_use(
2227 struct ieee802_1x_mka_participant *participant)
2229 struct ieee802_1x_kay *kay = participant->kay;
2230 struct ieee802_1x_kay_peer *peer;
2231 enum macsec_cap less_capability;
2234 if (!participant->is_key_server)
2237 /* key server self is MACsec-desired and requesting MACsec */
2238 if (!kay->macsec_desired) {
2239 participant->advised_desired = FALSE;
2242 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2243 participant->advised_desired = FALSE;
2246 less_capability = kay->macsec_capable;
2248 /* at least one of peers is MACsec-desired and requesting MACsec */
2250 dl_list_for_each(peer, &participant->live_peers,
2251 struct ieee802_1x_kay_peer, list) {
2252 if (!peer->macsec_desired)
2255 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2258 less_capability = (less_capability < peer->macsec_capability) ?
2259 less_capability : peer->macsec_capability;
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);
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;
2286 ieee802_1x_cp_connect_authenticated(kay->cp);
2287 ieee802_1x_cp_sm_step(kay->cp);
2293 static const u8 pae_group_addr[ETH_ALEN] = {
2294 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2299 * ieee802_1x_kay_encode_mkpdu -
2302 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2303 struct wpabuf *pbuf)
2306 struct ieee8023_hdr *ether_hdr;
2307 struct ieee802_1x_hdr *eapol_hdr;
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);
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);
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))
2332 * ieee802_1x_participant_send_mkpdu -
2335 ieee802_1x_participant_send_mkpdu(
2336 struct ieee802_1x_mka_participant *participant)
2339 struct ieee802_1x_kay *kay = participant->kay;
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);
2351 buf = wpabuf_alloc(length);
2353 wpa_printf(MSG_ERROR, "KaY: out of memory");
2357 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2358 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2362 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2366 participant->active = TRUE;
2372 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2374 * ieee802_1x_participant_timer -
2376 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
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);
2383 struct receive_sc *rxsc, *pre_rxsc;
2384 struct transmit_sa *txsa, *pre_txsa;
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;
2393 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
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;
2406 ieee802_1x_kay_delete_mka(kay,
2411 participant->mka_life = 0;
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,
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(
2432 dl_list_del(&peer->list);
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;
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);
2462 ieee802_1x_cp_connect_authenticated(kay->cp);
2463 ieee802_1x_cp_sm_step(kay->cp);
2465 ieee802_1x_kay_elect_key_server(participant);
2466 ieee802_1x_kay_decide_macsec_use(participant);
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,
2476 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2477 dl_list_del(&peer->list);
2482 if (participant->new_sak) {
2483 if (!ieee802_1x_kay_generate_new_sak(participant))
2484 participant->to_dist_sak = TRUE;
2486 participant->new_sak = FALSE;
2489 if (participant->retry_count < MAX_RETRY_CNT) {
2490 ieee802_1x_participant_send_mkpdu(participant);
2491 participant->retry_count++;
2494 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2495 ieee802_1x_participant_timer,
2501 * ieee802_1x_kay_init_transmit_sa -
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)
2507 struct transmit_sa *psa;
2509 key->tx_latest = TRUE;
2510 key->rx_latest = TRUE;
2512 psa = os_zalloc(sizeof(*psa));
2514 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2518 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2519 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2520 psa->confidentiality = TRUE;
2522 psa->confidentiality = FALSE;
2526 psa->next_pn = next_PN;
2529 os_get_time(&psa->created_time);
2530 psa->in_use = FALSE;
2532 dl_list_add(&psc->sa_list, &psa->list);
2533 wpa_printf(MSG_DEBUG,
2534 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2535 (int) an, next_PN, psc->channel);
2542 * ieee802_1x_kay_deinit_transmit_sa -
2544 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2547 wpa_printf(MSG_DEBUG,
2548 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2549 psa->an, psa->sc->channel);
2550 dl_list_del(&psa->list);
2556 * init_transmit_sc -
2558 static struct transmit_sc *
2559 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2562 struct transmit_sc *psc;
2564 psc = os_zalloc(sizeof(*psc));
2566 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2569 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2570 psc->channel = channel;
2572 os_get_time(&psc->created_time);
2573 psc->transmitting = FALSE;
2574 psc->encoding_sa = FALSE;
2575 psc->enciphering_sa = FALSE;
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));
2586 * ieee802_1x_kay_deinit_transmit_sc -
2589 ieee802_1x_kay_deinit_transmit_sc(
2590 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2592 struct transmit_sa *psa, *tmp;
2594 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2596 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2598 secy_disable_transmit_sa(participant->kay, psa);
2599 ieee802_1x_kay_deinit_transmit_sa(psa);
2606 /****************** Interface between CP and KAY *********************/
2608 * ieee802_1x_kay_set_latest_sa_attr -
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)
2614 struct ieee802_1x_mka_participant *principal;
2616 principal = ieee802_1x_kay_get_principal_participant(kay);
2621 os_memset(&principal->lki, 0, sizeof(principal->lki));
2623 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2625 principal->lan = lan;
2626 principal->ltx = ltx;
2627 principal->lrx = lrx;
2632 kay->ltx_kn = lki->kn;
2633 kay->lrx_kn = lki->kn;
2643 * ieee802_1x_kay_set_old_sa_attr -
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)
2649 struct ieee802_1x_mka_participant *principal;
2651 principal = ieee802_1x_kay_get_principal_participant(kay);
2656 os_memset(&principal->oki, 0, sizeof(principal->oki));
2658 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2660 principal->oan = oan;
2661 principal->otx = otx;
2662 principal->orx = orx;
2668 kay->otx_kn = oki->kn;
2669 kay->orx_kn = oki->kn;
2679 * ieee802_1x_kay_create_sas -
2681 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2682 struct ieee802_1x_mka_ki *lki)
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;
2690 principal = ieee802_1x_kay_get_principal_participant(kay);
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;
2702 sa_key->rx_latest = FALSE;
2703 sa_key->tx_latest = FALSE;
2707 wpa_printf(MSG_ERROR, "lki related sak not found");
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,
2717 secy_create_receive_sa(kay, rxsa);
2720 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2725 secy_create_transmit_sa(kay, txsa);
2734 * ieee802_1x_kay_delete_sas -
2736 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2737 struct ieee802_1x_mka_ki *ki)
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;
2745 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2746 principal = ieee802_1x_kay_get_principal_participant(kay);
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);
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);
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);
2777 if (principal->new_key == sa_key)
2778 principal->new_key = NULL;
2786 * ieee802_1x_kay_enable_tx_sas -
2788 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2789 struct ieee802_1x_mka_ki *lki)
2791 struct ieee802_1x_mka_participant *principal;
2792 struct transmit_sa *txsa;
2794 principal = ieee802_1x_kay_get_principal_participant(kay);
2798 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
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);
2814 * ieee802_1x_kay_enable_rx_sas -
2816 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2817 struct ieee802_1x_mka_ki *lki)
2819 struct ieee802_1x_mka_participant *principal;
2820 struct receive_sa *rxsa;
2821 struct receive_sc *rxsc;
2823 principal = ieee802_1x_kay_get_principal_participant(kay);
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)
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);
2845 * ieee802_1x_kay_enable_new_info -
2847 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2849 struct ieee802_1x_mka_participant *principal;
2851 principal = ieee802_1x_kay_get_principal_participant(kay);
2855 if (principal->retry_count < MAX_RETRY_CNT) {
2856 ieee802_1x_participant_send_mkpdu(principal);
2857 principal->retry_count++;
2865 * ieee802_1x_kay_cp_conf -
2867 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2868 struct ieee802_1x_cp_conf *pconf)
2870 pconf->protect = kay->macsec_protect;
2871 pconf->replay_protect = kay->macsec_replay_protect;
2872 pconf->validate = kay->macsec_validate;
2879 * ieee802_1x_kay_alloc_cp_sm -
2881 static struct ieee802_1x_cp_sm *
2882 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2884 struct ieee802_1x_cp_conf conf;
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;
2892 return ieee802_1x_cp_sm_init(kay, &conf);
2897 * ieee802_1x_kay_mkpdu_sanity_check -
2898 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2900 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2901 const u8 *buf, size_t len)
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;
2908 struct ieee802_1x_mka_participant *participant;
2910 u8 icv[MAX_ICV_LEN];
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);
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");
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");
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");
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 (%d bytes) is less than the Basic Parameter Set Header Length (%d bytes) + the Basic Parameter Set Body Length (%d bytes) + %d bytes of ICV",
2944 (int) mka_msg_len, (int) MKA_HDR_LEN,
2945 (int) body_len, DEFAULT_ICV_LEN);
2949 /* CKN should be owned by I */
2950 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2952 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
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");
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.
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");
2976 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2980 if (os_memcmp_const(msg_icv, icv,
2981 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2983 wpa_printf(MSG_ERROR,
2984 "KaY: Computed ICV is not equal to Received ICV");
2988 wpa_printf(MSG_ERROR, "KaY: No ICV");
2997 * ieee802_1x_kay_decode_mkpdu -
2999 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
3000 const u8 *buf, size_t len)
3002 struct ieee802_1x_mka_participant *participant;
3003 struct ieee802_1x_mka_hdr *hdr;
3009 Boolean my_included;
3010 Boolean handled[256];
3012 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
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);
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;
3029 /* check i am in the peer's peer list */
3030 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3032 /* accept the peer as live peer */
3033 if (!ieee802_1x_kay_is_in_peer(
3035 participant->current_peer_id.mi)) {
3036 if (!ieee802_1x_kay_create_live_peer(
3038 participant->current_peer_id.mi,
3040 participant->current_peer_id.mn)))
3042 ieee802_1x_kay_elect_key_server(participant);
3043 ieee802_1x_kay_decide_macsec_use(participant);
3045 if (ieee802_1x_kay_is_in_potential_peer(
3046 participant, participant->current_peer_id.mi)) {
3047 if (!ieee802_1x_kay_move_live_peer(
3049 participant->current_peer_id.mi,
3050 be_to_host32(participant->
3051 current_peer_id.mn)))
3053 ieee802_1x_kay_elect_key_server(participant);
3054 ieee802_1x_kay_decide_macsec_use(participant);
3059 * Handle other parameter set than basic parameter set.
3060 * Each parameter set should be present only once.
3062 for (i = 0; i < 256; i++)
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);
3071 if (body_type == MKA_ICV_INDICATOR)
3074 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3075 wpa_printf(MSG_ERROR,
3076 "KaY: MKA Peer Packet Body Length (%d bytes) is less than the Parameter Set Header Length (%d bytes) + the Parameter Set Body Length (%d bytes) + %d bytes of ICV",
3077 (int) left_len, (int) MKA_HDR_LEN,
3078 (int) body_len, DEFAULT_ICV_LEN);
3082 if (handled[body_type])
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);
3091 wpa_printf(MSG_ERROR,
3092 "The type %d not supported in this MKA version %d",
3093 body_type, MKA_VERSION_ID);
3097 pos += body_len + MKA_HDR_LEN;
3098 left_len -= body_len + MKA_HDR_LEN;
3102 participant->retry_count = 0;
3103 participant->active = TRUE;
3110 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3113 struct ieee802_1x_kay *kay = ctx;
3114 struct ieee8023_hdr *eth_hdr;
3115 struct ieee802_1x_hdr *eapol_hdr;
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);
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));
3134 if (eapol_hdr->version < EAPOL_VERSION) {
3135 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3136 eapol_hdr->version);
3139 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3140 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
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");
3149 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3154 * ieee802_1x_kay_init -
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)
3160 struct ieee802_1x_kay *kay;
3162 kay = os_zalloc(sizeof(*kay));
3164 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3171 kay->active = FALSE;
3173 kay->authenticated = FALSE;
3174 kay->secured = FALSE;
3175 kay->failed = FALSE;
3176 kay->policy = policy;
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;
3183 /* While actor acts as a key server, shall distribute sakey */
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;
3193 os_memcpy(kay->algo_agility, mka_algo_agility,
3194 sizeof(kay->algo_agility));
3196 dl_list_init(&kay->participant_list);
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;
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;
3216 wpa_printf(MSG_DEBUG, "KaY: state machine created");
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);
3222 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3225 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3226 if (kay->cp == NULL) {
3227 ieee802_1x_kay_deinit(kay);
3231 if (policy == DO_NOT_SECURE) {
3232 ieee802_1x_cp_connect_authenticated(kay->cp);
3233 ieee802_1x_cp_sm_step(kay->cp);
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);
3250 * ieee802_1x_kay_deinit -
3253 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3255 struct ieee802_1x_mka_participant *participant;
3260 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3262 while (!dl_list_empty(&kay->participant_list)) {
3263 participant = dl_list_entry(kay->participant_list.next,
3264 struct ieee802_1x_mka_participant,
3266 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3269 ieee802_1x_cp_sm_deinit(kay->cp);
3270 secy_deinit_macsec(kay);
3273 l2_packet_deinit(kay->l2_mka);
3283 * ieee802_1x_kay_create_mka -
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)
3290 struct ieee802_1x_mka_participant *participant;
3293 if (!kay || !ckn || !cak) {
3294 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3298 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3299 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3302 if (ckn->len > MAX_CKN_LEN) {
3303 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3307 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3311 participant = os_zalloc(sizeof(*participant));
3313 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
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);
3322 participant->cak_life = life + time(NULL);
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;
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;
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;
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;
3352 participant->cached = FALSE;
3354 participant->active = FALSE;
3355 participant->participant = FALSE;
3356 participant->retain = FALSE;
3357 participant->activate = DEFAULT;
3359 if (participant->is_key_server)
3360 participant->principal = TRUE;
3362 dl_list_init(&participant->live_peers);
3363 dl_list_init(&participant->potential_peers);
3365 participant->retry_count = 0;
3366 participant->kay = kay;
3368 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3370 participant->mn = 0;
3372 participant->lrx = FALSE;
3373 participant->ltx = FALSE;
3374 participant->orx = FALSE;
3375 participant->otx = FALSE;
3376 participant->to_dist_sak = FALSE;
3377 participant->to_use_sak = FALSE;
3378 participant->new_sak = FALSE;
3379 dl_list_init(&participant->sak_list);
3380 participant->new_key = NULL;
3381 dl_list_init(&participant->rxsc_list);
3382 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3384 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3385 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3386 kay->macsec_replay_window);
3387 secy_create_transmit_sc(kay, participant->txsc);
3389 /* to derive KEK from CAK and CKN */
3390 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3391 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3392 participant->ckn.name,
3393 participant->ckn.len,
3394 participant->kek.key)) {
3395 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3398 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3399 participant->kek.key, participant->kek.len);
3401 /* to derive ICK from CAK and CKN */
3402 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3403 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3404 participant->ckn.name,
3405 participant->ckn.len,
3406 participant->ick.key)) {
3407 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3410 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3411 participant->ick.key, participant->ick.len);
3413 dl_list_add(&kay->participant_list, &participant->list);
3414 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3415 ckn->name, ckn->len);
3417 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3418 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3420 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3426 os_free(participant);
3432 * ieee802_1x_kay_delete_mka -
3435 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3437 struct ieee802_1x_mka_participant *participant;
3438 struct ieee802_1x_kay_peer *peer;
3439 struct data_key *sak;
3440 struct receive_sc *rxsc;
3445 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3447 /* get the participant */
3448 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3450 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3451 ckn->name, ckn->len);
3455 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3456 dl_list_del(&participant->list);
3458 /* remove live peer */
3459 while (!dl_list_empty(&participant->live_peers)) {
3460 peer = dl_list_entry(participant->live_peers.next,
3461 struct ieee802_1x_kay_peer, list);
3462 dl_list_del(&peer->list);
3466 /* remove potential peer */
3467 while (!dl_list_empty(&participant->potential_peers)) {
3468 peer = dl_list_entry(participant->potential_peers.next,
3469 struct ieee802_1x_kay_peer, list);
3470 dl_list_del(&peer->list);
3475 while (!dl_list_empty(&participant->sak_list)) {
3476 sak = dl_list_entry(participant->sak_list.next,
3477 struct data_key, list);
3478 dl_list_del(&sak->list);
3482 while (!dl_list_empty(&participant->rxsc_list)) {
3483 rxsc = dl_list_entry(participant->rxsc_list.next,
3484 struct receive_sc, list);
3485 secy_delete_receive_sc(kay, rxsc);
3486 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3488 secy_delete_transmit_sc(kay, participant->txsc);
3489 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3491 os_memset(&participant->cak, 0, sizeof(participant->cak));
3492 os_memset(&participant->kek, 0, sizeof(participant->kek));
3493 os_memset(&participant->ick, 0, sizeof(participant->ick));
3494 os_free(participant);
3499 * ieee802_1x_kay_mka_participate -
3501 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3502 struct mka_key_name *ckn,
3505 struct ieee802_1x_mka_participant *participant;
3510 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3514 participant->active = status;
3519 * ieee802_1x_kay_new_sak -
3522 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3524 struct ieee802_1x_mka_participant *participant;
3529 participant = ieee802_1x_kay_get_principal_participant(kay);
3533 participant->new_sak = TRUE;
3534 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3541 * ieee802_1x_kay_change_cipher_suite -
3544 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3546 struct ieee802_1x_mka_participant *participant;
3551 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3552 wpa_printf(MSG_ERROR,
3553 "KaY: Configured cipher suite index is out of range");
3556 if (kay->macsec_csindex == cs_index)
3560 kay->macsec_desired = FALSE;
3562 kay->macsec_csindex = cs_index;
3563 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3565 participant = ieee802_1x_kay_get_principal_participant(kay);
3567 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3568 participant->new_sak = TRUE;