ikev2: Use a more dynamic vendor ID database, as we use with IKEv1
authorMartin Willi <martin@revosec.ch>
Fri, 17 Jan 2014 10:37:35 +0000 (11:37 +0100)
committerMartin Willi <martin@revosec.ch>
Thu, 23 Jan 2014 15:02:18 +0000 (16:02 +0100)
src/libcharon/sa/ikev2/tasks/ike_vendor.c

index 2730f58..f50030b 100644 (file)
@@ -42,24 +42,54 @@ struct private_ike_vendor_t {
 };
 
 /**
- * strongSwan specific vendor ID without version, MD5("strongSwan")
+ * Vendor ID database entry
  */
-static chunk_t strongswan_vid = chunk_from_chars(
-       0x88,0x2f,0xe5,0x6d,0x6f,0xd2,0x0d,0xbc,
-       0x22,0x51,0x61,0x3b,0x2e,0xbe,0x5b,0xeb
-);
+typedef struct {
+       /* Description */
+       char *desc;
+       /* extension flag negotiated with vendor ID, if any */
+       ike_extension_t extension;
+       /* length of vendor ID string, 0 for NULL terminated */
+       int len;
+       /* vendor ID string */
+       char *id;
+} vid_data_t;
+
+/**
+ * Get the data of a vendor ID as a chunk
+ */
+static chunk_t get_vid_data(vid_data_t *data)
+{
+       return chunk_create(data->id, data->len ?: strlen(data->id));
+}
+
+/**
+ * IKEv2 Vendor ID database entry
+ */
+static vid_data_t vids[] = {
+       /* strongSwan MD5("strongSwan") */
+       { "strongSwan", EXT_STRONGSWAN, 16,
+         "\x88\x2f\xe5\x6d\x6f\xd2\x0d\xbc\x22\x51\x61\x3b\x2e\xbe\x5b\xeb"},
+};
 
 METHOD(task_t, build, status_t,
        private_ike_vendor_t *this, message_t *message)
 {
-       if (lib->settings->get_bool(lib->settings,
-                                                               "%s.send_vendor_id", FALSE, charon->name))
-       {
-               vendor_id_payload_t *vid;
+       vendor_id_payload_t *vid;
+       bool strongswan;
+       int i;
 
-               vid = vendor_id_payload_create_data(VENDOR_ID,
-                                                                                       chunk_clone(strongswan_vid));
-               message->add_payload(message, &vid->payload_interface);
+       strongswan = lib->settings->get_bool(lib->settings,
+                                                       "%s.send_vendor_id", FALSE, charon->name);
+       for (i = 0; i < countof(vids); i++)
+       {
+               if (vids[i].extension == EXT_STRONGSWAN && strongswan)
+               {
+                       DBG2(DBG_IKE, "sending %s vendor ID", vids[i].desc);
+                       vid = vendor_id_payload_create_data(VENDOR_ID,
+                                                                               chunk_clone(get_vid_data(&vids[i])));
+                       message->add_payload(message, &vid->payload_interface);
+               }
        }
 
        return this->initiator ? NEED_MORE : SUCCESS;
@@ -70,6 +100,7 @@ METHOD(task_t, process, status_t,
 {
        enumerator_t *enumerator;
        payload_t *payload;
+       int i;
 
        enumerator = message->create_payload_enumerator(message);
        while (enumerator->enumerate(enumerator, &payload))
@@ -78,16 +109,26 @@ METHOD(task_t, process, status_t,
                {
                        vendor_id_payload_t *vid;
                        chunk_t data;
+                       bool found = FALSE;
 
                        vid = (vendor_id_payload_t*)payload;
                        data = vid->get_data(vid);
 
-                       if (chunk_equals(data, strongswan_vid))
+                       for (i = 0; i < countof(vids); i++)
                        {
-                               DBG1(DBG_IKE, "received strongSwan vendor ID");
-                               this->ike_sa->enable_extension(this->ike_sa, EXT_STRONGSWAN);
+                               if (chunk_equals(get_vid_data(&vids[i]), data))
+                               {
+                                       DBG1(DBG_IKE, "received %s vendor ID", vids[i].desc);
+                                       if (vids[i].extension)
+                                       {
+                                               this->ike_sa->enable_extension(this->ike_sa,
+                                                                                                          vids[i].extension);
+                                       }
+                                       found = TRUE;
+                                       break;
+                               }
                        }
-                       else
+                       if (!found)
                        {
                                DBG1(DBG_ENC, "received unknown vendor ID: %#B", &data);
                        }