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 = 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;
2101 if (participant->is_obliged_key_server) {
2102 participant->new_sak = TRUE;
2103 participant->to_dist_sak = FALSE;
2104 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2108 /* elect the key server among the peers */
2109 dl_list_for_each(peer, &participant->live_peers,
2110 struct ieee802_1x_kay_peer, list) {
2111 if (!peer->is_key_server)
2119 if (peer->key_server_priority <
2120 key_server->key_server_priority) {
2122 } else if (peer->key_server_priority ==
2123 key_server->key_server_priority) {
2124 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2130 /* elect the key server between me and the above elected peer */
2131 i_is_key_server = FALSE;
2132 if (key_server && participant->can_be_key_server) {
2133 if (kay->actor_priority
2134 < key_server->key_server_priority) {
2135 i_is_key_server = TRUE;
2136 } else if (kay->actor_priority
2137 == key_server->key_server_priority) {
2138 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2140 i_is_key_server = TRUE;
2142 } else if (participant->can_be_key_server) {
2143 i_is_key_server = TRUE;
2146 if (i_is_key_server) {
2147 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2148 if (os_memcmp(&kay->key_server_sci, &kay->actor_sci,
2149 sizeof(kay->key_server_sci))) {
2150 ieee802_1x_cp_signal_chgdserver(kay->cp);
2151 ieee802_1x_cp_sm_step(kay->cp);
2154 participant->is_key_server = TRUE;
2155 participant->principal = TRUE;
2156 participant->new_sak = TRUE;
2157 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2158 participant->to_dist_sak = FALSE;
2159 participant->is_elected = TRUE;
2161 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2162 sizeof(kay->key_server_sci));
2163 kay->key_server_priority = kay->actor_priority;
2164 } else if (key_server) {
2165 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2166 if (os_memcmp(&kay->key_server_sci, &key_server->sci,
2167 sizeof(kay->key_server_sci))) {
2168 ieee802_1x_cp_signal_chgdserver(kay->cp);
2169 ieee802_1x_cp_sm_step(kay->cp);
2172 participant->is_key_server = FALSE;
2173 participant->principal = TRUE;
2174 participant->is_elected = TRUE;
2176 os_memcpy(&kay->key_server_sci, &key_server->sci,
2177 sizeof(kay->key_server_sci));
2178 kay->key_server_priority = key_server->key_server_priority;
2180 participant->principal = FALSE;
2181 participant->is_key_server = FALSE;
2182 participant->is_elected = FALSE;
2190 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2191 * how to use MACsec: whether use MACsec and its capability
2192 * protectFrames will be advised if the key server and one of its live peers are
2193 * MACsec capable and one of those request MACsec protection
2196 ieee802_1x_kay_decide_macsec_use(
2197 struct ieee802_1x_mka_participant *participant)
2199 struct ieee802_1x_kay *kay = participant->kay;
2200 struct ieee802_1x_kay_peer *peer;
2201 enum macsec_cap less_capability;
2204 if (!participant->is_key_server)
2207 /* key server self is MACsec-desired and requesting MACsec */
2208 if (!kay->macsec_desired) {
2209 participant->advised_desired = FALSE;
2212 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2213 participant->advised_desired = FALSE;
2216 less_capability = kay->macsec_capable;
2218 /* at least one of peers is MACsec-desired and requesting MACsec */
2220 dl_list_for_each(peer, &participant->live_peers,
2221 struct ieee802_1x_kay_peer, list) {
2222 if (!peer->macsec_desired)
2225 if (peer->macsec_capbility == MACSEC_CAP_NOT_IMPLEMENTED)
2228 less_capability = (less_capability < peer->macsec_capbility) ?
2229 less_capability : peer->macsec_capbility;
2234 participant->advised_desired = TRUE;
2235 participant->advised_capability = less_capability;
2236 kay->authenticated = FALSE;
2237 kay->secured = TRUE;
2238 kay->failed = FALSE;
2239 ieee802_1x_cp_connect_secure(kay->cp);
2240 ieee802_1x_cp_sm_step(kay->cp);
2242 participant->advised_desired = FALSE;
2243 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2244 participant->to_use_sak = FALSE;
2245 kay->authenticated = TRUE;
2246 kay->secured = FALSE;
2247 kay->failed = FALSE;
2256 ieee802_1x_cp_connect_authenticated(kay->cp);
2257 ieee802_1x_cp_sm_step(kay->cp);
2263 static const u8 pae_group_addr[ETH_ALEN] = {
2264 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2269 * ieee802_1x_kay_encode_mkpdu -
2272 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2273 struct wpabuf *pbuf)
2276 struct ieee8023_hdr *ether_hdr;
2277 struct ieee802_1x_hdr *eapol_hdr;
2279 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2280 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2281 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2282 sizeof(ether_hdr->dest));
2283 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2285 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2286 eapol_hdr->version = EAPOL_VERSION;
2287 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2288 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2290 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2291 if (mak_body_handler[i].body_present &&
2292 mak_body_handler[i].body_present(participant)) {
2293 if (mak_body_handler[i].body_tx(participant, pbuf))
2302 * ieee802_1x_participant_send_mkpdu -
2305 ieee802_1x_participant_send_mkpdu(
2306 struct ieee802_1x_mka_participant *participant)
2309 struct ieee802_1x_kay *kay = participant->kay;
2313 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2314 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2315 for (i = 0; i < ARRAY_SIZE(mak_body_handler); i++) {
2316 if (mak_body_handler[i].body_present &&
2317 mak_body_handler[i].body_present(participant))
2318 length += mak_body_handler[i].body_length(participant);
2321 buf = wpabuf_alloc(length);
2323 wpa_printf(MSG_ERROR, "KaY: out of memory");
2327 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2328 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2332 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2336 participant->active = TRUE;
2342 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2344 * ieee802_1x_participant_timer -
2346 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2348 struct ieee802_1x_mka_participant *participant;
2349 struct ieee802_1x_kay *kay;
2350 struct ieee802_1x_kay_peer *peer, *pre_peer;
2351 time_t now = time(NULL);
2353 struct receive_sc *rxsc, *pre_rxsc;
2354 struct transmit_sa *txsa, *pre_txsa;
2356 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2357 kay = participant->kay;
2358 if (participant->cak_life) {
2359 if (now > participant->cak_life) {
2360 kay->authenticated = FALSE;
2361 kay->secured = FALSE;
2363 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2368 /* should delete MKA instance if there are not live peers
2369 * when the MKA life elapsed since its creating */
2370 if (participant->mka_life) {
2371 if (dl_list_empty(&participant->live_peers)) {
2372 if (now > participant->mka_life) {
2373 kay->authenticated = FALSE;
2374 kay->secured = FALSE;
2376 ieee802_1x_kay_delete_mka(kay,
2381 participant->mka_life = 0;
2386 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2387 struct ieee802_1x_kay_peer, list) {
2388 if (now > peer->expire) {
2389 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2390 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2392 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2393 dl_list_for_each_safe(rxsc, pre_rxsc,
2394 &participant->rxsc_list,
2395 struct receive_sc, list) {
2396 if (os_memcmp(&rxsc->sci, &peer->sci,
2397 sizeof(rxsc->sci)) == 0) {
2398 secy_delete_receive_sc(kay, rxsc);
2399 ieee802_1x_kay_deinit_receive_sc(
2403 dl_list_del(&peer->list);
2410 if (dl_list_empty(&participant->live_peers)) {
2411 participant->advised_desired = FALSE;
2412 participant->advised_capability =
2413 MACSEC_CAP_NOT_IMPLEMENTED;
2414 participant->to_use_sak = FALSE;
2415 kay->authenticated = TRUE;
2416 kay->secured = FALSE;
2417 kay->failed = FALSE;
2426 dl_list_for_each_safe(txsa, pre_txsa,
2427 &participant->txsc->sa_list,
2428 struct transmit_sa, list) {
2429 secy_disable_transmit_sa(kay, txsa);
2430 ieee802_1x_kay_deinit_transmit_sa(txsa);
2433 ieee802_1x_cp_connect_authenticated(kay->cp);
2434 ieee802_1x_cp_sm_step(kay->cp);
2436 ieee802_1x_kay_elect_key_server(participant);
2437 ieee802_1x_kay_decide_macsec_use(participant);
2441 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2442 struct ieee802_1x_kay_peer, list) {
2443 if (now > peer->expire) {
2444 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2445 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2447 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2448 dl_list_del(&peer->list);
2453 if (participant->new_sak) {
2454 if (!ieee802_1x_kay_generate_new_sak(participant))
2455 participant->to_dist_sak = TRUE;
2457 participant->new_sak = FALSE;
2460 if (participant->retry_count < MAX_RETRY_CNT) {
2461 ieee802_1x_participant_send_mkpdu(participant);
2462 participant->retry_count++;
2465 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2466 ieee802_1x_participant_timer,
2472 * ieee802_1x_kay_init_transmit_sa -
2474 static struct transmit_sa *
2475 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2476 struct data_key *key)
2478 struct transmit_sa *psa;
2480 key->tx_latest = TRUE;
2481 key->rx_latest = TRUE;
2483 psa = os_zalloc(sizeof(*psa));
2485 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2489 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2490 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2491 psa->confidentiality = TRUE;
2493 psa->confidentiality = FALSE;
2497 psa->next_pn = next_PN;
2500 os_get_time(&psa->created_time);
2501 psa->in_use = FALSE;
2503 dl_list_add(&psc->sa_list, &psa->list);
2504 wpa_printf(MSG_DEBUG,
2505 "KaY: Create transmit SA(an: %d, next_PN: %u) of SC(channel: %d)",
2506 (int) an, next_PN, psc->channel);
2513 * ieee802_1x_kay_deinit_transmit_sa -
2515 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2518 wpa_printf(MSG_DEBUG,
2519 "KaY: Delete transmit SA(an: %d) of SC(channel: %d)",
2520 psa->an, psa->sc->channel);
2521 dl_list_del(&psa->list);
2527 * init_transmit_sc -
2529 static struct transmit_sc *
2530 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2533 struct transmit_sc *psc;
2535 psc = os_zalloc(sizeof(*psc));
2537 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2540 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2541 psc->channel = channel;
2543 os_get_time(&psc->created_time);
2544 psc->transmitting = FALSE;
2545 psc->encoding_sa = FALSE;
2546 psc->enciphering_sa = FALSE;
2548 dl_list_init(&psc->sa_list);
2549 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2550 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2557 * ieee802_1x_kay_deinit_transmit_sc -
2560 ieee802_1x_kay_deinit_transmit_sc(
2561 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2563 struct transmit_sa *psa, *tmp;
2565 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2567 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2569 secy_disable_transmit_sa(participant->kay, psa);
2570 ieee802_1x_kay_deinit_transmit_sa(psa);
2577 /****************** Interface between CP and KAY *********************/
2579 * ieee802_1x_kay_set_latest_sa_attr -
2581 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2582 struct ieee802_1x_mka_ki *lki, u8 lan,
2583 Boolean ltx, Boolean lrx)
2585 struct ieee802_1x_mka_participant *principal;
2587 principal = ieee802_1x_kay_get_principal_participant(kay);
2592 os_memset(&principal->lki, 0, sizeof(principal->lki));
2594 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2596 principal->lan = lan;
2597 principal->ltx = ltx;
2598 principal->lrx = lrx;
2603 kay->ltx_kn = lki->kn;
2604 kay->lrx_kn = lki->kn;
2614 * ieee802_1x_kay_set_old_sa_attr -
2616 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2617 struct ieee802_1x_mka_ki *oki,
2618 u8 oan, Boolean otx, Boolean orx)
2620 struct ieee802_1x_mka_participant *principal;
2622 principal = ieee802_1x_kay_get_principal_participant(kay);
2627 os_memset(&principal->oki, 0, sizeof(principal->oki));
2629 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2631 principal->oan = oan;
2632 principal->otx = otx;
2633 principal->orx = orx;
2639 kay->otx_kn = oki->kn;
2640 kay->orx_kn = oki->kn;
2650 * ieee802_1x_kay_create_sas -
2652 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2653 struct ieee802_1x_mka_ki *lki)
2655 struct data_key *sa_key, *latest_sak;
2656 struct ieee802_1x_mka_participant *principal;
2657 struct receive_sc *rxsc;
2658 struct receive_sa *rxsa;
2659 struct transmit_sa *txsa;
2661 principal = ieee802_1x_kay_get_principal_participant(kay);
2666 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2667 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2668 sa_key->rx_latest = TRUE;
2669 sa_key->tx_latest = TRUE;
2670 latest_sak = sa_key;
2671 principal->to_use_sak = TRUE;
2673 sa_key->rx_latest = FALSE;
2674 sa_key->tx_latest = FALSE;
2678 wpa_printf(MSG_ERROR, "lki related sak not found");
2682 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2683 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2688 secy_create_receive_sa(kay, rxsa);
2691 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2696 secy_create_transmit_sa(kay, txsa);
2705 * ieee802_1x_kay_delete_sas -
2707 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2708 struct ieee802_1x_mka_ki *ki)
2710 struct data_key *sa_key, *pre_key;
2711 struct transmit_sa *txsa, *pre_txsa;
2712 struct receive_sa *rxsa, *pre_rxsa;
2713 struct receive_sc *rxsc;
2714 struct ieee802_1x_mka_participant *principal;
2716 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2717 principal = ieee802_1x_kay_get_principal_participant(kay);
2721 /* remove the transmit sa */
2722 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2723 struct transmit_sa, list) {
2724 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2725 secy_disable_transmit_sa(kay, txsa);
2726 ieee802_1x_kay_deinit_transmit_sa(txsa);
2730 /* remove the receive sa */
2731 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2732 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2733 struct receive_sa, list) {
2734 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2735 secy_disable_receive_sa(kay, rxsa);
2736 ieee802_1x_kay_deinit_receive_sa(rxsa);
2741 /* remove the sak */
2742 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2743 struct data_key, list) {
2744 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2745 ieee802_1x_kay_deinit_data_key(sa_key);
2748 if (principal->new_key == sa_key)
2749 principal->new_key = NULL;
2757 * ieee802_1x_kay_enable_tx_sas -
2759 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2760 struct ieee802_1x_mka_ki *lki)
2762 struct ieee802_1x_mka_participant *principal;
2763 struct transmit_sa *txsa;
2765 principal = ieee802_1x_kay_get_principal_participant(kay);
2769 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2771 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2772 txsa->in_use = TRUE;
2773 secy_enable_transmit_sa(kay, txsa);
2774 ieee802_1x_cp_set_usingtransmitas(
2775 principal->kay->cp, TRUE);
2776 ieee802_1x_cp_sm_step(principal->kay->cp);
2785 * ieee802_1x_kay_enable_rx_sas -
2787 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2788 struct ieee802_1x_mka_ki *lki)
2790 struct ieee802_1x_mka_participant *principal;
2791 struct receive_sa *rxsa;
2792 struct receive_sc *rxsc;
2794 principal = ieee802_1x_kay_get_principal_participant(kay);
2798 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2799 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2801 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2802 rxsa->in_use = TRUE;
2803 secy_enable_receive_sa(kay, rxsa);
2804 ieee802_1x_cp_set_usingreceivesas(
2805 principal->kay->cp, TRUE);
2806 ieee802_1x_cp_sm_step(principal->kay->cp);
2816 * ieee802_1x_kay_enable_new_info -
2818 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2820 struct ieee802_1x_mka_participant *principal;
2822 principal = ieee802_1x_kay_get_principal_participant(kay);
2826 if (principal->retry_count < MAX_RETRY_CNT) {
2827 ieee802_1x_participant_send_mkpdu(principal);
2828 principal->retry_count++;
2836 * ieee802_1x_kay_cp_conf -
2838 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2839 struct ieee802_1x_cp_conf *pconf)
2841 pconf->protect = kay->macsec_protect;
2842 pconf->replay_protect = kay->macsec_replay_protect;
2843 pconf->validate = kay->macsec_validate;
2850 * ieee802_1x_kay_alloc_cp_sm -
2852 static struct ieee802_1x_cp_sm *
2853 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2855 struct ieee802_1x_cp_conf conf;
2857 os_memset(&conf, 0, sizeof(conf));
2858 conf.protect = kay->macsec_protect;
2859 conf.replay_protect = kay->macsec_replay_protect;
2860 conf.validate = kay->macsec_validate;
2861 conf.replay_window = kay->macsec_replay_window;
2863 return ieee802_1x_cp_sm_init(kay, &conf);
2868 * ieee802_1x_kay_mkpdu_sanity_check -
2869 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2871 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2872 const u8 *buf, size_t len)
2874 struct ieee8023_hdr *eth_hdr;
2875 struct ieee802_1x_hdr *eapol_hdr;
2876 struct ieee802_1x_mka_hdr *mka_hdr;
2877 struct ieee802_1x_mka_basic_body *body;
2879 struct ieee802_1x_mka_participant *participant;
2881 u8 icv[MAX_ICV_LEN];
2884 eth_hdr = (struct ieee8023_hdr *) buf;
2885 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2886 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2888 /* destination address should be not individual address */
2889 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2890 wpa_printf(MSG_MSGDUMP,
2891 "KaY: ethernet destination address is not PAE group address");
2895 /* MKPDU should not less than 32 octets */
2896 mka_msg_len = be_to_host16(eapol_hdr->length);
2897 if (mka_msg_len < 32) {
2898 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2901 /* MKPDU should multiple 4 octets */
2902 if ((mka_msg_len % 4) != 0) {
2903 wpa_printf(MSG_MSGDUMP,
2904 "KaY: MKPDU is not multiple of 4 octets");
2908 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2909 ieee802_1x_mka_dump_basic_body(body);
2910 body_len = get_mka_param_body_len(body);
2911 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2912 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2913 wpa_printf(MSG_ERROR,
2914 "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",
2915 (int) mka_msg_len, (int) MKA_HDR_LEN,
2916 (int) body_len, DEFAULT_ICV_LEN);
2920 /* CKN should be owned by I */
2921 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2923 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2927 /* algorithm agility check */
2928 if (os_memcmp(body->algo_agility, mka_algo_agility,
2929 sizeof(body->algo_agility)) != 0) {
2930 wpa_printf(MSG_ERROR,
2931 "KaY: peer's algorithm agility not supported for me");
2937 * The ICV will comprise the final octets of the packet body, whatever
2938 * its size, not the fixed length 16 octets, indicated by the EAPOL
2939 * packet body length.
2941 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2942 participant->ick.key,
2943 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2944 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2947 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2951 if (os_memcmp_const(msg_icv, icv,
2952 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2954 wpa_printf(MSG_ERROR,
2955 "KaY: Computed ICV is not equal to Received ICV");
2959 wpa_printf(MSG_ERROR, "KaY: No ICV");
2968 * ieee802_1x_kay_decode_mkpdu -
2970 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2971 const u8 *buf, size_t len)
2973 struct ieee802_1x_mka_participant *participant;
2974 struct ieee802_1x_mka_hdr *hdr;
2980 Boolean my_included;
2981 Boolean handled[256];
2983 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2986 /* handle basic parameter set */
2987 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2988 left_len = len - sizeof(struct ieee8023_hdr) -
2989 sizeof(struct ieee802_1x_hdr);
2990 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2994 /* to skip basic parameter set */
2995 hdr = (struct ieee802_1x_mka_hdr *) pos;
2996 body_len = get_mka_param_body_len(hdr);
2997 pos += body_len + MKA_HDR_LEN;
2998 left_len -= body_len + MKA_HDR_LEN;
3000 /* check i am in the peer's peer list */
3001 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3003 /* accept the peer as live peer */
3004 if (!ieee802_1x_kay_is_in_peer(
3006 participant->current_peer_id.mi)) {
3007 if (!ieee802_1x_kay_create_live_peer(
3009 participant->current_peer_id.mi,
3011 participant->current_peer_id.mn)))
3013 ieee802_1x_kay_elect_key_server(participant);
3014 ieee802_1x_kay_decide_macsec_use(participant);
3016 if (ieee802_1x_kay_is_in_potential_peer(
3017 participant, participant->current_peer_id.mi)) {
3018 ieee802_1x_kay_move_live_peer(
3019 participant, participant->current_peer_id.mi,
3020 be_to_host32(participant->current_peer_id.mn));
3021 ieee802_1x_kay_elect_key_server(participant);
3022 ieee802_1x_kay_decide_macsec_use(participant);
3027 * Handle other parameter set than basic parameter set.
3028 * Each parameter set should be present only once.
3030 for (i = 0; i < 256; i++)
3034 while (left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN) {
3035 hdr = (struct ieee802_1x_mka_hdr *) pos;
3036 body_len = get_mka_param_body_len(hdr);
3037 body_type = get_mka_param_body_type(hdr);
3039 if (body_type == MKA_ICV_INDICATOR)
3042 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3043 wpa_printf(MSG_ERROR,
3044 "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",
3045 (int) left_len, (int) MKA_HDR_LEN,
3046 (int) body_len, DEFAULT_ICV_LEN);
3050 if (handled[body_type])
3053 handled[body_type] = TRUE;
3054 if (mak_body_handler[body_type].body_rx) {
3055 mak_body_handler[body_type].body_rx
3056 (participant, pos, left_len);
3058 wpa_printf(MSG_ERROR,
3059 "The type %d not supported in this MKA version %d",
3060 body_type, MKA_VERSION_ID);
3064 pos += body_len + MKA_HDR_LEN;
3065 left_len -= body_len + MKA_HDR_LEN;
3069 participant->retry_count = 0;
3070 participant->active = TRUE;
3077 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3080 struct ieee802_1x_kay *kay = ctx;
3081 struct ieee8023_hdr *eth_hdr;
3082 struct ieee802_1x_hdr *eapol_hdr;
3084 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3085 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3086 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3087 (unsigned long) len);
3091 eth_hdr = (struct ieee8023_hdr *) buf;
3092 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3093 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3094 be_to_host16(eapol_hdr->length)) {
3095 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3096 (unsigned long) len,
3097 (unsigned long) be_to_host16(eapol_hdr->length));
3101 if (eapol_hdr->version < EAPOL_VERSION) {
3102 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3103 eapol_hdr->version);
3106 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3107 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3110 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3111 if (dl_list_empty(&kay->participant_list)) {
3112 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3116 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3121 * ieee802_1x_kay_init -
3123 struct ieee802_1x_kay *
3124 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3125 const char *ifname, const u8 *addr)
3127 struct ieee802_1x_kay *kay;
3129 kay = os_zalloc(sizeof(*kay));
3131 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3138 kay->active = FALSE;
3140 kay->authenticated = FALSE;
3141 kay->secured = FALSE;
3142 kay->failed = FALSE;
3143 kay->policy = policy;
3145 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3146 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3147 kay->actor_sci.port = host_to_be16(0x0001);
3148 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3150 /* While actor acts as a key server, shall distribute sakey */
3155 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3156 kay->macsec_csindex = DEFAULT_CS_INDEX;
3157 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3158 kay->mka_version = MKA_VERSION_ID;
3160 os_memcpy(kay->algo_agility, mka_algo_agility,
3161 sizeof(kay->algo_agility));
3163 dl_list_init(&kay->participant_list);
3165 if (policy == DO_NOT_SECURE) {
3166 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3167 kay->macsec_desired = FALSE;
3168 kay->macsec_protect = FALSE;
3169 kay->macsec_validate = Disabled;
3170 kay->macsec_replay_protect = FALSE;
3171 kay->macsec_replay_window = 0;
3172 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3174 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3175 kay->macsec_desired = TRUE;
3176 kay->macsec_protect = TRUE;
3177 kay->macsec_validate = Strict;
3178 kay->macsec_replay_protect = FALSE;
3179 kay->macsec_replay_window = 0;
3180 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3183 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3185 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3186 secy_init_macsec(kay);
3187 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3189 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3192 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3193 if (kay->cp == NULL) {
3194 ieee802_1x_kay_deinit(kay);
3198 if (policy == DO_NOT_SECURE) {
3199 ieee802_1x_cp_connect_authenticated(kay->cp);
3200 ieee802_1x_cp_sm_step(kay->cp);
3202 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3203 kay_l2_receive, kay, 1);
3204 if (kay->l2_mka == NULL) {
3205 wpa_printf(MSG_WARNING,
3206 "KaY: Failed to initialize L2 packet processing for MKA packet");
3207 ieee802_1x_kay_deinit(kay);
3217 * ieee802_1x_kay_deinit -
3220 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3222 struct ieee802_1x_mka_participant *participant;
3227 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3229 while (!dl_list_empty(&kay->participant_list)) {
3230 participant = dl_list_entry(kay->participant_list.next,
3231 struct ieee802_1x_mka_participant,
3233 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3236 ieee802_1x_cp_sm_deinit(kay->cp);
3237 secy_deinit_macsec(kay);
3240 l2_packet_deinit(kay->l2_mka);
3250 * ieee802_1x_kay_create_mka -
3252 struct ieee802_1x_mka_participant *
3253 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3254 struct mka_key *cak, u32 life,
3255 enum mka_created_mode mode, Boolean is_authenticator)
3257 struct ieee802_1x_mka_participant *participant;
3260 if (!kay || !ckn || !cak) {
3261 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3265 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3266 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3269 if (ckn->len > MAX_CKN_LEN) {
3270 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3274 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3278 participant = os_zalloc(sizeof(*participant));
3280 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3284 participant->ckn.len = ckn->len;
3285 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3286 participant->cak.len = cak->len;
3287 os_memcpy(participant->cak.key, cak->key, cak->len);
3289 participant->cak_life = life + time(NULL);
3293 if (is_authenticator) {
3294 participant->is_obliged_key_server = TRUE;
3295 participant->can_be_key_server = TRUE;
3296 participant->is_key_server = TRUE;
3297 participant->principal = TRUE;
3299 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3300 sizeof(kay->key_server_sci));
3301 kay->key_server_priority = kay->actor_priority;
3302 participant->is_elected = TRUE;
3304 participant->is_obliged_key_server = FALSE;
3305 participant->can_be_key_server = FALSE;
3306 participant->is_key_server = FALSE;
3307 participant->is_elected = TRUE;
3312 participant->is_obliged_key_server = FALSE;
3313 participant->can_be_key_server = TRUE;
3314 participant->is_key_server = TRUE;
3315 participant->is_elected = FALSE;
3319 participant->cached = FALSE;
3321 participant->active = FALSE;
3322 participant->participant = FALSE;
3323 participant->retain = FALSE;
3324 participant->activate = DEFAULT;
3326 if (participant->is_key_server)
3327 participant->principal = TRUE;
3329 dl_list_init(&participant->live_peers);
3330 dl_list_init(&participant->potential_peers);
3332 participant->retry_count = 0;
3333 participant->kay = kay;
3335 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
3337 participant->mn = 0;
3339 participant->lrx = FALSE;
3340 participant->ltx = FALSE;
3341 participant->orx = FALSE;
3342 participant->otx = FALSE;
3343 participant->to_dist_sak = FALSE;
3344 participant->to_use_sak = FALSE;
3345 participant->new_sak = FALSE;
3346 dl_list_init(&participant->sak_list);
3347 participant->new_key = NULL;
3348 dl_list_init(&participant->rxsc_list);
3349 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3351 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3352 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3353 kay->macsec_replay_window);
3354 secy_create_transmit_sc(kay, participant->txsc);
3356 /* to derive KEK from CAK and CKN */
3357 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3358 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3359 participant->ckn.name,
3360 participant->ckn.len,
3361 participant->kek.key)) {
3362 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3365 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3366 participant->kek.key, participant->kek.len);
3368 /* to derive ICK from CAK and CKN */
3369 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3370 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3371 participant->ckn.name,
3372 participant->ckn.len,
3373 participant->ick.key)) {
3374 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3377 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3378 participant->ick.key, participant->ick.len);
3380 dl_list_add(&kay->participant_list, &participant->list);
3381 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3382 ckn->name, ckn->len);
3384 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3385 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3387 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3393 os_free(participant);
3399 * ieee802_1x_kay_delete_mka -
3402 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3404 struct ieee802_1x_mka_participant *participant;
3405 struct ieee802_1x_kay_peer *peer;
3406 struct data_key *sak;
3407 struct receive_sc *rxsc;
3412 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3414 /* get the participant */
3415 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3417 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3418 ckn->name, ckn->len);
3422 dl_list_del(&participant->list);
3424 /* remove live peer */
3425 while (!dl_list_empty(&participant->live_peers)) {
3426 peer = dl_list_entry(participant->live_peers.next,
3427 struct ieee802_1x_kay_peer, list);
3428 dl_list_del(&peer->list);
3432 /* remove potential peer */
3433 while (!dl_list_empty(&participant->potential_peers)) {
3434 peer = dl_list_entry(participant->potential_peers.next,
3435 struct ieee802_1x_kay_peer, list);
3436 dl_list_del(&peer->list);
3441 while (!dl_list_empty(&participant->sak_list)) {
3442 sak = dl_list_entry(participant->sak_list.next,
3443 struct data_key, list);
3444 dl_list_del(&sak->list);
3448 while (!dl_list_empty(&participant->rxsc_list)) {
3449 rxsc = dl_list_entry(participant->rxsc_list.next,
3450 struct receive_sc, list);
3451 secy_delete_receive_sc(kay, rxsc);
3452 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3454 secy_delete_transmit_sc(kay, participant->txsc);
3455 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3457 os_memset(&participant->cak, 0, sizeof(participant->cak));
3458 os_memset(&participant->kek, 0, sizeof(participant->kek));
3459 os_memset(&participant->ick, 0, sizeof(participant->ick));
3460 os_free(participant);
3465 * ieee802_1x_kay_mka_participate -
3467 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3468 struct mka_key_name *ckn,
3471 struct ieee802_1x_mka_participant *participant;
3476 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3480 participant->active = status;
3485 * ieee802_1x_kay_new_sak -
3488 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3490 struct ieee802_1x_mka_participant *participant;
3495 participant = ieee802_1x_kay_get_principal_participant(kay);
3499 participant->new_sak = TRUE;
3500 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3507 * ieee802_1x_kay_change_cipher_suite -
3510 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3512 struct ieee802_1x_mka_participant *participant;
3517 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3518 wpa_printf(MSG_ERROR,
3519 "KaY: Configured cipher suite index is out of range");
3522 if (kay->macsec_csindex == cs_index)
3526 kay->macsec_desired = FALSE;
3528 kay->macsec_csindex = cs_index;
3529 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3531 participant = ieee802_1x_kay_get_principal_participant(kay);
3533 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3534 participant->new_sak = TRUE;