notify-payload: Add methods to simplify encoding and retrieving IKE SPIs

The get_spi_data() method is currently not used, so that has been
simplified so it can be used for any protocol type and any SPI length.
Same for set_spi_data(), which is currently used for IKEv1 to encode
two SPIs.
This commit is contained in:
Tobias Brunner
2023-07-26 15:09:49 +02:00
parent e0e99c1dd3
commit 10a3c44a41
2 changed files with 57 additions and 38 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2018 Tobias Brunner * Copyright (C) 2006-2023 Tobias Brunner
* Copyright (C) 2005-2010 Martin Willi * Copyright (C) 2005-2010 Martin Willi
* Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
@@ -636,6 +636,21 @@ METHOD(notify_payload_t, set_notify_type, void,
this->notify_type = notify_type; this->notify_type = notify_type;
} }
METHOD(notify_payload_t, get_spi_data, chunk_t,
private_notify_payload_t *this)
{
return this->spi;
}
METHOD(notify_payload_t, set_spi_data, void,
private_notify_payload_t *this, chunk_t spi)
{
chunk_free(&this->spi);
this->spi = chunk_clone(spi);
this->spi_size = this->spi.len;
compute_length(this);
}
METHOD(notify_payload_t, get_spi, uint32_t, METHOD(notify_payload_t, get_spi, uint32_t,
private_notify_payload_t *this) private_notify_payload_t *this)
{ {
@@ -656,50 +671,35 @@ METHOD(notify_payload_t, get_spi, uint32_t,
METHOD(notify_payload_t, set_spi, void, METHOD(notify_payload_t, set_spi, void,
private_notify_payload_t *this, uint32_t spi) private_notify_payload_t *this, uint32_t spi)
{ {
chunk_free(&this->spi);
switch (this->protocol_id) switch (this->protocol_id)
{ {
case PROTO_AH: case PROTO_AH:
case PROTO_ESP: case PROTO_ESP:
this->spi = chunk_alloc(4); set_spi_data(this, chunk_from_thing(spi));
*((uint32_t*)this->spi.ptr) = spi;
break; break;
default: default:
break; break;
} }
this->spi_size = this->spi.len;
compute_length(this);
} }
METHOD(notify_payload_t, get_spi_data, chunk_t, METHOD(notify_payload_t, get_ike_spi, uint64_t,
private_notify_payload_t *this) private_notify_payload_t *this)
{ {
switch (this->protocol_id) if (this->protocol_id == PROTO_IKE &&
this->spi.len == 8)
{ {
case PROTO_IKE: return *((uint64_t*)this->spi.ptr);
if (this->spi.len == 16)
{
return this->spi;
}
default:
break;
} }
return chunk_empty; return 0;
} }
METHOD(notify_payload_t, set_spi_data, void, METHOD(notify_payload_t, set_ike_spi, void,
private_notify_payload_t *this, chunk_t spi) private_notify_payload_t *this, uint64_t spi)
{ {
chunk_free(&this->spi); if (this->protocol_id == PROTO_IKE)
switch (this->protocol_id)
{ {
case PROTO_IKE: set_spi_data(this, chunk_from_thing(spi));
this->spi = chunk_clone(spi);
default:
break;
} }
this->spi_size = this->spi.len;
compute_length(this);
} }
METHOD(notify_payload_t, get_notification_data, chunk_t, METHOD(notify_payload_t, get_notification_data, chunk_t,
@@ -749,6 +749,8 @@ notify_payload_t *notify_payload_create(payload_type_t type)
.set_notify_type = _set_notify_type, .set_notify_type = _set_notify_type,
.get_spi = _get_spi, .get_spi = _get_spi,
.set_spi = _set_spi, .set_spi = _set_spi,
.get_ike_spi = _get_ike_spi,
.set_ike_spi = _set_ike_spi,
.get_spi_data = _get_spi_data, .get_spi_data = _get_spi_data,
.set_spi_data = _set_spi_data, .set_spi_data = _set_spi_data,
.get_notification_data = _get_notification_data, .get_notification_data = _get_notification_data,
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2006-2018 Tobias Brunner * Copyright (C) 2006-2023 Tobias Brunner
* Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2006 Daniel Roethlisberger
* Copyright (C) 2005-2006 Martin Willi * Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter * Copyright (C) 2005 Jan Hutter
@@ -231,43 +231,60 @@ struct notify_payload_t {
void (*set_notify_type) (notify_payload_t *this, notify_type_t type); void (*set_notify_type) (notify_payload_t *this, notify_type_t type);
/** /**
* Returns the currently set spi of this payload. * Returns the currently set 32-bit SPI of this payload.
* *
* This is only valid for notifys with protocol AH|ESP * This is only valid for notify payloads with protocol AH|ESP.
* *
* @return SPI value * @return SPI value
*/ */
uint32_t (*get_spi) (notify_payload_t *this); uint32_t (*get_spi) (notify_payload_t *this);
/** /**
* Sets the spi of this payload. * Sets the 32-bit SPI of this payload.
* *
* This is only valid for notifys with protocol AH|ESP * This is only valid for notify payloads with protocol AH|ESP.
* *
* @param spi SPI value * @param spi SPI value
*/ */
void (*set_spi) (notify_payload_t *this, uint32_t spi); void (*set_spi) (notify_payload_t *this, uint32_t spi);
/** /**
* Returns the currently set spi of this payload. * Returns the currently set 64-bit SPI of this payload.
* *
* This is only valid for notifys with protocol ISAKMP * This is only valid for notify payloads with protocol IKE.
* *
* @return SPI value * @return SPI value
*/ */
chunk_t (*get_spi_data) (notify_payload_t *this); uint64_t (*get_ike_spi)(notify_payload_t *this);
/** /**
* Sets the spi of this payload. * Sets the 64-bit SPI of this payload.
* *
* This is only valid for notifys with protocol ISAKMP * This is only valid for notify payloads with protocol IKE.
* *
* @param spi SPI value * @param spi SPI value
*/ */
void (*set_spi_data) (notify_payload_t *this, chunk_t spi); void (*set_ike_spi)(notify_payload_t *this, uint64_t spi);
/** /**
* Returns the currently set notification data of payload. * Returns the data encoded as SPI in this payload.
*
* @return encoded SPI value
*/
chunk_t (*get_spi_data)(notify_payload_t *this);
/**
* Sets the data encoded as SPI in this payload.
*
* This is allowed for any protocol type, but is primarily used for ISAKMP,
* where notify payloads contain both SPIs.
*
* @param spi SPI value (cloned)
*/
void (*set_spi_data)(notify_payload_t *this, chunk_t spi);
/**
* Returns the currently set notification data of this payload.
* *
* Returned data are not copied. * Returned data are not copied.
* *