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,
535 dl_list_add(&participant->live_peers, &peer->list);
537 secy_get_available_receive_sc(participant->kay, &sc_ch);
539 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
543 dl_list_add(&participant->rxsc_list, &rxsc->list);
544 secy_create_receive_sc(participant->kay, rxsc);
546 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
547 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
548 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
549 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
550 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
557 * ieee802_1x_kay_create_potential_peer
559 static struct ieee802_1x_kay_peer *
560 ieee802_1x_kay_create_potential_peer(
561 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
563 struct ieee802_1x_kay_peer *peer;
565 peer = os_zalloc(sizeof(*peer));
567 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
571 os_memcpy(peer->mi, mi, MI_LEN);
573 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
574 peer->sak_used = FALSE;
576 dl_list_add(&participant->potential_peers, &peer->list);
578 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
579 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
580 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
581 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
582 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
589 * ieee802_1x_kay_move_live_peer
591 static struct ieee802_1x_kay_peer *
592 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
595 struct ieee802_1x_kay_peer *peer;
596 struct receive_sc *rxsc;
599 dl_list_for_each(peer, &participant->potential_peers,
600 struct ieee802_1x_kay_peer, list) {
601 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
605 os_memcpy(&peer->sci, &participant->current_peer_sci,
608 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
610 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
611 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
612 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
613 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
614 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
616 dl_list_del(&peer->list);
617 dl_list_add_tail(&participant->live_peers, &peer->list);
619 secy_get_available_receive_sc(participant->kay, &sc_ch);
621 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
625 dl_list_add(&participant->rxsc_list, &rxsc->list);
626 secy_create_receive_sc(participant->kay, rxsc);
634 * ieee802_1x_mka_basic_body_present -
637 ieee802_1x_mka_basic_body_present(
638 struct ieee802_1x_mka_participant *participant)
645 * ieee802_1x_mka_basic_body_length -
648 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
652 length = sizeof(struct ieee802_1x_mka_basic_body);
653 length += participant->ckn.len;
654 return (length + 0x3) & ~0x3;
659 * ieee802_1x_mka_encode_basic_body
662 ieee802_1x_mka_encode_basic_body(
663 struct ieee802_1x_mka_participant *participant,
666 struct ieee802_1x_mka_basic_body *body;
667 struct ieee802_1x_kay *kay = participant->kay;
668 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
670 body = wpabuf_put(buf, length);
672 body->version = kay->mka_version;
673 body->priority = kay->actor_priority;
674 if (participant->is_elected)
675 body->key_server = participant->is_key_server;
677 body->key_server = participant->can_be_key_server;
679 body->macsec_desired = kay->macsec_desired;
680 body->macsec_capbility = kay->macsec_capable;
681 set_mka_param_body_len(body, length - MKA_HDR_LEN);
683 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
684 sizeof(kay->actor_sci.addr));
685 body->actor_sci.port = kay->actor_sci.port;
687 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
688 participant->mn = participant->mn + 1;
689 body->actor_mn = host_to_be32(participant->mn);
690 os_memcpy(body->algo_agility, participant->kay->algo_agility,
691 sizeof(body->algo_agility));
693 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
695 ieee802_1x_mka_dump_basic_body(body);
702 * ieee802_1x_mka_decode_basic_body -
704 static struct ieee802_1x_mka_participant *
705 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
708 struct ieee802_1x_mka_participant *participant;
709 const struct ieee802_1x_mka_basic_body *body;
710 struct ieee802_1x_kay_peer *peer;
712 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
714 if (body->version > MKA_VERSION_ID) {
715 wpa_printf(MSG_DEBUG,
716 "KaY: peer's version(%d) greater than mka current version(%d)",
717 body->version, MKA_VERSION_ID);
719 if (kay->is_obliged_key_server && body->key_server) {
720 wpa_printf(MSG_DEBUG, "I must be as key server");
724 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
726 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
730 /* If the peer's MI is my MI, I will choose new MI */
731 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
732 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
737 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
738 participant->current_peer_id.mn = be_to_host32(body->actor_mn);
739 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
740 sizeof(participant->current_peer_sci.addr));
741 participant->current_peer_sci.port = body->actor_sci.port;
744 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
746 /* Check duplicated SCI */
747 /* TODO: What policy should be applied to detect duplicated SCI
748 * is active attacker or a valid peer whose MI is be changed?
750 peer = ieee802_1x_kay_get_peer_sci(participant,
753 wpa_printf(MSG_WARNING,
754 "KaY: duplicated SCI detected, Maybe active attacker");
755 dl_list_del(&peer->list);
759 peer = ieee802_1x_kay_create_potential_peer(
760 participant, body->actor_mi,
761 be_to_host32(body->actor_mn));
765 peer->macsec_desired = body->macsec_desired;
766 peer->macsec_capbility = body->macsec_capbility;
767 peer->is_key_server = (Boolean) body->key_server;
768 peer->key_server_priority = body->priority;
769 } else if (peer->mn < be_to_host32(body->actor_mn)) {
770 peer->mn = be_to_host32(body->actor_mn);
771 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
772 peer->macsec_desired = body->macsec_desired;
773 peer->macsec_capbility = body->macsec_capbility;
774 peer->is_key_server = (Boolean) body->key_server;
775 peer->key_server_priority = body->priority;
777 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
786 * ieee802_1x_mka_live_peer_body_present
789 ieee802_1x_mka_live_peer_body_present(
790 struct ieee802_1x_mka_participant *participant)
792 return !dl_list_empty(&participant->live_peers);
797 * ieee802_1x_kay_get_live_peer_length
800 ieee802_1x_mka_get_live_peer_length(
801 struct ieee802_1x_mka_participant *participant)
803 int len = MKA_HDR_LEN;
804 struct ieee802_1x_kay_peer *peer;
806 dl_list_for_each(peer, &participant->live_peers,
807 struct ieee802_1x_kay_peer, list)
808 len += sizeof(struct ieee802_1x_mka_peer_id);
810 return (len + 0x3) & ~0x3;
815 * ieee802_1x_mka_encode_live_peer_body -
818 ieee802_1x_mka_encode_live_peer_body(
819 struct ieee802_1x_mka_participant *participant,
822 struct ieee802_1x_mka_peer_body *body;
823 struct ieee802_1x_kay_peer *peer;
825 struct ieee802_1x_mka_peer_id *body_peer;
827 length = ieee802_1x_mka_get_live_peer_length(participant);
828 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
830 body->type = MKA_LIVE_PEER_LIST;
831 set_mka_param_body_len(body, length - MKA_HDR_LEN);
833 dl_list_for_each(peer, &participant->live_peers,
834 struct ieee802_1x_kay_peer, list) {
835 body_peer = wpabuf_put(buf,
836 sizeof(struct ieee802_1x_mka_peer_id));
837 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
838 body_peer->mn = host_to_be32(peer->mn);
842 ieee802_1x_mka_dump_peer_body(body);
847 * ieee802_1x_mka_potential_peer_body_present
850 ieee802_1x_mka_potential_peer_body_present(
851 struct ieee802_1x_mka_participant *participant)
853 return !dl_list_empty(&participant->potential_peers);
858 * ieee802_1x_kay_get_potential_peer_length
861 ieee802_1x_mka_get_potential_peer_length(
862 struct ieee802_1x_mka_participant *participant)
864 int len = MKA_HDR_LEN;
865 struct ieee802_1x_kay_peer *peer;
867 dl_list_for_each(peer, &participant->potential_peers,
868 struct ieee802_1x_kay_peer, list)
869 len += sizeof(struct ieee802_1x_mka_peer_id);
871 return (len + 0x3) & ~0x3;
876 * ieee802_1x_mka_encode_potential_peer_body -
879 ieee802_1x_mka_encode_potential_peer_body(
880 struct ieee802_1x_mka_participant *participant,
883 struct ieee802_1x_mka_peer_body *body;
884 struct ieee802_1x_kay_peer *peer;
886 struct ieee802_1x_mka_peer_id *body_peer;
888 length = ieee802_1x_mka_get_potential_peer_length(participant);
889 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
891 body->type = MKA_POTENTIAL_PEER_LIST;
892 set_mka_param_body_len(body, length - MKA_HDR_LEN);
894 dl_list_for_each(peer, &participant->potential_peers,
895 struct ieee802_1x_kay_peer, list) {
896 body_peer = wpabuf_put(buf,
897 sizeof(struct ieee802_1x_mka_peer_id));
898 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
899 body_peer->mn = host_to_be32(peer->mn);
903 ieee802_1x_mka_dump_peer_body(body);
909 * ieee802_1x_mka_i_in_peerlist -
912 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
913 const u8 *mka_msg, size_t msg_len)
915 Boolean included = FALSE;
916 struct ieee802_1x_mka_hdr *hdr;
928 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
929 hdr = (struct ieee802_1x_mka_hdr *) pos;
930 body_len = get_mka_param_body_len(hdr);
931 body_type = get_mka_param_body_type(hdr);
933 if (body_type != MKA_LIVE_PEER_LIST &&
934 body_type != MKA_POTENTIAL_PEER_LIST)
937 ieee802_1x_mka_dump_peer_body(
938 (struct ieee802_1x_mka_peer_body *)pos);
940 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
941 wpa_printf(MSG_ERROR,
942 "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",
943 (int) left_len, (int) MKA_HDR_LEN,
944 (int) body_len, DEFAULT_ICV_LEN);
948 if ((body_len % 16) != 0) {
949 wpa_printf(MSG_ERROR,
950 "KaY: MKA Peer Packet Body Length (%d bytes) should multiple of 16 octets",
955 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
956 peer_mi = MKA_HDR_LEN + pos + i;
957 os_memcpy(&_peer_mn, peer_mi + MI_LEN,
959 peer_mn = be_to_host32(_peer_mn);
960 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0 &&
961 peer_mn == participant->mn) {
971 left_len -= body_len + MKA_HDR_LEN;
972 pos += body_len + MKA_HDR_LEN;
980 * ieee802_1x_mka_decode_live_peer_body -
982 static int ieee802_1x_mka_decode_live_peer_body(
983 struct ieee802_1x_mka_participant *participant,
984 const u8 *peer_msg, size_t msg_len)
986 const struct ieee802_1x_mka_hdr *hdr;
987 struct ieee802_1x_kay_peer *peer;
995 is_included = ieee802_1x_kay_is_in_live_peer(
996 participant, participant->current_peer_id.mi);
998 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
999 body_len = get_mka_param_body_len(hdr);
1001 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1002 peer_mi = MKA_HDR_LEN + peer_msg + i;
1003 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1004 peer_mn = be_to_host32(_peer_mn);
1007 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1008 /* My message id is used by other participant */
1009 if (peer_mn > participant->mn) {
1010 if (os_get_random(participant->mi,
1011 sizeof(participant->mi)) < 0)
1012 wpa_printf(MSG_DEBUG,
1013 "KaY: Could not update mi");
1014 participant->mn = 0;
1021 peer = ieee802_1x_kay_get_peer(participant, peer_mi);
1024 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1026 if (!ieee802_1x_kay_create_potential_peer(
1027 participant, peer_mi, peer_mn)) {
1038 * ieee802_1x_mka_decode_potential_peer_body -
1041 ieee802_1x_mka_decode_potential_peer_body(
1042 struct ieee802_1x_mka_participant *participant,
1043 const u8 *peer_msg, size_t msg_len)
1045 struct ieee802_1x_mka_hdr *hdr;
1052 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1053 body_len = get_mka_param_body_len(hdr);
1055 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1056 peer_mi = MKA_HDR_LEN + peer_msg + i;
1057 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1058 peer_mn = be_to_host32(_peer_mn);
1061 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1062 /* My message id is used by other participant */
1063 if (peer_mn > participant->mn) {
1064 if (os_get_random(participant->mi,
1065 sizeof(participant->mi)) < 0)
1066 wpa_printf(MSG_DEBUG,
1067 "KaY: Could not update mi");
1068 participant->mn = 0;
1079 * ieee802_1x_mka_sak_use_body_present
1082 ieee802_1x_mka_sak_use_body_present(
1083 struct ieee802_1x_mka_participant *participant)
1085 if (participant->to_use_sak)
1093 * ieee802_1x_mka_get_sak_use_length
1096 ieee802_1x_mka_get_sak_use_length(
1097 struct ieee802_1x_mka_participant *participant)
1099 int length = MKA_HDR_LEN;
1101 if (participant->kay->macsec_desired && participant->advised_desired)
1102 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1104 length = MKA_HDR_LEN;
1106 length = (length + 0x3) & ~0x3;
1116 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1117 struct ieee802_1x_mka_ki *ki)
1119 struct receive_sa *rxsa;
1120 struct receive_sc *rxsc;
1123 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1124 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1126 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1127 secy_get_receive_lowest_pn(principal->kay,
1130 lpn = lpn > rxsa->lowest_pn ?
1131 lpn : rxsa->lowest_pn;
1145 * ieee802_1x_mka_encode_sak_use_body -
1148 ieee802_1x_mka_encode_sak_use_body(
1149 struct ieee802_1x_mka_participant *participant,
1152 struct ieee802_1x_mka_sak_use_body *body;
1153 unsigned int length;
1156 length = ieee802_1x_mka_get_sak_use_length(participant);
1157 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_sak_use_body));
1159 body->type = MKA_SAK_USE;
1160 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1162 if (length == MKA_HDR_LEN) {
1168 body->delay_protect = FALSE;
1172 /* data protect, lowest accept packet number */
1173 body->delay_protect = participant->kay->macsec_replay_protect;
1174 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1175 if (pn > participant->kay->pn_exhaustion) {
1176 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1177 if (participant->is_key_server)
1178 participant->new_sak = TRUE;
1181 body->llpn = host_to_be32(pn);
1182 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1183 body->olpn = host_to_be32(pn);
1185 /* plain tx, plain rx */
1186 if (participant->kay->macsec_protect)
1191 if (participant->kay->macsec_validate == Strict)
1196 /* latest key: rx, tx, key server member identifier key number */
1197 body->lan = participant->lan;
1198 os_memcpy(body->lsrv_mi, participant->lki.mi,
1199 sizeof(body->lsrv_mi));
1200 body->lkn = host_to_be32(participant->lki.kn);
1201 body->lrx = participant->lrx;
1202 body->ltx = participant->ltx;
1204 /* old key: rx, tx, key server member identifier key number */
1205 body->oan = participant->oan;
1206 if (participant->oki.kn != participant->lki.kn &&
1207 participant->oki.kn != 0) {
1210 os_memcpy(body->osrv_mi, participant->oki.mi,
1211 sizeof(body->osrv_mi));
1212 body->okn = host_to_be32(participant->oki.kn);
1218 /* set CP's variable */
1220 if (!participant->kay->tx_enable)
1221 participant->kay->tx_enable = TRUE;
1223 if (!participant->kay->port_enable)
1224 participant->kay->port_enable = TRUE;
1227 if (!participant->kay->rx_enable)
1228 participant->kay->rx_enable = TRUE;
1231 ieee802_1x_mka_dump_sak_use_body(body);
1237 * ieee802_1x_mka_decode_sak_use_body -
1240 ieee802_1x_mka_decode_sak_use_body(
1241 struct ieee802_1x_mka_participant *participant,
1242 const u8 *mka_msg, size_t msg_len)
1244 struct ieee802_1x_mka_hdr *hdr;
1245 struct ieee802_1x_mka_sak_use_body *body;
1246 struct ieee802_1x_kay_peer *peer;
1247 struct transmit_sa *txsa;
1248 struct data_key *sa_key = NULL;
1250 struct ieee802_1x_mka_ki ki;
1252 Boolean all_receiving;
1255 if (!participant->principal) {
1256 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1259 peer = ieee802_1x_kay_get_live_peer(participant,
1260 participant->current_peer_id.mi);
1262 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1266 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1267 body_len = get_mka_param_body_len(hdr);
1268 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1269 ieee802_1x_mka_dump_sak_use_body(body);
1271 if ((body_len != 0) && (body_len < 40)) {
1272 wpa_printf(MSG_ERROR,
1273 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 40, or more octets",
1278 /* TODO: what action should I take when peer does not support MACsec */
1279 if (body_len == 0) {
1280 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1284 /* TODO: when the plain tx or rx of peer is true, should I change
1285 * the attribute of controlled port
1288 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1291 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1293 /* check latest key is valid */
1294 if (body->ltx || body->lrx) {
1296 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1297 ki.kn = be_to_host32(body->lkn);
1298 dl_list_for_each(sa_key, &participant->sak_list,
1299 struct data_key, list) {
1300 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1306 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1309 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1310 sizeof(participant->lki.mi)) == 0 &&
1311 be_to_host32(body->lkn) == participant->lki.kn &&
1312 body->lan == participant->lan) {
1313 peer->sak_used = TRUE;
1315 if (body->ltx && peer->is_key_server) {
1316 ieee802_1x_cp_set_servertransmitting(
1317 participant->kay->cp, TRUE);
1318 ieee802_1x_cp_sm_step(participant->kay->cp);
1322 /* check old key is valid */
1323 if (body->otx || body->orx) {
1324 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1325 sizeof(participant->oki.mi)) != 0 ||
1326 be_to_host32(body->okn) != participant->oki.kn ||
1327 body->oan != participant->oan) {
1328 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1333 /* TODO: how to set the MACsec hardware when delay_protect is true */
1334 if (body->delay_protect &&
1335 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1336 wpa_printf(MSG_WARNING,
1337 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1341 /* check all live peer have used the sak for receiving sa */
1342 all_receiving = TRUE;
1343 dl_list_for_each(peer, &participant->live_peers,
1344 struct ieee802_1x_kay_peer, list) {
1345 if (!peer->sak_used) {
1346 all_receiving = FALSE;
1350 if (all_receiving) {
1351 participant->to_dist_sak = FALSE;
1352 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1353 ieee802_1x_cp_sm_step(participant->kay->cp);
1356 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1357 lpn = be_to_host32(body->llpn);
1358 if (lpn > participant->kay->pn_exhaustion) {
1359 if (participant->is_key_server) {
1360 participant->new_sak = TRUE;
1361 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1366 dl_list_for_each(txsa, &participant->txsc->sa_list,
1367 struct transmit_sa, list) {
1368 if (sa_key != NULL && txsa->pkey == sa_key) {
1374 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1378 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1379 * npn is larger than txsa's npn, set it to txsa.
1381 secy_get_transmit_next_pn(participant->kay, txsa);
1382 if (lpn > txsa->next_pn) {
1383 secy_set_transmit_next_pn(participant->kay, txsa);
1384 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1392 * ieee802_1x_mka_dist_sak_body_present
1395 ieee802_1x_mka_dist_sak_body_present(
1396 struct ieee802_1x_mka_participant *participant)
1398 if (!participant->to_dist_sak || !participant->new_key)
1406 * ieee802_1x_kay_get_dist_sak_length
1409 ieee802_1x_mka_get_dist_sak_length(
1410 struct ieee802_1x_mka_participant *participant)
1413 int cs_index = participant->kay->macsec_csindex;
1415 if (participant->advised_desired) {
1416 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1417 if (cs_index != DEFAULT_CS_INDEX)
1418 length += CS_ID_LEN;
1420 length += cipher_suite_tbl[cs_index].sak_len + 8;
1422 length = MKA_HDR_LEN;
1424 length = (length + 0x3) & ~0x3;
1431 * ieee802_1x_mka_encode_dist_sak_body -
1434 ieee802_1x_mka_encode_dist_sak_body(
1435 struct ieee802_1x_mka_participant *participant,
1438 struct ieee802_1x_mka_dist_sak_body *body;
1439 struct data_key *sak;
1440 unsigned int length;
1444 length = ieee802_1x_mka_get_dist_sak_length(participant);
1445 body = wpabuf_put(buf, length);
1446 body->type = MKA_DISTRIBUTED_SAK;
1447 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1448 if (length == MKA_HDR_LEN) {
1449 body->confid_offset = 0;
1454 sak = participant->new_key;
1455 body->confid_offset = sak->confidentiality_offset;
1456 body->dan = sak->an;
1457 body->kn = host_to_be32(sak->key_identifier.kn);
1458 cs_index = participant->kay->macsec_csindex;
1460 if (cs_index != DEFAULT_CS_INDEX) {
1461 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1462 sak_pos = CS_ID_LEN;
1464 if (aes_wrap(participant->kek.key, 16,
1465 cipher_suite_tbl[cs_index].sak_len / 8,
1466 sak->key, body->sak + sak_pos)) {
1467 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1471 ieee802_1x_mka_dump_dist_sak_body(body);
1478 * ieee802_1x_kay_init_data_key -
1480 static struct data_key *
1481 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1483 struct data_key *pkey;
1488 pkey = os_zalloc(sizeof(*pkey));
1490 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1494 pkey->key = os_zalloc(conf->key_len);
1495 if (pkey->key == NULL) {
1496 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1501 os_memcpy(pkey->key, conf->key, conf->key_len);
1502 os_memcpy(&pkey->key_identifier, &conf->ki,
1503 sizeof(pkey->key_identifier));
1504 pkey->confidentiality_offset = conf->offset;
1505 pkey->an = conf->an;
1506 pkey->transmits = conf->tx;
1507 pkey->receives = conf->rx;
1508 os_get_time(&pkey->created_time);
1517 * ieee802_1x_kay_decode_dist_sak_body -
1520 ieee802_1x_mka_decode_dist_sak_body(
1521 struct ieee802_1x_mka_participant *participant,
1522 const u8 *mka_msg, size_t msg_len)
1524 struct ieee802_1x_mka_hdr *hdr;
1525 struct ieee802_1x_mka_dist_sak_body *body;
1526 struct ieee802_1x_kay_peer *peer;
1527 struct macsec_ciphersuite *cs;
1529 struct key_conf *conf;
1530 struct data_key *sa_key = NULL;
1531 struct ieee802_1x_mka_ki sak_ki;
1536 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1537 body_len = get_mka_param_body_len(hdr);
1538 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1539 wpa_printf(MSG_ERROR,
1540 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 0, 28, 36, or more octets",
1545 if (!participant->principal) {
1546 wpa_printf(MSG_ERROR,
1547 "KaY: I can't accept the distributed SAK as I am not principal");
1550 if (participant->is_key_server) {
1551 wpa_printf(MSG_ERROR,
1552 "KaY: I can't accept the distributed SAK as myself is key server ");
1555 if (!participant->kay->macsec_desired ||
1556 participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1557 wpa_printf(MSG_ERROR,
1558 "KaY: I am not MACsec-desired or without MACsec capable");
1562 peer = ieee802_1x_kay_get_live_peer(participant,
1563 participant->current_peer_id.mi);
1565 wpa_printf(MSG_ERROR,
1566 "KaY: The key server is not in my live peers list");
1569 if (os_memcmp(&participant->kay->key_server_sci,
1570 &peer->sci, sizeof(struct ieee802_1x_mka_sci)) != 0) {
1571 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1574 if (body_len == 0) {
1575 participant->kay->authenticated = TRUE;
1576 participant->kay->secured = FALSE;
1577 participant->kay->failed = FALSE;
1578 participant->advised_desired = FALSE;
1579 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1580 ieee802_1x_cp_sm_step(participant->kay->cp);
1581 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1582 participant->to_use_sak = TRUE;
1585 participant->advised_desired = TRUE;
1586 participant->kay->authenticated = FALSE;
1587 participant->kay->secured = TRUE;
1588 participant->kay->failed = FALSE;
1589 ieee802_1x_cp_connect_secure(participant->kay->cp);
1590 ieee802_1x_cp_sm_step(participant->kay->cp);
1592 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1593 ieee802_1x_mka_dump_dist_sak_body(body);
1594 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1596 if (os_memcmp(sa_key->key_identifier.mi,
1597 participant->current_peer_id.mi, MI_LEN) == 0 &&
1598 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1599 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1603 if (body_len == 28) {
1604 sak_len = DEFAULT_SA_KEY_LEN;
1605 wrap_sak = body->sak;
1606 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1608 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1610 wpa_printf(MSG_ERROR,
1611 "KaY: I can't support the Cipher Suite advised by key server");
1614 sak_len = cs->sak_len;
1615 wrap_sak = body->sak + CS_ID_LEN;
1616 participant->kay->macsec_csindex = cs->index;
1619 unwrap_sak = os_zalloc(sak_len);
1621 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1624 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1626 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1627 os_free(unwrap_sak);
1630 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1632 conf = os_zalloc(sizeof(*conf));
1634 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1635 os_free(unwrap_sak);
1638 conf->key_len = sak_len;
1640 conf->key = os_zalloc(conf->key_len);
1642 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1643 os_free(unwrap_sak);
1648 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1650 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1652 sak_ki.kn = be_to_host32(body->kn);
1654 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1655 conf->ki.kn = sak_ki.kn;
1656 conf->an = body->dan;
1657 conf->offset = body->confid_offset;
1661 sa_key = ieee802_1x_kay_init_data_key(conf);
1663 os_free(unwrap_sak);
1669 dl_list_add(&participant->sak_list, &sa_key->list);
1671 ieee802_1x_cp_set_ciphersuite(
1672 participant->kay->cp,
1673 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1674 ieee802_1x_cp_sm_step(participant->kay->cp);
1675 ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1676 ieee802_1x_cp_sm_step(participant->kay->cp);
1677 ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1678 ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1679 ieee802_1x_cp_signal_newsak(participant->kay->cp);
1680 ieee802_1x_cp_sm_step(participant->kay->cp);
1682 participant->to_use_sak = TRUE;
1684 os_free(unwrap_sak);
1693 * ieee802_1x_mka_icv_body_present
1696 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1703 * ieee802_1x_kay_get_icv_length
1706 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1710 length = sizeof(struct ieee802_1x_mka_icv_body);
1711 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1713 return (length + 0x3) & ~0x3;
1718 * ieee802_1x_mka_encode_icv_body -
1721 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1724 struct ieee802_1x_mka_icv_body *body;
1725 unsigned int length;
1726 u8 cmac[MAX_ICV_LEN];
1728 length = ieee802_1x_mka_get_icv_length(participant);
1729 if (length != DEFAULT_ICV_LEN) {
1730 body = wpabuf_put(buf, MKA_HDR_LEN);
1731 body->type = MKA_ICV_INDICATOR;
1732 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1735 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1736 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1737 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1741 if (length != DEFAULT_ICV_LEN) {
1742 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1743 length - MKA_HDR_LEN);
1745 os_memcpy(wpabuf_put(buf, length), cmac, length);
1752 * ieee802_1x_mka_decode_icv_body -
1755 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1756 const u8 *mka_msg, size_t msg_len)
1758 struct ieee802_1x_mka_hdr *hdr;
1759 struct ieee802_1x_mka_icv_body *body;
1767 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1768 hdr = (struct ieee802_1x_mka_hdr *) pos;
1769 body_len = get_mka_param_body_len(hdr);
1770 body_type = get_mka_param_body_type(hdr);
1772 if (left_len < (body_len + MKA_HDR_LEN))
1775 if (body_type != MKA_ICV_INDICATOR) {
1776 left_len -= MKA_HDR_LEN + body_len;
1777 pos += MKA_HDR_LEN + body_len;
1781 body = (struct ieee802_1x_mka_icv_body *)pos;
1783 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1790 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1795 * ieee802_1x_mka_decode_dist_cak_body-
1798 ieee802_1x_mka_decode_dist_cak_body(
1799 struct ieee802_1x_mka_participant *participant,
1800 const u8 *mka_msg, size_t msg_len)
1802 struct ieee802_1x_mka_hdr *hdr;
1805 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1806 body_len = get_mka_param_body_len(hdr);
1807 if (body_len < 28) {
1808 wpa_printf(MSG_ERROR,
1809 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 28 or more octets",
1819 * ieee802_1x_mka_decode_kmd_body -
1822 ieee802_1x_mka_decode_kmd_body(
1823 struct ieee802_1x_mka_participant *participant,
1824 const u8 *mka_msg, size_t msg_len)
1826 struct ieee802_1x_mka_hdr *hdr;
1829 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1830 body_len = get_mka_param_body_len(hdr);
1832 wpa_printf(MSG_ERROR,
1833 "KaY: MKA Use SAK Packet Body Length (%d bytes) should be 5 or more octets",
1843 * ieee802_1x_mka_decode_announce_body -
1845 static int ieee802_1x_mka_decode_announce_body(
1846 struct ieee802_1x_mka_participant *participant,
1847 const u8 *mka_msg, size_t msg_len)
1853 static struct mka_param_body_handler mak_body_handler[] = {
1854 /* basic parameter set */
1856 ieee802_1x_mka_encode_basic_body,
1858 ieee802_1x_mka_basic_body_length,
1859 ieee802_1x_mka_basic_body_present
1862 /* live peer list parameter set */
1864 ieee802_1x_mka_encode_live_peer_body,
1865 ieee802_1x_mka_decode_live_peer_body,
1866 ieee802_1x_mka_get_live_peer_length,
1867 ieee802_1x_mka_live_peer_body_present
1870 /* potential peer list parameter set */
1872 ieee802_1x_mka_encode_potential_peer_body,
1873 ieee802_1x_mka_decode_potential_peer_body,
1874 ieee802_1x_mka_get_potential_peer_length,
1875 ieee802_1x_mka_potential_peer_body_present
1878 /* sak use parameter set */
1880 ieee802_1x_mka_encode_sak_use_body,
1881 ieee802_1x_mka_decode_sak_use_body,
1882 ieee802_1x_mka_get_sak_use_length,
1883 ieee802_1x_mka_sak_use_body_present
1886 /* distribute sak parameter set */
1888 ieee802_1x_mka_encode_dist_sak_body,
1889 ieee802_1x_mka_decode_dist_sak_body,
1890 ieee802_1x_mka_get_dist_sak_length,
1891 ieee802_1x_mka_dist_sak_body_present
1894 /* distribute cak parameter set */
1897 ieee802_1x_mka_decode_dist_cak_body,
1902 /* kmd parameter set */
1905 ieee802_1x_mka_decode_kmd_body,
1910 /* announce parameter set */
1913 ieee802_1x_mka_decode_announce_body,
1918 /* icv parameter set */
1920 ieee802_1x_mka_encode_icv_body,
1922 ieee802_1x_mka_get_icv_length,
1923 ieee802_1x_mka_icv_body_present
1929 * ieee802_1x_kay_deinit_data_key -
1931 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1940 dl_list_del(&pkey->list);
1947 * ieee802_1x_kay_generate_new_sak -
1950 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1952 struct data_key *sa_key = NULL;
1953 struct key_conf *conf;
1954 struct ieee802_1x_kay_peer *peer;
1955 struct ieee802_1x_kay *kay = participant->kay;
1956 int ctx_len, ctx_offset;
1959 /* check condition for generating a fresh SAK:
1960 * must have one live peer
1961 * and MKA life time elapse since last distribution
1962 * or potential peer is empty
1964 if (dl_list_empty(&participant->live_peers)) {
1965 wpa_printf(MSG_ERROR,
1966 "KaY: Live peers list must not empty when generating fresh SAK");
1970 /* FIXME: A fresh SAK not generated until
1971 * the live peer list contains at least one peer and
1972 * MKA life time has elapsed since the prior SAK was first distributed,
1973 * or the Key server's potential peer is empty
1974 * but I can't understand the second item, so
1975 * here only check first item and ingore
1976 * && (!dl_list_empty(&participant->potential_peers))) {
1978 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1979 wpa_printf(MSG_ERROR,
1980 "KaY: Life time have not elapsed since prior SAK distributed");
1984 conf = os_zalloc(sizeof(*conf));
1986 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1989 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1991 conf->key = os_zalloc(conf->key_len);
1994 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1998 ctx_len = conf->key_len + sizeof(kay->dist_kn);
1999 dl_list_for_each(peer, &participant->live_peers,
2000 struct ieee802_1x_kay_peer, list)
2001 ctx_len += sizeof(peer->mi);
2002 ctx_len += sizeof(participant->mi);
2004 context = os_zalloc(ctx_len);
2011 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2017 ctx_offset += conf->key_len;
2018 dl_list_for_each(peer, &participant->live_peers,
2019 struct ieee802_1x_kay_peer, list) {
2020 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2021 ctx_offset += sizeof(peer->mi);
2023 os_memcpy(context + ctx_offset, participant->mi,
2024 sizeof(participant->mi));
2025 ctx_offset += sizeof(participant->mi);
2026 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2028 if (conf->key_len == 16) {
2029 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2030 context, ctx_len, conf->key);
2031 } else if (conf->key_len == 32) {
2032 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2033 context, ctx_len, conf->key);
2035 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2041 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2042 conf->key, conf->key_len);
2044 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2045 conf->ki.kn = participant->kay->dist_kn;
2046 conf->an = participant->kay->dist_an;
2047 conf->offset = kay->macsec_confidentiality;
2051 sa_key = ieee802_1x_kay_init_data_key(conf);
2058 participant->new_key = sa_key;
2060 dl_list_add(&participant->sak_list, &sa_key->list);
2061 ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2062 cipher_suite_tbl[kay->macsec_csindex].id);
2063 ieee802_1x_cp_sm_step(kay->cp);
2064 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2065 ieee802_1x_cp_sm_step(kay->cp);
2066 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2067 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2068 ieee802_1x_cp_signal_newsak(kay->cp);
2069 ieee802_1x_cp_sm_step(kay->cp);
2071 dl_list_for_each(peer, &participant->live_peers,
2072 struct ieee802_1x_kay_peer, list)
2073 peer->sak_used = FALSE;
2075 participant->kay->dist_kn++;
2076 participant->kay->dist_an++;
2077 if (participant->kay->dist_an > 3)
2078 participant->kay->dist_an = 0;
2080 participant->kay->dist_time = time(NULL);
2090 * ieee802_1x_kay_elect_key_server - elect the key server
2091 * when to elect: whenever the live peers list changes
2094 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2096 struct ieee802_1x_kay_peer *peer;
2097 struct ieee802_1x_kay_peer *key_server = NULL;
2098 struct ieee802_1x_kay *kay = participant->kay;
2099 Boolean i_is_key_server;
2102 if (participant->is_obliged_key_server) {
2103 participant->new_sak = TRUE;
2104 participant->to_dist_sak = FALSE;
2105 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2109 /* elect the key server among the peers */
2110 dl_list_for_each(peer, &participant->live_peers,
2111 struct ieee802_1x_kay_peer, list) {
2112 if (!peer->is_key_server)
2120 if (peer->key_server_priority <
2121 key_server->key_server_priority) {
2123 } else if (peer->key_server_priority ==
2124 key_server->key_server_priority) {
2125 for (i = 0; i < 6; i++) {
2126 if (peer->sci.addr[i] <
2127 key_server->sci.addr[i])
2133 /* elect the key server between me and the above elected peer */
2134 i_is_key_server = FALSE;
2135 if (key_server && participant->can_be_key_server) {
2136 if (kay->actor_priority
2137 < key_server->key_server_priority) {
2138 i_is_key_server = TRUE;
2139 } else if (kay->actor_priority
2140 == key_server->key_server_priority) {
2141 for (i = 0; i < 6; i++) {
2142 if (kay->actor_sci.addr[i]
2143 < key_server->sci.addr[i]) {
2144 i_is_key_server = TRUE;
2150 if (!key_server && !i_is_key_server) {
2151 participant->principal = FALSE;
2152 participant->is_key_server = FALSE;
2153 participant->is_elected = FALSE;
2157 if (i_is_key_server) {
2158 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2159 if (os_memcmp(&kay->key_server_sci, &kay->actor_sci,
2160 sizeof(kay->key_server_sci))) {
2161 ieee802_1x_cp_signal_chgdserver(kay->cp);
2162 ieee802_1x_cp_sm_step(kay->cp);
2165 participant->is_key_server = TRUE;
2166 participant->principal = TRUE;
2167 participant->new_sak = TRUE;
2168 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2169 participant->to_dist_sak = FALSE;
2170 participant->is_elected = TRUE;
2172 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2173 sizeof(kay->key_server_sci));
2174 kay->key_server_priority = kay->actor_priority;
2178 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2179 if (os_memcmp(&kay->key_server_sci, &key_server->sci,
2180 sizeof(kay->key_server_sci))) {
2181 ieee802_1x_cp_signal_chgdserver(kay->cp);
2182 ieee802_1x_cp_sm_step(kay->cp);
2185 participant->is_key_server = FALSE;
2186 participant->principal = TRUE;
2187 participant->is_elected = TRUE;
2189 os_memcpy(&kay->key_server_sci, &key_server->sci,
2190 sizeof(kay->key_server_sci));
2191 kay->key_server_priority = key_server->key_server_priority;
2199 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2200 * how to use MACsec: whether use MACsec and its capability
2201 * protectFrames will be advised if the key server and one of its live peers are
2202 * MACsec capable and one of those request MACsec protection
2205 ieee802_1x_kay_decide_macsec_use(
2206 struct ieee802_1x_mka_participant *participant)
2208 struct ieee802_1x_kay *kay = participant->kay;
2209 struct ieee802_1x_kay_peer *peer;
2210 enum macsec_cap less_capability;
2213 if (!participant->is_key_server)
2216 /* key server self is MACsec-desired and requesting MACsec */
2217 if (!kay->macsec_desired) {
2218 participant->advised_desired = FALSE;
2221 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2222 participant->advised_desired = FALSE;
2225 less_capability = kay->macsec_capable;
2227 /* at least one of peers is MACsec-desired and requesting MACsec */
2229 dl_list_for_each(peer, &participant->live_peers,
2230 struct ieee802_1x_kay_peer, list) {
2231 if (!peer->macsec_desired)
2234 if (peer->macsec_capbility == MACSEC_CAP_NOT_IMPLEMENTED)
2237 less_capability = (less_capability < peer->macsec_capbility) ?
2238 less_capability : peer->macsec_capbility;
2243 participant->advised_desired = TRUE;
2244 participant->advised_capability = less_capability;
2245 kay->authenticated = FALSE;
2246 kay->secured = TRUE;
2247 kay->failed = FALSE;
2248 ieee802_1x_cp_connect_secure(kay->cp);
2249 ieee802_1x_cp_sm_step(kay->cp);
2251 participant->advised_desired = FALSE;
2252 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2253 participant->to_use_sak = FALSE;
2254 kay->authenticated = TRUE;
2255 kay->secured = FALSE;
2256 kay->failed = FALSE;
2265 ieee802_1x_cp_connect_authenticated(kay->cp);
2266 ieee802_1x_cp_sm_step(kay->cp);
2272 static const u8 pae_group_addr[ETH_ALEN] = {
2273 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2278 * ieee802_1x_kay_encode_mkpdu -
2281 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2282 struct wpabuf *pbuf)
2285 struct ieee8023_hdr *ether_hdr;
2286 struct ieee802_1x_hdr *eapol_hdr;
2288 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2289 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2290 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2291 sizeof(ether_hdr->dest));
2292 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2294 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2295 eapol_hdr->version = EAPOL_VERSION;
2296 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2297 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2299 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2300 if (mak_body_handler[i].body_present &&
2301 mak_body_handler[i].body_present(participant)) {
2302 if (mak_body_handler[i].body_tx(participant, pbuf))
2311 * ieee802_1x_participant_send_mkpdu -
2314 ieee802_1x_participant_send_mkpdu(
2315 struct ieee802_1x_mka_participant *participant)
2318 struct ieee802_1x_kay *kay = participant->kay;
2322 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2323 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2324 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2325 if (mak_body_handler[i].body_present &&
2326 mak_body_handler[i].body_present(participant))
2327 length += mak_body_handler[i].body_length(participant);
2330 buf = wpabuf_alloc(length);
2332 wpa_printf(MSG_ERROR, "KaY: out of memory");
2336 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2337 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2341 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2345 participant->active = TRUE;
2351 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2353 * ieee802_1x_participant_timer -
2355 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2357 struct ieee802_1x_mka_participant *participant;
2358 struct ieee802_1x_kay *kay;
2359 struct ieee802_1x_kay_peer *peer, *pre_peer;
2360 time_t now = time(NULL);
2362 struct receive_sc *rxsc, *pre_rxsc;
2363 struct transmit_sa *txsa, *pre_txsa;
2365 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2366 kay = participant->kay;
2367 if (participant->cak_life) {
2368 if (now > participant->cak_life) {
2369 kay->authenticated = FALSE;
2370 kay->secured = FALSE;
2372 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2377 /* should delete MKA instance if there are not live peers
2378 * when the MKA life elapsed since its creating */
2379 if (participant->mka_life) {
2380 if (dl_list_empty(&participant->live_peers)) {
2381 if (now > participant->mka_life) {
2382 kay->authenticated = FALSE;
2383 kay->secured = FALSE;
2385 ieee802_1x_kay_delete_mka(kay,
2390 participant->mka_life = 0;
2395 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2396 struct ieee802_1x_kay_peer, list) {
2397 if (now > peer->expire) {
2398 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2399 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2401 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2402 dl_list_for_each_safe(rxsc, pre_rxsc,
2403 &participant->rxsc_list,
2404 struct receive_sc, list) {
2405 if (os_memcmp(&rxsc->sci, &peer->sci,
2406 sizeof(rxsc->sci)) == 0) {
2407 secy_delete_receive_sc(kay, rxsc);
2408 ieee802_1x_kay_deinit_receive_sc(
2412 dl_list_del(&peer->list);
2419 if (dl_list_empty(&participant->live_peers)) {
2420 participant->advised_desired = FALSE;
2421 participant->advised_capability =
2422 MACSEC_CAP_NOT_IMPLEMENTED;
2423 participant->to_use_sak = FALSE;
2424 kay->authenticated = TRUE;
2425 kay->secured = FALSE;
2426 kay->failed = FALSE;
2435 dl_list_for_each_safe(txsa, pre_txsa,
2436 &participant->txsc->sa_list,
2437 struct transmit_sa, list) {
2438 secy_disable_transmit_sa(kay, txsa);
2439 ieee802_1x_kay_deinit_transmit_sa(txsa);
2442 ieee802_1x_cp_connect_authenticated(kay->cp);
2443 ieee802_1x_cp_sm_step(kay->cp);
2445 ieee802_1x_kay_elect_key_server(participant);
2446 ieee802_1x_kay_decide_macsec_use(participant);
2450 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2451 struct ieee802_1x_kay_peer, list) {
2452 if (now > peer->expire) {
2453 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2454 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2456 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2457 dl_list_del(&peer->list);
2462 if (participant->new_sak) {
2463 if (!ieee802_1x_kay_generate_new_sak(participant))
2464 participant->to_dist_sak = TRUE;
2466 participant->new_sak = FALSE;
2469 if (participant->retry_count < MAX_RETRY_CNT) {
2470 ieee802_1x_participant_send_mkpdu(participant);
2471 participant->retry_count++;
2474 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2475 ieee802_1x_participant_timer,
2481 * ieee802_1x_kay_init_transmit_sa -
2483 static struct transmit_sa *
2484 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2485 struct data_key *key)
2487 struct transmit_sa *psa;
2489 key->tx_latest = TRUE;
2490 key->rx_latest = TRUE;
2492 psa = os_zalloc(sizeof(*psa));
2494 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2498 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2499 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2500 psa->confidentiality = TRUE;
2502 psa->confidentiality = FALSE;
2506 psa->next_pn = next_PN;
2509 os_get_time(&psa->created_time);
2510 psa->in_use = FALSE;
2512 dl_list_add(&psc->sa_list, &psa->list);
2513 wpa_printf(MSG_DEBUG,
2514 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2515 (int) an, next_PN, psc->channel);
2522 * ieee802_1x_kay_deinit_transmit_sa -
2524 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2527 wpa_printf(MSG_DEBUG,
2528 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2529 psa->an, psa->sc->channel);
2530 dl_list_del(&psa->list);
2536 * init_transmit_sc -
2538 static struct transmit_sc *
2539 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2542 struct transmit_sc *psc;
2544 psc = os_zalloc(sizeof(*psc));
2546 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2549 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2550 psc->channel = channel;
2552 os_get_time(&psc->created_time);
2553 psc->transmitting = FALSE;
2554 psc->encoding_sa = FALSE;
2555 psc->enciphering_sa = FALSE;
2557 dl_list_init(&psc->sa_list);
2558 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2559 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2566 * ieee802_1x_kay_deinit_transmit_sc -
2569 ieee802_1x_kay_deinit_transmit_sc(
2570 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2572 struct transmit_sa *psa, *tmp;
2574 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2576 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2578 secy_disable_transmit_sa(participant->kay, psa);
2579 ieee802_1x_kay_deinit_transmit_sa(psa);
2586 /****************** Interface between CP and KAY *********************/
2588 * ieee802_1x_kay_set_latest_sa_attr -
2590 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2591 struct ieee802_1x_mka_ki *lki, u8 lan,
2592 Boolean ltx, Boolean lrx)
2594 struct ieee802_1x_mka_participant *principal;
2596 principal = ieee802_1x_kay_get_principal_participant(kay);
2601 os_memset(&principal->lki, 0, sizeof(principal->lki));
2603 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2605 principal->lan = lan;
2606 principal->ltx = ltx;
2607 principal->lrx = lrx;
2612 kay->ltx_kn = lki->kn;
2613 kay->lrx_kn = lki->kn;
2623 * ieee802_1x_kay_set_old_sa_attr -
2625 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2626 struct ieee802_1x_mka_ki *oki,
2627 u8 oan, Boolean otx, Boolean orx)
2629 struct ieee802_1x_mka_participant *principal;
2631 principal = ieee802_1x_kay_get_principal_participant(kay);
2636 os_memset(&principal->oki, 0, sizeof(principal->oki));
2638 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2640 principal->oan = oan;
2641 principal->otx = otx;
2642 principal->orx = orx;
2648 kay->otx_kn = oki->kn;
2649 kay->orx_kn = oki->kn;
2659 * ieee802_1x_kay_create_sas -
2661 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2662 struct ieee802_1x_mka_ki *lki)
2664 struct data_key *sa_key, *latest_sak;
2665 struct ieee802_1x_mka_participant *principal;
2666 struct receive_sc *rxsc;
2667 struct receive_sa *rxsa;
2668 struct transmit_sa *txsa;
2670 principal = ieee802_1x_kay_get_principal_participant(kay);
2675 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2676 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2677 sa_key->rx_latest = TRUE;
2678 sa_key->tx_latest = TRUE;
2679 latest_sak = sa_key;
2680 principal->to_use_sak = TRUE;
2682 sa_key->rx_latest = FALSE;
2683 sa_key->tx_latest = FALSE;
2687 wpa_printf(MSG_ERROR, "lki related sak not found");
2691 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2692 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2697 secy_create_receive_sa(kay, rxsa);
2700 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2705 secy_create_transmit_sa(kay, txsa);
2714 * ieee802_1x_kay_delete_sas -
2716 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2717 struct ieee802_1x_mka_ki *ki)
2719 struct data_key *sa_key, *pre_key;
2720 struct transmit_sa *txsa, *pre_txsa;
2721 struct receive_sa *rxsa, *pre_rxsa;
2722 struct receive_sc *rxsc;
2723 struct ieee802_1x_mka_participant *principal;
2725 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2726 principal = ieee802_1x_kay_get_principal_participant(kay);
2730 /* remove the transmit sa */
2731 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2732 struct transmit_sa, list) {
2733 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2734 secy_disable_transmit_sa(kay, txsa);
2735 ieee802_1x_kay_deinit_transmit_sa(txsa);
2739 /* remove the receive sa */
2740 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2741 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2742 struct receive_sa, list) {
2743 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2744 secy_disable_receive_sa(kay, rxsa);
2745 ieee802_1x_kay_deinit_receive_sa(rxsa);
2750 /* remove the sak */
2751 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2752 struct data_key, list) {
2753 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2754 ieee802_1x_kay_deinit_data_key(sa_key);
2757 if (principal->new_key == sa_key)
2758 principal->new_key = NULL;
2766 * ieee802_1x_kay_enable_tx_sas -
2768 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2769 struct ieee802_1x_mka_ki *lki)
2771 struct ieee802_1x_mka_participant *principal;
2772 struct transmit_sa *txsa;
2774 principal = ieee802_1x_kay_get_principal_participant(kay);
2778 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2780 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2781 txsa->in_use = TRUE;
2782 secy_enable_transmit_sa(kay, txsa);
2783 ieee802_1x_cp_set_usingtransmitas(
2784 principal->kay->cp, TRUE);
2785 ieee802_1x_cp_sm_step(principal->kay->cp);
2794 * ieee802_1x_kay_enable_rx_sas -
2796 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2797 struct ieee802_1x_mka_ki *lki)
2799 struct ieee802_1x_mka_participant *principal;
2800 struct receive_sa *rxsa;
2801 struct receive_sc *rxsc;
2803 principal = ieee802_1x_kay_get_principal_participant(kay);
2807 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2808 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2810 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2811 rxsa->in_use = TRUE;
2812 secy_enable_receive_sa(kay, rxsa);
2813 ieee802_1x_cp_set_usingreceivesas(
2814 principal->kay->cp, TRUE);
2815 ieee802_1x_cp_sm_step(principal->kay->cp);
2825 * ieee802_1x_kay_enable_new_info -
2827 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2829 struct ieee802_1x_mka_participant *principal;
2831 principal = ieee802_1x_kay_get_principal_participant(kay);
2835 if (principal->retry_count < MAX_RETRY_CNT) {
2836 ieee802_1x_participant_send_mkpdu(principal);
2837 principal->retry_count++;
2845 * ieee802_1x_kay_cp_conf -
2847 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2848 struct ieee802_1x_cp_conf *pconf)
2850 pconf->protect = kay->macsec_protect;
2851 pconf->replay_protect = kay->macsec_replay_protect;
2852 pconf->validate = kay->macsec_validate;
2859 * ieee802_1x_kay_alloc_cp_sm -
2861 static struct ieee802_1x_cp_sm *
2862 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2864 struct ieee802_1x_cp_conf conf;
2866 os_memset(&conf, 0, sizeof(conf));
2867 conf.protect = kay->macsec_protect;
2868 conf.replay_protect = kay->macsec_replay_protect;
2869 conf.validate = kay->macsec_validate;
2870 conf.replay_window = kay->macsec_replay_window;
2872 return ieee802_1x_cp_sm_init(kay, &conf);
2877 * ieee802_1x_kay_mkpdu_sanity_check -
2878 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2880 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2881 const u8 *buf, size_t len)
2883 struct ieee8023_hdr *eth_hdr;
2884 struct ieee802_1x_hdr *eapol_hdr;
2885 struct ieee802_1x_mka_hdr *mka_hdr;
2886 struct ieee802_1x_mka_basic_body *body;
2888 struct ieee802_1x_mka_participant *participant;
2890 u8 icv[MAX_ICV_LEN];
2893 eth_hdr = (struct ieee8023_hdr *) buf;
2894 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2895 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2897 /* destination address should be not individual address */
2898 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2899 wpa_printf(MSG_MSGDUMP,
2900 "KaY: ethernet destination address is not PAE group address");
2904 /* MKPDU should not less than 32 octets */
2905 mka_msg_len = be_to_host16(eapol_hdr->length);
2906 if (mka_msg_len < 32) {
2907 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2910 /* MKPDU should multiple 4 octets */
2911 if ((mka_msg_len % 4) != 0) {
2912 wpa_printf(MSG_MSGDUMP,
2913 "KaY: MKPDU is not multiple of 4 octets");
2917 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2918 ieee802_1x_mka_dump_basic_body(body);
2919 body_len = get_mka_param_body_len(body);
2920 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2921 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2922 wpa_printf(MSG_ERROR,
2923 "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",
2924 (int) mka_msg_len, (int) MKA_HDR_LEN,
2925 (int) body_len, DEFAULT_ICV_LEN);
2929 /* CKN should be owned by I */
2930 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2932 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2936 /* algorithm agility check */
2937 if (os_memcmp(body->algo_agility, mka_algo_agility,
2938 sizeof(body->algo_agility)) != 0) {
2939 wpa_printf(MSG_ERROR,
2940 "KaY: peer's algorithm agility not supported for me");
2946 * The ICV will comprise the final octets of the packet body, whatever
2947 * its size, not the fixed length 16 octets, indicated by the EAPOL
2948 * packet body length.
2950 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2951 participant->ick.key,
2952 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2953 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2956 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2960 if (os_memcmp_const(msg_icv, icv,
2961 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2963 wpa_printf(MSG_ERROR,
2964 "KaY: Computed ICV is not equal to Received ICV");
2968 wpa_printf(MSG_ERROR, "KaY: No ICV");
2977 * ieee802_1x_kay_decode_mkpdu -
2979 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2980 const u8 *buf, size_t len)
2982 struct ieee802_1x_mka_participant *participant;
2983 struct ieee802_1x_mka_hdr *hdr;
2989 Boolean my_included;
2990 Boolean handled[256];
2992 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2995 /* handle basic parameter set */
2996 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2997 left_len = len - sizeof(struct ieee8023_hdr) -
2998 sizeof(struct ieee802_1x_hdr);
2999 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3003 /* to skip basic parameter set */
3004 hdr = (struct ieee802_1x_mka_hdr *) pos;
3005 body_len = get_mka_param_body_len(hdr);
3006 pos += body_len + MKA_HDR_LEN;
3007 left_len -= body_len + MKA_HDR_LEN;
3009 /* check i am in the peer's peer list */
3010 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3012 /* accept the peer as live peer */
3013 if (!ieee802_1x_kay_is_in_peer(
3015 participant->current_peer_id.mi)) {
3016 if (!ieee802_1x_kay_create_live_peer(
3018 participant->current_peer_id.mi,
3019 participant->current_peer_id.mn))
3021 ieee802_1x_kay_elect_key_server(participant);
3022 ieee802_1x_kay_decide_macsec_use(participant);
3024 if (ieee802_1x_kay_is_in_potential_peer(
3025 participant, participant->current_peer_id.mi)) {
3026 ieee802_1x_kay_move_live_peer(
3027 participant, participant->current_peer_id.mi,
3028 participant->current_peer_id.mn);
3029 ieee802_1x_kay_elect_key_server(participant);
3030 ieee802_1x_kay_decide_macsec_use(participant);
3035 * Handle other parameter set than basic parameter set.
3036 * Each parameter set should be present only once.
3038 for (i = 0; i < 256; i++)
3042 while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3043 hdr = (struct ieee802_1x_mka_hdr *) pos;
3044 body_len = get_mka_param_body_len(hdr);
3045 body_type = get_mka_param_body_type(hdr);
3047 if (body_type == MKA_ICV_INDICATOR)
3050 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3051 wpa_printf(MSG_ERROR,
3052 "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",
3053 (int) left_len, (int) MKA_HDR_LEN,
3054 (int) body_len, DEFAULT_ICV_LEN);
3058 if (handled[body_type])
3061 handled[body_type] = TRUE;
3062 if (mak_body_handler[body_type].body_rx) {
3063 mak_body_handler[body_type].body_rx
3064 (participant, pos, left_len);
3066 wpa_printf(MSG_ERROR,
3067 "The type %d not supported in this MKA version %d",
3068 body_type, MKA_VERSION_ID);
3072 pos += body_len + MKA_HDR_LEN;
3073 left_len -= body_len + MKA_HDR_LEN;
3077 participant->retry_count = 0;
3078 participant->active = TRUE;
3085 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3088 struct ieee802_1x_kay *kay = ctx;
3089 struct ieee8023_hdr *eth_hdr;
3090 struct ieee802_1x_hdr *eapol_hdr;
3092 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3093 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3094 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3095 (unsigned long) len);
3099 eth_hdr = (struct ieee8023_hdr *) buf;
3100 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3101 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3102 be_to_host16(eapol_hdr->length)) {
3103 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3104 (unsigned long) len,
3105 (unsigned long) be_to_host16(eapol_hdr->length));
3109 if (eapol_hdr->version < EAPOL_VERSION) {
3110 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3111 eapol_hdr->version);
3114 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3115 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3118 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3119 if (dl_list_empty(&kay->participant_list)) {
3120 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3124 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3129 * ieee802_1x_kay_init -
3131 struct ieee802_1x_kay *
3132 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3133 const char *ifname, const u8 *addr)
3135 struct ieee802_1x_kay *kay;
3137 kay = os_zalloc(sizeof(*kay));
3139 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3146 kay->active = FALSE;
3148 kay->authenticated = FALSE;
3149 kay->secured = FALSE;
3150 kay->failed = FALSE;
3151 kay->policy = policy;
3153 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3154 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3155 kay->actor_sci.port = host_to_be16(0x0001);
3156 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3158 /* While actor acts as a key server, shall distribute sakey */
3163 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3164 kay->macsec_csindex = DEFAULT_CS_INDEX;
3165 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3166 kay->mka_version = MKA_VERSION_ID;
3168 os_memcpy(kay->algo_agility, mka_algo_agility,
3169 sizeof(kay->algo_agility));
3171 dl_list_init(&kay->participant_list);
3173 if (policy == DO_NOT_SECURE) {
3174 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3175 kay->macsec_desired = FALSE;
3176 kay->macsec_protect = FALSE;
3177 kay->macsec_validate = Disabled;
3178 kay->macsec_replay_protect = FALSE;
3179 kay->macsec_replay_window = 0;
3180 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3182 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3183 kay->macsec_desired = TRUE;
3184 kay->macsec_protect = TRUE;
3185 kay->macsec_validate = Strict;
3186 kay->macsec_replay_protect = FALSE;
3187 kay->macsec_replay_window = 0;
3188 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3191 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3193 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3194 secy_init_macsec(kay);
3195 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3197 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3200 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3201 if (kay->cp == NULL) {
3202 ieee802_1x_kay_deinit(kay);
3206 if (policy == DO_NOT_SECURE) {
3207 ieee802_1x_cp_connect_authenticated(kay->cp);
3208 ieee802_1x_cp_sm_step(kay->cp);
3210 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3211 kay_l2_receive, kay, 1);
3212 if (kay->l2_mka == NULL) {
3213 wpa_printf(MSG_WARNING,
3214 "KaY: Failed to initialize L2 packet processing for MKA packet");
3215 ieee802_1x_kay_deinit(kay);
3225 * ieee802_1x_kay_deinit -
3228 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3230 struct ieee802_1x_mka_participant *participant;
3235 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3237 while (!dl_list_empty(&kay->participant_list)) {
3238 participant = dl_list_entry(kay->participant_list.next,
3239 struct ieee802_1x_mka_participant,
3241 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3244 ieee802_1x_cp_sm_deinit(kay->cp);
3245 secy_deinit_macsec(kay);
3248 l2_packet_deinit(kay->l2_mka);
3258 * ieee802_1x_kay_create_mka -
3260 struct ieee802_1x_mka_participant *
3261 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3262 struct mka_key *cak, u32 life,
3263 enum mka_created_mode mode, Boolean is_authenticator)
3265 struct ieee802_1x_mka_participant *participant;
3268 if (!kay || !ckn || !cak) {
3269 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3273 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3274 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3277 if (ckn->len > MAX_CKN_LEN) {
3278 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3282 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3286 participant = os_zalloc(sizeof(*participant));
3288 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3292 participant->ckn.len = ckn->len;
3293 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3294 participant->cak.len = cak->len;
3295 os_memcpy(participant->cak.key, cak->key, cak->len);
3297 participant->cak_life = life + time(NULL);
3301 if (is_authenticator) {
3302 participant->is_obliged_key_server = TRUE;
3303 participant->can_be_key_server = TRUE;
3304 participant->is_key_server = TRUE;
3305 participant->principal = TRUE;
3307 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3308 sizeof(kay->key_server_sci));
3309 kay->key_server_priority = kay->actor_priority;
3310 participant->is_elected = TRUE;
3312 participant->is_obliged_key_server = FALSE;
3313 participant->can_be_key_server = FALSE;
3314 participant->is_key_server = FALSE;
3315 participant->is_elected = TRUE;
3320 participant->is_obliged_key_server = FALSE;
3321 participant->can_be_key_server = TRUE;
3322 participant->is_key_server = FALSE;
3323 participant->is_elected = FALSE;
3327 participant->cached = FALSE;
3329 participant->active = FALSE;
3330 participant->participant = FALSE;
3331 participant->retain = FALSE;
3332 participant->activate = DEFAULT;
3334 if (participant->is_key_server)
3335 participant->principal = TRUE;
3337 dl_list_init(&participant->live_peers);
3338 dl_list_init(&participant->potential_peers);
3340 participant->retry_count = 0;
3341 participant->kay = kay;
3343 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3345 participant->mn = 0;
3347 participant->lrx = FALSE;
3348 participant->ltx = FALSE;
3349 participant->orx = FALSE;
3350 participant->otx = FALSE;
3351 participant->to_dist_sak = FALSE;
3352 participant->to_use_sak = FALSE;
3353 participant->new_sak = FALSE;
3354 dl_list_init(&participant->sak_list);
3355 participant->new_key = NULL;
3356 dl_list_init(&participant->rxsc_list);
3357 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3359 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3360 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3361 kay->macsec_replay_window);
3362 secy_create_transmit_sc(kay, participant->txsc);
3364 /* to derive KEK from CAK and CKN */
3365 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3366 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3367 participant->ckn.name,
3368 participant->ckn.len,
3369 participant->kek.key)) {
3370 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3373 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3374 participant->kek.key, participant->kek.len);
3376 /* to derive ICK from CAK and CKN */
3377 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3378 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3379 participant->ckn.name,
3380 participant->ckn.len,
3381 participant->ick.key)) {
3382 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3385 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3386 participant->ick.key, participant->ick.len);
3388 dl_list_add(&kay->participant_list, &participant->list);
3389 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3390 ckn->name, ckn->len);
3392 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3393 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3395 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3401 os_free(participant);
3407 * ieee802_1x_kay_delete_mka -
3410 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3412 struct ieee802_1x_mka_participant *participant;
3413 struct ieee802_1x_kay_peer *peer;
3414 struct data_key *sak;
3415 struct receive_sc *rxsc;
3420 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3422 /* get the participant */
3423 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3425 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3426 ckn->name, ckn->len);
3430 dl_list_del(&participant->list);
3432 /* remove live peer */
3433 while (!dl_list_empty(&participant->live_peers)) {
3434 peer = dl_list_entry(participant->live_peers.next,
3435 struct ieee802_1x_kay_peer, list);
3436 dl_list_del(&peer->list);
3440 /* remove potential peer */
3441 while (!dl_list_empty(&participant->potential_peers)) {
3442 peer = dl_list_entry(participant->potential_peers.next,
3443 struct ieee802_1x_kay_peer, list);
3444 dl_list_del(&peer->list);
3449 while (!dl_list_empty(&participant->sak_list)) {
3450 sak = dl_list_entry(participant->sak_list.next,
3451 struct data_key, list);
3452 dl_list_del(&sak->list);
3456 while (!dl_list_empty(&participant->rxsc_list)) {
3457 rxsc = dl_list_entry(participant->rxsc_list.next,
3458 struct receive_sc, list);
3459 secy_delete_receive_sc(kay, rxsc);
3460 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3462 secy_delete_transmit_sc(kay, participant->txsc);
3463 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3465 os_memset(&participant->cak, 0, sizeof(participant->cak));
3466 os_memset(&participant->kek, 0, sizeof(participant->kek));
3467 os_memset(&participant->ick, 0, sizeof(participant->ick));
3468 os_free(participant);
3473 * ieee802_1x_kay_mka_participate -
3475 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3476 struct mka_key_name *ckn,
3479 struct ieee802_1x_mka_participant *participant;
3484 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3488 participant->active = status;
3493 * ieee802_1x_kay_new_sak -
3496 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3498 struct ieee802_1x_mka_participant *participant;
3503 participant = ieee802_1x_kay_get_principal_participant(kay);
3507 participant->new_sak = TRUE;
3508 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3515 * ieee802_1x_kay_change_cipher_suite -
3518 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3520 struct ieee802_1x_mka_participant *participant;
3525 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3526 wpa_printf(MSG_ERROR,
3527 "KaY: Configured cipher suite index is out of range");
3530 if (kay->macsec_csindex == cs_index)
3534 kay->macsec_desired = FALSE;
3536 kay->macsec_csindex = cs_index;
3537 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3539 participant = ieee802_1x_kay_get_principal_participant(kay);
3541 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3542 participant->new_sak = TRUE;