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

index 0213076..81730e5 100644 (file)
@@ -239,139 +239,59 @@ end:
                        BUILD_RSA_EXP2, exp2, BUILD_RSA_COEFF, coeff, BUILD_END);
 }
 
-typedef struct private_builder_t private_builder_t;
-
 /**
- * Builder implementation for private/public key loading
+ * See header.
  */
-struct private_builder_t {
-       /** implements the builder interface */
-       builder_t public;
-       /** asn1 der encoded 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 *pkcs1_public_key_load(key_type_t type, va_list args)
 {
-       public_key_t *key = NULL;
+       chunk_t blob = chunk_empty;
 
-       switch (this->type)
+       while (TRUE)
        {
-               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
- */
-static void add_public(private_builder_t *this, builder_part_t part, ...)
-{
-       va_list args;
-
-       switch (part)
-       {
-               case BUILD_BLOB_ASN1_DER:
+               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_ASN1_DER:
+                               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 *pkcs1_public_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_public_key(blob);
+               case KEY_RSA:
+                       return parse_rsa_public_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_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;
-
-       key = parse_rsa_private_key(this->blob);
-       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 *pkcs1_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_ASN1_DER:
+               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_ASN1_DER:
+                               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 *pkcs1_private_key_builder(key_type_t type)
-{
-       private_builder_t *this;
-
-       if (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_private;
-       this->public.build = (void*(*)(builder_t *this))build_private;
-
-       return &this->public;
+       return parse_rsa_private_key(blob);
 }
 
index fdc8025..a4a780b 100644 (file)
 #ifndef PKCS1_BUILDER_H_
 #define PKCS1_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.
+ * Load a generic or an RSA public key from PKCS#1 data.
  *
  * @param type         type of the key, either KEY_ANY or KEY_RSA
- * @return                     builder instance
+ * @param args         builder_part_t argument list
+ * @return                     public key, NULL on failure
  */
-builder_t *pkcs1_public_key_builder(key_type_t type);
+public_key_t *pkcs1_public_key_load(key_type_t type, va_list args);
 
 /**
- * Create the builder for a RSA private key.
+ * Load a RSA public key from PKCS#1 data.
  *
  * @param type         type of the key, KEY_RSA
- * @return                     builder instance
+ * @param args         builder_part_t argument list
+ * @return                     private key, NULL on failure
  */
-builder_t *pkcs1_private_key_builder(key_type_t type);
+private_key_t *pkcs1_private_key_load(key_type_t type, va_list args);
 
 #endif /** PKCS1_BUILDER_H_ @}*/
index d0ca856..9d71e13 100644 (file)
@@ -38,9 +38,9 @@ struct private_pkcs1_plugin_t {
 static void destroy(private_pkcs1_plugin_t *this)
 {
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)pkcs1_public_key_builder);
+                                                       (builder_function_t)pkcs1_public_key_load);
        lib->creds->remove_builder(lib->creds,
-                                                       (builder_constructor_t)pkcs1_private_key_builder);
+                                                       (builder_function_t)pkcs1_private_key_load);
 
        lib->encoding->remove_encoder(lib->encoding, pkcs1_encoder_encode);
 
@@ -57,11 +57,11 @@ 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)pkcs1_public_key_builder);
+                                                       (builder_function_t)pkcs1_public_key_load);
        lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY, KEY_RSA,
-                                                       (builder_constructor_t)pkcs1_public_key_builder);
+                                                       (builder_function_t)pkcs1_public_key_load);
        lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
-                                                       (builder_constructor_t)pkcs1_private_key_builder);
+                                                       (builder_function_t)pkcs1_private_key_load);
 
        lib->encoding->add_encoder(lib->encoding, pkcs1_encoder_encode);