- code cleaned

This commit is contained in:
Jan Hutter 2005-12-06 08:13:32 +00:00
parent 34852cae00
commit 0d74d84bd4
2 changed files with 213 additions and 222 deletions

View File

@ -52,14 +52,11 @@ struct private_responder_init_t {
/**
* Diffie Hellman object used to compute shared secret.
*
* After processing of incoming IKE_SA_INIT-Request the shared key is
* passed to the next state of type ike_sa_init_responded_t.
*/
diffie_hellman_t *diffie_hellman;
/**
* Diffie Hellman group number.
* Diffie Hellman group number from selected IKE proposal.
*/
u_int16_t dh_group_number;
@ -71,66 +68,61 @@ struct private_responder_init_t {
/**
* Sent nonce value.
*
* This value is passed to the next state of type ike_sa_init_responded_t.
* This value is passed to the next state of type IKE_SA_INIT_RESPONDED.
*/
chunk_t sent_nonce;
/**
* Received nonce value
*
* This value is passed to the next state of type ike_sa_init_responded_t.
* This value is passed to the next state of type IKE_SA_INIT_RESPONDED.
*/
chunk_t received_nonce;
/**
* Logger used to log data
* Logger used to log data .
*
* Is logger of ike_sa!
*/
logger_t *logger;
/**
* Selected proposal from suggested ones.
* Handles received SA payload and builds the SA payload for the response.
*
* @param this calling object
* @param sa_request The received SA payload
* @param response the SA payload is added to this response message_t object.
* @return
* - DELETE_ME
* - SUCCESS
*/
ike_proposal_t selected_proposal;
status_t (*build_sa_payload) (private_responder_init_t *this,sa_payload_t *sa_request, message_t *response);
/**
* Builds the IKE_SA_INIT reply message
* Handles received KE payload and builds the KE payload for the response.
*
* @param this calling object
* @param message the message will be written to this location.
* @param ke_request The received KE payload
* @param response the KE payload is added to this response message_t object.
* - DELETE_ME
* - SUCCESS
*/
void (*build_ike_sa_init_reply) (private_responder_init_t *this, message_t **message);
/**
* Builds the SA payload for this state.
*
* @param this calling object
* @param payload The generated SA payload object of type ke_payload_t is
* stored at this location.
*/
void (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
/**
* Builds the KE payload for this state.
*
* @param this calling object
* @param payload The generated KE payload object of type ke_payload_t is
* stored at this location.
*/
void (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
status_t (*build_ke_payload) (private_responder_init_t *this,ke_payload_t *ke_request, message_t *response);
/**
* Builds the NONCE payload for this state.
* Handles received NONCE payload and builds the NONCE payload for the response.
*
* @param this calling object
* @param payload The generated NONCE payload object of type ke_payload_t is
* stored at this location.
* @param this calling object
* @param nonce_request The received NONCE payload
* @param response the NONCE payload is added to this response message_t object.
* - DELETE_ME
* - SUCCESS
*/
void (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);
status_t (*build_nonce_payload) (private_responder_init_t *this,nonce_payload_t *nonce_request, message_t *response);
/**
* Destroy function called internally of this class after state change succeeded.
* Destroy function called internally of this class after change
* to state IKE_SA_INIT_RESPONDED succeeded.
*
* This destroy function does not destroy objects which were passed to the new state.
*
@ -139,7 +131,7 @@ struct private_responder_init_t {
void (*destroy_after_state_change) (private_responder_init_t *this);
/**
* Sends a IKE_SA_INIT reply with a notify payload.
* Sends a IKE_SA_INIT reply containing a notify payload.
*
* @param this calling object
* @param type type of notify message
@ -150,29 +142,26 @@ struct private_responder_init_t {
};
/**
* Implements state_t.get_state
* Implementation of state_t.process_message.
*/
static status_t process_message(private_responder_init_t *this, message_t *message)
{
ike_sa_init_responded_t *next_state;
chunk_t ike_sa_init_response_data;
chunk_t ike_sa_init_request_data;
exchange_type_t exchange_type;
nonce_payload_t *nonce_request;
host_t *source, *destination;
init_config_t *init_config;
randomizer_t *randomizer;
sa_payload_t *sa_request;
ke_payload_t *ke_request;
chunk_t shared_secret;
iterator_t *payloads;
message_t *response;
host_t *other_host;
host_t *my_host;
status_t status;
exchange_type = message->get_exchange_type(message);
if (exchange_type != IKE_SA_INIT)
if (message->get_exchange_type(message) != IKE_SA_INIT)
{
this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,exchange_type));
this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,message->get_exchange_type(message)));
return DELETE_ME;
}
if (!message->get_request(message))
@ -180,7 +169,8 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
this->logger->log(this->logger, ERROR | MORE, "Only requests of type IKE_SA_INIT supported in state responder_init");
return DELETE_ME;
}
/* this is the first message we process, so get host infos */
/* this is the first message to process, so get host infos */
source = message->get_source(message);
destination = message->get_destination(message);
@ -189,143 +179,60 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
{
/* no configuration matches given host */
this->logger->log(this->logger, ERROR | MORE, "No INIT configuration found for given remote and local hosts");
this->send_notify_reply(this,NO_PROPOSAL_CHOSEN,CHUNK_INITIALIZER);
return DELETE_ME;
}
this->ike_sa->set_init_config(this->ike_sa,init_config);
/* we need to clone them, since we destroy the message later */
my_host = destination->clone(destination);
other_host = source->clone(source);
this->ike_sa->set_my_host(this->ike_sa, my_host);
this->ike_sa->set_other_host(this->ike_sa, other_host);
this->ike_sa->set_my_host(this->ike_sa, destination->clone(destination));
this->ike_sa->set_other_host(this->ike_sa, source->clone(source));
/* parse incoming message */
status = message->parse_body(message, NULL, NULL);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
if (status == NOT_SUPPORTED)
{
this->logger->log(this->logger, ERROR | MORE, "Message contains unsupported payload with critical flag set");
/**
* TODO send unsupported type.
*/
this->send_notify_reply(this,UNSUPPORTED_CRITICAL_PAYLOAD,CHUNK_INITIALIZER);
}
else
{
this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
}
return DELETE_ME;
}
/* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
payloads = message->get_payload_iterator(message);
payloads = message->get_payload_iterator(message);
while (payloads->has_next(payloads))
{
payload_t *payload;
/* get current payload */
payloads->current(payloads, (void**)&payload);
this->logger->log(this->logger, CONTROL|MORE, "Processing payload of type %s", mapping_find(payload_type_m, payload->get_type(payload)));
switch (payload->get_type(payload))
{
case SECURITY_ASSOCIATION:
{
sa_payload_t *sa_payload = (sa_payload_t*)payload;
ike_proposal_t *ike_proposals;
size_t proposal_count;
/* get the list of suggested proposals */
status = sa_payload->get_ike_proposals (sa_payload, &ike_proposals,&proposal_count);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "SA payload does not contain IKE proposals");
payloads->destroy(payloads);
return DELETE_ME;
}
status = init_config->select_proposal(init_config, ike_proposals,proposal_count,&(this->selected_proposal));
allocator_free(ike_proposals);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "No proposal of suggested proposals selected");
payloads->destroy(payloads);
this->send_notify_reply(this,NO_PROPOSAL_CHOSEN,CHUNK_INITIALIZER);
return DELETE_ME;
}
this->dh_group_number = this->selected_proposal.diffie_hellman_group;
status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,&(this->selected_proposal));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
payloads->destroy(payloads);
return DELETE_ME;
}
this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
sa_request = (sa_payload_t*)payload;
break;
}
case KEY_EXCHANGE:
{
ke_payload_t *ke_payload = (ke_payload_t*)payload;
diffie_hellman_group_t group;
diffie_hellman_t *dh;
group = ke_payload->get_dh_group_number(ke_payload);
if (group == MODP_UNDEFINED)
{
this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group set to undefined!");
payloads->destroy(payloads);
return DELETE_ME;
}
if (this->dh_group_number != group)
{
u_int16_t accepted_group;
chunk_t accepted_group_chunk;
/* group not same as selected one
* Maybe key exchange payload is before SA payload */
this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group not as in selected proposal!");
payloads->destroy(payloads);
accepted_group = htons(this->dh_group_number);
accepted_group_chunk.ptr = (u_int8_t*) &(accepted_group);
accepted_group_chunk.len = 2;
this->send_notify_reply(this,INVALID_KE_PAYLOAD,accepted_group_chunk);
return DELETE_ME;
}
/* create diffie hellman object to handle DH exchange */
dh = diffie_hellman_create(group);
if (dh == NULL)
{
this->logger->log(this->logger, ERROR, "Could not generate DH object with group %d",mapping_find(diffie_hellman_group_m,group) );
payloads->destroy(payloads);
return DELETE_ME;
}
this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
this->diffie_hellman = dh;
this->logger->log(this->logger, CONTROL | MORE, "KE Payload processed.");
ke_request = (ke_payload_t*)payload;
break;
}
case NONCE:
{
nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
allocator_free(this->received_nonce.ptr);
this->received_nonce = CHUNK_INITIALIZER;
this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
nonce_request = (nonce_payload_t*)payload;
break;
}
case NOTIFY:
{
notify_payload_t *notify_payload = (notify_payload_t *) payload;
this->logger->log(this->logger, CONTROL|MORE, "Process notify type %s for protocol %s",
mapping_find(notify_message_type_m, notify_payload->get_notify_message_type(notify_payload)),
mapping_find(protocol_id_m, notify_payload->get_protocol_id(notify_payload)));
@ -334,7 +241,7 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
{
this->logger->log(this->logger, ERROR | MORE, "Notify not for IKE protocol.");
payloads->destroy(payloads);
return FAILED;
return DELETE_ME;
}
switch (notify_payload->get_notify_message_type(notify_payload))
{
@ -348,34 +255,50 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
}
default:
{
this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
payloads->destroy(payloads);
return DELETE_ME;
this->logger->log(this->logger, CONTROL | MORE, "Processing of Payload with Type number %d not implemented",payload->get_type(payload));
break;
}
}
}
/* iterator can be destroyed */
payloads->destroy(payloads);
this->logger->log(this->logger, CONTROL | MORE, "Request successfully handled. Going to create reply.");
this->logger->log(this->logger, CONTROL | MOST, "Going to create nonce.");
this->logger->log(this->logger, CONTROL | MORE, "Going to process received payloads");
this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
status = this->build_sa_payload(this, sa_request, response);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Building SA payload failed");
response->destroy(response);
return status;
}
status = this->build_ke_payload(this, ke_request, response);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Building KE payload failed");
response->destroy(response);
return status;
}
status = this->build_nonce_payload(this, nonce_request, response);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Building NONCE payload failed");
response->destroy(response);
return status;
}
randomizer = this->ike_sa->get_randomizer(this->ike_sa);
randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
/* store shared secret */
this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it.");
this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret);
this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret);
this->logger->log_chunk(this->logger, PRIVATE, "Shared Diffie Hellman secret", &shared_secret);
this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
/* not used anymore */
allocator_free_chunk(&shared_secret);
this->build_ike_sa_init_reply(this,&response);
/* message can now be sent (must not be destroyed) */
status = this->ike_sa->send_response(this->ike_sa, response);
if (status != SUCCESS)
@ -386,7 +309,7 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
}
/* state can now be changed */
this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
this->logger->log(this->logger, CONTROL|MOST, "Create next state object of type IKE_SA_INIT_RESPONDED");
response = this->ike_sa->get_last_responded_message(this->ike_sa);
ike_sa_init_response_data = response->get_packet_data(response);
@ -405,59 +328,107 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
return SUCCESS;
}
/**
* implements private_responder_init_t.build_ike_sa_init_reply
* Implementation of private_initiator_init_t.build_sa_payload.
*/
static void build_ike_sa_init_reply (private_responder_init_t *this, message_t **message)
{
message_t *response;
payload_t *payload;
this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
/* set up the reply */
this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
/* build SA payload */
this->build_sa_payload(this, &payload);
this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
response->add_payload(response, payload);
/* build KE payload */
this->build_ke_payload(this,&payload);
this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
response->add_payload(response, payload);
/* build Nonce payload */
this->build_nonce_payload(this, &payload);
this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
response->add_payload(response, payload);
*message = response;
}
/**
* implements private_initiator_init_t.build_sa_payload
*/
static void build_sa_payload(private_responder_init_t *this, payload_t **payload)
static status_t build_sa_payload(private_responder_init_t *this,sa_payload_t *sa_request, message_t *response)
{
ike_proposal_t selected_proposal;
ike_proposal_t *ike_proposals;
init_config_t *init_config;
sa_payload_t* sa_payload;
size_t proposal_count;
status_t status;
init_config = this->ike_sa->get_init_config(this->ike_sa);
this->logger->log(this->logger, CONTROL | MOST, "Process received SA payload");
/* get the list of suggested proposals */
status = sa_request->get_ike_proposals (sa_request, &ike_proposals,&proposal_count);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "SA payload does not contain IKE proposals");
this->send_notify_reply(this,NO_PROPOSAL_CHOSEN,CHUNK_INITIALIZER);
return DELETE_ME;
}
status = init_config->select_proposal(init_config, ike_proposals,proposal_count,&(selected_proposal));
allocator_free(ike_proposals);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "No proposal of suggested proposals selected");
this->send_notify_reply(this,NO_PROPOSAL_CHOSEN,CHUNK_INITIALIZER);
return DELETE_ME;
}
this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
sa_payload = sa_payload_create_from_ike_proposals(&(this->selected_proposal),1);
this->dh_group_number = selected_proposal.diffie_hellman_group;
*payload = (payload_t *) sa_payload;
status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,&(selected_proposal));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
return DELETE_ME;
}
this->logger->log(this->logger, CONTROL | MOST, "SA Payload processed");
this->logger->log(this->logger, CONTROL|MOST, "Building SA payload");
sa_payload = sa_payload_create_from_ike_proposals(&(selected_proposal),1);
this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
response->add_payload(response,(payload_t *) sa_payload);
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_ke_payload
* Implementation of private_initiator_init_t.build_ke_payload.
*/
static void build_ke_payload(private_responder_init_t *this, payload_t **payload)
static status_t build_ke_payload(private_responder_init_t *this,ke_payload_t *ke_request, message_t *response)
{
diffie_hellman_group_t group;
ke_payload_t *ke_payload;
diffie_hellman_t *dh;
chunk_t key_data;
this->logger->log(this->logger, CONTROL | MOST, "Process received KE payload");
group = ke_request->get_dh_group_number(ke_request);
if (group == MODP_UNDEFINED)
{
this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group set to undefined!");
return DELETE_ME;
}
if (this->dh_group_number != group)
{
u_int16_t accepted_group;
chunk_t accepted_group_chunk;
/* group not same as selected one
* Maybe key exchange payload is before SA payload */
this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group not as in selected proposal!");
accepted_group = htons(this->dh_group_number);
accepted_group_chunk.ptr = (u_int8_t*) &(accepted_group);
accepted_group_chunk.len = 2;
this->send_notify_reply(this,INVALID_KE_PAYLOAD,accepted_group_chunk);
return DELETE_ME;
}
/* create diffie hellman object to handle DH exchange */
dh = diffie_hellman_create(group);
if (dh == NULL)
{
this->logger->log(this->logger, ERROR, "Could not generate DH object with group %d",mapping_find(diffie_hellman_group_m,group) );
return DELETE_ME;
}
this->logger->log(this->logger, CONTROL | MOST, "Set other DH public value");
dh->set_other_public_value(dh, ke_request->get_key_exchange_data(ke_request));
this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
this->diffie_hellman = dh;
this->logger->log(this->logger, CONTROL | MOST, "KE Payload processed.");
this->logger->log(this->logger, CONTROL|MOST, "Building KE payload");
this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
ke_payload = ke_payload_create();
@ -465,26 +436,45 @@ static void build_ke_payload(private_responder_init_t *this, payload_t **payload
ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
allocator_free_chunk(&key_data);
*payload = (payload_t *) ke_payload;
this->logger->log(this->logger, CONTROL|MOST, "Add KE payload to message");
response->add_payload(response,(payload_t *) ke_payload);
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_nonce_payload
* Implementation of private_initiator_init_t.build_nonce_payload.
*/
static void build_nonce_payload(private_responder_init_t *this, payload_t **payload)
static status_t build_nonce_payload(private_responder_init_t *this,nonce_payload_t *nonce_request, message_t *response)
{
nonce_payload_t *nonce_payload;
randomizer_t *randomizer;
this->logger->log(this->logger, CONTROL | MOST, "Process received NONCE payload");
allocator_free(this->received_nonce.ptr);
this->received_nonce = CHUNK_INITIALIZER;
this->logger->log(this->logger, CONTROL | MOST, "Get NONCE value and store it");
nonce_request->get_nonce(nonce_request, &(this->received_nonce));
this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
this->logger->log(this->logger, CONTROL | MOST, "Create new NONCE value.");
randomizer = this->ike_sa->get_randomizer(this->ike_sa);
randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
this->logger->log(this->logger, CONTROL|MOST, "Building NONCE payload");
nonce_payload = nonce_payload_create();
nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
*payload = (payload_t *) nonce_payload;
this->logger->log(this->logger, CONTROL|MOST, "Add NONCE payload to message");
response->add_payload(response,(payload_t *) nonce_payload);
return SUCCESS;
}
/**
* Implements state_t.get_state
* Implementation of state_t.get_state.
*/
static ike_sa_state_t get_state(private_responder_init_t *this)
{
@ -530,7 +520,7 @@ static void send_notify_reply (private_responder_init_t *this,notify_message_typ
}
/**
* Implements state_t.get_state
* Implementation of state_t.destroy.
*/
static void destroy(private_responder_init_t *this)
{
@ -551,7 +541,7 @@ static void destroy(private_responder_init_t *this)
}
/**
* Implements private_responder_init_t.destroy_after_state_change
* Implementation of private_responder_init_t.destroy_after_state_change
*/
static void destroy_after_state_change (private_responder_init_t *this)
{
@ -581,7 +571,6 @@ responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private functions */
this->build_ike_sa_init_reply = build_ike_sa_init_reply;
this->build_sa_payload = build_sa_payload;
this->build_ke_payload = build_ke_payload;
this->build_nonce_payload = build_nonce_payload;

View File

@ -30,25 +30,27 @@
typedef struct responder_init_t responder_init_t;
/**
* @brief This class represents an IKE_SA state when initializing.
* a connection as responder.
* @brief This class represents an IKE_SA state when
* initializing a connection as responder.
*
* @b Constructors:
* - responder_init_create()
*
* @ingroup states
*/
struct responder_init_t {
/**
* methods of the state_t interface
* The state_t interface.
*/
state_t state_interface;
};
/**
* Constructor of class responder_init_t
* Constructor of class responder_init_t.
*
* @param ike_sa assigned IKE_SA
* @param ike_sa assigned IKE_SA
*
* @return responder_init state
* @return responder_init_t object
*
* @ingroup states
*/