Updated dnskey plugin to the new builder API
authorMartin Willi <martin@strongswan.org>
Wed, 9 Sep 2009 14:19:44 +0000 (16:19 +0200)
committerMartin Willi <martin@strongswan.org>
Thu, 10 Sep 2009 14:20:18 +0000 (16:20 +0200)
src/libstrongswan/plugins/dnskey/dnskey_builder.c
src/libstrongswan/plugins/dnskey/dnskey_builder.h
src/libstrongswan/plugins/dnskey/dnskey_plugin.c

index 792e7c1..ba20e71 100644 (file)
@@ -46,7 +46,7 @@ enum dnskey_algorithm_t {
 /**
  * Load a generic public key from a DNSKEY RR blob
  */
-static public_key_t *parse_public_key(chunk_t blob)
+static dnskey_public_key_t *parse_public_key(chunk_t blob)
 {
        dnskey_rr_t *rr = (dnskey_rr_t*)blob.ptr;
 
@@ -71,7 +71,7 @@ static public_key_t *parse_public_key(chunk_t blob)
 /**
  * Load a RSA public key from DNSKEY RR data
  */
-static public_key_t *parse_rsa_public_key(chunk_t blob)
+static dnskey_public_key_t *parse_rsa_public_key(chunk_t blob)
 {
        chunk_t n, e;
 
@@ -104,83 +104,39 @@ static public_key_t *parse_rsa_public_key(chunk_t blob)
                                                BUILD_END);
 }
 
-typedef struct private_builder_t private_builder_t;
-
-/**
- * Builder implementation for private/public key loading
- */
-struct private_builder_t {
-       /** implements the builder interface */
-       builder_t public;
-       /** dnskey packet data */
-       chunk_t blob;
-       /** type of key to build */
-       key_type_t type;
-};
-
-/**
- * Implementation of builder_t.build for public keys
- */
-static public_key_t *build_public(private_builder_t *this)
-{
-       public_key_t *key = NULL;
-
-       switch (this->type)
-       {
-               case KEY_ANY:
-                       key = parse_public_key(this->blob);
-                       break;
-               case KEY_RSA:
-                       key = parse_rsa_public_key(this->blob);
-                       break;
-               default:
-                       break;
-       }
-       free(this);
-       return key;
-}
-
 /**
- * Implementation of builder_t.add for public keys
+ * See header.
  */
-static void add_public(private_builder_t *this, builder_part_t part, ...)
+dnskey_public_key_t *dnskey_public_key_load(key_type_t type, va_list args)
 {
-       va_list args;
+       chunk_t blob = chunk_empty;
 
-       switch (part)
+       while (TRUE)
        {
-               case BUILD_BLOB_DNSKEY:
+               switch (va_arg(args, builder_part_t))
                {
-                       va_start(args, part);
-                       this->blob = va_arg(args, chunk_t);
-                       va_end(args);
-                       break;
+                       case BUILD_BLOB_DNSKEY:
+                               blob = va_arg(args, chunk_t);
+                               continue;
+                       case BUILD_END:
+                               break;
+                       default:
+                               return NULL;
                }
-               default:
-                       builder_cancel(&this->public);
-                       break;
+               break;
        }
-}
-
-/**
- * Builder construction function for public keys
- */
-builder_t *dnskey_public_key_builder(key_type_t type)
-{
-       private_builder_t *this;
-
-       if (type != KEY_ANY && type != KEY_RSA)
+       if (!blob.ptr)
        {
                return NULL;
        }
-
-       this = malloc_thing(private_builder_t);
-
-       this->blob = chunk_empty;
-       this->type = type;
-       this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add_public;
-       this->public.build = (void*(*)(builder_t *this))build_public;
-
-       return &this->public;
+       switch (type)
+       {
+               case KEY_ANY:
+                       return parse_public_key(blob);
+               case KEY_RSA:
+                       return parse_rsa_public_key(blob);
+               default:
+                       return NULL;
+       }
 }
 
index aa4a169..16eff32 100644 (file)
 #ifndef DNSKEY_BUILDER_H_
 #define DNSKEY_BUILDER_H_
 
+#include <credentials/builder.h>
 #include <credentials/keys/public_key.h>
 
+typedef struct dnskey_public_key_t dnskey_public_key_t;
+
+/**
+ * Public key implementation supporting RFC4034 decoding.
+ */
+struct dnskey_public_key_t {
+
+       /**
+        * Implements public_key_t interface.
+        */
+       public_key_t interface;
+};
+
 /**
- * Create the builder for a generic or an RSA public key.
+ * Load a public key in RFC4034 format.
+ *
+ * Takes a BUILD_BLOB_DNSKEY to parse the public key.
  *
- * @param type         type of the key, either KEY_ANY or KEY_RSA
- * @return                     builder instance
+ * @param type         type of the key, must be KEY_RSA or KEY_ANY
+ * @param args         builder_part_t argument list
+ * @return                     built key, NULL on failure
  */
-builder_t *dnskey_public_key_builder(key_type_t type);
+dnskey_public_key_t *dnskey_public_key_load(key_type_t type, va_list args);
 
 #endif /** DNSKEY_BUILDER_H_ @}*/
index d2cf6e4..83dbe31 100644 (file)
@@ -37,7 +37,7 @@ struct private_dnskey_plugin_t {
 static void destroy(private_dnskey_plugin_t *this)
 {
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)dnskey_public_key_builder);
+                                                       (builder_function_t)dnskey_public_key_load);
        free(this);
 }
 
@@ -51,9 +51,9 @@ plugin_t *plugin_create()
        this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
 
        lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
-                                                       (builder_constructor_t)dnskey_public_key_builder);
+                                                       (builder_function_t)dnskey_public_key_load);
        lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
-                                                       (builder_constructor_t)dnskey_public_key_builder);
+                                                       (builder_function_t)dnskey_public_key_load);
 
        return &this->public.plugin;
 }