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 /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
33 #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
34 static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
36 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
37 static struct macsec_ciphersuite cipher_suite_tbl[] = {
42 MACSEC_CAP_INTEG_AND_CONF_0_30_50,
48 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
49 #define DEFAULT_CS_INDEX 0
51 static struct mka_alg mka_alg_tbl[] = {
53 MKA_ALGO_AGILITY_2009,
54 /* 128-bit CAK, KEK, ICK, ICV */
56 ieee802_1x_cak_128bits_aes_cmac,
57 ieee802_1x_ckn_128bits_aes_cmac,
58 ieee802_1x_kek_128bits_aes_cmac,
59 ieee802_1x_ick_128bits_aes_cmac,
60 ieee802_1x_icv_128bits_aes_cmac,
65 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
68 static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
69 struct ieee802_1x_mka_ki *ki2)
71 return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
76 struct mka_param_body_handler {
77 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
79 int (*body_rx)(struct ieee802_1x_mka_participant *participant,
80 const u8 *mka_msg, size_t msg_len);
81 int (*body_length)(struct ieee802_1x_mka_participant *participant);
82 Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
86 static void set_mka_param_body_len(void *body, unsigned int len)
88 struct ieee802_1x_mka_hdr *hdr = body;
89 hdr->length = (len >> 8) & 0x0f;
90 hdr->length1 = len & 0xff;
94 static unsigned int get_mka_param_body_len(const void *body)
96 const struct ieee802_1x_mka_hdr *hdr = body;
97 return (hdr->length << 8) | hdr->length1;
101 static u8 get_mka_param_body_type(const void *body)
103 const struct ieee802_1x_mka_hdr *hdr = body;
109 * ieee802_1x_mka_dump_basic_body -
112 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
119 body_len = get_mka_param_body_len(body);
120 wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
121 wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
122 wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
123 wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
124 wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
125 wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
126 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
127 wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
128 MAC2STR(body->actor_sci.addr));
129 wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
130 be_to_host16(body->actor_sci.port));
131 wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
132 body->actor_mi, sizeof(body->actor_mi));
133 wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
134 be_to_host32(body->actor_mn));
135 wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
136 body->algo_agility, sizeof(body->algo_agility));
137 wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
138 body_len + MKA_HDR_LEN - sizeof(*body));
143 * ieee802_1x_mka_dump_peer_body -
146 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
156 body_len = get_mka_param_body_len(body);
157 if (body->type == MKA_LIVE_PEER_LIST) {
158 wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
159 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
160 } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
161 wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
162 wpa_printf(MSG_DEBUG, "\tBody Length...: %d", (int) body_len);
165 for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
167 os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
168 wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
169 wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
175 * ieee802_1x_mka_dump_dist_sak_body -
178 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
185 body_len = get_mka_param_body_len(body);
186 wpa_printf(MSG_INFO, "*** Distributed SAK ***");
187 wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
188 wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
189 body->confid_offset);
190 wpa_printf(MSG_INFO, "\tBody Length...........: %d", (int) body_len);
194 wpa_printf(MSG_INFO, "\tKey Number............: %d",
195 be_to_host32(body->kn));
196 wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
200 static const char * yes_no(int val)
202 return val ? "Yes" : "No";
207 * ieee802_1x_mka_dump_sak_use_body -
210 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
217 body_len = get_mka_param_body_len(body);
218 wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
219 wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
220 wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
221 wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
222 wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
223 wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
224 wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
225 wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
226 wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
227 wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
228 yes_no(body->delay_protect));
229 wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
233 wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
234 body->lsrv_mi, sizeof(body->lsrv_mi));
235 wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
236 be_to_host32(body->lkn));
237 wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
238 be_to_host32(body->llpn));
239 wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
240 body->osrv_mi, sizeof(body->osrv_mi));
241 wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
242 be_to_host32(body->okn));
243 wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
244 be_to_host32(body->olpn));
249 * ieee802_1x_kay_get_participant -
251 static struct ieee802_1x_mka_participant *
252 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
254 struct ieee802_1x_mka_participant *participant;
256 dl_list_for_each(participant, &kay->participant_list,
257 struct ieee802_1x_mka_participant, list) {
258 if (os_memcmp(participant->ckn.name, ckn,
259 participant->ckn.len) == 0)
263 wpa_printf(MSG_DEBUG, "KaY: participant is not found");
270 * ieee802_1x_kay_get_principal_participant -
272 static struct ieee802_1x_mka_participant *
273 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
275 struct ieee802_1x_mka_participant *participant;
277 dl_list_for_each(participant, &kay->participant_list,
278 struct ieee802_1x_mka_participant, list) {
279 if (participant->principal)
283 wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
288 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
291 struct ieee802_1x_kay_peer *peer;
293 dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
294 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
303 * ieee802_1x_kay_is_in_potential_peer
306 ieee802_1x_kay_is_in_potential_peer(
307 struct ieee802_1x_mka_participant *participant, const u8 *mi)
309 return get_peer_mi(&participant->potential_peers, mi) != NULL;
314 * ieee802_1x_kay_is_in_live_peer
317 ieee802_1x_kay_is_in_live_peer(
318 struct ieee802_1x_mka_participant *participant, const u8 *mi)
320 return get_peer_mi(&participant->live_peers, mi) != NULL;
325 * ieee802_1x_kay_is_in_peer
328 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
331 return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
332 ieee802_1x_kay_is_in_potential_peer(participant, mi);
337 * ieee802_1x_kay_get_peer
339 static struct ieee802_1x_kay_peer *
340 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
343 struct ieee802_1x_kay_peer *peer;
345 peer = get_peer_mi(&participant->live_peers, mi);
349 return get_peer_mi(&participant->potential_peers, mi);
354 * ieee802_1x_kay_get_live_peer
356 static struct ieee802_1x_kay_peer *
357 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
360 return get_peer_mi(&participant->live_peers, mi);
365 * ieee802_1x_kay_get_cipher_suite
367 static struct macsec_ciphersuite *
368 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
373 for (i = 0; i < CS_TABLE_SIZE; i++) {
374 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
377 if (i >= CS_TABLE_SIZE)
380 return &cipher_suite_tbl[i];
384 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
385 const struct ieee802_1x_mka_sci *b)
387 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
392 * ieee802_1x_kay_get_peer_sci
394 static struct ieee802_1x_kay_peer *
395 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
396 const struct ieee802_1x_mka_sci *sci)
398 struct ieee802_1x_kay_peer *peer;
400 dl_list_for_each(peer, &participant->live_peers,
401 struct ieee802_1x_kay_peer, list) {
402 if (sci_equal(&peer->sci, sci))
406 dl_list_for_each(peer, &participant->potential_peers,
407 struct ieee802_1x_kay_peer, list) {
408 if (sci_equal(&peer->sci, sci))
417 * ieee802_1x_kay_init_receive_sa -
419 static struct receive_sa *
420 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
421 struct data_key *key)
423 struct receive_sa *psa;
428 psa = os_zalloc(sizeof(*psa));
430 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
435 psa->lowest_pn = lowest_pn;
436 psa->next_pn = lowest_pn;
440 os_get_time(&psa->created_time);
443 dl_list_add(&psc->sa_list, &psa->list);
444 wpa_printf(MSG_DEBUG,
445 "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
446 (int) an, lowest_pn, psc->channel);
453 * ieee802_1x_kay_deinit_receive_sa -
455 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
458 wpa_printf(MSG_DEBUG,
459 "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
460 psa->an, psa->sc->channel);
461 dl_list_del(&psa->list);
467 * ieee802_1x_kay_init_receive_sc -
469 static struct receive_sc *
470 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
473 struct receive_sc *psc;
478 psc = os_zalloc(sizeof(*psc));
480 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
484 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
485 psc->channel = channel;
487 os_get_time(&psc->created_time);
488 psc->receiving = FALSE;
490 dl_list_init(&psc->sa_list);
491 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
492 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
499 * ieee802_1x_kay_deinit_receive_sc -
502 ieee802_1x_kay_deinit_receive_sc(
503 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
505 struct receive_sa *psa, *pre_sa;
507 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
509 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
511 secy_disable_receive_sa(participant->kay, psa);
512 ieee802_1x_kay_deinit_receive_sa(psa);
514 dl_list_del(&psc->list);
520 * ieee802_1x_kay_create_live_peer
522 static struct ieee802_1x_kay_peer *
523 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
526 struct ieee802_1x_kay_peer *peer;
527 struct receive_sc *rxsc;
530 peer = os_zalloc(sizeof(*peer));
532 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
536 os_memcpy(peer->mi, mi, MI_LEN);
538 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
539 peer->sak_used = FALSE;
540 os_memcpy(&peer->sci, &participant->current_peer_sci,
543 secy_get_available_receive_sc(participant->kay, &sc_ch);
545 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
551 dl_list_add(&participant->live_peers, &peer->list);
552 dl_list_add(&participant->rxsc_list, &rxsc->list);
553 secy_create_receive_sc(participant->kay, rxsc);
555 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
556 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
557 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
558 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
559 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
566 * ieee802_1x_kay_create_potential_peer
568 static struct ieee802_1x_kay_peer *
569 ieee802_1x_kay_create_potential_peer(
570 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
572 struct ieee802_1x_kay_peer *peer;
574 peer = os_zalloc(sizeof(*peer));
576 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
580 os_memcpy(peer->mi, mi, MI_LEN);
582 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
583 peer->sak_used = FALSE;
585 dl_list_add(&participant->potential_peers, &peer->list);
587 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
588 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
589 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
590 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
591 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
598 * ieee802_1x_kay_move_live_peer
600 static struct ieee802_1x_kay_peer *
601 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
604 struct ieee802_1x_kay_peer *peer;
605 struct receive_sc *rxsc;
608 dl_list_for_each(peer, &participant->potential_peers,
609 struct ieee802_1x_kay_peer, list) {
610 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
614 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
619 os_memcpy(&peer->sci, &participant->current_peer_sci,
622 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
624 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
625 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
626 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
627 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
628 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
630 dl_list_del(&peer->list);
631 dl_list_add_tail(&participant->live_peers, &peer->list);
633 secy_get_available_receive_sc(participant->kay, &sc_ch);
635 dl_list_add(&participant->rxsc_list, &rxsc->list);
636 secy_create_receive_sc(participant->kay, rxsc);
644 * ieee802_1x_mka_basic_body_present -
647 ieee802_1x_mka_basic_body_present(
648 struct ieee802_1x_mka_participant *participant)
655 * ieee802_1x_mka_basic_body_length -
658 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
662 length = sizeof(struct ieee802_1x_mka_basic_body);
663 length += participant->ckn.len;
664 return (length + 0x3) & ~0x3;
669 * ieee802_1x_mka_encode_basic_body
672 ieee802_1x_mka_encode_basic_body(
673 struct ieee802_1x_mka_participant *participant,
676 struct ieee802_1x_mka_basic_body *body;
677 struct ieee802_1x_kay *kay = participant->kay;
678 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
680 body = wpabuf_put(buf, length);
682 body->version = kay->mka_version;
683 body->priority = kay->actor_priority;
684 if (participant->is_elected)
685 body->key_server = participant->is_key_server;
687 body->key_server = participant->can_be_key_server;
689 body->macsec_desired = kay->macsec_desired;
690 body->macsec_capability = kay->macsec_capable;
691 set_mka_param_body_len(body, length - MKA_HDR_LEN);
693 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
694 sizeof(kay->actor_sci.addr));
695 body->actor_sci.port = kay->actor_sci.port;
697 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
698 participant->mn = participant->mn + 1;
699 body->actor_mn = host_to_be32(participant->mn);
700 os_memcpy(body->algo_agility, participant->kay->algo_agility,
701 sizeof(body->algo_agility));
703 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
705 ieee802_1x_mka_dump_basic_body(body);
712 * ieee802_1x_mka_decode_basic_body -
714 static struct ieee802_1x_mka_participant *
715 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
718 struct ieee802_1x_mka_participant *participant;
719 const struct ieee802_1x_mka_basic_body *body;
720 struct ieee802_1x_kay_peer *peer;
722 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
724 if (body->version > MKA_VERSION_ID) {
725 wpa_printf(MSG_DEBUG,
726 "KaY: peer's version(%d) greater than mka current version(%d)",
727 body->version, MKA_VERSION_ID);
729 if (kay->is_obliged_key_server && body->key_server) {
730 wpa_printf(MSG_DEBUG, "I must be as key server");
734 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
736 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
740 /* If the peer's MI is my MI, I will choose new MI */
741 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
742 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
747 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
748 participant->current_peer_id.mn = body->actor_mn;
749 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
750 sizeof(participant->current_peer_sci.addr));
751 participant->current_peer_sci.port = body->actor_sci.port;
754 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
756 /* Check duplicated SCI */
757 /* TODO: What policy should be applied to detect duplicated SCI
758 * is active attacker or a valid peer whose MI is be changed?
760 peer = ieee802_1x_kay_get_peer_sci(participant,
763 wpa_printf(MSG_WARNING,
764 "KaY: duplicated SCI detected, Maybe active attacker");
765 dl_list_del(&peer->list);
769 peer = ieee802_1x_kay_create_potential_peer(
770 participant, body->actor_mi,
771 be_to_host32(body->actor_mn));
775 peer->macsec_desired = body->macsec_desired;
776 peer->macsec_capability = body->macsec_capability;
777 peer->is_key_server = (Boolean) body->key_server;
778 peer->key_server_priority = body->priority;
779 } else if (peer->mn < be_to_host32(body->actor_mn)) {
780 peer->mn = be_to_host32(body->actor_mn);
781 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
782 peer->macsec_desired = body->macsec_desired;
783 peer->macsec_capability = body->macsec_capability;
784 peer->is_key_server = (Boolean) body->key_server;
785 peer->key_server_priority = body->priority;
787 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
796 * ieee802_1x_mka_live_peer_body_present
799 ieee802_1x_mka_live_peer_body_present(
800 struct ieee802_1x_mka_participant *participant)
802 return !dl_list_empty(&participant->live_peers);
807 * ieee802_1x_kay_get_live_peer_length
810 ieee802_1x_mka_get_live_peer_length(
811 struct ieee802_1x_mka_participant *participant)
813 int len = MKA_HDR_LEN;
814 struct ieee802_1x_kay_peer *peer;
816 dl_list_for_each(peer, &participant->live_peers,
817 struct ieee802_1x_kay_peer, list)
818 len += sizeof(struct ieee802_1x_mka_peer_id);
820 return (len + 0x3) & ~0x3;
825 * ieee802_1x_mka_encode_live_peer_body -
828 ieee802_1x_mka_encode_live_peer_body(
829 struct ieee802_1x_mka_participant *participant,
832 struct ieee802_1x_mka_peer_body *body;
833 struct ieee802_1x_kay_peer *peer;
835 struct ieee802_1x_mka_peer_id *body_peer;
837 length = ieee802_1x_mka_get_live_peer_length(participant);
838 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
840 body->type = MKA_LIVE_PEER_LIST;
841 set_mka_param_body_len(body, length - MKA_HDR_LEN);
843 dl_list_for_each(peer, &participant->live_peers,
844 struct ieee802_1x_kay_peer, list) {
845 body_peer = wpabuf_put(buf,
846 sizeof(struct ieee802_1x_mka_peer_id));
847 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
848 body_peer->mn = host_to_be32(peer->mn);
852 ieee802_1x_mka_dump_peer_body(body);
857 * ieee802_1x_mka_potential_peer_body_present
860 ieee802_1x_mka_potential_peer_body_present(
861 struct ieee802_1x_mka_participant *participant)
863 return !dl_list_empty(&participant->potential_peers);
868 * ieee802_1x_kay_get_potential_peer_length
871 ieee802_1x_mka_get_potential_peer_length(
872 struct ieee802_1x_mka_participant *participant)
874 int len = MKA_HDR_LEN;
875 struct ieee802_1x_kay_peer *peer;
877 dl_list_for_each(peer, &participant->potential_peers,
878 struct ieee802_1x_kay_peer, list)
879 len += sizeof(struct ieee802_1x_mka_peer_id);
881 return (len + 0x3) & ~0x3;
886 * ieee802_1x_mka_encode_potential_peer_body -
889 ieee802_1x_mka_encode_potential_peer_body(
890 struct ieee802_1x_mka_participant *participant,
893 struct ieee802_1x_mka_peer_body *body;
894 struct ieee802_1x_kay_peer *peer;
896 struct ieee802_1x_mka_peer_id *body_peer;
898 length = ieee802_1x_mka_get_potential_peer_length(participant);
899 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
901 body->type = MKA_POTENTIAL_PEER_LIST;
902 set_mka_param_body_len(body, length - MKA_HDR_LEN);
904 dl_list_for_each(peer, &participant->potential_peers,
905 struct ieee802_1x_kay_peer, list) {
906 body_peer = wpabuf_put(buf,
907 sizeof(struct ieee802_1x_mka_peer_id));
908 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
909 body_peer->mn = host_to_be32(peer->mn);
913 ieee802_1x_mka_dump_peer_body(body);
919 * ieee802_1x_mka_i_in_peerlist -
922 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
923 const u8 *mka_msg, size_t msg_len)
925 Boolean included = FALSE;
926 struct ieee802_1x_mka_hdr *hdr;
938 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
939 hdr = (struct ieee802_1x_mka_hdr *) pos;
940 body_len = get_mka_param_body_len(hdr);
941 body_type = get_mka_param_body_type(hdr);
943 if (body_type != MKA_LIVE_PEER_LIST &&
944 body_type != MKA_POTENTIAL_PEER_LIST)
947 ieee802_1x_mka_dump_peer_body(
948 (struct ieee802_1x_mka_peer_body *)pos);
950 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
951 wpa_printf(MSG_ERROR,
952 "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",
953 (int) left_len, (int) MKA_HDR_LEN,
954 (int) body_len, DEFAULT_ICV_LEN);
958 if ((body_len % 16) != 0) {
959 wpa_printf(MSG_ERROR,
960 "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
965 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
966 peer_mi = MKA_HDR_LEN + pos + i;
967 os_memcpy(&_peer_mn, peer_mi + MI_LEN,
969 peer_mn = be_to_host32(_peer_mn);
970 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
971 peer_mn == participant->mn) {
981 left_len -= body_len + MKA_HDR_LEN;
982 pos += body_len + MKA_HDR_LEN;
990 * ieee802_1x_mka_decode_live_peer_body -
992 static int ieee802_1x_mka_decode_live_peer_body(
993 struct ieee802_1x_mka_participant *participant,
994 const u8 *peer_msg, size_t msg_len)
996 const struct ieee802_1x_mka_hdr *hdr;
997 struct ieee802_1x_kay_peer *peer;
1003 Boolean is_included;
1005 is_included = ieee802_1x_kay_is_in_live_peer(
1006 participant, participant->current_peer_id.mi);
1008 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1009 body_len = get_mka_param_body_len(hdr);
1010 if (body_len % 16 != 0) {
1011 wpa_printf(MSG_ERROR,
1012 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1017 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1018 peer_mi = MKA_HDR_LEN + peer_msg + i;
1019 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1020 peer_mn = be_to_host32(_peer_mn);
1023 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1024 /* My message id is used by other participant */
1025 if (peer_mn > participant->mn) {
1026 if (os_get_random(participant->mi,
1027 sizeof(participant->mi)) < 0)
1028 wpa_printf(MSG_DEBUG,
1029 "KaY: Could not update mi");
1030 participant->mn = 0;
1037 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1040 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1042 if (!ieee802_1x_kay_create_potential_peer(
1043 participant, peer_mi, peer_mn)) {
1054 * ieee802_1x_mka_decode_potential_peer_body -
1057 ieee802_1x_mka_decode_potential_peer_body(
1058 struct ieee802_1x_mka_participant *participant,
1059 const u8 *peer_msg, size_t msg_len)
1061 struct ieee802_1x_mka_hdr *hdr;
1068 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1069 body_len = get_mka_param_body_len(hdr);
1070 if (body_len % 16 != 0) {
1071 wpa_printf(MSG_ERROR,
1072 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1077 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1078 peer_mi = MKA_HDR_LEN + peer_msg + i;
1079 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1080 peer_mn = be_to_host32(_peer_mn);
1083 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1084 /* My message id is used by other participant */
1085 if (peer_mn > participant->mn) {
1086 if (os_get_random(participant->mi,
1087 sizeof(participant->mi)) < 0)
1088 wpa_printf(MSG_DEBUG,
1089 "KaY: Could not update mi");
1090 participant->mn = 0;
1101 * ieee802_1x_mka_sak_use_body_present
1104 ieee802_1x_mka_sak_use_body_present(
1105 struct ieee802_1x_mka_participant *participant)
1107 if (participant->to_use_sak)
1115 * ieee802_1x_mka_get_sak_use_length
1118 ieee802_1x_mka_get_sak_use_length(
1119 struct ieee802_1x_mka_participant *participant)
1121 int length = MKA_HDR_LEN;
1123 if (participant->kay->macsec_desired && participant->advised_desired)
1124 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1126 length = MKA_HDR_LEN;
1128 length = (length + 0x3) & ~0x3;
1138 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1139 struct ieee802_1x_mka_ki *ki)
1141 struct receive_sa *rxsa;
1142 struct receive_sc *rxsc;
1145 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1146 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1148 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1149 secy_get_receive_lowest_pn(principal->kay,
1152 lpn = lpn > rxsa->lowest_pn ?
1153 lpn : rxsa->lowest_pn;
1167 * ieee802_1x_mka_encode_sak_use_body -
1170 ieee802_1x_mka_encode_sak_use_body(
1171 struct ieee802_1x_mka_participant *participant,
1174 struct ieee802_1x_mka_sak_use_body *body;
1175 unsigned int length;
1178 length = ieee802_1x_mka_get_sak_use_length(participant);
1179 body = wpabuf_put(buf, length);
1181 body->type = MKA_SAK_USE;
1182 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1184 if (length == MKA_HDR_LEN) {
1190 body->delay_protect = FALSE;
1194 /* data protect, lowest accept packet number */
1195 body->delay_protect = participant->kay->macsec_replay_protect;
1196 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1197 if (pn > participant->kay->pn_exhaustion) {
1198 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1199 if (participant->is_key_server)
1200 participant->new_sak = TRUE;
1203 body->llpn = host_to_be32(pn);
1204 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1205 body->olpn = host_to_be32(pn);
1207 /* plain tx, plain rx */
1208 if (participant->kay->macsec_protect)
1213 if (participant->kay->macsec_validate == Strict)
1218 /* latest key: rx, tx, key server member identifier key number */
1219 body->lan = participant->lan;
1220 os_memcpy(body->lsrv_mi, participant->lki.mi,
1221 sizeof(body->lsrv_mi));
1222 body->lkn = host_to_be32(participant->lki.kn);
1223 body->lrx = participant->lrx;
1224 body->ltx = participant->ltx;
1226 /* old key: rx, tx, key server member identifier key number */
1227 body->oan = participant->oan;
1228 if (participant->oki.kn != participant->lki.kn &&
1229 participant->oki.kn != 0) {
1232 os_memcpy(body->osrv_mi, participant->oki.mi,
1233 sizeof(body->osrv_mi));
1234 body->okn = host_to_be32(participant->oki.kn);
1240 /* set CP's variable */
1242 if (!participant->kay->tx_enable)
1243 participant->kay->tx_enable = TRUE;
1245 if (!participant->kay->port_enable)
1246 participant->kay->port_enable = TRUE;
1249 if (!participant->kay->rx_enable)
1250 participant->kay->rx_enable = TRUE;
1253 ieee802_1x_mka_dump_sak_use_body(body);
1259 * ieee802_1x_mka_decode_sak_use_body -
1262 ieee802_1x_mka_decode_sak_use_body(
1263 struct ieee802_1x_mka_participant *participant,
1264 const u8 *mka_msg, size_t msg_len)
1266 struct ieee802_1x_mka_hdr *hdr;
1267 struct ieee802_1x_mka_sak_use_body *body;
1268 struct ieee802_1x_kay_peer *peer;
1269 struct transmit_sa *txsa;
1270 struct data_key *sa_key = NULL;
1272 struct ieee802_1x_mka_ki ki;
1274 Boolean all_receiving;
1277 if (!participant->principal) {
1278 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1281 peer = ieee802_1x_kay_get_live_peer(participant,
1282 participant->current_peer_id.mi);
1284 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1288 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1289 body_len = get_mka_param_body_len(hdr);
1290 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1291 ieee802_1x_mka_dump_sak_use_body(body);
1293 if ((body_len != 0) && (body_len < 40)) {
1294 wpa_printf(MSG_ERROR,
1295 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1300 /* TODO: what action should I take when peer does not support MACsec */
1301 if (body_len == 0) {
1302 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1306 /* TODO: when the plain tx or rx of peer is true, should I change
1307 * the attribute of controlled port
1310 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1313 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1315 /* check latest key is valid */
1316 if (body->ltx || body->lrx) {
1318 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1319 ki.kn = be_to_host32(body->lkn);
1320 dl_list_for_each(sa_key, &participant->sak_list,
1321 struct data_key, list) {
1322 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1328 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1331 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1332 sizeof(participant->lki.mi)) == 0 &&
1333 be_to_host32(body->lkn) == participant->lki.kn &&
1334 body->lan == participant->lan) {
1335 peer->sak_used = TRUE;
1337 if (body->ltx && peer->is_key_server) {
1338 ieee802_1x_cp_set_servertransmitting(
1339 participant->kay->cp, TRUE);
1340 ieee802_1x_cp_sm_step(participant->kay->cp);
1344 /* check old key is valid */
1345 if (body->otx || body->orx) {
1346 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1347 sizeof(participant->oki.mi)) != 0 ||
1348 be_to_host32(body->okn) != participant->oki.kn ||
1349 body->oan != participant->oan) {
1350 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1355 /* TODO: how to set the MACsec hardware when delay_protect is true */
1356 if (body->delay_protect &&
1357 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1358 wpa_printf(MSG_WARNING,
1359 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1363 /* check all live peer have used the sak for receiving sa */
1364 all_receiving = TRUE;
1365 dl_list_for_each(peer, &participant->live_peers,
1366 struct ieee802_1x_kay_peer, list) {
1367 if (!peer->sak_used) {
1368 all_receiving = FALSE;
1372 if (all_receiving) {
1373 participant->to_dist_sak = FALSE;
1374 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1375 ieee802_1x_cp_sm_step(participant->kay->cp);
1378 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1379 lpn = be_to_host32(body->llpn);
1380 if (lpn > participant->kay->pn_exhaustion) {
1381 if (participant->is_key_server) {
1382 participant->new_sak = TRUE;
1383 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1388 dl_list_for_each(txsa, &participant->txsc->sa_list,
1389 struct transmit_sa, list) {
1390 if (sa_key != NULL && txsa->pkey == sa_key) {
1396 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1400 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1401 * npn is larger than txsa's npn, set it to txsa.
1403 secy_get_transmit_next_pn(participant->kay, txsa);
1404 if (lpn > txsa->next_pn) {
1405 secy_set_transmit_next_pn(participant->kay, txsa);
1406 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1414 * ieee802_1x_mka_dist_sak_body_present
1417 ieee802_1x_mka_dist_sak_body_present(
1418 struct ieee802_1x_mka_participant *participant)
1420 if (!participant->to_dist_sak || !participant->new_key)
1428 * ieee802_1x_kay_get_dist_sak_length
1431 ieee802_1x_mka_get_dist_sak_length(
1432 struct ieee802_1x_mka_participant *participant)
1435 int cs_index = participant->kay->macsec_csindex;
1437 if (participant->advised_desired) {
1438 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1439 if (cs_index != DEFAULT_CS_INDEX)
1440 length += CS_ID_LEN;
1442 length += cipher_suite_tbl[cs_index].sak_len + 8;
1444 length = MKA_HDR_LEN;
1446 length = (length + 0x3) & ~0x3;
1453 * ieee802_1x_mka_encode_dist_sak_body -
1456 ieee802_1x_mka_encode_dist_sak_body(
1457 struct ieee802_1x_mka_participant *participant,
1460 struct ieee802_1x_mka_dist_sak_body *body;
1461 struct data_key *sak;
1462 unsigned int length;
1466 length = ieee802_1x_mka_get_dist_sak_length(participant);
1467 body = wpabuf_put(buf, length);
1468 body->type = MKA_DISTRIBUTED_SAK;
1469 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1470 if (length == MKA_HDR_LEN) {
1471 body->confid_offset = 0;
1476 sak = participant->new_key;
1477 body->confid_offset = sak->confidentiality_offset;
1478 body->dan = sak->an;
1479 body->kn = host_to_be32(sak->key_identifier.kn);
1480 cs_index = participant->kay->macsec_csindex;
1482 if (cs_index != DEFAULT_CS_INDEX) {
1483 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1484 sak_pos = CS_ID_LEN;
1486 if (aes_wrap(participant->kek.key, 16,
1487 cipher_suite_tbl[cs_index].sak_len / 8,
1488 sak->key, body->sak + sak_pos)) {
1489 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1493 ieee802_1x_mka_dump_dist_sak_body(body);
1500 * ieee802_1x_kay_init_data_key -
1502 static struct data_key *
1503 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1505 struct data_key *pkey;
1510 pkey = os_zalloc(sizeof(*pkey));
1512 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1516 pkey->key = os_zalloc(conf->key_len);
1517 if (pkey->key == NULL) {
1518 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1523 os_memcpy(pkey->key, conf->key, conf->key_len);
1524 os_memcpy(&pkey->key_identifier, &conf->ki,
1525 sizeof(pkey->key_identifier));
1526 pkey->confidentiality_offset = conf->offset;
1527 pkey->an = conf->an;
1528 pkey->transmits = conf->tx;
1529 pkey->receives = conf->rx;
1530 os_get_time(&pkey->created_time);
1539 * ieee802_1x_kay_decode_dist_sak_body -
1542 ieee802_1x_mka_decode_dist_sak_body(
1543 struct ieee802_1x_mka_participant *participant,
1544 const u8 *mka_msg, size_t msg_len)
1546 struct ieee802_1x_mka_hdr *hdr;
1547 struct ieee802_1x_mka_dist_sak_body *body;
1548 struct ieee802_1x_kay_peer *peer;
1549 struct macsec_ciphersuite *cs;
1551 struct key_conf *conf;
1552 struct data_key *sa_key = NULL;
1553 struct ieee802_1x_mka_ki sak_ki;
1558 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1559 body_len = get_mka_param_body_len(hdr);
1560 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1561 wpa_printf(MSG_ERROR,
1562 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1567 if (!participant->principal) {
1568 wpa_printf(MSG_ERROR,
1569 "KaY: I can't accept the distributed SAK as I am not principal");
1572 if (participant->is_key_server) {
1573 wpa_printf(MSG_ERROR,
1574 "KaY: I can't accept the distributed SAK as myself is key server ");
1577 if (!participant->kay->macsec_desired ||
1578 participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1579 wpa_printf(MSG_ERROR,
1580 "KaY: I am not MACsec-desired or without MACsec capable");
1584 peer = ieee802_1x_kay_get_live_peer(participant,
1585 participant->current_peer_id.mi);
1587 wpa_printf(MSG_ERROR,
1588 "KaY: The key server is not in my live peers list");
1591 if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
1592 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1595 if (body_len == 0) {
1596 participant->kay->authenticated = TRUE;
1597 participant->kay->secured = FALSE;
1598 participant->kay->failed = FALSE;
1599 participant->advised_desired = FALSE;
1600 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1601 ieee802_1x_cp_sm_step(participant->kay->cp);
1602 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1603 participant->to_use_sak = TRUE;
1606 participant->advised_desired = TRUE;
1607 participant->kay->authenticated = FALSE;
1608 participant->kay->secured = TRUE;
1609 participant->kay->failed = FALSE;
1610 ieee802_1x_cp_connect_secure(participant->kay->cp);
1611 ieee802_1x_cp_sm_step(participant->kay->cp);
1613 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1614 ieee802_1x_mka_dump_dist_sak_body(body);
1615 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1617 if (os_memcmp(sa_key->key_identifier.mi,
1618 participant->current_peer_id.mi, MI_LEN) == 0 &&
1619 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1620 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1624 if (body_len == 28) {
1625 sak_len = DEFAULT_SA_KEY_LEN;
1626 wrap_sak = body->sak;
1627 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1629 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1631 wpa_printf(MSG_ERROR,
1632 "KaY: I can't support the Cipher Suite advised by key server");
1635 sak_len = cs->sak_len;
1636 wrap_sak = body->sak + CS_ID_LEN;
1637 participant->kay->macsec_csindex = cs->index;
1640 unwrap_sak = os_zalloc(sak_len);
1642 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1645 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1647 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1648 os_free(unwrap_sak);
1651 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1653 conf = os_zalloc(sizeof(*conf));
1655 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1656 os_free(unwrap_sak);
1659 conf->key_len = sak_len;
1661 conf->key = os_zalloc(conf->key_len);
1663 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1664 os_free(unwrap_sak);
1669 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1671 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1673 sak_ki.kn = be_to_host32(body->kn);
1675 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1676 conf->ki.kn = sak_ki.kn;
1677 conf->an = body->dan;
1678 conf->offset = body->confid_offset;
1682 sa_key = ieee802_1x_kay_init_data_key(conf);
1684 os_free(unwrap_sak);
1690 dl_list_add(&participant->sak_list, &sa_key->list);
1692 ieee802_1x_cp_set_ciphersuite(
1693 participant->kay->cp,
1694 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1695 ieee802_1x_cp_sm_step(participant->kay->cp);
1696 ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1697 ieee802_1x_cp_sm_step(participant->kay->cp);
1698 ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1699 ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1700 ieee802_1x_cp_signal_newsak(participant->kay->cp);
1701 ieee802_1x_cp_sm_step(participant->kay->cp);
1703 participant->to_use_sak = TRUE;
1705 os_free(unwrap_sak);
1714 * ieee802_1x_mka_icv_body_present
1717 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1724 * ieee802_1x_kay_get_icv_length
1727 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1731 length = sizeof(struct ieee802_1x_mka_icv_body);
1732 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1734 return (length + 0x3) & ~0x3;
1739 * ieee802_1x_mka_encode_icv_body -
1742 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1745 struct ieee802_1x_mka_icv_body *body;
1746 unsigned int length;
1747 u8 cmac[MAX_ICV_LEN];
1749 length = ieee802_1x_mka_get_icv_length(participant);
1750 if (length != DEFAULT_ICV_LEN) {
1751 body = wpabuf_put(buf, MKA_HDR_LEN);
1752 body->type = MKA_ICV_INDICATOR;
1753 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1756 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1757 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1758 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1762 if (length != DEFAULT_ICV_LEN) {
1763 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1764 length - MKA_HDR_LEN);
1766 os_memcpy(wpabuf_put(buf, length), cmac, length);
1773 * ieee802_1x_mka_decode_icv_body -
1776 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1777 const u8 *mka_msg, size_t msg_len)
1779 struct ieee802_1x_mka_hdr *hdr;
1780 struct ieee802_1x_mka_icv_body *body;
1788 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1789 hdr = (struct ieee802_1x_mka_hdr *) pos;
1790 body_len = get_mka_param_body_len(hdr);
1791 body_type = get_mka_param_body_type(hdr);
1793 if (left_len < (body_len + MKA_HDR_LEN))
1796 if (body_type != MKA_ICV_INDICATOR) {
1797 left_len -= MKA_HDR_LEN + body_len;
1798 pos += MKA_HDR_LEN + body_len;
1802 body = (struct ieee802_1x_mka_icv_body *)pos;
1804 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1811 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1816 * ieee802_1x_mka_decode_dist_cak_body-
1819 ieee802_1x_mka_decode_dist_cak_body(
1820 struct ieee802_1x_mka_participant *participant,
1821 const u8 *mka_msg, size_t msg_len)
1823 struct ieee802_1x_mka_hdr *hdr;
1826 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1827 body_len = get_mka_param_body_len(hdr);
1828 if (body_len < 28) {
1829 wpa_printf(MSG_ERROR,
1830 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1840 * ieee802_1x_mka_decode_kmd_body -
1843 ieee802_1x_mka_decode_kmd_body(
1844 struct ieee802_1x_mka_participant *participant,
1845 const u8 *mka_msg, size_t msg_len)
1847 struct ieee802_1x_mka_hdr *hdr;
1850 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1851 body_len = get_mka_param_body_len(hdr);
1853 wpa_printf(MSG_ERROR,
1854 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1864 * ieee802_1x_mka_decode_announce_body -
1866 static int ieee802_1x_mka_decode_announce_body(
1867 struct ieee802_1x_mka_participant *participant,
1868 const u8 *mka_msg, size_t msg_len)
1874 static struct mka_param_body_handler mka_body_handler[] = {
1875 /* basic parameter set */
1877 ieee802_1x_mka_encode_basic_body,
1879 ieee802_1x_mka_basic_body_length,
1880 ieee802_1x_mka_basic_body_present
1883 /* live peer list parameter set */
1885 ieee802_1x_mka_encode_live_peer_body,
1886 ieee802_1x_mka_decode_live_peer_body,
1887 ieee802_1x_mka_get_live_peer_length,
1888 ieee802_1x_mka_live_peer_body_present
1891 /* potential peer list parameter set */
1893 ieee802_1x_mka_encode_potential_peer_body,
1894 ieee802_1x_mka_decode_potential_peer_body,
1895 ieee802_1x_mka_get_potential_peer_length,
1896 ieee802_1x_mka_potential_peer_body_present
1899 /* sak use parameter set */
1901 ieee802_1x_mka_encode_sak_use_body,
1902 ieee802_1x_mka_decode_sak_use_body,
1903 ieee802_1x_mka_get_sak_use_length,
1904 ieee802_1x_mka_sak_use_body_present
1907 /* distribute sak parameter set */
1909 ieee802_1x_mka_encode_dist_sak_body,
1910 ieee802_1x_mka_decode_dist_sak_body,
1911 ieee802_1x_mka_get_dist_sak_length,
1912 ieee802_1x_mka_dist_sak_body_present
1915 /* distribute cak parameter set */
1918 ieee802_1x_mka_decode_dist_cak_body,
1923 /* kmd parameter set */
1926 ieee802_1x_mka_decode_kmd_body,
1931 /* announce parameter set */
1934 ieee802_1x_mka_decode_announce_body,
1939 /* icv parameter set */
1941 ieee802_1x_mka_encode_icv_body,
1943 ieee802_1x_mka_get_icv_length,
1944 ieee802_1x_mka_icv_body_present
1950 * ieee802_1x_kay_deinit_data_key -
1952 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1961 dl_list_del(&pkey->list);
1968 * ieee802_1x_kay_generate_new_sak -
1971 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1973 struct data_key *sa_key = NULL;
1974 struct key_conf *conf;
1975 struct ieee802_1x_kay_peer *peer;
1976 struct ieee802_1x_kay *kay = participant->kay;
1977 int ctx_len, ctx_offset;
1980 /* check condition for generating a fresh SAK:
1981 * must have one live peer
1982 * and MKA life time elapse since last distribution
1983 * or potential peer is empty
1985 if (dl_list_empty(&participant->live_peers)) {
1986 wpa_printf(MSG_ERROR,
1987 "KaY: Live peers list must not empty when generating fresh SAK");
1991 /* FIXME: A fresh SAK not generated until
1992 * the live peer list contains at least one peer and
1993 * MKA life time has elapsed since the prior SAK was first distributed,
1994 * or the Key server's potential peer is empty
1995 * but I can't understand the second item, so
1996 * here only check first item and ingore
1997 * && (!dl_list_empty(&participant->potential_peers))) {
1999 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
2000 wpa_printf(MSG_ERROR,
2001 "KaY: Life time have not elapsed since prior SAK distributed");
2005 conf = os_zalloc(sizeof(*conf));
2007 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2010 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2012 conf->key = os_zalloc(conf->key_len);
2015 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2019 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2020 dl_list_for_each(peer, &participant->live_peers,
2021 struct ieee802_1x_kay_peer, list)
2022 ctx_len += sizeof(peer->mi);
2023 ctx_len += sizeof(participant->mi);
2025 context = os_zalloc(ctx_len);
2032 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2038 ctx_offset += conf->key_len;
2039 dl_list_for_each(peer, &participant->live_peers,
2040 struct ieee802_1x_kay_peer, list) {
2041 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2042 ctx_offset += sizeof(peer->mi);
2044 os_memcpy(context + ctx_offset, participant->mi,
2045 sizeof(participant->mi));
2046 ctx_offset += sizeof(participant->mi);
2047 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2049 if (conf->key_len == 16) {
2050 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2051 context, ctx_len, conf->key);
2052 } else if (conf->key_len == 32) {
2053 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2054 context, ctx_len, conf->key);
2056 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2062 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2063 conf->key, conf->key_len);
2065 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2066 conf->ki.kn = participant->kay->dist_kn;
2067 conf->an = participant->kay->dist_an;
2068 conf->offset = kay->macsec_confidentiality;
2072 sa_key = ieee802_1x_kay_init_data_key(conf);
2079 participant->new_key = sa_key;
2081 dl_list_add(&participant->sak_list, &sa_key->list);
2082 ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2083 cipher_suite_tbl[kay->macsec_csindex].id);
2084 ieee802_1x_cp_sm_step(kay->cp);
2085 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2086 ieee802_1x_cp_sm_step(kay->cp);
2087 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2088 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2089 ieee802_1x_cp_signal_newsak(kay->cp);
2090 ieee802_1x_cp_sm_step(kay->cp);
2092 dl_list_for_each(peer, &participant->live_peers,
2093 struct ieee802_1x_kay_peer, list)
2094 peer->sak_used = FALSE;
2096 participant->kay->dist_kn++;
2097 participant->kay->dist_an++;
2098 if (participant->kay->dist_an > 3)
2099 participant->kay->dist_an = 0;
2101 participant->kay->dist_time = time(NULL);
2111 * ieee802_1x_kay_elect_key_server - elect the key server
2112 * when to elect: whenever the live peers list changes
2115 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2117 struct ieee802_1x_kay_peer *peer;
2118 struct ieee802_1x_kay_peer *key_server = NULL;
2119 struct ieee802_1x_kay *kay = participant->kay;
2120 Boolean i_is_key_server;
2122 if (participant->is_obliged_key_server) {
2123 participant->new_sak = TRUE;
2124 participant->to_dist_sak = FALSE;
2125 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2129 /* elect the key server among the peers */
2130 dl_list_for_each(peer, &participant->live_peers,
2131 struct ieee802_1x_kay_peer, list) {
2132 if (!peer->is_key_server)
2140 if (peer->key_server_priority <
2141 key_server->key_server_priority) {
2143 } else if (peer->key_server_priority ==
2144 key_server->key_server_priority) {
2145 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2151 /* elect the key server between me and the above elected peer */
2152 i_is_key_server = FALSE;
2153 if (key_server && participant->can_be_key_server) {
2154 if (kay->actor_priority
2155 < key_server->key_server_priority) {
2156 i_is_key_server = TRUE;
2157 } else if (kay->actor_priority
2158 == key_server->key_server_priority) {
2159 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2161 i_is_key_server = TRUE;
2163 } else if (participant->can_be_key_server) {
2164 i_is_key_server = TRUE;
2167 if (i_is_key_server) {
2168 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2169 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2170 ieee802_1x_cp_signal_chgdserver(kay->cp);
2171 ieee802_1x_cp_sm_step(kay->cp);
2174 participant->is_key_server = TRUE;
2175 participant->principal = TRUE;
2176 participant->new_sak = TRUE;
2177 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2178 participant->to_dist_sak = FALSE;
2179 participant->is_elected = TRUE;
2181 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2182 sizeof(kay->key_server_sci));
2183 kay->key_server_priority = kay->actor_priority;
2184 } else if (key_server) {
2185 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2186 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2187 ieee802_1x_cp_signal_chgdserver(kay->cp);
2188 ieee802_1x_cp_sm_step(kay->cp);
2191 participant->is_key_server = FALSE;
2192 participant->principal = TRUE;
2193 participant->is_elected = TRUE;
2195 os_memcpy(&kay->key_server_sci, &key_server->sci,
2196 sizeof(kay->key_server_sci));
2197 kay->key_server_priority = key_server->key_server_priority;
2199 participant->principal = FALSE;
2200 participant->is_key_server = FALSE;
2201 participant->is_elected = FALSE;
2209 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2210 * how to use MACsec: whether use MACsec and its capability
2211 * protectFrames will be advised if the key server and one of its live peers are
2212 * MACsec capable and one of those request MACsec protection
2215 ieee802_1x_kay_decide_macsec_use(
2216 struct ieee802_1x_mka_participant *participant)
2218 struct ieee802_1x_kay *kay = participant->kay;
2219 struct ieee802_1x_kay_peer *peer;
2220 enum macsec_cap less_capability;
2223 if (!participant->is_key_server)
2226 /* key server self is MACsec-desired and requesting MACsec */
2227 if (!kay->macsec_desired) {
2228 participant->advised_desired = FALSE;
2231 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2232 participant->advised_desired = FALSE;
2235 less_capability = kay->macsec_capable;
2237 /* at least one of peers is MACsec-desired and requesting MACsec */
2239 dl_list_for_each(peer, &participant->live_peers,
2240 struct ieee802_1x_kay_peer, list) {
2241 if (!peer->macsec_desired)
2244 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2247 less_capability = (less_capability < peer->macsec_capability) ?
2248 less_capability : peer->macsec_capability;
2253 participant->advised_desired = TRUE;
2254 participant->advised_capability = less_capability;
2255 kay->authenticated = FALSE;
2256 kay->secured = TRUE;
2257 kay->failed = FALSE;
2258 ieee802_1x_cp_connect_secure(kay->cp);
2259 ieee802_1x_cp_sm_step(kay->cp);
2261 participant->advised_desired = FALSE;
2262 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2263 participant->to_use_sak = FALSE;
2264 kay->authenticated = TRUE;
2265 kay->secured = FALSE;
2266 kay->failed = FALSE;
2275 ieee802_1x_cp_connect_authenticated(kay->cp);
2276 ieee802_1x_cp_sm_step(kay->cp);
2282 static const u8 pae_group_addr[ETH_ALEN] = {
2283 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2288 * ieee802_1x_kay_encode_mkpdu -
2291 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2292 struct wpabuf *pbuf)
2295 struct ieee8023_hdr *ether_hdr;
2296 struct ieee802_1x_hdr *eapol_hdr;
2298 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2299 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2300 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2301 sizeof(ether_hdr->dest));
2302 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2304 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2305 eapol_hdr->version = EAPOL_VERSION;
2306 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2307 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2309 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2310 if (mka_body_handler[i].body_present &&
2311 mka_body_handler[i].body_present(participant)) {
2312 if (mka_body_handler[i].body_tx(participant, pbuf))
2321 * ieee802_1x_participant_send_mkpdu -
2324 ieee802_1x_participant_send_mkpdu(
2325 struct ieee802_1x_mka_participant *participant)
2328 struct ieee802_1x_kay *kay = participant->kay;
2332 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2333 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2334 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2335 if (mka_body_handler[i].body_present &&
2336 mka_body_handler[i].body_present(participant))
2337 length += mka_body_handler[i].body_length(participant);
2340 buf = wpabuf_alloc(length);
2342 wpa_printf(MSG_ERROR, "KaY: out of memory");
2346 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2347 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2351 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2355 participant->active = TRUE;
2361 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2363 * ieee802_1x_participant_timer -
2365 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2367 struct ieee802_1x_mka_participant *participant;
2368 struct ieee802_1x_kay *kay;
2369 struct ieee802_1x_kay_peer *peer, *pre_peer;
2370 time_t now = time(NULL);
2372 struct receive_sc *rxsc, *pre_rxsc;
2373 struct transmit_sa *txsa, *pre_txsa;
2375 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2376 kay = participant->kay;
2377 if (participant->cak_life) {
2378 if (now > participant->cak_life) {
2379 kay->authenticated = FALSE;
2380 kay->secured = FALSE;
2382 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2387 /* should delete MKA instance if there are not live peers
2388 * when the MKA life elapsed since its creating */
2389 if (participant->mka_life) {
2390 if (dl_list_empty(&participant->live_peers)) {
2391 if (now > participant->mka_life) {
2392 kay->authenticated = FALSE;
2393 kay->secured = FALSE;
2395 ieee802_1x_kay_delete_mka(kay,
2400 participant->mka_life = 0;
2405 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2406 struct ieee802_1x_kay_peer, list) {
2407 if (now > peer->expire) {
2408 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2409 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2411 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2412 dl_list_for_each_safe(rxsc, pre_rxsc,
2413 &participant->rxsc_list,
2414 struct receive_sc, list) {
2415 if (sci_equal(&rxsc->sci, &peer->sci)) {
2416 secy_delete_receive_sc(kay, rxsc);
2417 ieee802_1x_kay_deinit_receive_sc(
2421 dl_list_del(&peer->list);
2428 if (dl_list_empty(&participant->live_peers)) {
2429 participant->advised_desired = FALSE;
2430 participant->advised_capability =
2431 MACSEC_CAP_NOT_IMPLEMENTED;
2432 participant->to_use_sak = FALSE;
2433 kay->authenticated = TRUE;
2434 kay->secured = FALSE;
2435 kay->failed = FALSE;
2444 dl_list_for_each_safe(txsa, pre_txsa,
2445 &participant->txsc->sa_list,
2446 struct transmit_sa, list) {
2447 secy_disable_transmit_sa(kay, txsa);
2448 ieee802_1x_kay_deinit_transmit_sa(txsa);
2451 ieee802_1x_cp_connect_authenticated(kay->cp);
2452 ieee802_1x_cp_sm_step(kay->cp);
2454 ieee802_1x_kay_elect_key_server(participant);
2455 ieee802_1x_kay_decide_macsec_use(participant);
2459 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2460 struct ieee802_1x_kay_peer, list) {
2461 if (now > peer->expire) {
2462 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2463 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2465 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2466 dl_list_del(&peer->list);
2471 if (participant->new_sak) {
2472 if (!ieee802_1x_kay_generate_new_sak(participant))
2473 participant->to_dist_sak = TRUE;
2475 participant->new_sak = FALSE;
2478 if (participant->retry_count < MAX_RETRY_CNT) {
2479 ieee802_1x_participant_send_mkpdu(participant);
2480 participant->retry_count++;
2483 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2484 ieee802_1x_participant_timer,
2490 * ieee802_1x_kay_init_transmit_sa -
2492 static struct transmit_sa *
2493 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2494 struct data_key *key)
2496 struct transmit_sa *psa;
2498 key->tx_latest = TRUE;
2499 key->rx_latest = TRUE;
2501 psa = os_zalloc(sizeof(*psa));
2503 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2507 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2508 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2509 psa->confidentiality = TRUE;
2511 psa->confidentiality = FALSE;
2515 psa->next_pn = next_PN;
2518 os_get_time(&psa->created_time);
2519 psa->in_use = FALSE;
2521 dl_list_add(&psc->sa_list, &psa->list);
2522 wpa_printf(MSG_DEBUG,
2523 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2524 (int) an, next_PN, psc->channel);
2531 * ieee802_1x_kay_deinit_transmit_sa -
2533 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2536 wpa_printf(MSG_DEBUG,
2537 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2538 psa->an, psa->sc->channel);
2539 dl_list_del(&psa->list);
2545 * init_transmit_sc -
2547 static struct transmit_sc *
2548 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2551 struct transmit_sc *psc;
2553 psc = os_zalloc(sizeof(*psc));
2555 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2558 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2559 psc->channel = channel;
2561 os_get_time(&psc->created_time);
2562 psc->transmitting = FALSE;
2563 psc->encoding_sa = FALSE;
2564 psc->enciphering_sa = FALSE;
2566 dl_list_init(&psc->sa_list);
2567 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2568 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2575 * ieee802_1x_kay_deinit_transmit_sc -
2578 ieee802_1x_kay_deinit_transmit_sc(
2579 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2581 struct transmit_sa *psa, *tmp;
2583 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2585 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2587 secy_disable_transmit_sa(participant->kay, psa);
2588 ieee802_1x_kay_deinit_transmit_sa(psa);
2595 /****************** Interface between CP and KAY *********************/
2597 * ieee802_1x_kay_set_latest_sa_attr -
2599 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2600 struct ieee802_1x_mka_ki *lki, u8 lan,
2601 Boolean ltx, Boolean lrx)
2603 struct ieee802_1x_mka_participant *principal;
2605 principal = ieee802_1x_kay_get_principal_participant(kay);
2610 os_memset(&principal->lki, 0, sizeof(principal->lki));
2612 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2614 principal->lan = lan;
2615 principal->ltx = ltx;
2616 principal->lrx = lrx;
2621 kay->ltx_kn = lki->kn;
2622 kay->lrx_kn = lki->kn;
2632 * ieee802_1x_kay_set_old_sa_attr -
2634 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2635 struct ieee802_1x_mka_ki *oki,
2636 u8 oan, Boolean otx, Boolean orx)
2638 struct ieee802_1x_mka_participant *principal;
2640 principal = ieee802_1x_kay_get_principal_participant(kay);
2645 os_memset(&principal->oki, 0, sizeof(principal->oki));
2647 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2649 principal->oan = oan;
2650 principal->otx = otx;
2651 principal->orx = orx;
2657 kay->otx_kn = oki->kn;
2658 kay->orx_kn = oki->kn;
2668 * ieee802_1x_kay_create_sas -
2670 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2671 struct ieee802_1x_mka_ki *lki)
2673 struct data_key *sa_key, *latest_sak;
2674 struct ieee802_1x_mka_participant *principal;
2675 struct receive_sc *rxsc;
2676 struct receive_sa *rxsa;
2677 struct transmit_sa *txsa;
2679 principal = ieee802_1x_kay_get_principal_participant(kay);
2684 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2685 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2686 sa_key->rx_latest = TRUE;
2687 sa_key->tx_latest = TRUE;
2688 latest_sak = sa_key;
2689 principal->to_use_sak = TRUE;
2691 sa_key->rx_latest = FALSE;
2692 sa_key->tx_latest = FALSE;
2696 wpa_printf(MSG_ERROR, "lki related sak not found");
2700 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2701 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2706 secy_create_receive_sa(kay, rxsa);
2709 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2714 secy_create_transmit_sa(kay, txsa);
2723 * ieee802_1x_kay_delete_sas -
2725 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2726 struct ieee802_1x_mka_ki *ki)
2728 struct data_key *sa_key, *pre_key;
2729 struct transmit_sa *txsa, *pre_txsa;
2730 struct receive_sa *rxsa, *pre_rxsa;
2731 struct receive_sc *rxsc;
2732 struct ieee802_1x_mka_participant *principal;
2734 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2735 principal = ieee802_1x_kay_get_principal_participant(kay);
2739 /* remove the transmit sa */
2740 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2741 struct transmit_sa, list) {
2742 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2743 secy_disable_transmit_sa(kay, txsa);
2744 ieee802_1x_kay_deinit_transmit_sa(txsa);
2748 /* remove the receive sa */
2749 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2750 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2751 struct receive_sa, list) {
2752 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2753 secy_disable_receive_sa(kay, rxsa);
2754 ieee802_1x_kay_deinit_receive_sa(rxsa);
2759 /* remove the sak */
2760 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2761 struct data_key, list) {
2762 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2763 ieee802_1x_kay_deinit_data_key(sa_key);
2766 if (principal->new_key == sa_key)
2767 principal->new_key = NULL;
2775 * ieee802_1x_kay_enable_tx_sas -
2777 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2778 struct ieee802_1x_mka_ki *lki)
2780 struct ieee802_1x_mka_participant *principal;
2781 struct transmit_sa *txsa;
2783 principal = ieee802_1x_kay_get_principal_participant(kay);
2787 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2789 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2790 txsa->in_use = TRUE;
2791 secy_enable_transmit_sa(kay, txsa);
2792 ieee802_1x_cp_set_usingtransmitas(
2793 principal->kay->cp, TRUE);
2794 ieee802_1x_cp_sm_step(principal->kay->cp);
2803 * ieee802_1x_kay_enable_rx_sas -
2805 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2806 struct ieee802_1x_mka_ki *lki)
2808 struct ieee802_1x_mka_participant *principal;
2809 struct receive_sa *rxsa;
2810 struct receive_sc *rxsc;
2812 principal = ieee802_1x_kay_get_principal_participant(kay);
2816 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2817 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2819 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2820 rxsa->in_use = TRUE;
2821 secy_enable_receive_sa(kay, rxsa);
2822 ieee802_1x_cp_set_usingreceivesas(
2823 principal->kay->cp, TRUE);
2824 ieee802_1x_cp_sm_step(principal->kay->cp);
2834 * ieee802_1x_kay_enable_new_info -
2836 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2838 struct ieee802_1x_mka_participant *principal;
2840 principal = ieee802_1x_kay_get_principal_participant(kay);
2844 if (principal->retry_count < MAX_RETRY_CNT) {
2845 ieee802_1x_participant_send_mkpdu(principal);
2846 principal->retry_count++;
2854 * ieee802_1x_kay_cp_conf -
2856 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2857 struct ieee802_1x_cp_conf *pconf)
2859 pconf->protect = kay->macsec_protect;
2860 pconf->replay_protect = kay->macsec_replay_protect;
2861 pconf->validate = kay->macsec_validate;
2868 * ieee802_1x_kay_alloc_cp_sm -
2870 static struct ieee802_1x_cp_sm *
2871 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2873 struct ieee802_1x_cp_conf conf;
2875 os_memset(&conf, 0, sizeof(conf));
2876 conf.protect = kay->macsec_protect;
2877 conf.replay_protect = kay->macsec_replay_protect;
2878 conf.validate = kay->macsec_validate;
2879 conf.replay_window = kay->macsec_replay_window;
2881 return ieee802_1x_cp_sm_init(kay, &conf);
2886 * ieee802_1x_kay_mkpdu_sanity_check -
2887 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2889 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2890 const u8 *buf, size_t len)
2892 struct ieee8023_hdr *eth_hdr;
2893 struct ieee802_1x_hdr *eapol_hdr;
2894 struct ieee802_1x_mka_hdr *mka_hdr;
2895 struct ieee802_1x_mka_basic_body *body;
2897 struct ieee802_1x_mka_participant *participant;
2899 u8 icv[MAX_ICV_LEN];
2902 eth_hdr = (struct ieee8023_hdr *) buf;
2903 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2904 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2906 /* destination address should be not individual address */
2907 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2908 wpa_printf(MSG_MSGDUMP,
2909 "KaY: ethernet destination address is not PAE group address");
2913 /* MKPDU should not less than 32 octets */
2914 mka_msg_len = be_to_host16(eapol_hdr->length);
2915 if (mka_msg_len < 32) {
2916 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2919 /* MKPDU should multiple 4 octets */
2920 if ((mka_msg_len % 4) != 0) {
2921 wpa_printf(MSG_MSGDUMP,
2922 "KaY: MKPDU is not multiple of 4 octets");
2926 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2927 ieee802_1x_mka_dump_basic_body(body);
2928 body_len = get_mka_param_body_len(body);
2929 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2930 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2931 wpa_printf(MSG_ERROR,
2932 "KaY: Received EAPOL-MKA Packet Body Length (%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",
2933 (int) mka_msg_len, (int) MKA_HDR_LEN,
2934 (int) body_len, DEFAULT_ICV_LEN);
2938 /* CKN should be owned by I */
2939 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2941 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2945 /* algorithm agility check */
2946 if (os_memcmp(body->algo_agility, mka_algo_agility,
2947 sizeof(body->algo_agility)) != 0) {
2948 wpa_printf(MSG_ERROR,
2949 "KaY: peer's algorithm agility not supported for me");
2955 * The ICV will comprise the final octets of the packet body, whatever
2956 * its size, not the fixed length 16 octets, indicated by the EAPOL
2957 * packet body length.
2959 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2960 participant->ick.key,
2961 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2962 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2965 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2969 if (os_memcmp_const(msg_icv, icv,
2970 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2972 wpa_printf(MSG_ERROR,
2973 "KaY: Computed ICV is not equal to Received ICV");
2977 wpa_printf(MSG_ERROR, "KaY: No ICV");
2986 * ieee802_1x_kay_decode_mkpdu -
2988 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2989 const u8 *buf, size_t len)
2991 struct ieee802_1x_mka_participant *participant;
2992 struct ieee802_1x_mka_hdr *hdr;
2998 Boolean my_included;
2999 Boolean handled[256];
3001 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
3004 /* handle basic parameter set */
3005 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
3006 left_len = len - sizeof(struct ieee8023_hdr) -
3007 sizeof(struct ieee802_1x_hdr);
3008 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3012 /* to skip basic parameter set */
3013 hdr = (struct ieee802_1x_mka_hdr *) pos;
3014 body_len = get_mka_param_body_len(hdr);
3015 pos += body_len + MKA_HDR_LEN;
3016 left_len -= body_len + MKA_HDR_LEN;
3018 /* check i am in the peer's peer list */
3019 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3021 /* accept the peer as live peer */
3022 if (!ieee802_1x_kay_is_in_peer(
3024 participant->current_peer_id.mi)) {
3025 if (!ieee802_1x_kay_create_live_peer(
3027 participant->current_peer_id.mi,
3029 participant->current_peer_id.mn)))
3031 ieee802_1x_kay_elect_key_server(participant);
3032 ieee802_1x_kay_decide_macsec_use(participant);
3034 if (ieee802_1x_kay_is_in_potential_peer(
3035 participant, participant->current_peer_id.mi)) {
3036 if (!ieee802_1x_kay_move_live_peer(
3038 participant->current_peer_id.mi,
3039 be_to_host32(participant->
3040 current_peer_id.mn)))
3042 ieee802_1x_kay_elect_key_server(participant);
3043 ieee802_1x_kay_decide_macsec_use(participant);
3048 * Handle other parameter set than basic parameter set.
3049 * Each parameter set should be present only once.
3051 for (i = 0; i < 256; i++)
3055 while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3056 hdr = (struct ieee802_1x_mka_hdr *) pos;
3057 body_len = get_mka_param_body_len(hdr);
3058 body_type = get_mka_param_body_type(hdr);
3060 if (body_type == MKA_ICV_INDICATOR)
3063 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3064 wpa_printf(MSG_ERROR,
3065 "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",
3066 (int) left_len, (int) MKA_HDR_LEN,
3067 (int) body_len, DEFAULT_ICV_LEN);
3071 if (handled[body_type])
3074 handled[body_type] = TRUE;
3075 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3076 mka_body_handler[body_type].body_rx) {
3077 mka_body_handler[body_type].body_rx
3078 (participant, pos, left_len);
3080 wpa_printf(MSG_ERROR,
3081 "The type %d not supported in this MKA version %d",
3082 body_type, MKA_VERSION_ID);
3086 pos += body_len + MKA_HDR_LEN;
3087 left_len -= body_len + MKA_HDR_LEN;
3091 participant->retry_count = 0;
3092 participant->active = TRUE;
3099 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3102 struct ieee802_1x_kay *kay = ctx;
3103 struct ieee8023_hdr *eth_hdr;
3104 struct ieee802_1x_hdr *eapol_hdr;
3106 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3107 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3108 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3109 (unsigned long) len);
3113 eth_hdr = (struct ieee8023_hdr *) buf;
3114 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3115 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3116 be_to_host16(eapol_hdr->length)) {
3117 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3118 (unsigned long) len,
3119 (unsigned long) be_to_host16(eapol_hdr->length));
3123 if (eapol_hdr->version < EAPOL_VERSION) {
3124 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3125 eapol_hdr->version);
3128 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3129 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3132 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3133 if (dl_list_empty(&kay->participant_list)) {
3134 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3138 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3143 * ieee802_1x_kay_init -
3145 struct ieee802_1x_kay *
3146 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3147 const char *ifname, const u8 *addr)
3149 struct ieee802_1x_kay *kay;
3151 kay = os_zalloc(sizeof(*kay));
3153 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3160 kay->active = FALSE;
3162 kay->authenticated = FALSE;
3163 kay->secured = FALSE;
3164 kay->failed = FALSE;
3165 kay->policy = policy;
3167 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3168 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3169 kay->actor_sci.port = host_to_be16(0x0001);
3170 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3172 /* While actor acts as a key server, shall distribute sakey */
3177 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3178 kay->macsec_csindex = DEFAULT_CS_INDEX;
3179 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3180 kay->mka_version = MKA_VERSION_ID;
3182 os_memcpy(kay->algo_agility, mka_algo_agility,
3183 sizeof(kay->algo_agility));
3185 dl_list_init(&kay->participant_list);
3187 if (policy == DO_NOT_SECURE) {
3188 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3189 kay->macsec_desired = FALSE;
3190 kay->macsec_protect = FALSE;
3191 kay->macsec_validate = Disabled;
3192 kay->macsec_replay_protect = FALSE;
3193 kay->macsec_replay_window = 0;
3194 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3196 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3197 kay->macsec_desired = TRUE;
3198 kay->macsec_protect = TRUE;
3199 kay->macsec_validate = Strict;
3200 kay->macsec_replay_protect = FALSE;
3201 kay->macsec_replay_window = 0;
3202 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3205 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3207 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3208 secy_init_macsec(kay);
3209 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3211 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3214 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3215 if (kay->cp == NULL) {
3216 ieee802_1x_kay_deinit(kay);
3220 if (policy == DO_NOT_SECURE) {
3221 ieee802_1x_cp_connect_authenticated(kay->cp);
3222 ieee802_1x_cp_sm_step(kay->cp);
3224 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3225 kay_l2_receive, kay, 1);
3226 if (kay->l2_mka == NULL) {
3227 wpa_printf(MSG_WARNING,
3228 "KaY: Failed to initialize L2 packet processing for MKA packet");
3229 ieee802_1x_kay_deinit(kay);
3239 * ieee802_1x_kay_deinit -
3242 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3244 struct ieee802_1x_mka_participant *participant;
3249 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3251 while (!dl_list_empty(&kay->participant_list)) {
3252 participant = dl_list_entry(kay->participant_list.next,
3253 struct ieee802_1x_mka_participant,
3255 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3258 ieee802_1x_cp_sm_deinit(kay->cp);
3259 secy_deinit_macsec(kay);
3262 l2_packet_deinit(kay->l2_mka);
3272 * ieee802_1x_kay_create_mka -
3274 struct ieee802_1x_mka_participant *
3275 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3276 struct mka_key *cak, u32 life,
3277 enum mka_created_mode mode, Boolean is_authenticator)
3279 struct ieee802_1x_mka_participant *participant;
3282 if (!kay || !ckn || !cak) {
3283 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3287 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3288 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3291 if (ckn->len > MAX_CKN_LEN) {
3292 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3296 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3300 participant = os_zalloc(sizeof(*participant));
3302 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3306 participant->ckn.len = ckn->len;
3307 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3308 participant->cak.len = cak->len;
3309 os_memcpy(participant->cak.key, cak->key, cak->len);
3311 participant->cak_life = life + time(NULL);
3315 if (is_authenticator) {
3316 participant->is_obliged_key_server = TRUE;
3317 participant->can_be_key_server = TRUE;
3318 participant->is_key_server = TRUE;
3319 participant->principal = TRUE;
3321 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3322 sizeof(kay->key_server_sci));
3323 kay->key_server_priority = kay->actor_priority;
3324 participant->is_elected = TRUE;
3326 participant->is_obliged_key_server = FALSE;
3327 participant->can_be_key_server = FALSE;
3328 participant->is_key_server = FALSE;
3329 participant->is_elected = TRUE;
3334 participant->is_obliged_key_server = FALSE;
3335 participant->can_be_key_server = TRUE;
3336 participant->is_key_server = TRUE;
3337 participant->is_elected = FALSE;
3341 participant->cached = FALSE;
3343 participant->active = FALSE;
3344 participant->participant = FALSE;
3345 participant->retain = FALSE;
3346 participant->activate = DEFAULT;
3348 if (participant->is_key_server)
3349 participant->principal = TRUE;
3351 dl_list_init(&participant->live_peers);
3352 dl_list_init(&participant->potential_peers);
3354 participant->retry_count = 0;
3355 participant->kay = kay;
3357 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3359 participant->mn = 0;
3361 participant->lrx = FALSE;
3362 participant->ltx = FALSE;
3363 participant->orx = FALSE;
3364 participant->otx = FALSE;
3365 participant->to_dist_sak = FALSE;
3366 participant->to_use_sak = FALSE;
3367 participant->new_sak = FALSE;
3368 dl_list_init(&participant->sak_list);
3369 participant->new_key = NULL;
3370 dl_list_init(&participant->rxsc_list);
3371 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3373 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3374 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3375 kay->macsec_replay_window);
3376 secy_create_transmit_sc(kay, participant->txsc);
3378 /* to derive KEK from CAK and CKN */
3379 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3380 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3381 participant->ckn.name,
3382 participant->ckn.len,
3383 participant->kek.key)) {
3384 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3387 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3388 participant->kek.key, participant->kek.len);
3390 /* to derive ICK from CAK and CKN */
3391 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3392 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3393 participant->ckn.name,
3394 participant->ckn.len,
3395 participant->ick.key)) {
3396 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3399 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3400 participant->ick.key, participant->ick.len);
3402 dl_list_add(&kay->participant_list, &participant->list);
3403 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3404 ckn->name, ckn->len);
3406 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3407 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3409 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3415 os_free(participant);
3421 * ieee802_1x_kay_delete_mka -
3424 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3426 struct ieee802_1x_mka_participant *participant;
3427 struct ieee802_1x_kay_peer *peer;
3428 struct data_key *sak;
3429 struct receive_sc *rxsc;
3434 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3436 /* get the participant */
3437 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3439 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3440 ckn->name, ckn->len);
3444 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3445 dl_list_del(&participant->list);
3447 /* remove live peer */
3448 while (!dl_list_empty(&participant->live_peers)) {
3449 peer = dl_list_entry(participant->live_peers.next,
3450 struct ieee802_1x_kay_peer, list);
3451 dl_list_del(&peer->list);
3455 /* remove potential peer */
3456 while (!dl_list_empty(&participant->potential_peers)) {
3457 peer = dl_list_entry(participant->potential_peers.next,
3458 struct ieee802_1x_kay_peer, list);
3459 dl_list_del(&peer->list);
3464 while (!dl_list_empty(&participant->sak_list)) {
3465 sak = dl_list_entry(participant->sak_list.next,
3466 struct data_key, list);
3467 dl_list_del(&sak->list);
3471 while (!dl_list_empty(&participant->rxsc_list)) {
3472 rxsc = dl_list_entry(participant->rxsc_list.next,
3473 struct receive_sc, list);
3474 secy_delete_receive_sc(kay, rxsc);
3475 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3477 secy_delete_transmit_sc(kay, participant->txsc);
3478 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3480 os_memset(&participant->cak, 0, sizeof(participant->cak));
3481 os_memset(&participant->kek, 0, sizeof(participant->kek));
3482 os_memset(&participant->ick, 0, sizeof(participant->ick));
3483 os_free(participant);
3488 * ieee802_1x_kay_mka_participate -
3490 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3491 struct mka_key_name *ckn,
3494 struct ieee802_1x_mka_participant *participant;
3499 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3503 participant->active = status;
3508 * ieee802_1x_kay_new_sak -
3511 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3513 struct ieee802_1x_mka_participant *participant;
3518 participant = ieee802_1x_kay_get_principal_participant(kay);
3522 participant->new_sak = TRUE;
3523 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3530 * ieee802_1x_kay_change_cipher_suite -
3533 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3535 struct ieee802_1x_mka_participant *participant;
3540 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3541 wpa_printf(MSG_ERROR,
3542 "KaY: Configured cipher suite index is out of range");
3545 if (kay->macsec_csindex == cs_index)
3549 kay->macsec_desired = FALSE;
3551 kay->macsec_csindex = cs_index;
3552 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3554 participant = ieee802_1x_kay_get_principal_participant(kay);
3556 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3557 participant->new_sak = TRUE;