2005-12-05 13:43:43 +00:00
|
|
|
/*
|
2010-08-25 15:00:01 +00:00
|
|
|
* Copyright (C) 2005-2010 Martin Willi
|
|
|
|
* Copyright (C) 2010 revosec AG
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-12-05 13:43:43 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#include <stddef.h>
|
2005-12-05 13:43:43 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#include "delete_payload.h"
|
2005-12-05 13:43:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct private_delete_payload_t private_delete_payload_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private data of an delete_payload_t object.
|
|
|
|
*/
|
|
|
|
struct private_delete_payload_t {
|
2011-11-17 17:14:51 +00:00
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* Public delete_payload_t interface.
|
|
|
|
*/
|
|
|
|
delete_payload_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* Next payload type.
|
|
|
|
*/
|
|
|
|
u_int8_t next_payload;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Critical flag.
|
|
|
|
*/
|
|
|
|
bool critical;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-11-24 15:34:16 +00:00
|
|
|
/**
|
|
|
|
* reserved bits
|
|
|
|
*/
|
2011-11-17 17:14:51 +00:00
|
|
|
bool reserved[8];
|
2010-11-24 15:34:16 +00:00
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* Length of this payload.
|
|
|
|
*/
|
|
|
|
u_int16_t payload_length;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-11-17 17:14:51 +00:00
|
|
|
/**
|
|
|
|
* IKEv1 Domain of Interpretation
|
|
|
|
*/
|
|
|
|
u_int32_t doi;
|
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* Protocol ID.
|
|
|
|
*/
|
|
|
|
u_int8_t protocol_id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SPI Size.
|
|
|
|
*/
|
|
|
|
u_int8_t spi_size;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* Number of SPI's.
|
|
|
|
*/
|
|
|
|
u_int16_t spi_count;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-12-05 13:43:43 +00:00
|
|
|
/**
|
|
|
|
* The contained SPI's.
|
|
|
|
*/
|
|
|
|
chunk_t spis;
|
2011-11-17 17:14:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Payload type, DELETE or DELETE_V1
|
|
|
|
*/
|
|
|
|
payload_type_t type;
|
2005-12-05 13:43:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-17 17:14:51 +00:00
|
|
|
* Encoding rules for an IKEv2 delete payload.
|
2005-12-05 13:43:43 +00:00
|
|
|
*/
|
2011-11-17 17:14:51 +00:00
|
|
|
static encoding_rule_t encodings_v2[] = {
|
2009-09-04 13:02:11 +00:00
|
|
|
/* 1 Byte next payload type, stored in the field next_payload */
|
2011-11-23 11:35:10 +00:00
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, next_payload) },
|
2005-12-05 13:43:43 +00:00
|
|
|
/* the critical bit */
|
2011-11-23 11:35:10 +00:00
|
|
|
{ FLAG, offsetof(private_delete_payload_t, critical) },
|
2010-11-24 15:34:16 +00:00
|
|
|
/* 7 Bit reserved bits */
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[0]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[1]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[2]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[3]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[4]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[5]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[6]) },
|
2005-12-05 13:43:43 +00:00
|
|
|
/* Length of the whole payload*/
|
2010-08-25 15:00:01 +00:00
|
|
|
{ PAYLOAD_LENGTH, offsetof(private_delete_payload_t, payload_length) },
|
2005-12-05 13:43:43 +00:00
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, protocol_id) },
|
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, spi_size) },
|
|
|
|
{ U_INT_16, offsetof(private_delete_payload_t, spi_count) },
|
|
|
|
/* some delete data bytes, length is defined in PAYLOAD_LENGTH */
|
2011-11-23 11:35:10 +00:00
|
|
|
{ CHUNK_DATA, offsetof(private_delete_payload_t, spis) },
|
2011-11-17 17:14:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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 !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! Protocol ID ! SPI Size ! # of SPIs !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! !
|
|
|
|
~ Security Parameter Index(es) (SPI) ~
|
|
|
|
! !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encoding rules for an IKEv1 delete payload.
|
|
|
|
*/
|
|
|
|
static encoding_rule_t encodings_v1[] = {
|
|
|
|
/* 1 Byte next payload type, stored in the field next_payload */
|
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, next_payload) },
|
|
|
|
/* 8 Bit reserved bits */
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[0]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[1]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[2]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[3]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[4]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[5]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[6]) },
|
|
|
|
{ RESERVED_BIT, offsetof(private_delete_payload_t, reserved[7]) },
|
|
|
|
/* Length of the whole payload*/
|
|
|
|
{ PAYLOAD_LENGTH, offsetof(private_delete_payload_t, payload_length) },
|
|
|
|
/* Domain of interpretation */
|
|
|
|
{ U_INT_32, offsetof(private_delete_payload_t, doi) },
|
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, protocol_id) },
|
|
|
|
{ U_INT_8, offsetof(private_delete_payload_t, spi_size) },
|
|
|
|
{ U_INT_16, offsetof(private_delete_payload_t, spi_count) },
|
|
|
|
/* some delete data bytes, length is defined in PAYLOAD_LENGTH */
|
2011-11-23 11:35:10 +00:00
|
|
|
{ CHUNK_DATA, offsetof(private_delete_payload_t, spis) },
|
2005-12-05 13:43:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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 !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2011-11-17 17:14:51 +00:00
|
|
|
! DOI !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2005-12-05 13:43:43 +00:00
|
|
|
! Protocol ID ! SPI Size ! # of SPIs !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! !
|
|
|
|
~ Security Parameter Index(es) (SPI) ~
|
|
|
|
! !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(payload_t, verify, status_t,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2006-06-09 15:12:43 +00:00
|
|
|
switch (this->protocol_id)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2006-06-09 15:12:43 +00:00
|
|
|
case PROTO_AH:
|
|
|
|
case PROTO_ESP:
|
|
|
|
if (this->spi_size != 4)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PROTO_IKE:
|
|
|
|
case 0:
|
|
|
|
/* IKE deletion has no spi assigned! */
|
|
|
|
if (this->spi_size != 0)
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FAILED;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
if (this->spis.len != (this->spi_count * this->spi_size))
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2011-11-17 10:27:55 +00:00
|
|
|
METHOD(payload_t, get_encoding_rules, int,
|
|
|
|
private_delete_payload_t *this, encoding_rule_t **rules)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2011-11-17 17:14:51 +00:00
|
|
|
if (this->type == DELETE)
|
|
|
|
{
|
|
|
|
*rules = encodings_v2;
|
|
|
|
return countof(encodings_v2);
|
|
|
|
}
|
|
|
|
*rules = encodings_v1;
|
|
|
|
return countof(encodings_v1);
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-17 11:27:46 +00:00
|
|
|
METHOD(payload_t, get_header_length, int,
|
|
|
|
private_delete_payload_t *this)
|
|
|
|
{
|
2011-11-17 17:14:51 +00:00
|
|
|
if (this->type == DELETE)
|
|
|
|
{
|
2011-11-23 11:35:10 +00:00
|
|
|
return 8;
|
2011-11-17 17:14:51 +00:00
|
|
|
}
|
2011-11-23 11:35:10 +00:00
|
|
|
return 12;
|
2011-11-17 11:27:46 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(payload_t, get_payload_type, payload_type_t,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2011-11-17 17:14:51 +00:00
|
|
|
return this->type;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(payload_t, get_next_type, payload_type_t,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
return this->next_payload;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(payload_t, set_next_type, void,
|
|
|
|
private_delete_payload_t *this,payload_type_t type)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
|
|
|
this->next_payload = type;
|
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(payload_t, get_length, size_t,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
|
|
|
return this->payload_length;
|
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(delete_payload_t, get_protocol_id, protocol_id_t,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
return this->protocol_id;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(delete_payload_t, add_spi, void,
|
|
|
|
private_delete_payload_t *this, u_int32_t spi)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
switch (this->protocol_id)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
case PROTO_AH:
|
|
|
|
case PROTO_ESP:
|
|
|
|
this->spi_count++;
|
2010-08-25 15:03:09 +00:00
|
|
|
this->payload_length += sizeof(spi);
|
2011-11-21 09:22:50 +00:00
|
|
|
this->spi_size += sizeof(spi);
|
2010-08-25 15:00:01 +00:00
|
|
|
this->spis = chunk_cat("mc", this->spis, chunk_from_thing(spi));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
2006-06-09 15:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-25 15:00:01 +00:00
|
|
|
* SPI enumerator implementation
|
2006-06-09 15:12:43 +00:00
|
|
|
*/
|
2010-08-25 15:00:01 +00:00
|
|
|
typedef struct {
|
|
|
|
/** implements enumerator_t */
|
|
|
|
enumerator_t public;
|
|
|
|
/** remaining SPIs */
|
|
|
|
chunk_t spis;
|
|
|
|
} spi_enumerator_t;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(enumerator_t, spis_enumerate, bool,
|
|
|
|
spi_enumerator_t *this, u_int32_t *spi)
|
|
|
|
{
|
|
|
|
if (this->spis.len >= sizeof(*spi))
|
2006-06-09 15:12:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
memcpy(spi, this->spis.ptr, sizeof(*spi));
|
|
|
|
this->spis = chunk_skip(this->spis, sizeof(*spi));
|
|
|
|
return TRUE;
|
2006-06-09 15:12:43 +00:00
|
|
|
}
|
2010-08-25 15:00:01 +00:00
|
|
|
return FALSE;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
METHOD(delete_payload_t, create_spi_enumerator, enumerator_t*,
|
|
|
|
private_delete_payload_t *this)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
spi_enumerator_t *e;
|
|
|
|
|
|
|
|
if (this->spi_size != sizeof(u_int32_t))
|
2006-06-09 15:12:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
return enumerator_create_empty();
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
2010-08-25 15:00:01 +00:00
|
|
|
INIT(e,
|
|
|
|
.public = {
|
|
|
|
.enumerate = (void*)_spis_enumerate,
|
|
|
|
.destroy = (void*)free,
|
|
|
|
},
|
|
|
|
.spis = this->spis,
|
|
|
|
);
|
|
|
|
return &e->public;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD2(payload_t, delete_payload_t, destroy, void,
|
|
|
|
private_delete_payload_t *this)
|
|
|
|
{
|
|
|
|
free(this->spis.ptr);
|
2009-09-04 11:46:09 +00:00
|
|
|
free(this);
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Described in header
|
|
|
|
*/
|
2011-11-17 17:14:51 +00:00
|
|
|
delete_payload_t *delete_payload_create(payload_type_t type,
|
|
|
|
protocol_id_t protocol_id)
|
2005-12-05 13:43:43 +00:00
|
|
|
{
|
2010-08-25 15:00:01 +00:00
|
|
|
private_delete_payload_t *this;
|
2005-12-05 13:43:43 +00:00
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.payload_interface = {
|
|
|
|
.verify = _verify,
|
|
|
|
.get_encoding_rules = _get_encoding_rules,
|
2011-11-17 11:27:46 +00:00
|
|
|
.get_header_length = _get_header_length,
|
2010-08-25 15:00:01 +00:00
|
|
|
.get_length = _get_length,
|
|
|
|
.get_next_type = _get_next_type,
|
|
|
|
.set_next_type = _set_next_type,
|
|
|
|
.get_type = _get_payload_type,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.get_protocol_id = _get_protocol_id,
|
|
|
|
.add_spi = _add_spi,
|
|
|
|
.create_spi_enumerator = _create_spi_enumerator,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.next_payload = NO_PAYLOAD,
|
2011-11-17 17:14:51 +00:00
|
|
|
.doi = IKEV1_DOI_IPSEC,
|
2010-08-25 15:00:01 +00:00
|
|
|
.protocol_id = protocol_id,
|
|
|
|
.spi_size = protocol_id == PROTO_AH || protocol_id == PROTO_ESP ? 4 : 0,
|
2011-11-17 17:14:51 +00:00
|
|
|
.type = type,
|
2010-08-25 15:00:01 +00:00
|
|
|
);
|
2011-11-21 09:22:50 +00:00
|
|
|
this->payload_length = get_header_length(this);
|
|
|
|
|
2010-08-25 15:00:01 +00:00
|
|
|
return &this->public;
|
2005-12-05 13:43:43 +00:00
|
|
|
}
|