From d690cdd74d6e3c907b790c6659cede5934bffd05 Mon Sep 17 00:00:00 2001 From: NĂ©lio Laranjeiro Date: Thu, 25 Oct 2012 14:36:01 +0200 Subject: cesar/cp/msg: handle correctly the hash key in cm_get_key.req, closes #2634 * Always store it into the MME (stores 0 if key type != HASH_KEY) * Only read it if the key type is HASH_KEY --- cesar/cp/av/msg/test/utest/src/msg_cm.c | 39 +++++++------------ cesar/cp/msg/inc/cm_get_key.h | 2 +- cesar/cp/msg/src/msg_cm.c | 67 ++++++++++++++------------------- 3 files changed, 43 insertions(+), 65 deletions(-) (limited to 'cesar/cp') diff --git a/cesar/cp/av/msg/test/utest/src/msg_cm.c b/cesar/cp/av/msg/test/utest/src/msg_cm.c index 156ee2dbcf..7fe95fbf43 100644 --- a/cesar/cp/av/msg/test/utest/src/msg_cm.c +++ b/cesar/cp/av/msg/test/utest/src/msg_cm.c @@ -33,11 +33,6 @@ #include "test.h" #include -void -cp_msg_cm_get_key_req_access (bitstream_t *stream, - cp_secu_protocol_run_t *prun, - cp_msg_cm_get_key_req_t *data); - struct mme_header_t { mac_t oda; @@ -817,7 +812,7 @@ test_case_cm_get_key (test_t test) prun.my_nonce = 0x12; prun.your_nonce = 0x13; - data.relayed = false; + data.relayed = 0; data.key_type = CP_MSG_KEY_HASH_KEY; data.nid = 0x123456789ull; @@ -831,31 +826,23 @@ test_case_cm_get_key (test_t test) data.hash_key[i] = vector[i]; } - bitstream_write_init (&stream, buffer, 1518); - cp_msg_cm_get_key_req_access (&stream, &prun, &data); - bitstream_finalise (&stream); + cp_msg_cm_get_key_req_send (&cp, &CP_MME_PEER (1, 1), + CP_MME_PEKS_NONE, &prun, &data); bitstream_read_init (&stream, buffer, 1518); - test_fail_unless (bitstream_direct_read (buffer, 0, 8) == false); - test_fail_unless (bitstream_direct_read (buffer, 8, 8) == - CP_MSG_KEY_HASH_KEY); - test_fail_unless (bitstream_direct_read_large (buffer, 16, 56) == - data.nid); - test_fail_unless (bitstream_direct_read (buffer, 72, 32) == - prun.my_nonce); - test_fail_unless (bitstream_direct_read (buffer, 104, 8) == - prun.pid); - test_fail_unless (bitstream_direct_read (buffer, 112, 16) == - prun.prn); - test_fail_unless (bitstream_direct_read (buffer, 128, 8) == - prun.pmn); + bitstream_skip (&stream, BYTES_SIZE_TO_BITS (HPAV_MME_HEADER)); + test_fail_unless (bitstream_read (&stream, 8) == data.relayed); + test_fail_unless (bitstream_read (&stream, 8) == data.key_type); + test_fail_unless (bitstream_read_large (&stream, 56) == data.nid); + test_fail_unless (bitstream_read (&stream, 32) == prun.my_nonce); + test_fail_unless (bitstream_read (&stream, 8) == prun.pid); + test_fail_unless (bitstream_read (&stream, 16) == prun.prn); + test_fail_unless (bitstream_read (&stream, 8) == prun.pmn); /* Check by 32bits. */ - for (i = 0; i < CP_HASH_KEY_SIZE / sizeof (u32); i++) + for (i = 0; i < COUNT (vector); i++) { - test_fail_unless - (bitstream_direct_read (buffer, 136 + i * 32, 32) - == vector[i]); + test_fail_unless (bitstream_read (&stream, 32) == vector[i]); } } test_end; diff --git a/cesar/cp/msg/inc/cm_get_key.h b/cesar/cp/msg/inc/cm_get_key.h index 0a46389cc5..e8af57f387 100644 --- a/cesar/cp/msg/inc/cm_get_key.h +++ b/cesar/cp/msg/inc/cm_get_key.h @@ -27,7 +27,7 @@ enum cp_msg_cm_get_key_cnf_result_t struct cp_msg_cm_get_key_req_t { /** Request type: direct or relayed. */ - bool relayed; + uint relayed; /** Requested key type. */ enum cp_msg_key_type_t key_type; /** NID. */ diff --git a/cesar/cp/msg/src/msg_cm.c b/cesar/cp/msg/src/msg_cm.c index db6c30350d..a0401d0a08 100644 --- a/cesar/cp/msg/src/msg_cm.c +++ b/cesar/cp/msg/src/msg_cm.c @@ -377,37 +377,6 @@ cp_msg_cm_get_key_req_get_pid (cp_t *ctx, cp_mme_rx_t *mme, u8 *pid) return cp_msg_cm_get_pid (ctx, mme, 13, pid); } -/** - * CM_GET_KEY.REQ Access. - * \param stream the bitstream context. - * \param prun the protocol run context. - * \param data the structure to use to read or write the data. - */ -PRIVATE void -cp_msg_cm_get_key_req_access (bitstream_t *stream, - cp_secu_protocol_run_t *prun, - cp_msg_cm_get_key_req_t *data) -{ - uint i; - dbg_assert (stream); - dbg_assert (prun); - dbg_assert (data); - - bitstream_access (stream, &data->relayed, 8); - bitstream_access (stream, &data->key_type, 8); - bitstream_access (stream, &data->nid, 56); - bitstream_access (stream, &prun->my_nonce, 32); - bitstream_access (stream, &prun->pid, 8); - bitstream_access (stream, &prun->prn, 16); - bitstream_access (stream, &prun->pmn, 8); - - if (data->key_type != CP_MSG_KEY_HASH_KEY) - memset (data->hash_key, 0, CP_HASH_KEY_SIZE); - - for (i = 0; i < COUNT (data->hash_key); i++) - bitstream_access (stream, &data->hash_key[i], 32); -} - /** * Send a CM_GET_KEY.REQ. * \param ctx control plane context @@ -422,6 +391,7 @@ cp_msg_cm_get_key_req_send ( const cp_secu_protocol_run_t *prun, const cp_msg_cm_get_key_req_t *data) { cp_mme_tx_t *mme; + uint i; dbg_assert (ctx); dbg_assert (peer); dbg_assert (prun); @@ -439,10 +409,16 @@ cp_msg_cm_get_key_req_send ( mme = cp_msg_mme_init_encrypted (ctx, peer, CM_GET_KEY_REQ, peks, prun); dbg_check (mme); - cp_msg_cm_get_key_req_access (&mme->bitstream, - (cp_secu_protocol_run_t *) prun, - (cp_msg_cm_get_key_req_t *) data); + bitstream_write (&mme->bitstream, data->relayed, 8); + bitstream_write (&mme->bitstream, data->key_type, 8); + bitstream_write_large (&mme->bitstream, data->nid, 56); + bitstream_write (&mme->bitstream, prun->my_nonce, 32); + bitstream_write (&mme->bitstream, prun->pid, 8); + bitstream_write (&mme->bitstream, prun->prn, 16); + bitstream_write (&mme->bitstream, prun->pmn, 8); + for (i = 0; i < COUNT (data->hash_key); i++) + bitstream_write (&mme->bitstream, data->hash_key[i], 32); cp_msg_mme_send (ctx, mme); } @@ -465,15 +441,30 @@ cp_msg_cm_get_key_req_receive (cp_t *ctx, cp_mme_rx_t *mme, if (cp_msg_mme_read_error (ctx, mme)) { - cp_msg_cm_get_key_req_access (&mme->bitstream, - &mme->prun, - data); + data->relayed = bitstream_read (&mme->bitstream, 8); + data->key_type = bitstream_read (&mme->bitstream, 8); + data->nid = bitstream_read_large (&mme->bitstream, 56); + mme->prun.my_nonce = bitstream_read (&mme->bitstream, 32); + mme->prun.pid = bitstream_read (&mme->bitstream, 8); + mme->prun.prn = bitstream_read (&mme->bitstream, 16); + mme->prun.pmn = bitstream_read (&mme->bitstream, 8); + + if (data->key_type == CP_MSG_KEY_HASH_KEY) + { + uint i; + for (i = 0; i < COUNT (data->hash_key); i++) + data->hash_key[i] = bitstream_read (&mme->bitstream, 32); + } + XCH (mme->prun.my_nonce, mme->prun.your_nonce); if ((!HPAV_NID_IS_VALID (data->nid)) || (data->key_type >= CP_MSG_KEY_NB) || (data->relayed >= CP_MSG_RELAY_NB) - || (!cp_msg_mme_read_end_check (ctx, mme))) + || (!(cp_msg_mme_read_end_check (ctx, mme) + || bitstream_available_bits (&mme->bitstream) + == BYTES_SIZE_TO_BITS (CP_HASH_KEY_SIZE))) + ) return false; return true; } -- cgit v1.2.3