Do not update payload length during generation, allows hooks override payload length

This commit is contained in:
Martin Willi
2011-01-05 16:45:47 +01:00
parent d58127af84
commit 2ecbd6186e
8 changed files with 12 additions and 58 deletions
+3 -23
View File
@@ -146,7 +146,7 @@ static payload_type_t get_type(private_ke_payload_t *this)
*/ */
static payload_type_t get_next_type(private_ke_payload_t *this) static payload_type_t get_next_type(private_ke_payload_t *this)
{ {
return (this->next_payload); return this->next_payload;
} }
/** /**
@@ -163,6 +163,7 @@ static void set_next_type(private_ke_payload_t *this,payload_type_t type)
static void compute_length(private_ke_payload_t *this) static void compute_length(private_ke_payload_t *this)
{ {
size_t length = KE_PAYLOAD_HEADER_LENGTH; size_t length = KE_PAYLOAD_HEADER_LENGTH;
if (this->key_exchange_data.ptr != NULL) if (this->key_exchange_data.ptr != NULL)
{ {
length += this->key_exchange_data.len; length += this->key_exchange_data.len;
@@ -175,7 +176,6 @@ static void compute_length(private_ke_payload_t *this)
*/ */
static size_t get_length(private_ke_payload_t *this) static size_t get_length(private_ke_payload_t *this)
{ {
compute_length(this);
return this->payload_length; return this->payload_length;
} }
@@ -184,26 +184,7 @@ static size_t get_length(private_ke_payload_t *this)
*/ */
static chunk_t get_key_exchange_data(private_ke_payload_t *this) static chunk_t get_key_exchange_data(private_ke_payload_t *this)
{ {
return (this->key_exchange_data); return this->key_exchange_data;
}
/**
* Implementation of ke_payload_t.set_key_exchange_data.
*/
static void set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
{
/* destroy existing data first */
if (this->key_exchange_data.ptr != NULL)
{
/* free existing value */
free(this->key_exchange_data.ptr);
this->key_exchange_data.ptr = NULL;
this->key_exchange_data.len = 0;
}
this->key_exchange_data = chunk_clone(key_exchange_data);
compute_length(this);
} }
/** /**
@@ -240,7 +221,6 @@ ke_payload_t *ke_payload_create()
/* public functions */ /* public functions */
this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data; this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
this->public.set_key_exchange_data = (void (*) (ke_payload_t *,chunk_t)) set_key_exchange_data;
this->public.get_dh_group_number = (diffie_hellman_group_t (*) (ke_payload_t *)) get_dh_group_number; this->public.get_dh_group_number = (diffie_hellman_group_t (*) (ke_payload_t *)) get_dh_group_number;
this->public.set_dh_group_number =(void (*) (ke_payload_t *,diffie_hellman_group_t)) set_dh_group_number; this->public.set_dh_group_number =(void (*) (ke_payload_t *,diffie_hellman_group_t)) set_dh_group_number;
this->public.destroy = (void (*) (ke_payload_t *)) destroy; this->public.destroy = (void (*) (ke_payload_t *)) destroy;
@@ -55,15 +55,6 @@ struct ke_payload_t {
*/ */
chunk_t (*get_key_exchange_data) (ke_payload_t *this); chunk_t (*get_key_exchange_data) (ke_payload_t *this);
/**
* Sets the key exchange data of this KE payload.
*
* Value is getting copied.
*
* @param key_exchange_data chunk_t pointing to the value to set
*/
void (*set_key_exchange_data) (ke_payload_t *this, chunk_t key_exchange_data);
/** /**
* Gets the Diffie-Hellman Group Number of this KE payload. * Gets the Diffie-Hellman Group Number of this KE payload.
* *
@@ -78,7 +78,7 @@ encoding_rule_t nonce_payload_encodings[] = {
/* Length of the whole nonce payload*/ /* Length of the whole nonce payload*/
{ PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) }, { PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) },
/* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */ /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
{ NONCE_DATA, offsetof(private_nonce_payload_t, nonce) } { NONCE_DATA, offsetof(private_nonce_payload_t, nonce) },
}; };
/* 1 2 3 /* 1 2 3
@@ -97,12 +97,10 @@ encoding_rule_t nonce_payload_encodings[] = {
*/ */
static status_t verify(private_nonce_payload_t *this) static status_t verify(private_nonce_payload_t *this)
{ {
if ((this->nonce.len < 16) || ((this->nonce.len > 256))) if (this->nonce.len < 16 || this->nonce.len > 256)
{ {
/* nonce length is wrong */
return FAILED; return FAILED;
} }
return SUCCESS; return SUCCESS;
} }
@@ -111,8 +109,7 @@ static status_t verify(private_nonce_payload_t *this)
*/ */
static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce) static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce)
{ {
this->nonce.ptr = clalloc(nonce.ptr, nonce.len); this->nonce = chunk_clone(nonce);
this->nonce.len = nonce.len;
this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len; this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
return SUCCESS; return SUCCESS;
} }
@@ -122,10 +119,7 @@ static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce)
*/ */
static chunk_t get_nonce(private_nonce_payload_t *this) static chunk_t get_nonce(private_nonce_payload_t *this)
{ {
chunk_t nonce; return chunk_clone(this->nonce);
nonce.ptr = clalloc(this->nonce.ptr,this->nonce.len);
nonce.len = this->nonce.len;
return nonce;
} }
/** /**
@@ -150,7 +144,7 @@ static payload_type_t get_type(private_nonce_payload_t *this)
*/ */
static payload_type_t get_next_type(private_nonce_payload_t *this) static payload_type_t get_next_type(private_nonce_payload_t *this)
{ {
return (this->next_payload); return this->next_payload;
} }
/** /**
@@ -161,20 +155,11 @@ static void set_next_type(private_nonce_payload_t *this,payload_type_t type)
this->next_payload = type; this->next_payload = type;
} }
/**
* recompute the length of the payload.
*/
static void compute_length(private_nonce_payload_t *this)
{
this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + this->nonce.len;
}
/** /**
* Implementation of payload_t.get_length. * Implementation of payload_t.get_length.
*/ */
static size_t get_length(private_nonce_payload_t *this) static size_t get_length(private_nonce_payload_t *this)
{ {
compute_length(this);
return this->payload_length; return this->payload_length;
} }
@@ -187,7 +172,6 @@ static void destroy(private_nonce_payload_t *this)
{ {
free(this->nonce.ptr); free(this->nonce.ptr);
} }
free(this); free(this);
} }
@@ -459,7 +459,6 @@ static void compute_length (private_notify_payload_t *this)
*/ */
static size_t get_length(private_notify_payload_t *this) static size_t get_length(private_notify_payload_t *this)
{ {
compute_length(this);
return this->payload_length; return this->payload_length;
} }
@@ -234,7 +234,6 @@ static void compute_length(private_proposal_substructure_t *this)
METHOD(payload_t, get_length, size_t, METHOD(payload_t, get_length, size_t,
private_proposal_substructure_t *this) private_proposal_substructure_t *this)
{ {
compute_length(this);
return this->proposal_length; return this->proposal_length;
} }
@@ -366,6 +365,7 @@ METHOD(proposal_substructure_t, clone_, proposal_substructure_t*,
add_transform_substructure(clone, current); add_transform_substructure(clone, current);
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
compute_length(clone);
return &clone->public; return &clone->public;
} }
@@ -409,6 +409,7 @@ proposal_substructure_t *proposal_substructure_create()
.destroy = _destroy, .destroy = _destroy,
}, },
.next_payload = NO_PAYLOAD, .next_payload = NO_PAYLOAD,
.proposal_length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH,
.transforms = linked_list_create(), .transforms = linked_list_create(),
); );
@@ -500,6 +501,7 @@ proposal_substructure_t *proposal_substructure_create_from_proposal(
} }
this->proposal_number = proposal->get_number(proposal); this->proposal_number = proposal->get_number(proposal);
this->protocol_id = proposal->get_protocol(proposal); this->protocol_id = proposal->get_protocol(proposal);
compute_length(this);
return &this->public; return &this->public;
} }
@@ -185,7 +185,6 @@ static void compute_length(private_sa_payload_t *this)
METHOD(payload_t, get_length, size_t, METHOD(payload_t, get_length, size_t,
private_sa_payload_t *this) private_sa_payload_t *this)
{ {
compute_length(this);
return this->payload_length; return this->payload_length;
} }
@@ -203,7 +203,6 @@ static void compute_length (private_transform_substructure_t *this)
*/ */
static size_t get_length(private_transform_substructure_t *this) static size_t get_length(private_transform_substructure_t *this)
{ {
compute_length(this);
return this->transform_length; return this->transform_length;
} }
+1 -1
View File
@@ -206,7 +206,6 @@ static void compute_length (private_ts_payload_t *this)
*/ */
static size_t get_length(private_ts_payload_t *this) static size_t get_length(private_ts_payload_t *this)
{ {
compute_length(this);
return this->payload_length; return this->payload_length;
} }
@@ -233,6 +232,7 @@ static void add_traffic_selector_substructure (private_ts_payload_t *this,traffi
{ {
this->traffic_selectors->insert_last(this->traffic_selectors,traffic_selector); this->traffic_selectors->insert_last(this->traffic_selectors,traffic_selector);
this->number_of_traffic_selectors = this->traffic_selectors->get_count(this->traffic_selectors); this->number_of_traffic_selectors = this->traffic_selectors->get_count(this->traffic_selectors);
compute_length(this);
} }
/** /**