removed %M printf handler, five more to go
[strongswan.git] / src / charon / encoding / message.c
index 24b4d8e..077b767 100644 (file)
@@ -6,7 +6,9 @@
  */
 
 /*
- * Copyright (C) 2005 Jan Hutter, Martin Willi
+ * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
+ * Copyright (C) 2005-2006 Martin Willi
+ * Copyright (C) 2005 Jan Hutter
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
  */
 
 #include <stdlib.h>
+#include <string.h>
 
 #include "message.h"
 
-#include <types.h>
+#include <library.h>
 #include <daemon.h>
 #include <sa/ike_sa_id.h>
 #include <encoding/generator.h>
 #include <encoding/parser.h>
 #include <utils/linked_list.h>
-#include <utils/logger_manager.h>
 #include <encoding/payloads/encodings.h>
 #include <encoding/payloads/payload.h>
 #include <encoding/payloads/encryption_payload.h>
@@ -39,7 +41,7 @@
 /**
  * Max number of notify payloads per IKEv2 Message
  */
-#define MAX_NOTIFY_PAYLOADS 10
+#define MAX_NOTIFY_PAYLOADS 20
 
 
 typedef struct payload_rule_t payload_rule_t;
@@ -63,7 +65,7 @@ struct payload_rule_t {
 
         /**
          * Max occurence of this payload.
-         */     
+         */
         size_t max_occurence;
         
         /**
@@ -122,6 +124,7 @@ static payload_rule_t ike_sa_init_i_payload_rules[] = {
        {SECURITY_ASSOCIATION,1,1,FALSE,FALSE},
        {KEY_EXCHANGE,1,1,FALSE,FALSE},
        {NONCE,1,1,FALSE,FALSE},
+       {VENDOR_ID,0,10,FALSE,FALSE},
 };
 
 /**
@@ -132,6 +135,7 @@ static payload_rule_t ike_sa_init_r_payload_rules[] = {
        {SECURITY_ASSOCIATION,1,1,FALSE,FALSE},
        {KEY_EXCHANGE,1,1,FALSE,FALSE},
        {NONCE,1,1,FALSE,FALSE},
+       {VENDOR_ID,0,10,FALSE,FALSE},
 };
 
 /**
@@ -139,15 +143,17 @@ static payload_rule_t ike_sa_init_r_payload_rules[] = {
  */
 static payload_rule_t ike_auth_i_payload_rules[] = {
        {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
+       {EXTENSIBLE_AUTHENTICATION,0,1,TRUE,TRUE},
+       {AUTHENTICATION,0,1,TRUE,TRUE},
        {ID_INITIATOR,1,1,TRUE,FALSE},
        {CERTIFICATE,0,1,TRUE,FALSE},
        {CERTIFICATE_REQUEST,0,1,TRUE,FALSE},
        {ID_RESPONDER,0,1,TRUE,FALSE},
-       {AUTHENTICATION,1,1,TRUE,FALSE},
        {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
        {TRAFFIC_SELECTOR_INITIATOR,1,1,TRUE,FALSE},
        {TRAFFIC_SELECTOR_RESPONDER,1,1,TRUE,FALSE},
        {CONFIGURATION,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
 };
 
 /**
@@ -155,13 +161,15 @@ static payload_rule_t ike_auth_i_payload_rules[] = {
  */
 static payload_rule_t ike_auth_r_payload_rules[] = {
        {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,TRUE},
+       {EXTENSIBLE_AUTHENTICATION,0,1,TRUE,TRUE},
        {CERTIFICATE,0,1,TRUE,FALSE},
-       {ID_RESPONDER,1,1,TRUE,FALSE},
-       {AUTHENTICATION,1,1,TRUE,FALSE},
-       {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
-       {TRAFFIC_SELECTOR_INITIATOR,1,1,TRUE,FALSE},
-       {TRAFFIC_SELECTOR_RESPONDER,1,1,TRUE,FALSE},
+       {ID_RESPONDER,0,1,TRUE,FALSE},
+       {AUTHENTICATION,0,1,TRUE,FALSE},
+       {SECURITY_ASSOCIATION,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
        {CONFIGURATION,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
 };
 
 
@@ -172,6 +180,7 @@ static payload_rule_t informational_i_payload_rules[] = {
        {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
        {CONFIGURATION,0,1,TRUE,FALSE},
        {DELETE,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
        
 };
 
@@ -182,6 +191,35 @@ static payload_rule_t informational_r_payload_rules[] = {
        {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
        {CONFIGURATION,0,1,TRUE,FALSE},
        {DELETE,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
+};
+
+/**
+ * Message rule for CREATE_CHILD_SA from initiator.
+ */
+static payload_rule_t create_child_sa_i_payload_rules[] = {
+       {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,FALSE},
+       {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
+       {NONCE,1,1,TRUE,FALSE},
+       {KEY_EXCHANGE,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
+       {CONFIGURATION,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
+};
+
+/**
+ * Message rule for CREATE_CHILD_SA from responder.
+ */
+static payload_rule_t create_child_sa_r_payload_rules[] = {
+       {NOTIFY,0,MAX_NOTIFY_PAYLOADS,TRUE,TRUE},
+       {SECURITY_ASSOCIATION,1,1,TRUE,FALSE},
+       {NONCE,1,1,TRUE,FALSE},
+       {KEY_EXCHANGE,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_INITIATOR,0,1,TRUE,FALSE},
+       {TRAFFIC_SELECTOR_RESPONDER,0,1,TRUE,FALSE},
+       {CONFIGURATION,0,1,TRUE,FALSE},
+       {VENDOR_ID,0,10,TRUE,FALSE},
 };
 
 
@@ -194,7 +232,9 @@ static message_rule_t message_rules[] = {
        {IKE_AUTH,TRUE,TRUE,(sizeof(ike_auth_i_payload_rules)/sizeof(payload_rule_t)),ike_auth_i_payload_rules},
        {IKE_AUTH,FALSE,TRUE,(sizeof(ike_auth_r_payload_rules)/sizeof(payload_rule_t)),ike_auth_r_payload_rules},
        {INFORMATIONAL,TRUE,TRUE,(sizeof(informational_i_payload_rules)/sizeof(payload_rule_t)),informational_i_payload_rules},
-       {INFORMATIONAL,FALSE,TRUE,(sizeof(informational_r_payload_rules)/sizeof(payload_rule_t)),informational_r_payload_rules}
+       {INFORMATIONAL,FALSE,TRUE,(sizeof(informational_r_payload_rules)/sizeof(payload_rule_t)),informational_r_payload_rules},
+       {CREATE_CHILD_SA,TRUE,TRUE,(sizeof(create_child_sa_i_payload_rules)/sizeof(payload_rule_t)),create_child_sa_i_payload_rules},
+       {CREATE_CHILD_SA,FALSE,TRUE,(sizeof(create_child_sa_r_payload_rules)/sizeof(payload_rule_t)),create_child_sa_r_payload_rules},
 };
 
 
@@ -264,72 +304,6 @@ struct private_message_t {
         * The message rule for this message instance
         */
        message_rule_t *message_rule;
-       
-       /**
-        * Assigned logger.
-        */
-       logger_t *logger;
-       
-       /**
-        * Sets the private message_rule member to the rule which 
-        * applies to this message. Must be called before get_payload_rule().
-        * 
-        * @param this                                  calling object
-        * @return
-        *                                                              - SUCCESS
-        *                                                              - NOT_FOUND if no message rule applies to this message.
-        */
-       status_t (*set_message_rule) (private_message_t *this);
-
-       /**
-        * Gets the payload_rule_t for a specific message_rule_t and payload type.
-        * 
-        * @param this                                  calling object
-        * @param payload_type                  payload type
-        * @param[out] payload_rule             returned payload_rule_t
-        * @return
-        *                                                              - SUCCESS
-        *                                                              - NOT_FOUND if payload not defined in current message rule
-        *                                                              - INVALID_STATE if message rule is not set via set_message_rule()
-        */     
-       status_t (*get_payload_rule) (private_message_t *this, payload_type_t payload_type, payload_rule_t **payload_rule);
-       
-       /**
-        * Encrypts all payloads which has to get encrypted.
-        * 
-        * Can also be called with messages not containing encrypted content.
-        * 
-        * @param this                  calling object
-        * @param crypter               crypter_t object
-        * @param signer                signer_t object
-        * @return
-        *                                              - SUCCESS
-        *                                              - INVALID_STATE if no crypter/signer supplied but needed
-        */
-       status_t (*encrypt_payloads) (private_message_t *this,crypter_t *crypter, signer_t* signer);
-       
-       /**
-        * Decrypts encrypted contents, and checks if a payload is encrypted if it has to be.
-        * 
-        * @param this                  calling object
-        * @param crypter               crypter_t object
-        * @param signer                signer_t object
-        * @return
-        *                                              - SUCCESS
-        *                                              - FAILED if decryption not successfull
-        *                                              - INVALID_STATE if no crypter/signer supplied but needed
-        */
-       status_t (*decrypt_payloads) (private_message_t *this,crypter_t *crypter, signer_t* signer);
-       
-       /**
-        * Verifies the message. Checks for payloads count.
-        * 
-        * @param                               calling object
-        * @return
-        *                                              - SUCCESS if message valid, or
-        *                                              - FAILED if message does not align with message rules.
-        */
-       status_t (*verify) (private_message_t *this);   
 };
 
 /**
@@ -378,20 +352,16 @@ static status_t get_payload_rule(private_message_t *this, payload_type_t payload
  */
 static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
 {
+       DESTROY_IF(this->ike_sa_id);
        this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
 }
 
 /**
  * Implementation of message_t.get_ike_sa_id.
  */
-static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
+static ike_sa_id_t* get_ike_sa_id (private_message_t *this)
 {
-       if (this->ike_sa_id == NULL)
-       {
-               return FAILED;
-       }
-       *ike_sa_id = this->ike_sa_id->clone(this->ike_sa_id);
-       return SUCCESS;
+       return this->ike_sa_id;
 }
 
 /**
@@ -411,6 +381,14 @@ static u_int32_t get_message_id (private_message_t *this)
 }
 
 /**
+ * Implementation of message_t.get_initiator_spi.
+ */
+static u_int64_t get_initiator_spi (private_message_t *this)
+{
+       return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
+}
+
+/**
  * Implementation of message_t.get_responder_spi.
  */
 static u_int64_t get_responder_spi (private_message_t *this)
@@ -484,25 +462,82 @@ static exchange_type_t get_request (private_message_t *this)
 }
 
 /**
+ * Is this message in an encoded form?
+ */
+static bool is_encoded(private_message_t *this)
+{
+       chunk_t data = this->packet->get_data(this->packet);
+       
+       if (data.ptr == NULL)
+       {
+               return FALSE;
+       }
+       return TRUE;
+}
+
+/**
  * Implementation of message_t.add_payload.
  */
 static void add_payload(private_message_t *this, payload_t *payload)
 {
-       payload_t *last_payload;
-       if (this->payloads->get_count(this->payloads) > 0)
+       payload_t *last_payload, *first_payload;
+       
+       if ((this->is_request && payload->get_type(payload) == ID_INITIATOR) ||
+               (!this->is_request && payload->get_type(payload) == ID_RESPONDER))
        {
-               this->payloads->get_last(this->payloads,(void **) &last_payload);
-               last_payload->set_next_type(last_payload, payload->get_type(payload));
+               /* HOTD: insert ID payload in the beginning to respect RFC */
+               if (this->payloads->get_first(this->payloads,
+                                                                         (void **)&first_payload) == SUCCESS)
+               {
+                       payload->set_next_type(payload, first_payload->get_type(first_payload));
+               }
+               else
+               {
+                       payload->set_next_type(payload, NO_PAYLOAD);
+               }
+               this->first_payload = payload->get_type(payload);
+               this->payloads->insert_first(this->payloads, payload);
        }
        else
        {
-               this->first_payload = payload->get_type(payload);
+               if (this->payloads->get_count(this->payloads) > 0)
+               {
+                       this->payloads->get_last(this->payloads,(void **) &last_payload);
+                       last_payload->set_next_type(last_payload, payload->get_type(payload));
+               }
+               else
+               {
+                       this->first_payload = payload->get_type(payload);
+               }
+               payload->set_next_type(payload, NO_PAYLOAD);
+               this->payloads->insert_last(this->payloads, payload);
        }
-       payload->set_next_type(payload, NO_PAYLOAD);
-       this->payloads->insert_last(this->payloads, (void*)payload);
 
-       this->logger->log(this->logger, CONTROL|LEVEL1, "Added payload of type %s to message", 
-                                         mapping_find(payload_type_m, payload->get_type(payload)));
+       DBG2(DBG_ENC ,"added payload of type %N to message",
+                payload_type_names, payload->get_type(payload));
+}
+
+/**
+ * Implementation of message_t.add_notify.
+ */
+static void add_notify(private_message_t *this, bool flush, notify_type_t type, 
+                                          chunk_t data)
+{
+       notify_payload_t *notify;
+       payload_t *payload;
+       
+       if (flush)
+       {
+               while (this->payloads->remove_last(this->payloads, 
+                                                                                               (void**)&payload) == SUCCESS)
+               {
+                       payload->destroy(payload);
+               }
+       }
+       notify = notify_payload_create();
+       notify->set_notify_type(notify, type);
+       notify->set_notification_data(notify, data);
+       add_payload(this, (payload_t*)notify);
 }
 
 /**
@@ -518,7 +553,6 @@ static void set_source(private_message_t *this, host_t *host)
  */
 static void set_destination(private_message_t *this, host_t *host)
 {
-
        this->packet->set_destination(this->packet, host);
 }
 
@@ -539,13 +573,147 @@ static host_t * get_destination(private_message_t *this)
 }
 
 /**
- * Implementation of message_t.get_destination.
+ * Implementation of message_t.get_payload_iterator.
  */
 static iterator_t *get_payload_iterator(private_message_t *this)
 {
        return this->payloads->create_iterator(this->payloads, TRUE);
 }
 
+/**
+ * Implementation of message_t.get_payload.
+ */
+static payload_t *get_payload(private_message_t *this, payload_type_t type)
+{
+       payload_t *current, *found = NULL;
+       iterator_t *iterator;
+       
+       iterator = this->payloads->create_iterator(this->payloads, TRUE);
+       while (iterator->iterate(iterator, (void**)&current))
+       {
+               if (current->get_type(current) == type)
+               {
+                       found = current;
+                       break;
+               }
+       }
+       iterator->destroy(iterator);
+       return found;
+}
+
+/**
+ * get a string representation of the message
+ */
+static void get_string(private_message_t *this, char *buf, int len)
+{
+       iterator_t *iterator;
+       payload_t *payload;
+       int written;
+       
+       written = snprintf(buf, len, "%N %s [", 
+                                          exchange_type_names, this->exchange_type,
+                                          this->is_request ? "request" : "response");
+       if (written >= len || written < 0)
+       {
+               return;
+       }
+       buf += written;
+       len -= written;
+       
+       iterator = this->payloads->create_iterator(this->payloads, TRUE);
+       while (iterator->iterate(iterator, (void**)&payload))
+       {
+               written = snprintf(buf, len, "%N ", payload_type_short_names,
+                                                  payload->get_type(payload));
+               if (written >= len || written < 0)
+               {
+                       return;
+               }
+               buf += written;
+               len -= written;
+       }
+       iterator->destroy(iterator);
+       
+       /* remove last space */
+       len++;
+       buf--;
+       snprintf(buf, len, "]");
+}
+
+/**
+ * Implementation of private_message_t.encrypt_payloads.
+ */
+static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
+{
+       encryption_payload_t *encryption_payload = NULL;
+       status_t status;
+       linked_list_t *all_payloads;
+       
+       if (!this->message_rule->encrypted_content)
+       {
+               DBG2(DBG_ENC, "message doesn't have to be encrypted");
+               /* message contains no content to encrypt */
+               return SUCCESS;
+       }
+       
+       DBG2(DBG_ENC, "copy all payloads to a temporary list");
+       all_payloads = linked_list_create();
+       
+       /* first copy all payloads in a temporary list */
+       while (this->payloads->get_count(this->payloads) > 0)
+       {
+               void *current_payload;
+               this->payloads->remove_first(this->payloads,&current_payload);
+               all_payloads->insert_last(all_payloads,current_payload);
+       }
+       
+       encryption_payload = encryption_payload_create();
+
+       DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
+       while (all_payloads->get_count(all_payloads) > 0)
+       {
+               payload_rule_t *payload_rule;
+               payload_t *current_payload;
+               bool to_encrypt = FALSE;
+               
+               all_payloads->remove_first(all_payloads,(void **)&current_payload);
+               
+               status = get_payload_rule(this,
+                                       current_payload->get_type(current_payload),&payload_rule);
+               /* for payload types which are not found in supported payload list, 
+                * it is presumed that they don't have to be encrypted */
+               if ((status == SUCCESS) && (payload_rule->encrypted))
+               {
+                       DBG2(DBG_ENC, "payload %N gets encrypted",
+                                payload_type_names, current_payload->get_type(current_payload));
+                       to_encrypt = TRUE;
+               }
+               
+               if (to_encrypt)
+               {
+                       DBG2(DBG_ENC, "insert payload %N to encryption payload",
+                                payload_type_names, current_payload->get_type(current_payload));
+                       encryption_payload->add_payload(encryption_payload,current_payload);
+               }
+               else
+               {
+                       DBG2(DBG_ENC, "insert payload %N unencrypted",
+                                payload_type_names ,current_payload->get_type(current_payload));
+                       add_payload(this, (payload_t*)encryption_payload);
+               }
+       }
+
+       status = SUCCESS;
+       DBG2(DBG_ENC, "encrypting encryption payload");
+       encryption_payload->set_transforms(encryption_payload, crypter,signer);
+       status = encryption_payload->encrypt(encryption_payload);
+       DBG2(DBG_ENC, "add encrypted payload to payload list");
+       add_payload(this, (payload_t*)encryption_payload);
+       
+       all_payloads->destroy(all_payloads);
+       
+       return status;
+}
 
 /**
  * Implementation of message_t.generate.
@@ -558,49 +726,51 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
        iterator_t *iterator;
        status_t status;
        chunk_t packet_data;
+       char str[128];
+       
+       if (is_encoded(this))
+       {
+               /* already generated, return a new packet clone */
+               *packet = this->packet->clone(this->packet);
+               return SUCCESS;
+       }
        
-       this->logger->log(this->logger, CONTROL, "Generating %s %s, contains %d payloads",
-                                         mapping_find(exchange_type_m,this->exchange_type),
-                                         this->is_request ? "request" : "response",
-                                         this->payloads->get_count(this->payloads));
+       get_string(this, str, sizeof(str));
+       DBG1(DBG_ENC, "generating %s", str);
        
        if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
        {
-               this->logger->log(this->logger, ERROR | LEVEL1, "Exchange type %s is not defined",
-                                                       mapping_find(exchange_type_m,this->exchange_type));
+               DBG1(DBG_ENC, "exchange type is not defined");
                return INVALID_STATE;
        }
        
        if (this->packet->get_source(this->packet) == NULL ||
                this->packet->get_destination(this->packet) == NULL) 
        {
-               this->logger->log(this->logger, ERROR|LEVEL1, "%s not defined",
-                                                       !this->packet->get_source(this->packet) ? "source" : "destination");
+               DBG1(DBG_ENC, "%s not defined",
+                        !this->packet->get_source(this->packet) ? "source" : "destination");
                return INVALID_STATE;
        }
        
        /* set the rules for this messge */
-       status = this->set_message_rule(this);
+       status = set_message_rule(this);
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR, "No message rules specified for a %s %s",
-                                                 mapping_find(exchange_type_m,this->exchange_type),
-                                                 this->is_request ? "request" : "response");
+               DBG1(DBG_ENC, "no message rules specified for this message type");
                return NOT_SUPPORTED;
        }
        
-       
        /* going to encrypt all content which have to be encrypted */
-       status = this->encrypt_payloads(this, crypter, signer);
+       status = encrypt_payloads(this, crypter, signer);
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR | LEVEL1, "Could not encrypt payloads");
+               DBG1(DBG_ENC, "payload encryption failed");
                return status;
        }
-
+       
        /* build ike header */
        ike_header = ike_header_create();
-
+       
        ike_header->set_exchange_type(ike_header, this->exchange_type);
        ike_header->set_message_id(ike_header, this->message_id);
        ike_header->set_response_flag(ike_header, !this->is_request);
@@ -615,9 +785,8 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
        
        /* generate every payload expect last one, this is doen later*/
        iterator = this->payloads->create_iterator(this->payloads, TRUE);
-       while(iterator->has_next(iterator))
+       while(iterator->iterate(iterator, (void**)&next_payload))
        {
-               iterator->current(iterator, (void**)&next_payload);
                payload->set_next_type(payload, next_payload->get_type(next_payload));
                generator->generate_payload(generator, payload);
                payload = next_payload;
@@ -638,7 +807,7 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
        /* if last payload is of type encrypted, integrity checksum if necessary */
        if (payload->get_type(payload) == ENCRYPTED)
        {
-               this->logger->log(this->logger, CONTROL | LEVEL1, "Build signature on whole message");
+               DBG2(DBG_ENC, "build signature on whole message");
                encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
                status = encryption_payload->build_signature(encryption_payload, packet_data);
                if (status != SUCCESS)
@@ -652,8 +821,7 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
        /* clone packet for caller */
        *packet = this->packet->clone(this->packet);
        
-       this->logger->log(this->logger, CONTROL|LEVEL1, "Message of type %s generated successfully",
-                                               mapping_find(exchange_type_m,this->exchange_type));
+       DBG2(DBG_ENC, "message generated successfully");
        return SUCCESS;
 }
 
@@ -662,6 +830,10 @@ static status_t generate(private_message_t *this, crypter_t *crypter, signer_t*
  */
 static packet_t *get_packet (private_message_t *this)
 {
+       if (this->packet == NULL)
+       {
+               return NULL;
+       }
        return this->packet->clone(this->packet);
 }
 
@@ -670,6 +842,10 @@ static packet_t *get_packet (private_message_t *this)
  */
 static chunk_t get_packet_data (private_message_t *this)
 {
+       if (this->packet == NULL)
+       {
+               return chunk_empty;
+       }
        return chunk_clone(this->packet->get_data(this->packet));
 }
 
@@ -681,14 +857,13 @@ static status_t parse_header(private_message_t *this)
        ike_header_t *ike_header;
        status_t status;
        
-       
-       this->logger->log(this->logger, CONTROL|LEVEL1, "parsing Header of message");
+       DBG2(DBG_ENC, "parsing header of message");
        
        this->parser->reset_context(this->parser);
        status = this->parser->parse_payload(this->parser,HEADER,(payload_t **) &ike_header);
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR | LEVEL1, "Header could not be parsed");
+               DBG1(DBG_ENC, "header could not be parsed");
                return status;
                
        }
@@ -697,10 +872,10 @@ static status_t parse_header(private_message_t *this)
        status = ike_header->payload_interface.verify(&(ike_header->payload_interface));
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR | LEVEL1, "Header verification failed");
+               DBG1(DBG_ENC, "header verification failed");
                ike_header->destroy(ike_header);
                return status;
-       }       
+       }
        
        if (this->ike_sa_id != NULL)
        {
@@ -718,186 +893,24 @@ static status_t parse_header(private_message_t *this)
        this->minor_version = ike_header->get_min_version(ike_header);
        this->first_payload = ike_header->payload_interface.get_next_type(&(ike_header->payload_interface));
        
-       this->logger->log(this->logger, CONTROL|LEVEL1, "Parsed a %s %s", 
-                                               mapping_find(exchange_type_m, this->exchange_type),
-                                               this->is_request ? "request" : "response");
+       DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
+                this->is_request ? "request" : "response");
        
-       ike_header->destroy(ike_header);                                        
+       ike_header->destroy(ike_header);
        
        /* get the rules for this messge */
-       status = this->set_message_rule(this);
+       status = set_message_rule(this);
        if (status != SUCCESS)
        {
-               this->logger->log(this->logger, ERROR, "No message rules specified for a %s %s",
-                                                 mapping_find(exchange_type_m,this->exchange_type),
-                                                 this->is_request ? "request" : "response");
+               DBG1(DBG_ENC, "no message rules specified for a %N %s",
+                        exchange_type_names, this->exchange_type,
+                        this->is_request ? "request" : "response");
        }
        
-       return status;  
-}
-
-/**
- * Implementation of message_t.parse_body.
- */
-static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
-{
-       status_t status = SUCCESS;
-       payload_type_t current_payload_type;
-               
-       current_payload_type = this->first_payload;     
-               
-       this->logger->log(this->logger, CONTROL|LEVEL1, "Parsing body of message, first payload is %s",
-                                         mapping_find(payload_type_m, current_payload_type));
-
-       /* parse payload for payload, while there are more available */
-       while ((current_payload_type != NO_PAYLOAD))
-       {
-               payload_t *current_payload;
-               
-               this->logger->log(this->logger, CONTROL|LEVEL2, "Start parsing a %s payload", 
-                                                       mapping_find(payload_type_m, current_payload_type));
-               
-               /* parse current payload */
-               status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
-               
-               if (status != SUCCESS)
-               {
-                       this->logger->log(this->logger, ERROR, "Payload type %s could not be parsed",
-                                                               mapping_find(payload_type_m,current_payload_type));
-                       return status;
-               }
-
-               this->logger->log(this->logger, CONTROL|LEVEL2, "Verify payload of type %s", 
-                                                       mapping_find(payload_type_m, current_payload_type));
-               
-               /* verify it, stop parsig if its invalid */
-               status = current_payload->verify(current_payload);
-               if (status != SUCCESS)
-               {
-                       this->logger->log(this->logger, ERROR, "%s payload verification failed",
-                                                               mapping_find(payload_type_m,current_payload_type));
-                       current_payload->destroy(current_payload);
-                       status = VERIFY_ERROR;
-                       return status;
-               }
-               
-               this->logger->log(this->logger, CONTROL|LEVEL2, "%s payload verified. Adding to payload list", 
-                                                       mapping_find(payload_type_m, current_payload_type));
-               this->payloads->insert_last(this->payloads,current_payload);
-               
-               /* an encryption payload is the last one, so STOP here. decryption is done later */
-               if (current_payload_type == ENCRYPTED)
-               {
-                       this->logger->log(this->logger, CONTROL|LEVEL2, "%s payload found. Stop parsing", 
-                                                               mapping_find(payload_type_m, current_payload_type));                    
-                       break;  
-               }
-               
-               /* get next payload type */
-               current_payload_type = current_payload->get_next_type(current_payload);
-       }
-
-       if (current_payload_type == ENCRYPTED)
-       status = this->decrypt_payloads(this,crypter,signer);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Could not decrypt payloads");
-               return status;
-       }
-       
-       status = this->verify(this);
-       if (status != SUCCESS)
-       {
-               this->logger->log(this->logger, ERROR, "Verification of message failed");
-       }
-       
-       this->logger->log(this->logger, CONTROL, "Parsed %s %s, contains %d payloads", 
-                                       mapping_find(exchange_type_m, this->exchange_type),
-                                       this->is_request ? "request" : "response",
-                                       this->payloads->get_count(this->payloads));
-       
        return status;
 }
 
 /**
- * Implementation of private_message_t.verify.
- */
-static status_t verify(private_message_t *this)
-{
-       int i;
-       iterator_t *iterator;
-       size_t total_found_payloads = 0;
-       
-       this->logger->log(this->logger, CONTROL|LEVEL1, "Verifying message structure");
-
-       iterator = this->payloads->create_iterator(this->payloads,TRUE);
-       /* check for payloads with wrong count*/
-       for (i = 0; i < this->message_rule->payload_rule_count;i++)
-       {
-               size_t found_payloads = 0;
-       
-               /* check all payloads for specific rule */
-               iterator->reset(iterator);
-               
-               while(iterator->has_next(iterator))
-               {
-                       payload_t *current_payload;
-                       payload_type_t current_payload_type;
-                       
-                       iterator->current(iterator,(void **)&current_payload);
-                       current_payload_type = current_payload->get_type(current_payload);
-                       
-                       if (current_payload_type == UNKNOWN_PAYLOAD)
-                       {
-                               /* unknown payloads are ignored, IF they are not critical */
-                               unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
-                               if (unknown_payload->is_critical(unknown_payload))
-                               {
-                                       this->logger->log(this->logger, ERROR|LEVEL1, "%s (%d) is not supported, but its critical!",
-                                                                         mapping_find(payload_type_m, current_payload_type), current_payload_type);
-                                       iterator->destroy(iterator);
-                                       return NOT_SUPPORTED;   
-                               }
-                       }
-                       else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
-                       {
-                               found_payloads++;
-                               total_found_payloads++;
-                               this->logger->log(this->logger, CONTROL|LEVEL2, "Found payload of type %s",
-                                                         mapping_find(payload_type_m, this->message_rule->payload_rules[i].payload_type));
-       
-                               /* as soon as ohe payload occures more then specified, the verification fails */
-                               if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
-                               {
-                                       this->logger->log(this->logger, ERROR|LEVEL1, "Payload of type %s more than %d times (%d) occured in current message",
-                                                                         mapping_find(payload_type_m, current_payload_type),
-                                                                         this->message_rule->payload_rules[i].max_occurence, found_payloads);
-                                       iterator->destroy(iterator);
-                                       return FAILED;                                  
-                               }
-                       }
-               }
-
-               if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
-               {
-                       this->logger->log(this->logger, ERROR|LEVEL1, "Payload of type %s not occured %d times (%d)",
-                                                         mapping_find(payload_type_m, this->message_rule->payload_rules[i].payload_type),
-                                                         this->message_rule->payload_rules[i].min_occurence, found_payloads);
-                       iterator->destroy(iterator);
-                       return FAILED;
-               }
-               if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
-               {
-                       iterator->destroy(iterator);
-                       return SUCCESS; 
-               }
-       }
-       iterator->destroy(iterator);
-       return SUCCESS;
-}
-
-
-/**
  * Implementation of private_message_t.decrypt_and_verify_payloads.
  */
 static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, signer_t* signer)
@@ -906,25 +919,22 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
        payload_t *previous_payload = NULL;
        int payload_number = 1;
        iterator_t *iterator;
+       payload_t *current_payload;
        status_t status;
 
        iterator = this->payloads->create_iterator(this->payloads,TRUE);
 
        /* process each payload and decrypt a encryption payload */
-       while(iterator->has_next(iterator))
+       while(iterator->iterate(iterator, (void**)&current_payload))
        {
                payload_rule_t *payload_rule;
                payload_type_t current_payload_type;
-               payload_t *current_payload;
-
-               /* get current payload */               
-               iterator->current(iterator,(void **)&current_payload);
                
                /* needed to check */
                current_payload_type = current_payload->get_type(current_payload);
                
-               this->logger->log(this->logger, CONTROL|LEVEL2, "Process payload of type %s",
-                                                       mapping_find(payload_type_m,current_payload_type));
+               DBG2(DBG_ENC, "process payload of type %N",
+                        payload_type_names, current_payload_type);
                
                if (current_payload_type == ENCRYPTED)
                {
@@ -933,34 +943,33 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
                        
                        encryption_payload = (encryption_payload_t*)current_payload;
                        
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Found an encryption payload");
+                       DBG2(DBG_ENC, "found an encryption payload");
 
                        if (payload_number != this->payloads->get_count(this->payloads))
                        {
                                /* encrypted payload is not last one */
-                               this->logger->log(this->logger, ERROR | LEVEL1, "Encrypted payload is not last payload");
+                               DBG1(DBG_ENC, "encrypted payload is not last payload");
                                iterator->destroy(iterator);
-                               return FAILED;
+                               return VERIFY_ERROR;
                        }
                        /* decrypt */
                        encryption_payload->set_transforms(encryption_payload, crypter, signer);
-                       this->logger->log(this->logger, CONTROL | LEVEL1, "Verify signature of encryption payload");
-                       status = encryption_payload->verify_signature(encryption_payload, this->packet->get_data(this->packet));
+                       DBG2(DBG_ENC, "verify signature of encryption payload");
+                       status = encryption_payload->verify_signature(encryption_payload,
+                                                                               this->packet->get_data(this->packet));
                        if (status != SUCCESS)
                        {
-                               this->logger->log(this->logger, ERROR | LEVEL1, "encryption payload signature invalid");
+                               DBG1(DBG_ENC, "encryption payload signature invalid");
                                iterator->destroy(iterator);
-                               return status;
+                               return FAILED;
                        }
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Decrypt content of encryption payload");
+                       DBG2(DBG_ENC, "decrypting content of encryption payload");
                        status = encryption_payload->decrypt(encryption_payload);
                        if (status != SUCCESS)
                        {
-                               this->logger->log(this->logger, ERROR | LEVEL1, 
-                                                                 "Encrypted payload could not be decrypted and parsed: %s", 
-                                                                 mapping_find(status_m, status));
+                               DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
                                iterator->destroy(iterator);
-                               return status;
+                               return PARSE_ERROR;
                        }
                        
                        /* needed later to find out if a payload was encrypted */
@@ -969,7 +978,7 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
                        /* check if there are payloads contained in the encryption payload */
                        if (encryption_payload->get_payload_count(encryption_payload) == 0)
                        {
-                               this->logger->log(this->logger, CONTROL|LEVEL2, "Encrypted payload is empty");
+                               DBG2(DBG_ENC, "encrypted payload is empty");
                                /* remove the encryption payload, is not needed anymore */
                                iterator->remove(iterator);
                                /* encrypted payload contains no other payload */
@@ -999,9 +1008,8 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
                        while (encryption_payload->get_payload_count(encryption_payload) > 0)
                        {
                                encryption_payload->remove_first_payload(encryption_payload, &current_encrypted_payload);
-                               this->logger->log(this->logger, CONTROL | LEVEL1, 
-                                                                 "Insert unencrypted payload of type %s at end of list.",
-                                                                  mapping_find(payload_type_m, current_encrypted_payload->get_type(current_encrypted_payload)));
+                               DBG2(DBG_ENC, "insert unencrypted payload of type %N at end of list",
+                                        payload_type_names, current_encrypted_payload->get_type(current_encrypted_payload));
                                this->payloads->insert_last(this->payloads,current_encrypted_payload);
                        }
                        
@@ -1013,25 +1021,25 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
                if (current_payload_type != UNKNOWN_PAYLOAD && current_payload_type != NO_PAYLOAD)
                {
                        /* get the ruleset for found payload */
-                       status = this->get_payload_rule(this, current_payload_type, &payload_rule);
+                       status = get_payload_rule(this, current_payload_type, &payload_rule);
                        if (status != SUCCESS)
                        {
                                /* payload is not allowed */
-                               this->logger->log(this->logger, ERROR | LEVEL1, "Payload type %s not allowed",
-                                                                 mapping_find(payload_type_m,current_payload_type));
+                               DBG1(DBG_ENC, "payload type %N not allowed",
+                                                                 payload_type_names, current_payload_type);
                                iterator->destroy(iterator);
-                               return status;
+                               return VERIFY_ERROR;
                        }
                        
                        /* check if the payload was encrypted, and if it should been have encrypted */
                        if (payload_rule->encrypted != current_payload_was_encrypted)
                        {
                                /* payload was not encrypted, but should have been. or vice-versa */
-                               this->logger->log(this->logger, ERROR | LEVEL1, "Payload type %s should be %s!", 
-                                                                       mapping_find(payload_type_m,current_payload_type),
-                                                                       (payload_rule->encrypted) ? "encrypted" : "not encrypted");
+                               DBG1(DBG_ENC, "payload type %N should be %s!",
+                                        payload_type_names, current_payload_type,
+                                        (payload_rule->encrypted) ? "encrypted" : "not encrypted");
                                iterator->destroy(iterator);
-                               return FAILED;
+                               return VERIFY_ERROR;
                        }
                }
                /* advance to the next payload */
@@ -1044,119 +1052,172 @@ static status_t decrypt_payloads(private_message_t *this,crypter_t *crypter, sig
 }
 
 /**
- * Implementation of private_message_t.encrypt_payloads.
+ * Implementation of private_message_t.verify.
  */
-static status_t encrypt_payloads (private_message_t *this,crypter_t *crypter, signer_t* signer)
+static status_t verify(private_message_t *this)
 {
-       encryption_payload_t *encryption_payload = NULL;
-       status_t status;
-       linked_list_t *all_payloads;
-       
-       if (!this->message_rule->encrypted_content)
-       {
-               this->logger->log(this->logger, CONTROL | LEVEL1, "Message doesn't have to be encrypted");
-               /* message contains no content to encrypt */
-               return SUCCESS;
-       }
+       int i;
+       iterator_t *iterator;
+       payload_t *current_payload;
+       size_t total_found_payloads = 0;
        
-       this->logger->log(this->logger, CONTROL | LEVEL2, "Copy all payloads to a temporary list");
-       all_payloads = linked_list_create();
+       DBG2(DBG_ENC, "verifying message structure");
        
-       /* first copy all payloads in a temporary list */
-       while (this->payloads->get_count(this->payloads) > 0)
+       iterator = this->payloads->create_iterator(this->payloads,TRUE);
+       /* check for payloads with wrong count*/
+       for (i = 0; i < this->message_rule->payload_rule_count;i++)
        {
-               void *current_payload;
-               this->payloads->remove_first(this->payloads,&current_payload);
-               all_payloads->insert_last(all_payloads,current_payload);
-       }
+               size_t found_payloads = 0;
        
-       encryption_payload = encryption_payload_create();
+               /* check all payloads for specific rule */
+               iterator->reset(iterator);
+               
+               while(iterator->iterate(iterator,(void **)&current_payload))
+               {
+                       payload_type_t current_payload_type;
+                       
+                       current_payload_type = current_payload->get_type(current_payload);
+                       if (current_payload_type == UNKNOWN_PAYLOAD)
+                       {
+                               /* unknown payloads are ignored, IF they are not critical */
+                               unknown_payload_t *unknown_payload = (unknown_payload_t*)current_payload;
+                               if (unknown_payload->is_critical(unknown_payload))
+                               {
+                                       DBG1(DBG_ENC, "%N is not supported, but its critical!",
+                                                payload_type_names, current_payload_type);
+                                       iterator->destroy(iterator);
+                                       return NOT_SUPPORTED;   
+                               }
+                       }
+                       else if (current_payload_type == this->message_rule->payload_rules[i].payload_type)
+                       {
+                               found_payloads++;
+                               total_found_payloads++;
+                               DBG2(DBG_ENC, "found payload of type %N",
+                                        payload_type_names, this->message_rule->payload_rules[i].payload_type);
+                               
+                               /* as soon as ohe payload occures more then specified, the verification fails */
+                               if (found_payloads > this->message_rule->payload_rules[i].max_occurence)
+                               {
+                                       DBG1(DBG_ENC, "payload of type %N more than %d times (%d) occured in current message",
+                                                payload_type_names, current_payload_type,
+                                                this->message_rule->payload_rules[i].max_occurence, found_payloads);
+                                       iterator->destroy(iterator);
+                                       return VERIFY_ERROR;
+                               }
+                       }
+               }
+               
+               if (found_payloads < this->message_rule->payload_rules[i].min_occurence)
+               {
+                       DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
+                                payload_type_names, this->message_rule->payload_rules[i].payload_type,
+                                this->message_rule->payload_rules[i].min_occurence, found_payloads);
+                       iterator->destroy(iterator);
+                       return VERIFY_ERROR;
+               }
+               if ((this->message_rule->payload_rules[i].sufficient) && (this->payloads->get_count(this->payloads) == total_found_payloads))
+               {
+                       iterator->destroy(iterator);
+                       return SUCCESS; 
+               }
+       }
+       iterator->destroy(iterator);
+       return SUCCESS;
+}
 
-       this->logger->log(this->logger, CONTROL | LEVEL2, "Check each payloads if they have to get encrypted");
-       while (all_payloads->get_count(all_payloads) > 0)
+/**
+ * Implementation of message_t.parse_body.
+ */
+static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t *signer)
+{
+       status_t status = SUCCESS;
+       payload_type_t current_payload_type;
+       char str[128];
+               
+       current_payload_type = this->first_payload;     
+               
+       DBG2(DBG_ENC, "parsing body of message, first payload is %N",
+                payload_type_names, current_payload_type);
+
+       /* parse payload for payload, while there are more available */
+       while ((current_payload_type != NO_PAYLOAD))
        {
-               payload_rule_t *payload_rule;
                payload_t *current_payload;
-               bool to_encrypt = FALSE;
                
-               all_payloads->remove_first(all_payloads,(void **)&current_payload);
-               this->logger->log(this->logger, CONTROL | LEVEL3, "Get rule for payload %s", 
-                                                       mapping_find(payload_type_m,current_payload->get_type(current_payload)));
+               DBG2(DBG_ENC, "starting parsing a %N payload", 
+                        payload_type_names, current_payload_type);
                
-               status = this->get_payload_rule(this,current_payload->get_type(current_payload),&payload_rule);
-               /* for payload types which are not found in supported payload list, it is presumed 
-                * that they don't have to be encrypted */
-               if ((status == SUCCESS) && (payload_rule->encrypted))
+               /* parse current payload */
+               status = this->parser->parse_payload(this->parser,current_payload_type,(payload_t **) &current_payload);
+               
+               if (status != SUCCESS)
                {
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Payload %s has to get encrypted", 
-                                                         mapping_find(payload_type_m,current_payload->get_type(current_payload)));
-                       to_encrypt = TRUE;
+                       DBG1(DBG_ENC, "payload type %N could not be parsed",
+                                payload_type_names, current_payload_type);
+                       return PARSE_ERROR;
                }
-               else if (status != SUCCESS)
+
+               DBG2(DBG_ENC, "verifying payload of type %N",
+                        payload_type_names, current_payload_type);
+               
+               /* verify it, stop parsig if its invalid */
+               status = current_payload->verify(current_payload);
+               if (status != SUCCESS)
                {
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Payload %s not defined for exchange type %s. Handle it anyway", 
-                                                         mapping_find(payload_type_m,current_payload->get_type(current_payload)),
-                                                         mapping_find(exchange_type_m,this->exchange_type));
+                       DBG1(DBG_ENC, "%N payload verification failed",
+                                payload_type_names, current_payload_type);
+                       current_payload->destroy(current_payload);
+                       return VERIFY_ERROR;
                }
                
-               if (to_encrypt)
+               DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
+                        payload_type_names, current_payload_type);
+               this->payloads->insert_last(this->payloads,current_payload);
+               
+               /* an encryption payload is the last one, so STOP here. decryption is done later */
+               if (current_payload_type == ENCRYPTED)
                {
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Insert payload %s to encryption payload", 
-                                                         mapping_find(payload_type_m,current_payload->get_type(current_payload)));
-
-                       encryption_payload->add_payload(encryption_payload,current_payload);
+                       DBG2(DBG_ENC, "%N payload found. Stop parsing",
+                                payload_type_names, current_payload_type);
+                       break;
                }
-               else
+               
+               /* get next payload type */
+               current_payload_type = current_payload->get_next_type(current_payload);
+       }
+
+       if (current_payload_type == ENCRYPTED)
+       {
+               status = decrypt_payloads(this,crypter,signer);
+               if (status != SUCCESS)
                {
-                       this->logger->log(this->logger, CONTROL | LEVEL2, "Insert payload %s as payload wich does not have to be encrypted", 
-                                                         mapping_find(payload_type_m,current_payload->get_type(current_payload)));
-                       this->public.add_payload(&(this->public), (payload_t*)encryption_payload);
+                       DBG1(DBG_ENC, "could not decrypt payloads");
+                       return status;
                }
        }
-
-       status = SUCCESS;
-       this->logger->log(this->logger, CONTROL | LEVEL2, "Set transforms for encryption payload ");
-       encryption_payload->set_transforms(encryption_payload,crypter,signer);
-       this->logger->log(this->logger, CONTROL | LEVEL1, "Encrypt all payloads of encrypted payload");
-       status = encryption_payload->encrypt(encryption_payload);
-       this->logger->log(this->logger, CONTROL | LEVEL2, "Add encrypted payload to payload list");
-       this->public.add_payload(&(this->public), (payload_t*)encryption_payload);
        
-       all_payloads->destroy(all_payloads);
+       status = verify(this);
+       if (status != SUCCESS)
+       {
+               return status;
+       }
        
-       return status;
+       get_string(this, str, sizeof(str));
+       DBG1(DBG_ENC, "parsed %s", str);
+       
+       return SUCCESS;
 }
 
-
 /**
  * Implementation of message_t.destroy.
  */
 static void destroy (private_message_t *this)
 {
-       iterator_t *iterator;
-       
-       this->logger->log(this->logger, CONTROL|LEVEL3, "Going to destroy message_t object");
-       
+       DESTROY_IF(this->ike_sa_id);
+       this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
        this->packet->destroy(this->packet);
-
-       if (this->ike_sa_id != NULL)
-       {
-               this->ike_sa_id->destroy(this->ike_sa_id);
-       }
-       
-       iterator = this->payloads->create_iterator(this->payloads, TRUE);
-       while (iterator->has_next(iterator))
-       {
-               payload_t *payload;
-               iterator->current(iterator, (void**)&payload);  
-               this->logger->log(this->logger, CONTROL|LEVEL3, "Destroying payload of type %s", 
-                                                       mapping_find(payload_type_m, payload->get_type(payload)));
-               payload->destroy(payload);
-       }
-       iterator->destroy(iterator);
-       this->payloads->destroy(this->payloads);
        this->parser->destroy(this->parser);
-       
        free(this);
 }
 
@@ -1174,20 +1235,23 @@ message_t *message_create_from_packet(packet_t *packet)
        this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
        this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
        this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
-       this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;   
+       this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
+       this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
        this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
-       this->public.get_ike_sa_id = (status_t(*)(message_t*, ike_sa_id_t **))get_ike_sa_id;
+       this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
        this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
        this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
        this->public.set_request = (void(*)(message_t*, bool))set_request;
        this->public.get_request = (bool(*)(message_t*))get_request;
        this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
+       this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
        this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
        this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
        this->public.get_source = (host_t * (*) (message_t*)) get_source;
        this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
        this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
        this->public.get_payload_iterator = (iterator_t * (*) (message_t *)) get_payload_iterator;
+       this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
        this->public.parse_header = (status_t (*) (message_t *)) parse_header;
        this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
        this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
@@ -1196,18 +1260,11 @@ message_t *message_create_from_packet(packet_t *packet)
                
        /* private values */
        this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
-       this->is_request = TRUE;
-       this->ike_sa_id = NULL;
-       this->first_payload = NO_PAYLOAD;
-       this->message_id = 0;
-
-       /* private functions */
-       this->set_message_rule = set_message_rule;
-       this->get_payload_rule = get_payload_rule;
-       this->encrypt_payloads = encrypt_payloads;
-       this->decrypt_payloads = decrypt_payloads;
-       this->verify = verify;
-
+       this->is_request = TRUE;
+       this->ike_sa_id = NULL;
+       this->first_payload = NO_PAYLOAD;
+       this->message_id = 0;
+       
        /* private values */
        if (packet == NULL)
        {
@@ -1218,10 +1275,8 @@ message_t *message_create_from_packet(packet_t *packet)
        this->payloads = linked_list_create();
        
        /* parser is created from data of packet */
-       this->parser = parser_create(this->packet->get_data(this->packet));
-               
-       this->logger = logger_manager->get_logger(logger_manager, MESSAGE);
-
+       this->parser = parser_create(this->packet->get_data(this->packet));
+       
        return (&this->public);
 }
 
@@ -1232,24 +1287,3 @@ message_t *message_create()
 {
        return message_create_from_packet(NULL);
 }
-
-/*
- * Described in Header.
- */
-message_t *message_create_notify_reply(host_t *source, host_t *destination, exchange_type_t exchange_type, bool original_initiator,ike_sa_id_t *ike_sa_id,notify_message_type_t notify_type)
-{
-       message_t *message = message_create_from_packet(NULL);
-       notify_payload_t *payload;
-       
-       message->set_source(message, source->clone(source));
-       message->set_destination(message, destination->clone(destination));
-       message->set_exchange_type(message, exchange_type);
-       message->set_request(message, FALSE);
-       message->set_message_id(message,0);
-       message->set_ike_sa_id(message, ike_sa_id);
-       
-       payload = notify_payload_create_from_protocol_and_type(PROTO_IKE, notify_type);
-       message->add_payload(message,(payload_t *) payload);
-       
-       return message;
-}