implemented and tested functionality to create sa_payload from

ike_proposal_t's and also generate ike_proposal_t's from sa_payload
This commit is contained in:
Jan Hutter
2005-12-01 08:48:57 +00:00
parent d45ec1dedf
commit b737e9d9e8
12 changed files with 518 additions and 12 deletions
@@ -144,7 +144,7 @@ static status_t verify(private_proposal_substructure_t *this)
status_t status = SUCCESS;
iterator_t *iterator;
if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != PROPOSAL_SUBSTRUCTURE))
if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != 2))
{
/* must be 0 or 2 */
return FAILED;
@@ -250,6 +250,15 @@ static void add_transform_substructure (private_proposal_substructure_t *this,tr
this->compute_length(this);
}
/**
* Implementation of proposal_substructure_t.proposal_substructure_t.
*/
static void set_is_last_proposal (private_proposal_substructure_t *this, bool is_last)
{
this->next_payload = (is_last) ? 0: PROPOSAL_TYPE_VALUE;
}
/**
* Implementation of proposal_substructure_t.set_proposal_number.
*/
@@ -373,6 +382,22 @@ static void compute_length (private_proposal_substructure_t *this)
}
/**
* Implementation of proposal_substructure_t.get_transform_count.
*/
static size_t get_transform_count (private_proposal_substructure_t *this)
{
return this->transforms->get_count(this->transforms);
}
/**
* Implementation of proposal_substructure_t.get_spi_size.
*/
static size_t get_spi_size (private_proposal_substructure_t *this)
{
return this->spi.len;
}
/**
* Implementation of proposal_substructure_t.clone.
*/
@@ -464,8 +489,12 @@ proposal_substructure_t *proposal_substructure_create()
this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
this->public.get_info_for_transform_type = (status_t (*) (proposal_substructure_t *,transform_type_t,u_int16_t *, u_int16_t *))get_info_for_transform_type;
this->public.set_is_last_proposal = (void (*) (proposal_substructure_t *,bool)) set_is_last_proposal;
this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
this->public.get_transform_count = (size_t (*) (proposal_substructure_t *)) get_transform_count;
this->public.get_spi_size = (size_t (*) (proposal_substructure_t *)) get_spi_size;
this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone;
this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
@@ -28,6 +28,13 @@
#include <encoding/payloads/transform_substructure.h>
#include <utils/linked_list.h>
/**
* IKEv1 Value for a proposal payload.
*
* @ingroup payloads
*/
#define PROPOSAL_TYPE_VALUE 2
/**
* Length of the proposal substructure header
* (without spi).
@@ -106,6 +113,22 @@ struct proposal_substructure_t {
*/
u_int8_t (*get_proposal_number) (proposal_substructure_t *this);
/**
* @brief get the number of transforms in current proposal.
*
* @param this calling proposal_substructure_t object
* @return transform count in current proposal
*/
size_t (*get_transform_count) (proposal_substructure_t *this);
/**
* @brief get size of the set spi in bytes.
*
* @param this calling proposal_substructure_t object
* @return size of the spi in bytes
*/
size_t (*get_spi_size) (proposal_substructure_t *this);
/**
* @brief Sets the protocol id of current proposal.
*
@@ -136,6 +159,16 @@ struct proposal_substructure_t {
*/
status_t (*get_info_for_transform_type) (proposal_substructure_t *this,transform_type_t type, u_int16_t *transform_id, u_int16_t *key_length);
/**
* @brief Sets the next_payload field of this substructure
*
* If this is the last proposal, next payload field is set to 0,
* otherwise to 2
*
* @param this calling proposal_substructure_t object
* @param is_last When TRUE, next payload field is set to 0, otherwise to 2
*/
void (*set_is_last_proposal) (proposal_substructure_t *this, bool is_last);
/**
* @brief Returns the currently set SPI of this proposal.
@@ -243,10 +243,156 @@ static iterator_t *create_proposal_substructure_iterator (private_sa_payload_t *
*/
static void add_proposal_substructure (private_sa_payload_t *this,proposal_substructure_t *proposal)
{
status_t status;
if (this->proposals->get_count(this->proposals) > 0)
{
proposal_substructure_t *last_proposal;
status = this->proposals->get_last(this->proposals,(void **) &last_proposal);
/* last transform is now not anymore last one */
last_proposal->set_is_last_proposal(last_proposal,FALSE);
}
proposal->set_is_last_proposal(proposal,TRUE);
this->proposals->insert_last(this->proposals,(void *) proposal);
this->compute_length(this);
}
/**
* Implementation of sa_payload_t.get_ike_proposals.
*/
static status_t get_ike_proposals (private_sa_payload_t *this,ike_proposal_t ** proposals, size_t *proposal_count)
{
int found_ike_proposals = 0;
int current_proposal_number = 0;
iterator_t *iterator;
ike_proposal_t *tmp_proposals;
iterator = this->proposals->create_iterator(this->proposals,TRUE);
/* first find out the number of ike proposals and check their number of transforms and
* if the SPI is empty!*/
while (iterator->has_next(iterator))
{
proposal_substructure_t *current_proposal;
iterator->current(iterator,(void **)&(current_proposal));
if (current_proposal->get_protocol_id(current_proposal) == IKE)
{
/* a ike proposal consists of 4 transforms and an empty spi*/
if ((current_proposal->get_transform_count(current_proposal) != 4) ||
(current_proposal->get_spi_size(current_proposal) != 0))
{
iterator->destroy(iterator);
return FAILED;
}
found_ike_proposals++;
}
}
iterator->reset(iterator);
if (found_ike_proposals == 0)
{
iterator->destroy(iterator);
return NOT_FOUND;
}
/* allocate memory to hold each proposal as ike_proposal_t */
tmp_proposals = allocator_alloc(found_ike_proposals * sizeof(ike_proposal_t));
/* create from each proposal_substructure a ike_proposal_t data area*/
while (iterator->has_next(iterator))
{
proposal_substructure_t *current_proposal;
iterator->current(iterator,(void **)&(current_proposal));
if (current_proposal->get_protocol_id(current_proposal) == IKE)
{
bool encryption_algorithm_found = FALSE;
bool integrity_algorithm_found = FALSE;
bool pseudo_random_function_found = FALSE;
bool diffie_hellman_group_found = FALSE;
status_t status;
iterator_t *transforms;
transforms = current_proposal->create_transform_substructure_iterator(current_proposal,TRUE);
while (transforms->has_next(transforms))
{
transform_substructure_t *current_transform;
transforms->current(transforms,(void **)&(current_transform));
switch (current_transform->get_transform_type(current_transform))
{
case ENCRYPTION_ALGORITHM:
{
tmp_proposals[current_proposal_number].encryption_algorithm = current_transform->get_transform_id(current_transform);
status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].encryption_algorithm_key_length));
if (status == SUCCESS)
{
encryption_algorithm_found = TRUE;
}
break;
}
case INTEGRITY_ALGORITHM:
{
tmp_proposals[current_proposal_number].integrity_algorithm = current_transform->get_transform_id(current_transform);
status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].integrity_algorithm_key_length));
if (status == SUCCESS)
{
integrity_algorithm_found = TRUE;
}
break;
}
case PSEUDO_RANDOM_FUNCTION:
{
tmp_proposals[current_proposal_number].pseudo_random_function = current_transform->get_transform_id(current_transform);
status = current_transform->get_key_length(current_transform,&(tmp_proposals[current_proposal_number].pseudo_random_function_key_length));
if (status == SUCCESS)
{
pseudo_random_function_found = TRUE;
}
break;
}
case DIFFIE_HELLMAN_GROUP:
{
tmp_proposals[current_proposal_number].diffie_hellman_group = current_transform->get_transform_id(current_transform);
diffie_hellman_group_found = TRUE;
break;
}
default:
{
/* not a transform of an ike proposal. Break here */
break;
}
}
}
transforms->destroy(transforms);
if ((!encryption_algorithm_found) ||
(!integrity_algorithm_found) ||
(!pseudo_random_function_found) ||
(!diffie_hellman_group_found))
{
/* one of needed transforms could not be found */
iterator->reset(iterator);
allocator_free(tmp_proposals);
return FAILED;
}
current_proposal_number++;
}
}
iterator->destroy(iterator);
*proposals = tmp_proposals;
*proposal_count = found_ike_proposals;
return SUCCESS;
}
/**
* Implementation of private_sa_payload_t.compute_length.
*/
@@ -285,6 +431,7 @@ sa_payload_t *sa_payload_create()
/* public functions */
this->public.create_proposal_substructure_iterator = (iterator_t* (*) (sa_payload_t *,bool)) create_proposal_substructure_iterator;
this->public.add_proposal_substructure = (void (*) (sa_payload_t *,proposal_substructure_t *)) add_proposal_substructure;
this->public.get_ike_proposals = (status_t (*) (sa_payload_t *, ike_proposal_t **, size_t *)) get_ike_proposals;
this->public.destroy = (void (*) (sa_payload_t *)) destroy;
/* private functions */
@@ -299,4 +446,43 @@ sa_payload_t *sa_payload_create()
return (&(this->public));
}
/*
* Described in header.
*/
sa_payload_t *sa_payload_create_from_ike_proposals(ike_proposal_t *proposals, size_t proposal_count)
{
int i;
sa_payload_t *sa_payload= sa_payload_create();
for (i = 0; i < proposal_count; i++)
{
proposal_substructure_t *proposal_substructure;
transform_substructure_t *encryption_algorithm;
transform_substructure_t *integrity_algorithm;
transform_substructure_t *pseudo_random_function;
transform_substructure_t *diffie_hellman_group;
/* create proposal substructure */
proposal_substructure = proposal_substructure_create();
proposal_substructure->set_protocol_id(proposal_substructure,IKE);
proposal_substructure->set_proposal_number(proposal_substructure,(i + 1));
/* create transform substructures to hold each specific transform for an ike proposal */
encryption_algorithm = transform_substructure_create_type(ENCRYPTION_ALGORITHM,proposals[i].encryption_algorithm,proposals[i].encryption_algorithm_key_length);
proposal_substructure->add_transform_substructure(proposal_substructure,encryption_algorithm);
pseudo_random_function = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION,proposals[i].pseudo_random_function,proposals[i].pseudo_random_function_key_length);
proposal_substructure->add_transform_substructure(proposal_substructure,pseudo_random_function);
integrity_algorithm = transform_substructure_create_type(INTEGRITY_ALGORITHM,proposals[i].integrity_algorithm,proposals[i].integrity_algorithm_key_length);
proposal_substructure->add_transform_substructure(proposal_substructure,integrity_algorithm);
diffie_hellman_group = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP,proposals[i].diffie_hellman_group,0);
proposal_substructure->add_transform_substructure(proposal_substructure,diffie_hellman_group);
/* add proposal to sa payload */
sa_payload->add_proposal_substructure(sa_payload,proposal_substructure);
}
return sa_payload;
}
@@ -27,6 +27,7 @@
#include <encoding/payloads/payload.h>
#include <encoding/payloads/proposal_substructure.h>
#include <utils/linked_list.h>
#include <config/init_config.h>
/**
* Critical flag must not be set.
@@ -82,6 +83,22 @@ struct sa_payload_t {
* @param proposal proposal_substructure_t object to add
*/
void (*add_proposal_substructure) (sa_payload_t *this,proposal_substructure_t *proposal);
/**
* Creates an array of ike_proposal_t's in this SA payload.
*
* An IKE proposal consist of transform of type ENCRYPTION_ALGORITHM,
* PSEUDO_RANDOM_FUNCTION, INTEGRITY_ALGORITHM and DIFFIE_HELLMAN_GROUP
*
* @param proposals the pointer to the first entry of ike_proposal_t's is set
* @param proposal_count the number of found proposals is written at this location
* @return
* - SUCCESS if an IKE proposal could be found
* - NOT_FOUND if no IKE proposal could be found
* - FAILED if a proposal does not contain all needed transforms
* for a IKE_PROPOSAL
*/
status_t (*get_ike_proposals) (sa_payload_t *this, ike_proposal_t **proposals, size_t *proposal_count);
/**
* @brief Destroys an sa_payload_t object.
@@ -100,5 +117,15 @@ struct sa_payload_t {
*/
sa_payload_t *sa_payload_create();
/**
* @brief Creates a sa_payload_t object from array of ike_proposal_t's.
*
* @return created sa_payload_t object
* @param proposals pointer to first proposal in array of type ike_proposal_t
* @param proposal_count number of ike_proposal_t's in array
*
* @ingroup payloads
*/
sa_payload_t *sa_payload_create_from_ike_proposals(ike_proposal_t *proposals, size_t proposal_count);
#endif /*SA_PAYLOAD_H_*/
@@ -322,3 +322,13 @@ transform_attribute_t *transform_attribute_create()
return (&(this->public));
}
/*
* Described in header.
*/
transform_attribute_t *transform_attribute_create_key_length(u_int16_t key_length)
{
transform_attribute_t *attribute = transform_attribute_create();
attribute->set_attribute_type(attribute,KEY_LENGTH);
attribute->set_value(attribute,key_length);
return attribute;
}
@@ -132,11 +132,23 @@ struct transform_attribute_t {
};
/**
* @brief Creates an empty transform_attribute_t object
* @brief Creates an empty transform_attribute_t object.
*
* @return created transform_attribute_t object
*
* @ingroup payloads
*/
transform_attribute_t *transform_attribute_create();
/**
* @brief Creates an transform_attribute_t of type KEY_LENGTH.
*
* @param key_length key length in bytes
* @return created transform_attribute_t object
*
* @ingroup payloads
*/
transform_attribute_t *transform_attribute_create_key_length(u_int16_t key_length);
#endif /*TRANSFORM_ATTRIBUTE_H_*/
@@ -448,7 +448,7 @@ static void destroy(private_transform_substructure_t *this)
}
/*
* Described in header
* Described in header.
*/
transform_substructure_t *transform_substructure_create()
{
@@ -488,3 +488,31 @@ transform_substructure_t *transform_substructure_create()
return (&(this->public));
}
/*
* Described in header
*/
transform_substructure_t *transform_substructure_create_type(transform_type_t transform_type, u_int16_t transform_id, u_int16_t key_length)
{
transform_substructure_t *transform = transform_substructure_create();
transform->set_transform_type(transform,transform_type);
transform->set_transform_id(transform,transform_id);
switch (transform_type)
{
case ENCRYPTION_ALGORITHM:
case PSEUDO_RANDOM_FUNCTION:
case INTEGRITY_ALGORITHM:
{
transform_attribute_t *attribute = transform_attribute_create_key_length(key_length);
transform->add_transform_attribute(transform,attribute);
break;
}
default:
{
/* no keylength attribute is created */
}
}
return transform;
}
@@ -137,7 +137,7 @@ struct transform_substructure_t {
* @brief Sets the next_payload field of this substructure
*
* If this is the last transform, next payload field is set to 0,
* otherwise to 3 (payload type of transform in IKEv1)
* otherwise to 3
*
* @param this calling transform_substructure_t object
* @param is_last When TRUE, next payload field is set to 0, otherwise to 3
@@ -213,7 +213,7 @@ struct transform_substructure_t {
};
/**
* @brief Creates an empty transform_substructure_t object
* @brief Creates an empty transform_substructure_t object.
*
* @return created transform_substructure_t object
*
@@ -221,4 +221,20 @@ struct transform_substructure_t {
*/
transform_substructure_t *transform_substructure_create();
/**
* @brief Creates an empty transform_substructure_t object.
*
* The key length is used for the transport types ENCRYPTION_ALGORITHM,
* PSEUDO_RANDOM_FUNCTION, INTEGRITY_ALGORITHM. For all
* other transport types the key_length parameter is not used
*
* @return created transform_substructure_t object
* @param transform_type type of transform to create
* @param transform_id transform id specifying the specific algorithm of a transform type
* @param key_length Key length for key lenght attribute
*
* @ingroup payloads
*/
transform_substructure_t *transform_substructure_create_type(transform_type_t transform_type, u_int16_t transform_id, u_int16_t key_length);
#endif /*TRANSFORM_SUBSTRUCTURE_H_*/