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 struct mka_param_body_handler {
83 int (*body_tx)(struct ieee802_1x_mka_participant *participant,
85 int (*body_rx)(struct ieee802_1x_mka_participant *participant,
86 const u8 *mka_msg, size_t msg_len);
87 int (*body_length)(struct ieee802_1x_mka_participant *participant);
88 Boolean (*body_present)(struct ieee802_1x_mka_participant *participant);
92 static void set_mka_param_body_len(void *body, unsigned int len)
94 struct ieee802_1x_mka_hdr *hdr = body;
95 hdr->length = (len >> 8) & 0x0f;
96 hdr->length1 = len & 0xff;
100 static unsigned int get_mka_param_body_len(const void *body)
102 const struct ieee802_1x_mka_hdr *hdr = body;
103 return (hdr->length << 8) | hdr->length1;
107 static u8 get_mka_param_body_type(const void *body)
109 const struct ieee802_1x_mka_hdr *hdr = body;
115 * ieee802_1x_mka_dump_basic_body -
118 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body *body)
125 body_len = get_mka_param_body_len(body);
126 wpa_printf(MSG_DEBUG, "*** MKA Basic Parameter set ***");
127 wpa_printf(MSG_DEBUG, "\tVersion.......: %d", body->version);
128 wpa_printf(MSG_DEBUG, "\tPriority......: %d", body->priority);
129 wpa_printf(MSG_DEBUG, "\tKeySvr........: %d", body->key_server);
130 wpa_printf(MSG_DEBUG, "\tMACSecDesired.: %d", body->macsec_desired);
131 wpa_printf(MSG_DEBUG, "\tMACSecCapable.: %d", body->macsec_capability);
132 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
133 wpa_printf(MSG_DEBUG, "\tSCI MAC.......: " MACSTR,
134 MAC2STR(body->actor_sci.addr));
135 wpa_printf(MSG_DEBUG, "\tSCI Port .....: %d",
136 be_to_host16(body->actor_sci.port));
137 wpa_hexdump(MSG_DEBUG, "\tMember Id.....:",
138 body->actor_mi, sizeof(body->actor_mi));
139 wpa_printf(MSG_DEBUG, "\tMessage Number: %d",
140 be_to_host32(body->actor_mn));
141 wpa_hexdump(MSG_DEBUG, "\tAlgo Agility..:",
142 body->algo_agility, sizeof(body->algo_agility));
143 wpa_hexdump_ascii(MSG_DEBUG, "\tCAK Name......:", body->ckn,
144 body_len + MKA_HDR_LEN - sizeof(*body));
149 * ieee802_1x_mka_dump_peer_body -
152 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body *body)
162 body_len = get_mka_param_body_len(body);
163 if (body->type == MKA_LIVE_PEER_LIST) {
164 wpa_printf(MSG_DEBUG, "*** Live Peer List ***");
165 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
166 } else if (body->type == MKA_POTENTIAL_PEER_LIST) {
167 wpa_printf(MSG_DEBUG, "*** Potential Live Peer List ***");
168 wpa_printf(MSG_DEBUG, "\tBody Length...: %zu", body_len);
171 for (i = 0; i < body_len; i += MI_LEN + sizeof(mn)) {
173 os_memcpy(&mn, mi + MI_LEN, sizeof(mn));
174 wpa_hexdump_ascii(MSG_DEBUG, "\tMember Id.....:", mi, MI_LEN);
175 wpa_printf(MSG_DEBUG, "\tMessage Number: %d", be_to_host32(mn));
181 * ieee802_1x_mka_dump_dist_sak_body -
184 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body *body)
191 body_len = get_mka_param_body_len(body);
192 wpa_printf(MSG_INFO, "*** Distributed SAK ***");
193 wpa_printf(MSG_INFO, "\tDistributed AN........: %d", body->dan);
194 wpa_printf(MSG_INFO, "\tConfidentiality Offset: %d",
195 body->confid_offset);
196 wpa_printf(MSG_INFO, "\tBody Length...........: %zu", body_len);
200 wpa_printf(MSG_INFO, "\tKey Number............: %d",
201 be_to_host32(body->kn));
202 wpa_hexdump(MSG_INFO, "\tAES Key Wrap of SAK...:", body->sak, 24);
206 static const char * yes_no(int val)
208 return val ? "Yes" : "No";
213 * ieee802_1x_mka_dump_sak_use_body -
216 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body *body)
223 body_len = get_mka_param_body_len(body);
224 wpa_printf(MSG_DEBUG, "*** MACsec SAK Use ***");
225 wpa_printf(MSG_DEBUG, "\tLatest Key AN....: %d", body->lan);
226 wpa_printf(MSG_DEBUG, "\tLatest Key Tx....: %s", yes_no(body->ltx));
227 wpa_printf(MSG_DEBUG, "\tLatest Key Rx....: %s", yes_no(body->lrx));
228 wpa_printf(MSG_DEBUG, "\tOld Key AN....: %d", body->oan);
229 wpa_printf(MSG_DEBUG, "\tOld Key Tx....: %s", yes_no(body->otx));
230 wpa_printf(MSG_DEBUG, "\tOld Key Rx....: %s", yes_no(body->orx));
231 wpa_printf(MSG_DEBUG, "\tPlain Key Tx....: %s", yes_no(body->ptx));
232 wpa_printf(MSG_DEBUG, "\tPlain Key Rx....: %s", yes_no(body->prx));
233 wpa_printf(MSG_DEBUG, "\tDelay Protect....: %s",
234 yes_no(body->delay_protect));
235 wpa_printf(MSG_DEBUG, "\tBody Length......: %d", body_len);
239 wpa_hexdump(MSG_DEBUG, "\tKey Server MI....:",
240 body->lsrv_mi, sizeof(body->lsrv_mi));
241 wpa_printf(MSG_DEBUG, "\tKey Number.......: %u",
242 be_to_host32(body->lkn));
243 wpa_printf(MSG_DEBUG, "\tLowest PN........: %u",
244 be_to_host32(body->llpn));
245 wpa_hexdump_ascii(MSG_DEBUG, "\tOld Key Server MI....:",
246 body->osrv_mi, sizeof(body->osrv_mi));
247 wpa_printf(MSG_DEBUG, "\tOld Key Number.......: %u",
248 be_to_host32(body->okn));
249 wpa_printf(MSG_DEBUG, "\tOld Lowest PN........: %u",
250 be_to_host32(body->olpn));
255 * ieee802_1x_kay_get_participant -
257 static struct ieee802_1x_mka_participant *
258 ieee802_1x_kay_get_participant(struct ieee802_1x_kay *kay, const u8 *ckn)
260 struct ieee802_1x_mka_participant *participant;
262 dl_list_for_each(participant, &kay->participant_list,
263 struct ieee802_1x_mka_participant, list) {
264 if (os_memcmp(participant->ckn.name, ckn,
265 participant->ckn.len) == 0)
269 wpa_printf(MSG_DEBUG, "KaY: participant is not found");
276 * ieee802_1x_kay_get_principal_participant -
278 static struct ieee802_1x_mka_participant *
279 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay *kay)
281 struct ieee802_1x_mka_participant *participant;
283 dl_list_for_each(participant, &kay->participant_list,
284 struct ieee802_1x_mka_participant, list) {
285 if (participant->principal)
289 wpa_printf(MSG_DEBUG, "KaY: principal participant is not founded");
294 static struct ieee802_1x_kay_peer * get_peer_mi(struct dl_list *peers,
297 struct ieee802_1x_kay_peer *peer;
299 dl_list_for_each(peer, peers, struct ieee802_1x_kay_peer, list) {
300 if (os_memcmp(peer->mi, mi, MI_LEN) == 0)
309 * ieee802_1x_kay_get_potential_peer
311 static struct ieee802_1x_kay_peer *
312 ieee802_1x_kay_get_potential_peer(
313 struct ieee802_1x_mka_participant *participant, const u8 *mi)
315 return get_peer_mi(&participant->potential_peers, mi);
320 * ieee802_1x_kay_get_live_peer
322 static struct ieee802_1x_kay_peer *
323 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant *participant,
326 return get_peer_mi(&participant->live_peers, mi);
331 * ieee802_1x_kay_is_in_potential_peer
334 ieee802_1x_kay_is_in_potential_peer(
335 struct ieee802_1x_mka_participant *participant, const u8 *mi)
337 return ieee802_1x_kay_get_potential_peer(participant, mi) != NULL;
342 * ieee802_1x_kay_is_in_live_peer
345 ieee802_1x_kay_is_in_live_peer(
346 struct ieee802_1x_mka_participant *participant, const u8 *mi)
348 return ieee802_1x_kay_get_live_peer(participant, mi) != NULL;
353 * ieee802_1x_kay_is_in_peer
356 ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
359 return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
360 ieee802_1x_kay_is_in_potential_peer(participant, mi);
365 * ieee802_1x_kay_get_peer
367 static struct ieee802_1x_kay_peer *
368 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
371 struct ieee802_1x_kay_peer *peer;
373 peer = ieee802_1x_kay_get_live_peer(participant, mi);
377 return ieee802_1x_kay_get_potential_peer(participant, mi);
382 * ieee802_1x_kay_get_cipher_suite
384 static struct macsec_ciphersuite *
385 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
390 for (i = 0; i < CS_TABLE_SIZE; i++) {
391 if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
392 return &cipher_suite_tbl[i];
399 static Boolean sci_equal(const struct ieee802_1x_mka_sci *a,
400 const struct ieee802_1x_mka_sci *b)
402 return os_memcmp(a, b, sizeof(struct ieee802_1x_mka_sci)) == 0;
407 * ieee802_1x_kay_get_peer_sci
409 static struct ieee802_1x_kay_peer *
410 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant *participant,
411 const struct ieee802_1x_mka_sci *sci)
413 struct ieee802_1x_kay_peer *peer;
415 dl_list_for_each(peer, &participant->live_peers,
416 struct ieee802_1x_kay_peer, list) {
417 if (sci_equal(&peer->sci, sci))
421 dl_list_for_each(peer, &participant->potential_peers,
422 struct ieee802_1x_kay_peer, list) {
423 if (sci_equal(&peer->sci, sci))
432 * ieee802_1x_kay_init_receive_sa -
434 static struct receive_sa *
435 ieee802_1x_kay_init_receive_sa(struct receive_sc *psc, u8 an, u32 lowest_pn,
436 struct data_key *key)
438 struct receive_sa *psa;
443 psa = os_zalloc(sizeof(*psa));
445 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
450 psa->lowest_pn = lowest_pn;
451 psa->next_pn = lowest_pn;
455 os_get_time(&psa->created_time);
458 dl_list_add(&psc->sa_list, &psa->list);
459 wpa_printf(MSG_DEBUG,
460 "KaY: Create receive SA(AN: %hhu lowest_pn: %u of SC(channel: %d)",
461 an, lowest_pn, psc->channel);
468 * ieee802_1x_kay_deinit_receive_sa -
470 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa *psa)
473 wpa_printf(MSG_DEBUG,
474 "KaY: Delete receive SA(an: %hhu) of SC",
476 dl_list_del(&psa->list);
482 * ieee802_1x_kay_init_receive_sc -
484 static struct receive_sc *
485 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci *psci,
488 struct receive_sc *psc;
493 psc = os_zalloc(sizeof(*psc));
495 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
499 os_memcpy(&psc->sci, psci, sizeof(psc->sci));
500 psc->channel = channel;
502 os_get_time(&psc->created_time);
503 psc->receiving = FALSE;
505 dl_list_init(&psc->sa_list);
506 wpa_printf(MSG_DEBUG, "KaY: Create receive SC(channel: %d)", channel);
507 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)psci, sizeof(*psci));
514 * ieee802_1x_kay_deinit_receive_sc -
517 ieee802_1x_kay_deinit_receive_sc(
518 struct ieee802_1x_mka_participant *participant, struct receive_sc *psc)
520 struct receive_sa *psa, *pre_sa;
522 wpa_printf(MSG_DEBUG, "KaY: Delete receive SC(channel: %d)",
524 dl_list_for_each_safe(psa, pre_sa, &psc->sa_list, struct receive_sa,
526 secy_disable_receive_sa(participant->kay, psa);
527 ieee802_1x_kay_deinit_receive_sa(psa);
529 dl_list_del(&psc->list);
534 static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer *peer)
536 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi, sizeof(peer->mi));
537 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
538 wpa_hexdump(MSG_DEBUG, "\tSCI Addr: ", peer->sci.addr, ETH_ALEN);
539 wpa_printf(MSG_DEBUG, "\tPort: %d", peer->sci.port);
543 static struct ieee802_1x_kay_peer *
544 ieee802_1x_kay_create_peer(const u8 *mi, u32 mn)
546 struct ieee802_1x_kay_peer *peer;
548 peer = os_zalloc(sizeof(*peer));
550 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
554 os_memcpy(peer->mi, mi, MI_LEN);
556 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
557 peer->sak_used = FALSE;
564 * ieee802_1x_kay_create_live_peer
566 static struct ieee802_1x_kay_peer *
567 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant *participant,
568 const u8 *mi, u32 mn)
570 struct ieee802_1x_kay_peer *peer;
571 struct receive_sc *rxsc;
574 peer = ieee802_1x_kay_create_peer(mi, mn);
578 os_memcpy(&peer->sci, &participant->current_peer_sci,
581 secy_get_available_receive_sc(participant->kay, &sc_ch);
583 rxsc = ieee802_1x_kay_init_receive_sc(&peer->sci, sc_ch);
589 dl_list_add(&participant->live_peers, &peer->list);
590 dl_list_add(&participant->rxsc_list, &rxsc->list);
591 secy_create_receive_sc(participant->kay, rxsc);
593 wpa_printf(MSG_DEBUG, "KaY: Live peer created");
594 ieee802_1x_kay_dump_peer(peer);
601 * ieee802_1x_kay_create_potential_peer
603 static struct ieee802_1x_kay_peer *
604 ieee802_1x_kay_create_potential_peer(
605 struct ieee802_1x_mka_participant *participant, const u8 *mi, u32 mn)
607 struct ieee802_1x_kay_peer *peer;
609 peer = ieee802_1x_kay_create_peer(mi, mn);
613 dl_list_add(&participant->potential_peers, &peer->list);
615 wpa_printf(MSG_DEBUG, "KaY: potential peer created");
616 ieee802_1x_kay_dump_peer(peer);
623 * ieee802_1x_kay_move_live_peer
625 static struct ieee802_1x_kay_peer *
626 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant *participant,
629 struct ieee802_1x_kay_peer *peer;
630 struct receive_sc *rxsc;
633 peer = ieee802_1x_kay_get_potential_peer(participant, mi);
635 rxsc = ieee802_1x_kay_init_receive_sc(&participant->current_peer_sci,
640 os_memcpy(&peer->sci, &participant->current_peer_sci,
643 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
645 wpa_printf(MSG_DEBUG, "KaY: move potential peer to live peer");
646 ieee802_1x_kay_dump_peer(peer);
648 dl_list_del(&peer->list);
649 dl_list_add_tail(&participant->live_peers, &peer->list);
651 secy_get_available_receive_sc(participant->kay, &sc_ch);
653 dl_list_add(&participant->rxsc_list, &rxsc->list);
654 secy_create_receive_sc(participant->kay, rxsc);
662 * ieee802_1x_mka_basic_body_present -
665 ieee802_1x_mka_basic_body_present(
666 struct ieee802_1x_mka_participant *participant)
673 * ieee802_1x_mka_basic_body_length -
676 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant *participant)
680 length = sizeof(struct ieee802_1x_mka_basic_body);
681 length += participant->ckn.len;
682 return MKA_ALIGN_LENGTH(length);
687 * ieee802_1x_mka_encode_basic_body
690 ieee802_1x_mka_encode_basic_body(
691 struct ieee802_1x_mka_participant *participant,
694 struct ieee802_1x_mka_basic_body *body;
695 struct ieee802_1x_kay *kay = participant->kay;
696 unsigned int length = ieee802_1x_mka_basic_body_length(participant);
698 body = wpabuf_put(buf, length);
700 body->version = kay->mka_version;
701 body->priority = kay->actor_priority;
702 if (participant->is_elected)
703 body->key_server = participant->is_key_server;
705 body->key_server = participant->can_be_key_server;
707 body->macsec_desired = kay->macsec_desired;
708 body->macsec_capability = kay->macsec_capable;
709 set_mka_param_body_len(body, length - MKA_HDR_LEN);
711 os_memcpy(body->actor_sci.addr, kay->actor_sci.addr,
712 sizeof(kay->actor_sci.addr));
713 body->actor_sci.port = kay->actor_sci.port;
715 os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
716 participant->mn = participant->mn + 1;
717 body->actor_mn = host_to_be32(participant->mn);
718 os_memcpy(body->algo_agility, participant->kay->algo_agility,
719 sizeof(body->algo_agility));
721 os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
723 ieee802_1x_mka_dump_basic_body(body);
730 reset_participant_mi(struct ieee802_1x_mka_participant *participant)
732 if (os_get_random(participant->mi, sizeof(participant->mi)) < 0)
741 * ieee802_1x_mka_decode_basic_body -
743 static struct ieee802_1x_mka_participant *
744 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay *kay, const u8 *mka_msg,
747 struct ieee802_1x_mka_participant *participant;
748 const struct ieee802_1x_mka_basic_body *body;
749 struct ieee802_1x_kay_peer *peer;
751 body = (const struct ieee802_1x_mka_basic_body *) mka_msg;
753 if (body->version > MKA_VERSION_ID) {
754 wpa_printf(MSG_DEBUG,
755 "KaY: peer's version(%d) greater than mka current version(%d)",
756 body->version, MKA_VERSION_ID);
758 if (kay->is_obliged_key_server && body->key_server) {
759 wpa_printf(MSG_DEBUG, "I must be as key server");
763 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
765 wpa_printf(MSG_DEBUG, "Peer is not included in my CA");
769 /* If the peer's MI is my MI, I will choose new MI */
770 if (os_memcmp(body->actor_mi, participant->mi, MI_LEN) == 0) {
771 if (!reset_participant_mi(participant))
775 os_memcpy(participant->current_peer_id.mi, body->actor_mi, MI_LEN);
776 participant->current_peer_id.mn = body->actor_mn;
777 os_memcpy(participant->current_peer_sci.addr, body->actor_sci.addr,
778 sizeof(participant->current_peer_sci.addr));
779 participant->current_peer_sci.port = body->actor_sci.port;
782 peer = ieee802_1x_kay_get_peer(participant, body->actor_mi);
784 /* Check duplicated SCI */
785 /* TODO: What policy should be applied to detect duplicated SCI
786 * is active attacker or a valid peer whose MI is be changed?
788 peer = ieee802_1x_kay_get_peer_sci(participant,
791 wpa_printf(MSG_WARNING,
792 "KaY: duplicated SCI detected, Maybe active attacker");
793 dl_list_del(&peer->list);
797 peer = ieee802_1x_kay_create_potential_peer(
798 participant, body->actor_mi,
799 be_to_host32(body->actor_mn));
803 peer->macsec_desired = body->macsec_desired;
804 peer->macsec_capability = body->macsec_capability;
805 peer->is_key_server = (Boolean) body->key_server;
806 peer->key_server_priority = body->priority;
807 } else if (peer->mn < be_to_host32(body->actor_mn)) {
808 peer->mn = be_to_host32(body->actor_mn);
809 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
810 peer->macsec_desired = body->macsec_desired;
811 peer->macsec_capability = body->macsec_capability;
812 peer->is_key_server = (Boolean) body->key_server;
813 peer->key_server_priority = body->priority;
815 wpa_printf(MSG_WARNING, "KaY: The peer MN have received");
824 * ieee802_1x_mka_live_peer_body_present
827 ieee802_1x_mka_live_peer_body_present(
828 struct ieee802_1x_mka_participant *participant)
830 return !dl_list_empty(&participant->live_peers);
835 * ieee802_1x_kay_get_live_peer_length
838 ieee802_1x_mka_get_live_peer_length(
839 struct ieee802_1x_mka_participant *participant)
841 int len = MKA_HDR_LEN;
842 struct ieee802_1x_kay_peer *peer;
844 dl_list_for_each(peer, &participant->live_peers,
845 struct ieee802_1x_kay_peer, list)
846 len += sizeof(struct ieee802_1x_mka_peer_id);
848 return MKA_ALIGN_LENGTH(len);
853 * ieee802_1x_mka_encode_live_peer_body -
856 ieee802_1x_mka_encode_live_peer_body(
857 struct ieee802_1x_mka_participant *participant,
860 struct ieee802_1x_mka_peer_body *body;
861 struct ieee802_1x_kay_peer *peer;
863 struct ieee802_1x_mka_peer_id *body_peer;
865 length = ieee802_1x_mka_get_live_peer_length(participant);
866 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
868 body->type = MKA_LIVE_PEER_LIST;
869 set_mka_param_body_len(body, length - MKA_HDR_LEN);
871 dl_list_for_each(peer, &participant->live_peers,
872 struct ieee802_1x_kay_peer, list) {
873 body_peer = wpabuf_put(buf,
874 sizeof(struct ieee802_1x_mka_peer_id));
875 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
876 body_peer->mn = host_to_be32(peer->mn);
879 ieee802_1x_mka_dump_peer_body(body);
884 * ieee802_1x_mka_potential_peer_body_present
887 ieee802_1x_mka_potential_peer_body_present(
888 struct ieee802_1x_mka_participant *participant)
890 return !dl_list_empty(&participant->potential_peers);
895 * ieee802_1x_kay_get_potential_peer_length
898 ieee802_1x_mka_get_potential_peer_length(
899 struct ieee802_1x_mka_participant *participant)
901 int len = MKA_HDR_LEN;
902 struct ieee802_1x_kay_peer *peer;
904 dl_list_for_each(peer, &participant->potential_peers,
905 struct ieee802_1x_kay_peer, list)
906 len += sizeof(struct ieee802_1x_mka_peer_id);
908 return MKA_ALIGN_LENGTH(len);
913 * ieee802_1x_mka_encode_potential_peer_body -
916 ieee802_1x_mka_encode_potential_peer_body(
917 struct ieee802_1x_mka_participant *participant,
920 struct ieee802_1x_mka_peer_body *body;
921 struct ieee802_1x_kay_peer *peer;
923 struct ieee802_1x_mka_peer_id *body_peer;
925 length = ieee802_1x_mka_get_potential_peer_length(participant);
926 body = wpabuf_put(buf, sizeof(struct ieee802_1x_mka_peer_body));
928 body->type = MKA_POTENTIAL_PEER_LIST;
929 set_mka_param_body_len(body, length - MKA_HDR_LEN);
931 dl_list_for_each(peer, &participant->potential_peers,
932 struct ieee802_1x_kay_peer, list) {
933 body_peer = wpabuf_put(buf,
934 sizeof(struct ieee802_1x_mka_peer_id));
935 os_memcpy(body_peer->mi, peer->mi, MI_LEN);
936 body_peer->mn = host_to_be32(peer->mn);
939 ieee802_1x_mka_dump_peer_body(body);
945 * ieee802_1x_mka_i_in_peerlist -
948 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant *participant,
949 const u8 *mka_msg, size_t msg_len)
951 struct ieee802_1x_mka_hdr *hdr;
958 for (pos = mka_msg, left_len = msg_len;
959 left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
960 left_len -= body_len + MKA_HDR_LEN,
961 pos += body_len + MKA_HDR_LEN) {
962 hdr = (struct ieee802_1x_mka_hdr *) pos;
963 body_len = get_mka_param_body_len(hdr);
964 body_type = get_mka_param_body_type(hdr);
966 if (body_type != MKA_LIVE_PEER_LIST &&
967 body_type != MKA_POTENTIAL_PEER_LIST)
970 ieee802_1x_mka_dump_peer_body(
971 (struct ieee802_1x_mka_peer_body *)pos);
973 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
974 wpa_printf(MSG_ERROR,
975 "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",
976 left_len, MKA_HDR_LEN,
977 body_len, DEFAULT_ICV_LEN);
981 if ((body_len % 16) != 0) {
982 wpa_printf(MSG_ERROR,
983 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
988 for (i = 0; i < body_len;
989 i += sizeof(struct ieee802_1x_mka_peer_id)) {
990 const struct ieee802_1x_mka_peer_id *peer_mi;
992 peer_mi = (const struct ieee802_1x_mka_peer_id *)
993 (pos + MKA_HDR_LEN + i);
994 if (os_memcmp(peer_mi->mi, participant->mi,
996 be_to_host32(peer_mi->mn) == participant->mn)
1006 * ieee802_1x_mka_decode_live_peer_body -
1008 static int ieee802_1x_mka_decode_live_peer_body(
1009 struct ieee802_1x_mka_participant *participant,
1010 const u8 *peer_msg, size_t msg_len)
1012 const struct ieee802_1x_mka_hdr *hdr;
1013 struct ieee802_1x_kay_peer *peer;
1016 Boolean is_included;
1018 is_included = ieee802_1x_kay_is_in_live_peer(
1019 participant, participant->current_peer_id.mi);
1021 hdr = (const struct ieee802_1x_mka_hdr *) peer_msg;
1022 body_len = get_mka_param_body_len(hdr);
1023 if (body_len % 16 != 0) {
1024 wpa_printf(MSG_ERROR,
1025 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1030 for (i = 0; i < body_len; i += sizeof(struct ieee802_1x_mka_peer_id)) {
1031 const struct ieee802_1x_mka_peer_id *peer_mi;
1034 peer_mi = (const struct ieee802_1x_mka_peer_id *)
1035 (peer_msg + MKA_HDR_LEN + i);
1036 peer_mn = be_to_host32(peer_mi->mn);
1039 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1040 /* My message id is used by other participant */
1041 if (peer_mn > participant->mn &&
1042 !reset_participant_mi(participant))
1043 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1050 peer = ieee802_1x_kay_get_peer(participant, peer_mi->mi);
1053 peer->expire = time(NULL) + MKA_LIFE_TIME / 1000;
1054 } else if (!ieee802_1x_kay_create_potential_peer(
1055 participant, peer_mi->mi, peer_mn)) {
1065 * ieee802_1x_mka_decode_potential_peer_body -
1068 ieee802_1x_mka_decode_potential_peer_body(
1069 struct ieee802_1x_mka_participant *participant,
1070 const u8 *peer_msg, size_t msg_len)
1072 struct ieee802_1x_mka_hdr *hdr;
1079 hdr = (struct ieee802_1x_mka_hdr *) peer_msg;
1080 body_len = get_mka_param_body_len(hdr);
1081 if (body_len % 16 != 0) {
1082 wpa_printf(MSG_ERROR,
1083 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1088 for (i = 0; i < body_len; i += MI_LEN + sizeof(peer_mn)) {
1089 peer_mi = MKA_HDR_LEN + peer_msg + i;
1090 os_memcpy(&_peer_mn, peer_mi + MI_LEN, sizeof(_peer_mn));
1091 peer_mn = be_to_host32(_peer_mn);
1094 if (os_memcmp(peer_mi, participant->mi, MI_LEN) == 0) {
1095 /* My message id is used by other participant */
1096 if (peer_mn > participant->mn &&
1097 !reset_participant_mi(participant))
1098 wpa_printf(MSG_DEBUG, "KaY: Could not update mi");
1108 * ieee802_1x_mka_sak_use_body_present
1111 ieee802_1x_mka_sak_use_body_present(
1112 struct ieee802_1x_mka_participant *participant)
1114 if (participant->to_use_sak)
1122 * ieee802_1x_mka_get_sak_use_length
1125 ieee802_1x_mka_get_sak_use_length(
1126 struct ieee802_1x_mka_participant *participant)
1128 int length = MKA_HDR_LEN;
1130 if (participant->kay->macsec_desired && participant->advised_desired)
1131 length = sizeof(struct ieee802_1x_mka_sak_use_body);
1133 return MKA_ALIGN_LENGTH(length);
1141 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant *principal,
1142 struct ieee802_1x_mka_ki *ki)
1144 struct receive_sa *rxsa;
1145 struct receive_sc *rxsc;
1148 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
1149 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
1151 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
1152 secy_get_receive_lowest_pn(principal->kay,
1155 lpn = lpn > rxsa->lowest_pn ?
1156 lpn : rxsa->lowest_pn;
1170 * ieee802_1x_mka_encode_sak_use_body -
1173 ieee802_1x_mka_encode_sak_use_body(
1174 struct ieee802_1x_mka_participant *participant,
1177 struct ieee802_1x_mka_sak_use_body *body;
1178 unsigned int length;
1181 length = ieee802_1x_mka_get_sak_use_length(participant);
1182 body = wpabuf_put(buf, length);
1184 body->type = MKA_SAK_USE;
1185 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1187 if (length == MKA_HDR_LEN) {
1193 body->delay_protect = FALSE;
1197 /* data protect, lowest accept packet number */
1198 body->delay_protect = participant->kay->macsec_replay_protect;
1199 pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
1200 if (pn > participant->kay->pn_exhaustion) {
1201 wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
1202 if (participant->is_key_server)
1203 participant->new_sak = TRUE;
1206 body->llpn = host_to_be32(pn);
1207 pn = ieee802_1x_mka_get_lpn(participant, &participant->oki);
1208 body->olpn = host_to_be32(pn);
1210 /* plain tx, plain rx */
1211 if (participant->kay->macsec_protect)
1216 if (participant->kay->macsec_validate == Strict)
1221 /* latest key: rx, tx, key server member identifier key number */
1222 body->lan = participant->lan;
1223 os_memcpy(body->lsrv_mi, participant->lki.mi,
1224 sizeof(body->lsrv_mi));
1225 body->lkn = host_to_be32(participant->lki.kn);
1226 body->lrx = participant->lrx;
1227 body->ltx = participant->ltx;
1229 /* old key: rx, tx, key server member identifier key number */
1230 body->oan = participant->oan;
1231 if (participant->oki.kn != participant->lki.kn &&
1232 participant->oki.kn != 0) {
1235 os_memcpy(body->osrv_mi, participant->oki.mi,
1236 sizeof(body->osrv_mi));
1237 body->okn = host_to_be32(participant->oki.kn);
1243 /* set CP's variable */
1245 if (!participant->kay->tx_enable)
1246 participant->kay->tx_enable = TRUE;
1248 if (!participant->kay->port_enable)
1249 participant->kay->port_enable = TRUE;
1252 if (!participant->kay->rx_enable)
1253 participant->kay->rx_enable = TRUE;
1256 ieee802_1x_mka_dump_sak_use_body(body);
1262 * ieee802_1x_mka_decode_sak_use_body -
1265 ieee802_1x_mka_decode_sak_use_body(
1266 struct ieee802_1x_mka_participant *participant,
1267 const u8 *mka_msg, size_t msg_len)
1269 struct ieee802_1x_mka_hdr *hdr;
1270 struct ieee802_1x_mka_sak_use_body *body;
1271 struct ieee802_1x_kay_peer *peer;
1272 struct transmit_sa *txsa;
1273 struct data_key *sa_key = NULL;
1275 struct ieee802_1x_mka_ki ki;
1277 Boolean all_receiving;
1280 if (!participant->principal) {
1281 wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
1284 peer = ieee802_1x_kay_get_live_peer(participant,
1285 participant->current_peer_id.mi);
1287 wpa_printf(MSG_WARNING, "KaY: the peer is not my live peer");
1291 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1292 body_len = get_mka_param_body_len(hdr);
1293 body = (struct ieee802_1x_mka_sak_use_body *) mka_msg;
1294 ieee802_1x_mka_dump_sak_use_body(body);
1296 if ((body_len != 0) && (body_len < 40)) {
1297 wpa_printf(MSG_ERROR,
1298 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
1303 /* TODO: what action should I take when peer does not support MACsec */
1304 if (body_len == 0) {
1305 wpa_printf(MSG_WARNING, "KaY: Peer does not support MACsec");
1309 /* TODO: when the plain tx or rx of peer is true, should I change
1310 * the attribute of controlled port
1313 wpa_printf(MSG_WARNING, "KaY: peer's plain rx are TRUE");
1316 wpa_printf(MSG_WARNING, "KaY: peer's plain tx are TRUE");
1318 /* check latest key is valid */
1319 if (body->ltx || body->lrx) {
1321 os_memcpy(ki.mi, body->lsrv_mi, sizeof(ki.mi));
1322 ki.kn = be_to_host32(body->lkn);
1323 dl_list_for_each(sa_key, &participant->sak_list,
1324 struct data_key, list) {
1325 if (is_ki_equal(&sa_key->key_identifier, &ki)) {
1331 wpa_printf(MSG_WARNING, "KaY: Latest key is invalid");
1334 if (os_memcmp(participant->lki.mi, body->lsrv_mi,
1335 sizeof(participant->lki.mi)) == 0 &&
1336 be_to_host32(body->lkn) == participant->lki.kn &&
1337 body->lan == participant->lan) {
1338 peer->sak_used = TRUE;
1340 if (body->ltx && peer->is_key_server) {
1341 ieee802_1x_cp_set_servertransmitting(
1342 participant->kay->cp, TRUE);
1343 ieee802_1x_cp_sm_step(participant->kay->cp);
1347 /* check old key is valid */
1348 if (body->otx || body->orx) {
1349 if (os_memcmp(participant->oki.mi, body->osrv_mi,
1350 sizeof(participant->oki.mi)) != 0 ||
1351 be_to_host32(body->okn) != participant->oki.kn ||
1352 body->oan != participant->oan) {
1353 wpa_printf(MSG_WARNING, "KaY: Old key is invalid");
1358 /* TODO: how to set the MACsec hardware when delay_protect is true */
1359 if (body->delay_protect &&
1360 (!be_to_host32(body->llpn) || !be_to_host32(body->olpn))) {
1361 wpa_printf(MSG_WARNING,
1362 "KaY: Lowest packet number should greater than 0 when delay_protect is TRUE");
1366 /* check all live peer have used the sak for receiving sa */
1367 all_receiving = TRUE;
1368 dl_list_for_each(peer, &participant->live_peers,
1369 struct ieee802_1x_kay_peer, list) {
1370 if (!peer->sak_used) {
1371 all_receiving = FALSE;
1375 if (all_receiving) {
1376 participant->to_dist_sak = FALSE;
1377 ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
1378 ieee802_1x_cp_sm_step(participant->kay->cp);
1381 /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
1382 lpn = be_to_host32(body->llpn);
1383 if (lpn > participant->kay->pn_exhaustion) {
1384 if (participant->is_key_server) {
1385 participant->new_sak = TRUE;
1386 wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
1391 dl_list_for_each(txsa, &participant->txsc->sa_list,
1392 struct transmit_sa, list) {
1393 if (sa_key != NULL && txsa->pkey == sa_key) {
1399 wpa_printf(MSG_WARNING, "KaY: Can't find txsa");
1403 /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
1404 * npn is larger than txsa's npn, set it to txsa.
1406 secy_get_transmit_next_pn(participant->kay, txsa);
1407 if (lpn > txsa->next_pn) {
1408 secy_set_transmit_next_pn(participant->kay, txsa);
1409 wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
1417 * ieee802_1x_mka_dist_sak_body_present
1420 ieee802_1x_mka_dist_sak_body_present(
1421 struct ieee802_1x_mka_participant *participant)
1423 if (!participant->to_dist_sak || !participant->new_key)
1431 * ieee802_1x_kay_get_dist_sak_length
1434 ieee802_1x_mka_get_dist_sak_length(
1435 struct ieee802_1x_mka_participant *participant)
1437 int length = MKA_HDR_LEN;
1438 int cs_index = participant->kay->macsec_csindex;
1440 if (participant->advised_desired) {
1441 length = sizeof(struct ieee802_1x_mka_dist_sak_body);
1442 if (cs_index != DEFAULT_CS_INDEX)
1443 length += CS_ID_LEN;
1445 length += cipher_suite_tbl[cs_index].sak_len + 8;
1448 return MKA_ALIGN_LENGTH(length);
1453 * ieee802_1x_mka_encode_dist_sak_body -
1456 ieee802_1x_mka_encode_dist_sak_body(
1457 struct ieee802_1x_mka_participant *participant,
1460 struct ieee802_1x_mka_dist_sak_body *body;
1461 struct data_key *sak;
1462 unsigned int length;
1466 length = ieee802_1x_mka_get_dist_sak_length(participant);
1467 body = wpabuf_put(buf, length);
1468 body->type = MKA_DISTRIBUTED_SAK;
1469 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1470 if (length == MKA_HDR_LEN) {
1471 body->confid_offset = 0;
1476 sak = participant->new_key;
1477 body->confid_offset = sak->confidentiality_offset;
1478 body->dan = sak->an;
1479 body->kn = host_to_be32(sak->key_identifier.kn);
1480 cs_index = participant->kay->macsec_csindex;
1482 if (cs_index != DEFAULT_CS_INDEX) {
1483 os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
1484 sak_pos = CS_ID_LEN;
1486 if (aes_wrap(participant->kek.key, 16,
1487 cipher_suite_tbl[cs_index].sak_len / 8,
1488 sak->key, body->sak + sak_pos)) {
1489 wpa_printf(MSG_ERROR, "KaY: AES wrap failed");
1493 ieee802_1x_mka_dump_dist_sak_body(body);
1500 * ieee802_1x_kay_init_data_key -
1502 static struct data_key *
1503 ieee802_1x_kay_init_data_key(const struct key_conf *conf)
1505 struct data_key *pkey;
1510 pkey = os_zalloc(sizeof(*pkey));
1512 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1516 pkey->key = os_zalloc(conf->key_len);
1517 if (pkey->key == NULL) {
1518 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
1523 os_memcpy(pkey->key, conf->key, conf->key_len);
1524 os_memcpy(&pkey->key_identifier, &conf->ki,
1525 sizeof(pkey->key_identifier));
1526 pkey->confidentiality_offset = conf->offset;
1527 pkey->an = conf->an;
1528 pkey->transmits = conf->tx;
1529 pkey->receives = conf->rx;
1530 os_get_time(&pkey->created_time);
1539 * ieee802_1x_kay_decode_dist_sak_body -
1542 ieee802_1x_mka_decode_dist_sak_body(
1543 struct ieee802_1x_mka_participant *participant,
1544 const u8 *mka_msg, size_t msg_len)
1546 struct ieee802_1x_mka_hdr *hdr;
1547 struct ieee802_1x_mka_dist_sak_body *body;
1548 struct ieee802_1x_kay_peer *peer;
1549 struct macsec_ciphersuite *cs;
1551 struct key_conf *conf;
1552 struct data_key *sa_key = NULL;
1553 struct ieee802_1x_mka_ki sak_ki;
1558 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1559 body_len = get_mka_param_body_len(hdr);
1560 if ((body_len != 0) && (body_len != 28) && (body_len < 36)) {
1561 wpa_printf(MSG_ERROR,
1562 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1567 if (!participant->principal) {
1568 wpa_printf(MSG_ERROR,
1569 "KaY: I can't accept the distributed SAK as I am not principal");
1572 if (participant->is_key_server) {
1573 wpa_printf(MSG_ERROR,
1574 "KaY: I can't accept the distributed SAK as myself is key server ");
1577 if (!participant->kay->macsec_desired ||
1578 participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
1579 wpa_printf(MSG_ERROR,
1580 "KaY: I am not MACsec-desired or without MACsec capable");
1584 peer = ieee802_1x_kay_get_live_peer(participant,
1585 participant->current_peer_id.mi);
1587 wpa_printf(MSG_ERROR,
1588 "KaY: The key server is not in my live peers list");
1591 if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
1592 wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
1595 if (body_len == 0) {
1596 participant->kay->authenticated = TRUE;
1597 participant->kay->secured = FALSE;
1598 participant->kay->failed = FALSE;
1599 participant->advised_desired = FALSE;
1600 ieee802_1x_cp_connect_authenticated(participant->kay->cp);
1601 ieee802_1x_cp_sm_step(participant->kay->cp);
1602 wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
1603 participant->to_use_sak = TRUE;
1606 participant->advised_desired = TRUE;
1607 participant->kay->authenticated = FALSE;
1608 participant->kay->secured = TRUE;
1609 participant->kay->failed = FALSE;
1610 ieee802_1x_cp_connect_secure(participant->kay->cp);
1611 ieee802_1x_cp_sm_step(participant->kay->cp);
1613 body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
1614 ieee802_1x_mka_dump_dist_sak_body(body);
1615 dl_list_for_each(sa_key, &participant->sak_list, struct data_key, list)
1617 if (os_memcmp(sa_key->key_identifier.mi,
1618 participant->current_peer_id.mi, MI_LEN) == 0 &&
1619 sa_key->key_identifier.kn == be_to_host32(body->kn)) {
1620 wpa_printf(MSG_WARNING, "KaY:The Key has installed");
1624 if (body_len == 28) {
1625 sak_len = DEFAULT_SA_KEY_LEN;
1626 wrap_sak = body->sak;
1627 participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
1629 cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
1631 wpa_printf(MSG_ERROR,
1632 "KaY: I can't support the Cipher Suite advised by key server");
1635 sak_len = cs->sak_len;
1636 wrap_sak = body->sak + CS_ID_LEN;
1637 participant->kay->macsec_csindex = cs->index;
1640 unwrap_sak = os_zalloc(sak_len);
1642 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1645 if (aes_unwrap(participant->kek.key, 16, sak_len >> 3, wrap_sak,
1647 wpa_printf(MSG_ERROR, "KaY: AES unwrap failed");
1648 os_free(unwrap_sak);
1651 wpa_hexdump(MSG_DEBUG, "\tAES Key Unwrap of SAK:", unwrap_sak, sak_len);
1653 conf = os_zalloc(sizeof(*conf));
1655 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1656 os_free(unwrap_sak);
1659 conf->key_len = sak_len;
1661 conf->key = os_zalloc(conf->key_len);
1663 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
1664 os_free(unwrap_sak);
1669 os_memcpy(conf->key, unwrap_sak, conf->key_len);
1671 os_memcpy(&sak_ki.mi, &participant->current_peer_id.mi,
1673 sak_ki.kn = be_to_host32(body->kn);
1675 os_memcpy(conf->ki.mi, sak_ki.mi, MI_LEN);
1676 conf->ki.kn = sak_ki.kn;
1677 conf->an = body->dan;
1678 conf->offset = body->confid_offset;
1682 sa_key = ieee802_1x_kay_init_data_key(conf);
1684 os_free(unwrap_sak);
1690 dl_list_add(&participant->sak_list, &sa_key->list);
1692 ieee802_1x_cp_set_ciphersuite(
1693 participant->kay->cp,
1694 cipher_suite_tbl[participant->kay->macsec_csindex].id);
1695 ieee802_1x_cp_sm_step(participant->kay->cp);
1696 ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
1697 ieee802_1x_cp_sm_step(participant->kay->cp);
1698 ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
1699 ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
1700 ieee802_1x_cp_signal_newsak(participant->kay->cp);
1701 ieee802_1x_cp_sm_step(participant->kay->cp);
1703 participant->to_use_sak = TRUE;
1705 os_free(unwrap_sak);
1714 * ieee802_1x_mka_icv_body_present
1717 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant *participant)
1724 * ieee802_1x_kay_get_icv_length
1727 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant *participant)
1731 length = sizeof(struct ieee802_1x_mka_icv_body);
1732 length += mka_alg_tbl[participant->kay->mka_algindex].icv_len;
1734 return MKA_ALIGN_LENGTH(length);
1739 * ieee802_1x_mka_encode_icv_body -
1742 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant *participant,
1745 struct ieee802_1x_mka_icv_body *body;
1746 unsigned int length;
1747 u8 cmac[MAX_ICV_LEN];
1749 length = ieee802_1x_mka_get_icv_length(participant);
1750 if (length != DEFAULT_ICV_LEN) {
1751 body = wpabuf_put(buf, MKA_HDR_LEN);
1752 body->type = MKA_ICV_INDICATOR;
1753 set_mka_param_body_len(body, length - MKA_HDR_LEN);
1756 if (mka_alg_tbl[participant->kay->mka_algindex].icv_hash(
1757 participant->ick.key, wpabuf_head(buf), buf->used, cmac)) {
1758 wpa_printf(MSG_ERROR, "KaY, omac1_aes_128 failed");
1762 if (length != DEFAULT_ICV_LEN) {
1763 os_memcpy(wpabuf_put(buf, length - MKA_HDR_LEN), cmac,
1764 length - MKA_HDR_LEN);
1766 os_memcpy(wpabuf_put(buf, length), cmac, length);
1773 * ieee802_1x_mka_decode_icv_body -
1776 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant *participant,
1777 const u8 *mka_msg, size_t msg_len)
1779 struct ieee802_1x_mka_hdr *hdr;
1780 struct ieee802_1x_mka_icv_body *body;
1788 while (left_len > (MKA_HDR_LEN + DEFAULT_ICV_LEN)) {
1789 hdr = (struct ieee802_1x_mka_hdr *) pos;
1790 body_len = get_mka_param_body_len(hdr);
1791 body_type = get_mka_param_body_type(hdr);
1793 if (left_len < (body_len + MKA_HDR_LEN))
1796 if (body_type != MKA_ICV_INDICATOR) {
1797 left_len -= MKA_HDR_LEN + body_len;
1798 pos += MKA_HDR_LEN + body_len;
1802 body = (struct ieee802_1x_mka_icv_body *)pos;
1804 < mka_alg_tbl[participant->kay->mka_algindex].icv_len) {
1811 return (u8 *) (mka_msg + msg_len - DEFAULT_ICV_LEN);
1816 * ieee802_1x_mka_decode_dist_cak_body-
1819 ieee802_1x_mka_decode_dist_cak_body(
1820 struct ieee802_1x_mka_participant *participant,
1821 const u8 *mka_msg, size_t msg_len)
1823 struct ieee802_1x_mka_hdr *hdr;
1826 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1827 body_len = get_mka_param_body_len(hdr);
1828 if (body_len < 28) {
1829 wpa_printf(MSG_ERROR,
1830 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 28 or more octets",
1840 * ieee802_1x_mka_decode_kmd_body -
1843 ieee802_1x_mka_decode_kmd_body(
1844 struct ieee802_1x_mka_participant *participant,
1845 const u8 *mka_msg, size_t msg_len)
1847 struct ieee802_1x_mka_hdr *hdr;
1850 hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
1851 body_len = get_mka_param_body_len(hdr);
1853 wpa_printf(MSG_ERROR,
1854 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 5 or more octets",
1864 * ieee802_1x_mka_decode_announce_body -
1866 static int ieee802_1x_mka_decode_announce_body(
1867 struct ieee802_1x_mka_participant *participant,
1868 const u8 *mka_msg, size_t msg_len)
1874 static struct mka_param_body_handler mka_body_handler[] = {
1875 /* basic parameter set */
1877 ieee802_1x_mka_encode_basic_body,
1879 ieee802_1x_mka_basic_body_length,
1880 ieee802_1x_mka_basic_body_present
1883 /* live peer list parameter set */
1885 ieee802_1x_mka_encode_live_peer_body,
1886 ieee802_1x_mka_decode_live_peer_body,
1887 ieee802_1x_mka_get_live_peer_length,
1888 ieee802_1x_mka_live_peer_body_present
1891 /* potential peer list parameter set */
1893 ieee802_1x_mka_encode_potential_peer_body,
1894 ieee802_1x_mka_decode_potential_peer_body,
1895 ieee802_1x_mka_get_potential_peer_length,
1896 ieee802_1x_mka_potential_peer_body_present
1899 /* sak use parameter set */
1901 ieee802_1x_mka_encode_sak_use_body,
1902 ieee802_1x_mka_decode_sak_use_body,
1903 ieee802_1x_mka_get_sak_use_length,
1904 ieee802_1x_mka_sak_use_body_present
1907 /* distribute sak parameter set */
1909 ieee802_1x_mka_encode_dist_sak_body,
1910 ieee802_1x_mka_decode_dist_sak_body,
1911 ieee802_1x_mka_get_dist_sak_length,
1912 ieee802_1x_mka_dist_sak_body_present
1915 /* distribute cak parameter set */
1918 ieee802_1x_mka_decode_dist_cak_body,
1923 /* kmd parameter set */
1926 ieee802_1x_mka_decode_kmd_body,
1931 /* announce parameter set */
1934 ieee802_1x_mka_decode_announce_body,
1939 /* icv parameter set */
1941 ieee802_1x_mka_encode_icv_body,
1943 ieee802_1x_mka_get_icv_length,
1944 ieee802_1x_mka_icv_body_present
1950 * ieee802_1x_kay_deinit_data_key -
1952 static void ieee802_1x_kay_deinit_data_key(struct data_key *pkey)
1961 dl_list_del(&pkey->list);
1968 * ieee802_1x_kay_generate_new_sak -
1971 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
1973 struct data_key *sa_key = NULL;
1974 struct key_conf *conf;
1975 struct ieee802_1x_kay_peer *peer;
1976 struct ieee802_1x_kay *kay = participant->kay;
1977 int ctx_len, ctx_offset;
1980 /* check condition for generating a fresh SAK:
1981 * must have one live peer
1982 * and MKA life time elapse since last distribution
1983 * or potential peer is empty
1985 if (dl_list_empty(&participant->live_peers)) {
1986 wpa_printf(MSG_ERROR,
1987 "KaY: Live peers list must not empty when generating fresh SAK");
1991 /* FIXME: A fresh SAK not generated until
1992 * the live peer list contains at least one peer and
1993 * MKA life time has elapsed since the prior SAK was first distributed,
1994 * or the Key server's potential peer is empty
1995 * but I can't understand the second item, so
1996 * here only check first item and ingore
1997 * && (!dl_list_empty(&participant->potential_peers))) {
1999 if ((time(NULL) - kay->dist_time) < MKA_LIFE_TIME / 1000) {
2000 wpa_printf(MSG_ERROR,
2001 "KaY: Life time have not elapsed since prior SAK distributed");
2005 conf = os_zalloc(sizeof(*conf));
2007 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2010 conf->key_len = cipher_suite_tbl[kay->macsec_csindex].sak_len;
2012 conf->key = os_zalloc(conf->key_len);
2015 wpa_printf(MSG_ERROR, "KaY-%s: Out of memory", __func__);
2019 ctx_len = conf->key_len + sizeof(kay->dist_kn);
2020 dl_list_for_each(peer, &participant->live_peers,
2021 struct ieee802_1x_kay_peer, list)
2022 ctx_len += sizeof(peer->mi);
2023 ctx_len += sizeof(participant->mi);
2025 context = os_zalloc(ctx_len);
2032 if (os_get_random(context + ctx_offset, conf->key_len) < 0) {
2038 ctx_offset += conf->key_len;
2039 dl_list_for_each(peer, &participant->live_peers,
2040 struct ieee802_1x_kay_peer, list) {
2041 os_memcpy(context + ctx_offset, peer->mi, sizeof(peer->mi));
2042 ctx_offset += sizeof(peer->mi);
2044 os_memcpy(context + ctx_offset, participant->mi,
2045 sizeof(participant->mi));
2046 ctx_offset += sizeof(participant->mi);
2047 os_memcpy(context + ctx_offset, &kay->dist_kn, sizeof(kay->dist_kn));
2049 if (conf->key_len == 16) {
2050 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2051 context, ctx_len, conf->key);
2052 } else if (conf->key_len == 32) {
2053 ieee802_1x_sak_128bits_aes_cmac(participant->cak.key,
2054 context, ctx_len, conf->key);
2056 wpa_printf(MSG_ERROR, "KaY: SAK Length not support");
2062 wpa_hexdump(MSG_DEBUG, "KaY: generated new SAK",
2063 conf->key, conf->key_len);
2065 os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
2066 conf->ki.kn = participant->kay->dist_kn;
2067 conf->an = participant->kay->dist_an;
2068 conf->offset = kay->macsec_confidentiality;
2072 sa_key = ieee802_1x_kay_init_data_key(conf);
2079 participant->new_key = sa_key;
2081 dl_list_add(&participant->sak_list, &sa_key->list);
2082 ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
2083 cipher_suite_tbl[kay->macsec_csindex].id);
2084 ieee802_1x_cp_sm_step(kay->cp);
2085 ieee802_1x_cp_set_offset(kay->cp, conf->offset);
2086 ieee802_1x_cp_sm_step(kay->cp);
2087 ieee802_1x_cp_set_distributedki(kay->cp, &conf->ki);
2088 ieee802_1x_cp_set_distributedan(kay->cp, conf->an);
2089 ieee802_1x_cp_signal_newsak(kay->cp);
2090 ieee802_1x_cp_sm_step(kay->cp);
2092 dl_list_for_each(peer, &participant->live_peers,
2093 struct ieee802_1x_kay_peer, list)
2094 peer->sak_used = FALSE;
2096 participant->kay->dist_kn++;
2097 participant->kay->dist_an++;
2098 if (participant->kay->dist_an > 3)
2099 participant->kay->dist_an = 0;
2101 participant->kay->dist_time = time(NULL);
2111 * ieee802_1x_kay_elect_key_server - elect the key server
2112 * when to elect: whenever the live peers list changes
2115 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
2117 struct ieee802_1x_kay_peer *peer;
2118 struct ieee802_1x_kay_peer *key_server = NULL;
2119 struct ieee802_1x_kay *kay = participant->kay;
2120 Boolean i_is_key_server;
2122 if (participant->is_obliged_key_server) {
2123 participant->new_sak = TRUE;
2124 participant->to_dist_sak = FALSE;
2125 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2129 /* elect the key server among the peers */
2130 dl_list_for_each(peer, &participant->live_peers,
2131 struct ieee802_1x_kay_peer, list) {
2132 if (!peer->is_key_server)
2140 if (peer->key_server_priority <
2141 key_server->key_server_priority) {
2143 } else if (peer->key_server_priority ==
2144 key_server->key_server_priority) {
2145 if (os_memcmp(peer->sci.addr, key_server->sci.addr,
2151 /* elect the key server between me and the above elected peer */
2152 i_is_key_server = FALSE;
2153 if (key_server && participant->can_be_key_server) {
2154 if (kay->actor_priority
2155 < key_server->key_server_priority) {
2156 i_is_key_server = TRUE;
2157 } else if (kay->actor_priority
2158 == key_server->key_server_priority) {
2159 if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
2161 i_is_key_server = TRUE;
2163 } else if (participant->can_be_key_server) {
2164 i_is_key_server = TRUE;
2167 if (i_is_key_server) {
2168 ieee802_1x_cp_set_electedself(kay->cp, TRUE);
2169 if (!sci_equal(&kay->key_server_sci, &kay->actor_sci)) {
2170 ieee802_1x_cp_signal_chgdserver(kay->cp);
2171 ieee802_1x_cp_sm_step(kay->cp);
2174 participant->is_key_server = TRUE;
2175 participant->principal = TRUE;
2176 participant->new_sak = TRUE;
2177 wpa_printf(MSG_DEBUG, "KaY: I is elected as key server");
2178 participant->to_dist_sak = FALSE;
2179 participant->is_elected = TRUE;
2181 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
2182 sizeof(kay->key_server_sci));
2183 kay->key_server_priority = kay->actor_priority;
2184 } else if (key_server) {
2185 ieee802_1x_cp_set_electedself(kay->cp, FALSE);
2186 if (!sci_equal(&kay->key_server_sci, &key_server->sci)) {
2187 ieee802_1x_cp_signal_chgdserver(kay->cp);
2188 ieee802_1x_cp_sm_step(kay->cp);
2191 participant->is_key_server = FALSE;
2192 participant->principal = TRUE;
2193 participant->is_elected = TRUE;
2195 os_memcpy(&kay->key_server_sci, &key_server->sci,
2196 sizeof(kay->key_server_sci));
2197 kay->key_server_priority = key_server->key_server_priority;
2199 participant->principal = FALSE;
2200 participant->is_key_server = FALSE;
2201 participant->is_elected = FALSE;
2209 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2210 * how to use MACsec: whether use MACsec and its capability
2211 * protectFrames will be advised if the key server and one of its live peers are
2212 * MACsec capable and one of those request MACsec protection
2215 ieee802_1x_kay_decide_macsec_use(
2216 struct ieee802_1x_mka_participant *participant)
2218 struct ieee802_1x_kay *kay = participant->kay;
2219 struct ieee802_1x_kay_peer *peer;
2220 enum macsec_cap less_capability;
2223 if (!participant->is_key_server)
2226 /* key server self is MACsec-desired and requesting MACsec */
2227 if (!kay->macsec_desired) {
2228 participant->advised_desired = FALSE;
2231 if (kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
2232 participant->advised_desired = FALSE;
2235 less_capability = kay->macsec_capable;
2237 /* at least one of peers is MACsec-desired and requesting MACsec */
2239 dl_list_for_each(peer, &participant->live_peers,
2240 struct ieee802_1x_kay_peer, list) {
2241 if (!peer->macsec_desired)
2244 if (peer->macsec_capability == MACSEC_CAP_NOT_IMPLEMENTED)
2247 less_capability = (less_capability < peer->macsec_capability) ?
2248 less_capability : peer->macsec_capability;
2253 participant->advised_desired = TRUE;
2254 participant->advised_capability = less_capability;
2255 kay->authenticated = FALSE;
2256 kay->secured = TRUE;
2257 kay->failed = FALSE;
2258 ieee802_1x_cp_connect_secure(kay->cp);
2259 ieee802_1x_cp_sm_step(kay->cp);
2261 participant->advised_desired = FALSE;
2262 participant->advised_capability = MACSEC_CAP_NOT_IMPLEMENTED;
2263 participant->to_use_sak = FALSE;
2264 kay->authenticated = TRUE;
2265 kay->secured = FALSE;
2266 kay->failed = FALSE;
2275 ieee802_1x_cp_connect_authenticated(kay->cp);
2276 ieee802_1x_cp_sm_step(kay->cp);
2282 static const u8 pae_group_addr[ETH_ALEN] = {
2283 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2288 * ieee802_1x_kay_encode_mkpdu -
2291 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant *participant,
2292 struct wpabuf *pbuf)
2295 struct ieee8023_hdr *ether_hdr;
2296 struct ieee802_1x_hdr *eapol_hdr;
2298 ether_hdr = wpabuf_put(pbuf, sizeof(*ether_hdr));
2299 os_memcpy(ether_hdr->dest, pae_group_addr, sizeof(ether_hdr->dest));
2300 os_memcpy(ether_hdr->src, participant->kay->actor_sci.addr,
2301 sizeof(ether_hdr->dest));
2302 ether_hdr->ethertype = host_to_be16(ETH_P_EAPOL);
2304 eapol_hdr = wpabuf_put(pbuf, sizeof(*eapol_hdr));
2305 eapol_hdr->version = EAPOL_VERSION;
2306 eapol_hdr->type = IEEE802_1X_TYPE_EAPOL_MKA;
2307 eapol_hdr->length = host_to_be16(pbuf->size - pbuf->used);
2309 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2310 if (mka_body_handler[i].body_present &&
2311 mka_body_handler[i].body_present(participant)) {
2312 if (mka_body_handler[i].body_tx(participant, pbuf))
2321 * ieee802_1x_participant_send_mkpdu -
2324 ieee802_1x_participant_send_mkpdu(
2325 struct ieee802_1x_mka_participant *participant)
2328 struct ieee802_1x_kay *kay = participant->kay;
2332 wpa_printf(MSG_DEBUG, "KaY: to enpacket and send the MKPDU");
2333 length += sizeof(struct ieee802_1x_hdr) + sizeof(struct ieee8023_hdr);
2334 for (i = 0; i < ARRAY_SIZE(mka_body_handler); i++) {
2335 if (mka_body_handler[i].body_present &&
2336 mka_body_handler[i].body_present(participant))
2337 length += mka_body_handler[i].body_length(participant);
2340 buf = wpabuf_alloc(length);
2342 wpa_printf(MSG_ERROR, "KaY: out of memory");
2346 if (ieee802_1x_kay_encode_mkpdu(participant, buf)) {
2347 wpa_printf(MSG_ERROR, "KaY: encode mkpdu fail!");
2351 l2_packet_send(kay->l2_mka, NULL, 0, wpabuf_head(buf), wpabuf_len(buf));
2355 participant->active = TRUE;
2361 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa);
2363 * ieee802_1x_participant_timer -
2365 static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
2367 struct ieee802_1x_mka_participant *participant;
2368 struct ieee802_1x_kay *kay;
2369 struct ieee802_1x_kay_peer *peer, *pre_peer;
2370 time_t now = time(NULL);
2372 struct receive_sc *rxsc, *pre_rxsc;
2373 struct transmit_sa *txsa, *pre_txsa;
2375 participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
2376 kay = participant->kay;
2377 if (participant->cak_life) {
2378 if (now > participant->cak_life) {
2379 kay->authenticated = FALSE;
2380 kay->secured = FALSE;
2382 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
2387 /* should delete MKA instance if there are not live peers
2388 * when the MKA life elapsed since its creating */
2389 if (participant->mka_life) {
2390 if (dl_list_empty(&participant->live_peers)) {
2391 if (now > participant->mka_life) {
2392 kay->authenticated = FALSE;
2393 kay->secured = FALSE;
2395 ieee802_1x_kay_delete_mka(kay,
2400 participant->mka_life = 0;
2405 dl_list_for_each_safe(peer, pre_peer, &participant->live_peers,
2406 struct ieee802_1x_kay_peer, list) {
2407 if (now > peer->expire) {
2408 wpa_printf(MSG_DEBUG, "KaY: Live peer removed");
2409 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2411 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2412 dl_list_for_each_safe(rxsc, pre_rxsc,
2413 &participant->rxsc_list,
2414 struct receive_sc, list) {
2415 if (sci_equal(&rxsc->sci, &peer->sci)) {
2416 secy_delete_receive_sc(kay, rxsc);
2417 ieee802_1x_kay_deinit_receive_sc(
2421 dl_list_del(&peer->list);
2428 if (dl_list_empty(&participant->live_peers)) {
2429 participant->advised_desired = FALSE;
2430 participant->advised_capability =
2431 MACSEC_CAP_NOT_IMPLEMENTED;
2432 participant->to_use_sak = FALSE;
2433 kay->authenticated = TRUE;
2434 kay->secured = FALSE;
2435 kay->failed = FALSE;
2444 dl_list_for_each_safe(txsa, pre_txsa,
2445 &participant->txsc->sa_list,
2446 struct transmit_sa, list) {
2447 secy_disable_transmit_sa(kay, txsa);
2448 ieee802_1x_kay_deinit_transmit_sa(txsa);
2451 ieee802_1x_cp_connect_authenticated(kay->cp);
2452 ieee802_1x_cp_sm_step(kay->cp);
2454 ieee802_1x_kay_elect_key_server(participant);
2455 ieee802_1x_kay_decide_macsec_use(participant);
2459 dl_list_for_each_safe(peer, pre_peer, &participant->potential_peers,
2460 struct ieee802_1x_kay_peer, list) {
2461 if (now > peer->expire) {
2462 wpa_printf(MSG_DEBUG, "KaY: Potential peer removed");
2463 wpa_hexdump(MSG_DEBUG, "\tMI: ", peer->mi,
2465 wpa_printf(MSG_DEBUG, "\tMN: %d", peer->mn);
2466 dl_list_del(&peer->list);
2471 if (participant->new_sak) {
2472 if (!ieee802_1x_kay_generate_new_sak(participant))
2473 participant->to_dist_sak = TRUE;
2475 participant->new_sak = FALSE;
2478 if (participant->retry_count < MAX_RETRY_CNT) {
2479 ieee802_1x_participant_send_mkpdu(participant);
2480 participant->retry_count++;
2483 eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
2484 ieee802_1x_participant_timer,
2490 * ieee802_1x_kay_init_transmit_sa -
2492 static struct transmit_sa *
2493 ieee802_1x_kay_init_transmit_sa(struct transmit_sc *psc, u8 an, u32 next_PN,
2494 struct data_key *key)
2496 struct transmit_sa *psa;
2498 key->tx_latest = TRUE;
2499 key->rx_latest = TRUE;
2501 psa = os_zalloc(sizeof(*psa));
2503 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2507 if (key->confidentiality_offset >= CONFIDENTIALITY_OFFSET_0 &&
2508 key->confidentiality_offset <= CONFIDENTIALITY_OFFSET_50)
2509 psa->confidentiality = TRUE;
2511 psa->confidentiality = FALSE;
2515 psa->next_pn = next_PN;
2518 os_get_time(&psa->created_time);
2519 psa->in_use = FALSE;
2521 dl_list_add(&psc->sa_list, &psa->list);
2522 wpa_printf(MSG_DEBUG,
2523 "KaY: Create transmit SA(an: %hhu, next_PN: %u) of SC(channel: %d)",
2524 an, next_PN, psc->channel);
2531 * ieee802_1x_kay_deinit_transmit_sa -
2533 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa *psa)
2536 wpa_printf(MSG_DEBUG,
2537 "KaY: Delete transmit SA(an: %hhu) of SC",
2539 dl_list_del(&psa->list);
2545 * init_transmit_sc -
2547 static struct transmit_sc *
2548 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci *sci,
2551 struct transmit_sc *psc;
2553 psc = os_zalloc(sizeof(*psc));
2555 wpa_printf(MSG_ERROR, "%s: out of memory", __func__);
2558 os_memcpy(&psc->sci, sci, sizeof(psc->sci));
2559 psc->channel = channel;
2561 os_get_time(&psc->created_time);
2562 psc->transmitting = FALSE;
2563 psc->encoding_sa = FALSE;
2564 psc->enciphering_sa = FALSE;
2566 dl_list_init(&psc->sa_list);
2567 wpa_printf(MSG_DEBUG, "KaY: Create transmit SC(channel: %d)", channel);
2568 wpa_hexdump(MSG_DEBUG, "SCI: ", (u8 *)sci , sizeof(*sci));
2575 * ieee802_1x_kay_deinit_transmit_sc -
2578 ieee802_1x_kay_deinit_transmit_sc(
2579 struct ieee802_1x_mka_participant *participant, struct transmit_sc *psc)
2581 struct transmit_sa *psa, *tmp;
2583 wpa_printf(MSG_DEBUG, "KaY: Delete transmit SC(channel: %d)",
2585 dl_list_for_each_safe(psa, tmp, &psc->sa_list, struct transmit_sa,
2587 secy_disable_transmit_sa(participant->kay, psa);
2588 ieee802_1x_kay_deinit_transmit_sa(psa);
2595 /****************** Interface between CP and KAY *********************/
2597 * ieee802_1x_kay_set_latest_sa_attr -
2599 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay *kay,
2600 struct ieee802_1x_mka_ki *lki, u8 lan,
2601 Boolean ltx, Boolean lrx)
2603 struct ieee802_1x_mka_participant *principal;
2605 principal = ieee802_1x_kay_get_principal_participant(kay);
2610 os_memset(&principal->lki, 0, sizeof(principal->lki));
2612 os_memcpy(&principal->lki, lki, sizeof(principal->lki));
2614 principal->lan = lan;
2615 principal->ltx = ltx;
2616 principal->lrx = lrx;
2621 kay->ltx_kn = lki->kn;
2622 kay->lrx_kn = lki->kn;
2632 * ieee802_1x_kay_set_old_sa_attr -
2634 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay *kay,
2635 struct ieee802_1x_mka_ki *oki,
2636 u8 oan, Boolean otx, Boolean orx)
2638 struct ieee802_1x_mka_participant *principal;
2640 principal = ieee802_1x_kay_get_principal_participant(kay);
2645 os_memset(&principal->oki, 0, sizeof(principal->oki));
2647 os_memcpy(&principal->oki, oki, sizeof(principal->oki));
2649 principal->oan = oan;
2650 principal->otx = otx;
2651 principal->orx = orx;
2657 kay->otx_kn = oki->kn;
2658 kay->orx_kn = oki->kn;
2668 * ieee802_1x_kay_create_sas -
2670 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay *kay,
2671 struct ieee802_1x_mka_ki *lki)
2673 struct data_key *sa_key, *latest_sak;
2674 struct ieee802_1x_mka_participant *principal;
2675 struct receive_sc *rxsc;
2676 struct receive_sa *rxsa;
2677 struct transmit_sa *txsa;
2679 principal = ieee802_1x_kay_get_principal_participant(kay);
2684 dl_list_for_each(sa_key, &principal->sak_list, struct data_key, list) {
2685 if (is_ki_equal(&sa_key->key_identifier, lki)) {
2686 sa_key->rx_latest = TRUE;
2687 sa_key->tx_latest = TRUE;
2688 latest_sak = sa_key;
2689 principal->to_use_sak = TRUE;
2691 sa_key->rx_latest = FALSE;
2692 sa_key->tx_latest = FALSE;
2696 wpa_printf(MSG_ERROR, "lki related sak not found");
2700 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2701 rxsa = ieee802_1x_kay_init_receive_sa(rxsc, latest_sak->an, 1,
2706 secy_create_receive_sa(kay, rxsa);
2709 txsa = ieee802_1x_kay_init_transmit_sa(principal->txsc, latest_sak->an,
2714 secy_create_transmit_sa(kay, txsa);
2723 * ieee802_1x_kay_delete_sas -
2725 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay *kay,
2726 struct ieee802_1x_mka_ki *ki)
2728 struct data_key *sa_key, *pre_key;
2729 struct transmit_sa *txsa, *pre_txsa;
2730 struct receive_sa *rxsa, *pre_rxsa;
2731 struct receive_sc *rxsc;
2732 struct ieee802_1x_mka_participant *principal;
2734 wpa_printf(MSG_DEBUG, "KaY: Entry into %s", __func__);
2735 principal = ieee802_1x_kay_get_principal_participant(kay);
2739 /* remove the transmit sa */
2740 dl_list_for_each_safe(txsa, pre_txsa, &principal->txsc->sa_list,
2741 struct transmit_sa, list) {
2742 if (is_ki_equal(&txsa->pkey->key_identifier, ki)) {
2743 secy_disable_transmit_sa(kay, txsa);
2744 ieee802_1x_kay_deinit_transmit_sa(txsa);
2748 /* remove the receive sa */
2749 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2750 dl_list_for_each_safe(rxsa, pre_rxsa, &rxsc->sa_list,
2751 struct receive_sa, list) {
2752 if (is_ki_equal(&rxsa->pkey->key_identifier, ki)) {
2753 secy_disable_receive_sa(kay, rxsa);
2754 ieee802_1x_kay_deinit_receive_sa(rxsa);
2759 /* remove the sak */
2760 dl_list_for_each_safe(sa_key, pre_key, &principal->sak_list,
2761 struct data_key, list) {
2762 if (is_ki_equal(&sa_key->key_identifier, ki)) {
2763 ieee802_1x_kay_deinit_data_key(sa_key);
2766 if (principal->new_key == sa_key)
2767 principal->new_key = NULL;
2775 * ieee802_1x_kay_enable_tx_sas -
2777 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay *kay,
2778 struct ieee802_1x_mka_ki *lki)
2780 struct ieee802_1x_mka_participant *principal;
2781 struct transmit_sa *txsa;
2783 principal = ieee802_1x_kay_get_principal_participant(kay);
2787 dl_list_for_each(txsa, &principal->txsc->sa_list, struct transmit_sa,
2789 if (is_ki_equal(&txsa->pkey->key_identifier, lki)) {
2790 txsa->in_use = TRUE;
2791 secy_enable_transmit_sa(kay, txsa);
2792 ieee802_1x_cp_set_usingtransmitas(
2793 principal->kay->cp, TRUE);
2794 ieee802_1x_cp_sm_step(principal->kay->cp);
2803 * ieee802_1x_kay_enable_rx_sas -
2805 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay *kay,
2806 struct ieee802_1x_mka_ki *lki)
2808 struct ieee802_1x_mka_participant *principal;
2809 struct receive_sa *rxsa;
2810 struct receive_sc *rxsc;
2812 principal = ieee802_1x_kay_get_principal_participant(kay);
2816 dl_list_for_each(rxsc, &principal->rxsc_list, struct receive_sc, list) {
2817 dl_list_for_each(rxsa, &rxsc->sa_list, struct receive_sa, list)
2819 if (is_ki_equal(&rxsa->pkey->key_identifier, lki)) {
2820 rxsa->in_use = TRUE;
2821 secy_enable_receive_sa(kay, rxsa);
2822 ieee802_1x_cp_set_usingreceivesas(
2823 principal->kay->cp, TRUE);
2824 ieee802_1x_cp_sm_step(principal->kay->cp);
2834 * ieee802_1x_kay_enable_new_info -
2836 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay *kay)
2838 struct ieee802_1x_mka_participant *principal;
2840 principal = ieee802_1x_kay_get_principal_participant(kay);
2844 if (principal->retry_count < MAX_RETRY_CNT) {
2845 ieee802_1x_participant_send_mkpdu(principal);
2846 principal->retry_count++;
2854 * ieee802_1x_kay_cp_conf -
2856 int ieee802_1x_kay_cp_conf(struct ieee802_1x_kay *kay,
2857 struct ieee802_1x_cp_conf *pconf)
2859 pconf->protect = kay->macsec_protect;
2860 pconf->replay_protect = kay->macsec_replay_protect;
2861 pconf->validate = kay->macsec_validate;
2868 * ieee802_1x_kay_alloc_cp_sm -
2870 static struct ieee802_1x_cp_sm *
2871 ieee802_1x_kay_alloc_cp_sm(struct ieee802_1x_kay *kay)
2873 struct ieee802_1x_cp_conf conf;
2875 os_memset(&conf, 0, sizeof(conf));
2876 conf.protect = kay->macsec_protect;
2877 conf.replay_protect = kay->macsec_replay_protect;
2878 conf.validate = kay->macsec_validate;
2879 conf.replay_window = kay->macsec_replay_window;
2881 return ieee802_1x_cp_sm_init(kay, &conf);
2886 * ieee802_1x_kay_mkpdu_sanity_check -
2887 * sanity check specified in clause 11.11.2 of IEEE802.1X-2010
2889 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
2890 const u8 *buf, size_t len)
2892 struct ieee8023_hdr *eth_hdr;
2893 struct ieee802_1x_hdr *eapol_hdr;
2894 struct ieee802_1x_mka_hdr *mka_hdr;
2895 struct ieee802_1x_mka_basic_body *body;
2897 struct ieee802_1x_mka_participant *participant;
2899 u8 icv[MAX_ICV_LEN];
2902 eth_hdr = (struct ieee8023_hdr *) buf;
2903 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
2904 mka_hdr = (struct ieee802_1x_mka_hdr *) (eapol_hdr + 1);
2906 /* destination address should be not individual address */
2907 if (os_memcmp(eth_hdr->dest, pae_group_addr, ETH_ALEN) != 0) {
2908 wpa_printf(MSG_MSGDUMP,
2909 "KaY: ethernet destination address is not PAE group address");
2913 /* MKPDU should not less than 32 octets */
2914 mka_msg_len = be_to_host16(eapol_hdr->length);
2915 if (mka_msg_len < 32) {
2916 wpa_printf(MSG_MSGDUMP, "KaY: MKPDU is less than 32 octets");
2919 /* MKPDU should multiple 4 octets */
2920 if ((mka_msg_len % 4) != 0) {
2921 wpa_printf(MSG_MSGDUMP,
2922 "KaY: MKPDU is not multiple of 4 octets");
2926 body = (struct ieee802_1x_mka_basic_body *) mka_hdr;
2927 ieee802_1x_mka_dump_basic_body(body);
2928 body_len = get_mka_param_body_len(body);
2929 /* EAPOL-MKA body should comprise basic parameter set and ICV */
2930 if (mka_msg_len < MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN) {
2931 wpa_printf(MSG_ERROR,
2932 "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",
2933 mka_msg_len, MKA_HDR_LEN,
2934 body_len, DEFAULT_ICV_LEN);
2938 /* CKN should be owned by I */
2939 participant = ieee802_1x_kay_get_participant(kay, body->ckn);
2941 wpa_printf(MSG_DEBUG, "CKN is not included in my CA");
2945 /* algorithm agility check */
2946 if (os_memcmp(body->algo_agility, mka_algo_agility,
2947 sizeof(body->algo_agility)) != 0) {
2948 wpa_printf(MSG_ERROR,
2949 "KaY: peer's algorithm agility not supported for me");
2955 * The ICV will comprise the final octets of the packet body, whatever
2956 * its size, not the fixed length 16 octets, indicated by the EAPOL
2957 * packet body length.
2959 if (mka_alg_tbl[kay->mka_algindex].icv_hash(
2960 participant->ick.key,
2961 buf, len - mka_alg_tbl[kay->mka_algindex].icv_len, icv)) {
2962 wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
2965 msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
2969 if (os_memcmp_const(msg_icv, icv,
2970 mka_alg_tbl[kay->mka_algindex].icv_len) !=
2972 wpa_printf(MSG_ERROR,
2973 "KaY: Computed ICV is not equal to Received ICV");
2977 wpa_printf(MSG_ERROR, "KaY: No ICV");
2986 * ieee802_1x_kay_decode_mkpdu -
2988 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
2989 const u8 *buf, size_t len)
2991 struct ieee802_1x_mka_participant *participant;
2992 struct ieee802_1x_mka_hdr *hdr;
2998 Boolean my_included;
2999 Boolean handled[256];
3001 if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
3004 /* handle basic parameter set */
3005 pos = buf + sizeof(struct ieee8023_hdr) + sizeof(struct ieee802_1x_hdr);
3006 left_len = len - sizeof(struct ieee8023_hdr) -
3007 sizeof(struct ieee802_1x_hdr);
3008 participant = ieee802_1x_mka_decode_basic_body(kay, pos, left_len);
3012 /* to skip basic parameter set */
3013 hdr = (struct ieee802_1x_mka_hdr *) pos;
3014 body_len = get_mka_param_body_len(hdr);
3015 pos += body_len + MKA_HDR_LEN;
3016 left_len -= body_len + MKA_HDR_LEN;
3018 /* check i am in the peer's peer list */
3019 my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
3021 /* accept the peer as live peer */
3022 if (!ieee802_1x_kay_is_in_peer(
3024 participant->current_peer_id.mi)) {
3025 if (!ieee802_1x_kay_create_live_peer(
3027 participant->current_peer_id.mi,
3029 participant->current_peer_id.mn)))
3031 ieee802_1x_kay_elect_key_server(participant);
3032 ieee802_1x_kay_decide_macsec_use(participant);
3034 if (ieee802_1x_kay_is_in_potential_peer(
3035 participant, participant->current_peer_id.mi)) {
3036 if (!ieee802_1x_kay_move_live_peer(
3038 participant->current_peer_id.mi,
3039 be_to_host32(participant->
3040 current_peer_id.mn)))
3042 ieee802_1x_kay_elect_key_server(participant);
3043 ieee802_1x_kay_decide_macsec_use(participant);
3048 * Handle other parameter set than basic parameter set.
3049 * Each parameter set should be present only once.
3051 for (i = 0; i < 256; i++)
3055 for (; left_len > MKA_HDR_LEN + DEFAULT_ICV_LEN;
3056 pos += body_len + MKA_HDR_LEN,
3057 left_len -= body_len + MKA_HDR_LEN) {
3058 hdr = (struct ieee802_1x_mka_hdr *) pos;
3059 body_len = get_mka_param_body_len(hdr);
3060 body_type = get_mka_param_body_type(hdr);
3062 if (body_type == MKA_ICV_INDICATOR)
3065 if (left_len < (MKA_HDR_LEN + body_len + DEFAULT_ICV_LEN)) {
3066 wpa_printf(MSG_ERROR,
3067 "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",
3068 left_len, MKA_HDR_LEN,
3069 body_len, DEFAULT_ICV_LEN);
3073 if (handled[body_type])
3076 handled[body_type] = TRUE;
3077 if (body_type < ARRAY_SIZE(mka_body_handler) &&
3078 mka_body_handler[body_type].body_rx) {
3079 mka_body_handler[body_type].body_rx
3080 (participant, pos, left_len);
3082 wpa_printf(MSG_ERROR,
3083 "The type %d is not supported in this MKA version %d",
3084 body_type, MKA_VERSION_ID);
3089 participant->retry_count = 0;
3090 participant->active = TRUE;
3097 static void kay_l2_receive(void *ctx, const u8 *src_addr, const u8 *buf,
3100 struct ieee802_1x_kay *kay = ctx;
3101 struct ieee8023_hdr *eth_hdr;
3102 struct ieee802_1x_hdr *eapol_hdr;
3104 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3105 if (len < sizeof(*eth_hdr) + sizeof(*eapol_hdr)) {
3106 wpa_printf(MSG_MSGDUMP, "KaY: EAPOL frame too short (%lu)",
3107 (unsigned long) len);
3111 eth_hdr = (struct ieee8023_hdr *) buf;
3112 eapol_hdr = (struct ieee802_1x_hdr *) (eth_hdr + 1);
3113 if (len != sizeof(*eth_hdr) + sizeof(*eapol_hdr) +
3114 be_to_host16(eapol_hdr->length)) {
3115 wpa_printf(MSG_MSGDUMP, "KAY: EAPOL MPDU is invalid: (%lu-%lu)",
3116 (unsigned long) len,
3117 (unsigned long) be_to_host16(eapol_hdr->length));
3121 if (eapol_hdr->version < EAPOL_VERSION) {
3122 wpa_printf(MSG_MSGDUMP, "KaY: version %d does not support MKA",
3123 eapol_hdr->version);
3126 if (be_to_host16(eth_hdr->ethertype) != ETH_P_PAE ||
3127 eapol_hdr->type != IEEE802_1X_TYPE_EAPOL_MKA)
3130 wpa_hexdump(MSG_DEBUG, "RX EAPOL-MKA: ", buf, len);
3131 if (dl_list_empty(&kay->participant_list)) {
3132 wpa_printf(MSG_ERROR, "KaY: no MKA participant instance");
3136 ieee802_1x_kay_decode_mkpdu(kay, buf, len);
3141 * ieee802_1x_kay_init -
3143 struct ieee802_1x_kay *
3144 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx *ctx, enum macsec_policy policy,
3145 const char *ifname, const u8 *addr)
3147 struct ieee802_1x_kay *kay;
3149 kay = os_zalloc(sizeof(*kay));
3151 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3158 kay->active = FALSE;
3160 kay->authenticated = FALSE;
3161 kay->secured = FALSE;
3162 kay->failed = FALSE;
3163 kay->policy = policy;
3165 os_strlcpy(kay->if_name, ifname, IFNAMSIZ);
3166 os_memcpy(kay->actor_sci.addr, addr, ETH_ALEN);
3167 kay->actor_sci.port = host_to_be16(0x0001);
3168 kay->actor_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3170 /* While actor acts as a key server, shall distribute sakey */
3175 kay->pn_exhaustion = PENDING_PN_EXHAUSTION;
3176 kay->macsec_csindex = DEFAULT_CS_INDEX;
3177 kay->mka_algindex = DEFAULT_MKA_ALG_INDEX;
3178 kay->mka_version = MKA_VERSION_ID;
3180 os_memcpy(kay->algo_agility, mka_algo_agility,
3181 sizeof(kay->algo_agility));
3183 dl_list_init(&kay->participant_list);
3185 if (policy == DO_NOT_SECURE) {
3186 kay->macsec_capable = MACSEC_CAP_NOT_IMPLEMENTED;
3187 kay->macsec_desired = FALSE;
3188 kay->macsec_protect = FALSE;
3189 kay->macsec_validate = Disabled;
3190 kay->macsec_replay_protect = FALSE;
3191 kay->macsec_replay_window = 0;
3192 kay->macsec_confidentiality = CONFIDENTIALITY_NONE;
3194 kay->macsec_capable = MACSEC_CAP_INTEG_AND_CONF_0_30_50;
3195 kay->macsec_desired = TRUE;
3196 kay->macsec_protect = TRUE;
3197 kay->macsec_validate = Strict;
3198 kay->macsec_replay_protect = FALSE;
3199 kay->macsec_replay_window = 0;
3200 kay->macsec_confidentiality = CONFIDENTIALITY_OFFSET_0;
3203 wpa_printf(MSG_DEBUG, "KaY: state machine created");
3205 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3206 secy_init_macsec(kay);
3207 secy_get_available_transmit_sc(kay, &kay->sc_ch);
3209 wpa_printf(MSG_DEBUG, "KaY: secy init macsec done");
3212 kay->cp = ieee802_1x_kay_alloc_cp_sm(kay);
3213 if (kay->cp == NULL) {
3214 ieee802_1x_kay_deinit(kay);
3218 if (policy == DO_NOT_SECURE) {
3219 ieee802_1x_cp_connect_authenticated(kay->cp);
3220 ieee802_1x_cp_sm_step(kay->cp);
3222 kay->l2_mka = l2_packet_init(kay->if_name, NULL, ETH_P_PAE,
3223 kay_l2_receive, kay, 1);
3224 if (kay->l2_mka == NULL) {
3225 wpa_printf(MSG_WARNING,
3226 "KaY: Failed to initialize L2 packet processing for MKA packet");
3227 ieee802_1x_kay_deinit(kay);
3237 * ieee802_1x_kay_deinit -
3240 ieee802_1x_kay_deinit(struct ieee802_1x_kay *kay)
3242 struct ieee802_1x_mka_participant *participant;
3247 wpa_printf(MSG_DEBUG, "KaY: state machine removed");
3249 while (!dl_list_empty(&kay->participant_list)) {
3250 participant = dl_list_entry(kay->participant_list.next,
3251 struct ieee802_1x_mka_participant,
3253 ieee802_1x_kay_delete_mka(kay, &participant->ckn);
3256 ieee802_1x_cp_sm_deinit(kay->cp);
3257 secy_deinit_macsec(kay);
3260 l2_packet_deinit(kay->l2_mka);
3270 * ieee802_1x_kay_create_mka -
3272 struct ieee802_1x_mka_participant *
3273 ieee802_1x_kay_create_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn,
3274 struct mka_key *cak, u32 life,
3275 enum mka_created_mode mode, Boolean is_authenticator)
3277 struct ieee802_1x_mka_participant *participant;
3280 if (!kay || !ckn || !cak) {
3281 wpa_printf(MSG_ERROR, "KaY: ckn or cak is null");
3285 if (cak->len != mka_alg_tbl[kay->mka_algindex].cak_len) {
3286 wpa_printf(MSG_ERROR, "KaY: CAK length not follow key schema");
3289 if (ckn->len > MAX_CKN_LEN) {
3290 wpa_printf(MSG_ERROR, "KaY: CKN is out of range(<=32 bytes)");
3294 wpa_printf(MSG_ERROR, "KaY: Now is at disable state");
3298 participant = os_zalloc(sizeof(*participant));
3300 wpa_printf(MSG_ERROR, "KaY-%s: out of memory", __func__);
3304 participant->ckn.len = ckn->len;
3305 os_memcpy(participant->ckn.name, ckn->name, ckn->len);
3306 participant->cak.len = cak->len;
3307 os_memcpy(participant->cak.key, cak->key, cak->len);
3309 participant->cak_life = life + time(NULL);
3313 if (is_authenticator) {
3314 participant->is_obliged_key_server = TRUE;
3315 participant->can_be_key_server = TRUE;
3316 participant->is_key_server = TRUE;
3317 participant->principal = TRUE;
3319 os_memcpy(&kay->key_server_sci, &kay->actor_sci,
3320 sizeof(kay->key_server_sci));
3321 kay->key_server_priority = kay->actor_priority;
3322 participant->is_elected = TRUE;
3324 participant->is_obliged_key_server = FALSE;
3325 participant->can_be_key_server = FALSE;
3326 participant->is_key_server = FALSE;
3327 participant->is_elected = TRUE;
3332 participant->is_obliged_key_server = FALSE;
3333 participant->can_be_key_server = TRUE;
3334 participant->is_key_server = TRUE;
3335 participant->is_elected = FALSE;
3339 participant->cached = FALSE;
3341 participant->active = FALSE;
3342 participant->participant = FALSE;
3343 participant->retain = FALSE;
3344 participant->activate = DEFAULT;
3346 if (participant->is_key_server)
3347 participant->principal = TRUE;
3349 dl_list_init(&participant->live_peers);
3350 dl_list_init(&participant->potential_peers);
3352 participant->retry_count = 0;
3353 participant->kay = kay;
3355 if (!reset_participant_mi(participant))
3358 participant->lrx = FALSE;
3359 participant->ltx = FALSE;
3360 participant->orx = FALSE;
3361 participant->otx = FALSE;
3362 participant->to_dist_sak = FALSE;
3363 participant->to_use_sak = FALSE;
3364 participant->new_sak = FALSE;
3365 dl_list_init(&participant->sak_list);
3366 participant->new_key = NULL;
3367 dl_list_init(&participant->rxsc_list);
3368 participant->txsc = ieee802_1x_kay_init_transmit_sc(&kay->actor_sci,
3370 secy_cp_control_protect_frames(kay, kay->macsec_protect);
3371 secy_cp_control_replay(kay, kay->macsec_replay_protect,
3372 kay->macsec_replay_window);
3373 secy_create_transmit_sc(kay, participant->txsc);
3375 /* to derive KEK from CAK and CKN */
3376 participant->kek.len = mka_alg_tbl[kay->mka_algindex].kek_len;
3377 if (mka_alg_tbl[kay->mka_algindex].kek_trfm(participant->cak.key,
3378 participant->ckn.name,
3379 participant->ckn.len,
3380 participant->kek.key)) {
3381 wpa_printf(MSG_ERROR, "KaY: Derived KEK failed");
3384 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived KEK",
3385 participant->kek.key, participant->kek.len);
3387 /* to derive ICK from CAK and CKN */
3388 participant->ick.len = mka_alg_tbl[kay->mka_algindex].ick_len;
3389 if (mka_alg_tbl[kay->mka_algindex].ick_trfm(participant->cak.key,
3390 participant->ckn.name,
3391 participant->ckn.len,
3392 participant->ick.key)) {
3393 wpa_printf(MSG_ERROR, "KaY: Derived ICK failed");
3396 wpa_hexdump_key(MSG_DEBUG, "KaY: Derived ICK",
3397 participant->ick.key, participant->ick.len);
3399 dl_list_add(&kay->participant_list, &participant->list);
3400 wpa_hexdump(MSG_DEBUG, "KaY: Participant created:",
3401 ckn->name, ckn->len);
3403 usecs = os_random() % (MKA_HELLO_TIME * 1000);
3404 eloop_register_timeout(0, usecs, ieee802_1x_participant_timer,
3406 participant->mka_life = MKA_LIFE_TIME / 1000 + time(NULL) +
3412 os_free(participant);
3418 * ieee802_1x_kay_delete_mka -
3421 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay *kay, struct mka_key_name *ckn)
3423 struct ieee802_1x_mka_participant *participant;
3424 struct ieee802_1x_kay_peer *peer;
3425 struct data_key *sak;
3426 struct receive_sc *rxsc;
3431 wpa_printf(MSG_DEBUG, "KaY: participant removed");
3433 /* get the participant */
3434 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3436 wpa_hexdump(MSG_DEBUG, "KaY: participant is not found",
3437 ckn->name, ckn->len);
3441 eloop_cancel_timeout(ieee802_1x_participant_timer, participant, NULL);
3442 dl_list_del(&participant->list);
3444 /* remove live peer */
3445 while (!dl_list_empty(&participant->live_peers)) {
3446 peer = dl_list_entry(participant->live_peers.next,
3447 struct ieee802_1x_kay_peer, list);
3448 dl_list_del(&peer->list);
3452 /* remove potential peer */
3453 while (!dl_list_empty(&participant->potential_peers)) {
3454 peer = dl_list_entry(participant->potential_peers.next,
3455 struct ieee802_1x_kay_peer, list);
3456 dl_list_del(&peer->list);
3461 while (!dl_list_empty(&participant->sak_list)) {
3462 sak = dl_list_entry(participant->sak_list.next,
3463 struct data_key, list);
3464 dl_list_del(&sak->list);
3468 while (!dl_list_empty(&participant->rxsc_list)) {
3469 rxsc = dl_list_entry(participant->rxsc_list.next,
3470 struct receive_sc, list);
3471 secy_delete_receive_sc(kay, rxsc);
3472 ieee802_1x_kay_deinit_receive_sc(participant, rxsc);
3474 secy_delete_transmit_sc(kay, participant->txsc);
3475 ieee802_1x_kay_deinit_transmit_sc(participant, participant->txsc);
3477 os_memset(&participant->cak, 0, sizeof(participant->cak));
3478 os_memset(&participant->kek, 0, sizeof(participant->kek));
3479 os_memset(&participant->ick, 0, sizeof(participant->ick));
3480 os_free(participant);
3485 * ieee802_1x_kay_mka_participate -
3487 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay *kay,
3488 struct mka_key_name *ckn,
3491 struct ieee802_1x_mka_participant *participant;
3496 participant = ieee802_1x_kay_get_participant(kay, ckn->name);
3500 participant->active = status;
3505 * ieee802_1x_kay_new_sak -
3508 ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
3510 struct ieee802_1x_mka_participant *participant;
3515 participant = ieee802_1x_kay_get_principal_participant(kay);
3519 participant->new_sak = TRUE;
3520 wpa_printf(MSG_DEBUG, "KaY: new SAK signal");
3527 * ieee802_1x_kay_change_cipher_suite -
3530 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
3532 struct ieee802_1x_mka_participant *participant;
3537 if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
3538 wpa_printf(MSG_ERROR,
3539 "KaY: Configured cipher suite index is out of range");
3542 if (kay->macsec_csindex == cs_index)
3546 kay->macsec_desired = FALSE;
3548 kay->macsec_csindex = cs_index;
3549 kay->macsec_capable = cipher_suite_tbl[kay->macsec_csindex].capable;
3551 participant = ieee802_1x_kay_get_principal_participant(kay);
3553 wpa_printf(MSG_INFO, "KaY: Cipher Suite changed");
3554 participant->new_sak = TRUE;