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 int 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_capbility);
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];
385 * ieee802_1x_kay_get_peer_sci
387 static struct ieee802_1x_kay_peer *
388 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
389 const struct ieee802_1x_mka_sci *sci)
391 struct ieee802_1x_kay_peer *peer;
393 dl_list_for_each(peer, &participant->live_peers,
394 struct ieee802_1x_kay_peer, list) {
395 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
399 dl_list_for_each(peer, &participant->potential_peers,
400 struct ieee802_1x_kay_peer, list) {
401 if (os_memcmp(&peer->sci, sci, sizeof(peer->sci)) == 0)
410 * ieee802_1x_kay_init_receive_sa -
412 static struct receive_sa *
413 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
414 struct data_key *key)
416 struct receive_sa *psa;
421 psa = os_zalloc(sizeof(*psa));
423 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
428 psa->lowest_pn = lowest_pn;
429 psa->next_pn = lowest_pn;
433 os_get_time(&psa->created_time);
436 dl_list_add(&psc->sa_list, &psa->list);
437 wpa_printf(MSG_DEBUG,
438 "KaY: Create receive SA(AN: %d lowest_pn: %u of SC(channel: %d)",
439 (int) an, lowest_pn, psc->channel);
446 * ieee802_1x_kay_deinit_receive_sa -
448 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
451 wpa_printf(MSG_DEBUG,
452 "KaY: Delete receive SA(an: %d) of SC(channel: %d)",
453 psa->an, psa->sc->channel);
454 dl_list_del(&psa->list);
460 * ieee802_1x_kay_init_receive_sc -
462 static struct receive_sc *
463 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
466 struct receive_sc *psc;
471 psc = os_zalloc(sizeof(*psc));
473 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
477 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
478 psc->channel = channel;
480 os_get_time(&psc->created_time);
481 psc->receiving = FALSE;
483 dl_list_init(&psc->sa_list);
484 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
485 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
492 * ieee802_1x_kay_deinit_receive_sc -
495 ieee802_1x_kay_deinit_receive_sc(
496 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
498 struct receive_sa *psa, *pre_sa;
500 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
502 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
504 secy_disable_receive_sa(participant->kay, psa);
505 ieee802_1x_kay_deinit_receive_sa(psa);
507 dl_list_del(&psc->list);
513 * ieee802_1x_kay_create_live_peer
515 static struct ieee802_1x_kay_peer *
516 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
519 struct ieee802_1x_kay_peer *peer;
520 struct receive_sc *rxsc;
523 peer = os_zalloc(sizeof(*peer));
525 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
529 os_memcpy(peer->mi, mi, MI_LEN);
531 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
532 peer->sak_used = FALSE;
533 os_memcpy(&peer->sci, &participant->current_peer_sci,
536 secy_get_available_receive_sc(participant->kay, &sc_ch);
538 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
544 dl_list_add(&participant->live_peers, &peer->list);
545 dl_list_add(&participant->rxsc_list, &rxsc->list);
546 secy_create_receive_sc(participant->kay, rxsc);
548 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
549 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
550 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
551 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
552 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
559 * ieee802_1x_kay_create_potential_peer
561 static struct ieee802_1x_kay_peer *
562 ieee802_1x_kay_create_potential_peer(
563 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
565 struct ieee802_1x_kay_peer *peer;
567 peer = os_zalloc(sizeof(*peer));
569 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
573 os_memcpy(peer->mi, mi, MI_LEN);
575 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
576 peer->sak_used = FALSE;
578 dl_list_add(&participant->potential_peers, &peer->list);
580 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
581 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
582 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
583 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
584 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
591 * ieee802_1x_kay_move_live_peer
593 static struct ieee802_1x_kay_peer *
594 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
597 struct ieee802_1x_kay_peer *peer;
598 struct receive_sc *rxsc;
601 dl_list_for_each(peer, &participant->potential_peers,
602 struct ieee802_1x_kay_peer, list) {
603 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
607 os_memcpy(&peer->sci, &participant->current_peer_sci,
610 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
612 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
613 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
614 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
615 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
616 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
618 dl_list_del(&peer->list);
619 dl_list_add_tail(&participant->live_peers, &peer->list);
621 secy_get_available_receive_sc(participant->kay, &sc_ch);
623 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
627 dl_list_add(&participant->rxsc_list, &rxsc->list);
628 secy_create_receive_sc(participant->kay, rxsc);
636 * ieee802_1x_mka_basic_body_present -
639 ieee802_1x_mka_basic_body_present(
640 struct ieee802_1x_mka_participant *participant)
647 * ieee802_1x_mka_basic_body_length -
650 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
654 length = sizeof(struct ieee802_1x_mka_basic_body);
655 length += participant->ckn.len;
656 return (length + 0x3) & ~0x3;
661 * ieee802_1x_mka_encode_basic_body
664 ieee802_1x_mka_encode_basic_body(
665 struct ieee802_1x_mka_participant *participant,
668 struct ieee802_1x_mka_basic_body *body;
669 struct ieee802_1x_kay *kay = participant->kay;
670 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
672 body = wpabuf_put(buf, length);
674 body->version = kay->mka_version;
675 body->priority = kay->actor_priority;
676 if (participant->is_elected)
677 body->key_server = participant->is_key_server;
679 body->key_server = participant->can_be_key_server;
681 body->macsec_desired = kay->macsec_desired;
682 body->macsec_capbility = kay->macsec_capable;
683 set_mka_param_body_len(body, length - MKA_HDR_LEN);
685 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
686 sizeof(kay->actor_sci.addr));
687 body->actor_sci.port = kay->actor_sci.port;
689 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
690 participant->mn = participant->mn + 1;
691 body->actor_mn = host_to_be32(participant->mn);
692 os_memcpy(body->algo_agility, participant->kay->algo_agility,
693 sizeof(body->algo_agility));
695 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
697 ieee802_1x_mka_dump_basic_body(body);
704 * ieee802_1x_mka_decode_basic_body -
706 static struct ieee802_1x_mka_participant *
707 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
710 struct ieee802_1x_mka_participant *participant;
711 const struct ieee802_1x_mka_basic_body *body;
712 struct ieee802_1x_kay_peer *peer;
714 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
716 if (body->version > MKA_VERSION_ID) {
717 wpa_printf(MSG_DEBUG,
718 "KaY: peer's version(%d) greater than mka current version(%d)",
719 body->version, MKA_VERSION_ID);
721 if (kay->is_obliged_key_server && body->key_server) {
722 wpa_printf(MSG_DEBUG, "I must be as key server");
726 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
728 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
732 /* If the peer's MI is my MI, I will choose new MI */
733 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
734 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
739 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
740 participant->current_peer_id.mn = body->actor_mn;
741 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
742 sizeof(participant->current_peer_sci.addr));
743 participant->current_peer_sci.port = body->actor_sci.port;
746 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
748 /* Check duplicated SCI */
749 /* TODO: What policy should be applied to detect duplicated SCI
750 * is active attacker or a valid peer whose MI is be changed?
752 peer = ieee802_1x_kay_get_peer_sci(participant,
755 wpa_printf(MSG_WARNING,
756 "KaY: duplicated SCI detected, Maybe active attacker");
757 dl_list_del(&peer->list);
761 peer = ieee802_1x_kay_create_potential_peer(
762 participant, body->actor_mi,
763 be_to_host32(body->actor_mn));
767 peer->macsec_desired = body->macsec_desired;
768 peer->macsec_capbility = body->macsec_capbility;
769 peer->is_key_server = (Boolean) body->key_server;
770 peer->key_server_priority = body->priority;
771 } else if (peer->mn < be_to_host32(body->actor_mn)) {
772 peer->mn = be_to_host32(body->actor_mn);
773 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
774 peer->macsec_desired = body->macsec_desired;
775 peer->macsec_capbility = body->macsec_capbility;
776 peer->is_key_server = (Boolean) body->key_server;
777 peer->key_server_priority = body->priority;
779 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
788 * ieee802_1x_mka_live_peer_body_present
791 ieee802_1x_mka_live_peer_body_present(
792 struct ieee802_1x_mka_participant *participant)
794 return !dl_list_empty(&participant->live_peers);
799 * ieee802_1x_kay_get_live_peer_length
802 ieee802_1x_mka_get_live_peer_length(
803 struct ieee802_1x_mka_participant *participant)
805 int len = MKA_HDR_LEN;
806 struct ieee802_1x_kay_peer *peer;
808 dl_list_for_each(peer, &participant->live_peers,
809 struct ieee802_1x_kay_peer, list)
810 len += sizeof(struct ieee802_1x_mka_peer_id);
812 return (len + 0x3) & ~0x3;
817 * ieee802_1x_mka_encode_live_peer_body -
820 ieee802_1x_mka_encode_live_peer_body(
821 struct ieee802_1x_mka_participant *participant,
824 struct ieee802_1x_mka_peer_body *body;
825 struct ieee802_1x_kay_peer *peer;
827 struct ieee802_1x_mka_peer_id *body_peer;
829 length = ieee802_1x_mka_get_live_peer_length(participant);
830 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
832 body->type = MKA_LIVE_PEER_LIST;
833 set_mka_param_body_len(body, length - MKA_HDR_LEN);
835 dl_list_for_each(peer, &participant->live_peers,
836 struct ieee802_1x_kay_peer, list) {
837 body_peer = wpabuf_put(buf,
838 sizeof(struct ieee802_1x_mka_peer_id));
839 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
840 body_peer->mn = host_to_be32(peer->mn);
844 ieee802_1x_mka_dump_peer_body(body);
849 * ieee802_1x_mka_potential_peer_body_present
852 ieee802_1x_mka_potential_peer_body_present(
853 struct ieee802_1x_mka_participant *participant)
855 return !dl_list_empty(&participant->potential_peers);
860 * ieee802_1x_kay_get_potential_peer_length
863 ieee802_1x_mka_get_potential_peer_length(
864 struct ieee802_1x_mka_participant *participant)
866 int len = MKA_HDR_LEN;
867 struct ieee802_1x_kay_peer *peer;
869 dl_list_for_each(peer, &participant->potential_peers,
870 struct ieee802_1x_kay_peer, list)
871 len += sizeof(struct ieee802_1x_mka_peer_id);
873 return (len + 0x3) & ~0x3;
878 * ieee802_1x_mka_encode_potential_peer_body -
881 ieee802_1x_mka_encode_potential_peer_body(
882 struct ieee802_1x_mka_participant *participant,
885 struct ieee802_1x_mka_peer_body *body;
886 struct ieee802_1x_kay_peer *peer;
888 struct ieee802_1x_mka_peer_id *body_peer;
890 length = ieee802_1x_mka_get_potential_peer_length(participant);
891 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
893 body->type = MKA_POTENTIAL_PEER_LIST;
894 set_mka_param_body_len(body, length - MKA_HDR_LEN);
896 dl_list_for_each(peer, &participant->potential_peers,
897 struct ieee802_1x_kay_peer, list) {
898 body_peer = wpabuf_put(buf,
899 sizeof(struct ieee802_1x_mka_peer_id));
900 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
901 body_peer->mn = host_to_be32(peer->mn);
905 ieee802_1x_mka_dump_peer_body(body);
911 * ieee802_1x_mka_i_in_peerlist -
914 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
915 const u8 *mka_msg, size_t msg_len)
917 Boolean included = FALSE;
918 struct ieee802_1x_mka_hdr *hdr;
930 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
931 hdr = (struct ieee802_1x_mka_hdr *) pos;
932 body_len = get_mka_param_body_len(hdr);
933 body_type = get_mka_param_body_type(hdr);
935 if (body_type != MKA_LIVE_PEER_LIST &&
936 body_type != MKA_POTENTIAL_PEER_LIST)
939 ieee802_1x_mka_dump_peer_body(
940 (struct ieee802_1x_mka_peer_body *)pos);
942 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
943 wpa_printf(MSG_ERROR,
944 "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",
945 (int) left_len, (int) MKA_HDR_LEN,
946 (int) body_len, DEFAULT_ICV_LEN);
950 if ((body_len % 16) != 0) {
951 wpa_printf(MSG_ERROR,
952 "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
957 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
958 peer_mi = MKA_HDR_LEN + pos + i;
959 os_memcpy(&_peer_mn, peer_mi + MI_LEN,
961 peer_mn = be_to_host32(_peer_mn);
962 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
963 peer_mn == participant->mn) {
973 left_len -= body_len + MKA_HDR_LEN;
974 pos += body_len + MKA_HDR_LEN;
982 * ieee802_1x_mka_decode_live_peer_body -
984 static int ieee802_1x_mka_decode_live_peer_body(
985 struct ieee802_1x_mka_participant *participant,
986 const u8 *peer_msg, size_t msg_len)
988 const struct ieee802_1x_mka_hdr *hdr;
989 struct ieee802_1x_kay_peer *peer;
997 is_included = ieee802_1x_kay_is_in_live_peer(
998 participant, participant->current_peer_id.mi);
1000 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1001 body_len = get_mka_param_body_len(hdr);
1003 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1004 peer_mi = MKA_HDR_LEN + peer_msg + i;
1005 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1006 peer_mn = be_to_host32(_peer_mn);
1009 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1010 /* My message id is used by other participant */
1011 if (peer_mn > participant->mn) {
1012 if (os_get_random(participant->mi,
1013 sizeof(participant->mi)) < 0)
1014 wpa_printf(MSG_DEBUG,
1015 "KaY: Could not update mi");
1016 participant->mn = 0;
1023 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1026 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1028 if (!ieee802_1x_kay_create_potential_peer(
1029 participant, peer_mi, peer_mn)) {
1040 * ieee802_1x_mka_decode_potential_peer_body -
1043 ieee802_1x_mka_decode_potential_peer_body(
1044 struct ieee802_1x_mka_participant *participant,
1045 const u8 *peer_msg, size_t msg_len)
1047 struct ieee802_1x_mka_hdr *hdr;
1054 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1055 body_len = get_mka_param_body_len(hdr);
1057 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1058 peer_mi = MKA_HDR_LEN + peer_msg + i;
1059 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1060 peer_mn = be_to_host32(_peer_mn);
1063 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1064 /* My message id is used by other participant */
1065 if (peer_mn > participant->mn) {
1066 if (os_get_random(participant->mi,
1067 sizeof(participant->mi)) < 0)
1068 wpa_printf(MSG_DEBUG,
1069 "KaY: Could not update mi");
1070 participant->mn = 0;
1081 * ieee802_1x_mka_sak_use_body_present
1084 ieee802_1x_mka_sak_use_body_present(
1085 struct ieee802_1x_mka_participant *participant)
1087 if (participant->to_use_sak)
1095 * ieee802_1x_mka_get_sak_use_length
1098 ieee802_1x_mka_get_sak_use_length(
1099 struct ieee802_1x_mka_participant *participant)
1101 int length = MKA_HDR_LEN;
1103 if (participant->kay->macsec_desired && participant->advised_desired)
1104 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1106 length = MKA_HDR_LEN;
1108 length = (length + 0x3) & ~0x3;
1118 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1119 struct ieee802_1x_mka_ki *ki)
1121 struct receive_sa *rxsa;
1122 struct receive_sc *rxsc;
1125 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1126 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1128 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1129 secy_get_receive_lowest_pn(principal->kay,
1132 lpn = lpn > rxsa->lowest_pn ?
1133 lpn : rxsa->lowest_pn;
1147 * ieee802_1x_mka_encode_sak_use_body -
1150 ieee802_1x_mka_encode_sak_use_body(
1151 struct ieee802_1x_mka_participant *participant,
1154 struct ieee802_1x_mka_sak_use_body *body;
1155 unsigned int length;
1158 length = ieee802_1x_mka_get_sak_use_length(participant);
1159 body = wpabuf_put(buf, length);
1161 body->type = MKA_SAK_USE;
1162 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1164 if (length == MKA_HDR_LEN) {
1170 body->delay_protect = FALSE;
1174 /* data protect, lowest accept packet number */
1175 body->delay_protect = participant->kay->macsec_replay_protect;
1176 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1177 if (pn > participant->kay->pn_exhaustion) {
1178 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1179 if (participant->is_key_server)
1180 participant->new_sak = TRUE;
1183 body->llpn = host_to_be32(pn);
1184 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1185 body->olpn = host_to_be32(pn);
1187 /* plain tx, plain rx */
1188 if (participant->kay->macsec_protect)
1193 if (participant->kay->macsec_validate == Strict)
1198 /* latest key: rx, tx, key server member identifier key number */
1199 body->lan = participant->lan;
1200 os_memcpy(body->lsrv_mi, participant->lki.mi,
1201 sizeof(body->lsrv_mi));
1202 body->lkn = host_to_be32(participant->lki.kn);
1203 body->lrx = participant->lrx;
1204 body->ltx = participant->ltx;
1206 /* old key: rx, tx, key server member identifier key number */
1207 body->oan = participant->oan;
1208 if (participant->oki.kn != participant->lki.kn &&
1209 participant->oki.kn != 0) {
1212 os_memcpy(body->osrv_mi, participant->oki.mi,
1213 sizeof(body->osrv_mi));
1214 body->okn = host_to_be32(participant->oki.kn);
1220 /* set CP's variable */
1222 if (!participant->kay->tx_enable)
1223 participant->kay->tx_enable = TRUE;
1225 if (!participant->kay->port_enable)
1226 participant->kay->port_enable = TRUE;
1229 if (!participant->kay->rx_enable)
1230 participant->kay->rx_enable = TRUE;
1233 ieee802_1x_mka_dump_sak_use_body(body);
1239 * ieee802_1x_mka_decode_sak_use_body -
1242 ieee802_1x_mka_decode_sak_use_body(
1243 struct ieee802_1x_mka_participant *participant,
1244 const u8 *mka_msg, size_t msg_len)
1246 struct ieee802_1x_mka_hdr *hdr;
1247 struct ieee802_1x_mka_sak_use_body *body;
1248 struct ieee802_1x_kay_peer *peer;
1249 struct transmit_sa *txsa;
1250 struct data_key *sa_key = NULL;
1252 struct ieee802_1x_mka_ki ki;
1254 Boolean all_receiving;
1257 if (!participant->principal) {
1258 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1261 peer = ieee802_1x_kay_get_live_peer(participant,
1262 participant->current_peer_id.mi);
1264 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1268 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1269 body_len = get_mka_param_body_len(hdr);
1270 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1271 ieee802_1x_mka_dump_sak_use_body(body);
1273 if ((body_len != 0) && (body_len < 40)) {
1274 wpa_printf(MSG_ERROR,
1275 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1280 /* TODO: what action should I take when peer does not support MACsec */
1281 if (body_len == 0) {
1282 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1286 /* TODO: when the plain tx or rx of peer is true, should I change
1287 * the attribute of controlled port
1290 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1293 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1295 /* check latest key is valid */
1296 if (body->ltx || body->lrx) {
1298 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1299 ki.kn = be_to_host32(body->lkn);
1300 dl_list_for_each(sa_key, &participant->sak_list,
1301 struct data_key, list) {
1302 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1308 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1311 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1312 sizeof(participant->lki.mi)) == 0 &&
1313 be_to_host32(body->lkn) == participant->lki.kn &&
1314 body->lan == participant->lan) {
1315 peer->sak_used = TRUE;
1317 if (body->ltx && peer->is_key_server) {
1318 ieee802_1x_cp_set_servertransmitting(
1319 participant->kay->cp, TRUE);
1320 ieee802_1x_cp_sm_step(participant->kay->cp);
1324 /* check old key is valid */
1325 if (body->otx || body->orx) {
1326 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1327 sizeof(participant->oki.mi)) != 0 ||
1328 be_to_host32(body->okn) != participant->oki.kn ||
1329 body->oan != participant->oan) {
1330 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1335 /* TODO: how to set the MACsec hardware when delay_protect is true */
1336 if (body->delay_protect &&
1337 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1338 wpa_printf(MSG_WARNING,
1339 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1343 /* check all live peer have used the sak for receiving sa */
1344 all_receiving = TRUE;
1345 dl_list_for_each(peer, &participant->live_peers,
1346 struct ieee802_1x_kay_peer, list) {
1347 if (!peer->sak_used) {
1348 all_receiving = FALSE;
1352 if (all_receiving) {
1353 participant->to_dist_sak = FALSE;
1354 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1355 ieee802_1x_cp_sm_step(participant->kay->cp);
1358 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1359 lpn = be_to_host32(body->llpn);
1360 if (lpn > participant->kay->pn_exhaustion) {
1361 if (participant->is_key_server) {
1362 participant->new_sak = TRUE;
1363 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1368 dl_list_for_each(txsa, &participant->txsc->sa_list,
1369 struct transmit_sa, list) {
1370 if (sa_key != NULL && txsa->pkey == sa_key) {
1376 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1380 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1381 * npn is larger than txsa's npn, set it to txsa.
1383 secy_get_transmit_next_pn(participant->kay, txsa);
1384 if (lpn > txsa->next_pn) {
1385 secy_set_transmit_next_pn(participant->kay, txsa);
1386 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1394 * ieee802_1x_mka_dist_sak_body_present
1397 ieee802_1x_mka_dist_sak_body_present(
1398 struct ieee802_1x_mka_participant *participant)
1400 if (!participant->to_dist_sak || !participant->new_key)
1408 * ieee802_1x_kay_get_dist_sak_length
1411 ieee802_1x_mka_get_dist_sak_length(
1412 struct ieee802_1x_mka_participant *participant)
1415 int cs_index = participant->kay->macsec_csindex;
1417 if (participant->advised_desired) {
1418 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1419 if (cs_index != DEFAULT_CS_INDEX)
1420 length += CS_ID_LEN;
1422 length += cipher_suite_tbl[cs_index].sak_len + 8;
1424 length = MKA_HDR_LEN;
1426 length = (length + 0x3) & ~0x3;
1433 * ieee802_1x_mka_encode_dist_sak_body -
1436 ieee802_1x_mka_encode_dist_sak_body(
1437 struct ieee802_1x_mka_participant *participant,
1440 struct ieee802_1x_mka_dist_sak_body *body;
1441 struct data_key *sak;
1442 unsigned int length;
1446 length = ieee802_1x_mka_get_dist_sak_length(participant);
1447 body = wpabuf_put(buf, length);
1448 body->type = MKA_DISTRIBUTED_SAK;
1449 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1450 if (length == MKA_HDR_LEN) {
1451 body->confid_offset = 0;
1456 sak = participant->new_key;
1457 body->confid_offset = sak->confidentiality_offset;
1458 body->dan = sak->an;
1459 body->kn = host_to_be32(sak->key_identifier.kn);
1460 cs_index = participant->kay->macsec_csindex;
1462 if (cs_index != DEFAULT_CS_INDEX) {
1463 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1464 sak_pos = CS_ID_LEN;
1466 if (aes_wrap(participant->kek.key, 16,
1467 cipher_suite_tbl[cs_index].sak_len / 8,
1468 sak->key, body->sak + sak_pos)) {
1469 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1473 ieee802_1x_mka_dump_dist_sak_body(body);
1480 * ieee802_1x_kay_init_data_key -
1482 static struct data_key *
1483 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1485 struct data_key *pkey;
1490 pkey = os_zalloc(sizeof(*pkey));
1492 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1496 pkey->key = os_zalloc(conf->key_len);
1497 if (pkey->key == NULL) {
1498 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1503 os_memcpy(pkey->key, conf->key, conf->key_len);
1504 os_memcpy(&pkey->key_identifier, &conf->ki,
1505 sizeof(pkey->key_identifier));
1506 pkey->confidentiality_offset = conf->offset;
1507 pkey->an = conf->an;
1508 pkey->transmits = conf->tx;
1509 pkey->receives = conf->rx;
1510 os_get_time(&pkey->created_time);
1519 * ieee802_1x_kay_decode_dist_sak_body -
1522 ieee802_1x_mka_decode_dist_sak_body(
1523 struct ieee802_1x_mka_participant *participant,
1524 const u8 *mka_msg, size_t msg_len)
1526 struct ieee802_1x_mka_hdr *hdr;
1527 struct ieee802_1x_mka_dist_sak_body *body;
1528 struct ieee802_1x_kay_peer *peer;
1529 struct macsec_ciphersuite *cs;
1531 struct key_conf *conf;
1532 struct data_key *sa_key = NULL;
1533 struct ieee802_1x_mka_ki sak_ki;
1538 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1539 body_len = get_mka_param_body_len(hdr);
1540 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1541 wpa_printf(MSG_ERROR,
1542 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1547 if (!participant->principal) {
1548 wpa_printf(MSG_ERROR,
1549 "KaY: I can't accept the distributed SAK as I am not principal");
1552 if (participant->is_key_server) {
1553 wpa_printf(MSG_ERROR,
1554 "KaY: I can't accept the distributed SAK as myself is key server ");
1557 if (!participant->kay->macsec_desired ||
1558 participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1559 wpa_printf(MSG_ERROR,
1560 "KaY: I am not MACsec-desired or without MACsec capable");
1564 peer = ieee802_1x_kay_get_live_peer(participant,
1565 participant->current_peer_id.mi);
1567 wpa_printf(MSG_ERROR,
1568 "KaY: The key server is not in my live peers list");
1571 if (os_memcmp(&participant->kay->key_server_sci,
1572 &peer->sci, sizeof(struct ieee802_1x_mka_sci)) != 0) {
1573 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1576 if (body_len == 0) {
1577 participant->kay->authenticated = TRUE;
1578 participant->kay->secured = FALSE;
1579 participant->kay->failed = FALSE;
1580 participant->advised_desired = FALSE;
1581 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1582 ieee802_1x_cp_sm_step(participant->kay->cp);
1583 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1584 participant->to_use_sak = TRUE;
1587 participant->advised_desired = TRUE;
1588 participant->kay->authenticated = FALSE;
1589 participant->kay->secured = TRUE;
1590 participant->kay->failed = FALSE;
1591 ieee802_1x_cp_connect_secure(participant->kay->cp);
1592 ieee802_1x_cp_sm_step(participant->kay->cp);
1594 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1595 ieee802_1x_mka_dump_dist_sak_body(body);
1596 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1598 if (os_memcmp(sa_key->key_identifier.mi,
1599 participant->current_peer_id.mi, MI_LEN) == 0 &&
1600 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1601 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1605 if (body_len == 28) {
1606 sak_len = DEFAULT_SA_KEY_LEN;
1607 wrap_sak = body->sak;
1608 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1610 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1612 wpa_printf(MSG_ERROR,
1613 "KaY: I can't support the Cipher Suite advised by key server");
1616 sak_len = cs->sak_len;
1617 wrap_sak = body->sak + CS_ID_LEN;
1618 participant->kay->macsec_csindex = cs->index;
1621 unwrap_sak = os_zalloc(sak_len);
1623 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1626 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1628 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1629 os_free(unwrap_sak);
1632 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1634 conf = os_zalloc(sizeof(*conf));
1636 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1637 os_free(unwrap_sak);
1640 conf->key_len = sak_len;
1642 conf->key = os_zalloc(conf->key_len);
1644 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1645 os_free(unwrap_sak);
1650 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1652 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1654 sak_ki.kn = be_to_host32(body->kn);
1656 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1657 conf->ki.kn = sak_ki.kn;
1658 conf->an = body->dan;
1659 conf->offset = body->confid_offset;
1663 sa_key = ieee802_1x_kay_init_data_key(conf);
1665 os_free(unwrap_sak);
1671 dl_list_add(&participant->sak_list, &sa_key->list);
1673 ieee802_1x_cp_set_ciphersuite(
1674 participant->kay->cp,
1675 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1676 ieee802_1x_cp_sm_step(participant->kay->cp);
1677 ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1678 ieee802_1x_cp_sm_step(participant->kay->cp);
1679 ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1680 ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1681 ieee802_1x_cp_signal_newsak(participant->kay->cp);
1682 ieee802_1x_cp_sm_step(participant->kay->cp);
1684 participant->to_use_sak = TRUE;
1686 os_free(unwrap_sak);
1695 * ieee802_1x_mka_icv_body_present
1698 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1705 * ieee802_1x_kay_get_icv_length
1708 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1712 length = sizeof(struct ieee802_1x_mka_icv_body);
1713 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1715 return (length + 0x3) & ~0x3;
1720 * ieee802_1x_mka_encode_icv_body -
1723 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1726 struct ieee802_1x_mka_icv_body *body;
1727 unsigned int length;
1728 u8 cmac[MAX_ICV_LEN];
1730 length = ieee802_1x_mka_get_icv_length(participant);
1731 if (length != DEFAULT_ICV_LEN) {
1732 body = wpabuf_put(buf, MKA_HDR_LEN);
1733 body->type = MKA_ICV_INDICATOR;
1734 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1737 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1738 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1739 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1743 if (length != DEFAULT_ICV_LEN) {
1744 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1745 length - MKA_HDR_LEN);
1747 os_memcpy(wpabuf_put(buf, length), cmac, length);
1754 * ieee802_1x_mka_decode_icv_body -
1757 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1758 const u8 *mka_msg, size_t msg_len)
1760 struct ieee802_1x_mka_hdr *hdr;
1761 struct ieee802_1x_mka_icv_body *body;
1769 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1770 hdr = (struct ieee802_1x_mka_hdr *) pos;
1771 body_len = get_mka_param_body_len(hdr);
1772 body_type = get_mka_param_body_type(hdr);
1774 if (left_len < (body_len + MKA_HDR_LEN))
1777 if (body_type != MKA_ICV_INDICATOR) {
1778 left_len -= MKA_HDR_LEN + body_len;
1779 pos += MKA_HDR_LEN + body_len;
1783 body = (struct ieee802_1x_mka_icv_body *)pos;
1785 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1792 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1797 * ieee802_1x_mka_decode_dist_cak_body-
1800 ieee802_1x_mka_decode_dist_cak_body(
1801 struct ieee802_1x_mka_participant *participant,
1802 const u8 *mka_msg, size_t msg_len)
1804 struct ieee802_1x_mka_hdr *hdr;
1807 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1808 body_len = get_mka_param_body_len(hdr);
1809 if (body_len < 28) {
1810 wpa_printf(MSG_ERROR,
1811 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1821 * ieee802_1x_mka_decode_kmd_body -
1824 ieee802_1x_mka_decode_kmd_body(
1825 struct ieee802_1x_mka_participant *participant,
1826 const u8 *mka_msg, size_t msg_len)
1828 struct ieee802_1x_mka_hdr *hdr;
1831 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1832 body_len = get_mka_param_body_len(hdr);
1834 wpa_printf(MSG_ERROR,
1835 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1845 * ieee802_1x_mka_decode_announce_body -
1847 static int ieee802_1x_mka_decode_announce_body(
1848 struct ieee802_1x_mka_participant *participant,
1849 const u8 *mka_msg, size_t msg_len)
1855 static struct mka_param_body_handler mak_body_handler[] = {
1856 /* basic parameter set */
1858 ieee802_1x_mka_encode_basic_body,
1860 ieee802_1x_mka_basic_body_length,
1861 ieee802_1x_mka_basic_body_present
1864 /* live peer list parameter set */
1866 ieee802_1x_mka_encode_live_peer_body,
1867 ieee802_1x_mka_decode_live_peer_body,
1868 ieee802_1x_mka_get_live_peer_length,
1869 ieee802_1x_mka_live_peer_body_present
1872 /* potential peer list parameter set */
1874 ieee802_1x_mka_encode_potential_peer_body,
1875 ieee802_1x_mka_decode_potential_peer_body,
1876 ieee802_1x_mka_get_potential_peer_length,
1877 ieee802_1x_mka_potential_peer_body_present
1880 /* sak use parameter set */
1882 ieee802_1x_mka_encode_sak_use_body,
1883 ieee802_1x_mka_decode_sak_use_body,
1884 ieee802_1x_mka_get_sak_use_length,
1885 ieee802_1x_mka_sak_use_body_present
1888 /* distribute sak parameter set */
1890 ieee802_1x_mka_encode_dist_sak_body,
1891 ieee802_1x_mka_decode_dist_sak_body,
1892 ieee802_1x_mka_get_dist_sak_length,
1893 ieee802_1x_mka_dist_sak_body_present
1896 /* distribute cak parameter set */
1899 ieee802_1x_mka_decode_dist_cak_body,
1904 /* kmd parameter set */
1907 ieee802_1x_mka_decode_kmd_body,
1912 /* announce parameter set */
1915 ieee802_1x_mka_decode_announce_body,
1920 /* icv parameter set */
1922 ieee802_1x_mka_encode_icv_body,
1924 ieee802_1x_mka_get_icv_length,
1925 ieee802_1x_mka_icv_body_present
1931 * ieee802_1x_kay_deinit_data_key -
1933 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1942 dl_list_del(&pkey->list);
1949 * ieee802_1x_kay_generate_new_sak -
1952 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1954 struct data_key *sa_key = NULL;
1955 struct key_conf *conf;
1956 struct ieee802_1x_kay_peer *peer;
1957 struct ieee802_1x_kay *kay = participant->kay;
1958 int ctx_len, ctx_offset;
1961 /* check condition for generating a fresh SAK:
1962 * must have one live peer
1963 * and MKA life time elapse since last distribution
1964 * or potential peer is empty
1966 if (dl_list_empty(&participant->live_peers)) {
1967 wpa_printf(MSG_ERROR,
1968 "KaY: Live peers list must not empty when generating fresh SAK");
1972 /* FIXME: A fresh SAK not generated until
1973 * the live peer list contains at least one peer and
1974 * MKA life time has elapsed since the prior SAK was first distributed,
1975 * or the Key server's potential peer is empty
1976 * but I can't understand the second item, so
1977 * here only check first item and ingore
1978 * && (!dl_list_empty(&participant->potential_peers))) {
1980 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1981 wpa_printf(MSG_ERROR,
1982 "KaY: Life time have not elapsed since prior SAK distributed");
1986 conf = os_zalloc(sizeof(*conf));
1988 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1991 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1993 conf->key = os_zalloc(conf->key_len);
1996 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2000 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2001 dl_list_for_each(peer, &participant->live_peers,
2002 struct ieee802_1x_kay_peer, list)
2003 ctx_len += sizeof(peer->mi);
2004 ctx_len += sizeof(participant->mi);
2006 context = os_zalloc(ctx_len);
2013 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2019 ctx_offset += conf->key_len;
2020 dl_list_for_each(peer, &participant->live_peers,
2021 struct ieee802_1x_kay_peer, list) {
2022 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2023 ctx_offset += sizeof(peer->mi);
2025 os_memcpy(context + ctx_offset, participant->mi,
2026 sizeof(participant->mi));
2027 ctx_offset += sizeof(participant->mi);
2028 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2030 if (conf->key_len == 16) {
2031 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2032 context, ctx_len, conf->key);
2033 } else if (conf->key_len == 32) {
2034 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2035 context, ctx_len, conf->key);
2037 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2043 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2044 conf->key, conf->key_len);
2046 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2047 conf->ki.kn = participant->kay->dist_kn;
2048 conf->an = participant->kay->dist_an;
2049 conf->offset = kay->macsec_confidentiality;
2053 sa_key = ieee802_1x_kay_init_data_key(conf);
2060 participant->new_key = sa_key;
2062 dl_list_add(&participant->sak_list, &sa_key->list);
2063 ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2064 cipher_suite_tbl[kay->macsec_csindex].id);
2065 ieee802_1x_cp_sm_step(kay->cp);
2066 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2067 ieee802_1x_cp_sm_step(kay->cp);
2068 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2069 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2070 ieee802_1x_cp_signal_newsak(kay->cp);
2071 ieee802_1x_cp_sm_step(kay->cp);
2073 dl_list_for_each(peer, &participant->live_peers,
2074 struct ieee802_1x_kay_peer, list)
2075 peer->sak_used = FALSE;
2077 participant->kay->dist_kn++;
2078 participant->kay->dist_an++;
2079 if (participant->kay->dist_an > 3)
2080 participant->kay->dist_an = 0;
2082 participant->kay->dist_time = time(NULL);
2092 * ieee802_1x_kay_elect_key_server - elect the key server
2093 * when to elect: whenever the live peers list changes
2096 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2098 struct ieee802_1x_kay_peer *peer;
2099 struct ieee802_1x_kay_peer *key_server = NULL;
2100 struct ieee802_1x_kay *kay = participant->kay;
2101 Boolean i_is_key_server;
2103 if (participant->is_obliged_key_server) {
2104 participant->new_sak = TRUE;
2105 participant->to_dist_sak = FALSE;
2106 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2110 /* elect the key server among the peers */
2111 dl_list_for_each(peer, &participant->live_peers,
2112 struct ieee802_1x_kay_peer, list) {
2113 if (!peer->is_key_server)
2121 if (peer->key_server_priority <
2122 key_server->key_server_priority) {
2124 } else if (peer->key_server_priority ==
2125 key_server->key_server_priority) {
2126 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2132 /* elect the key server between me and the above elected peer */
2133 i_is_key_server = FALSE;
2134 if (key_server && participant->can_be_key_server) {
2135 if (kay->actor_priority
2136 < key_server->key_server_priority) {
2137 i_is_key_server = TRUE;
2138 } else if (kay->actor_priority
2139 == key_server->key_server_priority) {
2140 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2142 i_is_key_server = TRUE;
2144 } else if (participant->can_be_key_server) {
2145 i_is_key_server = TRUE;
2148 if (i_is_key_server) {
2149 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2150 if (os_memcmp(&kay->key_server_sci, &kay->actor_sci,
2151 sizeof(kay->key_server_sci))) {
2152 ieee802_1x_cp_signal_chgdserver(kay->cp);
2153 ieee802_1x_cp_sm_step(kay->cp);
2156 participant->is_key_server = TRUE;
2157 participant->principal = TRUE;
2158 participant->new_sak = TRUE;
2159 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2160 participant->to_dist_sak = FALSE;
2161 participant->is_elected = TRUE;
2163 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2164 sizeof(kay->key_server_sci));
2165 kay->key_server_priority = kay->actor_priority;
2166 } else if (key_server) {
2167 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2168 if (os_memcmp(&kay->key_server_sci, &key_server->sci,
2169 sizeof(kay->key_server_sci))) {
2170 ieee802_1x_cp_signal_chgdserver(kay->cp);
2171 ieee802_1x_cp_sm_step(kay->cp);
2174 participant->is_key_server = FALSE;
2175 participant->principal = TRUE;
2176 participant->is_elected = TRUE;
2178 os_memcpy(&kay->key_server_sci, &key_server->sci,
2179 sizeof(kay->key_server_sci));
2180 kay->key_server_priority = key_server->key_server_priority;
2182 participant->principal = FALSE;
2183 participant->is_key_server = FALSE;
2184 participant->is_elected = FALSE;
2192 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2193 * how to use MACsec: whether use MACsec and its capability
2194 * protectFrames will be advised if the key server and one of its live peers are
2195 * MACsec capable and one of those request MACsec protection
2198 ieee802_1x_kay_decide_macsec_use(
2199 struct ieee802_1x_mka_participant *participant)
2201 struct ieee802_1x_kay *kay = participant->kay;
2202 struct ieee802_1x_kay_peer *peer;
2203 enum macsec_cap less_capability;
2206 if (!participant->is_key_server)
2209 /* key server self is MACsec-desired and requesting MACsec */
2210 if (!kay->macsec_desired) {
2211 participant->advised_desired = FALSE;
2214 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2215 participant->advised_desired = FALSE;
2218 less_capability = kay->macsec_capable;
2220 /* at least one of peers is MACsec-desired and requesting MACsec */
2222 dl_list_for_each(peer, &participant->live_peers,
2223 struct ieee802_1x_kay_peer, list) {
2224 if (!peer->macsec_desired)
2227 if (peer->macsec_capbility == MACSEC_CAP_NOT_IMPLEMENTED)
2230 less_capability = (less_capability < peer->macsec_capbility) ?
2231 less_capability : peer->macsec_capbility;
2236 participant->advised_desired = TRUE;
2237 participant->advised_capability = less_capability;
2238 kay->authenticated = FALSE;
2239 kay->secured = TRUE;
2240 kay->failed = FALSE;
2241 ieee802_1x_cp_connect_secure(kay->cp);
2242 ieee802_1x_cp_sm_step(kay->cp);
2244 participant->advised_desired = FALSE;
2245 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2246 participant->to_use_sak = FALSE;
2247 kay->authenticated = TRUE;
2248 kay->secured = FALSE;
2249 kay->failed = FALSE;
2258 ieee802_1x_cp_connect_authenticated(kay->cp);
2259 ieee802_1x_cp_sm_step(kay->cp);
2265 static const u8 pae_group_addr[ETH_ALEN] = {
2266 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2271 * ieee802_1x_kay_encode_mkpdu -
2274 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2275 struct wpabuf *pbuf)
2278 struct ieee8023_hdr *ether_hdr;
2279 struct ieee802_1x_hdr *eapol_hdr;
2281 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2282 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2283 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2284 sizeof(ether_hdr->dest));
2285 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2287 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2288 eapol_hdr->version = EAPOL_VERSION;
2289 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2290 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2292 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2293 if (mak_body_handler[i].body_present &&
2294 mak_body_handler[i].body_present(participant)) {
2295 if (mak_body_handler[i].body_tx(participant, pbuf))
2304 * ieee802_1x_participant_send_mkpdu -
2307 ieee802_1x_participant_send_mkpdu(
2308 struct ieee802_1x_mka_participant *participant)
2311 struct ieee802_1x_kay *kay = participant->kay;
2315 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2316 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2317 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2318 if (mak_body_handler[i].body_present &&
2319 mak_body_handler[i].body_present(participant))
2320 length += mak_body_handler[i].body_length(participant);
2323 buf = wpabuf_alloc(length);
2325 wpa_printf(MSG_ERROR, "KaY: out of memory");
2329 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2330 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2334 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2338 participant->active = TRUE;
2344 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2346 * ieee802_1x_participant_timer -
2348 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2350 struct ieee802_1x_mka_participant *participant;
2351 struct ieee802_1x_kay *kay;
2352 struct ieee802_1x_kay_peer *peer, *pre_peer;
2353 time_t now = time(NULL);
2355 struct receive_sc *rxsc, *pre_rxsc;
2356 struct transmit_sa *txsa, *pre_txsa;
2358 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2359 kay = participant->kay;
2360 if (participant->cak_life) {
2361 if (now > participant->cak_life) {
2362 kay->authenticated = FALSE;
2363 kay->secured = FALSE;
2365 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2370 /* should delete MKA instance if there are not live peers
2371 * when the MKA life elapsed since its creating */
2372 if (participant->mka_life) {
2373 if (dl_list_empty(&participant->live_peers)) {
2374 if (now > participant->mka_life) {
2375 kay->authenticated = FALSE;
2376 kay->secured = FALSE;
2378 ieee802_1x_kay_delete_mka(kay,
2383 participant->mka_life = 0;
2388 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2389 struct ieee802_1x_kay_peer, list) {
2390 if (now > peer->expire) {
2391 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2392 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2394 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2395 dl_list_for_each_safe(rxsc, pre_rxsc,
2396 &participant->rxsc_list,
2397 struct receive_sc, list) {
2398 if (os_memcmp(&rxsc->sci, &peer->sci,
2399 sizeof(rxsc->sci)) == 0) {
2400 secy_delete_receive_sc(kay, rxsc);
2401 ieee802_1x_kay_deinit_receive_sc(
2405 dl_list_del(&peer->list);
2412 if (dl_list_empty(&participant->live_peers)) {
2413 participant->advised_desired = FALSE;
2414 participant->advised_capability =
2415 MACSEC_CAP_NOT_IMPLEMENTED;
2416 participant->to_use_sak = FALSE;
2417 kay->authenticated = TRUE;
2418 kay->secured = FALSE;
2419 kay->failed = FALSE;
2428 dl_list_for_each_safe(txsa, pre_txsa,
2429 &participant->txsc->sa_list,
2430 struct transmit_sa, list) {
2431 secy_disable_transmit_sa(kay, txsa);
2432 ieee802_1x_kay_deinit_transmit_sa(txsa);
2435 ieee802_1x_cp_connect_authenticated(kay->cp);
2436 ieee802_1x_cp_sm_step(kay->cp);
2438 ieee802_1x_kay_elect_key_server(participant);
2439 ieee802_1x_kay_decide_macsec_use(participant);
2443 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2444 struct ieee802_1x_kay_peer, list) {
2445 if (now > peer->expire) {
2446 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2447 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2449 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2450 dl_list_del(&peer->list);
2455 if (participant->new_sak) {
2456 if (!ieee802_1x_kay_generate_new_sak(participant))
2457 participant->to_dist_sak = TRUE;
2459 participant->new_sak = FALSE;
2462 if (participant->retry_count < MAX_RETRY_CNT) {
2463 ieee802_1x_participant_send_mkpdu(participant);
2464 participant->retry_count++;
2467 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2468 ieee802_1x_participant_timer,
2474 * ieee802_1x_kay_init_transmit_sa -
2476 static struct transmit_sa *
2477 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2478 struct data_key *key)
2480 struct transmit_sa *psa;
2482 key->tx_latest = TRUE;
2483 key->rx_latest = TRUE;
2485 psa = os_zalloc(sizeof(*psa));
2487 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2491 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2492 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2493 psa->confidentiality = TRUE;
2495 psa->confidentiality = FALSE;
2499 psa->next_pn = next_PN;
2502 os_get_time(&psa->created_time);
2503 psa->in_use = FALSE;
2505 dl_list_add(&psc->sa_list, &psa->list);
2506 wpa_printf(MSG_DEBUG,
2507 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2508 (int) an, next_PN, psc->channel);
2515 * ieee802_1x_kay_deinit_transmit_sa -
2517 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2520 wpa_printf(MSG_DEBUG,
2521 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2522 psa->an, psa->sc->channel);
2523 dl_list_del(&psa->list);
2529 * init_transmit_sc -
2531 static struct transmit_sc *
2532 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2535 struct transmit_sc *psc;
2537 psc = os_zalloc(sizeof(*psc));
2539 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2542 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2543 psc->channel = channel;
2545 os_get_time(&psc->created_time);
2546 psc->transmitting = FALSE;
2547 psc->encoding_sa = FALSE;
2548 psc->enciphering_sa = FALSE;
2550 dl_list_init(&psc->sa_list);
2551 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2552 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2559 * ieee802_1x_kay_deinit_transmit_sc -
2562 ieee802_1x_kay_deinit_transmit_sc(
2563 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2565 struct transmit_sa *psa, *tmp;
2567 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2569 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2571 secy_disable_transmit_sa(participant->kay, psa);
2572 ieee802_1x_kay_deinit_transmit_sa(psa);
2579 /****************** Interface between CP and KAY *********************/
2581 * ieee802_1x_kay_set_latest_sa_attr -
2583 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2584 struct ieee802_1x_mka_ki *lki, u8 lan,
2585 Boolean ltx, Boolean lrx)
2587 struct ieee802_1x_mka_participant *principal;
2589 principal = ieee802_1x_kay_get_principal_participant(kay);
2594 os_memset(&principal->lki, 0, sizeof(principal->lki));
2596 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2598 principal->lan = lan;
2599 principal->ltx = ltx;
2600 principal->lrx = lrx;
2605 kay->ltx_kn = lki->kn;
2606 kay->lrx_kn = lki->kn;
2616 * ieee802_1x_kay_set_old_sa_attr -
2618 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2619 struct ieee802_1x_mka_ki *oki,
2620 u8 oan, Boolean otx, Boolean orx)
2622 struct ieee802_1x_mka_participant *principal;
2624 principal = ieee802_1x_kay_get_principal_participant(kay);
2629 os_memset(&principal->oki, 0, sizeof(principal->oki));
2631 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2633 principal->oan = oan;
2634 principal->otx = otx;
2635 principal->orx = orx;
2641 kay->otx_kn = oki->kn;
2642 kay->orx_kn = oki->kn;
2652 * ieee802_1x_kay_create_sas -
2654 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2655 struct ieee802_1x_mka_ki *lki)
2657 struct data_key *sa_key, *latest_sak;
2658 struct ieee802_1x_mka_participant *principal;
2659 struct receive_sc *rxsc;
2660 struct receive_sa *rxsa;
2661 struct transmit_sa *txsa;
2663 principal = ieee802_1x_kay_get_principal_participant(kay);
2668 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2669 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2670 sa_key->rx_latest = TRUE;
2671 sa_key->tx_latest = TRUE;
2672 latest_sak = sa_key;
2673 principal->to_use_sak = TRUE;
2675 sa_key->rx_latest = FALSE;
2676 sa_key->tx_latest = FALSE;
2680 wpa_printf(MSG_ERROR, "lki related sak not found");
2684 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2685 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2690 secy_create_receive_sa(kay, rxsa);
2693 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2698 secy_create_transmit_sa(kay, txsa);
2707 * ieee802_1x_kay_delete_sas -
2709 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2710 struct ieee802_1x_mka_ki *ki)
2712 struct data_key *sa_key, *pre_key;
2713 struct transmit_sa *txsa, *pre_txsa;
2714 struct receive_sa *rxsa, *pre_rxsa;
2715 struct receive_sc *rxsc;
2716 struct ieee802_1x_mka_participant *principal;
2718 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2719 principal = ieee802_1x_kay_get_principal_participant(kay);
2723 /* remove the transmit sa */
2724 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2725 struct transmit_sa, list) {
2726 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2727 secy_disable_transmit_sa(kay, txsa);
2728 ieee802_1x_kay_deinit_transmit_sa(txsa);
2732 /* remove the receive sa */
2733 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2734 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2735 struct receive_sa, list) {
2736 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2737 secy_disable_receive_sa(kay, rxsa);
2738 ieee802_1x_kay_deinit_receive_sa(rxsa);
2743 /* remove the sak */
2744 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2745 struct data_key, list) {
2746 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2747 ieee802_1x_kay_deinit_data_key(sa_key);
2750 if (principal->new_key == sa_key)
2751 principal->new_key = NULL;
2759 * ieee802_1x_kay_enable_tx_sas -
2761 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2762 struct ieee802_1x_mka_ki *lki)
2764 struct ieee802_1x_mka_participant *principal;
2765 struct transmit_sa *txsa;
2767 principal = ieee802_1x_kay_get_principal_participant(kay);
2771 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2773 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2774 txsa->in_use = TRUE;
2775 secy_enable_transmit_sa(kay, txsa);
2776 ieee802_1x_cp_set_usingtransmitas(
2777 principal->kay->cp, TRUE);
2778 ieee802_1x_cp_sm_step(principal->kay->cp);
2787 * ieee802_1x_kay_enable_rx_sas -
2789 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2790 struct ieee802_1x_mka_ki *lki)
2792 struct ieee802_1x_mka_participant *principal;
2793 struct receive_sa *rxsa;
2794 struct receive_sc *rxsc;
2796 principal = ieee802_1x_kay_get_principal_participant(kay);
2800 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2801 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2803 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2804 rxsa->in_use = TRUE;
2805 secy_enable_receive_sa(kay, rxsa);
2806 ieee802_1x_cp_set_usingreceivesas(
2807 principal->kay->cp, TRUE);
2808 ieee802_1x_cp_sm_step(principal->kay->cp);
2818 * ieee802_1x_kay_enable_new_info -
2820 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2822 struct ieee802_1x_mka_participant *principal;
2824 principal = ieee802_1x_kay_get_principal_participant(kay);
2828 if (principal->retry_count < MAX_RETRY_CNT) {
2829 ieee802_1x_participant_send_mkpdu(principal);
2830 principal->retry_count++;
2838 * ieee802_1x_kay_cp_conf -
2840 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2841 struct ieee802_1x_cp_conf *pconf)
2843 pconf->protect = kay->macsec_protect;
2844 pconf->replay_protect = kay->macsec_replay_protect;
2845 pconf->validate = kay->macsec_validate;
2852 * ieee802_1x_kay_alloc_cp_sm -
2854 static struct ieee802_1x_cp_sm *
2855 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2857 struct ieee802_1x_cp_conf conf;
2859 os_memset(&conf, 0, sizeof(conf));
2860 conf.protect = kay->macsec_protect;
2861 conf.replay_protect = kay->macsec_replay_protect;
2862 conf.validate = kay->macsec_validate;
2863 conf.replay_window = kay->macsec_replay_window;
2865 return ieee802_1x_cp_sm_init(kay, &conf);
2870 * ieee802_1x_kay_mkpdu_sanity_check -
2871 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2873 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2874 const u8 *buf, size_t len)
2876 struct ieee8023_hdr *eth_hdr;
2877 struct ieee802_1x_hdr *eapol_hdr;
2878 struct ieee802_1x_mka_hdr *mka_hdr;
2879 struct ieee802_1x_mka_basic_body *body;
2881 struct ieee802_1x_mka_participant *participant;
2883 u8 icv[MAX_ICV_LEN];
2886 eth_hdr = (struct ieee8023_hdr *) buf;
2887 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2888 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2890 /* destination address should be not individual address */
2891 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2892 wpa_printf(MSG_MSGDUMP,
2893 "KaY: ethernet destination address is not PAE group address");
2897 /* MKPDU should not less than 32 octets */
2898 mka_msg_len = be_to_host16(eapol_hdr->length);
2899 if (mka_msg_len < 32) {
2900 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2903 /* MKPDU should multiple 4 octets */
2904 if ((mka_msg_len % 4) != 0) {
2905 wpa_printf(MSG_MSGDUMP,
2906 "KaY: MKPDU is not multiple of 4 octets");
2910 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2911 ieee802_1x_mka_dump_basic_body(body);
2912 body_len = get_mka_param_body_len(body);
2913 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2914 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2915 wpa_printf(MSG_ERROR,
2916 "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",
2917 (int) mka_msg_len, (int) MKA_HDR_LEN,
2918 (int) body_len, DEFAULT_ICV_LEN);
2922 /* CKN should be owned by I */
2923 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2925 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2929 /* algorithm agility check */
2930 if (os_memcmp(body->algo_agility, mka_algo_agility,
2931 sizeof(body->algo_agility)) != 0) {
2932 wpa_printf(MSG_ERROR,
2933 "KaY: peer's algorithm agility not supported for me");
2939 * The ICV will comprise the final octets of the packet body, whatever
2940 * its size, not the fixed length 16 octets, indicated by the EAPOL
2941 * packet body length.
2943 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2944 participant->ick.key,
2945 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2946 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2949 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2953 if (os_memcmp_const(msg_icv, icv,
2954 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2956 wpa_printf(MSG_ERROR,
2957 "KaY: Computed ICV is not equal to Received ICV");
2961 wpa_printf(MSG_ERROR, "KaY: No ICV");
2970 * ieee802_1x_kay_decode_mkpdu -
2972 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2973 const u8 *buf, size_t len)
2975 struct ieee802_1x_mka_participant *participant;
2976 struct ieee802_1x_mka_hdr *hdr;
2982 Boolean my_included;
2983 Boolean handled[256];
2985 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2988 /* handle basic parameter set */
2989 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2990 left_len = len - sizeof(struct ieee8023_hdr) -
2991 sizeof(struct ieee802_1x_hdr);
2992 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2996 /* to skip basic parameter set */
2997 hdr = (struct ieee802_1x_mka_hdr *) pos;
2998 body_len = get_mka_param_body_len(hdr);
2999 pos += body_len + MKA_HDR_LEN;
3000 left_len -= body_len + MKA_HDR_LEN;
3002 /* check i am in the peer's peer list */
3003 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3005 /* accept the peer as live peer */
3006 if (!ieee802_1x_kay_is_in_peer(
3008 participant->current_peer_id.mi)) {
3009 if (!ieee802_1x_kay_create_live_peer(
3011 participant->current_peer_id.mi,
3013 participant->current_peer_id.mn)))
3015 ieee802_1x_kay_elect_key_server(participant);
3016 ieee802_1x_kay_decide_macsec_use(participant);
3018 if (ieee802_1x_kay_is_in_potential_peer(
3019 participant, participant->current_peer_id.mi)) {
3020 ieee802_1x_kay_move_live_peer(
3021 participant, participant->current_peer_id.mi,
3022 be_to_host32(participant->current_peer_id.mn));
3023 ieee802_1x_kay_elect_key_server(participant);
3024 ieee802_1x_kay_decide_macsec_use(participant);
3029 * Handle other parameter set than basic parameter set.
3030 * Each parameter set should be present only once.
3032 for (i = 0; i < 256; i++)
3036 while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3037 hdr = (struct ieee802_1x_mka_hdr *) pos;
3038 body_len = get_mka_param_body_len(hdr);
3039 body_type = get_mka_param_body_type(hdr);
3041 if (body_type == MKA_ICV_INDICATOR)
3044 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3045 wpa_printf(MSG_ERROR,
3046 "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",
3047 (int) left_len, (int) MKA_HDR_LEN,
3048 (int) body_len, DEFAULT_ICV_LEN);
3052 if (handled[body_type])
3055 handled[body_type] = TRUE;
3056 if (mak_body_handler[body_type].body_rx) {
3057 mak_body_handler[body_type].body_rx
3058 (participant, pos, left_len);
3060 wpa_printf(MSG_ERROR,
3061 "The type %d not supported in this MKA version %d",
3062 body_type, MKA_VERSION_ID);
3066 pos += body_len + MKA_HDR_LEN;
3067 left_len -= body_len + MKA_HDR_LEN;
3071 participant->retry_count = 0;
3072 participant->active = TRUE;
3079 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3082 struct ieee802_1x_kay *kay = ctx;
3083 struct ieee8023_hdr *eth_hdr;
3084 struct ieee802_1x_hdr *eapol_hdr;
3086 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3087 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3088 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3089 (unsigned long) len);
3093 eth_hdr = (struct ieee8023_hdr *) buf;
3094 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3095 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3096 be_to_host16(eapol_hdr->length)) {
3097 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3098 (unsigned long) len,
3099 (unsigned long) be_to_host16(eapol_hdr->length));
3103 if (eapol_hdr->version < EAPOL_VERSION) {
3104 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3105 eapol_hdr->version);
3108 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3109 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3112 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3113 if (dl_list_empty(&kay->participant_list)) {
3114 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3118 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3123 * ieee802_1x_kay_init -
3125 struct ieee802_1x_kay *
3126 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3127 const char *ifname, const u8 *addr)
3129 struct ieee802_1x_kay *kay;
3131 kay = os_zalloc(sizeof(*kay));
3133 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3140 kay->active = FALSE;
3142 kay->authenticated = FALSE;
3143 kay->secured = FALSE;
3144 kay->failed = FALSE;
3145 kay->policy = policy;
3147 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3148 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3149 kay->actor_sci.port = host_to_be16(0x0001);
3150 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3152 /* While actor acts as a key server, shall distribute sakey */
3157 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3158 kay->macsec_csindex = DEFAULT_CS_INDEX;
3159 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3160 kay->mka_version = MKA_VERSION_ID;
3162 os_memcpy(kay->algo_agility, mka_algo_agility,
3163 sizeof(kay->algo_agility));
3165 dl_list_init(&kay->participant_list);
3167 if (policy == DO_NOT_SECURE) {
3168 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3169 kay->macsec_desired = FALSE;
3170 kay->macsec_protect = FALSE;
3171 kay->macsec_validate = Disabled;
3172 kay->macsec_replay_protect = FALSE;
3173 kay->macsec_replay_window = 0;
3174 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3176 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3177 kay->macsec_desired = TRUE;
3178 kay->macsec_protect = TRUE;
3179 kay->macsec_validate = Strict;
3180 kay->macsec_replay_protect = FALSE;
3181 kay->macsec_replay_window = 0;
3182 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3185 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3187 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3188 secy_init_macsec(kay);
3189 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3191 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3194 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3195 if (kay->cp == NULL) {
3196 ieee802_1x_kay_deinit(kay);
3200 if (policy == DO_NOT_SECURE) {
3201 ieee802_1x_cp_connect_authenticated(kay->cp);
3202 ieee802_1x_cp_sm_step(kay->cp);
3204 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3205 kay_l2_receive, kay, 1);
3206 if (kay->l2_mka == NULL) {
3207 wpa_printf(MSG_WARNING,
3208 "KaY: Failed to initialize L2 packet processing for MKA packet");
3209 ieee802_1x_kay_deinit(kay);
3219 * ieee802_1x_kay_deinit -
3222 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3224 struct ieee802_1x_mka_participant *participant;
3229 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3231 while (!dl_list_empty(&kay->participant_list)) {
3232 participant = dl_list_entry(kay->participant_list.next,
3233 struct ieee802_1x_mka_participant,
3235 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3238 ieee802_1x_cp_sm_deinit(kay->cp);
3239 secy_deinit_macsec(kay);
3242 l2_packet_deinit(kay->l2_mka);
3252 * ieee802_1x_kay_create_mka -
3254 struct ieee802_1x_mka_participant *
3255 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3256 struct mka_key *cak, u32 life,
3257 enum mka_created_mode mode, Boolean is_authenticator)
3259 struct ieee802_1x_mka_participant *participant;
3262 if (!kay || !ckn || !cak) {
3263 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3267 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3268 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3271 if (ckn->len > MAX_CKN_LEN) {
3272 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3276 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3280 participant = os_zalloc(sizeof(*participant));
3282 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3286 participant->ckn.len = ckn->len;
3287 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3288 participant->cak.len = cak->len;
3289 os_memcpy(participant->cak.key, cak->key, cak->len);
3291 participant->cak_life = life + time(NULL);
3295 if (is_authenticator) {
3296 participant->is_obliged_key_server = TRUE;
3297 participant->can_be_key_server = TRUE;
3298 participant->is_key_server = TRUE;
3299 participant->principal = TRUE;
3301 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3302 sizeof(kay->key_server_sci));
3303 kay->key_server_priority = kay->actor_priority;
3304 participant->is_elected = TRUE;
3306 participant->is_obliged_key_server = FALSE;
3307 participant->can_be_key_server = FALSE;
3308 participant->is_key_server = FALSE;
3309 participant->is_elected = TRUE;
3314 participant->is_obliged_key_server = FALSE;
3315 participant->can_be_key_server = TRUE;
3316 participant->is_key_server = TRUE;
3317 participant->is_elected = FALSE;
3321 participant->cached = FALSE;
3323 participant->active = FALSE;
3324 participant->participant = FALSE;
3325 participant->retain = FALSE;
3326 participant->activate = DEFAULT;
3328 if (participant->is_key_server)
3329 participant->principal = TRUE;
3331 dl_list_init(&participant->live_peers);
3332 dl_list_init(&participant->potential_peers);
3334 participant->retry_count = 0;
3335 participant->kay = kay;
3337 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3339 participant->mn = 0;
3341 participant->lrx = FALSE;
3342 participant->ltx = FALSE;
3343 participant->orx = FALSE;
3344 participant->otx = FALSE;
3345 participant->to_dist_sak = FALSE;
3346 participant->to_use_sak = FALSE;
3347 participant->new_sak = FALSE;
3348 dl_list_init(&participant->sak_list);
3349 participant->new_key = NULL;
3350 dl_list_init(&participant->rxsc_list);
3351 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3353 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3354 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3355 kay->macsec_replay_window);
3356 secy_create_transmit_sc(kay, participant->txsc);
3358 /* to derive KEK from CAK and CKN */
3359 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3360 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3361 participant->ckn.name,
3362 participant->ckn.len,
3363 participant->kek.key)) {
3364 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3367 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3368 participant->kek.key, participant->kek.len);
3370 /* to derive ICK from CAK and CKN */
3371 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3372 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3373 participant->ckn.name,
3374 participant->ckn.len,
3375 participant->ick.key)) {
3376 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3379 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3380 participant->ick.key, participant->ick.len);
3382 dl_list_add(&kay->participant_list, &participant->list);
3383 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3384 ckn->name, ckn->len);
3386 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3387 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3389 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3395 os_free(participant);
3401 * ieee802_1x_kay_delete_mka -
3404 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3406 struct ieee802_1x_mka_participant *participant;
3407 struct ieee802_1x_kay_peer *peer;
3408 struct data_key *sak;
3409 struct receive_sc *rxsc;
3414 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3416 /* get the participant */
3417 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3419 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3420 ckn->name, ckn->len);
3424 dl_list_del(&participant->list);
3426 /* remove live peer */
3427 while (!dl_list_empty(&participant->live_peers)) {
3428 peer = dl_list_entry(participant->live_peers.next,
3429 struct ieee802_1x_kay_peer, list);
3430 dl_list_del(&peer->list);
3434 /* remove potential peer */
3435 while (!dl_list_empty(&participant->potential_peers)) {
3436 peer = dl_list_entry(participant->potential_peers.next,
3437 struct ieee802_1x_kay_peer, list);
3438 dl_list_del(&peer->list);
3443 while (!dl_list_empty(&participant->sak_list)) {
3444 sak = dl_list_entry(participant->sak_list.next,
3445 struct data_key, list);
3446 dl_list_del(&sak->list);
3450 while (!dl_list_empty(&participant->rxsc_list)) {
3451 rxsc = dl_list_entry(participant->rxsc_list.next,
3452 struct receive_sc, list);
3453 secy_delete_receive_sc(kay, rxsc);
3454 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3456 secy_delete_transmit_sc(kay, participant->txsc);
3457 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3459 os_memset(&participant->cak, 0, sizeof(participant->cak));
3460 os_memset(&participant->kek, 0, sizeof(participant->kek));
3461 os_memset(&participant->ick, 0, sizeof(participant->ick));
3462 os_free(participant);
3467 * ieee802_1x_kay_mka_participate -
3469 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3470 struct mka_key_name *ckn,
3473 struct ieee802_1x_mka_participant *participant;
3478 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3482 participant->active = status;
3487 * ieee802_1x_kay_new_sak -
3490 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3492 struct ieee802_1x_mka_participant *participant;
3497 participant = ieee802_1x_kay_get_principal_participant(kay);
3501 participant->new_sak = TRUE;
3502 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3509 * ieee802_1x_kay_change_cipher_suite -
3512 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3514 struct ieee802_1x_mka_participant *participant;
3519 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3520 wpa_printf(MSG_ERROR,
3521 "KaY: Configured cipher suite index is out of range");
3524 if (kay->macsec_csindex == cs_index)
3528 kay->macsec_desired = FALSE;
3530 kay->macsec_csindex = cs_index;
3531 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3533 participant = ieee802_1x_kay_get_principal_participant(kay);
3535 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3536 participant->new_sak = TRUE;