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_is_in_peer
346 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
349 return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
350 ieee802_1x_kay_is_in_potential_peer(participant, mi);
355 * ieee802_1x_kay_get_peer
357 static struct ieee802_1x_kay_peer *
358 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
361 struct ieee802_1x_kay_peer *peer;
363 peer = ieee802_1x_kay_get_live_peer(participant, mi);
367 return ieee802_1x_kay_get_potential_peer(participant, mi);
372 * ieee802_1x_kay_get_cipher_suite
374 static struct macsec_ciphersuite *
375 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
380 for (i = 0; i < CS_TABLE_SIZE; i++) {
381 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
382 return &cipher_suite_tbl[i];
389 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
390 const struct ieee802_1x_mka_sci *b)
392 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
397 * ieee802_1x_kay_get_peer_sci
399 static struct ieee802_1x_kay_peer *
400 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
401 const struct ieee802_1x_mka_sci *sci)
403 struct ieee802_1x_kay_peer *peer;
405 dl_list_for_each(peer, &participant->live_peers,
406 struct ieee802_1x_kay_peer, list) {
407 if (sci_equal(&peer->sci, sci))
411 dl_list_for_each(peer, &participant->potential_peers,
412 struct ieee802_1x_kay_peer, list) {
413 if (sci_equal(&peer->sci, sci))
422 * ieee802_1x_kay_init_receive_sa -
424 static struct receive_sa *
425 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
426 struct data_key *key)
428 struct receive_sa *psa;
433 psa = os_zalloc(sizeof(*psa));
435 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
440 psa->lowest_pn = lowest_pn;
441 psa->next_pn = lowest_pn;
445 os_get_time(&psa->created_time);
448 dl_list_add(&psc->sa_list, &psa->list);
449 wpa_printf(MSG_DEBUG,
450 "KaY: Create receive SA(AN: %hhu lowest_pn: %u of SC(channel: %d)",
451 an, lowest_pn, psc->channel);
458 * ieee802_1x_kay_deinit_receive_sa -
460 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
463 wpa_printf(MSG_DEBUG,
464 "KaY: Delete receive SA(an: %hhu) of SC",
466 dl_list_del(&psa->list);
472 * ieee802_1x_kay_init_receive_sc -
474 static struct receive_sc *
475 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
478 struct receive_sc *psc;
483 psc = os_zalloc(sizeof(*psc));
485 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
489 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
490 psc->channel = channel;
492 os_get_time(&psc->created_time);
493 psc->receiving = FALSE;
495 dl_list_init(&psc->sa_list);
496 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
497 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
504 * ieee802_1x_kay_deinit_receive_sc -
507 ieee802_1x_kay_deinit_receive_sc(
508 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
510 struct receive_sa *psa, *pre_sa;
512 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
514 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
516 secy_disable_receive_sa(participant->kay, psa);
517 ieee802_1x_kay_deinit_receive_sa(psa);
519 dl_list_del(&psc->list);
524 static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
526 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
527 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
528 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
529 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
533 static struct ieee802_1x_kay_peer *
534 ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
536 struct ieee802_1x_kay_peer *peer;
538 peer = os_zalloc(sizeof(*peer));
540 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
544 os_memcpy(peer->mi, mi, MI_LEN);
546 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
547 peer->sak_used = FALSE;
554 * ieee802_1x_kay_create_live_peer
556 static struct ieee802_1x_kay_peer *
557 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
558 const u8 *mi, u32 mn)
560 struct ieee802_1x_kay_peer *peer;
561 struct receive_sc *rxsc;
564 peer = ieee802_1x_kay_create_peer(mi, mn);
568 os_memcpy(&peer->sci, &participant->current_peer_sci,
571 secy_get_available_receive_sc(participant->kay, &sc_ch);
573 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
579 dl_list_add(&participant->live_peers, &peer->list);
580 dl_list_add(&participant->rxsc_list, &rxsc->list);
581 secy_create_receive_sc(participant->kay, rxsc);
583 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
584 ieee802_1x_kay_dump_peer(peer);
591 * ieee802_1x_kay_create_potential_peer
593 static struct ieee802_1x_kay_peer *
594 ieee802_1x_kay_create_potential_peer(
595 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
597 struct ieee802_1x_kay_peer *peer;
599 peer = ieee802_1x_kay_create_peer(mi, mn);
603 dl_list_add(&participant->potential_peers, &peer->list);
605 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
606 ieee802_1x_kay_dump_peer(peer);
613 * ieee802_1x_kay_move_live_peer
615 static struct ieee802_1x_kay_peer *
616 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
619 struct ieee802_1x_kay_peer *peer;
620 struct receive_sc *rxsc;
623 peer = ieee802_1x_kay_get_potential_peer(participant, mi);
625 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
630 os_memcpy(&peer->sci, &participant->current_peer_sci,
633 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
635 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
636 ieee802_1x_kay_dump_peer(peer);
638 dl_list_del(&peer->list);
639 dl_list_add_tail(&participant->live_peers, &peer->list);
641 secy_get_available_receive_sc(participant->kay, &sc_ch);
643 dl_list_add(&participant->rxsc_list, &rxsc->list);
644 secy_create_receive_sc(participant->kay, rxsc);
652 * ieee802_1x_mka_basic_body_present -
655 ieee802_1x_mka_basic_body_present(
656 struct ieee802_1x_mka_participant *participant)
663 * ieee802_1x_mka_basic_body_length -
666 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
670 length = sizeof(struct ieee802_1x_mka_basic_body);
671 length += participant->ckn.len;
672 return MKA_ALIGN_LENGTH(length);
677 * ieee802_1x_mka_encode_basic_body
680 ieee802_1x_mka_encode_basic_body(
681 struct ieee802_1x_mka_participant *participant,
684 struct ieee802_1x_mka_basic_body *body;
685 struct ieee802_1x_kay *kay = participant->kay;
686 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
688 body = wpabuf_put(buf, length);
690 body->version = kay->mka_version;
691 body->priority = kay->actor_priority;
692 if (participant->is_elected)
693 body->key_server = participant->is_key_server;
695 body->key_server = participant->can_be_key_server;
697 body->macsec_desired = kay->macsec_desired;
698 body->macsec_capability = kay->macsec_capable;
699 set_mka_param_body_len(body, length - MKA_HDR_LEN);
701 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
702 sizeof(kay->actor_sci.addr));
703 body->actor_sci.port = kay->actor_sci.port;
705 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
706 participant->mn = participant->mn + 1;
707 body->actor_mn = host_to_be32(participant->mn);
708 os_memcpy(body->algo_agility, kay->algo_agility,
709 sizeof(body->algo_agility));
711 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
713 ieee802_1x_mka_dump_basic_body(body);
720 reset_participant_mi(struct ieee802_1x_mka_participant *participant)
722 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
731 * ieee802_1x_mka_decode_basic_body -
733 static struct ieee802_1x_mka_participant *
734 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
737 struct ieee802_1x_mka_participant *participant;
738 const struct ieee802_1x_mka_basic_body *body;
739 struct ieee802_1x_kay_peer *peer;
741 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
743 if (body->version > MKA_VERSION_ID) {
744 wpa_printf(MSG_DEBUG,
745 "KaY: peer's version(%d) greater than mka current version(%d)",
746 body->version, MKA_VERSION_ID);
748 if (kay->is_obliged_key_server && body->key_server) {
749 wpa_printf(MSG_DEBUG, "I must be as key server");
753 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
755 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
759 /* If the peer's MI is my MI, I will choose new MI */
760 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
761 if (!reset_participant_mi(participant))
765 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
766 participant->current_peer_id.mn = body->actor_mn;
767 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
768 sizeof(participant->current_peer_sci.addr));
769 participant->current_peer_sci.port = body->actor_sci.port;
772 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
774 /* Check duplicated SCI */
775 /* TODO: What policy should be applied to detect duplicated SCI
776 * is active attacker or a valid peer whose MI is be changed?
778 peer = ieee802_1x_kay_get_peer_sci(participant,
781 wpa_printf(MSG_WARNING,
782 "KaY: duplicated SCI detected, Maybe active attacker");
783 dl_list_del(&peer->list);
787 peer = ieee802_1x_kay_create_potential_peer(
788 participant, body->actor_mi,
789 be_to_host32(body->actor_mn));
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;
797 } else if (peer->mn < be_to_host32(body->actor_mn)) {
798 peer->mn = be_to_host32(body->actor_mn);
799 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
800 peer->macsec_desired = body->macsec_desired;
801 peer->macsec_capability = body->macsec_capability;
802 peer->is_key_server = (Boolean) body->key_server;
803 peer->key_server_priority = body->priority;
805 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
814 * ieee802_1x_mka_live_peer_body_present
817 ieee802_1x_mka_live_peer_body_present(
818 struct ieee802_1x_mka_participant *participant)
820 return !dl_list_empty(&participant->live_peers);
825 * ieee802_1x_kay_get_live_peer_length
828 ieee802_1x_mka_get_live_peer_length(
829 struct ieee802_1x_mka_participant *participant)
831 int len = MKA_HDR_LEN;
832 struct ieee802_1x_kay_peer *peer;
834 dl_list_for_each(peer, &participant->live_peers,
835 struct ieee802_1x_kay_peer, list)
836 len += sizeof(struct ieee802_1x_mka_peer_id);
838 return MKA_ALIGN_LENGTH(len);
843 * ieee802_1x_mka_encode_live_peer_body -
846 ieee802_1x_mka_encode_live_peer_body(
847 struct ieee802_1x_mka_participant *participant,
850 struct ieee802_1x_mka_peer_body *body;
851 struct ieee802_1x_kay_peer *peer;
853 struct ieee802_1x_mka_peer_id *body_peer;
855 length = ieee802_1x_mka_get_live_peer_length(participant);
856 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
858 body->type = MKA_LIVE_PEER_LIST;
859 set_mka_param_body_len(body, length - MKA_HDR_LEN);
861 dl_list_for_each(peer, &participant->live_peers,
862 struct ieee802_1x_kay_peer, list) {
863 body_peer = wpabuf_put(buf,
864 sizeof(struct ieee802_1x_mka_peer_id));
865 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
866 body_peer->mn = host_to_be32(peer->mn);
869 ieee802_1x_mka_dump_peer_body(body);
874 * ieee802_1x_mka_potential_peer_body_present
877 ieee802_1x_mka_potential_peer_body_present(
878 struct ieee802_1x_mka_participant *participant)
880 return !dl_list_empty(&participant->potential_peers);
885 * ieee802_1x_kay_get_potential_peer_length
888 ieee802_1x_mka_get_potential_peer_length(
889 struct ieee802_1x_mka_participant *participant)
891 int len = MKA_HDR_LEN;
892 struct ieee802_1x_kay_peer *peer;
894 dl_list_for_each(peer, &participant->potential_peers,
895 struct ieee802_1x_kay_peer, list)
896 len += sizeof(struct ieee802_1x_mka_peer_id);
898 return MKA_ALIGN_LENGTH(len);
903 * ieee802_1x_mka_encode_potential_peer_body -
906 ieee802_1x_mka_encode_potential_peer_body(
907 struct ieee802_1x_mka_participant *participant,
910 struct ieee802_1x_mka_peer_body *body;
911 struct ieee802_1x_kay_peer *peer;
913 struct ieee802_1x_mka_peer_id *body_peer;
915 length = ieee802_1x_mka_get_potential_peer_length(participant);
916 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
918 body->type = MKA_POTENTIAL_PEER_LIST;
919 set_mka_param_body_len(body, length - MKA_HDR_LEN);
921 dl_list_for_each(peer, &participant->potential_peers,
922 struct ieee802_1x_kay_peer, list) {
923 body_peer = wpabuf_put(buf,
924 sizeof(struct ieee802_1x_mka_peer_id));
925 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
926 body_peer->mn = host_to_be32(peer->mn);
929 ieee802_1x_mka_dump_peer_body(body);
935 * ieee802_1x_mka_i_in_peerlist -
938 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
939 const u8 *mka_msg, size_t msg_len)
941 struct ieee802_1x_mka_hdr *hdr;
948 for (pos = mka_msg, left_len = msg_len;
949 left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
950 left_len -= body_len + MKA_HDR_LEN,
951 pos += body_len + MKA_HDR_LEN) {
952 hdr = (struct ieee802_1x_mka_hdr *) pos;
953 body_len = get_mka_param_body_len(hdr);
954 body_type = get_mka_param_body_type(hdr);
956 if (body_type != MKA_LIVE_PEER_LIST &&
957 body_type != MKA_POTENTIAL_PEER_LIST)
960 ieee802_1x_mka_dump_peer_body(
961 (struct ieee802_1x_mka_peer_body *)pos);
963 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
964 wpa_printf(MSG_ERROR,
965 "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",
966 left_len, MKA_HDR_LEN,
967 body_len, DEFAULT_ICV_LEN);
971 if ((body_len % 16) != 0) {
972 wpa_printf(MSG_ERROR,
973 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
978 for (i = 0; i < body_len;
979 i += sizeof(struct ieee802_1x_mka_peer_id)) {
980 const struct ieee802_1x_mka_peer_id *peer_mi;
982 peer_mi = (const struct ieee802_1x_mka_peer_id *)
983 (pos + MKA_HDR_LEN + i);
984 if (os_memcmp(peer_mi->mi, participant->mi,
986 be_to_host32(peer_mi->mn) == participant->mn)
996 * ieee802_1x_mka_decode_live_peer_body -
998 static int ieee802_1x_mka_decode_live_peer_body(
999 struct ieee802_1x_mka_participant *participant,
1000 const u8 *peer_msg, size_t msg_len)
1002 const struct ieee802_1x_mka_hdr *hdr;
1003 struct ieee802_1x_kay_peer *peer;
1006 Boolean is_included;
1008 is_included = ieee802_1x_kay_is_in_live_peer(
1009 participant, participant->current_peer_id.mi);
1011 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1012 body_len = get_mka_param_body_len(hdr);
1013 if (body_len % 16 != 0) {
1014 wpa_printf(MSG_ERROR,
1015 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1020 for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
1021 const struct ieee802_1x_mka_peer_id *peer_mi;
1024 peer_mi = (const struct ieee802_1x_mka_peer_id *)
1025 (peer_msg + MKA_HDR_LEN + i);
1026 peer_mn = be_to_host32(peer_mi->mn);
1029 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1030 /* My message id is used by other participant */
1031 if (peer_mn > participant->mn &&
1032 !reset_participant_mi(participant))
1033 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1040 peer = ieee802_1x_kay_get_peer(participant, peer_mi->mi);
1043 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1044 } else if (!ieee802_1x_kay_create_potential_peer(
1045 participant, peer_mi->mi, peer_mn)) {
1055 * ieee802_1x_mka_decode_potential_peer_body -
1058 ieee802_1x_mka_decode_potential_peer_body(
1059 struct ieee802_1x_mka_participant *participant,
1060 const u8 *peer_msg, size_t msg_len)
1062 const struct ieee802_1x_mka_hdr *hdr;
1066 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1067 body_len = get_mka_param_body_len(hdr);
1068 if (body_len % 16 != 0) {
1069 wpa_printf(MSG_ERROR,
1070 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1075 for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
1076 const struct ieee802_1x_mka_peer_id *peer_mi;
1079 peer_mi = (struct ieee802_1x_mka_peer_id *)
1080 (peer_msg + MKA_HDR_LEN + i);
1081 peer_mn = be_to_host32(peer_mi->mn);
1084 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1085 /* My message id is used by other participant */
1086 if (peer_mn > participant->mn &&
1087 !reset_participant_mi(participant))
1088 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1098 * ieee802_1x_mka_sak_use_body_present
1101 ieee802_1x_mka_sak_use_body_present(
1102 struct ieee802_1x_mka_participant *participant)
1104 return participant->to_use_sak;
1109 * ieee802_1x_mka_get_sak_use_length
1112 ieee802_1x_mka_get_sak_use_length(
1113 struct ieee802_1x_mka_participant *participant)
1115 int length = MKA_HDR_LEN;
1117 if (participant->kay->macsec_desired && participant->advised_desired)
1118 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1120 return MKA_ALIGN_LENGTH(length);
1128 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1129 struct ieee802_1x_mka_ki *ki)
1131 struct receive_sa *rxsa;
1132 struct receive_sc *rxsc;
1135 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1136 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1138 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1139 secy_get_receive_lowest_pn(principal->kay,
1142 lpn = lpn > rxsa->lowest_pn ?
1143 lpn : rxsa->lowest_pn;
1157 * ieee802_1x_mka_encode_sak_use_body -
1160 ieee802_1x_mka_encode_sak_use_body(
1161 struct ieee802_1x_mka_participant *participant,
1164 struct ieee802_1x_mka_sak_use_body *body;
1165 struct ieee802_1x_kay *kay = participant->kay;
1166 unsigned int length;
1169 length = ieee802_1x_mka_get_sak_use_length(participant);
1170 body = wpabuf_put(buf, length);
1172 body->type = MKA_SAK_USE;
1173 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1175 if (length == MKA_HDR_LEN) {
1181 body->delay_protect = FALSE;
1185 /* data protect, lowest accept packet number */
1186 body->delay_protect = kay->macsec_replay_protect;
1187 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1188 if (pn > kay->pn_exhaustion) {
1189 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1190 if (participant->is_key_server)
1191 participant->new_sak = TRUE;
1194 body->llpn = host_to_be32(pn);
1195 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1196 body->olpn = host_to_be32(pn);
1198 /* plain tx, plain rx */
1199 body->ptx = !kay->macsec_protect;
1200 body->prx = kay->macsec_validate != Strict;
1202 /* latest key: rx, tx, key server member identifier key number */
1203 body->lan = participant->lan;
1204 os_memcpy(body->lsrv_mi, participant->lki.mi, sizeof(body->lsrv_mi));
1205 body->lkn = host_to_be32(participant->lki.kn);
1206 body->lrx = participant->lrx;
1207 body->ltx = participant->ltx;
1209 /* old key: rx, tx, key server member identifier key number */
1210 body->oan = participant->oan;
1211 if (participant->oki.kn != participant->lki.kn &&
1212 participant->oki.kn != 0) {
1215 os_memcpy(body->osrv_mi, participant->oki.mi,
1216 sizeof(body->osrv_mi));
1217 body->okn = host_to_be32(participant->oki.kn);
1223 /* set CP's variable */
1225 kay->tx_enable = TRUE;
1226 kay->port_enable = TRUE;
1229 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;
1254 struct ieee802_1x_kay *kay = participant->kay;
1256 if (!participant->principal) {
1257 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1260 peer = ieee802_1x_kay_get_live_peer(participant,
1261 participant->current_peer_id.mi);
1263 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1267 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1268 body_len = get_mka_param_body_len(hdr);
1269 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1270 ieee802_1x_mka_dump_sak_use_body(body);
1272 if ((body_len != 0) && (body_len < 40)) {
1273 wpa_printf(MSG_ERROR,
1274 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
1279 /* TODO: what action should I take when peer does not support MACsec */
1280 if (body_len == 0) {
1281 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1285 /* TODO: when the plain tx or rx of peer is true, should I change
1286 * the attribute of controlled port
1289 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1292 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1294 /* check latest key is valid */
1295 if (body->ltx || body->lrx) {
1297 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1298 ki.kn = be_to_host32(body->lkn);
1299 dl_list_for_each(sa_key, &participant->sak_list,
1300 struct data_key, list) {
1301 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1307 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1310 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1311 sizeof(participant->lki.mi)) == 0 &&
1312 be_to_host32(body->lkn) == participant->lki.kn &&
1313 body->lan == participant->lan) {
1314 peer->sak_used = TRUE;
1316 if (body->ltx && peer->is_key_server) {
1317 ieee802_1x_cp_set_servertransmitting(kay->cp, TRUE);
1318 ieee802_1x_cp_sm_step(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(kay->cp, TRUE);
1353 ieee802_1x_cp_sm_step(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 > 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(kay, txsa);
1382 if (lpn > txsa->next_pn) {
1383 secy_set_transmit_next_pn(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 return participant->to_dist_sak && participant->new_key;
1403 * ieee802_1x_kay_get_dist_sak_length
1406 ieee802_1x_mka_get_dist_sak_length(
1407 struct ieee802_1x_mka_participant *participant)
1409 int length = MKA_HDR_LEN;
1410 int cs_index = participant->kay->macsec_csindex;
1412 if (participant->advised_desired) {
1413 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1414 if (cs_index != DEFAULT_CS_INDEX)
1415 length += CS_ID_LEN;
1417 length += cipher_suite_tbl[cs_index].sak_len + 8;
1420 return MKA_ALIGN_LENGTH(length);
1425 * ieee802_1x_mka_encode_dist_sak_body -
1428 ieee802_1x_mka_encode_dist_sak_body(
1429 struct ieee802_1x_mka_participant *participant,
1432 struct ieee802_1x_mka_dist_sak_body *body;
1433 struct data_key *sak;
1434 unsigned int length;
1438 length = ieee802_1x_mka_get_dist_sak_length(participant);
1439 body = wpabuf_put(buf, length);
1440 body->type = MKA_DISTRIBUTED_SAK;
1441 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1442 if (length == MKA_HDR_LEN) {
1443 body->confid_offset = 0;
1448 sak = participant->new_key;
1449 body->confid_offset = sak->confidentiality_offset;
1450 body->dan = sak->an;
1451 body->kn = host_to_be32(sak->key_identifier.kn);
1452 cs_index = participant->kay->macsec_csindex;
1454 if (cs_index != DEFAULT_CS_INDEX) {
1455 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1456 sak_pos = CS_ID_LEN;
1458 if (aes_wrap(participant->kek.key, 16,
1459 cipher_suite_tbl[cs_index].sak_len / 8,
1460 sak->key, body->sak + sak_pos)) {
1461 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1465 ieee802_1x_mka_dump_dist_sak_body(body);
1472 * ieee802_1x_kay_init_data_key -
1474 static struct data_key *
1475 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1477 struct data_key *pkey;
1482 pkey = os_zalloc(sizeof(*pkey));
1484 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1488 pkey->key = os_zalloc(conf->key_len);
1489 if (pkey->key == NULL) {
1490 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1495 os_memcpy(pkey->key, conf->key, conf->key_len);
1496 os_memcpy(&pkey->key_identifier, &conf->ki,
1497 sizeof(pkey->key_identifier));
1498 pkey->confidentiality_offset = conf->offset;
1499 pkey->an = conf->an;
1500 pkey->transmits = conf->tx;
1501 pkey->receives = conf->rx;
1502 os_get_time(&pkey->created_time);
1511 * ieee802_1x_kay_decode_dist_sak_body -
1514 ieee802_1x_mka_decode_dist_sak_body(
1515 struct ieee802_1x_mka_participant *participant,
1516 const u8 *mka_msg, size_t msg_len)
1518 struct ieee802_1x_mka_hdr *hdr;
1519 struct ieee802_1x_mka_dist_sak_body *body;
1520 struct ieee802_1x_kay_peer *peer;
1521 struct macsec_ciphersuite *cs;
1523 struct key_conf *conf;
1524 struct data_key *sa_key = NULL;
1525 struct ieee802_1x_mka_ki sak_ki;
1529 struct ieee802_1x_kay *kay = participant->kay;
1531 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1532 body_len = get_mka_param_body_len(hdr);
1533 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1534 wpa_printf(MSG_ERROR,
1535 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1540 if (!participant->principal) {
1541 wpa_printf(MSG_ERROR,
1542 "KaY: I can't accept the distributed SAK as I am not principal");
1545 if (participant->is_key_server) {
1546 wpa_printf(MSG_ERROR,
1547 "KaY: I can't accept the distributed SAK as myself is key server ");
1550 if (!kay->macsec_desired ||
1551 kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1552 wpa_printf(MSG_ERROR,
1553 "KaY: I am not MACsec-desired or without MACsec capable");
1557 peer = ieee802_1x_kay_get_live_peer(participant,
1558 participant->current_peer_id.mi);
1560 wpa_printf(MSG_ERROR,
1561 "KaY: The key server is not in my live peers list");
1564 if (!sci_equal(&kay->key_server_sci, &peer->sci)) {
1565 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1569 if (body_len == 0) {
1570 kay->authenticated = TRUE;
1571 kay->secured = FALSE;
1572 kay->failed = FALSE;
1573 participant->advised_desired = FALSE;
1574 ieee802_1x_cp_connect_authenticated(kay->cp);
1575 ieee802_1x_cp_sm_step(kay->cp);
1576 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1577 participant->to_use_sak = TRUE;
1581 participant->advised_desired = TRUE;
1582 kay->authenticated = FALSE;
1583 kay->secured = TRUE;
1584 kay->failed = FALSE;
1585 ieee802_1x_cp_connect_secure(kay->cp);
1586 ieee802_1x_cp_sm_step(kay->cp);
1588 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1589 ieee802_1x_mka_dump_dist_sak_body(body);
1590 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1592 if (os_memcmp(sa_key->key_identifier.mi,
1593 participant->current_peer_id.mi, MI_LEN) == 0 &&
1594 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1595 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1600 if (body_len == 28) {
1601 sak_len = DEFAULT_SA_KEY_LEN;
1602 wrap_sak = body->sak;
1603 kay->macsec_csindex = DEFAULT_CS_INDEX;
1605 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1607 wpa_printf(MSG_ERROR,
1608 "KaY: I can't support the Cipher Suite advised by key server");
1611 sak_len = cs->sak_len;
1612 wrap_sak = body->sak + CS_ID_LEN;
1613 kay->macsec_csindex = cs->index;
1616 unwrap_sak = os_zalloc(sak_len);
1618 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1621 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1623 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1624 os_free(unwrap_sak);
1627 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1629 conf = os_zalloc(sizeof(*conf));
1631 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1632 os_free(unwrap_sak);
1635 conf->key_len = sak_len;
1637 conf->key = os_zalloc(conf->key_len);
1639 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1640 os_free(unwrap_sak);
1645 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1647 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1649 sak_ki.kn = be_to_host32(body->kn);
1651 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1652 conf->ki.kn = sak_ki.kn;
1653 conf->an = body->dan;
1654 conf->offset = body->confid_offset;
1658 sa_key = ieee802_1x_kay_init_data_key(conf);
1660 os_free(unwrap_sak);
1666 dl_list_add(&participant->sak_list, &sa_key->list);
1668 ieee802_1x_cp_set_ciphersuite(kay->cp,
1669 cipher_suite_tbl[kay->macsec_csindex].id);
1670 ieee802_1x_cp_sm_step(kay->cp);
1671 ieee802_1x_cp_set_offset(kay->cp, body->confid_offset);
1672 ieee802_1x_cp_sm_step(kay->cp);
1673 ieee802_1x_cp_set_distributedki(kay->cp, &sak_ki);
1674 ieee802_1x_cp_set_distributedan(kay->cp, body->dan);
1675 ieee802_1x_cp_signal_newsak(kay->cp);
1676 ieee802_1x_cp_sm_step(kay->cp);
1678 participant->to_use_sak = TRUE;
1680 os_free(unwrap_sak);
1689 * ieee802_1x_mka_icv_body_present
1692 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1699 * ieee802_1x_kay_get_icv_length
1702 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1706 length = sizeof(struct ieee802_1x_mka_icv_body);
1707 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1709 return MKA_ALIGN_LENGTH(length);
1714 * ieee802_1x_mka_encode_icv_body -
1717 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1720 struct ieee802_1x_mka_icv_body *body;
1721 unsigned int length;
1722 u8 cmac[MAX_ICV_LEN];
1724 length = ieee802_1x_mka_get_icv_length(participant);
1725 if (length != DEFAULT_ICV_LEN) {
1726 body = wpabuf_put(buf, MKA_HDR_LEN);
1727 body->type = MKA_ICV_INDICATOR;
1728 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1731 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1732 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1733 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1737 if (length != DEFAULT_ICV_LEN)
1738 length -= MKA_HDR_LEN;
1739 os_memcpy(wpabuf_put(buf, length), cmac, length);
1745 * ieee802_1x_mka_decode_icv_body -
1748 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1749 const u8 *mka_msg, size_t msg_len)
1751 struct ieee802_1x_mka_hdr *hdr;
1752 struct ieee802_1x_mka_icv_body *body;
1760 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1761 hdr = (struct ieee802_1x_mka_hdr *) pos;
1762 body_len = get_mka_param_body_len(hdr);
1763 body_type = get_mka_param_body_type(hdr);
1765 if (left_len < (body_len + MKA_HDR_LEN))
1768 if (body_type != MKA_ICV_INDICATOR) {
1769 left_len -= MKA_HDR_LEN + body_len;
1770 pos += MKA_HDR_LEN + body_len;
1774 body = (struct ieee802_1x_mka_icv_body *)pos;
1776 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1783 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1788 * ieee802_1x_mka_decode_dist_cak_body-
1791 ieee802_1x_mka_decode_dist_cak_body(
1792 struct ieee802_1x_mka_participant *participant,
1793 const u8 *mka_msg, size_t msg_len)
1795 struct ieee802_1x_mka_hdr *hdr;
1798 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1799 body_len = get_mka_param_body_len(hdr);
1800 if (body_len < 28) {
1801 wpa_printf(MSG_ERROR,
1802 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
1812 * ieee802_1x_mka_decode_kmd_body -
1815 ieee802_1x_mka_decode_kmd_body(
1816 struct ieee802_1x_mka_participant *participant,
1817 const u8 *mka_msg, size_t msg_len)
1819 struct ieee802_1x_mka_hdr *hdr;
1822 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1823 body_len = get_mka_param_body_len(hdr);
1825 wpa_printf(MSG_ERROR,
1826 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
1836 * ieee802_1x_mka_decode_announce_body -
1838 static int ieee802_1x_mka_decode_announce_body(
1839 struct ieee802_1x_mka_participant *participant,
1840 const u8 *mka_msg, size_t msg_len)
1846 struct mka_param_body_handler {
1847 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
1848 struct wpabuf *buf);
1849 int (*body_rx)(struct ieee802_1x_mka_participant *participant,
1850 const u8 *mka_msg, size_t msg_len);
1851 int (*body_length)(struct ieee802_1x_mka_participant *participant);
1852 Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
1856 static struct mka_param_body_handler mka_body_handler[] = {
1857 /* basic parameter set */
1859 .body_tx = ieee802_1x_mka_encode_basic_body,
1861 .body_length = ieee802_1x_mka_basic_body_length,
1862 .body_present = ieee802_1x_mka_basic_body_present
1865 /* live peer list parameter set */
1867 .body_tx = ieee802_1x_mka_encode_live_peer_body,
1868 .body_rx = ieee802_1x_mka_decode_live_peer_body,
1869 .body_length = ieee802_1x_mka_get_live_peer_length,
1870 .body_present = ieee802_1x_mka_live_peer_body_present
1873 /* potential peer list parameter set */
1875 .body_tx = ieee802_1x_mka_encode_potential_peer_body,
1876 .body_rx = ieee802_1x_mka_decode_potential_peer_body,
1877 .body_length = ieee802_1x_mka_get_potential_peer_length,
1878 .body_present = ieee802_1x_mka_potential_peer_body_present
1881 /* sak use parameter set */
1883 .body_tx = ieee802_1x_mka_encode_sak_use_body,
1884 .body_rx = ieee802_1x_mka_decode_sak_use_body,
1885 .body_length = ieee802_1x_mka_get_sak_use_length,
1886 .body_present = ieee802_1x_mka_sak_use_body_present
1889 /* distribute sak parameter set */
1891 .body_tx = ieee802_1x_mka_encode_dist_sak_body,
1892 .body_rx = ieee802_1x_mka_decode_dist_sak_body,
1893 .body_length = ieee802_1x_mka_get_dist_sak_length,
1894 .body_present = ieee802_1x_mka_dist_sak_body_present
1897 /* distribute cak parameter set */
1900 .body_rx = ieee802_1x_mka_decode_dist_cak_body,
1901 .body_length = NULL,
1902 .body_present = NULL
1905 /* kmd parameter set */
1908 .body_rx = ieee802_1x_mka_decode_kmd_body,
1909 .body_length = NULL,
1910 .body_present = NULL
1913 /* announce parameter set */
1916 .body_rx = ieee802_1x_mka_decode_announce_body,
1917 .body_length = NULL,
1918 .body_present = NULL
1921 /* icv parameter set */
1923 .body_tx = ieee802_1x_mka_encode_icv_body,
1925 .body_length = ieee802_1x_mka_get_icv_length,
1926 .body_present = ieee802_1x_mka_icv_body_present
1932 * ieee802_1x_kay_deinit_data_key -
1934 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1943 dl_list_del(&pkey->list);
1950 * ieee802_1x_kay_generate_new_sak -
1953 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1955 struct data_key *sa_key = NULL;
1956 struct key_conf *conf;
1957 struct ieee802_1x_kay_peer *peer;
1958 struct ieee802_1x_kay *kay = participant->kay;
1959 int ctx_len, ctx_offset;
1962 /* check condition for generating a fresh SAK:
1963 * must have one live peer
1964 * and MKA life time elapse since last distribution
1965 * or potential peer is empty
1967 if (dl_list_empty(&participant->live_peers)) {
1968 wpa_printf(MSG_ERROR,
1969 "KaY: Live peers list must not empty when generating fresh SAK");
1973 /* FIXME: A fresh SAK not generated until
1974 * the live peer list contains at least one peer and
1975 * MKA life time has elapsed since the prior SAK was first distributed,
1976 * or the Key server's potential peer is empty
1977 * but I can't understand the second item, so
1978 * here only check first item and ingore
1979 * && (!dl_list_empty(&participant->potential_peers))) {
1981 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1982 wpa_printf(MSG_ERROR,
1983 "KaY: Life time have not elapsed since prior SAK distributed");
1987 conf = os_zalloc(sizeof(*conf));
1989 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1992 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1994 conf->key = os_zalloc(conf->key_len);
1997 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2001 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2002 dl_list_for_each(peer, &participant->live_peers,
2003 struct ieee802_1x_kay_peer, list)
2004 ctx_len += sizeof(peer->mi);
2005 ctx_len += sizeof(participant->mi);
2007 context = os_zalloc(ctx_len);
2014 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2020 ctx_offset += conf->key_len;
2021 dl_list_for_each(peer, &participant->live_peers,
2022 struct ieee802_1x_kay_peer, list) {
2023 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2024 ctx_offset += sizeof(peer->mi);
2026 os_memcpy(context + ctx_offset, participant->mi,
2027 sizeof(participant->mi));
2028 ctx_offset += sizeof(participant->mi);
2029 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2031 if (conf->key_len == 16) {
2032 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2033 context, ctx_len, conf->key);
2034 } else if (conf->key_len == 32) {
2035 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2036 context, ctx_len, conf->key);
2038 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2044 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2045 conf->key, conf->key_len);
2047 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2048 conf->ki.kn = kay->dist_kn;
2049 conf->an = kay->dist_an;
2050 conf->offset = kay->macsec_confidentiality;
2054 sa_key = ieee802_1x_kay_init_data_key(conf);
2061 participant->new_key = sa_key;
2063 dl_list_add(&participant->sak_list, &sa_key->list);
2064 ieee802_1x_cp_set_ciphersuite(kay->cp,
2065 cipher_suite_tbl[kay->macsec_csindex].id);
2066 ieee802_1x_cp_sm_step(kay->cp);
2067 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2068 ieee802_1x_cp_sm_step(kay->cp);
2069 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2070 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2071 ieee802_1x_cp_signal_newsak(kay->cp);
2072 ieee802_1x_cp_sm_step(kay->cp);
2074 dl_list_for_each(peer, &participant->live_peers,
2075 struct ieee802_1x_kay_peer, list)
2076 peer->sak_used = FALSE;
2080 if (kay->dist_an > 3)
2083 kay->dist_time = time(NULL);
2092 static int compare_priorities(const struct ieee802_1x_kay_peer *peer,
2093 const struct ieee802_1x_kay_peer *other)
2095 if (peer->key_server_priority < other->key_server_priority)
2097 if (other->key_server_priority < peer->key_server_priority)
2100 return os_memcmp(peer->sci.addr, other->sci.addr, ETH_ALEN);
2105 * ieee802_1x_kay_elect_key_server - elect the key server
2106 * when to elect: whenever the live peers list changes
2109 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2111 struct ieee802_1x_kay_peer *peer;
2112 struct ieee802_1x_kay_peer *key_server = NULL;
2113 struct ieee802_1x_kay *kay = participant->kay;
2114 Boolean i_is_key_server;
2116 if (participant->is_obliged_key_server) {
2117 participant->new_sak = TRUE;
2118 participant->to_dist_sak = FALSE;
2119 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2123 /* elect the key server among the peers */
2124 dl_list_for_each(peer, &participant->live_peers,
2125 struct ieee802_1x_kay_peer, list) {
2126 if (!peer->is_key_server)
2134 if (compare_priorities(peer, key_server) < 0)
2138 /* elect the key server between me and the above elected peer */
2139 i_is_key_server = FALSE;
2140 if (key_server && participant->can_be_key_server) {
2141 struct ieee802_1x_kay_peer tmp;
2143 tmp.key_server_priority = kay->actor_priority;
2144 os_memcpy(&tmp.sci, &kay->actor_sci, sizeof(tmp.sci));
2145 if (compare_priorities(&tmp, key_server) < 0)
2146 i_is_key_server = TRUE;
2147 } else if (participant->can_be_key_server) {
2148 i_is_key_server = TRUE;
2151 if (i_is_key_server) {
2152 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2153 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2154 ieee802_1x_cp_signal_chgdserver(kay->cp);
2155 ieee802_1x_cp_sm_step(kay->cp);
2158 participant->is_key_server = TRUE;
2159 participant->principal = TRUE;
2160 participant->new_sak = TRUE;
2161 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2162 participant->to_dist_sak = FALSE;
2163 participant->is_elected = TRUE;
2165 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2166 sizeof(kay->key_server_sci));
2167 kay->key_server_priority = kay->actor_priority;
2168 } else if (key_server) {
2169 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2170 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2171 ieee802_1x_cp_signal_chgdserver(kay->cp);
2172 ieee802_1x_cp_sm_step(kay->cp);
2175 participant->is_key_server = FALSE;
2176 participant->principal = TRUE;
2177 participant->is_elected = TRUE;
2179 os_memcpy(&kay->key_server_sci, &key_server->sci,
2180 sizeof(kay->key_server_sci));
2181 kay->key_server_priority = key_server->key_server_priority;
2183 participant->principal = FALSE;
2184 participant->is_key_server = FALSE;
2185 participant->is_elected = FALSE;
2193 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2194 * how to use MACsec: whether use MACsec and its capability
2195 * protectFrames will be advised if the key server and one of its live peers are
2196 * MACsec capable and one of those request MACsec protection
2199 ieee802_1x_kay_decide_macsec_use(
2200 struct ieee802_1x_mka_participant *participant)
2202 struct ieee802_1x_kay *kay = participant->kay;
2203 struct ieee802_1x_kay_peer *peer;
2204 enum macsec_cap less_capability;
2207 if (!participant->is_key_server)
2210 /* key server self is MACsec-desired and requesting MACsec */
2211 if (!kay->macsec_desired) {
2212 participant->advised_desired = FALSE;
2215 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2216 participant->advised_desired = FALSE;
2219 less_capability = kay->macsec_capable;
2221 /* at least one of peers is MACsec-desired and requesting MACsec */
2223 dl_list_for_each(peer, &participant->live_peers,
2224 struct ieee802_1x_kay_peer, list) {
2225 if (!peer->macsec_desired)
2228 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2231 less_capability = (less_capability < peer->macsec_capability) ?
2232 less_capability : peer->macsec_capability;
2237 participant->advised_desired = TRUE;
2238 participant->advised_capability = less_capability;
2239 kay->authenticated = FALSE;
2240 kay->secured = TRUE;
2241 kay->failed = FALSE;
2242 ieee802_1x_cp_connect_secure(kay->cp);
2243 ieee802_1x_cp_sm_step(kay->cp);
2245 participant->advised_desired = FALSE;
2246 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2247 participant->to_use_sak = FALSE;
2248 kay->authenticated = TRUE;
2249 kay->secured = FALSE;
2250 kay->failed = FALSE;
2259 ieee802_1x_cp_connect_authenticated(kay->cp);
2260 ieee802_1x_cp_sm_step(kay->cp);
2266 static const u8 pae_group_addr[ETH_ALEN] = {
2267 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2272 * ieee802_1x_kay_encode_mkpdu -
2275 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2276 struct wpabuf *pbuf)
2279 struct ieee8023_hdr *ether_hdr;
2280 struct ieee802_1x_hdr *eapol_hdr;
2282 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2283 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2284 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2285 sizeof(ether_hdr->dest));
2286 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2288 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2289 eapol_hdr->version = EAPOL_VERSION;
2290 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2291 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2293 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2294 if (mka_body_handler[i].body_present &&
2295 mka_body_handler[i].body_present(participant)) {
2296 if (mka_body_handler[i].body_tx(participant, pbuf))
2305 * ieee802_1x_participant_send_mkpdu -
2308 ieee802_1x_participant_send_mkpdu(
2309 struct ieee802_1x_mka_participant *participant)
2312 struct ieee802_1x_kay *kay = participant->kay;
2316 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2317 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2318 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2319 if (mka_body_handler[i].body_present &&
2320 mka_body_handler[i].body_present(participant))
2321 length += mka_body_handler[i].body_length(participant);
2324 buf = wpabuf_alloc(length);
2326 wpa_printf(MSG_ERROR, "KaY: out of memory");
2330 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2331 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2335 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2339 participant->active = TRUE;
2345 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2347 * ieee802_1x_participant_timer -
2349 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2351 struct ieee802_1x_mka_participant *participant;
2352 struct ieee802_1x_kay *kay;
2353 struct ieee802_1x_kay_peer *peer, *pre_peer;
2354 time_t now = time(NULL);
2356 struct receive_sc *rxsc, *pre_rxsc;
2357 struct transmit_sa *txsa, *pre_txsa;
2359 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2360 kay = participant->kay;
2361 if (participant->cak_life) {
2362 if (now > participant->cak_life)
2366 /* should delete MKA instance if there are not live peers
2367 * when the MKA life elapsed since its creating */
2368 if (participant->mka_life) {
2369 if (dl_list_empty(&participant->live_peers)) {
2370 if (now > participant->mka_life)
2373 participant->mka_life = 0;
2378 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2379 struct ieee802_1x_kay_peer, list) {
2380 if (now > peer->expire) {
2381 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2382 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2384 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2385 dl_list_for_each_safe(rxsc, pre_rxsc,
2386 &participant->rxsc_list,
2387 struct receive_sc, list) {
2388 if (sci_equal(&rxsc->sci, &peer->sci)) {
2389 secy_delete_receive_sc(kay, rxsc);
2390 ieee802_1x_kay_deinit_receive_sc(
2394 dl_list_del(&peer->list);
2401 if (dl_list_empty(&participant->live_peers)) {
2402 participant->advised_desired = FALSE;
2403 participant->advised_capability =
2404 MACSEC_CAP_NOT_IMPLEMENTED;
2405 participant->to_use_sak = FALSE;
2406 kay->authenticated = TRUE;
2407 kay->secured = FALSE;
2408 kay->failed = FALSE;
2417 dl_list_for_each_safe(txsa, pre_txsa,
2418 &participant->txsc->sa_list,
2419 struct transmit_sa, list) {
2420 secy_disable_transmit_sa(kay, txsa);
2421 ieee802_1x_kay_deinit_transmit_sa(txsa);
2424 ieee802_1x_cp_connect_authenticated(kay->cp);
2425 ieee802_1x_cp_sm_step(kay->cp);
2427 ieee802_1x_kay_elect_key_server(participant);
2428 ieee802_1x_kay_decide_macsec_use(participant);
2432 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2433 struct ieee802_1x_kay_peer, list) {
2434 if (now > peer->expire) {
2435 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2436 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2438 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2439 dl_list_del(&peer->list);
2444 if (participant->new_sak) {
2445 if (!ieee802_1x_kay_generate_new_sak(participant))
2446 participant->to_dist_sak = TRUE;
2448 participant->new_sak = FALSE;
2451 if (participant->retry_count < MAX_RETRY_CNT) {
2452 ieee802_1x_participant_send_mkpdu(participant);
2453 participant->retry_count++;
2456 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2457 ieee802_1x_participant_timer,
2463 kay->authenticated = FALSE;
2464 kay->secured = FALSE;
2466 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2471 * ieee802_1x_kay_init_transmit_sa -
2473 static struct transmit_sa *
2474 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2475 struct data_key *key)
2477 struct transmit_sa *psa;
2479 key->tx_latest = TRUE;
2480 key->rx_latest = TRUE;
2482 psa = os_zalloc(sizeof(*psa));
2484 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2488 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2489 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2490 psa->confidentiality = TRUE;
2492 psa->confidentiality = FALSE;
2496 psa->next_pn = next_PN;
2499 os_get_time(&psa->created_time);
2500 psa->in_use = FALSE;
2502 dl_list_add(&psc->sa_list, &psa->list);
2503 wpa_printf(MSG_DEBUG,
2504 "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC(channel: %d)",
2505 an, next_PN, psc->channel);
2512 * ieee802_1x_kay_deinit_transmit_sa -
2514 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2517 wpa_printf(MSG_DEBUG,
2518 "KaY: Delete transmit SA(an: %hhu) of SC",
2520 dl_list_del(&psa->list);
2526 * init_transmit_sc -
2528 static struct transmit_sc *
2529 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2532 struct transmit_sc *psc;
2534 psc = os_zalloc(sizeof(*psc));
2536 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2539 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2540 psc->channel = channel;
2542 os_get_time(&psc->created_time);
2543 psc->transmitting = FALSE;
2544 psc->encoding_sa = FALSE;
2545 psc->enciphering_sa = FALSE;
2547 dl_list_init(&psc->sa_list);
2548 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2549 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2556 * ieee802_1x_kay_deinit_transmit_sc -
2559 ieee802_1x_kay_deinit_transmit_sc(
2560 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2562 struct transmit_sa *psa, *tmp;
2564 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2566 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2568 secy_disable_transmit_sa(participant->kay, psa);
2569 ieee802_1x_kay_deinit_transmit_sa(psa);
2576 /****************** Interface between CP and KAY *********************/
2578 * ieee802_1x_kay_set_latest_sa_attr -
2580 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2581 struct ieee802_1x_mka_ki *lki, u8 lan,
2582 Boolean ltx, Boolean lrx)
2584 struct ieee802_1x_mka_participant *principal;
2586 principal = ieee802_1x_kay_get_principal_participant(kay);
2591 os_memset(&principal->lki, 0, sizeof(principal->lki));
2593 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2595 principal->lan = lan;
2596 principal->ltx = ltx;
2597 principal->lrx = lrx;
2602 kay->ltx_kn = lki->kn;
2603 kay->lrx_kn = lki->kn;
2613 * ieee802_1x_kay_set_old_sa_attr -
2615 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2616 struct ieee802_1x_mka_ki *oki,
2617 u8 oan, Boolean otx, Boolean orx)
2619 struct ieee802_1x_mka_participant *principal;
2621 principal = ieee802_1x_kay_get_principal_participant(kay);
2626 os_memset(&principal->oki, 0, sizeof(principal->oki));
2628 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2630 principal->oan = oan;
2631 principal->otx = otx;
2632 principal->orx = orx;
2638 kay->otx_kn = oki->kn;
2639 kay->orx_kn = oki->kn;
2649 * ieee802_1x_kay_create_sas -
2651 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2652 struct ieee802_1x_mka_ki *lki)
2654 struct data_key *sa_key, *latest_sak;
2655 struct ieee802_1x_mka_participant *principal;
2656 struct receive_sc *rxsc;
2657 struct receive_sa *rxsa;
2658 struct transmit_sa *txsa;
2660 principal = ieee802_1x_kay_get_principal_participant(kay);
2665 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2666 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2667 sa_key->rx_latest = TRUE;
2668 sa_key->tx_latest = TRUE;
2669 latest_sak = sa_key;
2670 principal->to_use_sak = TRUE;
2672 sa_key->rx_latest = FALSE;
2673 sa_key->tx_latest = FALSE;
2677 wpa_printf(MSG_ERROR, "lki related sak not found");
2681 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2682 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2687 secy_create_receive_sa(kay, rxsa);
2690 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2695 secy_create_transmit_sa(kay, txsa);
2704 * ieee802_1x_kay_delete_sas -
2706 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2707 struct ieee802_1x_mka_ki *ki)
2709 struct data_key *sa_key, *pre_key;
2710 struct transmit_sa *txsa, *pre_txsa;
2711 struct receive_sa *rxsa, *pre_rxsa;
2712 struct receive_sc *rxsc;
2713 struct ieee802_1x_mka_participant *principal;
2715 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2716 principal = ieee802_1x_kay_get_principal_participant(kay);
2720 /* remove the transmit sa */
2721 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2722 struct transmit_sa, list) {
2723 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2724 secy_disable_transmit_sa(kay, txsa);
2725 ieee802_1x_kay_deinit_transmit_sa(txsa);
2729 /* remove the receive sa */
2730 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2731 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2732 struct receive_sa, list) {
2733 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2734 secy_disable_receive_sa(kay, rxsa);
2735 ieee802_1x_kay_deinit_receive_sa(rxsa);
2740 /* remove the sak */
2741 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2742 struct data_key, list) {
2743 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2744 ieee802_1x_kay_deinit_data_key(sa_key);
2747 if (principal->new_key == sa_key)
2748 principal->new_key = NULL;
2756 * ieee802_1x_kay_enable_tx_sas -
2758 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2759 struct ieee802_1x_mka_ki *lki)
2761 struct ieee802_1x_mka_participant *principal;
2762 struct transmit_sa *txsa;
2764 principal = ieee802_1x_kay_get_principal_participant(kay);
2768 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2770 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2771 txsa->in_use = TRUE;
2772 secy_enable_transmit_sa(kay, txsa);
2773 ieee802_1x_cp_set_usingtransmitas(
2774 principal->kay->cp, TRUE);
2775 ieee802_1x_cp_sm_step(principal->kay->cp);
2784 * ieee802_1x_kay_enable_rx_sas -
2786 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2787 struct ieee802_1x_mka_ki *lki)
2789 struct ieee802_1x_mka_participant *principal;
2790 struct receive_sa *rxsa;
2791 struct receive_sc *rxsc;
2793 principal = ieee802_1x_kay_get_principal_participant(kay);
2797 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2798 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2800 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2801 rxsa->in_use = TRUE;
2802 secy_enable_receive_sa(kay, rxsa);
2803 ieee802_1x_cp_set_usingreceivesas(
2804 principal->kay->cp, TRUE);
2805 ieee802_1x_cp_sm_step(principal->kay->cp);
2815 * ieee802_1x_kay_enable_new_info -
2817 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2819 struct ieee802_1x_mka_participant *principal;
2821 principal = ieee802_1x_kay_get_principal_participant(kay);
2825 if (principal->retry_count < MAX_RETRY_CNT) {
2826 ieee802_1x_participant_send_mkpdu(principal);
2827 principal->retry_count++;
2835 * ieee802_1x_kay_cp_conf -
2837 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2838 struct ieee802_1x_cp_conf *pconf)
2840 pconf->protect = kay->macsec_protect;
2841 pconf->replay_protect = kay->macsec_replay_protect;
2842 pconf->validate = kay->macsec_validate;
2849 * ieee802_1x_kay_alloc_cp_sm -
2851 static struct ieee802_1x_cp_sm *
2852 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2854 struct ieee802_1x_cp_conf conf;
2856 os_memset(&conf, 0, sizeof(conf));
2857 conf.protect = kay->macsec_protect;
2858 conf.replay_protect = kay->macsec_replay_protect;
2859 conf.validate = kay->macsec_validate;
2860 conf.replay_window = kay->macsec_replay_window;
2862 return ieee802_1x_cp_sm_init(kay, &conf);
2867 * ieee802_1x_kay_mkpdu_sanity_check -
2868 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2870 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2871 const u8 *buf, size_t len)
2873 struct ieee8023_hdr *eth_hdr;
2874 struct ieee802_1x_hdr *eapol_hdr;
2875 struct ieee802_1x_mka_hdr *mka_hdr;
2876 struct ieee802_1x_mka_basic_body *body;
2878 struct ieee802_1x_mka_participant *participant;
2880 u8 icv[MAX_ICV_LEN];
2883 eth_hdr = (struct ieee8023_hdr *) buf;
2884 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2885 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2887 /* destination address should be not individual address */
2888 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2889 wpa_printf(MSG_MSGDUMP,
2890 "KaY: ethernet destination address is not PAE group address");
2894 /* MKPDU should not be less than 32 octets */
2895 mka_msg_len = be_to_host16(eapol_hdr->length);
2896 if (mka_msg_len < 32) {
2897 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2900 /* MKPDU should be a multiple of 4 octets */
2901 if ((mka_msg_len % 4) != 0) {
2902 wpa_printf(MSG_MSGDUMP,
2903 "KaY: MKPDU is not multiple of 4 octets");
2907 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2908 ieee802_1x_mka_dump_basic_body(body);
2909 body_len = get_mka_param_body_len(body);
2910 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2911 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2912 wpa_printf(MSG_ERROR,
2913 "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",
2914 mka_msg_len, MKA_HDR_LEN,
2915 body_len, DEFAULT_ICV_LEN);
2919 /* CKN should be owned by I */
2920 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2922 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2926 /* algorithm agility check */
2927 if (os_memcmp(body->algo_agility, mka_algo_agility,
2928 sizeof(body->algo_agility)) != 0) {
2929 wpa_printf(MSG_ERROR,
2930 "KaY: peer's algorithm agility not supported for me");
2936 * The ICV will comprise the final octets of the packet body, whatever
2937 * its size, not the fixed length 16 octets, indicated by the EAPOL
2938 * packet body length.
2940 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2941 participant->ick.key,
2942 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2943 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2947 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2950 wpa_printf(MSG_ERROR, "KaY: No ICV");
2953 if (os_memcmp_const(msg_icv, icv,
2954 mka_alg_tbl[kay->mka_algindex].icv_len) != 0) {
2955 wpa_printf(MSG_ERROR,
2956 "KaY: Computed ICV is not equal to Received ICV");
2965 * ieee802_1x_kay_decode_mkpdu -
2967 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2968 const u8 *buf, size_t len)
2970 struct ieee802_1x_mka_participant *participant;
2971 struct ieee802_1x_mka_hdr *hdr;
2977 Boolean my_included;
2978 Boolean handled[256];
2980 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2983 /* handle basic parameter set */
2984 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2985 left_len = len - sizeof(struct ieee8023_hdr) -
2986 sizeof(struct ieee802_1x_hdr);
2987 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2991 /* to skip basic parameter set */
2992 hdr = (struct ieee802_1x_mka_hdr *) pos;
2993 body_len = get_mka_param_body_len(hdr);
2994 pos += body_len + MKA_HDR_LEN;
2995 left_len -= body_len + MKA_HDR_LEN;
2997 /* check i am in the peer's peer list */
2998 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3000 /* accept the peer as live peer */
3001 if (!ieee802_1x_kay_is_in_peer(
3003 participant->current_peer_id.mi)) {
3004 if (!ieee802_1x_kay_create_live_peer(
3006 participant->current_peer_id.mi,
3008 participant->current_peer_id.mn)))
3010 ieee802_1x_kay_elect_key_server(participant);
3011 ieee802_1x_kay_decide_macsec_use(participant);
3013 if (ieee802_1x_kay_is_in_potential_peer(
3014 participant, participant->current_peer_id.mi)) {
3015 if (!ieee802_1x_kay_move_live_peer(
3017 participant->current_peer_id.mi,
3018 be_to_host32(participant->
3019 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 for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
3035 pos += body_len + MKA_HDR_LEN,
3036 left_len -= body_len + MKA_HDR_LEN) {
3037 hdr = (struct ieee802_1x_mka_hdr *) pos;
3038 body_len = get_mka_param_body_len(hdr);
3039 body_type = get_mka_param_body_type(hdr);
3041 if (body_type == MKA_ICV_INDICATOR)
3044 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3045 wpa_printf(MSG_ERROR,
3046 "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
3047 left_len, MKA_HDR_LEN,
3048 body_len, DEFAULT_ICV_LEN);
3052 if (handled[body_type])
3055 handled[body_type] = TRUE;
3056 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3057 mka_body_handler[body_type].body_rx) {
3058 mka_body_handler[body_type].body_rx
3059 (participant, pos, left_len);
3061 wpa_printf(MSG_ERROR,
3062 "The type %d is not supported in this MKA version %d",
3063 body_type, MKA_VERSION_ID);
3068 participant->retry_count = 0;
3069 participant->active = TRUE;
3076 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3079 struct ieee802_1x_kay *kay = ctx;
3080 struct ieee8023_hdr *eth_hdr;
3081 struct ieee802_1x_hdr *eapol_hdr;
3083 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3084 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3085 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3086 (unsigned long) len);
3090 eth_hdr = (struct ieee8023_hdr *) buf;
3091 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3092 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3093 be_to_host16(eapol_hdr->length)) {
3094 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3095 (unsigned long) len,
3096 (unsigned long) be_to_host16(eapol_hdr->length));
3100 if (eapol_hdr->version < EAPOL_VERSION) {
3101 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3102 eapol_hdr->version);
3105 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3106 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3109 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3110 if (dl_list_empty(&kay->participant_list)) {
3111 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3115 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3120 * ieee802_1x_kay_init -
3122 struct ieee802_1x_kay *
3123 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3124 const char *ifname, const u8 *addr)
3126 struct ieee802_1x_kay *kay;
3128 kay = os_zalloc(sizeof(*kay));
3130 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3137 kay->active = FALSE;
3139 kay->authenticated = FALSE;
3140 kay->secured = FALSE;
3141 kay->failed = FALSE;
3142 kay->policy = policy;
3144 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3145 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3146 kay->actor_sci.port = host_to_be16(0x0001);
3147 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3149 /* While actor acts as a key server, shall distribute sakey */
3154 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3155 kay->macsec_csindex = DEFAULT_CS_INDEX;
3156 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3157 kay->mka_version = MKA_VERSION_ID;
3159 os_memcpy(kay->algo_agility, mka_algo_agility,
3160 sizeof(kay->algo_agility));
3162 dl_list_init(&kay->participant_list);
3164 if (policy == DO_NOT_SECURE) {
3165 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3166 kay->macsec_desired = FALSE;
3167 kay->macsec_protect = FALSE;
3168 kay->macsec_validate = Disabled;
3169 kay->macsec_replay_protect = FALSE;
3170 kay->macsec_replay_window = 0;
3171 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3173 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3174 kay->macsec_desired = TRUE;
3175 kay->macsec_protect = TRUE;
3176 kay->macsec_validate = Strict;
3177 kay->macsec_replay_protect = FALSE;
3178 kay->macsec_replay_window = 0;
3179 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3182 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3184 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3185 secy_init_macsec(kay);
3186 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3188 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3191 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3192 if (kay->cp == NULL) {
3193 ieee802_1x_kay_deinit(kay);
3197 if (policy == DO_NOT_SECURE) {
3198 ieee802_1x_cp_connect_authenticated(kay->cp);
3199 ieee802_1x_cp_sm_step(kay->cp);
3201 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3202 kay_l2_receive, kay, 1);
3203 if (kay->l2_mka == NULL) {
3204 wpa_printf(MSG_WARNING,
3205 "KaY: Failed to initialize L2 packet processing for MKA packet");
3206 ieee802_1x_kay_deinit(kay);
3216 * ieee802_1x_kay_deinit -
3219 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3221 struct ieee802_1x_mka_participant *participant;
3226 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3228 while (!dl_list_empty(&kay->participant_list)) {
3229 participant = dl_list_entry(kay->participant_list.next,
3230 struct ieee802_1x_mka_participant,
3232 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3235 ieee802_1x_cp_sm_deinit(kay->cp);
3236 secy_deinit_macsec(kay);
3239 l2_packet_deinit(kay->l2_mka);
3249 * ieee802_1x_kay_create_mka -
3251 struct ieee802_1x_mka_participant *
3252 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3253 struct mka_key *cak, u32 life,
3254 enum mka_created_mode mode, Boolean is_authenticator)
3256 struct ieee802_1x_mka_participant *participant;
3259 if (!kay || !ckn || !cak) {
3260 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3264 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3265 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3268 if (ckn->len > MAX_CKN_LEN) {
3269 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3273 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3277 participant = os_zalloc(sizeof(*participant));
3279 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3283 participant->ckn.len = ckn->len;
3284 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3285 participant->cak.len = cak->len;
3286 os_memcpy(participant->cak.key, cak->key, cak->len);
3288 participant->cak_life = life + time(NULL);
3292 if (is_authenticator) {
3293 participant->is_obliged_key_server = TRUE;
3294 participant->can_be_key_server = TRUE;
3295 participant->is_key_server = TRUE;
3296 participant->principal = TRUE;
3298 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3299 sizeof(kay->key_server_sci));
3300 kay->key_server_priority = kay->actor_priority;
3301 participant->is_elected = TRUE;
3303 participant->is_obliged_key_server = FALSE;
3304 participant->can_be_key_server = FALSE;
3305 participant->is_key_server = FALSE;
3306 participant->is_elected = TRUE;
3311 participant->is_obliged_key_server = FALSE;
3312 participant->can_be_key_server = TRUE;
3313 participant->is_key_server = TRUE;
3314 participant->is_elected = FALSE;
3318 participant->cached = FALSE;
3320 participant->active = FALSE;
3321 participant->participant = FALSE;
3322 participant->retain = FALSE;
3323 participant->activate = DEFAULT;
3325 if (participant->is_key_server)
3326 participant->principal = TRUE;
3328 dl_list_init(&participant->live_peers);
3329 dl_list_init(&participant->potential_peers);
3331 participant->retry_count = 0;
3332 participant->kay = kay;
3334 if (!reset_participant_mi(participant))
3337 participant->lrx = FALSE;
3338 participant->ltx = FALSE;
3339 participant->orx = FALSE;
3340 participant->otx = FALSE;
3341 participant->to_dist_sak = FALSE;
3342 participant->to_use_sak = FALSE;
3343 participant->new_sak = FALSE;
3344 dl_list_init(&participant->sak_list);
3345 participant->new_key = NULL;
3346 dl_list_init(&participant->rxsc_list);
3347 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3349 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3350 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3351 kay->macsec_replay_window);
3352 secy_create_transmit_sc(kay, participant->txsc);
3354 /* to derive KEK from CAK and CKN */
3355 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3356 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3357 participant->ckn.name,
3358 participant->ckn.len,
3359 participant->kek.key)) {
3360 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3363 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3364 participant->kek.key, participant->kek.len);
3366 /* to derive ICK from CAK and CKN */
3367 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3368 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3369 participant->ckn.name,
3370 participant->ckn.len,
3371 participant->ick.key)) {
3372 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3375 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3376 participant->ick.key, participant->ick.len);
3378 dl_list_add(&kay->participant_list, &participant->list);
3379 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3380 ckn->name, ckn->len);
3382 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3383 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3385 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3391 os_free(participant);
3397 * ieee802_1x_kay_delete_mka -
3400 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3402 struct ieee802_1x_mka_participant *participant;
3403 struct ieee802_1x_kay_peer *peer;
3404 struct data_key *sak;
3405 struct receive_sc *rxsc;
3410 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3412 /* get the participant */
3413 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3415 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3416 ckn->name, ckn->len);
3420 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3421 dl_list_del(&participant->list);
3423 /* remove live peer */
3424 while (!dl_list_empty(&participant->live_peers)) {
3425 peer = dl_list_entry(participant->live_peers.next,
3426 struct ieee802_1x_kay_peer, list);
3427 dl_list_del(&peer->list);
3431 /* remove potential peer */
3432 while (!dl_list_empty(&participant->potential_peers)) {
3433 peer = dl_list_entry(participant->potential_peers.next,
3434 struct ieee802_1x_kay_peer, list);
3435 dl_list_del(&peer->list);
3440 while (!dl_list_empty(&participant->sak_list)) {
3441 sak = dl_list_entry(participant->sak_list.next,
3442 struct data_key, list);
3443 dl_list_del(&sak->list);
3447 while (!dl_list_empty(&participant->rxsc_list)) {
3448 rxsc = dl_list_entry(participant->rxsc_list.next,
3449 struct receive_sc, list);
3450 secy_delete_receive_sc(kay, rxsc);
3451 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3453 secy_delete_transmit_sc(kay, participant->txsc);
3454 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3456 os_memset(&participant->cak, 0, sizeof(participant->cak));
3457 os_memset(&participant->kek, 0, sizeof(participant->kek));
3458 os_memset(&participant->ick, 0, sizeof(participant->ick));
3459 os_free(participant);
3464 * ieee802_1x_kay_mka_participate -
3466 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3467 struct mka_key_name *ckn,
3470 struct ieee802_1x_mka_participant *participant;
3475 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3479 participant->active = status;
3484 * ieee802_1x_kay_new_sak -
3487 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3489 struct ieee802_1x_mka_participant *participant;
3494 participant = ieee802_1x_kay_get_principal_participant(kay);
3498 participant->new_sak = TRUE;
3499 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3506 * ieee802_1x_kay_change_cipher_suite -
3509 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3511 struct ieee802_1x_mka_participant *participant;
3516 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3517 wpa_printf(MSG_ERROR,
3518 "KaY: Configured cipher suite index is out of range");
3521 if (kay->macsec_csindex == cs_index)
3525 kay->macsec_desired = FALSE;
3527 kay->macsec_csindex = cs_index;
3528 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3530 participant = ieee802_1x_kay_get_principal_participant(kay);
3532 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3533 participant->new_sak = TRUE;