Add keymat_t constructor registration function
authorAdrian-Ken Rueegsegger <ken@codelabs.ch>
Tue, 7 Aug 2012 12:00:28 +0000 (14:00 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 20 Aug 2012 11:02:47 +0000 (13:02 +0200)
Using the register_constructor function enables custom keymat_t
implementations per IKE version. If no constructor is registered the
default behavior is preserved.

src/libcharon/sa/keymat.c
src/libcharon/sa/keymat.h

index 7ef0b9f..26c305f 100644 (file)
 #include <sa/ikev1/keymat_v1.h>
 #include <sa/ikev2/keymat_v2.h>
 
+static keymat_constructor_t keymat_v1_ctor = NULL, keymat_v2_ctor = NULL;
+
 /**
  * See header
  */
 keymat_t *keymat_create(ike_version_t version, bool initiator)
 {
+       keymat_t *keymat = NULL;
+
        switch (version)
        {
                case IKEV1:
 #ifdef USE_IKEV1
-                       return &keymat_v1_create(initiator)->keymat;
+                       keymat = keymat_v1_ctor ? keymat_v1_ctor(initiator)
+                                                                       : &keymat_v1_create(initiator)->keymat;
 #endif
                        break;
                case IKEV2:
 #ifdef USE_IKEV2
-                       return &keymat_v2_create(initiator)->keymat;
+                       keymat = keymat_v2_ctor ? keymat_v2_ctor(initiator)
+                                                                       : &keymat_v2_create(initiator)->keymat;
 #endif
                        break;
                default:
                        break;
        }
-       return NULL;
+       return keymat;
 }
 
 /**
@@ -99,3 +105,22 @@ int keymat_get_keylen_integ(integrity_algorithm_t alg)
        }
        return 0;
 }
+
+/**
+ * See header.
+ */
+void keymat_register_constructor(ike_version_t version,
+                                                                keymat_constructor_t create)
+{
+       switch (version)
+       {
+               case IKEV1:
+                       keymat_v1_ctor = create;
+                       break;
+               case IKEV2:
+                       keymat_v2_ctor = create;
+                       break;
+               default:
+                       break;
+       }
+}
index a64be23..02db5ca 100644 (file)
@@ -32,6 +32,14 @@ typedef struct keymat_t keymat_t;
 #include <sa/ike_sa_id.h>
 
 /**
+ * Constructor function for custom keymat implementations
+ *
+ * @param initiator            TRUE if the keymat is used as initiator
+ * @return                             keymat_t implementation
+ */
+typedef keymat_t* (*keymat_constructor_t)(bool initiator);
+
+/**
  * Derivation an management of sensitive keying material.
  */
 struct keymat_t {
@@ -110,4 +118,13 @@ int keymat_get_keylen_encr(encryption_algorithm_t alg);
  */
 int keymat_get_keylen_integ(integrity_algorithm_t alg);
 
+/**
+ * Register keymat_t constructor for given IKE version.
+ *
+ * @param version                      IKE version of given keymat constructor
+ * @param create                       keymat constructor function, NULL to unregister
+ */
+void keymat_register_constructor(ike_version_t version,
+                                                                keymat_constructor_t create);
+
 #endif /** KEYMAT_H_ @}*/