mka: Store cipher suite ID in a u64 instead of u8 pointer
[mech_eap.git] / src / pae / ieee802_1x_kay.c
index 32e7243..9bb4a94 100644 (file)
@@ -340,18 +340,6 @@ ieee802_1x_kay_is_in_live_peer(
 
 
 /**
- * ieee802_1x_kay_is_in_peer
- */
-static Boolean
-ieee802_1x_kay_is_in_peer(struct ieee802_1x_mka_participant *participant,
-                         const u8 *mi)
-{
-       return ieee802_1x_kay_is_in_live_peer(participant, mi) ||
-               ieee802_1x_kay_is_in_potential_peer(participant, mi);
-}
-
-
-/**
  * ieee802_1x_kay_get_peer
  */
 static struct ieee802_1x_kay_peer *
@@ -373,12 +361,17 @@ ieee802_1x_kay_get_peer(struct ieee802_1x_mka_participant *participant,
  */
 static struct macsec_ciphersuite *
 ieee802_1x_kay_get_cipher_suite(struct ieee802_1x_mka_participant *participant,
-                               u8 *cs_id)
+                               const u8 *cs_id)
 {
        unsigned int i;
+       u64 cs;
+       be64 _cs;
+
+       os_memcpy(&_cs, cs_id, CS_ID_LEN);
+       cs = be_to_host64(_cs);
 
        for (i = 0; i < CS_TABLE_SIZE; i++) {
-               if (os_memcmp(cipher_suite_tbl[i].id, cs_id, CS_ID_LEN) == 0)
+               if (cipher_suite_tbl[i].id == cs)
                        return &cipher_suite_tbl[i];
        }
 
@@ -705,7 +698,7 @@ ieee802_1x_mka_encode_basic_body(
        os_memcpy(body->actor_mi, participant->mi, sizeof(body->actor_mi));
        participant->mn = participant->mn + 1;
        body->actor_mn = host_to_be32(participant->mn);
-       os_memcpy(body->algo_agility, participant->kay->algo_agility,
+       os_memcpy(body->algo_agility, kay->algo_agility,
                  sizeof(body->algo_agility));
 
        os_memcpy(body->ckn, participant->ckn.name, participant->ckn.len);
@@ -1162,6 +1155,7 @@ ieee802_1x_mka_encode_sak_use_body(
        struct wpabuf *buf)
 {
        struct ieee802_1x_mka_sak_use_body *body;
+       struct ieee802_1x_kay *kay = participant->kay;
        unsigned int length;
        u32 pn = 1;
 
@@ -1182,9 +1176,9 @@ ieee802_1x_mka_encode_sak_use_body(
        }
 
        /* data protect, lowest accept packet number */
-       body->delay_protect = participant->kay->macsec_replay_protect;
+       body->delay_protect = kay->macsec_replay_protect;
        pn = ieee802_1x_mka_get_lpn(participant, &participant->lki);
-       if (pn > participant->kay->pn_exhaustion) {
+       if (pn > kay->pn_exhaustion) {
                wpa_printf(MSG_WARNING, "KaY: My LPN exhaustion");
                if (participant->is_key_server)
                        participant->new_sak = TRUE;
@@ -1195,20 +1189,12 @@ ieee802_1x_mka_encode_sak_use_body(
        body->olpn = host_to_be32(pn);
 
        /* plain tx, plain rx */
-       if (participant->kay->macsec_protect)
-               body->ptx = FALSE;
-       else
-               body->ptx = TRUE;
-
-       if (participant->kay->macsec_validate == Strict)
-               body->prx = FALSE;
-       else
-               body->prx = TRUE;
+       body->ptx = !kay->macsec_protect;
+       body->prx = kay->macsec_validate != Strict;
 
        /* latest key: rx, tx, key server member identifier key number */
        body->lan = participant->lan;
-       os_memcpy(body->lsrv_mi, participant->lki.mi,
-                 sizeof(body->lsrv_mi));
+       os_memcpy(body->lsrv_mi, participant->lki.mi, sizeof(body->lsrv_mi));
        body->lkn = host_to_be32(participant->lki.kn);
        body->lrx = participant->lrx;
        body->ltx = participant->ltx;
@@ -1229,16 +1215,11 @@ ieee802_1x_mka_encode_sak_use_body(
 
        /* set CP's variable */
        if (body->ltx) {
-               if (!participant->kay->tx_enable)
-                       participant->kay->tx_enable = TRUE;
-
-               if (!participant->kay->port_enable)
-                       participant->kay->port_enable = TRUE;
-       }
-       if (body->lrx) {
-               if (!participant->kay->rx_enable)
-                       participant->kay->rx_enable = TRUE;
+               kay->tx_enable = TRUE;
+               kay->port_enable = TRUE;
        }
+       if (body->lrx)
+               kay->rx_enable = TRUE;
 
        ieee802_1x_mka_dump_sak_use_body(body);
        return 0;
@@ -1263,6 +1244,7 @@ ieee802_1x_mka_decode_sak_use_body(
        u32 lpn;
        Boolean all_receiving;
        Boolean found;
+       struct ieee802_1x_kay *kay = participant->kay;
 
        if (!participant->principal) {
                wpa_printf(MSG_WARNING, "KaY: Participant is not principal");
@@ -1325,9 +1307,8 @@ ieee802_1x_mka_decode_sak_use_body(
                        peer->sak_used = TRUE;
                }
                if (body->ltx && peer->is_key_server) {
-                       ieee802_1x_cp_set_servertransmitting(
-                               participant->kay->cp, TRUE);
-                       ieee802_1x_cp_sm_step(participant->kay->cp);
+                       ieee802_1x_cp_set_servertransmitting(kay->cp, TRUE);
+                       ieee802_1x_cp_sm_step(kay->cp);
                }
        }
 
@@ -1361,13 +1342,13 @@ ieee802_1x_mka_decode_sak_use_body(
        }
        if (all_receiving) {
                participant->to_dist_sak = FALSE;
-               ieee802_1x_cp_set_allreceiving(participant->kay->cp, TRUE);
-               ieee802_1x_cp_sm_step(participant->kay->cp);
+               ieee802_1x_cp_set_allreceiving(kay->cp, TRUE);
+               ieee802_1x_cp_sm_step(kay->cp);
        }
 
        /* if i'm key server, and detects peer member pn exhaustion, rekey.*/
        lpn = be_to_host32(body->llpn);
-       if (lpn > participant->kay->pn_exhaustion) {
+       if (lpn > kay->pn_exhaustion) {
                if (participant->is_key_server) {
                        participant->new_sak = TRUE;
                        wpa_printf(MSG_WARNING, "KaY: Peer LPN exhaustion");
@@ -1390,9 +1371,9 @@ ieee802_1x_mka_decode_sak_use_body(
        /* FIXME: Secy creates txsa with default npn. If MKA detected Latest Key
         * npn is larger than txsa's npn, set it to txsa.
         */
-       secy_get_transmit_next_pn(participant->kay, txsa);
+       secy_get_transmit_next_pn(kay, txsa);
        if (lpn > txsa->next_pn) {
-               secy_set_transmit_next_pn(participant->kay, txsa);
+               secy_set_transmit_next_pn(kay, txsa);
                wpa_printf(MSG_INFO, "KaY: update lpn =0x%x", lpn);
        }
 
@@ -1407,10 +1388,7 @@ static Boolean
 ieee802_1x_mka_dist_sak_body_present(
        struct ieee802_1x_mka_participant *participant)
 {
-       if (!participant->to_dist_sak || !participant->new_key)
-               return FALSE;
-
-       return TRUE;
+       return participant->to_dist_sak && participant->new_key;
 }
 
 
@@ -1422,7 +1400,7 @@ ieee802_1x_mka_get_dist_sak_length(
        struct ieee802_1x_mka_participant *participant)
 {
        int length = MKA_HDR_LEN;
-       int cs_index = participant->kay->macsec_csindex;
+       unsigned int cs_index = participant->kay->macsec_csindex;
 
        if (participant->advised_desired) {
                length = sizeof(struct ieee802_1x_mka_dist_sak_body);
@@ -1447,7 +1425,7 @@ ieee802_1x_mka_encode_dist_sak_body(
        struct ieee802_1x_mka_dist_sak_body *body;
        struct data_key *sak;
        unsigned int length;
-       int cs_index;
+       unsigned int cs_index;
        int sak_pos;
 
        length = ieee802_1x_mka_get_dist_sak_length(participant);
@@ -1467,7 +1445,10 @@ ieee802_1x_mka_encode_dist_sak_body(
        cs_index = participant->kay->macsec_csindex;
        sak_pos = 0;
        if (cs_index != DEFAULT_CS_INDEX) {
-               os_memcpy(body->sak, cipher_suite_tbl[cs_index].id, CS_ID_LEN);
+               be64 cs;
+
+               cs = host_to_be64(cipher_suite_tbl[cs_index].id);
+               os_memcpy(body->sak, &cs, CS_ID_LEN);
                sak_pos = CS_ID_LEN;
        }
        if (aes_wrap(participant->kek.key, 16,
@@ -1541,6 +1522,7 @@ ieee802_1x_mka_decode_dist_sak_body(
        int sak_len;
        u8 *wrap_sak;
        u8 *unwrap_sak;
+       struct ieee802_1x_kay *kay = participant->kay;
 
        hdr = (struct ieee802_1x_mka_hdr *) mka_msg;
        body_len = get_mka_param_body_len(hdr);
@@ -1561,8 +1543,8 @@ ieee802_1x_mka_decode_dist_sak_body(
                           "KaY: I can't accept the distributed SAK as myself is key server ");
                return -1;
        }
-       if (!participant->kay->macsec_desired ||
-           participant->kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
+       if (!kay->macsec_desired ||
+           kay->macsec_capable == MACSEC_CAP_NOT_IMPLEMENTED) {
                wpa_printf(MSG_ERROR,
                           "KaY: I am not MACsec-desired or without MACsec capable");
                return -1;
@@ -1575,27 +1557,29 @@ ieee802_1x_mka_decode_dist_sak_body(
                           "KaY: The key server is not in my live peers list");
                return -1;
        }
-       if (!sci_equal(&participant->kay->key_server_sci, &peer->sci)) {
+       if (!sci_equal(&kay->key_server_sci, &peer->sci)) {
                wpa_printf(MSG_ERROR, "KaY: The key server is not elected");
                return -1;
        }
+
        if (body_len == 0) {
-               participant->kay->authenticated = TRUE;
-               participant->kay->secured = FALSE;
-               participant->kay->failed = FALSE;
+               kay->authenticated = TRUE;
+               kay->secured = FALSE;
+               kay->failed = FALSE;
                participant->advised_desired = FALSE;
-               ieee802_1x_cp_connect_authenticated(participant->kay->cp);
-               ieee802_1x_cp_sm_step(participant->kay->cp);
+               ieee802_1x_cp_connect_authenticated(kay->cp);
+               ieee802_1x_cp_sm_step(kay->cp);
                wpa_printf(MSG_WARNING, "KaY:The Key server advise no MACsec");
                participant->to_use_sak = TRUE;
                return 0;
        }
+
        participant->advised_desired = TRUE;
-       participant->kay->authenticated = FALSE;
-       participant->kay->secured = TRUE;
-       participant->kay->failed = FALSE;
-       ieee802_1x_cp_connect_secure(participant->kay->cp);
-       ieee802_1x_cp_sm_step(participant->kay->cp);
+       kay->authenticated = FALSE;
+       kay->secured = TRUE;
+       kay->failed = FALSE;
+       ieee802_1x_cp_connect_secure(kay->cp);
+       ieee802_1x_cp_sm_step(kay->cp);
 
        body = (struct ieee802_1x_mka_dist_sak_body *)mka_msg;
        ieee802_1x_mka_dump_dist_sak_body(body);
@@ -1608,10 +1592,11 @@ ieee802_1x_mka_decode_dist_sak_body(
                        return 0;
                }
        }
+
        if (body_len == 28) {
                sak_len = DEFAULT_SA_KEY_LEN;
                wrap_sak =  body->sak;
-               participant->kay->macsec_csindex = DEFAULT_CS_INDEX;
+               kay->macsec_csindex = DEFAULT_CS_INDEX;
        } else {
                cs = ieee802_1x_kay_get_cipher_suite(participant, body->sak);
                if (!cs) {
@@ -1621,7 +1606,7 @@ ieee802_1x_mka_decode_dist_sak_body(
                }
                sak_len = cs->sak_len;
                wrap_sak = body->sak + CS_ID_LEN;
-               participant->kay->macsec_csindex = cs->index;
+               kay->macsec_csindex = cs->index;
        }
 
        unwrap_sak = os_zalloc(sak_len);
@@ -1676,16 +1661,15 @@ ieee802_1x_mka_decode_dist_sak_body(
 
        dl_list_add(&participant->sak_list, &sa_key->list);
 
-       ieee802_1x_cp_set_ciphersuite(
-               participant->kay->cp,
-               cipher_suite_tbl[participant->kay->macsec_csindex].id);
-       ieee802_1x_cp_sm_step(participant->kay->cp);
-       ieee802_1x_cp_set_offset(participant->kay->cp, body->confid_offset);
-       ieee802_1x_cp_sm_step(participant->kay->cp);
-       ieee802_1x_cp_set_distributedki(participant->kay->cp, &sak_ki);
-       ieee802_1x_cp_set_distributedan(participant->kay->cp, body->dan);
-       ieee802_1x_cp_signal_newsak(participant->kay->cp);
-       ieee802_1x_cp_sm_step(participant->kay->cp);
+       ieee802_1x_cp_set_ciphersuite(kay->cp,
+                                     cipher_suite_tbl[kay->macsec_csindex].id);
+       ieee802_1x_cp_sm_step(kay->cp);
+       ieee802_1x_cp_set_offset(kay->cp, body->confid_offset);
+       ieee802_1x_cp_sm_step(kay->cp);
+       ieee802_1x_cp_set_distributedki(kay->cp, &sak_ki);
+       ieee802_1x_cp_set_distributedan(kay->cp, body->dan);
+       ieee802_1x_cp_signal_newsak(kay->cp);
+       ieee802_1x_cp_sm_step(kay->cp);
 
        participant->to_use_sak = TRUE;
 
@@ -2057,8 +2041,8 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
                    conf->key, conf->key_len);
 
        os_memcpy(conf->ki.mi, participant->mi, MI_LEN);
-       conf->ki.kn = participant->kay->dist_kn;
-       conf->an = participant->kay->dist_an;
+       conf->ki.kn = kay->dist_kn;
+       conf->an = kay->dist_an;
        conf->offset = kay->macsec_confidentiality;
        conf->rx = TRUE;
        conf->tx = TRUE;
@@ -2073,7 +2057,7 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
        participant->new_key = sa_key;
 
        dl_list_add(&participant->sak_list, &sa_key->list);
-       ieee802_1x_cp_set_ciphersuite(participant->kay->cp,
+       ieee802_1x_cp_set_ciphersuite(kay->cp,
                                      cipher_suite_tbl[kay->macsec_csindex].id);
        ieee802_1x_cp_sm_step(kay->cp);
        ieee802_1x_cp_set_offset(kay->cp, conf->offset);
@@ -2087,12 +2071,12 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
                         struct ieee802_1x_kay_peer, list)
                peer->sak_used = FALSE;
 
-       participant->kay->dist_kn++;
-       participant->kay->dist_an++;
-       if (participant->kay->dist_an > 3)
-               participant->kay->dist_an = 0;
+       kay->dist_kn++;
+       kay->dist_an++;
+       if (kay->dist_an > 3)
+               kay->dist_an = 0;
 
-       participant->kay->dist_time = time(NULL);
+       kay->dist_time = time(NULL);
 
        os_free(conf->key);
        os_free(conf);
@@ -2101,6 +2085,18 @@ ieee802_1x_kay_generate_new_sak(struct ieee802_1x_mka_participant *participant)
 }
 
 
+static int compare_priorities(const struct ieee802_1x_kay_peer *peer,
+                             const struct ieee802_1x_kay_peer *other)
+{
+       if (peer->key_server_priority < other->key_server_priority)
+               return -1;
+       if (other->key_server_priority < peer->key_server_priority)
+               return 1;
+
+       return os_memcmp(peer->sci.addr, other->sci.addr, ETH_ALEN);
+}
+
+
 /**
  * ieee802_1x_kay_elect_key_server - elect the key server
  * when to elect: whenever the live peers list changes
@@ -2131,29 +2127,19 @@ ieee802_1x_kay_elect_key_server(struct ieee802_1x_mka_participant *participant)
                        continue;
                }
 
-               if (peer->key_server_priority <
-                   key_server->key_server_priority) {
+               if (compare_priorities(peer, key_server) < 0)
                        key_server = peer;
-               } else if (peer->key_server_priority ==
-                          key_server->key_server_priority) {
-                       if (os_memcmp(peer->sci.addr, key_server->sci.addr,
-                                     ETH_ALEN) < 0)
-                               key_server = peer;
-               }
        }
 
        /* elect the key server between me and the above elected peer */
        i_is_key_server = FALSE;
        if (key_server && participant->can_be_key_server) {
-               if (kay->actor_priority
-                          < key_server->key_server_priority) {
+               struct ieee802_1x_kay_peer tmp;
+
+               tmp.key_server_priority = kay->actor_priority;
+               os_memcpy(&tmp.sci, &kay->actor_sci, sizeof(tmp.sci));
+               if (compare_priorities(&tmp, key_server) < 0)
                        i_is_key_server = TRUE;
-               } else if (kay->actor_priority
-                                       == key_server->key_server_priority) {
-                       if (os_memcmp(kay->actor_sci.addr, key_server->sci.addr,
-                                     ETH_ALEN) < 0)
-                               i_is_key_server = TRUE;
-               }
        } else if (participant->can_be_key_server) {
                i_is_key_server = TRUE;
        }
@@ -2369,27 +2355,16 @@ static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
        participant = (struct ieee802_1x_mka_participant *)eloop_ctx;
        kay = participant->kay;
        if (participant->cak_life) {
-               if (now > participant->cak_life) {
-                       kay->authenticated = FALSE;
-                       kay->secured = FALSE;
-                       kay->failed = TRUE;
-                       ieee802_1x_kay_delete_mka(kay, &participant->ckn);
-                       return;
-               }
+               if (now > participant->cak_life)
+                       goto delete_mka;
        }
 
        /* should delete MKA instance if there are not live peers
         * when the MKA life elapsed since its creating */
        if (participant->mka_life) {
                if (dl_list_empty(&participant->live_peers)) {
-                       if (now > participant->mka_life) {
-                               kay->authenticated = FALSE;
-                               kay->secured = FALSE;
-                               kay->failed = TRUE;
-                               ieee802_1x_kay_delete_mka(kay,
-                                                         &participant->ckn);
-                               return;
-                       }
+                       if (now > participant->mka_life)
+                               goto delete_mka;
                } else {
                        participant->mka_life = 0;
                }
@@ -2477,6 +2452,14 @@ static void ieee802_1x_participant_timer(void *eloop_ctx, void *timeout_ctx)
        eloop_register_timeout(MKA_HELLO_TIME / 1000, 0,
                               ieee802_1x_participant_timer,
                               participant, NULL);
+
+       return;
+
+delete_mka:
+       kay->authenticated = FALSE;
+       kay->secured = FALSE;
+       kay->failed = TRUE;
+       ieee802_1x_kay_delete_mka(kay, &participant->ckn);
 }
 
 
@@ -2956,21 +2939,19 @@ static int ieee802_1x_kay_mkpdu_sanity_check(struct ieee802_1x_kay *kay,
                wpa_printf(MSG_ERROR, "KaY: omac1_aes_128 failed");
                return -1;
        }
+
        msg_icv = ieee802_1x_mka_decode_icv_body(participant, (u8 *) mka_hdr,
                                                 mka_msg_len);
-
-       if (msg_icv) {
-               if (os_memcmp_const(msg_icv, icv,
-                                   mka_alg_tbl[kay->mka_algindex].icv_len) !=
-                   0) {
-                       wpa_printf(MSG_ERROR,
-                                  "KaY: Computed ICV is not equal to Received ICV");
-               return -1;
-               }
-       } else {
+       if (!msg_icv) {
                wpa_printf(MSG_ERROR, "KaY: No ICV");
                return -1;
        }
+       if (os_memcmp_const(msg_icv, icv,
+                           mka_alg_tbl[kay->mka_algindex].icv_len) != 0) {
+               wpa_printf(MSG_ERROR,
+                          "KaY: Computed ICV is not equal to Received ICV");
+               return -1;
+       }
 
        return 0;
 }
@@ -2989,7 +2970,6 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
        u8 body_type;
        int i;
        const u8 *pos;
-       Boolean my_included;
        Boolean handled[256];
 
        if (ieee802_1x_kay_mkpdu_sanity_check(kay, buf, len))
@@ -3010,21 +2990,10 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
        left_len -= body_len + MKA_HDR_LEN;
 
        /* check i am in the peer's peer list */
-       my_included = ieee802_1x_mka_i_in_peerlist(participant, pos, left_len);
-       if (my_included) {
+       if (ieee802_1x_mka_i_in_peerlist(participant, pos, left_len) &&
+           !ieee802_1x_kay_is_in_live_peer(participant,
+                                           participant->current_peer_id.mi)) {
                /* accept the peer as live peer */
-               if (!ieee802_1x_kay_is_in_peer(
-                           participant,
-                           participant->current_peer_id.mi)) {
-                       if (!ieee802_1x_kay_create_live_peer(
-                                   participant,
-                                   participant->current_peer_id.mi,
-                                   be_to_host32(
-                                           participant->current_peer_id.mn)))
-                               return -1;
-                       ieee802_1x_kay_elect_key_server(participant);
-                       ieee802_1x_kay_decide_macsec_use(participant);
-               }
                if (ieee802_1x_kay_is_in_potential_peer(
                            participant, participant->current_peer_id.mi)) {
                        if (!ieee802_1x_kay_move_live_peer(
@@ -3033,9 +3002,15 @@ static int ieee802_1x_kay_decode_mkpdu(struct ieee802_1x_kay *kay,
                                    be_to_host32(participant->
                                                 current_peer_id.mn)))
                                return -1;
-                       ieee802_1x_kay_elect_key_server(participant);
-                       ieee802_1x_kay_decide_macsec_use(participant);
+               } else if (!ieee802_1x_kay_create_live_peer(
+                                  participant, participant->current_peer_id.mi,
+                                  be_to_host32(participant->
+                                               current_peer_id.mn))) {
+                               return -1;
                }
+
+               ieee802_1x_kay_elect_key_server(participant);
+               ieee802_1x_kay_decide_macsec_use(participant);
        }
 
        /*
@@ -3521,14 +3496,15 @@ ieee802_1x_kay_new_sak(struct ieee802_1x_kay *kay)
  * ieee802_1x_kay_change_cipher_suite -
  */
 int
-ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay, int cs_index)
+ieee802_1x_kay_change_cipher_suite(struct ieee802_1x_kay *kay,
+                                  unsigned int cs_index)
 {
        struct ieee802_1x_mka_participant *participant;
 
        if (!kay)
                return -1;
 
-       if ((unsigned int) cs_index >= CS_TABLE_SIZE) {
+       if (cs_index >= CS_TABLE_SIZE) {
                wpa_printf(MSG_ERROR,
                           "KaY: Configured cipher suite index is out of range");
                return -1;