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

index fad8fe1..5147237 100644 (file)
@@ -357,149 +357,67 @@ static private_key_t *parse_private_key(chunk_t blob)
        }
 }
 
-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;
-       /** PGP 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, ...)
+public_key_t *pgp_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_PGP:
+               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_PGP:
+                               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 *pgp_public_key_builder(key_type_t type)
-{
-       private_builder_t *this;
-
-       if (type != KEY_ANY && type != KEY_RSA)
-       {
-               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;
-}
-
-/**
- * Implementation of builder_t.build for private keys
- */
-static private_key_t *build_private(private_builder_t *this)
-{
-       private_key_t *key = NULL;
-
-       switch (this->type)
+       switch (type)
        {
                case KEY_ANY:
-                       key = parse_private_key(this->blob);
-                       break;
+                       return parse_public_key(blob);
                case KEY_RSA:
-                       key = parse_rsa_private_key(this->blob);
-                       break;
+                       return parse_rsa_public_key(blob);
                default:
-                       break;
+                       return NULL;
        }
-       free(this);
-       return key;
 }
 
 /**
- * Implementation of builder_t.add for private keys
+ * See header.
  */
-static void add_private(private_builder_t *this, builder_part_t part, ...)
+private_key_t *pgp_private_key_load(key_type_t type, va_list args)
 {
-       va_list args;
+       chunk_t blob = chunk_empty;
 
-       switch (part)
+       while (TRUE)
        {
-               case BUILD_BLOB_PGP:
+               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_PGP:
+                               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 private keys
- */
-builder_t *pgp_private_key_builder(key_type_t type)
-{
-       private_builder_t *this;
-
-       if (type != KEY_ANY && type != KEY_RSA)
+       switch (type)
        {
-               return NULL;
+               case KEY_ANY:
+                       return parse_private_key(blob);
+               case KEY_RSA:
+                       return parse_rsa_private_key(blob);
+               default:
+                       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_private;
-       this->public.build = (void*(*)(builder_t *this))build_private;
-
-       return &this->public;
 }
 
index 4968d7c..938bbac 100644 (file)
 #ifndef PGP_BUILDER_H_
 #define PGP_BUILDER_H_
 
-#include <credentials/keys/public_key.h>
+#include <credentials/builder.h>
+#include <credentials/keys/private_key.h>
 
 /**
- * Create the builder for a generic or an RSA public key using PGP decoding.
+ * Load a generic or an RSA public key using PGP decoding.
  *
  * @param type         type of the key, either KEY_ANY or KEY_RSA
- * @return                     builder instance
+ * @param args         builder_part_t argument list
+ * @return                     private key, NULL if failed
  */
-builder_t *pgp_public_key_builder(key_type_t type);
+public_key_t *pgp_public_key_load(key_type_t type, va_list args);
 
 /**
- * Create the builder for a generic or RSA private key using PGP decoding.
+ * Load a generic or RSA private key using PGP decoding.
  *
  * @param type         type of the key, either KEY_ANY or KEY_RSA
+ * @param args         builder_part_t argument list
  * @return                     builder instance
  */
-builder_t *pgp_private_key_builder(key_type_t type);
+private_key_t *pgp_private_key_load(key_type_t type, va_list args);
 
 #endif /** PGP_BUILDER_H_ @}*/
index ed37e1d..198b58c 100644 (file)
@@ -38,9 +38,9 @@ struct private_pgp_plugin_t {
 static void destroy(private_pgp_plugin_t *this)
 {
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)pgp_public_key_builder);
+                                                       (builder_function_t)pgp_public_key_load);
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)pgp_private_key_builder);
+                                                       (builder_function_t)pgp_private_key_load);
 
        lib->encoding->remove_encoder(lib->encoding, pgp_encoder_encode);
 
@@ -57,13 +57,13 @@ 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)pgp_public_key_builder);
+                                                       (builder_function_t)pgp_public_key_load);
        lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
-                                                       (builder_constructor_t)pgp_public_key_builder);
+                                                       (builder_function_t)pgp_public_key_load);
        lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
-                                                       (builder_constructor_t)pgp_private_key_builder);
+                                                       (builder_function_t)pgp_private_key_load);
        lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
-                                                       (builder_constructor_t)pgp_private_key_builder);
+                                                       (builder_function_t)pgp_private_key_load);
 
        lib->encoding->add_encoder(lib->encoding, pgp_encoder_encode);