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_get_potential_peer
305 static struct ieee802_1x_kay_peer *
306 ieee802_1x_kay_get_potential_peer(
307 struct ieee802_1x_mka_participant *participant, const u8 *mi)
309 return get_peer_mi(&participant->potential_peers, mi);
314 * ieee802_1x_kay_get_live_peer
316 static struct ieee802_1x_kay_peer *
317 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
320 return get_peer_mi(&participant->live_peers, mi);
325 * ieee802_1x_kay_is_in_potential_peer
328 ieee802_1x_kay_is_in_potential_peer(
329 struct ieee802_1x_mka_participant *participant, const u8 *mi)
331 return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
336 * ieee802_1x_kay_is_in_live_peer
339 ieee802_1x_kay_is_in_live_peer(
340 struct ieee802_1x_mka_participant *participant, const u8 *mi)
342 return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
347 * ieee802_1x_kay_is_in_peer
350 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
353 return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
354 ieee802_1x_kay_is_in_potential_peer(participant, mi);
359 * ieee802_1x_kay_get_peer
361 static struct ieee802_1x_kay_peer *
362 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
365 struct ieee802_1x_kay_peer *peer;
367 peer = ieee802_1x_kay_get_live_peer(participant, mi);
371 return ieee802_1x_kay_get_potential_peer(participant, mi);
376 * ieee802_1x_kay_get_cipher_suite
378 static struct macsec_ciphersuite *
379 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
384 for (i = 0; i < CS_TABLE_SIZE; i++) {
385 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
388 if (i >= CS_TABLE_SIZE)
391 return &cipher_suite_tbl[i];
395 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
396 const struct ieee802_1x_mka_sci *b)
398 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
403 * ieee802_1x_kay_get_peer_sci
405 static struct ieee802_1x_kay_peer *
406 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
407 const struct ieee802_1x_mka_sci *sci)
409 struct ieee802_1x_kay_peer *peer;
411 dl_list_for_each(peer, &participant->live_peers,
412 struct ieee802_1x_kay_peer, list) {
413 if (sci_equal(&peer->sci, sci))
417 dl_list_for_each(peer, &participant->potential_peers,
418 struct ieee802_1x_kay_peer, list) {
419 if (sci_equal(&peer->sci, sci))
428 * ieee802_1x_kay_init_receive_sa -
430 static struct receive_sa *
431 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
432 struct data_key *key)
434 struct receive_sa *psa;
439 psa = os_zalloc(sizeof(*psa));
441 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
446 psa->lowest_pn = lowest_pn;
447 psa->next_pn = lowest_pn;
451 os_get_time(&psa->created_time);
454 dl_list_add(&psc->sa_list, &psa->list);
455 wpa_printf(MSG_DEBUG,
456 "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
457 (int) an, lowest_pn, psc->channel);
464 * ieee802_1x_kay_deinit_receive_sa -
466 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
469 wpa_printf(MSG_DEBUG,
470 "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
471 psa->an, psa->sc->channel);
472 dl_list_del(&psa->list);
478 * ieee802_1x_kay_init_receive_sc -
480 static struct receive_sc *
481 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
484 struct receive_sc *psc;
489 psc = os_zalloc(sizeof(*psc));
491 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
495 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
496 psc->channel = channel;
498 os_get_time(&psc->created_time);
499 psc->receiving = FALSE;
501 dl_list_init(&psc->sa_list);
502 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
503 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
510 * ieee802_1x_kay_deinit_receive_sc -
513 ieee802_1x_kay_deinit_receive_sc(
514 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
516 struct receive_sa *psa, *pre_sa;
518 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
520 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
522 secy_disable_receive_sa(participant->kay, psa);
523 ieee802_1x_kay_deinit_receive_sa(psa);
525 dl_list_del(&psc->list);
531 * ieee802_1x_kay_create_live_peer
533 static struct ieee802_1x_kay_peer *
534 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
537 struct ieee802_1x_kay_peer *peer;
538 struct receive_sc *rxsc;
541 peer = os_zalloc(sizeof(*peer));
543 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
547 os_memcpy(peer->mi, mi, MI_LEN);
549 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
550 peer->sak_used = FALSE;
551 os_memcpy(&peer->sci, &participant->current_peer_sci,
554 secy_get_available_receive_sc(participant->kay, &sc_ch);
556 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
562 dl_list_add(&participant->live_peers, &peer->list);
563 dl_list_add(&participant->rxsc_list, &rxsc->list);
564 secy_create_receive_sc(participant->kay, rxsc);
566 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
567 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
568 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
569 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
570 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
577 * ieee802_1x_kay_create_potential_peer
579 static struct ieee802_1x_kay_peer *
580 ieee802_1x_kay_create_potential_peer(
581 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
583 struct ieee802_1x_kay_peer *peer;
585 peer = os_zalloc(sizeof(*peer));
587 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
591 os_memcpy(peer->mi, mi, MI_LEN);
593 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
594 peer->sak_used = FALSE;
596 dl_list_add(&participant->potential_peers, &peer->list);
598 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
599 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
600 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
601 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
602 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
609 * ieee802_1x_kay_move_live_peer
611 static struct ieee802_1x_kay_peer *
612 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
615 struct ieee802_1x_kay_peer *peer;
616 struct receive_sc *rxsc;
619 peer = ieee802_1x_kay_get_potential_peer(participant, mi);
621 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
626 os_memcpy(&peer->sci, &participant->current_peer_sci,
629 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
631 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
632 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
633 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
634 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
635 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
637 dl_list_del(&peer->list);
638 dl_list_add_tail(&participant->live_peers, &peer->list);
640 secy_get_available_receive_sc(participant->kay, &sc_ch);
642 dl_list_add(&participant->rxsc_list, &rxsc->list);
643 secy_create_receive_sc(participant->kay, rxsc);
651 * ieee802_1x_mka_basic_body_present -
654 ieee802_1x_mka_basic_body_present(
655 struct ieee802_1x_mka_participant *participant)
662 * ieee802_1x_mka_basic_body_length -
665 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
669 length = sizeof(struct ieee802_1x_mka_basic_body);
670 length += participant->ckn.len;
671 return (length + 0x3) & ~0x3;
676 * ieee802_1x_mka_encode_basic_body
679 ieee802_1x_mka_encode_basic_body(
680 struct ieee802_1x_mka_participant *participant,
683 struct ieee802_1x_mka_basic_body *body;
684 struct ieee802_1x_kay *kay = participant->kay;
685 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
687 body = wpabuf_put(buf, length);
689 body->version = kay->mka_version;
690 body->priority = kay->actor_priority;
691 if (participant->is_elected)
692 body->key_server = participant->is_key_server;
694 body->key_server = participant->can_be_key_server;
696 body->macsec_desired = kay->macsec_desired;
697 body->macsec_capability = kay->macsec_capable;
698 set_mka_param_body_len(body, length - MKA_HDR_LEN);
700 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
701 sizeof(kay->actor_sci.addr));
702 body->actor_sci.port = kay->actor_sci.port;
704 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
705 participant->mn = participant->mn + 1;
706 body->actor_mn = host_to_be32(participant->mn);
707 os_memcpy(body->algo_agility, participant->kay->algo_agility,
708 sizeof(body->algo_agility));
710 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
712 ieee802_1x_mka_dump_basic_body(body);
719 * ieee802_1x_mka_decode_basic_body -
721 static struct ieee802_1x_mka_participant *
722 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
725 struct ieee802_1x_mka_participant *participant;
726 const struct ieee802_1x_mka_basic_body *body;
727 struct ieee802_1x_kay_peer *peer;
729 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
731 if (body->version > MKA_VERSION_ID) {
732 wpa_printf(MSG_DEBUG,
733 "KaY: peer's version(%d) greater than mka current version(%d)",
734 body->version, MKA_VERSION_ID);
736 if (kay->is_obliged_key_server && body->key_server) {
737 wpa_printf(MSG_DEBUG, "I must be as key server");
741 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
743 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
747 /* If the peer's MI is my MI, I will choose new MI */
748 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
749 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
754 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
755 participant->current_peer_id.mn = body->actor_mn;
756 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
757 sizeof(participant->current_peer_sci.addr));
758 participant->current_peer_sci.port = body->actor_sci.port;
761 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
763 /* Check duplicated SCI */
764 /* TODO: What policy should be applied to detect duplicated SCI
765 * is active attacker or a valid peer whose MI is be changed?
767 peer = ieee802_1x_kay_get_peer_sci(participant,
770 wpa_printf(MSG_WARNING,
771 "KaY: duplicated SCI detected, Maybe active attacker");
772 dl_list_del(&peer->list);
776 peer = ieee802_1x_kay_create_potential_peer(
777 participant, body->actor_mi,
778 be_to_host32(body->actor_mn));
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;
786 } else if (peer->mn < be_to_host32(body->actor_mn)) {
787 peer->mn = be_to_host32(body->actor_mn);
788 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
789 peer->macsec_desired = body->macsec_desired;
790 peer->macsec_capability = body->macsec_capability;
791 peer->is_key_server = (Boolean) body->key_server;
792 peer->key_server_priority = body->priority;
794 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
803 * ieee802_1x_mka_live_peer_body_present
806 ieee802_1x_mka_live_peer_body_present(
807 struct ieee802_1x_mka_participant *participant)
809 return !dl_list_empty(&participant->live_peers);
814 * ieee802_1x_kay_get_live_peer_length
817 ieee802_1x_mka_get_live_peer_length(
818 struct ieee802_1x_mka_participant *participant)
820 int len = MKA_HDR_LEN;
821 struct ieee802_1x_kay_peer *peer;
823 dl_list_for_each(peer, &participant->live_peers,
824 struct ieee802_1x_kay_peer, list)
825 len += sizeof(struct ieee802_1x_mka_peer_id);
827 return (len + 0x3) & ~0x3;
832 * ieee802_1x_mka_encode_live_peer_body -
835 ieee802_1x_mka_encode_live_peer_body(
836 struct ieee802_1x_mka_participant *participant,
839 struct ieee802_1x_mka_peer_body *body;
840 struct ieee802_1x_kay_peer *peer;
842 struct ieee802_1x_mka_peer_id *body_peer;
844 length = ieee802_1x_mka_get_live_peer_length(participant);
845 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
847 body->type = MKA_LIVE_PEER_LIST;
848 set_mka_param_body_len(body, length - MKA_HDR_LEN);
850 dl_list_for_each(peer, &participant->live_peers,
851 struct ieee802_1x_kay_peer, list) {
852 body_peer = wpabuf_put(buf,
853 sizeof(struct ieee802_1x_mka_peer_id));
854 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
855 body_peer->mn = host_to_be32(peer->mn);
859 ieee802_1x_mka_dump_peer_body(body);
864 * ieee802_1x_mka_potential_peer_body_present
867 ieee802_1x_mka_potential_peer_body_present(
868 struct ieee802_1x_mka_participant *participant)
870 return !dl_list_empty(&participant->potential_peers);
875 * ieee802_1x_kay_get_potential_peer_length
878 ieee802_1x_mka_get_potential_peer_length(
879 struct ieee802_1x_mka_participant *participant)
881 int len = MKA_HDR_LEN;
882 struct ieee802_1x_kay_peer *peer;
884 dl_list_for_each(peer, &participant->potential_peers,
885 struct ieee802_1x_kay_peer, list)
886 len += sizeof(struct ieee802_1x_mka_peer_id);
888 return (len + 0x3) & ~0x3;
893 * ieee802_1x_mka_encode_potential_peer_body -
896 ieee802_1x_mka_encode_potential_peer_body(
897 struct ieee802_1x_mka_participant *participant,
900 struct ieee802_1x_mka_peer_body *body;
901 struct ieee802_1x_kay_peer *peer;
903 struct ieee802_1x_mka_peer_id *body_peer;
905 length = ieee802_1x_mka_get_potential_peer_length(participant);
906 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
908 body->type = MKA_POTENTIAL_PEER_LIST;
909 set_mka_param_body_len(body, length - MKA_HDR_LEN);
911 dl_list_for_each(peer, &participant->potential_peers,
912 struct ieee802_1x_kay_peer, list) {
913 body_peer = wpabuf_put(buf,
914 sizeof(struct ieee802_1x_mka_peer_id));
915 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
916 body_peer->mn = host_to_be32(peer->mn);
920 ieee802_1x_mka_dump_peer_body(body);
926 * ieee802_1x_mka_i_in_peerlist -
929 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
930 const u8 *mka_msg, size_t msg_len)
932 Boolean included = FALSE;
933 struct ieee802_1x_mka_hdr *hdr;
945 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
946 hdr = (struct ieee802_1x_mka_hdr *) pos;
947 body_len = get_mka_param_body_len(hdr);
948 body_type = get_mka_param_body_type(hdr);
950 if (body_type != MKA_LIVE_PEER_LIST &&
951 body_type != MKA_POTENTIAL_PEER_LIST)
954 ieee802_1x_mka_dump_peer_body(
955 (struct ieee802_1x_mka_peer_body *)pos);
957 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
958 wpa_printf(MSG_ERROR,
959 "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",
960 (int) left_len, (int) MKA_HDR_LEN,
961 (int) body_len, DEFAULT_ICV_LEN);
965 if ((body_len % 16) != 0) {
966 wpa_printf(MSG_ERROR,
967 "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
972 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
973 peer_mi = MKA_HDR_LEN + pos + i;
974 os_memcpy(&_peer_mn, peer_mi + MI_LEN,
976 peer_mn = be_to_host32(_peer_mn);
977 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
978 peer_mn == participant->mn) {
988 left_len -= body_len + MKA_HDR_LEN;
989 pos += body_len + MKA_HDR_LEN;
997 * ieee802_1x_mka_decode_live_peer_body -
999 static int ieee802_1x_mka_decode_live_peer_body(
1000 struct ieee802_1x_mka_participant *participant,
1001 const u8 *peer_msg, size_t msg_len)
1003 const struct ieee802_1x_mka_hdr *hdr;
1004 struct ieee802_1x_kay_peer *peer;
1010 Boolean is_included;
1012 is_included = ieee802_1x_kay_is_in_live_peer(
1013 participant, participant->current_peer_id.mi);
1015 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1016 body_len = get_mka_param_body_len(hdr);
1017 if (body_len % 16 != 0) {
1018 wpa_printf(MSG_ERROR,
1019 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1024 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1025 peer_mi = MKA_HDR_LEN + peer_msg + i;
1026 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1027 peer_mn = be_to_host32(_peer_mn);
1030 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1031 /* My message id is used by other participant */
1032 if (peer_mn > participant->mn) {
1033 if (os_get_random(participant->mi,
1034 sizeof(participant->mi)) < 0)
1035 wpa_printf(MSG_DEBUG,
1036 "KaY: Could not update mi");
1037 participant->mn = 0;
1044 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1047 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1049 if (!ieee802_1x_kay_create_potential_peer(
1050 participant, peer_mi, peer_mn)) {
1061 * ieee802_1x_mka_decode_potential_peer_body -
1064 ieee802_1x_mka_decode_potential_peer_body(
1065 struct ieee802_1x_mka_participant *participant,
1066 const u8 *peer_msg, size_t msg_len)
1068 struct ieee802_1x_mka_hdr *hdr;
1075 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1076 body_len = get_mka_param_body_len(hdr);
1077 if (body_len % 16 != 0) {
1078 wpa_printf(MSG_ERROR,
1079 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1084 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1085 peer_mi = MKA_HDR_LEN + peer_msg + i;
1086 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1087 peer_mn = be_to_host32(_peer_mn);
1090 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1091 /* My message id is used by other participant */
1092 if (peer_mn > participant->mn) {
1093 if (os_get_random(participant->mi,
1094 sizeof(participant->mi)) < 0)
1095 wpa_printf(MSG_DEBUG,
1096 "KaY: Could not update mi");
1097 participant->mn = 0;
1108 * ieee802_1x_mka_sak_use_body_present
1111 ieee802_1x_mka_sak_use_body_present(
1112 struct ieee802_1x_mka_participant *participant)
1114 if (participant->to_use_sak)
1122 * ieee802_1x_mka_get_sak_use_length
1125 ieee802_1x_mka_get_sak_use_length(
1126 struct ieee802_1x_mka_participant *participant)
1128 int length = MKA_HDR_LEN;
1130 if (participant->kay->macsec_desired && participant->advised_desired)
1131 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1133 length = MKA_HDR_LEN;
1135 length = (length + 0x3) & ~0x3;
1145 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1146 struct ieee802_1x_mka_ki *ki)
1148 struct receive_sa *rxsa;
1149 struct receive_sc *rxsc;
1152 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1153 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1155 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1156 secy_get_receive_lowest_pn(principal->kay,
1159 lpn = lpn > rxsa->lowest_pn ?
1160 lpn : rxsa->lowest_pn;
1174 * ieee802_1x_mka_encode_sak_use_body -
1177 ieee802_1x_mka_encode_sak_use_body(
1178 struct ieee802_1x_mka_participant *participant,
1181 struct ieee802_1x_mka_sak_use_body *body;
1182 unsigned int length;
1185 length = ieee802_1x_mka_get_sak_use_length(participant);
1186 body = wpabuf_put(buf, length);
1188 body->type = MKA_SAK_USE;
1189 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1191 if (length == MKA_HDR_LEN) {
1197 body->delay_protect = FALSE;
1201 /* data protect, lowest accept packet number */
1202 body->delay_protect = participant->kay->macsec_replay_protect;
1203 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1204 if (pn > participant->kay->pn_exhaustion) {
1205 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1206 if (participant->is_key_server)
1207 participant->new_sak = TRUE;
1210 body->llpn = host_to_be32(pn);
1211 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1212 body->olpn = host_to_be32(pn);
1214 /* plain tx, plain rx */
1215 if (participant->kay->macsec_protect)
1220 if (participant->kay->macsec_validate == Strict)
1225 /* latest key: rx, tx, key server member identifier key number */
1226 body->lan = participant->lan;
1227 os_memcpy(body->lsrv_mi, participant->lki.mi,
1228 sizeof(body->lsrv_mi));
1229 body->lkn = host_to_be32(participant->lki.kn);
1230 body->lrx = participant->lrx;
1231 body->ltx = participant->ltx;
1233 /* old key: rx, tx, key server member identifier key number */
1234 body->oan = participant->oan;
1235 if (participant->oki.kn != participant->lki.kn &&
1236 participant->oki.kn != 0) {
1239 os_memcpy(body->osrv_mi, participant->oki.mi,
1240 sizeof(body->osrv_mi));
1241 body->okn = host_to_be32(participant->oki.kn);
1247 /* set CP's variable */
1249 if (!participant->kay->tx_enable)
1250 participant->kay->tx_enable = TRUE;
1252 if (!participant->kay->port_enable)
1253 participant->kay->port_enable = TRUE;
1256 if (!participant->kay->rx_enable)
1257 participant->kay->rx_enable = TRUE;
1260 ieee802_1x_mka_dump_sak_use_body(body);
1266 * ieee802_1x_mka_decode_sak_use_body -
1269 ieee802_1x_mka_decode_sak_use_body(
1270 struct ieee802_1x_mka_participant *participant,
1271 const u8 *mka_msg, size_t msg_len)
1273 struct ieee802_1x_mka_hdr *hdr;
1274 struct ieee802_1x_mka_sak_use_body *body;
1275 struct ieee802_1x_kay_peer *peer;
1276 struct transmit_sa *txsa;
1277 struct data_key *sa_key = NULL;
1279 struct ieee802_1x_mka_ki ki;
1281 Boolean all_receiving;
1284 if (!participant->principal) {
1285 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1288 peer = ieee802_1x_kay_get_live_peer(participant,
1289 participant->current_peer_id.mi);
1291 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1295 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1296 body_len = get_mka_param_body_len(hdr);
1297 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1298 ieee802_1x_mka_dump_sak_use_body(body);
1300 if ((body_len != 0) && (body_len < 40)) {
1301 wpa_printf(MSG_ERROR,
1302 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1307 /* TODO: what action should I take when peer does not support MACsec */
1308 if (body_len == 0) {
1309 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1313 /* TODO: when the plain tx or rx of peer is true, should I change
1314 * the attribute of controlled port
1317 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1320 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1322 /* check latest key is valid */
1323 if (body->ltx || body->lrx) {
1325 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1326 ki.kn = be_to_host32(body->lkn);
1327 dl_list_for_each(sa_key, &participant->sak_list,
1328 struct data_key, list) {
1329 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1335 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1338 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1339 sizeof(participant->lki.mi)) == 0 &&
1340 be_to_host32(body->lkn) == participant->lki.kn &&
1341 body->lan == participant->lan) {
1342 peer->sak_used = TRUE;
1344 if (body->ltx && peer->is_key_server) {
1345 ieee802_1x_cp_set_servertransmitting(
1346 participant->kay->cp, TRUE);
1347 ieee802_1x_cp_sm_step(participant->kay->cp);
1351 /* check old key is valid */
1352 if (body->otx || body->orx) {
1353 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1354 sizeof(participant->oki.mi)) != 0 ||
1355 be_to_host32(body->okn) != participant->oki.kn ||
1356 body->oan != participant->oan) {
1357 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1362 /* TODO: how to set the MACsec hardware when delay_protect is true */
1363 if (body->delay_protect &&
1364 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1365 wpa_printf(MSG_WARNING,
1366 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1370 /* check all live peer have used the sak for receiving sa */
1371 all_receiving = TRUE;
1372 dl_list_for_each(peer, &participant->live_peers,
1373 struct ieee802_1x_kay_peer, list) {
1374 if (!peer->sak_used) {
1375 all_receiving = FALSE;
1379 if (all_receiving) {
1380 participant->to_dist_sak = FALSE;
1381 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1382 ieee802_1x_cp_sm_step(participant->kay->cp);
1385 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1386 lpn = be_to_host32(body->llpn);
1387 if (lpn > participant->kay->pn_exhaustion) {
1388 if (participant->is_key_server) {
1389 participant->new_sak = TRUE;
1390 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1395 dl_list_for_each(txsa, &participant->txsc->sa_list,
1396 struct transmit_sa, list) {
1397 if (sa_key != NULL && txsa->pkey == sa_key) {
1403 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1407 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1408 * npn is larger than txsa's npn, set it to txsa.
1410 secy_get_transmit_next_pn(participant->kay, txsa);
1411 if (lpn > txsa->next_pn) {
1412 secy_set_transmit_next_pn(participant->kay, txsa);
1413 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1421 * ieee802_1x_mka_dist_sak_body_present
1424 ieee802_1x_mka_dist_sak_body_present(
1425 struct ieee802_1x_mka_participant *participant)
1427 if (!participant->to_dist_sak || !participant->new_key)
1435 * ieee802_1x_kay_get_dist_sak_length
1438 ieee802_1x_mka_get_dist_sak_length(
1439 struct ieee802_1x_mka_participant *participant)
1442 int cs_index = participant->kay->macsec_csindex;
1444 if (participant->advised_desired) {
1445 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1446 if (cs_index != DEFAULT_CS_INDEX)
1447 length += CS_ID_LEN;
1449 length += cipher_suite_tbl[cs_index].sak_len + 8;
1451 length = MKA_HDR_LEN;
1453 length = (length + 0x3) & ~0x3;
1460 * ieee802_1x_mka_encode_dist_sak_body -
1463 ieee802_1x_mka_encode_dist_sak_body(
1464 struct ieee802_1x_mka_participant *participant,
1467 struct ieee802_1x_mka_dist_sak_body *body;
1468 struct data_key *sak;
1469 unsigned int length;
1473 length = ieee802_1x_mka_get_dist_sak_length(participant);
1474 body = wpabuf_put(buf, length);
1475 body->type = MKA_DISTRIBUTED_SAK;
1476 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1477 if (length == MKA_HDR_LEN) {
1478 body->confid_offset = 0;
1483 sak = participant->new_key;
1484 body->confid_offset = sak->confidentiality_offset;
1485 body->dan = sak->an;
1486 body->kn = host_to_be32(sak->key_identifier.kn);
1487 cs_index = participant->kay->macsec_csindex;
1489 if (cs_index != DEFAULT_CS_INDEX) {
1490 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1491 sak_pos = CS_ID_LEN;
1493 if (aes_wrap(participant->kek.key, 16,
1494 cipher_suite_tbl[cs_index].sak_len / 8,
1495 sak->key, body->sak + sak_pos)) {
1496 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1500 ieee802_1x_mka_dump_dist_sak_body(body);
1507 * ieee802_1x_kay_init_data_key -
1509 static struct data_key *
1510 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1512 struct data_key *pkey;
1517 pkey = os_zalloc(sizeof(*pkey));
1519 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1523 pkey->key = os_zalloc(conf->key_len);
1524 if (pkey->key == NULL) {
1525 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1530 os_memcpy(pkey->key, conf->key, conf->key_len);
1531 os_memcpy(&pkey->key_identifier, &conf->ki,
1532 sizeof(pkey->key_identifier));
1533 pkey->confidentiality_offset = conf->offset;
1534 pkey->an = conf->an;
1535 pkey->transmits = conf->tx;
1536 pkey->receives = conf->rx;
1537 os_get_time(&pkey->created_time);
1546 * ieee802_1x_kay_decode_dist_sak_body -
1549 ieee802_1x_mka_decode_dist_sak_body(
1550 struct ieee802_1x_mka_participant *participant,
1551 const u8 *mka_msg, size_t msg_len)
1553 struct ieee802_1x_mka_hdr *hdr;
1554 struct ieee802_1x_mka_dist_sak_body *body;
1555 struct ieee802_1x_kay_peer *peer;
1556 struct macsec_ciphersuite *cs;
1558 struct key_conf *conf;
1559 struct data_key *sa_key = NULL;
1560 struct ieee802_1x_mka_ki sak_ki;
1565 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1566 body_len = get_mka_param_body_len(hdr);
1567 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1568 wpa_printf(MSG_ERROR,
1569 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1574 if (!participant->principal) {
1575 wpa_printf(MSG_ERROR,
1576 "KaY: I can't accept the distributed SAK as I am not principal");
1579 if (participant->is_key_server) {
1580 wpa_printf(MSG_ERROR,
1581 "KaY: I can't accept the distributed SAK as myself is key server ");
1584 if (!participant->kay->macsec_desired ||
1585 participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1586 wpa_printf(MSG_ERROR,
1587 "KaY: I am not MACsec-desired or without MACsec capable");
1591 peer = ieee802_1x_kay_get_live_peer(participant,
1592 participant->current_peer_id.mi);
1594 wpa_printf(MSG_ERROR,
1595 "KaY: The key server is not in my live peers list");
1598 if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
1599 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1602 if (body_len == 0) {
1603 participant->kay->authenticated = TRUE;
1604 participant->kay->secured = FALSE;
1605 participant->kay->failed = FALSE;
1606 participant->advised_desired = FALSE;
1607 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1608 ieee802_1x_cp_sm_step(participant->kay->cp);
1609 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1610 participant->to_use_sak = TRUE;
1613 participant->advised_desired = TRUE;
1614 participant->kay->authenticated = FALSE;
1615 participant->kay->secured = TRUE;
1616 participant->kay->failed = FALSE;
1617 ieee802_1x_cp_connect_secure(participant->kay->cp);
1618 ieee802_1x_cp_sm_step(participant->kay->cp);
1620 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1621 ieee802_1x_mka_dump_dist_sak_body(body);
1622 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1624 if (os_memcmp(sa_key->key_identifier.mi,
1625 participant->current_peer_id.mi, MI_LEN) == 0 &&
1626 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1627 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1631 if (body_len == 28) {
1632 sak_len = DEFAULT_SA_KEY_LEN;
1633 wrap_sak = body->sak;
1634 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1636 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1638 wpa_printf(MSG_ERROR,
1639 "KaY: I can't support the Cipher Suite advised by key server");
1642 sak_len = cs->sak_len;
1643 wrap_sak = body->sak + CS_ID_LEN;
1644 participant->kay->macsec_csindex = cs->index;
1647 unwrap_sak = os_zalloc(sak_len);
1649 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1652 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1654 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1655 os_free(unwrap_sak);
1658 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1660 conf = os_zalloc(sizeof(*conf));
1662 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1663 os_free(unwrap_sak);
1666 conf->key_len = sak_len;
1668 conf->key = os_zalloc(conf->key_len);
1670 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1671 os_free(unwrap_sak);
1676 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1678 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1680 sak_ki.kn = be_to_host32(body->kn);
1682 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1683 conf->ki.kn = sak_ki.kn;
1684 conf->an = body->dan;
1685 conf->offset = body->confid_offset;
1689 sa_key = ieee802_1x_kay_init_data_key(conf);
1691 os_free(unwrap_sak);
1697 dl_list_add(&participant->sak_list, &sa_key->list);
1699 ieee802_1x_cp_set_ciphersuite(
1700 participant->kay->cp,
1701 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1702 ieee802_1x_cp_sm_step(participant->kay->cp);
1703 ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1704 ieee802_1x_cp_sm_step(participant->kay->cp);
1705 ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1706 ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1707 ieee802_1x_cp_signal_newsak(participant->kay->cp);
1708 ieee802_1x_cp_sm_step(participant->kay->cp);
1710 participant->to_use_sak = TRUE;
1712 os_free(unwrap_sak);
1721 * ieee802_1x_mka_icv_body_present
1724 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1731 * ieee802_1x_kay_get_icv_length
1734 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1738 length = sizeof(struct ieee802_1x_mka_icv_body);
1739 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1741 return (length + 0x3) & ~0x3;
1746 * ieee802_1x_mka_encode_icv_body -
1749 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1752 struct ieee802_1x_mka_icv_body *body;
1753 unsigned int length;
1754 u8 cmac[MAX_ICV_LEN];
1756 length = ieee802_1x_mka_get_icv_length(participant);
1757 if (length != DEFAULT_ICV_LEN) {
1758 body = wpabuf_put(buf, MKA_HDR_LEN);
1759 body->type = MKA_ICV_INDICATOR;
1760 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1763 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1764 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1765 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1769 if (length != DEFAULT_ICV_LEN) {
1770 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1771 length - MKA_HDR_LEN);
1773 os_memcpy(wpabuf_put(buf, length), cmac, length);
1780 * ieee802_1x_mka_decode_icv_body -
1783 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1784 const u8 *mka_msg, size_t msg_len)
1786 struct ieee802_1x_mka_hdr *hdr;
1787 struct ieee802_1x_mka_icv_body *body;
1795 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1796 hdr = (struct ieee802_1x_mka_hdr *) pos;
1797 body_len = get_mka_param_body_len(hdr);
1798 body_type = get_mka_param_body_type(hdr);
1800 if (left_len < (body_len + MKA_HDR_LEN))
1803 if (body_type != MKA_ICV_INDICATOR) {
1804 left_len -= MKA_HDR_LEN + body_len;
1805 pos += MKA_HDR_LEN + body_len;
1809 body = (struct ieee802_1x_mka_icv_body *)pos;
1811 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1818 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1823 * ieee802_1x_mka_decode_dist_cak_body-
1826 ieee802_1x_mka_decode_dist_cak_body(
1827 struct ieee802_1x_mka_participant *participant,
1828 const u8 *mka_msg, size_t msg_len)
1830 struct ieee802_1x_mka_hdr *hdr;
1833 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1834 body_len = get_mka_param_body_len(hdr);
1835 if (body_len < 28) {
1836 wpa_printf(MSG_ERROR,
1837 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1847 * ieee802_1x_mka_decode_kmd_body -
1850 ieee802_1x_mka_decode_kmd_body(
1851 struct ieee802_1x_mka_participant *participant,
1852 const u8 *mka_msg, size_t msg_len)
1854 struct ieee802_1x_mka_hdr *hdr;
1857 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1858 body_len = get_mka_param_body_len(hdr);
1860 wpa_printf(MSG_ERROR,
1861 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1871 * ieee802_1x_mka_decode_announce_body -
1873 static int ieee802_1x_mka_decode_announce_body(
1874 struct ieee802_1x_mka_participant *participant,
1875 const u8 *mka_msg, size_t msg_len)
1881 static struct mka_param_body_handler mka_body_handler[] = {
1882 /* basic parameter set */
1884 ieee802_1x_mka_encode_basic_body,
1886 ieee802_1x_mka_basic_body_length,
1887 ieee802_1x_mka_basic_body_present
1890 /* live peer list parameter set */
1892 ieee802_1x_mka_encode_live_peer_body,
1893 ieee802_1x_mka_decode_live_peer_body,
1894 ieee802_1x_mka_get_live_peer_length,
1895 ieee802_1x_mka_live_peer_body_present
1898 /* potential peer list parameter set */
1900 ieee802_1x_mka_encode_potential_peer_body,
1901 ieee802_1x_mka_decode_potential_peer_body,
1902 ieee802_1x_mka_get_potential_peer_length,
1903 ieee802_1x_mka_potential_peer_body_present
1906 /* sak use parameter set */
1908 ieee802_1x_mka_encode_sak_use_body,
1909 ieee802_1x_mka_decode_sak_use_body,
1910 ieee802_1x_mka_get_sak_use_length,
1911 ieee802_1x_mka_sak_use_body_present
1914 /* distribute sak parameter set */
1916 ieee802_1x_mka_encode_dist_sak_body,
1917 ieee802_1x_mka_decode_dist_sak_body,
1918 ieee802_1x_mka_get_dist_sak_length,
1919 ieee802_1x_mka_dist_sak_body_present
1922 /* distribute cak parameter set */
1925 ieee802_1x_mka_decode_dist_cak_body,
1930 /* kmd parameter set */
1933 ieee802_1x_mka_decode_kmd_body,
1938 /* announce parameter set */
1941 ieee802_1x_mka_decode_announce_body,
1946 /* icv parameter set */
1948 ieee802_1x_mka_encode_icv_body,
1950 ieee802_1x_mka_get_icv_length,
1951 ieee802_1x_mka_icv_body_present
1957 * ieee802_1x_kay_deinit_data_key -
1959 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1968 dl_list_del(&pkey->list);
1975 * ieee802_1x_kay_generate_new_sak -
1978 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1980 struct data_key *sa_key = NULL;
1981 struct key_conf *conf;
1982 struct ieee802_1x_kay_peer *peer;
1983 struct ieee802_1x_kay *kay = participant->kay;
1984 int ctx_len, ctx_offset;
1987 /* check condition for generating a fresh SAK:
1988 * must have one live peer
1989 * and MKA life time elapse since last distribution
1990 * or potential peer is empty
1992 if (dl_list_empty(&participant->live_peers)) {
1993 wpa_printf(MSG_ERROR,
1994 "KaY: Live peers list must not empty when generating fresh SAK");
1998 /* FIXME: A fresh SAK not generated until
1999 * the live peer list contains at least one peer and
2000 * MKA life time has elapsed since the prior SAK was first distributed,
2001 * or the Key server's potential peer is empty
2002 * but I can't understand the second item, so
2003 * here only check first item and ingore
2004 * && (!dl_list_empty(&participant->potential_peers))) {
2006 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
2007 wpa_printf(MSG_ERROR,
2008 "KaY: Life time have not elapsed since prior SAK distributed");
2012 conf = os_zalloc(sizeof(*conf));
2014 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2017 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2019 conf->key = os_zalloc(conf->key_len);
2022 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2026 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2027 dl_list_for_each(peer, &participant->live_peers,
2028 struct ieee802_1x_kay_peer, list)
2029 ctx_len += sizeof(peer->mi);
2030 ctx_len += sizeof(participant->mi);
2032 context = os_zalloc(ctx_len);
2039 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2045 ctx_offset += conf->key_len;
2046 dl_list_for_each(peer, &participant->live_peers,
2047 struct ieee802_1x_kay_peer, list) {
2048 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2049 ctx_offset += sizeof(peer->mi);
2051 os_memcpy(context + ctx_offset, participant->mi,
2052 sizeof(participant->mi));
2053 ctx_offset += sizeof(participant->mi);
2054 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2056 if (conf->key_len == 16) {
2057 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2058 context, ctx_len, conf->key);
2059 } else if (conf->key_len == 32) {
2060 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2061 context, ctx_len, conf->key);
2063 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2069 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2070 conf->key, conf->key_len);
2072 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2073 conf->ki.kn = participant->kay->dist_kn;
2074 conf->an = participant->kay->dist_an;
2075 conf->offset = kay->macsec_confidentiality;
2079 sa_key = ieee802_1x_kay_init_data_key(conf);
2086 participant->new_key = sa_key;
2088 dl_list_add(&participant->sak_list, &sa_key->list);
2089 ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2090 cipher_suite_tbl[kay->macsec_csindex].id);
2091 ieee802_1x_cp_sm_step(kay->cp);
2092 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2093 ieee802_1x_cp_sm_step(kay->cp);
2094 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2095 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2096 ieee802_1x_cp_signal_newsak(kay->cp);
2097 ieee802_1x_cp_sm_step(kay->cp);
2099 dl_list_for_each(peer, &participant->live_peers,
2100 struct ieee802_1x_kay_peer, list)
2101 peer->sak_used = FALSE;
2103 participant->kay->dist_kn++;
2104 participant->kay->dist_an++;
2105 if (participant->kay->dist_an > 3)
2106 participant->kay->dist_an = 0;
2108 participant->kay->dist_time = time(NULL);
2118 * ieee802_1x_kay_elect_key_server - elect the key server
2119 * when to elect: whenever the live peers list changes
2122 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2124 struct ieee802_1x_kay_peer *peer;
2125 struct ieee802_1x_kay_peer *key_server = NULL;
2126 struct ieee802_1x_kay *kay = participant->kay;
2127 Boolean i_is_key_server;
2129 if (participant->is_obliged_key_server) {
2130 participant->new_sak = TRUE;
2131 participant->to_dist_sak = FALSE;
2132 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2136 /* elect the key server among the peers */
2137 dl_list_for_each(peer, &participant->live_peers,
2138 struct ieee802_1x_kay_peer, list) {
2139 if (!peer->is_key_server)
2147 if (peer->key_server_priority <
2148 key_server->key_server_priority) {
2150 } else if (peer->key_server_priority ==
2151 key_server->key_server_priority) {
2152 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2158 /* elect the key server between me and the above elected peer */
2159 i_is_key_server = FALSE;
2160 if (key_server && participant->can_be_key_server) {
2161 if (kay->actor_priority
2162 < key_server->key_server_priority) {
2163 i_is_key_server = TRUE;
2164 } else if (kay->actor_priority
2165 == key_server->key_server_priority) {
2166 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2168 i_is_key_server = TRUE;
2170 } else if (participant->can_be_key_server) {
2171 i_is_key_server = TRUE;
2174 if (i_is_key_server) {
2175 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2176 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2177 ieee802_1x_cp_signal_chgdserver(kay->cp);
2178 ieee802_1x_cp_sm_step(kay->cp);
2181 participant->is_key_server = TRUE;
2182 participant->principal = TRUE;
2183 participant->new_sak = TRUE;
2184 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2185 participant->to_dist_sak = FALSE;
2186 participant->is_elected = TRUE;
2188 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2189 sizeof(kay->key_server_sci));
2190 kay->key_server_priority = kay->actor_priority;
2191 } else if (key_server) {
2192 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2193 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2194 ieee802_1x_cp_signal_chgdserver(kay->cp);
2195 ieee802_1x_cp_sm_step(kay->cp);
2198 participant->is_key_server = FALSE;
2199 participant->principal = TRUE;
2200 participant->is_elected = TRUE;
2202 os_memcpy(&kay->key_server_sci, &key_server->sci,
2203 sizeof(kay->key_server_sci));
2204 kay->key_server_priority = key_server->key_server_priority;
2206 participant->principal = FALSE;
2207 participant->is_key_server = FALSE;
2208 participant->is_elected = FALSE;
2216 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2217 * how to use MACsec: whether use MACsec and its capability
2218 * protectFrames will be advised if the key server and one of its live peers are
2219 * MACsec capable and one of those request MACsec protection
2222 ieee802_1x_kay_decide_macsec_use(
2223 struct ieee802_1x_mka_participant *participant)
2225 struct ieee802_1x_kay *kay = participant->kay;
2226 struct ieee802_1x_kay_peer *peer;
2227 enum macsec_cap less_capability;
2230 if (!participant->is_key_server)
2233 /* key server self is MACsec-desired and requesting MACsec */
2234 if (!kay->macsec_desired) {
2235 participant->advised_desired = FALSE;
2238 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2239 participant->advised_desired = FALSE;
2242 less_capability = kay->macsec_capable;
2244 /* at least one of peers is MACsec-desired and requesting MACsec */
2246 dl_list_for_each(peer, &participant->live_peers,
2247 struct ieee802_1x_kay_peer, list) {
2248 if (!peer->macsec_desired)
2251 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2254 less_capability = (less_capability < peer->macsec_capability) ?
2255 less_capability : peer->macsec_capability;
2260 participant->advised_desired = TRUE;
2261 participant->advised_capability = less_capability;
2262 kay->authenticated = FALSE;
2263 kay->secured = TRUE;
2264 kay->failed = FALSE;
2265 ieee802_1x_cp_connect_secure(kay->cp);
2266 ieee802_1x_cp_sm_step(kay->cp);
2268 participant->advised_desired = FALSE;
2269 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2270 participant->to_use_sak = FALSE;
2271 kay->authenticated = TRUE;
2272 kay->secured = FALSE;
2273 kay->failed = FALSE;
2282 ieee802_1x_cp_connect_authenticated(kay->cp);
2283 ieee802_1x_cp_sm_step(kay->cp);
2289 static const u8 pae_group_addr[ETH_ALEN] = {
2290 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2295 * ieee802_1x_kay_encode_mkpdu -
2298 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2299 struct wpabuf *pbuf)
2302 struct ieee8023_hdr *ether_hdr;
2303 struct ieee802_1x_hdr *eapol_hdr;
2305 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2306 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2307 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2308 sizeof(ether_hdr->dest));
2309 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2311 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2312 eapol_hdr->version = EAPOL_VERSION;
2313 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2314 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2316 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2317 if (mka_body_handler[i].body_present &&
2318 mka_body_handler[i].body_present(participant)) {
2319 if (mka_body_handler[i].body_tx(participant, pbuf))
2328 * ieee802_1x_participant_send_mkpdu -
2331 ieee802_1x_participant_send_mkpdu(
2332 struct ieee802_1x_mka_participant *participant)
2335 struct ieee802_1x_kay *kay = participant->kay;
2339 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2340 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2341 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2342 if (mka_body_handler[i].body_present &&
2343 mka_body_handler[i].body_present(participant))
2344 length += mka_body_handler[i].body_length(participant);
2347 buf = wpabuf_alloc(length);
2349 wpa_printf(MSG_ERROR, "KaY: out of memory");
2353 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2354 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2358 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2362 participant->active = TRUE;
2368 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2370 * ieee802_1x_participant_timer -
2372 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2374 struct ieee802_1x_mka_participant *participant;
2375 struct ieee802_1x_kay *kay;
2376 struct ieee802_1x_kay_peer *peer, *pre_peer;
2377 time_t now = time(NULL);
2379 struct receive_sc *rxsc, *pre_rxsc;
2380 struct transmit_sa *txsa, *pre_txsa;
2382 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2383 kay = participant->kay;
2384 if (participant->cak_life) {
2385 if (now > participant->cak_life) {
2386 kay->authenticated = FALSE;
2387 kay->secured = FALSE;
2389 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2394 /* should delete MKA instance if there are not live peers
2395 * when the MKA life elapsed since its creating */
2396 if (participant->mka_life) {
2397 if (dl_list_empty(&participant->live_peers)) {
2398 if (now > participant->mka_life) {
2399 kay->authenticated = FALSE;
2400 kay->secured = FALSE;
2402 ieee802_1x_kay_delete_mka(kay,
2407 participant->mka_life = 0;
2412 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2413 struct ieee802_1x_kay_peer, list) {
2414 if (now > peer->expire) {
2415 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2416 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2418 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2419 dl_list_for_each_safe(rxsc, pre_rxsc,
2420 &participant->rxsc_list,
2421 struct receive_sc, list) {
2422 if (sci_equal(&rxsc->sci, &peer->sci)) {
2423 secy_delete_receive_sc(kay, rxsc);
2424 ieee802_1x_kay_deinit_receive_sc(
2428 dl_list_del(&peer->list);
2435 if (dl_list_empty(&participant->live_peers)) {
2436 participant->advised_desired = FALSE;
2437 participant->advised_capability =
2438 MACSEC_CAP_NOT_IMPLEMENTED;
2439 participant->to_use_sak = FALSE;
2440 kay->authenticated = TRUE;
2441 kay->secured = FALSE;
2442 kay->failed = FALSE;
2451 dl_list_for_each_safe(txsa, pre_txsa,
2452 &participant->txsc->sa_list,
2453 struct transmit_sa, list) {
2454 secy_disable_transmit_sa(kay, txsa);
2455 ieee802_1x_kay_deinit_transmit_sa(txsa);
2458 ieee802_1x_cp_connect_authenticated(kay->cp);
2459 ieee802_1x_cp_sm_step(kay->cp);
2461 ieee802_1x_kay_elect_key_server(participant);
2462 ieee802_1x_kay_decide_macsec_use(participant);
2466 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2467 struct ieee802_1x_kay_peer, list) {
2468 if (now > peer->expire) {
2469 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2470 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2472 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2473 dl_list_del(&peer->list);
2478 if (participant->new_sak) {
2479 if (!ieee802_1x_kay_generate_new_sak(participant))
2480 participant->to_dist_sak = TRUE;
2482 participant->new_sak = FALSE;
2485 if (participant->retry_count < MAX_RETRY_CNT) {
2486 ieee802_1x_participant_send_mkpdu(participant);
2487 participant->retry_count++;
2490 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2491 ieee802_1x_participant_timer,
2497 * ieee802_1x_kay_init_transmit_sa -
2499 static struct transmit_sa *
2500 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2501 struct data_key *key)
2503 struct transmit_sa *psa;
2505 key->tx_latest = TRUE;
2506 key->rx_latest = TRUE;
2508 psa = os_zalloc(sizeof(*psa));
2510 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2514 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2515 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2516 psa->confidentiality = TRUE;
2518 psa->confidentiality = FALSE;
2522 psa->next_pn = next_PN;
2525 os_get_time(&psa->created_time);
2526 psa->in_use = FALSE;
2528 dl_list_add(&psc->sa_list, &psa->list);
2529 wpa_printf(MSG_DEBUG,
2530 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2531 (int) an, next_PN, psc->channel);
2538 * ieee802_1x_kay_deinit_transmit_sa -
2540 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2543 wpa_printf(MSG_DEBUG,
2544 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2545 psa->an, psa->sc->channel);
2546 dl_list_del(&psa->list);
2552 * init_transmit_sc -
2554 static struct transmit_sc *
2555 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2558 struct transmit_sc *psc;
2560 psc = os_zalloc(sizeof(*psc));
2562 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2565 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2566 psc->channel = channel;
2568 os_get_time(&psc->created_time);
2569 psc->transmitting = FALSE;
2570 psc->encoding_sa = FALSE;
2571 psc->enciphering_sa = FALSE;
2573 dl_list_init(&psc->sa_list);
2574 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2575 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2582 * ieee802_1x_kay_deinit_transmit_sc -
2585 ieee802_1x_kay_deinit_transmit_sc(
2586 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2588 struct transmit_sa *psa, *tmp;
2590 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2592 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2594 secy_disable_transmit_sa(participant->kay, psa);
2595 ieee802_1x_kay_deinit_transmit_sa(psa);
2602 /****************** Interface between CP and KAY *********************/
2604 * ieee802_1x_kay_set_latest_sa_attr -
2606 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2607 struct ieee802_1x_mka_ki *lki, u8 lan,
2608 Boolean ltx, Boolean lrx)
2610 struct ieee802_1x_mka_participant *principal;
2612 principal = ieee802_1x_kay_get_principal_participant(kay);
2617 os_memset(&principal->lki, 0, sizeof(principal->lki));
2619 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2621 principal->lan = lan;
2622 principal->ltx = ltx;
2623 principal->lrx = lrx;
2628 kay->ltx_kn = lki->kn;
2629 kay->lrx_kn = lki->kn;
2639 * ieee802_1x_kay_set_old_sa_attr -
2641 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2642 struct ieee802_1x_mka_ki *oki,
2643 u8 oan, Boolean otx, Boolean orx)
2645 struct ieee802_1x_mka_participant *principal;
2647 principal = ieee802_1x_kay_get_principal_participant(kay);
2652 os_memset(&principal->oki, 0, sizeof(principal->oki));
2654 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2656 principal->oan = oan;
2657 principal->otx = otx;
2658 principal->orx = orx;
2664 kay->otx_kn = oki->kn;
2665 kay->orx_kn = oki->kn;
2675 * ieee802_1x_kay_create_sas -
2677 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2678 struct ieee802_1x_mka_ki *lki)
2680 struct data_key *sa_key, *latest_sak;
2681 struct ieee802_1x_mka_participant *principal;
2682 struct receive_sc *rxsc;
2683 struct receive_sa *rxsa;
2684 struct transmit_sa *txsa;
2686 principal = ieee802_1x_kay_get_principal_participant(kay);
2691 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2692 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2693 sa_key->rx_latest = TRUE;
2694 sa_key->tx_latest = TRUE;
2695 latest_sak = sa_key;
2696 principal->to_use_sak = TRUE;
2698 sa_key->rx_latest = FALSE;
2699 sa_key->tx_latest = FALSE;
2703 wpa_printf(MSG_ERROR, "lki related sak not found");
2707 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2708 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2713 secy_create_receive_sa(kay, rxsa);
2716 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2721 secy_create_transmit_sa(kay, txsa);
2730 * ieee802_1x_kay_delete_sas -
2732 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2733 struct ieee802_1x_mka_ki *ki)
2735 struct data_key *sa_key, *pre_key;
2736 struct transmit_sa *txsa, *pre_txsa;
2737 struct receive_sa *rxsa, *pre_rxsa;
2738 struct receive_sc *rxsc;
2739 struct ieee802_1x_mka_participant *principal;
2741 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2742 principal = ieee802_1x_kay_get_principal_participant(kay);
2746 /* remove the transmit sa */
2747 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2748 struct transmit_sa, list) {
2749 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2750 secy_disable_transmit_sa(kay, txsa);
2751 ieee802_1x_kay_deinit_transmit_sa(txsa);
2755 /* remove the receive sa */
2756 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2757 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2758 struct receive_sa, list) {
2759 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2760 secy_disable_receive_sa(kay, rxsa);
2761 ieee802_1x_kay_deinit_receive_sa(rxsa);
2766 /* remove the sak */
2767 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2768 struct data_key, list) {
2769 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2770 ieee802_1x_kay_deinit_data_key(sa_key);
2773 if (principal->new_key == sa_key)
2774 principal->new_key = NULL;
2782 * ieee802_1x_kay_enable_tx_sas -
2784 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2785 struct ieee802_1x_mka_ki *lki)
2787 struct ieee802_1x_mka_participant *principal;
2788 struct transmit_sa *txsa;
2790 principal = ieee802_1x_kay_get_principal_participant(kay);
2794 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2796 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2797 txsa->in_use = TRUE;
2798 secy_enable_transmit_sa(kay, txsa);
2799 ieee802_1x_cp_set_usingtransmitas(
2800 principal->kay->cp, TRUE);
2801 ieee802_1x_cp_sm_step(principal->kay->cp);
2810 * ieee802_1x_kay_enable_rx_sas -
2812 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2813 struct ieee802_1x_mka_ki *lki)
2815 struct ieee802_1x_mka_participant *principal;
2816 struct receive_sa *rxsa;
2817 struct receive_sc *rxsc;
2819 principal = ieee802_1x_kay_get_principal_participant(kay);
2823 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2824 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2826 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2827 rxsa->in_use = TRUE;
2828 secy_enable_receive_sa(kay, rxsa);
2829 ieee802_1x_cp_set_usingreceivesas(
2830 principal->kay->cp, TRUE);
2831 ieee802_1x_cp_sm_step(principal->kay->cp);
2841 * ieee802_1x_kay_enable_new_info -
2843 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2845 struct ieee802_1x_mka_participant *principal;
2847 principal = ieee802_1x_kay_get_principal_participant(kay);
2851 if (principal->retry_count < MAX_RETRY_CNT) {
2852 ieee802_1x_participant_send_mkpdu(principal);
2853 principal->retry_count++;
2861 * ieee802_1x_kay_cp_conf -
2863 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2864 struct ieee802_1x_cp_conf *pconf)
2866 pconf->protect = kay->macsec_protect;
2867 pconf->replay_protect = kay->macsec_replay_protect;
2868 pconf->validate = kay->macsec_validate;
2875 * ieee802_1x_kay_alloc_cp_sm -
2877 static struct ieee802_1x_cp_sm *
2878 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2880 struct ieee802_1x_cp_conf conf;
2882 os_memset(&conf, 0, sizeof(conf));
2883 conf.protect = kay->macsec_protect;
2884 conf.replay_protect = kay->macsec_replay_protect;
2885 conf.validate = kay->macsec_validate;
2886 conf.replay_window = kay->macsec_replay_window;
2888 return ieee802_1x_cp_sm_init(kay, &conf);
2893 * ieee802_1x_kay_mkpdu_sanity_check -
2894 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2896 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2897 const u8 *buf, size_t len)
2899 struct ieee8023_hdr *eth_hdr;
2900 struct ieee802_1x_hdr *eapol_hdr;
2901 struct ieee802_1x_mka_hdr *mka_hdr;
2902 struct ieee802_1x_mka_basic_body *body;
2904 struct ieee802_1x_mka_participant *participant;
2906 u8 icv[MAX_ICV_LEN];
2909 eth_hdr = (struct ieee8023_hdr *) buf;
2910 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2911 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2913 /* destination address should be not individual address */
2914 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2915 wpa_printf(MSG_MSGDUMP,
2916 "KaY: ethernet destination address is not PAE group address");
2920 /* MKPDU should not less than 32 octets */
2921 mka_msg_len = be_to_host16(eapol_hdr->length);
2922 if (mka_msg_len < 32) {
2923 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2926 /* MKPDU should multiple 4 octets */
2927 if ((mka_msg_len % 4) != 0) {
2928 wpa_printf(MSG_MSGDUMP,
2929 "KaY: MKPDU is not multiple of 4 octets");
2933 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2934 ieee802_1x_mka_dump_basic_body(body);
2935 body_len = get_mka_param_body_len(body);
2936 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2937 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2938 wpa_printf(MSG_ERROR,
2939 "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",
2940 (int) mka_msg_len, (int) MKA_HDR_LEN,
2941 (int) body_len, DEFAULT_ICV_LEN);
2945 /* CKN should be owned by I */
2946 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2948 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2952 /* algorithm agility check */
2953 if (os_memcmp(body->algo_agility, mka_algo_agility,
2954 sizeof(body->algo_agility)) != 0) {
2955 wpa_printf(MSG_ERROR,
2956 "KaY: peer's algorithm agility not supported for me");
2962 * The ICV will comprise the final octets of the packet body, whatever
2963 * its size, not the fixed length 16 octets, indicated by the EAPOL
2964 * packet body length.
2966 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2967 participant->ick.key,
2968 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2969 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2972 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2976 if (os_memcmp_const(msg_icv, icv,
2977 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2979 wpa_printf(MSG_ERROR,
2980 "KaY: Computed ICV is not equal to Received ICV");
2984 wpa_printf(MSG_ERROR, "KaY: No ICV");
2993 * ieee802_1x_kay_decode_mkpdu -
2995 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2996 const u8 *buf, size_t len)
2998 struct ieee802_1x_mka_participant *participant;
2999 struct ieee802_1x_mka_hdr *hdr;
3005 Boolean my_included;
3006 Boolean handled[256];
3008 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
3011 /* handle basic parameter set */
3012 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
3013 left_len = len - sizeof(struct ieee8023_hdr) -
3014 sizeof(struct ieee802_1x_hdr);
3015 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3019 /* to skip basic parameter set */
3020 hdr = (struct ieee802_1x_mka_hdr *) pos;
3021 body_len = get_mka_param_body_len(hdr);
3022 pos += body_len + MKA_HDR_LEN;
3023 left_len -= body_len + MKA_HDR_LEN;
3025 /* check i am in the peer's peer list */
3026 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3028 /* accept the peer as live peer */
3029 if (!ieee802_1x_kay_is_in_peer(
3031 participant->current_peer_id.mi)) {
3032 if (!ieee802_1x_kay_create_live_peer(
3034 participant->current_peer_id.mi,
3036 participant->current_peer_id.mn)))
3038 ieee802_1x_kay_elect_key_server(participant);
3039 ieee802_1x_kay_decide_macsec_use(participant);
3041 if (ieee802_1x_kay_is_in_potential_peer(
3042 participant, participant->current_peer_id.mi)) {
3043 if (!ieee802_1x_kay_move_live_peer(
3045 participant->current_peer_id.mi,
3046 be_to_host32(participant->
3047 current_peer_id.mn)))
3049 ieee802_1x_kay_elect_key_server(participant);
3050 ieee802_1x_kay_decide_macsec_use(participant);
3055 * Handle other parameter set than basic parameter set.
3056 * Each parameter set should be present only once.
3058 for (i = 0; i < 256; i++)
3062 while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3063 hdr = (struct ieee802_1x_mka_hdr *) pos;
3064 body_len = get_mka_param_body_len(hdr);
3065 body_type = get_mka_param_body_type(hdr);
3067 if (body_type == MKA_ICV_INDICATOR)
3070 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3071 wpa_printf(MSG_ERROR,
3072 "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",
3073 (int) left_len, (int) MKA_HDR_LEN,
3074 (int) body_len, DEFAULT_ICV_LEN);
3078 if (handled[body_type])
3081 handled[body_type] = TRUE;
3082 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3083 mka_body_handler[body_type].body_rx) {
3084 mka_body_handler[body_type].body_rx
3085 (participant, pos, left_len);
3087 wpa_printf(MSG_ERROR,
3088 "The type %d not supported in this MKA version %d",
3089 body_type, MKA_VERSION_ID);
3093 pos += body_len + MKA_HDR_LEN;
3094 left_len -= body_len + MKA_HDR_LEN;
3098 participant->retry_count = 0;
3099 participant->active = TRUE;
3106 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3109 struct ieee802_1x_kay *kay = ctx;
3110 struct ieee8023_hdr *eth_hdr;
3111 struct ieee802_1x_hdr *eapol_hdr;
3113 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3114 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3115 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3116 (unsigned long) len);
3120 eth_hdr = (struct ieee8023_hdr *) buf;
3121 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3122 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3123 be_to_host16(eapol_hdr->length)) {
3124 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3125 (unsigned long) len,
3126 (unsigned long) be_to_host16(eapol_hdr->length));
3130 if (eapol_hdr->version < EAPOL_VERSION) {
3131 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3132 eapol_hdr->version);
3135 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3136 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3139 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3140 if (dl_list_empty(&kay->participant_list)) {
3141 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3145 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3150 * ieee802_1x_kay_init -
3152 struct ieee802_1x_kay *
3153 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3154 const char *ifname, const u8 *addr)
3156 struct ieee802_1x_kay *kay;
3158 kay = os_zalloc(sizeof(*kay));
3160 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3167 kay->active = FALSE;
3169 kay->authenticated = FALSE;
3170 kay->secured = FALSE;
3171 kay->failed = FALSE;
3172 kay->policy = policy;
3174 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3175 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3176 kay->actor_sci.port = host_to_be16(0x0001);
3177 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3179 /* While actor acts as a key server, shall distribute sakey */
3184 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3185 kay->macsec_csindex = DEFAULT_CS_INDEX;
3186 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3187 kay->mka_version = MKA_VERSION_ID;
3189 os_memcpy(kay->algo_agility, mka_algo_agility,
3190 sizeof(kay->algo_agility));
3192 dl_list_init(&kay->participant_list);
3194 if (policy == DO_NOT_SECURE) {
3195 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3196 kay->macsec_desired = FALSE;
3197 kay->macsec_protect = FALSE;
3198 kay->macsec_validate = Disabled;
3199 kay->macsec_replay_protect = FALSE;
3200 kay->macsec_replay_window = 0;
3201 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3203 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3204 kay->macsec_desired = TRUE;
3205 kay->macsec_protect = TRUE;
3206 kay->macsec_validate = Strict;
3207 kay->macsec_replay_protect = FALSE;
3208 kay->macsec_replay_window = 0;
3209 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3212 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3214 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3215 secy_init_macsec(kay);
3216 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3218 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3221 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3222 if (kay->cp == NULL) {
3223 ieee802_1x_kay_deinit(kay);
3227 if (policy == DO_NOT_SECURE) {
3228 ieee802_1x_cp_connect_authenticated(kay->cp);
3229 ieee802_1x_cp_sm_step(kay->cp);
3231 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3232 kay_l2_receive, kay, 1);
3233 if (kay->l2_mka == NULL) {
3234 wpa_printf(MSG_WARNING,
3235 "KaY: Failed to initialize L2 packet processing for MKA packet");
3236 ieee802_1x_kay_deinit(kay);
3246 * ieee802_1x_kay_deinit -
3249 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3251 struct ieee802_1x_mka_participant *participant;
3256 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3258 while (!dl_list_empty(&kay->participant_list)) {
3259 participant = dl_list_entry(kay->participant_list.next,
3260 struct ieee802_1x_mka_participant,
3262 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3265 ieee802_1x_cp_sm_deinit(kay->cp);
3266 secy_deinit_macsec(kay);
3269 l2_packet_deinit(kay->l2_mka);
3279 * ieee802_1x_kay_create_mka -
3281 struct ieee802_1x_mka_participant *
3282 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3283 struct mka_key *cak, u32 life,
3284 enum mka_created_mode mode, Boolean is_authenticator)
3286 struct ieee802_1x_mka_participant *participant;
3289 if (!kay || !ckn || !cak) {
3290 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3294 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3295 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3298 if (ckn->len > MAX_CKN_LEN) {
3299 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3303 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3307 participant = os_zalloc(sizeof(*participant));
3309 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3313 participant->ckn.len = ckn->len;
3314 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3315 participant->cak.len = cak->len;
3316 os_memcpy(participant->cak.key, cak->key, cak->len);
3318 participant->cak_life = life + time(NULL);
3322 if (is_authenticator) {
3323 participant->is_obliged_key_server = TRUE;
3324 participant->can_be_key_server = TRUE;
3325 participant->is_key_server = TRUE;
3326 participant->principal = TRUE;
3328 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3329 sizeof(kay->key_server_sci));
3330 kay->key_server_priority = kay->actor_priority;
3331 participant->is_elected = TRUE;
3333 participant->is_obliged_key_server = FALSE;
3334 participant->can_be_key_server = FALSE;
3335 participant->is_key_server = FALSE;
3336 participant->is_elected = TRUE;
3341 participant->is_obliged_key_server = FALSE;
3342 participant->can_be_key_server = TRUE;
3343 participant->is_key_server = TRUE;
3344 participant->is_elected = FALSE;
3348 participant->cached = FALSE;
3350 participant->active = FALSE;
3351 participant->participant = FALSE;
3352 participant->retain = FALSE;
3353 participant->activate = DEFAULT;
3355 if (participant->is_key_server)
3356 participant->principal = TRUE;
3358 dl_list_init(&participant->live_peers);
3359 dl_list_init(&participant->potential_peers);
3361 participant->retry_count = 0;
3362 participant->kay = kay;
3364 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3366 participant->mn = 0;
3368 participant->lrx = FALSE;
3369 participant->ltx = FALSE;
3370 participant->orx = FALSE;
3371 participant->otx = FALSE;
3372 participant->to_dist_sak = FALSE;
3373 participant->to_use_sak = FALSE;
3374 participant->new_sak = FALSE;
3375 dl_list_init(&participant->sak_list);
3376 participant->new_key = NULL;
3377 dl_list_init(&participant->rxsc_list);
3378 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3380 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3381 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3382 kay->macsec_replay_window);
3383 secy_create_transmit_sc(kay, participant->txsc);
3385 /* to derive KEK from CAK and CKN */
3386 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3387 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3388 participant->ckn.name,
3389 participant->ckn.len,
3390 participant->kek.key)) {
3391 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3394 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3395 participant->kek.key, participant->kek.len);
3397 /* to derive ICK from CAK and CKN */
3398 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3399 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3400 participant->ckn.name,
3401 participant->ckn.len,
3402 participant->ick.key)) {
3403 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3406 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3407 participant->ick.key, participant->ick.len);
3409 dl_list_add(&kay->participant_list, &participant->list);
3410 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3411 ckn->name, ckn->len);
3413 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3414 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3416 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3422 os_free(participant);
3428 * ieee802_1x_kay_delete_mka -
3431 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3433 struct ieee802_1x_mka_participant *participant;
3434 struct ieee802_1x_kay_peer *peer;
3435 struct data_key *sak;
3436 struct receive_sc *rxsc;
3441 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3443 /* get the participant */
3444 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3446 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3447 ckn->name, ckn->len);
3451 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3452 dl_list_del(&participant->list);
3454 /* remove live peer */
3455 while (!dl_list_empty(&participant->live_peers)) {
3456 peer = dl_list_entry(participant->live_peers.next,
3457 struct ieee802_1x_kay_peer, list);
3458 dl_list_del(&peer->list);
3462 /* remove potential peer */
3463 while (!dl_list_empty(&participant->potential_peers)) {
3464 peer = dl_list_entry(participant->potential_peers.next,
3465 struct ieee802_1x_kay_peer, list);
3466 dl_list_del(&peer->list);
3471 while (!dl_list_empty(&participant->sak_list)) {
3472 sak = dl_list_entry(participant->sak_list.next,
3473 struct data_key, list);
3474 dl_list_del(&sak->list);
3478 while (!dl_list_empty(&participant->rxsc_list)) {
3479 rxsc = dl_list_entry(participant->rxsc_list.next,
3480 struct receive_sc, list);
3481 secy_delete_receive_sc(kay, rxsc);
3482 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3484 secy_delete_transmit_sc(kay, participant->txsc);
3485 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3487 os_memset(&participant->cak, 0, sizeof(participant->cak));
3488 os_memset(&participant->kek, 0, sizeof(participant->kek));
3489 os_memset(&participant->ick, 0, sizeof(participant->ick));
3490 os_free(participant);
3495 * ieee802_1x_kay_mka_participate -
3497 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3498 struct mka_key_name *ckn,
3501 struct ieee802_1x_mka_participant *participant;
3506 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3510 participant->active = status;
3515 * ieee802_1x_kay_new_sak -
3518 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3520 struct ieee802_1x_mka_participant *participant;
3525 participant = ieee802_1x_kay_get_principal_participant(kay);
3529 participant->new_sak = TRUE;
3530 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3537 * ieee802_1x_kay_change_cipher_suite -
3540 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3542 struct ieee802_1x_mka_participant *participant;
3547 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3548 wpa_printf(MSG_ERROR,
3549 "KaY: Configured cipher suite index is out of range");
3552 if (kay->macsec_csindex == cs_index)
3556 kay->macsec_desired = FALSE;
3558 kay->macsec_csindex = cs_index;
3559 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3561 participant = ieee802_1x_kay_get_principal_participant(kay);
3563 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3564 participant->new_sak = TRUE;