Use a bool return value in keymat_v1_t.get_hash_phase2()

This commit is contained in:
Martin Willi
2012-07-16 14:53:34 +02:00
parent e4c5c1d03e
commit a7e6539135
3 changed files with 29 additions and 31 deletions
+2 -4
View File
@@ -1455,8 +1455,7 @@ METHOD(message_t, generate, status_t,
if (keymat && keymat->get_version(keymat) == IKEV1) if (keymat && keymat->get_version(keymat) == IKEV1)
{ {
/* get a hash for this message, if any is required */ /* get a hash for this message, if any is required */
hash = keymat_v1->get_hash_phase2(keymat_v1, &this->public); if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
if (hash.ptr)
{ /* insert a HASH payload as first payload */ { /* insert a HASH payload as first payload */
hash_payload_t *hash_payload; hash_payload_t *hash_payload;
@@ -2003,8 +2002,7 @@ METHOD(message_t, parse_body, status_t,
keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat; keymat_v1_t *keymat_v1 = (keymat_v1_t*)keymat;
chunk_t hash; chunk_t hash;
hash = keymat_v1->get_hash_phase2(keymat_v1, &this->public); if (keymat_v1->get_hash_phase2(keymat_v1, &this->public, &hash))
if (hash.ptr)
{ {
hash_payload_t *hash_payload; hash_payload_t *hash_payload;
chunk_t other_hash; chunk_t other_hash;
+24 -24
View File
@@ -827,19 +827,22 @@ static qm_data_t *lookup_quick_mode(private_keymat_v1_t *this, u_int32_t mid)
return found; return found;
} }
METHOD(keymat_v1_t, get_hash_phase2, chunk_t, METHOD(keymat_v1_t, get_hash_phase2, bool,
private_keymat_v1_t *this, message_t *message) private_keymat_v1_t *this, message_t *message, chunk_t *hash)
{ {
u_int32_t mid = message->get_message_id(message), mid_n = htonl(mid); u_int32_t mid, mid_n;
chunk_t data = chunk_empty, hash = chunk_empty; chunk_t data = chunk_empty;
bool add_message = TRUE; bool add_message = TRUE;
char *name = "Hash"; char *name = "Hash";
if (!this->prf) if (!this->prf)
{ /* no keys derived yet */ { /* no keys derived yet */
return hash; return FALSE;
} }
mid = message->get_message_id(message);
mid_n = htonl(mid);
/* Hashes are simple for most exchanges in Phase 2: /* Hashes are simple for most exchanges in Phase 2:
* Hash = prf(SKEYID_a, M-ID | Complete message after HASH payload) * Hash = prf(SKEYID_a, M-ID | Complete message after HASH payload)
* For Quick Mode there are three hashes: * For Quick Mode there are three hashes:
@@ -858,7 +861,7 @@ METHOD(keymat_v1_t, get_hash_phase2, chunk_t,
name = "Hash(1)"; name = "Hash(1)";
if (!get_nonce(message, &qm->n_i)) if (!get_nonce(message, &qm->n_i))
{ {
return hash; return FALSE;
} }
data = chunk_from_thing(mid_n); data = chunk_from_thing(mid_n);
} }
@@ -867,7 +870,7 @@ METHOD(keymat_v1_t, get_hash_phase2, chunk_t,
name = "Hash(2)"; name = "Hash(2)";
if (!get_nonce(message, &qm->n_r)) if (!get_nonce(message, &qm->n_r))
{ {
return hash; return FALSE;
} }
data = chunk_cata("cc", chunk_from_thing(mid_n), qm->n_i); data = chunk_cata("cc", chunk_from_thing(mid_n), qm->n_i);
} }
@@ -889,26 +892,23 @@ METHOD(keymat_v1_t, get_hash_phase2, chunk_t,
data = chunk_from_thing(mid_n); data = chunk_from_thing(mid_n);
break; break;
default: default:
break; return FALSE;
} }
if (data.ptr) this->prf->set_key(this->prf, this->skeyid_a);
if (add_message)
{ {
this->prf->set_key(this->prf, this->skeyid_a); generator_t *generator = generator_create_no_dbg();
if (add_message) chunk_t msg = get_message_data(message, generator);
{ this->prf->allocate_bytes(this->prf, data, NULL);
generator_t *generator = generator_create_no_dbg(); this->prf->allocate_bytes(this->prf, msg, hash);
chunk_t msg = get_message_data(message, generator); generator->destroy(generator);
this->prf->allocate_bytes(this->prf, data, NULL);
this->prf->allocate_bytes(this->prf, msg, &hash);
generator->destroy(generator);
}
else
{
this->prf->allocate_bytes(this->prf, data, &hash);
}
DBG3(DBG_IKE, "%s %B", name, &hash);
} }
return hash; else
{
this->prf->allocate_bytes(this->prf, data, hash);
}
DBG3(DBG_IKE, "%s %B", name, hash);
return TRUE;
} }
/** /**
+3 -3
View File
@@ -112,10 +112,10 @@ struct keymat_v1_t {
* Get HASH data for integrity/authentication in Phase 2 exchanges. * Get HASH data for integrity/authentication in Phase 2 exchanges.
* *
* @param message message to generate the HASH data for * @param message message to generate the HASH data for
* @return allocated HASH data * @param hash chunk receiving allocated hash data
* @return TRUE if hash allocated successfully
*/ */
chunk_t (*get_hash_phase2)(keymat_v1_t *this, message_t *message); bool (*get_hash_phase2)(keymat_v1_t *this, message_t *message, chunk_t *hash);
/** /**
* Returns the IV for a message with the given message ID. * Returns the IV for a message with the given message ID.