diff options
Diffstat (limited to 'src/charon/encoding/payloads/eap_payload.c')
-rw-r--r-- | src/charon/encoding/payloads/eap_payload.c | 181 |
1 files changed, 142 insertions, 39 deletions
diff --git a/src/charon/encoding/payloads/eap_payload.c b/src/charon/encoding/payloads/eap_payload.c index aa886e9c4..79ab32fe5 100644 --- a/src/charon/encoding/payloads/eap_payload.c +++ b/src/charon/encoding/payloads/eap_payload.c @@ -25,6 +25,7 @@ #include "eap_payload.h" +#include <daemon.h> typedef struct private_eap_payload_t private_eap_payload_t; @@ -54,9 +55,9 @@ struct private_eap_payload_t { u_int16_t payload_length; /** - * The contained message. + * EAP message data, if available */ - chunk_t message; + chunk_t data; }; /** @@ -80,21 +81,21 @@ encoding_rule_t eap_payload_encodings[] = { { RESERVED_BIT, 0 }, { RESERVED_BIT, 0 }, /* Length of the whole payload*/ - { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length)}, - /* some eap data bytes, length is defined in PAYLOAD_LENGTH */ - { EAP_MESSAGE, offsetof(private_eap_payload_t, message) } + { PAYLOAD_LENGTH, offsetof(private_eap_payload_t, payload_length) }, + /* chunt to data, starting at "code" */ + { EAP_DATA, offsetof(private_eap_payload_t, data) }, }; /* - 1 2 3 + 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ! Next Payload !C! RESERVED ! Payload Length ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - ! ! - ~ EAP Message ~ - ! ! + ! Code ! Identifier ! Length ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + ! Type ! Type_Data... + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */ /** @@ -102,6 +103,47 @@ encoding_rule_t eap_payload_encodings[] = { */ static status_t verify(private_eap_payload_t *this) { + u_int16_t length; + u_int8_t code; + + if (this->data.len < 4) + { + DBG1(DBG_ENC, "EAP payloads EAP message too short (%d)", this->data.len); + return FAILED; + } + code = *this->data.ptr; + length = htons(*(u_int16_t*)(this->data.ptr + 2)); + if (this->data.len != length) + { + DBG1(DBG_ENC, "EAP payload length (%d) does not match contained message length (%d)", + this->data.len, length); + return FAILED; + } + switch (code) + { + case EAP_REQUEST: + case EAP_RESPONSE: + { + if (this->data.len < 4) + { + DBG1(DBG_ENC, "EAP Request/Response does not have any data"); + return FAILED; + } + break; + } + case EAP_SUCCESS: + case EAP_FAILURE: + { + if (this->data.len != 4) + { + DBG1(DBG_ENC, "EAP Success/Failure has data"); + return FAILED; + } + break; + } + default: + return FAILED; + } return SUCCESS; } @@ -147,40 +189,59 @@ static size_t get_length(private_eap_payload_t *this) } /** - * Implementation of eap_payload_t.set_message. + * Implementation of eap_payload_t.get_data. + */ +static chunk_t get_data(private_eap_payload_t *this) +{ + return this->data; +} + +/** + * Implementation of eap_payload_t.set_data. */ -static void set_message (private_eap_payload_t *this, chunk_t message) +static void set_data(private_eap_payload_t *this, chunk_t data) { - if (this->message.ptr != NULL) + chunk_free(&this->data); + this->data = chunk_clone(data); + this->payload_length = this->data.len + 4; +} + +/** + * Implementation of eap_payload_t.get_code. + */ +static eap_code_t get_code(private_eap_payload_t *this) +{ + if (this->data.len > 0) { - chunk_free(&(this->message)); + return *this->data.ptr; } - this->message.ptr = clalloc(message.ptr,message.len); - this->message.len = message.len; - this->payload_length = EAP_PAYLOAD_HEADER_LENGTH + this->message.len; + /* should not happen, as it is verified */ + return 0; } /** - * Implementation of eap_payload_t.get_message. + * Implementation of eap_payload_t.get_identifier. */ -static chunk_t get_message (private_eap_payload_t *this) +static u_int8_t get_identifier(private_eap_payload_t *this) { - return (this->message); + if (this->data.len > 1) + { + return *(this->data.ptr + 1); + } + /* should not happen, as it is verified */ + return 0; } /** - * Implementation of eap_payload_t.get_data_clone. + * Implementation of eap_payload_t.get_type. */ -static chunk_t get_message_clone (private_eap_payload_t *this) +static eap_type_t get_type(private_eap_payload_t *this) { - chunk_t cloned_message; - if (this->message.ptr == NULL) + if (this->data.len > 4) { - return (this->message); + return *(this->data.ptr + 4); } - cloned_message.ptr = clalloc(this->message.ptr,this->message.len); - cloned_message.len = this->message.len; - return cloned_message; + return 0; } /** @@ -188,12 +249,8 @@ static chunk_t get_message_clone (private_eap_payload_t *this) */ static void destroy(private_eap_payload_t *this) { - if (this->message.ptr != NULL) - { - chunk_free(&(this->message)); - } - - free(this); + chunk_free(&this->data); + free(this); } /* @@ -202,7 +259,7 @@ static void destroy(private_eap_payload_t *this) eap_payload_t *eap_payload_create() { private_eap_payload_t *this = malloc_thing(private_eap_payload_t); - + /* interface functions */ this->public.payload_interface.verify = (status_t (*) (payload_t *))verify; this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules; @@ -214,15 +271,61 @@ eap_payload_t *eap_payload_create() /* public functions */ this->public.destroy = (void (*) (eap_payload_t *)) destroy; - this->public.set_message = (void (*) (eap_payload_t *,chunk_t)) set_message; - this->public.get_message_clone = (chunk_t (*) (eap_payload_t *)) get_message_clone; - this->public.get_message = (chunk_t (*) (eap_payload_t *)) get_message; + this->public.get_data = (chunk_t (*) (eap_payload_t*))get_data; + this->public.set_data = (void (*) (eap_payload_t *,chunk_t))set_data; + this->public.get_code = (eap_code_t (*) (eap_payload_t*))get_code; + this->public.get_identifier = (u_int8_t (*) (eap_payload_t*))get_identifier; + this->public.get_type = (eap_type_t (*) (eap_payload_t*))get_type; /* private variables */ this->critical = FALSE; this->next_payload = NO_PAYLOAD; this->payload_length = EAP_PAYLOAD_HEADER_LENGTH; - this->message = chunk_empty; + this->data = chunk_empty; + + return &(this->public); +} + +/* + * Described in header + */ +eap_payload_t *eap_payload_create_data(chunk_t data) +{ + eap_payload_t *this = eap_payload_create(); + + this->set_data(this, data); + return this; +} - return (&(this->public)); +/* + * Described in header + */ +eap_payload_t *eap_payload_create_code(eap_code_t code) +{ + eap_payload_t *this = eap_payload_create(); + chunk_t data = chunk_alloca(4); + + *(data.ptr + 0) = code; + *(data.ptr + 1) = 0; + *(u_int16_t*)(data.ptr + 2) = htons(data.len); + + this->set_data(this, data); + return this; +} + +/* + * Described in header + */ +eap_payload_t *eap_payload_create_nak() +{ + eap_payload_t *this = eap_payload_create(); + chunk_t data = chunk_alloca(5); + + *(data.ptr + 0) = EAP_RESPONSE; + *(data.ptr + 1) = 0; + *(u_int16_t*)(data.ptr + 2) = htons(data.len); + *(data.ptr + 4) = EAP_NAK; + + this->set_data(this, data); + return this; } |