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 if (!participant->to_dist_sak || !participant->new_key)
1406 * ieee802_1x_kay_get_dist_sak_length
1409 ieee802_1x_mka_get_dist_sak_length(
1410 struct ieee802_1x_mka_participant *participant)
1412 int length = MKA_HDR_LEN;
1413 int cs_index = participant->kay->macsec_csindex;
1415 if (participant->advised_desired) {
1416 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1417 if (cs_index != DEFAULT_CS_INDEX)
1418 length += CS_ID_LEN;
1420 length += cipher_suite_tbl[cs_index].sak_len + 8;
1423 return MKA_ALIGN_LENGTH(length);
1428 * ieee802_1x_mka_encode_dist_sak_body -
1431 ieee802_1x_mka_encode_dist_sak_body(
1432 struct ieee802_1x_mka_participant *participant,
1435 struct ieee802_1x_mka_dist_sak_body *body;
1436 struct data_key *sak;
1437 unsigned int length;
1441 length = ieee802_1x_mka_get_dist_sak_length(participant);
1442 body = wpabuf_put(buf, length);
1443 body->type = MKA_DISTRIBUTED_SAK;
1444 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1445 if (length == MKA_HDR_LEN) {
1446 body->confid_offset = 0;
1451 sak = participant->new_key;
1452 body->confid_offset = sak->confidentiality_offset;
1453 body->dan = sak->an;
1454 body->kn = host_to_be32(sak->key_identifier.kn);
1455 cs_index = participant->kay->macsec_csindex;
1457 if (cs_index != DEFAULT_CS_INDEX) {
1458 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1459 sak_pos = CS_ID_LEN;
1461 if (aes_wrap(participant->kek.key, 16,
1462 cipher_suite_tbl[cs_index].sak_len / 8,
1463 sak->key, body->sak + sak_pos)) {
1464 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1468 ieee802_1x_mka_dump_dist_sak_body(body);
1475 * ieee802_1x_kay_init_data_key -
1477 static struct data_key *
1478 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1480 struct data_key *pkey;
1485 pkey = os_zalloc(sizeof(*pkey));
1487 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1491 pkey->key = os_zalloc(conf->key_len);
1492 if (pkey->key == NULL) {
1493 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1498 os_memcpy(pkey->key, conf->key, conf->key_len);
1499 os_memcpy(&pkey->key_identifier, &conf->ki,
1500 sizeof(pkey->key_identifier));
1501 pkey->confidentiality_offset = conf->offset;
1502 pkey->an = conf->an;
1503 pkey->transmits = conf->tx;
1504 pkey->receives = conf->rx;
1505 os_get_time(&pkey->created_time);
1514 * ieee802_1x_kay_decode_dist_sak_body -
1517 ieee802_1x_mka_decode_dist_sak_body(
1518 struct ieee802_1x_mka_participant *participant,
1519 const u8 *mka_msg, size_t msg_len)
1521 struct ieee802_1x_mka_hdr *hdr;
1522 struct ieee802_1x_mka_dist_sak_body *body;
1523 struct ieee802_1x_kay_peer *peer;
1524 struct macsec_ciphersuite *cs;
1526 struct key_conf *conf;
1527 struct data_key *sa_key = NULL;
1528 struct ieee802_1x_mka_ki sak_ki;
1532 struct ieee802_1x_kay *kay = participant->kay;
1534 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1535 body_len = get_mka_param_body_len(hdr);
1536 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1537 wpa_printf(MSG_ERROR,
1538 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1543 if (!participant->principal) {
1544 wpa_printf(MSG_ERROR,
1545 "KaY: I can't accept the distributed SAK as I am not principal");
1548 if (participant->is_key_server) {
1549 wpa_printf(MSG_ERROR,
1550 "KaY: I can't accept the distributed SAK as myself is key server ");
1553 if (!kay->macsec_desired ||
1554 kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1555 wpa_printf(MSG_ERROR,
1556 "KaY: I am not MACsec-desired or without MACsec capable");
1560 peer = ieee802_1x_kay_get_live_peer(participant,
1561 participant->current_peer_id.mi);
1563 wpa_printf(MSG_ERROR,
1564 "KaY: The key server is not in my live peers list");
1567 if (!sci_equal(&kay->key_server_sci, &peer->sci)) {
1568 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1572 if (body_len == 0) {
1573 kay->authenticated = TRUE;
1574 kay->secured = FALSE;
1575 kay->failed = FALSE;
1576 participant->advised_desired = FALSE;
1577 ieee802_1x_cp_connect_authenticated(kay->cp);
1578 ieee802_1x_cp_sm_step(kay->cp);
1579 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1580 participant->to_use_sak = TRUE;
1584 participant->advised_desired = TRUE;
1585 kay->authenticated = FALSE;
1586 kay->secured = TRUE;
1587 kay->failed = FALSE;
1588 ieee802_1x_cp_connect_secure(kay->cp);
1589 ieee802_1x_cp_sm_step(kay->cp);
1591 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1592 ieee802_1x_mka_dump_dist_sak_body(body);
1593 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1595 if (os_memcmp(sa_key->key_identifier.mi,
1596 participant->current_peer_id.mi, MI_LEN) == 0 &&
1597 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1598 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1603 if (body_len == 28) {
1604 sak_len = DEFAULT_SA_KEY_LEN;
1605 wrap_sak = body->sak;
1606 kay->macsec_csindex = DEFAULT_CS_INDEX;
1608 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1610 wpa_printf(MSG_ERROR,
1611 "KaY: I can't support the Cipher Suite advised by key server");
1614 sak_len = cs->sak_len;
1615 wrap_sak = body->sak + CS_ID_LEN;
1616 kay->macsec_csindex = cs->index;
1619 unwrap_sak = os_zalloc(sak_len);
1621 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1624 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1626 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1627 os_free(unwrap_sak);
1630 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1632 conf = os_zalloc(sizeof(*conf));
1634 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1635 os_free(unwrap_sak);
1638 conf->key_len = sak_len;
1640 conf->key = os_zalloc(conf->key_len);
1642 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1643 os_free(unwrap_sak);
1648 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1650 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1652 sak_ki.kn = be_to_host32(body->kn);
1654 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1655 conf->ki.kn = sak_ki.kn;
1656 conf->an = body->dan;
1657 conf->offset = body->confid_offset;
1661 sa_key = ieee802_1x_kay_init_data_key(conf);
1663 os_free(unwrap_sak);
1669 dl_list_add(&participant->sak_list, &sa_key->list);
1671 ieee802_1x_cp_set_ciphersuite(kay->cp,
1672 cipher_suite_tbl[kay->macsec_csindex].id);
1673 ieee802_1x_cp_sm_step(kay->cp);
1674 ieee802_1x_cp_set_offset(kay->cp, body->confid_offset);
1675 ieee802_1x_cp_sm_step(kay->cp);
1676 ieee802_1x_cp_set_distributedki(kay->cp, &sak_ki);
1677 ieee802_1x_cp_set_distributedan(kay->cp, body->dan);
1678 ieee802_1x_cp_signal_newsak(kay->cp);
1679 ieee802_1x_cp_sm_step(kay->cp);
1681 participant->to_use_sak = TRUE;
1683 os_free(unwrap_sak);
1692 * ieee802_1x_mka_icv_body_present
1695 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1702 * ieee802_1x_kay_get_icv_length
1705 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1709 length = sizeof(struct ieee802_1x_mka_icv_body);
1710 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1712 return MKA_ALIGN_LENGTH(length);
1717 * ieee802_1x_mka_encode_icv_body -
1720 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1723 struct ieee802_1x_mka_icv_body *body;
1724 unsigned int length;
1725 u8 cmac[MAX_ICV_LEN];
1727 length = ieee802_1x_mka_get_icv_length(participant);
1728 if (length != DEFAULT_ICV_LEN) {
1729 body = wpabuf_put(buf, MKA_HDR_LEN);
1730 body->type = MKA_ICV_INDICATOR;
1731 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1734 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1735 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1736 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1740 if (length != DEFAULT_ICV_LEN)
1741 length -= MKA_HDR_LEN;
1742 os_memcpy(wpabuf_put(buf, length), cmac, length);
1748 * ieee802_1x_mka_decode_icv_body -
1751 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1752 const u8 *mka_msg, size_t msg_len)
1754 struct ieee802_1x_mka_hdr *hdr;
1755 struct ieee802_1x_mka_icv_body *body;
1763 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1764 hdr = (struct ieee802_1x_mka_hdr *) pos;
1765 body_len = get_mka_param_body_len(hdr);
1766 body_type = get_mka_param_body_type(hdr);
1768 if (left_len < (body_len + MKA_HDR_LEN))
1771 if (body_type != MKA_ICV_INDICATOR) {
1772 left_len -= MKA_HDR_LEN + body_len;
1773 pos += MKA_HDR_LEN + body_len;
1777 body = (struct ieee802_1x_mka_icv_body *)pos;
1779 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1786 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1791 * ieee802_1x_mka_decode_dist_cak_body-
1794 ieee802_1x_mka_decode_dist_cak_body(
1795 struct ieee802_1x_mka_participant *participant,
1796 const u8 *mka_msg, size_t msg_len)
1798 struct ieee802_1x_mka_hdr *hdr;
1801 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1802 body_len = get_mka_param_body_len(hdr);
1803 if (body_len < 28) {
1804 wpa_printf(MSG_ERROR,
1805 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
1815 * ieee802_1x_mka_decode_kmd_body -
1818 ieee802_1x_mka_decode_kmd_body(
1819 struct ieee802_1x_mka_participant *participant,
1820 const u8 *mka_msg, size_t msg_len)
1822 struct ieee802_1x_mka_hdr *hdr;
1825 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1826 body_len = get_mka_param_body_len(hdr);
1828 wpa_printf(MSG_ERROR,
1829 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
1839 * ieee802_1x_mka_decode_announce_body -
1841 static int ieee802_1x_mka_decode_announce_body(
1842 struct ieee802_1x_mka_participant *participant,
1843 const u8 *mka_msg, size_t msg_len)
1849 struct mka_param_body_handler {
1850 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
1851 struct wpabuf *buf);
1852 int (*body_rx)(struct ieee802_1x_mka_participant *participant,
1853 const u8 *mka_msg, size_t msg_len);
1854 int (*body_length)(struct ieee802_1x_mka_participant *participant);
1855 Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
1859 static struct mka_param_body_handler mka_body_handler[] = {
1860 /* basic parameter set */
1862 .body_tx = ieee802_1x_mka_encode_basic_body,
1864 .body_length = ieee802_1x_mka_basic_body_length,
1865 .body_present = ieee802_1x_mka_basic_body_present
1868 /* live peer list parameter set */
1870 .body_tx = ieee802_1x_mka_encode_live_peer_body,
1871 .body_rx = ieee802_1x_mka_decode_live_peer_body,
1872 .body_length = ieee802_1x_mka_get_live_peer_length,
1873 .body_present = ieee802_1x_mka_live_peer_body_present
1876 /* potential peer list parameter set */
1878 .body_tx = ieee802_1x_mka_encode_potential_peer_body,
1879 .body_rx = ieee802_1x_mka_decode_potential_peer_body,
1880 .body_length = ieee802_1x_mka_get_potential_peer_length,
1881 .body_present = ieee802_1x_mka_potential_peer_body_present
1884 /* sak use parameter set */
1886 .body_tx = ieee802_1x_mka_encode_sak_use_body,
1887 .body_rx = ieee802_1x_mka_decode_sak_use_body,
1888 .body_length = ieee802_1x_mka_get_sak_use_length,
1889 .body_present = ieee802_1x_mka_sak_use_body_present
1892 /* distribute sak parameter set */
1894 .body_tx = ieee802_1x_mka_encode_dist_sak_body,
1895 .body_rx = ieee802_1x_mka_decode_dist_sak_body,
1896 .body_length = ieee802_1x_mka_get_dist_sak_length,
1897 .body_present = ieee802_1x_mka_dist_sak_body_present
1900 /* distribute cak parameter set */
1903 .body_rx = ieee802_1x_mka_decode_dist_cak_body,
1904 .body_length = NULL,
1905 .body_present = NULL
1908 /* kmd parameter set */
1911 .body_rx = ieee802_1x_mka_decode_kmd_body,
1912 .body_length = NULL,
1913 .body_present = NULL
1916 /* announce parameter set */
1919 .body_rx = ieee802_1x_mka_decode_announce_body,
1920 .body_length = NULL,
1921 .body_present = NULL
1924 /* icv parameter set */
1926 .body_tx = ieee802_1x_mka_encode_icv_body,
1928 .body_length = ieee802_1x_mka_get_icv_length,
1929 .body_present = ieee802_1x_mka_icv_body_present
1935 * ieee802_1x_kay_deinit_data_key -
1937 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1946 dl_list_del(&pkey->list);
1953 * ieee802_1x_kay_generate_new_sak -
1956 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1958 struct data_key *sa_key = NULL;
1959 struct key_conf *conf;
1960 struct ieee802_1x_kay_peer *peer;
1961 struct ieee802_1x_kay *kay = participant->kay;
1962 int ctx_len, ctx_offset;
1965 /* check condition for generating a fresh SAK:
1966 * must have one live peer
1967 * and MKA life time elapse since last distribution
1968 * or potential peer is empty
1970 if (dl_list_empty(&participant->live_peers)) {
1971 wpa_printf(MSG_ERROR,
1972 "KaY: Live peers list must not empty when generating fresh SAK");
1976 /* FIXME: A fresh SAK not generated until
1977 * the live peer list contains at least one peer and
1978 * MKA life time has elapsed since the prior SAK was first distributed,
1979 * or the Key server's potential peer is empty
1980 * but I can't understand the second item, so
1981 * here only check first item and ingore
1982 * && (!dl_list_empty(&participant->potential_peers))) {
1984 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
1985 wpa_printf(MSG_ERROR,
1986 "KaY: Life time have not elapsed since prior SAK distributed");
1990 conf = os_zalloc(sizeof(*conf));
1992 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1995 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
1997 conf->key = os_zalloc(conf->key_len);
2000 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2004 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2005 dl_list_for_each(peer, &participant->live_peers,
2006 struct ieee802_1x_kay_peer, list)
2007 ctx_len += sizeof(peer->mi);
2008 ctx_len += sizeof(participant->mi);
2010 context = os_zalloc(ctx_len);
2017 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2023 ctx_offset += conf->key_len;
2024 dl_list_for_each(peer, &participant->live_peers,
2025 struct ieee802_1x_kay_peer, list) {
2026 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2027 ctx_offset += sizeof(peer->mi);
2029 os_memcpy(context + ctx_offset, participant->mi,
2030 sizeof(participant->mi));
2031 ctx_offset += sizeof(participant->mi);
2032 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2034 if (conf->key_len == 16) {
2035 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2036 context, ctx_len, conf->key);
2037 } else if (conf->key_len == 32) {
2038 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2039 context, ctx_len, conf->key);
2041 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2047 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2048 conf->key, conf->key_len);
2050 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2051 conf->ki.kn = kay->dist_kn;
2052 conf->an = kay->dist_an;
2053 conf->offset = kay->macsec_confidentiality;
2057 sa_key = ieee802_1x_kay_init_data_key(conf);
2064 participant->new_key = sa_key;
2066 dl_list_add(&participant->sak_list, &sa_key->list);
2067 ieee802_1x_cp_set_ciphersuite(kay->cp,
2068 cipher_suite_tbl[kay->macsec_csindex].id);
2069 ieee802_1x_cp_sm_step(kay->cp);
2070 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2071 ieee802_1x_cp_sm_step(kay->cp);
2072 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2073 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2074 ieee802_1x_cp_signal_newsak(kay->cp);
2075 ieee802_1x_cp_sm_step(kay->cp);
2077 dl_list_for_each(peer, &participant->live_peers,
2078 struct ieee802_1x_kay_peer, list)
2079 peer->sak_used = FALSE;
2083 if (kay->dist_an > 3)
2086 kay->dist_time = time(NULL);
2096 * ieee802_1x_kay_elect_key_server - elect the key server
2097 * when to elect: whenever the live peers list changes
2100 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2102 struct ieee802_1x_kay_peer *peer;
2103 struct ieee802_1x_kay_peer *key_server = NULL;
2104 struct ieee802_1x_kay *kay = participant->kay;
2105 Boolean i_is_key_server;
2107 if (participant->is_obliged_key_server) {
2108 participant->new_sak = TRUE;
2109 participant->to_dist_sak = FALSE;
2110 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2114 /* elect the key server among the peers */
2115 dl_list_for_each(peer, &participant->live_peers,
2116 struct ieee802_1x_kay_peer, list) {
2117 if (!peer->is_key_server)
2125 if (peer->key_server_priority <
2126 key_server->key_server_priority) {
2128 } else if (peer->key_server_priority ==
2129 key_server->key_server_priority) {
2130 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2136 /* elect the key server between me and the above elected peer */
2137 i_is_key_server = FALSE;
2138 if (key_server && participant->can_be_key_server) {
2139 if (kay->actor_priority
2140 < key_server->key_server_priority) {
2141 i_is_key_server = TRUE;
2142 } else if (kay->actor_priority
2143 == key_server->key_server_priority) {
2144 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2146 i_is_key_server = TRUE;
2148 } else if (participant->can_be_key_server) {
2149 i_is_key_server = TRUE;
2152 if (i_is_key_server) {
2153 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2154 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2155 ieee802_1x_cp_signal_chgdserver(kay->cp);
2156 ieee802_1x_cp_sm_step(kay->cp);
2159 participant->is_key_server = TRUE;
2160 participant->principal = TRUE;
2161 participant->new_sak = TRUE;
2162 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2163 participant->to_dist_sak = FALSE;
2164 participant->is_elected = TRUE;
2166 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2167 sizeof(kay->key_server_sci));
2168 kay->key_server_priority = kay->actor_priority;
2169 } else if (key_server) {
2170 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2171 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2172 ieee802_1x_cp_signal_chgdserver(kay->cp);
2173 ieee802_1x_cp_sm_step(kay->cp);
2176 participant->is_key_server = FALSE;
2177 participant->principal = TRUE;
2178 participant->is_elected = TRUE;
2180 os_memcpy(&kay->key_server_sci, &key_server->sci,
2181 sizeof(kay->key_server_sci));
2182 kay->key_server_priority = key_server->key_server_priority;
2184 participant->principal = FALSE;
2185 participant->is_key_server = FALSE;
2186 participant->is_elected = FALSE;
2194 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2195 * how to use MACsec: whether use MACsec and its capability
2196 * protectFrames will be advised if the key server and one of its live peers are
2197 * MACsec capable and one of those request MACsec protection
2200 ieee802_1x_kay_decide_macsec_use(
2201 struct ieee802_1x_mka_participant *participant)
2203 struct ieee802_1x_kay *kay = participant->kay;
2204 struct ieee802_1x_kay_peer *peer;
2205 enum macsec_cap less_capability;
2208 if (!participant->is_key_server)
2211 /* key server self is MACsec-desired and requesting MACsec */
2212 if (!kay->macsec_desired) {
2213 participant->advised_desired = FALSE;
2216 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2217 participant->advised_desired = FALSE;
2220 less_capability = kay->macsec_capable;
2222 /* at least one of peers is MACsec-desired and requesting MACsec */
2224 dl_list_for_each(peer, &participant->live_peers,
2225 struct ieee802_1x_kay_peer, list) {
2226 if (!peer->macsec_desired)
2229 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2232 less_capability = (less_capability < peer->macsec_capability) ?
2233 less_capability : peer->macsec_capability;
2238 participant->advised_desired = TRUE;
2239 participant->advised_capability = less_capability;
2240 kay->authenticated = FALSE;
2241 kay->secured = TRUE;
2242 kay->failed = FALSE;
2243 ieee802_1x_cp_connect_secure(kay->cp);
2244 ieee802_1x_cp_sm_step(kay->cp);
2246 participant->advised_desired = FALSE;
2247 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2248 participant->to_use_sak = FALSE;
2249 kay->authenticated = TRUE;
2250 kay->secured = FALSE;
2251 kay->failed = FALSE;
2260 ieee802_1x_cp_connect_authenticated(kay->cp);
2261 ieee802_1x_cp_sm_step(kay->cp);
2267 static const u8 pae_group_addr[ETH_ALEN] = {
2268 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2273 * ieee802_1x_kay_encode_mkpdu -
2276 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2277 struct wpabuf *pbuf)
2280 struct ieee8023_hdr *ether_hdr;
2281 struct ieee802_1x_hdr *eapol_hdr;
2283 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2284 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2285 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2286 sizeof(ether_hdr->dest));
2287 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2289 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2290 eapol_hdr->version = EAPOL_VERSION;
2291 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2292 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2294 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2295 if (mka_body_handler[i].body_present &&
2296 mka_body_handler[i].body_present(participant)) {
2297 if (mka_body_handler[i].body_tx(participant, pbuf))
2306 * ieee802_1x_participant_send_mkpdu -
2309 ieee802_1x_participant_send_mkpdu(
2310 struct ieee802_1x_mka_participant *participant)
2313 struct ieee802_1x_kay *kay = participant->kay;
2317 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2318 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2319 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2320 if (mka_body_handler[i].body_present &&
2321 mka_body_handler[i].body_present(participant))
2322 length += mka_body_handler[i].body_length(participant);
2325 buf = wpabuf_alloc(length);
2327 wpa_printf(MSG_ERROR, "KaY: out of memory");
2331 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2332 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2336 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2340 participant->active = TRUE;
2346 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2348 * ieee802_1x_participant_timer -
2350 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2352 struct ieee802_1x_mka_participant *participant;
2353 struct ieee802_1x_kay *kay;
2354 struct ieee802_1x_kay_peer *peer, *pre_peer;
2355 time_t now = time(NULL);
2357 struct receive_sc *rxsc, *pre_rxsc;
2358 struct transmit_sa *txsa, *pre_txsa;
2360 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2361 kay = participant->kay;
2362 if (participant->cak_life) {
2363 if (now > participant->cak_life) {
2364 kay->authenticated = FALSE;
2365 kay->secured = FALSE;
2367 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2372 /* should delete MKA instance if there are not live peers
2373 * when the MKA life elapsed since its creating */
2374 if (participant->mka_life) {
2375 if (dl_list_empty(&participant->live_peers)) {
2376 if (now > participant->mka_life) {
2377 kay->authenticated = FALSE;
2378 kay->secured = FALSE;
2380 ieee802_1x_kay_delete_mka(kay,
2385 participant->mka_life = 0;
2390 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2391 struct ieee802_1x_kay_peer, list) {
2392 if (now > peer->expire) {
2393 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2394 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2396 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2397 dl_list_for_each_safe(rxsc, pre_rxsc,
2398 &participant->rxsc_list,
2399 struct receive_sc, list) {
2400 if (sci_equal(&rxsc->sci, &peer->sci)) {
2401 secy_delete_receive_sc(kay, rxsc);
2402 ieee802_1x_kay_deinit_receive_sc(
2406 dl_list_del(&peer->list);
2413 if (dl_list_empty(&participant->live_peers)) {
2414 participant->advised_desired = FALSE;
2415 participant->advised_capability =
2416 MACSEC_CAP_NOT_IMPLEMENTED;
2417 participant->to_use_sak = FALSE;
2418 kay->authenticated = TRUE;
2419 kay->secured = FALSE;
2420 kay->failed = FALSE;
2429 dl_list_for_each_safe(txsa, pre_txsa,
2430 &participant->txsc->sa_list,
2431 struct transmit_sa, list) {
2432 secy_disable_transmit_sa(kay, txsa);
2433 ieee802_1x_kay_deinit_transmit_sa(txsa);
2436 ieee802_1x_cp_connect_authenticated(kay->cp);
2437 ieee802_1x_cp_sm_step(kay->cp);
2439 ieee802_1x_kay_elect_key_server(participant);
2440 ieee802_1x_kay_decide_macsec_use(participant);
2444 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2445 struct ieee802_1x_kay_peer, list) {
2446 if (now > peer->expire) {
2447 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2448 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2450 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2451 dl_list_del(&peer->list);
2456 if (participant->new_sak) {
2457 if (!ieee802_1x_kay_generate_new_sak(participant))
2458 participant->to_dist_sak = TRUE;
2460 participant->new_sak = FALSE;
2463 if (participant->retry_count < MAX_RETRY_CNT) {
2464 ieee802_1x_participant_send_mkpdu(participant);
2465 participant->retry_count++;
2468 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2469 ieee802_1x_participant_timer,
2475 * ieee802_1x_kay_init_transmit_sa -
2477 static struct transmit_sa *
2478 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2479 struct data_key *key)
2481 struct transmit_sa *psa;
2483 key->tx_latest = TRUE;
2484 key->rx_latest = TRUE;
2486 psa = os_zalloc(sizeof(*psa));
2488 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2492 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2493 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2494 psa->confidentiality = TRUE;
2496 psa->confidentiality = FALSE;
2500 psa->next_pn = next_PN;
2503 os_get_time(&psa->created_time);
2504 psa->in_use = FALSE;
2506 dl_list_add(&psc->sa_list, &psa->list);
2507 wpa_printf(MSG_DEBUG,
2508 "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC(channel: %d)",
2509 an, next_PN, psc->channel);
2516 * ieee802_1x_kay_deinit_transmit_sa -
2518 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2521 wpa_printf(MSG_DEBUG,
2522 "KaY: Delete transmit SA(an: %hhu) of SC",
2524 dl_list_del(&psa->list);
2530 * init_transmit_sc -
2532 static struct transmit_sc *
2533 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2536 struct transmit_sc *psc;
2538 psc = os_zalloc(sizeof(*psc));
2540 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2543 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2544 psc->channel = channel;
2546 os_get_time(&psc->created_time);
2547 psc->transmitting = FALSE;
2548 psc->encoding_sa = FALSE;
2549 psc->enciphering_sa = FALSE;
2551 dl_list_init(&psc->sa_list);
2552 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2553 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2560 * ieee802_1x_kay_deinit_transmit_sc -
2563 ieee802_1x_kay_deinit_transmit_sc(
2564 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2566 struct transmit_sa *psa, *tmp;
2568 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2570 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2572 secy_disable_transmit_sa(participant->kay, psa);
2573 ieee802_1x_kay_deinit_transmit_sa(psa);
2580 /****************** Interface between CP and KAY *********************/
2582 * ieee802_1x_kay_set_latest_sa_attr -
2584 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2585 struct ieee802_1x_mka_ki *lki, u8 lan,
2586 Boolean ltx, Boolean lrx)
2588 struct ieee802_1x_mka_participant *principal;
2590 principal = ieee802_1x_kay_get_principal_participant(kay);
2595 os_memset(&principal->lki, 0, sizeof(principal->lki));
2597 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2599 principal->lan = lan;
2600 principal->ltx = ltx;
2601 principal->lrx = lrx;
2606 kay->ltx_kn = lki->kn;
2607 kay->lrx_kn = lki->kn;
2617 * ieee802_1x_kay_set_old_sa_attr -
2619 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2620 struct ieee802_1x_mka_ki *oki,
2621 u8 oan, Boolean otx, Boolean orx)
2623 struct ieee802_1x_mka_participant *principal;
2625 principal = ieee802_1x_kay_get_principal_participant(kay);
2630 os_memset(&principal->oki, 0, sizeof(principal->oki));
2632 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2634 principal->oan = oan;
2635 principal->otx = otx;
2636 principal->orx = orx;
2642 kay->otx_kn = oki->kn;
2643 kay->orx_kn = oki->kn;
2653 * ieee802_1x_kay_create_sas -
2655 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2656 struct ieee802_1x_mka_ki *lki)
2658 struct data_key *sa_key, *latest_sak;
2659 struct ieee802_1x_mka_participant *principal;
2660 struct receive_sc *rxsc;
2661 struct receive_sa *rxsa;
2662 struct transmit_sa *txsa;
2664 principal = ieee802_1x_kay_get_principal_participant(kay);
2669 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2670 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2671 sa_key->rx_latest = TRUE;
2672 sa_key->tx_latest = TRUE;
2673 latest_sak = sa_key;
2674 principal->to_use_sak = TRUE;
2676 sa_key->rx_latest = FALSE;
2677 sa_key->tx_latest = FALSE;
2681 wpa_printf(MSG_ERROR, "lki related sak not found");
2685 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2686 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2691 secy_create_receive_sa(kay, rxsa);
2694 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2699 secy_create_transmit_sa(kay, txsa);
2708 * ieee802_1x_kay_delete_sas -
2710 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2711 struct ieee802_1x_mka_ki *ki)
2713 struct data_key *sa_key, *pre_key;
2714 struct transmit_sa *txsa, *pre_txsa;
2715 struct receive_sa *rxsa, *pre_rxsa;
2716 struct receive_sc *rxsc;
2717 struct ieee802_1x_mka_participant *principal;
2719 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2720 principal = ieee802_1x_kay_get_principal_participant(kay);
2724 /* remove the transmit sa */
2725 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2726 struct transmit_sa, list) {
2727 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2728 secy_disable_transmit_sa(kay, txsa);
2729 ieee802_1x_kay_deinit_transmit_sa(txsa);
2733 /* remove the receive sa */
2734 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2735 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2736 struct receive_sa, list) {
2737 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2738 secy_disable_receive_sa(kay, rxsa);
2739 ieee802_1x_kay_deinit_receive_sa(rxsa);
2744 /* remove the sak */
2745 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2746 struct data_key, list) {
2747 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2748 ieee802_1x_kay_deinit_data_key(sa_key);
2751 if (principal->new_key == sa_key)
2752 principal->new_key = NULL;
2760 * ieee802_1x_kay_enable_tx_sas -
2762 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2763 struct ieee802_1x_mka_ki *lki)
2765 struct ieee802_1x_mka_participant *principal;
2766 struct transmit_sa *txsa;
2768 principal = ieee802_1x_kay_get_principal_participant(kay);
2772 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2774 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2775 txsa->in_use = TRUE;
2776 secy_enable_transmit_sa(kay, txsa);
2777 ieee802_1x_cp_set_usingtransmitas(
2778 principal->kay->cp, TRUE);
2779 ieee802_1x_cp_sm_step(principal->kay->cp);
2788 * ieee802_1x_kay_enable_rx_sas -
2790 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2791 struct ieee802_1x_mka_ki *lki)
2793 struct ieee802_1x_mka_participant *principal;
2794 struct receive_sa *rxsa;
2795 struct receive_sc *rxsc;
2797 principal = ieee802_1x_kay_get_principal_participant(kay);
2801 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2802 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2804 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2805 rxsa->in_use = TRUE;
2806 secy_enable_receive_sa(kay, rxsa);
2807 ieee802_1x_cp_set_usingreceivesas(
2808 principal->kay->cp, TRUE);
2809 ieee802_1x_cp_sm_step(principal->kay->cp);
2819 * ieee802_1x_kay_enable_new_info -
2821 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2823 struct ieee802_1x_mka_participant *principal;
2825 principal = ieee802_1x_kay_get_principal_participant(kay);
2829 if (principal->retry_count < MAX_RETRY_CNT) {
2830 ieee802_1x_participant_send_mkpdu(principal);
2831 principal->retry_count++;
2839 * ieee802_1x_kay_cp_conf -
2841 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2842 struct ieee802_1x_cp_conf *pconf)
2844 pconf->protect = kay->macsec_protect;
2845 pconf->replay_protect = kay->macsec_replay_protect;
2846 pconf->validate = kay->macsec_validate;
2853 * ieee802_1x_kay_alloc_cp_sm -
2855 static struct ieee802_1x_cp_sm *
2856 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2858 struct ieee802_1x_cp_conf conf;
2860 os_memset(&conf, 0, sizeof(conf));
2861 conf.protect = kay->macsec_protect;
2862 conf.replay_protect = kay->macsec_replay_protect;
2863 conf.validate = kay->macsec_validate;
2864 conf.replay_window = kay->macsec_replay_window;
2866 return ieee802_1x_cp_sm_init(kay, &conf);
2871 * ieee802_1x_kay_mkpdu_sanity_check -
2872 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2874 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2875 const u8 *buf, size_t len)
2877 struct ieee8023_hdr *eth_hdr;
2878 struct ieee802_1x_hdr *eapol_hdr;
2879 struct ieee802_1x_mka_hdr *mka_hdr;
2880 struct ieee802_1x_mka_basic_body *body;
2882 struct ieee802_1x_mka_participant *participant;
2884 u8 icv[MAX_ICV_LEN];
2887 eth_hdr = (struct ieee8023_hdr *) buf;
2888 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2889 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2891 /* destination address should be not individual address */
2892 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2893 wpa_printf(MSG_MSGDUMP,
2894 "KaY: ethernet destination address is not PAE group address");
2898 /* MKPDU should not be less than 32 octets */
2899 mka_msg_len = be_to_host16(eapol_hdr->length);
2900 if (mka_msg_len < 32) {
2901 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2904 /* MKPDU should be a multiple of 4 octets */
2905 if ((mka_msg_len % 4) != 0) {
2906 wpa_printf(MSG_MSGDUMP,
2907 "KaY: MKPDU is not multiple of 4 octets");
2911 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2912 ieee802_1x_mka_dump_basic_body(body);
2913 body_len = get_mka_param_body_len(body);
2914 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2915 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2916 wpa_printf(MSG_ERROR,
2917 "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",
2918 mka_msg_len, MKA_HDR_LEN,
2919 body_len, DEFAULT_ICV_LEN);
2923 /* CKN should be owned by I */
2924 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2926 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2930 /* algorithm agility check */
2931 if (os_memcmp(body->algo_agility, mka_algo_agility,
2932 sizeof(body->algo_agility)) != 0) {
2933 wpa_printf(MSG_ERROR,
2934 "KaY: peer's algorithm agility not supported for me");
2940 * The ICV will comprise the final octets of the packet body, whatever
2941 * its size, not the fixed length 16 octets, indicated by the EAPOL
2942 * packet body length.
2944 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2945 participant->ick.key,
2946 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2947 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2950 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2954 if (os_memcmp_const(msg_icv, icv,
2955 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2957 wpa_printf(MSG_ERROR,
2958 "KaY: Computed ICV is not equal to Received ICV");
2962 wpa_printf(MSG_ERROR, "KaY: No ICV");
2971 * ieee802_1x_kay_decode_mkpdu -
2973 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2974 const u8 *buf, size_t len)
2976 struct ieee802_1x_mka_participant *participant;
2977 struct ieee802_1x_mka_hdr *hdr;
2983 Boolean my_included;
2984 Boolean handled[256];
2986 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
2989 /* handle basic parameter set */
2990 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
2991 left_len = len - sizeof(struct ieee8023_hdr) -
2992 sizeof(struct ieee802_1x_hdr);
2993 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
2997 /* to skip basic parameter set */
2998 hdr = (struct ieee802_1x_mka_hdr *) pos;
2999 body_len = get_mka_param_body_len(hdr);
3000 pos += body_len + MKA_HDR_LEN;
3001 left_len -= body_len + MKA_HDR_LEN;
3003 /* check i am in the peer's peer list */
3004 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3006 /* accept the peer as live peer */
3007 if (!ieee802_1x_kay_is_in_peer(
3009 participant->current_peer_id.mi)) {
3010 if (!ieee802_1x_kay_create_live_peer(
3012 participant->current_peer_id.mi,
3014 participant->current_peer_id.mn)))
3016 ieee802_1x_kay_elect_key_server(participant);
3017 ieee802_1x_kay_decide_macsec_use(participant);
3019 if (ieee802_1x_kay_is_in_potential_peer(
3020 participant, participant->current_peer_id.mi)) {
3021 if (!ieee802_1x_kay_move_live_peer(
3023 participant->current_peer_id.mi,
3024 be_to_host32(participant->
3025 current_peer_id.mn)))
3027 ieee802_1x_kay_elect_key_server(participant);
3028 ieee802_1x_kay_decide_macsec_use(participant);
3033 * Handle other parameter set than basic parameter set.
3034 * Each parameter set should be present only once.
3036 for (i = 0; i < 256; i++)
3040 for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
3041 pos += body_len + MKA_HDR_LEN,
3042 left_len -= body_len + MKA_HDR_LEN) {
3043 hdr = (struct ieee802_1x_mka_hdr *) pos;
3044 body_len = get_mka_param_body_len(hdr);
3045 body_type = get_mka_param_body_type(hdr);
3047 if (body_type == MKA_ICV_INDICATOR)
3050 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3051 wpa_printf(MSG_ERROR,
3052 "KaY: MKA Peer Packet Body Length (%zu bytes) is less than the Parameter Set Header Length (%zu bytes) + the Parameter Set Body Length (%zu bytes) + %d bytes of ICV",
3053 left_len, MKA_HDR_LEN,
3054 body_len, DEFAULT_ICV_LEN);
3058 if (handled[body_type])
3061 handled[body_type] = TRUE;
3062 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3063 mka_body_handler[body_type].body_rx) {
3064 mka_body_handler[body_type].body_rx
3065 (participant, pos, left_len);
3067 wpa_printf(MSG_ERROR,
3068 "The type %d is not supported in this MKA version %d",
3069 body_type, MKA_VERSION_ID);
3074 participant->retry_count = 0;
3075 participant->active = TRUE;
3082 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3085 struct ieee802_1x_kay *kay = ctx;
3086 struct ieee8023_hdr *eth_hdr;
3087 struct ieee802_1x_hdr *eapol_hdr;
3089 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3090 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3091 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3092 (unsigned long) len);
3096 eth_hdr = (struct ieee8023_hdr *) buf;
3097 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3098 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3099 be_to_host16(eapol_hdr->length)) {
3100 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3101 (unsigned long) len,
3102 (unsigned long) be_to_host16(eapol_hdr->length));
3106 if (eapol_hdr->version < EAPOL_VERSION) {
3107 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3108 eapol_hdr->version);
3111 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3112 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3115 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3116 if (dl_list_empty(&kay->participant_list)) {
3117 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3121 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3126 * ieee802_1x_kay_init -
3128 struct ieee802_1x_kay *
3129 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3130 const char *ifname, const u8 *addr)
3132 struct ieee802_1x_kay *kay;
3134 kay = os_zalloc(sizeof(*kay));
3136 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3143 kay->active = FALSE;
3145 kay->authenticated = FALSE;
3146 kay->secured = FALSE;
3147 kay->failed = FALSE;
3148 kay->policy = policy;
3150 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3151 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3152 kay->actor_sci.port = host_to_be16(0x0001);
3153 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3155 /* While actor acts as a key server, shall distribute sakey */
3160 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3161 kay->macsec_csindex = DEFAULT_CS_INDEX;
3162 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3163 kay->mka_version = MKA_VERSION_ID;
3165 os_memcpy(kay->algo_agility, mka_algo_agility,
3166 sizeof(kay->algo_agility));
3168 dl_list_init(&kay->participant_list);
3170 if (policy == DO_NOT_SECURE) {
3171 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3172 kay->macsec_desired = FALSE;
3173 kay->macsec_protect = FALSE;
3174 kay->macsec_validate = Disabled;
3175 kay->macsec_replay_protect = FALSE;
3176 kay->macsec_replay_window = 0;
3177 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3179 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3180 kay->macsec_desired = TRUE;
3181 kay->macsec_protect = TRUE;
3182 kay->macsec_validate = Strict;
3183 kay->macsec_replay_protect = FALSE;
3184 kay->macsec_replay_window = 0;
3185 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3188 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3190 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3191 secy_init_macsec(kay);
3192 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3194 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3197 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3198 if (kay->cp == NULL) {
3199 ieee802_1x_kay_deinit(kay);
3203 if (policy == DO_NOT_SECURE) {
3204 ieee802_1x_cp_connect_authenticated(kay->cp);
3205 ieee802_1x_cp_sm_step(kay->cp);
3207 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3208 kay_l2_receive, kay, 1);
3209 if (kay->l2_mka == NULL) {
3210 wpa_printf(MSG_WARNING,
3211 "KaY: Failed to initialize L2 packet processing for MKA packet");
3212 ieee802_1x_kay_deinit(kay);
3222 * ieee802_1x_kay_deinit -
3225 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3227 struct ieee802_1x_mka_participant *participant;
3232 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3234 while (!dl_list_empty(&kay->participant_list)) {
3235 participant = dl_list_entry(kay->participant_list.next,
3236 struct ieee802_1x_mka_participant,
3238 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3241 ieee802_1x_cp_sm_deinit(kay->cp);
3242 secy_deinit_macsec(kay);
3245 l2_packet_deinit(kay->l2_mka);
3255 * ieee802_1x_kay_create_mka -
3257 struct ieee802_1x_mka_participant *
3258 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3259 struct mka_key *cak, u32 life,
3260 enum mka_created_mode mode, Boolean is_authenticator)
3262 struct ieee802_1x_mka_participant *participant;
3265 if (!kay || !ckn || !cak) {
3266 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3270 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3271 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3274 if (ckn->len > MAX_CKN_LEN) {
3275 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3279 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3283 participant = os_zalloc(sizeof(*participant));
3285 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3289 participant->ckn.len = ckn->len;
3290 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3291 participant->cak.len = cak->len;
3292 os_memcpy(participant->cak.key, cak->key, cak->len);
3294 participant->cak_life = life + time(NULL);
3298 if (is_authenticator) {
3299 participant->is_obliged_key_server = TRUE;
3300 participant->can_be_key_server = TRUE;
3301 participant->is_key_server = TRUE;
3302 participant->principal = TRUE;
3304 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3305 sizeof(kay->key_server_sci));
3306 kay->key_server_priority = kay->actor_priority;
3307 participant->is_elected = TRUE;
3309 participant->is_obliged_key_server = FALSE;
3310 participant->can_be_key_server = FALSE;
3311 participant->is_key_server = FALSE;
3312 participant->is_elected = TRUE;
3317 participant->is_obliged_key_server = FALSE;
3318 participant->can_be_key_server = TRUE;
3319 participant->is_key_server = TRUE;
3320 participant->is_elected = FALSE;
3324 participant->cached = FALSE;
3326 participant->active = FALSE;
3327 participant->participant = FALSE;
3328 participant->retain = FALSE;
3329 participant->activate = DEFAULT;
3331 if (participant->is_key_server)
3332 participant->principal = TRUE;
3334 dl_list_init(&participant->live_peers);
3335 dl_list_init(&participant->potential_peers);
3337 participant->retry_count = 0;
3338 participant->kay = kay;
3340 if (!reset_participant_mi(participant))
3343 participant->lrx = FALSE;
3344 participant->ltx = FALSE;
3345 participant->orx = FALSE;
3346 participant->otx = FALSE;
3347 participant->to_dist_sak = FALSE;
3348 participant->to_use_sak = FALSE;
3349 participant->new_sak = FALSE;
3350 dl_list_init(&participant->sak_list);
3351 participant->new_key = NULL;
3352 dl_list_init(&participant->rxsc_list);
3353 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3355 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3356 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3357 kay->macsec_replay_window);
3358 secy_create_transmit_sc(kay, participant->txsc);
3360 /* to derive KEK from CAK and CKN */
3361 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3362 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3363 participant->ckn.name,
3364 participant->ckn.len,
3365 participant->kek.key)) {
3366 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3369 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3370 participant->kek.key, participant->kek.len);
3372 /* to derive ICK from CAK and CKN */
3373 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3374 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3375 participant->ckn.name,
3376 participant->ckn.len,
3377 participant->ick.key)) {
3378 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3381 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3382 participant->ick.key, participant->ick.len);
3384 dl_list_add(&kay->participant_list, &participant->list);
3385 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3386 ckn->name, ckn->len);
3388 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3389 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3391 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3397 os_free(participant);
3403 * ieee802_1x_kay_delete_mka -
3406 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3408 struct ieee802_1x_mka_participant *participant;
3409 struct ieee802_1x_kay_peer *peer;
3410 struct data_key *sak;
3411 struct receive_sc *rxsc;
3416 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3418 /* get the participant */
3419 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3421 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3422 ckn->name, ckn->len);
3426 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3427 dl_list_del(&participant->list);
3429 /* remove live peer */
3430 while (!dl_list_empty(&participant->live_peers)) {
3431 peer = dl_list_entry(participant->live_peers.next,
3432 struct ieee802_1x_kay_peer, list);
3433 dl_list_del(&peer->list);
3437 /* remove potential peer */
3438 while (!dl_list_empty(&participant->potential_peers)) {
3439 peer = dl_list_entry(participant->potential_peers.next,
3440 struct ieee802_1x_kay_peer, list);
3441 dl_list_del(&peer->list);
3446 while (!dl_list_empty(&participant->sak_list)) {
3447 sak = dl_list_entry(participant->sak_list.next,
3448 struct data_key, list);
3449 dl_list_del(&sak->list);
3453 while (!dl_list_empty(&participant->rxsc_list)) {
3454 rxsc = dl_list_entry(participant->rxsc_list.next,
3455 struct receive_sc, list);
3456 secy_delete_receive_sc(kay, rxsc);
3457 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3459 secy_delete_transmit_sc(kay, participant->txsc);
3460 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3462 os_memset(&participant->cak, 0, sizeof(participant->cak));
3463 os_memset(&participant->kek, 0, sizeof(participant->kek));
3464 os_memset(&participant->ick, 0, sizeof(participant->ick));
3465 os_free(participant);
3470 * ieee802_1x_kay_mka_participate -
3472 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3473 struct mka_key_name *ckn,
3476 struct ieee802_1x_mka_participant *participant;
3481 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3485 participant->active = status;
3490 * ieee802_1x_kay_new_sak -
3493 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3495 struct ieee802_1x_mka_participant *participant;
3500 participant = ieee802_1x_kay_get_principal_participant(kay);
3504 participant->new_sak = TRUE;
3505 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3512 * ieee802_1x_kay_change_cipher_suite -
3515 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3517 struct ieee802_1x_mka_participant *participant;
3522 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3523 wpa_printf(MSG_ERROR,
3524 "KaY: Configured cipher suite index is out of range");
3527 if (kay->macsec_csindex == cs_index)
3531 kay->macsec_desired = FALSE;
3533 kay->macsec_csindex = cs_index;
3534 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3536 participant = ieee802_1x_kay_get_principal_participant(kay);
3538 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3539 participant->new_sak = TRUE;