]> 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, 26 Jul 2023 13:09:49 +0000 (15:09 +0200)
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.

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

index 98d0a55192f3eb076b8c1a7bae2a24a8d7ea67ff..edb3c489a05052ccd2fce875b0ef4416fe486a6d 100644 (file)
@@ -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,
index 226de580df365b5a81012e1802e35c546533009d..a1596bb0593d6b8e84f751105ed240ef62fdc6d9 100644 (file)
@@ -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.
         *