From: Tobias Brunner Date: Thu, 16 Mar 2023 14:42:11 +0000 (+0100) Subject: notify-payload: Add methods to simplify encoding and retrieving IKE SPIs X-Git-Tag: android-2.4.2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=10a3c44a414ced78ac1e9bf61614872e890f052d;p=thirdparty%2Fstrongswan.git 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. --- diff --git a/src/libcharon/encoding/payloads/notify_payload.c b/src/libcharon/encoding/payloads/notify_payload.c index 98d0a55192..edb3c489a0 100644 --- a/src/libcharon/encoding/payloads/notify_payload.c +++ b/src/libcharon/encoding/payloads/notify_payload.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2018 Tobias Brunner + * Copyright (C) 2006-2023 Tobias Brunner * Copyright (C) 2005-2010 Martin Willi * Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2005 Jan Hutter @@ -636,6 +636,21 @@ METHOD(notify_payload_t, set_notify_type, void, 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, private_notify_payload_t *this) { @@ -656,50 +671,35 @@ METHOD(notify_payload_t, get_spi, uint32_t, METHOD(notify_payload_t, set_spi, void, private_notify_payload_t *this, uint32_t spi) { - chunk_free(&this->spi); switch (this->protocol_id) { case PROTO_AH: case PROTO_ESP: - this->spi = chunk_alloc(4); - *((uint32_t*)this->spi.ptr) = spi; + set_spi_data(this, chunk_from_thing(spi)); break; default: 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) { - switch (this->protocol_id) + if (this->protocol_id == PROTO_IKE && + this->spi.len == 8) { - case PROTO_IKE: - if (this->spi.len == 16) - { - return this->spi; - } - default: - break; + return *((uint64_t*)this->spi.ptr); } - return chunk_empty; + return 0; } -METHOD(notify_payload_t, set_spi_data, void, - private_notify_payload_t *this, chunk_t spi) +METHOD(notify_payload_t, set_ike_spi, void, + private_notify_payload_t *this, uint64_t spi) { - chunk_free(&this->spi); - switch (this->protocol_id) + if (this->protocol_id == PROTO_IKE) { - case PROTO_IKE: - this->spi = chunk_clone(spi); - default: - break; + set_spi_data(this, chunk_from_thing(spi)); } - this->spi_size = this->spi.len; - compute_length(this); } 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, .get_spi = _get_spi, .set_spi = _set_spi, + .get_ike_spi = _get_ike_spi, + .set_ike_spi = _set_ike_spi, .get_spi_data = _get_spi_data, .set_spi_data = _set_spi_data, .get_notification_data = _get_notification_data, diff --git a/src/libcharon/encoding/payloads/notify_payload.h b/src/libcharon/encoding/payloads/notify_payload.h index 226de580df..a1596bb059 100644 --- a/src/libcharon/encoding/payloads/notify_payload.h +++ b/src/libcharon/encoding/payloads/notify_payload.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2018 Tobias Brunner + * Copyright (C) 2006-2023 Tobias Brunner * Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2005-2006 Martin Willi * 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); /** - * 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 */ 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 */ 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 */ - 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 */ - 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. *