Updated pubkey plugin to the new builder API
authorMartin Willi <martin@strongswan.org>
Wed, 9 Sep 2009 14:23:41 +0000 (16:23 +0200)
committerMartin Willi <martin@strongswan.org>
Thu, 10 Sep 2009 14:20:20 +0000 (16:20 +0200)
src/libstrongswan/plugins/pubkey/pubkey_cert.c
src/libstrongswan/plugins/pubkey/pubkey_cert.h
src/libstrongswan/plugins/pubkey/pubkey_plugin.c

index 2f1fb09..f149f63 100644 (file)
@@ -242,89 +242,44 @@ static pubkey_cert_t *pubkey_cert_create(public_key_t *key)
        return &this->public;
 }
 
        return &this->public;
 }
 
-typedef struct private_builder_t private_builder_t;
 /**
 /**
- * Builder implementation for key loading
+ * See header.
  */
  */
-struct private_builder_t {
-       /** implements the builder interface */
-       builder_t public;
-       /** loaded public key */
-       pubkey_cert_t *key;
-};
-
-/**
- * Implementation of builder_t.build
- */
-static pubkey_cert_t *build(private_builder_t *this)
+pubkey_cert_t *pubkey_cert_wrap(certificate_type_t type, va_list args)
 {
 {
-       pubkey_cert_t *key = this->key;
+       public_key_t *key = NULL;
+       chunk_t blob = chunk_empty;
 
 
-       free(this);
-       return key;
-}
-
-/**
- * Implementation of builder_t.add
- */
-static void add(private_builder_t *this, builder_part_t part, ...)
-{
-       if (!this->key)
+       while (TRUE)
        {
        {
-               public_key_t *key;
-               va_list args;
-
-               switch (part)
+               switch (va_arg(args, builder_part_t))
                {
                        case BUILD_BLOB_ASN1_DER:
                {
                        case BUILD_BLOB_ASN1_DER:
-                       {
-                               va_start(args, part);
-                               key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
-                                                                                va_arg(args, chunk_t));
-                               if (key)
-                               {
-                                       this->key = pubkey_cert_create(key);
-                               }
-                               va_end(args);
-                               return;
-                       }
+                               blob = va_arg(args, chunk_t);
+                               continue;
                        case BUILD_PUBLIC_KEY:
                        case BUILD_PUBLIC_KEY:
-                       {
-                               va_start(args, part);
                                key = va_arg(args, public_key_t*);
                                key = va_arg(args, public_key_t*);
-                               pubkey_cert_create(key->get_ref(key));
-                               va_end(args);
-                               return;
-                       }
-                       default:
+                               continue;
+                       case BUILD_END:
                                break;
                                break;
+                       default:
+                               return NULL;
                }
                }
+               break;
        }
        }
-       if (this->key)
+       if (key)
        {
        {
-               destroy((private_pubkey_cert_t*)this->key);
+               key->get_ref(key);
        }
        }
-       builder_cancel(&this->public);
-}
-
-/**
- * Builder construction function
- */
-builder_t *pubkey_cert_builder(certificate_type_t type)
-{
-       private_builder_t *this;
-
-       if (type != CERT_TRUSTED_PUBKEY)
+       else if (blob.ptr)
        {
        {
-               return NULL;
+               key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
+                                                                BUILD_BLOB_ASN1_DER, blob, BUILD_END);
        }
        }
-
-       this = malloc_thing(private_builder_t);
-
-       this->key = NULL;
-       this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add;
-       this->public.build = (void*(*)(builder_t *this))build;
-
-       return &this->public;
+       if (key)
+       {
+               return pubkey_cert_create(key);
+       }
+       return NULL;
 }
 
 }
 
index 394fc8b..a2d7353 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PUBKEY_CERT_H_
 #define PUBKEY_CERT_H_
 
 #ifndef PUBKEY_CERT_H_
 #define PUBKEY_CERT_H_
 
+#include <credentials/builder.h>
 #include <credentials/certificates/certificate.h>
 
 typedef struct pubkey_cert_t pubkey_cert_t;
 #include <credentials/certificates/certificate.h>
 
 typedef struct pubkey_cert_t pubkey_cert_t;
@@ -37,13 +38,14 @@ struct pubkey_cert_t {
 };
 
 /**
 };
 
 /**
- * Create the builder for a trusted public key.
+ * Create a trusted public key cert using a public key.
  *
  *
- * The builders add() function takes BUILD_PUBLIC_KEY to enwrap.
+ * The build accepts a BUILD_PUBLIC_KEY or a BUILD_BLOB_ASN1_DER part.
  *
  * @param type         type of the certificate, must be CERT_pubkey_cert
  *
  * @param type         type of the certificate, must be CERT_pubkey_cert
- * @return                     builder instance
+ * @param args         builder_part_t argument list
+ * @return                     pubkey_cert_t, NULL on failure
  */
  */
-builder_t *pubkey_cert_builder(certificate_type_t type);
+pubkey_cert_t *pubkey_cert_wrap(certificate_type_t type, va_list args);
 
 #endif /** PUBKEY_CERT_H_ @}*/
 
 #endif /** PUBKEY_CERT_H_ @}*/
index 2af8c9c..ad84eed 100644 (file)
@@ -37,7 +37,7 @@ struct private_pubkey_plugin_t {
 static void destroy(private_pubkey_plugin_t *this)
 {
        lib->creds->remove_builder(lib->creds,
 static void destroy(private_pubkey_plugin_t *this)
 {
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)pubkey_cert_builder);
+                                                       (builder_function_t)pubkey_cert_wrap);
        free(this);
 }
 
        free(this);
 }
 
@@ -51,7 +51,7 @@ plugin_t *plugin_create()
        this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
 
        lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
        this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
 
        lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
-                                                       (builder_constructor_t)pubkey_cert_builder);
+                                                       (builder_function_t)pubkey_cert_wrap);
 
        return &this->public.plugin;
 }
 
        return &this->public.plugin;
 }