diff options
Diffstat (limited to 'Source/charon/sa')
-rw-r--r-- | Source/charon/sa/ike_sa.c | 405 | ||||
-rw-r--r-- | Source/charon/sa/ike_sa.h | 53 | ||||
-rw-r--r-- | Source/charon/sa/ike_sa_id.c | 79 | ||||
-rw-r--r-- | Source/charon/sa/ike_sa_id.h | 86 | ||||
-rw-r--r-- | Source/charon/sa/ike_sa_manager.c | 332 | ||||
-rw-r--r-- | Source/charon/sa/ike_sa_manager.h | 31 | ||||
-rw-r--r-- | Source/charon/sa/states/ike_auth_requested.c | 10 | ||||
-rw-r--r-- | Source/charon/sa/states/ike_sa_established.c | 10 | ||||
-rw-r--r-- | Source/charon/sa/states/ike_sa_init_requested.c | 102 | ||||
-rw-r--r-- | Source/charon/sa/states/ike_sa_init_responded.c | 7 | ||||
-rw-r--r-- | Source/charon/sa/states/ike_sa_init_responded.h | 2 | ||||
-rw-r--r-- | Source/charon/sa/states/initiator_init.c | 281 | ||||
-rw-r--r-- | Source/charon/sa/states/responder_init.c | 309 | ||||
-rw-r--r-- | Source/charon/sa/states/responder_init.h | 8 | ||||
-rw-r--r-- | Source/charon/sa/states/state.h | 13 |
15 files changed, 458 insertions, 1270 deletions
diff --git a/Source/charon/sa/ike_sa.c b/Source/charon/sa/ike_sa.c index 2b1fe9421..33a7fd388 100644 --- a/Source/charon/sa/ike_sa.c +++ b/Source/charon/sa/ike_sa.c @@ -206,14 +206,13 @@ struct private_ike_sa_t { /** - * @brief implements function process_message of protected_ike_sa_t + * Implements protected_ike_sa_t.process_message. */ static status_t process_message (private_ike_sa_t *this, message_t *message) -{ +{ u_int32_t message_id; exchange_type_t exchange_type; bool is_request; - status_t status; /* we must process each request or response from remote host */ @@ -228,7 +227,7 @@ static status_t process_message (private_ike_sa_t *this, message_t *message) /* * It has to be checked, if the message has to be resent cause of lost packets! */ - if (is_request && ( message_id == (this->message_id_in - 1))) + if (is_request && (message_id == (this->message_id_in - 1))) { /* message can be resent ! */ this->logger->log(this->logger, CONTROL|MORE, "Resent message detected. Send stored reply"); @@ -257,61 +256,34 @@ static status_t process_message (private_ike_sa_t *this, message_t *message) /* now the message is processed by the current state object */ /* the current state does change the current change to the next one*/ - status = this->current_state->process_message(this->current_state,message); - - return status; + return this->current_state->process_message(this->current_state,message); } /** - * @brief Implements function build_message of protected_ike_sa_t. + * Implements protected_ike_sa_t.build_message. */ -static status_t build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message) +static void build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message) { - status_t status; message_t *new_message; host_t *source, *destination; this->logger->log(this->logger, CONTROL|MORE, "build empty message"); new_message = message_create(); - if (new_message == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not create empty message object"); - return OUT_OF_RES; - } - - status = this->me.host->clone(this->me.host, &source); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not clone my host information"); - new_message->destroy(new_message); - return status; - } - status = this->other.host->clone(this->other.host, &destination); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not clone other host information"); - source->destroy(source); - new_message->destroy(new_message); - return status; - } + source = this->me.host->clone(this->me.host); + destination = this->other.host->clone(this->other.host); + new_message->set_source(new_message, source); new_message->set_destination(new_message, destination); - new_message->set_exchange_type(new_message, type); new_message->set_request(new_message, request); - new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in); - new_message->set_ike_sa_id(new_message, this->ike_sa_id); - *message = new_message; - - return SUCCESS; } /** - * @brief implements function process_configuration of protected_ike_sa_t + * Implements protected_ike_sa_t.process_configuration. */ static status_t initialize_connection(private_ike_sa_t *this, char *name) { @@ -336,7 +308,7 @@ static status_t initialize_connection(private_ike_sa_t *this, char *name) } /** - * @brief implements function protected_ike_sa_t.get_id + * Implements protected_ike_sa_t.get_id. */ static ike_sa_id_t* get_id(private_ike_sa_t *this) { @@ -344,14 +316,13 @@ static ike_sa_id_t* get_id(private_ike_sa_t *this) } /** - * @brief implements function protected_ike_sa_t.compute_secrets + * Implements protected_ike_sa_t.compute_secrets. */ -static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce) +static void compute_secrets(private_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce) { chunk_t concatenated_nonces; chunk_t skeyseed; chunk_t prf_plus_seed; - status_t status; u_int64_t initiator_spi; u_int64_t responder_spi; prf_plus_t *prf_plus; @@ -362,11 +333,7 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret */ concatenated_nonces.len = (initiator_nonce.len + responder_nonce.len); concatenated_nonces.ptr = allocator_alloc(concatenated_nonces.len); - if (concatenated_nonces.ptr == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for concatenated nonces"); - return FAILED; - } + /* first is initiator */ memcpy(concatenated_nonces.ptr,initiator_nonce.ptr,initiator_nonce.len); /* second is responder */ @@ -374,28 +341,15 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret this->logger->log_chunk(this->logger, RAW, "Nonce data", &concatenated_nonces); - /* status of set_key is not checked */ - status = this->prf->set_key(this->prf,concatenated_nonces); + this->prf->set_key(this->prf,concatenated_nonces); + + this->prf->allocate_bytes(this->prf,dh_shared_secret,&skeyseed); - status = this->prf->allocate_bytes(this->prf,dh_shared_secret,&skeyseed); - if (status != SUCCESS) - { - allocator_free_chunk(&concatenated_nonces); - this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate bytes for skeyseed"); - return status; - } allocator_free_chunk(&concatenated_nonces); prf_plus_seed.len = (initiator_nonce.len + responder_nonce.len + 16); prf_plus_seed.ptr = allocator_alloc(prf_plus_seed.len); - if (prf_plus_seed.ptr == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for prf+ seed"); - allocator_free_chunk(&skeyseed); - return FAILED; - } - /* first is initiator */ memcpy(prf_plus_seed.ptr,initiator_nonce.ptr,initiator_nonce.len); @@ -412,113 +366,45 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret this->logger->log_chunk(this->logger, PRIVATE | MORE, "PRF+ Seed", &prf_plus_seed); this->logger->log(this->logger, CONTROL | MOST, "Set new key of prf object"); - status = this->prf->set_key(this->prf,skeyseed); + this->prf->set_key(this->prf,skeyseed); allocator_free_chunk(&skeyseed); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for prf+ seed"); - allocator_free_chunk(&prf_plus_seed); - return FAILED; - } - + this->logger->log(this->logger, CONTROL | MOST, "Create new prf+ object"); prf_plus = prf_plus_create(this->prf, prf_plus_seed); allocator_free_chunk(&prf_plus_seed); - if (prf_plus == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal errror: prf+ object could not be created"); - return FAILED; - } - status = prf_plus->allocate_bytes(prf_plus,this->prf->get_block_size(this->prf),&(this->secrets.d_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_d"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->prf->get_block_size(this->prf),&(this->secrets.d_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", &(this->secrets.d_key)); - status = prf_plus->allocate_bytes(prf_plus,this->crypter_initiator->get_block_size(this->crypter_initiator),&(this->secrets.ei_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ei"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->crypter_initiator->get_block_size(this->crypter_initiator),&(this->secrets.ei_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", &(this->secrets.ei_key)); - status = this->crypter_initiator->set_key(this->crypter_initiator,this->secrets.ei_key); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not set encryption key initiator crypter"); - return status; - } + this->crypter_initiator->set_key(this->crypter_initiator,this->secrets.ei_key); - status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.er_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_er"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.er_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", &(this->secrets.er_key)); - status = this->crypter_responder->set_key(this->crypter_responder,this->secrets.er_key); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not set encryption key responder crypter"); - return status; - } + this->crypter_responder->set_key(this->crypter_responder,this->secrets.er_key); - status = prf_plus->allocate_bytes(prf_plus,this->signer_initiator->get_block_size(this->signer_initiator),&(this->secrets.ai_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ai"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->signer_initiator->get_block_size(this->signer_initiator),&(this->secrets.ai_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", &(this->secrets.ai_key)); - status = this->signer_initiator->set_key(this->signer_initiator,this->secrets.ai_key); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not set key for initiator signer"); - return status; - } + this->signer_initiator->set_key(this->signer_initiator,this->secrets.ai_key); - status = prf_plus->allocate_bytes(prf_plus,this->signer_responder->get_block_size(this->signer_responder),&(this->secrets.ar_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ar"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->signer_responder->get_block_size(this->signer_responder),&(this->secrets.ar_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", &(this->secrets.ar_key)); - status = this->signer_responder->set_key(this->signer_responder,this->secrets.ar_key); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not set key for responder signer"); - return status; - } + this->signer_responder->set_key(this->signer_responder,this->secrets.ar_key); - status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pi_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_pi"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pi_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", &(this->secrets.pi_key)); - status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pr_key)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_pr"); - return status; - } + prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pr_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", &(this->secrets.pr_key)); prf_plus->destroy(prf_plus); - - return SUCCESS; } /** - * @brief implements function resend_last_reply of protected_ike_sa_t + * Implements protected_ike_sa_t.resend_last_reply. */ -status_t resend_last_reply (private_ike_sa_t *this) +static status_t resend_last_reply(private_ike_sa_t *this) { packet_t *packet; status_t status; @@ -530,37 +416,22 @@ status_t resend_last_reply (private_ike_sa_t *this) return status; } - 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"); - packet->destroy(packet); - return status; - } + global_send_queue->add(global_send_queue, packet); return SUCCESS; } -status_t create_delete_job (private_ike_sa_t *this) +/** + * Implements protected_ike_sa_t.resend_last_reply. + */ +static status_t create_delete_job(private_ike_sa_t *this) { job_t *delete_job; - status_t status; this->logger->log(this->logger, CONTROL | MORE, "Going to create job to delete this IKE_SA"); delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id); - if (delete_job == NULL) - { - this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created"); - return FAILED; - } - - status = global_job_queue->add(global_job_queue,delete_job); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status)); - delete_job->destroy_all(delete_job); - return status; - } + global_job_queue->add(global_job_queue,delete_job); + return SUCCESS; } @@ -636,96 +507,87 @@ static status_t create_transforms_from_proposal (private_ike_sa_t *this,proposal u_int16_t pseudo_random_function; u_int16_t pseudo_random_function_key_length; - this ->logger->log(this->logger, CONTROL|MORE, "Going to create transform objects for proposal"); + this->logger->log(this->logger, CONTROL|MORE, "Going to create transform objects for proposal"); - this ->logger->log(this->logger, CONTROL|MOST, "Get encryption transform type"); + this->logger->log(this->logger, CONTROL|MOST, "Get encryption transform type"); status = proposal->get_info_for_transform_type(proposal,ENCRYPTION_ALGORITHM,&(encryption_algorithm),&(encryption_algorithm_key_length)); if (status != SUCCESS) { - this ->logger->log(this->logger, ERROR|MORE, "Could not get encryption transform type"); + this->logger->log(this->logger, ERROR|MORE, "Could not get encryption transform type"); return status; } - this ->logger->log(this->logger, CONTROL|MORE, "Encryption algorithm: %s with keylength %d",mapping_find(encryption_algorithm_m,encryption_algorithm),encryption_algorithm_key_length); + this->logger->log(this->logger, CONTROL|MORE, "Encryption algorithm: %s with keylength %d",mapping_find(encryption_algorithm_m,encryption_algorithm),encryption_algorithm_key_length); - this ->logger->log(this->logger, CONTROL|MOST, "Get integrity transform type"); + this->logger->log(this->logger, CONTROL|MOST, "Get integrity transform type"); status = proposal->get_info_for_transform_type(proposal,INTEGRITY_ALGORITHM,&(integrity_algorithm),&(integrity_algorithm_key_length)); if (status != SUCCESS) { - this ->logger->log(this->logger, ERROR|MORE, "Could not get integrity transform type"); + this->logger->log(this->logger, ERROR|MORE, "Could not get integrity transform type"); return status; } - this ->logger->log(this->logger, CONTROL|MORE, "integrity algorithm: %s with keylength %d",mapping_find(integrity_algorithm_m,integrity_algorithm),integrity_algorithm_key_length); + this->logger->log(this->logger, CONTROL|MORE, "integrity algorithm: %s with keylength %d",mapping_find(integrity_algorithm_m,integrity_algorithm),integrity_algorithm_key_length); - this ->logger->log(this->logger, CONTROL|MOST, "Get prf transform type"); + this->logger->log(this->logger, CONTROL|MOST, "Get prf transform type"); status = proposal->get_info_for_transform_type(proposal,PSEUDO_RANDOM_FUNCTION,&(pseudo_random_function),&(pseudo_random_function_key_length)); if (status != SUCCESS) { - this ->logger->log(this->logger, ERROR|MORE, "Could not prf transform type"); + this->logger->log(this->logger, ERROR|MORE, "Could not prf transform type"); return status; } - this ->logger->log(this->logger, CONTROL|MORE, "prf: %s with keylength %d",mapping_find(pseudo_random_function_m,pseudo_random_function),pseudo_random_function_key_length); - - + this->logger->log(this->logger, CONTROL|MORE, "prf: %s with keylength %d",mapping_find(pseudo_random_function_m,pseudo_random_function),pseudo_random_function_key_length); - if (this->prf != NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing prf_t object"); this->prf->destroy(this->prf); } this->prf = prf_create(pseudo_random_function); if (this->prf == NULL) { - this ->logger->log(this->logger, ERROR|MORE, "prf does not seem to be supported!"); + this->logger->log(this->logger, ERROR|MORE, "prf not supported!"); return FAILED; } if (this->crypter_initiator != NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing initiator crypter_t object"); this->crypter_initiator->destroy(this->crypter_initiator); } this->crypter_initiator = crypter_create(encryption_algorithm,encryption_algorithm_key_length); if (this->crypter_initiator == NULL) { - this ->logger->log(this->logger, ERROR|MORE, "encryption algorithm does not seem to be supported!"); + this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!"); return FAILED; } if (this->crypter_responder != NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing responder crypter_t object"); this->crypter_responder->destroy(this->crypter_responder); } this->crypter_responder = crypter_create(encryption_algorithm,encryption_algorithm_key_length); if (this->crypter_responder == NULL) { - this ->logger->log(this->logger, ERROR|MORE, "encryption algorithm does not seem to be supported!"); + this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!"); return FAILED; } if (this->signer_initiator != NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing initiator signer_t object"); this->signer_initiator->destroy(this->signer_initiator); } this->signer_initiator = signer_create(integrity_algorithm); if (this->signer_initiator == NULL) { - this ->logger->log(this->logger, ERROR|MORE, "integrity algorithm does not seem to be supported!"); + this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!"); return FAILED; } - if (this->signer_responder != NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing responder signer_t object"); this->signer_responder->destroy(this->signer_responder); } this->signer_responder = signer_create(integrity_algorithm); if (this->signer_responder == NULL) { - this ->logger->log(this->logger, ERROR|MORE, "integrity algorithm does not seem to be supported!"); + this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!"); return FAILED; } @@ -745,50 +607,47 @@ static randomizer_t *get_randomizer (private_ike_sa_t *this) */ static status_t set_last_requested_message (private_ike_sa_t *this,message_t * message) { - if ( this->last_requested_message != NULL) + if (this->last_requested_message != NULL) { /* destroy message */ - this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last requested message"); this->last_requested_message->destroy(this->last_requested_message); } if (message->get_message_id(message) != this->message_id_out) { - this ->logger->log(this->logger, CONTROL|MOST, "last requested message could not be set cause id was not as expected"); + this->logger->log(this->logger, CONTROL|MOST, "last requested message could not be set cause id was not as expected"); return FAILED; } - this ->logger->log(this->logger, CONTROL|MOST, "replace last requested message with new one"); - this->last_requested_message = message; + this->logger->log(this->logger, CONTROL|MOST, "replace last requested message with new one"); + this->last_requested_message = message; /* message counter can now be increased */ - this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages"); + this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages"); this->message_id_out++; return SUCCESS; } - /** * Implementation of protected_ike_sa_t.set_last_responded_message. */ static status_t set_last_responded_message (private_ike_sa_t *this,message_t * message) { - if ( this->last_responded_message != NULL) + if (this->last_responded_message != NULL) { /* destroy message */ - this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last responded message"); this->last_responded_message->destroy(this->last_responded_message); } if (message->get_message_id(message) != this->message_id_in) { - this ->logger->log(this->logger, CONTROL|MOST, "last responded message could not be set cause id was not as expected"); + this->logger->log(this->logger, CONTROL|MOST, "last responded message could not be set cause id was not as expected"); return FAILED; } - this ->logger->log(this->logger, CONTROL|MOST, "replace last responded message with new one"); - this->last_responded_message = message; + this->logger->log(this->logger, CONTROL|MOST, "replace last responded message with new one"); + this->last_responded_message = message; /* message counter can now be increased */ - this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages"); + this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages"); this->message_id_in++; return SUCCESS; @@ -796,11 +655,10 @@ static status_t set_last_responded_message (private_ike_sa_t *this,message_t * m /** - * @brief implements function destroy of protected_ike_sa_t + * Implements protected_ike_sa_t.destroy. */ -static status_t destroy (private_ike_sa_t *this) +static void destroy (private_ike_sa_t *this) { - this->logger->log(this->logger, CONTROL | MORE, "Going to destroy IKE_SA"); /* destroy child sa's */ @@ -808,7 +666,7 @@ static status_t destroy (private_ike_sa_t *this) while (this->child_sas->get_count(this->child_sas) > 0) { void *child_sa; - if (this->child_sas->remove_first(this->child_sas,&child_sa) != SUCCESS) + if (this->child_sas->remove_first(this->child_sas, &child_sa) != SUCCESS) { break; } @@ -817,95 +675,64 @@ static status_t destroy (private_ike_sa_t *this) this->child_sas->destroy(this->child_sas); this->logger->log(this->logger, CONTROL | MOST, "Destroy secrets"); - if (this->secrets.d_key.ptr != NULL) - { - allocator_free(this->secrets.d_key.ptr); - } - if (this->secrets.ai_key.ptr != NULL) - { - allocator_free(this->secrets.ai_key.ptr); - } - if (this->secrets.ar_key.ptr != NULL) - { - allocator_free(this->secrets.ar_key.ptr); - } - if (this->secrets.ei_key.ptr != NULL) - { - allocator_free(this->secrets.ei_key.ptr); - } - if (this->secrets.er_key.ptr != NULL) - { - allocator_free(this->secrets.er_key.ptr); - } - if (this->secrets.pi_key.ptr != NULL) - { - allocator_free(this->secrets.pi_key.ptr); - } - if (this->secrets.pr_key.ptr != NULL) - { - allocator_free(this->secrets.pr_key.ptr); - } - if ( this->crypter_initiator != NULL) + allocator_free(this->secrets.d_key.ptr); + allocator_free(this->secrets.ai_key.ptr); + allocator_free(this->secrets.ar_key.ptr); + allocator_free(this->secrets.ei_key.ptr); + allocator_free(this->secrets.er_key.ptr); + allocator_free(this->secrets.pi_key.ptr); + allocator_free(this->secrets.pr_key.ptr); + + if (this->crypter_initiator != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy initiator crypter"); this->crypter_initiator->destroy(this->crypter_initiator); } - if ( this->crypter_responder != NULL) + if (this->crypter_responder != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy responder crypter"); this->crypter_responder->destroy(this->crypter_responder); } - if ( this->signer_initiator != NULL) + if (this->signer_initiator != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy initiator signer"); this->signer_initiator->destroy(this->signer_initiator); } if (this->signer_responder != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy responder signer"); this->signer_responder->destroy(this->signer_responder); } if (this->prf != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy prf"); this->prf->destroy(this->prf); } /* destroy ike_sa_id */ - this->logger->log(this->logger, CONTROL | MOST, "Destroy assigned ike_sa_id"); this->ike_sa_id->destroy(this->ike_sa_id); /* destroy stored requested message */ if (this->last_requested_message != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy last requested message"); this->last_requested_message->destroy(this->last_requested_message); } /* destroy stored responded messages */ if (this->last_responded_message != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy last responded message"); this->last_responded_message->destroy(this->last_responded_message); } - this->logger->log(this->logger, CONTROL | MOST, "Destroy randomizer"); this->randomizer->destroy(this->randomizer); if (this->me.host != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy host informations of me"); this->me.host->destroy(this->me.host); } if (this->other.host != NULL) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy host informations of other"); this->other.host->destroy(this->other.host); } @@ -913,11 +740,9 @@ static status_t destroy (private_ike_sa_t *this) this->current_state->destroy(this->current_state); this->logger->log(this->logger, CONTROL | MOST, "Destroy logger of IKE_SA"); - global_logger_manager->destroy_logger(global_logger_manager, this->logger); allocator_free(this); - return SUCCESS; } /* @@ -926,20 +751,16 @@ static status_t destroy (private_ike_sa_t *this) ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id) { private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t); - if (this == NULL) - { - return NULL; - } /* Public functions */ this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message; this->protected.public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection; this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id; - this->protected.public.destroy = (status_t(*)(ike_sa_t*))destroy; + this->protected.public.destroy = (void(*)(ike_sa_t*))destroy; /* protected functions */ - this->protected.build_message = (status_t (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message; - this->protected.compute_secrets = (status_t (*) (protected_ike_sa_t *,chunk_t ,chunk_t , chunk_t )) compute_secrets; + this->protected.build_message = (void (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message; + this->protected.compute_secrets = (void (*) (protected_ike_sa_t *,chunk_t ,chunk_t , chunk_t )) compute_secrets; this->protected.get_logger = (logger_t *(*) (protected_ike_sa_t *)) get_logger; this->protected.get_my_host = (host_t *(*) (protected_ike_sa_t *)) get_my_host; this->protected.get_other_host = (host_t *(*) (protected_ike_sa_t *)) get_other_host; @@ -958,36 +779,10 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id) /* initialize private fields */ this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL); - if (this->logger == NULL) - { - allocator_free(this); - } - if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not clone ike_sa_id"); - global_logger_manager->destroy_logger(global_logger_manager,this->logger); - allocator_free(this); - return NULL; - } + this->ike_sa_id = ike_sa_id->clone(ike_sa_id); this->child_sas = linked_list_create(); - if (this->child_sas == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's"); - this->ike_sa_id->destroy(this->ike_sa_id); - global_logger_manager->destroy_logger(global_logger_manager,this->logger); - allocator_free(this); - return NULL; - } this->randomizer = randomizer_create(); - if (this->randomizer == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's"); - this->child_sas->destroy(this->child_sas); - this->ike_sa_id->destroy(this->ike_sa_id); - global_logger_manager->destroy_logger(global_logger_manager,this->logger); - allocator_free(this); - } this->me.host = NULL; this->other.host = NULL; @@ -995,29 +790,19 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id) this->last_responded_message = NULL; this->message_id_out = 0; this->message_id_in = 0; - this->secrets.d_key.ptr = NULL; - this->secrets.d_key.len = 0; - this->secrets.ai_key.ptr = NULL; - this->secrets.ai_key.len = 0; - this->secrets.ar_key.ptr = NULL; - this->secrets.ar_key.len = 0; - this->secrets.ei_key.ptr = NULL; - this->secrets.ei_key.len = 0; - this->secrets.er_key.ptr = NULL; - this->secrets.er_key.len = 0; - this->secrets.pi_key.ptr = NULL; - this->secrets.pi_key.len = 0; - this->secrets.pr_key.ptr = NULL; - this->secrets.pr_key.len = 0; + this->secrets.d_key = CHUNK_INITIALIZER; + this->secrets.ai_key = CHUNK_INITIALIZER; + this->secrets.ar_key = CHUNK_INITIALIZER; + this->secrets.ei_key = CHUNK_INITIALIZER; + this->secrets.er_key = CHUNK_INITIALIZER; + this->secrets.pi_key = CHUNK_INITIALIZER; + this->secrets.pr_key = CHUNK_INITIALIZER; this->crypter_initiator = NULL; this->crypter_responder = NULL; this->signer_initiator = NULL; this->signer_responder = NULL; this->prf = NULL; - - - /* at creation time, IKE_SA is in a initiator state */ if (ike_sa_id->is_initiator(ike_sa_id)) { @@ -1027,17 +812,5 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id) { this->current_state = (state_t *) responder_init_create(&(this->protected)); } - - if (this->current_state == NULL) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not create state object"); - this->child_sas->destroy(this->child_sas); - this->ike_sa_id->destroy(this->ike_sa_id); - global_logger_manager->destroy_logger(global_logger_manager,this->logger); - this->randomizer->destroy(this->randomizer); - allocator_free(this); - } - - return &(this->protected.public); } diff --git a/Source/charon/sa/ike_sa.h b/Source/charon/sa/ike_sa.h index 8ea445b4e..9545822fd 100644 --- a/Source/charon/sa/ike_sa.h +++ b/Source/charon/sa/ike_sa.h @@ -1,8 +1,7 @@ /** * @file ike_sa.h * - * @brief Class ike_sa_t. An object of this type is managed by an - * ike_sa_manager_t object and represents an IKE_SA + * @brief Interface of ike_sa_id_t. * */ @@ -45,8 +44,8 @@ typedef struct ike_sa_t ike_sa_t; /** - * @brief This class is used to represent an IKE_SA - * + * @brief Class ike_sa_t. An object of this type is managed by an + * ike_sa_manager_t object and represents an IKE_SA. */ struct ike_sa_t { @@ -60,7 +59,7 @@ struct ike_sa_t { status_t (*process_message) (ike_sa_t *this,message_t *message); /** - * Initiate a new connection with given configuration name + * @brief Initiate a new connection with given configuration name. * * @param this calling object * @param name name of the configuration @@ -69,26 +68,28 @@ struct ike_sa_t { status_t (*initialize_connection) (ike_sa_t *this, char *name); /** - * @brief Get the id of the SA + * @brief Get the id of the SA. * - * @param this ike_sa_t-message_t object object - * @return ike_sa's ike_sa_id_t + * @param this ike_sa_t object object + * @return ike_sa's ike_sa_id_t */ ike_sa_id_t* (*get_id) (ike_sa_t *this); /** - * @brief Destroys a ike_sa_t object + * @brief Destroys a ike_sa_t object. * - * @param this ike_sa_t object - * @return SUCCESSFUL if succeeded, FAILED otherwise + * @param this ike_sa_t object */ - status_t (*destroy) (ike_sa_t *this); + void (*destroy) (ike_sa_t *this); }; typedef struct protected_ike_sa_t protected_ike_sa_t; /** - * Protected data of an ike_sa_t object + * @brief Protected data of an ike_sa_t object. + * + * This members should only be accessed from + * the varius state classes. */ struct protected_ike_sa_t { @@ -105,15 +106,12 @@ struct protected_ike_sa_t { * * Used in every state. * - * @param this calling object - * @param type exchange type of new message - * @param request TRUE, if message has to be a request - * @param message new message is stored at this location - * @return - * - SUCCESS - * - OUT_OF_RES + * @param this calling object + * @param type exchange type of new message + * @param request TRUE, if message has to be a request + * @param message new message is stored at this location */ - status_t (*build_message) (protected_ike_sa_t *this, exchange_type_t type, bool request, message_t **message); + void (*build_message) (protected_ike_sa_t *this, exchange_type_t type, bool request, message_t **message); /** * Initiate a new connection with given configuration name @@ -122,9 +120,8 @@ struct protected_ike_sa_t { * @param dh_shared_secret shared secret of diffie hellman exchange * @param initiator_nonce nonce of initiator * @param responder_nonce nonce of responder - * @return TODO */ - status_t (*compute_secrets) (protected_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce); + void (*compute_secrets) (protected_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce); /** * Gets the internal stored logger_t object for given ike_sa_t object. @@ -234,14 +231,14 @@ struct protected_ike_sa_t { /** * Creates an ike_sa_t object with a specific ike_sa_id_t object * - * @param[in] ike_sa_id ike_sa_id_t object to associate with new IKE_SA. - * The object is internal getting cloned - * and so has to be destroyed by the caller. + * @param[in] ike_sa_id ike_sa_id_t object to associate with new IKE_SA. + * The object is internal getting cloned + * and so has to be destroyed by the caller. * * @warning the Content of internal ike_sa_id_t object can change over time - * e.g. when a IKE_SA_INIT has been finished + * e.g. when a IKE_SA_INIT has been finished. * - * @return created ike_sa_t object + * @return created ike_sa_t object */ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id); diff --git a/Source/charon/sa/ike_sa_id.c b/Source/charon/sa/ike_sa_id.c index 542959558..f5c93cf47 100644 --- a/Source/charon/sa/ike_sa_id.c +++ b/Source/charon/sa/ike_sa_id.c @@ -1,7 +1,7 @@ /** * @file ike_sa_id.c * - * @brief Class for identification of an IKE_SA + * @brief Implementation of ike_sa_id_t. * */ @@ -39,9 +39,6 @@ struct private_ike_sa_id_t { */ ike_sa_id_t public; - - /* Private values */ - /** * SPI of Initiator */ @@ -61,22 +58,23 @@ struct private_ike_sa_id_t { /** - * @brief implements function set_responder_spi of ike_sa_id_t + * implements ike_sa_id_t.set_responder_spi. */ -static status_t set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi) +static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi) { this->responder_spi = responder_spi; - return SUCCESS; } -static status_t set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi) +/** + * implements ike_sa_id_t.set_initiator_spi. + */ +static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi) { this->initiator_spi = initiator_spi; - return SUCCESS; } /** - * @brief implements ike_sa_id_t.get_initiator_spi + * implements ike_sa_id_t.get_initiator_spi. */ static u_int64_t get_initiator_spi (private_ike_sa_id_t *this) { @@ -84,7 +82,7 @@ static u_int64_t get_initiator_spi (private_ike_sa_id_t *this) } /** - * @brief implements ike_sa_id_t.get_responder_spi + * implements ike_sa_id_t.get_responder_spi. */ static u_int64_t get_responder_spi (private_ike_sa_id_t *this) { @@ -92,51 +90,40 @@ static u_int64_t get_responder_spi (private_ike_sa_id_t *this) } /** - * @brief implements function equals of ike_sa_id_t + * implements ike_sa_id_t.get_responder_spi. */ -static status_t equals (private_ike_sa_id_t *this,private_ike_sa_id_t *other, bool *are_equal) +static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other) { if ((this == NULL)||(other == NULL)) { - return FAILED; + return FALSE; } if ((this->is_initiator_flag == other->is_initiator_flag) && (this->initiator_spi == other->initiator_spi) && (this->responder_spi == other->responder_spi)) - { /* private_ike_sa_id's are equal */ - *are_equal = TRUE; + return TRUE; } else { /* private_ike_sa_id's are not equal */ - *are_equal = FALSE; + return FALSE; } - - return SUCCESS; } /** - * @brief implements function replace_values of ike_sa_id_t + * implements ike_sa_id_t.replace_values. */ -status_t replace_values (private_ike_sa_id_t *this, private_ike_sa_id_t *other) +static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other) { - if ((this == NULL) || (other == NULL)) - { - return FAILED; - } - this->initiator_spi = other->initiator_spi; this->responder_spi = other->responder_spi; this->is_initiator_flag = other->is_initiator_flag; - - return SUCCESS; } - /** - * @brief implements ike_sa_id_t.is_initiator + * implements ike_sa_id_t.is_initiator. */ static bool is_initiator(private_ike_sa_id_t *this) { @@ -144,7 +131,7 @@ static bool is_initiator(private_ike_sa_id_t *this) } /** - * @brief implements ike_sa_id_t.switch_initiator + * implements ike_sa_id_t.switch_initiator. */ static bool switch_initiator(private_ike_sa_id_t *this) { @@ -159,24 +146,20 @@ static bool switch_initiator(private_ike_sa_id_t *this) return this->is_initiator_flag; } - /** - * @brief implements function clone of ike_sa_id_t + * implements ike_sa_id_t.clone. */ -static status_t clone (private_ike_sa_id_t *this, ike_sa_id_t **clone_of_this) +static ike_sa_id_t* clone(private_ike_sa_id_t *this) { - *clone_of_this = ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag); - - return (*clone_of_this == NULL) ? OUT_OF_RES : SUCCESS; + return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag); } /** - * @brief implements function destroy of ike_sa_id_t + * implements ike_sa_id_t.clone. */ -static status_t destroy (private_ike_sa_id_t *this) +static void destroy(private_ike_sa_id_t *this) { allocator_free(this); - return SUCCESS; } /* @@ -185,24 +168,20 @@ static status_t destroy (private_ike_sa_id_t *this) ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag) { private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t); - if (this == NULL) - { - return NULL; - } /* Public functions */ - this->public.set_responder_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi; - this->public.set_initiator_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi; + this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi; + this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi; this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi; this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi; - this->public.equals = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*,bool*)) equals; - this->public.replace_values = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values; + this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals; + this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values; this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator; this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator; - this->public.clone = (status_t(*)(ike_sa_id_t*,ike_sa_id_t**)) clone; - this->public.destroy = (status_t(*)(ike_sa_id_t*))destroy; + this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone; + this->public.destroy = (void(*)(ike_sa_id_t*))destroy; /* private data */ this->initiator_spi = initiator_spi; diff --git a/Source/charon/sa/ike_sa_id.h b/Source/charon/sa/ike_sa_id.h index 7eb8a326d..2b4b643d3 100644 --- a/Source/charon/sa/ike_sa_id.h +++ b/Source/charon/sa/ike_sa_id.h @@ -1,7 +1,7 @@ /** * @file ike_sa_id.h * - * @brief Class for identification of an IKE_SA + * @brief Interface of ike_sa_id_t. * */ @@ -42,98 +42,96 @@ struct ike_sa_id_t { * * This function is called when a request or reply of a IKE_SA_INIT is received. * - * @param this ike_sa_id_t object - * @param responder_spi SPI of responder to set - * @return SUCCESSFUL in any case + * @param this ike_sa_id_t object + * @param responder_spi SPI of responder to set */ - status_t (*set_responder_spi) (ike_sa_id_t *this, u_int64_t responder_spi); + void (*set_responder_spi) (ike_sa_id_t *this, u_int64_t responder_spi); /** * @brief Sets the SPI of the initiator. * * - * @param this ike_sa_id_t object - * @param initiator_spi SPI to set - * @return SUCCESSFUL in any case + * @param this ike_sa_id_t object + * @param initiator_spi SPI to set */ - status_t (*set_initiator_spi) (ike_sa_id_t *this, u_int64_t initiator_spi); + void (*set_initiator_spi) (ike_sa_id_t *this, u_int64_t initiator_spi); /** - * @brief Returns the initiator spi + * @brief Returns the initiator spi. * - * @param this ike_sa_id_t object - * @return spi of the initiator + * @param this ike_sa_id_t object + * @return spi of the initiator */ u_int64_t (*get_initiator_spi) (ike_sa_id_t *this); /** - * @brief Returns the responder spi + * @brief Returns the responder spi. * - * @param this ike_sa_id_t object - * @return spi of the responder + * @param this ike_sa_id_t object + * @return spi of the responder */ u_int64_t (*get_responder_spi) (ike_sa_id_t *this); /** - * @brief Check if two ike_sa_ids are equal + * @brief Check if two ike_sa_ids are equal. * - * @param this ike_sa_id_t object - * @param other ike_sa_id object to check if equal - * @param are_equal is set to TRUE, if given ike_sa_ids are equal, FALSE otherwise - * @return SUCCESSFUL if succeeded, FAILED otherwise + * @param this ike_sa_id_t object + * @param other ike_sa_id object to check if equal + * @return TRUE if given ike_sa_ids are equal, FALSE otherwise */ - status_t (*equals) (ike_sa_id_t *this,ike_sa_id_t *other, bool *are_equal); + bool (*equals) (ike_sa_id_t *this, ike_sa_id_t *other); /** - * @brief Replace the values of a given ike_sa_id_t object with values - * from another ike_sa_id_t object + * @brief Replace the values of a given ike_sa_id_t object with values. + * from another ike_sa_id_t object. * - * @param this ike_sa_id_t object - * @param other ike_sa_id_t object which values will be taken - * @return SUCCESSFUL if succeeded, FAILED otherwise + * @param this ike_sa_id_t object + * @param other ike_sa_id_t object which values will be taken */ - status_t (*replace_values) (ike_sa_id_t *this, ike_sa_id_t *other); + void (*replace_values) (ike_sa_id_t *this, ike_sa_id_t *other); /** - * @brief gets the initiator flag + * @brief gets the initiator flag. * - * @param this ike_sa_id_t object - * @return TRUE if we are the original initator + * @param this ike_sa_id_t object + * @return TRUE if we are the original initator */ bool (*is_initiator) (ike_sa_id_t *this); /** - * @brief switches the initiator flag + * @brief switches the initiator flag. * - * @param this ike_sa_id_t object - * @return TRUE if we are the original initator after switch + * @param this ike_sa_id_t object + * @return TRUE if we are the original initator after switch */ bool (*switch_initiator) (ike_sa_id_t *this); /** - * @brief Clones a given ike_sa_id_t object + * @brief Clones a given ike_sa_id_t object. * - * @param this ike_sa_id_t object - * @param clone_of_this ike_sa_id_t object which will be created - * @return SUCCESSFUL if succeeded, FAILED otherwise + * @param this ike_sa_id_t object + * @return cloned ike_sa_id */ - status_t (*clone) (ike_sa_id_t *this,ike_sa_id_t **clone_of_this); + ike_sa_id_t *(*clone) (ike_sa_id_t *this); /** - * @brief Destroys a ike_sa_id_tobject + * @brief Destroys a ike_sa_id_tobject. * - * @param this ike_sa_id_t object - * @return SUCCESSFUL if succeeded, FAILED otherwise + * @param this ike_sa_id_t object */ - status_t (*destroy) (ike_sa_id_t *this); + void (*destroy) (ike_sa_id_t *this); }; /** - * Creates an ike_sa_id_t object with specific spi's and defined role + * @brief Creates an ike_sa_id_t object with specific spi's and defined role * * @warning The initiator SPI and role is not changeable after initiating a ike_sa_id object + * + * @param initiator_spi initiators spi + * @param responder_spi responders spi + * @param is_initiator TRUE if we are the original initiator + * @return created ike_sa_id_t object */ - ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiaor); #endif /*IKE_SA_ID_H_*/ diff --git a/Source/charon/sa/ike_sa_manager.c b/Source/charon/sa/ike_sa_manager.c index 8348dae38..698d69315 100644 --- a/Source/charon/sa/ike_sa_manager.c +++ b/Source/charon/sa/ike_sa_manager.c @@ -96,11 +96,6 @@ static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id) { ike_sa_entry_t *this = allocator_alloc_thing(ike_sa_entry_t); - if (this == NULL) - { - return NULL; - } - /* destroy function */ this->destroy = ike_sa_entry_destroy; @@ -113,22 +108,11 @@ static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id) this->driveout_waiting_threads = FALSE; /* ike_sa_id is always cloned */ - ike_sa_id->clone(ike_sa_id, &(this->ike_sa_id)); - if (this->ike_sa_id == NULL) - { - allocator_free(this); - return NULL; - } - + this->ike_sa_id = ike_sa_id->clone(ike_sa_id); + /* create new ike_sa */ this->ike_sa = ike_sa_create(ike_sa_id); - if (this->ike_sa == NULL) - { - this->ike_sa_id->destroy(this->ike_sa_id); - allocator_free(this); - return NULL; - - } + return this; } @@ -149,11 +133,9 @@ struct private_ike_sa_manager_t { * we give out SPIs incremental * * @param this the ike_sa_manager - * @param spi[out] spi will be written here - * @return SUCCESS or, - * OUT_OF_RES when we already served 2^64 SPIs ;-) + * @return the next spi */ - status_t (*get_next_spi) (private_ike_sa_manager_t *this, u_int64_t *spi); + u_int64_t (*get_next_spi) (private_ike_sa_manager_t *this); /** * @brief find the ike_sa_entry in the list by SPIs @@ -167,7 +149,6 @@ struct private_ike_sa_manager_t { * @return * - SUCCESS when found, * - NOT_FOUND when no such ike_sa_id in list - * - OUT_OF_RES */ status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry); @@ -183,7 +164,6 @@ struct private_ike_sa_manager_t { * @return * - SUCCESS when found, * - NOT_FOUND when no such ike_sa_id in list - * - OUT_OF_RES */ status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry); @@ -221,7 +201,7 @@ struct private_ike_sa_manager_t { /** - * @see private_ike_sa_manager_t.get_entry_by_id + * Implements private_ike_sa_manager_t.get_entry_by_id. */ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry) { @@ -230,22 +210,14 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike status_t status; /* create iterator over list of ike_sa's */ - status = list->create_iterator(list, &iterator, TRUE); - if (status != SUCCESS) - { - this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status)); - /* out of res */ - return status; - } - + list->create_iterator(list, &iterator, TRUE); + /* default status */ status = NOT_FOUND; - while (iterator->has_next(iterator)) { ike_sa_entry_t *current; - bool are_equal = FALSE; iterator->current(iterator, (void**)¤t); if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0) { @@ -259,10 +231,9 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike break; } } - current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id, &are_equal); - if (are_equal) + if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id)) { - this->logger->log(this->logger,CONTROL | MOST,"Found entry by full ID"); + this->logger->log(this->logger,CONTROL | MOST,"Found entry by full ID"); *entry = current; status = SUCCESS; break; @@ -274,7 +245,7 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike } /** - * @see private_ike_sa_manager_t.get_entry_by_sa + * Implements private_ike_sa_manager_t.get_entry_by_sa. */ static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry) { @@ -282,12 +253,7 @@ static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa iterator_t *iterator; status_t status; - status = list->create_iterator(list, &iterator, TRUE); - if (status != SUCCESS) - { - this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status)); - return status; - } + list->create_iterator(list, &iterator, TRUE); /* default status */ status = NOT_FOUND; @@ -306,11 +272,12 @@ static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa } } iterator->destroy(iterator); + return status; } /** - * @see private_ike_sa_manager_s.delete_entry + * Implements private_ike_sa_manager_s.delete_entry. */ static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry) { @@ -318,13 +285,7 @@ static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *ent iterator_t *iterator; status_t status; - status = list->create_iterator(list, &iterator, TRUE); - - if (status != SUCCESS) - { - this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status)); - return status; - } + list->create_iterator(list, &iterator, TRUE); status = NOT_FOUND; @@ -347,85 +308,47 @@ static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *ent /** - * @see private_ike_sa_manager_t.get_next_spi + * Implements private_ike_sa_manager_t.get_next_spi. */ -static status_t get_next_spi(private_ike_sa_manager_t *this, u_int64_t *spi) +static u_int64_t get_next_spi(private_ike_sa_manager_t *this) { this->next_spi++; if (this->next_spi == 0) { - /* our software ran so incredible stable, we have no more - * SPIs to give away :-/. */ - this->logger->log(this->logger,CONTROL | MOST,"No more SPI values available. WOW!"); - return OUT_OF_RES; + /* TODO handle overflow, + * delete all SAs or so + */ } - *spi = this->next_spi; - return SUCCESS; + return this->next_spi; } /** * Implementation of ike_sa_manager.create_and_checkout. */ -static status_t create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa) +static void create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa) { - status_t retval; u_int64_t initiator_spi; ike_sa_entry_t *new_ike_sa_entry; ike_sa_id_t *new_ike_sa_id; - retval = this->get_next_spi(this, &initiator_spi); - if (retval == SUCCESS) - { - new_ike_sa_id = ike_sa_id_create(0, 0, TRUE); - if (new_ike_sa_id != NULL) - { - new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi); - - /* create entry */ - new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id); - new_ike_sa_id->destroy(new_ike_sa_id); - if (new_ike_sa_entry != NULL) - { - /* each access is locked */ - pthread_mutex_lock(&(this->mutex)); - - retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry); + initiator_spi = this->get_next_spi(this); + new_ike_sa_id = ike_sa_id_create(0, 0, TRUE); + new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi); + + /* create entry */ + new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id); + new_ike_sa_id->destroy(new_ike_sa_id); + + /* each access is locked */ + pthread_mutex_lock(&(this->mutex)); - if (retval == SUCCESS) - { - /* check ike_sa out */ - this->logger->log(this->logger,CONTROL | MORE ,"New IKE_SA created and added to list of known IKE_SA's"); - new_ike_sa_entry->checked_out = TRUE; - *ike_sa = new_ike_sa_entry->ike_sa; - /* DON'T use return, we must unlock the mutex! */ - } - else - { - /* ike_sa_entry could not be added to list*/ - this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry_t could not be added to list"); - } - pthread_mutex_unlock(&(this->mutex)); - } - else - { - /* new ike_sa_entry could not be created */ - this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry_t could not be created"); - retval = OUT_OF_RES; - } - } - else - { - /* new ike_sa_id could not be created */ - this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_id_t could not be created"); - retval = OUT_OF_RES; - } - } - else - { - /* next SPI could not be created */ - this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created"); - } + this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry); - return retval; + /* check ike_sa out */ + this->logger->log(this->logger,CONTROL | MORE ,"New IKE_SA created and added to list of known IKE_SA's"); + new_ike_sa_entry->checked_out = TRUE; + *ike_sa = new_ike_sa_entry->ike_sa; + + pthread_mutex_unlock(&(this->mutex)); } /** @@ -436,13 +359,13 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, bool responder_spi_set; bool initiator_spi_set; status_t retval; - + /* each access is locked */ pthread_mutex_lock(&(this->mutex)); - + responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id)); initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id)); - + if (initiator_spi_set && responder_spi_set) { /* we SHOULD have an IKE_SA for these SPIs in the list, @@ -461,44 +384,44 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, } else { - /* is this IKE_SA already checked out ?? - * are we welcome to get this SA ? */ - while (entry->checked_out && !entry->driveout_waiting_threads) - { - /* so wait until we can get it for us. - * we register us as waiting. - */ - entry->waiting_threads++; - pthread_cond_wait(&(entry->condvar), &(this->mutex)); - entry->waiting_threads--; - } - - /* hm, a deletion request forbids us to get this SA, go home */ - if (entry->driveout_waiting_threads) - { - /* we must signal here, others are interested that we leave */ - pthread_cond_signal(&(entry->condvar)); - this->logger->log(this->logger,CONTROL|MORE,"Drive out waiting thread for existing IKE_SA"); - retval = NOT_FOUND; - } - else - { - this->logger->log(this->logger,CONTROL|MOST,"IKE SA successfully checked out"); - /* ok, this IKE_SA is finally ours */ - entry->checked_out = TRUE; - *ike_sa = entry->ike_sa; - /* DON'T use return, we must unlock the mutex! */ - retval = SUCCESS; - } - } - } - else - { - this->logger->log(this->logger,ERROR | MORE,"IKE SA not stored in known IKE_SA list"); - /* looks like there is no such IKE_SA, better luck next time... */ - /* DON'T use return, we must unlock the mutex! */ - retval = NOT_FOUND; - } + /* is this IKE_SA already checked out ?? + * are we welcome to get this SA ? */ + while (entry->checked_out && !entry->driveout_waiting_threads) + { + /* so wait until we can get it for us. + * we register us as waiting. + */ + entry->waiting_threads++; + pthread_cond_wait(&(entry->condvar), &(this->mutex)); + entry->waiting_threads--; + } + + /* hm, a deletion request forbids us to get this SA, go home */ + if (entry->driveout_waiting_threads) + { + /* we must signal here, others are interested that we leave */ + pthread_cond_signal(&(entry->condvar)); + this->logger->log(this->logger,CONTROL|MORE,"Drive out waiting thread for existing IKE_SA"); + retval = NOT_FOUND; + } + else + { + this->logger->log(this->logger,CONTROL|MOST,"IKE SA successfully checked out"); + /* ok, this IKE_SA is finally ours */ + entry->checked_out = TRUE; + *ike_sa = entry->ike_sa; + /* DON'T use return, we must unlock the mutex! */ + retval = SUCCESS; + } + } + } + else + { + this->logger->log(this->logger,ERROR | MORE,"IKE SA not stored in known IKE_SA list"); + /* looks like there is no such IKE_SA, better luck next time... */ + /* DON'T use return, we must unlock the mutex! */ + retval = NOT_FOUND; + } } else if (initiator_spi_set && !responder_spi_set) { @@ -512,53 +435,29 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, u_int64_t responder_spi; ike_sa_entry_t *new_ike_sa_entry; - + /* set SPIs, we are the responder */ - retval = this->get_next_spi(this, &responder_spi); - - if (retval == SUCCESS) - { /* next SPI could be created */ - - /* we also set arguments spi, so its still valid */ - ike_sa_id->set_responder_spi(ike_sa_id, responder_spi); - - /* create entry */ - new_ike_sa_entry = ike_sa_entry_create(ike_sa_id); - if (new_ike_sa_entry != NULL) - { - retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry); - if (retval == SUCCESS) - { - /* check ike_sa out */ - this->logger->log(this->logger,CONTROL | MORE ,"IKE_SA added to list of known IKE_SA's"); - new_ike_sa_entry->checked_out = TRUE; - *ike_sa = new_ike_sa_entry->ike_sa; - - /* DON'T use return, we must unlock the mutex! */ - } - else - { - /* ike_sa_entry could not be added to list*/ - this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be added to list"); - } - } - else - { - /* new ike_sa_entry could not be created */ - this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be created"); - retval = OUT_OF_RES; - } - } - else - { /* next SPI could not be created */ - this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created"); - } + responder_spi = this->get_next_spi(this); + + /* we also set arguments spi, so its still valid */ + ike_sa_id->set_responder_spi(ike_sa_id, responder_spi); + + /* create entry */ + new_ike_sa_entry = ike_sa_entry_create(ike_sa_id); + this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry); + + /* check ike_sa out */ + this->logger->log(this->logger,CONTROL | MORE ,"IKE_SA added to list of known IKE_SA's"); + new_ike_sa_entry->checked_out = TRUE; + *ike_sa = new_ike_sa_entry->ike_sa; + + retval = SUCCESS; } else { /* responder set, initiator not: here is something seriously wrong! */ - this->logger->log(this->logger,ERROR | MORE,"Invalid IKE_SA SPI's"); + this->logger->log(this->logger,ERROR | MORE, "Invalid IKE_SA SPI's"); /* DON'T use return, we must unlock the mutex! */ retval = INVALID_ARG; } @@ -647,14 +546,13 @@ static status_t checkin_and_delete(private_ike_sa_manager_t *this, ike_sa_t *ike this->logger->log(this->logger,ERROR,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA"); retval = NOT_FOUND; } - + pthread_mutex_unlock(&(this->mutex)); return retval; } /** - * Implements ike_sa_manager_t-function delete. - * @see ike_sa_manager_t.delete. + * Implements ike_sa_manager_t.delete. */ static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id) { @@ -697,15 +595,13 @@ static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id) } /** - * Implements ike_sa_manager_t-function destroy. - * @see ike_sa_manager_t.destroy. + * Implements ike_sa_manager_t.destroy. */ -static status_t destroy(private_ike_sa_manager_t *this) +static void destroy(private_ike_sa_manager_t *this) { /* destroy all list entries */ linked_list_t *list = this->ike_sa_list; iterator_t *iterator; - status_t status; ike_sa_entry_t *entry; pthread_mutex_lock(&(this->mutex)); @@ -713,13 +609,7 @@ static status_t destroy(private_ike_sa_manager_t *this) this->logger->log(this->logger,CONTROL | MORE,"Going to destroy IKE_SA manager and all managed IKE_SA's"); /* Step 1: drive out all waiting threads */ - status = list->create_iterator(list, &iterator, TRUE); - - if (status != SUCCESS) - { - this->logger->log(this->logger,ERROR,"Fatal Error: Create of iterator while destroying IKE_SA-Manager failed"); - return FAILED; - } + list->create_iterator(list, &iterator, TRUE); this->logger->log(this->logger,CONTROL | MOST,"Set driveout flags for all stored IKE_SA's"); while (iterator->has_next(iterator)) @@ -761,8 +651,6 @@ static status_t destroy(private_ike_sa_manager_t *this) global_logger_manager->destroy_logger(global_logger_manager,this->logger); allocator_free(this); - - return SUCCESS; } /* @@ -773,8 +661,8 @@ ike_sa_manager_t *ike_sa_manager_create() private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t); /* assign public functions */ - this->public.destroy = (status_t(*)(ike_sa_manager_t*))destroy; - this->public.create_and_checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_t **sa))create_and_checkout; + this->public.destroy = (void(*)(ike_sa_manager_t*))destroy; + this->public.create_and_checkout = (void(*)(ike_sa_manager_t*, ike_sa_t **sa))create_and_checkout; this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id, ike_sa_t **sa))checkout; this->public.checkin = (status_t(*)(ike_sa_manager_t*, ike_sa_t *sa))checkin; this->public.delete = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id))delete; @@ -788,20 +676,8 @@ ike_sa_manager_t *ike_sa_manager_create() /* initialize private variables */ this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL); - if (this->logger == NULL) - { - allocator_free(this); - return NULL; - } this->ike_sa_list = linked_list_create(); - if (this->ike_sa_list == NULL) - { - this->logger->log(this->logger,ERROR,"Fatal error: Failed to create list for managed IKE_SA"); - global_logger_manager->destroy_logger(global_logger_manager,this->logger); - allocator_free(this); - return NULL; - } pthread_mutex_init(&(this->mutex), NULL); diff --git a/Source/charon/sa/ike_sa_manager.h b/Source/charon/sa/ike_sa_manager.h index 4ef1bf75d..ae54e618a 100644 --- a/Source/charon/sa/ike_sa_manager.h +++ b/Source/charon/sa/ike_sa_manager.h @@ -58,7 +58,6 @@ struct ike_sa_manager_t { * @returns * - SUCCESS if checkout successful * - NOT_FOUND when no such SA is available - * - OUT_OF_RES */ status_t (*checkout) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *sa_id, ike_sa_t **ike_sa); @@ -73,12 +72,9 @@ struct ike_sa_manager_t { * * @param ike_sa_manager the manager object * @param ike_sa[out] checked out SA - * @returns - * - SUCCESS if checkout successful - * - OUT_OF_RES */ - status_t (*create_and_checkout) (ike_sa_manager_t* ike_sa_manager,ike_sa_t **ike_sa); - + void (*create_and_checkout) (ike_sa_manager_t* ike_sa_manager,ike_sa_t **ike_sa); + /** * @brief Checkin the SA after usage * @@ -88,8 +84,9 @@ struct ike_sa_manager_t { * @param ike_sa_manager the manager object * @param ike_sa_id[in/out] the SA identifier, will be updated * @param ike_sa[out] checked out SA - * @returns SUCCESS if checked in - * NOT_FOUND when not found (shouldn't happen!) + * @returns + * - SUCCESS if checked in + * - NOT_FOUND when not found (shouldn't happen!) */ status_t (*checkin) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa); /** @@ -100,17 +97,20 @@ struct ike_sa_manager_t { * * @param ike_sa_manager the manager object * @param ike_sa_id[in/out] the SA identifier - * @returns SUCCESS if found - * NOT_FOUND when no such SA is available + * @returns + * - SUCCESS if found + * - NOT_FOUND when no such SA is available */ status_t (*delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *ike_sa_id); + /** * @brief delete a checked out SA - * + * * @param ike_sa_manager the manager object * @param ike_sa SA to delete - * @returns SUCCESS if found - * NOT_FOUND when no such SA is available + * @returns + * - SUCCESS if found + * - NOT_FOUND when no such SA is available */ status_t (*checkin_and_delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa); @@ -120,15 +120,14 @@ struct ike_sa_manager_t { * Threads will be driven out, so all SAs can be deleted cleanly * * @param ike_sa_manager the manager object - * @returns SUCCESS if succeeded, FAILED otherwise */ - status_t (*destroy) (ike_sa_manager_t *ike_sa_manager); + void (*destroy) (ike_sa_manager_t *ike_sa_manager); }; /** * @brief Create a manager * - * @returns the manager + * @returns the created manager */ ike_sa_manager_t *ike_sa_manager_create(); diff --git a/Source/charon/sa/states/ike_auth_requested.c b/Source/charon/sa/states/ike_auth_requested.c index 43af215c2..28ff3a0f5 100644 --- a/Source/charon/sa/states/ike_auth_requested.c +++ b/Source/charon/sa/states/ike_auth_requested.c @@ -63,10 +63,9 @@ static ike_sa_state_t get_state(private_ike_auth_requested_t *this) /** * Implements state_t.get_state */ -static status_t destroy(private_ike_auth_requested_t *this) +static void destroy(private_ike_auth_requested_t *this) { allocator_free(this); - return SUCCESS; } /* @@ -75,16 +74,11 @@ static status_t destroy(private_ike_auth_requested_t *this) ike_auth_requested_t *ike_auth_requested_create(protected_ike_sa_t *ike_sa) { private_ike_auth_requested_t *this = allocator_alloc_thing(private_ike_auth_requested_t); - - if (this == NULL) - { - return NULL; - } /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* private data */ this->ike_sa = ike_sa; diff --git a/Source/charon/sa/states/ike_sa_established.c b/Source/charon/sa/states/ike_sa_established.c index f9c68f162..d1f9a6bd7 100644 --- a/Source/charon/sa/states/ike_sa_established.c +++ b/Source/charon/sa/states/ike_sa_established.c @@ -63,10 +63,9 @@ static ike_sa_state_t get_state(private_ike_sa_established_t *this) /** * Implements state_t.get_state */ -static status_t destroy(private_ike_sa_established_t *this) +static void destroy(private_ike_sa_established_t *this) { allocator_free(this); - return SUCCESS; } /* @@ -75,16 +74,11 @@ static status_t destroy(private_ike_sa_established_t *this) ike_sa_established_t *ike_sa_established_create(protected_ike_sa_t *ike_sa) { private_ike_sa_established_t *this = allocator_alloc_thing(private_ike_sa_established_t); - - if (this == NULL) - { - return NULL; - } /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* private data */ this->ike_sa = ike_sa; diff --git a/Source/charon/sa/states/ike_sa_init_requested.c b/Source/charon/sa/states/ike_sa_init_requested.c index 4f79143a5..f301b307d 100644 --- a/Source/charon/sa/states/ike_sa_init_requested.c +++ b/Source/charon/sa/states/ike_sa_init_requested.c @@ -138,13 +138,8 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t /* 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 errror: Could not create iterator on suggested proposals"); - payloads->destroy(payloads); - return status; - } + sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE); + /* now let the configuration-manager check the selected proposals*/ this->logger->log(this->logger, CONTROL | MOST, "Check suggested proposals"); @@ -194,14 +189,8 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t case KEY_EXCHANGE: { ke_payload_t *ke_payload = (ke_payload_t*)payload; - - status = this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not set other public value for DH exchange. Status %s",mapping_find(status_m,status)); - payloads->destroy(payloads); - return OUT_OF_RES; - } + + this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload)); /* shared secret is computed AFTER processing of all payloads... */ break; @@ -209,28 +198,16 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t case NONCE: { nonce_payload_t *nonce_payload = (nonce_payload_t*)payload; - - if (this->received_nonce.ptr != NULL) - { - this->logger->log(this->logger, CONTROL | MOST, "Destroy existing received nonce"); - allocator_free(this->received_nonce.ptr); - this->received_nonce.ptr = NULL; - this->received_nonce.len = 0; - } - - status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce)); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not get received nonce"); - payloads->destroy(payloads); - return OUT_OF_RES; - } + allocator_free(this->received_nonce.ptr); + this->received_nonce = CHUNK_INITIALIZER; + + nonce_payload->get_nonce(nonce_payload, &(this->received_nonce)); break; } default: { - this->logger->log(this->logger, ERROR, "Fatal errror: Payload type not supported!!!!"); + this->logger->log(this->logger, ERROR, "Payload type not supported!!!!"); payloads->destroy(payloads); return FAILED; } @@ -239,31 +216,16 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t } payloads->destroy(payloads); - - if (this->shared_secret.ptr != NULL) - { - this->logger->log(this->logger, CONTROL | MOST, "Destroy existing shared_secret"); - allocator_free(this->shared_secret.ptr); - this->shared_secret.ptr = NULL; - this->shared_secret.len = 0; - } - - + + allocator_free(this->shared_secret.ptr); + this->shared_secret = CHUNK_INITIALIZER; + /* 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, &(this->shared_secret)); this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &this->shared_secret); - status = this->ike_sa->compute_secrets(this->ike_sa,this->shared_secret,this->sent_nonce, this->received_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,this->shared_secret,this->sent_nonce, this->received_nonce); /**************************** * @@ -285,7 +247,6 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t // response->destroy(response); - return SUCCESS; } @@ -301,31 +262,17 @@ static ike_sa_state_t get_state(private_ike_sa_init_requested_t *this) /** * Implements state_t.get_state */ -static status_t destroy(private_ike_sa_init_requested_t *this) +static void destroy(private_ike_sa_init_requested_t *this) { this->logger->log(this->logger, CONTROL | MORE, "Going to destroy state of type ike_sa_init_requested_t"); this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object"); this->diffie_hellman->destroy(this->diffie_hellman); - if (this->sent_nonce.ptr != NULL) - { - this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce"); - allocator_free(this->sent_nonce.ptr); - } - if (this->received_nonce.ptr != NULL) - { - this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce"); - allocator_free(this->received_nonce.ptr); - } - - if (this->shared_secret.ptr != NULL) - { - this->logger->log(this->logger, CONTROL | MOST, "Destroy shared secret"); - allocator_free(this->shared_secret.ptr); - } + allocator_free(this->sent_nonce.ptr); + allocator_free(this->received_nonce.ptr); + allocator_free(this->shared_secret.ptr); allocator_free(this); - return SUCCESS; } /* @@ -335,22 +282,15 @@ ike_sa_init_requested_t *ike_sa_init_requested_create(protected_ike_sa_t *ike_sa { private_ike_sa_init_requested_t *this = allocator_alloc_thing(private_ike_sa_init_requested_t); - if (this == NULL) - { - return NULL; - } - /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* private data */ this->ike_sa = ike_sa; - this->received_nonce.ptr = NULL; - this->received_nonce.len = 0; - this->shared_secret.ptr = NULL; - this->shared_secret.len = 0; + this->received_nonce = CHUNK_INITIALIZER; + this->shared_secret = CHUNK_INITIALIZER; this->logger = this->ike_sa->get_logger(this->ike_sa); this->diffie_hellman = diffie_hellman; this->sent_nonce = sent_nonce; diff --git a/Source/charon/sa/states/ike_sa_init_responded.c b/Source/charon/sa/states/ike_sa_init_responded.c index 7bc3ee4ea..c4f7e2d54 100644 --- a/Source/charon/sa/states/ike_sa_init_responded.c +++ b/Source/charon/sa/states/ike_sa_init_responded.c @@ -111,16 +111,11 @@ static status_t destroy(private_ike_sa_init_responded_t *this) ike_sa_init_responded_t *ike_sa_init_responded_create(protected_ike_sa_t *ike_sa, chunk_t shared_secret, chunk_t received_nonce, chunk_t sent_nonce) { private_ike_sa_init_responded_t *this = allocator_alloc_thing(private_ike_sa_init_responded_t); - - if (this == NULL) - { - return NULL; - } /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* private data */ this->ike_sa = ike_sa; diff --git a/Source/charon/sa/states/ike_sa_init_responded.h b/Source/charon/sa/states/ike_sa_init_responded.h index 5e871702d..2e1fabc1d 100644 --- a/Source/charon/sa/states/ike_sa_init_responded.h +++ b/Source/charon/sa/states/ike_sa_init_responded.h @@ -1,7 +1,7 @@ /** * @file ike_sa_init_responded.h * - * @brief State of a IKE_SA after responding to an IKE_SA_INIT request + * @brief Interface of ike_sa_init_responded_t. * */ diff --git a/Source/charon/sa/states/initiator_init.c b/Source/charon/sa/states/initiator_init.c index 1ffc32447..b1e25a4c5 100644 --- a/Source/charon/sa/states/initiator_init.c +++ b/Source/charon/sa/states/initiator_init.c @@ -91,11 +91,8 @@ struct private_initiator_init_t { * * @param this calling object * @param message the created message will be stored at this location - * @return - * - SUCCESS - * - OUT_OF_RES */ - status_t (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message); + void (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message); /** * Builds the SA payload for this state. @@ -103,11 +100,8 @@ struct private_initiator_init_t { * @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_initiator_init_t *this, payload_t **payload); + void (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload); /** * Builds the KE payload for this state. @@ -115,22 +109,17 @@ struct private_initiator_init_t { * @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_initiator_init_t *this, payload_t **payload); + void (*build_ke_payload) (private_initiator_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. - * @return - * - SUCCESS - * - OUT_OF_RES */ - status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload); + void (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload); /** * Destroy function called internally of this class after state change succeeded. @@ -138,9 +127,8 @@ struct private_initiator_init_t { * 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_initiator_init_t *this); + void (*destroy_after_state_change) (private_initiator_init_t *this); }; /** @@ -148,18 +136,18 @@ struct private_initiator_init_t { */ static status_t initiate_connection (private_initiator_init_t *this, char *name) { - iterator_t *proposal_iterator; - ike_sa_init_requested_t *next_state; - message_t *message; - packet_t *packet; - status_t status; - host_t *my_host; - host_t *other_host; - randomizer_t *randomizer; + iterator_t *proposal_iterator; + ike_sa_init_requested_t *next_state; + message_t *message; + packet_t *packet; + status_t status; + host_t *my_host; + host_t *other_host; + randomizer_t *randomizer; - this->logger->log(this->logger, CONTROL, "Initializing connection %s",name); + /* get local host */ status = global_configuration_manager->get_local_host(global_configuration_manager, name, &my_host); if (status != SUCCESS) { @@ -168,6 +156,7 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name) } this->ike_sa->set_my_host(this->ike_sa,my_host); + /* get remote host */ status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &other_host); if (status != SUCCESS) { @@ -176,22 +165,17 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name) } this->ike_sa->set_other_host(this->ike_sa,other_host); + /* get dh group */ status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority); if (status != SUCCESS) - { + { this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name); return INVALID_ARG; } - 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; - } - + /* get proposals */ + this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->get_other_host(this->ike_sa), proposal_iterator); - /* not needed anymore */ proposal_iterator->destroy(proposal_iterator); if (status != SUCCESS) { @@ -202,70 +186,45 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name) /* a diffie hellman object could allready exist caused by an failed initiate_connection call */ if (this->diffie_hellman == NULL) { - this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object"); this->diffie_hellman = diffie_hellman_create(this->dh_group_number); } if (this->diffie_hellman == NULL) { this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!"); - return FAILED; + return FAILED; } if (this->sent_nonce.ptr != NULL) { - this->logger->log(this->logger, ERROR, "Free existing sent nonce!"); allocator_free(this->sent_nonce.ptr); this->sent_nonce = CHUNK_INITIALIZER; } - this ->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce"); + this->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce"); randomizer = this->ike_sa->get_randomizer(this->ike_sa); - if (randomizer->allocate_pseudo_random_bytes(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, RAW|MOST, "Nonce",&(this->sent_nonce)); + randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce)); + + this->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce)); + + this->build_ike_sa_init_request (this,&message); - - - status = this->build_ike_sa_init_request (this,&message); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not build IKE_SA_INIT request message"); - return status; - } - /* generate packet */ this->logger->log(this->logger, CONTROL|MOST, "generate packet from message"); status = message->generate(message, NULL, NULL, &packet); 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"); message->destroy(message); 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"); - packet->destroy(packet); - message->destroy(message); - return status; - } + global_send_queue->add(global_send_queue, packet); /* state can now be changed */ this->logger->log(this->logger, CONTROL|MOST, "Create next state object"); next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, 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_requested_t"); - message->destroy(message); - return FAILED; - } /* last message can now be set */ status = this->ike_sa->set_last_requested_message(this->ike_sa, message); @@ -282,7 +241,7 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name) this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state); /* 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,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) ); + this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s", mapping_find(ike_sa_state_m,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) ); this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object"); this->destroy_after_state_change(this); @@ -293,200 +252,99 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name) /** * implements private_initiator_init_t.build_ike_sa_init_request */ -static status_t build_ike_sa_init_request (private_initiator_init_t *this, message_t **request) +static void build_ike_sa_init_request (private_initiator_init_t *this, message_t **request) { - status_t status; payload_t *payload; message_t *message; /* going to build message */ - this ->logger->log(this->logger, CONTROL|MOST, "Going to build message"); - status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not build empty message"); - return status; - } - - /* build SA payload */ - status = this->build_sa_payload(this, &payload); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not build SA payload"); - message->destroy(message); - return status; - } + this->logger->log(this->logger, CONTROL|MOST, "Going to build message"); + this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message); - this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message"); + /* build SA payload */ + this->build_sa_payload(this, &payload); + this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message"); message->add_payload(message, 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"); - message->destroy(message); - return status; - } - - this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message"); + this->build_ke_payload(this, &payload); + this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message"); message->add_payload(message, 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"); - message->destroy(message); - return status; - } - - this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message"); + this->build_nonce_payload(this, &payload); + this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message"); message->add_payload(message, payload); *request = message; - return SUCCESS; } /** * implements private_initiator_init_t.build_sa_payload */ -static status_t build_sa_payload(private_initiator_init_t *this, payload_t **payload) +static void build_sa_payload(private_initiator_init_t *this, payload_t **payload) { sa_payload_t* sa_payload; 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; - } + this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); 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 **) ¤t_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,¤t_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->current(proposal_iterator,(void **) ¤t_proposal); + current_proposal->clone(current_proposal,¤t_proposal_clone); + + sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone); } proposal_iterator->destroy(proposal_iterator); - this->logger->log(this->logger, CONTROL|MORE, "sa payload builded"); - *payload = (payload_t *) sa_payload; - return SUCCESS; } /** * implements private_initiator_init_t.build_ke_payload */ -static status_t build_ke_payload(private_initiator_init_t *this, payload_t **payload) +static void build_ke_payload(private_initiator_init_t *this, payload_t **payload) { 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, "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(); - 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_dh_group_number(ke_payload, this->dh_group_number); - 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); + ke_payload->set_key_exchange_data(ke_payload, key_data); - this->logger->log(this->logger, CONTROL|MORE, "ke payload builded"); - + allocator_free_chunk(&key_data); *payload = (payload_t *) ke_payload; - return SUCCESS; } /** * implements private_initiator_init_t.build_nonce_payload */ -static status_t build_nonce_payload(private_initiator_init_t *this, payload_t **payload) +static void build_nonce_payload(private_initiator_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 = 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); - 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; + nonce_payload = nonce_payload_create(); - this->logger->log(this->logger, CONTROL|MORE, "nonce payload builded"); + nonce_payload->set_nonce(nonce_payload, this->sent_nonce); - return SUCCESS; + *payload = (payload_t *) nonce_payload; } /** @@ -510,7 +368,7 @@ static ike_sa_state_t get_state(private_initiator_init_t *this) /** * Implements state_t.get_state */ -static status_t destroy(private_initiator_init_t *this) +static void destroy(private_initiator_init_t *this) { this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object"); @@ -535,15 +393,13 @@ static status_t destroy(private_initiator_init_t *this) this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce"); allocator_free(this->sent_nonce.ptr); } - allocator_free(this); - return SUCCESS; } /** * Implements private_initiator_init_t.destroy_after_state_change */ -static status_t destroy_after_state_change (private_initiator_init_t *this) +static void destroy_after_state_change (private_initiator_init_t *this) { this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object"); @@ -557,7 +413,6 @@ static status_t destroy_after_state_change (private_initiator_init_t *this) } this->proposals->destroy(this->proposals); allocator_free(this); - return SUCCESS; } /* @@ -566,16 +421,11 @@ static status_t destroy_after_state_change (private_initiator_init_t *this) initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa) { private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t); - - if (this == NULL) - { - return NULL; - } /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* public functions */ this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection; @@ -593,11 +443,6 @@ initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa) this->logger = this->ike_sa->get_logger(this->ike_sa); this->proposals = linked_list_create(); this->sent_nonce = CHUNK_INITIALIZER; - if (this->proposals == NULL) - { - allocator_free(this); - return NULL; - } - + return &(this->public); } diff --git a/Source/charon/sa/states/responder_init.c b/Source/charon/sa/states/responder_init.c index 0d93a5b77..57e75037b 100644 --- a/Source/charon/sa/states/responder_init.c +++ b/Source/charon/sa/states/responder_init.c @@ -1,7 +1,7 @@ /** * @file responder_init.c * - * @brief Start state of a IKE_SA as responder + * @brief Implementation of responder_init_t. * */ @@ -99,11 +99,8 @@ struct private_responder_init_t { * @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. @@ -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. - * @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. - * @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. @@ -134,9 +126,8 @@ struct private_responder_init_t { * 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); }; /** @@ -175,20 +166,8 @@ static status_t process_message(private_responder_init_t *this, message_t *messa message->get_destination(message, &destination); /* we need to clone them, since we destroy the message later */ - status = destination->clone(destination, &my_host); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not clone my host informations"); - return status; - } - status = source->clone(source, &other_host); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: could not clone other host informations"); - my_host->destroy(my_host); - return status; - } - + 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); @@ -198,7 +177,7 @@ static status_t process_message(private_responder_init_t *this, message_t *messa if (status != SUCCESS) { this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message"); - return status; + return status; } /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */ @@ -219,24 +198,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa sa_payload_t *sa_payload = (sa_payload_t*)payload; iterator_t *suggested_proposals, *accepted_proposals; proposal_substructure_t *accepted_proposal; - - status = this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals"); - payloads->destroy(payloads); - return status; - } + + this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE); /* 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; - } + sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE); /* now let the configuration-manager select a subset of the proposals */ status = global_configuration_manager->select_proposals_for_host(global_configuration_manager, @@ -253,7 +219,6 @@ static status_t process_message(private_responder_init_t *this, message_t *messa /* iterators are not needed anymore */ suggested_proposals->destroy(suggested_proposals); - /* let the ike_sa create their own transforms from proposal informations */ accepted_proposals->reset(accepted_proposals); /* TODO check for true*/ @@ -309,19 +274,12 @@ 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); - return OUT_OF_RES; + return NOT_SUPPORTED; } 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; @@ -332,22 +290,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa { 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"); - 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; @@ -356,11 +303,9 @@ 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); - return OUT_OF_RES; + return NOT_SUPPORTED; } - } - } /* iterator can be destroyed */ payloads->destroy(payloads); @@ -371,63 +316,31 @@ static status_t process_message(private_responder_init_t *this, message_t *messa randomizer = this->ike_sa->get_randomizer(this->ike_sa); - if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not create nonce!"); - return OUT_OF_RES; - } - + 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); - 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 */ - 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 */ - status = this->build_sa_payload(this, &payload); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not build SA payload"); - 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 */ - status = this->build_ke_payload(this,&payload); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not build KE payload"); - 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 */ - status = this->build_nonce_payload(this, &payload); - if (status != SUCCESS) - { - this->logger->log(this->logger, ERROR, "Could not build NONCE payload"); - 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); @@ -436,30 +349,17 @@ static status_t process_message(private_responder_init_t *this, message_t *messa status = response->generate(response, NULL, NULL, &packet); 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; } - 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"); - packet->destroy(packet); - return status; - } + this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue"); + global_send_queue->add(global_send_queue, packet); /* 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); - - 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; - } /* last message can now be set */ status = this->ike_sa->set_last_responded_message(this->ike_sa, response); @@ -475,9 +375,9 @@ static status_t process_message(private_responder_init_t *this, message_t *messa /* state can now be changed */ this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state); /* 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; @@ -486,142 +386,67 @@ static status_t process_message(private_responder_init_t *this, message_t *messa /** * 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; 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 */ - + 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; - } + this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); 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 **) ¤t_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,¤t_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->current(proposal_iterator,(void **) ¤t_proposal); + current_proposal->clone(current_proposal,¤t_proposal_clone); + sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone); } - - proposal_iterator->destroy(proposal_iterator); - this->logger->log(this->logger, CONTROL|MORE, "sa payload builded"); + proposal_iterator->destroy(proposal_iterator); *payload = (payload_t *) sa_payload; - - return SUCCESS; } /** * 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; - status_t status; 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(); - 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_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; - return SUCCESS; } /** * 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 = 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); - 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; - - return SUCCESS; } @@ -636,7 +461,7 @@ static ike_sa_state_t get_state(private_responder_init_t *this) /** * 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"); @@ -650,35 +475,20 @@ static status_t destroy(private_responder_init_t *this) } 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) { - this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object"); this->diffie_hellman->destroy(this->diffie_hellman); } - allocator_free(this); - - return SUCCESS; - } /** * 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"); @@ -700,7 +510,6 @@ static status_t destroy_after_state_change (private_responder_init_t *this) } allocator_free(this); - return SUCCESS; } /* @@ -709,16 +518,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); - - if (this == NULL) - { - return NULL; - } /* interface functions */ 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.destroy = (status_t (*) (state_t *)) destroy; + this->public.state_interface.destroy = (void (*) (state_t *)) destroy; /* private functions */ this->build_sa_payload = build_sa_payload; @@ -729,16 +533,9 @@ responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa) /* private data */ this->ike_sa = ike_sa; this->logger = this->ike_sa->get_logger(this->ike_sa); - this->sent_nonce.ptr = NULL; - this->sent_nonce.len = 0; - this->received_nonce.ptr = NULL; - this->received_nonce.len = 0; + this->sent_nonce = CHUNK_INITIALIZER; + this->received_nonce = CHUNK_INITIALIZER; this->proposals = linked_list_create(); - if (this->proposals == NULL) - { - allocator_free(this); - return NULL; - } - + return &(this->public); } diff --git a/Source/charon/sa/states/responder_init.h b/Source/charon/sa/states/responder_init.h index 592cb6168..1606579ab 100644 --- a/Source/charon/sa/states/responder_init.h +++ b/Source/charon/sa/states/responder_init.h @@ -1,7 +1,7 @@ /** * @file responder_init.h * - * @brief Start state of a IKE_SA as responder + * @brief Interface of responder_init_t. * */ @@ -31,7 +31,7 @@ typedef struct responder_init_t responder_init_t; /** * @brief This class represents an IKE_SA state when initializing. - * a connection as responder + * a connection as responder. * */ struct responder_init_t { @@ -45,7 +45,9 @@ struct 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 */ responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa); diff --git a/Source/charon/sa/states/state.h b/Source/charon/sa/states/state.h index 7a8f8430c..471822f88 100644 --- a/Source/charon/sa/states/state.h +++ b/Source/charon/sa/states/state.h @@ -1,7 +1,7 @@ /** * @file state.h * - * @brief Interface for a specific IKE_SA state + * @brief Interface for a specific IKE_SA state. * */ @@ -83,7 +83,7 @@ typedef struct state_t state_t; struct state_t { /** - * @brief Processes a incoming IKEv2-Message of type message_t + * @brief Processes a incoming IKEv2-Message of type message_t. * * @param this state_t object * @param[in] message message_t object to process @@ -94,7 +94,7 @@ struct state_t { status_t (*process_message) (state_t *this,message_t *message); /** - * @brief Get the current state + * @brief Get the current state. * * @param this state_t object * @return state @@ -102,12 +102,11 @@ struct state_t { ike_sa_state_t (*get_state) (state_t *this); /** - * @brief Destroys a state_t object + * @brief Destroys a state_t object. * - * @param this state_t object - * @return SUCCESS in any case + * @param this state_t object to destroy */ - status_t (*destroy) (state_t *this); + void (*destroy) (state_t *this); }; |