- new configuration support added to ike_sa and states
[strongswan.git] / Source / charon / sa / states / responder_init.c
index 885e0d7..fd730db 100644 (file)
@@ -1,7 +1,7 @@
 /**
  * @file responder_init.c
  * 
 /**
  * @file responder_init.c
  * 
- * @brief Start state of a IKE_SA as responder
+ * @brief Implementation of responder_init_t.
  * 
  */
 
  * 
  */
 
@@ -22,7 +22,7 @@
  
 #include "responder_init.h"
 
  
 #include "responder_init.h"
 
-#include <globals.h>
+#include <daemon.h>
 #include <sa/states/state.h>
 #include <sa/states/ike_sa_init_responded.h>
 #include <utils/allocator.h>
 #include <sa/states/state.h>
 #include <sa/states/ike_sa_init_responded.h>
 #include <utils/allocator.h>
@@ -89,21 +89,18 @@ struct private_responder_init_t {
        logger_t *logger;
        
        /**
        logger_t *logger;
        
        /**
-        * Proposals used to initiate connection
+        * Selected proposal from suggested ones.
         */
         */
-       linked_list_t *proposals;
-       
+       ike_proposal_t selected_proposal;
+
        /**
         * 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.
        /**
         * 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.
-        * @return                      
-        *                                      - SUCCESS
-        *                                      - OUT_OF_RES
         */
         */
-       status_t (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
+       void (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
 
        /**
         * Builds the KE payload for this state.
 
        /**
         * Builds the KE payload for this state.
@@ -111,22 +108,17 @@ struct private_responder_init_t {
         * @param this          calling object
         * @param payload       The generated KE payload object of type ke_payload_t is 
         *                                      stored at this location.
         * @param this          calling object
         * @param payload       The generated KE payload object of type ke_payload_t is 
         *                                      stored at this location.
-        * @return                      
-        *                                      - SUCCESS
-        *                                      - OUT_OF_RES
         */
         */
-       status_t (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
+       void (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
+       
        /**
         * Builds the NONCE payload for this state.
         * 
         * @param this          calling object
         * @param payload       The generated NONCE payload object of type ke_payload_t is 
         *                                      stored at this location.
        /**
         * Builds the NONCE payload for this state.
         * 
         * @param this          calling object
         * @param payload       The generated NONCE payload object of type ke_payload_t is 
         *                                      stored at this location.
-        * @return                      
-        *                                      - SUCCESS
-        *                                      - OUT_OF_RES
         */
         */
-       status_t (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);  
+       void (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);      
        
        /**
         * Destroy function called internally of this class after state change succeeded.
        
        /**
         * Destroy function called internally of this class after state change succeeded.
@@ -134,17 +126,16 @@ struct private_responder_init_t {
         * This destroy function does not destroy objects which were passed to the new state.
         * 
         * @param this          calling object
         * This destroy function does not destroy objects which were passed to the new state.
         * 
         * @param this          calling object
-        * @return                      SUCCESS in any case
         */
         */
-       status_t (*destroy_after_state_change) (private_responder_init_t *this);
+       void (*destroy_after_state_change) (private_responder_init_t *this);
 };
 
 /**
  * Implements state_t.get_state
  */
 };
 
 /**
  * Implements state_t.get_state
  */
-static status_t process_message(private_responder_init_t *this, message_t *message, state_t **new_state)
+static status_t process_message(private_responder_init_t *this, message_t *message)
 {
 {
-       linked_list_iterator_t *payloads;
+       iterator_t *payloads;
        host_t *source, *destination;
        status_t status;
        message_t *response;
        host_t *source, *destination;
        status_t status;
        message_t *response;
@@ -153,6 +144,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
        chunk_t shared_secret;
        exchange_type_t exchange_type;
        ike_sa_init_responded_t *next_state;
        chunk_t shared_secret;
        exchange_type_t exchange_type;
        ike_sa_init_responded_t *next_state;
+       host_t *my_host;
+       host_t *other_host;
+       randomizer_t *randomizer;
+       init_config_t *init_config;
+       diffie_hellman_group_t dh_group = MODP_UNDEFINED;
 
        exchange_type = message->get_exchange_type(message);
        if (exchange_type != IKE_SA_INIT)
 
        exchange_type = message->get_exchange_type(message);
        if (exchange_type != IKE_SA_INIT)
@@ -160,36 +156,43 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,exchange_type));
                return FAILED;
        }
                this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,exchange_type));
                return FAILED;
        }
-       
        if (!message->get_request(message))
        {
                this->logger->log(this->logger, ERROR | MORE, "Only requests of type IKE_SA_INIT supported in state responder_init");
                return FAILED;
        }
        if (!message->get_request(message))
        {
                this->logger->log(this->logger, ERROR | MORE, "Only requests of type IKE_SA_INIT supported in state responder_init");
                return FAILED;
        }
-       
        /* this is the first message we process, so copy host infos */
        /* this is the first message we process, so copy host infos */
-       message->get_source(message, &source);
-       message->get_destination(message, &destination);
+       source = message->get_source(message);
+       destination = message->get_destination(message);
+
+       status = charon->configuration_manager->get_init_config_for_host(charon->configuration_manager,destination,source,&init_config);
+
+       if (status != SUCCESS)
+       {
+               /* no configuration matches given host */
+               this->logger->log(this->logger, ERROR | MORE, "No INIT configuration found for given remote and local hosts");
+               return FAILED;
+       }
+       
+       this->ike_sa->set_init_config(this->ike_sa,init_config);
+       
        
        /* we need to clone them, since we destroy the message later */
        
        /* we need to clone them, since we destroy the message later */
-       destination->clone(destination, &(this->ike_sa->me.host));
-       source->clone(source, &(this->ike_sa->other.host));
+       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);
        
        /* parse incoming message */
        
        /* parse incoming message */
-       status = message->parse_body(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 != SUCCESS)
        {
                this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
-               return status;  
-       }
-
-       /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
-       status = message->get_payload_iterator(message, &payloads);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Fatal error: Could not get payload interator");
                return status;
        }
                return status;
        }
+       /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
+       payloads = message->get_payload_iterator(message);
        
        while (payloads->has_next(payloads))
        {
        
        while (payloads->has_next(payloads))
        {
@@ -204,65 +207,36 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                        case SECURITY_ASSOCIATION:
                        {
                                sa_payload_t *sa_payload = (sa_payload_t*)payload;
                        case SECURITY_ASSOCIATION:
                        {
                                sa_payload_t *sa_payload = (sa_payload_t*)payload;
-                               linked_list_iterator_t *suggested_proposals, *accepted_proposals;
-                               encryption_algorithm_t          encryption_algorithm = ENCR_UNDEFINED;
-                               pseudo_random_function_t                pseudo_random_function = PRF_UNDEFINED;
-                               integrity_algorithm_t           integrity_algorithm = AUTH_UNDEFINED;
-
-                               status = this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE);
+                               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)
                                {
                                if (status != SUCCESS)
                                {
-                                       this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
+                                       this->logger->log(this->logger, ERROR | MORE, "SA payload does not contain IKE proposals");
                                        payloads->destroy(payloads);
                                        return status;  
                                }
                                        payloads->destroy(payloads);
                                        return status;  
                                }
-                               
-                               /* get the list of suggested proposals */ 
-                               status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
-                               if (status != SUCCESS)
-                               {       
-                                       this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on suggested proposals");
-                                       accepted_proposals->destroy(accepted_proposals);
-                                       payloads->destroy(payloads);
-                                       return status;
-                               }
-                               
-                               /* now let the configuration-manager select a subset of the proposals */
-                               status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
-                                                                       this->ike_sa->other.host, suggested_proposals, accepted_proposals);
+       
+                               status = init_config->select_proposal(init_config, ike_proposals,proposal_count,&(this->selected_proposal));
+                               allocator_free(ike_proposals);
                                if (status != SUCCESS)
                                {
                                if (status != SUCCESS)
                                {
-                                       this->logger->log(this->logger, CONTROL | MORE, "No proposal of suggested proposals selected");
-                                       suggested_proposals->destroy(suggested_proposals);
-                                       accepted_proposals->destroy(accepted_proposals);
+                                       this->logger->log(this->logger, ERROR | MORE, "No proposal of suggested proposals selected");
                                        payloads->destroy(payloads);
                                        return status;
                                }
                                
                                        payloads->destroy(payloads);
                                        return status;
                                }
                                
-                               /* iterators are not needed anymore */                  
-                               suggested_proposals->destroy(suggested_proposals);
-                                       
+                               dh_group = this->selected_proposal.diffie_hellman_group;
                                
                                
-                               /* now let the configuration-manager return the transforms for the given proposal*/
-                               this->logger->log(this->logger, CONTROL | MOST, "Get transforms for accepted proposal");
-                               status = global_configuration_manager->get_transforms_for_host_and_proposals(global_configuration_manager,
-                                                                       this->ike_sa->other.host, accepted_proposals, &encryption_algorithm,&pseudo_random_function,&integrity_algorithm);
+                               status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,&(this->selected_proposal));        
                                if (status != SUCCESS)
                                {
                                if (status != SUCCESS)
                                {
-                                       this->logger->log(this->logger, ERROR | MORE, "Accepted proposals not supported?!");
-                                       accepted_proposals->destroy(accepted_proposals);
+                                       this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
                                        payloads->destroy(payloads);
                                        return status;
                                }
                                        payloads->destroy(payloads);
                                        return status;
                                }
-                               accepted_proposals->destroy(accepted_proposals);
-                               
-                               this->ike_sa->prf = prf_create(pseudo_random_function);
-                               if (this->ike_sa->prf == NULL)
-                               {
-                                       this->logger->log(this->logger, ERROR | MORE, "PRF type not supported");
-                                       payloads->destroy(payloads);
-                                       return FAILED;
-                               }
                                
                                this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
                                /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
                                
                                this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
                                /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
@@ -277,12 +251,9 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                                
                                group = ke_payload->get_dh_group_number(ke_payload);
                                
                                
                                group = ke_payload->get_dh_group_number(ke_payload);
                                
-                               status = global_configuration_manager->is_dh_group_allowed_for_host(global_configuration_manager,
-                                                               this->ike_sa->other.host, group, &allowed_group);
-
-                               if (status != SUCCESS)
+                               if (dh_group == MODP_UNDEFINED)
                                {
                                {
-                                       this->logger->log(this->logger, ERROR | MORE, "Could not get informations about DH group");
+                                       this->logger->log(this->logger, ERROR | MORE, "Could not get informations about DH group. SA payload before KE payload?");
                                        payloads->destroy(payloads);
                                        return status;
                                }
                                        payloads->destroy(payloads);
                                        return status;
                                }
@@ -297,19 +268,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                                {
                                        this->logger->log(this->logger, ERROR, "Could not generate DH object");
                                        payloads->destroy(payloads);
                                {
                                        this->logger->log(this->logger, ERROR, "Could not generate DH object");
                                        payloads->destroy(payloads);
-                                       return OUT_OF_RES;
+                                       return NOT_SUPPORTED;
                                }
                                }
-
                                this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
                                
                                this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
                                
-                               status = dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
-                               if (status != SUCCESS)
-                               {
-                                       this->logger->log(this->logger, ERROR, "Could not set other DH public value");
-                                       dh->destroy(dh);
-                                       payloads->destroy(payloads);
-                                       return OUT_OF_RES;
-                               }
+                               dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
 
                                this->diffie_hellman = dh;
                                
 
                                this->diffie_hellman = dh;
                                
@@ -320,22 +283,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                        {
                                nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
 
                        {
                                nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
 
-                               if (this->received_nonce.ptr != NULL)
-                               {
-                                       this->logger->log(this->logger, CONTROL | MOST, "Destroy stored received nonce");
-                                       allocator_free(this->received_nonce.ptr);
-                                       this->received_nonce.ptr = NULL;
-                                       this->received_nonce.len = 0;
-                               }
+                               allocator_free(this->received_nonce.ptr);
+                               this->received_nonce = CHUNK_INITIALIZER;
 
                                this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
 
                                this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
-                               status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
-                               if (status != SUCCESS)
-                               {
-                                       this->logger->log(this->logger, ERROR, "Fatal error: Could not get nonce");
-                                       payloads->destroy(payloads);
-                                       return OUT_OF_RES;
-                               }
+                               nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
                                
                                this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
                                break;
                                
                                this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
                                break;
@@ -344,141 +296,81 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
                        {
                                this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
                                payloads->destroy(payloads);
                        {
                                this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
                                payloads->destroy(payloads);
-                               return OUT_OF_RES;
+                               return NOT_SUPPORTED;
                        }
                        }
-                               
                }
                }
-                       
        }
        /* iterator can be destroyed */
        payloads->destroy(payloads);
        
        this->logger->log(this->logger, CONTROL | MORE, "Request successfully handled. Going to create reply.");
 
        }
        /* 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.");              
-       if (this->ike_sa->randomizer->allocate_pseudo_random_bytes(this->ike_sa->randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not create nonce!");
-               return OUT_OF_RES;
-       }
+       this->logger->log(this->logger, CONTROL | MOST, "Going to create nonce.");      
        
        
+       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");
        status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret);
        this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret);
 
        /* store shared secret  */
        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);
 
-       status = this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
-       if (status != SUCCESS)
-       {
-               /* secrets could not be computed */
-               this->logger->log(this->logger, ERROR | MORE, "Secrets could not be computed!");
-               return status;
-       }
-       
-               
+       this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
 
        /* set up the reply */
 
        /* set up the reply */
-       status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not create empty message");
-               return status;  
-       }
+       this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
+
        
        /* build SA payload */          
        
        /* build SA payload */          
-       status = this->build_sa_payload(this, &payload);
-       if (status != SUCCESS)
-       {       
-               this->logger->log(this->logger, ERROR, "Could not build SA payload");
-               return status;
-       }
-       
-       this    ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
-       status = response->add_payload(response, payload);
-       if (status != SUCCESS)
-       {       
-               this->logger->log(this->logger, ERROR, "Could not add SA payload to message");
-               return status;
-       }
+       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 */
        
        /* build KE payload */
-       status = this->build_ke_payload(this,&payload);
-       if (status != SUCCESS)
-       {       
-               this->logger->log(this->logger, ERROR, "Could not build KE payload");
-               return status;
-       }
-
-       this    ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
-       status = response->add_payload(response, payload);
-       if (status != SUCCESS)
-       {       
-               this->logger->log(this->logger, ERROR, "Could not add KE payload to message");
-               return status;
-       }
+       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 */
        
        /* build Nonce payload */
-       status = this->build_nonce_payload(this, &payload);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
-               return status;
-       }
-
-       this    ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
-       status = response->add_payload(response, payload);
-       if (status != SUCCESS)
-       {       
-               this->logger->log(this->logger, ERROR, "Could not add nonce payload to message");
-               return status;
-       }
+       this->build_nonce_payload(this, &payload);
+       this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
+       response->add_payload(response, payload);
        
        /* generate packet */   
        
        /* generate packet */   
-       this    ->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
-       status = response->generate(response, &packet);
+       this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
+       status = response->generate(response, NULL, NULL, &packet);
        if (status != SUCCESS)
        {
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message");
+               this->logger->log(this->logger, ERROR, "could not generate packet from message");
                return status;
        }
        
                return status;
        }
        
-       this    ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
-       status = global_send_queue->add(global_send_queue, packet);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
-               return status;
-       }
+       this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
+        charon->send_queue->add(charon->send_queue, packet);
 
        /* state can now be changed */
 
        /* 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");
 
        next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce);
 
        next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce);
-
-       if (next_state == NULL)
-       {
-               this    ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_responded_t");
-               allocator_free_chunk(shared_secret);
-               return FAILED;
-       }
        
        
-       if (    this->ike_sa->last_responded_message != NULL)
+       /* last message can now be set */
+       status = this->ike_sa->set_last_responded_message(this->ike_sa, response);
+
+       if (status != SUCCESS)
        {
        {
-               /* destroy message */
-               this    ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last responded message");
-               this->ike_sa->last_responded_message->destroy(this->ike_sa->last_responded_message);
+               this->logger->log(this->logger, ERROR, "Could not set last responded message");
+               response->destroy(response);
+               (next_state->state_interface).destroy(&(next_state->state_interface));
+               return status;
        }
        }
-       this->ike_sa->last_responded_message     = response;
-
-       /* message counter can now be increased */
-       this    ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages");
-       this->ike_sa->message_id_in++;
 
 
-       *new_state = (state_t *) next_state;
+       /* state can now be changed */
+       this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state);
        /* state has NOW changed :-) */
        /* state has NOW changed :-) */
-       this    ->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) );
+       this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) );
 
 
-       this    ->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
+       this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
        this->destroy_after_state_change(this); 
        
        return SUCCESS;
        this->destroy_after_state_change(this); 
        
        return SUCCESS;
@@ -487,142 +379,53 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
 /**
  * implements private_initiator_init_t.build_sa_payload
  */
 /**
  * implements private_initiator_init_t.build_sa_payload
  */
-static status_t build_sa_payload(private_responder_init_t *this, payload_t **payload)
+static void build_sa_payload(private_responder_init_t *this, payload_t **payload)
 {
        sa_payload_t* sa_payload;
 {
        sa_payload_t* sa_payload;
-       linked_list_iterator_t *proposal_iterator;
-       status_t status;
-       
        
        /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
        
        /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
-
-       this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
-       
-       status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
-               return status;  
-       }
        
        
-       sa_payload = sa_payload_create();
-       if (sa_payload == NULL)
-       {
-               this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
-               return OUT_OF_RES;
-       }
-       
-       while (proposal_iterator->has_next(proposal_iterator))
-       {
-               proposal_substructure_t *current_proposal;
-               proposal_substructure_t *current_proposal_clone;
-               status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
-               if (status != SUCCESS)
-               {
-                       this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
-                       proposal_iterator->destroy(proposal_iterator);
-                       sa_payload->destroy(sa_payload);
-                       return status;  
-               }
-               status = current_proposal->clone(current_proposal,&current_proposal_clone);
-               if (status != SUCCESS)
-               {
-                       this->logger->log(this->logger, ERROR, "Could not clone current proposal");
-                       proposal_iterator->destroy(proposal_iterator);
-                       sa_payload->destroy(sa_payload);
-                       return status;  
-               }
-               
-               status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
-               if (status != SUCCESS)
-               {
-                       this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
-                       proposal_iterator->destroy(proposal_iterator);
-                       sa_payload->destroy(sa_payload);
-                       return status;  
-               }
-
-       }
-
-       proposal_iterator->destroy(proposal_iterator);  
+       this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
        
        
-       this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
+       sa_payload = sa_payload_create_from_ike_proposals(&(this->selected_proposal),1);        
        
        *payload = (payload_t *) sa_payload;
        
        *payload = (payload_t *) sa_payload;
-       
-       return SUCCESS;
 }
 
 /**
  * implements private_initiator_init_t.build_ke_payload
  */
 }
 
 /**
  * implements private_initiator_init_t.build_ke_payload
  */
-static status_t build_ke_payload(private_responder_init_t *this, payload_t **payload)
+static void build_ke_payload(private_responder_init_t *this, payload_t **payload)
 {
        ke_payload_t *ke_payload;
        chunk_t key_data;
 {
        ke_payload_t *ke_payload;
        chunk_t key_data;
-       status_t status;
 
        this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
 
        this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
-       
-
-       this    ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
-       status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not get my DH public value");
-               return status;
-       }
+       this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
 
        ke_payload = ke_payload_create();
 
        ke_payload = ke_payload_create();
-       if (ke_payload == NULL)
-       {
-               this->logger->log(this->logger, ERROR, "Could not create KE payload");
-               allocator_free_chunk(key_data);
-               return OUT_OF_RES;      
-       }
+       ke_payload->set_key_exchange_data(ke_payload,key_data);
        ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
        ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
-       if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
-               ke_payload->destroy(ke_payload);
-               allocator_free_chunk(key_data);
-               return OUT_OF_RES;
-       }
-       allocator_free_chunk(key_data);
 
 
+       allocator_free_chunk(&key_data);
        *payload = (payload_t *) ke_payload;
        *payload = (payload_t *) ke_payload;
-       return SUCCESS;                 
 }
 
 /**
  * implements private_initiator_init_t.build_nonce_payload
  */
 }
 
 /**
  * implements private_initiator_init_t.build_nonce_payload
  */
-static status_t build_nonce_payload(private_responder_init_t *this, payload_t **payload)
+static void build_nonce_payload(private_responder_init_t *this, payload_t **payload)
 {
        nonce_payload_t *nonce_payload;
        status_t status;
        
        this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
 {
        nonce_payload_t *nonce_payload;
        status_t status;
        
        this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
-
+       
        nonce_payload = nonce_payload_create();
        nonce_payload = nonce_payload_create();
-       if (nonce_payload == NULL)
-       {       
-               this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
-               return OUT_OF_RES;      
-       }
-
+       
        status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
        
        status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
        
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
-               nonce_payload->destroy(nonce_payload);
-               return status;
-       }
-               
        *payload = (payload_t *) nonce_payload;
        *payload = (payload_t *) nonce_payload;
-       
-       return SUCCESS;
 }
 
 
 }
 
 
@@ -637,61 +440,32 @@ static ike_sa_state_t get_state(private_responder_init_t *this)
 /**
  * Implements state_t.get_state
  */
 /**
  * Implements state_t.get_state
  */
-static status_t destroy(private_responder_init_t *this)
+static void destroy(private_responder_init_t *this)
 {
        this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object");
        
        /* destroy stored proposal */
        this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
 {
        this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object");
        
        /* destroy stored proposal */
        this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
-       while (this->proposals->get_count(this->proposals) > 0)
-       {
-               proposal_substructure_t *current_proposal;
-               this->proposals->remove_first(this->proposals,(void **)&current_proposal);
-               current_proposal->destroy(current_proposal);
-       }
-       this->proposals->destroy(this->proposals);
        
        
-       if (this->sent_nonce.ptr != NULL)
-       {
-               this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
-               allocator_free(this->sent_nonce.ptr);
-       }
+       allocator_free(this->sent_nonce.ptr);
+       allocator_free(this->received_nonce.ptr);
 
 
-       if (this->received_nonce.ptr != NULL)
-       {
-               this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
-               allocator_free(this->received_nonce.ptr);
-       }
-       
-       /* destroy diffie hellman object */
        if (this->diffie_hellman != NULL)
        {
        if (this->diffie_hellman != NULL)
        {
-               this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
                this->diffie_hellman->destroy(this->diffie_hellman);
        }
                this->diffie_hellman->destroy(this->diffie_hellman);
        }
-       
        allocator_free(this);
        allocator_free(this);
-               
-       return SUCCESS;
-       
 }
 
 /**
  * Implements private_responder_init_t.destroy_after_state_change
  */
 }
 
 /**
  * Implements private_responder_init_t.destroy_after_state_change
  */
-static status_t destroy_after_state_change (private_responder_init_t *this)
+static void destroy_after_state_change (private_responder_init_t *this)
 {
        this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object");
        
        /* destroy stored proposal */
        this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
 {
        this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object");
        
        /* destroy stored proposal */
        this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
-       while (this->proposals->get_count(this->proposals) > 0)
-       {
-               proposal_substructure_t *current_proposal;
-               this->proposals->remove_first(this->proposals,(void **)&current_proposal);
-               current_proposal->destroy(current_proposal);
-       }
-       this->proposals->destroy(this->proposals);
        
        /* destroy diffie hellman object */
        if (this->diffie_hellman != NULL)
        
        /* destroy diffie hellman object */
        if (this->diffie_hellman != NULL)
@@ -701,7 +475,6 @@ static status_t destroy_after_state_change (private_responder_init_t *this)
        }
        
        allocator_free(this);
        }
        
        allocator_free(this);
-       return SUCCESS;
 }
 
 /* 
 }
 
 /* 
@@ -710,16 +483,11 @@ static status_t destroy_after_state_change (private_responder_init_t *this)
 responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
 {
        private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
 responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
 {
        private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
-       
-       if (this == NULL)
-       {
-               return NULL;
-       }
 
        /* interface functions */
 
        /* interface functions */
-       this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
+       this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
        this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
        this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
-       this->public.state_interface.destroy  = (status_t (*) (state_t *)) destroy;
+       this->public.state_interface.destroy  = (void (*) (state_t *)) destroy;
        
        /* private functions */
        this->build_sa_payload = build_sa_payload;
        
        /* private functions */
        this->build_sa_payload = build_sa_payload;
@@ -729,17 +497,9 @@ responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
        
        /* private data */
        this->ike_sa = ike_sa;
        
        /* private data */
        this->ike_sa = ike_sa;
-       this->logger = this->ike_sa->logger;
-       this->sent_nonce.ptr = NULL;
-       this->sent_nonce.len = 0;
-       this->received_nonce.ptr = NULL;
-       this->received_nonce.len = 0;
-       this->proposals = linked_list_create();
-       if (this->proposals == NULL)
-       {
-               allocator_free(this);
-               return NULL;
-       }
-       
+       this->logger = this->ike_sa->get_logger(this->ike_sa);
+       this->sent_nonce = CHUNK_INITIALIZER;
+       this->received_nonce = CHUNK_INITIALIZER;
+
        return &(this->public);
 }
        return &(this->public);
 }