2 * IEEE 802.1X-2010 Key Agreement 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 MAX_MISSING_SAK_USE 10 /* Accept up to 10 inbound MKPDUs without
31 * SAK-USE before dropping */
33 #define PENDING_PN_EXHAUSTION 0xC0000000
35 #define MKA_ALIGN_LENGTH(len) (((len) + 0x3) & ~0x3)
37 /* IEEE Std 802.1X-2010, Table 9-1 - MKA Algorithm Agility */
38 #define MKA_ALGO_AGILITY_2009 { 0x00, 0x80, 0xC2, 0x01 }
39 static u8 mka_algo_agility
[4] = MKA_ALGO_AGILITY_2009
;
41 /* IEEE802.1AE-2006 Table 14-1 MACsec Cipher Suites */
42 static struct macsec_ciphersuite cipher_suite_tbl
[] = {
45 .id
= CS_ID_GCM_AES_128
,
46 .name
= CS_NAME_GCM_AES_128
,
47 .capable
= MACSEC_CAP_INTEG_AND_CONF_0_30_50
,
48 .sak_len
= DEFAULT_SA_KEY_LEN
,
52 .id
= CS_ID_GCM_AES_256
,
53 .name
= CS_NAME_GCM_AES_256
,
54 .capable
= MACSEC_CAP_INTEG_AND_CONF_0_30_50
,
58 #define CS_TABLE_SIZE (ARRAY_SIZE(cipher_suite_tbl))
59 #define DEFAULT_CS_INDEX 0
61 static struct mka_alg mka_alg_tbl
[] = {
63 .parameter
= MKA_ALGO_AGILITY_2009
,
65 .icv_len
= DEFAULT_ICV_LEN
,
67 .cak_trfm
= ieee802_1x_cak_aes_cmac
,
68 .ckn_trfm
= ieee802_1x_ckn_aes_cmac
,
69 .kek_trfm
= ieee802_1x_kek_aes_cmac
,
70 .ick_trfm
= ieee802_1x_ick_aes_cmac
,
71 .icv_hash
= ieee802_1x_icv_aes_cmac
,
74 #define MKA_ALG_TABLE_SIZE (ARRAY_SIZE(mka_alg_tbl))
77 static int is_ki_equal(struct ieee802_1x_mka_ki
*ki1
,
78 struct ieee802_1x_mka_ki
*ki2
)
80 return os_memcmp(ki1
->mi
, ki2
->mi
, MI_LEN
) == 0 &&
85 static void set_mka_param_body_len(void *body
, unsigned int len
)
87 struct ieee802_1x_mka_hdr
*hdr
= body
;
88 hdr
->length
= (len
>> 8) & 0x0f;
89 hdr
->length1
= len
& 0xff;
93 static unsigned int get_mka_param_body_len(const void *body
)
95 const struct ieee802_1x_mka_hdr
*hdr
= body
;
96 return (hdr
->length
<< 8) | hdr
->length1
;
100 static u8
get_mka_param_body_type(const void *body
)
102 const struct ieee802_1x_mka_hdr
*hdr
= body
;
107 static const char * mi_txt(const u8
*mi
)
109 static char txt
[MI_LEN
* 2 + 1];
111 wpa_snprintf_hex(txt
, sizeof(txt
), mi
, MI_LEN
);
116 static const char * sci_txt(const struct ieee802_1x_mka_sci
*sci
)
118 static char txt
[ETH_ALEN
* 3 + 1 + 5 + 1];
120 os_snprintf(txt
, sizeof(txt
), MACSTR
"@%u",
121 MAC2STR(sci
->addr
), be_to_host16(sci
->port
));
126 static const char * algo_agility_txt(const u8
*algo_agility
)
128 static char txt
[4 * 2 + 1];
130 wpa_snprintf_hex(txt
, sizeof(txt
), algo_agility
, 4);
136 * ieee802_1x_mka_dump_basic_body -
139 ieee802_1x_mka_dump_basic_body(struct ieee802_1x_mka_basic_body
*body
)
146 /* IEEE Std 802.1X-2010, Figure 11-8 */
147 body_len
= get_mka_param_body_len(body
);
148 wpa_printf(MSG_DEBUG
, "MKA Basic Parameter Set");
149 wpa_printf(MSG_DEBUG
, "\tMKA Version Identifier: %d", body
->version
);
150 wpa_printf(MSG_DEBUG
, "\tKey Server Priority: %d", body
->priority
);
151 wpa_printf(MSG_DEBUG
, "\tKey Server: %d", body
->key_server
);
152 wpa_printf(MSG_DEBUG
, "\tMACsec Desired: %d", body
->macsec_desired
);
153 wpa_printf(MSG_DEBUG
, "\tMACsec Capability: %d",
154 body
->macsec_capability
);
155 wpa_printf(MSG_DEBUG
, "\tParameter set body length: %zu", body_len
);
156 wpa_printf(MSG_DEBUG
, "\tSCI: %s", sci_txt(&body
->actor_sci
));
157 wpa_printf(MSG_DEBUG
, "\tActor's Member Identifier: %s",
158 mi_txt(body
->actor_mi
));
159 wpa_printf(MSG_DEBUG
, "\tActor's Message Number: %d",
160 be_to_host32(body
->actor_mn
));
161 wpa_printf(MSG_DEBUG
, "\tAlgorithm Agility: %s",
162 algo_agility_txt(body
->algo_agility
));
163 wpa_hexdump(MSG_DEBUG
, "\tCAK Name", body
->ckn
,
164 body_len
+ MKA_HDR_LEN
- sizeof(*body
));
169 * ieee802_1x_mka_dump_peer_body -
172 ieee802_1x_mka_dump_peer_body(struct ieee802_1x_mka_peer_body
*body
)
182 /* IEEE Std 802.1X-2010, Figure 11-9 */
183 body_len
= get_mka_param_body_len(body
);
184 if (body
->type
== MKA_LIVE_PEER_LIST
) {
185 wpa_printf(MSG_DEBUG
, "Live Peer List parameter set");
186 wpa_printf(MSG_DEBUG
, "\tBody Length: %zu", body_len
);
187 } else if (body
->type
== MKA_POTENTIAL_PEER_LIST
) {
188 wpa_printf(MSG_DEBUG
, "Potential Peer List parameter set");
189 wpa_printf(MSG_DEBUG
, "\tBody Length: %zu", body_len
);
192 for (i
= 0; i
< body_len
; i
+= MI_LEN
+ sizeof(mn
)) {
194 os_memcpy(&mn
, mi
+ MI_LEN
, sizeof(mn
));
195 wpa_printf(MSG_DEBUG
, "\tMember Id: %s Message Number: %d",
196 mi_txt(mi
), be_to_host32(mn
));
202 * ieee802_1x_mka_dump_dist_sak_body -
205 ieee802_1x_mka_dump_dist_sak_body(struct ieee802_1x_mka_dist_sak_body
*body
)
212 /* IEEE Std 802.1X-2010, Figure 11-11 and 11-12 */
213 body_len
= get_mka_param_body_len(body
);
214 wpa_printf(MSG_DEBUG
, "Distributed SAK parameter set");
215 wpa_printf(MSG_DEBUG
, "\tDistributed AN........: %d", body
->dan
);
216 wpa_printf(MSG_DEBUG
, "\tConfidentiality Offset: %d",
217 body
->confid_offset
);
218 wpa_printf(MSG_DEBUG
, "\tBody Length...........: %zu", body_len
);
222 wpa_printf(MSG_DEBUG
, "\tKey Number............: %d",
223 be_to_host32(body
->kn
));
224 /* TODO: Other than GCM-AES-128 case: MACsec Cipher Suite */
225 wpa_hexdump(MSG_DEBUG
, "\tAES Key Wrap of SAK...:", body
->sak
, 24);
229 static const char * yes_no(int val
)
231 return val
? "Yes" : "No";
236 * ieee802_1x_mka_dump_sak_use_body -
239 ieee802_1x_mka_dump_sak_use_body(struct ieee802_1x_mka_sak_use_body
*body
)
246 /* IEEE Std 802.1X-2010, Figure 11-10 */
247 body_len
= get_mka_param_body_len(body
);
248 wpa_printf(MSG_DEBUG
, "MACsec SAK Use parameter set");
249 wpa_printf(MSG_DEBUG
, "\tLatest Key AN....: %d", body
->lan
);
250 wpa_printf(MSG_DEBUG
, "\tLatest Key Tx....: %s", yes_no(body
->ltx
));
251 wpa_printf(MSG_DEBUG
, "\tLatest Key Rx....: %s", yes_no(body
->lrx
));
252 wpa_printf(MSG_DEBUG
, "\tOld Key AN.......: %d", body
->oan
);
253 wpa_printf(MSG_DEBUG
, "\tOld Key Tx.......: %s", yes_no(body
->otx
));
254 wpa_printf(MSG_DEBUG
, "\tOld Key Rx.......: %s", yes_no(body
->orx
));
255 wpa_printf(MSG_DEBUG
, "\tPlain Tx.........: %s", yes_no(body
->ptx
));
256 wpa_printf(MSG_DEBUG
, "\tPlain Rx.........: %s", yes_no(body
->prx
));
257 wpa_printf(MSG_DEBUG
, "\tDelay Protect....: %s",
258 yes_no(body
->delay_protect
));
259 wpa_printf(MSG_DEBUG
, "\tBody Length......: %d", body_len
);
263 wpa_printf(MSG_DEBUG
, "\tKey Server MI....: %s", mi_txt(body
->lsrv_mi
));
264 wpa_printf(MSG_DEBUG
, "\tKey Number.......: %u",
265 be_to_host32(body
->lkn
));
266 wpa_printf(MSG_DEBUG
, "\tLowest PN........: %u",
267 be_to_host32(body
->llpn
));
268 wpa_printf(MSG_DEBUG
, "\tOld Key Server MI: %s", mi_txt(body
->osrv_mi
));
269 wpa_printf(MSG_DEBUG
, "\tOld Key Number...: %u",
270 be_to_host32(body
->okn
));
271 wpa_printf(MSG_DEBUG
, "\tOld Lowest PN....: %u",
272 be_to_host32(body
->olpn
));
277 * ieee802_1x_kay_get_participant -
279 static struct ieee802_1x_mka_participant
*
280 ieee802_1x_kay_get_participant(struct ieee802_1x_kay
*kay
, const u8
*ckn
,
283 struct ieee802_1x_mka_participant
*participant
;
285 dl_list_for_each(participant
, &kay
->participant_list
,
286 struct ieee802_1x_mka_participant
, list
) {
287 if (participant
->ckn
.len
== len
&&
288 os_memcmp(participant
->ckn
.name
, ckn
,
289 participant
->ckn
.len
) == 0)
293 wpa_printf(MSG_DEBUG
, "KaY: participant is not found");
300 * ieee802_1x_kay_get_principal_participant -
302 static struct ieee802_1x_mka_participant
*
303 ieee802_1x_kay_get_principal_participant(struct ieee802_1x_kay
*kay
)
305 struct ieee802_1x_mka_participant
*participant
;
307 dl_list_for_each(participant
, &kay
->participant_list
,
308 struct ieee802_1x_mka_participant
, list
) {
309 if (participant
->principal
)
313 wpa_printf(MSG_DEBUG
, "KaY: principal participant is not found");
318 static struct ieee802_1x_kay_peer
* get_peer_mi(struct dl_list
*peers
,
321 struct ieee802_1x_kay_peer
*peer
;
323 dl_list_for_each(peer
, peers
, struct ieee802_1x_kay_peer
, list
) {
324 if (os_memcmp(peer
->mi
, mi
, MI_LEN
) == 0)
333 * ieee802_1x_kay_get_potential_peer
335 static struct ieee802_1x_kay_peer
*
336 ieee802_1x_kay_get_potential_peer(
337 struct ieee802_1x_mka_participant
*participant
, const u8
*mi
)
339 return get_peer_mi(&participant
->potential_peers
, mi
);
344 * ieee802_1x_kay_get_live_peer
346 static struct ieee802_1x_kay_peer
*
347 ieee802_1x_kay_get_live_peer(struct ieee802_1x_mka_participant
*participant
,
350 return get_peer_mi(&participant
->live_peers
, mi
);
355 * ieee802_1x_kay_is_in_potential_peer
358 ieee802_1x_kay_is_in_potential_peer(
359 struct ieee802_1x_mka_participant
*participant
, const u8
*mi
)
361 return ieee802_1x_kay_get_potential_peer(participant
, mi
) != NULL
;
366 * ieee802_1x_kay_is_in_live_peer
369 ieee802_1x_kay_is_in_live_peer(
370 struct ieee802_1x_mka_participant
*participant
, const u8
*mi
)
372 return ieee802_1x_kay_get_live_peer(participant
, mi
) != NULL
;
377 * ieee802_1x_kay_get_peer
379 static struct ieee802_1x_kay_peer
*
380 ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant
*participant
,
383 struct ieee802_1x_kay_peer
*peer
;
385 peer
= ieee802_1x_kay_get_live_peer(participant
, mi
);
389 return ieee802_1x_kay_get_potential_peer(participant
, mi
);
394 * ieee802_1x_kay_get_cipher_suite
396 static struct macsec_ciphersuite
*
397 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant
*participant
,
398 const u8
*cs_id
, unsigned int *idx
)
404 os_memcpy(&_cs
, cs_id
, CS_ID_LEN
);
405 cs
= be_to_host64(_cs
);
407 for (i
= 0; i
< CS_TABLE_SIZE
; i
++) {
408 if (cipher_suite_tbl
[i
].id
== cs
) {
410 return &cipher_suite_tbl
[i
];
418 u64
mka_sci_u64(struct ieee802_1x_mka_sci
*sci
)
420 struct ieee802_1x_mka_sci tmp
;
422 os_memcpy(tmp
.addr
, sci
->addr
, ETH_ALEN
);
423 tmp
.port
= sci
->port
;
425 return *((u64
*) &tmp
);
429 static Boolean
sci_equal(const struct ieee802_1x_mka_sci
*a
,
430 const struct ieee802_1x_mka_sci
*b
)
432 return os_memcmp(a
, b
, sizeof(struct ieee802_1x_mka_sci
)) == 0;
437 * ieee802_1x_kay_get_peer_sci
439 static struct ieee802_1x_kay_peer
*
440 ieee802_1x_kay_get_peer_sci(struct ieee802_1x_mka_participant
*participant
,
441 const struct ieee802_1x_mka_sci
*sci
)
443 struct ieee802_1x_kay_peer
*peer
;
445 dl_list_for_each(peer
, &participant
->live_peers
,
446 struct ieee802_1x_kay_peer
, list
) {
447 if (sci_equal(&peer
->sci
, sci
))
451 dl_list_for_each(peer
, &participant
->potential_peers
,
452 struct ieee802_1x_kay_peer
, list
) {
453 if (sci_equal(&peer
->sci
, sci
))
461 static void ieee802_1x_kay_use_data_key(struct data_key
*pkey
);
464 * ieee802_1x_kay_init_receive_sa -
466 static struct receive_sa
*
467 ieee802_1x_kay_init_receive_sa(struct receive_sc
*psc
, u8 an
, u32 lowest_pn
,
468 struct data_key
*key
)
470 struct receive_sa
*psa
;
475 psa
= os_zalloc(sizeof(*psa
));
477 wpa_printf(MSG_ERROR
, "%s: out of memory", __func__
);
481 ieee802_1x_kay_use_data_key(key
);
483 psa
->lowest_pn
= lowest_pn
;
484 psa
->next_pn
= lowest_pn
;
488 os_get_time(&psa
->created_time
);
491 dl_list_add(&psc
->sa_list
, &psa
->list
);
492 wpa_printf(MSG_DEBUG
,
493 "KaY: Create receive SA(an: %hhu lowest_pn: %u) of SC",
500 static void ieee802_1x_kay_deinit_data_key(struct data_key
*pkey
);
503 * ieee802_1x_kay_deinit_receive_sa -
505 static void ieee802_1x_kay_deinit_receive_sa(struct receive_sa
*psa
)
507 ieee802_1x_kay_deinit_data_key(psa
->pkey
);
509 wpa_printf(MSG_DEBUG
,
510 "KaY: Delete receive SA(an: %hhu) of SC",
512 dl_list_del(&psa
->list
);
518 * ieee802_1x_kay_init_receive_sc -
520 static struct receive_sc
*
521 ieee802_1x_kay_init_receive_sc(const struct ieee802_1x_mka_sci
*psci
)
523 struct receive_sc
*psc
;
528 psc
= os_zalloc(sizeof(*psc
));
530 wpa_printf(MSG_ERROR
, "%s: out of memory", __func__
);
534 os_memcpy(&psc
->sci
, psci
, sizeof(psc
->sci
));
536 os_get_time(&psc
->created_time
);
537 psc
->receiving
= FALSE
;
539 dl_list_init(&psc
->sa_list
);
540 wpa_printf(MSG_DEBUG
, "KaY: Create receive SC: SCI %s",
547 static void ieee802_1x_delete_receive_sa(struct ieee802_1x_kay
*kay
,
548 struct receive_sa
*sa
)
550 secy_disable_receive_sa(kay
, sa
);
551 secy_delete_receive_sa(kay
, sa
);
552 ieee802_1x_kay_deinit_receive_sa(sa
);
557 * ieee802_1x_kay_deinit_receive_sc -
560 ieee802_1x_kay_deinit_receive_sc(
561 struct ieee802_1x_mka_participant
*participant
, struct receive_sc
*psc
)
563 struct receive_sa
*psa
, *pre_sa
;
565 wpa_printf(MSG_DEBUG
, "KaY: Delete receive SC");
566 dl_list_for_each_safe(psa
, pre_sa
, &psc
->sa_list
, struct receive_sa
,
568 ieee802_1x_delete_receive_sa(participant
->kay
, psa
);
570 dl_list_del(&psc
->list
);
571 secy_delete_receive_sc(participant
->kay
, psc
);
576 static void ieee802_1x_kay_dump_peer(struct ieee802_1x_kay_peer
*peer
)
578 wpa_printf(MSG_DEBUG
, "\tMI: %s MN: %d SCI: %s",
579 mi_txt(peer
->mi
), peer
->mn
, sci_txt(&peer
->sci
));
583 static struct ieee802_1x_kay_peer
*
584 ieee802_1x_kay_create_peer(const u8
*mi
, u32 mn
)
586 struct ieee802_1x_kay_peer
*peer
;
588 peer
= os_zalloc(sizeof(*peer
));
590 wpa_printf(MSG_ERROR
, "KaY-%s: out of memory", __func__
);
594 os_memcpy(peer
->mi
, mi
, MI_LEN
);
596 peer
->expire
= time(NULL
) + MKA_LIFE_TIME
/ 1000;
597 peer
->sak_used
= FALSE
;
598 peer
->missing_sak_use_count
= 0;
605 * ieee802_1x_kay_create_live_peer
607 static struct ieee802_1x_kay_peer
*
608 ieee802_1x_kay_create_live_peer(struct ieee802_1x_mka_participant
*participant
,
609 const u8
*mi
, u32 mn
)
611 struct ieee802_1x_kay_peer
*peer
;
612 struct receive_sc
*rxsc
;
614 peer
= ieee802_1x_kay_create_peer(mi
, mn
);
618 os_memcpy(&peer
->sci
, &participant
->current_peer_sci
,
621 rxsc
= ieee802_1x_kay_init_receive_sc(&peer
->sci
);
627 if (secy_create_receive_sc(participant
->kay
, rxsc
)) {
632 dl_list_add(&participant
->live_peers
, &peer
->list
);
633 dl_list_add(&participant
->rxsc_list
, &rxsc
->list
);
635 wpa_printf(MSG_DEBUG
, "KaY: Live peer created");
636 ieee802_1x_kay_dump_peer(peer
);
643 * ieee802_1x_kay_create_potential_peer
645 static struct ieee802_1x_kay_peer
*
646 ieee802_1x_kay_create_potential_peer(
647 struct ieee802_1x_mka_participant
*participant
, const u8
*mi
, u32 mn
)
649 struct ieee802_1x_kay_peer
*peer
;
651 peer
= ieee802_1x_kay_create_peer(mi
, mn
);
655 dl_list_add(&participant
->potential_peers
, &peer
->list
);
657 wpa_printf(MSG_DEBUG
, "KaY: Potential peer created");
658 ieee802_1x_kay_dump_peer(peer
);
665 * ieee802_1x_kay_move_live_peer
667 static struct ieee802_1x_kay_peer
*
668 ieee802_1x_kay_move_live_peer(struct ieee802_1x_mka_participant
*participant
,
671 struct ieee802_1x_kay_peer
*peer
;
672 struct receive_sc
*rxsc
;
674 peer
= ieee802_1x_kay_get_potential_peer(participant
, mi
);
678 rxsc
= ieee802_1x_kay_init_receive_sc(&participant
->current_peer_sci
);
682 os_memcpy(&peer
->sci
, &participant
->current_peer_sci
,
685 peer
->expire
= time(NULL
) + MKA_LIFE_TIME
/ 1000;
687 wpa_printf(MSG_DEBUG
, "KaY: Move potential peer to live peer");
688 ieee802_1x_kay_dump_peer(peer
);
690 dl_list_del(&peer
->list
);
691 if (secy_create_receive_sc(participant
->kay
, rxsc
)) {
692 wpa_printf(MSG_ERROR
, "KaY: Can't create SC, discard peer");
697 dl_list_add_tail(&participant
->live_peers
, &peer
->list
);
699 dl_list_add(&participant
->rxsc_list
, &rxsc
->list
);
707 * ieee802_1x_mka_basic_body_present -
710 ieee802_1x_mka_basic_body_present(
711 struct ieee802_1x_mka_participant
*participant
)
718 * ieee802_1x_mka_basic_body_length -
721 ieee802_1x_mka_basic_body_length(struct ieee802_1x_mka_participant
*participant
)
725 length
= sizeof(struct ieee802_1x_mka_basic_body
);
726 length
+= participant
->ckn
.len
;
727 return MKA_ALIGN_LENGTH(length
);
732 * ieee802_1x_mka_encode_basic_body
735 ieee802_1x_mka_encode_basic_body(
736 struct ieee802_1x_mka_participant
*participant
,
739 struct ieee802_1x_mka_basic_body
*body
;
740 struct ieee802_1x_kay
*kay
= participant
->kay
;
741 unsigned int length
= sizeof(struct ieee802_1x_mka_basic_body
);
743 length
+= participant
->ckn
.len
;
744 body
= wpabuf_put(buf
, MKA_ALIGN_LENGTH(length
));
746 body
->version
= kay
->mka_version
;
747 body
->priority
= kay
->actor_priority
;
748 /* The Key Server flag is set if and only if the participant has not
749 * decided that another participant is or will be the Key Server. */
750 if (participant
->is_elected
)
751 body
->key_server
= participant
->is_key_server
;
753 body
->key_server
= participant
->can_be_key_server
;
755 body
->macsec_desired
= kay
->macsec_desired
;
756 body
->macsec_capability
= kay
->macsec_capable
;
757 set_mka_param_body_len(body
, length
- MKA_HDR_LEN
);
759 os_memcpy(body
->actor_sci
.addr
, kay
->actor_sci
.addr
,
760 sizeof(kay
->actor_sci
.addr
));
761 body
->actor_sci
.port
= kay
->actor_sci
.port
;
763 os_memcpy(body
->actor_mi
, participant
->mi
, sizeof(body
->actor_mi
));
764 participant
->mn
= participant
->mn
+ 1;
765 body
->actor_mn
= host_to_be32(participant
->mn
);
766 os_memcpy(body
->algo_agility
, kay
->algo_agility
,
767 sizeof(body
->algo_agility
));
769 os_memcpy(body
->ckn
, participant
->ckn
.name
, participant
->ckn
.len
);
771 ieee802_1x_mka_dump_basic_body(body
);
778 reset_participant_mi(struct ieee802_1x_mka_participant
*participant
)
780 if (os_get_random(participant
->mi
, sizeof(participant
->mi
)) < 0)
789 * ieee802_1x_mka_decode_basic_body -
791 static struct ieee802_1x_mka_participant
*
792 ieee802_1x_mka_decode_basic_body(struct ieee802_1x_kay
*kay
, const u8
*mka_msg
,
795 struct ieee802_1x_mka_participant
*participant
;
796 const struct ieee802_1x_mka_basic_body
*body
;
797 struct ieee802_1x_kay_peer
*peer
;
801 body
= (const struct ieee802_1x_mka_basic_body
*) mka_msg
;
803 if (body
->version
> MKA_VERSION_ID
) {
804 wpa_printf(MSG_DEBUG
,
805 "KaY: Peer's version(%d) greater than MKA current version(%d)",
806 body
->version
, MKA_VERSION_ID
);
808 if (kay
->is_obliged_key_server
&& body
->key_server
) {
809 wpa_printf(MSG_DEBUG
, "KaY: I must be key server - ignore MKPDU claiming to be from a key server");
813 body_len
= get_mka_param_body_len(body
);
814 if (body_len
< sizeof(struct ieee802_1x_mka_basic_body
) - MKA_HDR_LEN
) {
815 wpa_printf(MSG_DEBUG
, "KaY: Too small body length %zu",
820 (sizeof(struct ieee802_1x_mka_basic_body
) - MKA_HDR_LEN
);
821 participant
= ieee802_1x_kay_get_participant(kay
, body
->ckn
, ckn_len
);
823 wpa_printf(MSG_DEBUG
,
824 "KaY: Peer is not included in my CA - ignore MKPDU");
828 /* If the peer's MI is my MI, I will choose new MI */
829 if (os_memcmp(body
->actor_mi
, participant
->mi
, MI_LEN
) == 0) {
830 if (!reset_participant_mi(participant
))
832 wpa_printf(MSG_DEBUG
,
833 "KaY: Peer using my MI - selected a new random MI: %s",
834 mi_txt(participant
->mi
));
837 os_memcpy(participant
->current_peer_id
.mi
, body
->actor_mi
, MI_LEN
);
838 participant
->current_peer_id
.mn
= body
->actor_mn
;
839 os_memcpy(participant
->current_peer_sci
.addr
, body
->actor_sci
.addr
,
840 sizeof(participant
->current_peer_sci
.addr
));
841 participant
->current_peer_sci
.port
= body
->actor_sci
.port
;
844 peer
= ieee802_1x_kay_get_peer(participant
, body
->actor_mi
);
846 /* Check duplicated SCI
848 * A duplicated SCI indicates either an active attacker or
849 * a valid peer whose MI is being changed. The latter scenario
850 * is more likely because to have gotten this far the received
851 * MKPDU must have had a valid ICV, indicating the peer holds
852 * the same CAK as our participant.
854 * Before creating a new peer object for the new MI we must
855 * clean up the resources (SCs and SAs) associated with the
856 * old peer. An easy way to do this is to ignore MKPDUs with
857 * the new MI's for now and just wait for the old peer to
858 * time out and clean itself up (within MKA_LIFE_TIME).
860 * This method is preferable to deleting the old peer here
861 * and now and continuing on with processing because if this
862 * MKPDU is from an attacker it's better to ignore the MKPDU
863 * than to process it (and delete a valid peer as well).
865 peer
= ieee802_1x_kay_get_peer_sci(participant
,
870 wpa_printf(MSG_WARNING
,
871 "KaY: duplicated SCI detected - maybe active attacker or peer selected new MI - ignore MKPDU");
872 /* Reduce timeout to speed up this process but left the
873 * chance for old one to prove aliveness. */
874 new_expire
= time(NULL
) + MKA_HELLO_TIME
* 1.5 / 1000;
875 if (peer
->expire
> new_expire
)
876 peer
->expire
= new_expire
;
880 peer
= ieee802_1x_kay_create_potential_peer(
881 participant
, body
->actor_mi
,
882 be_to_host32(body
->actor_mn
));
884 wpa_printf(MSG_DEBUG
,
885 "KaY: No potential peer entry found - ignore MKPDU");
889 peer
->macsec_desired
= body
->macsec_desired
;
890 peer
->macsec_capability
= body
->macsec_capability
;
891 peer
->is_key_server
= (Boolean
) body
->key_server
;
892 peer
->key_server_priority
= body
->priority
;
893 } else if (peer
->mn
< be_to_host32(body
->actor_mn
)) {
894 peer
->mn
= be_to_host32(body
->actor_mn
);
895 peer
->macsec_desired
= body
->macsec_desired
;
896 peer
->macsec_capability
= body
->macsec_capability
;
897 peer
->is_key_server
= (Boolean
) body
->key_server
;
898 peer
->key_server_priority
= body
->priority
;
900 wpa_printf(MSG_WARNING
,
901 "KaY: The peer MN did not increase - ignore MKPDU");
910 * ieee802_1x_mka_live_peer_body_present
913 ieee802_1x_mka_live_peer_body_present(
914 struct ieee802_1x_mka_participant
*participant
)
916 return !dl_list_empty(&participant
->live_peers
);
921 * ieee802_1x_kay_get_live_peer_length
924 ieee802_1x_mka_get_live_peer_length(
925 struct ieee802_1x_mka_participant
*participant
)
927 int len
= MKA_HDR_LEN
;
928 struct ieee802_1x_kay_peer
*peer
;
930 dl_list_for_each(peer
, &participant
->live_peers
,
931 struct ieee802_1x_kay_peer
, list
)
932 len
+= sizeof(struct ieee802_1x_mka_peer_id
);
934 return MKA_ALIGN_LENGTH(len
);
939 * ieee802_1x_mka_encode_live_peer_body -
942 ieee802_1x_mka_encode_live_peer_body(
943 struct ieee802_1x_mka_participant
*participant
,
946 struct ieee802_1x_mka_peer_body
*body
;
947 struct ieee802_1x_kay_peer
*peer
;
949 struct ieee802_1x_mka_peer_id
*body_peer
;
951 length
= ieee802_1x_mka_get_live_peer_length(participant
);
952 body
= wpabuf_put(buf
, sizeof(struct ieee802_1x_mka_peer_body
));
954 body
->type
= MKA_LIVE_PEER_LIST
;
955 set_mka_param_body_len(body
, length
- MKA_HDR_LEN
);
957 dl_list_for_each(peer
, &participant
->live_peers
,
958 struct ieee802_1x_kay_peer
, list
) {
959 body_peer
= wpabuf_put(buf
,
960 sizeof(struct ieee802_1x_mka_peer_id
));
961 os_memcpy(body_peer
->mi
, peer
->mi
, MI_LEN
);
962 body_peer
->mn
= host_to_be32(peer
->mn
);
965 ieee802_1x_mka_dump_peer_body(body
);
970 * ieee802_1x_mka_potential_peer_body_present
973 ieee802_1x_mka_potential_peer_body_present(
974 struct ieee802_1x_mka_participant
*participant
)
976 return !dl_list_empty(&participant
->potential_peers
);
981 * ieee802_1x_kay_get_potential_peer_length
984 ieee802_1x_mka_get_potential_peer_length(
985 struct ieee802_1x_mka_participant
*participant
)
987 int len
= MKA_HDR_LEN
;
988 struct ieee802_1x_kay_peer
*peer
;
990 dl_list_for_each(peer
, &participant
->potential_peers
,
991 struct ieee802_1x_kay_peer
, list
)
992 len
+= sizeof(struct ieee802_1x_mka_peer_id
);
994 return MKA_ALIGN_LENGTH(len
);
999 * ieee802_1x_mka_encode_potential_peer_body -
1002 ieee802_1x_mka_encode_potential_peer_body(
1003 struct ieee802_1x_mka_participant
*participant
,
1006 struct ieee802_1x_mka_peer_body
*body
;
1007 struct ieee802_1x_kay_peer
*peer
;
1008 unsigned int length
;
1009 struct ieee802_1x_mka_peer_id
*body_peer
;
1011 length
= ieee802_1x_mka_get_potential_peer_length(participant
);
1012 body
= wpabuf_put(buf
, sizeof(struct ieee802_1x_mka_peer_body
));
1014 body
->type
= MKA_POTENTIAL_PEER_LIST
;
1015 set_mka_param_body_len(body
, length
- MKA_HDR_LEN
);
1017 dl_list_for_each(peer
, &participant
->potential_peers
,
1018 struct ieee802_1x_kay_peer
, list
) {
1019 body_peer
= wpabuf_put(buf
,
1020 sizeof(struct ieee802_1x_mka_peer_id
));
1021 os_memcpy(body_peer
->mi
, peer
->mi
, MI_LEN
);
1022 body_peer
->mn
= host_to_be32(peer
->mn
);
1025 ieee802_1x_mka_dump_peer_body(body
);
1031 * ieee802_1x_mka_i_in_peerlist -
1034 ieee802_1x_mka_i_in_peerlist(struct ieee802_1x_mka_participant
*participant
,
1035 const u8
*mka_msg
, size_t msg_len
)
1037 struct ieee802_1x_mka_hdr
*hdr
;
1044 for (pos
= mka_msg
, left_len
= msg_len
;
1045 left_len
> MKA_HDR_LEN
+ DEFAULT_ICV_LEN
;
1046 left_len
-= MKA_ALIGN_LENGTH(body_len
) + MKA_HDR_LEN
,
1047 pos
+= MKA_ALIGN_LENGTH(body_len
) + MKA_HDR_LEN
) {
1048 hdr
= (struct ieee802_1x_mka_hdr
*) pos
;
1049 body_len
= get_mka_param_body_len(hdr
);
1050 body_type
= get_mka_param_body_type(hdr
);
1052 if (left_len
< (MKA_HDR_LEN
+ MKA_ALIGN_LENGTH(body_len
) + DEFAULT_ICV_LEN
)) {
1053 wpa_printf(MSG_ERROR
,
1054 "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",
1055 left_len
, MKA_HDR_LEN
,
1056 MKA_ALIGN_LENGTH(body_len
),
1061 if (body_type
!= MKA_LIVE_PEER_LIST
&&
1062 body_type
!= MKA_POTENTIAL_PEER_LIST
)
1065 if ((body_len
% 16) != 0) {
1066 wpa_printf(MSG_ERROR
,
1067 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1072 ieee802_1x_mka_dump_peer_body(
1073 (struct ieee802_1x_mka_peer_body
*)pos
);
1075 for (i
= 0; i
< body_len
;
1076 i
+= sizeof(struct ieee802_1x_mka_peer_id
)) {
1077 const struct ieee802_1x_mka_peer_id
*peer_mi
;
1079 peer_mi
= (const struct ieee802_1x_mka_peer_id
*)
1080 (pos
+ MKA_HDR_LEN
+ i
);
1081 if (os_memcmp(peer_mi
->mi
, participant
->mi
,
1083 u32 mn
= be_to_host32(peer_mi
->mn
);
1085 wpa_printf(MSG_DEBUG
,
1086 "KaY: My MI - received MN %u, most recently transmitted MN %u",
1087 mn
, participant
->mn
);
1088 /* IEEE Std 802.1X-2010 is not exactly clear
1089 * which values of MN should be accepted here.
1090 * It uses "acceptably recent MN" language
1091 * without defining what would be acceptable
1092 * recent. For now, allow the last two used MN
1093 * values (i.e., peer having copied my MI,MN
1094 * from either of the last two MKPDUs that I
1096 if (mn
== participant
->mn
||
1097 (participant
->mn
> 1 &&
1098 mn
== participant
->mn
- 1))
1109 * ieee802_1x_mka_decode_live_peer_body -
1111 static int ieee802_1x_mka_decode_live_peer_body(
1112 struct ieee802_1x_mka_participant
*participant
,
1113 const u8
*peer_msg
, size_t msg_len
)
1115 const struct ieee802_1x_mka_hdr
*hdr
;
1116 struct ieee802_1x_kay_peer
*peer
;
1119 Boolean is_included
;
1121 is_included
= ieee802_1x_kay_is_in_live_peer(
1122 participant
, participant
->current_peer_id
.mi
);
1124 hdr
= (const struct ieee802_1x_mka_hdr
*) peer_msg
;
1125 body_len
= get_mka_param_body_len(hdr
);
1126 if (body_len
% 16 != 0) {
1127 wpa_printf(MSG_ERROR
,
1128 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1133 for (i
= 0; i
< body_len
; i
+= sizeof(struct ieee802_1x_mka_peer_id
)) {
1134 const struct ieee802_1x_mka_peer_id
*peer_mi
;
1137 peer_mi
= (const struct ieee802_1x_mka_peer_id
*)
1138 (peer_msg
+ MKA_HDR_LEN
+ i
);
1139 peer_mn
= be_to_host32(peer_mi
->mn
);
1142 if (os_memcmp(peer_mi
, participant
->mi
, MI_LEN
) == 0) {
1143 /* My message id is used by other participant */
1144 if (peer_mn
> participant
->mn
&&
1145 !reset_participant_mi(participant
))
1146 wpa_printf(MSG_DEBUG
, "KaY: Could not update mi");
1153 peer
= ieee802_1x_kay_get_peer(participant
, peer_mi
->mi
);
1156 } else if (!ieee802_1x_kay_create_potential_peer(
1157 participant
, peer_mi
->mi
, peer_mn
)) {
1167 * ieee802_1x_mka_decode_potential_peer_body -
1170 ieee802_1x_mka_decode_potential_peer_body(
1171 struct ieee802_1x_mka_participant
*participant
,
1172 const u8
*peer_msg
, size_t msg_len
)
1174 const struct ieee802_1x_mka_hdr
*hdr
;
1178 hdr
= (const struct ieee802_1x_mka_hdr
*) peer_msg
;
1179 body_len
= get_mka_param_body_len(hdr
);
1180 if (body_len
% 16 != 0) {
1181 wpa_printf(MSG_ERROR
,
1182 "KaY: MKA Peer Packet Body Length (%zu bytes) should be a multiple of 16 octets",
1187 for (i
= 0; i
< body_len
; i
+= sizeof(struct ieee802_1x_mka_peer_id
)) {
1188 const struct ieee802_1x_mka_peer_id
*peer_mi
;
1191 peer_mi
= (struct ieee802_1x_mka_peer_id
*)
1192 (peer_msg
+ MKA_HDR_LEN
+ i
);
1193 peer_mn
= be_to_host32(peer_mi
->mn
);
1196 if (os_memcmp(peer_mi
, participant
->mi
, MI_LEN
) == 0) {
1197 /* My message id is used by other participant */
1198 if (peer_mn
> participant
->mn
&&
1199 !reset_participant_mi(participant
))
1200 wpa_printf(MSG_DEBUG
, "KaY: Could not update mi");
1210 * ieee802_1x_mka_sak_use_body_present
1213 ieee802_1x_mka_sak_use_body_present(
1214 struct ieee802_1x_mka_participant
*participant
)
1216 return participant
->to_use_sak
;
1221 * ieee802_1x_mka_get_sak_use_length
1224 ieee802_1x_mka_get_sak_use_length(
1225 struct ieee802_1x_mka_participant
*participant
)
1227 int length
= MKA_HDR_LEN
;
1229 if (participant
->kay
->macsec_desired
&& participant
->advised_desired
)
1230 length
= sizeof(struct ieee802_1x_mka_sak_use_body
);
1232 return MKA_ALIGN_LENGTH(length
);
1237 * ieee802_1x_mka_get_lpn
1240 ieee802_1x_mka_get_lpn(struct ieee802_1x_mka_participant
*principal
,
1241 struct ieee802_1x_mka_ki
*ki
)
1243 struct transmit_sa
*txsa
;
1246 dl_list_for_each(txsa
, &principal
->txsc
->sa_list
,
1247 struct transmit_sa
, list
) {
1248 if (is_ki_equal(&txsa
->pkey
->key_identifier
, ki
)) {
1249 /* Per IEEE Std 802.1X-2010, Clause 9, "Each SecY uses
1250 * MKA to communicate the lowest PN used for
1251 * transmission with the SAK within the last two
1252 * seconds". Achieve this 2 second delay by setting the
1253 * lpn using the transmit next PN (i.e., txsa->next_pn)
1254 * that was read last time here (i.e., mka_hello_time
1257 * The lowest acceptable PN is the same as the last
1258 * transmitted PN, which is one less than the next
1261 * NOTE: This method only works if mka_hello_time is 2s.
1263 lpn
= (txsa
->next_pn
> 0) ? (txsa
->next_pn
- 1) : 0;
1265 /* Now read the current transmit next PN for use next
1267 secy_get_transmit_next_pn(principal
->kay
, txsa
);
1280 * ieee802_1x_mka_encode_sak_use_body -
1283 ieee802_1x_mka_encode_sak_use_body(
1284 struct ieee802_1x_mka_participant
*participant
,
1287 struct ieee802_1x_mka_sak_use_body
*body
;
1288 struct ieee802_1x_kay
*kay
= participant
->kay
;
1289 unsigned int length
;
1292 length
= ieee802_1x_mka_get_sak_use_length(participant
);
1293 body
= wpabuf_put(buf
, length
);
1295 body
->type
= MKA_SAK_USE
;
1296 set_mka_param_body_len(body
, length
- MKA_HDR_LEN
);
1298 if (length
== MKA_HDR_LEN
) {
1304 body
->delay_protect
= FALSE
;
1308 /* data delay protect */
1309 body
->delay_protect
= kay
->mka_hello_time
<= MKA_BOUNDED_HELLO_TIME
;
1310 /* lowest accept packet numbers */
1311 olpn
= ieee802_1x_mka_get_lpn(participant
, &participant
->oki
);
1312 body
->olpn
= host_to_be32(olpn
);
1313 llpn
= ieee802_1x_mka_get_lpn(participant
, &participant
->lki
);
1314 body
->llpn
= host_to_be32(llpn
);
1315 if (participant
->is_key_server
) {
1316 /* The CP will spend most of it's time in RETIRE where only
1317 * the old key is populated. Therefore we should be checking
1318 * the OLPN most of the time.
1320 if (participant
->lrx
) {
1321 if (llpn
> kay
->pn_exhaustion
) {
1322 wpa_printf(MSG_WARNING
,
1323 "KaY: My LLPN exhaustion");
1324 participant
->new_sak
= TRUE
;
1327 if (olpn
> kay
->pn_exhaustion
) {
1328 wpa_printf(MSG_WARNING
,
1329 "KaY: My OLPN exhaustion");
1330 participant
->new_sak
= TRUE
;
1335 /* plain tx, plain rx */
1336 body
->ptx
= !kay
->macsec_protect
;
1337 body
->prx
= kay
->macsec_validate
!= Strict
;
1339 /* latest key: rx, tx, key server member identifier key number */
1340 body
->lan
= participant
->lan
;
1341 os_memcpy(body
->lsrv_mi
, participant
->lki
.mi
, sizeof(body
->lsrv_mi
));
1342 body
->lkn
= host_to_be32(participant
->lki
.kn
);
1343 body
->lrx
= participant
->lrx
;
1344 body
->ltx
= participant
->ltx
;
1346 /* old key: rx, tx, key server member identifier key number */
1347 body
->oan
= participant
->oan
;
1348 if (participant
->oki
.kn
!= participant
->lki
.kn
&&
1349 participant
->oki
.kn
!= 0) {
1352 os_memcpy(body
->osrv_mi
, participant
->oki
.mi
,
1353 sizeof(body
->osrv_mi
));
1354 body
->okn
= host_to_be32(participant
->oki
.kn
);
1360 /* set CP's variable */
1362 kay
->tx_enable
= TRUE
;
1363 kay
->port_enable
= TRUE
;
1366 kay
->rx_enable
= TRUE
;
1368 ieee802_1x_mka_dump_sak_use_body(body
);
1374 * ieee802_1x_mka_decode_sak_use_body -
1377 ieee802_1x_mka_decode_sak_use_body(
1378 struct ieee802_1x_mka_participant
*participant
,
1379 const u8
*mka_msg
, size_t msg_len
)
1381 struct ieee802_1x_mka_hdr
*hdr
;
1382 struct ieee802_1x_mka_sak_use_body
*body
;
1383 struct ieee802_1x_kay_peer
*peer
;
1384 struct data_key
*sa_key
= NULL
;
1386 struct ieee802_1x_mka_ki ki
;
1388 struct ieee802_1x_kay
*kay
= participant
->kay
;
1391 if (!participant
->principal
) {
1392 wpa_printf(MSG_WARNING
, "KaY: Participant is not principal");
1395 peer
= ieee802_1x_kay_get_live_peer(participant
,
1396 participant
->current_peer_id
.mi
);
1398 wpa_printf(MSG_WARNING
,
1399 "KaY: The peer (%s) is not my live peer - ignore MACsec SAK Use parameter set",
1400 mi_txt(participant
->current_peer_id
.mi
));
1404 hdr
= (struct ieee802_1x_mka_hdr
*) mka_msg
;
1405 body_len
= get_mka_param_body_len(hdr
);
1406 body
= (struct ieee802_1x_mka_sak_use_body
*) mka_msg
;
1407 ieee802_1x_mka_dump_sak_use_body(body
);
1409 if ((body_len
!= 0) && (body_len
< 40)) {
1410 wpa_printf(MSG_ERROR
,
1411 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 40, or more octets",
1416 /* TODO: what action should I take when peer does not support MACsec */
1417 if (body_len
== 0) {
1418 wpa_printf(MSG_WARNING
, "KaY: Peer does not support MACsec");
1422 /* TODO: when the plain tx or rx of peer is true, should I change
1423 * the attribute of controlled port
1426 wpa_printf(MSG_WARNING
, "KaY: peer's plain rx are TRUE");
1429 wpa_printf(MSG_WARNING
, "KaY: peer's plain tx are TRUE");
1430 /* TODO: how to set the MACsec hardware when delay_protect is true */
1431 if (body
->delay_protect
&&
1432 (!be_to_host32(body
->llpn
) || !be_to_host32(body
->olpn
))) {
1433 wpa_printf(MSG_WARNING
,
1434 "KaY: Lowest packet number should be greater than 0 when delay_protect is TRUE");
1438 olpn
= be_to_host32(body
->olpn
);
1439 llpn
= be_to_host32(body
->llpn
);
1441 /* Our most recent distributed key should be the first in the list.
1442 * If it doesn't exist then we can't really do anything.
1443 * Be lenient and don't return error here as there are legitimate cases
1444 * where this can happen such as when a new participant joins the CA and
1445 * the first frame it receives can have a SAKuse but not distSAK.
1447 sa_key
= dl_list_first(&participant
->sak_list
, struct data_key
, list
);
1449 wpa_printf(MSG_INFO
,
1450 "KaY: We don't have a latest distributed key - ignore SAK use");
1454 /* The peer's most recent key will be the "latest key" if it is present
1455 * otherwise it will be the "old key" if in the RETIRE state.
1458 os_memcpy(ki
.mi
, body
->lsrv_mi
, sizeof(ki
.mi
));
1459 ki
.kn
= be_to_host32(body
->lkn
);
1462 os_memcpy(ki
.mi
, body
->osrv_mi
, sizeof(ki
.mi
));
1463 ki
.kn
= be_to_host32(body
->okn
);
1467 /* If the most recent distributed keys don't agree then someone is out
1468 * of sync. Perhaps non key server hasn't processed the most recent
1469 * distSAK yet and the key server is processing an old packet after it
1470 * has done distSAK. Be lenient and don't return error in this
1471 * particular case; otherwise, the key server will reset its MI and
1472 * cause a traffic disruption which is really undesired for a simple
1475 if (!is_ki_equal(&sa_key
->key_identifier
, &ki
)) {
1476 wpa_printf(MSG_INFO
,
1477 "KaY: Distributed keys don't match - ignore SAK use");
1480 sa_key
->next_pn
= lpn
;
1482 /* The key server must check that all peers are using the most recent
1483 * distributed key. Non key servers must check if the key server is
1486 if (participant
->is_key_server
) {
1487 struct ieee802_1x_kay_peer
*peer_iter
;
1488 Boolean all_receiving
= TRUE
;
1490 /* Distributed keys are equal from above comparison. */
1491 peer
->sak_used
= TRUE
;
1493 dl_list_for_each(peer_iter
, &participant
->live_peers
,
1494 struct ieee802_1x_kay_peer
, list
) {
1495 if (!peer_iter
->sak_used
) {
1496 all_receiving
= FALSE
;
1500 if (all_receiving
) {
1501 participant
->to_dist_sak
= FALSE
;
1502 ieee802_1x_cp_set_allreceiving(kay
->cp
, TRUE
);
1503 ieee802_1x_cp_sm_step(kay
->cp
);
1505 } else if (peer
->is_key_server
) {
1507 ieee802_1x_cp_set_servertransmitting(kay
->cp
, TRUE
);
1508 ieee802_1x_cp_sm_step(kay
->cp
);
1512 /* If I'm key server, and detects peer member PN exhaustion, rekey.
1513 * We only need to check the PN of the most recent distributed key. This
1514 * could be the peer's "latest" or "old" key depending on its current
1515 * state. If both "old" and "latest" keys are present then the "old" key
1516 * has already been exhausted.
1518 if (participant
->is_key_server
&& lpn
> kay
->pn_exhaustion
) {
1519 participant
->new_sak
= TRUE
;
1520 wpa_printf(MSG_WARNING
, "KaY: Peer LPN exhaustion");
1523 /* Get the associated RX SAs of the keys for delay protection since both
1524 * can be in use. Delay protect window (communicated via MKA) is tighter
1525 * than SecY's current replay protect window, so tell SecY the new (and
1528 if (body
->delay_protect
) {
1529 struct receive_sc
*rxsc
;
1530 struct receive_sa
*rxsa
;
1531 Boolean found
= FALSE
;
1533 dl_list_for_each(rxsc
, &participant
->rxsc_list
,
1534 struct receive_sc
, list
) {
1535 dl_list_for_each(rxsa
, &rxsc
->sa_list
,
1536 struct receive_sa
, list
) {
1537 if (sa_key
&& rxsa
->pkey
== sa_key
) {
1546 secy_get_receive_lowest_pn(participant
->kay
, rxsa
);
1547 if (lpn
> rxsa
->lowest_pn
) {
1548 rxsa
->lowest_pn
= lpn
;
1549 secy_set_receive_lowest_pn(participant
->kay
,
1551 wpa_printf(MSG_DEBUG
,
1552 "KaY: update dist LPN=0x%x", lpn
);
1556 /* FIX: Delay protection for the SA being replaced is not
1557 * implemented. Note that this key will be active for at least
1558 * MKA_SAK_RETIRE_TIME (3 seconds) but could be longer depending
1559 * on how long it takes to get from RECEIVE to TRANSMITTING or
1560 * if going via ABANDON. Delay protection does allow PNs within
1561 * a 2 second window, so getting PN would be a lot of work for
1562 * just 1 second's worth of protection.
1571 * ieee802_1x_mka_dist_sak_body_present
1574 ieee802_1x_mka_dist_sak_body_present(
1575 struct ieee802_1x_mka_participant
*participant
)
1577 return participant
->is_key_server
&& participant
->to_dist_sak
&&
1578 participant
->new_key
;
1583 * ieee802_1x_kay_get_dist_sak_length
1586 ieee802_1x_mka_get_dist_sak_length(
1587 struct ieee802_1x_mka_participant
*participant
)
1589 int length
= MKA_HDR_LEN
;
1590 unsigned int cs_index
= participant
->kay
->macsec_csindex
;
1592 if (participant
->advised_desired
&& cs_index
< CS_TABLE_SIZE
) {
1593 length
= sizeof(struct ieee802_1x_mka_dist_sak_body
);
1594 if (cs_index
!= DEFAULT_CS_INDEX
)
1595 length
+= CS_ID_LEN
;
1597 length
+= cipher_suite_tbl
[cs_index
].sak_len
+ 8;
1600 return MKA_ALIGN_LENGTH(length
);
1605 * ieee802_1x_mka_encode_dist_sak_body -
1608 ieee802_1x_mka_encode_dist_sak_body(
1609 struct ieee802_1x_mka_participant
*participant
,
1612 struct ieee802_1x_mka_dist_sak_body
*body
;
1613 struct data_key
*sak
;
1614 unsigned int length
;
1615 unsigned int cs_index
;
1618 length
= ieee802_1x_mka_get_dist_sak_length(participant
);
1619 body
= wpabuf_put(buf
, length
);
1620 body
->type
= MKA_DISTRIBUTED_SAK
;
1621 set_mka_param_body_len(body
, length
- MKA_HDR_LEN
);
1622 if (length
== MKA_HDR_LEN
) {
1623 body
->confid_offset
= 0;
1628 sak
= participant
->new_key
;
1630 wpa_printf(MSG_DEBUG
,
1631 "KaY: No SAK available to build Distributed SAK parameter set");
1634 body
->confid_offset
= sak
->confidentiality_offset
;
1635 body
->dan
= sak
->an
;
1636 body
->kn
= host_to_be32(sak
->key_identifier
.kn
);
1637 cs_index
= participant
->kay
->macsec_csindex
;
1639 if (cs_index
>= CS_TABLE_SIZE
)
1641 if (cs_index
!= DEFAULT_CS_INDEX
) {
1644 cs
= host_to_be64(cipher_suite_tbl
[cs_index
].id
);
1645 os_memcpy(body
->sak
, &cs
, CS_ID_LEN
);
1646 sak_pos
= CS_ID_LEN
;
1648 if (aes_wrap(participant
->kek
.key
, participant
->kek
.len
,
1649 cipher_suite_tbl
[cs_index
].sak_len
/ 8,
1650 sak
->key
, body
->sak
+ sak_pos
)) {
1651 wpa_printf(MSG_ERROR
, "KaY: AES wrap failed");
1655 ieee802_1x_mka_dump_dist_sak_body(body
);
1662 * ieee802_1x_kay_init_data_key -
1664 static void ieee802_1x_kay_init_data_key(struct data_key
*pkey
)
1666 pkey
->transmits
= TRUE
;
1667 pkey
->receives
= TRUE
;
1668 os_get_time(&pkey
->created_time
);
1676 * ieee802_1x_kay_decode_dist_sak_body -
1679 ieee802_1x_mka_decode_dist_sak_body(
1680 struct ieee802_1x_mka_participant
*participant
,
1681 const u8
*mka_msg
, size_t msg_len
)
1683 struct ieee802_1x_mka_hdr
*hdr
;
1684 struct ieee802_1x_mka_dist_sak_body
*body
;
1685 struct ieee802_1x_kay_peer
*peer
;
1686 struct macsec_ciphersuite
*cs
;
1688 struct data_key
*sa_key
= NULL
;
1692 struct ieee802_1x_kay
*kay
= participant
->kay
;
1694 hdr
= (struct ieee802_1x_mka_hdr
*) mka_msg
;
1695 body_len
= get_mka_param_body_len(hdr
);
1696 if ((body_len
!= 0) && (body_len
!= 28) && (body_len
< 36)) {
1697 wpa_printf(MSG_ERROR
,
1698 "KaY: MKA Use SAK Packet Body Length (%zu bytes) should be 0, 28, 36, or more octets",
1703 if (!participant
->principal
) {
1704 wpa_printf(MSG_ERROR
,
1705 "KaY: I can't accept the distributed SAK as I am not principal");
1708 if (participant
->is_key_server
) {
1709 wpa_printf(MSG_ERROR
,
1710 "KaY: Reject distributed SAK since I'm a key server");
1713 if (!kay
->macsec_desired
||
1714 kay
->macsec_capable
== MACSEC_CAP_NOT_IMPLEMENTED
) {
1715 wpa_printf(MSG_ERROR
,
1716 "KaY: I am not MACsec-desired or without MACsec capable");
1720 peer
= ieee802_1x_kay_get_live_peer(participant
,
1721 participant
->current_peer_id
.mi
);
1723 wpa_printf(MSG_ERROR
,
1724 "KaY: The key server is not in my live peers list");
1727 if (!sci_equal(&kay
->key_server_sci
, &peer
->sci
)) {
1728 wpa_printf(MSG_ERROR
, "KaY: The key server is not elected");
1732 if (body_len
== 0) {
1733 kay
->authenticated
= TRUE
;
1734 kay
->secured
= FALSE
;
1735 kay
->failed
= FALSE
;
1736 participant
->advised_desired
= FALSE
;
1737 ieee802_1x_cp_connect_authenticated(kay
->cp
);
1738 ieee802_1x_cp_sm_step(kay
->cp
);
1739 wpa_printf(MSG_WARNING
, "KaY: The Key server advise no MACsec");
1740 participant
->to_use_sak
= FALSE
;
1744 participant
->advised_desired
= TRUE
;
1745 kay
->authenticated
= FALSE
;
1746 kay
->secured
= TRUE
;
1747 kay
->failed
= FALSE
;
1748 ieee802_1x_cp_connect_secure(kay
->cp
);
1749 ieee802_1x_cp_sm_step(kay
->cp
);
1751 body
= (struct ieee802_1x_mka_dist_sak_body
*)mka_msg
;
1752 ieee802_1x_mka_dump_dist_sak_body(body
);
1753 dl_list_for_each(sa_key
, &participant
->sak_list
, struct data_key
, list
)
1755 if (os_memcmp(sa_key
->key_identifier
.mi
,
1756 participant
->current_peer_id
.mi
, MI_LEN
) == 0 &&
1757 sa_key
->key_identifier
.kn
== be_to_host32(body
->kn
)) {
1758 wpa_printf(MSG_DEBUG
,
1759 "KaY: SAK has already been installed - do not set it again");
1764 if (body_len
== 28) {
1765 sak_len
= DEFAULT_SA_KEY_LEN
;
1766 wrap_sak
= body
->sak
;
1767 kay
->macsec_csindex
= DEFAULT_CS_INDEX
;
1768 cs
= &cipher_suite_tbl
[kay
->macsec_csindex
];
1772 cs
= ieee802_1x_kay_get_cipher_suite(participant
, body
->sak
,
1775 wpa_printf(MSG_ERROR
,
1776 "KaY: I can't support the Cipher Suite advised by key server");
1779 sak_len
= cs
->sak_len
;
1780 wrap_sak
= body
->sak
+ CS_ID_LEN
;
1781 kay
->macsec_csindex
= idx
;
1784 unwrap_sak
= os_zalloc(sak_len
);
1786 wpa_printf(MSG_ERROR
, "KaY-%s: Out of memory", __func__
);
1789 if (aes_unwrap(participant
->kek
.key
, participant
->kek
.len
,
1790 sak_len
>> 3, wrap_sak
, unwrap_sak
)) {
1791 wpa_printf(MSG_ERROR
, "KaY: AES unwrap failed");
1792 os_free(unwrap_sak
);
1795 wpa_hexdump_key(MSG_DEBUG
, "\tAES Key Unwrap of SAK.:",
1796 unwrap_sak
, sak_len
);
1798 sa_key
= os_zalloc(sizeof(*sa_key
));
1800 os_free(unwrap_sak
);
1804 os_memcpy(&sa_key
->key_identifier
.mi
, &participant
->current_peer_id
.mi
,
1806 sa_key
->key_identifier
.kn
= be_to_host32(body
->kn
);
1808 sa_key
->key
= unwrap_sak
;
1809 sa_key
->key_len
= sak_len
;
1811 sa_key
->confidentiality_offset
= body
->confid_offset
;
1812 sa_key
->an
= body
->dan
;
1813 ieee802_1x_kay_init_data_key(sa_key
);
1815 ieee802_1x_kay_use_data_key(sa_key
);
1816 dl_list_add(&participant
->sak_list
, &sa_key
->list
);
1818 ieee802_1x_cp_set_ciphersuite(kay
->cp
, cs
->id
);
1819 ieee802_1x_cp_sm_step(kay
->cp
);
1820 ieee802_1x_cp_set_offset(kay
->cp
, body
->confid_offset
);
1821 ieee802_1x_cp_sm_step(kay
->cp
);
1822 ieee802_1x_cp_set_distributedki(kay
->cp
, &sa_key
->key_identifier
);
1823 ieee802_1x_cp_set_distributedan(kay
->cp
, body
->dan
);
1824 ieee802_1x_cp_signal_newsak(kay
->cp
);
1825 ieee802_1x_cp_sm_step(kay
->cp
);
1828 participant
->to_use_sak
= TRUE
;
1835 * ieee802_1x_mka_icv_body_present
1838 ieee802_1x_mka_icv_body_present(struct ieee802_1x_mka_participant
*participant
)
1845 * ieee802_1x_kay_get_icv_length
1848 ieee802_1x_mka_get_icv_length(struct ieee802_1x_mka_participant
*participant
)
1852 /* Determine if we need space for the ICV Indicator */
1853 if (mka_alg_tbl
[participant
->kay
->mka_algindex
].icv_len
!=
1855 length
= sizeof(struct ieee802_1x_mka_icv_body
);
1858 length
+= mka_alg_tbl
[participant
->kay
->mka_algindex
].icv_len
;
1860 return MKA_ALIGN_LENGTH(length
);
1865 * ieee802_1x_mka_encode_icv_body -
1868 ieee802_1x_mka_encode_icv_body(struct ieee802_1x_mka_participant
*participant
,
1871 struct ieee802_1x_mka_icv_body
*body
;
1872 unsigned int length
;
1873 u8 cmac
[MAX_ICV_LEN
];
1875 length
= ieee802_1x_mka_get_icv_length(participant
);
1876 if (mka_alg_tbl
[participant
->kay
->mka_algindex
].icv_len
!=
1878 wpa_printf(MSG_DEBUG
, "KaY: ICV Indicator");
1879 body
= wpabuf_put(buf
, MKA_HDR_LEN
);
1880 body
->type
= MKA_ICV_INDICATOR
;
1881 length
-= MKA_HDR_LEN
;
1882 set_mka_param_body_len(body
, length
);
1885 if (mka_alg_tbl
[participant
->kay
->mka_algindex
].icv_hash(
1886 participant
->ick
.key
, participant
->ick
.len
,
1887 wpabuf_head(buf
), wpabuf_len(buf
), cmac
)) {
1888 wpa_printf(MSG_ERROR
, "KaY: failed to calculate ICV");
1891 wpa_hexdump(MSG_DEBUG
, "KaY: ICV", cmac
, length
);
1893 os_memcpy(wpabuf_put(buf
, length
), cmac
, length
);
1899 * ieee802_1x_mka_decode_icv_body -
1902 ieee802_1x_mka_decode_icv_body(struct ieee802_1x_mka_participant
*participant
,
1903 const u8
*mka_msg
, size_t msg_len
)
1905 const struct ieee802_1x_mka_hdr
*hdr
;
1906 const struct ieee802_1x_mka_icv_body
*body
;
1914 while (left_len
> MKA_HDR_LEN
+ DEFAULT_ICV_LEN
) {
1915 hdr
= (const struct ieee802_1x_mka_hdr
*) pos
;
1916 body_len
= MKA_ALIGN_LENGTH(get_mka_param_body_len(hdr
));
1917 body_type
= get_mka_param_body_type(hdr
);
1919 if (left_len
< body_len
+ MKA_HDR_LEN
)
1922 if (body_type
!= MKA_ICV_INDICATOR
) {
1923 left_len
-= MKA_HDR_LEN
+ body_len
;
1924 pos
+= MKA_HDR_LEN
+ body_len
;
1928 body
= (const struct ieee802_1x_mka_icv_body
*) pos
;
1930 < mka_alg_tbl
[participant
->kay
->mka_algindex
].icv_len
)
1936 return mka_msg
+ msg_len
- DEFAULT_ICV_LEN
;
1941 * ieee802_1x_mka_decode_dist_cak_body-
1944 ieee802_1x_mka_decode_dist_cak_body(
1945 struct ieee802_1x_mka_participant
*participant
,
1946 const u8
*mka_msg
, size_t msg_len
)
1948 struct ieee802_1x_mka_hdr
*hdr
;
1951 hdr
= (struct ieee802_1x_mka_hdr
*) mka_msg
;
1952 body_len
= get_mka_param_body_len(hdr
);
1953 if (body_len
< 28) {
1954 wpa_printf(MSG_ERROR
,
1955 "KaY: MKA Use CAK Packet Body Length (%zu bytes) should be 28 or more octets",
1965 * ieee802_1x_mka_decode_kmd_body -
1968 ieee802_1x_mka_decode_kmd_body(
1969 struct ieee802_1x_mka_participant
*participant
,
1970 const u8
*mka_msg
, size_t msg_len
)
1972 struct ieee802_1x_mka_hdr
*hdr
;
1975 hdr
= (struct ieee802_1x_mka_hdr
*) mka_msg
;
1976 body_len
= get_mka_param_body_len(hdr
);
1978 wpa_printf(MSG_ERROR
,
1979 "KaY: MKA Use KMD Packet Body Length (%zu bytes) should be 5 or more octets",
1989 * ieee802_1x_mka_decode_announce_body -
1991 static int ieee802_1x_mka_decode_announce_body(
1992 struct ieee802_1x_mka_participant
*participant
,
1993 const u8
*mka_msg
, size_t msg_len
)
1999 struct mka_param_body_handler
{
2000 int (*body_tx
)(struct ieee802_1x_mka_participant
*participant
,
2001 struct wpabuf
*buf
);
2002 int (*body_rx
)(struct ieee802_1x_mka_participant
*participant
,
2003 const u8
*mka_msg
, size_t msg_len
);
2004 int (*body_length
)(struct ieee802_1x_mka_participant
*participant
);
2005 Boolean (*body_present
)(struct ieee802_1x_mka_participant
*participant
);
2009 static struct mka_param_body_handler mka_body_handler
[] = {
2010 /* Basic parameter set */
2012 .body_tx
= ieee802_1x_mka_encode_basic_body
,
2014 .body_length
= ieee802_1x_mka_basic_body_length
,
2015 .body_present
= ieee802_1x_mka_basic_body_present
2018 /* Live Peer List parameter set */
2020 .body_tx
= ieee802_1x_mka_encode_live_peer_body
,
2021 .body_rx
= ieee802_1x_mka_decode_live_peer_body
,
2022 .body_length
= ieee802_1x_mka_get_live_peer_length
,
2023 .body_present
= ieee802_1x_mka_live_peer_body_present
2026 /* Potential Peer List parameter set */
2028 .body_tx
= ieee802_1x_mka_encode_potential_peer_body
,
2029 .body_rx
= ieee802_1x_mka_decode_potential_peer_body
,
2030 .body_length
= ieee802_1x_mka_get_potential_peer_length
,
2031 .body_present
= ieee802_1x_mka_potential_peer_body_present
2034 /* MACsec SAK Use parameter set */
2036 .body_tx
= ieee802_1x_mka_encode_sak_use_body
,
2037 .body_rx
= ieee802_1x_mka_decode_sak_use_body
,
2038 .body_length
= ieee802_1x_mka_get_sak_use_length
,
2039 .body_present
= ieee802_1x_mka_sak_use_body_present
2042 /* Distributed SAK parameter set */
2044 .body_tx
= ieee802_1x_mka_encode_dist_sak_body
,
2045 .body_rx
= ieee802_1x_mka_decode_dist_sak_body
,
2046 .body_length
= ieee802_1x_mka_get_dist_sak_length
,
2047 .body_present
= ieee802_1x_mka_dist_sak_body_present
2050 /* Distribute CAK parameter set */
2053 .body_rx
= ieee802_1x_mka_decode_dist_cak_body
,
2054 .body_length
= NULL
,
2055 .body_present
= NULL
2058 /* KMD parameter set */
2061 .body_rx
= ieee802_1x_mka_decode_kmd_body
,
2062 .body_length
= NULL
,
2063 .body_present
= NULL
2066 /* Announcement parameter set */
2069 .body_rx
= ieee802_1x_mka_decode_announce_body
,
2070 .body_length
= NULL
,
2071 .body_present
= NULL
2074 /* ICV Indicator parameter set */
2076 .body_tx
= ieee802_1x_mka_encode_icv_body
,
2078 .body_length
= ieee802_1x_mka_get_icv_length
,
2079 .body_present
= ieee802_1x_mka_icv_body_present
2085 * ieee802_1x_kay_use_data_key - Take reference on a key
2087 static void ieee802_1x_kay_use_data_key(struct data_key
*pkey
)
2094 * ieee802_1x_kay_deinit_data_key - Release reference on a key and
2095 * free if there are no remaining users
2097 static void ieee802_1x_kay_deinit_data_key(struct data_key
*pkey
)
2112 * ieee802_1x_kay_generate_new_sak -
2115 ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant
*participant
)
2117 struct data_key
*sa_key
= NULL
;
2118 struct ieee802_1x_kay_peer
*peer
;
2119 struct ieee802_1x_kay
*kay
= participant
->kay
;
2120 int ctx_len
, ctx_offset
;
2122 unsigned int key_len
;
2124 struct macsec_ciphersuite
*cs
;
2126 /* check condition for generating a fresh SAK:
2127 * must have one live peer
2128 * and MKA life time elapse since last distribution
2129 * or potential peer is empty
2131 if (dl_list_empty(&participant
->live_peers
)) {
2132 wpa_printf(MSG_ERROR
,
2133 "KaY: Live peers list must not be empty when generating fresh SAK");
2137 /* FIXME: A fresh SAK not generated until
2138 * the live peer list contains at least one peer and
2139 * MKA life time has elapsed since the prior SAK was first distributed,
2140 * or the Key server's potential peer is empty
2141 * but I can't understand the second item, so
2142 * here only check first item and ingore
2143 * && (!dl_list_empty(&participant->potential_peers))) {
2145 if ((time(NULL
) - kay
->dist_time
) < MKA_LIFE_TIME
/ 1000) {
2146 wpa_printf(MSG_ERROR
,
2147 "KaY: Life time has not elapsed since prior SAK distributed");
2151 cs
= &cipher_suite_tbl
[kay
->macsec_csindex
];
2152 key_len
= cs
->sak_len
;
2153 key
= os_zalloc(key_len
);
2155 wpa_printf(MSG_ERROR
, "KaY-%s: Out of memory", __func__
);
2159 ctx_len
= key_len
+ sizeof(kay
->dist_kn
);
2160 dl_list_for_each(peer
, &participant
->live_peers
,
2161 struct ieee802_1x_kay_peer
, list
)
2162 ctx_len
+= sizeof(peer
->mi
);
2163 ctx_len
+= sizeof(participant
->mi
);
2165 context
= os_zalloc(ctx_len
);
2170 if (os_get_random(context
+ ctx_offset
, key_len
) < 0)
2173 ctx_offset
+= key_len
;
2174 dl_list_for_each(peer
, &participant
->live_peers
,
2175 struct ieee802_1x_kay_peer
, list
) {
2176 os_memcpy(context
+ ctx_offset
, peer
->mi
, sizeof(peer
->mi
));
2177 ctx_offset
+= sizeof(peer
->mi
);
2179 os_memcpy(context
+ ctx_offset
, participant
->mi
,
2180 sizeof(participant
->mi
));
2181 ctx_offset
+= sizeof(participant
->mi
);
2182 os_memcpy(context
+ ctx_offset
, &kay
->dist_kn
, sizeof(kay
->dist_kn
));
2184 if (key_len
== 16 || key_len
== 32) {
2185 if (ieee802_1x_sak_aes_cmac(participant
->cak
.key
,
2186 participant
->cak
.len
,
2189 wpa_printf(MSG_ERROR
, "KaY: Failed to generate SAK");
2193 wpa_printf(MSG_ERROR
, "KaY: SAK Length(%u) not supported",
2197 wpa_hexdump_key(MSG_DEBUG
, "KaY: generated new SAK", key
, key_len
);
2201 sa_key
= os_zalloc(sizeof(*sa_key
));
2203 wpa_printf(MSG_ERROR
, "KaY-%s: Out of memory", __func__
);
2208 sa_key
->key_len
= key_len
;
2209 os_memcpy(sa_key
->key_identifier
.mi
, participant
->mi
, MI_LEN
);
2210 sa_key
->key_identifier
.kn
= kay
->dist_kn
;
2212 sa_key
->confidentiality_offset
= kay
->macsec_confidentiality
;
2213 sa_key
->an
= kay
->dist_an
;
2214 ieee802_1x_kay_init_data_key(sa_key
);
2216 participant
->new_key
= sa_key
;
2218 ieee802_1x_kay_use_data_key(sa_key
);
2219 dl_list_add(&participant
->sak_list
, &sa_key
->list
);
2221 ieee802_1x_cp_set_ciphersuite(kay
->cp
, cs
->id
);
2222 ieee802_1x_cp_sm_step(kay
->cp
);
2223 ieee802_1x_cp_set_offset(kay
->cp
, kay
->macsec_confidentiality
);
2224 ieee802_1x_cp_sm_step(kay
->cp
);
2225 ieee802_1x_cp_set_distributedki(kay
->cp
, &sa_key
->key_identifier
);
2226 ieee802_1x_cp_set_distributedan(kay
->cp
, sa_key
->an
);
2227 ieee802_1x_cp_signal_newsak(kay
->cp
);
2228 ieee802_1x_cp_sm_step(kay
->cp
);
2230 dl_list_for_each(peer
, &participant
->live_peers
,
2231 struct ieee802_1x_kay_peer
, list
)
2232 peer
->sak_used
= FALSE
;
2236 if (kay
->dist_an
> 3)
2239 kay
->dist_time
= time(NULL
);
2250 static int compare_priorities(const struct ieee802_1x_kay_peer
*peer
,
2251 const struct ieee802_1x_kay_peer
*other
)
2253 if (peer
->key_server_priority
< other
->key_server_priority
)
2255 if (other
->key_server_priority
< peer
->key_server_priority
)
2258 return os_memcmp(peer
->sci
.addr
, other
->sci
.addr
, ETH_ALEN
);
2263 * ieee802_1x_kay_elect_key_server - elect the key server
2264 * when to elect: whenever the live peers list changes
2267 ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant
*participant
)
2269 struct ieee802_1x_kay_peer
*peer
;
2270 struct ieee802_1x_kay_peer
*key_server
= NULL
;
2271 struct ieee802_1x_kay
*kay
= participant
->kay
;
2272 Boolean i_is_key_server
;
2273 int priority_comparison
;
2275 if (participant
->is_obliged_key_server
) {
2276 participant
->new_sak
= TRUE
;
2277 participant
->to_dist_sak
= FALSE
;
2278 ieee802_1x_cp_set_electedself(kay
->cp
, TRUE
);
2282 /* elect the key server among the peers */
2283 dl_list_for_each(peer
, &participant
->live_peers
,
2284 struct ieee802_1x_kay_peer
, list
) {
2285 if (!peer
->is_key_server
)
2293 if (compare_priorities(peer
, key_server
) < 0)
2297 /* elect the key server between me and the above elected peer */
2298 i_is_key_server
= FALSE
;
2299 if (key_server
&& participant
->can_be_key_server
) {
2300 struct ieee802_1x_kay_peer tmp
;
2302 tmp
.key_server_priority
= kay
->actor_priority
;
2303 os_memcpy(&tmp
.sci
, &kay
->actor_sci
, sizeof(tmp
.sci
));
2304 priority_comparison
= compare_priorities(&tmp
, key_server
);
2305 if (priority_comparison
< 0) {
2306 i_is_key_server
= TRUE
;
2307 } else if (priority_comparison
== 0) {
2308 wpa_printf(MSG_WARNING
,
2309 "KaY: Cannot elect key server between me and peer, duplicate MAC detected");
2312 } else if (participant
->can_be_key_server
) {
2313 i_is_key_server
= TRUE
;
2316 if (i_is_key_server
) {
2317 ieee802_1x_cp_set_electedself(kay
->cp
, TRUE
);
2318 if (!sci_equal(&kay
->key_server_sci
, &kay
->actor_sci
)) {
2319 ieee802_1x_cp_signal_chgdserver(kay
->cp
);
2320 ieee802_1x_cp_sm_step(kay
->cp
);
2323 participant
->is_key_server
= TRUE
;
2324 participant
->principal
= TRUE
;
2325 participant
->new_sak
= TRUE
;
2326 wpa_printf(MSG_DEBUG
, "KaY: I am elected as key server");
2327 participant
->to_dist_sak
= FALSE
;
2328 participant
->is_elected
= TRUE
;
2330 os_memcpy(&kay
->key_server_sci
, &kay
->actor_sci
,
2331 sizeof(kay
->key_server_sci
));
2332 kay
->key_server_priority
= kay
->actor_priority
;
2333 } else if (key_server
) {
2334 wpa_printf(MSG_DEBUG
,
2335 "KaY: Peer %s was elected as the key server",
2336 mi_txt(key_server
->mi
));
2337 ieee802_1x_cp_set_electedself(kay
->cp
, FALSE
);
2338 if (!sci_equal(&kay
->key_server_sci
, &key_server
->sci
)) {
2339 ieee802_1x_cp_signal_chgdserver(kay
->cp
);
2340 ieee802_1x_cp_sm_step(kay
->cp
);
2343 participant
->is_key_server
= FALSE
;
2344 participant
->principal
= TRUE
;
2345 participant
->is_elected
= TRUE
;
2347 os_memcpy(&kay
->key_server_sci
, &key_server
->sci
,
2348 sizeof(kay
->key_server_sci
));
2349 kay
->key_server_priority
= key_server
->key_server_priority
;
2351 participant
->principal
= FALSE
;
2352 participant
->is_key_server
= FALSE
;
2353 participant
->is_elected
= FALSE
;
2361 * ieee802_1x_kay_decide_macsec_use - the key server determinate
2362 * how to use MACsec: whether use MACsec and its capability
2363 * protectFrames will be advised if the key server and one of its live peers are
2364 * MACsec capable and one of those request MACsec protection
2367 ieee802_1x_kay_decide_macsec_use(
2368 struct ieee802_1x_mka_participant
*participant
)
2370 struct ieee802_1x_kay
*kay
= participant
->kay
;
2371 struct ieee802_1x_kay_peer
*peer
;
2372 enum macsec_cap less_capability
;
2375 if (!participant
->is_key_server
)
2378 /* key server self is MACsec-desired and requesting MACsec */
2379 if (!kay
->macsec_desired
) {
2380 participant
->advised_desired
= FALSE
;
2383 if (kay
->macsec_capable
== MACSEC_CAP_NOT_IMPLEMENTED
) {
2384 participant
->advised_desired
= FALSE
;
2387 less_capability
= kay
->macsec_capable
;
2389 /* at least one of peers is MACsec-desired and requesting MACsec */
2391 dl_list_for_each(peer
, &participant
->live_peers
,
2392 struct ieee802_1x_kay_peer
, list
) {
2393 if (!peer
->macsec_desired
)
2396 if (peer
->macsec_capability
== MACSEC_CAP_NOT_IMPLEMENTED
)
2399 less_capability
= (less_capability
< peer
->macsec_capability
) ?
2400 less_capability
: peer
->macsec_capability
;
2405 participant
->advised_desired
= TRUE
;
2406 participant
->advised_capability
= less_capability
;
2407 kay
->authenticated
= FALSE
;
2408 kay
->secured
= TRUE
;
2409 kay
->failed
= FALSE
;
2410 ieee802_1x_cp_connect_secure(kay
->cp
);
2411 ieee802_1x_cp_sm_step(kay
->cp
);
2413 participant
->advised_desired
= FALSE
;
2414 participant
->advised_capability
= MACSEC_CAP_NOT_IMPLEMENTED
;
2415 participant
->to_use_sak
= FALSE
;
2416 kay
->authenticated
= TRUE
;
2417 kay
->secured
= FALSE
;
2418 kay
->failed
= FALSE
;
2427 ieee802_1x_cp_connect_authenticated(kay
->cp
);
2428 ieee802_1x_cp_sm_step(kay
->cp
);
2434 static const u8 pae_group_addr
[ETH_ALEN
] = {
2435 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03
2440 * ieee802_1x_kay_encode_mkpdu -
2443 ieee802_1x_kay_encode_mkpdu(struct ieee802_1x_mka_participant
*participant
,
2444 struct wpabuf
*pbuf
)
2447 struct ieee8023_hdr
*ether_hdr
;
2448 struct ieee802_1x_hdr
*eapol_hdr
;
2450 ether_hdr
= wpabuf_put(pbuf
, sizeof(*ether_hdr
));
2451 os_memcpy(ether_hdr
->dest
, pae_group_addr
, sizeof(ether_hdr
->dest
));
2452 os_memcpy(ether_hdr
->src
, participant
->kay
->actor_sci
.addr
,
2453 sizeof(ether_hdr
->dest
));
2454 ether_hdr
->ethertype
= host_to_be16(ETH_P_EAPOL
);
2455 wpa_printf(MSG_DEBUG
, "KaY: Ethernet header: DA=" MACSTR
" SA=" MACSTR
2457 MAC2STR(ether_hdr
->dest
), MAC2STR(ether_hdr
->src
),
2458 be_to_host16(ether_hdr
->ethertype
));
2460 eapol_hdr
= wpabuf_put(pbuf
, sizeof(*eapol_hdr
));
2461 eapol_hdr
->version
= EAPOL_VERSION
;
2462 eapol_hdr
->type
= IEEE802_1X_TYPE_EAPOL_MKA
;
2463 eapol_hdr
->length
= host_to_be16(wpabuf_tailroom(pbuf
));
2464 wpa_printf(MSG_DEBUG
,
2465 "KaY: Common EAPOL PDU structure: Protocol Version=%u Packet Type=%u Packet Body Length=%u",
2466 eapol_hdr
->version
, eapol_hdr
->type
,
2467 be_to_host16(eapol_hdr
->length
));
2469 for (i
= 0; i
< ARRAY_SIZE(mka_body_handler
); i
++) {
2470 if (mka_body_handler
[i
].body_present
&&
2471 mka_body_handler
[i
].body_present(participant
)) {
2472 if (mka_body_handler
[i
].body_tx(participant
, pbuf
))
2482 * ieee802_1x_participant_send_mkpdu -
2485 ieee802_1x_participant_send_mkpdu(
2486 struct ieee802_1x_mka_participant
*participant
)
2489 struct ieee802_1x_kay
*kay
= participant
->kay
;
2493 wpa_printf(MSG_DEBUG
, "KaY: Encode and send an MKPDU (ifname=%s)",
2495 length
+= sizeof(struct ieee802_1x_hdr
) + sizeof(struct ieee8023_hdr
);
2496 for (i
= 0; i
< ARRAY_SIZE(mka_body_handler
); i
++) {
2497 if (mka_body_handler
[i
].body_present
&&
2498 mka_body_handler
[i
].body_present(participant
))
2499 length
+= mka_body_handler
[i
].body_length(participant
);
2502 buf
= wpabuf_alloc(length
);
2504 wpa_printf(MSG_ERROR
, "KaY: out of memory");
2508 if (ieee802_1x_kay_encode_mkpdu(participant
, buf
)) {
2509 wpa_printf(MSG_ERROR
, "KaY: encode mkpdu fail");
2513 wpa_hexdump_buf(MSG_MSGDUMP
, "KaY: Outgoing MKPDU", buf
);
2514 l2_packet_send(kay
->l2_mka
, NULL
, 0, wpabuf_head(buf
), wpabuf_len(buf
));
2518 participant
->active
= TRUE
;
2524 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa
*psa
);
2526 static void ieee802_1x_delete_transmit_sa(struct ieee802_1x_kay
*kay
,
2527 struct transmit_sa
*sa
)
2529 secy_disable_transmit_sa(kay
, sa
);
2530 secy_delete_transmit_sa(kay
, sa
);
2531 ieee802_1x_kay_deinit_transmit_sa(sa
);
2536 * ieee802_1x_participant_timer -
2538 static void ieee802_1x_participant_timer(void *eloop_ctx
, void *timeout_ctx
)
2540 struct ieee802_1x_mka_participant
*participant
;
2541 struct ieee802_1x_kay
*kay
;
2542 struct ieee802_1x_kay_peer
*peer
, *pre_peer
;
2543 time_t now
= time(NULL
);
2545 struct receive_sc
*rxsc
, *pre_rxsc
;
2546 struct transmit_sa
*txsa
, *pre_txsa
;
2548 participant
= (struct ieee802_1x_mka_participant
*)eloop_ctx
;
2549 kay
= participant
->kay
;
2550 wpa_printf(MSG_DEBUG
, "KaY: Participant timer (ifname=%s)",
2552 if (participant
->cak_life
) {
2553 if (now
> participant
->cak_life
)
2557 /* should delete MKA instance if there are not live peers
2558 * when the MKA life elapsed since its creating */
2559 if (participant
->mka_life
) {
2560 if (dl_list_empty(&participant
->live_peers
)) {
2561 if (now
> participant
->mka_life
)
2564 participant
->mka_life
= 0;
2569 dl_list_for_each_safe(peer
, pre_peer
, &participant
->live_peers
,
2570 struct ieee802_1x_kay_peer
, list
) {
2571 if (now
> peer
->expire
) {
2572 wpa_printf(MSG_DEBUG
, "KaY: Live peer removed");
2573 wpa_hexdump(MSG_DEBUG
, "\tMI: ", peer
->mi
,
2575 wpa_printf(MSG_DEBUG
, "\tMN: %d", peer
->mn
);
2576 dl_list_for_each_safe(rxsc
, pre_rxsc
,
2577 &participant
->rxsc_list
,
2578 struct receive_sc
, list
) {
2579 if (sci_equal(&rxsc
->sci
, &peer
->sci
)) {
2580 ieee802_1x_kay_deinit_receive_sc(
2584 dl_list_del(&peer
->list
);
2591 if (dl_list_empty(&participant
->live_peers
)) {
2592 participant
->advised_desired
= FALSE
;
2593 participant
->advised_capability
=
2594 MACSEC_CAP_NOT_IMPLEMENTED
;
2595 participant
->to_use_sak
= FALSE
;
2596 participant
->ltx
= FALSE
;
2597 participant
->lrx
= FALSE
;
2598 participant
->otx
= FALSE
;
2599 participant
->orx
= FALSE
;
2600 participant
->is_key_server
= FALSE
;
2601 participant
->is_elected
= FALSE
;
2602 kay
->authenticated
= FALSE
;
2603 kay
->secured
= FALSE
;
2604 kay
->failed
= FALSE
;
2613 dl_list_for_each_safe(txsa
, pre_txsa
,
2614 &participant
->txsc
->sa_list
,
2615 struct transmit_sa
, list
) {
2616 ieee802_1x_delete_transmit_sa(kay
, txsa
);
2619 ieee802_1x_cp_connect_pending(kay
->cp
);
2620 ieee802_1x_cp_sm_step(kay
->cp
);
2622 ieee802_1x_kay_elect_key_server(participant
);
2623 ieee802_1x_kay_decide_macsec_use(participant
);
2627 dl_list_for_each_safe(peer
, pre_peer
, &participant
->potential_peers
,
2628 struct ieee802_1x_kay_peer
, list
) {
2629 if (now
> peer
->expire
) {
2630 wpa_printf(MSG_DEBUG
, "KaY: Potential peer removed");
2631 wpa_hexdump(MSG_DEBUG
, "\tMI: ", peer
->mi
,
2633 wpa_printf(MSG_DEBUG
, "\tMN: %d", peer
->mn
);
2634 dl_list_del(&peer
->list
);
2639 if (participant
->new_sak
&& participant
->is_key_server
) {
2640 if (!ieee802_1x_kay_generate_new_sak(participant
))
2641 participant
->to_dist_sak
= TRUE
;
2643 participant
->new_sak
= FALSE
;
2646 if (participant
->retry_count
< MAX_RETRY_CNT
||
2647 participant
->mode
== PSK
) {
2648 ieee802_1x_participant_send_mkpdu(participant
);
2649 participant
->retry_count
++;
2652 eloop_register_timeout(kay
->mka_hello_time
/ 1000, 0,
2653 ieee802_1x_participant_timer
,
2659 kay
->authenticated
= FALSE
;
2660 kay
->secured
= FALSE
;
2662 ieee802_1x_kay_delete_mka(kay
, &participant
->ckn
);
2667 * ieee802_1x_kay_init_transmit_sa -
2669 static struct transmit_sa
*
2670 ieee802_1x_kay_init_transmit_sa(struct transmit_sc
*psc
, u8 an
, u32 next_PN
,
2671 struct data_key
*key
)
2673 struct transmit_sa
*psa
;
2675 key
->tx_latest
= TRUE
;
2676 key
->rx_latest
= TRUE
;
2678 psa
= os_zalloc(sizeof(*psa
));
2680 wpa_printf(MSG_ERROR
, "%s: out of memory", __func__
);
2684 if (key
->confidentiality_offset
>= CONFIDENTIALITY_OFFSET_0
&&
2685 key
->confidentiality_offset
<= CONFIDENTIALITY_OFFSET_50
)
2686 psa
->confidentiality
= TRUE
;
2688 psa
->confidentiality
= FALSE
;
2691 ieee802_1x_kay_use_data_key(key
);
2693 psa
->next_pn
= next_PN
;
2696 os_get_time(&psa
->created_time
);
2697 psa
->in_use
= FALSE
;
2699 dl_list_add(&psc
->sa_list
, &psa
->list
);
2700 wpa_printf(MSG_DEBUG
,
2701 "KaY: Create transmit SA(an: %hhu, next_pn: %u) of SC",
2709 * ieee802_1x_kay_deinit_transmit_sa -
2711 static void ieee802_1x_kay_deinit_transmit_sa(struct transmit_sa
*psa
)
2713 ieee802_1x_kay_deinit_data_key(psa
->pkey
);
2715 wpa_printf(MSG_DEBUG
,
2716 "KaY: Delete transmit SA(an: %hhu) of SC",
2718 dl_list_del(&psa
->list
);
2724 * init_transmit_sc -
2726 static struct transmit_sc
*
2727 ieee802_1x_kay_init_transmit_sc(const struct ieee802_1x_mka_sci
*sci
)
2729 struct transmit_sc
*psc
;
2731 psc
= os_zalloc(sizeof(*psc
));
2733 wpa_printf(MSG_ERROR
, "%s: out of memory", __func__
);
2736 os_memcpy(&psc
->sci
, sci
, sizeof(psc
->sci
));
2738 os_get_time(&psc
->created_time
);
2739 psc
->transmitting
= FALSE
;
2740 psc
->encoding_sa
= FALSE
;
2741 psc
->enciphering_sa
= FALSE
;
2743 dl_list_init(&psc
->sa_list
);
2744 wpa_printf(MSG_DEBUG
, "KaY: Create transmit SC - SCI: %s",
2745 sci_txt(&psc
->sci
));
2752 * ieee802_1x_kay_deinit_transmit_sc -
2755 ieee802_1x_kay_deinit_transmit_sc(
2756 struct ieee802_1x_mka_participant
*participant
, struct transmit_sc
*psc
)
2758 struct transmit_sa
*psa
, *tmp
;
2760 wpa_printf(MSG_DEBUG
, "KaY: Delete transmit SC");
2761 dl_list_for_each_safe(psa
, tmp
, &psc
->sa_list
, struct transmit_sa
, list
)
2762 ieee802_1x_delete_transmit_sa(participant
->kay
, psa
);
2764 secy_delete_transmit_sc(participant
->kay
, psc
);
2769 /****************** Interface between CP and KAY *********************/
2771 * ieee802_1x_kay_set_latest_sa_attr -
2773 int ieee802_1x_kay_set_latest_sa_attr(struct ieee802_1x_kay
*kay
,
2774 struct ieee802_1x_mka_ki
*lki
, u8 lan
,
2775 Boolean ltx
, Boolean lrx
)
2777 struct ieee802_1x_mka_participant
*principal
;
2779 principal
= ieee802_1x_kay_get_principal_participant(kay
);
2784 os_memset(&principal
->lki
, 0, sizeof(principal
->lki
));
2786 os_memcpy(&principal
->lki
, lki
, sizeof(principal
->lki
));
2788 principal
->lan
= lan
;
2789 principal
->ltx
= ltx
;
2790 principal
->lrx
= lrx
;
2795 kay
->ltx_kn
= lki
->kn
;
2796 kay
->lrx_kn
= lki
->kn
;
2806 * ieee802_1x_kay_set_old_sa_attr -
2808 int ieee802_1x_kay_set_old_sa_attr(struct ieee802_1x_kay
*kay
,
2809 struct ieee802_1x_mka_ki
*oki
,
2810 u8 oan
, Boolean otx
, Boolean orx
)
2812 struct ieee802_1x_mka_participant
*principal
;
2814 principal
= ieee802_1x_kay_get_principal_participant(kay
);
2819 os_memset(&principal
->oki
, 0, sizeof(principal
->oki
));
2821 os_memcpy(&principal
->oki
, oki
, sizeof(principal
->oki
));
2823 principal
->oan
= oan
;
2824 principal
->otx
= otx
;
2825 principal
->orx
= orx
;
2831 kay
->otx_kn
= oki
->kn
;
2832 kay
->orx_kn
= oki
->kn
;
2841 static struct transmit_sa
* lookup_txsa_by_an(struct transmit_sc
*txsc
, u8 an
)
2843 struct transmit_sa
*txsa
;
2845 dl_list_for_each(txsa
, &txsc
->sa_list
, struct transmit_sa
, list
) {
2854 static struct receive_sa
* lookup_rxsa_by_an(struct receive_sc
*rxsc
, u8 an
)
2856 struct receive_sa
*rxsa
;
2858 dl_list_for_each(rxsa
, &rxsc
->sa_list
, struct receive_sa
, list
) {
2868 * ieee802_1x_kay_create_sas -
2870 int ieee802_1x_kay_create_sas(struct ieee802_1x_kay
*kay
,
2871 struct ieee802_1x_mka_ki
*lki
)
2873 struct data_key
*sa_key
, *latest_sak
;
2874 struct ieee802_1x_mka_participant
*principal
;
2875 struct receive_sc
*rxsc
;
2876 struct receive_sa
*rxsa
;
2877 struct transmit_sa
*txsa
;
2879 principal
= ieee802_1x_kay_get_principal_participant(kay
);
2884 dl_list_for_each(sa_key
, &principal
->sak_list
, struct data_key
, list
) {
2885 if (is_ki_equal(&sa_key
->key_identifier
, lki
)) {
2886 sa_key
->rx_latest
= TRUE
;
2887 sa_key
->tx_latest
= TRUE
;
2888 latest_sak
= sa_key
;
2889 principal
->to_use_sak
= TRUE
;
2891 sa_key
->rx_latest
= FALSE
;
2892 sa_key
->tx_latest
= FALSE
;
2896 wpa_printf(MSG_ERROR
, "KaY: lki related sak not found");
2900 dl_list_for_each(rxsc
, &principal
->rxsc_list
, struct receive_sc
, list
) {
2901 while ((rxsa
= lookup_rxsa_by_an(rxsc
, latest_sak
->an
)) != NULL
)
2902 ieee802_1x_delete_receive_sa(kay
, rxsa
);
2904 rxsa
= ieee802_1x_kay_init_receive_sa(rxsc
, latest_sak
->an
, 1,
2909 secy_create_receive_sa(kay
, rxsa
);
2912 while ((txsa
= lookup_txsa_by_an(principal
->txsc
, latest_sak
->an
)) !=
2914 ieee802_1x_delete_transmit_sa(kay
, txsa
);
2916 txsa
= ieee802_1x_kay_init_transmit_sa(principal
->txsc
, latest_sak
->an
,
2917 latest_sak
->next_pn
?
2918 latest_sak
->next_pn
: 1,
2923 secy_create_transmit_sa(kay
, txsa
);
2932 * ieee802_1x_kay_delete_sas -
2934 int ieee802_1x_kay_delete_sas(struct ieee802_1x_kay
*kay
,
2935 struct ieee802_1x_mka_ki
*ki
)
2937 struct data_key
*sa_key
, *pre_key
;
2938 struct transmit_sa
*txsa
, *pre_txsa
;
2939 struct receive_sa
*rxsa
, *pre_rxsa
;
2940 struct receive_sc
*rxsc
;
2941 struct ieee802_1x_mka_participant
*principal
;
2943 wpa_printf(MSG_DEBUG
, "KaY: Entry into %s", __func__
);
2944 principal
= ieee802_1x_kay_get_principal_participant(kay
);
2948 /* remove the transmit sa */
2949 dl_list_for_each_safe(txsa
, pre_txsa
, &principal
->txsc
->sa_list
,
2950 struct transmit_sa
, list
) {
2951 if (is_ki_equal(&txsa
->pkey
->key_identifier
, ki
))
2952 ieee802_1x_delete_transmit_sa(kay
, txsa
);
2955 /* remove the receive sa */
2956 dl_list_for_each(rxsc
, &principal
->rxsc_list
, struct receive_sc
, list
) {
2957 dl_list_for_each_safe(rxsa
, pre_rxsa
, &rxsc
->sa_list
,
2958 struct receive_sa
, list
) {
2959 if (is_ki_equal(&rxsa
->pkey
->key_identifier
, ki
))
2960 ieee802_1x_delete_receive_sa(kay
, rxsa
);
2964 /* remove the sak */
2965 dl_list_for_each_safe(sa_key
, pre_key
, &principal
->sak_list
,
2966 struct data_key
, list
) {
2967 if (is_ki_equal(&sa_key
->key_identifier
, ki
)) {
2968 if (principal
->new_key
== sa_key
)
2969 principal
->new_key
= NULL
;
2970 dl_list_del(&sa_key
->list
);
2971 ieee802_1x_kay_deinit_data_key(sa_key
);
2981 * ieee802_1x_kay_enable_tx_sas -
2983 int ieee802_1x_kay_enable_tx_sas(struct ieee802_1x_kay
*kay
,
2984 struct ieee802_1x_mka_ki
*lki
)
2986 struct ieee802_1x_mka_participant
*principal
;
2987 struct transmit_sa
*txsa
;
2989 principal
= ieee802_1x_kay_get_principal_participant(kay
);
2993 dl_list_for_each(txsa
, &principal
->txsc
->sa_list
, struct transmit_sa
,
2995 if (is_ki_equal(&txsa
->pkey
->key_identifier
, lki
)) {
2996 txsa
->in_use
= TRUE
;
2997 secy_enable_transmit_sa(kay
, txsa
);
2998 ieee802_1x_cp_set_usingtransmitas(
2999 principal
->kay
->cp
, TRUE
);
3000 ieee802_1x_cp_sm_step(principal
->kay
->cp
);
3009 * ieee802_1x_kay_enable_rx_sas -
3011 int ieee802_1x_kay_enable_rx_sas(struct ieee802_1x_kay
*kay
,
3012 struct ieee802_1x_mka_ki
*lki
)
3014 struct ieee802_1x_mka_participant
*principal
;
3015 struct receive_sa
*rxsa
;
3016 struct receive_sc
*rxsc
;
3018 principal
= ieee802_1x_kay_get_principal_participant(kay
);
3022 dl_list_for_each(rxsc
, &principal
->rxsc_list
, struct receive_sc
, list
) {
3023 dl_list_for_each(rxsa
, &rxsc
->sa_list
, struct receive_sa
, list
)
3025 if (is_ki_equal(&rxsa
->pkey
->key_identifier
, lki
)) {
3026 rxsa
->in_use
= TRUE
;
3027 secy_enable_receive_sa(kay
, rxsa
);
3028 ieee802_1x_cp_set_usingreceivesas(
3029 principal
->kay
->cp
, TRUE
);
3030 ieee802_1x_cp_sm_step(principal
->kay
->cp
);
3040 * ieee802_1x_kay_enable_new_info -
3042 int ieee802_1x_kay_enable_new_info(struct ieee802_1x_kay
*kay
)
3044 struct ieee802_1x_mka_participant
*principal
;
3046 principal
= ieee802_1x_kay_get_principal_participant(kay
);
3050 if (principal
->retry_count
< MAX_RETRY_CNT
|| principal
->mode
== PSK
) {
3051 ieee802_1x_participant_send_mkpdu(principal
);
3052 principal
->retry_count
++;
3060 * ieee802_1x_kay_mkpdu_sanity_check -
3061 * Sanity checks specified in IEEE Std 802.1X-2010, 11.11.2 (Validation of
3064 static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay
*kay
,
3065 const u8
*buf
, size_t len
)
3067 struct ieee8023_hdr
*eth_hdr
;
3068 struct ieee802_1x_hdr
*eapol_hdr
;
3069 struct ieee802_1x_mka_hdr
*mka_hdr
;
3070 struct ieee802_1x_mka_basic_body
*body
;
3072 struct ieee802_1x_mka_participant
*participant
;
3075 u8 icv
[MAX_ICV_LEN
];
3078 /* len > eth+eapol header already verified in kay_l2_receive();
3079 * likewise, eapol_hdr->length validated there */
3080 eth_hdr
= (struct ieee8023_hdr
*) buf
;
3081 eapol_hdr
= (struct ieee802_1x_hdr
*) (eth_hdr
+ 1);
3082 mka_hdr
= (struct ieee802_1x_mka_hdr
*) (eapol_hdr
+ 1);
3084 wpa_printf(MSG_DEBUG
, "KaY: Ethernet header: DA=" MACSTR
" SA=" MACSTR
3086 MAC2STR(eth_hdr
->dest
), MAC2STR(eth_hdr
->src
),
3087 be_to_host16(eth_hdr
->ethertype
));
3089 /* the destination address shall not be an individual address */
3090 if (os_memcmp(eth_hdr
->dest
, pae_group_addr
, ETH_ALEN
) != 0) {
3091 wpa_printf(MSG_DEBUG
,
3092 "KaY: ethernet destination address is not PAE group address");
3096 wpa_printf(MSG_DEBUG
,
3097 "KaY: Common EAPOL PDU structure: Protocol Version=%u Packet Type=%u Packet Body Length=%u",
3098 eapol_hdr
->version
, eapol_hdr
->type
,
3099 be_to_host16(eapol_hdr
->length
));
3101 /* MKPDU shall not be less than 32 octets */
3102 mka_msg_len
= be_to_host16(eapol_hdr
->length
);
3103 if (mka_msg_len
< 32) {
3104 wpa_printf(MSG_DEBUG
, "KaY: MKPDU is less than 32 octets");
3107 /* MKPDU shall be a multiple of 4 octets */
3108 if ((mka_msg_len
% 4) != 0) {
3109 wpa_printf(MSG_DEBUG
,
3110 "KaY: MKPDU is not multiple of 4 octets");
3114 wpa_hexdump(MSG_MSGDUMP
, "KaY: EAPOL-MKA Packet Body (MKPDU)",
3115 mka_hdr
, mka_msg_len
);
3117 /* Room for body_len already verified in kay_l2_receive() */
3118 body
= (struct ieee802_1x_mka_basic_body
*) mka_hdr
;
3119 body_len
= get_mka_param_body_len(body
);
3120 /* EAPOL-MKA body should comprise basic parameter set and ICV */
3121 if (mka_msg_len
< MKA_HDR_LEN
+ body_len
+ DEFAULT_ICV_LEN
) {
3122 wpa_printf(MSG_ERROR
,
3123 "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",
3124 mka_msg_len
, MKA_HDR_LEN
,
3125 body_len
, DEFAULT_ICV_LEN
);
3129 if (body_len
< sizeof(struct ieee802_1x_mka_basic_body
) - MKA_HDR_LEN
) {
3130 wpa_printf(MSG_DEBUG
, "KaY: Too small body length %zu",
3134 ckn_len
= body_len
-
3135 (sizeof(struct ieee802_1x_mka_basic_body
) - MKA_HDR_LEN
);
3136 if (ckn_len
< 1 || ckn_len
> MAX_CKN_LEN
) {
3137 wpa_printf(MSG_WARNING
,
3138 "KaY: Received EAPOL-MKA CKN Length (%zu bytes) is out of range (<= %u bytes)",
3139 ckn_len
, MAX_CKN_LEN
);
3143 ieee802_1x_mka_dump_basic_body(body
);
3145 /* CKN should be owned by I */
3146 participant
= ieee802_1x_kay_get_participant(kay
, body
->ckn
, ckn_len
);
3148 wpa_printf(MSG_DEBUG
, "KaY: CKN is not included in my CA");
3152 /* algorithm agility check */
3153 if (os_memcmp(body
->algo_agility
, mka_algo_agility
,
3154 sizeof(body
->algo_agility
)) != 0) {
3155 wpa_printf(MSG_INFO
,
3156 "KaY: Peer's algorithm agility (%s) not supported",
3157 algo_agility_txt(body
->algo_agility
));
3163 * The ICV will comprise the final octets of the packet body, whatever
3164 * its size, not the fixed length 16 octets, indicated by the EAPOL
3165 * packet body length.
3167 if (len
< mka_alg_tbl
[kay
->mka_algindex
].icv_len
||
3168 mka_alg_tbl
[kay
->mka_algindex
].icv_hash(
3169 participant
->ick
.key
, participant
->ick
.len
,
3170 buf
, len
- mka_alg_tbl
[kay
->mka_algindex
].icv_len
, icv
)) {
3171 wpa_printf(MSG_ERROR
, "KaY: Failed to calculate ICV");
3175 msg_icv
= ieee802_1x_mka_decode_icv_body(participant
,
3176 (const u8
*) mka_hdr
,
3179 wpa_printf(MSG_WARNING
, "KaY: No ICV in MKPDU - ignore it");
3182 wpa_hexdump(MSG_DEBUG
, "KaY: Received ICV",
3183 msg_icv
, mka_alg_tbl
[kay
->mka_algindex
].icv_len
);
3184 if (os_memcmp_const(msg_icv
, icv
,
3185 mka_alg_tbl
[kay
->mka_algindex
].icv_len
) != 0) {
3186 wpa_printf(MSG_WARNING
,
3187 "KaY: Computed ICV is not equal to Received ICV");
3188 wpa_hexdump(MSG_DEBUG
, "KaY: Calculated ICV",
3189 icv
, mka_alg_tbl
[kay
->mka_algindex
].icv_len
);
3198 * ieee802_1x_kay_decode_mkpdu -
3200 static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay
*kay
,
3201 const u8
*buf
, size_t len
)
3203 struct ieee802_1x_mka_participant
*participant
;
3204 struct ieee802_1x_mka_hdr
*hdr
;
3205 struct ieee802_1x_kay_peer
*peer
;
3211 Boolean handled
[256];
3212 Boolean bad_sak_use
= FALSE
; /* Error detected while processing SAK Use
3214 Boolean i_in_peerlist
, is_in_live_peer
, is_in_potential_peer
;
3216 wpa_printf(MSG_DEBUG
, "KaY: Decode received MKPDU (ifname=%s)",
3218 if (ieee802_1x_kay_mkpdu_sanity_check(kay
, buf
, len
))
3221 /* handle basic parameter set */
3222 pos
= buf
+ sizeof(struct ieee8023_hdr
) + sizeof(struct ieee802_1x_hdr
);
3223 left_len
= len
- sizeof(struct ieee8023_hdr
) -
3224 sizeof(struct ieee802_1x_hdr
);
3225 participant
= ieee802_1x_mka_decode_basic_body(kay
, pos
, left_len
);
3229 /* to skip basic parameter set */
3230 hdr
= (struct ieee802_1x_mka_hdr
*) pos
;
3231 body_len
= MKA_ALIGN_LENGTH(get_mka_param_body_len(hdr
));
3232 if (left_len
< body_len
+ MKA_HDR_LEN
)
3234 pos
+= body_len
+ MKA_HDR_LEN
;
3235 left_len
-= body_len
+ MKA_HDR_LEN
;
3237 /* check i am in the peer's peer list */
3238 i_in_peerlist
= ieee802_1x_mka_i_in_peerlist(participant
, pos
,
3240 is_in_live_peer
= ieee802_1x_kay_is_in_live_peer(
3241 participant
, participant
->current_peer_id
.mi
);
3242 wpa_printf(MSG_DEBUG
, "KaY: i_in_peerlist=%s is_in_live_peer=%s",
3243 yes_no(i_in_peerlist
), yes_no(is_in_live_peer
));
3244 if (i_in_peerlist
&& !is_in_live_peer
) {
3245 /* accept the peer as live peer */
3246 is_in_potential_peer
= ieee802_1x_kay_is_in_potential_peer(
3247 participant
, participant
->current_peer_id
.mi
);
3248 if (is_in_potential_peer
) {
3249 if (!ieee802_1x_kay_move_live_peer(
3251 participant
->current_peer_id
.mi
,
3252 be_to_host32(participant
->
3253 current_peer_id
.mn
)))
3255 } else if (!ieee802_1x_kay_create_live_peer(
3256 participant
, participant
->current_peer_id
.mi
,
3257 be_to_host32(participant
->
3258 current_peer_id
.mn
))) {
3262 ieee802_1x_kay_elect_key_server(participant
);
3263 ieee802_1x_kay_decide_macsec_use(participant
);
3267 * Handle other parameter set than basic parameter set.
3268 * Each parameter set should be present only once.
3270 for (i
= 0; i
< 256; i
++)
3274 for (; left_len
> MKA_HDR_LEN
+ DEFAULT_ICV_LEN
;
3275 pos
+= body_len
+ MKA_HDR_LEN
,
3276 left_len
-= body_len
+ MKA_HDR_LEN
) {
3277 hdr
= (struct ieee802_1x_mka_hdr
*) pos
;
3278 body_len
= MKA_ALIGN_LENGTH(get_mka_param_body_len(hdr
));
3279 body_type
= get_mka_param_body_type(hdr
);
3281 if (body_type
== MKA_ICV_INDICATOR
)
3284 if (left_len
< (MKA_HDR_LEN
+ body_len
+ DEFAULT_ICV_LEN
)) {
3285 wpa_printf(MSG_ERROR
,
3286 "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",
3287 left_len
, MKA_HDR_LEN
,
3288 body_len
, DEFAULT_ICV_LEN
);
3292 if (handled
[body_type
]) {
3293 wpa_printf(MSG_DEBUG
,
3294 "KaY: Ignore duplicated body type %u",
3299 handled
[body_type
] = TRUE
;
3300 if (body_type
< ARRAY_SIZE(mka_body_handler
) &&
3301 mka_body_handler
[body_type
].body_rx
) {
3302 if (mka_body_handler
[body_type
].body_rx
3303 (participant
, pos
, left_len
) != 0) {
3304 /* Handle parameter set failure */
3305 if (body_type
!= MKA_SAK_USE
) {
3306 wpa_printf(MSG_INFO
,
3307 "KaY: Discarding Rx MKPDU: decode of parameter set type (%d) failed",
3312 /* Ideally DIST-SAK should be processed before
3313 * SAK-USE. Unfortunately IEEE Std 802.1X-2010,
3314 * 11.11.3 (Encoding MKPDUs) states SAK-USE(3)
3315 * must always be encoded before DIST-SAK(4).
3316 * Rather than redesigning mka_body_handler so
3317 * that it somehow processes DIST-SAK before
3318 * SAK-USE, just ignore SAK-USE failures if
3319 * DIST-SAK is also present in this MKPDU. */
3323 wpa_printf(MSG_ERROR
,
3324 "KaY: The body type %d is not supported in this MKA version %d",
3325 body_type
, MKA_VERSION_ID
);
3329 if (bad_sak_use
&& !handled
[MKA_DISTRIBUTED_SAK
]) {
3330 wpa_printf(MSG_INFO
,
3331 "KaY: Discarding Rx MKPDU: decode of parameter set type (%d) failed",
3333 if (!reset_participant_mi(participant
))
3334 wpa_printf(MSG_DEBUG
, "KaY: Could not update mi");
3336 wpa_printf(MSG_DEBUG
,
3337 "KaY: Selected a new random MI: %s",
3338 mi_txt(participant
->mi
));
3342 /* Detect missing parameter sets */
3343 peer
= ieee802_1x_kay_get_live_peer(participant
,
3344 participant
->current_peer_id
.mi
);
3346 /* MKPDU is from live peer */
3347 if (!handled
[MKA_SAK_USE
]) {
3348 /* Once a live peer starts sending SAK-USE, it should be
3349 * sent every time. */
3350 if (peer
->sak_used
) {
3351 wpa_printf(MSG_INFO
,
3352 "KaY: Discarding Rx MKPDU: Live Peer stopped sending SAK-USE");
3356 /* Live peer is probably hung if it hasn't sent SAK-USE
3357 * after a reasonable number of MKPDUs. Drop the MKPDU,
3358 * which will eventually force an timeout. */
3359 if (++peer
->missing_sak_use_count
>
3360 MAX_MISSING_SAK_USE
) {
3361 wpa_printf(MSG_INFO
,
3362 "KaY: Discarding Rx MKPDU: Live Peer not sending SAK-USE");
3366 peer
->missing_sak_use_count
= 0;
3368 /* Only update live peer watchdog after successful
3369 * decode of all parameter sets */
3370 peer
->expire
= time(NULL
) + MKA_LIFE_TIME
/ 1000;
3373 /* MKPDU is from new or potential peer */
3374 peer
= ieee802_1x_kay_get_peer(participant
,
3375 participant
->current_peer_id
.mi
);
3377 wpa_printf(MSG_DEBUG
, "KaY: No peer entry found");
3381 /* Do not update potential peer watchdog. Per IEEE Std
3382 * 802.1X-2010, 9.4.3, potential peers need to show liveness by
3383 * including our MI/MN in their transmitted MKPDU (within
3384 * potential or live parameter sets). Whena potential peer does
3385 * include our MI/MN in an MKPDU, we respond by moving the peer
3386 * from 'potential_peers' to 'live_peers'. */
3390 participant
->retry_count
= 0;
3391 participant
->active
= TRUE
;
3398 static void kay_l2_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
3401 struct ieee802_1x_kay
*kay
= ctx
;
3402 struct ieee8023_hdr
*eth_hdr
;
3403 struct ieee802_1x_hdr
*eapol_hdr
;
3406 /* IEEE Std 802.1X-2010, 11.4 (Validation of received EAPOL PDUs) */
3408 /* must contain at least ieee8023_hdr + ieee802_1x_hdr */
3409 if (len
< sizeof(*eth_hdr
) + sizeof(*eapol_hdr
)) {
3410 wpa_printf(MSG_MSGDUMP
, "KaY: EAPOL frame too short (%lu)",
3411 (unsigned long) len
);
3415 eth_hdr
= (struct ieee8023_hdr
*) buf
;
3416 eapol_hdr
= (struct ieee802_1x_hdr
*) (eth_hdr
+ 1);
3417 calc_len
= sizeof(*eth_hdr
) + sizeof(*eapol_hdr
) +
3418 be_to_host16(eapol_hdr
->length
);
3419 if (len
< calc_len
) {
3420 wpa_printf(MSG_MSGDUMP
, "KaY: EAPOL MPDU is invalid: (received len %lu, calculated len %lu, EAPOL length %u)",
3421 (unsigned long) len
,
3422 (unsigned long) calc_len
,
3423 be_to_host16(eapol_hdr
->length
));
3426 if (len
> calc_len
) {
3427 wpa_hexdump(MSG_DEBUG
,
3428 "KaY: Ignore extra octets following the Packey Body field",
3429 &buf
[calc_len
], len
- calc_len
);
3433 if (eapol_hdr
->version
< EAPOL_VERSION
) {
3434 wpa_printf(MSG_MSGDUMP
, "KaY: version %d does not support MKA",
3435 eapol_hdr
->version
);
3438 if (be_to_host16(eth_hdr
->ethertype
) != ETH_P_PAE
||
3439 eapol_hdr
->type
!= IEEE802_1X_TYPE_EAPOL_MKA
)
3440 return; /* ignore other EAPOL types silently here */
3442 wpa_hexdump(MSG_DEBUG
, "KaY: RX EAPOL-MKA", buf
, len
);
3443 if (dl_list_empty(&kay
->participant_list
)) {
3444 wpa_printf(MSG_ERROR
,
3445 "KaY: No MKA participant instance - ignore EAPOL-MKA");
3449 ieee802_1x_kay_decode_mkpdu(kay
, buf
, len
);
3454 * ieee802_1x_kay_init -
3456 struct ieee802_1x_kay
*
3457 ieee802_1x_kay_init(struct ieee802_1x_kay_ctx
*ctx
, enum macsec_policy policy
,
3458 Boolean macsec_replay_protect
, u32 macsec_replay_window
,
3459 u16 port
, u8 priority
, const char *ifname
, const u8
*addr
)
3461 struct ieee802_1x_kay
*kay
;
3463 wpa_printf(MSG_DEBUG
, "KaY: Initialize - ifname=%s addr=" MACSTR
3464 " port=%u priority=%u",
3465 ifname
, MAC2STR(addr
), port
, priority
);
3466 kay
= os_zalloc(sizeof(*kay
));
3468 wpa_printf(MSG_ERROR
, "KaY-%s: out of memory", __func__
);
3476 kay
->active
= FALSE
;
3478 kay
->authenticated
= FALSE
;
3479 kay
->secured
= FALSE
;
3480 kay
->failed
= FALSE
;
3481 kay
->policy
= policy
;
3483 os_strlcpy(kay
->if_name
, ifname
, IFNAMSIZ
);
3484 os_memcpy(kay
->actor_sci
.addr
, addr
, ETH_ALEN
);
3485 kay
->actor_sci
.port
= host_to_be16(port
? port
: 0x0001);
3486 wpa_printf(MSG_DEBUG
, "KaY: Generated SCI: %s",
3487 sci_txt(&kay
->actor_sci
));
3488 kay
->actor_priority
= priority
;
3490 /* While actor acts as a key server, shall distribute sakey */
3495 kay
->pn_exhaustion
= PENDING_PN_EXHAUSTION
;
3496 kay
->macsec_csindex
= DEFAULT_CS_INDEX
;
3497 kay
->mka_algindex
= DEFAULT_MKA_ALG_INDEX
;
3498 kay
->mka_version
= MKA_VERSION_ID
;
3500 os_memcpy(kay
->algo_agility
, mka_algo_agility
,
3501 sizeof(kay
->algo_agility
));
3503 dl_list_init(&kay
->participant_list
);
3505 if (policy
!= DO_NOT_SECURE
&&
3506 secy_get_capability(kay
, &kay
->macsec_capable
) < 0)
3509 if (policy
== DO_NOT_SECURE
||
3510 kay
->macsec_capable
== MACSEC_CAP_NOT_IMPLEMENTED
) {
3511 kay
->macsec_capable
= MACSEC_CAP_NOT_IMPLEMENTED
;
3512 kay
->macsec_desired
= FALSE
;
3513 kay
->macsec_protect
= FALSE
;
3514 kay
->macsec_encrypt
= FALSE
;
3515 kay
->macsec_validate
= Disabled
;
3516 kay
->macsec_replay_protect
= FALSE
;
3517 kay
->macsec_replay_window
= 0;
3518 kay
->macsec_confidentiality
= CONFIDENTIALITY_NONE
;
3519 kay
->mka_hello_time
= MKA_HELLO_TIME
;
3521 kay
->macsec_desired
= TRUE
;
3522 kay
->macsec_protect
= TRUE
;
3523 if (kay
->macsec_capable
>= MACSEC_CAP_INTEG_AND_CONF
&&
3524 policy
== SHOULD_ENCRYPT
) {
3525 kay
->macsec_encrypt
= TRUE
;
3526 kay
->macsec_confidentiality
= CONFIDENTIALITY_OFFSET_0
;
3527 } else { /* SHOULD_SECURE */
3528 kay
->macsec_encrypt
= FALSE
;
3529 kay
->macsec_confidentiality
= CONFIDENTIALITY_NONE
;
3531 kay
->macsec_validate
= Strict
;
3532 kay
->macsec_replay_protect
= macsec_replay_protect
;
3533 kay
->macsec_replay_window
= macsec_replay_window
;
3534 kay
->mka_hello_time
= MKA_HELLO_TIME
;
3537 wpa_printf(MSG_DEBUG
, "KaY: state machine created");
3539 /* Initialize the SecY must be prio to CP, as CP will control SecY */
3540 if (secy_init_macsec(kay
) < 0) {
3541 wpa_printf(MSG_DEBUG
, "KaY: Could not initialize MACsec");
3545 wpa_printf(MSG_DEBUG
, "KaY: secy init macsec done");
3548 kay
->cp
= ieee802_1x_cp_sm_init(kay
);
3549 if (kay
->cp
== NULL
)
3552 if (policy
== DO_NOT_SECURE
) {
3553 ieee802_1x_cp_connect_authenticated(kay
->cp
);
3554 ieee802_1x_cp_sm_step(kay
->cp
);
3556 kay
->l2_mka
= l2_packet_init(kay
->if_name
, NULL
, ETH_P_PAE
,
3557 kay_l2_receive
, kay
, 1);
3558 if (kay
->l2_mka
== NULL
) {
3559 wpa_printf(MSG_WARNING
,
3560 "KaY: Failed to initialize L2 packet processing for MKA packet");
3568 ieee802_1x_kay_deinit(kay
);
3574 * ieee802_1x_kay_deinit -
3577 ieee802_1x_kay_deinit(struct ieee802_1x_kay
*kay
)
3579 struct ieee802_1x_mka_participant
*participant
;
3584 wpa_printf(MSG_DEBUG
, "KaY: state machine removed");
3586 while (!dl_list_empty(&kay
->participant_list
)) {
3587 participant
= dl_list_entry(kay
->participant_list
.next
,
3588 struct ieee802_1x_mka_participant
,
3590 ieee802_1x_kay_delete_mka(kay
, &participant
->ckn
);
3593 ieee802_1x_cp_sm_deinit(kay
->cp
);
3594 secy_deinit_macsec(kay
);
3597 l2_packet_deinit(kay
->l2_mka
);
3606 static const char * mode_txt(enum mka_created_mode mode
)
3620 * ieee802_1x_kay_create_mka -
3622 struct ieee802_1x_mka_participant
*
3623 ieee802_1x_kay_create_mka(struct ieee802_1x_kay
*kay
,
3624 const struct mka_key_name
*ckn
,
3625 const struct mka_key
*cak
, u32 life
,
3626 enum mka_created_mode mode
, Boolean is_authenticator
)
3628 struct ieee802_1x_mka_participant
*participant
;
3631 wpa_printf(MSG_DEBUG
,
3632 "KaY: Create MKA (ifname=%s mode=%s authenticator=%s)",
3633 kay
->if_name
, mode_txt(mode
), yes_no(is_authenticator
));
3635 if (!kay
|| !ckn
|| !cak
) {
3636 wpa_printf(MSG_ERROR
, "KaY: ckn or cak is null");
3640 if (cak
->len
!= 16 && cak
->len
!= 32) {
3641 wpa_printf(MSG_ERROR
, "KaY: Unexpected CAK length %u",
3642 (unsigned int) cak
->len
);
3645 if (ckn
->len
> MAX_CKN_LEN
) {
3646 wpa_printf(MSG_ERROR
, "KaY: CKN is out of range (>32 bytes)");
3650 wpa_printf(MSG_ERROR
, "KaY: Now is at disable state");
3654 participant
= os_zalloc(sizeof(*participant
));
3656 wpa_printf(MSG_ERROR
, "KaY-%s: out of memory", __func__
);
3660 participant
->ckn
.len
= ckn
->len
;
3661 os_memcpy(participant
->ckn
.name
, ckn
->name
, ckn
->len
);
3662 wpa_hexdump(MSG_DEBUG
, "KaY: CKN", participant
->ckn
.name
,
3663 participant
->ckn
.len
);
3664 participant
->cak
.len
= cak
->len
;
3665 os_memcpy(participant
->cak
.key
, cak
->key
, cak
->len
);
3666 wpa_hexdump_key(MSG_DEBUG
, "KaY: CAK", participant
->cak
.key
,
3667 participant
->cak
.len
);
3669 participant
->cak_life
= life
+ time(NULL
);
3673 if (is_authenticator
) {
3674 participant
->is_obliged_key_server
= TRUE
;
3675 participant
->can_be_key_server
= TRUE
;
3676 participant
->is_key_server
= TRUE
;
3677 participant
->principal
= TRUE
;
3679 os_memcpy(&kay
->key_server_sci
, &kay
->actor_sci
,
3680 sizeof(kay
->key_server_sci
));
3681 kay
->key_server_priority
= kay
->actor_priority
;
3682 participant
->is_elected
= TRUE
;
3684 participant
->is_obliged_key_server
= FALSE
;
3685 participant
->can_be_key_server
= FALSE
;
3686 participant
->is_key_server
= FALSE
;
3687 participant
->is_elected
= TRUE
;
3692 participant
->is_obliged_key_server
= FALSE
;
3693 participant
->can_be_key_server
= TRUE
;
3694 participant
->is_key_server
= TRUE
;
3695 participant
->is_elected
= FALSE
;
3699 participant
->cached
= FALSE
;
3701 participant
->active
= FALSE
;
3702 participant
->participant
= FALSE
;
3703 participant
->retain
= FALSE
;
3704 participant
->activate
= DEFAULT
;
3706 if (participant
->is_key_server
)
3707 participant
->principal
= TRUE
;
3709 dl_list_init(&participant
->live_peers
);
3710 dl_list_init(&participant
->potential_peers
);
3712 participant
->retry_count
= 0;
3713 participant
->kay
= kay
;
3715 if (!reset_participant_mi(participant
))
3717 wpa_printf(MSG_DEBUG
, "KaY: Selected random MI: %s",
3718 mi_txt(participant
->mi
));
3720 participant
->lrx
= FALSE
;
3721 participant
->ltx
= FALSE
;
3722 participant
->orx
= FALSE
;
3723 participant
->otx
= FALSE
;
3724 participant
->to_dist_sak
= FALSE
;
3725 participant
->to_use_sak
= FALSE
;
3726 participant
->new_sak
= FALSE
;
3727 dl_list_init(&participant
->sak_list
);
3728 participant
->new_key
= NULL
;
3729 dl_list_init(&participant
->rxsc_list
);
3730 participant
->txsc
= ieee802_1x_kay_init_transmit_sc(&kay
->actor_sci
);
3731 secy_cp_control_protect_frames(kay
, kay
->macsec_protect
);
3732 secy_cp_control_replay(kay
, kay
->macsec_replay_protect
,
3733 kay
->macsec_replay_window
);
3734 if (secy_create_transmit_sc(kay
, participant
->txsc
))
3737 /* to derive KEK from CAK and CKN */
3738 participant
->kek
.len
= participant
->cak
.len
;
3739 if (mka_alg_tbl
[kay
->mka_algindex
].kek_trfm(participant
->cak
.key
,
3740 participant
->cak
.len
,
3741 participant
->ckn
.name
,
3742 participant
->ckn
.len
,
3743 participant
->kek
.key
,
3744 participant
->kek
.len
)) {
3745 wpa_printf(MSG_ERROR
, "KaY: KEK derivation failed");
3748 wpa_hexdump_key(MSG_DEBUG
, "KaY: Derived KEK",
3749 participant
->kek
.key
, participant
->kek
.len
);
3751 /* to derive ICK from CAK and CKN */
3752 participant
->ick
.len
= participant
->cak
.len
;
3753 if (mka_alg_tbl
[kay
->mka_algindex
].ick_trfm(participant
->cak
.key
,
3754 participant
->cak
.len
,
3755 participant
->ckn
.name
,
3756 participant
->ckn
.len
,
3757 participant
->ick
.key
,
3758 participant
->ick
.len
)) {
3759 wpa_printf(MSG_ERROR
, "KaY: ICK derivation failed");
3762 wpa_hexdump_key(MSG_DEBUG
, "KaY: Derived ICK",
3763 participant
->ick
.key
, participant
->ick
.len
);
3765 dl_list_add(&kay
->participant_list
, &participant
->list
);
3767 usecs
= os_random() % (kay
->mka_hello_time
* 1000);
3768 eloop_register_timeout(0, usecs
, ieee802_1x_participant_timer
,
3771 /* Disable MKA lifetime for PSK mode.
3772 * The peer(s) can take a long time to come up, because we
3773 * create a "standby" MKA, and we need it to remain live until
3774 * some peer appears.
3777 participant
->mka_life
= MKA_LIFE_TIME
/ 1000 + time(NULL
) +
3780 participant
->mode
= mode
;
3785 os_free(participant
->txsc
);
3786 os_free(participant
);
3792 * ieee802_1x_kay_delete_mka -
3795 ieee802_1x_kay_delete_mka(struct ieee802_1x_kay
*kay
, struct mka_key_name
*ckn
)
3797 struct ieee802_1x_mka_participant
*participant
;
3798 struct ieee802_1x_kay_peer
*peer
;
3799 struct data_key
*sak
;
3800 struct receive_sc
*rxsc
;
3805 wpa_printf(MSG_DEBUG
, "KaY: participant removed");
3807 /* get the participant */
3808 participant
= ieee802_1x_kay_get_participant(kay
, ckn
->name
, ckn
->len
);
3810 wpa_hexdump(MSG_DEBUG
, "KaY: participant is not found",
3811 ckn
->name
, ckn
->len
);
3815 eloop_cancel_timeout(ieee802_1x_participant_timer
, participant
, NULL
);
3816 dl_list_del(&participant
->list
);
3818 /* remove live peer */
3819 while (!dl_list_empty(&participant
->live_peers
)) {
3820 peer
= dl_list_entry(participant
->live_peers
.next
,
3821 struct ieee802_1x_kay_peer
, list
);
3822 dl_list_del(&peer
->list
);
3826 /* remove potential peer */
3827 while (!dl_list_empty(&participant
->potential_peers
)) {
3828 peer
= dl_list_entry(participant
->potential_peers
.next
,
3829 struct ieee802_1x_kay_peer
, list
);
3830 dl_list_del(&peer
->list
);
3835 while (!dl_list_empty(&participant
->sak_list
)) {
3836 sak
= dl_list_entry(participant
->sak_list
.next
,
3837 struct data_key
, list
);
3838 dl_list_del(&sak
->list
);
3839 ieee802_1x_kay_deinit_data_key(sak
);
3841 while (!dl_list_empty(&participant
->rxsc_list
)) {
3842 rxsc
= dl_list_entry(participant
->rxsc_list
.next
,
3843 struct receive_sc
, list
);
3844 ieee802_1x_kay_deinit_receive_sc(participant
, rxsc
);
3846 ieee802_1x_kay_deinit_transmit_sc(participant
, participant
->txsc
);
3848 os_memset(&participant
->cak
, 0, sizeof(participant
->cak
));
3849 os_memset(&participant
->kek
, 0, sizeof(participant
->kek
));
3850 os_memset(&participant
->ick
, 0, sizeof(participant
->ick
));
3851 os_free(participant
);
3856 * ieee802_1x_kay_mka_participate -
3858 void ieee802_1x_kay_mka_participate(struct ieee802_1x_kay
*kay
,
3859 struct mka_key_name
*ckn
,
3862 struct ieee802_1x_mka_participant
*participant
;
3867 participant
= ieee802_1x_kay_get_participant(kay
, ckn
->name
, ckn
->len
);
3871 participant
->active
= status
;
3876 * ieee802_1x_kay_new_sak -
3879 ieee802_1x_kay_new_sak(struct ieee802_1x_kay
*kay
)
3881 struct ieee802_1x_mka_participant
*participant
;
3886 participant
= ieee802_1x_kay_get_principal_participant(kay
);
3890 participant
->new_sak
= TRUE
;
3891 wpa_printf(MSG_DEBUG
, "KaY: new SAK signal");
3898 * ieee802_1x_kay_change_cipher_suite -
3901 ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay
*kay
,
3902 unsigned int cs_index
)
3904 struct ieee802_1x_mka_participant
*participant
;
3905 enum macsec_cap secy_cap
;
3910 if (cs_index
>= CS_TABLE_SIZE
) {
3911 wpa_printf(MSG_ERROR
,
3912 "KaY: Configured cipher suite index is out of range");
3915 if (kay
->macsec_csindex
== cs_index
)
3919 kay
->macsec_desired
= FALSE
;
3921 kay
->macsec_csindex
= cs_index
;
3922 kay
->macsec_capable
= cipher_suite_tbl
[kay
->macsec_csindex
].capable
;
3924 if (secy_get_capability(kay
, &secy_cap
) < 0)
3927 if (kay
->macsec_capable
> secy_cap
)
3928 kay
->macsec_capable
= secy_cap
;
3930 participant
= ieee802_1x_kay_get_principal_participant(kay
);
3932 wpa_printf(MSG_INFO
, "KaY: Cipher Suite changed");
3933 participant
->new_sak
= TRUE
;
3940 #ifdef CONFIG_CTRL_IFACE
3943 * ieee802_1x_kay_get_status - Get IEEE 802.1X KaY status details
3944 * @sm: Pointer to KaY allocated with ieee802_1x_kay_init()
3945 * @buf: Buffer for status information
3946 * @buflen: Maximum buffer length
3947 * @verbose: Whether to include verbose status information
3948 * Returns: Number of bytes written to buf.
3950 * Query KaY status information. This function fills in a text area with current
3951 * status information. If the buffer (buf) is not large enough, status
3952 * information will be truncated to fit the buffer.
3954 int ieee802_1x_kay_get_status(struct ieee802_1x_kay
*kay
, char *buf
,
3959 struct ieee802_1x_mka_participant
*p
;
3967 res
= os_snprintf(pos
, end
- pos
,
3968 "PAE KaY status=%s\n"
3969 "Authenticated=%s\n"
3972 "Actor Priority=%u\n"
3973 "Key Server Priority=%u\n"
3974 "Is Key Server=%s\n"
3975 "Number of Keys Distributed=%u\n"
3976 "Number of Keys Received=%u\n"
3977 "MKA Hello Time=%u\n",
3978 kay
->active
? "Active" : "Not-Active",
3979 kay
->authenticated
? "Yes" : "No",
3980 kay
->secured
? "Yes" : "No",
3981 kay
->failed
? "Yes" : "No",
3982 kay
->actor_priority
,
3983 kay
->key_server_priority
,
3984 kay
->is_key_server
? "Yes" : "No",
3987 kay
->mka_hello_time
);
3988 if (os_snprintf_error(buflen
, res
))
3992 res
= os_snprintf(pos
, end
- pos
,
3993 "actor_sci=%s\n", sci_txt(&kay
->actor_sci
));
3994 if (os_snprintf_error(buflen
, res
))
3998 res
= os_snprintf(pos
, end
- pos
,
3999 "key_server_sci=%s\n", sci_txt(&kay
->key_server_sci
));
4000 if (os_snprintf_error(buflen
, res
))
4005 dl_list_for_each(p
, &kay
->participant_list
,
4006 struct ieee802_1x_mka_participant
, list
) {
4009 res
= os_snprintf(pos2
, end
- pos2
, "participant_idx=%d\nckn=",
4011 if (os_snprintf_error(buflen
, res
))
4016 pos2
+= wpa_snprintf_hex(pos2
, end
- pos2
, p
->ckn
.name
,
4019 res
= os_snprintf(pos2
, end
- pos2
,
4026 "potential_peers=%u\n"
4027 "is_key_server=%s\n"
4029 mi_txt(p
->mi
), p
->mn
,
4031 yes_no(p
->participant
),
4033 dl_list_len(&p
->live_peers
),
4034 dl_list_len(&p
->potential_peers
),
4035 yes_no(p
->is_key_server
),
4036 yes_no(p
->is_elected
));
4037 if (os_snprintf_error(buflen
, res
))
4047 static const char * true_false(Boolean val
)
4049 return val
? "true" : "false";
4053 static const char * activate_control_txt(enum activate_ctrl activate
)
4070 static char * mka_mib_peer(struct dl_list
*peers
, Boolean live
, char *buf
,
4074 struct ieee802_1x_kay_peer
*p
;
4077 dl_list_for_each(p
, peers
, struct ieee802_1x_kay_peer
, list
) {
4078 res
= os_snprintf(pos
, end
- pos
,
4079 "ieee8021XKayMkaPeerListMI=%s\n"
4080 "ieee8021XKayMkaPeerListMN=%u\n"
4081 "ieee8021XKayMkaPeerListType=%u\n"
4082 "ieee8021XKayMkaPeerListSCI=%s\n",
4087 if (os_snprintf_error(end
- pos
, res
))
4096 int ieee802_1x_kay_get_mib(struct ieee802_1x_kay
*kay
, char *buf
,
4101 struct ieee802_1x_mka_participant
*p
;
4109 dl_list_for_each(p
, &kay
->participant_list
,
4110 struct ieee802_1x_mka_participant
, list
) {
4113 res
= os_snprintf(pos2
, end
- pos2
, "ieee8021XKayMkaPartCKN=");
4114 if (os_snprintf_error(buflen
, res
))
4118 pos2
+= wpa_snprintf_hex(pos2
, end
- pos2
, p
->ckn
.name
,
4121 res
= os_snprintf(pos2
, end
- pos2
,
4122 "\nieee8021XKayMkaPartCached=%s\n"
4123 "ieee8021XKayMkaPartActive=%s\n"
4124 "ieee8021XKayMkaPartRetain=%s\n"
4125 "ieee8021XKayMkaPartActivateControl=%s\n"
4126 "ieee8021XKayMkaPartPrincipal=%s\n",
4127 true_false(p
->cached
),
4128 true_false(p
->active
),
4129 true_false(p
->retain
),
4130 activate_control_txt(p
->activate
),
4131 true_false(p
->principal
));
4132 if (os_snprintf_error(buflen
, res
))
4137 pos
= mka_mib_peer(&p
->live_peers
, TRUE
, pos
, end
);
4138 pos
= mka_mib_peer(&p
->potential_peers
, FALSE
, pos
, end
);
4144 #endif /* CONFIG_CTRL_IFACE */