]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
notify-payload: Add methods to simplify encoding and retrieving IKE SPIs
authorTobias Brunner <tobias@strongswan.org>
Thu, 16 Mar 2023 14:42:11 +0000 (15:42 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 22 Mar 2023 10:37:52 +0000 (11:37 +0100)
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.

src/libcharon/encoding/payloads/notify_payload.c
src/libcharon/encoding/payloads/notify_payload.h

index 7d4cad7dd25c14c542b686acb8ce9db484e34af6..2b2c6e93071d5cd15079b36460c95ed35da848f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2019 Tobias Brunner
+ * Copyright (C) 2006-2023 Tobias Brunner
  * Copyright (C) 2005-2010 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
@@ -646,6 +646,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)
 {
@@ -666,50 +681,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,
@@ -759,6 +759,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,
index 9882c00637dcd71efc76ea6a2a80e45688f98a4b..eb0784b3edb6cf942d1d6286d58e72b7ff698759 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2019 Tobias Brunner
+ * Copyright (C) 2006-2023 Tobias Brunner
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005-2006 Martin Willi
  * Copyright (C) 2005 Jan Hutter
@@ -244,43 +244,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.
         *