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 #define MKA_ALIGN_LENGTH(len) (((len) + 0x3) & ~0x3)
34 /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
35 #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
36 static u8 mka_algo_agility[4] = MKA_ALGO_AGILITY_2009;
38 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
39 static struct macsec_ciphersuite cipher_suite_tbl[] = {
42 .id = CS_ID_GCM_AES_128,
43 .name = CS_NAME_GCM_AES_128,
44 .capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50,
45 .sak_len = DEFAULT_SA_KEY_LEN,
49 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
50 #define DEFAULT_CS_INDEX 0
52 static struct mka_alg mka_alg_tbl[] = {
54 .parameter = MKA_ALGO_AGILITY_2009,
56 /* 128-bit CAK, KEK, ICK, ICV */
57 .cak_len = DEFAULT_ICV_LEN,
58 .kek_len = DEFAULT_ICV_LEN,
59 .ick_len = DEFAULT_ICV_LEN,
60 .icv_len = DEFAULT_ICV_LEN,
62 .cak_trfm = ieee802_1x_cak_128bits_aes_cmac,
63 .ckn_trfm = ieee802_1x_ckn_128bits_aes_cmac,
64 .kek_trfm = ieee802_1x_kek_128bits_aes_cmac,
65 .ick_trfm = ieee802_1x_ick_128bits_aes_cmac,
66 .icv_hash = ieee802_1x_icv_128bits_aes_cmac,
71 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
74 static int is_ki_equal(struct ieee802_1x_mka_ki *ki1,
75 struct ieee802_1x_mka_ki *ki2)
77 return os_memcmp(ki1->mi, ki2->mi, MI_LEN) == 0 &&
82 static void set_mka_param_body_len(void *body, unsigned int len)
84 struct ieee802_1x_mka_hdr *hdr = body;
85 hdr->length = (len >> 8) & 0x0f;
86 hdr->length1 = len & 0xff;
90 static unsigned int get_mka_param_body_len(const void *body)
92 const struct ieee802_1x_mka_hdr *hdr = body;
93 return (hdr->length << 8) | hdr->length1;
97 static u8 get_mka_param_body_type(const void *body)
99 const struct ieee802_1x_mka_hdr *hdr = body;
105 * ieee802_1x_mka_dump_basic_body -
108 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
115 body_len = get_mka_param_body_len(body);
116 wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
117 wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
118 wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
119 wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
120 wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
121 wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
122 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
123 wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
124 MAC2STR(body->actor_sci.addr));
125 wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
126 be_to_host16(body->actor_sci.port));
127 wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
128 body->actor_mi, sizeof(body->actor_mi));
129 wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
130 be_to_host32(body->actor_mn));
131 wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
132 body->algo_agility, sizeof(body->algo_agility));
133 wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
134 body_len + MKA_HDR_LEN - sizeof(*body));
139 * ieee802_1x_mka_dump_peer_body -
142 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
152 body_len = get_mka_param_body_len(body);
153 if (body->type == MKA_LIVE_PEER_LIST) {
154 wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
155 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
156 } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
157 wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
158 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
161 for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
163 os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
164 wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
165 wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
171 * ieee802_1x_mka_dump_dist_sak_body -
174 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
181 body_len = get_mka_param_body_len(body);
182 wpa_printf(MSG_INFO, "*** Distributed SAK ***");
183 wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
184 wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
185 body->confid_offset);
186 wpa_printf(MSG_INFO, "\tBody Length...........: %zu", body_len);
190 wpa_printf(MSG_INFO, "\tKey Number............: %d",
191 be_to_host32(body->kn));
192 wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
196 static const char * yes_no(int val)
198 return val ? "Yes" : "No";
203 * ieee802_1x_mka_dump_sak_use_body -
206 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
213 body_len = get_mka_param_body_len(body);
214 wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
215 wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
216 wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
217 wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
218 wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
219 wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
220 wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
221 wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
222 wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
223 wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
224 yes_no(body->delay_protect));
225 wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
229 wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
230 body->lsrv_mi, sizeof(body->lsrv_mi));
231 wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
232 be_to_host32(body->lkn));
233 wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
234 be_to_host32(body->llpn));
235 wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
236 body->osrv_mi, sizeof(body->osrv_mi));
237 wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
238 be_to_host32(body->okn));
239 wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
240 be_to_host32(body->olpn));
245 * ieee802_1x_kay_get_participant -
247 static struct ieee802_1x_mka_participant *
248 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
250 struct ieee802_1x_mka_participant *participant;
252 dl_list_for_each(participant, &kay->participant_list,
253 struct ieee802_1x_mka_participant, list) {
254 if (os_memcmp(participant->ckn.name, ckn,
255 participant->ckn.len) == 0)
259 wpa_printf(MSG_DEBUG, "KaY: participant is not found");
266 * ieee802_1x_kay_get_principal_participant -
268 static struct ieee802_1x_mka_participant *
269 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
271 struct ieee802_1x_mka_participant *participant;
273 dl_list_for_each(participant, &kay->participant_list,
274 struct ieee802_1x_mka_participant, list) {
275 if (participant->principal)
279 wpa_printf(MSG_DEBUG, "KaY: principal participant is not found");
284 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
287 struct ieee802_1x_kay_peer *peer;
289 dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
290 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
299 * ieee802_1x_kay_get_potential_peer
301 static struct ieee802_1x_kay_peer *
302 ieee802_1x_kay_get_potential_peer(
303 struct ieee802_1x_mka_participant *participant, const u8 *mi)
305 return get_peer_mi(&participant->potential_peers, mi);
310 * ieee802_1x_kay_get_live_peer
312 static struct ieee802_1x_kay_peer *
313 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
316 return get_peer_mi(&participant->live_peers, mi);
321 * ieee802_1x_kay_is_in_potential_peer
324 ieee802_1x_kay_is_in_potential_peer(
325 struct ieee802_1x_mka_participant *participant, const u8 *mi)
327 return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
332 * ieee802_1x_kay_is_in_live_peer
335 ieee802_1x_kay_is_in_live_peer(
336 struct ieee802_1x_mka_participant *participant, const u8 *mi)
338 return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
343 * ieee802_1x_kay_get_peer
345 static struct ieee802_1x_kay_peer *
346 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
349 struct ieee802_1x_kay_peer *peer;
351 peer = ieee802_1x_kay_get_live_peer(participant, mi);
355 return ieee802_1x_kay_get_potential_peer(participant, mi);
360 * ieee802_1x_kay_get_cipher_suite
362 static struct macsec_ciphersuite *
363 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
370 os_memcpy(&_cs, cs_id, CS_ID_LEN);
371 cs = be_to_host64(_cs);
373 for (i = 0; i < CS_TABLE_SIZE; i++) {
374 if (cipher_suite_tbl[i].id == cs)
375 return &cipher_suite_tbl[i];
382 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
383 const struct ieee802_1x_mka_sci *b)
385 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
390 * ieee802_1x_kay_get_peer_sci
392 static struct ieee802_1x_kay_peer *
393 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
394 const struct ieee802_1x_mka_sci *sci)
396 struct ieee802_1x_kay_peer *peer;
398 dl_list_for_each(peer, &participant->live_peers,
399 struct ieee802_1x_kay_peer, list) {
400 if (sci_equal(&peer->sci, sci))
404 dl_list_for_each(peer, &participant->potential_peers,
405 struct ieee802_1x_kay_peer, list) {
406 if (sci_equal(&peer->sci, sci))
415 * ieee802_1x_kay_init_receive_sa -
417 static struct receive_sa *
418 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
419 struct data_key *key)
421 struct receive_sa *psa;
426 psa = os_zalloc(sizeof(*psa));
428 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
433 psa->lowest_pn = lowest_pn;
434 psa->next_pn = lowest_pn;
438 os_get_time(&psa->created_time);
441 dl_list_add(&psc->sa_list, &psa->list);
442 wpa_printf(MSG_DEBUG,
443 "KaY: Create receive SA(AN: %hhu lowest_pn: %u of SC(channel: %d)",
444 an, lowest_pn, psc->channel);
451 * ieee802_1x_kay_deinit_receive_sa -
453 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
456 wpa_printf(MSG_DEBUG,
457 "KaY: Delete receive SA(an: %hhu) of SC",
459 dl_list_del(&psa->list);
465 * ieee802_1x_kay_init_receive_sc -
467 static struct receive_sc *
468 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
471 struct receive_sc *psc;
476 psc = os_zalloc(sizeof(*psc));
478 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
482 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
483 psc->channel = channel;
485 os_get_time(&psc->created_time);
486 psc->receiving = FALSE;
488 dl_list_init(&psc->sa_list);
489 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
490 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
497 * ieee802_1x_kay_deinit_receive_sc -
500 ieee802_1x_kay_deinit_receive_sc(
501 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
503 struct receive_sa *psa, *pre_sa;
505 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
507 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
509 secy_disable_receive_sa(participant->kay, psa);
510 ieee802_1x_kay_deinit_receive_sa(psa);
512 dl_list_del(&psc->list);
517 static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
519 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
520 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
521 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
522 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
526 static struct ieee802_1x_kay_peer *
527 ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
529 struct ieee802_1x_kay_peer *peer;
531 peer = os_zalloc(sizeof(*peer));
533 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
537 os_memcpy(peer->mi, mi, MI_LEN);
539 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
540 peer->sak_used = FALSE;
547 * ieee802_1x_kay_create_live_peer
549 static struct ieee802_1x_kay_peer *
550 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
551 const u8 *mi, u32 mn)
553 struct ieee802_1x_kay_peer *peer;
554 struct receive_sc *rxsc;
557 peer = ieee802_1x_kay_create_peer(mi, mn);
561 os_memcpy(&peer->sci, &participant->current_peer_sci,
564 secy_get_available_receive_sc(participant->kay, &sc_ch);
566 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
572 dl_list_add(&participant->live_peers, &peer->list);
573 dl_list_add(&participant->rxsc_list, &rxsc->list);
574 secy_create_receive_sc(participant->kay, rxsc);
576 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
577 ieee802_1x_kay_dump_peer(peer);
584 * ieee802_1x_kay_create_potential_peer
586 static struct ieee802_1x_kay_peer *
587 ieee802_1x_kay_create_potential_peer(
588 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
590 struct ieee802_1x_kay_peer *peer;
592 peer = ieee802_1x_kay_create_peer(mi, mn);
596 dl_list_add(&participant->potential_peers, &peer->list);
598 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
599 ieee802_1x_kay_dump_peer(peer);
606 * ieee802_1x_kay_move_live_peer
608 static struct ieee802_1x_kay_peer *
609 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
612 struct ieee802_1x_kay_peer *peer;
613 struct receive_sc *rxsc;
616 peer = ieee802_1x_kay_get_potential_peer(participant, mi);
618 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
623 os_memcpy(&peer->sci, &participant->current_peer_sci,
626 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
628 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
629 ieee802_1x_kay_dump_peer(peer);
631 dl_list_del(&peer->list);
632 dl_list_add_tail(&participant->live_peers, &peer->list);
634 secy_get_available_receive_sc(participant->kay, &sc_ch);
636 dl_list_add(&participant->rxsc_list, &rxsc->list);
637 secy_create_receive_sc(participant->kay, rxsc);
645 * ieee802_1x_mka_basic_body_present -
648 ieee802_1x_mka_basic_body_present(
649 struct ieee802_1x_mka_participant *participant)
656 * ieee802_1x_mka_basic_body_length -
659 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
663 length = sizeof(struct ieee802_1x_mka_basic_body);
664 length += participant->ckn.len;
665 return MKA_ALIGN_LENGTH(length);
670 * ieee802_1x_mka_encode_basic_body
673 ieee802_1x_mka_encode_basic_body(
674 struct ieee802_1x_mka_participant *participant,
677 struct ieee802_1x_mka_basic_body *body;
678 struct ieee802_1x_kay *kay = participant->kay;
679 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
681 body = wpabuf_put(buf, length);
683 body->version = kay->mka_version;
684 body->priority = kay->actor_priority;
685 if (participant->is_elected)
686 body->key_server = participant->is_key_server;
688 body->key_server = participant->can_be_key_server;
690 body->macsec_desired = kay->macsec_desired;
691 body->macsec_capability = kay->macsec_capable;
692 set_mka_param_body_len(body, length - MKA_HDR_LEN);
694 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
695 sizeof(kay->actor_sci.addr));
696 body->actor_sci.port = kay->actor_sci.port;
698 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
699 participant->mn = participant->mn + 1;
700 body->actor_mn = host_to_be32(participant->mn);
701 os_memcpy(body->algo_agility, kay->algo_agility,
702 sizeof(body->algo_agility));
704 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
706 ieee802_1x_mka_dump_basic_body(body);
713 reset_participant_mi(struct ieee802_1x_mka_participant *participant)
715 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
724 * ieee802_1x_mka_decode_basic_body -
726 static struct ieee802_1x_mka_participant *
727 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
730 struct ieee802_1x_mka_participant *participant;
731 const struct ieee802_1x_mka_basic_body *body;
732 struct ieee802_1x_kay_peer *peer;
734 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
736 if (body->version > MKA_VERSION_ID) {
737 wpa_printf(MSG_DEBUG,
738 "KaY: peer's version(%d) greater than mka current version(%d)",
739 body->version, MKA_VERSION_ID);
741 if (kay->is_obliged_key_server && body->key_server) {
742 wpa_printf(MSG_DEBUG, "I must be as key server");
746 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
748 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
752 /* If the peer's MI is my MI, I will choose new MI */
753 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
754 if (!reset_participant_mi(participant))
758 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
759 participant->current_peer_id.mn = body->actor_mn;
760 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
761 sizeof(participant->current_peer_sci.addr));
762 participant->current_peer_sci.port = body->actor_sci.port;
765 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
767 /* Check duplicated SCI */
768 /* TODO: What policy should be applied to detect duplicated SCI
769 * is active attacker or a valid peer whose MI is be changed?
771 peer = ieee802_1x_kay_get_peer_sci(participant,
774 wpa_printf(MSG_WARNING,
775 "KaY: duplicated SCI detected, Maybe active attacker");
776 dl_list_del(&peer->list);
780 peer = ieee802_1x_kay_create_potential_peer(
781 participant, body->actor_mi,
782 be_to_host32(body->actor_mn));
786 peer->macsec_desired = body->macsec_desired;
787 peer->macsec_capability = body->macsec_capability;
788 peer->is_key_server = (Boolean) body->key_server;
789 peer->key_server_priority = body->priority;
790 } else if (peer->mn < be_to_host32(body->actor_mn)) {
791 peer->mn = be_to_host32(body->actor_mn);
792 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
793 peer->macsec_desired = body->macsec_desired;
794 peer->macsec_capability = body->macsec_capability;
795 peer->is_key_server = (Boolean) body->key_server;
796 peer->key_server_priority = body->priority;
798 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
807 * ieee802_1x_mka_live_peer_body_present
810 ieee802_1x_mka_live_peer_body_present(
811 struct ieee802_1x_mka_participant *participant)
813 return !dl_list_empty(&participant->live_peers);
818 * ieee802_1x_kay_get_live_peer_length
821 ieee802_1x_mka_get_live_peer_length(
822 struct ieee802_1x_mka_participant *participant)
824 int len = MKA_HDR_LEN;
825 struct ieee802_1x_kay_peer *peer;
827 dl_list_for_each(peer, &participant->live_peers,
828 struct ieee802_1x_kay_peer, list)
829 len += sizeof(struct ieee802_1x_mka_peer_id);
831 return MKA_ALIGN_LENGTH(len);
836 * ieee802_1x_mka_encode_live_peer_body -
839 ieee802_1x_mka_encode_live_peer_body(
840 struct ieee802_1x_mka_participant *participant,
843 struct ieee802_1x_mka_peer_body *body;
844 struct ieee802_1x_kay_peer *peer;
846 struct ieee802_1x_mka_peer_id *body_peer;
848 length = ieee802_1x_mka_get_live_peer_length(participant);
849 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
851 body->type = MKA_LIVE_PEER_LIST;
852 set_mka_param_body_len(body, length - MKA_HDR_LEN);
854 dl_list_for_each(peer, &participant->live_peers,
855 struct ieee802_1x_kay_peer, list) {
856 body_peer = wpabuf_put(buf,
857 sizeof(struct ieee802_1x_mka_peer_id));
858 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
859 body_peer->mn = host_to_be32(peer->mn);
862 ieee802_1x_mka_dump_peer_body(body);
867 * ieee802_1x_mka_potential_peer_body_present
870 ieee802_1x_mka_potential_peer_body_present(
871 struct ieee802_1x_mka_participant *participant)
873 return !dl_list_empty(&participant->potential_peers);
878 * ieee802_1x_kay_get_potential_peer_length
881 ieee802_1x_mka_get_potential_peer_length(
882 struct ieee802_1x_mka_participant *participant)
884 int len = MKA_HDR_LEN;
885 struct ieee802_1x_kay_peer *peer;
887 dl_list_for_each(peer, &participant->potential_peers,
888 struct ieee802_1x_kay_peer, list)
889 len += sizeof(struct ieee802_1x_mka_peer_id);
891 return MKA_ALIGN_LENGTH(len);
896 * ieee802_1x_mka_encode_potential_peer_body -
899 ieee802_1x_mka_encode_potential_peer_body(
900 struct ieee802_1x_mka_participant *participant,
903 struct ieee802_1x_mka_peer_body *body;
904 struct ieee802_1x_kay_peer *peer;
906 struct ieee802_1x_mka_peer_id *body_peer;
908 length = ieee802_1x_mka_get_potential_peer_length(participant);
909 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
911 body->type = MKA_POTENTIAL_PEER_LIST;
912 set_mka_param_body_len(body, length - MKA_HDR_LEN);
914 dl_list_for_each(peer, &participant->potential_peers,
915 struct ieee802_1x_kay_peer, list) {
916 body_peer = wpabuf_put(buf,
917 sizeof(struct ieee802_1x_mka_peer_id));
918 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
919 body_peer->mn = host_to_be32(peer->mn);
922 ieee802_1x_mka_dump_peer_body(body);
928 * ieee802_1x_mka_i_in_peerlist -
931 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
932 const u8 *mka_msg, size_t msg_len)
934 struct ieee802_1x_mka_hdr *hdr;
941 for (pos = mka_msg, left_len = msg_len;
942 left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
943 left_len -= body_len + MKA_HDR_LEN,
944 pos += body_len + MKA_HDR_LEN) {
945 hdr = (struct ieee802_1x_mka_hdr *) pos;
946 body_len = get_mka_param_body_len(hdr);
947 body_type = get_mka_param_body_type(hdr);
949 if (body_type != MKA_LIVE_PEER_LIST &&
950 body_type != MKA_POTENTIAL_PEER_LIST)
953 ieee802_1x_mka_dump_peer_body(
954 (struct ieee802_1x_mka_peer_body *)pos);
956 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
957 wpa_printf(MSG_ERROR,
958 "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
959 left_len, MKA_HDR_LEN,
960 body_len, DEFAULT_ICV_LEN);
964 if ((body_len % 16) != 0) {
965 wpa_printf(MSG_ERROR,
966 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
971 for (i = 0; i < body_len;
972 i += sizeof(struct ieee802_1x_mka_peer_id)) {
973 const struct ieee802_1x_mka_peer_id *peer_mi;
975 peer_mi = (const struct ieee802_1x_mka_peer_id *)
976 (pos + MKA_HDR_LEN + i);
977 if (os_memcmp(peer_mi->mi, participant->mi,
979 be_to_host32(peer_mi->mn) == participant->mn)
989 * ieee802_1x_mka_decode_live_peer_body -
991 static int ieee802_1x_mka_decode_live_peer_body(
992 struct ieee802_1x_mka_participant *participant,
993 const u8 *peer_msg, size_t msg_len)
995 const struct ieee802_1x_mka_hdr *hdr;
996 struct ieee802_1x_kay_peer *peer;
1001 is_included = ieee802_1x_kay_is_in_live_peer(
1002 participant, participant->current_peer_id.mi);
1004 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1005 body_len = get_mka_param_body_len(hdr);
1006 if (body_len % 16 != 0) {
1007 wpa_printf(MSG_ERROR,
1008 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1013 for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
1014 const struct ieee802_1x_mka_peer_id *peer_mi;
1017 peer_mi = (const struct ieee802_1x_mka_peer_id *)
1018 (peer_msg + MKA_HDR_LEN + i);
1019 peer_mn = be_to_host32(peer_mi->mn);
1022 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1023 /* My message id is used by other participant */
1024 if (peer_mn > participant->mn &&
1025 !reset_participant_mi(participant))
1026 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1033 peer = ieee802_1x_kay_get_peer(participant, peer_mi->mi);
1036 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1037 } else if (!ieee802_1x_kay_create_potential_peer(
1038 participant, peer_mi->mi, peer_mn)) {
1048 * ieee802_1x_mka_decode_potential_peer_body -
1051 ieee802_1x_mka_decode_potential_peer_body(
1052 struct ieee802_1x_mka_participant *participant,
1053 const u8 *peer_msg, size_t msg_len)
1055 const struct ieee802_1x_mka_hdr *hdr;
1059 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1060 body_len = get_mka_param_body_len(hdr);
1061 if (body_len % 16 != 0) {
1062 wpa_printf(MSG_ERROR,
1063 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1068 for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
1069 const struct ieee802_1x_mka_peer_id *peer_mi;
1072 peer_mi = (struct ieee802_1x_mka_peer_id *)
1073 (peer_msg + MKA_HDR_LEN + i);
1074 peer_mn = be_to_host32(peer_mi->mn);
1077 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1078 /* My message id is used by other participant */
1079 if (peer_mn > participant->mn &&
1080 !reset_participant_mi(participant))
1081 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1091 * ieee802_1x_mka_sak_use_body_present
1094 ieee802_1x_mka_sak_use_body_present(
1095 struct ieee802_1x_mka_participant *participant)
1097 return participant->to_use_sak;
1102 * ieee802_1x_mka_get_sak_use_length
1105 ieee802_1x_mka_get_sak_use_length(
1106 struct ieee802_1x_mka_participant *participant)
1108 int length = MKA_HDR_LEN;
1110 if (participant->kay->macsec_desired && participant->advised_desired)
1111 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1113 return MKA_ALIGN_LENGTH(length);
1121 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1122 struct ieee802_1x_mka_ki *ki)
1124 struct receive_sa *rxsa;
1125 struct receive_sc *rxsc;
1128 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1129 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1131 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1132 secy_get_receive_lowest_pn(principal->kay,
1135 lpn = lpn > rxsa->lowest_pn ?
1136 lpn : rxsa->lowest_pn;
1150 * ieee802_1x_mka_encode_sak_use_body -
1153 ieee802_1x_mka_encode_sak_use_body(
1154 struct ieee802_1x_mka_participant *participant,
1157 struct ieee802_1x_mka_sak_use_body *body;
1158 struct ieee802_1x_kay *kay = participant->kay;
1159 unsigned int length;
1162 length = ieee802_1x_mka_get_sak_use_length(participant);
1163 body = wpabuf_put(buf, length);
1165 body->type = MKA_SAK_USE;
1166 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1168 if (length == MKA_HDR_LEN) {
1174 body->delay_protect = FALSE;
1178 /* data protect, lowest accept packet number */
1179 body->delay_protect = kay->macsec_replay_protect;
1180 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1181 if (pn > kay->pn_exhaustion) {
1182 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1183 if (participant->is_key_server)
1184 participant->new_sak = TRUE;
1187 body->llpn = host_to_be32(pn);
1188 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1189 body->olpn = host_to_be32(pn);
1191 /* plain tx, plain rx */
1192 body->ptx = !kay->macsec_protect;
1193 body->prx = kay->macsec_validate != Strict;
1195 /* latest key: rx, tx, key server member identifier key number */
1196 body->lan = participant->lan;
1197 os_memcpy(body->lsrv_mi, participant->lki.mi, sizeof(body->lsrv_mi));
1198 body->lkn = host_to_be32(participant->lki.kn);
1199 body->lrx = participant->lrx;
1200 body->ltx = participant->ltx;
1202 /* old key: rx, tx, key server member identifier key number */
1203 body->oan = participant->oan;
1204 if (participant->oki.kn != participant->lki.kn &&
1205 participant->oki.kn != 0) {
1208 os_memcpy(body->osrv_mi, participant->oki.mi,
1209 sizeof(body->osrv_mi));
1210 body->okn = host_to_be32(participant->oki.kn);
1216 /* set CP's variable */
1218 kay->tx_enable = TRUE;
1219 kay->port_enable = TRUE;
1222 kay->rx_enable = TRUE;
1224 ieee802_1x_mka_dump_sak_use_body(body);
1230 * ieee802_1x_mka_decode_sak_use_body -
1233 ieee802_1x_mka_decode_sak_use_body(
1234 struct ieee802_1x_mka_participant *participant,
1235 const u8 *mka_msg, size_t msg_len)
1237 struct ieee802_1x_mka_hdr *hdr;
1238 struct ieee802_1x_mka_sak_use_body *body;
1239 struct ieee802_1x_kay_peer *peer;
1240 struct transmit_sa *txsa;
1241 struct data_key *sa_key = NULL;
1243 struct ieee802_1x_mka_ki ki;
1245 Boolean all_receiving;
1247 struct ieee802_1x_kay *kay = participant->kay;
1249 if (!participant->principal) {
1250 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1253 peer = ieee802_1x_kay_get_live_peer(participant,
1254 participant->current_peer_id.mi);
1256 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1260 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1261 body_len = get_mka_param_body_len(hdr);
1262 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1263 ieee802_1x_mka_dump_sak_use_body(body);
1265 if ((body_len != 0) && (body_len < 40)) {
1266 wpa_printf(MSG_ERROR,
1267 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
1272 /* TODO: what action should I take when peer does not support MACsec */
1273 if (body_len == 0) {
1274 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1278 /* TODO: when the plain tx or rx of peer is true, should I change
1279 * the attribute of controlled port
1282 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1285 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1287 /* check latest key is valid */
1288 if (body->ltx || body->lrx) {
1290 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1291 ki.kn = be_to_host32(body->lkn);
1292 dl_list_for_each(sa_key, &participant->sak_list,
1293 struct data_key, list) {
1294 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1300 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1303 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1304 sizeof(participant->lki.mi)) == 0 &&
1305 be_to_host32(body->lkn) == participant->lki.kn &&
1306 body->lan == participant->lan) {
1307 peer->sak_used = TRUE;
1309 if (body->ltx && peer->is_key_server) {
1310 ieee802_1x_cp_set_servertransmitting(kay->cp, TRUE);
1311 ieee802_1x_cp_sm_step(kay->cp);
1315 /* check old key is valid */
1316 if (body->otx || body->orx) {
1317 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1318 sizeof(participant->oki.mi)) != 0 ||
1319 be_to_host32(body->okn) != participant->oki.kn ||
1320 body->oan != participant->oan) {
1321 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1326 /* TODO: how to set the MACsec hardware when delay_protect is true */
1327 if (body->delay_protect &&
1328 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1329 wpa_printf(MSG_WARNING,
1330 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1334 /* check all live peer have used the sak for receiving sa */
1335 all_receiving = TRUE;
1336 dl_list_for_each(peer, &participant->live_peers,
1337 struct ieee802_1x_kay_peer, list) {
1338 if (!peer->sak_used) {
1339 all_receiving = FALSE;
1343 if (all_receiving) {
1344 participant->to_dist_sak = FALSE;
1345 ieee802_1x_cp_set_allreceiving(kay->cp, TRUE);
1346 ieee802_1x_cp_sm_step(kay->cp);
1349 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1350 lpn = be_to_host32(body->llpn);
1351 if (lpn > kay->pn_exhaustion) {
1352 if (participant->is_key_server) {
1353 participant->new_sak = TRUE;
1354 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1359 dl_list_for_each(txsa, &participant->txsc->sa_list,
1360 struct transmit_sa, list) {
1361 if (sa_key != NULL && txsa->pkey == sa_key) {
1367 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1371 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1372 * npn is larger than txsa's npn, set it to txsa.
1374 secy_get_transmit_next_pn(kay, txsa);
1375 if (lpn > txsa->next_pn) {
1376 secy_set_transmit_next_pn(kay, txsa);
1377 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1385 * ieee802_1x_mka_dist_sak_body_present
1388 ieee802_1x_mka_dist_sak_body_present(
1389 struct ieee802_1x_mka_participant *participant)
1391 return participant->to_dist_sak && participant->new_key;
1396 * ieee802_1x_kay_get_dist_sak_length
1399 ieee802_1x_mka_get_dist_sak_length(
1400 struct ieee802_1x_mka_participant *participant)
1402 int length = MKA_HDR_LEN;
1403 unsigned int cs_index = participant->kay->macsec_csindex;
1405 if (participant->advised_desired) {
1406 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1407 if (cs_index != DEFAULT_CS_INDEX)
1408 length += CS_ID_LEN;
1410 length += cipher_suite_tbl[cs_index].sak_len + 8;
1413 return MKA_ALIGN_LENGTH(length);
1418 * ieee802_1x_mka_encode_dist_sak_body -
1421 ieee802_1x_mka_encode_dist_sak_body(
1422 struct ieee802_1x_mka_participant *participant,
1425 struct ieee802_1x_mka_dist_sak_body *body;
1426 struct data_key *sak;
1427 unsigned int length;
1428 unsigned int cs_index;
1431 length = ieee802_1x_mka_get_dist_sak_length(participant);
1432 body = wpabuf_put(buf, length);
1433 body->type = MKA_DISTRIBUTED_SAK;
1434 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1435 if (length == MKA_HDR_LEN) {
1436 body->confid_offset = 0;
1441 sak = participant->new_key;
1442 body->confid_offset = sak->confidentiality_offset;
1443 body->dan = sak->an;
1444 body->kn = host_to_be32(sak->key_identifier.kn);
1445 cs_index = participant->kay->macsec_csindex;
1447 if (cs_index != DEFAULT_CS_INDEX) {
1450 cs = host_to_be64(cipher_suite_tbl[cs_index].id);
1451 os_memcpy(body->sak, &cs, CS_ID_LEN);
1452 sak_pos = CS_ID_LEN;
1454 if (aes_wrap(participant->kek.key, 16,
1455 cipher_suite_tbl[cs_index].sak_len / 8,
1456 sak->key, body->sak + sak_pos)) {
1457 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1461 ieee802_1x_mka_dump_dist_sak_body(body);
1468 * ieee802_1x_kay_init_data_key -
1470 static struct data_key *
1471 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1473 struct data_key *pkey;
1478 pkey = os_zalloc(sizeof(*pkey));
1480 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1484 pkey->key = os_zalloc(conf->key_len);
1485 if (pkey->key == NULL) {
1486 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1491 os_memcpy(pkey->key, conf->key, conf->key_len);
1492 os_memcpy(&pkey->key_identifier, &conf->ki,
1493 sizeof(pkey->key_identifier));
1494 pkey->confidentiality_offset = conf->offset;
1495 pkey->an = conf->an;
1496 pkey->transmits = conf->tx;
1497 pkey->receives = conf->rx;
1498 os_get_time(&pkey->created_time);
1507 * ieee802_1x_kay_decode_dist_sak_body -
1510 ieee802_1x_mka_decode_dist_sak_body(
1511 struct ieee802_1x_mka_participant *participant,
1512 const u8 *mka_msg, size_t msg_len)
1514 struct ieee802_1x_mka_hdr *hdr;
1515 struct ieee802_1x_mka_dist_sak_body *body;
1516 struct ieee802_1x_kay_peer *peer;
1517 struct macsec_ciphersuite *cs;
1519 struct key_conf *conf;
1520 struct data_key *sa_key = NULL;
1521 struct ieee802_1x_mka_ki sak_ki;
1525 struct ieee802_1x_kay *kay = participant->kay;
1527 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1528 body_len = get_mka_param_body_len(hdr);
1529 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1530 wpa_printf(MSG_ERROR,
1531 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1536 if (!participant->principal) {
1537 wpa_printf(MSG_ERROR,
1538 "KaY: I can't accept the distributed SAK as I am not principal");
1541 if (participant->is_key_server) {
1542 wpa_printf(MSG_ERROR,
1543 "KaY: I can't accept the distributed SAK as myself is key server ");
1546 if (!kay->macsec_desired ||
1547 kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1548 wpa_printf(MSG_ERROR,
1549 "KaY: I am not MACsec-desired or without MACsec capable");
1553 peer = ieee802_1x_kay_get_live_peer(participant,
1554 participant->current_peer_id.mi);
1556 wpa_printf(MSG_ERROR,
1557 "KaY: The key server is not in my live peers list");
1560 if (!sci_equal(&kay->key_server_sci, &peer->sci)) {
1561 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1565 if (body_len == 0) {
1566 kay->authenticated = TRUE;
1567 kay->secured = FALSE;
1568 kay->failed = FALSE;
1569 participant->advised_desired = FALSE;
1570 ieee802_1x_cp_connect_authenticated(kay->cp);
1571 ieee802_1x_cp_sm_step(kay->cp);
1572 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1573 participant->to_use_sak = TRUE;
1577 participant->advised_desired = TRUE;
1578 kay->authenticated = FALSE;
1579 kay->secured = TRUE;
1580 kay->failed = FALSE;
1581 ieee802_1x_cp_connect_secure(kay->cp);
1582 ieee802_1x_cp_sm_step(kay->cp);
1584 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1585 ieee802_1x_mka_dump_dist_sak_body(body);
1586 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1588 if (os_memcmp(sa_key->key_identifier.mi,
1589 participant->current_peer_id.mi, MI_LEN) == 0 &&
1590 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1591 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1596 if (body_len == 28) {
1597 sak_len = DEFAULT_SA_KEY_LEN;
1598 wrap_sak = body->sak;
1599 kay->macsec_csindex = DEFAULT_CS_INDEX;
1601 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1603 wpa_printf(MSG_ERROR,
1604 "KaY: I can't support the Cipher Suite advised by key server");
1607 sak_len = cs->sak_len;
1608 wrap_sak = body->sak + CS_ID_LEN;
1609 kay->macsec_csindex = cs->index;
1612 unwrap_sak = os_zalloc(sak_len);
1614 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1617 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1619 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1620 os_free(unwrap_sak);
1623 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1625 conf = os_zalloc(sizeof(*conf));
1627 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1628 os_free(unwrap_sak);
1631 conf->key_len = sak_len;
1633 conf->key = os_zalloc(conf->key_len);
1635 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1636 os_free(unwrap_sak);
1641 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1643 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1645 sak_ki.kn = be_to_host32(body->kn);
1647 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1648 conf->ki.kn = sak_ki.kn;
1649 conf->an = body->dan;
1650 conf->offset = body->confid_offset;
1654 sa_key = ieee802_1x_kay_init_data_key(conf);
1656 os_free(unwrap_sak);
1662 dl_list_add(&participant->sak_list, &sa_key->list);
1664 ieee802_1x_cp_set_ciphersuite(kay->cp,
1665 cipher_suite_tbl[kay->macsec_csindex].id);
1666 ieee802_1x_cp_sm_step(kay->cp);
1667 ieee802_1x_cp_set_offset(kay->cp, body->confid_offset);
1668 ieee802_1x_cp_sm_step(kay->cp);
1669 ieee802_1x_cp_set_distributedki(kay->cp, &sak_ki);
1670 ieee802_1x_cp_set_distributedan(kay->cp, body->dan);
1671 ieee802_1x_cp_signal_newsak(kay->cp);
1672 ieee802_1x_cp_sm_step(kay->cp);
1674 participant->to_use_sak = TRUE;
1676 os_free(unwrap_sak);
1685 * ieee802_1x_mka_icv_body_present
1688 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1695 * ieee802_1x_kay_get_icv_length
1698 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1702 length = sizeof(struct ieee802_1x_mka_icv_body);
1703 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1705 return MKA_ALIGN_LENGTH(length);
1710 * ieee802_1x_mka_encode_icv_body -
1713 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1716 struct ieee802_1x_mka_icv_body *body;
1717 unsigned int length;
1718 u8 cmac[MAX_ICV_LEN];
1720 length = ieee802_1x_mka_get_icv_length(participant);
1721 if (length != DEFAULT_ICV_LEN) {
1722 body = wpabuf_put(buf, MKA_HDR_LEN);
1723 body->type = MKA_ICV_INDICATOR;
1724 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1727 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1728 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1729 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1733 if (length != DEFAULT_ICV_LEN)
1734 length -= MKA_HDR_LEN;
1735 os_memcpy(wpabuf_put(buf, length), cmac, length);
1741 * ieee802_1x_mka_decode_icv_body -
1744 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1745 const u8 *mka_msg, size_t msg_len)
1747 struct ieee802_1x_mka_hdr *hdr;
1748 struct ieee802_1x_mka_icv_body *body;
1756 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1757 hdr = (struct ieee802_1x_mka_hdr *) pos;
1758 body_len = get_mka_param_body_len(hdr);
1759 body_type = get_mka_param_body_type(hdr);
1761 if (left_len < (body_len + MKA_HDR_LEN))
1764 if (body_type != MKA_ICV_INDICATOR) {
1765 left_len -= MKA_HDR_LEN + body_len;
1766 pos += MKA_HDR_LEN + body_len;
1770 body = (struct ieee802_1x_mka_icv_body *)pos;
1772 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1779 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1784 * ieee802_1x_mka_decode_dist_cak_body-
1787 ieee802_1x_mka_decode_dist_cak_body(
1788 struct ieee802_1x_mka_participant *participant,
1789 const u8 *mka_msg, size_t msg_len)
1791 struct ieee802_1x_mka_hdr *hdr;
1794 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1795 body_len = get_mka_param_body_len(hdr);
1796 if (body_len < 28) {
1797 wpa_printf(MSG_ERROR,
1798 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
1808 * ieee802_1x_mka_decode_kmd_body -
1811 ieee802_1x_mka_decode_kmd_body(
1812 struct ieee802_1x_mka_participant *participant,
1813 const u8 *mka_msg, size_t msg_len)
1815 struct ieee802_1x_mka_hdr *hdr;
1818 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1819 body_len = get_mka_param_body_len(hdr);
1821 wpa_printf(MSG_ERROR,
1822 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
1832 * ieee802_1x_mka_decode_announce_body -
1834 static int ieee802_1x_mka_decode_announce_body(
1835 struct ieee802_1x_mka_participant *participant,
1836 const u8 *mka_msg, size_t msg_len)
1842 struct mka_param_body_handler {
1843 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
1844 struct wpabuf *buf);
1845 int (*body_rx)(struct ieee802_1x_mka_participant *participant,
1846 const u8 *mka_msg, size_t msg_len);
1847 int (*body_length)(struct ieee802_1x_mka_participant *participant);
1848 Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
1852 static struct mka_param_body_handler mka_body_handler[] = {
1853 /* basic parameter set */
1855 .body_tx = ieee802_1x_mka_encode_basic_body,
1857 .body_length = ieee802_1x_mka_basic_body_length,
1858 .body_present = ieee802_1x_mka_basic_body_present
1861 /* live peer list parameter set */
1863 .body_tx = ieee802_1x_mka_encode_live_peer_body,
1864 .body_rx = ieee802_1x_mka_decode_live_peer_body,
1865 .body_length = ieee802_1x_mka_get_live_peer_length,
1866 .body_present = ieee802_1x_mka_live_peer_body_present
1869 /* potential peer list parameter set */
1871 .body_tx = ieee802_1x_mka_encode_potential_peer_body,
1872 .body_rx = ieee802_1x_mka_decode_potential_peer_body,
1873 .body_length = ieee802_1x_mka_get_potential_peer_length,
1874 .body_present = ieee802_1x_mka_potential_peer_body_present
1877 /* sak use parameter set */
1879 .body_tx = ieee802_1x_mka_encode_sak_use_body,
1880 .body_rx = ieee802_1x_mka_decode_sak_use_body,
1881 .body_length = ieee802_1x_mka_get_sak_use_length,
1882 .body_present = ieee802_1x_mka_sak_use_body_present
1885 /* distribute sak parameter set */
1887 .body_tx = ieee802_1x_mka_encode_dist_sak_body,
1888 .body_rx = ieee802_1x_mka_decode_dist_sak_body,
1889 .body_length = ieee802_1x_mka_get_dist_sak_length,
1890 .body_present = ieee802_1x_mka_dist_sak_body_present
1893 /* distribute cak parameter set */
1896 .body_rx = ieee802_1x_mka_decode_dist_cak_body,
1897 .body_length = NULL,
1898 .body_present = NULL
1901 /* kmd parameter set */
1904 .body_rx = ieee802_1x_mka_decode_kmd_body,
1905 .body_length = NULL,
1906 .body_present = NULL
1909 /* announce parameter set */
1912 .body_rx = ieee802_1x_mka_decode_announce_body,
1913 .body_length = NULL,
1914 .body_present = NULL
1917 /* icv parameter set */
1919 .body_tx = ieee802_1x_mka_encode_icv_body,
1921 .body_length = ieee802_1x_mka_get_icv_length,
1922 .body_present = ieee802_1x_mka_icv_body_present
1928 * ieee802_1x_kay_deinit_data_key -
1930 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1939 dl_list_del(&pkey->list);
1946 * ieee802_1x_kay_generate_new_sak -
1949 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1951 struct data_key *sa_key = NULL;
1952 struct key_conf *conf;
1953 struct ieee802_1x_kay_peer *peer;
1954 struct ieee802_1x_kay *kay = participant->kay;
1955 int ctx_len, ctx_offset;
1958 /* check condition for generating a fresh SAK:
1959 * must have one live peer
1960 * and MKA life time elapse since last distribution
1961 * or potential peer is empty
1963 if (dl_list_empty(&participant->live_peers)) {
1964 wpa_printf(MSG_ERROR,
1965 "KaY: Live peers list must not empty when generating fresh SAK");
1969 /* FIXME: A fresh SAK not generated until
1970 * the live peer list contains at least one peer and
1971 * MKA life time has elapsed since the prior SAK was first distributed,
1972 * or the Key server's potential peer is empty
1973 * but I can't understand the second item, so
1974 * here only check first item and ingore
1975 * && (!dl_list_empty(&participant->potential_peers))) {
1977 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1978 wpa_printf(MSG_ERROR,
1979 "KaY: Life time have not elapsed since prior SAK distributed");
1983 conf = os_zalloc(sizeof(*conf));
1985 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1988 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1990 conf->key = os_zalloc(conf->key_len);
1993 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1997 ctx_len = conf->key_len + sizeof(kay->dist_kn);
1998 dl_list_for_each(peer, &participant->live_peers,
1999 struct ieee802_1x_kay_peer, list)
2000 ctx_len += sizeof(peer->mi);
2001 ctx_len += sizeof(participant->mi);
2003 context = os_zalloc(ctx_len);
2010 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2016 ctx_offset += conf->key_len;
2017 dl_list_for_each(peer, &participant->live_peers,
2018 struct ieee802_1x_kay_peer, list) {
2019 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2020 ctx_offset += sizeof(peer->mi);
2022 os_memcpy(context + ctx_offset, participant->mi,
2023 sizeof(participant->mi));
2024 ctx_offset += sizeof(participant->mi);
2025 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2027 if (conf->key_len == 16) {
2028 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2029 context, ctx_len, conf->key);
2030 } else if (conf->key_len == 32) {
2031 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2032 context, ctx_len, conf->key);
2034 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2040 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2041 conf->key, conf->key_len);
2043 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2044 conf->ki.kn = kay->dist_kn;
2045 conf->an = kay->dist_an;
2046 conf->offset = kay->macsec_confidentiality;
2050 sa_key = ieee802_1x_kay_init_data_key(conf);
2057 participant->new_key = sa_key;
2059 dl_list_add(&participant->sak_list, &sa_key->list);
2060 ieee802_1x_cp_set_ciphersuite(kay->cp,
2061 cipher_suite_tbl[kay->macsec_csindex].id);
2062 ieee802_1x_cp_sm_step(kay->cp);
2063 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2064 ieee802_1x_cp_sm_step(kay->cp);
2065 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2066 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2067 ieee802_1x_cp_signal_newsak(kay->cp);
2068 ieee802_1x_cp_sm_step(kay->cp);
2070 dl_list_for_each(peer, &participant->live_peers,
2071 struct ieee802_1x_kay_peer, list)
2072 peer->sak_used = FALSE;
2076 if (kay->dist_an > 3)
2079 kay->dist_time = time(NULL);
2088 static int compare_priorities(const struct ieee802_1x_kay_peer *peer,
2089 const struct ieee802_1x_kay_peer *other)
2091 if (peer->key_server_priority < other->key_server_priority)
2093 if (other->key_server_priority < peer->key_server_priority)
2096 return os_memcmp(peer->sci.addr, other->sci.addr, ETH_ALEN);
2101 * ieee802_1x_kay_elect_key_server - elect the key server
2102 * when to elect: whenever the live peers list changes
2105 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2107 struct ieee802_1x_kay_peer *peer;
2108 struct ieee802_1x_kay_peer *key_server = NULL;
2109 struct ieee802_1x_kay *kay = participant->kay;
2110 Boolean i_is_key_server;
2112 if (participant->is_obliged_key_server) {
2113 participant->new_sak = TRUE;
2114 participant->to_dist_sak = FALSE;
2115 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2119 /* elect the key server among the peers */
2120 dl_list_for_each(peer, &participant->live_peers,
2121 struct ieee802_1x_kay_peer, list) {
2122 if (!peer->is_key_server)
2130 if (compare_priorities(peer, key_server) < 0)
2134 /* elect the key server between me and the above elected peer */
2135 i_is_key_server = FALSE;
2136 if (key_server && participant->can_be_key_server) {
2137 struct ieee802_1x_kay_peer tmp;
2139 tmp.key_server_priority = kay->actor_priority;
2140 os_memcpy(&tmp.sci, &kay->actor_sci, sizeof(tmp.sci));
2141 if (compare_priorities(&tmp, key_server) < 0)
2142 i_is_key_server = TRUE;
2143 } else if (participant->can_be_key_server) {
2144 i_is_key_server = TRUE;
2147 if (i_is_key_server) {
2148 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2149 if (!sci_equal(&kay->key_server_sci, &kay->actor_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 (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2167 ieee802_1x_cp_signal_chgdserver(kay->cp);
2168 ieee802_1x_cp_sm_step(kay->cp);
2171 participant->is_key_server = FALSE;
2172 participant->principal = TRUE;
2173 participant->is_elected = TRUE;
2175 os_memcpy(&kay->key_server_sci, &key_server->sci,
2176 sizeof(kay->key_server_sci));
2177 kay->key_server_priority = key_server->key_server_priority;
2179 participant->principal = FALSE;
2180 participant->is_key_server = FALSE;
2181 participant->is_elected = FALSE;
2189 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2190 * how to use MACsec: whether use MACsec and its capability
2191 * protectFrames will be advised if the key server and one of its live peers are
2192 * MACsec capable and one of those request MACsec protection
2195 ieee802_1x_kay_decide_macsec_use(
2196 struct ieee802_1x_mka_participant *participant)
2198 struct ieee802_1x_kay *kay = participant->kay;
2199 struct ieee802_1x_kay_peer *peer;
2200 enum macsec_cap less_capability;
2203 if (!participant->is_key_server)
2206 /* key server self is MACsec-desired and requesting MACsec */
2207 if (!kay->macsec_desired) {
2208 participant->advised_desired = FALSE;
2211 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2212 participant->advised_desired = FALSE;
2215 less_capability = kay->macsec_capable;
2217 /* at least one of peers is MACsec-desired and requesting MACsec */
2219 dl_list_for_each(peer, &participant->live_peers,
2220 struct ieee802_1x_kay_peer, list) {
2221 if (!peer->macsec_desired)
2224 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2227 less_capability = (less_capability < peer->macsec_capability) ?
2228 less_capability : peer->macsec_capability;
2233 participant->advised_desired = TRUE;
2234 participant->advised_capability = less_capability;
2235 kay->authenticated = FALSE;
2236 kay->secured = TRUE;
2237 kay->failed = FALSE;
2238 ieee802_1x_cp_connect_secure(kay->cp);
2239 ieee802_1x_cp_sm_step(kay->cp);
2241 participant->advised_desired = FALSE;
2242 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2243 participant->to_use_sak = FALSE;
2244 kay->authenticated = TRUE;
2245 kay->secured = FALSE;
2246 kay->failed = FALSE;
2255 ieee802_1x_cp_connect_authenticated(kay->cp);
2256 ieee802_1x_cp_sm_step(kay->cp);
2262 static const u8 pae_group_addr[ETH_ALEN] = {
2263 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2268 * ieee802_1x_kay_encode_mkpdu -
2271 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2272 struct wpabuf *pbuf)
2275 struct ieee8023_hdr *ether_hdr;
2276 struct ieee802_1x_hdr *eapol_hdr;
2278 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2279 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2280 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2281 sizeof(ether_hdr->dest));
2282 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2284 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2285 eapol_hdr->version = EAPOL_VERSION;
2286 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2287 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2289 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2290 if (mka_body_handler[i].body_present &&
2291 mka_body_handler[i].body_present(participant)) {
2292 if (mka_body_handler[i].body_tx(participant, pbuf))
2301 * ieee802_1x_participant_send_mkpdu -
2304 ieee802_1x_participant_send_mkpdu(
2305 struct ieee802_1x_mka_participant *participant)
2308 struct ieee802_1x_kay *kay = participant->kay;
2312 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2313 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2314 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2315 if (mka_body_handler[i].body_present &&
2316 mka_body_handler[i].body_present(participant))
2317 length += mka_body_handler[i].body_length(participant);
2320 buf = wpabuf_alloc(length);
2322 wpa_printf(MSG_ERROR, "KaY: out of memory");
2326 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2327 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2331 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2335 participant->active = TRUE;
2341 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2343 * ieee802_1x_participant_timer -
2345 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2347 struct ieee802_1x_mka_participant *participant;
2348 struct ieee802_1x_kay *kay;
2349 struct ieee802_1x_kay_peer *peer, *pre_peer;
2350 time_t now = time(NULL);
2352 struct receive_sc *rxsc, *pre_rxsc;
2353 struct transmit_sa *txsa, *pre_txsa;
2355 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2356 kay = participant->kay;
2357 if (participant->cak_life) {
2358 if (now > participant->cak_life)
2362 /* should delete MKA instance if there are not live peers
2363 * when the MKA life elapsed since its creating */
2364 if (participant->mka_life) {
2365 if (dl_list_empty(&participant->live_peers)) {
2366 if (now > participant->mka_life)
2369 participant->mka_life = 0;
2374 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2375 struct ieee802_1x_kay_peer, list) {
2376 if (now > peer->expire) {
2377 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2378 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2380 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2381 dl_list_for_each_safe(rxsc, pre_rxsc,
2382 &participant->rxsc_list,
2383 struct receive_sc, list) {
2384 if (sci_equal(&rxsc->sci, &peer->sci)) {
2385 secy_delete_receive_sc(kay, rxsc);
2386 ieee802_1x_kay_deinit_receive_sc(
2390 dl_list_del(&peer->list);
2397 if (dl_list_empty(&participant->live_peers)) {
2398 participant->advised_desired = FALSE;
2399 participant->advised_capability =
2400 MACSEC_CAP_NOT_IMPLEMENTED;
2401 participant->to_use_sak = FALSE;
2402 kay->authenticated = TRUE;
2403 kay->secured = FALSE;
2404 kay->failed = FALSE;
2413 dl_list_for_each_safe(txsa, pre_txsa,
2414 &participant->txsc->sa_list,
2415 struct transmit_sa, list) {
2416 secy_disable_transmit_sa(kay, txsa);
2417 ieee802_1x_kay_deinit_transmit_sa(txsa);
2420 ieee802_1x_cp_connect_authenticated(kay->cp);
2421 ieee802_1x_cp_sm_step(kay->cp);
2423 ieee802_1x_kay_elect_key_server(participant);
2424 ieee802_1x_kay_decide_macsec_use(participant);
2428 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2429 struct ieee802_1x_kay_peer, list) {
2430 if (now > peer->expire) {
2431 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2432 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2434 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2435 dl_list_del(&peer->list);
2440 if (participant->new_sak) {
2441 if (!ieee802_1x_kay_generate_new_sak(participant))
2442 participant->to_dist_sak = TRUE;
2444 participant->new_sak = FALSE;
2447 if (participant->retry_count < MAX_RETRY_CNT) {
2448 ieee802_1x_participant_send_mkpdu(participant);
2449 participant->retry_count++;
2452 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2453 ieee802_1x_participant_timer,
2459 kay->authenticated = FALSE;
2460 kay->secured = FALSE;
2462 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2467 * ieee802_1x_kay_init_transmit_sa -
2469 static struct transmit_sa *
2470 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2471 struct data_key *key)
2473 struct transmit_sa *psa;
2475 key->tx_latest = TRUE;
2476 key->rx_latest = TRUE;
2478 psa = os_zalloc(sizeof(*psa));
2480 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2484 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2485 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2486 psa->confidentiality = TRUE;
2488 psa->confidentiality = FALSE;
2492 psa->next_pn = next_PN;
2495 os_get_time(&psa->created_time);
2496 psa->in_use = FALSE;
2498 dl_list_add(&psc->sa_list, &psa->list);
2499 wpa_printf(MSG_DEBUG,
2500 "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC(channel: %d)",
2501 an, next_PN, psc->channel);
2508 * ieee802_1x_kay_deinit_transmit_sa -
2510 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2513 wpa_printf(MSG_DEBUG,
2514 "KaY: Delete transmit SA(an: %hhu) of SC",
2516 dl_list_del(&psa->list);
2522 * init_transmit_sc -
2524 static struct transmit_sc *
2525 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2528 struct transmit_sc *psc;
2530 psc = os_zalloc(sizeof(*psc));
2532 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2535 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2536 psc->channel = channel;
2538 os_get_time(&psc->created_time);
2539 psc->transmitting = FALSE;
2540 psc->encoding_sa = FALSE;
2541 psc->enciphering_sa = FALSE;
2543 dl_list_init(&psc->sa_list);
2544 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2545 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2552 * ieee802_1x_kay_deinit_transmit_sc -
2555 ieee802_1x_kay_deinit_transmit_sc(
2556 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2558 struct transmit_sa *psa, *tmp;
2560 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2562 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2564 secy_disable_transmit_sa(participant->kay, psa);
2565 ieee802_1x_kay_deinit_transmit_sa(psa);
2572 /****************** Interface between CP and KAY *********************/
2574 * ieee802_1x_kay_set_latest_sa_attr -
2576 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2577 struct ieee802_1x_mka_ki *lki, u8 lan,
2578 Boolean ltx, Boolean lrx)
2580 struct ieee802_1x_mka_participant *principal;
2582 principal = ieee802_1x_kay_get_principal_participant(kay);
2587 os_memset(&principal->lki, 0, sizeof(principal->lki));
2589 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2591 principal->lan = lan;
2592 principal->ltx = ltx;
2593 principal->lrx = lrx;
2598 kay->ltx_kn = lki->kn;
2599 kay->lrx_kn = lki->kn;
2609 * ieee802_1x_kay_set_old_sa_attr -
2611 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2612 struct ieee802_1x_mka_ki *oki,
2613 u8 oan, Boolean otx, Boolean orx)
2615 struct ieee802_1x_mka_participant *principal;
2617 principal = ieee802_1x_kay_get_principal_participant(kay);
2622 os_memset(&principal->oki, 0, sizeof(principal->oki));
2624 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2626 principal->oan = oan;
2627 principal->otx = otx;
2628 principal->orx = orx;
2634 kay->otx_kn = oki->kn;
2635 kay->orx_kn = oki->kn;
2645 * ieee802_1x_kay_create_sas -
2647 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2648 struct ieee802_1x_mka_ki *lki)
2650 struct data_key *sa_key, *latest_sak;
2651 struct ieee802_1x_mka_participant *principal;
2652 struct receive_sc *rxsc;
2653 struct receive_sa *rxsa;
2654 struct transmit_sa *txsa;
2656 principal = ieee802_1x_kay_get_principal_participant(kay);
2661 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2662 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2663 sa_key->rx_latest = TRUE;
2664 sa_key->tx_latest = TRUE;
2665 latest_sak = sa_key;
2666 principal->to_use_sak = TRUE;
2668 sa_key->rx_latest = FALSE;
2669 sa_key->tx_latest = FALSE;
2673 wpa_printf(MSG_ERROR, "lki related sak not found");
2677 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2678 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2683 secy_create_receive_sa(kay, rxsa);
2686 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2691 secy_create_transmit_sa(kay, txsa);
2700 * ieee802_1x_kay_delete_sas -
2702 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2703 struct ieee802_1x_mka_ki *ki)
2705 struct data_key *sa_key, *pre_key;
2706 struct transmit_sa *txsa, *pre_txsa;
2707 struct receive_sa *rxsa, *pre_rxsa;
2708 struct receive_sc *rxsc;
2709 struct ieee802_1x_mka_participant *principal;
2711 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2712 principal = ieee802_1x_kay_get_principal_participant(kay);
2716 /* remove the transmit sa */
2717 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2718 struct transmit_sa, list) {
2719 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2720 secy_disable_transmit_sa(kay, txsa);
2721 ieee802_1x_kay_deinit_transmit_sa(txsa);
2725 /* remove the receive sa */
2726 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2727 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2728 struct receive_sa, list) {
2729 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2730 secy_disable_receive_sa(kay, rxsa);
2731 ieee802_1x_kay_deinit_receive_sa(rxsa);
2736 /* remove the sak */
2737 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2738 struct data_key, list) {
2739 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2740 ieee802_1x_kay_deinit_data_key(sa_key);
2743 if (principal->new_key == sa_key)
2744 principal->new_key = NULL;
2752 * ieee802_1x_kay_enable_tx_sas -
2754 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2755 struct ieee802_1x_mka_ki *lki)
2757 struct ieee802_1x_mka_participant *principal;
2758 struct transmit_sa *txsa;
2760 principal = ieee802_1x_kay_get_principal_participant(kay);
2764 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2766 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2767 txsa->in_use = TRUE;
2768 secy_enable_transmit_sa(kay, txsa);
2769 ieee802_1x_cp_set_usingtransmitas(
2770 principal->kay->cp, TRUE);
2771 ieee802_1x_cp_sm_step(principal->kay->cp);
2780 * ieee802_1x_kay_enable_rx_sas -
2782 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2783 struct ieee802_1x_mka_ki *lki)
2785 struct ieee802_1x_mka_participant *principal;
2786 struct receive_sa *rxsa;
2787 struct receive_sc *rxsc;
2789 principal = ieee802_1x_kay_get_principal_participant(kay);
2793 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2794 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2796 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2797 rxsa->in_use = TRUE;
2798 secy_enable_receive_sa(kay, rxsa);
2799 ieee802_1x_cp_set_usingreceivesas(
2800 principal->kay->cp, TRUE);
2801 ieee802_1x_cp_sm_step(principal->kay->cp);
2811 * ieee802_1x_kay_enable_new_info -
2813 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2815 struct ieee802_1x_mka_participant *principal;
2817 principal = ieee802_1x_kay_get_principal_participant(kay);
2821 if (principal->retry_count < MAX_RETRY_CNT) {
2822 ieee802_1x_participant_send_mkpdu(principal);
2823 principal->retry_count++;
2831 * ieee802_1x_kay_cp_conf -
2833 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2834 struct ieee802_1x_cp_conf *pconf)
2836 pconf->protect = kay->macsec_protect;
2837 pconf->replay_protect = kay->macsec_replay_protect;
2838 pconf->validate = kay->macsec_validate;
2845 * ieee802_1x_kay_alloc_cp_sm -
2847 static struct ieee802_1x_cp_sm *
2848 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2850 struct ieee802_1x_cp_conf conf;
2852 os_memset(&conf, 0, sizeof(conf));
2853 conf.protect = kay->macsec_protect;
2854 conf.replay_protect = kay->macsec_replay_protect;
2855 conf.validate = kay->macsec_validate;
2856 conf.replay_window = kay->macsec_replay_window;
2858 return ieee802_1x_cp_sm_init(kay, &conf);
2863 * ieee802_1x_kay_mkpdu_sanity_check -
2864 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2866 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2867 const u8 *buf, size_t len)
2869 struct ieee8023_hdr *eth_hdr;
2870 struct ieee802_1x_hdr *eapol_hdr;
2871 struct ieee802_1x_mka_hdr *mka_hdr;
2872 struct ieee802_1x_mka_basic_body *body;
2874 struct ieee802_1x_mka_participant *participant;
2876 u8 icv[MAX_ICV_LEN];
2879 eth_hdr = (struct ieee8023_hdr *) buf;
2880 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2881 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2883 /* destination address should be not individual address */
2884 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2885 wpa_printf(MSG_MSGDUMP,
2886 "KaY: ethernet destination address is not PAE group address");
2890 /* MKPDU should not be less than 32 octets */
2891 mka_msg_len = be_to_host16(eapol_hdr->length);
2892 if (mka_msg_len < 32) {
2893 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2896 /* MKPDU should be a multiple of 4 octets */
2897 if ((mka_msg_len % 4) != 0) {
2898 wpa_printf(MSG_MSGDUMP,
2899 "KaY: MKPDU is not multiple of 4 octets");
2903 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2904 ieee802_1x_mka_dump_basic_body(body);
2905 body_len = get_mka_param_body_len(body);
2906 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2907 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2908 wpa_printf(MSG_ERROR,
2909 "KaY: Received EAPOL-MKA Packet Body Length (%zu bytes) is less than the Basic Parameter Set Header Length (%zu bytes) + the Basic Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
2910 mka_msg_len, MKA_HDR_LEN,
2911 body_len, DEFAULT_ICV_LEN);
2915 /* CKN should be owned by I */
2916 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2918 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2922 /* algorithm agility check */
2923 if (os_memcmp(body->algo_agility, mka_algo_agility,
2924 sizeof(body->algo_agility)) != 0) {
2925 wpa_printf(MSG_ERROR,
2926 "KaY: peer's algorithm agility not supported for me");
2932 * The ICV will comprise the final octets of the packet body, whatever
2933 * its size, not the fixed length 16 octets, indicated by the EAPOL
2934 * packet body length.
2936 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2937 participant->ick.key,
2938 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2939 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2943 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2946 wpa_printf(MSG_ERROR, "KaY: No ICV");
2949 if (os_memcmp_const(msg_icv, icv,
2950 mka_alg_tbl[kay->mka_algindex].icv_len) != 0) {
2951 wpa_printf(MSG_ERROR,
2952 "KaY: Computed ICV is not equal to Received ICV");
2961 * ieee802_1x_kay_decode_mkpdu -
2963 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2964 const u8 *buf, size_t len)
2966 struct ieee802_1x_mka_participant *participant;
2967 struct ieee802_1x_mka_hdr *hdr;
2973 Boolean handled[256];
2975 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2978 /* handle basic parameter set */
2979 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2980 left_len = len - sizeof(struct ieee8023_hdr) -
2981 sizeof(struct ieee802_1x_hdr);
2982 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2986 /* to skip basic parameter set */
2987 hdr = (struct ieee802_1x_mka_hdr *) pos;
2988 body_len = get_mka_param_body_len(hdr);
2989 pos += body_len + MKA_HDR_LEN;
2990 left_len -= body_len + MKA_HDR_LEN;
2992 /* check i am in the peer's peer list */
2993 if (ieee802_1x_mka_i_in_peerlist(participant, pos, left_len) &&
2994 !ieee802_1x_kay_is_in_live_peer(participant,
2995 participant->current_peer_id.mi)) {
2996 /* accept the peer as live peer */
2997 if (ieee802_1x_kay_is_in_potential_peer(
2998 participant, participant->current_peer_id.mi)) {
2999 if (!ieee802_1x_kay_move_live_peer(
3001 participant->current_peer_id.mi,
3002 be_to_host32(participant->
3003 current_peer_id.mn)))
3005 } else if (!ieee802_1x_kay_create_live_peer(
3006 participant, participant->current_peer_id.mi,
3007 be_to_host32(participant->
3008 current_peer_id.mn))) {
3012 ieee802_1x_kay_elect_key_server(participant);
3013 ieee802_1x_kay_decide_macsec_use(participant);
3017 * Handle other parameter set than basic parameter set.
3018 * Each parameter set should be present only once.
3020 for (i = 0; i < 256; i++)
3024 for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
3025 pos += body_len + MKA_HDR_LEN,
3026 left_len -= body_len + MKA_HDR_LEN) {
3027 hdr = (struct ieee802_1x_mka_hdr *) pos;
3028 body_len = get_mka_param_body_len(hdr);
3029 body_type = get_mka_param_body_type(hdr);
3031 if (body_type == MKA_ICV_INDICATOR)
3034 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3035 wpa_printf(MSG_ERROR,
3036 "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
3037 left_len, MKA_HDR_LEN,
3038 body_len, DEFAULT_ICV_LEN);
3042 if (handled[body_type])
3045 handled[body_type] = TRUE;
3046 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3047 mka_body_handler[body_type].body_rx) {
3048 mka_body_handler[body_type].body_rx
3049 (participant, pos, left_len);
3051 wpa_printf(MSG_ERROR,
3052 "The type %d is not supported in this MKA version %d",
3053 body_type, MKA_VERSION_ID);
3058 participant->retry_count = 0;
3059 participant->active = TRUE;
3066 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3069 struct ieee802_1x_kay *kay = ctx;
3070 struct ieee8023_hdr *eth_hdr;
3071 struct ieee802_1x_hdr *eapol_hdr;
3073 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3074 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3075 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3076 (unsigned long) len);
3080 eth_hdr = (struct ieee8023_hdr *) buf;
3081 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3082 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3083 be_to_host16(eapol_hdr->length)) {
3084 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3085 (unsigned long) len,
3086 (unsigned long) be_to_host16(eapol_hdr->length));
3090 if (eapol_hdr->version < EAPOL_VERSION) {
3091 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3092 eapol_hdr->version);
3095 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3096 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3099 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3100 if (dl_list_empty(&kay->participant_list)) {
3101 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3105 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3110 * ieee802_1x_kay_init -
3112 struct ieee802_1x_kay *
3113 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3114 const char *ifname, const u8 *addr)
3116 struct ieee802_1x_kay *kay;
3118 kay = os_zalloc(sizeof(*kay));
3120 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3127 kay->active = FALSE;
3129 kay->authenticated = FALSE;
3130 kay->secured = FALSE;
3131 kay->failed = FALSE;
3132 kay->policy = policy;
3134 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3135 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3136 kay->actor_sci.port = host_to_be16(0x0001);
3137 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3139 /* While actor acts as a key server, shall distribute sakey */
3144 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3145 kay->macsec_csindex = DEFAULT_CS_INDEX;
3146 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3147 kay->mka_version = MKA_VERSION_ID;
3149 os_memcpy(kay->algo_agility, mka_algo_agility,
3150 sizeof(kay->algo_agility));
3152 dl_list_init(&kay->participant_list);
3154 if (policy == DO_NOT_SECURE) {
3155 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3156 kay->macsec_desired = FALSE;
3157 kay->macsec_protect = FALSE;
3158 kay->macsec_validate = Disabled;
3159 kay->macsec_replay_protect = FALSE;
3160 kay->macsec_replay_window = 0;
3161 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3163 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3164 kay->macsec_desired = TRUE;
3165 kay->macsec_protect = TRUE;
3166 kay->macsec_validate = Strict;
3167 kay->macsec_replay_protect = FALSE;
3168 kay->macsec_replay_window = 0;
3169 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3172 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3174 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3175 secy_init_macsec(kay);
3176 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3178 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3181 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3182 if (kay->cp == NULL) {
3183 ieee802_1x_kay_deinit(kay);
3187 if (policy == DO_NOT_SECURE) {
3188 ieee802_1x_cp_connect_authenticated(kay->cp);
3189 ieee802_1x_cp_sm_step(kay->cp);
3191 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3192 kay_l2_receive, kay, 1);
3193 if (kay->l2_mka == NULL) {
3194 wpa_printf(MSG_WARNING,
3195 "KaY: Failed to initialize L2 packet processing for MKA packet");
3196 ieee802_1x_kay_deinit(kay);
3206 * ieee802_1x_kay_deinit -
3209 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3211 struct ieee802_1x_mka_participant *participant;
3216 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3218 while (!dl_list_empty(&kay->participant_list)) {
3219 participant = dl_list_entry(kay->participant_list.next,
3220 struct ieee802_1x_mka_participant,
3222 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3225 ieee802_1x_cp_sm_deinit(kay->cp);
3226 secy_deinit_macsec(kay);
3229 l2_packet_deinit(kay->l2_mka);
3239 * ieee802_1x_kay_create_mka -
3241 struct ieee802_1x_mka_participant *
3242 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3243 struct mka_key *cak, u32 life,
3244 enum mka_created_mode mode, Boolean is_authenticator)
3246 struct ieee802_1x_mka_participant *participant;
3249 if (!kay || !ckn || !cak) {
3250 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3254 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3255 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3258 if (ckn->len > MAX_CKN_LEN) {
3259 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3263 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3267 participant = os_zalloc(sizeof(*participant));
3269 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3273 participant->ckn.len = ckn->len;
3274 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3275 participant->cak.len = cak->len;
3276 os_memcpy(participant->cak.key, cak->key, cak->len);
3278 participant->cak_life = life + time(NULL);
3282 if (is_authenticator) {
3283 participant->is_obliged_key_server = TRUE;
3284 participant->can_be_key_server = TRUE;
3285 participant->is_key_server = TRUE;
3286 participant->principal = TRUE;
3288 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3289 sizeof(kay->key_server_sci));
3290 kay->key_server_priority = kay->actor_priority;
3291 participant->is_elected = TRUE;
3293 participant->is_obliged_key_server = FALSE;
3294 participant->can_be_key_server = FALSE;
3295 participant->is_key_server = FALSE;
3296 participant->is_elected = TRUE;
3301 participant->is_obliged_key_server = FALSE;
3302 participant->can_be_key_server = TRUE;
3303 participant->is_key_server = TRUE;
3304 participant->is_elected = FALSE;
3308 participant->cached = FALSE;
3310 participant->active = FALSE;
3311 participant->participant = FALSE;
3312 participant->retain = FALSE;
3313 participant->activate = DEFAULT;
3315 if (participant->is_key_server)
3316 participant->principal = TRUE;
3318 dl_list_init(&participant->live_peers);
3319 dl_list_init(&participant->potential_peers);
3321 participant->retry_count = 0;
3322 participant->kay = kay;
3324 if (!reset_participant_mi(participant))
3327 participant->lrx = FALSE;
3328 participant->ltx = FALSE;
3329 participant->orx = FALSE;
3330 participant->otx = FALSE;
3331 participant->to_dist_sak = FALSE;
3332 participant->to_use_sak = FALSE;
3333 participant->new_sak = FALSE;
3334 dl_list_init(&participant->sak_list);
3335 participant->new_key = NULL;
3336 dl_list_init(&participant->rxsc_list);
3337 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3339 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3340 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3341 kay->macsec_replay_window);
3342 secy_create_transmit_sc(kay, participant->txsc);
3344 /* to derive KEK from CAK and CKN */
3345 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3346 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3347 participant->ckn.name,
3348 participant->ckn.len,
3349 participant->kek.key)) {
3350 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3353 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3354 participant->kek.key, participant->kek.len);
3356 /* to derive ICK from CAK and CKN */
3357 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3358 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3359 participant->ckn.name,
3360 participant->ckn.len,
3361 participant->ick.key)) {
3362 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3365 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3366 participant->ick.key, participant->ick.len);
3368 dl_list_add(&kay->participant_list, &participant->list);
3369 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3370 ckn->name, ckn->len);
3372 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3373 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3375 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3381 os_free(participant);
3387 * ieee802_1x_kay_delete_mka -
3390 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3392 struct ieee802_1x_mka_participant *participant;
3393 struct ieee802_1x_kay_peer *peer;
3394 struct data_key *sak;
3395 struct receive_sc *rxsc;
3400 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3402 /* get the participant */
3403 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3405 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3406 ckn->name, ckn->len);
3410 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3411 dl_list_del(&participant->list);
3413 /* remove live peer */
3414 while (!dl_list_empty(&participant->live_peers)) {
3415 peer = dl_list_entry(participant->live_peers.next,
3416 struct ieee802_1x_kay_peer, list);
3417 dl_list_del(&peer->list);
3421 /* remove potential peer */
3422 while (!dl_list_empty(&participant->potential_peers)) {
3423 peer = dl_list_entry(participant->potential_peers.next,
3424 struct ieee802_1x_kay_peer, list);
3425 dl_list_del(&peer->list);
3430 while (!dl_list_empty(&participant->sak_list)) {
3431 sak = dl_list_entry(participant->sak_list.next,
3432 struct data_key, list);
3433 dl_list_del(&sak->list);
3437 while (!dl_list_empty(&participant->rxsc_list)) {
3438 rxsc = dl_list_entry(participant->rxsc_list.next,
3439 struct receive_sc, list);
3440 secy_delete_receive_sc(kay, rxsc);
3441 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3443 secy_delete_transmit_sc(kay, participant->txsc);
3444 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3446 os_memset(&participant->cak, 0, sizeof(participant->cak));
3447 os_memset(&participant->kek, 0, sizeof(participant->kek));
3448 os_memset(&participant->ick, 0, sizeof(participant->ick));
3449 os_free(participant);
3454 * ieee802_1x_kay_mka_participate -
3456 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3457 struct mka_key_name *ckn,
3460 struct ieee802_1x_mka_participant *participant;
3465 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3469 participant->active = status;
3474 * ieee802_1x_kay_new_sak -
3477 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3479 struct ieee802_1x_mka_participant *participant;
3484 participant = ieee802_1x_kay_get_principal_participant(kay);
3488 participant->new_sak = TRUE;
3489 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3496 * ieee802_1x_kay_change_cipher_suite -
3499 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay,
3500 unsigned int cs_index)
3502 struct ieee802_1x_mka_participant *participant;
3507 if (cs_index >= CS_TABLE_SIZE) {
3508 wpa_printf(MSG_ERROR,
3509 "KaY: Configured cipher suite index is out of range");
3512 if (kay->macsec_csindex == cs_index)
3516 kay->macsec_desired = FALSE;
3518 kay->macsec_csindex = cs_index;
3519 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3521 participant = ieee802_1x_kay_get_principal_participant(kay);
3523 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3524 participant->new_sak = TRUE;